dao_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package core
  17. import (
  18. "math/big"
  19. "testing"
  20. "github.com/ethereum/go-ethereum/core/vm"
  21. "github.com/ethereum/go-ethereum/ethdb"
  22. "github.com/ethereum/go-ethereum/event"
  23. "github.com/ethereum/go-ethereum/params"
  24. )
  25. // Tests that DAO-fork enabled clients can properly filter out fork-commencing
  26. // blocks based on their extradata fields.
  27. func TestDAOForkRangeExtradata(t *testing.T) {
  28. forkBlock := big.NewInt(32)
  29. // Generate a common prefix for both pro-forkers and non-forkers
  30. db, _ := ethdb.NewMemDatabase()
  31. genesis := WriteGenesisBlockForTesting(db)
  32. prefix, _ := GenerateChain(params.TestChainConfig, genesis, db, int(forkBlock.Int64()-1), func(i int, gen *BlockGen) {})
  33. // Create the concurrent, conflicting two nodes
  34. proDb, _ := ethdb.NewMemDatabase()
  35. WriteGenesisBlockForTesting(proDb)
  36. proConf := &params.ChainConfig{HomesteadBlock: big.NewInt(0), DAOForkBlock: forkBlock, DAOForkSupport: true}
  37. proBc, _ := NewBlockChain(proDb, proConf, new(FakePow), new(event.TypeMux), vm.Config{})
  38. conDb, _ := ethdb.NewMemDatabase()
  39. WriteGenesisBlockForTesting(conDb)
  40. conConf := &params.ChainConfig{HomesteadBlock: big.NewInt(0), DAOForkBlock: forkBlock, DAOForkSupport: false}
  41. conBc, _ := NewBlockChain(conDb, conConf, new(FakePow), new(event.TypeMux), vm.Config{})
  42. if _, err := proBc.InsertChain(prefix); err != nil {
  43. t.Fatalf("pro-fork: failed to import chain prefix: %v", err)
  44. }
  45. if _, err := conBc.InsertChain(prefix); err != nil {
  46. t.Fatalf("con-fork: failed to import chain prefix: %v", err)
  47. }
  48. // Try to expand both pro-fork and non-fork chains iteratively with other camp's blocks
  49. for i := int64(0); i < params.DAOForkExtraRange.Int64(); i++ {
  50. // Create a pro-fork block, and try to feed into the no-fork chain
  51. db, _ = ethdb.NewMemDatabase()
  52. WriteGenesisBlockForTesting(db)
  53. bc, _ := NewBlockChain(db, conConf, new(FakePow), new(event.TypeMux), vm.Config{})
  54. blocks := conBc.GetBlocksFromHash(conBc.CurrentBlock().Hash(), int(conBc.CurrentBlock().NumberU64()+1))
  55. for j := 0; j < len(blocks)/2; j++ {
  56. blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
  57. }
  58. if _, err := bc.InsertChain(blocks); err != nil {
  59. t.Fatalf("failed to import contra-fork chain for expansion: %v", err)
  60. }
  61. blocks, _ = GenerateChain(proConf, conBc.CurrentBlock(), db, 1, func(i int, gen *BlockGen) {})
  62. if _, err := conBc.InsertChain(blocks); err == nil {
  63. t.Fatalf("contra-fork chain accepted pro-fork block: %v", blocks[0])
  64. }
  65. // Create a proper no-fork block for the contra-forker
  66. blocks, _ = GenerateChain(conConf, conBc.CurrentBlock(), db, 1, func(i int, gen *BlockGen) {})
  67. if _, err := conBc.InsertChain(blocks); err != nil {
  68. t.Fatalf("contra-fork chain didn't accepted no-fork block: %v", err)
  69. }
  70. // Create a no-fork block, and try to feed into the pro-fork chain
  71. db, _ = ethdb.NewMemDatabase()
  72. WriteGenesisBlockForTesting(db)
  73. bc, _ = NewBlockChain(db, proConf, new(FakePow), new(event.TypeMux), vm.Config{})
  74. blocks = proBc.GetBlocksFromHash(proBc.CurrentBlock().Hash(), int(proBc.CurrentBlock().NumberU64()+1))
  75. for j := 0; j < len(blocks)/2; j++ {
  76. blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
  77. }
  78. if _, err := bc.InsertChain(blocks); err != nil {
  79. t.Fatalf("failed to import pro-fork chain for expansion: %v", err)
  80. }
  81. blocks, _ = GenerateChain(conConf, proBc.CurrentBlock(), db, 1, func(i int, gen *BlockGen) {})
  82. if _, err := proBc.InsertChain(blocks); err == nil {
  83. t.Fatalf("pro-fork chain accepted contra-fork block: %v", blocks[0])
  84. }
  85. // Create a proper pro-fork block for the pro-forker
  86. blocks, _ = GenerateChain(proConf, proBc.CurrentBlock(), db, 1, func(i int, gen *BlockGen) {})
  87. if _, err := proBc.InsertChain(blocks); err != nil {
  88. t.Fatalf("pro-fork chain didn't accepted pro-fork block: %v", err)
  89. }
  90. }
  91. // Verify that contra-forkers accept pro-fork extra-datas after forking finishes
  92. db, _ = ethdb.NewMemDatabase()
  93. WriteGenesisBlockForTesting(db)
  94. bc, _ := NewBlockChain(db, conConf, new(FakePow), new(event.TypeMux), vm.Config{})
  95. blocks := conBc.GetBlocksFromHash(conBc.CurrentBlock().Hash(), int(conBc.CurrentBlock().NumberU64()+1))
  96. for j := 0; j < len(blocks)/2; j++ {
  97. blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
  98. }
  99. if _, err := bc.InsertChain(blocks); err != nil {
  100. t.Fatalf("failed to import contra-fork chain for expansion: %v", err)
  101. }
  102. blocks, _ = GenerateChain(proConf, conBc.CurrentBlock(), db, 1, func(i int, gen *BlockGen) {})
  103. if _, err := conBc.InsertChain(blocks); err != nil {
  104. t.Fatalf("contra-fork chain didn't accept pro-fork block post-fork: %v", err)
  105. }
  106. // Verify that pro-forkers accept contra-fork extra-datas after forking finishes
  107. db, _ = ethdb.NewMemDatabase()
  108. WriteGenesisBlockForTesting(db)
  109. bc, _ = NewBlockChain(db, proConf, new(FakePow), new(event.TypeMux), vm.Config{})
  110. blocks = proBc.GetBlocksFromHash(proBc.CurrentBlock().Hash(), int(proBc.CurrentBlock().NumberU64()+1))
  111. for j := 0; j < len(blocks)/2; j++ {
  112. blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
  113. }
  114. if _, err := bc.InsertChain(blocks); err != nil {
  115. t.Fatalf("failed to import pro-fork chain for expansion: %v", err)
  116. }
  117. blocks, _ = GenerateChain(conConf, proBc.CurrentBlock(), db, 1, func(i int, gen *BlockGen) {})
  118. if _, err := proBc.InsertChain(blocks); err != nil {
  119. t.Fatalf("pro-fork chain didn't accept contra-fork block post-fork: %v", err)
  120. }
  121. }