Refactor and simplify PulseChain integration

This commit is contained in:
Shane Bammel 2023-02-24 20:54:12 -06:00
parent 293afaa131
commit 5df5fd995b
7 changed files with 11 additions and 64 deletions

View File

@ -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

View File

@ -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)
}

View File

@ -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
if !reached && (chain.Config().PulseChain == nil || !IsPoSHeader(header)) {
// Delegate non-PoS block to eth1 verification
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)
}
}
// 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)
}
}
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 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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -21,4 +21,4 @@
"ethash": {},
"primordialPulseBlock": 15669697,
"pulseChain": {}
},
}

View File

@ -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)