2019-05-10 17:04:10 +03:00
|
|
|
// Copyright 2019 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package clique
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This test case is a repro of an annoying bug that took us forever to catch.
|
2024-08-20 21:59:48 +08:00
|
|
|
// In Clique PoA networks, consecutive blocks might have the same state root (no
|
|
|
|
// block subsidy, empty block). If a node crashes, the chain ends up losing the
|
|
|
|
// recent state and needs to regenerate it from blocks already in the database.
|
|
|
|
// The bug was that processing the block *prior* to an empty one **also
|
|
|
|
// completes** the empty one, ending up in a known-block error.
|
2019-05-10 17:04:10 +03:00
|
|
|
func TestReimportMirroredState(t *testing.T) {
|
|
|
|
// Initialize a Clique chain with a single signer
|
|
|
|
var (
|
|
|
|
db = rawdb.NewMemoryDatabase()
|
|
|
|
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
|
|
|
addr = crypto.PubkeyToAddress(key.PublicKey)
|
|
|
|
engine = New(params.AllCliqueProtocolChanges.Clique, db)
|
|
|
|
signer = new(types.HomesteadSigner)
|
|
|
|
)
|
|
|
|
genspec := &core.Genesis{
|
2022-08-31 00:22:28 +08:00
|
|
|
Config: params.AllCliqueProtocolChanges,
|
2019-05-10 17:04:10 +03:00
|
|
|
ExtraData: make([]byte, extraVanity+common.AddressLength+extraSeal),
|
2024-02-16 19:05:33 +01:00
|
|
|
Alloc: map[common.Address]types.Account{
|
2021-06-15 13:56:14 +03:00
|
|
|
addr: {Balance: big.NewInt(10000000000000000)},
|
2019-05-10 17:04:10 +03:00
|
|
|
},
|
2021-06-15 13:56:14 +03:00
|
|
|
BaseFee: big.NewInt(params.InitialBaseFee),
|
2019-05-10 17:04:10 +03:00
|
|
|
}
|
|
|
|
copy(genspec.ExtraData[extraVanity:], addr[:])
|
|
|
|
|
|
|
|
// Generate a batch of blocks, each properly signed
|
2024-09-04 15:03:06 +02:00
|
|
|
chain, _ := core.NewBlockChain(rawdb.NewMemoryDatabase(), nil, genspec, nil, engine, vm.Config{}, nil)
|
2019-05-10 17:04:10 +03:00
|
|
|
defer chain.Stop()
|
|
|
|
|
2022-09-08 02:21:59 +08:00
|
|
|
_, blocks, _ := core.GenerateChainWithGenesis(genspec, engine, 3, func(i int, block *core.BlockGen) {
|
2019-05-10 17:04:10 +03:00
|
|
|
// The chain maker doesn't have access to a chain, so the difficulty will be
|
|
|
|
// lets unset (nil). Set it here to the correct value.
|
|
|
|
block.SetDifficulty(diffInTurn)
|
|
|
|
|
|
|
|
// We want to simulate an empty middle block, having the same state as the
|
|
|
|
// first one. The last is needs a state change again to force a reorg.
|
|
|
|
if i != 1 {
|
2021-06-15 13:56:14 +03:00
|
|
|
tx, err := types.SignTx(types.NewTransaction(block.TxNonce(addr), common.Address{0x00}, new(big.Int), params.TxGas, block.BaseFee(), nil), signer, key)
|
2019-05-10 17:04:10 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
block.AddTxWithChain(chain, tx)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
for i, block := range blocks {
|
|
|
|
header := block.Header()
|
|
|
|
if i > 0 {
|
|
|
|
header.ParentHash = blocks[i-1].Hash()
|
|
|
|
}
|
|
|
|
header.Extra = make([]byte, extraVanity+extraSeal)
|
|
|
|
header.Difficulty = diffInTurn
|
|
|
|
|
|
|
|
sig, _ := crypto.Sign(SealHash(header).Bytes(), key)
|
|
|
|
copy(header.Extra[len(header.Extra)-extraSeal:], sig)
|
|
|
|
blocks[i] = block.WithSeal(header)
|
|
|
|
}
|
|
|
|
// Insert the first two blocks and make sure the chain is valid
|
|
|
|
db = rawdb.NewMemoryDatabase()
|
2024-09-04 15:03:06 +02:00
|
|
|
chain, _ = core.NewBlockChain(db, nil, genspec, nil, engine, vm.Config{}, nil)
|
2019-05-10 17:04:10 +03:00
|
|
|
defer chain.Stop()
|
|
|
|
|
|
|
|
if _, err := chain.InsertChain(blocks[:2]); err != nil {
|
|
|
|
t.Fatalf("failed to insert initial blocks: %v", err)
|
|
|
|
}
|
2023-03-02 08:29:15 +02:00
|
|
|
if head := chain.CurrentBlock().Number.Uint64(); head != 2 {
|
2019-05-10 17:04:10 +03:00
|
|
|
t.Fatalf("chain head mismatch: have %d, want %d", head, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simulate a crash by creating a new chain on top of the database, without
|
2020-05-25 16:21:28 +08:00
|
|
|
// flushing the dirty states out. Insert the last block, triggering a sidechain
|
2019-05-10 17:04:10 +03:00
|
|
|
// reimport.
|
2024-09-04 15:03:06 +02:00
|
|
|
chain, _ = core.NewBlockChain(db, nil, genspec, nil, engine, vm.Config{}, nil)
|
2019-05-10 17:04:10 +03:00
|
|
|
defer chain.Stop()
|
|
|
|
|
|
|
|
if _, err := chain.InsertChain(blocks[2:]); err != nil {
|
|
|
|
t.Fatalf("failed to insert final block: %v", err)
|
|
|
|
}
|
2023-03-02 08:29:15 +02:00
|
|
|
if head := chain.CurrentBlock().Number.Uint64(); head != 3 {
|
2019-05-10 17:04:10 +03:00
|
|
|
t.Fatalf("chain head mismatch: have %d, want %d", head, 3)
|
|
|
|
}
|
|
|
|
}
|
2021-07-02 15:18:50 +08:00
|
|
|
|
|
|
|
func TestSealHash(t *testing.T) {
|
|
|
|
have := SealHash(&types.Header{
|
|
|
|
Difficulty: new(big.Int),
|
|
|
|
Number: new(big.Int),
|
|
|
|
Extra: make([]byte, 32+65),
|
|
|
|
BaseFee: new(big.Int),
|
|
|
|
})
|
|
|
|
want := common.HexToHash("0xbd3d1fa43fbc4c5bfcc91b179ec92e2861df3654de60468beb908ff805359e8f")
|
|
|
|
if have != want {
|
|
|
|
t.Errorf("have %x, want %x", have, want)
|
|
|
|
}
|
|
|
|
}
|