mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 17:33:47 +00:00
consensus/beacon: remove TestingTTDBlock
This commit is contained in:
parent
4cda8f06ea
commit
0e4c13a62d
6 changed files with 16 additions and 49 deletions
|
|
@ -61,8 +61,7 @@ var (
|
||||||
// is only used for necessary consensus checks. The legacy consensus engine can be any
|
// is only used for necessary consensus checks. The legacy consensus engine can be any
|
||||||
// engine implements the consensus interface (except the beacon itself).
|
// engine implements the consensus interface (except the beacon itself).
|
||||||
type Beacon struct {
|
type Beacon struct {
|
||||||
ethone consensus.Engine // Original consensus engine used in eth1, e.g. ethash or clique
|
ethone consensus.Engine // Original consensus engine used in eth1, e.g. ethash or clique
|
||||||
ttdblock *uint64 // Merge block-number for testchain generation without TTDs
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a consensus engine with the given embedded eth1 engine.
|
// New creates a consensus engine with the given embedded eth1 engine.
|
||||||
|
|
@ -73,16 +72,9 @@ func New(ethone consensus.Engine) *Beacon {
|
||||||
return &Beacon{ethone: ethone}
|
return &Beacon{ethone: ethone}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestingTTDBlock is a replacement mechanism for TTD-based pre-/post-merge
|
func isPreMerge(config *params.ChainConfig, block uint64) bool {
|
||||||
// splitting. With chain history deletion, TD calculations become impossible.
|
return !(config.TerminalTotalDifficulty != nil && config.TerminalTotalDifficulty.Sign() == 0) ||
|
||||||
// This is fine for progressing the live chain, but to be able to generate test
|
(config.MergeNetsplitBlock != nil && block < config.MergeNetsplitBlock.Uint64())
|
||||||
// chains, we do need a split point. This method supports setting an explicit
|
|
||||||
// block number to use as the splitter *for testing*, instead of having to keep
|
|
||||||
// the notion of TDs in the client just for testing.
|
|
||||||
//
|
|
||||||
// The block with supplied number is regarded as the last pre-merge block.
|
|
||||||
func (beacon *Beacon) TestingTTDBlock(number uint64) {
|
|
||||||
beacon.ttdblock = &number
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Author implements consensus.Engine, returning the verified author of the block.
|
// Author implements consensus.Engine, returning the verified author of the block.
|
||||||
|
|
@ -332,15 +324,7 @@ func (beacon *Beacon) verifyHeaders(chain consensus.ChainHeaderReader, headers [
|
||||||
// Prepare implements consensus.Engine, initializing the difficulty field of a
|
// Prepare implements consensus.Engine, initializing the difficulty field of a
|
||||||
// header to conform to the beacon protocol. The changes are done inline.
|
// header to conform to the beacon protocol. The changes are done inline.
|
||||||
func (beacon *Beacon) Prepare(chain consensus.ChainHeaderReader, header *types.Header) error {
|
func (beacon *Beacon) Prepare(chain consensus.ChainHeaderReader, header *types.Header) error {
|
||||||
// The beacon engine requires access to total difficulties to be able to
|
if isPreMerge(chain.Config(), header.Number.Uint64()) {
|
||||||
// seal pre-merge and post-merge blocks. With the transition to removing
|
|
||||||
// old blocks, TDs become unaccessible, thus making TTD based pre-/post-
|
|
||||||
// merge decisions impossible.
|
|
||||||
//
|
|
||||||
// We do not need to seal non-merge blocks anymore live, but we do need
|
|
||||||
// to be able to generate test chains, thus we're reverting to a testing-
|
|
||||||
// settable field to direct that.
|
|
||||||
if beacon.ttdblock != nil && *beacon.ttdblock >= header.Number.Uint64() {
|
|
||||||
return beacon.ethone.Prepare(chain, header)
|
return beacon.ethone.Prepare(chain, header)
|
||||||
}
|
}
|
||||||
header.Difficulty = beaconDifficulty
|
header.Difficulty = beaconDifficulty
|
||||||
|
|
@ -450,15 +434,7 @@ func (beacon *Beacon) SealHash(header *types.Header) common.Hash {
|
||||||
// the difficulty that a new block should have when created at time
|
// the difficulty that a new block should have when created at time
|
||||||
// given the parent block's time and difficulty.
|
// given the parent block's time and difficulty.
|
||||||
func (beacon *Beacon) CalcDifficulty(chain consensus.ChainHeaderReader, time uint64, parent *types.Header) *big.Int {
|
func (beacon *Beacon) CalcDifficulty(chain consensus.ChainHeaderReader, time uint64, parent *types.Header) *big.Int {
|
||||||
// The beacon engine requires access to total difficulties to be able to
|
if isPreMerge(chain.Config(), parent.Number.Uint64()+1) {
|
||||||
// seal pre-merge and post-merge blocks. With the transition to removing
|
|
||||||
// old blocks, TDs become unaccessible, thus making TTD based pre-/post-
|
|
||||||
// merge decisions impossible.
|
|
||||||
//
|
|
||||||
// We do not need to seal non-merge blocks anymore live, but we do need
|
|
||||||
// to be able to generate test chains, thus we're reverting to a testing-
|
|
||||||
// settable field to direct that.
|
|
||||||
if beacon.ttdblock != nil && *beacon.ttdblock > parent.Number.Uint64() {
|
|
||||||
return beacon.ethone.CalcDifficulty(chain, time, parent)
|
return beacon.ethone.CalcDifficulty(chain, time, parent)
|
||||||
}
|
}
|
||||||
return beaconDifficulty
|
return beaconDifficulty
|
||||||
|
|
|
||||||
|
|
@ -64,9 +64,9 @@ func generateMergeChain(n int, merged bool) (*core.Genesis, []*types.Block) {
|
||||||
engine := beacon.New(ethash.NewFaker())
|
engine := beacon.New(ethash.NewFaker())
|
||||||
if merged {
|
if merged {
|
||||||
config.TerminalTotalDifficulty = common.Big0
|
config.TerminalTotalDifficulty = common.Big0
|
||||||
} else {
|
config.MergeNetsplitBlock = common.Big0
|
||||||
engine.TestingTTDBlock(uint64(n))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
genesis := &core.Genesis{
|
genesis := &core.Genesis{
|
||||||
Config: &config,
|
Config: &config,
|
||||||
Alloc: types.GenesisAlloc{
|
Alloc: types.GenesisAlloc{
|
||||||
|
|
|
||||||
|
|
@ -147,11 +147,11 @@ func newTestBackend(t *testing.T, londonBlock *big.Int, cancunBlock *big.Int, pe
|
||||||
config.LondonBlock = londonBlock
|
config.LondonBlock = londonBlock
|
||||||
config.ArrowGlacierBlock = londonBlock
|
config.ArrowGlacierBlock = londonBlock
|
||||||
config.GrayGlacierBlock = londonBlock
|
config.GrayGlacierBlock = londonBlock
|
||||||
|
if cancunBlock != nil {
|
||||||
|
// Enable the merge with cancun fork.
|
||||||
|
config.MergeNetsplitBlock = cancunBlock
|
||||||
|
}
|
||||||
engine := beacon.New(ethash.NewFaker())
|
engine := beacon.New(ethash.NewFaker())
|
||||||
engine.TestingTTDBlock(testHead + 1)
|
|
||||||
|
|
||||||
td := params.GenesisDifficulty.Uint64()
|
|
||||||
|
|
||||||
if cancunBlock != nil {
|
if cancunBlock != nil {
|
||||||
ts := gspec.Timestamp + cancunBlock.Uint64()*10 // fixed 10 sec block time in blockgen
|
ts := gspec.Timestamp + cancunBlock.Uint64()*10 // fixed 10 sec block time in blockgen
|
||||||
|
|
@ -209,10 +209,9 @@ func newTestBackend(t *testing.T, londonBlock *big.Int, cancunBlock *big.Int, pe
|
||||||
b.AddTx(types.MustSignNewTx(key, signer, blobTx))
|
b.AddTx(types.MustSignNewTx(key, signer, blobTx))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
td += b.Difficulty().Uint64()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Construct testing chain
|
// Construct testing chain
|
||||||
gspec.Config.TerminalTotalDifficulty = new(big.Int).SetUint64(td)
|
|
||||||
chain, err := core.NewBlockChain(db, &core.CacheConfig{TrieCleanNoPrefetch: true}, gspec, nil, engine, vm.Config{}, nil)
|
chain, err := core.NewBlockChain(db, &core.CacheConfig{TrieCleanNoPrefetch: true}, gspec, nil, engine, vm.Config{}, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to create local chain, %v", err)
|
t.Fatalf("Failed to create local chain, %v", err)
|
||||||
|
|
|
||||||
|
|
@ -74,9 +74,7 @@ func newTestBackendWithGenerator(blocks int, shanghai bool, generator func(int,
|
||||||
config = params.TestChainConfig
|
config = params.TestChainConfig
|
||||||
engine = beacon.New(ethash.NewFaker())
|
engine = beacon.New(ethash.NewFaker())
|
||||||
)
|
)
|
||||||
if !shanghai {
|
if shanghai {
|
||||||
engine.TestingTTDBlock(math.MaxUint64)
|
|
||||||
} else {
|
|
||||||
config = ¶ms.ChainConfig{
|
config = ¶ms.ChainConfig{
|
||||||
ChainID: big.NewInt(1),
|
ChainID: big.NewInt(1),
|
||||||
HomesteadBlock: big.NewInt(0),
|
HomesteadBlock: big.NewInt(0),
|
||||||
|
|
|
||||||
|
|
@ -75,8 +75,6 @@ func TestSupplyOmittedFields(t *testing.T) {
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
gspec.Config.TerminalTotalDifficulty = big.NewInt(0)
|
|
||||||
|
|
||||||
out, _, err := testSupplyTracer(t, gspec, func(b *core.BlockGen) {
|
out, _, err := testSupplyTracer(t, gspec, func(b *core.BlockGen) {
|
||||||
b.SetPoS()
|
b.SetPoS()
|
||||||
})
|
})
|
||||||
|
|
@ -546,7 +544,6 @@ func TestSupplySelfdestructItselfAndRevert(t *testing.T) {
|
||||||
|
|
||||||
func testSupplyTracer(t *testing.T, genesis *core.Genesis, gen func(*core.BlockGen)) ([]supplyInfo, *core.BlockChain, error) {
|
func testSupplyTracer(t *testing.T, genesis *core.Genesis, gen func(*core.BlockGen)) ([]supplyInfo, *core.BlockChain, error) {
|
||||||
engine := beacon.New(ethash.NewFaker())
|
engine := beacon.New(ethash.NewFaker())
|
||||||
engine.TestingTTDBlock(1)
|
|
||||||
|
|
||||||
traceOutputPath := filepath.ToSlash(t.TempDir())
|
traceOutputPath := filepath.ToSlash(t.TempDir())
|
||||||
traceOutputFilename := path.Join(traceOutputPath, "supply.jsonl")
|
traceOutputFilename := path.Join(traceOutputPath, "supply.jsonl")
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,6 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math"
|
|
||||||
"math/big"
|
"math/big"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -459,15 +458,13 @@ func newGQLService(t *testing.T, stack *node.Node, shanghai bool, gspec *core.Ge
|
||||||
var engine = beacon.New(ethash.NewFaker())
|
var engine = beacon.New(ethash.NewFaker())
|
||||||
if shanghai {
|
if shanghai {
|
||||||
gspec.Config.TerminalTotalDifficulty = common.Big0
|
gspec.Config.TerminalTotalDifficulty = common.Big0
|
||||||
|
gspec.Config.MergeNetsplitBlock = common.Big0
|
||||||
// GenerateChain will increment timestamps by 10.
|
// GenerateChain will increment timestamps by 10.
|
||||||
// Shanghai upgrade at block 1.
|
// Shanghai upgrade at block 1.
|
||||||
shanghaiTime := uint64(5)
|
shanghaiTime := uint64(5)
|
||||||
gspec.Config.ShanghaiTime = &shanghaiTime
|
gspec.Config.ShanghaiTime = &shanghaiTime
|
||||||
} else {
|
|
||||||
// set an arbitrary large ttd as chains are required to be known to be merged
|
|
||||||
gspec.Config.TerminalTotalDifficulty = big.NewInt(math.MaxInt64)
|
|
||||||
engine.TestingTTDBlock(math.MaxUint64)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ethBackend, err := eth.New(stack, ethConf)
|
ethBackend, err := eth.New(stack, ethConf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("could not create eth backend: %v", err)
|
t.Fatalf("could not create eth backend: %v", err)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue