From 5df5fd995bd3e39cddf6d096e39bb10e7414e747 Mon Sep 17 00:00:00 2001 From: Shane Bammel Date: Fri, 24 Feb 2023 20:54:12 -0600 Subject: [PATCH] Refactor and simplify PulseChain integration --- consensus/clique/clique.go | 1 - consensus/ethash/consensus.go | 2 +- consensus/serenity/serenity.go | 43 ++++--------------------------- core/rawdb/accessors_chain.go | 13 +--------- eth/stagedsync/stage_headers.go | 6 ++--- params/chainspecs/pulsechain.json | 2 +- pulse/pulse.go | 8 ------ 7 files changed, 11 insertions(+), 64 deletions(-) diff --git a/consensus/clique/clique.go b/consensus/clique/clique.go index a3dc1100d..dab221f12 100644 --- a/consensus/clique/clique.go +++ b/consensus/clique/clique.go @@ -371,7 +371,6 @@ func (c *Clique) Finalize(config *chain.Config, header *types.Header, state *sta txs types.Transactions, uncles []*types.Header, r types.Receipts, withdrawals []*types.Withdrawal, e consensus.EpochReader, chain consensus.ChainHeaderReader, syscall consensus.SystemCall, ) (types.Transactions, types.Receipts, error) { - // No block rewards in PoA, so the state remains as is and uncles are dropped header.UncleHash = types.CalcUncleHash(nil) return txs, r, nil diff --git a/consensus/ethash/consensus.go b/consensus/ethash/consensus.go index 105226e4d..77c52e05f 100644 --- a/consensus/ethash/consensus.go +++ b/consensus/ethash/consensus.go @@ -565,7 +565,7 @@ func (ethash *Ethash) Finalize(config *chain.Config, header *types.Header, state e consensus.EpochReader, chain consensus.ChainHeaderReader, syscall consensus.SystemCall, ) (types.Transactions, types.Receipts, error) { // Apply fork changes on PrimordialPulse block - if cfg := chain.Config(); cfg.PulseChain != nil && cfg.PrimordialPulseBlock.Uint64() == header.Number.Uint64() { + if cfg := chain.Config(); cfg.IsPrimordialPulseBlock(header.Number.Uint64()) { pulse.PrimordialPulseFork(state, cfg.PulseChain) } diff --git a/consensus/serenity/serenity.go b/consensus/serenity/serenity.go index 8feeb1dab..38dd0aa8c 100644 --- a/consensus/serenity/serenity.go +++ b/consensus/serenity/serenity.go @@ -16,7 +16,6 @@ import ( "github.com/ledgerwatch/erigon/core/state" "github.com/ledgerwatch/erigon/core/types" "github.com/ledgerwatch/erigon/params" - "github.com/ledgerwatch/erigon/pulse" "github.com/ledgerwatch/erigon/rpc" ) @@ -85,17 +84,9 @@ func (s *Serenity) VerifyHeader(chain consensus.ChainHeaderReader, header *types if err != nil { return err } - if !reached { - if chain.Config().PulseChain == nil { - // Not verifying seals if the TTD is passed - return s.eth1Engine.VerifyHeader(chain, header, !chain.Config().TerminalTotalDifficultyPassed) - } else if !pulse.IsBeaconBlock(header.Number.Uint64()) { - // If this is not a PoS block then verify as PoW block - return s.eth1Engine.VerifyHeader(chain, header, !chain.Config().TerminalTotalDifficultyPassed) - } else if chain.Config().PrimordialPulseBlock.Cmp(header.Number) == 0 { - // If this is the PulseChain fork block then verify as PoW block - return s.eth1Engine.VerifyHeader(chain, header, !chain.Config().TerminalTotalDifficultyPassed) - } + if !reached && (chain.Config().PulseChain == nil || !IsPoSHeader(header)) { + // Delegate non-PoS block to eth1 verification + return s.eth1Engine.VerifyHeader(chain, header, !chain.Config().TerminalTotalDifficultyPassed) } // Short circuit if the parent is not known parent := chain.GetHeader(header.ParentHash, header.Number.Uint64()-1) @@ -125,15 +116,7 @@ func (s *Serenity) Prepare(chain consensus.ChainHeaderReader, header *types.Head return err } if !reached { - if chain.Config().PulseChain == nil { - return s.eth1Engine.Prepare(chain, header, state) - } else if !pulse.IsBeaconBlock(header.Number.Uint64()) { - // If this is not a PoS block then verify as PoW block - return s.eth1Engine.Prepare(chain, header, state) - } else if chain.Config().PrimordialPulseBlock.Cmp(header.Number) == 0 { - // If this is the PulseChain fork block then verify as PoW block - return s.eth1Engine.Prepare(chain, header, state) - } + return s.eth1Engine.Prepare(chain, header, state) } header.Difficulty = SerenityDifficulty header.Nonce = SerenityNonce @@ -187,15 +170,7 @@ func (s *Serenity) CalcDifficulty(chain consensus.ChainHeaderReader, time, paren return nil } if !reached { - if chain.Config().PulseChain == nil { - return s.eth1Engine.CalcDifficulty(chain, time, parentTime, parentDifficulty, parentNumber, parentHash, parentUncleHash, parentAuRaStep) - } else if !pulse.IsBeaconBlock(parentNumber) { - // If this is not a PoS block then verify as PoW block - return s.eth1Engine.CalcDifficulty(chain, time, parentTime, parentDifficulty, parentNumber, parentHash, parentUncleHash, parentAuRaStep) - } else if chain.Config().PrimordialPulseBlock.Uint64() == parentNumber { - // If this is the PulseChain fork block then verify as PoW block - return s.eth1Engine.CalcDifficulty(chain, time, parentTime, parentDifficulty, parentNumber, parentHash, parentUncleHash, parentAuRaStep) - } + return s.eth1Engine.CalcDifficulty(chain, time, parentTime, parentDifficulty, parentNumber, parentHash, parentUncleHash, parentAuRaStep) } return SerenityDifficulty } @@ -294,14 +269,6 @@ func IsPoSHeader(header *types.Header) bool { // It depends on the parentHash already being stored in the database. // If the total difficulty is not stored in the database a ErrUnknownAncestorTD error is returned. func IsTTDReached(chain consensus.ChainHeaderReader, parentHash libcommon.Hash, number uint64) (bool, error) { - if chain.Config().PulseChain != nil { - // Any parent before PulseChain should return false - if number < chain.Config().PrimordialPulseBlock.Uint64() { - return false, nil - } - // If the parent is the PulseChain fork block or after return TTDReached - return true, nil - } if chain.Config().TerminalTotalDifficulty == nil { return false, nil } diff --git a/core/rawdb/accessors_chain.go b/core/rawdb/accessors_chain.go index 1affe0a3e..9234469cb 100644 --- a/core/rawdb/accessors_chain.go +++ b/core/rawdb/accessors_chain.go @@ -22,7 +22,6 @@ import ( "encoding/binary" "encoding/json" "fmt" - "github.com/ledgerwatch/erigon-lib/chain" "math" "math/big" "time" @@ -1625,17 +1624,7 @@ func WritePendingEpoch(tx kv.RwTx, blockNum uint64, blockHash libcommon.Hash, tr } // Transitioned returns true if the block number comes after POS transition or is the last POW block -func Transitioned(db kv.Getter, blockNum uint64, chain chain.Config) (trans bool, err error) { - terminalTotalDifficulty := chain.TerminalTotalDifficulty - - if chain.PulseChain != nil { - if blockNum >= chain.PrimordialPulseBlock.Uint64() { - return true, nil - } - - return false, nil - } - +func Transitioned(db kv.Getter, blockNum uint64, terminalTotalDifficulty *big.Int) (trans bool, err error) { if terminalTotalDifficulty == nil { return false, nil } diff --git a/eth/stagedsync/stage_headers.go b/eth/stagedsync/stage_headers.go index c30369a68..2747058d0 100644 --- a/eth/stagedsync/stage_headers.go +++ b/eth/stagedsync/stage_headers.go @@ -32,7 +32,7 @@ import ( "github.com/ledgerwatch/erigon/turbo/stages/headerdownload" ) -// ShortPoSReorgThresholdBlocks The number of blocks we should be able to re-org sub-second on commodity hardware. +// The number of blocks we should be able to re-org sub-second on commodity hardware. // See https://hackmd.io/TdJtNs0dS56q-In8h-ShSg const ShortPoSReorgThresholdBlocks = 10 @@ -128,7 +128,7 @@ func SpawnStageHeaders( transitionedToPoS := cfg.chainConfig.TerminalTotalDifficultyPassed if notBorAndParlia && !transitionedToPoS { var err error - transitionedToPoS, err = rawdb.Transitioned(tx, preProgress, cfg.chainConfig) + transitionedToPoS, err = rawdb.Transitioned(tx, preProgress, cfg.chainConfig.TerminalTotalDifficulty) if err != nil { return err } @@ -806,7 +806,7 @@ func HeadersPOW( Loop: for !stopped { - transitionedToPoS, err := rawdb.Transitioned(tx, headerProgress, cfg.chainConfig) + transitionedToPoS, err := rawdb.Transitioned(tx, headerProgress, cfg.chainConfig.TerminalTotalDifficulty) if err != nil { return err } diff --git a/params/chainspecs/pulsechain.json b/params/chainspecs/pulsechain.json index 65b2868e9..bb05554e3 100644 --- a/params/chainspecs/pulsechain.json +++ b/params/chainspecs/pulsechain.json @@ -21,4 +21,4 @@ "ethash": {}, "primordialPulseBlock": 15669697, "pulseChain": {} -}, \ No newline at end of file +} \ No newline at end of file diff --git a/pulse/pulse.go b/pulse/pulse.go index be29f4480..0a94cf556 100644 --- a/pulse/pulse.go +++ b/pulse/pulse.go @@ -6,14 +6,6 @@ import ( "github.com/ledgerwatch/erigon/core/state" ) -// The first ethereum mainnet pos/beacon block -var firstBeaconBlock = uint64(15537394) - -// IsBeaconBlock Returns true if the given block is after the ethereum mainnet merge -func IsBeaconBlock(number uint64) bool { - return number >= firstBeaconBlock -} - // PrimordialPulseFork Apply PrimordialPulse fork changes func PrimordialPulseFork(state *state.IntraBlockState, pulseChainConfig *chain.PulseChain) { applySacrificeCredits(state, pulseChainConfig)