chaincmd.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "fmt"
  19. "os"
  20. "path/filepath"
  21. "strconv"
  22. "time"
  23. "github.com/codegangsta/cli"
  24. "github.com/ethereum/go-ethereum/cmd/utils"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/core"
  27. "github.com/ethereum/go-ethereum/core/state"
  28. "github.com/ethereum/go-ethereum/core/types"
  29. "github.com/ethereum/go-ethereum/ethdb"
  30. "github.com/ethereum/go-ethereum/logger/glog"
  31. )
  32. var (
  33. importCommand = cli.Command{
  34. Action: importChain,
  35. Name: "import",
  36. Usage: `import a blockchain file`,
  37. }
  38. exportCommand = cli.Command{
  39. Action: exportChain,
  40. Name: "export",
  41. Usage: `export blockchain into file`,
  42. Description: `
  43. Requires a first argument of the file to write to.
  44. Optional second and third arguments control the first and
  45. last block to write. In this mode, the file will be appended
  46. if already existing.
  47. `,
  48. }
  49. upgradedbCommand = cli.Command{
  50. Action: upgradeDB,
  51. Name: "upgradedb",
  52. Usage: "upgrade chainblock database",
  53. }
  54. removedbCommand = cli.Command{
  55. Action: removeDB,
  56. Name: "removedb",
  57. Usage: "Remove blockchain and state databases",
  58. }
  59. dumpCommand = cli.Command{
  60. Action: dump,
  61. Name: "dump",
  62. Usage: `dump a specific block from storage`,
  63. Description: `
  64. The arguments are interpreted as block numbers or hashes.
  65. Use "ethereum dump 0" to dump the genesis block.
  66. `,
  67. }
  68. )
  69. func importChain(ctx *cli.Context) {
  70. if len(ctx.Args()) != 1 {
  71. utils.Fatalf("This command requires an argument.")
  72. }
  73. chain, chainDb := utils.MakeChain(ctx)
  74. start := time.Now()
  75. err := utils.ImportChain(chain, ctx.Args().First())
  76. chainDb.Close()
  77. if err != nil {
  78. utils.Fatalf("Import error: %v", err)
  79. }
  80. fmt.Printf("Import done in %v", time.Since(start))
  81. }
  82. func exportChain(ctx *cli.Context) {
  83. if len(ctx.Args()) < 1 {
  84. utils.Fatalf("This command requires an argument.")
  85. }
  86. chain, _ := utils.MakeChain(ctx)
  87. start := time.Now()
  88. var err error
  89. fp := ctx.Args().First()
  90. if len(ctx.Args()) < 3 {
  91. err = utils.ExportChain(chain, fp)
  92. } else {
  93. // This can be improved to allow for numbers larger than 9223372036854775807
  94. first, ferr := strconv.ParseInt(ctx.Args().Get(1), 10, 64)
  95. last, lerr := strconv.ParseInt(ctx.Args().Get(2), 10, 64)
  96. if ferr != nil || lerr != nil {
  97. utils.Fatalf("Export error in parsing parameters: block number not an integer\n")
  98. }
  99. if first < 0 || last < 0 {
  100. utils.Fatalf("Export error: block number must be greater than 0\n")
  101. }
  102. err = utils.ExportAppendChain(chain, fp, uint64(first), uint64(last))
  103. }
  104. if err != nil {
  105. utils.Fatalf("Export error: %v\n", err)
  106. }
  107. fmt.Printf("Export done in %v", time.Since(start))
  108. }
  109. func removeDB(ctx *cli.Context) {
  110. confirm, err := utils.PromptConfirm("Remove local database?")
  111. if err != nil {
  112. utils.Fatalf("%v", err)
  113. }
  114. if confirm {
  115. fmt.Println("Removing chaindata...")
  116. start := time.Now()
  117. os.RemoveAll(filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), "chaindata"))
  118. fmt.Printf("Removed in %v\n", time.Since(start))
  119. } else {
  120. fmt.Println("Operation aborted")
  121. }
  122. }
  123. func upgradeDB(ctx *cli.Context) {
  124. glog.Infoln("Upgrading blockchain database")
  125. chain, chainDb := utils.MakeChain(ctx)
  126. v, _ := chainDb.Get([]byte("BlockchainVersion"))
  127. bcVersion := int(common.NewValue(v).Uint())
  128. if bcVersion == 0 {
  129. bcVersion = core.BlockChainVersion
  130. }
  131. // Export the current chain.
  132. filename := fmt.Sprintf("blockchain_%d_%s.chain", bcVersion, time.Now().Format("20060102_150405"))
  133. exportFile := filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), filename)
  134. if err := utils.ExportChain(chain, exportFile); err != nil {
  135. utils.Fatalf("Unable to export chain for reimport %s", err)
  136. }
  137. chainDb.Close()
  138. os.RemoveAll(filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), "chaindata"))
  139. // Import the chain file.
  140. chain, chainDb = utils.MakeChain(ctx)
  141. chainDb.Put([]byte("BlockchainVersion"), common.NewValue(core.BlockChainVersion).Bytes())
  142. err := utils.ImportChain(chain, exportFile)
  143. chainDb.Close()
  144. if err != nil {
  145. utils.Fatalf("Import error %v (a backup is made in %s, use the import command to import it)", err, exportFile)
  146. } else {
  147. os.Remove(exportFile)
  148. glog.Infoln("Import finished")
  149. }
  150. }
  151. func dump(ctx *cli.Context) {
  152. chain, chainDb := utils.MakeChain(ctx)
  153. for _, arg := range ctx.Args() {
  154. var block *types.Block
  155. if hashish(arg) {
  156. block = chain.GetBlock(common.HexToHash(arg))
  157. } else {
  158. num, _ := strconv.Atoi(arg)
  159. block = chain.GetBlockByNumber(uint64(num))
  160. }
  161. if block == nil {
  162. fmt.Println("{}")
  163. utils.Fatalf("block not found")
  164. } else {
  165. state, err := state.New(block.Root(), chainDb)
  166. if err != nil {
  167. utils.Fatalf("could not create new state: %v", err)
  168. return
  169. }
  170. fmt.Printf("%s\n", state.Dump())
  171. }
  172. }
  173. chainDb.Close()
  174. }
  175. // hashish returns true for strings that look like hashes.
  176. func hashish(x string) bool {
  177. _, err := strconv.Atoi(x)
  178. return err != nil
  179. }
  180. func closeAll(dbs ...ethdb.Database) {
  181. for _, db := range dbs {
  182. db.Close()
  183. }
  184. }