From 2565dbdf4e33e9b724392f714b0fd79ebd371dc5 Mon Sep 17 00:00:00 2001 From: Marius van der Wijden Date: Wed, 11 Jan 2023 15:01:39 +0100 Subject: [PATCH] core: fix rebasing issues --- core/blockchain_test.go | 2 ++ core/forkid/forkid.go | 39 +++------------------- core/state_processor_test.go | 65 +++++++++++++++++++++--------------- eth/catalyst/api_test.go | 4 +-- 4 files changed, 47 insertions(+), 63 deletions(-) diff --git a/core/blockchain_test.go b/core/blockchain_test.go index b462801ae0bb..8ab235bdcaf3 100644 --- a/core/blockchain_test.go +++ b/core/blockchain_test.go @@ -4276,6 +4276,8 @@ func TestEIP3651(t *testing.T) { gspec.Config.BerlinBlock = common.Big0 gspec.Config.LondonBlock = common.Big0 + gspec.Config.TerminalTotalDifficulty = common.Big0 + gspec.Config.TerminalTotalDifficultyPassed = true gspec.Config.ShanghaiTime = common.Big0 signer := types.LatestSigner(gspec.Config) diff --git a/core/forkid/forkid.go b/core/forkid/forkid.go index 0442ef05e039..300dca6cfba0 100644 --- a/core/forkid/forkid.go +++ b/core/forkid/forkid.go @@ -139,10 +139,9 @@ func newFilter(config *params.ChainConfig, genesis common.Hash, headfn func() (u forks = append(append([]uint64{}, forksByBlock...), forksByTime...) sums = make([][4]byte, len(forks)+1) // 0th is the genesis ) - allForks := append(forks, forksByTime...) hash := crc32.ChecksumIEEE(genesis[:]) sums[0] = checksumToBytes(hash) - for i, fork := range allForks { + for i, fork := range forks { hash = checksumUpdate(hash, fork) sums[i+1] = checksumToBytes(hash) } @@ -188,7 +187,7 @@ func newFilter(config *params.ChainConfig, genesis common.Hash, headfn func() (u } // Found the first unpassed fork block, check if our current state matches // the remote checksum (rule #1). - if sums[index] == id.Hash { + if sums[i] == id.Hash { // Fork checksum matched, check if a remote future fork block already passed // locally without the local node being aware of it (rule #1a). if id.Next > 0 && (head >= id.Next || (id.Next > timestampThreshold && time >= id.Next)) { @@ -199,10 +198,10 @@ func newFilter(config *params.ChainConfig, genesis common.Hash, headfn func() (u } // The local and remote nodes are in different forks currently, check if the // remote checksum is a subset of our local forks (rule #2). - for j := 0; j < index; j++ { + for j := 0; j < i; j++ { if sums[j] == id.Hash { // Remote checksum is a subset, validate based on the announced next fork - if allForks[j] != id.Next { + if forks[j] != id.Next { return ErrRemoteStale } return nil @@ -210,7 +209,7 @@ func newFilter(config *params.ChainConfig, genesis common.Hash, headfn func() (u } // Remote chain is not a subset of our local one, check if it's a superset by // any chance, signalling that we're simply out of sync (rule #3). - for j := index + 1; j < len(sums); j++ { + for j := i + 1; j < len(sums); j++ { if sums[j] == id.Hash { // Yay, remote checksum is a superset, ignore upcoming forks return nil @@ -219,28 +218,6 @@ func newFilter(config *params.ChainConfig, genesis common.Hash, headfn func() (u // No exact, subset or superset match. We are on differing chains, reject. return ErrLocalIncompatibleOrStale } - - head, time := headfn() - // Verify forks by block - for i, fork := range forks { - // If our head is beyond this fork, continue to the next (we have a dummy - // fork of maxuint64 as the last item to always fail this check eventually). - if head >= fork { - continue - } - return verify(i, head) - } - // Verify forks by time - for i := len(forks); i < len(forks)+len(forksByTime); i++ { - fork := forksByTime[i-len(forks)] - // If our head is beyond this fork, continue to the next (we have a dummy - // fork of maxuint64 as the last item to always fail this check eventually). - if time >= fork { - continue - } - return verify(i, time) - } - log.Error("Impossible fork ID validation", "id", id) return nil // Something's very wrong, accept rather than reject } @@ -309,12 +286,6 @@ func gatherForks(config *params.ChainConfig) ([]uint64, []uint64) { i-- } } - for i := 1; i < len(forksByTime); i++ { - if forksByTime[i] == forksByTime[i-1] { - forksByTime = append(forksByTime[:i], forksByTime[i+1:]...) - i-- - } - } // Skip any forks in block 0, that's the genesis ruleset if len(forksByBlock) > 0 && forksByBlock[0] == 0 { forksByBlock = forksByBlock[1:] diff --git a/core/state_processor_test.go b/core/state_processor_test.go index 400ccd318793..0d8bd391262d 100644 --- a/core/state_processor_test.go +++ b/core/state_processor_test.go @@ -24,6 +24,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/consensus" + "github.com/ethereum/go-ethereum/consensus/beacon" "github.com/ethereum/go-ethereum/consensus/ethash" "github.com/ethereum/go-ethereum/consensus/misc" "github.com/ethereum/go-ethereum/core/rawdb" @@ -312,22 +313,24 @@ func TestStateProcessorErrors(t *testing.T) { db = rawdb.NewMemoryDatabase() gspec = &Genesis{ Config: ¶ms.ChainConfig{ - ChainID: big.NewInt(1), - HomesteadBlock: big.NewInt(0), - EIP150Block: big.NewInt(0), - EIP155Block: big.NewInt(0), - EIP158Block: big.NewInt(0), - ByzantiumBlock: big.NewInt(0), - ConstantinopleBlock: big.NewInt(0), - PetersburgBlock: big.NewInt(0), - IstanbulBlock: big.NewInt(0), - MuirGlacierBlock: big.NewInt(0), - BerlinBlock: big.NewInt(0), - LondonBlock: big.NewInt(0), - ArrowGlacierBlock: big.NewInt(0), - GrayGlacierBlock: big.NewInt(0), - MergeNetsplitBlock: big.NewInt(0), - ShanghaiTime: big.NewInt(0), + ChainID: big.NewInt(1), + HomesteadBlock: big.NewInt(0), + EIP150Block: big.NewInt(0), + EIP155Block: big.NewInt(0), + EIP158Block: big.NewInt(0), + ByzantiumBlock: big.NewInt(0), + ConstantinopleBlock: big.NewInt(0), + PetersburgBlock: big.NewInt(0), + IstanbulBlock: big.NewInt(0), + MuirGlacierBlock: big.NewInt(0), + BerlinBlock: big.NewInt(0), + LondonBlock: big.NewInt(0), + ArrowGlacierBlock: big.NewInt(0), + GrayGlacierBlock: big.NewInt(0), + MergeNetsplitBlock: big.NewInt(0), + TerminalTotalDifficulty: big.NewInt(0), + TerminalTotalDifficultyPassed: true, + ShanghaiTime: big.NewInt(0), }, Alloc: GenesisAlloc{ common.HexToAddress("0x71562b71999873DB5b286dF957af199Ec94617F7"): GenesisAccount{ @@ -337,7 +340,7 @@ func TestStateProcessorErrors(t *testing.T) { }, } genesis = gspec.MustCommit(db) - blockchain, _ = NewBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + blockchain, _ = NewBlockChain(db, nil, gspec, nil, beacon.New(ethash.NewFaker()), vm.Config{}, nil, nil) tooBigInitCode = [params.MaxInitCodeSize + 1]byte{} smallInitCode = [320]byte{} ) @@ -359,7 +362,7 @@ func TestStateProcessorErrors(t *testing.T) { want: "could not apply tx 0 [0x39b7436cb432d3662a25626474282c5c4c1a213326fd87e4e18a91477bae98b2]: intrinsic gas too low: have 54299, want 54300", }, } { - block := GenerateBadBlock(genesis, ethash.NewFaker(), tt.txs, gspec.Config) + block := GenerateBadBlock(genesis, beacon.New(ethash.NewFaker()), tt.txs, gspec.Config) _, err := blockchain.InsertChain(types.Blocks{block}) if err == nil { t.Fatal("block imported without errors") @@ -376,23 +379,31 @@ func TestStateProcessorErrors(t *testing.T) { // valid to be considered for import: // - valid pow (fake), ancestry, difficulty, gaslimit etc func GenerateBadBlock(parent *types.Block, engine consensus.Engine, txs types.Transactions, config *params.ChainConfig) *types.Block { - header := &types.Header{ - ParentHash: parent.Hash(), - Coinbase: parent.Coinbase(), - Difficulty: engine.CalcDifficulty(&fakeChainReader{config}, parent.Time()+10, &types.Header{ + difficulty := big.NewInt(0) + if !config.TerminalTotalDifficultyPassed { + difficulty = engine.CalcDifficulty(&fakeChainReader{config}, parent.Time()+10, &types.Header{ Number: parent.Number(), Time: parent.Time(), Difficulty: parent.Difficulty(), UncleHash: parent.UncleHash(), - }), - GasLimit: parent.GasLimit(), - Number: new(big.Int).Add(parent.Number(), common.Big1), - Time: parent.Time() + 10, - UncleHash: types.EmptyUncleHash, + }) + } + + header := &types.Header{ + ParentHash: parent.Hash(), + Coinbase: parent.Coinbase(), + Difficulty: difficulty, + GasLimit: parent.GasLimit(), + Number: new(big.Int).Add(parent.Number(), common.Big1), + Time: parent.Time() + 10, + UncleHash: types.EmptyUncleHash, } if config.IsLondon(header.Number) { header.BaseFee = misc.CalcBaseFee(config, parent.Header()) } + if config.IsShanghai(big.NewInt(int64(header.Time))) { + header.WithdrawalsHash = &types.EmptyRootHash + } var receipts []*types.Receipt // The post-state result doesn't need to be correct (this is a bad block), but we do need something there // Preferably something unique. So let's use a combo of blocknum + txhash diff --git a/eth/catalyst/api_test.go b/eth/catalyst/api_test.go index 7cdfb2d51aff..cea807c98fdf 100644 --- a/eth/catalyst/api_test.go +++ b/eth/catalyst/api_test.go @@ -864,11 +864,11 @@ func TestInvalidBloom(t *testing.T) { func TestNewPayloadOnInvalidTerminalBlock(t *testing.T) { genesis, preMergeBlocks := generateMergeChain(100, false) - genesis.Config.TerminalTotalDifficulty = preMergeBlocks[0].Difficulty() //.Sub(genesis.Config.TerminalTotalDifficulty, preMergeBlocks[len(preMergeBlocks)-1].Difficulty()) - n, ethservice := startEthService(t, genesis, preMergeBlocks) defer n.Close() + genesis.Config.TerminalTotalDifficulty = preMergeBlocks[0].Difficulty() //.Sub(genesis.Config.TerminalTotalDifficulty, preMergeBlocks[len(preMergeBlocks)-1].Difficulty()) + var ( api = NewConsensusAPI(ethservice) parent = preMergeBlocks[len(preMergeBlocks)-1]