From 651030c98d0173db272aaee814c99f0a664d992b Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Wed, 27 May 2015 13:43:49 +0200 Subject: cmd/geth: move blockchain commands to chaincmd.go --- cmd/geth/blocktest.go | 120 ----------------------------------- cmd/geth/blocktestcmd.go | 120 +++++++++++++++++++++++++++++++++++ cmd/geth/chaincmd.go | 159 +++++++++++++++++++++++++++++++++++++++++++++++ cmd/geth/main.go | 153 +++------------------------------------------ 4 files changed, 286 insertions(+), 266 deletions(-) delete mode 100644 cmd/geth/blocktest.go create mode 100644 cmd/geth/blocktestcmd.go create mode 100644 cmd/geth/chaincmd.go (limited to 'cmd') diff --git a/cmd/geth/blocktest.go b/cmd/geth/blocktest.go deleted file mode 100644 index 81a64b5f2..000000000 --- a/cmd/geth/blocktest.go +++ /dev/null @@ -1,120 +0,0 @@ -package main - -import ( - "fmt" - "os" - - "github.com/codegangsta/cli" - "github.com/ethereum/go-ethereum/cmd/utils" - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/eth" - "github.com/ethereum/go-ethereum/ethdb" - "github.com/ethereum/go-ethereum/tests" -) - -var blocktestCmd = cli.Command{ - Action: runBlockTest, - Name: "blocktest", - Usage: `loads a block test file`, - Description: ` -The first argument should be a block test file. -The second argument is the name of a block test from the file. - -The block test will be loaded into an in-memory database. -If loading succeeds, the RPC server is started. Clients will -be able to interact with the chain defined by the test. -`, -} - -func runBlockTest(ctx *cli.Context) { - var ( - file, testname string - rpc bool - ) - args := ctx.Args() - switch { - case len(args) == 1: - file = args[0] - case len(args) == 2: - file, testname = args[0], args[1] - case len(args) == 3: - file, testname = args[0], args[1] - rpc = true - default: - utils.Fatalf(`Usage: ethereum blocktest [ [ "rpc" ] ]`) - } - bt, err := tests.LoadBlockTests(file) - if err != nil { - utils.Fatalf("%v", err) - } - - // run all tests if no test name is specified - if testname == "" { - ecode := 0 - for name, test := range bt { - fmt.Printf("----------------- Running Block Test %q\n", name) - ethereum, err := runOneBlockTest(ctx, test) - if err != nil { - fmt.Println(err) - fmt.Println("FAIL") - ecode = 1 - } - if ethereum != nil { - ethereum.Stop() - ethereum.WaitForShutdown() - } - } - os.Exit(ecode) - return - } - // otherwise, run the given test - test, ok := bt[testname] - if !ok { - utils.Fatalf("Test file does not contain test named %q", testname) - } - ethereum, err := runOneBlockTest(ctx, test) - if err != nil { - utils.Fatalf("%v", err) - } - defer ethereum.Stop() - if rpc { - fmt.Println("Block Test post state validated, starting RPC interface.") - startEth(ctx, ethereum) - utils.StartRPC(ethereum, ctx) - ethereum.WaitForShutdown() - } -} - -func runOneBlockTest(ctx *cli.Context, test *tests.BlockTest) (*eth.Ethereum, error) { - cfg := utils.MakeEthConfig(ClientIdentifier, Version, ctx) - cfg.NewDB = func(path string) (common.Database, error) { return ethdb.NewMemDatabase() } - cfg.MaxPeers = 0 // disable network - cfg.Shh = false // disable whisper - cfg.NAT = nil // disable port mapping - - ethereum, err := eth.New(cfg) - if err != nil { - return nil, err - } - if err := ethereum.Start(); err != nil { - return nil, err - } - - // import the genesis block - ethereum.ResetWithGenesisBlock(test.Genesis) - - // import pre accounts - statedb, err := test.InsertPreState(ethereum) - if err != nil { - return ethereum, fmt.Errorf("InsertPreState: %v", err) - } - - if err := test.TryBlocksInsert(ethereum.ChainManager()); err != nil { - return ethereum, fmt.Errorf("Block Test load error: %v", err) - } - - if err := test.ValidatePostState(statedb); err != nil { - return ethereum, fmt.Errorf("post state validation failed: %v", err) - } - return ethereum, nil -} diff --git a/cmd/geth/blocktestcmd.go b/cmd/geth/blocktestcmd.go new file mode 100644 index 000000000..f4dcb0286 --- /dev/null +++ b/cmd/geth/blocktestcmd.go @@ -0,0 +1,120 @@ +package main + +import ( + "fmt" + "os" + + "github.com/codegangsta/cli" + "github.com/ethereum/go-ethereum/cmd/utils" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/eth" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/tests" +) + +var blocktestCommand = cli.Command{ + Action: runBlockTest, + Name: "blocktest", + Usage: `loads a block test file`, + Description: ` +The first argument should be a block test file. +The second argument is the name of a block test from the file. + +The block test will be loaded into an in-memory database. +If loading succeeds, the RPC server is started. Clients will +be able to interact with the chain defined by the test. +`, +} + +func runBlockTest(ctx *cli.Context) { + var ( + file, testname string + rpc bool + ) + args := ctx.Args() + switch { + case len(args) == 1: + file = args[0] + case len(args) == 2: + file, testname = args[0], args[1] + case len(args) == 3: + file, testname = args[0], args[1] + rpc = true + default: + utils.Fatalf(`Usage: ethereum blocktest [ [ "rpc" ] ]`) + } + bt, err := tests.LoadBlockTests(file) + if err != nil { + utils.Fatalf("%v", err) + } + + // run all tests if no test name is specified + if testname == "" { + ecode := 0 + for name, test := range bt { + fmt.Printf("----------------- Running Block Test %q\n", name) + ethereum, err := runOneBlockTest(ctx, test) + if err != nil { + fmt.Println(err) + fmt.Println("FAIL") + ecode = 1 + } + if ethereum != nil { + ethereum.Stop() + ethereum.WaitForShutdown() + } + } + os.Exit(ecode) + return + } + // otherwise, run the given test + test, ok := bt[testname] + if !ok { + utils.Fatalf("Test file does not contain test named %q", testname) + } + ethereum, err := runOneBlockTest(ctx, test) + if err != nil { + utils.Fatalf("%v", err) + } + defer ethereum.Stop() + if rpc { + fmt.Println("Block Test post state validated, starting RPC interface.") + startEth(ctx, ethereum) + utils.StartRPC(ethereum, ctx) + ethereum.WaitForShutdown() + } +} + +func runOneBlockTest(ctx *cli.Context, test *tests.BlockTest) (*eth.Ethereum, error) { + cfg := utils.MakeEthConfig(ClientIdentifier, Version, ctx) + cfg.NewDB = func(path string) (common.Database, error) { return ethdb.NewMemDatabase() } + cfg.MaxPeers = 0 // disable network + cfg.Shh = false // disable whisper + cfg.NAT = nil // disable port mapping + + ethereum, err := eth.New(cfg) + if err != nil { + return nil, err + } + if err := ethereum.Start(); err != nil { + return nil, err + } + + // import the genesis block + ethereum.ResetWithGenesisBlock(test.Genesis) + + // import pre accounts + statedb, err := test.InsertPreState(ethereum) + if err != nil { + return ethereum, fmt.Errorf("InsertPreState: %v", err) + } + + if err := test.TryBlocksInsert(ethereum.ChainManager()); err != nil { + return ethereum, fmt.Errorf("Block Test load error: %v", err) + } + + if err := test.ValidatePostState(statedb); err != nil { + return ethereum, fmt.Errorf("post state validation failed: %v", err) + } + return ethereum, nil +} diff --git a/cmd/geth/chaincmd.go b/cmd/geth/chaincmd.go new file mode 100644 index 000000000..e17d1dc9b --- /dev/null +++ b/cmd/geth/chaincmd.go @@ -0,0 +1,159 @@ +package main + +import ( + "fmt" + "os" + "path/filepath" + "strconv" + "time" + + "github.com/codegangsta/cli" + "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/logger/glog" +) + +var ( + importCommand = cli.Command{ + Action: importChain, + Name: "import", + Usage: `import a blockchain file`, + } + exportCommand = cli.Command{ + Action: exportChain, + Name: "export", + Usage: `export blockchain into file`, + } + upgradedbCommand = cli.Command{ + Action: upgradeDB, + Name: "upgradedb", + Usage: "upgrade chainblock database", + } + removedbCommand = cli.Command{ + Action: removeDB, + Name: "removedb", + Usage: "Remove blockchain and state databases", + } + dumpCommand = cli.Command{ + Action: dump, + Name: "dump", + Usage: `dump a specific block from storage`, + Description: ` +The arguments are interpreted as block numbers or hashes. +Use "ethereum dump 0" to dump the genesis block. +`, + } +) + +func importChain(ctx *cli.Context) { + if len(ctx.Args()) != 1 { + utils.Fatalf("This command requires an argument.") + } + chain, blockDB, stateDB, extraDB := utils.GetChain(ctx) + start := time.Now() + if err := utils.ImportChain(chain, ctx.Args().First()); err != nil { + utils.Fatalf("Import error: %v\n", err) + } + flushAll(blockDB, stateDB, extraDB) + fmt.Printf("Import done in %v", time.Since(start)) +} + +func exportChain(ctx *cli.Context) { + if len(ctx.Args()) != 1 { + utils.Fatalf("This command requires an argument.") + } + chain, _, _, _ := utils.GetChain(ctx) + start := time.Now() + if err := utils.ExportChain(chain, ctx.Args().First()); err != nil { + utils.Fatalf("Export error: %v\n", err) + } + fmt.Printf("Export done in %v", time.Since(start)) +} + +func removeDB(ctx *cli.Context) { + confirm, err := utils.PromptConfirm("Remove local databases?") + if err != nil { + utils.Fatalf("%v", err) + } + + if confirm { + fmt.Println("Removing chain and state databases...") + start := time.Now() + + os.RemoveAll(filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), "blockchain")) + os.RemoveAll(filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), "state")) + + fmt.Printf("Removed in %v\n", time.Since(start)) + } else { + fmt.Println("Operation aborted") + } +} + +func upgradeDB(ctx *cli.Context) { + glog.Infoln("Upgrading blockchain database") + + chain, blockDB, stateDB, extraDB := utils.GetChain(ctx) + v, _ := blockDB.Get([]byte("BlockchainVersion")) + bcVersion := int(common.NewValue(v).Uint()) + if bcVersion == 0 { + bcVersion = core.BlockChainVersion + } + + // Export the current chain. + filename := fmt.Sprintf("blockchain_%d_%s.chain", bcVersion, time.Now().Format("20060102_150405")) + exportFile := filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), filename) + if err := utils.ExportChain(chain, exportFile); err != nil { + utils.Fatalf("Unable to export chain for reimport %s\n", err) + } + flushAll(blockDB, stateDB, extraDB) + os.RemoveAll(filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), "blockchain")) + os.RemoveAll(filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), "state")) + + // Import the chain file. + chain, blockDB, stateDB, extraDB = utils.GetChain(ctx) + blockDB.Put([]byte("BlockchainVersion"), common.NewValue(core.BlockChainVersion).Bytes()) + err := utils.ImportChain(chain, exportFile) + flushAll(blockDB, stateDB, extraDB) + if err != nil { + utils.Fatalf("Import error %v (a backup is made in %s, use the import command to import it)\n", err, exportFile) + } else { + os.Remove(exportFile) + glog.Infoln("Import finished") + } +} + +func dump(ctx *cli.Context) { + chain, _, stateDB, _ := utils.GetChain(ctx) + for _, arg := range ctx.Args() { + var block *types.Block + if hashish(arg) { + block = chain.GetBlock(common.HexToHash(arg)) + } else { + num, _ := strconv.Atoi(arg) + block = chain.GetBlockByNumber(uint64(num)) + } + if block == nil { + fmt.Println("{}") + utils.Fatalf("block not found") + } else { + state := state.New(block.Root(), stateDB) + fmt.Printf("%s\n", state.Dump()) + } + } +} + +// hashish returns true for strings that look like hashes. +func hashish(x string) bool { + _, err := strconv.Atoi(x) + return err != nil +} + +func flushAll(dbs ...common.Database) { + for _, db := range dbs { + db.Flush() + db.Close() + } +} diff --git a/cmd/geth/main.go b/cmd/geth/main.go index f849063fa..9bf0fe610 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -24,28 +24,23 @@ import ( "fmt" "io" "io/ioutil" + _ "net/http/pprof" "os" "path/filepath" "runtime" "strconv" "strings" - "time" "github.com/codegangsta/cli" "github.com/ethereum/ethash" "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/ethereum/go-ethereum/logger/glog" "github.com/mattn/go-colorable" "github.com/mattn/go-isatty" ) -import _ "net/http/pprof" const ( ClientIdentifier = "Geth" @@ -69,7 +64,12 @@ func init() { app.Action = run app.HideVersion = true // we have a command to print the version app.Commands = []cli.Command{ - blocktestCmd, + blocktestCommand, + importCommand, + exportCommand, + upgradedbCommand, + removedbCommand, + dumpCommand, { Action: makedag, Name: "makedag", @@ -194,15 +194,6 @@ nodes. }, }, }, - { - Action: dump, - Name: "dump", - Usage: `dump a specific block from storage`, - Description: ` -The arguments are interpreted as block numbers or hashes. -Use "ethereum dump 0" to dump the genesis block. -`, - }, { Action: console, Name: "console", @@ -222,26 +213,6 @@ The JavaScript VM exposes a node admin interface as well as the Ðapp JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Console `, }, - { - Action: importchain, - Name: "import", - Usage: `import a blockchain file`, - }, - { - Action: exportchain, - Name: "export", - Usage: `export blockchain into file`, - }, - { - Action: upgradeDb, - Name: "upgradedb", - Usage: "upgrade chainblock database", - }, - { - Action: removeDb, - Name: "removedb", - Usage: "Remove blockchain and state databases", - }, } app.Flags = []cli.Flag{ utils.IdentityFlag, @@ -508,103 +479,6 @@ func accountImport(ctx *cli.Context) { fmt.Printf("Address: %x\n", acct) } -func importchain(ctx *cli.Context) { - if len(ctx.Args()) != 1 { - utils.Fatalf("This command requires an argument.") - } - chain, blockDB, stateDB, extraDB := utils.GetChain(ctx) - start := time.Now() - if err := utils.ImportChain(chain, ctx.Args().First()); err != nil { - utils.Fatalf("Import error: %v\n", err) - } - flushAll(blockDB, stateDB, extraDB) - fmt.Printf("Import done in %v", time.Since(start)) -} - -func exportchain(ctx *cli.Context) { - if len(ctx.Args()) != 1 { - utils.Fatalf("This command requires an argument.") - } - chain, _, _, _ := utils.GetChain(ctx) - start := time.Now() - if err := utils.ExportChain(chain, ctx.Args().First()); err != nil { - utils.Fatalf("Export error: %v\n", err) - } - fmt.Printf("Export done in %v", time.Since(start)) -} - -func removeDb(ctx *cli.Context) { - confirm, err := utils.PromptConfirm("Remove local databases?") - if err != nil { - utils.Fatalf("%v", err) - } - - if confirm { - fmt.Println("Removing chain and state databases...") - start := time.Now() - - os.RemoveAll(filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), "blockchain")) - os.RemoveAll(filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), "state")) - - fmt.Printf("Removed in %v\n", time.Since(start)) - } else { - fmt.Println("Operation aborted") - } -} - -func upgradeDb(ctx *cli.Context) { - glog.Infoln("Upgrading blockchain database") - - chain, blockDB, stateDB, extraDB := utils.GetChain(ctx) - v, _ := blockDB.Get([]byte("BlockchainVersion")) - bcVersion := int(common.NewValue(v).Uint()) - if bcVersion == 0 { - bcVersion = core.BlockChainVersion - } - - // Export the current chain. - filename := fmt.Sprintf("blockchain_%d_%s.chain", bcVersion, time.Now().Format("20060102_150405")) - exportFile := filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), filename) - if err := utils.ExportChain(chain, exportFile); err != nil { - utils.Fatalf("Unable to export chain for reimport %s\n", err) - } - flushAll(blockDB, stateDB, extraDB) - os.RemoveAll(filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), "blockchain")) - os.RemoveAll(filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), "state")) - - // Import the chain file. - chain, blockDB, stateDB, extraDB = utils.GetChain(ctx) - blockDB.Put([]byte("BlockchainVersion"), common.NewValue(core.BlockChainVersion).Bytes()) - err := utils.ImportChain(chain, exportFile) - flushAll(blockDB, stateDB, extraDB) - if err != nil { - utils.Fatalf("Import error %v (a backup is made in %s, use the import command to import it)\n", err, exportFile) - } else { - os.Remove(exportFile) - glog.Infoln("Import finished") - } -} - -func dump(ctx *cli.Context) { - chain, _, stateDB, _ := utils.GetChain(ctx) - for _, arg := range ctx.Args() { - var block *types.Block - if hashish(arg) { - block = chain.GetBlock(common.HexToHash(arg)) - } else { - num, _ := strconv.Atoi(arg) - block = chain.GetBlockByNumber(uint64(num)) - } - if block == nil { - fmt.Println("{}") - utils.Fatalf("block not found") - } else { - state := state.New(block.Root(), stateDB) - fmt.Printf("%s\n", state.Dump()) - } - } -} - func makedag(ctx *cli.Context) { args := ctx.Args() wrongArgs := func() { @@ -647,16 +521,3 @@ func version(c *cli.Context) { fmt.Printf("GOPATH=%s\n", os.Getenv("GOPATH")) fmt.Printf("GOROOT=%s\n", runtime.GOROOT()) } - -// hashish returns true for strings that look like hashes. -func hashish(x string) bool { - _, err := strconv.Atoi(x) - return err != nil -} - -func flushAll(dbs ...common.Database) { - for _, db := range dbs { - db.Flush() - db.Close() - } -} -- cgit