dao_test.go 6.1 KB

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