dao_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/ethdb"
  21. "github.com/ethereum/go-ethereum/event"
  22. "github.com/ethereum/go-ethereum/params"
  23. )
  24. // Tests that DAO-fork enabled clients can properly filter out fork-commencing
  25. // blocks based on their extradata fields.
  26. func TestDAOForkRangeExtradata(t *testing.T) {
  27. forkBlock := big.NewInt(32)
  28. // Generate a common prefix for both pro-forkers and non-forkers
  29. db, _ := ethdb.NewMemDatabase()
  30. genesis := WriteGenesisBlockForTesting(db)
  31. prefix, _ := GenerateChain(params.TestChainConfig, genesis, db, int(forkBlock.Int64()-1), func(i int, gen *BlockGen) {})
  32. // Create the concurrent, conflicting two nodes
  33. proDb, _ := ethdb.NewMemDatabase()
  34. WriteGenesisBlockForTesting(proDb)
  35. proConf := &params.ChainConfig{HomesteadBlock: big.NewInt(0), DAOForkBlock: forkBlock, DAOForkSupport: true}
  36. proBc, _ := NewBlockChain(proDb, proConf, new(FakePow), new(event.TypeMux))
  37. conDb, _ := ethdb.NewMemDatabase()
  38. WriteGenesisBlockForTesting(conDb)
  39. conConf := &params.ChainConfig{HomesteadBlock: big.NewInt(0), DAOForkBlock: forkBlock, DAOForkSupport: false}
  40. conBc, _ := NewBlockChain(conDb, conConf, new(FakePow), new(event.TypeMux))
  41. if _, err := proBc.InsertChain(prefix); err != nil {
  42. t.Fatalf("pro-fork: failed to import chain prefix: %v", err)
  43. }
  44. if _, err := conBc.InsertChain(prefix); err != nil {
  45. t.Fatalf("con-fork: failed to import chain prefix: %v", err)
  46. }
  47. // Try to expand both pro-fork and non-fork chains iteratively with other camp's blocks
  48. for i := int64(0); i < params.DAOForkExtraRange.Int64(); i++ {
  49. // Create a pro-fork block, and try to feed into the no-fork chain
  50. db, _ = ethdb.NewMemDatabase()
  51. WriteGenesisBlockForTesting(db)
  52. bc, _ := NewBlockChain(db, conConf, new(FakePow), new(event.TypeMux))
  53. blocks := conBc.GetBlocksFromHash(conBc.CurrentBlock().Hash(), int(conBc.CurrentBlock().NumberU64()+1))
  54. for j := 0; j < len(blocks)/2; j++ {
  55. blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
  56. }
  57. if _, err := bc.InsertChain(blocks); err != nil {
  58. t.Fatalf("failed to import contra-fork chain for expansion: %v", err)
  59. }
  60. blocks, _ = GenerateChain(proConf, conBc.CurrentBlock(), db, 1, func(i int, gen *BlockGen) {})
  61. if _, err := conBc.InsertChain(blocks); err == nil {
  62. t.Fatalf("contra-fork chain accepted pro-fork block: %v", blocks[0])
  63. }
  64. // Create a proper no-fork block for the contra-forker
  65. blocks, _ = GenerateChain(conConf, conBc.CurrentBlock(), db, 1, func(i int, gen *BlockGen) {})
  66. if _, err := conBc.InsertChain(blocks); err != nil {
  67. t.Fatalf("contra-fork chain didn't accepted no-fork block: %v", err)
  68. }
  69. // Create a no-fork block, and try to feed into the pro-fork chain
  70. db, _ = ethdb.NewMemDatabase()
  71. WriteGenesisBlockForTesting(db)
  72. bc, _ = NewBlockChain(db, proConf, new(FakePow), new(event.TypeMux))
  73. blocks = proBc.GetBlocksFromHash(proBc.CurrentBlock().Hash(), int(proBc.CurrentBlock().NumberU64()+1))
  74. for j := 0; j < len(blocks)/2; j++ {
  75. blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
  76. }
  77. if _, err := bc.InsertChain(blocks); err != nil {
  78. t.Fatalf("failed to import pro-fork chain for expansion: %v", err)
  79. }
  80. blocks, _ = GenerateChain(conConf, proBc.CurrentBlock(), db, 1, func(i int, gen *BlockGen) {})
  81. if _, err := proBc.InsertChain(blocks); err == nil {
  82. t.Fatalf("pro-fork chain accepted contra-fork block: %v", blocks[0])
  83. }
  84. // Create a proper pro-fork block for the pro-forker
  85. blocks, _ = GenerateChain(proConf, proBc.CurrentBlock(), db, 1, func(i int, gen *BlockGen) {})
  86. if _, err := proBc.InsertChain(blocks); err != nil {
  87. t.Fatalf("pro-fork chain didn't accepted pro-fork block: %v", err)
  88. }
  89. }
  90. // Verify that contra-forkers accept pro-fork extra-datas after forking finishes
  91. db, _ = ethdb.NewMemDatabase()
  92. WriteGenesisBlockForTesting(db)
  93. bc, _ := NewBlockChain(db, conConf, new(FakePow), new(event.TypeMux))
  94. blocks := conBc.GetBlocksFromHash(conBc.CurrentBlock().Hash(), int(conBc.CurrentBlock().NumberU64()+1))
  95. for j := 0; j < len(blocks)/2; j++ {
  96. blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
  97. }
  98. if _, err := bc.InsertChain(blocks); err != nil {
  99. t.Fatalf("failed to import contra-fork chain for expansion: %v", err)
  100. }
  101. blocks, _ = GenerateChain(proConf, conBc.CurrentBlock(), db, 1, func(i int, gen *BlockGen) {})
  102. if _, err := conBc.InsertChain(blocks); err != nil {
  103. t.Fatalf("contra-fork chain didn't accept pro-fork block post-fork: %v", err)
  104. }
  105. // Verify that pro-forkers accept contra-fork extra-datas after forking finishes
  106. db, _ = ethdb.NewMemDatabase()
  107. WriteGenesisBlockForTesting(db)
  108. bc, _ = NewBlockChain(db, proConf, new(FakePow), new(event.TypeMux))
  109. blocks = proBc.GetBlocksFromHash(proBc.CurrentBlock().Hash(), int(proBc.CurrentBlock().NumberU64()+1))
  110. for j := 0; j < len(blocks)/2; j++ {
  111. blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
  112. }
  113. if _, err := bc.InsertChain(blocks); err != nil {
  114. t.Fatalf("failed to import pro-fork chain for expansion: %v", err)
  115. }
  116. blocks, _ = GenerateChain(conConf, proBc.CurrentBlock(), db, 1, func(i int, gen *BlockGen) {})
  117. if _, err := proBc.InsertChain(blocks); err != nil {
  118. t.Fatalf("pro-fork chain didn't accept contra-fork block post-fork: %v", err)
  119. }
  120. }