chain_manager_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package core
  2. import (
  3. "fmt"
  4. "math/big"
  5. "testing"
  6. "time"
  7. "github.com/ethereum/go-ethereum/chain/types"
  8. "github.com/ethereum/go-ethereum/ethdb"
  9. "github.com/ethereum/go-ethereum/ethutil"
  10. "github.com/ethereum/go-ethereum/state"
  11. )
  12. var TD *big.Int
  13. func init() {
  14. ethutil.ReadConfig(".ethtest", "/tmp/ethtest", "")
  15. ethutil.Config.Db, _ = ethdb.NewMemDatabase()
  16. }
  17. type fakeproc struct {
  18. }
  19. func (self fakeproc) ProcessWithParent(a, b *types.Block) (*big.Int, state.Messages, error) {
  20. TD = new(big.Int).Add(TD, big.NewInt(1))
  21. return TD, nil, nil
  22. }
  23. func makechain(cman *ChainManager, max int) *BlockChain {
  24. blocks := make(types.Blocks, max)
  25. for i := 0; i < max; i++ {
  26. addr := ethutil.LeftPadBytes([]byte{byte(i)}, 20)
  27. block := cman.NewBlock(addr)
  28. if i != 0 {
  29. cman.CurrentBlock = blocks[i-1]
  30. }
  31. blocks[i] = block
  32. }
  33. return NewChain(blocks)
  34. }
  35. func TestLongerFork(t *testing.T) {
  36. cman := NewChainManager()
  37. cman.SetProcessor(fakeproc{})
  38. TD = big.NewInt(1)
  39. chainA := makechain(cman, 5)
  40. TD = big.NewInt(1)
  41. chainB := makechain(cman, 10)
  42. td, err := cman.TestChain(chainA)
  43. if err != nil {
  44. t.Error("unable to create new TD from chainA:", err)
  45. }
  46. cman.TD = td
  47. _, err = cman.TestChain(chainB)
  48. if err != nil {
  49. t.Error("expected chainB not to give errors:", err)
  50. }
  51. }
  52. func TestEqualFork(t *testing.T) {
  53. cman := NewChainManager()
  54. cman.SetProcessor(fakeproc{})
  55. TD = big.NewInt(1)
  56. chainA := makechain(cman, 5)
  57. TD = big.NewInt(2)
  58. chainB := makechain(cman, 5)
  59. td, err := cman.TestChain(chainA)
  60. if err != nil {
  61. t.Error("unable to create new TD from chainA:", err)
  62. }
  63. cman.TD = td
  64. _, err = cman.TestChain(chainB)
  65. if err != nil {
  66. t.Error("expected chainB not to give errors:", err)
  67. }
  68. }
  69. func TestBrokenChain(t *testing.T) {
  70. cman := NewChainManager()
  71. cman.SetProcessor(fakeproc{})
  72. TD = big.NewInt(1)
  73. chain := makechain(cman, 5)
  74. chain.Remove(chain.Front())
  75. _, err := cman.TestChain(chain)
  76. if err == nil {
  77. t.Error("expected broken chain to return error")
  78. }
  79. }
  80. func BenchmarkChainTesting(b *testing.B) {
  81. const chainlen = 1000
  82. ethutil.ReadConfig(".ethtest", "/tmp/ethtest", "")
  83. ethutil.Config.Db, _ = ethdb.NewMemDatabase()
  84. cman := NewChainManager()
  85. cman.SetProcessor(fakeproc{})
  86. TD = big.NewInt(1)
  87. chain := makechain(cman, chainlen)
  88. stime := time.Now()
  89. cman.TestChain(chain)
  90. fmt.Println(chainlen, "took", time.Since(stime))
  91. }