diff options
Diffstat (limited to 'cmd/geth')
-rw-r--r-- | cmd/geth/admin.go | 33 | ||||
-rw-r--r-- | cmd/geth/js_test.go | 4 | ||||
-rw-r--r-- | cmd/geth/main.go | 95 |
3 files changed, 125 insertions, 7 deletions
diff --git a/cmd/geth/admin.go b/cmd/geth/admin.go index 5f1cb8c96..f8c717187 100644 --- a/cmd/geth/admin.go +++ b/cmd/geth/admin.go @@ -32,7 +32,6 @@ func (js *jsre) adminBindings() { admin.Set("unlock", js.unlock) admin.Set("import", js.importChain) admin.Set("export", js.exportChain) - admin.Set("dumpBlock", js.dumpBlock) admin.Set("verbosity", js.verbosity) admin.Set("backtrace", js.backtrace) @@ -43,6 +42,12 @@ func (js *jsre) adminBindings() { miner.Set("stop", js.stopMining) miner.Set("hashrate", js.hashrate) miner.Set("setExtra", js.setExtra) + + admin.Set("debug", struct{}{}) + t, _ = admin.Get("debug") + debug := t.Object() + debug.Set("printBlock", js.printBlock) + debug.Set("dumpBlock", js.dumpBlock) } func (js *jsre) setExtra(call otto.FunctionCall) otto.Value { @@ -284,6 +289,32 @@ func (js *jsre) exportChain(call otto.FunctionCall) otto.Value { return otto.TrueValue() } +func (js *jsre) printBlock(call otto.FunctionCall) otto.Value { + var block *types.Block + if len(call.ArgumentList) > 0 { + if call.Argument(0).IsNumber() { + num, _ := call.Argument(0).ToInteger() + block = js.ethereum.ChainManager().GetBlockByNumber(uint64(num)) + } else if call.Argument(0).IsString() { + hash, _ := call.Argument(0).ToString() + block = js.ethereum.ChainManager().GetBlock(common.HexToHash(hash)) + } else { + fmt.Println("invalid argument for dump. Either hex string or number") + } + + } else { + block = js.ethereum.ChainManager().CurrentBlock() + } + if block == nil { + fmt.Println("block not found") + return otto.UndefinedValue() + } + + fmt.Println(block) + + return otto.UndefinedValue() +} + func (js *jsre) dumpBlock(call otto.FunctionCall) otto.Value { var block *types.Block if len(call.ArgumentList) > 0 { diff --git a/cmd/geth/js_test.go b/cmd/geth/js_test.go index 5b962f621..4421b1d68 100644 --- a/cmd/geth/js_test.go +++ b/cmd/geth/js_test.go @@ -175,7 +175,7 @@ func TestBlockChain(t *testing.T) { defer ethereum.Stop() // should get current block - val0, err := repl.re.Run("admin.dumpBlock()") + val0, err := repl.re.Run("admin.debug.dumpBlock()") if err != nil { t.Errorf("expected no error, got %v", err) } @@ -197,7 +197,7 @@ func TestBlockChain(t *testing.T) { var val1 otto.Value // should get current block - val1, err = repl.re.Run("admin.dumpBlock()") + val1, err = repl.re.Run("admin.debug.dumpBlock()") if err != nil { t.Errorf("expected no error, got %v", err) } diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 9e5ae5857..e18b92a2e 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -36,11 +36,13 @@ import ( "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/logger" "github.com/peterh/liner" + "path" ) import _ "net/http/pprof" @@ -208,12 +210,18 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso Name: "export", Usage: `export blockchain into file`, }, + { + Action: upgradeDb, + Name: "upgradedb", + Usage: "upgrade chainblock database", + }, } app.Flags = []cli.Flag{ utils.UnlockedAccountFlag, utils.PasswordFileFlag, utils.BootnodesFlag, utils.DataDirFlag, + utils.BlockchainVersionFlag, utils.JSpathFlag, utils.ListenPortFlag, utils.MaxPeersFlag, @@ -437,13 +445,29 @@ func importchain(ctx *cli.Context) { if len(ctx.Args()) != 1 { utils.Fatalf("This command requires an argument.") } - chainmgr, _, _ := utils.GetChain(ctx) + + cfg := utils.MakeEthConfig(ClientIdentifier, Version, ctx) + cfg.SkipBcVersionCheck = true + + ethereum, err := eth.New(cfg) + if err != nil { + utils.Fatalf("%v\n", err) + } + + chainmgr := ethereum.ChainManager() start := time.Now() - err := utils.ImportChain(chainmgr, ctx.Args().First()) + err = utils.ImportChain(chainmgr, ctx.Args().First()) if err != nil { utils.Fatalf("Import error: %v\n", err) } + + // force database flush + ethereum.BlockDb().Close() + ethereum.StateDb().Close() + ethereum.ExtraDb().Close() + fmt.Printf("Import done in %v", time.Since(start)) + return } @@ -451,9 +475,18 @@ func exportchain(ctx *cli.Context) { if len(ctx.Args()) != 1 { utils.Fatalf("This command requires an argument.") } - chainmgr, _, _ := utils.GetChain(ctx) + + cfg := utils.MakeEthConfig(ClientIdentifier, Version, ctx) + cfg.SkipBcVersionCheck = true + + ethereum, err := eth.New(cfg) + if err != nil { + utils.Fatalf("%v\n", err) + } + + chainmgr := ethereum.ChainManager() start := time.Now() - err := utils.ExportChain(chainmgr, ctx.Args().First()) + err = utils.ExportChain(chainmgr, ctx.Args().First()) if err != nil { utils.Fatalf("Export error: %v\n", err) } @@ -461,6 +494,60 @@ func exportchain(ctx *cli.Context) { return } +func upgradeDb(ctx *cli.Context) { + fmt.Println("Upgrade blockchain DB") + + cfg := utils.MakeEthConfig(ClientIdentifier, Version, ctx) + cfg.SkipBcVersionCheck = true + + ethereum, err := eth.New(cfg) + if err != nil { + utils.Fatalf("%v\n", err) + } + + v, _ := ethereum.BlockDb().Get([]byte("BlockchainVersion")) + bcVersion := int(common.NewValue(v).Uint()) + + if bcVersion == 0 { + bcVersion = core.BlockChainVersion + } + + filename := fmt.Sprintf("blockchain_%d_%s.chain", bcVersion, time.Now().Format("2006-01-02_15:04:05")) + exportFile := path.Join(ctx.GlobalString(utils.DataDirFlag.Name), filename) + + err = utils.ExportChain(ethereum.ChainManager(), exportFile) + if err != nil { + utils.Fatalf("Unable to export chain for reimport %s\n", err) + } + + ethereum.BlockDb().Close() + ethereum.StateDb().Close() + ethereum.ExtraDb().Close() + + os.RemoveAll(path.Join(ctx.GlobalString(utils.DataDirFlag.Name), "blockchain")) + + ethereum, err = eth.New(cfg) + if err != nil { + utils.Fatalf("%v\n", err) + } + + ethereum.BlockDb().Put([]byte("BlockchainVersion"), common.NewValue(core.BlockChainVersion).Bytes()) + + err = utils.ImportChain(ethereum.ChainManager(), exportFile) + if err != nil { + utils.Fatalf("Import error %v (a backup is made in %s, use the import command to import it)\n", err, exportFile) + } + + // force database flush + ethereum.BlockDb().Close() + ethereum.StateDb().Close() + ethereum.ExtraDb().Close() + + os.Remove(exportFile) + + fmt.Println("Import finished") +} + func dump(ctx *cli.Context) { chainmgr, _, stateDb := utils.GetChain(ctx) for _, arg := range ctx.Args() { |