2019-05-10 14:04:10 +00: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/>.
|
|
|
|
|
2021-06-06 21:44:14 +00:00
|
|
|
package clique_test
|
2019-05-10 14:04:10 +00:00
|
|
|
|
|
|
|
import (
|
2021-06-05 15:17:04 +00:00
|
|
|
"context"
|
2019-05-10 14:04:10 +00:00
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
2020-06-04 07:43:08 +00:00
|
|
|
"github.com/holiman/uint256"
|
2021-07-29 11:53:13 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
|
|
|
"github.com/ledgerwatch/erigon-lib/kv/memdb"
|
2020-06-04 07:43:08 +00:00
|
|
|
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
2021-06-06 21:44:14 +00:00
|
|
|
"github.com/ledgerwatch/erigon/consensus/clique"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/core"
|
|
|
|
"github.com/ledgerwatch/erigon/core/rawdb"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
|
|
|
"github.com/ledgerwatch/erigon/crypto"
|
|
|
|
"github.com/ledgerwatch/erigon/params"
|
2021-06-06 21:44:14 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/stages"
|
2019-05-10 14:04:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// This test case is a repro of an annoying bug that took us forever to catch.
|
|
|
|
// In Clique PoA networks (Rinkeby, Görli, etc), 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.
|
|
|
|
func TestReimportMirroredState(t *testing.T) {
|
|
|
|
// Initialize a Clique chain with a single signer
|
|
|
|
var (
|
2021-07-28 02:47:38 +00:00
|
|
|
cliqueDB = memdb.NewTestDB(t)
|
2021-04-19 21:58:05 +00:00
|
|
|
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
|
|
|
addr = crypto.PubkeyToAddress(key.PublicKey)
|
2021-06-06 21:44:14 +00:00
|
|
|
engine = clique.New(params.AllCliqueProtocolChanges, params.CliqueSnapshot, cliqueDB)
|
2021-04-22 17:11:37 +00:00
|
|
|
signer = types.LatestSignerForChainID(nil)
|
2019-05-10 14:04:10 +00:00
|
|
|
)
|
|
|
|
genspec := &core.Genesis{
|
2021-06-06 21:44:14 +00:00
|
|
|
ExtraData: make([]byte, clique.ExtraVanity+common.AddressLength+clique.ExtraSeal),
|
2019-05-10 14:04:10 +00:00
|
|
|
Alloc: map[common.Address]core.GenesisAccount{
|
2021-10-13 04:04:59 +00:00
|
|
|
addr: {Balance: big.NewInt(10000000000000000)},
|
2019-05-10 14:04:10 +00:00
|
|
|
},
|
2021-06-06 21:44:14 +00:00
|
|
|
Config: params.AllCliqueProtocolChanges,
|
2019-05-10 14:04:10 +00:00
|
|
|
}
|
2021-06-06 21:44:14 +00:00
|
|
|
copy(genspec.ExtraData[clique.ExtraVanity:], addr[:])
|
|
|
|
m := stages.MockWithGenesisEngine(t, genspec, engine)
|
2019-05-10 14:04:10 +00:00
|
|
|
|
|
|
|
// Generate a batch of blocks, each properly signed
|
2021-06-05 15:17:04 +00:00
|
|
|
getHeader := func(hash common.Hash, number uint64) (h *types.Header) {
|
2021-07-28 02:47:38 +00:00
|
|
|
if err := m.DB.View(context.Background(), func(tx kv.Tx) error {
|
2021-06-05 15:17:04 +00:00
|
|
|
h = rawdb.ReadHeader(tx, hash, number)
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return h
|
|
|
|
}
|
2019-05-10 14:04:10 +00:00
|
|
|
|
2021-06-06 21:44:14 +00:00
|
|
|
chain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, 3, func(i int, block *core.BlockGen) {
|
2019-05-10 14:04:10 +00: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.
|
2021-06-06 21:44:14 +00:00
|
|
|
block.SetDifficulty(clique.DiffInTurn)
|
2019-05-10 14:04:10 +00:00
|
|
|
|
|
|
|
// 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-10-13 04:04:59 +00:00
|
|
|
baseFee, _ := uint256.FromBig(block.GetHeader().BaseFee)
|
|
|
|
tx, err := types.SignTx(types.NewTransaction(block.TxNonce(addr), common.Address{0x00}, new(uint256.Int), params.TxGas, baseFee, nil), *signer, key)
|
2019-05-10 14:04:10 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-05-23 03:48:31 +00:00
|
|
|
block.AddTxWithChain(getHeader, engine, tx)
|
2019-05-10 14:04:10 +00:00
|
|
|
}
|
2020-07-10 21:37:34 +00:00
|
|
|
}, false /* intermediateHashes */)
|
2020-07-09 06:15:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("generate blocks: %v", err)
|
|
|
|
}
|
2021-05-23 16:10:34 +00:00
|
|
|
for i, block := range chain.Blocks {
|
2019-05-10 14:04:10 +00:00
|
|
|
header := block.Header()
|
|
|
|
if i > 0 {
|
2021-05-23 16:10:34 +00:00
|
|
|
header.ParentHash = chain.Blocks[i-1].Hash()
|
2019-05-10 14:04:10 +00:00
|
|
|
}
|
2021-06-06 21:44:14 +00:00
|
|
|
header.Extra = make([]byte, clique.ExtraVanity+clique.ExtraSeal)
|
|
|
|
header.Difficulty = clique.DiffInTurn
|
2019-05-10 14:04:10 +00:00
|
|
|
|
2021-06-06 21:44:14 +00:00
|
|
|
sig, _ := crypto.Sign(clique.SealHash(header).Bytes(), key)
|
|
|
|
copy(header.Extra[len(header.Extra)-clique.ExtraSeal:], sig)
|
|
|
|
chain.Headers[i] = header
|
2021-05-23 16:10:34 +00:00
|
|
|
chain.Blocks[i] = block.WithSeal(header)
|
2019-05-10 14:04:10 +00:00
|
|
|
}
|
2021-05-23 03:48:31 +00:00
|
|
|
|
2019-05-10 14:04:10 +00:00
|
|
|
// Insert the first two blocks and make sure the chain is valid
|
2021-06-06 21:44:14 +00:00
|
|
|
if err := m.InsertChain(chain.Slice(0, 2)); err != nil {
|
2019-05-10 14:04:10 +00:00
|
|
|
t.Fatalf("failed to insert initial blocks: %v", err)
|
|
|
|
}
|
2021-07-28 02:47:38 +00:00
|
|
|
if err := m.DB.View(context.Background(), func(tx kv.Tx) error {
|
2021-06-05 15:17:04 +00:00
|
|
|
if head, err1 := rawdb.ReadBlockByHash(tx, rawdb.ReadHeadHeaderHash(tx)); err1 != nil {
|
|
|
|
t.Errorf("could not read chain head: %v", err1)
|
|
|
|
} else if head.NumberU64() != 2 {
|
|
|
|
t.Errorf("chain head mismatch: have %d, want %d", head.NumberU64(), 2)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
2019-05-10 14:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Simulate a crash by creating a new chain on top of the database, without
|
2020-05-25 08:21:28 +00:00
|
|
|
// flushing the dirty states out. Insert the last block, triggering a sidechain
|
2019-05-10 14:04:10 +00:00
|
|
|
// reimport.
|
2021-06-06 21:44:14 +00:00
|
|
|
if err := m.InsertChain(chain.Slice(2, chain.Length)); err != nil {
|
2019-05-10 14:04:10 +00:00
|
|
|
t.Fatalf("failed to insert final block: %v", err)
|
|
|
|
}
|
2021-07-28 02:47:38 +00:00
|
|
|
if err := m.DB.View(context.Background(), func(tx kv.Tx) error {
|
2021-06-05 15:17:04 +00:00
|
|
|
if head, err1 := rawdb.ReadBlockByHash(tx, rawdb.ReadHeadHeaderHash(tx)); err1 != nil {
|
|
|
|
t.Errorf("could not read chain head: %v", err1)
|
|
|
|
} else if head.NumberU64() != 3 {
|
|
|
|
t.Errorf("chain head mismatch: have %d, want %d", head.NumberU64(), 3)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
2019-05-10 14:04:10 +00:00
|
|
|
}
|
2021-06-05 15:17:04 +00:00
|
|
|
|
2019-05-10 14:04:10 +00:00
|
|
|
}
|