chaincmd.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "strconv"
  7. "time"
  8. "github.com/codegangsta/cli"
  9. "github.com/ethereum/go-ethereum/cmd/utils"
  10. "github.com/ethereum/go-ethereum/common"
  11. "github.com/ethereum/go-ethereum/core"
  12. "github.com/ethereum/go-ethereum/core/state"
  13. "github.com/ethereum/go-ethereum/core/types"
  14. "github.com/ethereum/go-ethereum/logger/glog"
  15. )
  16. var (
  17. importCommand = cli.Command{
  18. Action: importChain,
  19. Name: "import",
  20. Usage: `import a blockchain file`,
  21. }
  22. exportCommand = cli.Command{
  23. Action: exportChain,
  24. Name: "export",
  25. Usage: `export blockchain into file`,
  26. Description: `
  27. Requires a first argument of the file to write to.
  28. Optional second and third arguments control the first and
  29. last block to write. In this mode, the file will be appended
  30. if already existing.
  31. `,
  32. }
  33. upgradedbCommand = cli.Command{
  34. Action: upgradeDB,
  35. Name: "upgradedb",
  36. Usage: "upgrade chainblock database",
  37. }
  38. removedbCommand = cli.Command{
  39. Action: removeDB,
  40. Name: "removedb",
  41. Usage: "Remove blockchain and state databases",
  42. }
  43. dumpCommand = cli.Command{
  44. Action: dump,
  45. Name: "dump",
  46. Usage: `dump a specific block from storage`,
  47. Description: `
  48. The arguments are interpreted as block numbers or hashes.
  49. Use "ethereum dump 0" to dump the genesis block.
  50. `,
  51. }
  52. )
  53. func importChain(ctx *cli.Context) {
  54. if len(ctx.Args()) != 1 {
  55. utils.Fatalf("This command requires an argument.")
  56. }
  57. chain, blockDB, stateDB, extraDB := utils.MakeChain(ctx)
  58. start := time.Now()
  59. err := utils.ImportChain(chain, ctx.Args().First())
  60. closeAll(blockDB, stateDB, extraDB)
  61. if err != nil {
  62. utils.Fatalf("Import error: %v", err)
  63. }
  64. fmt.Printf("Import done in %v", time.Since(start))
  65. }
  66. func exportChain(ctx *cli.Context) {
  67. if len(ctx.Args()) != 1 {
  68. utils.Fatalf("This command requires an argument.")
  69. }
  70. chain, _, _, _ := utils.MakeChain(ctx)
  71. start := time.Now()
  72. var err error
  73. if len(ctx.Args()) < 3 {
  74. err = utils.ExportChain(chain, ctx.Args().First())
  75. } else {
  76. // This can be improved to allow for numbers larger than 9223372036854775807
  77. first, ferr := strconv.ParseInt(ctx.Args().Get(1), 10, 64)
  78. last, lerr := strconv.ParseInt(ctx.Args().Get(2), 10, 64)
  79. if ferr != nil || lerr != nil {
  80. utils.Fatalf("Export error in parsing parameters\n")
  81. }
  82. err = utils.ExportAppendChain(chain, ctx.Args().First(), uint64(first), uint64(last))
  83. }
  84. if err != nil {
  85. utils.Fatalf("Export error: %v\n", err)
  86. }
  87. fmt.Printf("Export done in %v", time.Since(start))
  88. }
  89. func removeDB(ctx *cli.Context) {
  90. confirm, err := utils.PromptConfirm("Remove local databases?")
  91. if err != nil {
  92. utils.Fatalf("%v", err)
  93. }
  94. if confirm {
  95. fmt.Println("Removing chain and state databases...")
  96. start := time.Now()
  97. os.RemoveAll(filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), "blockchain"))
  98. os.RemoveAll(filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), "state"))
  99. fmt.Printf("Removed in %v\n", time.Since(start))
  100. } else {
  101. fmt.Println("Operation aborted")
  102. }
  103. }
  104. func upgradeDB(ctx *cli.Context) {
  105. glog.Infoln("Upgrading blockchain database")
  106. chain, blockDB, stateDB, extraDB := utils.MakeChain(ctx)
  107. v, _ := blockDB.Get([]byte("BlockchainVersion"))
  108. bcVersion := int(common.NewValue(v).Uint())
  109. if bcVersion == 0 {
  110. bcVersion = core.BlockChainVersion
  111. }
  112. // Export the current chain.
  113. filename := fmt.Sprintf("blockchain_%d_%s.chain", bcVersion, time.Now().Format("20060102_150405"))
  114. exportFile := filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), filename)
  115. if err := utils.ExportChain(chain, exportFile); err != nil {
  116. utils.Fatalf("Unable to export chain for reimport %s", err)
  117. }
  118. closeAll(blockDB, stateDB, extraDB)
  119. os.RemoveAll(filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), "blockchain"))
  120. os.RemoveAll(filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), "state"))
  121. // Import the chain file.
  122. chain, blockDB, stateDB, extraDB = utils.MakeChain(ctx)
  123. blockDB.Put([]byte("BlockchainVersion"), common.NewValue(core.BlockChainVersion).Bytes())
  124. err := utils.ImportChain(chain, exportFile)
  125. closeAll(blockDB, stateDB, extraDB)
  126. if err != nil {
  127. utils.Fatalf("Import error %v (a backup is made in %s, use the import command to import it)", err, exportFile)
  128. } else {
  129. os.Remove(exportFile)
  130. glog.Infoln("Import finished")
  131. }
  132. }
  133. func dump(ctx *cli.Context) {
  134. chain, _, stateDB, _ := utils.MakeChain(ctx)
  135. for _, arg := range ctx.Args() {
  136. var block *types.Block
  137. if hashish(arg) {
  138. block = chain.GetBlock(common.HexToHash(arg))
  139. } else {
  140. num, _ := strconv.Atoi(arg)
  141. block = chain.GetBlockByNumber(uint64(num))
  142. }
  143. if block == nil {
  144. fmt.Println("{}")
  145. utils.Fatalf("block not found")
  146. } else {
  147. state := state.New(block.Root(), stateDB)
  148. fmt.Printf("%s\n", state.Dump())
  149. }
  150. }
  151. }
  152. // hashish returns true for strings that look like hashes.
  153. func hashish(x string) bool {
  154. _, err := strconv.Atoi(x)
  155. return err != nil
  156. }
  157. func closeAll(dbs ...common.Database) {
  158. for _, db := range dbs {
  159. db.Close()
  160. }
  161. }