blocktest.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/codegangsta/cli"
  5. "github.com/ethereum/go-ethereum/cmd/utils"
  6. "github.com/ethereum/go-ethereum/eth"
  7. "github.com/ethereum/go-ethereum/ethdb"
  8. "github.com/ethereum/go-ethereum/common"
  9. "github.com/ethereum/go-ethereum/tests"
  10. )
  11. var blocktestCmd = cli.Command{
  12. Action: runblocktest,
  13. Name: "blocktest",
  14. Usage: `loads a block test file`,
  15. Description: `
  16. The first argument should be a block test file.
  17. The second argument is the name of a block test from the file.
  18. The block test will be loaded into an in-memory database.
  19. If loading succeeds, the RPC server is started. Clients will
  20. be able to interact with the chain defined by the test.
  21. `,
  22. }
  23. func runblocktest(ctx *cli.Context) {
  24. if len(ctx.Args()) != 2 {
  25. utils.Fatalf("This command requires two arguments.")
  26. }
  27. file, testname := ctx.Args()[0], ctx.Args()[1]
  28. bt, err := tests.LoadBlockTests(file)
  29. if err != nil {
  30. utils.Fatalf("%v", err)
  31. }
  32. test, ok := bt[testname]
  33. if !ok {
  34. utils.Fatalf("Test file does not contain test named %q", testname)
  35. }
  36. cfg := utils.MakeEthConfig(ClientIdentifier, Version, ctx)
  37. cfg.NewDB = func(path string) (common.Database, error) { return ethdb.NewMemDatabase() }
  38. ethereum, err := eth.New(cfg)
  39. if err != nil {
  40. utils.Fatalf("%v", err)
  41. }
  42. // import the genesis block
  43. ethereum.ResetWithGenesisBlock(test.Genesis)
  44. // import pre accounts
  45. if err := test.InsertPreState(ethereum.StateDb()); err != nil {
  46. utils.Fatalf("could not insert genesis accounts: %v", err)
  47. }
  48. // insert the test blocks, which will execute all transactions
  49. chain := ethereum.ChainManager()
  50. if err := chain.InsertChain(test.Blocks); err != nil {
  51. utils.Fatalf("Block Test load error: %v", err)
  52. } else {
  53. fmt.Println("Block Test chain loaded, starting ethereum.")
  54. }
  55. startEth(ctx, ethereum)
  56. }