all: get rid of beacon.faker test consensus type

This commit is contained in:
Péter Szilágyi 2024-11-12 01:38:23 +07:00 committed by Gary Rong
parent 125b68a797
commit 27766d0a99
7 changed files with 17 additions and 61 deletions

View file

@ -1,41 +0,0 @@
// Copyright 2023 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/>.
package beacon
import (
"math/big"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/core/types"
)
// NewFaker creates a fake consensus engine for testing.
// The fake engine simulates a merged network.
// It can not be used to test the merge transition.
// This type is needed since the fakeChainReader can not be used with
// a normal beacon consensus engine.
func NewFaker() consensus.Engine {
return new(faker)
}
type faker struct {
Beacon
}
func (f *faker) CalcDifficulty(chain consensus.ChainHeaderReader, time uint64, parent *types.Header) *big.Int {
return beaconDifficulty
}

View file

@ -3987,7 +3987,7 @@ func TestEIP3651(t *testing.T) {
var (
aa = common.HexToAddress("0x000000000000000000000000000000000000aaaa")
bb = common.HexToAddress("0x000000000000000000000000000000000000bbbb")
engine = beacon.NewFaker()
engine = beacon.New(ethash.NewFaker())
// A sender who makes transactions, has some funds
key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
@ -4103,7 +4103,7 @@ func TestPragueRequests(t *testing.T) {
addr1 = crypto.PubkeyToAddress(key1.PublicKey)
config = *params.MergedTestChainConfig
signer = types.LatestSigner(&config)
engine = beacon.NewFaker()
engine = beacon.New(ethash.NewFaker())
)
gspec := &Genesis{
Config: &config,

View file

@ -80,8 +80,9 @@ func TestGeneratePOSChain(t *testing.T) {
Code: common.Hex2Bytes("600154600354"),
}
genesis := gspec.MustCommit(gendb, triedb.NewDatabase(gendb, triedb.HashDefaults))
engine := beacon.New(ethash.NewFaker())
genchain, genreceipts := GenerateChain(gspec.Config, genesis, beacon.NewFaker(), gendb, 4, func(i int, gen *BlockGen) {
genchain, genreceipts := GenerateChain(gspec.Config, genesis, engine, gendb, 4, func(i int, gen *BlockGen) {
gen.SetParentBeaconRoot(common.Hash{byte(i + 1)})
// Add value transfer tx.
@ -122,7 +123,7 @@ func TestGeneratePOSChain(t *testing.T) {
})
// Import the chain. This runs all block validation rules.
blockchain, _ := NewBlockChain(db, nil, gspec, nil, beacon.NewFaker(), vm.Config{}, nil)
blockchain, _ := NewBlockChain(db, nil, gspec, nil, engine, vm.Config{}, nil)
defer blockchain.Stop()
if i, err := blockchain.InsertChain(genchain); err != nil {

View file

@ -32,8 +32,7 @@ import (
"github.com/ethereum/go-ethereum/beacon/engine"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/consensus"
beaconConsensus "github.com/ethereum/go-ethereum/consensus/beacon"
"github.com/ethereum/go-ethereum/consensus/beacon"
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
@ -62,12 +61,11 @@ var (
func generateMergeChain(n int, merged bool) (*core.Genesis, []*types.Block) {
config := *params.AllEthashProtocolChanges
engine := consensus.Engine(beaconConsensus.New(ethash.NewFaker()))
engine := beacon.New(ethash.NewFaker())
if merged {
config.TerminalTotalDifficulty = common.Big0
engine = beaconConsensus.NewFaker()
} else {
engine.(*beaconConsensus.Beacon).TestingTTDBlock(uint64(n))
engine.TestingTTDBlock(uint64(n))
}
genesis := &core.Genesis{
Config: &config,

View file

@ -25,7 +25,6 @@ import (
"time"
"github.com/ethereum/go-ethereum/common"
"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/core"
@ -71,12 +70,13 @@ func newTestBackend(blocks int) *testBackend {
func newTestBackendWithGenerator(blocks int, shanghai bool, generator func(int, *core.BlockGen)) *testBackend {
var (
// Create a database pre-initialize with a genesis block
db = rawdb.NewMemoryDatabase()
config = params.TestChainConfig
engine consensus.Engine = ethash.NewFaker()
db = rawdb.NewMemoryDatabase()
config = params.TestChainConfig
engine = beacon.New(ethash.NewFaker())
)
if shanghai {
if !shanghai {
engine.TestingTTDBlock(math.MaxUint64)
} else {
config = &params.ChainConfig{
ChainID: big.NewInt(1),
HomesteadBlock: big.NewInt(0),
@ -99,7 +99,6 @@ func newTestBackendWithGenerator(blocks int, shanghai bool, generator func(int,
TerminalTotalDifficulty: big.NewInt(0),
Ethash: new(params.EthashConfig),
}
engine = beacon.NewFaker()
}
gspec := &core.Genesis{

View file

@ -1089,7 +1089,7 @@ func TestTraceChain(t *testing.T) {
func newTestMergedBackend(t *testing.T, n int, gspec *core.Genesis, generator func(i int, b *core.BlockGen)) *testBackend {
backend := &testBackend{
chainConfig: gspec.Config,
engine: beacon.NewFaker(),
engine: beacon.New(ethash.NewFaker()),
chaindb: rawdb.NewMemoryDatabase(),
}
// Generate blocks for testing

View file

@ -29,7 +29,6 @@ import (
"time"
"github.com/ethereum/go-ethereum/common"
"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/core"
@ -457,9 +456,8 @@ func newGQLService(t *testing.T, stack *node.Node, shanghai bool, gspec *core.Ge
RPCGasCap: 1000000,
StateScheme: rawdb.HashScheme,
}
var engine consensus.Engine = ethash.NewFaker()
var engine = beacon.New(ethash.NewFaker())
if shanghai {
engine = beacon.NewFaker()
gspec.Config.TerminalTotalDifficulty = common.Big0
// GenerateChain will increment timestamps by 10.
// Shanghai upgrade at block 1.
@ -468,6 +466,7 @@ func newGQLService(t *testing.T, stack *node.Node, shanghai bool, gspec *core.Ge
} 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)
if err != nil {