clique_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2019 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 clique
  17. import (
  18. "math/big"
  19. "testing"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/core"
  22. "github.com/ethereum/go-ethereum/core/rawdb"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/core/vm"
  25. "github.com/ethereum/go-ethereum/crypto"
  26. "github.com/ethereum/go-ethereum/params"
  27. )
  28. // This test case is a repro of an annoying bug that took us forever to catch.
  29. // In Clique PoA networks (Rinkeby, Görli, etc), consecutive blocks might have
  30. // the same state root (no block subsidy, empty block). If a node crashes, the
  31. // chain ends up losing the recent state and needs to regenerate it from blocks
  32. // already in the database. The bug was that processing the block *prior* to an
  33. // empty one **also completes** the empty one, ending up in a known-block error.
  34. func TestReimportMirroredState(t *testing.T) {
  35. // Initialize a Clique chain with a single signer
  36. var (
  37. db = rawdb.NewMemoryDatabase()
  38. key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
  39. addr = crypto.PubkeyToAddress(key.PublicKey)
  40. engine = New(params.AllCliqueProtocolChanges.Clique, db)
  41. signer = new(types.HomesteadSigner)
  42. )
  43. genspec := &core.Genesis{
  44. ExtraData: make([]byte, extraVanity+common.AddressLength+extraSeal),
  45. Alloc: map[common.Address]core.GenesisAccount{
  46. addr: {Balance: big.NewInt(1)},
  47. },
  48. }
  49. copy(genspec.ExtraData[extraVanity:], addr[:])
  50. genesis := genspec.MustCommit(db)
  51. // Generate a batch of blocks, each properly signed
  52. chain, _ := core.NewBlockChain(db, nil, params.AllCliqueProtocolChanges, engine, vm.Config{}, nil, nil, nil)
  53. defer chain.Stop()
  54. blocks, _ := core.GenerateChain(params.AllCliqueProtocolChanges, genesis, engine, db, 3, func(i int, block *core.BlockGen) {
  55. // The chain maker doesn't have access to a chain, so the difficulty will be
  56. // lets unset (nil). Set it here to the correct value.
  57. block.SetDifficulty(diffInTurn)
  58. // We want to simulate an empty middle block, having the same state as the
  59. // first one. The last is needs a state change again to force a reorg.
  60. if i != 1 {
  61. tx, err := types.SignTx(types.NewTransaction(block.TxNonce(addr), common.Address{0x00}, new(big.Int), params.TxGas, nil, nil), signer, key)
  62. if err != nil {
  63. panic(err)
  64. }
  65. block.AddTxWithChain(chain, tx)
  66. }
  67. })
  68. for i, block := range blocks {
  69. header := block.Header()
  70. if i > 0 {
  71. header.ParentHash = blocks[i-1].Hash()
  72. }
  73. header.Extra = make([]byte, extraVanity+extraSeal)
  74. header.Difficulty = diffInTurn
  75. sig, _ := crypto.Sign(SealHash(header).Bytes(), key)
  76. copy(header.Extra[len(header.Extra)-extraSeal:], sig)
  77. blocks[i] = block.WithSeal(header)
  78. }
  79. // Insert the first two blocks and make sure the chain is valid
  80. db = rawdb.NewMemoryDatabase()
  81. genspec.MustCommit(db)
  82. chain, _ = core.NewBlockChain(db, nil, params.AllCliqueProtocolChanges, engine, vm.Config{}, nil, nil, nil)
  83. defer chain.Stop()
  84. if _, err := chain.InsertChain(blocks[:2]); err != nil {
  85. t.Fatalf("failed to insert initial blocks: %v", err)
  86. }
  87. if head := chain.CurrentBlock().NumberU64(); head != 2 {
  88. t.Fatalf("chain head mismatch: have %d, want %d", head, 2)
  89. }
  90. // Simulate a crash by creating a new chain on top of the database, without
  91. // flushing the dirty states out. Insert the last block, triggering a sidechain
  92. // reimport.
  93. chain, _ = core.NewBlockChain(db, nil, params.AllCliqueProtocolChanges, engine, vm.Config{}, nil, nil, nil)
  94. defer chain.Stop()
  95. if _, err := chain.InsertChain(blocks[2:]); err != nil {
  96. t.Fatalf("failed to insert final block: %v", err)
  97. }
  98. if head := chain.CurrentBlock().NumberU64(); head != 3 {
  99. t.Fatalf("chain head mismatch: have %d, want %d", head, 3)
  100. }
  101. }