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