Skip to content

Commit

Permalink
simulators/ethereum/engine: remove terminal total difficulty (#1186)
Browse files Browse the repository at this point in the history
* simulators/ethereum/engine: WIP removing terminal total difficulty

* simulators/ethereum/engine: upgrade to go-ethereum v1.14.12

* simulators/ethereum/engine: WIP removing ttd

* simulators/ethereum/engine: call InitChain at start of test

* simulators/ethereum/engine: remove ttd in main

* simulators/ethereum/engine: remove configurability of london and merge fork

* simulators/ethereum/engine: fixup

* simulators/ethereum/engine: fixup genesis

* simulators/ethereum/engine: add comment

* simulators/ethereum/engine: change difficulty check

* simulators/ethereum/engine/config/cancun: fix deprecation warning

* simulators/ethereum/engine/config: remove even more config support
  • Loading branch information
fjl authored Jan 3, 2025
1 parent 1e01f7e commit c30dd42
Show file tree
Hide file tree
Showing 34 changed files with 92 additions and 557 deletions.
4 changes: 0 additions & 4 deletions simulators/ethereum/engine/client/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ type EngineClient interface {
GetNextAccountNonce(testCtx context.Context, account common.Address, head *types.Header) (uint64, error)
UpdateNonce(testCtx context.Context, account common.Address, newNonce uint64) error

// TTD Methods
TerminalTotalDifficulty() *big.Int
GetTotalDifficulty(context.Context) (*big.Int, error)

// Test Methods
PostRunVerifications() error

Expand Down
65 changes: 7 additions & 58 deletions simulators/ethereum/engine/client/hive_rpc/hive_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package hive_rpc

import (
"context"
"encoding/json"
"fmt"
"math/big"
"net"
Expand Down Expand Up @@ -30,12 +29,11 @@ import (

type HiveRPCEngineStarter struct {
// Client parameters used to launch the default client
ClientType string
ChainFile string
TerminalTotalDifficulty *big.Int
EnginePort int
EthPort int
JWTSecret []byte
ClientType string
ChainFile string
EnginePort int
EthPort int
JWTSecret []byte
}

var _ client.EngineStarter = (*HiveRPCEngineStarter)(nil)
Expand All @@ -46,7 +44,6 @@ func (s HiveRPCEngineStarter) StartClient(T *hivesim.T, testContext context.Cont
enginePort = s.EnginePort
ethPort = s.EthPort
jwtSecret = s.JWTSecret
ttd = s.TerminalTotalDifficulty
)
if clientType == "" {
cs, err := T.Sim.ClientTypes()
Expand All @@ -70,20 +67,6 @@ func (s HiveRPCEngineStarter) StartClient(T *hivesim.T, testContext context.Cont
if s.ChainFile != "" {
ClientFiles = ClientFiles.Set("/chain.rlp", "./chains/"+s.ChainFile)
}
if ttd == nil {
if ttdStr, ok := ClientParams["HIVE_TERMINAL_TOTAL_DIFFICULTY"]; ok {
// Retrieve TTD from parameters
ttd, ok = new(big.Int).SetString(ttdStr, 10)
if !ok {
return nil, fmt.Errorf("unable to parse TTD from parameters")
}
}
} else {
// Real TTD must be calculated adding the genesis difficulty
ttdInt := helper.CalculateRealTTD(genesis, ttd.Int64())
ClientParams = ClientParams.Set("HIVE_TERMINAL_TOTAL_DIFFICULTY", fmt.Sprintf("%d", ttdInt))
ttd = big.NewInt(ttdInt)
}
if len(bootClients) > 0 {
var (
enodes = make([]string, len(bootClients))
Expand All @@ -108,7 +91,7 @@ func (s HiveRPCEngineStarter) StartClient(T *hivesim.T, testContext context.Cont
if err := CheckEthEngineLive(c); err != nil {
return nil, fmt.Errorf("Engine/Eth ports were never open for client: %v", err)
}
ec := NewHiveRPCEngineClient(c, enginePort, ethPort, jwtSecret, ttd, &helper.LoggingRoundTrip{
ec := NewHiveRPCEngineClient(c, enginePort, ethPort, jwtSecret, &helper.LoggingRoundTrip{
Logger: T,
ID: c.Container,
Inner: http.DefaultTransport,
Expand Down Expand Up @@ -154,7 +137,6 @@ type HiveRPCEngineClient struct {
h *hivesim.Client
c *rpc.Client
cEth *rpc.Client
ttd *big.Int
JWTSecretBytes []byte

// Engine updates info
Expand All @@ -173,7 +155,7 @@ type HiveRPCEngineClient struct {
var _ client.EngineClient = (*HiveRPCEngineClient)(nil)

// NewClient creates a engine client that uses the given RPC client.
func NewHiveRPCEngineClient(h *hivesim.Client, enginePort int, ethPort int, jwtSecretBytes []byte, ttd *big.Int, transport http.RoundTripper) *HiveRPCEngineClient {
func NewHiveRPCEngineClient(h *hivesim.Client, enginePort int, ethPort int, jwtSecretBytes []byte, transport http.RoundTripper) *HiveRPCEngineClient {
// Prepare HTTP Client
httpClient := rpc.WithHTTPClient(&http.Client{Transport: transport})

Expand All @@ -193,7 +175,6 @@ func NewHiveRPCEngineClient(h *hivesim.Client, enginePort int, ethPort int, jwtS
c: engineRpcClient,
Client: eth,
cEth: ethRpcClient,
ttd: ttd,
JWTSecretBytes: jwtSecretBytes,
accTxInfoMap: make(map[common.Address]*AccountTransactionInfo),
}
Expand All @@ -207,10 +188,6 @@ func (ec *HiveRPCEngineClient) EnodeURL() (string, error) {
return ec.h.EnodeURL()
}

func (ec *HiveRPCEngineClient) TerminalTotalDifficulty() *big.Int {
return ec.ttd
}

var (
Head *big.Int // Nil
Pending = big.NewInt(-2)
Expand Down Expand Up @@ -275,34 +252,6 @@ func (ec *HiveRPCEngineClient) HeaderByNumber(ctx context.Context, number *big.I
return header, err
}

// Helper structs to fetch the TotalDifficulty
type TD struct {
TotalDifficulty *hexutil.Big `json:"totalDifficulty"`
}
type TotalDifficultyHeader struct {
types.Header
TD
}

func (tdh *TotalDifficultyHeader) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &tdh.Header); err != nil {
return err
}
if err := json.Unmarshal(data, &tdh.TD); err != nil {
return err
}
return nil
}

func (ec *HiveRPCEngineClient) GetTotalDifficulty(ctx context.Context) (*big.Int, error) {
var td *TotalDifficultyHeader
if err := ec.cEth.CallContext(ctx, &td, "eth_getBlockByNumber", "latest", false); err == nil {
return td.TotalDifficulty.ToInt(), nil
} else {
return nil, err
}
}

func (ec *HiveRPCEngineClient) Close() error {
ec.c.Close()
ec.Client.Close()
Expand Down
95 changes: 20 additions & 75 deletions simulators/ethereum/engine/client/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ import (
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/hive/hivesim"
"github.com/ethereum/hive/simulators/ethereum/engine/client"
"github.com/ethereum/hive/simulators/ethereum/engine/helper"
typ "github.com/ethereum/hive/simulators/ethereum/engine/types"
"github.com/pkg/errors"
)
Expand All @@ -53,8 +51,7 @@ type GethNodeTestConfiguration struct {
}
type GethNodeEngineStarter struct {
// Client parameters used to launch the default client
ChainFile string
TerminalTotalDifficulty *big.Int
ChainFile string

// Test specific configuration
Config GethNodeTestConfiguration
Expand All @@ -69,29 +66,7 @@ func (s GethNodeEngineStarter) StartClient(T *hivesim.T, testContext context.Con
}

func (s GethNodeEngineStarter) StartGethNode(T *hivesim.T, testContext context.Context, genesis *core.Genesis, ClientParams hivesim.Params, ClientFiles hivesim.Params, bootClients ...client.EngineClient) (*GethNode, error) {
var (
ttd = s.TerminalTotalDifficulty
err error
)

if ttd == nil {
if ttdStr, ok := ClientParams["HIVE_TERMINAL_TOTAL_DIFFICULTY"]; ok {
// Retrieve TTD from parameters
ttd, ok = new(big.Int).SetString(ttdStr, 10)
if !ok {
return nil, fmt.Errorf("Unable to parse TTD from parameters")
}
}
} else {
ttd = big.NewInt(helper.CalculateRealTTD(genesis, ttd.Int64()))
ClientParams = ClientParams.Set("HIVE_TERMINAL_TOTAL_DIFFICULTY", fmt.Sprintf("%d", ttd))
}

// Not sure if this hack works
genesisCopy := *genesis
configCopy := *genesisCopy.Config
configCopy.TerminalTotalDifficulty = ttd
genesisCopy.Config = &configCopy
var err error

var enodes []string
if bootClients != nil && len(bootClients) > 0 {
Expand All @@ -116,7 +91,7 @@ func (s GethNodeEngineStarter) StartGethNode(T *hivesim.T, testContext context.C
}
}

g, err := newNode(s.Config, enodes, &genesisCopy)
g, err := newNode(s.Config, enodes, genesis)
if err != nil {
return nil, err
}
Expand All @@ -138,6 +113,7 @@ type AccountTransactionInfo struct {
PreviousBlock common.Hash
PreviousNonce uint64
}

type GethNode struct {
node *node.Node
eth *eth.Ethereum
Expand All @@ -146,7 +122,6 @@ type GethNode struct {

datadir string
genesis *core.Genesis
ttd *big.Int
api *ethcatalyst.ConsensusAPI
running context.Context
closing context.CancelFunc
Expand Down Expand Up @@ -183,7 +158,6 @@ func restart(startConfig GethNodeTestConfiguration, bootnodes []string, datadir
}
config := &node.Config{
Name: startConfig.Name,
Version: params.Version,
DataDir: datadir,
P2P: p2p.Config{
ListenAddr: "0.0.0.0:0",
Expand All @@ -200,22 +174,16 @@ func restart(startConfig GethNodeTestConfiguration, bootnodes []string, datadir
if genesis == nil || genesis.Config == nil {
return nil, fmt.Errorf("genesis configuration is nil")
}
if genesis.Config.TerminalTotalDifficultyPassed == false {
return nil, fmt.Errorf("genesis configuration is has not passed terminal total difficulty")
}
econfig := &ethconfig.Config{
Genesis: genesis,
NetworkId: genesis.Config.ChainID.Uint64(),
SyncMode: downloader.FullSync,
DatabaseCache: 256,
DatabaseHandles: 256,
StateScheme: rawdb.PathScheme,
TxPool: ethconfig.Defaults.TxPool,
GPO: ethconfig.Defaults.GPO,
Miner: ethconfig.Defaults.Miner,
LightServ: 100,
LightPeers: int(startConfig.MaxPeers.Int64()) - 1,
LightNoSyncServe: true,
Genesis: genesis,
NetworkId: genesis.Config.ChainID.Uint64(),
SyncMode: downloader.FullSync,
DatabaseCache: 256,
DatabaseHandles: 256,
StateScheme: rawdb.PathScheme,
TxPool: ethconfig.Defaults.TxPool,
GPO: ethconfig.Defaults.GPO,
Miner: ethconfig.Defaults.Miner,
}
ethBackend, err := eth.New(stack, econfig)
if err != nil {
Expand Down Expand Up @@ -248,7 +216,6 @@ func restart(startConfig GethNodeTestConfiguration, bootnodes []string, datadir
eth: ethBackend,
datadir: datadir,
genesis: genesis,
ttd: genesis.Config.TerminalTotalDifficulty,
api: ethcatalyst.NewConsensusAPI(ethBackend),
accTxInfoMap: make(map[common.Address]*AccountTransactionInfo),
// Test related configuration
Expand Down Expand Up @@ -352,7 +319,7 @@ type validator struct{}
func (v *validator) ValidateBody(block *types.Block) error {
return nil
}
func (v *validator) ValidateState(block *types.Block, state *state.StateDB, receipts types.Receipts, usedGas uint64, stateless bool) error {
func (v *validator) ValidateState(block *types.Block, state *state.StateDB, result *core.ProcessResult, stateless bool) error {
return nil
}
func (v *validator) ValidateWitness(witness *stateless.Witness, receiptRoot common.Hash, stateRoot common.Hash) error {
Expand All @@ -361,8 +328,8 @@ func (v *validator) ValidateWitness(witness *stateless.Witness, receiptRoot comm

type processor struct{}

func (p *processor) Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (types.Receipts, []*types.Log, uint64, error) {
return types.Receipts{}, []*types.Log{}, 21000, nil
func (p *processor) Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (*core.ProcessResult, error) {
return &core.ProcessResult{GasUsed: 21000}, nil
}

var headerPrefix = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header
Expand Down Expand Up @@ -396,17 +363,17 @@ func (n *GethNode) SetBlock(block *types.Block, parentNumber uint64, parentRoot
bc := n.eth.BlockChain()
bc.SetBlockValidatorAndProcessorForTesting(new(validator), n.eth.BlockChain().Processor())

statedb, err := state.New(parentRoot, bc.StateCache(), bc.Snapshots())
statedb, err := state.New(parentRoot, bc.StateCache())
if err != nil {
return errors.Wrap(err, "failed to create state db")
}
statedb.StartPrefetcher("chain", nil)
var failedProcessing bool
receipts, _, _, err := n.eth.BlockChain().Processor().Process(block, statedb, *n.eth.BlockChain().GetVMConfig())
result, err := n.eth.BlockChain().Processor().Process(block, statedb, *n.eth.BlockChain().GetVMConfig())
if err != nil {
failedProcessing = true
}
rawdb.WriteReceipts(db, block.Hash(), block.NumberU64(), receipts)
rawdb.WriteReceipts(db, block.Hash(), block.NumberU64(), result.Receipts)
root, err := statedb.Commit(block.NumberU64(), false)
if err != nil {
return errors.Wrap(err, "failed to commit state")
Expand Down Expand Up @@ -681,7 +648,7 @@ func (n *GethNode) getStateDB(ctx context.Context, blockNumber *big.Int) (*state
if err != nil {
return nil, err
}
return state.New(b.Root(), n.eth.BlockChain().StateCache(), n.eth.BlockChain().Snapshots())
return state.New(b.Root(), n.eth.BlockChain().StateCache())
}

func (n *GethNode) BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error) {
Expand Down Expand Up @@ -724,28 +691,6 @@ func (n *GethNode) PendingTransactionCount(ctx context.Context) (uint, error) {
panic("NOT IMPLEMENTED")
}

func (n *GethNode) GetBlockTotalDifficulty(ctx context.Context, hash common.Hash) (*big.Int, error) {
block := n.eth.BlockChain().GetBlockByHash(hash)
if block == nil {
return big.NewInt(0), nil
}
return n.eth.BlockChain().GetTd(hash, block.NumberU64()), nil
}

func (n *GethNode) GetTotalDifficulty(ctx context.Context) (*big.Int, error) {
if n.mustHeadBlock != nil {
return n.GetBlockTotalDifficulty(ctx, n.mustHeadBlock.Hash())
}
return n.GetBlockTotalDifficulty(ctx, n.eth.BlockChain().CurrentHeader().Hash())
}

func (n *GethNode) TerminalTotalDifficulty() *big.Int {
if n.ttd != nil {
return n.ttd
}
return n.genesis.Config.TerminalTotalDifficulty
}

func (n *GethNode) EnodeURL() (string, error) {
return n.node.Server().NodeInfo().Enode, nil
}
Expand Down
Loading

0 comments on commit c30dd42

Please sign in to comment.