From 29e0a6f4042d19f507c6a11c8a468bda3c332f04 Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Mon, 20 Apr 2026 15:33:29 +0800 Subject: [PATCH 01/80] core/vm, eth, tests: introduce gas budget (#34712) This PR introduces a gasBudget struct to track the available gas for EVM execution. With the upcoming EIP-8037, multi-dimensional gas accounting will be introduced, requiring multiple gas budget counters to be tracked simultaneously. To support this, the counters are grouped into a gasBudget structure. This change is a prerequisite for internal refactoring in preparation for EIP-8037. --------- Co-authored-by: MariusVanDerWijden --- cmd/evm/internal/t8ntool/transaction.go | 8 +- core/bench_test.go | 4 +- core/bintrie_witness_test.go | 4 +- core/state_processor.go | 6 +- core/state_transition.go | 69 ++++++++------- core/tracing/hooks.go | 2 + core/txpool/validation.go | 4 +- core/vm/contract.go | 24 +++--- core/vm/contracts.go | 16 ++-- core/vm/contracts_fuzz_test.go | 2 +- core/vm/contracts_test.go | 8 +- core/vm/eips.go | 6 +- core/vm/evm.go | 110 ++++++++++++------------ core/vm/gas_table_test.go | 12 +-- core/vm/gascosts.go | 63 +++++++++++++- core/vm/instructions.go | 16 ++-- core/vm/instructions_test.go | 2 +- core/vm/interpreter.go | 2 +- core/vm/interpreter_test.go | 4 +- core/vm/operations_acl.go | 6 +- core/vm/runtime/runtime.go | 16 ++-- eth/tracers/js/tracer_test.go | 6 +- eth/tracers/logger/logger_test.go | 2 +- tests/state_test.go | 8 +- tests/transaction_test_util.go | 3 +- 25 files changed, 235 insertions(+), 168 deletions(-) diff --git a/cmd/evm/internal/t8ntool/transaction.go b/cmd/evm/internal/t8ntool/transaction.go index 3a457eeaec..82c0f30b42 100644 --- a/cmd/evm/internal/t8ntool/transaction.go +++ b/cmd/evm/internal/t8ntool/transaction.go @@ -133,15 +133,15 @@ func Transaction(ctx *cli.Context) error { } // Check intrinsic gas rules := chainConfig.Rules(common.Big0, true, 0) - gas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.SetCodeAuthorizations(), tx.To() == nil, rules.IsHomestead, rules.IsIstanbul, rules.IsShanghai) + cost, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.SetCodeAuthorizations(), tx.To() == nil, rules.IsHomestead, rules.IsIstanbul, rules.IsShanghai) if err != nil { r.Error = err results = append(results, r) continue } - r.IntrinsicGas = gas - if tx.Gas() < gas { - r.Error = fmt.Errorf("%w: have %d, want %d", core.ErrIntrinsicGas, tx.Gas(), gas) + r.IntrinsicGas = cost.RegularGas + if tx.Gas() < cost.RegularGas { + r.Error = fmt.Errorf("%w: have %d, want %d", core.ErrIntrinsicGas, tx.Gas(), cost.RegularGas) results = append(results, r) continue } diff --git a/core/bench_test.go b/core/bench_test.go index 932188f8e2..20d1a7794b 100644 --- a/core/bench_test.go +++ b/core/bench_test.go @@ -89,7 +89,7 @@ func genValueTx(nbytes int) func(int, *BlockGen) { data := make([]byte, nbytes) return func(i int, gen *BlockGen) { toaddr := common.Address{} - gas, _ := IntrinsicGas(data, nil, nil, false, false, false, false) + cost, _ := IntrinsicGas(data, nil, nil, false, false, false, false) signer := gen.Signer() gasPrice := big.NewInt(0) if gen.header.BaseFee != nil { @@ -99,7 +99,7 @@ func genValueTx(nbytes int) func(int, *BlockGen) { Nonce: gen.TxNonce(benchRootAddr), To: &toaddr, Value: big.NewInt(1), - Gas: gas, + Gas: cost.RegularGas, Data: data, GasPrice: gasPrice, }) diff --git a/core/bintrie_witness_test.go b/core/bintrie_witness_test.go index e4cb34cb56..1b033151d3 100644 --- a/core/bintrie_witness_test.go +++ b/core/bintrie_witness_test.go @@ -97,11 +97,11 @@ func TestProcessUBT(t *testing.T) { txCost1 := params.TxGas txCost2 := params.TxGas - contractCreationCost := intrinsicContractCreationGas + + contractCreationCost := intrinsicContractCreationGas.RegularGas + params.WitnessChunkReadCost + params.WitnessChunkWriteCost + params.WitnessBranchReadCost + params.WitnessBranchWriteCost + /* creation */ params.WitnessChunkReadCost + params.WitnessChunkWriteCost + /* creation with value */ 739 /* execution costs */ - codeWithExtCodeCopyGas := intrinsicCodeWithExtCodeCopyGas + + codeWithExtCodeCopyGas := intrinsicCodeWithExtCodeCopyGas.RegularGas + params.WitnessChunkReadCost + params.WitnessChunkWriteCost + params.WitnessBranchReadCost + params.WitnessBranchWriteCost + /* creation (tx) */ params.WitnessChunkReadCost + params.WitnessChunkWriteCost + params.WitnessBranchReadCost + params.WitnessBranchWriteCost + /* creation (CREATE at pc=0x20) */ params.WitnessChunkReadCost + params.WitnessChunkWriteCost + /* write code hash */ diff --git a/core/state_processor.go b/core/state_processor.go index 0a324379f9..9cb7cc73bc 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -259,7 +259,7 @@ func ProcessBeaconBlockRoot(beaconRoot common.Hash, evm *vm.EVM) { } evm.SetTxContext(NewEVMTxContext(msg)) evm.StateDB.AddAddressToAccessList(params.BeaconRootsAddress) - _, _, _ = evm.Call(msg.From, *msg.To, msg.Data, 30_000_000, common.U2560) + _, _, _ = evm.Call(msg.From, *msg.To, msg.Data, vm.NewGasBudget(30_000_000), common.U2560) if evm.StateDB.AccessEvents() != nil { evm.StateDB.AccessEvents().Merge(evm.AccessEvents) } @@ -286,7 +286,7 @@ func ProcessParentBlockHash(prevHash common.Hash, evm *vm.EVM) { } evm.SetTxContext(NewEVMTxContext(msg)) evm.StateDB.AddAddressToAccessList(params.HistoryStorageAddress) - _, _, err := evm.Call(msg.From, *msg.To, msg.Data, 30_000_000, common.U2560) + _, _, err := evm.Call(msg.From, *msg.To, msg.Data, vm.NewGasBudget(30_000_000), common.U2560) if err != nil { panic(err) } @@ -325,7 +325,7 @@ func processRequestsSystemCall(requests *[][]byte, evm *vm.EVM, requestType byte } evm.SetTxContext(NewEVMTxContext(msg)) evm.StateDB.AddAddressToAccessList(addr) - ret, _, err := evm.Call(msg.From, *msg.To, msg.Data, 30_000_000, common.U2560) + ret, _, err := evm.Call(msg.From, *msg.To, msg.Data, vm.NewGasBudget(30_000_000), common.U2560) if evm.StateDB.AccessEvents() != nil { evm.StateDB.AccessEvents().Merge(evm.AccessEvents) } diff --git a/core/state_transition.go b/core/state_transition.go index bd7e5daeff..297e8580ee 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -68,7 +68,7 @@ func (result *ExecutionResult) Revert() []byte { } // IntrinsicGas computes the 'intrinsic gas' for a message with the given data. -func IntrinsicGas(data []byte, accessList types.AccessList, authList []types.SetCodeAuthorization, isContractCreation, isHomestead, isEIP2028, isEIP3860 bool) (uint64, error) { +func IntrinsicGas(data []byte, accessList types.AccessList, authList []types.SetCodeAuthorization, isContractCreation, isHomestead, isEIP2028, isEIP3860 bool) (vm.GasCosts, error) { // Set the starting gas for the raw transaction var gas uint64 if isContractCreation && isHomestead { @@ -89,19 +89,19 @@ func IntrinsicGas(data []byte, accessList types.AccessList, authList []types.Set nonZeroGas = params.TxDataNonZeroGasEIP2028 } if (math.MaxUint64-gas)/nonZeroGas < nz { - return 0, ErrGasUintOverflow + return vm.GasCosts{}, ErrGasUintOverflow } gas += nz * nonZeroGas if (math.MaxUint64-gas)/params.TxDataZeroGas < z { - return 0, ErrGasUintOverflow + return vm.GasCosts{}, ErrGasUintOverflow } gas += z * params.TxDataZeroGas if isContractCreation && isEIP3860 { lenWords := toWordSize(dataLen) if (math.MaxUint64-gas)/params.InitCodeWordGas < lenWords { - return 0, ErrGasUintOverflow + return vm.GasCosts{}, ErrGasUintOverflow } gas += lenWords * params.InitCodeWordGas } @@ -113,7 +113,7 @@ func IntrinsicGas(data []byte, accessList types.AccessList, authList []types.Set if authList != nil { gas += uint64(len(authList)) * params.CallNewAccountGas } - return gas, nil + return vm.GasCosts{RegularGas: gas}, nil } // FloorDataGas computes the minimum gas required for a transaction based on its data tokens (EIP-7623). @@ -242,12 +242,12 @@ func ApplyMessage(evm *vm.EVM, msg *Message, gp *GasPool) (*ExecutionResult, err // 5. Run Script section // 6. Derive new state root type stateTransition struct { - gp *GasPool - msg *Message - gasRemaining uint64 - initialGas uint64 - state vm.StateDB - evm *vm.EVM + gp *GasPool + msg *Message + initialBudget vm.GasBudget + gasRemaining vm.GasBudget + state vm.StateDB + evm *vm.EVM } // newStateTransition initialises and returns a new state transition object. @@ -304,8 +304,8 @@ func (st *stateTransition) buyGas() error { if st.evm.Config.Tracer != nil && st.evm.Config.Tracer.OnGasChange != nil { st.evm.Config.Tracer.OnGasChange(0, st.msg.GasLimit, tracing.GasChangeTxInitialBalance) } - st.gasRemaining = st.msg.GasLimit - st.initialGas = st.msg.GasLimit + st.gasRemaining = vm.NewGasBudget(st.msg.GasLimit) + st.initialBudget = st.gasRemaining.Copy() mgvalU256, _ := uint256.FromBig(mgval) st.state.SubBalance(st.msg.From, mgvalU256, tracing.BalanceDecreaseGasBuy) @@ -446,14 +446,17 @@ func (st *stateTransition) execute() (*ExecutionResult, error) { contractCreation = msg.To == nil floorDataGas uint64 ) - // Check clauses 4-5, subtract intrinsic gas if everything is correct - gas, err := IntrinsicGas(msg.Data, msg.AccessList, msg.SetCodeAuthorizations, contractCreation, rules.IsHomestead, rules.IsIstanbul, rules.IsShanghai) + cost, err := IntrinsicGas(msg.Data, msg.AccessList, msg.SetCodeAuthorizations, contractCreation, rules.IsHomestead, rules.IsIstanbul, rules.IsShanghai) if err != nil { return nil, err } - if st.gasRemaining < gas { - return nil, fmt.Errorf("%w: have %d, want %d", ErrIntrinsicGas, st.gasRemaining, gas) + prior, sufficient := st.gasRemaining.Charge(cost) + if !sufficient { + return nil, fmt.Errorf("%w: have %d, want %d", ErrIntrinsicGas, st.gasRemaining.RegularGas, cost.RegularGas) + } + if t := st.evm.Config.Tracer; t != nil && t.OnGasChange != nil { + t.OnGasChange(prior, st.gasRemaining.RegularGas, tracing.GasChangeTxIntrinsicGas) } // Gas limit suffices for the floor data cost (EIP-7623) if rules.IsPrague { @@ -465,10 +468,6 @@ func (st *stateTransition) execute() (*ExecutionResult, error) { return nil, fmt.Errorf("%w: have %d, want %d", ErrFloorDataGas, msg.GasLimit, floorDataGas) } } - if t := st.evm.Config.Tracer; t != nil && t.OnGasChange != nil { - t.OnGasChange(st.gasRemaining, st.gasRemaining-gas, tracing.GasChangeTxIntrinsicGas) - } - st.gasRemaining -= gas if rules.IsEIP4762 { st.evm.AccessEvents.AddTxOrigin(msg.From) @@ -535,14 +534,14 @@ func (st *stateTransition) execute() (*ExecutionResult, error) { peakGasUsed := st.gasUsed() // Compute refund counter, capped to a refund quotient. - st.gasRemaining += st.calcRefund() + st.gasRemaining.Refund(st.calcRefund()) + if rules.IsPrague { // After EIP-7623: Data-heavy transactions pay the floor gas. - if st.gasUsed() < floorDataGas { - prev := st.gasRemaining - st.gasRemaining = st.initialGas - floorDataGas + if used := st.gasUsed(); used < floorDataGas { + prior, _ := st.gasRemaining.Charge(vm.GasCosts{RegularGas: floorDataGas - used}) if t := st.evm.Config.Tracer; t != nil && t.OnGasChange != nil { - t.OnGasChange(prev, st.gasRemaining, tracing.GasChangeTxDataFloor) + t.OnGasChange(prior, st.gasRemaining.RegularGas, tracing.GasChangeTxDataFloor) } } if peakGasUsed < floorDataGas { @@ -555,10 +554,10 @@ func (st *stateTransition) execute() (*ExecutionResult, error) { // Return gas to the gas pool if rules.IsAmsterdam { // Refund is excluded for returning - err = st.gp.ReturnGas(st.initialGas-peakGasUsed, st.gasUsed()) + err = st.gp.ReturnGas(st.initialBudget.RegularGas-peakGasUsed, st.gasUsed()) } else { // Refund is included for returning - err = st.gp.ReturnGas(st.gasRemaining, st.gasUsed()) + err = st.gp.ReturnGas(st.gasRemaining.RegularGas, st.gasUsed()) } if err != nil { return nil, err @@ -655,7 +654,7 @@ func (st *stateTransition) applyAuthorization(auth *types.SetCodeAuthorization) } // calcRefund computes refund counter, capped to a refund quotient. -func (st *stateTransition) calcRefund() uint64 { +func (st *stateTransition) calcRefund() vm.GasBudget { var refund uint64 if !st.evm.ChainConfig().IsLondon(st.evm.Context.BlockNumber) { // Before EIP-3529: refunds were capped to gasUsed / 2 @@ -668,26 +667,26 @@ func (st *stateTransition) calcRefund() uint64 { refund = st.state.GetRefund() } if st.evm.Config.Tracer != nil && st.evm.Config.Tracer.OnGasChange != nil && refund > 0 { - st.evm.Config.Tracer.OnGasChange(st.gasRemaining, st.gasRemaining+refund, tracing.GasChangeTxRefunds) + st.evm.Config.Tracer.OnGasChange(st.gasRemaining.RegularGas, st.gasRemaining.RegularGas+refund, tracing.GasChangeTxRefunds) } - return refund + return vm.NewGasBudget(refund) } // returnGas returns ETH for remaining gas, // exchanged at the original rate. func (st *stateTransition) returnGas() { - remaining := uint256.NewInt(st.gasRemaining) + remaining := uint256.NewInt(st.gasRemaining.RegularGas) remaining.Mul(remaining, uint256.MustFromBig(st.msg.GasPrice)) st.state.AddBalance(st.msg.From, remaining, tracing.BalanceIncreaseGasReturn) - if st.evm.Config.Tracer != nil && st.evm.Config.Tracer.OnGasChange != nil && st.gasRemaining > 0 { - st.evm.Config.Tracer.OnGasChange(st.gasRemaining, 0, tracing.GasChangeTxLeftOverReturned) + if st.evm.Config.Tracer != nil && st.evm.Config.Tracer.OnGasChange != nil && st.gasRemaining.RegularGas > 0 { + st.evm.Config.Tracer.OnGasChange(st.gasRemaining.RegularGas, 0, tracing.GasChangeTxLeftOverReturned) } } // gasUsed returns the amount of gas used up by the state transition. func (st *stateTransition) gasUsed() uint64 { - return st.initialGas - st.gasRemaining + return st.gasRemaining.Used(st.initialBudget) } // blobGasUsed returns the amount of blob gas used by the message. diff --git a/core/tracing/hooks.go b/core/tracing/hooks.go index de63689bc5..62c62ac3b3 100644 --- a/core/tracing/hooks.go +++ b/core/tracing/hooks.go @@ -167,6 +167,8 @@ type ( // GasChangeHook is invoked when the gas changes. GasChangeHook = func(old, new uint64, reason GasChangeReason) + // TODO(sina, rjl), please add GasChangeV2Hook by landing the multi-dimensional gas + /* - Chain events - */ diff --git a/core/txpool/validation.go b/core/txpool/validation.go index 13b1bfa312..3569bba08d 100644 --- a/core/txpool/validation.go +++ b/core/txpool/validation.go @@ -129,8 +129,8 @@ func ValidateTransaction(tx *types.Transaction, head *types.Header, signer types if err != nil { return err } - if tx.Gas() < intrGas { - return fmt.Errorf("%w: gas %v, minimum needed %v", core.ErrIntrinsicGas, tx.Gas(), intrGas) + if tx.Gas() < intrGas.RegularGas { + return fmt.Errorf("%w: gas %v, minimum needed %v", core.ErrIntrinsicGas, tx.Gas(), intrGas.RegularGas) } // Ensure the transaction can cover floor data gas. if rules.IsPrague { diff --git a/core/vm/contract.go b/core/vm/contract.go index 3b5695e21a..a55a5dde8b 100644 --- a/core/vm/contract.go +++ b/core/vm/contract.go @@ -42,12 +42,12 @@ type Contract struct { IsDeployment bool IsSystemCall bool - Gas GasCosts + Gas GasBudget value *uint256.Int } // NewContract returns a new contract environment for the execution of EVM. -func NewContract(caller common.Address, address common.Address, value *uint256.Int, gas uint64, jumpDests JumpDestCache) *Contract { +func NewContract(caller common.Address, address common.Address, value *uint256.Int, gas GasBudget, jumpDests JumpDestCache) *Contract { // Initialize the jump analysis cache if it's nil, mostly for tests if jumpDests == nil { jumpDests = newMapJumpDests() @@ -56,7 +56,7 @@ func NewContract(caller common.Address, address common.Address, value *uint256.I caller: caller, address: address, jumpDests: jumpDests, - Gas: GasCosts{RegularGas: gas}, + Gas: gas, value: value, } } @@ -126,26 +126,26 @@ func (c *Contract) Caller() common.Address { } // UseGas attempts the use gas and subtracts it and returns true on success -func (c *Contract) UseGas(gas uint64, logger *tracing.Hooks, reason tracing.GasChangeReason) (ok bool) { - if c.Gas.RegularGas < gas { +func (c *Contract) UseGas(cost GasCosts, logger *tracing.Hooks, reason tracing.GasChangeReason) (ok bool) { + prior, ok := c.Gas.Charge(cost) + if !ok { return false } if logger != nil && logger.OnGasChange != nil && reason != tracing.GasChangeIgnored { - logger.OnGasChange(c.Gas.RegularGas, c.Gas.RegularGas-gas, reason) + logger.OnGasChange(prior, c.Gas.RegularGas, reason) } - c.Gas.RegularGas -= gas return true } -// RefundGas refunds gas to the contract -func (c *Contract) RefundGas(gas uint64, logger *tracing.Hooks, reason tracing.GasChangeReason) { - if gas == 0 { +// RefundGas refunds the leftover gas budget back to the contract. +func (c *Contract) RefundGas(refund GasBudget, logger *tracing.Hooks, reason tracing.GasChangeReason) { + prior, changed := c.Gas.Refund(refund) + if !changed { return } if logger != nil && logger.OnGasChange != nil && reason != tracing.GasChangeIgnored { - logger.OnGasChange(c.Gas.RegularGas, c.Gas.RegularGas+gas, reason) + logger.OnGasChange(prior, c.Gas.RegularGas, reason) } - c.Gas.RegularGas += gas } // Address returns the contracts address diff --git a/core/vm/contracts.go b/core/vm/contracts.go index b4cddf0bca..b1eed79282 100644 --- a/core/vm/contracts.go +++ b/core/vm/contracts.go @@ -260,25 +260,25 @@ func ActivePrecompiles(rules params.Rules) []common.Address { // RunPrecompiledContract runs and evaluates the output of a precompiled contract. // It returns // - the returned bytes, -// - the _remaining_ gas, +// - the remaining gas budget, // - any error that occurred -func RunPrecompiledContract(stateDB StateDB, p PrecompiledContract, address common.Address, input []byte, suppliedGas uint64, logger *tracing.Hooks) (ret []byte, remainingGas uint64, err error) { +func RunPrecompiledContract(stateDB StateDB, p PrecompiledContract, address common.Address, input []byte, gas GasBudget, logger *tracing.Hooks) (ret []byte, remaining GasBudget, err error) { gasCost := p.RequiredGas(input) - if suppliedGas < gasCost { - return nil, 0, ErrOutOfGas + prior, ok := gas.Charge(GasCosts{RegularGas: gasCost}) + if !ok { + gas.Exhaust() + return nil, gas, ErrOutOfGas } if logger != nil && logger.OnGasChange != nil { - logger.OnGasChange(suppliedGas, suppliedGas-gasCost, tracing.GasChangeCallPrecompiledContract) + logger.OnGasChange(prior, gas.RegularGas, tracing.GasChangeCallPrecompiledContract) } - suppliedGas -= gasCost - // Touch the precompile for block-level accessList recording once Amsterdam // fork is activated. if stateDB != nil { stateDB.Exist(address) } output, err := p.Run(input) - return output, suppliedGas, err + return output, gas, err } // ecrecover implemented as a native contract. diff --git a/core/vm/contracts_fuzz_test.go b/core/vm/contracts_fuzz_test.go index 34ed1d87fe..35a9bd0257 100644 --- a/core/vm/contracts_fuzz_test.go +++ b/core/vm/contracts_fuzz_test.go @@ -36,7 +36,7 @@ func FuzzPrecompiledContracts(f *testing.F) { return } inWant := string(input) - RunPrecompiledContract(nil, p, a, input, gas, nil) + RunPrecompiledContract(nil, p, a, input, NewGasBudget(gas), nil) if inHave := string(input); inWant != inHave { t.Errorf("Precompiled %v modified input data", a) } diff --git a/core/vm/contracts_test.go b/core/vm/contracts_test.go index b647dcc62b..dda753f504 100644 --- a/core/vm/contracts_test.go +++ b/core/vm/contracts_test.go @@ -99,7 +99,7 @@ func testPrecompiled(addr string, test precompiledTest, t *testing.T) { in := common.Hex2Bytes(test.Input) gas := p.RequiredGas(in) t.Run(fmt.Sprintf("%s-Gas=%d", test.Name, gas), func(t *testing.T) { - if res, _, err := RunPrecompiledContract(nil, p, common.HexToAddress(addr), in, gas, nil); err != nil { + if res, _, err := RunPrecompiledContract(nil, p, common.HexToAddress(addr), in, NewGasBudget(gas), nil); err != nil { t.Error(err) } else if common.Bytes2Hex(res) != test.Expected { t.Errorf("Expected %v, got %v", test.Expected, common.Bytes2Hex(res)) @@ -121,7 +121,7 @@ func testPrecompiledOOG(addr string, test precompiledTest, t *testing.T) { gas := test.Gas - 1 t.Run(fmt.Sprintf("%s-Gas=%d", test.Name, gas), func(t *testing.T) { - _, _, err := RunPrecompiledContract(nil, p, common.HexToAddress(addr), in, gas, nil) + _, _, err := RunPrecompiledContract(nil, p, common.HexToAddress(addr), in, NewGasBudget(gas), nil) if err.Error() != "out of gas" { t.Errorf("Expected error [out of gas], got [%v]", err) } @@ -138,7 +138,7 @@ func testPrecompiledFailure(addr string, test precompiledFailureTest, t *testing in := common.Hex2Bytes(test.Input) gas := p.RequiredGas(in) t.Run(test.Name, func(t *testing.T) { - _, _, err := RunPrecompiledContract(nil, p, common.HexToAddress(addr), in, gas, nil) + _, _, err := RunPrecompiledContract(nil, p, common.HexToAddress(addr), in, NewGasBudget(gas), nil) if err.Error() != test.ExpectedError { t.Errorf("Expected error [%v], got [%v]", test.ExpectedError, err) } @@ -169,7 +169,7 @@ func benchmarkPrecompiled(addr string, test precompiledTest, bench *testing.B) { start := time.Now() for bench.Loop() { copy(data, in) - res, _, err = RunPrecompiledContract(nil, p, common.HexToAddress(addr), data, reqGas, nil) + res, _, err = RunPrecompiledContract(nil, p, common.HexToAddress(addr), data, NewGasBudget(reqGas), nil) } elapsed := uint64(time.Since(start)) if elapsed < 1 { diff --git a/core/vm/eips.go b/core/vm/eips.go index 8f4ca3ae41..54e5cb0c60 100644 --- a/core/vm/eips.go +++ b/core/vm/eips.go @@ -382,7 +382,7 @@ func opExtCodeCopyEIP4762(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, er code := evm.StateDB.GetCode(addr) paddedCodeCopy, copyOffset, nonPaddedCopyLength := getDataAndAdjustedBounds(code, uint64CodeOffset, length.Uint64()) consumed, wanted := evm.AccessEvents.CodeChunksRangeGas(addr, copyOffset, nonPaddedCopyLength, uint64(len(code)), false, scope.Contract.Gas.RegularGas) - scope.Contract.UseGas(consumed, evm.Config.Tracer, tracing.GasChangeUnspecified) + scope.Contract.UseGas(GasCosts{RegularGas: consumed}, evm.Config.Tracer, tracing.GasChangeUnspecified) if consumed < wanted { return nil, ErrOutOfGas } @@ -408,7 +408,7 @@ func opPush1EIP4762(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { // advanced past this boundary. contractAddr := scope.Contract.Address() consumed, wanted := evm.AccessEvents.CodeChunksRangeGas(contractAddr, *pc+1, uint64(1), uint64(len(scope.Contract.Code)), false, scope.Contract.Gas.RegularGas) - scope.Contract.UseGas(wanted, evm.Config.Tracer, tracing.GasChangeUnspecified) + scope.Contract.UseGas(GasCosts{RegularGas: wanted}, evm.Config.Tracer, tracing.GasChangeUnspecified) if consumed < wanted { return nil, ErrOutOfGas } @@ -436,7 +436,7 @@ func makePushEIP4762(size uint64, pushByteSize int) executionFunc { if !scope.Contract.IsDeployment && !scope.Contract.IsSystemCall { contractAddr := scope.Contract.Address() consumed, wanted := evm.AccessEvents.CodeChunksRangeGas(contractAddr, uint64(start), uint64(pushByteSize), uint64(len(scope.Contract.Code)), false, scope.Contract.Gas.RegularGas) - scope.Contract.UseGas(consumed, evm.Config.Tracer, tracing.GasChangeUnspecified) + scope.Contract.UseGas(GasCosts{RegularGas: consumed}, evm.Config.Tracer, tracing.GasChangeUnspecified) if consumed < wanted { return nil, ErrOutOfGas } diff --git a/core/vm/evm.go b/core/vm/evm.go index 8f68478842..ca8d8967ec 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -236,13 +236,13 @@ func isSystemCall(caller common.Address) bool { // parameters. It also handles any necessary value transfer required and takse // the necessary steps to create accounts and reverses the state in case of an // execution error or failed value transfer. -func (evm *EVM) Call(caller common.Address, addr common.Address, input []byte, gas uint64, value *uint256.Int) (ret []byte, leftOverGas uint64, err error) { +func (evm *EVM) Call(caller common.Address, addr common.Address, input []byte, gas GasBudget, value *uint256.Int) (ret []byte, leftOverGas GasBudget, err error) { // Capture the tracer start/end events in debug mode if evm.Config.Tracer != nil { - evm.captureBegin(evm.depth, CALL, caller, addr, input, gas, value.ToBig()) + evm.captureBegin(evm.depth, CALL, caller, addr, input, gas.RegularGas, value.ToBig()) defer func(startGas uint64) { - evm.captureEnd(evm.depth, startGas, leftOverGas, ret, err) - }(gas) + evm.captureEnd(evm.depth, startGas, leftOverGas.RegularGas, ret, err) + }(gas.RegularGas) } // Fail if we're trying to execute above the call depth limit if evm.depth > int(params.CallCreateDepth) { @@ -265,12 +265,12 @@ func (evm *EVM) Call(caller common.Address, addr common.Address, input []byte, g // list in write mode. If there is enough gas paying for the addition of the code // hash leaf to the access list, then account creation will proceed unimpaired. // Thus, only pay for the creation of the code hash leaf here. - wgas := evm.AccessEvents.CodeHashGas(addr, true, gas, false) - if gas < wgas { + wgas := evm.AccessEvents.CodeHashGas(addr, true, gas.RegularGas, false) + if _, ok := gas.Charge(GasCosts{RegularGas: wgas}); !ok { evm.StateDB.RevertToSnapshot(snapshot) - return nil, 0, ErrOutOfGas + gas.Exhaust() + return nil, gas, ErrOutOfGas } - gas -= wgas } if !isPrecompile && evm.chainRules.IsEIP158 && value.IsZero() { @@ -303,7 +303,7 @@ func (evm *EVM) Call(caller common.Address, addr common.Address, input []byte, g contract.IsSystemCall = isSystemCall(caller) contract.SetCallCode(evm.resolveCodeHash(addr), code) ret, err = evm.Run(contract, input, false) - gas = contract.Gas.RegularGas + gas = contract.Gas } } // When an error was returned by the EVM or when setting the creation code @@ -313,9 +313,9 @@ func (evm *EVM) Call(caller common.Address, addr common.Address, input []byte, g evm.StateDB.RevertToSnapshot(snapshot) if err != ErrExecutionReverted { if evm.Config.Tracer != nil && evm.Config.Tracer.OnGasChange != nil { - evm.Config.Tracer.OnGasChange(gas, 0, tracing.GasChangeCallFailedExecution) + evm.Config.Tracer.OnGasChange(gas.RegularGas, 0, tracing.GasChangeCallFailedExecution) } - gas = 0 + gas.Exhaust() } // TODO: consider clearing up unused snapshots: //} else { @@ -331,13 +331,13 @@ func (evm *EVM) Call(caller common.Address, addr common.Address, input []byte, g // // CallCode differs from Call in the sense that it executes the given address' // code with the caller as context. -func (evm *EVM) CallCode(caller common.Address, addr common.Address, input []byte, gas uint64, value *uint256.Int) (ret []byte, leftOverGas uint64, err error) { +func (evm *EVM) CallCode(caller common.Address, addr common.Address, input []byte, gas GasBudget, value *uint256.Int) (ret []byte, leftOverGas GasBudget, err error) { // Invoke tracer hooks that signal entering/exiting a call frame if evm.Config.Tracer != nil { - evm.captureBegin(evm.depth, CALLCODE, caller, addr, input, gas, value.ToBig()) + evm.captureBegin(evm.depth, CALLCODE, caller, addr, input, gas.RegularGas, value.ToBig()) defer func(startGas uint64) { - evm.captureEnd(evm.depth, startGas, leftOverGas, ret, err) - }(gas) + evm.captureEnd(evm.depth, startGas, leftOverGas.RegularGas, ret, err) + }(gas.RegularGas) } // Fail if we're trying to execute above the call depth limit if evm.depth > int(params.CallCreateDepth) { @@ -365,15 +365,15 @@ func (evm *EVM) CallCode(caller common.Address, addr common.Address, input []byt contract := NewContract(caller, caller, value, gas, evm.jumpDests) contract.SetCallCode(evm.resolveCodeHash(addr), evm.resolveCode(addr)) ret, err = evm.Run(contract, input, false) - gas = contract.Gas.RegularGas + gas = contract.Gas } if err != nil { evm.StateDB.RevertToSnapshot(snapshot) if err != ErrExecutionReverted { if evm.Config.Tracer != nil && evm.Config.Tracer.OnGasChange != nil { - evm.Config.Tracer.OnGasChange(gas, 0, tracing.GasChangeCallFailedExecution) + evm.Config.Tracer.OnGasChange(gas.RegularGas, 0, tracing.GasChangeCallFailedExecution) } - gas = 0 + gas.Exhaust() } } return ret, gas, err @@ -384,14 +384,14 @@ func (evm *EVM) CallCode(caller common.Address, addr common.Address, input []byt // // DelegateCall differs from CallCode in the sense that it executes the given address' // code with the caller as context and the caller is set to the caller of the caller. -func (evm *EVM) DelegateCall(originCaller common.Address, caller common.Address, addr common.Address, input []byte, gas uint64, value *uint256.Int) (ret []byte, leftOverGas uint64, err error) { +func (evm *EVM) DelegateCall(originCaller common.Address, caller common.Address, addr common.Address, input []byte, gas GasBudget, value *uint256.Int) (ret []byte, leftOverGas GasBudget, err error) { // Invoke tracer hooks that signal entering/exiting a call frame if evm.Config.Tracer != nil { // DELEGATECALL inherits value from parent call - evm.captureBegin(evm.depth, DELEGATECALL, caller, addr, input, gas, value.ToBig()) + evm.captureBegin(evm.depth, DELEGATECALL, caller, addr, input, gas.RegularGas, value.ToBig()) defer func(startGas uint64) { - evm.captureEnd(evm.depth, startGas, leftOverGas, ret, err) - }(gas) + evm.captureEnd(evm.depth, startGas, leftOverGas.RegularGas, ret, err) + }(gas.RegularGas) } // Fail if we're trying to execute above the call depth limit if evm.depth > int(params.CallCreateDepth) { @@ -413,15 +413,15 @@ func (evm *EVM) DelegateCall(originCaller common.Address, caller common.Address, contract := NewContract(originCaller, caller, value, gas, evm.jumpDests) contract.SetCallCode(evm.resolveCodeHash(addr), evm.resolveCode(addr)) ret, err = evm.Run(contract, input, false) - gas = contract.Gas.RegularGas + gas = contract.Gas } if err != nil { evm.StateDB.RevertToSnapshot(snapshot) if err != ErrExecutionReverted { if evm.Config.Tracer != nil && evm.Config.Tracer.OnGasChange != nil { - evm.Config.Tracer.OnGasChange(gas, 0, tracing.GasChangeCallFailedExecution) + evm.Config.Tracer.OnGasChange(gas.RegularGas, 0, tracing.GasChangeCallFailedExecution) } - gas = 0 + gas.Exhaust() } } return ret, gas, err @@ -431,13 +431,13 @@ func (evm *EVM) DelegateCall(originCaller common.Address, caller common.Address, // as parameters while disallowing any modifications to the state during the call. // Opcodes that attempt to perform such modifications will result in exceptions // instead of performing the modifications. -func (evm *EVM) StaticCall(caller common.Address, addr common.Address, input []byte, gas uint64) (ret []byte, leftOverGas uint64, err error) { +func (evm *EVM) StaticCall(caller common.Address, addr common.Address, input []byte, gas GasBudget) (ret []byte, leftOverGas GasBudget, err error) { // Invoke tracer hooks that signal entering/exiting a call frame if evm.Config.Tracer != nil { - evm.captureBegin(evm.depth, STATICCALL, caller, addr, input, gas, nil) + evm.captureBegin(evm.depth, STATICCALL, caller, addr, input, gas.RegularGas, nil) defer func(startGas uint64) { - evm.captureEnd(evm.depth, startGas, leftOverGas, ret, err) - }(gas) + evm.captureEnd(evm.depth, startGas, leftOverGas.RegularGas, ret, err) + }(gas.RegularGas) } // Fail if we're trying to execute above the call depth limit if evm.depth > int(params.CallCreateDepth) { @@ -472,28 +472,27 @@ func (evm *EVM) StaticCall(caller common.Address, addr common.Address, input []b // above we revert to the snapshot and consume any gas remaining. Additionally // when we're in Homestead this also counts for code storage gas errors. ret, err = evm.Run(contract, input, true) - gas = contract.Gas.RegularGas + gas = contract.Gas } if err != nil { evm.StateDB.RevertToSnapshot(snapshot) if err != ErrExecutionReverted { if evm.Config.Tracer != nil && evm.Config.Tracer.OnGasChange != nil { - evm.Config.Tracer.OnGasChange(gas, 0, tracing.GasChangeCallFailedExecution) + evm.Config.Tracer.OnGasChange(gas.RegularGas, 0, tracing.GasChangeCallFailedExecution) } - - gas = 0 + gas.Exhaust() } } return ret, gas, err } // create creates a new contract using code as deployment code. -func (evm *EVM) create(caller common.Address, code []byte, gas uint64, value *uint256.Int, address common.Address, typ OpCode) (ret []byte, createAddress common.Address, leftOverGas uint64, err error) { +func (evm *EVM) create(caller common.Address, code []byte, gas GasBudget, value *uint256.Int, address common.Address, typ OpCode) (ret []byte, createAddress common.Address, leftOverGas GasBudget, err error) { if evm.Config.Tracer != nil { - evm.captureBegin(evm.depth, typ, caller, address, code, gas, value.ToBig()) + evm.captureBegin(evm.depth, typ, caller, address, code, gas.RegularGas, value.ToBig()) defer func(startGas uint64) { - evm.captureEnd(evm.depth, startGas, leftOverGas, ret, err) - }(gas) + evm.captureEnd(evm.depth, startGas, leftOverGas.RegularGas, ret, err) + }(gas.RegularGas) } // Depth check execution. Fail if we're trying to execute above the // limit. @@ -511,14 +510,15 @@ func (evm *EVM) create(caller common.Address, code []byte, gas uint64, value *ui // Charge the contract creation init gas in verkle mode if evm.chainRules.IsEIP4762 { - statelessGas := evm.AccessEvents.ContractCreatePreCheckGas(address, gas) - if statelessGas > gas { - return nil, common.Address{}, 0, ErrOutOfGas + statelessGas := evm.AccessEvents.ContractCreatePreCheckGas(address, gas.RegularGas) + prior, ok := gas.Charge(GasCosts{RegularGas: statelessGas}) + if !ok { + gas.Exhaust() + return nil, common.Address{}, gas, ErrOutOfGas } if evm.Config.Tracer != nil && evm.Config.Tracer.OnGasChange != nil { - evm.Config.Tracer.OnGasChange(gas, gas-statelessGas, tracing.GasChangeWitnessContractCollisionCheck) + evm.Config.Tracer.OnGasChange(prior, gas.RegularGas, tracing.GasChangeWitnessContractCollisionCheck) } - gas = gas - statelessGas } // We add this to the access list _before_ taking a snapshot. Even if the @@ -536,9 +536,10 @@ func (evm *EVM) create(caller common.Address, code []byte, gas uint64, value *ui (contractHash != (common.Hash{}) && contractHash != types.EmptyCodeHash) || // non-empty code isEIP7610RejectedAccount(evm.ChainConfig().ChainID, address, evm.chainRules.IsEIP158) { if evm.Config.Tracer != nil && evm.Config.Tracer.OnGasChange != nil { - evm.Config.Tracer.OnGasChange(gas, 0, tracing.GasChangeCallFailedExecution) + evm.Config.Tracer.OnGasChange(gas.RegularGas, 0, tracing.GasChangeCallFailedExecution) } - return nil, common.Address{}, 0, ErrContractAddressCollision + gas.Exhaust() + return nil, common.Address{}, gas, ErrContractAddressCollision } // Create a new account on the state only if the object was not present. // It might be possible the contract code is deployed to a pre-existent @@ -558,14 +559,15 @@ func (evm *EVM) create(caller common.Address, code []byte, gas uint64, value *ui } // Charge the contract creation init gas in verkle mode if evm.chainRules.IsEIP4762 { - consumed, wanted := evm.AccessEvents.ContractCreateInitGas(address, gas) + consumed, wanted := evm.AccessEvents.ContractCreateInitGas(address, gas.RegularGas) if consumed < wanted { - return nil, common.Address{}, 0, ErrOutOfGas + gas.Exhaust() + return nil, common.Address{}, gas, ErrOutOfGas } + prior, _ := gas.Charge(GasCosts{RegularGas: consumed}) if evm.Config.Tracer != nil && evm.Config.Tracer.OnGasChange != nil { - evm.Config.Tracer.OnGasChange(gas, gas-consumed, tracing.GasChangeWitnessContractInit) + evm.Config.Tracer.OnGasChange(prior, gas.RegularGas, tracing.GasChangeWitnessContractInit) } - gas = gas - consumed } evm.Context.Transfer(evm.StateDB, caller, address, value, &evm.chainRules) @@ -582,10 +584,10 @@ func (evm *EVM) create(caller common.Address, code []byte, gas uint64, value *ui if err != nil && (evm.chainRules.IsHomestead || err != ErrCodeStoreOutOfGas) { evm.StateDB.RevertToSnapshot(snapshot) if err != ErrExecutionReverted { - contract.UseGas(contract.Gas.RegularGas, evm.Config.Tracer, tracing.GasChangeCallFailedExecution) + contract.UseGas(GasCosts{RegularGas: contract.Gas.RegularGas}, evm.Config.Tracer, tracing.GasChangeCallFailedExecution) } } - return ret, address, contract.Gas.RegularGas, err + return ret, address, contract.Gas, err } // initNewContract runs a new contract's creation code, performs checks on the @@ -608,12 +610,12 @@ func (evm *EVM) initNewContract(contract *Contract, address common.Address) ([]b if !evm.chainRules.IsEIP4762 { createDataGas := uint64(len(ret)) * params.CreateDataGas - if !contract.UseGas(createDataGas, evm.Config.Tracer, tracing.GasChangeCallCodeStorage) { + if !contract.UseGas(GasCosts{RegularGas: createDataGas}, evm.Config.Tracer, tracing.GasChangeCallCodeStorage) { return ret, ErrCodeStoreOutOfGas } } else { consumed, wanted := evm.AccessEvents.CodeChunksRangeGas(address, 0, uint64(len(ret)), uint64(len(ret)), true, contract.Gas.RegularGas) - contract.UseGas(consumed, evm.Config.Tracer, tracing.GasChangeWitnessCodeChunk) + contract.UseGas(GasCosts{RegularGas: consumed}, evm.Config.Tracer, tracing.GasChangeWitnessCodeChunk) if len(ret) > 0 && (consumed < wanted) { return ret, ErrCodeStoreOutOfGas } @@ -626,7 +628,7 @@ func (evm *EVM) initNewContract(contract *Contract, address common.Address) ([]b } // Create creates a new contract using code as deployment code. -func (evm *EVM) Create(caller common.Address, code []byte, gas uint64, value *uint256.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) { +func (evm *EVM) Create(caller common.Address, code []byte, gas GasBudget, value *uint256.Int) (ret []byte, contractAddr common.Address, leftOverGas GasBudget, err error) { contractAddr = crypto.CreateAddress(caller, evm.StateDB.GetNonce(caller)) return evm.create(caller, code, gas, value, contractAddr, CREATE) } @@ -635,7 +637,7 @@ func (evm *EVM) Create(caller common.Address, code []byte, gas uint64, value *ui // // The different between Create2 with Create is Create2 uses keccak256(0xff ++ msg.sender ++ salt ++ keccak256(init_code))[12:] // instead of the usual sender-and-nonce-hash as the address where the contract is initialized at. -func (evm *EVM) Create2(caller common.Address, code []byte, gas uint64, endowment *uint256.Int, salt *uint256.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) { +func (evm *EVM) Create2(caller common.Address, code []byte, gas GasBudget, endowment *uint256.Int, salt *uint256.Int) (ret []byte, contractAddr common.Address, leftOverGas GasBudget, err error) { inithash := crypto.Keccak256Hash(code) contractAddr = crypto.CreateAddress2(caller, salt.Bytes32(), inithash[:]) return evm.create(caller, code, gas, endowment, contractAddr, CREATE2) diff --git a/core/vm/gas_table_test.go b/core/vm/gas_table_test.go index e9e56038dd..16ce651a7d 100644 --- a/core/vm/gas_table_test.go +++ b/core/vm/gas_table_test.go @@ -97,12 +97,12 @@ func TestEIP2200(t *testing.T) { Transfer: func(StateDB, common.Address, common.Address, *uint256.Int, *params.Rules) {}, } evm := NewEVM(vmctx, statedb, params.AllEthashProtocolChanges, Config{ExtraEips: []int{2200}}) - - _, gas, err := evm.Call(common.Address{}, address, nil, tt.gaspool, new(uint256.Int)) + initialGas := NewGasBudget(tt.gaspool) + _, leftOver, err := evm.Call(common.Address{}, address, nil, initialGas.Copy(), new(uint256.Int)) if !errors.Is(err, tt.failure) { t.Errorf("test %d: failure mismatch: have %v, want %v", i, err, tt.failure) } - if used := tt.gaspool - gas; used != tt.used { + if used := leftOver.Used(initialGas); used != tt.used { t.Errorf("test %d: gas used mismatch: have %v, want %v", i, used, tt.used) } if refund := evm.StateDB.GetRefund(); refund != tt.refund { @@ -157,12 +157,12 @@ func TestCreateGas(t *testing.T) { } evm := NewEVM(vmctx, statedb, chainConfig, config) - var startGas = uint64(testGas) - ret, gas, err := evm.Call(common.Address{}, address, nil, startGas, new(uint256.Int)) + initialGas := NewGasBudget(uint64(testGas)) + ret, leftOver, err := evm.Call(common.Address{}, address, nil, initialGas.Copy(), new(uint256.Int)) if err != nil { return false } - gasUsed = startGas - gas + gasUsed = leftOver.Used(initialGas) if len(ret) != 32 { t.Fatalf("test %d: expected 32 bytes returned, have %d", i, len(ret)) } diff --git a/core/vm/gascosts.go b/core/vm/gascosts.go index ba6746758b..cc90c54798 100644 --- a/core/vm/gascosts.go +++ b/core/vm/gascosts.go @@ -19,7 +19,8 @@ package vm import "fmt" // GasCosts denotes a vector of gas costs in the -// multidimensional metering paradigm. +// multidimensional metering paradigm. It represents the cost +// charged by an individual operation. type GasCosts struct { RegularGas uint64 StateGas uint64 @@ -34,3 +35,63 @@ func (g GasCosts) Sum() uint64 { func (g GasCosts) String() string { return fmt.Sprintf("<%v,%v>", g.RegularGas, g.StateGas) } + +// GasBudget denotes a vector of remaining gas allowances available +// for EVM execution in the multidimensional metering paradigm. +// Unlike GasCosts which represents the price of an operation, +// GasBudget tracks how much gas is left to spend. +type GasBudget struct { + RegularGas uint64 // The leftover gas for execution and state gas usage + StateGas uint64 // The state gas reservoir +} + +// NewGasBudget creates a GasBudget with the given initial regular gas allowance. +func NewGasBudget(gas uint64) GasBudget { + return GasBudget{RegularGas: gas} +} + +// Used returns the amount of regular gas consumed so far. +func (g GasBudget) Used(initial GasBudget) uint64 { + return initial.RegularGas - g.RegularGas +} + +// Exhaust sets all remaining gas to zero, preserving the initial amount +// for usage tracking. +func (g *GasBudget) Exhaust() { + g.RegularGas = 0 + g.StateGas = 0 +} + +func (g *GasBudget) Copy() GasBudget { + return GasBudget{RegularGas: g.RegularGas, StateGas: g.StateGas} +} + +// String returns a visual representation of the gas budget vector. +func (g GasBudget) String() string { + return fmt.Sprintf("<%v,%v>", g.RegularGas, g.StateGas) +} + +// CanAfford reports whether the budget has sufficient gas to cover the cost. +func (g GasBudget) CanAfford(cost GasCosts) bool { + return g.RegularGas >= cost.RegularGas +} + +// Charge deducts the given gas cost from the budget. It returns the +// pre-charge gas value and false if the budget does not have sufficient +// gas to cover the cost. +func (g *GasBudget) Charge(cost GasCosts) (uint64, bool) { + prior := g.RegularGas + if prior < cost.RegularGas { + return prior, false + } + g.RegularGas -= cost.RegularGas + return prior, true +} + +// Refund adds the given gas budget back. It returns the pre-refund gas +// value and whether the budget was actually changed. +func (g *GasBudget) Refund(other GasBudget) (uint64, bool) { + prior := g.RegularGas + g.RegularGas += other.RegularGas + return prior, g.RegularGas != prior +} diff --git a/core/vm/instructions.go b/core/vm/instructions.go index c3d1633c78..3311af0d22 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -667,9 +667,9 @@ func opCreate(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { // reuse size int for stackvalue stackvalue := size - scope.Contract.UseGas(gas, evm.Config.Tracer, tracing.GasChangeCallContractCreation) + scope.Contract.UseGas(GasCosts{RegularGas: gas}, evm.Config.Tracer, tracing.GasChangeCallContractCreation) - res, addr, returnGas, suberr := evm.Create(scope.Contract.Address(), input, gas, &value) + res, addr, returnGas, suberr := evm.Create(scope.Contract.Address(), input, NewGasBudget(gas), &value) // Push item on the stack based on the returned error. If the ruleset is // homestead we must check for CodeStoreOutOfGasError (homestead only // rule) and treat as an error, if the ruleset is frontier we must @@ -707,10 +707,10 @@ func opCreate2(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { // Apply EIP150 gas -= gas / 64 - scope.Contract.UseGas(gas, evm.Config.Tracer, tracing.GasChangeCallContractCreation2) + scope.Contract.UseGas(GasCosts{RegularGas: gas}, evm.Config.Tracer, tracing.GasChangeCallContractCreation2) // reuse size int for stackvalue stackvalue := size - res, addr, returnGas, suberr := evm.Create2(scope.Contract.Address(), input, gas, + res, addr, returnGas, suberr := evm.Create2(scope.Contract.Address(), input, NewGasBudget(gas), &endowment, &salt) // Push item on the stack based on the returned error. if suberr != nil { @@ -747,7 +747,7 @@ func opCall(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { if !value.IsZero() { gas += params.CallStipend } - ret, returnGas, err := evm.Call(scope.Contract.Address(), toAddr, args, gas, &value) + ret, returnGas, err := evm.Call(scope.Contract.Address(), toAddr, args, NewGasBudget(gas), &value) if err != nil { temp.Clear() @@ -781,7 +781,7 @@ func opCallCode(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { gas += params.CallStipend } - ret, returnGas, err := evm.CallCode(scope.Contract.Address(), toAddr, args, gas, &value) + ret, returnGas, err := evm.CallCode(scope.Contract.Address(), toAddr, args, NewGasBudget(gas), &value) if err != nil { temp.Clear() } else { @@ -810,7 +810,7 @@ func opDelegateCall(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { // Get arguments from the memory. args := scope.Memory.GetPtr(inOffset.Uint64(), inSize.Uint64()) - ret, returnGas, err := evm.DelegateCall(scope.Contract.Caller(), scope.Contract.Address(), toAddr, args, gas, scope.Contract.value) + ret, returnGas, err := evm.DelegateCall(scope.Contract.Caller(), scope.Contract.Address(), toAddr, args, NewGasBudget(gas), scope.Contract.value) if err != nil { temp.Clear() } else { @@ -839,7 +839,7 @@ func opStaticCall(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { // Get arguments from the memory. args := scope.Memory.GetPtr(inOffset.Uint64(), inSize.Uint64()) - ret, returnGas, err := evm.StaticCall(scope.Contract.Address(), toAddr, args, gas) + ret, returnGas, err := evm.StaticCall(scope.Contract.Address(), toAddr, args, NewGasBudget(gas)) if err != nil { temp.Clear() } else { diff --git a/core/vm/instructions_test.go b/core/vm/instructions_test.go index 5055ccb16d..dbe055f2ac 100644 --- a/core/vm/instructions_test.go +++ b/core/vm/instructions_test.go @@ -565,7 +565,7 @@ func TestOpTstore(t *testing.T) { mem = NewMemory() caller = common.Address{} to = common.Address{1} - contract = NewContract(caller, to, new(uint256.Int), 0, nil) + contract = NewContract(caller, to, new(uint256.Int), GasBudget{}, nil) scopeContext = ScopeContext{mem, stack, contract} value = common.Hex2Bytes("abcdef00000000000000abba000000000deaf000000c0de00100000000133700") ) diff --git a/core/vm/interpreter.go b/core/vm/interpreter.go index b507595fab..9dc0a0b787 100644 --- a/core/vm/interpreter.go +++ b/core/vm/interpreter.go @@ -174,7 +174,7 @@ func (evm *EVM) Run(contract *Contract, input []byte, readOnly bool) (ret []byte // associated costs. contractAddr := contract.Address() consumed, wanted := evm.TxContext.AccessEvents.CodeChunksRangeGas(contractAddr, pc, 1, uint64(len(contract.Code)), false, contract.Gas.RegularGas) - contract.UseGas(consumed, evm.Config.Tracer, tracing.GasChangeWitnessCodeChunk) + contract.UseGas(GasCosts{RegularGas: consumed}, evm.Config.Tracer, tracing.GasChangeWitnessCodeChunk) if consumed < wanted { return nil, ErrOutOfGas } diff --git a/core/vm/interpreter_test.go b/core/vm/interpreter_test.go index 28df8546b5..69c2316907 100644 --- a/core/vm/interpreter_test.go +++ b/core/vm/interpreter_test.go @@ -55,7 +55,7 @@ func TestLoopInterrupt(t *testing.T) { timeout := make(chan bool) go func(evm *EVM) { - _, _, err := evm.Call(common.Address{}, address, nil, math.MaxUint64, new(uint256.Int)) + _, _, err := evm.Call(common.Address{}, address, nil, NewGasBudget(math.MaxUint64), new(uint256.Int)) errChannel <- err }(evm) @@ -85,7 +85,7 @@ func BenchmarkInterpreter(b *testing.B) { value = uint256.NewInt(0) stack = newstack() mem = NewMemory() - contract = NewContract(common.Address{}, common.Address{}, value, startGas, nil) + contract = NewContract(common.Address{}, common.Address{}, value, NewGasBudget(startGas), nil) ) stack.push(uint256.NewInt(123)) stack.push(uint256.NewInt(123)) diff --git a/core/vm/operations_acl.go b/core/vm/operations_acl.go index 154c261cae..313d03819e 100644 --- a/core/vm/operations_acl.go +++ b/core/vm/operations_acl.go @@ -168,7 +168,7 @@ func makeCallVariantGasCallEIP2929(oldCalculator gasFunc, addressPosition int) g evm.StateDB.AddAddressToAccessList(addr) // Charge the remaining difference here already, to correctly calculate available // gas for call - if !contract.UseGas(coldCost, evm.Config.Tracer, tracing.GasChangeCallStorageColdAccess) { + if !contract.UseGas(GasCosts{RegularGas: coldCost}, evm.Config.Tracer, tracing.GasChangeCallStorageColdAccess) { return GasCosts{}, ErrOutOfGas } } @@ -295,7 +295,7 @@ func makeCallVariantGasCallEIP7702(intrinsicFunc gasFunc) gasFunc { // Charge the remaining difference here already, to correctly calculate // available gas for call - if !contract.UseGas(eip2929Cost, evm.Config.Tracer, tracing.GasChangeCallStorageColdAccess) { + if !contract.UseGas(GasCosts{RegularGas: eip2929Cost}, evm.Config.Tracer, tracing.GasChangeCallStorageColdAccess) { return GasCosts{}, ErrOutOfGas } } @@ -324,7 +324,7 @@ func makeCallVariantGasCallEIP7702(intrinsicFunc gasFunc) gasFunc { evm.StateDB.AddAddressToAccessList(target) eip7702Cost = params.ColdAccountAccessCostEIP2929 } - if !contract.UseGas(eip7702Cost, evm.Config.Tracer, tracing.GasChangeCallStorageColdAccess) { + if !contract.UseGas(GasCosts{RegularGas: eip7702Cost}, evm.Config.Tracer, tracing.GasChangeCallStorageColdAccess) { return GasCosts{}, ErrOutOfGas } } diff --git a/core/vm/runtime/runtime.go b/core/vm/runtime/runtime.go index af394aa054..4fafdf3a50 100644 --- a/core/vm/runtime/runtime.go +++ b/core/vm/runtime/runtime.go @@ -148,11 +148,11 @@ func Execute(code, input []byte, cfg *Config) ([]byte, *state.StateDB, error) { cfg.Origin, common.BytesToAddress([]byte("contract")), input, - cfg.GasLimit, + vm.NewGasBudget(cfg.GasLimit), uint256.MustFromBig(cfg.Value), ) if cfg.EVMConfig.Tracer != nil && cfg.EVMConfig.Tracer.OnTxEnd != nil { - cfg.EVMConfig.Tracer.OnTxEnd(&types.Receipt{GasUsed: cfg.GasLimit - leftOverGas}, err) + cfg.EVMConfig.Tracer.OnTxEnd(&types.Receipt{GasUsed: cfg.GasLimit - leftOverGas.RegularGas}, err) } return ret, cfg.State, err } @@ -182,13 +182,13 @@ func Create(input []byte, cfg *Config) ([]byte, common.Address, uint64, error) { code, address, leftOverGas, err := vmenv.Create( cfg.Origin, input, - cfg.GasLimit, + vm.NewGasBudget(cfg.GasLimit), uint256.MustFromBig(cfg.Value), ) if cfg.EVMConfig.Tracer != nil && cfg.EVMConfig.Tracer.OnTxEnd != nil { - cfg.EVMConfig.Tracer.OnTxEnd(&types.Receipt{GasUsed: cfg.GasLimit - leftOverGas}, err) + cfg.EVMConfig.Tracer.OnTxEnd(&types.Receipt{GasUsed: cfg.GasLimit - leftOverGas.RegularGas}, err) } - return code, address, leftOverGas, err + return code, address, leftOverGas.RegularGas, err } // Call executes the code given by the contract's address. It will return the @@ -217,11 +217,11 @@ func Call(address common.Address, input []byte, cfg *Config) ([]byte, uint64, er cfg.Origin, address, input, - cfg.GasLimit, + vm.NewGasBudget(cfg.GasLimit), uint256.MustFromBig(cfg.Value), ) if cfg.EVMConfig.Tracer != nil && cfg.EVMConfig.Tracer.OnTxEnd != nil { - cfg.EVMConfig.Tracer.OnTxEnd(&types.Receipt{GasUsed: cfg.GasLimit - leftOverGas}, err) + cfg.EVMConfig.Tracer.OnTxEnd(&types.Receipt{GasUsed: cfg.GasLimit - leftOverGas.RegularGas}, err) } - return ret, leftOverGas, err + return ret, leftOverGas.RegularGas, err } diff --git a/eth/tracers/js/tracer_test.go b/eth/tracers/js/tracer_test.go index 694debcf98..6570d73575 100644 --- a/eth/tracers/js/tracer_test.go +++ b/eth/tracers/js/tracer_test.go @@ -55,7 +55,7 @@ func runTrace(tracer *tracers.Tracer, vmctx *vmContext, chaincfg *params.ChainCo gasLimit uint64 = 31000 startGas uint64 = 10000 value = uint256.NewInt(0) - contract = vm.NewContract(common.Address{}, common.Address{}, value, startGas, nil) + contract = vm.NewContract(common.Address{}, common.Address{}, value, vm.NewGasBudget(startGas), nil) ) evm.SetTxContext(vmctx.txCtx) contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x1, 0x0} @@ -183,7 +183,7 @@ func TestHaltBetweenSteps(t *testing.T) { t.Fatal(err) } scope := &vm.ScopeContext{ - Contract: vm.NewContract(common.Address{}, common.Address{}, uint256.NewInt(0), 0, nil), + Contract: vm.NewContract(common.Address{}, common.Address{}, uint256.NewInt(0), vm.GasBudget{}, nil), } evm := vm.NewEVM(vm.BlockContext{BlockNumber: big.NewInt(1)}, &dummyStatedb{}, chainConfig, vm.Config{Tracer: tracer.Hooks}) evm.SetTxContext(vm.TxContext{GasPrice: uint256.NewInt(1)}) @@ -281,7 +281,7 @@ func TestEnterExit(t *testing.T) { t.Fatal(err) } scope := &vm.ScopeContext{ - Contract: vm.NewContract(common.Address{}, common.Address{}, uint256.NewInt(0), 0, nil), + Contract: vm.NewContract(common.Address{}, common.Address{}, uint256.NewInt(0), vm.GasBudget{}, nil), } tracer.OnEnter(1, byte(vm.CALL), scope.Contract.Caller(), scope.Contract.Address(), []byte{}, 1000, new(big.Int)) tracer.OnExit(1, []byte{}, 400, nil, false) diff --git a/eth/tracers/logger/logger_test.go b/eth/tracers/logger/logger_test.go index 554a37aff1..decdf588e1 100644 --- a/eth/tracers/logger/logger_test.go +++ b/eth/tracers/logger/logger_test.go @@ -47,7 +47,7 @@ func TestStoreCapture(t *testing.T) { var ( logger = NewStructLogger(nil) evm = vm.NewEVM(vm.BlockContext{}, &dummyStatedb{}, params.TestChainConfig, vm.Config{Tracer: logger.Hooks()}) - contract = vm.NewContract(common.Address{}, common.Address{}, new(uint256.Int), 100000, nil) + contract = vm.NewContract(common.Address{}, common.Address{}, new(uint256.Int), vm.NewGasBudget(100000), nil) ) contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x0, byte(vm.SSTORE)} var index common.Hash diff --git a/tests/state_test.go b/tests/state_test.go index 25e90d388d..8444d211cf 100644 --- a/tests/state_test.go +++ b/tests/state_test.go @@ -312,7 +312,7 @@ func runBenchmark(b *testing.B, t *StateTest) { evm.SetTxContext(txContext) // Create "contract" for sender to cache code analysis. - sender := vm.NewContract(msg.From, msg.From, nil, 0, nil) + sender := vm.NewContract(msg.From, msg.From, nil, vm.GasBudget{}, nil) var ( gasUsed uint64 @@ -326,8 +326,10 @@ func runBenchmark(b *testing.B, t *StateTest) { b.StartTimer() start := time.Now() + initialGas := vm.NewGasBudget(msg.GasLimit) + // Execute the message. - _, leftOverGas, err := evm.Call(sender.Address(), *msg.To, msg.Data, msg.GasLimit, uint256.MustFromBig(msg.Value)) + _, leftOverGas, err := evm.Call(sender.Address(), *msg.To, msg.Data, initialGas.Copy(), uint256.MustFromBig(msg.Value)) if err != nil { b.Error(err) return @@ -336,7 +338,7 @@ func runBenchmark(b *testing.B, t *StateTest) { b.StopTimer() elapsed += uint64(time.Since(start)) refund += state.StateDB.GetRefund() - gasUsed += msg.GasLimit - leftOverGas + gasUsed += leftOverGas.Used(initialGas) state.StateDB.RevertToSnapshot(snapshot) } diff --git a/tests/transaction_test_util.go b/tests/transaction_test_util.go index a90c2d522f..a659a1f786 100644 --- a/tests/transaction_test_util.go +++ b/tests/transaction_test_util.go @@ -81,10 +81,11 @@ func (tt *TransactionTest) Run() error { return } // Intrinsic gas - requiredGas, err = core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.SetCodeAuthorizations(), tx.To() == nil, rules.IsHomestead, rules.IsIstanbul, rules.IsShanghai) + cost, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.SetCodeAuthorizations(), tx.To() == nil, rules.IsHomestead, rules.IsIstanbul, rules.IsShanghai) if err != nil { return } + requiredGas = cost.RegularGas if requiredGas > tx.Gas() { return sender, hash, 0, fmt.Errorf("insufficient gas ( %d < %d )", tx.Gas(), requiredGas) } From b6d415c88d79d165acad68db69666f35871457e4 Mon Sep 17 00:00:00 2001 From: CPerezz <37264926+CPerezz@users.noreply.github.com> Date: Mon, 20 Apr 2026 14:08:30 +0200 Subject: [PATCH 02/80] trie/bintrie: replace BinaryNode interface with GC-free NodeRef arena (#34055) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary Replace the `BinaryNode` interface with `NodeRef uint32` indices into typed arena pools, eliminating GC-scanned pointers from binary trie nodes. Inspired by [fjl's observation](https://github.com/ethereum/go-ethereum/pull/34034#issuecomment-4075176446): > *"if the binary trie produces such a large graph, it should probably be changed so that the trie node type does not contain pointers. The runtime does not scan objects that do not contain pointers, so it can really help with the performance to build it this way."* ### The problem CPU profiling of the binary trie (EIP-7864) showed **44% of CPU time in garbage collection**. Each `InternalNode` held two `BinaryNode` interface values (2 pointer-words each), and the GC scanned every one. With ~25K `InternalNode`s in memory during block processing, this created enormous GC pressure. ### The solution `NodeRef` is a compact `uint32` (2-bit kind tag + 30-bit pool index). `NodeStore` manages chunked typed pools per node kind: - **InternalNode pool**: ZERO Go pointers (children are `NodeRef`, hash is `[32]byte`) → noscan spans - **HashedNode pool**: ZERO Go pointers → noscan spans - **StemNode pool**: retains `Values [][]byte` (matching existing format) The serialization format is unchanged — flat InternalNode `[type][leftHash][rightHash]` = 65 bytes. ## Benchmark: Apple M4 Pro (`--benchtime=10s --count=3`, on top of #34021) | Metric | Baseline | Arena | Delta | |--------|----------|-------|-------| | Approve (Mgas/s) | 374 | 382 | **+2.1%** | | BalanceOf (Mgas/s) | 885 | 901 | **+1.8%** | | Approve allocs/op | 775K | **607K** | **-21.7%** | | BalanceOf allocs/op | 265K | **228K** | **-14.0%** | ## Benchmark: AMD EPYC 48-core (50GB state, execution-specs ERC-20, on top of #34021 + #34032) | Benchmark | Baseline | Arena | Delta | |-----------|----------|-------|-------| | erc20_approve (write) | 22.4 Mgas/s | **27.0 Mgas/s** | **+20.5%** | | mixed_sload_sstore | 62.9 Mgas/s | **97.3 Mgas/s** | **+54.7%** | | erc20_balanceof (read) | 180.8 Mgas/s | 167.6 Mgas/s | -7.3% (cold cache variance) | The arena benefit scales with heap size — the EPYC (larger heap, more GC pressure) shows much larger gains than the M4 Pro (efficient unified memory). The mixed workload baseline was unstable (62.9 vs 16.3 Mgas/s between runs due to GC-induced throughput collapse); the arena eliminates this entirely (95-97 Mgas/s, stable). ## Dependencies Benchmarked with #34021 (H01 N+1 fix) + #34032 (R14 parallel hashing). No code dependency — applies independently to master. All test suites pass (`trie/bintrie` with `-race`, `core/state`, `triedb/pathdb`, `cmd/geth`). --------- Co-authored-by: Guillaume Ballet <3272758+gballet@users.noreply.github.com> --- trie/bintrie/binary_node.go | 140 +-------- trie/bintrie/binary_node_test.go | 148 ++++++---- trie/bintrie/empty.go | 76 ----- trie/bintrie/empty_test.go | 264 ----------------- trie/bintrie/hashed_node.go | 73 +---- trie/bintrie/hashed_node_test.go | 181 +++++------- trie/bintrie/internal_node.go | 278 +----------------- trie/bintrie/internal_node_test.go | 449 +++++++++-------------------- trie/bintrie/iterator.go | 235 ++++++++------- trie/bintrie/iterator_test.go | 36 ++- trie/bintrie/node_ref.go | 56 ++++ trie/bintrie/node_store.go | 184 ++++++++++++ trie/bintrie/stem_node.go | 245 ++++------------ trie/bintrie/stem_node_test.go | 417 ++++++++++----------------- trie/bintrie/store_commit.go | 310 ++++++++++++++++++++ trie/bintrie/store_ops.go | 345 ++++++++++++++++++++++ trie/bintrie/trie.go | 83 ++---- trie/bintrie/trie_test.go | 231 ++++++--------- triedb/pathdb/database.go | 6 +- 19 files changed, 1677 insertions(+), 2080 deletions(-) delete mode 100644 trie/bintrie/empty.go delete mode 100644 trie/bintrie/empty_test.go create mode 100644 trie/bintrie/node_ref.go create mode 100644 trie/bintrie/node_store.go create mode 100644 trie/bintrie/store_commit.go create mode 100644 trie/bintrie/store_ops.go diff --git a/trie/bintrie/binary_node.go b/trie/bintrie/binary_node.go index 8905a82285..e7f57d45a2 100644 --- a/trie/bintrie/binary_node.go +++ b/trie/bintrie/binary_node.go @@ -16,140 +16,32 @@ package bintrie -import ( - "errors" - - "github.com/ethereum/go-ethereum/common" -) - -type ( - NodeFlushFn func([]byte, BinaryNode) - NodeResolverFn func([]byte, common.Hash) ([]byte, error) -) +import "github.com/ethereum/go-ethereum/common" // zero is the zero value for a 32-byte array. var zero [32]byte const ( - StemNodeWidth = 256 // Number of child per leaf node - StemSize = 31 // Number of bytes to travel before reaching a group of leaves - NodeTypeBytes = 1 // Size of node type prefix in serialization - HashSize = 32 // Size of a hash in bytes - BitmapSize = 32 // Size of the bitmap in a stem node + StemNodeWidth = 256 // Number of children per leaf node + StemSize = 31 // Number of bytes to travel before reaching a group of leaves + NodeTypeBytes = 1 // Size of node type prefix in serialization + HashSize = 32 // Size of a hash in bytes + StemBitmapSize = 32 // Size of the bitmap in a stem node (256 values = 32 bytes) ) const ( - nodeTypeStem = iota + 1 // Stem node, contains a stem and a bitmap of values + nodeTypeStem = iota + 1 nodeTypeInternal ) -// BinaryNode is an interface for a binary trie node. -type BinaryNode interface { - Get([]byte, NodeResolverFn) ([]byte, error) - Insert([]byte, []byte, NodeResolverFn, int) (BinaryNode, error) - Copy() BinaryNode - Hash() common.Hash - GetValuesAtStem([]byte, NodeResolverFn) ([][]byte, error) - InsertValuesAtStem([]byte, [][]byte, NodeResolverFn, int) (BinaryNode, error) - CollectNodes([]byte, NodeFlushFn) error - - toDot(parent, path string) string - GetHeight() int -} - -// SerializeNode serializes a binary trie node into a byte slice. -func SerializeNode(node BinaryNode) []byte { - switch n := (node).(type) { - case *InternalNode: - // InternalNode: 1 byte type + 32 bytes left hash + 32 bytes right hash - var serialized [NodeTypeBytes + HashSize + HashSize]byte - serialized[0] = nodeTypeInternal - copy(serialized[1:33], n.left.Hash().Bytes()) - copy(serialized[33:65], n.right.Hash().Bytes()) - return serialized[:] - case *StemNode: - // StemNode: 1 byte type + 31 bytes stem + 32 bytes bitmap + 256*32 bytes values - var serialized [NodeTypeBytes + StemSize + BitmapSize + StemNodeWidth*HashSize]byte - serialized[0] = nodeTypeStem - copy(serialized[NodeTypeBytes:NodeTypeBytes+StemSize], n.Stem) - bitmap := serialized[NodeTypeBytes+StemSize : NodeTypeBytes+StemSize+BitmapSize] - offset := NodeTypeBytes + StemSize + BitmapSize - for i, v := range n.Values { - if v != nil { - bitmap[i/8] |= 1 << (7 - (i % 8)) - copy(serialized[offset:offset+HashSize], v) - offset += HashSize - } - } - // Only return the actual data, not the entire array - return serialized[:offset] - default: - panic("invalid node type") - } -} - -var invalidSerializedLength = errors.New("invalid serialized node length") - -// DeserializeNode deserializes a binary trie node from a byte slice. The -// hash will be recomputed from the deserialized data. -func DeserializeNode(serialized []byte, depth int) (BinaryNode, error) { - return deserializeNode(serialized, depth, common.Hash{}, true, true) -} - -// DeserializeNodeWithHash deserializes a binary trie node from a byte slice, using the provided hash. -func DeserializeNodeWithHash(serialized []byte, depth int, hn common.Hash) (BinaryNode, error) { - return deserializeNode(serialized, depth, hn, false, false) -} - -func deserializeNode(serialized []byte, depth int, hn common.Hash, mustRecompute, dirty bool) (BinaryNode, error) { - if len(serialized) == 0 { - return Empty{}, nil +// DeserializeAndHash deserializes a node from bytes and returns its hash. +// This is a convenience function for external callers that need to compute +// the hash of a serialized node without maintaining a nodeStore. +func DeserializeAndHash(blob []byte, depth int) (common.Hash, error) { + s := newNodeStore() + ref, err := s.deserializeNode(blob, depth) + if err != nil { + return common.Hash{}, err } - - switch serialized[0] { - case nodeTypeInternal: - if len(serialized) != 65 { - return nil, invalidSerializedLength - } - return &InternalNode{ - depth: depth, - left: HashedNode(common.BytesToHash(serialized[1:33])), - right: HashedNode(common.BytesToHash(serialized[33:65])), - hash: hn, - mustRecompute: mustRecompute, - dirty: dirty, - }, nil - case nodeTypeStem: - if len(serialized) < 64 { - return nil, invalidSerializedLength - } - var values [StemNodeWidth][]byte - bitmap := serialized[NodeTypeBytes+StemSize : NodeTypeBytes+StemSize+BitmapSize] - offset := NodeTypeBytes + StemSize + BitmapSize - - for i := range StemNodeWidth { - if bitmap[i/8]>>(7-(i%8))&1 == 1 { - if len(serialized) < offset+HashSize { - return nil, invalidSerializedLength - } - values[i] = serialized[offset : offset+HashSize] - offset += HashSize - } - } - return &StemNode{ - Stem: serialized[NodeTypeBytes : NodeTypeBytes+StemSize], - Values: values[:], - depth: depth, - hash: hn, - mustRecompute: mustRecompute, - dirty: dirty, - }, nil - default: - return nil, errors.New("invalid node type") - } -} - -// ToDot converts the binary trie to a DOT language representation. Useful for debugging. -func ToDot(root BinaryNode) string { - return root.toDot("", "") + return s.computeHash(ref), nil } diff --git a/trie/bintrie/binary_node_test.go b/trie/bintrie/binary_node_test.go index 242743ba53..12ac199903 100644 --- a/trie/bintrie/binary_node_test.go +++ b/trie/bintrie/binary_node_test.go @@ -23,79 +23,100 @@ import ( "github.com/ethereum/go-ethereum/common" ) -// TestSerializeDeserializeInternalNode tests serialization and deserialization of InternalNode +// TestSerializeDeserializeInternalNode tests flat 65-byte serialization and +// deserialization of InternalNode through nodeStore. func TestSerializeDeserializeInternalNode(t *testing.T) { - // Create an internal node with two hashed children leftHash := common.HexToHash("0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef") rightHash := common.HexToHash("0xfedcba0987654321fedcba0987654321fedcba0987654321fedcba0987654321") - node := &InternalNode{ - depth: 5, - left: HashedNode(leftHash), - right: HashedNode(rightHash), - } + s := newNodeStore() + leftRef := s.newHashedRef(leftHash) + rightRef := s.newHashedRef(rightHash) - // Serialize the node - serialized := SerializeNode(node) + rootRef := s.newInternalRef(0) + rootNode := s.getInternal(rootRef.Index()) + rootNode.left = leftRef + rootNode.right = rightRef + s.root = rootRef - // Check the serialized format + // Serialize the node — flat 65-byte format + serialized := s.serializeNode(rootRef) + + // Check the serialized format: [type(1)][leftHash(32)][rightHash(32)] if serialized[0] != nodeTypeInternal { t.Errorf("Expected type byte to be %d, got %d", nodeTypeInternal, serialized[0]) } - if len(serialized) != 65 { - t.Errorf("Expected serialized length to be 65, got %d", len(serialized)) + expectedLen := NodeTypeBytes + 2*HashSize // 1 + 64 = 65 + if len(serialized) != expectedLen { + t.Errorf("Expected serialized length to be %d, got %d", expectedLen, len(serialized)) } - // Deserialize the node - deserialized, err := DeserializeNode(serialized, 5) + // Check that left and right hashes are embedded directly + if !bytes.Equal(serialized[NodeTypeBytes:NodeTypeBytes+HashSize], leftHash[:]) { + t.Error("Left hash not found at expected position") + } + if !bytes.Equal(serialized[NodeTypeBytes+HashSize:], rightHash[:]) { + t.Error("Right hash not found at expected position") + } + + // Deserialize into a new store + ds := newNodeStore() + deserialized, err := ds.deserializeNode(serialized, 0) if err != nil { t.Fatalf("Failed to deserialize node: %v", err) } - // Check that it's an internal node - internalNode, ok := deserialized.(*InternalNode) - if !ok { - t.Fatalf("Expected InternalNode, got %T", deserialized) + // Root should be an InternalNode + if deserialized.Kind() != kindInternal { + t.Fatalf("Expected kindInternal, got kind %d", deserialized.Kind()) } - // Check the depth - if internalNode.depth != 5 { - t.Errorf("Expected depth 5, got %d", internalNode.depth) + internalNode := ds.getInternal(deserialized.Index()) + if internalNode.depth != 0 { + t.Errorf("Expected depth 0, got %d", internalNode.depth) } - // Check the left and right hashes - if internalNode.left.Hash() != leftHash { - t.Errorf("Left hash mismatch: expected %x, got %x", leftHash, internalNode.left.Hash()) + // Left child should be a HashedNode with the correct hash + if internalNode.left.Kind() != kindHashed { + t.Fatalf("Expected left child to be kindHashed, got %d", internalNode.left.Kind()) + } + if ds.computeHash(internalNode.left) != leftHash { + t.Errorf("Left hash mismatch: expected %x, got %x", leftHash, ds.computeHash(internalNode.left)) } - if internalNode.right.Hash() != rightHash { - t.Errorf("Right hash mismatch: expected %x, got %x", rightHash, internalNode.right.Hash()) + // Right child should be a HashedNode with the correct hash + if internalNode.right.Kind() != kindHashed { + t.Fatalf("Expected right child to be kindHashed, got %d", internalNode.right.Kind()) + } + if ds.computeHash(internalNode.right) != rightHash { + t.Errorf("Right hash mismatch: expected %x, got %x", rightHash, ds.computeHash(internalNode.right)) } } -// TestSerializeDeserializeStemNode tests serialization and deserialization of StemNode +// TestSerializeDeserializeStemNode tests serialization and deserialization of StemNode through nodeStore. func TestSerializeDeserializeStemNode(t *testing.T) { - // Create a stem node with some values stem := make([]byte, StemSize) for i := range stem { stem[i] = byte(i) } var values [StemNodeWidth][]byte - // Add some values at different indices values[0] = common.HexToHash("0x0101010101010101010101010101010101010101010101010101010101010101").Bytes() values[10] = common.HexToHash("0x0202020202020202020202020202020202020202020202020202020202020202").Bytes() values[255] = common.HexToHash("0x0303030303030303030303030303030303030303030303030303030303030303").Bytes() - node := &StemNode{ - Stem: stem, - Values: values[:], - depth: 10, + s := newNodeStore() + ref := s.newStemRef(stem, 10) + sn := s.getStem(ref.Index()) + for i, v := range values { + if v != nil { + sn.setValue(byte(i), v) + } } // Serialize the node - serialized := SerializeNode(node) + serialized := s.serializeNode(ref) // Check the serialized format if serialized[0] != nodeTypeStem { @@ -107,31 +128,32 @@ func TestSerializeDeserializeStemNode(t *testing.T) { t.Errorf("Stem mismatch in serialized data") } - // Deserialize the node - deserialized, err := DeserializeNode(serialized, 10) + // Deserialize into a new store + ds := newNodeStore() + deserializedRef, err := ds.deserializeNode(serialized, 10) if err != nil { t.Fatalf("Failed to deserialize node: %v", err) } - // Check that it's a stem node - stemNode, ok := deserialized.(*StemNode) - if !ok { - t.Fatalf("Expected StemNode, got %T", deserialized) + if deserializedRef.Kind() != kindStem { + t.Fatalf("Expected kindStem, got kind %d", deserializedRef.Kind()) } + stemNode := ds.getStem(deserializedRef.Index()) + // Check the stem - if !bytes.Equal(stemNode.Stem, stem) { + if !bytes.Equal(stemNode.Stem[:], stem) { t.Errorf("Stem mismatch after deserialization") } // Check the values - if !bytes.Equal(stemNode.Values[0], values[0]) { + if !bytes.Equal(stemNode.getValue(0), values[0]) { t.Errorf("Value at index 0 mismatch") } - if !bytes.Equal(stemNode.Values[10], values[10]) { + if !bytes.Equal(stemNode.getValue(10), values[10]) { t.Errorf("Value at index 10 mismatch") } - if !bytes.Equal(stemNode.Values[255], values[255]) { + if !bytes.Equal(stemNode.getValue(255), values[255]) { t.Errorf("Value at index 255 mismatch") } @@ -140,43 +162,43 @@ func TestSerializeDeserializeStemNode(t *testing.T) { if i == 0 || i == 10 || i == 255 { continue } - if stemNode.Values[i] != nil { - t.Errorf("Expected nil value at index %d, got %x", i, stemNode.Values[i]) + if stemNode.hasValue(byte(i)) { + t.Errorf("Expected no value at index %d, got %x", i, stemNode.getValue(byte(i))) } } } -// TestDeserializeEmptyNode tests deserialization of empty node +// TestDeserializeEmptyNode tests deserialization of empty node. func TestDeserializeEmptyNode(t *testing.T) { - // Empty byte slice should deserialize to Empty node - deserialized, err := DeserializeNode([]byte{}, 0) + s := newNodeStore() + deserialized, err := s.deserializeNode([]byte{}, 0) if err != nil { t.Fatalf("Failed to deserialize empty node: %v", err) } - _, ok := deserialized.(Empty) - if !ok { - t.Fatalf("Expected Empty node, got %T", deserialized) + if !deserialized.IsEmpty() { + t.Fatalf("Expected emptyRef, got kind %d", deserialized.Kind()) } } -// TestDeserializeInvalidType tests deserialization with invalid type byte +// TestDeserializeInvalidType tests deserialization with invalid type byte. func TestDeserializeInvalidType(t *testing.T) { - // Create invalid serialized data with unknown type byte + s := newNodeStore() invalidData := []byte{99, 0, 0, 0} // Type byte 99 is invalid - _, err := DeserializeNode(invalidData, 0) + _, err := s.deserializeNode(invalidData, 0) if err == nil { t.Fatal("Expected error for invalid type byte, got nil") } } -// TestDeserializeInvalidLength tests deserialization with invalid data length +// TestDeserializeInvalidLength tests deserialization with invalid data length. func TestDeserializeInvalidLength(t *testing.T) { - // InternalNode with type byte 1 but wrong length - invalidData := []byte{nodeTypeInternal, 0, 0} // Too short for internal node + s := newNodeStore() + // InternalNode with valid type byte but wrong length (needs exactly 65 bytes) + invalidData := []byte{nodeTypeInternal, 0, 0, 0} - _, err := DeserializeNode(invalidData, 0) + _, err := s.deserializeNode(invalidData, 0) if err == nil { t.Fatal("Expected error for invalid data length, got nil") } @@ -186,7 +208,7 @@ func TestDeserializeInvalidLength(t *testing.T) { } } -// TestKeyToPath tests the keyToPath function +// TestKeyToPath tests the keyToPath function. func TestKeyToPath(t *testing.T) { tests := []struct { name string @@ -218,14 +240,14 @@ func TestKeyToPath(t *testing.T) { }, { name: "max valid depth", - depth: StemSize * 8, + depth: StemSize*8 - 1, key: make([]byte, HashSize), - expected: make([]byte, StemSize*8+1), + expected: make([]byte, StemSize*8), wantErr: false, }, { name: "depth too large", - depth: StemSize*8 + 1, + depth: StemSize * 8, key: make([]byte, HashSize), wantErr: true, }, diff --git a/trie/bintrie/empty.go b/trie/bintrie/empty.go deleted file mode 100644 index c47e284dac..0000000000 --- a/trie/bintrie/empty.go +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright 2025 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 . - -package bintrie - -import ( - "slices" - - "github.com/ethereum/go-ethereum/common" -) - -type Empty struct{} - -func (e Empty) Get(_ []byte, _ NodeResolverFn) ([]byte, error) { - return nil, nil -} - -func (e Empty) Insert(key []byte, value []byte, _ NodeResolverFn, depth int) (BinaryNode, error) { - var values [256][]byte - values[key[31]] = value - return &StemNode{ - Stem: slices.Clone(key[:31]), - Values: values[:], - depth: depth, - mustRecompute: true, - dirty: true, - }, nil -} - -func (e Empty) Copy() BinaryNode { - return Empty{} -} - -func (e Empty) Hash() common.Hash { - return common.Hash{} -} - -func (e Empty) GetValuesAtStem(_ []byte, _ NodeResolverFn) ([][]byte, error) { - var values [256][]byte - return values[:], nil -} - -func (e Empty) InsertValuesAtStem(key []byte, values [][]byte, _ NodeResolverFn, depth int) (BinaryNode, error) { - return &StemNode{ - Stem: slices.Clone(key[:31]), - Values: values, - depth: depth, - mustRecompute: true, - dirty: true, - }, nil -} - -func (e Empty) CollectNodes(_ []byte, _ NodeFlushFn) error { - return nil -} - -func (e Empty) toDot(parent string, path string) string { - return "" -} - -func (e Empty) GetHeight() int { - return 0 -} diff --git a/trie/bintrie/empty_test.go b/trie/bintrie/empty_test.go deleted file mode 100644 index 4da1ed15a0..0000000000 --- a/trie/bintrie/empty_test.go +++ /dev/null @@ -1,264 +0,0 @@ -// Copyright 2025 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 . - -package bintrie - -import ( - "bytes" - "testing" - - "github.com/ethereum/go-ethereum/common" -) - -// TestEmptyGet tests the Get method -func TestEmptyGet(t *testing.T) { - node := Empty{} - - key := make([]byte, 32) - value, err := node.Get(key, nil) - if err != nil { - t.Fatalf("Unexpected error: %v", err) - } - - if value != nil { - t.Errorf("Expected nil value from empty node, got %x", value) - } -} - -// TestEmptyInsert tests the Insert method -func TestEmptyInsert(t *testing.T) { - node := Empty{} - - key := make([]byte, 32) - key[0] = 0x12 - key[31] = 0x34 - value := common.HexToHash("0xabcd").Bytes() - - newNode, err := node.Insert(key, value, nil, 0) - if err != nil { - t.Fatalf("Failed to insert: %v", err) - } - - // Should create a StemNode - stemNode, ok := newNode.(*StemNode) - if !ok { - t.Fatalf("Expected StemNode, got %T", newNode) - } - - // Check the stem (first 31 bytes of key) - if !bytes.Equal(stemNode.Stem, key[:31]) { - t.Errorf("Stem mismatch: expected %x, got %x", key[:31], stemNode.Stem) - } - - // Check the value at the correct index (last byte of key) - if !bytes.Equal(stemNode.Values[key[31]], value) { - t.Errorf("Value mismatch at index %d: expected %x, got %x", key[31], value, stemNode.Values[key[31]]) - } - - // Check that other values are nil - for i := 0; i < 256; i++ { - if i != int(key[31]) && stemNode.Values[i] != nil { - t.Errorf("Expected nil value at index %d, got %x", i, stemNode.Values[i]) - } - } -} - -// TestEmptyCopy tests the Copy method -func TestEmptyCopy(t *testing.T) { - node := Empty{} - - copied := node.Copy() - copiedEmpty, ok := copied.(Empty) - if !ok { - t.Fatalf("Expected Empty, got %T", copied) - } - - // Both should be empty - if node != copiedEmpty { - // Empty is a zero-value struct, so copies should be equal - t.Errorf("Empty nodes should be equal") - } -} - -// TestEmptyHash tests the Hash method -func TestEmptyHash(t *testing.T) { - node := Empty{} - - hash := node.Hash() - - // Empty node should have zero hash - if hash != (common.Hash{}) { - t.Errorf("Expected zero hash for empty node, got %x", hash) - } -} - -// TestEmptyGetValuesAtStem tests the GetValuesAtStem method -func TestEmptyGetValuesAtStem(t *testing.T) { - node := Empty{} - - stem := make([]byte, 31) - values, err := node.GetValuesAtStem(stem, nil) - if err != nil { - t.Fatalf("Unexpected error: %v", err) - } - - // Should return an array of 256 nil values - if len(values) != 256 { - t.Errorf("Expected 256 values, got %d", len(values)) - } - - for i, v := range values { - if v != nil { - t.Errorf("Expected nil value at index %d, got %x", i, v) - } - } -} - -// TestEmptyInsertValuesAtStem tests the InsertValuesAtStem method -func TestEmptyInsertValuesAtStem(t *testing.T) { - node := Empty{} - - stem := make([]byte, 31) - stem[0] = 0x42 - - var values [256][]byte - values[0] = common.HexToHash("0x0101").Bytes() - values[10] = common.HexToHash("0x0202").Bytes() - values[255] = common.HexToHash("0x0303").Bytes() - - newNode, err := node.InsertValuesAtStem(stem, values[:], nil, 5) - if err != nil { - t.Fatalf("Failed to insert values: %v", err) - } - - // Should create a StemNode - stemNode, ok := newNode.(*StemNode) - if !ok { - t.Fatalf("Expected StemNode, got %T", newNode) - } - - // Check the stem - if !bytes.Equal(stemNode.Stem, stem) { - t.Errorf("Stem mismatch: expected %x, got %x", stem, stemNode.Stem) - } - - // Check the depth - if stemNode.depth != 5 { - t.Errorf("Depth mismatch: expected 5, got %d", stemNode.depth) - } - - // Check the values - if !bytes.Equal(stemNode.Values[0], values[0]) { - t.Error("Value at index 0 mismatch") - } - if !bytes.Equal(stemNode.Values[10], values[10]) { - t.Error("Value at index 10 mismatch") - } - if !bytes.Equal(stemNode.Values[255], values[255]) { - t.Error("Value at index 255 mismatch") - } - - // Check that values is the same slice (not a copy) - if &stemNode.Values[0] != &values[0] { - t.Error("Expected values to be the same slice reference") - } -} - -// TestEmptyCollectNodes tests the CollectNodes method -func TestEmptyCollectNodes(t *testing.T) { - node := Empty{} - - var collected []BinaryNode - flushFn := func(path []byte, n BinaryNode) { - collected = append(collected, n) - } - - err := node.CollectNodes([]byte{0, 1, 0}, flushFn) - if err != nil { - t.Fatalf("Unexpected error: %v", err) - } - - // Should not collect anything for empty node - if len(collected) != 0 { - t.Errorf("Expected no collected nodes for empty, got %d", len(collected)) - } -} - -// TestEmptyToDot tests the toDot method -func TestEmptyToDot(t *testing.T) { - node := Empty{} - - dot := node.toDot("parent", "010") - - // Should return empty string for empty node - if dot != "" { - t.Errorf("Expected empty string for empty node toDot, got %s", dot) - } -} - -// TestEmptyGetHeight tests the GetHeight method -func TestEmptyGetHeight(t *testing.T) { - node := Empty{} - - height := node.GetHeight() - - // Empty node should have height 0 - if height != 0 { - t.Errorf("Expected height 0 for empty node, got %d", height) - } -} - -// TestEmptyInsertMarksDirty verifies that a StemNode produced by Empty.Insert -// is marked dirty. Without this, CollectNodes would skip the freshly created -// stem and its blob would never reach disk, producing "missing trie node" -// errors on subsequent reads. -func TestEmptyInsertMarksDirty(t *testing.T) { - key := make([]byte, 32) - key[0] = 0xaa - val := make([]byte, 32) - val[0] = 0xbb - n, err := Empty{}.Insert(key, val, nil, 0) - if err != nil { - t.Fatalf("Insert: %v", err) - } - sn, ok := n.(*StemNode) - if !ok { - t.Fatalf("expected *StemNode, got %T", n) - } - if !sn.dirty { - t.Fatalf("stem produced by Empty.Insert must have dirty=true") - } -} - -// TestEmptyInsertValuesAtStemMarksDirty is the analogous guard for the -// bulk-insert entry point. Fresh stems created here must be dirty. -func TestEmptyInsertValuesAtStemMarksDirty(t *testing.T) { - key := make([]byte, 32) - key[0] = 0xcc - values := make([][]byte, 256) - values[0] = make([]byte, 32) - n, err := Empty{}.InsertValuesAtStem(key, values, nil, 3) - if err != nil { - t.Fatalf("InsertValuesAtStem: %v", err) - } - sn, ok := n.(*StemNode) - if !ok { - t.Fatalf("expected *StemNode, got %T", n) - } - if !sn.dirty { - t.Fatalf("stem produced by Empty.InsertValuesAtStem must have dirty=true") - } -} diff --git a/trie/bintrie/hashed_node.go b/trie/bintrie/hashed_node.go index e44c6d1e8a..b176df079b 100644 --- a/trie/bintrie/hashed_node.go +++ b/trie/bintrie/hashed_node.go @@ -16,75 +16,10 @@ package bintrie -import ( - "errors" - "fmt" - - "github.com/ethereum/go-ethereum/common" -) +import "github.com/ethereum/go-ethereum/common" +// HashedNode is an unresolved node — only its hash is known. type HashedNode common.Hash -func (h HashedNode) Get(_ []byte, _ NodeResolverFn) ([]byte, error) { - panic("not implemented") // TODO: Implement -} - -func (h HashedNode) Insert(key []byte, value []byte, resolver NodeResolverFn, depth int) (BinaryNode, error) { - return nil, errors.New("insert not implemented for hashed node") -} - -func (h HashedNode) Copy() BinaryNode { - nh := common.Hash(h) - return HashedNode(nh) -} - -func (h HashedNode) Hash() common.Hash { - return common.Hash(h) -} - -func (h HashedNode) GetValuesAtStem(_ []byte, _ NodeResolverFn) ([][]byte, error) { - return nil, errors.New("attempted to get values from an unresolved node") -} - -func (h HashedNode) InsertValuesAtStem(stem []byte, values [][]byte, resolver NodeResolverFn, depth int) (BinaryNode, error) { - // Step 1: Generate the path for this node's position in the tree - path, err := keyToPath(depth, stem) - if err != nil { - return nil, fmt.Errorf("InsertValuesAtStem path generation error: %w", err) - } - - if resolver == nil { - return nil, errors.New("InsertValuesAtStem resolve error: resolver is nil") - } - - // Step 2: Resolve the hashed node to get the actual node data - data, err := resolver(path, common.Hash(h)) - if err != nil { - return nil, fmt.Errorf("InsertValuesAtStem resolve error: %w", err) - } - - // Step 3: Deserialize the resolved data into a concrete node - node, err := DeserializeNodeWithHash(data, depth, common.Hash(h)) - if err != nil { - return nil, fmt.Errorf("InsertValuesAtStem node deserialization error: %w", err) - } - - // Step 4: Call InsertValuesAtStem on the resolved concrete node - return node.InsertValuesAtStem(stem, values, resolver, depth) -} - -func (h HashedNode) toDot(parent string, path string) string { - me := fmt.Sprintf("hash%s", path) - ret := fmt.Sprintf("%s [label=\"%x\"]\n", me, h) - ret = fmt.Sprintf("%s %s -> %s\n", ret, parent, me) - return ret -} - -func (h HashedNode) CollectNodes([]byte, NodeFlushFn) error { - // HashedNodes are already persisted in the database and don't need to be collected. - return nil -} - -func (h HashedNode) GetHeight() int { - panic("tried to get the height of a hashed node, this is a bug") -} +// Hash returns the node's hash. +func (h HashedNode) Hash() common.Hash { return common.Hash(h) } diff --git a/trie/bintrie/hashed_node_test.go b/trie/bintrie/hashed_node_test.go index f9e6984888..ae77b7c570 100644 --- a/trie/bintrie/hashed_node_test.go +++ b/trie/bintrie/hashed_node_test.go @@ -18,180 +18,137 @@ package bintrie import ( "bytes" + "errors" "testing" "github.com/ethereum/go-ethereum/common" ) -// TestHashedNodeHash tests the Hash method +// TestHashedNodeHash tests the Hash method via nodeStore. func TestHashedNodeHash(t *testing.T) { hash := common.HexToHash("0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef") - node := HashedNode(hash) + s := newNodeStore() + ref := s.newHashedRef(hash) - // Hash should return the stored hash - if node.Hash() != hash { - t.Errorf("Hash mismatch: expected %x, got %x", hash, node.Hash()) + if s.computeHash(ref) != hash { + t.Errorf("Hash mismatch: expected %x, got %x", hash, s.computeHash(ref)) } } -// TestHashedNodeCopy tests the Copy method +// TestHashedNodeCopy tests the Copy method via nodeStore. func TestHashedNodeCopy(t *testing.T) { hash := common.HexToHash("0xabcdef") - node := HashedNode(hash) + s := newNodeStore() + ref := s.newHashedRef(hash) + s.root = ref - copied := node.Copy() - copiedHash, ok := copied.(HashedNode) - if !ok { - t.Fatalf("Expected HashedNode, got %T", copied) - } + ns := s.Copy() + copiedHash := ns.computeHash(ns.root) - // Hash should be the same - if common.Hash(copiedHash) != hash { + if copiedHash != hash { t.Errorf("Hash mismatch after copy: expected %x, got %x", hash, copiedHash) } - - // But should be a different object - if &node == &copiedHash { - t.Error("Copy returned same object reference") - } -} - -// TestHashedNodeInsert tests that Insert returns an error -func TestHashedNodeInsert(t *testing.T) { - node := HashedNode(common.HexToHash("0x1234")) - - key := make([]byte, HashSize) - value := make([]byte, HashSize) - - _, err := node.Insert(key, value, nil, 0) - if err == nil { - t.Fatal("Expected error for Insert on HashedNode") - } - - if err.Error() != "insert not implemented for hashed node" { - t.Errorf("Unexpected error message: %v", err) - } -} - -// TestHashedNodeGetValuesAtStem tests that GetValuesAtStem returns an error -func TestHashedNodeGetValuesAtStem(t *testing.T) { - node := HashedNode(common.HexToHash("0x1234")) - - stem := make([]byte, StemSize) - _, err := node.GetValuesAtStem(stem, nil) - if err == nil { - t.Fatal("Expected error for GetValuesAtStem on HashedNode") - } - - if err.Error() != "attempted to get values from an unresolved node" { - t.Errorf("Unexpected error message: %v", err) - } } -// TestHashedNodeInsertValuesAtStem tests that InsertValuesAtStem returns an error +// TestHashedNodeInsertValuesAtStem tests InsertValuesAtStem resolution via nodeStore. func TestHashedNodeInsertValuesAtStem(t *testing.T) { - node := HashedNode(common.HexToHash("0x1234")) + // Test 1: nil resolver should return an error + s := newNodeStore() + hashedRef := s.newHashedRef(common.HexToHash("0x1234")) + s.root = hashedRef stem := make([]byte, StemSize) values := make([][]byte, StemNodeWidth) - // Test 1: nil resolver should return an error - _, err := node.InsertValuesAtStem(stem, values, nil, 0) + err := s.InsertValuesAtStem(stem, values, nil) if err == nil { - t.Fatal("Expected error for InsertValuesAtStem on HashedNode with nil resolver") - } - - if err.Error() != "InsertValuesAtStem resolve error: resolver is nil" { - t.Errorf("Unexpected error message: %v", err) + t.Fatal("Expected error for InsertValuesAtStem with nil resolver") } // Test 2: mock resolver returning invalid data should return deserialization error mockResolver := func(path []byte, hash common.Hash) ([]byte, error) { - // Return invalid/nonsense data that cannot be deserialized return []byte{0xff, 0xff, 0xff}, nil } - _, err = node.InsertValuesAtStem(stem, values, mockResolver, 0) - if err == nil { - t.Fatal("Expected error for InsertValuesAtStem on HashedNode with invalid resolver data") - } + s2 := newNodeStore() + hashedRef2 := s2.newHashedRef(common.HexToHash("0x1234")) + s2.root = hashedRef2 - expectedPrefix := "InsertValuesAtStem node deserialization error:" - if len(err.Error()) < len(expectedPrefix) || err.Error()[:len(expectedPrefix)] != expectedPrefix { - t.Errorf("Expected deserialization error, got: %v", err) + err = s2.InsertValuesAtStem(stem, values, mockResolver) + if err == nil { + t.Fatal("Expected error for InsertValuesAtStem with invalid resolver data") } // Test 3: mock resolver returning valid serialized node should succeed stem = make([]byte, StemSize) stem[0] = 0xaa - var originalValues [StemNodeWidth][]byte + originalValues := make([][]byte, StemNodeWidth) originalValues[0] = common.HexToHash("0x1111111111111111111111111111111111111111111111111111111111111111").Bytes() originalValues[1] = common.HexToHash("0x2222222222222222222222222222222222222222222222222222222222222222").Bytes() - originalNode := &StemNode{ - Stem: stem, - Values: originalValues[:], - depth: 0, + // Build the serialized node + rs := newNodeStore() + ref := rs.newStemRef(stem, 0) + sn := rs.getStem(ref.Index()) + for i, v := range originalValues { + if v != nil { + sn.setValue(byte(i), v) + } } + serialized := rs.serializeNode(ref) - // Serialize the node - serialized := SerializeNode(originalNode) - - // Create a mock resolver that returns the serialized node validResolver := func(path []byte, hash common.Hash) ([]byte, error) { return serialized, nil } - var newValues [StemNodeWidth][]byte + s3 := newNodeStore() + hashedRef3 := s3.newHashedRef(common.HexToHash("0x1234")) + s3.root = hashedRef3 + + newValues := make([][]byte, StemNodeWidth) newValues[2] = common.HexToHash("0x3333333333333333333333333333333333333333333333333333333333333333").Bytes() - resolvedNode, err := node.InsertValuesAtStem(stem, newValues[:], validResolver, 0) + err = s3.InsertValuesAtStem(stem, newValues, validResolver) if err != nil { t.Fatalf("Expected successful resolution and insertion, got error: %v", err) } - resultStem, ok := resolvedNode.(*StemNode) - if !ok { - t.Fatalf("Expected resolved node to be *StemNode, got %T", resolvedNode) + // Verify original values are preserved + retrieved, err := s3.GetValuesAtStem(stem, nil) + if err != nil { + t.Fatal(err) } - - if !bytes.Equal(resultStem.Stem, stem) { - t.Errorf("Stem mismatch: expected %x, got %x", stem, resultStem.Stem) + if !bytes.Equal(retrieved[0], originalValues[0]) { + t.Errorf("Original value at index 0 not preserved") } - - // Verify the original values are preserved - if !bytes.Equal(resultStem.Values[0], originalValues[0]) { - t.Errorf("Original value at index 0 not preserved: expected %x, got %x", originalValues[0], resultStem.Values[0]) + if !bytes.Equal(retrieved[1], originalValues[1]) { + t.Errorf("Original value at index 1 not preserved") } - if !bytes.Equal(resultStem.Values[1], originalValues[1]) { - t.Errorf("Original value at index 1 not preserved: expected %x, got %x", originalValues[1], resultStem.Values[1]) - } - - // Verify the new value was inserted - if !bytes.Equal(resultStem.Values[2], newValues[2]) { - t.Errorf("New value at index 2 not inserted correctly: expected %x, got %x", newValues[2], resultStem.Values[2]) + if !bytes.Equal(retrieved[2], newValues[2]) { + t.Errorf("New value at index 2 not inserted correctly") } } -// TestHashedNodeToDot tests the toDot method for visualization -func TestHashedNodeToDot(t *testing.T) { - hash := common.HexToHash("0x1234") - node := HashedNode(hash) +// TestHashedNodeGetError tests that getting through an unresolved HashedNode root returns error. +func TestHashedNodeGetError(t *testing.T) { + s := newNodeStore() + // Create root as hashed, then try to resolve through InternalNode parent + rootRef := s.newInternalRef(0) + rootNode := s.getInternal(rootRef.Index()) + hashedLeft := s.newHashedRef(common.HexToHash("0x1234")) + rootNode.left = hashedLeft + rootNode.right = emptyRef + s.root = rootRef - dot := node.toDot("parent", "010") + key := make([]byte, 32) // goes left + key[31] = 5 - // Should contain the hash value and parent connection - expectedHash := "hash010" - if !contains(dot, expectedHash) { - t.Errorf("Expected dot output to contain %s", expectedHash) + resolver := func(path []byte, hash common.Hash) ([]byte, error) { + return nil, errors.New("node not found") } - if !contains(dot, "parent -> hash010") { - t.Error("Expected dot output to contain parent connection") + _, err := s.Get(key, resolver) + if err == nil { + t.Fatal("Expected error when resolver fails") } } - -// Helper function -func contains(s, substr string) bool { - return len(s) >= len(substr) && s != "" && len(substr) > 0 -} diff --git a/trie/bintrie/internal_node.go b/trie/bintrie/internal_node.go index 811f65bcd8..b83cb92d87 100644 --- a/trie/bintrie/internal_node.go +++ b/trie/bintrie/internal_node.go @@ -17,35 +17,13 @@ package bintrie import ( - "crypto/sha256" "errors" - "fmt" - "math/bits" - "runtime" - "sync" "github.com/ethereum/go-ethereum/common" ) -// parallelDepth returns the tree depth below which Hash() spawns goroutines. -func parallelDepth() int { - return min(bits.Len(uint(runtime.NumCPU())), 8) -} - -// isDirty reports whether a BinaryNode child needs rehashing. -func isDirty(n BinaryNode) bool { - switch v := n.(type) { - case *InternalNode: - return v.mustRecompute - case *StemNode: - return v.mustRecompute - default: - return false - } -} - func keyToPath(depth int, key []byte) ([]byte, error) { - if depth > 31*8 { + if depth >= 31*8 { return nil, errors.New("node too deep") } path := make([]byte, 0, depth+1) @@ -56,252 +34,12 @@ func keyToPath(depth int, key []byte) ([]byte, error) { return path, nil } -// InternalNode is a binary trie internal node. +// Invariant: dirty=false implies mustRecompute=false. Every mutation that +// invalidates the cached hash MUST also mark the blob for re-flush. type InternalNode struct { - left, right BinaryNode - depth int - - mustRecompute bool // true if the hash needs to be recomputed - dirty bool // true if the node's on-disk blob is stale (needs flush) - hash common.Hash // cached hash when mustRecompute == false -} - -// GetValuesAtStem retrieves the group of values located at the given stem key. -func (bt *InternalNode) GetValuesAtStem(stem []byte, resolver NodeResolverFn) ([][]byte, error) { - if bt.depth > 31*8 { - return nil, errors.New("node too deep") - } - - bit := stem[bt.depth/8] >> (7 - (bt.depth % 8)) & 1 - if bit == 0 { - if hn, ok := bt.left.(HashedNode); ok { - path, err := keyToPath(bt.depth, stem) - if err != nil { - return nil, fmt.Errorf("GetValuesAtStem resolve error: %w", err) - } - data, err := resolver(path, common.Hash(hn)) - if err != nil { - return nil, fmt.Errorf("GetValuesAtStem resolve error: %w", err) - } - node, err := DeserializeNodeWithHash(data, bt.depth+1, common.Hash(hn)) - if err != nil { - return nil, fmt.Errorf("GetValuesAtStem node deserialization error: %w", err) - } - bt.left = node - } - return bt.left.GetValuesAtStem(stem, resolver) - } - - if hn, ok := bt.right.(HashedNode); ok { - path, err := keyToPath(bt.depth, stem) - if err != nil { - return nil, fmt.Errorf("GetValuesAtStem resolve error: %w", err) - } - data, err := resolver(path, common.Hash(hn)) - if err != nil { - return nil, fmt.Errorf("GetValuesAtStem resolve error: %w", err) - } - node, err := DeserializeNodeWithHash(data, bt.depth+1, common.Hash(hn)) - if err != nil { - return nil, fmt.Errorf("GetValuesAtStem node deserialization error: %w", err) - } - bt.right = node - } - return bt.right.GetValuesAtStem(stem, resolver) -} - -// Get retrieves the value for the given key. -func (bt *InternalNode) Get(key []byte, resolver NodeResolverFn) ([]byte, error) { - values, err := bt.GetValuesAtStem(key[:31], resolver) - if err != nil { - return nil, fmt.Errorf("get error: %w", err) - } - if values == nil { - return nil, nil - } - return values[key[31]], nil -} - -// Insert inserts a new key-value pair into the trie. -func (bt *InternalNode) Insert(key []byte, value []byte, resolver NodeResolverFn, depth int) (BinaryNode, error) { - var values [256][]byte - values[key[31]] = value - return bt.InsertValuesAtStem(key[:31], values[:], resolver, depth) -} - -// Copy creates a deep copy of the node. -func (bt *InternalNode) Copy() BinaryNode { - return &InternalNode{ - left: bt.left.Copy(), - right: bt.right.Copy(), - depth: bt.depth, - mustRecompute: bt.mustRecompute, - dirty: bt.dirty, - hash: bt.hash, - } -} - -// Hash returns the hash of the node. -func (bt *InternalNode) Hash() common.Hash { - if !bt.mustRecompute { - return bt.hash - } - - // At shallow depths, parallelize when both children need rehashing: - // hash left subtree in a goroutine, right subtree inline, then combine. - // Skip goroutine overhead when only one child is dirty (common case - // for narrow state updates that touch a single path through the trie). - if bt.depth < parallelDepth() && isDirty(bt.left) && isDirty(bt.right) { - var input [64]byte - var lh common.Hash - var wg sync.WaitGroup - wg.Add(1) - go func() { - defer wg.Done() - lh = bt.left.Hash() - }() - rh := bt.right.Hash() - copy(input[32:], rh[:]) - wg.Wait() - copy(input[:32], lh[:]) - bt.hash = sha256.Sum256(input[:]) - bt.mustRecompute = false - return bt.hash - } - - // Deeper nodes: sequential using pooled hasher (goroutine overhead > hash cost) - h := newSha256() - defer returnSha256(h) - if bt.left != nil { - h.Write(bt.left.Hash().Bytes()) - } else { - h.Write(zero[:]) - } - if bt.right != nil { - h.Write(bt.right.Hash().Bytes()) - } else { - h.Write(zero[:]) - } - bt.hash = common.BytesToHash(h.Sum(nil)) - bt.mustRecompute = false - return bt.hash -} - -// InsertValuesAtStem inserts a full value group at the given stem in the internal node. -// Already-existing values will be overwritten. -func (bt *InternalNode) InsertValuesAtStem(stem []byte, values [][]byte, resolver NodeResolverFn, depth int) (BinaryNode, error) { - var err error - bit := stem[bt.depth/8] >> (7 - (bt.depth % 8)) & 1 - if bit == 0 { - if bt.left == nil { - bt.left = Empty{} - } - - if hn, ok := bt.left.(HashedNode); ok { - path, err := keyToPath(bt.depth, stem) - if err != nil { - return nil, fmt.Errorf("InsertValuesAtStem resolve error: %w", err) - } - data, err := resolver(path, common.Hash(hn)) - if err != nil { - return nil, fmt.Errorf("InsertValuesAtStem resolve error: %w", err) - } - node, err := DeserializeNodeWithHash(data, bt.depth+1, common.Hash(hn)) - if err != nil { - return nil, fmt.Errorf("InsertValuesAtStem node deserialization error: %w", err) - } - bt.left = node - } - - bt.left, err = bt.left.InsertValuesAtStem(stem, values, resolver, depth+1) - bt.mustRecompute = true - bt.dirty = true - return bt, err - } - - if bt.right == nil { - bt.right = Empty{} - } - - if hn, ok := bt.right.(HashedNode); ok { - path, err := keyToPath(bt.depth, stem) - if err != nil { - return nil, fmt.Errorf("InsertValuesAtStem resolve error: %w", err) - } - data, err := resolver(path, common.Hash(hn)) - if err != nil { - return nil, fmt.Errorf("InsertValuesAtStem resolve error: %w", err) - } - node, err := DeserializeNodeWithHash(data, bt.depth+1, common.Hash(hn)) - if err != nil { - return nil, fmt.Errorf("InsertValuesAtStem node deserialization error: %w", err) - } - bt.right = node - } - - bt.right, err = bt.right.InsertValuesAtStem(stem, values, resolver, depth+1) - bt.mustRecompute = true - bt.dirty = true - return bt, err -} - -// CollectNodes collects all child nodes at a given path, and flushes it -// into the provided node collector. Clean subtrees (dirty == false) are -// skipped. -func (bt *InternalNode) CollectNodes(path []byte, flushfn NodeFlushFn) error { - if !bt.dirty { - return nil - } - if bt.left != nil { - var p [256]byte - copy(p[:], path) - childpath := p[:len(path)] - childpath = append(childpath, 0) - if err := bt.left.CollectNodes(childpath, flushfn); err != nil { - return err - } - } - if bt.right != nil { - var p [256]byte - copy(p[:], path) - childpath := p[:len(path)] - childpath = append(childpath, 1) - if err := bt.right.CollectNodes(childpath, flushfn); err != nil { - return err - } - } - flushfn(path, bt) - bt.dirty = false - return nil -} - -// GetHeight returns the height of the node. -func (bt *InternalNode) GetHeight() int { - var ( - leftHeight int - rightHeight int - ) - if bt.left != nil { - leftHeight = bt.left.GetHeight() - } - if bt.right != nil { - rightHeight = bt.right.GetHeight() - } - return 1 + max(leftHeight, rightHeight) -} - -func (bt *InternalNode) toDot(parent, path string) string { - me := fmt.Sprintf("internal%s", path) - ret := fmt.Sprintf("%s [label=\"I: %x\"]\n", me, bt.Hash()) - if len(parent) > 0 { - ret = fmt.Sprintf("%s %s -> %s\n", ret, parent, me) - } - - if bt.left != nil { - ret = fmt.Sprintf("%s%s", ret, bt.left.toDot(me, fmt.Sprintf("%s%02x", path, 0))) - } - if bt.right != nil { - ret = fmt.Sprintf("%s%s", ret, bt.right.toDot(me, fmt.Sprintf("%s%02x", path, 1))) - } - return ret + left, right nodeRef + depth uint8 + mustRecompute bool // hash is stale (cleared by Hash) + dirty bool // on-disk blob is stale (cleared by CollectNodes) + hash common.Hash } diff --git a/trie/bintrie/internal_node_test.go b/trie/bintrie/internal_node_test.go index ddcec8085d..8d5a75de8c 100644 --- a/trie/bintrie/internal_node_test.go +++ b/trie/bintrie/internal_node_test.go @@ -24,35 +24,33 @@ import ( "github.com/ethereum/go-ethereum/common" ) -// TestInternalNodeGet tests the Get method +// TestInternalNodeGet tests the Get method via nodeStore. func TestInternalNodeGet(t *testing.T) { - // Create a simple tree structure + s := newNodeStore() + leftStem := make([]byte, 31) rightStem := make([]byte, 31) - rightStem[0] = 0x80 // First bit is 1 + rightStem[0] = 0x80 - var leftValues, rightValues [256][]byte + leftValues := make([][]byte, 256) leftValues[0] = common.HexToHash("0x0101").Bytes() + rightValues := make([][]byte, 256) rightValues[0] = common.HexToHash("0x0202").Bytes() - node := &InternalNode{ - depth: 0, - left: &StemNode{ - Stem: leftStem, - Values: leftValues[:], - depth: 1, - }, - right: &StemNode{ - Stem: rightStem, - Values: rightValues[:], - depth: 1, - }, + // Build tree: root -> left stem, right stem + // Insert left stem values + s.root = emptyRef + if err := s.InsertValuesAtStem(leftStem, leftValues, nil); err != nil { + t.Fatal(err) + } + if err := s.InsertValuesAtStem(rightStem, rightValues, nil); err != nil { + t.Fatal(err) } // Get value from left subtree leftKey := make([]byte, 32) leftKey[31] = 0 - value, err := node.Get(leftKey, nil) + value, err := s.Get(leftKey, nil) if err != nil { t.Fatalf("Failed to get left value: %v", err) } @@ -64,7 +62,7 @@ func TestInternalNodeGet(t *testing.T) { rightKey := make([]byte, 32) rightKey[0] = 0x80 rightKey[31] = 0 - value, err = node.Get(rightKey, nil) + value, err = s.Get(rightKey, nil) if err != nil { t.Fatalf("Failed to get right value: %v", err) } @@ -73,29 +71,26 @@ func TestInternalNodeGet(t *testing.T) { } } -// TestInternalNodeGetWithResolver tests Get with HashedNode resolution +// TestInternalNodeGetWithResolver tests Get with HashedNode resolution via nodeStore. func TestInternalNodeGetWithResolver(t *testing.T) { - // Create an internal node with a hashed child - hashedChild := HashedNode(common.HexToHash("0x1234")) - - node := &InternalNode{ - depth: 0, - left: hashedChild, - right: Empty{}, - } + // Create a store with an internal node containing a hashed child + s := newNodeStore() + hashedChild := s.newHashedRef(common.HexToHash("0x1234")) + rootRef := s.newInternalRef(0) + rootNode := s.getInternal(rootRef.Index()) + rootNode.left = hashedChild + rootNode.right = emptyRef + s.root = rootRef // Mock resolver that returns a stem node resolver := func(path []byte, hash common.Hash) ([]byte, error) { - if hash == common.Hash(hashedChild) { + if hash == common.HexToHash("0x1234") { + rs := newNodeStore() stem := make([]byte, 31) - var values [256][]byte - values[5] = common.HexToHash("0xabcd").Bytes() - stemNode := &StemNode{ - Stem: stem, - Values: values[:], - depth: 1, - } - return SerializeNode(stemNode), nil + ref := rs.newStemRef(stem, 1) + sn := rs.getStem(ref.Index()) + sn.setValue(5, common.HexToHash("0xabcd").Bytes()) + return rs.serializeNode(ref), nil } return nil, errors.New("node not found") } @@ -103,7 +98,7 @@ func TestInternalNodeGetWithResolver(t *testing.T) { // Get value through the hashed node key := make([]byte, 32) key[31] = 5 - value, err := node.Get(key, resolver) + value, err := s.Get(key, resolver) if err != nil { t.Fatalf("Failed to get value: %v", err) } @@ -114,179 +109,113 @@ func TestInternalNodeGetWithResolver(t *testing.T) { } } -// TestInternalNodeInsert tests the Insert method +// TestInternalNodeInsert tests the Insert method via nodeStore. func TestInternalNodeInsert(t *testing.T) { - // Start with an internal node with empty children - node := &InternalNode{ - depth: 0, - left: Empty{}, - right: Empty{}, - } + s := newNodeStore() - // Insert a value into the left subtree leftKey := make([]byte, 32) leftKey[31] = 10 leftValue := common.HexToHash("0x0101").Bytes() - newNode, err := node.Insert(leftKey, leftValue, nil, 0) - if err != nil { + if err := s.Insert(leftKey, leftValue, nil); err != nil { t.Fatalf("Failed to insert: %v", err) } - internalNode, ok := newNode.(*InternalNode) - if !ok { - t.Fatalf("Expected InternalNode, got %T", newNode) - } - - // Check that left child is now a StemNode - leftStem, ok := internalNode.left.(*StemNode) - if !ok { - t.Fatalf("Expected left child to be StemNode, got %T", internalNode.left) - } - - // Check the inserted value - if !bytes.Equal(leftStem.Values[10], leftValue) { - t.Errorf("Value mismatch: expected %x, got %x", leftValue, leftStem.Values[10]) + // Verify the value was stored + value, err := s.Get(leftKey, nil) + if err != nil { + t.Fatalf("Failed to get: %v", err) } - - // Right child should still be Empty - _, ok = internalNode.right.(Empty) - if !ok { - t.Errorf("Expected right child to remain Empty, got %T", internalNode.right) + if !bytes.Equal(value, leftValue) { + t.Errorf("Value mismatch: expected %x, got %x", leftValue, value) } } -// TestInternalNodeCopy tests the Copy method +// TestInternalNodeCopy tests the Copy method via nodeStore. func TestInternalNodeCopy(t *testing.T) { - // Create an internal node with stem children - leftStem := &StemNode{ - Stem: make([]byte, 31), - Values: make([][]byte, 256), - depth: 1, - } - leftStem.Values[0] = common.HexToHash("0x0101").Bytes() + s := newNodeStore() - rightStem := &StemNode{ - Stem: make([]byte, 31), - Values: make([][]byte, 256), - depth: 1, - } - rightStem.Stem[0] = 0x80 - rightStem.Values[0] = common.HexToHash("0x0202").Bytes() - - node := &InternalNode{ - depth: 0, - left: leftStem, - right: rightStem, - } - - // Create a copy - copied := node.Copy() - copiedInternal, ok := copied.(*InternalNode) - if !ok { - t.Fatalf("Expected InternalNode, got %T", copied) - } + leftKey := make([]byte, 32) + leftKey[31] = 0 + leftValue := common.HexToHash("0x0101").Bytes() - // Check depth - if copiedInternal.depth != node.depth { - t.Errorf("Depth mismatch: expected %d, got %d", node.depth, copiedInternal.depth) - } + rightKey := make([]byte, 32) + rightKey[0] = 0x80 + rightKey[31] = 0 + rightValue := common.HexToHash("0x0202").Bytes() - // Check that children are copied - copiedLeft, ok := copiedInternal.left.(*StemNode) - if !ok { - t.Fatalf("Expected left child to be StemNode, got %T", copiedInternal.left) + if err := s.Insert(leftKey, leftValue, nil); err != nil { + t.Fatal(err) } - - copiedRight, ok := copiedInternal.right.(*StemNode) - if !ok { - t.Fatalf("Expected right child to be StemNode, got %T", copiedInternal.right) + if err := s.Insert(rightKey, rightValue, nil); err != nil { + t.Fatal(err) } - // Verify deep copy (children should be different objects) - if copiedLeft == leftStem { - t.Error("Left child not properly copied") - } - if copiedRight == rightStem { - t.Error("Right child not properly copied") - } + ns := s.Copy() - // But values should be equal - if !bytes.Equal(copiedLeft.Values[0], leftStem.Values[0]) { + // Values should be equal + v1, _ := ns.Get(leftKey, nil) + if !bytes.Equal(v1, leftValue) { t.Error("Left child value mismatch after copy") } - if !bytes.Equal(copiedRight.Values[0], rightStem.Values[0]) { + v2, _ := ns.Get(rightKey, nil) + if !bytes.Equal(v2, rightValue) { t.Error("Right child value mismatch after copy") } } -// TestInternalNodeHash tests the Hash method +// TestInternalNodeHash tests the Hash method via nodeStore. func TestInternalNodeHash(t *testing.T) { - // Create an internal node - node := &InternalNode{ - depth: 0, - left: HashedNode(common.HexToHash("0x1111")), - right: HashedNode(common.HexToHash("0x2222")), - } + s := newNodeStore() + leftRef := s.newHashedRef(common.HexToHash("0x1111")) + rightRef := s.newHashedRef(common.HexToHash("0x2222")) + rootRef := s.newInternalRef(0) + rootNode := s.getInternal(rootRef.Index()) + rootNode.left = leftRef + rootNode.right = rightRef + s.root = rootRef - hash1 := node.Hash() + hash1 := s.computeHash(rootRef) // Hash should be deterministic - hash2 := node.Hash() + hash2 := s.computeHash(rootRef) if hash1 != hash2 { t.Errorf("Hash not deterministic: %x != %x", hash1, hash2) } // Changing a child should change the hash - node.left = HashedNode(common.HexToHash("0x3333")) - node.mustRecompute = true - hash3 := node.Hash() + rootNode.left = s.newHashedRef(common.HexToHash("0x3333")) + rootNode.mustRecompute = true + hash3 := s.computeHash(rootRef) if hash1 == hash3 { t.Error("Hash didn't change after modifying left child") } - - // Test with nil children (should use zero hash) - nodeWithNil := &InternalNode{ - depth: 0, - left: nil, - right: HashedNode(common.HexToHash("0x4444")), - mustRecompute: true, - } - hashWithNil := nodeWithNil.Hash() - if hashWithNil == (common.Hash{}) { - t.Error("Hash shouldn't be zero even with nil child") - } } -// TestInternalNodeGetValuesAtStem tests GetValuesAtStem method +// TestInternalNodeGetValuesAtStem tests GetValuesAtStem method via nodeStore. func TestInternalNodeGetValuesAtStem(t *testing.T) { - // Create a tree with values at different stems + s := newNodeStore() + leftStem := make([]byte, 31) rightStem := make([]byte, 31) rightStem[0] = 0x80 - var leftValues, rightValues [256][]byte + leftValues := make([][]byte, 256) leftValues[0] = common.HexToHash("0x0101").Bytes() leftValues[10] = common.HexToHash("0x0102").Bytes() + rightValues := make([][]byte, 256) rightValues[0] = common.HexToHash("0x0201").Bytes() rightValues[20] = common.HexToHash("0x0202").Bytes() - node := &InternalNode{ - depth: 0, - left: &StemNode{ - Stem: leftStem, - Values: leftValues[:], - depth: 1, - }, - right: &StemNode{ - Stem: rightStem, - Values: rightValues[:], - depth: 1, - }, + if err := s.InsertValuesAtStem(leftStem, leftValues, nil); err != nil { + t.Fatal(err) + } + if err := s.InsertValuesAtStem(rightStem, rightValues, nil); err != nil { + t.Fatal(err) } // Get values from left stem - values, err := node.GetValuesAtStem(leftStem, nil) + values, err := s.GetValuesAtStem(leftStem, nil) if err != nil { t.Fatalf("Failed to get left values: %v", err) } @@ -298,7 +227,7 @@ func TestInternalNodeGetValuesAtStem(t *testing.T) { } // Get values from right stem - values, err = node.GetValuesAtStem(rightStem, nil) + values, err = s.GetValuesAtStem(rightStem, nil) if err != nil { t.Fatalf("Failed to get right values: %v", err) } @@ -310,201 +239,103 @@ func TestInternalNodeGetValuesAtStem(t *testing.T) { } } -// TestInternalNodeInsertValuesAtStem tests InsertValuesAtStem method +// TestInternalNodeInsertValuesAtStem tests InsertValuesAtStem method via nodeStore. func TestInternalNodeInsertValuesAtStem(t *testing.T) { - // Start with an internal node with empty children - node := &InternalNode{ - depth: 0, - left: Empty{}, - right: Empty{}, - } + s := newNodeStore() - // Insert values at a stem in the left subtree stem := make([]byte, 31) - var values [256][]byte + values := make([][]byte, 256) values[5] = common.HexToHash("0x0505").Bytes() values[10] = common.HexToHash("0x1010").Bytes() - newNode, err := node.InsertValuesAtStem(stem, values[:], nil, 0) - if err != nil { + if err := s.InsertValuesAtStem(stem, values, nil); err != nil { t.Fatalf("Failed to insert values: %v", err) } - internalNode, ok := newNode.(*InternalNode) - if !ok { - t.Fatalf("Expected InternalNode, got %T", newNode) - } - - // Check that left child is now a StemNode with the values - leftStem, ok := internalNode.left.(*StemNode) - if !ok { - t.Fatalf("Expected left child to be StemNode, got %T", internalNode.left) + // Check that the values are stored + retrieved, err := s.GetValuesAtStem(stem, nil) + if err != nil { + t.Fatalf("Failed to get values: %v", err) } - - if !bytes.Equal(leftStem.Values[5], values[5]) { + if !bytes.Equal(retrieved[5], values[5]) { t.Error("Value at index 5 mismatch") } - if !bytes.Equal(leftStem.Values[10], values[10]) { + if !bytes.Equal(retrieved[10], values[10]) { t.Error("Value at index 10 mismatch") } } -// TestInternalNodeCollectNodes tests CollectNodes method +// TestInternalNodeCollectNodes tests CollectNodes method via nodeStore. func TestInternalNodeCollectNodes(t *testing.T) { - // Create an internal node with two stem children. All three are - // marked dirty to mirror production semantics — see CollectNodes. - leftStem := &StemNode{ - Stem: make([]byte, 31), - Values: make([][]byte, 256), - depth: 1, - dirty: true, - } + s := newNodeStore() - rightStem := &StemNode{ - Stem: make([]byte, 31), - Values: make([][]byte, 256), - depth: 1, - dirty: true, - } - rightStem.Stem[0] = 0x80 + leftStem := make([]byte, 31) + rightStem := make([]byte, 31) + rightStem[0] = 0x80 - node := &InternalNode{ - depth: 0, - left: leftStem, - right: rightStem, - dirty: true, + leftValues := make([][]byte, 256) + rightValues := make([][]byte, 256) + + if err := s.InsertValuesAtStem(leftStem, leftValues, nil); err != nil { + t.Fatal(err) + } + if err := s.InsertValuesAtStem(rightStem, rightValues, nil); err != nil { + t.Fatal(err) } var collectedPaths [][]byte - var collectedNodes []BinaryNode - - flushFn := func(path []byte, n BinaryNode) { + flushFn := func(path []byte, hash common.Hash, serialized []byte) { pathCopy := make([]byte, len(path)) copy(pathCopy, path) collectedPaths = append(collectedPaths, pathCopy) - collectedNodes = append(collectedNodes, n) } - err := node.CollectNodes([]byte{1}, flushFn) + err := s.collectNodes(s.root, []byte{1}, flushFn) if err != nil { t.Fatalf("Failed to collect nodes: %v", err) } // Should have collected 3 nodes: left stem, right stem, and the internal node itself - if len(collectedNodes) != 3 { - t.Errorf("Expected 3 collected nodes, got %d", len(collectedNodes)) - } - - // Check paths - expectedPaths := [][]byte{ - {1, 0}, // left child - {1, 1}, // right child - {1}, // internal node itself - } - - for i, expectedPath := range expectedPaths { - if !bytes.Equal(collectedPaths[i], expectedPath) { - t.Errorf("Path %d mismatch: expected %v, got %v", i, expectedPath, collectedPaths[i]) - } + if len(collectedPaths) != 3 { + t.Errorf("Expected 3 collected nodes, got %d", len(collectedPaths)) } } -// TestInternalNodeCollectNodesSkipsClean verifies clean subtrees are not -// flushed. A dirty internal node over clean children only flushes itself; -// a fully clean tree flushes nothing. -func TestInternalNodeCollectNodesSkipsClean(t *testing.T) { - leftStem := &StemNode{ - Stem: make([]byte, 31), - Values: make([][]byte, 256), - depth: 1, - } - rightStem := &StemNode{ - Stem: make([]byte, 31), - Values: make([][]byte, 256), - depth: 1, - } - rightStem.Stem[0] = 0x80 - - dirtyParent := &InternalNode{ - depth: 0, - left: leftStem, - right: rightStem, - dirty: true, - } +// TestInternalNodeGetHeight tests GetHeight method via nodeStore. +func TestInternalNodeGetHeight(t *testing.T) { + s := newNodeStore() - var collected []BinaryNode - flushFn := func(_ []byte, n BinaryNode) { collected = append(collected, n) } + // Insert values that create a deeper tree + stem1 := make([]byte, 31) // left + stem2 := make([]byte, 31) + stem2[0] = 0x40 // 01... -> goes left at depth 0, right at depth 1 - if err := dirtyParent.CollectNodes([]byte{1}, flushFn); err != nil { - t.Fatalf("CollectNodes: %v", err) - } - if len(collected) != 1 || collected[0] != dirtyParent { - t.Fatalf("expected only the dirty parent to be flushed, got %d nodes", len(collected)) - } - if dirtyParent.dirty { - t.Errorf("parent dirty flag should be cleared after flush") - } + values1 := make([][]byte, 256) + values1[0] = common.HexToHash("0x01").Bytes() + values2 := make([][]byte, 256) + values2[0] = common.HexToHash("0x02").Bytes() - // Second call on the same tree should be a no-op: everything is clean. - collected = nil - if err := dirtyParent.CollectNodes([]byte{1}, flushFn); err != nil { - t.Fatalf("CollectNodes (second call): %v", err) + if err := s.InsertValuesAtStem(stem1, values1, nil); err != nil { + t.Fatal(err) } - if len(collected) != 0 { - t.Errorf("expected no nodes to be flushed on clean tree, got %d", len(collected)) + if err := s.InsertValuesAtStem(stem2, values2, nil); err != nil { + t.Fatal(err) } -} -// TestInternalNodeGetHeight tests GetHeight method -func TestInternalNodeGetHeight(t *testing.T) { - // Create a tree with different heights - // Left subtree: depth 2 (internal -> stem) - // Right subtree: depth 1 (stem) - leftInternal := &InternalNode{ - depth: 1, - left: &StemNode{ - Stem: make([]byte, 31), - Values: make([][]byte, 256), - depth: 2, - }, - right: Empty{}, - } - - rightStem := &StemNode{ - Stem: make([]byte, 31), - Values: make([][]byte, 256), - depth: 1, - } - - node := &InternalNode{ - depth: 0, - left: leftInternal, - right: rightStem, - } - - height := node.GetHeight() - // Height should be max(left height, right height) + 1 - // Left height: 2, Right height: 1, so total: 3 - if height != 3 { - t.Errorf("Expected height 3, got %d", height) + height := s.getHeight(s.root) + if height < 2 { + t.Errorf("Expected height >= 2, got %d", height) } } -// TestInternalNodeDepthTooLarge tests handling of excessive depth +// TestInternalNodeDepthTooLarge tests handling of excessive depth via nodeStore. func TestInternalNodeDepthTooLarge(t *testing.T) { - // Create an internal node at max depth - node := &InternalNode{ - depth: 31*8 + 1, - left: Empty{}, - right: Empty{}, - } - - stem := make([]byte, 31) - _, err := node.GetValuesAtStem(stem, nil) - if err == nil { - t.Fatal("Expected error for excessive depth") - } - if err.Error() != "node too deep" { - t.Errorf("Expected 'node too deep' error, got: %v", err) - } + s := newNodeStore() + // Creating an internal node beyond max depth should panic + defer func() { + if r := recover(); r == nil { + t.Fatal("Expected panic for excessive depth") + } + }() + s.newInternalRef(31*8 + 1) } diff --git a/trie/bintrie/iterator.go b/trie/bintrie/iterator.go index 048d37f766..31645430c3 100644 --- a/trie/bintrie/iterator.go +++ b/trie/bintrie/iterator.go @@ -26,13 +26,14 @@ import ( var errIteratorEnd = errors.New("end of iteration") type binaryNodeIteratorState struct { - Node BinaryNode + Node nodeRef Index int } type binaryNodeIterator struct { trie *BinaryTrie - current BinaryNode + store *nodeStore + current nodeRef lastErr error stack []binaryNodeIteratorState @@ -40,56 +41,63 @@ type binaryNodeIterator struct { func newBinaryNodeIterator(t *BinaryTrie, _ []byte) (trie.NodeIterator, error) { if t.Hash() == zero { - return &binaryNodeIterator{trie: t, lastErr: errIteratorEnd}, nil + return &binaryNodeIterator{trie: t, store: t.store, lastErr: errIteratorEnd}, nil } - it := &binaryNodeIterator{trie: t, current: t.root} - // it.err = it.seek(start) + it := &binaryNodeIterator{trie: t, store: t.store, current: t.store.root} return it, nil } -// Next moves the iterator to the next node. If the parameter is false, any child -// nodes will be skipped. +// Next moves the iterator to the next node. If descend is false, children of +// the current node are skipped. func (it *binaryNodeIterator) Next(descend bool) bool { if it.lastErr == errIteratorEnd { - it.lastErr = errIteratorEnd return false } if len(it.stack) == 0 { - it.stack = append(it.stack, binaryNodeIteratorState{Node: it.trie.root}) - it.current = it.trie.root - + it.stack = append(it.stack, binaryNodeIteratorState{Node: it.trie.store.root}) + it.current = it.trie.store.root return true } - switch node := it.current.(type) { - case *InternalNode: - // index: 0 = nothing visited, 1=left visited, 2=right visited + switch it.current.Kind() { + case kindInternal: + // index: 0 = nothing visited, 1 = left visited, 2 = right visited. + node := it.store.getInternal(it.current.Index()) context := &it.stack[len(it.stack)-1] - // recurse into both children + if !descend { + // Skip children: pop this node and advance parent. + if len(it.stack) == 1 { + it.lastErr = errIteratorEnd + return false + } + it.stack = it.stack[:len(it.stack)-1] + it.current = it.stack[len(it.stack)-1].Node + it.stack[len(it.stack)-1].Index++ + return it.Next(true) + } + + // Recurse into both children. if context.Index == 0 { - if _, isempty := node.left.(Empty); node.left != nil && !isempty { + if !node.left.IsEmpty() { it.stack = append(it.stack, binaryNodeIteratorState{Node: node.left}) it.current = node.left return it.Next(descend) } - context.Index++ } if context.Index == 1 { - if _, isempty := node.right.(Empty); node.right != nil && !isempty { + if !node.right.IsEmpty() { it.stack = append(it.stack, binaryNodeIteratorState{Node: node.right}) it.current = node.right return it.Next(descend) } - context.Index++ } - // Reached the end of this node, go back to the parent, if - // this isn't root. + // Reached the end of this node; go back to the parent unless we're at the root. if len(it.stack) == 1 { it.lastErr = errIteratorEnd return false @@ -98,17 +106,18 @@ func (it *binaryNodeIterator) Next(descend bool) bool { it.current = it.stack[len(it.stack)-1].Node it.stack[len(it.stack)-1].Index++ return it.Next(descend) - case *StemNode: - // Look for the next non-empty value + + case kindStem: + // Look for the next non-empty value in this stem. + sn := it.store.getStem(it.current.Index()) for i := it.stack[len(it.stack)-1].Index; i < 256; i++ { - if node.Values[i] != nil { + if sn.hasValue(byte(i)) { it.stack[len(it.stack)-1].Index = i + 1 return true } } - // go back to parent to get the next leaf - // Check if we're at the root before popping + // No more values in this stem; go back to parent to get the next leaf. if len(it.stack) == 1 { it.lastErr = errIteratorEnd return false @@ -117,51 +126,47 @@ func (it *binaryNodeIterator) Next(descend bool) bool { it.current = it.stack[len(it.stack)-1].Node it.stack[len(it.stack)-1].Index++ return it.Next(descend) - case HashedNode: - // resolve the node - resolverPath := it.Path() - data, err := it.trie.nodeResolver(resolverPath, common.Hash(node)) - if err != nil { - panic(err) + + case kindHashed: + // Resolve the hashed node from disk, then rewire the parent to point at the + // resolved node in place. + if len(it.stack) < 2 { + it.lastErr = errors.New("cannot resolve hashed root during iteration") + return false } - if data == nil { - // Empty/nil node — treat as Empty, backtrack - it.current = Empty{} - it.stack[len(it.stack)-1].Node = it.current - return it.Next(descend) + hn := it.store.getHashed(it.current.Index()) + data, err := it.trie.nodeResolver(it.Path(), hn.Hash()) + if err != nil { + it.lastErr = err + return false } - it.current, err = DeserializeNodeWithHash(data, len(it.stack)-1, common.Hash(node)) + resolved, err := it.store.deserializeNodeWithHash(data, len(it.stack)-1, hn.Hash()) if err != nil { - panic(err) + it.lastErr = err + return false } - // update the stack and parent with the resolved node - it.stack[len(it.stack)-1].Node = it.current - if len(it.stack) >= 2 { - parent := &it.stack[len(it.stack)-2] - if parent.Index == 0 { - parent.Node.(*InternalNode).left = it.current - } else { - parent.Node.(*InternalNode).right = it.current - } - } - return it.Next(descend) - case Empty: - // Empty node - go back to parent and continue - if len(it.stack) <= 1 { - it.lastErr = errIteratorEnd - return false + oldHashedIdx := it.current.Index() + it.current = resolved + it.stack[len(it.stack)-1].Node = resolved + parent := &it.stack[len(it.stack)-2] + parentNode := it.store.getInternal(parent.Node.Index()) + if parent.Index == 0 { + parentNode.left = resolved + } else { + parentNode.right = resolved } - it.stack = it.stack[:len(it.stack)-1] - it.current = it.stack[len(it.stack)-1].Node - it.stack[len(it.stack)-1].Index++ + it.store.freeHashedNode(oldHashedIdx) return it.Next(descend) + + case kindEmpty: + return false + default: panic("invalid node type") } } -// Error returns the error status of the iterator. func (it *binaryNodeIterator) Error() error { if it.lastErr == errIteratorEnd { return nil @@ -169,27 +174,28 @@ func (it *binaryNodeIterator) Error() error { return it.lastErr } -// Hash returns the hash of the current node. func (it *binaryNodeIterator) Hash() common.Hash { - return it.current.Hash() + return it.store.computeHash(it.current) } -// Parent returns the hash of the parent of the current node. The hash may be the one -// grandparent if the immediate parent is an internal node with no hash. +// Parent returns the hash of the current node's parent. When the immediate +// parent is an internal node whose hash has not been materialised, the +// returned hash may be the one of a grandparent instead. func (it *binaryNodeIterator) Parent() common.Hash { - return it.stack[len(it.stack)-1].Node.Hash() + if len(it.stack) < 2 { + return common.Hash{} + } + return it.store.computeHash(it.stack[len(it.stack)-2].Node) } -// Path returns the hex-encoded path to the current node. -// Callers must not retain references to the return value after calling Next. -// For leaf nodes, the last element of the path is the 'terminator symbol' 0x10. +// Path returns the bit-path to the current node. +// Callers must not retain references to the returned slice after calling Next. func (it *binaryNodeIterator) Path() []byte { if it.Leaf() { return it.LeafKey() } var path []byte for i, state := range it.stack { - // skip the last byte if i >= len(it.stack)-1 { break } @@ -198,107 +204,94 @@ func (it *binaryNodeIterator) Path() []byte { return path } -// NodeBlob returns the serialized bytes of the current node. func (it *binaryNodeIterator) NodeBlob() []byte { - return SerializeNode(it.current) + return it.store.serializeNode(it.current) } -// Leaf returns true iff the current node is a leaf node. -// In a Binary Trie, a StemNode contains up to 256 leaf values. -// The iterator is only considered to be "at a leaf" when it's positioned -// at a specific non-nil value within the StemNode, not just at the StemNode itself. +// Leaf reports whether the iterator is currently positioned at a leaf value. +// A StemNode holds up to 256 values; the iterator is only "at a leaf" when +// positioned at a specific non-nil value inside the stem, not merely at the +// StemNode itself. The stack Index points to the NEXT position after the +// current value, so Index == 0 means we haven't yielded anything yet. func (it *binaryNodeIterator) Leaf() bool { - sn, ok := it.current.(*StemNode) - if !ok { + if it.current.Kind() != kindStem { return false } - // Check if we have a valid stack position if len(it.stack) == 0 { return false } - // The Index in the stack state points to the NEXT position after the current value. - // So if Index is 0, we haven't started iterating through the values yet. - // If Index is 5, we're currently at value[4] (the 5th value, 0-indexed). idx := it.stack[len(it.stack)-1].Index if idx == 0 || idx > 256 { return false } - // Check if there's actually a value at the current position + sn := it.store.getStem(it.current.Index()) currentValueIndex := idx - 1 - return sn.Values[currentValueIndex] != nil + return sn.hasValue(byte(currentValueIndex)) } -// LeafKey returns the key of the leaf. The method panics if the iterator is not -// positioned at a leaf. Callers must not retain references to the value after -// calling Next. +// LeafKey returns the key of the leaf. Panics if the iterator is not +// positioned at a leaf. Callers must not retain references to the returned +// slice after calling Next. func (it *binaryNodeIterator) LeafKey() []byte { - leaf, ok := it.current.(*StemNode) - if !ok { + if it.current.Kind() != kindStem { panic("Leaf() called on an binary node iterator not at a leaf location") } - return leaf.Key(it.stack[len(it.stack)-1].Index - 1) + sn := it.store.getStem(it.current.Index()) + return sn.Key(it.stack[len(it.stack)-1].Index - 1) } -// LeafBlob returns the content of the leaf. The method panics if the iterator -// is not positioned at a leaf. Callers must not retain references to the value -// after calling Next. +// LeafBlob returns the leaf value. Panics if the iterator is not positioned +// at a leaf. Callers must not retain references to the returned slice after +// calling Next. func (it *binaryNodeIterator) LeafBlob() []byte { - leaf, ok := it.current.(*StemNode) - if !ok { + if it.current.Kind() != kindStem { panic("LeafBlob() called on an binary node iterator not at a leaf location") } - return leaf.Values[it.stack[len(it.stack)-1].Index-1] + sn := it.store.getStem(it.current.Index()) + return sn.getValue(byte(it.stack[len(it.stack)-1].Index - 1)) } -// LeafProof returns the Merkle proof of the leaf. The method panics if the -// iterator is not positioned at a leaf. Callers must not retain references -// to the value after calling Next. +// LeafProof returns the Merkle proof of the leaf. Panics if the iterator is +// not positioned at a leaf. Callers must not retain references to the +// returned slices after calling Next. func (it *binaryNodeIterator) LeafProof() [][]byte { - sn, ok := it.current.(*StemNode) - if !ok { + if it.current.Kind() != kindStem { panic("LeafProof() called on an binary node iterator not at a leaf location") } + sn := it.store.getStem(it.current.Index()) proof := make([][]byte, 0, len(it.stack)+StemNodeWidth) - // Build proof by walking up the stack and collecting sibling hashes + if len(it.stack) < 2 { + proof = append(proof, sn.Stem[:]) + proof = append(proof, sn.allValues()...) + return proof + } + for i := range it.stack[:len(it.stack)-2] { state := it.stack[i] - internalNode := state.Node.(*InternalNode) // should panic if the node isn't an InternalNode + internalNode := it.store.getInternal(state.Node.Index()) - // Add the sibling hash to the proof if state.Index == 0 { - // We came from left, so include right sibling - proof = append(proof, internalNode.right.Hash().Bytes()) + rh := it.store.computeHash(internalNode.right) + proof = append(proof, rh.Bytes()) } else { - // We came from right, so include left sibling - proof = append(proof, internalNode.left.Hash().Bytes()) + lh := it.store.computeHash(internalNode.left) + proof = append(proof, lh.Bytes()) } } // Add the stem and siblings - proof = append(proof, sn.Stem) - for _, v := range sn.Values { - proof = append(proof, v) - } + proof = append(proof, sn.Stem[:]) + proof = append(proof, sn.allValues()...) return proof } -// AddResolver sets an intermediate database to use for looking up trie nodes -// before reaching into the real persistent layer. -// -// This is not required for normal operation, rather is an optimization for -// cases where trie nodes can be recovered from some external mechanism without -// reading from disk. In those cases, this resolver allows short circuiting -// accesses and returning them from memory. -// -// Before adding a similar mechanism to any other place in Geth, consider -// making trie.Database an interface and wrapping at that level. It's a huge -// refactor, but it could be worth it if another occurrence arises. +// AddResolver is a no-op (satisfies the NodeIterator interface). func (it *binaryNodeIterator) AddResolver(trie.NodeResolver) { // Not implemented, but should not panic } diff --git a/trie/bintrie/iterator_test.go b/trie/bintrie/iterator_test.go index 3e717c07ba..746f6e8c0f 100644 --- a/trie/bintrie/iterator_test.go +++ b/trie/bintrie/iterator_test.go @@ -27,14 +27,13 @@ import ( // makeTrie creates a BinaryTrie populated with the given key-value pairs. func makeTrie(t *testing.T, entries [][2]common.Hash) *BinaryTrie { t.Helper() + store := newNodeStore() tr := &BinaryTrie{ - root: NewBinaryNode(), + store: store, tracer: trie.NewPrevalueTracer(), } for _, kv := range entries { - var err error - tr.root, err = tr.root.Insert(kv[0][:], kv[1][:], nil, 0) - if err != nil { + if err := store.Insert(kv[0][:], kv[1][:], nil); err != nil { t.Fatal(err) } } @@ -64,7 +63,7 @@ func countLeaves(t *testing.T, tr *BinaryTrie) int { // no nodes and reports no error. func TestIteratorEmptyTrie(t *testing.T) { tr := &BinaryTrie{ - root: Empty{}, + store: newNodeStore(), tracer: trie.NewPrevalueTracer(), } it, err := newBinaryNodeIterator(tr, nil) @@ -145,8 +144,8 @@ func TestIteratorEmptyNodeBacktrack(t *testing.T) { {common.HexToHash("8000000000000000000000000000000000000000000000000000000000000001"), oneKey}, }) - if _, ok := tr.root.(*InternalNode); !ok { - t.Fatalf("expected InternalNode root, got %T", tr.root) + if tr.store.root.Kind() != kindInternal { + t.Fatalf("expected InternalNode root, got kind %d", tr.store.root.Kind()) } if leaves := countLeaves(t, tr); leaves != 2 { t.Fatalf("expected 2 leaves, got %d (Empty backtrack bug?)", leaves) @@ -162,18 +161,31 @@ func TestIteratorHashedNodeNilData(t *testing.T) { {common.HexToHash("8000000000000000000000000000000000000000000000000000000000000001"), oneKey}, }) - root, ok := tr.root.(*InternalNode) - if !ok { - t.Fatalf("expected InternalNode root, got %T", tr.root) + root := tr.store.root + if root.Kind() != kindInternal { + t.Fatalf("expected InternalNode root, got kind %d", root.Kind()) } + rootNode := tr.store.getInternal(root.Index()) // Replace right child with a zero-hash HashedNode. nodeResolver // short-circuits on common.Hash{} and returns (nil, nil), which // triggers the nil-data guard in the iterator. - root.right = HashedNode(common.Hash{}) + rootNode.right = tr.store.newHashedRef(common.Hash{}) // Should not panic; the zero-hash right child should be treated as Empty. - if leaves := countLeaves(t, tr); leaves != 1 { + // Since the hashed node can't be resolved (nil data -> empty deserialization), + // only the left leaf should be counted. + it, err := newBinaryNodeIterator(tr, nil) + if err != nil { + t.Fatal(err) + } + leaves := 0 + for it.Next(true) { + if it.Leaf() { + leaves++ + } + } + if leaves != 1 { t.Fatalf("expected 1 leaf (zero-hash right node skipped), got %d", leaves) } } diff --git a/trie/bintrie/node_ref.go b/trie/bintrie/node_ref.go new file mode 100644 index 0000000000..1c9c0f6284 --- /dev/null +++ b/trie/bintrie/node_ref.go @@ -0,0 +1,56 @@ +// Copyright 2026 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 . + +package bintrie + +// nodeKind identifies the type of a trie node stored in a nodeRef. +type nodeKind uint8 + +const ( + kindEmpty nodeKind = iota + kindInternal + kindStem // up to 256 values per stem + kindHashed +) + +// nodeRef is a compact, GC-invisible reference to a node in a nodeStore. +// It packs a 2-bit type tag (bits 31-30) and a 30-bit index (bits 29-0) +// into a single uint32. Because nodeRef contains no Go pointers, slices +// of structs containing nodeRef fields are allocated in noscan spans — +// the garbage collector never examines them. +type nodeRef uint32 + +const ( + kindShift uint32 = 30 + indexMask uint32 = (1 << kindShift) - 1 + + // emptyRef represents an empty node. + emptyRef nodeRef = 0 +) + +func makeRef(kind nodeKind, idx uint32) nodeRef { + if idx > indexMask { + panic("nodeRef index overflow") + } + return nodeRef(uint32(kind)<> kindShift) } + +// Index within the typed pool. +func (r nodeRef) Index() uint32 { return uint32(r) & indexMask } + +func (r nodeRef) IsEmpty() bool { return r.Kind() == kindEmpty } diff --git a/trie/bintrie/node_store.go b/trie/bintrie/node_store.go new file mode 100644 index 0000000000..8a35f06ee1 --- /dev/null +++ b/trie/bintrie/node_store.go @@ -0,0 +1,184 @@ +// Copyright 2026 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 . + +package bintrie + +import "github.com/ethereum/go-ethereum/common" + +// storeChunkSize is the number of nodes per chunk in each typed pool. +const storeChunkSize = 4096 + +// nodeStore is a GC-friendly arena for binary trie nodes. Nodes are packed +// into typed chunked pools so pointer-free types (InternalNode, HashedNode) +// land in noscan spans the GC skips entirely. +type nodeStore struct { + internalChunks []*[storeChunkSize]InternalNode + internalCount uint32 + + stemChunks []*[storeChunkSize]StemNode + stemCount uint32 + + hashedChunks []*[storeChunkSize]HashedNode + hashedCount uint32 + + root nodeRef + + // Free list for recycling hashed-node slots after resolve. Internal and + // stem nodes are never freed under current semantics (no delete path, + // stem-split keeps the old stem at a deeper position), so they don't + // have free lists. + freeHashed []uint32 +} + +func newNodeStore() *nodeStore { + return &nodeStore{root: emptyRef} +} + +func (s *nodeStore) allocInternal() uint32 { + idx := s.internalCount + chunkIdx := idx / storeChunkSize + if uint32(len(s.internalChunks)) <= chunkIdx { + s.internalChunks = append(s.internalChunks, new([storeChunkSize]InternalNode)) + } + s.internalCount++ + if s.internalCount > indexMask { + panic("internal node pool overflow") + } + return idx +} + +func (s *nodeStore) getInternal(idx uint32) *InternalNode { + return &s.internalChunks[idx/storeChunkSize][idx%storeChunkSize] +} + +func (s *nodeStore) newInternalRef(depth int) nodeRef { + if depth > 248 { + panic("node depth exceeds maximum binary trie depth") + } + idx := s.allocInternal() + n := s.getInternal(idx) + n.depth = uint8(depth) + n.mustRecompute = true + n.dirty = true + return makeRef(kindInternal, idx) +} + +func (s *nodeStore) allocStem() uint32 { + idx := s.stemCount + chunkIdx := idx / storeChunkSize + if uint32(len(s.stemChunks)) <= chunkIdx { + s.stemChunks = append(s.stemChunks, new([storeChunkSize]StemNode)) + } + s.stemCount++ + if s.stemCount > indexMask { + panic("stem node pool overflow") + } + return idx +} + +func (s *nodeStore) getStem(idx uint32) *StemNode { + return &s.stemChunks[idx/storeChunkSize][idx%storeChunkSize] +} + +func (s *nodeStore) newStemRef(stem []byte, depth int) nodeRef { + if depth > 248 { + panic("node depth exceeds maximum binary trie depth") + } + idx := s.allocStem() + sn := s.getStem(idx) + copy(sn.Stem[:], stem[:StemSize]) + sn.depth = uint8(depth) + sn.mustRecompute = true + sn.dirty = true + return makeRef(kindStem, idx) +} + +func (s *nodeStore) allocHashed() uint32 { + if n := len(s.freeHashed); n > 0 { + idx := s.freeHashed[n-1] + s.freeHashed = s.freeHashed[:n-1] + *s.getHashed(idx) = HashedNode{} + return idx + } + idx := s.hashedCount + chunkIdx := idx / storeChunkSize + if uint32(len(s.hashedChunks)) <= chunkIdx { + s.hashedChunks = append(s.hashedChunks, new([storeChunkSize]HashedNode)) + } + s.hashedCount++ + if s.hashedCount > indexMask { + panic("hashed node pool overflow") + } + return idx +} + +func (s *nodeStore) getHashed(idx uint32) *HashedNode { + return &s.hashedChunks[idx/storeChunkSize][idx%storeChunkSize] +} + +func (s *nodeStore) freeHashedNode(idx uint32) { + s.freeHashed = append(s.freeHashed, idx) +} + +func (s *nodeStore) newHashedRef(hash common.Hash) nodeRef { + idx := s.allocHashed() + *s.getHashed(idx) = HashedNode(hash) + return makeRef(kindHashed, idx) +} + +func (s *nodeStore) Copy() *nodeStore { + ns := &nodeStore{ + root: s.root, + internalCount: s.internalCount, + stemCount: s.stemCount, + hashedCount: s.hashedCount, + } + ns.internalChunks = make([]*[storeChunkSize]InternalNode, len(s.internalChunks)) + for i, chunk := range s.internalChunks { + cp := *chunk + ns.internalChunks[i] = &cp + } + ns.stemChunks = make([]*[storeChunkSize]StemNode, len(s.stemChunks)) + for i, chunk := range s.stemChunks { + cp := *chunk + ns.stemChunks[i] = &cp + } + // Deep-copy each stem's value slots — they may alias serialized buffers, + // so we can't rely on the chunk-wise struct copy above. + for i := uint32(0); i < s.stemCount; i++ { + src := s.getStem(i) + dst := ns.getStem(i) + for j, v := range src.values { + if v == nil { + continue + } + cp := make([]byte, len(v)) + copy(cp, v) + dst.values[j] = cp + } + } + ns.hashedChunks = make([]*[storeChunkSize]HashedNode, len(s.hashedChunks)) + for i, chunk := range s.hashedChunks { + cp := *chunk + ns.hashedChunks[i] = &cp + } + if len(s.freeHashed) > 0 { + ns.freeHashed = make([]uint32, len(s.freeHashed)) + copy(ns.freeHashed, s.freeHashed) + } + + return ns +} diff --git a/trie/bintrie/stem_node.go b/trie/bintrie/stem_node.go index 0ceae6b062..93c55acefa 100644 --- a/trie/bintrie/stem_node.go +++ b/trie/bintrie/stem_node.go @@ -17,236 +17,93 @@ package bintrie import ( - "bytes" - "errors" - "fmt" - "slices" + "crypto/sha256" "github.com/ethereum/go-ethereum/common" ) -// StemNode represents a group of `NodeWith` values sharing the same stem. +// StemNode holds up to 256 values sharing a 31-byte stem. +// +// Invariant: dirty=false implies mustRecompute=false. Every mutation that +// invalidates the cached hash MUST also mark the blob for re-flush. type StemNode struct { - Stem []byte // Stem path to get to StemNodeWidth values - Values [][]byte // All values, indexed by the last byte of the key. - depth int // Depth of the node + Stem [StemSize]byte + values [StemNodeWidth][]byte // nil == slot absent + + depth uint8 - mustRecompute bool // true if the hash needs to be recomputed - dirty bool // true if the node's on-disk blob is stale (needs flush) + mustRecompute bool // hash is stale (cleared by Hash) + dirty bool // on-disk blob is stale (cleared by CollectNodes) hash common.Hash // cached hash when mustRecompute == false } -// Get retrieves the value for the given key. -func (bt *StemNode) Get(key []byte, _ NodeResolverFn) ([]byte, error) { - if !bytes.Equal(bt.Stem, key[:StemSize]) { - return nil, nil - } - return bt.Values[key[StemSize]], nil +func (sn *StemNode) getValue(suffix byte) []byte { + return sn.values[suffix] } -// Insert inserts a new key-value pair into the node. -func (bt *StemNode) Insert(key []byte, value []byte, _ NodeResolverFn, depth int) (BinaryNode, error) { - if !bytes.Equal(bt.Stem, key[:StemSize]) { - bitStem := bt.Stem[bt.depth/8] >> (7 - (bt.depth % 8)) & 1 - - n := &InternalNode{depth: bt.depth, mustRecompute: true, dirty: true} - bt.depth++ - // bt is re-parented under n and sits at a new path — rewrite its blob. - bt.mustRecompute = true - bt.dirty = true - var child, other *BinaryNode - if bitStem == 0 { - n.left = bt - child = &n.left - other = &n.right - } else { - n.right = bt - child = &n.right - other = &n.left - } - - bitKey := key[n.depth/8] >> (7 - (n.depth % 8)) & 1 - if bitKey == bitStem { - var err error - *child, err = (*child).Insert(key, value, nil, depth+1) - if err != nil { - return n, fmt.Errorf("insert error: %w", err) - } - *other = Empty{} - } else { - var values [StemNodeWidth][]byte - values[key[StemSize]] = value - *other = &StemNode{ - Stem: slices.Clone(key[:StemSize]), - Values: values[:], - depth: depth + 1, - mustRecompute: true, - dirty: true, - } - } - return n, nil - } - if len(value) != HashSize { - return bt, errors.New("invalid insertion: value length") - } - bt.Values[key[StemSize]] = value - bt.mustRecompute = true - bt.dirty = true - return bt, nil +func (sn *StemNode) hasValue(suffix byte) bool { + return sn.values[suffix] != nil } -// Copy creates a deep copy of the node. -func (bt *StemNode) Copy() BinaryNode { - var values [StemNodeWidth][]byte - for i, v := range bt.Values { - values[i] = slices.Clone(v) - } - return &StemNode{ - Stem: slices.Clone(bt.Stem), - Values: values[:], - depth: bt.depth, - hash: bt.hash, - mustRecompute: bt.mustRecompute, - dirty: bt.dirty, - } +// allValues returns the underlying slot array as a slice. nil entries mean +// absent. Callers must treat it as read-only. +func (sn *StemNode) allValues() [][]byte { + return sn.values[:] } -// GetHeight returns the height of the node. -func (bt *StemNode) GetHeight() int { - return 1 +// setValue mutates a value slot and marks the stem for re-hash and +// re-flush. This is the only API for post-load value mutation; direct +// values[...] writes are reserved for the on-disk load path in +// decodeNode, which must leave mustRecompute/dirty at their loaded +// state. +func (sn *StemNode) setValue(suffix byte, value []byte) { + sn.values[suffix] = value + sn.mustRecompute = true + sn.dirty = true } -// Hash returns the hash of the node. -func (bt *StemNode) Hash() common.Hash { - if !bt.mustRecompute { - return bt.hash +func (sn *StemNode) Hash() common.Hash { + if !sn.mustRecompute { + return sn.hash } + // Use sha256.Sum256 (returns [32]byte by value) instead of a pooled + // hash.Hash: feeding data[i][:0] into the interface method Sum forces + // data to heap (escape analysis is conservative through interfaces). + // Sum256 takes []byte and returns by value, so data stays on stack. var data [StemNodeWidth]common.Hash - h := newSha256() - defer returnSha256(h) - for i, v := range bt.Values { + + for i, v := range sn.values { if v != nil { - h.Reset() - h.Write(v) - h.Sum(data[i][:0]) + data[i] = sha256.Sum256(v) } } - h.Reset() + var pair [2 * HashSize]byte for level := 1; level <= 8; level++ { for i := range StemNodeWidth / (1 << level) { - h.Reset() - if data[i*2] == (common.Hash{}) && data[i*2+1] == (common.Hash{}) { data[i] = common.Hash{} continue } - - h.Write(data[i*2][:]) - h.Write(data[i*2+1][:]) - data[i] = common.Hash(h.Sum(nil)) - } - } - - h.Reset() - h.Write(bt.Stem) - h.Write([]byte{0}) - h.Write(data[0][:]) - bt.hash = common.BytesToHash(h.Sum(nil)) - bt.mustRecompute = false - return bt.hash -} - -// CollectNodes flushes the stem via the collector when dirty; clean stems -// are skipped. -func (bt *StemNode) CollectNodes(path []byte, flush NodeFlushFn) error { - if !bt.dirty { - return nil - } - flush(path, bt) - bt.dirty = false - return nil -} - -// GetValuesAtStem retrieves the group of values located at the given stem key. -func (bt *StemNode) GetValuesAtStem(stem []byte, _ NodeResolverFn) ([][]byte, error) { - if !bytes.Equal(bt.Stem, stem) { - return nil, nil - } - return bt.Values[:], nil -} - -// InsertValuesAtStem inserts a full value group at the given stem in the internal node. -// Already-existing values will be overwritten. -func (bt *StemNode) InsertValuesAtStem(key []byte, values [][]byte, _ NodeResolverFn, depth int) (BinaryNode, error) { - if !bytes.Equal(bt.Stem, key[:StemSize]) { - bitStem := bt.Stem[bt.depth/8] >> (7 - (bt.depth % 8)) & 1 - - n := &InternalNode{depth: bt.depth, mustRecompute: true, dirty: true} - bt.depth++ - // bt is re-parented under n and sits at a new path — rewrite its blob. - bt.mustRecompute = true - bt.dirty = true - var child, other *BinaryNode - if bitStem == 0 { - n.left = bt - child = &n.left - other = &n.right - } else { - n.right = bt - child = &n.right - other = &n.left - } - - bitKey := key[n.depth/8] >> (7 - (n.depth % 8)) & 1 - if bitKey == bitStem { - var err error - *child, err = (*child).InsertValuesAtStem(key, values, nil, depth+1) - if err != nil { - return n, fmt.Errorf("insert error: %w", err) - } - *other = Empty{} - } else { - *other = &StemNode{ - Stem: slices.Clone(key[:StemSize]), - Values: values, - depth: n.depth + 1, - mustRecompute: true, - dirty: true, - } + copy(pair[:HashSize], data[i*2][:]) + copy(pair[HashSize:], data[i*2+1][:]) + data[i] = sha256.Sum256(pair[:]) } - return n, nil } - // same stem, just merge the two value lists - for i, v := range values { - if v != nil { - bt.Values[i] = v - bt.mustRecompute = true - bt.dirty = true - } - } - return bt, nil -} - -func (bt *StemNode) toDot(parent, path string) string { - me := fmt.Sprintf("stem%s", path) - ret := fmt.Sprintf("%s [label=\"stem=%x c=%x\"]\n", me, bt.Stem, bt.Hash()) - ret = fmt.Sprintf("%s %s -> %s\n", ret, parent, me) - for i, v := range bt.Values { - if v != nil { - ret = fmt.Sprintf("%s%s%x [label=\"%x\"]\n", ret, me, i, v) - ret = fmt.Sprintf("%s%s -> %s%x\n", ret, me, me, i) - } - } - return ret + var final [StemSize + 1 + HashSize]byte + copy(final[:StemSize], sn.Stem[:]) + final[StemSize] = 0 + copy(final[StemSize+1:], data[0][:]) + sn.hash = sha256.Sum256(final[:]) + sn.mustRecompute = false + return sn.hash } -// Key returns the full key for the given index. -func (bt *StemNode) Key(i int) []byte { +func (sn *StemNode) Key(i int) []byte { var ret [HashSize]byte - copy(ret[:], bt.Stem) + copy(ret[:], sn.Stem[:]) ret[StemSize] = byte(i) return ret[:] } diff --git a/trie/bintrie/stem_node_test.go b/trie/bintrie/stem_node_test.go index 2743e7ce9b..5faf903fba 100644 --- a/trie/bintrie/stem_node_test.go +++ b/trie/bintrie/stem_node_test.go @@ -23,165 +23,99 @@ import ( "github.com/ethereum/go-ethereum/common" ) -// TestStemNodeGet tests the Get method for matching stem, non-matching stem, -// and nil-value suffix scenarios. -func TestStemNodeGet(t *testing.T) { - stem := make([]byte, StemSize) - stem[0] = 0xAB - var values [StemNodeWidth][]byte - values[5] = common.HexToHash("0xdeadbeef").Bytes() - - node := &StemNode{Stem: stem, Values: values[:], depth: 0} - - // Matching stem, populated suffix → returns value. - key := make([]byte, HashSize) - copy(key[:StemSize], stem) - key[StemSize] = 5 - got, err := node.Get(key, nil) - if err != nil { - t.Fatalf("Get error: %v", err) - } - if !bytes.Equal(got, values[5]) { - t.Fatalf("Get = %x, want %x", got, values[5]) - } - - // Matching stem, empty suffix → returns nil (slot not set). - key[StemSize] = 99 - got, err = node.Get(key, nil) - if err != nil { - t.Fatalf("Get error: %v", err) - } - if got != nil { - t.Fatalf("Get(empty suffix) = %x, want nil", got) - } - - // Non-matching stem → returns nil, nil. - otherKey := make([]byte, HashSize) - otherKey[0] = 0xFF - got, err = node.Get(otherKey, nil) - if err != nil { - t.Fatalf("Get error: %v", err) - } - if got != nil { - t.Fatalf("Get(wrong stem) = %x, want nil", got) - } -} - -// TestStemNodeInsertSameStem tests inserting values with the same stem +// TestStemNodeInsertSameStem tests inserting values with the same stem via nodeStore. func TestStemNodeInsertSameStem(t *testing.T) { + s := newNodeStore() + stem := make([]byte, 31) for i := range stem { stem[i] = byte(i) } - var values [256][]byte - values[0] = common.HexToHash("0x0101").Bytes() - - node := &StemNode{ - Stem: stem, - Values: values[:], - depth: 0, + // Insert first value + key1 := make([]byte, 32) + copy(key1[:31], stem) + key1[31] = 0 + value1 := common.HexToHash("0x0101").Bytes() + if err := s.Insert(key1, value1, nil); err != nil { + t.Fatal(err) } // Insert another value with the same stem but different last byte - key := make([]byte, 32) - copy(key[:31], stem) - key[31] = 10 - value := common.HexToHash("0x0202").Bytes() - - newNode, err := node.Insert(key, value, nil, 0) - if err != nil { - t.Fatalf("Failed to insert: %v", err) + key2 := make([]byte, 32) + copy(key2[:31], stem) + key2[31] = 10 + value2 := common.HexToHash("0x0202").Bytes() + if err := s.Insert(key2, value2, nil); err != nil { + t.Fatal(err) } - // Should still be a StemNode - stemNode, ok := newNode.(*StemNode) - if !ok { - t.Fatalf("Expected StemNode, got %T", newNode) + // Root should still be a StemNode + if s.root.Kind() != kindStem { + t.Fatalf("Expected kindStem root, got kind %d", s.root.Kind()) } // Check that both values are present - if !bytes.Equal(stemNode.Values[0], values[0]) { + v1, _ := s.Get(key1, nil) + if !bytes.Equal(v1, value1) { t.Errorf("Value at index 0 mismatch") } - if !bytes.Equal(stemNode.Values[10], value) { + v2, _ := s.Get(key2, nil) + if !bytes.Equal(v2, value2) { t.Errorf("Value at index 10 mismatch") } } -// TestStemNodeInsertDifferentStem tests inserting values with different stems +// TestStemNodeInsertDifferentStem tests inserting values with different stems via nodeStore. func TestStemNodeInsertDifferentStem(t *testing.T) { - stem1 := make([]byte, 31) - for i := range stem1 { - stem1[i] = 0x00 - } + s := newNodeStore() - var values [256][]byte - values[0] = common.HexToHash("0x0101").Bytes() - - node := &StemNode{ - Stem: stem1, - Values: values[:], - depth: 0, + // Insert first value with stem of all zeros + key1 := make([]byte, 32) + key1[31] = 0 + value1 := common.HexToHash("0x0101").Bytes() + if err := s.Insert(key1, value1, nil); err != nil { + t.Fatal(err) } // Insert with a different stem (first bit different) - key := make([]byte, 32) - key[0] = 0x80 // First bit is 1 instead of 0 - value := common.HexToHash("0x0202").Bytes() - - newNode, err := node.Insert(key, value, nil, 0) - if err != nil { - t.Fatalf("Failed to insert: %v", err) + key2 := make([]byte, 32) + key2[0] = 0x80 // First bit is 1 instead of 0 + value2 := common.HexToHash("0x0202").Bytes() + if err := s.Insert(key2, value2, nil); err != nil { + t.Fatal(err) } // Should now be an InternalNode - internalNode, ok := newNode.(*InternalNode) - if !ok { - t.Fatalf("Expected InternalNode, got %T", newNode) + if s.root.Kind() != kindInternal { + t.Fatalf("Expected kindInternal root, got kind %d", s.root.Kind()) } // Check depth - if internalNode.depth != 0 { - t.Errorf("Expected depth 0, got %d", internalNode.depth) - } - - // Original stem should be on the left (bit 0) - leftStem, ok := internalNode.left.(*StemNode) - if !ok { - t.Fatalf("Expected left child to be StemNode, got %T", internalNode.left) - } - if !bytes.Equal(leftStem.Stem, stem1) { - t.Errorf("Left stem mismatch") + rootNode := s.getInternal(s.root.Index()) + if rootNode.depth != 0 { + t.Errorf("Expected depth 0, got %d", rootNode.depth) } - // New stem should be on the right (bit 1) - rightStem, ok := internalNode.right.(*StemNode) - if !ok { - t.Fatalf("Expected right child to be StemNode, got %T", internalNode.right) + // Verify both values are retrievable + v1, _ := s.Get(key1, nil) + if !bytes.Equal(v1, value1) { + t.Error("Value 1 mismatch") } - if !bytes.Equal(rightStem.Stem, key[:31]) { - t.Errorf("Right stem mismatch") + v2, _ := s.Get(key2, nil) + if !bytes.Equal(v2, value2) { + t.Error("Value 2 mismatch") } } -// TestStemNodeInsertInvalidValueLength tests inserting value with invalid length +// TestStemNodeInsertInvalidValueLength tests inserting value with invalid length via nodeStore. func TestStemNodeInsertInvalidValueLength(t *testing.T) { - stem := make([]byte, 31) - var values [256][]byte - - node := &StemNode{ - Stem: stem, - Values: values[:], - depth: 0, - } + s := newNodeStore() - // Try to insert value with wrong length key := make([]byte, 32) - copy(key[:31], stem) invalidValue := []byte{1, 2, 3} // Not 32 bytes - _, err := node.Insert(key, invalidValue, nil, 0) + err := s.Insert(key, invalidValue, nil) if err == nil { t.Fatal("Expected error for invalid value length") } @@ -191,221 +125,209 @@ func TestStemNodeInsertInvalidValueLength(t *testing.T) { } } -// TestStemNodeCopy tests the Copy method +// TestStemNodeCopy tests the Copy method via nodeStore. func TestStemNodeCopy(t *testing.T) { - stem := make([]byte, 31) - for i := range stem { - stem[i] = byte(i) - } - - var values [256][]byte - values[0] = common.HexToHash("0x0101").Bytes() - values[255] = common.HexToHash("0x0202").Bytes() + s := newNodeStore() - node := &StemNode{ - Stem: stem, - Values: values[:], - depth: 10, + key1 := make([]byte, 32) + for i := range 31 { + key1[i] = byte(i) } + key1[31] = 0 + value1 := common.HexToHash("0x0101").Bytes() - // Create a copy - copied := node.Copy() - copiedStem, ok := copied.(*StemNode) - if !ok { - t.Fatalf("Expected StemNode, got %T", copied) - } + key2 := make([]byte, 32) + copy(key2[:31], key1[:31]) + key2[31] = 255 + value2 := common.HexToHash("0x0202").Bytes() - // Check that values are equal but not the same slice - if !bytes.Equal(copiedStem.Stem, node.Stem) { - t.Errorf("Stem mismatch after copy") + if err := s.Insert(key1, value1, nil); err != nil { + t.Fatal(err) } - if &copiedStem.Stem[0] == &node.Stem[0] { - t.Error("Stem slice not properly cloned") + if err := s.Insert(key2, value2, nil); err != nil { + t.Fatal(err) } - // Check values - if !bytes.Equal(copiedStem.Values[0], node.Values[0]) { + ns := s.Copy() + + // Check that values are equal + v1, _ := ns.Get(key1, nil) + if !bytes.Equal(v1, value1) { t.Errorf("Value at index 0 mismatch after copy") } - if !bytes.Equal(copiedStem.Values[255], node.Values[255]) { + v2, _ := ns.Get(key2, nil) + if !bytes.Equal(v2, value2) { t.Errorf("Value at index 255 mismatch after copy") } - - // Check that value slices are cloned - if copiedStem.Values[0] != nil && &copiedStem.Values[0][0] == &node.Values[0][0] { - t.Error("Value slice not properly cloned") - } - - // Check depth - if copiedStem.depth != node.depth { - t.Errorf("Depth mismatch: expected %d, got %d", node.depth, copiedStem.depth) - } } -// TestStemNodeHash tests the Hash method +// TestStemNodeHash tests the Hash method. func TestStemNodeHash(t *testing.T) { - stem := make([]byte, 31) - var values [256][]byte - values[0] = common.HexToHash("0x0101").Bytes() + s := newNodeStore() - node := &StemNode{ - Stem: stem, - Values: values[:], - depth: 0, + key := make([]byte, 32) + key[31] = 0 + value := common.HexToHash("0x0101").Bytes() + if err := s.Insert(key, value, nil); err != nil { + t.Fatal(err) } - hash1 := node.Hash() + hash1 := s.computeHash(s.root) // Hash should be deterministic - hash2 := node.Hash() + hash2 := s.computeHash(s.root) if hash1 != hash2 { t.Errorf("Hash not deterministic: %x != %x", hash1, hash2) } // Changing a value should change the hash - node.Values[1] = common.HexToHash("0x0202").Bytes() - node.mustRecompute = true - hash3 := node.Hash() + key2 := make([]byte, 32) + key2[31] = 1 + value2 := common.HexToHash("0x0202").Bytes() + if err := s.Insert(key2, value2, nil); err != nil { + t.Fatal(err) + } + hash3 := s.computeHash(s.root) if hash1 == hash3 { t.Error("Hash didn't change after modifying values") } } -// TestStemNodeGetValuesAtStem tests GetValuesAtStem method +// TestStemNodeGetValuesAtStem tests GetValuesAtStem method via nodeStore. func TestStemNodeGetValuesAtStem(t *testing.T) { + s := newNodeStore() + stem := make([]byte, 31) for i := range stem { stem[i] = byte(i) } - var values [256][]byte + values := make([][]byte, 256) values[0] = common.HexToHash("0x0101").Bytes() values[10] = common.HexToHash("0x0202").Bytes() values[255] = common.HexToHash("0x0303").Bytes() - node := &StemNode{ - Stem: stem, - Values: values[:], - depth: 0, + if err := s.InsertValuesAtStem(stem, values, nil); err != nil { + t.Fatal(err) } // GetValuesAtStem with matching stem - retrievedValues, err := node.GetValuesAtStem(stem, nil) + retrievedValues, err := s.GetValuesAtStem(stem, nil) if err != nil { t.Fatalf("Failed to get values: %v", err) } - // Check that all values match - for i := range 256 { - if !bytes.Equal(retrievedValues[i], values[i]) { - t.Errorf("Value mismatch at index %d", i) - } + if !bytes.Equal(retrievedValues[0], values[0]) { + t.Error("Value at index 0 mismatch") + } + if !bytes.Equal(retrievedValues[10], values[10]) { + t.Error("Value at index 10 mismatch") + } + if !bytes.Equal(retrievedValues[255], values[255]) { + t.Error("Value at index 255 mismatch") } - // GetValuesAtStem with different stem should return nil + // GetValuesAtStem with different stem should return nil values differentStem := make([]byte, 31) differentStem[0] = 0xFF - shouldBeNil, err := node.GetValuesAtStem(differentStem, nil) + shouldBeEmpty, err := s.GetValuesAtStem(differentStem, nil) if err != nil { t.Fatalf("Failed to get values with different stem: %v", err) } - if shouldBeNil != nil { - t.Error("Expected nil for different stem, got non-nil") + allNil := true + for _, v := range shouldBeEmpty { + if v != nil { + allNil = false + break + } + } + if !allNil { + t.Error("Expected all nil values for different stem") } } -// TestStemNodeInsertValuesAtStem tests InsertValuesAtStem method +// TestStemNodeInsertValuesAtStem tests InsertValuesAtStem method via nodeStore. func TestStemNodeInsertValuesAtStem(t *testing.T) { + s := newNodeStore() + stem := make([]byte, 31) - var values [256][]byte + values := make([][]byte, 256) values[0] = common.HexToHash("0x0101").Bytes() - node := &StemNode{ - Stem: stem, - Values: values[:], - depth: 0, + if err := s.InsertValuesAtStem(stem, values, nil); err != nil { + t.Fatal(err) } // Insert new values at the same stem - var newValues [256][]byte + newValues := make([][]byte, 256) newValues[1] = common.HexToHash("0x0202").Bytes() newValues[2] = common.HexToHash("0x0303").Bytes() - newNode, err := node.InsertValuesAtStem(stem, newValues[:], nil, 0) - if err != nil { - t.Fatalf("Failed to insert values: %v", err) - } - - stemNode, ok := newNode.(*StemNode) - if !ok { - t.Fatalf("Expected StemNode, got %T", newNode) + if err := s.InsertValuesAtStem(stem, newValues, nil); err != nil { + t.Fatal(err) } // Check that all values are present - if !bytes.Equal(stemNode.Values[0], values[0]) { + retrieved, err := s.GetValuesAtStem(stem, nil) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(retrieved[0], values[0]) { t.Error("Original value at index 0 missing") } - if !bytes.Equal(stemNode.Values[1], newValues[1]) { + if !bytes.Equal(retrieved[1], newValues[1]) { t.Error("New value at index 1 missing") } - if !bytes.Equal(stemNode.Values[2], newValues[2]) { + if !bytes.Equal(retrieved[2], newValues[2]) { t.Error("New value at index 2 missing") } } -// TestStemNodeGetHeight tests GetHeight method +// TestStemNodeGetHeight tests GetHeight method via nodeStore. func TestStemNodeGetHeight(t *testing.T) { - node := &StemNode{ - Stem: make([]byte, 31), - Values: make([][]byte, 256), - depth: 0, + s := newNodeStore() + + key := make([]byte, 32) + value := common.HexToHash("0x01").Bytes() + if err := s.Insert(key, value, nil); err != nil { + t.Fatal(err) } - height := node.GetHeight() + height := s.getHeight(s.root) if height != 1 { t.Errorf("Expected height 1, got %d", height) } } -// TestStemNodeCollectNodes tests CollectNodes method +// TestStemNodeCollectNodes tests CollectNodes method via nodeStore. func TestStemNodeCollectNodes(t *testing.T) { + s := newNodeStore() + stem := make([]byte, 31) - var values [256][]byte + values := make([][]byte, 256) values[0] = common.HexToHash("0x0101").Bytes() - node := &StemNode{ - Stem: stem, - Values: values[:], - depth: 0, - dirty: true, + if err := s.InsertValuesAtStem(stem, values, nil); err != nil { + t.Fatal(err) } var collectedPaths [][]byte - var collectedNodes []BinaryNode - - flushFn := func(path []byte, n BinaryNode) { - // Make a copy of the path + flushFn := func(path []byte, hash common.Hash, serialized []byte) { pathCopy := make([]byte, len(path)) copy(pathCopy, path) collectedPaths = append(collectedPaths, pathCopy) - collectedNodes = append(collectedNodes, n) } - err := node.CollectNodes([]byte{0, 1, 0}, flushFn) + err := s.collectNodes(s.root, []byte{0, 1, 0}, flushFn) if err != nil { t.Fatalf("Failed to collect nodes: %v", err) } // Should have collected one node (itself) - if len(collectedNodes) != 1 { - t.Errorf("Expected 1 collected node, got %d", len(collectedNodes)) - } - - // Check that the collected node is the same - if collectedNodes[0] != node { - t.Error("Collected node doesn't match original") + if len(collectedPaths) != 1 { + t.Errorf("Expected 1 collected node, got %d", len(collectedPaths)) } // Check the path @@ -413,44 +335,3 @@ func TestStemNodeCollectNodes(t *testing.T) { t.Errorf("Path mismatch: expected [0, 1, 0], got %v", collectedPaths[0]) } } - -// TestStemNodeCollectNodesSkipsClean verifies that a clean stem is not -// flushed, and that flushing a dirty stem clears its dirty flag so that -// a subsequent CollectNodes on the same node is a no-op. -func TestStemNodeCollectNodesSkipsClean(t *testing.T) { - stem := make([]byte, 31) - node := &StemNode{ - Stem: stem, - Values: make([][]byte, 256), - depth: 0, - } - - var collected []BinaryNode - flushFn := func(_ []byte, n BinaryNode) { collected = append(collected, n) } - - if err := node.CollectNodes([]byte{0}, flushFn); err != nil { - t.Fatalf("CollectNodes on clean stem: %v", err) - } - if len(collected) != 0 { - t.Fatalf("expected clean stem not to be flushed, got %d", len(collected)) - } - - node.dirty = true - if err := node.CollectNodes([]byte{0}, flushFn); err != nil { - t.Fatalf("CollectNodes on dirty stem: %v", err) - } - if len(collected) != 1 { - t.Fatalf("expected dirty stem to be flushed once, got %d", len(collected)) - } - if node.dirty { - t.Errorf("stem dirty flag should be cleared after flush") - } - - collected = nil - if err := node.CollectNodes([]byte{0}, flushFn); err != nil { - t.Fatalf("CollectNodes after flush: %v", err) - } - if len(collected) != 0 { - t.Errorf("expected no flush on clean stem, got %d", len(collected)) - } -} diff --git a/trie/bintrie/store_commit.go b/trie/bintrie/store_commit.go new file mode 100644 index 0000000000..b142ecb34c --- /dev/null +++ b/trie/bintrie/store_commit.go @@ -0,0 +1,310 @@ +// Copyright 2026 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 . + +package bintrie + +import ( + "crypto/sha256" + "errors" + "fmt" + "math/bits" + "runtime" + "sync" + + "github.com/ethereum/go-ethereum/common" +) + +type nodeFlushFn func(path []byte, hash common.Hash, serialized []byte) + +func (s *nodeStore) Hash() common.Hash { + return s.computeHash(s.root) +} + +func (s *nodeStore) computeHash(ref nodeRef) common.Hash { + switch ref.Kind() { + case kindInternal: + return s.hashInternal(ref.Index()) + case kindStem: + return s.getStem(ref.Index()).Hash() + case kindHashed: + return s.getHashed(ref.Index()).Hash() + case kindEmpty: + return common.Hash{} + default: + return common.Hash{} + } +} + +// parallelHashDepth is the tree depth below which hashInternal spawns +// goroutines for shallow-depth parallelism. Computed once at init because +// NumCPU() never changes after startup. +var parallelHashDepth = min(bits.Len(uint(runtime.NumCPU())), 8) + +// hashInternal hashes an InternalNode and caches the result. +// +// At shallow depths (< parallelHashDepth) the left subtree is hashed in a +// goroutine while the right subtree is hashed inline, then the two digests +// are combined. Below that threshold the goroutine spawn cost outweighs the +// hashing work, so deeper nodes hash both children sequentially. +func (s *nodeStore) hashInternal(idx uint32) common.Hash { + node := s.getInternal(idx) + if !node.mustRecompute { + return node.hash + } + + if int(node.depth) < parallelHashDepth { + var input [64]byte + var lh common.Hash + var wg sync.WaitGroup + if !node.left.IsEmpty() { + wg.Add(1) + go func() { + // defer wg.Done() so a panic in computeHash still releases + // the waiter; without this, a recover() higher in the call + // stack would leave the parent stuck in wg.Wait forever. + defer wg.Done() + lh = s.computeHash(node.left) + }() + } + if !node.right.IsEmpty() { + rh := s.computeHash(node.right) + copy(input[32:], rh[:]) + } + wg.Wait() + copy(input[:32], lh[:]) + node.hash = sha256.Sum256(input[:]) + node.mustRecompute = false + return node.hash + } + + // Deep sequential branch — mirrors the shallow branch's shape to keep + // input on the stack. Writing lh/rh through hash.Hash (interface) + // forces escape; copy into a local [64]byte and hash it in one shot. + var input [64]byte + if !node.left.IsEmpty() { + lh := s.computeHash(node.left) + copy(input[:HashSize], lh[:]) + } + if !node.right.IsEmpty() { + rh := s.computeHash(node.right) + copy(input[HashSize:], rh[:]) + } + node.hash = sha256.Sum256(input[:]) + node.mustRecompute = false + return node.hash +} + +// SerializeNode serializes a node into the flat on-disk format. +func (s *nodeStore) serializeNode(ref nodeRef) []byte { + switch ref.Kind() { + case kindInternal: + node := s.getInternal(ref.Index()) + var serialized [NodeTypeBytes + HashSize + HashSize]byte + serialized[0] = nodeTypeInternal + lh := s.computeHash(node.left) + rh := s.computeHash(node.right) + copy(serialized[NodeTypeBytes:NodeTypeBytes+HashSize], lh[:]) + copy(serialized[NodeTypeBytes+HashSize:], rh[:]) + return serialized[:] + + case kindStem: + sn := s.getStem(ref.Index()) + // Count present slots to size the blob. + var count int + for _, v := range sn.values { + if v != nil { + count++ + } + } + serializedLen := NodeTypeBytes + StemSize + StemBitmapSize + count*HashSize + serialized := make([]byte, serializedLen) + serialized[0] = nodeTypeStem + copy(serialized[NodeTypeBytes:NodeTypeBytes+StemSize], sn.Stem[:]) + bitmap := serialized[NodeTypeBytes+StemSize : NodeTypeBytes+StemSize+StemBitmapSize] + offset := NodeTypeBytes + StemSize + StemBitmapSize + for i, v := range sn.values { + if v != nil { + bitmap[i/8] |= 1 << (7 - (i % 8)) + copy(serialized[offset:offset+HashSize], v) + offset += HashSize + } + } + return serialized + + default: + panic(fmt.Sprintf("SerializeNode: unexpected node kind %d", ref.Kind())) + } +} + +var errInvalidSerializedLength = errors.New("invalid serialized node length") + +// DeserializeNode deserializes a node from bytes, recomputing its hash. The +// returned node is marked dirty (provenance unknown, safe re-flush default). +func (s *nodeStore) deserializeNode(serialized []byte, depth int) (nodeRef, error) { + return s.decodeNode(serialized, depth, common.Hash{}, true, true) +} + +// DeserializeNodeWithHash deserializes a node whose hash is already known and +// whose blob is already on disk (mustRecompute=false, dirty=false). +func (s *nodeStore) deserializeNodeWithHash(serialized []byte, depth int, hn common.Hash) (nodeRef, error) { + return s.decodeNode(serialized, depth, hn, false, false) +} + +func (s *nodeStore) decodeNode(serialized []byte, depth int, hn common.Hash, mustRecompute, dirty bool) (nodeRef, error) { + if len(serialized) == 0 { + return emptyRef, nil + } + + switch serialized[0] { + case nodeTypeInternal: + if len(serialized) != NodeTypeBytes+2*HashSize { + return emptyRef, errInvalidSerializedLength + } + var leftHash, rightHash common.Hash + copy(leftHash[:], serialized[NodeTypeBytes:NodeTypeBytes+HashSize]) + copy(rightHash[:], serialized[NodeTypeBytes+HashSize:]) + + var leftRef, rightRef nodeRef + if leftHash != (common.Hash{}) { + leftRef = s.newHashedRef(leftHash) + } + if rightHash != (common.Hash{}) { + rightRef = s.newHashedRef(rightHash) + } + + ref := s.newInternalRef(depth) + node := s.getInternal(ref.Index()) + node.left = leftRef + node.right = rightRef + if !mustRecompute { + node.hash = hn + node.mustRecompute = false + } + node.dirty = dirty + return ref, nil + + case nodeTypeStem: + if len(serialized) < NodeTypeBytes+StemSize+StemBitmapSize { + return emptyRef, errInvalidSerializedLength + } + stemIdx := s.allocStem() + sn := s.getStem(stemIdx) + copy(sn.Stem[:], serialized[NodeTypeBytes:NodeTypeBytes+StemSize]) + bitmap := serialized[NodeTypeBytes+StemSize : NodeTypeBytes+StemSize+StemBitmapSize] + offset := NodeTypeBytes + StemSize + StemBitmapSize + for i := range StemNodeWidth { + if bitmap[i/8]>>(7-(i%8))&1 != 1 { + continue + } + if len(serialized) < offset+HashSize { + return emptyRef, errInvalidSerializedLength + } + // Zero-copy: each slot aliases the serialized input buffer. + sn.values[i] = serialized[offset : offset+HashSize] + offset += HashSize + } + sn.depth = uint8(depth) + sn.hash = hn + sn.mustRecompute = mustRecompute + sn.dirty = dirty + return makeRef(kindStem, stemIdx), nil + + default: + return emptyRef, errors.New("invalid node type") + } +} + +// CollectNodes flushes every node that needs flushing via flushfn in post-order. +// Invariant: any ancestor of a node that needs flushing is itself marked, so a +// clean root means the whole subtree is clean. +func (s *nodeStore) collectNodes(ref nodeRef, path []byte, flushfn nodeFlushFn) error { + switch ref.Kind() { + case kindEmpty: + return nil + case kindInternal: + node := s.getInternal(ref.Index()) + if !node.dirty { + return nil + } + // Reuse path buffer across children: flushfn consumers + // (NodeSet.AddNode, tracer.Get) clone via string(path), so in-place + // mutation is safe. + path = append(path, 0) + if err := s.collectNodes(node.left, path, flushfn); err != nil { + return err + } + path[len(path)-1] = 1 + if err := s.collectNodes(node.right, path, flushfn); err != nil { + return err + } + path = path[:len(path)-1] + flushfn(path, s.computeHash(ref), s.serializeNode(ref)) + node.dirty = false + return nil + case kindStem: + sn := s.getStem(ref.Index()) + if !sn.dirty { + return nil + } + flushfn(path, s.computeHash(ref), s.serializeNode(ref)) + sn.dirty = false + return nil + case kindHashed: + return nil // Already committed + default: + return fmt.Errorf("CollectNodes: unexpected kind %d", ref.Kind()) + } +} + +func (s *nodeStore) toDot(ref nodeRef, parent, path string) string { + switch ref.Kind() { + case kindInternal: + node := s.getInternal(ref.Index()) + me := fmt.Sprintf("internal%s", path) + ret := fmt.Sprintf("%s [label=\"I: %x\"]\n", me, s.computeHash(ref)) + if len(parent) > 0 { + ret = fmt.Sprintf("%s %s -> %s\n", ret, parent, me) + } + if !node.left.IsEmpty() { + ret += s.toDot(node.left, me, fmt.Sprintf("%s%02x", path, 0)) + } + if !node.right.IsEmpty() { + ret += s.toDot(node.right, me, fmt.Sprintf("%s%02x", path, 1)) + } + return ret + case kindStem: + sn := s.getStem(ref.Index()) + me := fmt.Sprintf("stem%s", path) + ret := fmt.Sprintf("%s [label=\"stem=%x c=%x\"]\n", me, sn.Stem, sn.Hash()) + ret = fmt.Sprintf("%s %s -> %s\n", ret, parent, me) + for i, v := range sn.values { + if v == nil { + continue + } + ret += fmt.Sprintf("%s%x [label=\"%x\"]\n", me, i, v) + ret += fmt.Sprintf("%s -> %s%x\n", me, me, i) + } + return ret + case kindHashed: + hn := s.getHashed(ref.Index()) + me := fmt.Sprintf("hash%s", path) + ret := fmt.Sprintf("%s [label=\"%x\"]\n", me, hn.Hash()) + ret = fmt.Sprintf("%s %s -> %s\n", ret, parent, me) + return ret + default: + return "" + } +} diff --git a/trie/bintrie/store_ops.go b/trie/bintrie/store_ops.go new file mode 100644 index 0000000000..9a73c8bd64 --- /dev/null +++ b/trie/bintrie/store_ops.go @@ -0,0 +1,345 @@ +// Copyright 2026 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 . + +package bintrie + +import ( + "errors" + "fmt" + + "github.com/ethereum/go-ethereum/common" +) + +// nodeResolverFn resolves a hashed node from the database. +type nodeResolverFn func([]byte, common.Hash) ([]byte, error) + +// GetValue returns the value at (stem, suffix) or nil if absent. Thin +// wrapper over GetValuesAtStem — the underlying StemNode returns its +// 256-slot array as a slice header (no allocation), so the per-call cost +// is the tree walk plus one index. +func (s *nodeStore) GetValue(stem []byte, suffix byte, resolver nodeResolverFn) ([]byte, error) { + values, err := s.GetValuesAtStem(stem, resolver) + if err != nil || values == nil { + return nil, err + } + return values[suffix], nil +} + +// GetValuesAtStem returns the 256 value slots at stem, or nil if the stem +// is not in the trie. The returned slice is a view over the in-place +// StemNode values array (no allocation) and must be treated read-only. +func (s *nodeStore) GetValuesAtStem(stem []byte, resolver nodeResolverFn) ([][]byte, error) { + cur := s.root + var parentIdx uint32 + var parentIsLeft bool + + for { + switch cur.Kind() { + case kindInternal: + node := s.getInternal(cur.Index()) + if node.depth >= 31*8 { + return nil, errors.New("node too deep") + } + bit := stem[node.depth/8] >> (7 - (node.depth % 8)) & 1 + parentIdx = cur.Index() + if bit == 0 { + parentIsLeft = true + cur = node.left + } else { + parentIsLeft = false + cur = node.right + } + + case kindStem: + sn := s.getStem(cur.Index()) + if sn.Stem != [StemSize]byte(stem[:StemSize]) { + return nil, nil + } + return sn.allValues(), nil + + case kindHashed: + // HashedNode at root is impossible: NewBinaryTrie resolves the + // root eagerly before any query. Any HashedNode we encounter here + // is necessarily a child of a previously-visited internal node. + if resolver == nil { + return nil, errors.New("getValuesAtStem: cannot resolve hashed node without resolver") + } + hn := s.getHashed(cur.Index()) + parentNode := s.getInternal(parentIdx) + path, err := keyToPath(int(parentNode.depth), stem) + if err != nil { + return nil, fmt.Errorf("getValuesAtStem path error: %w", err) + } + data, err := resolver(path, hn.Hash()) + if err != nil { + return nil, fmt.Errorf("getValuesAtStem resolve error: %w", err) + } + resolved, err := s.deserializeNodeWithHash(data, int(parentNode.depth)+1, hn.Hash()) + if err != nil { + return nil, fmt.Errorf("getValuesAtStem deserialization error: %w", err) + } + s.freeHashedNode(cur.Index()) + if parentIsLeft { + parentNode.left = resolved + } else { + parentNode.right = resolved + } + cur = resolved + + case kindEmpty: + var values [StemNodeWidth][]byte + return values[:], nil + + default: + return nil, fmt.Errorf("getValuesAtStem: unexpected node kind %d", cur.Kind()) + } + } +} + +// InsertSingle writes a single value slot at (stem, suffix). Thin wrapper +// over InsertValuesAtStem — builds a stack-allocated 256-slot array with +// only the target slot set and delegates. Matches the original design +// gballet referenced (comment 3101751325): one primary insert path; the +// single-slot variant dispatches through it so the split / resolve logic +// lives in one place. +func (s *nodeStore) InsertSingle(stem []byte, suffix byte, value []byte, resolver nodeResolverFn) error { + if len(value) != HashSize { + return errors.New("invalid insertion: value length") + } + var values [StemNodeWidth][]byte + values[suffix] = value + return s.InsertValuesAtStem(stem, values[:], resolver) +} + +// InsertValuesAtStem writes the supplied value slots at stem. values may be +// sparse (nil entries are ignored). The recursive implementation dispatches +// through the same body, so a single code path handles internal descent, +// HashedNode resolution, stem merge, and stem split. +func (s *nodeStore) InsertValuesAtStem(stem []byte, values [][]byte, resolver nodeResolverFn) error { + var err error + s.root, err = s.insertValuesAtStem(s.root, stem, values, resolver, 0) + return err +} + +func (s *nodeStore) insertValuesAtStem(ref nodeRef, stem []byte, values [][]byte, resolver nodeResolverFn, depth int) (nodeRef, error) { + switch ref.Kind() { + case kindInternal: + node := s.getInternal(ref.Index()) + bit := stem[node.depth/8] >> (7 - (node.depth % 8)) & 1 + if bit == 0 { + if node.left.Kind() == kindHashed { + if resolver == nil { + return ref, errors.New("insertValuesAtStem: cannot resolve hashed node without resolver") + } + hn := s.getHashed(node.left.Index()) + path, err := keyToPath(int(node.depth), stem) + if err != nil { + return ref, fmt.Errorf("InsertValuesAtStem path error: %w", err) + } + data, err := resolver(path, hn.Hash()) + if err != nil { + return ref, fmt.Errorf("InsertValuesAtStem resolve error: %w", err) + } + resolved, err := s.deserializeNodeWithHash(data, int(node.depth)+1, hn.Hash()) + if err != nil { + return ref, fmt.Errorf("InsertValuesAtStem deserialization error: %w", err) + } + s.freeHashedNode(node.left.Index()) + node.left = resolved + } + newChild, err := s.insertValuesAtStem(node.left, stem, values, resolver, depth+1) + if err != nil { + return ref, err + } + node.left = newChild + } else { + if node.right.Kind() == kindHashed { + if resolver == nil { + return ref, errors.New("insertValuesAtStem: cannot resolve hashed node without resolver") + } + hn := s.getHashed(node.right.Index()) + path, err := keyToPath(int(node.depth), stem) + if err != nil { + return ref, fmt.Errorf("InsertValuesAtStem path error: %w", err) + } + data, err := resolver(path, hn.Hash()) + if err != nil { + return ref, fmt.Errorf("InsertValuesAtStem resolve error: %w", err) + } + resolved, err := s.deserializeNodeWithHash(data, int(node.depth)+1, hn.Hash()) + if err != nil { + return ref, fmt.Errorf("InsertValuesAtStem deserialization error: %w", err) + } + s.freeHashedNode(node.right.Index()) + node.right = resolved + } + newChild, err := s.insertValuesAtStem(node.right, stem, values, resolver, depth+1) + if err != nil { + return ref, err + } + node.right = newChild + } + node.mustRecompute = true + node.dirty = true + return ref, nil + + case kindStem: + sn := s.getStem(ref.Index()) + if sn.Stem == [StemSize]byte(stem[:StemSize]) { + // Same stem — merge values (setValue marks dirty+mustRecompute) + for i, v := range values { + if v != nil { + sn.setValue(byte(i), v) + } + } + return ref, nil + } + // Different stem — split + return s.splitStemValuesInsert(ref, stem, values, resolver, depth) + + case kindHashed: + hn := s.getHashed(ref.Index()) + path, err := keyToPath(depth, stem) + if err != nil { + return ref, fmt.Errorf("InsertValuesAtStem path error: %w", err) + } + if resolver == nil { + return ref, errors.New("InsertValuesAtStem: resolver is nil") + } + data, err := resolver(path, hn.Hash()) + if err != nil { + return ref, fmt.Errorf("InsertValuesAtStem resolve error: %w", err) + } + resolved, err := s.deserializeNodeWithHash(data, depth, hn.Hash()) + if err != nil { + return ref, fmt.Errorf("InsertValuesAtStem deserialization error: %w", err) + } + s.freeHashedNode(ref.Index()) + return s.insertValuesAtStem(resolved, stem, values, resolver, depth) + + case kindEmpty: + // Create new StemNode. Flag flips before the value loop so an + // all-nil values input still marks the newly-created stem dirty. + stemIdx := s.allocStem() + sn := s.getStem(stemIdx) + copy(sn.Stem[:], stem[:StemSize]) + sn.depth = uint8(depth) + sn.mustRecompute = true + sn.dirty = true + for i, v := range values { + if v != nil { + sn.setValue(byte(i), v) + } + } + return makeRef(kindStem, stemIdx), nil + + default: + return ref, fmt.Errorf("insertValuesAtStem: unexpected kind %d", ref.Kind()) + } +} + +// splitStemValuesInsert splits a StemNode when the new stem diverges. +func (s *nodeStore) splitStemValuesInsert(existingRef nodeRef, newStem []byte, values [][]byte, resolver nodeResolverFn, depth int) (nodeRef, error) { + existing := s.getStem(existingRef.Index()) + + if int(existing.depth) >= StemSize*8 { + panic("splitStemValuesInsert: identical stems") + } + + bitStem := existing.Stem[existing.depth/8] >> (7 - (existing.depth % 8)) & 1 + nRef := s.newInternalRef(int(existing.depth)) + nNode := s.getInternal(nRef.Index()) + existing.depth++ + + bitKey := newStem[nNode.depth/8] >> (7 - (nNode.depth % 8)) & 1 + if bitKey == bitStem { + // Same direction — need deeper split + var child nodeRef + if bitStem == 0 { + nNode.left = existingRef + child = nNode.left + } else { + nNode.right = existingRef + child = nNode.right + } + newChild, err := s.insertValuesAtStem(child, newStem, values, resolver, depth+1) + if err != nil { + // Roll back the depth increment so a retry sees the same + // existing state and extracts bitStem at the correct offset. + // nRef itself leaks (no internal free-list), but the slot is + // unreachable from the tree and harmless. + existing.depth-- + return nRef, err + } + if bitStem == 0 { + nNode.left = newChild + nNode.right = emptyRef + } else { + nNode.right = newChild + nNode.left = emptyRef + } + } else { + // Divergence — create new StemNode for the new values + newStemIdx := s.allocStem() + newSn := s.getStem(newStemIdx) + copy(newSn.Stem[:], newStem[:StemSize]) + newSn.depth = nNode.depth + 1 + newSn.mustRecompute = true + newSn.dirty = true + for i, v := range values { + if v != nil { + newSn.setValue(byte(i), v) + } + } + newStemRef := makeRef(kindStem, newStemIdx) + + if bitStem == 0 { + nNode.left = existingRef + nNode.right = newStemRef + } else { + nNode.left = newStemRef + nNode.right = existingRef + } + } + return nRef, nil +} + +func (s *nodeStore) Insert(key []byte, value []byte, resolver nodeResolverFn) error { + return s.InsertSingle(key[:StemSize], key[StemSize], value, resolver) +} + +func (s *nodeStore) Get(key []byte, resolver nodeResolverFn) ([]byte, error) { + return s.GetValue(key[:StemSize], key[StemSize], resolver) +} + +func (s *nodeStore) getHeight(ref nodeRef) int { + switch ref.Kind() { + case kindInternal: + node := s.getInternal(ref.Index()) + lh := s.getHeight(node.left) + rh := s.getHeight(node.right) + if lh > rh { + return 1 + lh + } + return 1 + rh + case kindStem: + return 1 + case kindEmpty: + return 0 + default: + return 0 + } +} diff --git a/trie/bintrie/trie.go b/trie/bintrie/trie.go index 23d014eb33..8c69e0aa00 100644 --- a/trie/bintrie/trie.go +++ b/trie/bintrie/trie.go @@ -19,7 +19,6 @@ package bintrie import ( "bytes" "encoding/binary" - "errors" "fmt" "github.com/ethereum/go-ethereum/common" @@ -31,8 +30,6 @@ import ( "github.com/holiman/uint256" ) -var errInvalidRootType = errors.New("invalid root type") - // ChunkedCode represents a sequence of HashSize-byte chunks of code (StemSize bytes of which // are actual code, and NodeTypeBytes byte is the pushdata offset). type ChunkedCode []byte @@ -108,22 +105,17 @@ func ChunkifyCode(code []byte) ChunkedCode { return chunks } -// NewBinaryNode creates a new empty binary trie -func NewBinaryNode() BinaryNode { - return Empty{} -} - // BinaryTrie is the implementation of https://eips.ethereum.org/EIPS/eip-7864. type BinaryTrie struct { - root BinaryNode + store *nodeStore reader *trie.Reader tracer *trie.PrevalueTracer } // ToDot converts the binary trie to a DOT language representation. Useful for debugging. func (t *BinaryTrie) ToDot() string { - t.root.Hash() - return ToDot(t.root) + t.store.computeHash(t.store.root) + return t.store.toDot(t.store.root, "", "") } // NewBinaryTrie creates a new binary trie. @@ -133,7 +125,7 @@ func NewBinaryTrie(root common.Hash, db database.NodeDatabase) (*BinaryTrie, err return nil, err } t := &BinaryTrie{ - root: NewBinaryNode(), + store: newNodeStore(), reader: reader, tracer: trie.NewPrevalueTracer(), } @@ -143,11 +135,11 @@ func NewBinaryTrie(root common.Hash, db database.NodeDatabase) (*BinaryTrie, err if err != nil { return nil, err } - node, err := DeserializeNodeWithHash(blob, 0, root) + ref, err := t.store.deserializeNodeWithHash(blob, 0, root) if err != nil { return nil, err } - t.root = node + t.store.root = ref } return t, nil } @@ -176,29 +168,18 @@ func (t *BinaryTrie) GetKey(key []byte) []byte { // GetWithHashedKey returns the value, assuming that the key has already // been hashed. func (t *BinaryTrie) GetWithHashedKey(key []byte) ([]byte, error) { - return t.root.Get(key, t.nodeResolver) + return t.store.Get(key, t.nodeResolver) } // GetAccount returns the account information for the given address. func (t *BinaryTrie) GetAccount(addr common.Address) (*types.StateAccount, error) { var ( - values [][]byte - err error - acc = &types.StateAccount{} - key = GetBinaryTreeKey(addr, zero[:]) + err error + acc = &types.StateAccount{} + key = GetBinaryTreeKey(addr, zero[:]) ) - switch r := t.root.(type) { - case *InternalNode: - values, err = r.GetValuesAtStem(key[:StemSize], t.nodeResolver) - case *StemNode: - values, err = r.GetValuesAtStem(key[:StemSize], t.nodeResolver) - case Empty: - return nil, nil - default: - // This will cover HashedNode but that should be fine since the - // root node should always be resolved. - return nil, errInvalidRootType - } + + values, err := t.store.GetValuesAtStem(key[:StemSize], t.nodeResolver) if err != nil { return nil, fmt.Errorf("GetAccount (%x) error: %v", addr, err) } @@ -219,7 +200,7 @@ func (t *BinaryTrie) GetAccount(addr common.Address) (*types.StateAccount, error // If the account has been deleted, BasicData and CodeHash will both be // 32-byte zero blobs (not nil). If the account is recreated afterwards, // UpdateAccount overwrites BasicData and CodeHash with non-zero values, - // so this branch won't activate.. + // so this branch won't activate. if bytes.Equal(values[BasicDataLeafKey], zero[:]) && bytes.Equal(values[CodeHashLeafKey], zero[:]) { return nil, nil @@ -238,13 +219,12 @@ func (t *BinaryTrie) GetAccount(addr common.Address) (*types.StateAccount, error // not be modified by the caller. If a node was not found in the database, a // trie.MissingNodeError is returned. func (t *BinaryTrie) GetStorage(addr common.Address, key []byte) ([]byte, error) { - return t.root.Get(GetBinaryTreeKeyStorageSlot(addr, key), t.nodeResolver) + return t.store.Get(GetBinaryTreeKeyStorageSlot(addr, key), t.nodeResolver) } // UpdateAccount updates the account information for the given address. func (t *BinaryTrie) UpdateAccount(addr common.Address, acc *types.StateAccount, codeLen int) error { var ( - err error basicData [HashSize]byte values = make([][]byte, StemNodeWidth) stem = GetBinaryTreeKey(addr, zero[:]) @@ -265,15 +245,12 @@ func (t *BinaryTrie) UpdateAccount(addr common.Address, acc *types.StateAccount, values[BasicDataLeafKey] = basicData[:] values[CodeHashLeafKey] = acc.CodeHash[:] - t.root, err = t.root.InsertValuesAtStem(stem, values, t.nodeResolver, 0) - return err + return t.store.InsertValuesAtStem(stem, values, t.nodeResolver) } // UpdateStem updates the values for the given stem key. func (t *BinaryTrie) UpdateStem(key []byte, values [][]byte) error { - var err error - t.root, err = t.root.InsertValuesAtStem(key, values, t.nodeResolver, 0) - return err + return t.store.InsertValuesAtStem(key, values, t.nodeResolver) } // UpdateStorage associates key with value in the trie. If value has length zero, any @@ -288,11 +265,10 @@ func (t *BinaryTrie) UpdateStorage(address common.Address, key, value []byte) er } else { copy(v[HashSize-len(value):], value[:]) } - root, err := t.root.Insert(k, v[:], t.nodeResolver, 0) + err := t.store.Insert(k, v[:], t.nodeResolver) if err != nil { return fmt.Errorf("UpdateStorage (%x) error: %v", address, err) } - t.root = root return nil } @@ -307,12 +283,7 @@ func (t *BinaryTrie) DeleteAccount(addr common.Address) error { values[BasicDataLeafKey] = zero[:] values[CodeHashLeafKey] = zero[:] - root, err := t.root.InsertValuesAtStem(stem, values, t.nodeResolver, 0) - if err != nil { - return fmt.Errorf("DeleteAccount (%x) error: %v", addr, err) - } - t.root = root - return nil + return t.store.InsertValuesAtStem(stem, values, t.nodeResolver) } // DeleteStorage removes any existing value for key from the trie. If a node was not @@ -320,18 +291,17 @@ func (t *BinaryTrie) DeleteAccount(addr common.Address) error { func (t *BinaryTrie) DeleteStorage(addr common.Address, key []byte) error { k := GetBinaryTreeKeyStorageSlot(addr, key) var zero [HashSize]byte - root, err := t.root.Insert(k, zero[:], t.nodeResolver, 0) + err := t.store.Insert(k, zero[:], t.nodeResolver) if err != nil { return fmt.Errorf("DeleteStorage (%x) error: %v", addr, err) } - t.root = root return nil } // Hash returns the root hash of the trie. It does not write to the database and // can be used even if the trie doesn't have one. func (t *BinaryTrie) Hash() common.Hash { - return t.root.Hash() + return t.store.computeHash(t.store.root) } // Commit writes all nodes to the trie's memory database, tracking the internal @@ -339,15 +309,15 @@ func (t *BinaryTrie) Hash() common.Hash { func (t *BinaryTrie) Commit(_ bool) (common.Hash, *trienode.NodeSet) { nodeset := trienode.NewNodeSet(common.Hash{}) - // The root can be any type of BinaryNode (InternalNode, StemNode, etc.) - err := t.root.CollectNodes(nil, func(path []byte, node BinaryNode) { - serialized := SerializeNode(node) - nodeset.AddNode(path, trienode.NewNodeWithPrev(node.Hash(), serialized, t.tracer.Get(path))) + // Pre-size the path buffer: collectNodes reuses it in-place via + // append/truncate; 32 covers typical binary-trie depth without regrowth. + pathBuf := make([]byte, 0, 32) + err := t.store.collectNodes(t.store.root, pathBuf, func(path []byte, hash common.Hash, serialized []byte) { + nodeset.AddNode(path, trienode.NewNodeWithPrev(hash, serialized, t.tracer.Get(path))) }) if err != nil { panic(fmt.Errorf("CollectNodes failed: %v", err)) } - // Serialize root commitment form return t.Hash(), nodeset } @@ -371,7 +341,7 @@ func (t *BinaryTrie) Prove(key []byte, proofDb ethdb.KeyValueWriter) error { // Copy creates a deep copy of the trie. func (t *BinaryTrie) Copy() *BinaryTrie { return &BinaryTrie{ - root: t.root.Copy(), + store: t.store.Copy(), reader: t.reader, tracer: t.tracer.Copy(), } @@ -407,7 +377,6 @@ func (t *BinaryTrie) UpdateContractCode(addr common.Address, codeHash common.Has if groupOffset == StemNodeWidth-1 || len(chunks)-i <= HashSize { err = t.UpdateStem(key[:StemSize], values) - if err != nil { return fmt.Errorf("UpdateContractCode (addr=%x) error: %w", addr[:], err) } diff --git a/trie/bintrie/trie_test.go b/trie/bintrie/trie_test.go index c436501464..73aacb76c4 100644 --- a/trie/bintrie/trie_test.go +++ b/trie/bintrie/trie_test.go @@ -37,147 +37,130 @@ var ( ) func TestSingleEntry(t *testing.T) { - tree := NewBinaryNode() - tree, err := tree.Insert(zeroKey[:], oneKey[:], nil, 0) - if err != nil { + s := newNodeStore() + if err := s.Insert(zeroKey[:], oneKey[:], nil); err != nil { t.Fatal(err) } - if tree.GetHeight() != 1 { + if s.getHeight(s.root) != 1 { t.Fatal("invalid depth") } expected := common.HexToHash("aab1060e04cb4f5dc6f697ae93156a95714debbf77d54238766adc5709282b6f") - got := tree.Hash() + got := s.Hash() if got != expected { t.Fatalf("invalid tree root, got %x, want %x", got, expected) } } func TestTwoEntriesDiffFirstBit(t *testing.T) { - var err error - tree := NewBinaryNode() - tree, err = tree.Insert(zeroKey[:], oneKey[:], nil, 0) - if err != nil { + s := newNodeStore() + if err := s.Insert(zeroKey[:], oneKey[:], nil); err != nil { t.Fatal(err) } - tree, err = tree.Insert(common.HexToHash("8000000000000000000000000000000000000000000000000000000000000000").Bytes(), twoKey[:], nil, 0) - if err != nil { + if err := s.Insert(common.HexToHash("8000000000000000000000000000000000000000000000000000000000000000").Bytes(), twoKey[:], nil); err != nil { t.Fatal(err) } - if tree.GetHeight() != 2 { + if s.getHeight(s.root) != 2 { t.Fatal("invalid height") } - if tree.Hash() != common.HexToHash("dfc69c94013a8b3c65395625a719a87534a7cfd38719251ad8c8ea7fe79f065e") { + if s.Hash() != common.HexToHash("dfc69c94013a8b3c65395625a719a87534a7cfd38719251ad8c8ea7fe79f065e") { t.Fatal("invalid tree root") } } func TestOneStemColocatedValues(t *testing.T) { - var err error - tree := NewBinaryNode() - tree, err = tree.Insert(common.HexToHash("0000000000000000000000000000000000000000000000000000000000000003").Bytes(), oneKey[:], nil, 0) - if err != nil { + s := newNodeStore() + if err := s.Insert(common.HexToHash("0000000000000000000000000000000000000000000000000000000000000003").Bytes(), oneKey[:], nil); err != nil { t.Fatal(err) } - tree, err = tree.Insert(common.HexToHash("0000000000000000000000000000000000000000000000000000000000000004").Bytes(), twoKey[:], nil, 0) - if err != nil { + if err := s.Insert(common.HexToHash("0000000000000000000000000000000000000000000000000000000000000004").Bytes(), twoKey[:], nil); err != nil { t.Fatal(err) } - tree, err = tree.Insert(common.HexToHash("0000000000000000000000000000000000000000000000000000000000000009").Bytes(), threeKey[:], nil, 0) - if err != nil { + if err := s.Insert(common.HexToHash("0000000000000000000000000000000000000000000000000000000000000009").Bytes(), threeKey[:], nil); err != nil { t.Fatal(err) } - tree, err = tree.Insert(common.HexToHash("00000000000000000000000000000000000000000000000000000000000000FF").Bytes(), fourKey[:], nil, 0) - if err != nil { + if err := s.Insert(common.HexToHash("00000000000000000000000000000000000000000000000000000000000000FF").Bytes(), fourKey[:], nil); err != nil { t.Fatal(err) } - if tree.GetHeight() != 1 { + if s.getHeight(s.root) != 1 { t.Fatal("invalid height") } } func TestTwoStemColocatedValues(t *testing.T) { - var err error - tree := NewBinaryNode() + s := newNodeStore() // stem: 0...0 - tree, err = tree.Insert(common.HexToHash("0000000000000000000000000000000000000000000000000000000000000003").Bytes(), oneKey[:], nil, 0) - if err != nil { + if err := s.Insert(common.HexToHash("0000000000000000000000000000000000000000000000000000000000000003").Bytes(), oneKey[:], nil); err != nil { t.Fatal(err) } - tree, err = tree.Insert(common.HexToHash("0000000000000000000000000000000000000000000000000000000000000004").Bytes(), twoKey[:], nil, 0) - if err != nil { + if err := s.Insert(common.HexToHash("0000000000000000000000000000000000000000000000000000000000000004").Bytes(), twoKey[:], nil); err != nil { t.Fatal(err) } // stem: 10...0 - tree, err = tree.Insert(common.HexToHash("8000000000000000000000000000000000000000000000000000000000000003").Bytes(), oneKey[:], nil, 0) - if err != nil { + if err := s.Insert(common.HexToHash("8000000000000000000000000000000000000000000000000000000000000003").Bytes(), oneKey[:], nil); err != nil { t.Fatal(err) } - tree, err = tree.Insert(common.HexToHash("8000000000000000000000000000000000000000000000000000000000000004").Bytes(), twoKey[:], nil, 0) - if err != nil { + if err := s.Insert(common.HexToHash("8000000000000000000000000000000000000000000000000000000000000004").Bytes(), twoKey[:], nil); err != nil { t.Fatal(err) } - if tree.GetHeight() != 2 { + if s.getHeight(s.root) != 2 { t.Fatal("invalid height") } } func TestTwoKeysMatchFirst42Bits(t *testing.T) { - var err error - tree := NewBinaryNode() + s := newNodeStore() // key1 and key 2 have the same prefix of 42 bits (b0*42+b1+b1) and differ after. key1 := common.HexToHash("0000000000C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0").Bytes() key2 := common.HexToHash("0000000000E00000000000000000000000000000000000000000000000000000").Bytes() - tree, err = tree.Insert(key1, oneKey[:], nil, 0) - if err != nil { + if err := s.Insert(key1, oneKey[:], nil); err != nil { t.Fatal(err) } - tree, err = tree.Insert(key2, twoKey[:], nil, 0) - if err != nil { + if err := s.Insert(key2, twoKey[:], nil); err != nil { t.Fatal(err) } - if tree.GetHeight() != 1+42+1 { + if s.getHeight(s.root) != 1+42+1 { t.Fatal("invalid height") } } + func TestInsertDuplicateKey(t *testing.T) { - var err error - tree := NewBinaryNode() - tree, err = tree.Insert(oneKey[:], oneKey[:], nil, 0) - if err != nil { + s := newNodeStore() + if err := s.Insert(oneKey[:], oneKey[:], nil); err != nil { t.Fatal(err) } - tree, err = tree.Insert(oneKey[:], twoKey[:], nil, 0) - if err != nil { + if err := s.Insert(oneKey[:], twoKey[:], nil); err != nil { t.Fatal(err) } - if tree.GetHeight() != 1 { + if s.getHeight(s.root) != 1 { t.Fatal("invalid height") } // Verify that the value is updated - if !bytes.Equal(tree.(*StemNode).Values[1], twoKey[:]) { - t.Fatal("invalid height") + v, err := s.Get(oneKey[:], nil) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(v, twoKey[:]) { + t.Fatal("value not updated") } } + func TestLargeNumberOfEntries(t *testing.T) { - var err error - tree := NewBinaryNode() + s := newNodeStore() for i := range StemNodeWidth { var key [HashSize]byte key[0] = byte(i) - tree, err = tree.Insert(key[:], ffKey[:], nil, 0) - if err != nil { + if err := s.Insert(key[:], ffKey[:], nil); err != nil { t.Fatal(err) } } - height := tree.GetHeight() + height := s.getHeight(s.root) if height != 1+8 { t.Fatalf("invalid height, wanted %d, got %d", 1+8, height) } } func TestMerkleizeMultipleEntries(t *testing.T) { - var err error - tree := NewBinaryNode() + s := newNodeStore() keys := [][]byte{ zeroKey[:], common.HexToHash("8000000000000000000000000000000000000000000000000000000000000000").Bytes(), @@ -187,12 +170,11 @@ func TestMerkleizeMultipleEntries(t *testing.T) { for i, key := range keys { var v [HashSize]byte binary.LittleEndian.PutUint64(v[:8], uint64(i)) - tree, err = tree.Insert(key, v[:], nil, 0) - if err != nil { + if err := s.Insert(key, v[:], nil); err != nil { t.Fatal(err) } } - got := tree.Hash() + got := s.Hash() expected := common.HexToHash("9317155862f7a3867660ddd0966ff799a3d16aa4df1e70a7516eaa4a675191b5") if got != expected { t.Fatalf("invalid root, expected=%x, got = %x", expected, got) @@ -206,7 +188,7 @@ func TestMerkleizeMultipleEntries(t *testing.T) { func TestStorageRoundTrip(t *testing.T) { tracer := trie.NewPrevalueTracer() tr := &BinaryTrie{ - root: NewBinaryNode(), + store: newNodeStore(), tracer: tracer, } addr := common.HexToAddress("0x1234567890abcdef1234567890abcdef12345678") @@ -274,7 +256,7 @@ func TestStorageRoundTrip(t *testing.T) { func newEmptyTestTrie(t *testing.T) *BinaryTrie { t.Helper() return &BinaryTrie{ - root: NewBinaryNode(), + store: newNodeStore(), tracer: trie.NewPrevalueTracer(), } } @@ -599,7 +581,7 @@ func TestBinaryTrieWitness(t *testing.T) { tracer := trie.NewPrevalueTracer() tr := &BinaryTrie{ - root: NewBinaryNode(), + store: newNodeStore(), tracer: tracer, } if w := tr.Witness(); len(w) != 0 { @@ -626,7 +608,7 @@ func TestBinaryTrieWitness(t *testing.T) { func testAccount(t *testing.T, addr common.Address, nonce uint64, balance uint64) *BinaryTrie { t.Helper() tr := &BinaryTrie{ - root: NewBinaryNode(), + store: newNodeStore(), tracer: trie.NewPrevalueTracer(), } acc := &types.StateAccount{ @@ -649,8 +631,8 @@ func TestGetAccountNonMembershipStemRoot(t *testing.T) { tr := testAccount(t, addr, 42, 100) // Verify root is a StemNode (single stem inserted). - if _, ok := tr.root.(*StemNode); !ok { - t.Fatalf("expected StemNode root, got %T", tr.root) + if tr.store.root.Kind() != kindStem { + t.Fatalf("expected StemNode root, got kind %d", tr.store.root.Kind()) } // Query a completely different address — must return nil. @@ -680,7 +662,7 @@ func TestGetAccountNonMembershipStemRoot(t *testing.T) { // address returns nil when the trie root is an InternalNode (multi-account trie). func TestGetAccountNonMembershipInternalRoot(t *testing.T) { tr := &BinaryTrie{ - root: NewBinaryNode(), + store: newNodeStore(), tracer: trie.NewPrevalueTracer(), } @@ -700,8 +682,8 @@ func TestGetAccountNonMembershipInternalRoot(t *testing.T) { } // Verify root is an InternalNode. - if _, ok := tr.root.(*InternalNode); !ok { - t.Fatalf("expected InternalNode root, got %T", tr.root) + if tr.store.root.Kind() != kindInternal { + t.Fatalf("expected InternalNode root, got kind %d", tr.store.root.Kind()) } // Query a non-existent address — must return nil. @@ -723,8 +705,8 @@ func TestGetStorageNonMembershipStemRoot(t *testing.T) { tr := testAccount(t, addr, 1, 100) // Verify root is a StemNode. - if _, ok := tr.root.(*StemNode); !ok { - t.Fatalf("expected StemNode root, got %T", tr.root) + if tr.store.root.Kind() != kindStem { + t.Fatalf("expected StemNode root, got kind %d", tr.store.root.Kind()) } // Query storage for a different address — must return nil, not panic. @@ -743,7 +725,7 @@ func TestGetStorageNonMembershipStemRoot(t *testing.T) { // non-existent address returns nil when the root is an InternalNode. func TestGetStorageNonMembershipInternalRoot(t *testing.T) { tr := &BinaryTrie{ - root: NewBinaryNode(), + store: newNodeStore(), tracer: trie.NewPrevalueTracer(), } @@ -765,8 +747,8 @@ func TestGetStorageNonMembershipInternalRoot(t *testing.T) { t.Fatalf("UpdateStorage error: %v", err) } - if _, ok := tr.root.(*InternalNode); !ok { - t.Fatalf("expected InternalNode root, got %T", tr.root) + if tr.store.root.Kind() != kindInternal { + t.Fatalf("expected InternalNode root, got kind %d", tr.store.root.Kind()) } // Query storage for a non-existent address — must return nil. @@ -780,36 +762,29 @@ func TestGetStorageNonMembershipInternalRoot(t *testing.T) { } } -// commitKeyN derives a distinct 32-byte key from a seed integer. Used by -// TestBinaryTrieCommitIncremental and BenchmarkCollectNodes_SparseWrite to -// populate a trie with many disjoint stems. -func commitKeyN(i int) [HashSize]byte { - var k [HashSize]byte - binary.BigEndian.PutUint64(k[:8], uint64(i)*0x9e3779b97f4a7c15) - binary.BigEndian.PutUint64(k[8:16], uint64(i)*0xc2b2ae3d27d4eb4f) - binary.BigEndian.PutUint64(k[16:24], uint64(i)*0x165667b19e3779f9) - binary.BigEndian.PutUint64(k[24:32], uint64(i)*0x85ebca77c2b2ae63) - return k -} - -// TestBinaryTrieCommitIncremental verifies that a second Commit with only a -// single modified leaf flushes only the path from that leaf to the root, -// not the entire tree. -func TestBinaryTrieCommitIncremental(t *testing.T) { +// TestCommitSkipCleanSubtrees verifies that CollectNodes short-circuits on +// clean subtrees. First Commit flushes every resolved node; a follow-up +// Commit with no modifications flushes nothing; a single-leaf modification +// flushes only the root-to-leaf path. +func TestCommitSkipCleanSubtrees(t *testing.T) { tr := &BinaryTrie{ - root: NewBinaryNode(), + store: newNodeStore(), tracer: trie.NewPrevalueTracer(), } - - const n = 512 - keys := make([][HashSize]byte, n) + const n = 200 + key := func(i int) [HashSize]byte { + var k [HashSize]byte + binary.BigEndian.PutUint64(k[:8], uint64(i+1)*0x9e3779b97f4a7c15) + binary.BigEndian.PutUint64(k[8:16], uint64(i+1)*0xc2b2ae3d27d4eb4f) + binary.BigEndian.PutUint64(k[16:24], uint64(i+1)*0x165667b19e3779f9) + binary.BigEndian.PutUint64(k[24:32], uint64(i+1)*0x85ebca77c2b2ae63) + return k + } for i := range n { - keys[i] = commitKeyN(i + 1) + k := key(i) var v [HashSize]byte binary.BigEndian.PutUint64(v[24:], uint64(i+1)) - var err error - tr.root, err = tr.root.Insert(keys[i][:], v[:], nil, 0) - if err != nil { + if err := tr.store.Insert(k[:], v[:], nil); err != nil { t.Fatalf("Insert %d: %v", i, err) } } @@ -818,69 +793,55 @@ func TestBinaryTrieCommitIncremental(t *testing.T) { if len(ns1.Nodes) == 0 { t.Fatal("first Commit produced empty NodeSet") } - if len(ns1.Nodes) < n { - t.Fatalf("first Commit: expected at least %d nodes, got %d", n, len(ns1.Nodes)) - } - // Second Commit on the same trie with no modifications: NodeSet must - // be empty because every subtree is clean. _, nsNoop := tr.Commit(false) if len(nsNoop.Nodes) != 0 { - t.Fatalf("no-op Commit: expected empty NodeSet, got %d nodes", len(nsNoop.Nodes)) + t.Fatalf("no-op Commit: expected empty NodeSet, got %d", len(nsNoop.Nodes)) } - // Modify a single leaf's value. Only the path from that leaf to the - // root should appear in the next Commit's NodeSet. + // Modify a single leaf — only the root-to-leaf path should flush. + k := key(n / 2) var newVal [HashSize]byte newVal[0] = 0xff - var err error - tr.root, err = tr.root.Insert(keys[n/2][:], newVal[:], nil, 0) - if err != nil { + if err := tr.store.Insert(k[:], newVal[:], nil); err != nil { t.Fatalf("Insert (modify): %v", err) } _, ns2 := tr.Commit(false) - - // Path length for a binary trie of n=512 stems is bounded by the - // internal depth at which the modified stem sits. Allow generous - // slack: up to 64 nodes is fine, anywhere near n (512) is a regression. if len(ns2.Nodes) == 0 { t.Fatal("modified Commit produced empty NodeSet") } - if len(ns2.Nodes) > 64 { - t.Fatalf("modified Commit: expected small NodeSet, got %d nodes (first Commit had %d)", len(ns2.Nodes), len(ns1.Nodes)) + if len(ns2.Nodes) > 32 { + t.Fatalf("modified Commit: expected ≤32 nodes (path+stem), got %d", len(ns2.Nodes)) } if len(ns2.Nodes) >= len(ns1.Nodes) { t.Fatalf("expected second NodeSet (%d) to be smaller than first (%d)", len(ns2.Nodes), len(ns1.Nodes)) } } -// BenchmarkCollectNodes_SparseWrite measures Commit cost when only one leaf -// changes between blocks — the common case for state updates. After warm-up +// BenchmarkCollectNodesSparseWrite measures Commit cost when one leaf +// changes per block — the common case for state updates. After warm-up // (populate + initial Commit), each iteration modifies a single leaf and -// re-Commits. Under the skip-clean optimization, each iteration flushes -// only the root-to-leaf path; pre-fix behavior would re-flush the entire -// tree every iteration. -func BenchmarkCollectNodes_SparseWrite(b *testing.B) { +// re-Commits. Matches the shape of the same-named benchmark on master so +// the two trees can be benchstat'd directly. +func BenchmarkCollectNodesSparseWrite(b *testing.B) { const n = 10_000 - tr := &BinaryTrie{ - root: NewBinaryNode(), + store: newNodeStore(), tracer: trie.NewPrevalueTracer(), } keys := make([][HashSize]byte, n) for i := range n { - keys[i] = commitKeyN(i + 1) + binary.BigEndian.PutUint64(keys[i][:8], uint64(i+1)*0x9e3779b97f4a7c15) + binary.BigEndian.PutUint64(keys[i][8:16], uint64(i+1)*0xc2b2ae3d27d4eb4f) + binary.BigEndian.PutUint64(keys[i][16:24], uint64(i+1)*0x165667b19e3779f9) + binary.BigEndian.PutUint64(keys[i][24:32], uint64(i+1)*0x85ebca77c2b2ae63) var v [HashSize]byte binary.BigEndian.PutUint64(v[24:], uint64(i+1)) - var err error - tr.root, err = tr.root.Insert(keys[i][:], v[:], nil, 0) - if err != nil { - b.Fatalf("Insert %d: %v", i, err) + if err := tr.store.Insert(keys[i][:], v[:], nil); err != nil { + b.Fatalf("warmup Insert %d: %v", i, err) } } - // Flush the initial tree so subsequent Commits reflect the - // single-modification workload we want to measure. - _, _ = tr.Commit(false) + _, _ = tr.Commit(false) // warmup flush var newVal [HashSize]byte b.ReportAllocs() @@ -888,10 +849,8 @@ func BenchmarkCollectNodes_SparseWrite(b *testing.B) { for i := 0; i < b.N; i++ { idx := i % n binary.BigEndian.PutUint64(newVal[24:], uint64(i+1)) - var err error - tr.root, err = tr.root.Insert(keys[idx][:], newVal[:], nil, 0) - if err != nil { - b.Fatalf("Insert at iter %d: %v", i, err) + if err := tr.store.Insert(keys[idx][:], newVal[:], nil); err != nil { + b.Fatalf("iter %d Insert: %v", i, err) } _, ns := tr.Commit(false) if len(ns.Nodes) == 0 { diff --git a/triedb/pathdb/database.go b/triedb/pathdb/database.go index 04c76cfd53..98975d7fa5 100644 --- a/triedb/pathdb/database.go +++ b/triedb/pathdb/database.go @@ -102,11 +102,7 @@ func binaryNodeHasher(blob []byte) (common.Hash, error) { if len(blob) == 0 { return types.EmptyBinaryHash, nil } - n, err := bintrie.DeserializeNode(blob, 0) - if err != nil { - return common.Hash{}, err - } - return n.Hash(), nil + return bintrie.DeserializeAndHash(blob, 0) } // Database is a multiple-layered structure for maintaining in-memory states From 7e388fd09ee286967a47c2213c5d49a393350c0e Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Mon, 20 Apr 2026 21:04:42 +0800 Subject: [PATCH 03/80] core/state: separate trie reader to mptReader and ubtReader (#34763) This PR separates the trie reader to mptTrieReader and ubtTrieReader for improved readability and extensibility. --------- Co-authored-by: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- core/state/database_mpt.go | 2 +- core/state/database_ubt.go | 2 +- core/state/reader.go | 195 ++++++++++++++++++++++--------------- 3 files changed, 116 insertions(+), 83 deletions(-) diff --git a/core/state/database_mpt.go b/core/state/database_mpt.go index bf9c7c9fff..4099ea495f 100644 --- a/core/state/database_mpt.go +++ b/core/state/database_mpt.go @@ -81,7 +81,7 @@ func (db *MPTDatabase) StateReader(stateRoot common.Hash) (StateReader, error) { } // Configure the trie reader, which is expected to be available as the // gatekeeper unless the state is corrupted. - tr, err := newTrieReader(stateRoot, db.triedb) + tr, err := newMPTTrieReader(stateRoot, db.triedb) if err != nil { return nil, err } diff --git a/core/state/database_ubt.go b/core/state/database_ubt.go index 39aa64508b..f854f2f46b 100644 --- a/core/state/database_ubt.go +++ b/core/state/database_ubt.go @@ -61,7 +61,7 @@ func (db *UBTDatabase) StateReader(stateRoot common.Hash) (StateReader, error) { } // Configure the trie reader, which is expected to be available as the // gatekeeper unless the state is corrupted. - tr, err := newTrieReader(stateRoot, db.triedb) + tr, err := newUBTTrieReader(stateRoot, db.triedb) if err != nil { return nil, err } diff --git a/core/state/reader.go b/core/state/reader.go index b837c6a0a1..5df0acbb9b 100644 --- a/core/state/reader.go +++ b/core/state/reader.go @@ -148,70 +148,28 @@ func (r *flatReader) Storage(addr common.Address, key common.Hash) (common.Hash, return value, nil } -// trieReader implements the StateReader interface, providing functions to access -// state from the referenced trie. +// mptTrieReader implements the StateReader interface, providing functions to +// access state from the referenced Merkle-Patricia-tree. // -// trieReader is safe for concurrent read. -type trieReader struct { - root common.Hash // State root which uniquely represent a state +// mptTrieReader is safe for concurrent read. +type mptTrieReader struct { + root common.Hash // State root which uniquely represents a state db *triedb.Database // Database for loading trie - // Main trie, resolved in constructor. Note either the Merkle-Patricia-tree - // or Verkle-tree is not safe for concurrent read. - mainTrie Trie - + mainTrie Trie // Main trie, resolved in constructor, not thread-safe subRoots map[common.Address]common.Hash // Set of storage roots, cached when the account is resolved subTries map[common.Address]Trie // Group of storage tries, cached when it's resolved lock sync.Mutex // Lock for protecting concurrent read } -// newTrieReader constructs a trie reader of the specific state. An error will be -// returned if the associated trie specified by root is not existent. -func newTrieReader(root common.Hash, db *triedb.Database) (*trieReader, error) { - var ( - tr Trie - err error - ) - if !db.IsUBT() { - tr, err = trie.NewStateTrie(trie.StateTrieID(root), db) - } else { - // When IsUBT() is true, create a BinaryTrie wrapped in TransitionTrie - binTrie, binErr := bintrie.NewBinaryTrie(root, db) - if binErr != nil { - return nil, binErr - } - - // Based on the transition status, determine if the overlay - // tree needs to be created, or if a single, target tree is - // to be picked. - ts := overlay.LoadTransitionState(db.Disk(), root, true) - if ts.InTransition() { - mpt, err := trie.NewStateTrie(trie.StateTrieID(ts.BaseRoot), db) - if err != nil { - return nil, err - } - tr = transitiontrie.NewTransitionTrie(mpt, binTrie, false) - } else { - // HACK: Use TransitionTrie with nil base as a wrapper to make BinaryTrie - // satisfy the Trie interface. This works around the import cycle between - // trie and trie/bintrie packages. - // - // TODO: In future PRs, refactor the package structure to avoid this hack: - // - Option 1: Move common interfaces (Trie, NodeIterator) to a separate - // package that both trie and trie/bintrie can import - // - Option 2: Create a factory function in the trie package that returns - // BinaryTrie as a Trie interface without direct import - // - Option 3: Move BinaryTrie to the main trie package - // - // The current approach works but adds unnecessary overhead and complexity - // by using TransitionTrie when there's no actual transition happening. - tr = transitiontrie.NewTransitionTrie(nil, binTrie, false) - } - } +// newMPTTrieReader constructs a Merkle-Patricia-tree reader of the specific state. +// An error will be returned if the associated trie specified by root is not existent. +func newMPTTrieReader(root common.Hash, db *triedb.Database) (*mptTrieReader, error) { + tr, err := trie.NewStateTrie(trie.StateTrieID(root), db) if err != nil { return nil, err } - return &trieReader{ + return &mptTrieReader{ root: root, db: db, mainTrie: tr, @@ -221,7 +179,7 @@ func newTrieReader(root common.Hash, db *triedb.Database) (*trieReader, error) { } // account is the inner version of Account and assumes the r.lock is already held. -func (r *trieReader) account(addr common.Address) (*types.StateAccount, error) { +func (r *mptTrieReader) account(addr common.Address) (*types.StateAccount, error) { account, err := r.mainTrie.GetAccount(addr) if err != nil { return nil, err @@ -236,9 +194,9 @@ func (r *trieReader) account(addr common.Address) (*types.StateAccount, error) { // Account implements StateReader, retrieving the account specified by the address. // -// An error will be returned if the trie state is corrupted. An nil account +// An error will be returned if the trie state is corrupted. A nil account // will be returned if it's not existent in the trie. -func (r *trieReader) Account(addr common.Address) (*types.StateAccount, error) { +func (r *mptTrieReader) Account(addr common.Address) (*types.StateAccount, error) { r.lock.Lock() defer r.lock.Unlock() @@ -250,43 +208,118 @@ func (r *trieReader) Account(addr common.Address) (*types.StateAccount, error) { // // An error will be returned if the trie state is corrupted. An empty storage // slot will be returned if it's not existent in the trie. -func (r *trieReader) Storage(addr common.Address, key common.Hash) (common.Hash, error) { +func (r *mptTrieReader) Storage(addr common.Address, key common.Hash) (common.Hash, error) { r.lock.Lock() defer r.lock.Unlock() - var ( - tr Trie - found bool - value common.Hash - ) - if r.db.IsUBT() { - tr = r.mainTrie - } else { - tr, found = r.subTries[addr] - if !found { - root, ok := r.subRoots[addr] - - // The storage slot is accessed without account caching. It's unexpected - // behavior but try to resolve the account first anyway. - if !ok { - _, err := r.account(addr) - if err != nil { - return common.Hash{}, err - } - root = r.subRoots[addr] - } - var err error - tr, err = trie.NewStateTrie(trie.StorageTrieID(r.root, crypto.Keccak256Hash(addr.Bytes()), root), r.db) + tr, found := r.subTries[addr] + if !found { + root, ok := r.subRoots[addr] + + // The storage slot is accessed without account caching. It's unexpected + // behavior but try to resolve the account first anyway. + if !ok { + _, err := r.account(addr) if err != nil { return common.Hash{}, err } - r.subTries[addr] = tr + root = r.subRoots[addr] + } + var err error + tr, err = trie.NewStateTrie(trie.StorageTrieID(r.root, crypto.Keccak256Hash(addr.Bytes()), root), r.db) + if err != nil { + return common.Hash{}, err } + r.subTries[addr] = tr } ret, err := tr.GetStorage(addr, key.Bytes()) if err != nil { return common.Hash{}, err } + var value common.Hash + value.SetBytes(ret) + return value, nil +} + +// ubtTrieReader implements the StateReader interface, providing functions to access +// state from the referenced Unified-binary-trie. +// +// ubtTrieReader is safe for concurrent read. +type ubtTrieReader struct { + root common.Hash // State root which uniquely represents a state + db *triedb.Database // Database for loading trie + tr Trie // Referenced unified binary trie + lock sync.Mutex // Lock for protecting concurrent read +} + +// newUBTTrieReader constructs a Unified-binary-trie reader of the specific state. +// An error will be returned if the associated trie specified by root is not existent. +func newUBTTrieReader(root common.Hash, db *triedb.Database) (*ubtTrieReader, error) { + binTrie, binErr := bintrie.NewBinaryTrie(root, db) + if binErr != nil { + return nil, binErr + } + // Based on the transition status, determine if the overlay + // tree needs to be created, or if a single, target tree is + // to be picked. + var ( + tr Trie + ts = overlay.LoadTransitionState(db.Disk(), root, true) + ) + if ts.InTransition() { + mpt, err := trie.NewStateTrie(trie.StateTrieID(ts.BaseRoot), db) + if err != nil { + return nil, err + } + tr = transitiontrie.NewTransitionTrie(mpt, binTrie, false) + } else { + // HACK: Use TransitionTrie with nil base as a wrapper to make BinaryTrie + // satisfy the Trie interface. This works around the import cycle between + // trie and trie/bintrie packages. + // + // TODO: In future PRs, refactor the package structure to avoid this hack: + // - Option 1: Move common interfaces (Trie, NodeIterator) to a separate + // package that both trie and trie/bintrie can import + // - Option 2: Create a factory function in the trie package that returns + // BinaryTrie as a Trie interface without direct import + // - Option 3: Move BinaryTrie to the main trie package + // + // The current approach works but adds unnecessary overhead and complexity + // by using TransitionTrie when there's no actual transition happening. + tr = transitiontrie.NewTransitionTrie(nil, binTrie, false) + } + return &ubtTrieReader{ + root: root, + db: db, + tr: tr, + }, nil +} + +// Account implements StateReader, retrieving the account specified by the address. +// +// An error will be returned if the trie state is corrupted. A nil account +// will be returned if it's not existent in the trie. +func (r *ubtTrieReader) Account(addr common.Address) (*types.StateAccount, error) { + r.lock.Lock() + defer r.lock.Unlock() + + return r.tr.GetAccount(addr) +} + +// Storage implements StateReader, retrieving the storage slot specified by the +// address and slot key. +// +// An error will be returned if the trie state is corrupted. An empty storage +// slot will be returned if it's not existent in the trie. +func (r *ubtTrieReader) Storage(addr common.Address, key common.Hash) (common.Hash, error) { + r.lock.Lock() + defer r.lock.Unlock() + + ret, err := r.tr.GetStorage(addr, key.Bytes()) + if err != nil { + return common.Hash{}, err + } + var value common.Hash value.SetBytes(ret) return value, nil } From acbf699c33254b3517683c2098cf5e956920f2f7 Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Mon, 20 Apr 2026 23:12:10 +0800 Subject: [PATCH 04/80] core/state: export StateUpdate struct (#34724) In the recent refactoring, the state commit logic has been abstracted, making it more flexible to design state databases for various use cases. For example, execution-only modes where state mutation is disabled. As part of this change, the database interface was extended with a Commit function. However, it currently accepts an unexported struct `stateUpdate`, which prevents downstream projects from customizing the state commit behavior. To address this limitation, the stateUpdate type is now exported. --- core/state/database.go | 2 +- core/state/database_history.go | 2 +- core/state/database_iterator.go | 68 +++-- core/state/database_iterator_test.go | 21 +- core/state/database_mpt.go | 33 ++- core/state/database_ubt.go | 23 +- core/state/dump.go | 37 +-- core/state/state_object.go | 62 ++-- core/state/state_sizer.go | 59 ++-- core/state/state_sizer_test.go | 4 +- core/state/statedb.go | 55 ++-- core/state/statedb_fuzz_test.go | 15 +- core/state/stateupdate.go | 417 +++++++++++++++------------ 13 files changed, 444 insertions(+), 354 deletions(-) diff --git a/core/state/database.go b/core/state/database.go index b9ccff658f..3b1e627f28 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -70,7 +70,7 @@ type Database interface { // Commit flushes all pending writes and finalizes the state transition, // committing the changes to the underlying storage. It returns an error // if the commit fails. - Commit(update *stateUpdate) error + Commit(update *StateUpdate) error } // Trie is a Ethereum Merkle Patricia trie. diff --git a/core/state/database_history.go b/core/state/database_history.go index d1ed2fe194..fbf4ab5f9c 100644 --- a/core/state/database_history.go +++ b/core/state/database_history.go @@ -297,7 +297,7 @@ func (db *HistoricDB) TrieDB() *triedb.Database { // Commit flushes all pending writes and finalizes the state transition, // committing the changes to the underlying storage. It returns an error // if the commit fails. -func (db *HistoricDB) Commit(update *stateUpdate) error { +func (db *HistoricDB) Commit(update *StateUpdate) error { return errors.New("not implemented") } diff --git a/core/state/database_iterator.go b/core/state/database_iterator.go index 8fad66a1e8..1ff164b002 100644 --- a/core/state/database_iterator.go +++ b/core/state/database_iterator.go @@ -23,6 +23,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/state/snapshot" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/triedb" ) @@ -57,9 +58,9 @@ type AccountIterator interface { // An error will be returned if the preimage is not available. Address() (common.Address, error) - // Account returns the RLP encoded account the iterator is currently at. + // Account returns the account the iterator is currently at. // An error will be retained if the iterator becomes invalid. - Account() []byte + Account() *types.StateAccount } // StorageIterator is an iterator to step over the specific storage in the @@ -73,7 +74,7 @@ type StorageIterator interface { // Slot returns the storage slot the iterator is currently at. An error will // be retained if the iterator becomes invalid. - Slot() []byte + Slot() common.Hash } // Iteratee wraps the NewIterator methods for traversing the accounts and @@ -131,10 +132,7 @@ func (ai *flatAccountIterator) Next() bool { // Error returns any failure that occurred during iteration, which might have // caused a premature iteration exit. func (ai *flatAccountIterator) Error() error { - if ai.err != nil { - return ai.err - } - return ai.it.Error() + return errors.Join(ai.err, ai.it.Error()) } // Hash returns the hash of the account or storage slot the iterator is @@ -165,8 +163,8 @@ func (ai *flatAccountIterator) Address() (common.Address, error) { // Account returns the account data the iterator is currently at. The account // data is encoded as slim format from the underlying iterator, the conversion // is required. -func (ai *flatAccountIterator) Account() []byte { - data, err := types.FullAccountRLP(ai.it.Account()) +func (ai *flatAccountIterator) Account() *types.StateAccount { + data, err := types.FullAccount(ai.it.Account()) if err != nil { ai.err = err return nil @@ -176,6 +174,7 @@ func (ai *flatAccountIterator) Account() []byte { // flatStorageIterator is a wrapper around the underlying flat state iterator. type flatStorageIterator struct { + err error it snapshot.StorageIterator preimage PreimageReader } @@ -190,13 +189,16 @@ func newFlatStorageIterator(it snapshot.StorageIterator, preimage PreimageReader // is exhausted or if an error occurs. Any error encountered is retained and // can be retrieved via Error(). func (si *flatStorageIterator) Next() bool { + if si.err != nil { + return false + } return si.it.Next() } // Error returns any failure that occurred during iteration, which might have // caused a premature iteration exit. func (si *flatStorageIterator) Error() error { - return si.it.Error() + return errors.Join(si.err, si.it.Error()) } // Hash returns the hash of the account or storage slot the iterator is @@ -225,14 +227,24 @@ func (si *flatStorageIterator) Key() (common.Hash, error) { } // Slot returns the storage slot data the iterator is currently at. -func (si *flatStorageIterator) Slot() []byte { - return si.it.Slot() +func (si *flatStorageIterator) Slot() common.Hash { + // Perform the rlp-decode as the slot value is RLP-encoded + blob := si.it.Slot() + _, content, _, err := rlp.Split(blob) + if err != nil { + si.err = err + return common.Hash{} + } + var value common.Hash + value.SetBytes(content) + return value } // merkleIterator implements the Iterator interface, providing functions to traverse // the accounts or storages with the manner of Merkle-Patricia-Trie. type merkleIterator struct { tr Trie + err error it *trie.Iterator account bool } @@ -254,13 +266,16 @@ func newMerkleTrieIterator(tr Trie, start common.Hash, account bool) (*merkleIte // is exhausted or if an error occurs. Any error encountered is retained and // can be retrieved via Error(). func (ti *merkleIterator) Next() bool { + if ti.err != nil { + return false + } return ti.it.Next() } // Error returns any failure that occurred during iteration, which might have // caused a premature iteration exit. func (ti *merkleIterator) Error() error { - return ti.it.Err + return errors.Join(ti.err, ti.it.Err) } // Hash returns the hash of the account or storage slot the iterator is @@ -287,11 +302,16 @@ func (ti *merkleIterator) Address() (common.Address, error) { } // Account returns the account data the iterator is currently at. -func (ti *merkleIterator) Account() []byte { +func (ti *merkleIterator) Account() *types.StateAccount { if !ti.account { return nil } - return ti.it.Value + var account types.StateAccount + if err := rlp.DecodeBytes(ti.it.Value, &account); err != nil { + ti.err = err + return nil + } + return &account } // Key returns the raw storage slot key the iterator is currently at. @@ -308,11 +328,19 @@ func (ti *merkleIterator) Key() (common.Hash, error) { } // Slot returns the storage slot the iterator is currently at. -func (ti *merkleIterator) Slot() []byte { +func (ti *merkleIterator) Slot() common.Hash { if ti.account { - return nil + return common.Hash{} } - return ti.it.Value + // Perform the rlp-decode as the slot value is RLP-encoded + _, content, _, err := rlp.Split(ti.it.Value) + if err != nil { + ti.err = err + return common.Hash{} + } + var value common.Hash + value.SetBytes(content) + return value } // stateIteratee implements Iteratee interface, providing the state traversal @@ -430,6 +458,6 @@ func (e exhaustedIterator) Key() (common.Hash, error) { return common.Hash{}, nil } -func (e exhaustedIterator) Slot() []byte { - return nil +func (e exhaustedIterator) Slot() common.Hash { + return common.Hash{} } diff --git a/core/state/database_iterator_test.go b/core/state/database_iterator_test.go index 87819e5526..8313f86403 100644 --- a/core/state/database_iterator_test.go +++ b/core/state/database_iterator_test.go @@ -24,7 +24,6 @@ import ( "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" ) @@ -45,7 +44,7 @@ func TestExhaustedIterator(t *testing.T) { if key, err := it.Key(); key != (common.Hash{}) || err != nil { t.Fatalf("Key() = %x, %v; want zero, nil", key, err) } - if slot := it.Slot(); slot != nil { + if slot := it.Slot(); slot != (common.Hash{}) { t.Fatalf("Slot() = %x, want nil", slot) } it.Release() @@ -95,20 +94,16 @@ func testAccountIterator(t *testing.T, scheme string) { hashes = append(hashes, hash) // Decode and verify account data. - blob := acctIt.Account() - if blob == nil { + got := acctIt.Account() + if got == nil { t.Fatalf("(%s) nil account at %x", scheme, hash) } - var decoded types.StateAccount - if err := rlp.DecodeBytes(blob, &decoded); err != nil { - t.Fatalf("(%s) bad RLP at %x: %v", scheme, hash, err) - } acc := addrByHash[hash] - if decoded.Nonce != acc.nonce { - t.Fatalf("(%s) nonce %x: got %d, want %d", scheme, hash, decoded.Nonce, acc.nonce) + if got.Nonce != acc.nonce { + t.Fatalf("(%s) nonce %x: got %d, want %d", scheme, hash, got.Nonce, acc.nonce) } - if decoded.Balance.Cmp(acc.balance) != 0 { - t.Fatalf("(%s) balance %x: got %v, want %v", scheme, hash, decoded.Balance, acc.balance) + if got.Balance.Cmp(acc.balance) != 0 { + t.Fatalf("(%s) balance %x: got %v, want %v", scheme, hash, got.Balance, acc.balance) } // Verify address preimage resolution. addr, err := acctIt.Address() @@ -183,7 +178,7 @@ func testStorageIterator(t *testing.T, scheme string) { t.Fatalf("(%s) storage hashes not ascending for %x", scheme, acc.address) } prevHash = hash - if storageIt.Slot() == nil { + if storageIt.Slot() == (common.Hash{}) { t.Fatalf("(%s) nil slot at %x", scheme, hash) } // Check key preimage resolution on first slot. diff --git a/core/state/database_mpt.go b/core/state/database_mpt.go index 4099ea495f..42c5f2e5ef 100644 --- a/core/state/database_mpt.go +++ b/core/state/database_mpt.go @@ -140,35 +140,44 @@ func (db *MPTDatabase) TrieDB() *triedb.Database { // Commit flushes all pending writes and finalizes the state transition, // committing the changes to the underlying storage. It returns an error // if the commit fails. -func (db *MPTDatabase) Commit(update *stateUpdate) error { +func (db *MPTDatabase) Commit(update *StateUpdate) error { // Short circuit if nothing to commit - if update.empty() { + if update.Empty() { return nil } // Commit dirty contract code if any exists - if len(update.codes) > 0 { - batch := db.codedb.NewBatchWithSize(len(update.codes)) - for _, code := range update.codes { - batch.Put(code.hash, code.blob) + if len(update.Codes) > 0 { + batch := db.codedb.NewBatchWithSize(len(update.Codes)) + for _, code := range update.Codes { + batch.Put(code.Hash, code.Blob) } if err := batch.Commit(); err != nil { return err } } + // Encode the state mutations in the MPT format + accounts, accountOrigin, storages, storageOrigin := update.EncodeMPTState() + // If snapshotting is enabled, update the snapshot tree with this new version - if db.snap != nil && db.snap.Snapshot(update.originRoot) != nil { - if err := db.snap.Update(update.root, update.originRoot, update.accounts, update.storages); err != nil { - log.Warn("Failed to update snapshot tree", "from", update.originRoot, "to", update.root, "err", err) + if db.snap != nil && db.snap.Snapshot(update.OriginRoot) != nil { + if err := db.snap.Update(update.Root, update.OriginRoot, accounts, storages); err != nil { + log.Warn("Failed to update snapshot tree", "from", update.OriginRoot, "to", update.Root, "err", err) } // Keep 128 diff layers in the memory, persistent layer is 129th. // - head layer is paired with HEAD state // - head-1 layer is paired with HEAD-1 state // - head-127 layer(bottom-most diff layer) is paired with HEAD-127 state - if err := db.snap.Cap(update.root, TriesInMemory); err != nil { - log.Warn("Failed to cap snapshot tree", "root", update.root, "layers", TriesInMemory, "err", err) + if err := db.snap.Cap(update.Root, TriesInMemory); err != nil { + log.Warn("Failed to cap snapshot tree", "root", update.Root, "layers", TriesInMemory, "err", err) } } - return db.triedb.Update(update.root, update.originRoot, update.blockNumber, update.nodes, update.stateSet()) + return db.triedb.Update(update.Root, update.OriginRoot, update.BlockNumber, update.Nodes, &triedb.StateSet{ + Accounts: accounts, + AccountsOrigin: accountOrigin, + Storages: storages, + StoragesOrigin: storageOrigin, + RawStorageKey: update.StorageKeyType == StorageKeyPlain, + }) } // Iteratee returns a state iteratee associated with the specified state root, diff --git a/core/state/database_ubt.go b/core/state/database_ubt.go index f854f2f46b..718d93df87 100644 --- a/core/state/database_ubt.go +++ b/core/state/database_ubt.go @@ -113,22 +113,31 @@ func (db *UBTDatabase) TrieDB() *triedb.Database { // Commit flushes all pending writes and finalizes the state transition, // committing the changes to the underlying storage. It returns an error // if the commit fails. -func (db *UBTDatabase) Commit(update *stateUpdate) error { +func (db *UBTDatabase) Commit(update *StateUpdate) error { // Short circuit if nothing to commit - if update.empty() { + if update.Empty() { return nil } // Commit dirty contract code if any exists - if len(update.codes) > 0 { - batch := db.codedb.NewBatchWithSize(len(update.codes)) - for _, code := range update.codes { - batch.Put(code.hash, code.blob) + if len(update.Codes) > 0 { + batch := db.codedb.NewBatchWithSize(len(update.Codes)) + for _, code := range update.Codes { + batch.Put(code.Hash, code.Blob) } if err := batch.Commit(); err != nil { return err } } - return db.triedb.Update(update.root, update.originRoot, update.blockNumber, update.nodes, update.stateSet()) + // Encode the state mutations in the UBT format + accounts, accountOrigin, storages, storageOrigin := update.EncodeUBTState() + + return db.triedb.Update(update.Root, update.OriginRoot, update.BlockNumber, update.Nodes, &triedb.StateSet{ + Accounts: accounts, + AccountsOrigin: accountOrigin, + Storages: storages, + StoragesOrigin: storageOrigin, + RawStorageKey: update.StorageKeyType == StorageKeyPlain, + }) } // Iteratee returns a state iteratee associated with the specified state root, diff --git a/core/state/dump.go b/core/state/dump.go index 71138143d9..cbf53de053 100644 --- a/core/state/dump.go +++ b/core/state/dump.go @@ -24,9 +24,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" - "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/log" - "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie/bintrie" ) @@ -115,7 +113,7 @@ func (d iterativeDump) OnRoot(root common.Hash) { // DumpToCollector iterates the state according to the given options and inserts // the items into a collector for aggregation or serialization. -func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey []byte) { +func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey []byte, err error) { // Sanitize the input to allow nil configs if conf == nil { conf = new(DumpConfig) @@ -131,7 +129,7 @@ func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey [] iteratee, err := s.db.Iteratee(s.originalRoot) if err != nil { - return nil + return nil, err } var startHash common.Hash if conf.Start != nil { @@ -139,14 +137,17 @@ func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey [] } acctIt, err := iteratee.NewAccountIterator(startHash) if err != nil { - return nil + return nil, err } defer acctIt.Release() for acctIt.Next() { - var data types.StateAccount - if err := rlp.DecodeBytes(acctIt.Account(), &data); err != nil { - panic(err) + data := acctIt.Account() + if err := acctIt.Error(); err != nil { + return nil, err + } + if data == nil { + return nil, fmt.Errorf("unexpected nil account value") } var ( account = DumpAccount{ @@ -168,7 +169,7 @@ func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey [] address = &addrBytes account.Address = address } - obj := newObject(s, addrBytes, &data) + obj := newObject(s, addrBytes, data) if !conf.SkipCode { account.Code = obj.Code() } @@ -177,20 +178,19 @@ func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey [] storageIt, err := iteratee.NewStorageIterator(acctIt.Hash(), common.Hash{}) if err != nil { - log.Error("Failed to load storage trie", "err", err) - continue + return nil, err } for storageIt.Next() { - _, content, _, err := rlp.Split(storageIt.Slot()) - if err != nil { - log.Error("Failed to decode the value returned by iterator", "error", err) - continue + val := storageIt.Slot() + if err := storageIt.Error(); err != nil { + storageIt.Release() + return nil, err } key, err := storageIt.Key() if err != nil { continue } - account.Storage[key] = common.Bytes2Hex(content) + account.Storage[key] = common.Bytes2Hex(common.TrimLeftZeroes(val[:])) } storageIt.Release() } @@ -211,7 +211,7 @@ func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey [] log.Warn("Dump incomplete due to missing preimages", "missing", missingPreimages) } log.Info("Trie dumping complete", "accounts", accounts, "elapsed", common.PrettyDuration(time.Since(start))) - return nextKey + return nextKey, nil } // DumpBinTrieLeaves collects all binary trie leaf nodes into the provided map. @@ -242,7 +242,8 @@ func (s *StateDB) RawDump(opts *DumpConfig) Dump { dump := &Dump{ Accounts: make(map[string]DumpAccount), } - dump.Next = s.DumpToCollector(dump, opts) + next, _ := s.DumpToCollector(dump, opts) + dump.Next = next return *dump } diff --git a/core/state/state_object.go b/core/state/state_object.go index a812359368..df54733d63 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -27,7 +27,6 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/log" - "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie/bintrie" "github.com/ethereum/go-ethereum/trie/transitiontrie" @@ -398,17 +397,8 @@ func (s *stateObject) updateRoot() { } // commitStorage overwrites the clean storage with the storage changes and -// fulfills the storage diffs into the given accountUpdate struct. -func (s *stateObject) commitStorage(op *accountUpdate) { - var ( - encode = func(val common.Hash) []byte { - if val == (common.Hash{}) { - return nil - } - blob, _ := rlp.EncodeToBytes(common.TrimLeftZeroes(val[:])) - return blob - } - ) +// fulfills the storage diffs into the given AccountUpdate struct. +func (s *stateObject) commitStorage(op *AccountUpdate) { for key, val := range s.pendingStorage { // Skip the noop storage changes, it might be possible the value // of tracked slot is same in originStorage and pendingStorage @@ -418,20 +408,20 @@ func (s *stateObject) commitStorage(op *accountUpdate) { continue } hash := crypto.Keccak256Hash(key[:]) - if op.storages == nil { - op.storages = make(map[common.Hash][]byte) + if op.Storages == nil { + op.Storages = make(map[common.Hash]common.Hash) } - op.storages[hash] = encode(val) + op.Storages[hash] = val - if op.storagesOriginByKey == nil { - op.storagesOriginByKey = make(map[common.Hash][]byte) + if op.StoragesOriginByKey == nil { + op.StoragesOriginByKey = make(map[common.Hash]common.Hash) } - if op.storagesOriginByHash == nil { - op.storagesOriginByHash = make(map[common.Hash][]byte) + if op.StoragesOriginByHash == nil { + op.StoragesOriginByHash = make(map[common.Hash]common.Hash) } - origin := encode(s.originStorage[key]) - op.storagesOriginByKey[key] = origin - op.storagesOriginByHash[hash] = origin + origin := s.originStorage[key] + op.StoragesOriginByKey[key] = origin + op.StoragesOriginByHash[hash] = origin // Overwrite the clean value of storage slots s.originStorage[key] = val @@ -444,32 +434,32 @@ func (s *stateObject) commitStorage(op *accountUpdate) { // // Note, commit may run concurrently across all the state objects. Do not assume // thread-safe access to the statedb. -func (s *stateObject) commit() (*accountUpdate, *trienode.NodeSet, error) { - // commit the account metadata changes - op := &accountUpdate{ - address: s.address, - data: types.SlimAccountRLP(s.data), - } - if s.origin != nil { - op.origin = types.SlimAccountRLP(*s.origin) +func (s *stateObject) commit() (*AccountUpdate, *trienode.NodeSet, error) { + // commit the account metadata changes, the data must be deep-copied + // to prevent accidental mutations later on (in practice the stateDB + // won't be modified after commit). The origin is safe to use directly. + op := &AccountUpdate{ + Address: s.address, + Data: s.data.Copy(), + Origin: s.origin, } // commit the contract code if it's modified if s.dirtyCode { - op.code = &contractCode{ - hash: common.BytesToHash(s.CodeHash()), - blob: s.code, + op.Code = &ContractCode{ + Hash: common.BytesToHash(s.CodeHash()), + Blob: s.code, } s.dirtyCode = false // reset the dirty flag if s.origin == nil { - op.code.originHash = types.EmptyCodeHash + op.Code.OriginHash = types.EmptyCodeHash } else { - op.code.originHash = common.BytesToHash(s.origin.CodeHash) + op.Code.OriginHash = common.BytesToHash(s.origin.CodeHash) } } // Commit storage changes and the associated storage trie s.commitStorage(op) - if len(op.storages) == 0 { + if len(op.Storages) == 0 { // nothing changed, don't bother to commit the trie s.origin = s.data.Copy() return op, nil, nil diff --git a/core/state/state_sizer.go b/core/state/state_sizer.go index 02b73e5575..3293d7e950 100644 --- a/core/state/state_sizer.go +++ b/core/state/state_sizer.go @@ -125,16 +125,17 @@ func (s SizeStats) add(diff SizeStats) SizeStats { } // calSizeStats measures the state size changes of the provided state update. -func calSizeStats(update *stateUpdate) (SizeStats, error) { +func calSizeStats(update *StateUpdate) (SizeStats, error) { stats := SizeStats{ - BlockNumber: update.blockNumber, - StateRoot: update.root, + BlockNumber: update.BlockNumber, + StateRoot: update.Root, } + accounts, accountOrigin, storages, storageOrigin := update.EncodeMPTState() // Measure the account changes - for addr, oldValue := range update.accountsOrigin { + for addr, oldValue := range accountOrigin { addrHash := crypto.Keccak256Hash(addr.Bytes()) - newValue, exists := update.accounts[addrHash] + newValue, exists := accounts[addrHash] if !exists { return SizeStats{}, fmt.Errorf("account %x not found", addr) } @@ -156,9 +157,9 @@ func calSizeStats(update *stateUpdate) (SizeStats, error) { } // Measure storage changes - for addr, slots := range update.storagesOrigin { + for addr, slots := range storageOrigin { addrHash := crypto.Keccak256Hash(addr.Bytes()) - subset, exists := update.storages[addrHash] + subset, exists := storages[addrHash] if !exists { return SizeStats{}, fmt.Errorf("storage %x not found", addr) } @@ -167,7 +168,7 @@ func calSizeStats(update *stateUpdate) (SizeStats, error) { exists bool newValue []byte ) - if update.rawStorageKey { + if update.StorageKeyType == StorageKeyPlain { newValue, exists = subset[crypto.Keccak256Hash(key.Bytes())] } else { newValue, exists = subset[key] @@ -194,7 +195,7 @@ func calSizeStats(update *stateUpdate) (SizeStats, error) { } // Measure trienode changes - for owner, subset := range update.nodes.Sets { + for owner, subset := range update.Nodes.Sets { var ( keyPrefix int64 isAccount = owner == (common.Hash{}) @@ -244,13 +245,13 @@ func calSizeStats(update *stateUpdate) (SizeStats, error) { } codeExists := make(map[common.Hash]struct{}) - for _, code := range update.codes { - if _, ok := codeExists[code.hash]; ok || code.duplicate { + for _, code := range update.Codes { + if _, ok := codeExists[code.Hash]; ok || code.Duplicate { continue } stats.ContractCodes += 1 - stats.ContractCodeBytes += codeKeySize + int64(len(code.blob)) - codeExists[code.hash] = struct{}{} + stats.ContractCodeBytes += codeKeySize + int64(len(code.Blob)) + codeExists[code.Hash] = struct{}{} } return stats, nil } @@ -267,7 +268,7 @@ type SizeTracker struct { triedb *triedb.Database abort chan struct{} aborted chan struct{} - updateCh chan *stateUpdate + updateCh chan *StateUpdate queryCh chan *stateSizeQuery } @@ -281,7 +282,7 @@ func NewSizeTracker(db ethdb.KeyValueStore, triedb *triedb.Database) (*SizeTrack triedb: triedb, abort: make(chan struct{}), aborted: make(chan struct{}), - updateCh: make(chan *stateUpdate), + updateCh: make(chan *StateUpdate), queryCh: make(chan *stateSizeQuery), } go t.run() @@ -328,9 +329,9 @@ func (t *SizeTracker) run() { for { select { case u := <-t.updateCh: - base, found := stats[u.originRoot] + base, found := stats[u.OriginRoot] if !found { - log.Debug("Ignored the state size without parent", "parent", u.originRoot, "root", u.root, "number", u.blockNumber) + log.Debug("Ignored the state size without parent", "parent", u.OriginRoot, "root", u.Root, "number", u.BlockNumber) continue } diff, err := calSizeStats(u) @@ -338,15 +339,15 @@ func (t *SizeTracker) run() { continue } stat := base.add(diff) - stats[u.root] = stat - last = u.root + stats[u.Root] = stat + last = u.Root // Publish statistics to metric system stat.publish() // Evict the stale statistics - heap.Push(&h, stats[u.root]) - for len(h) > 0 && u.blockNumber-h[0].BlockNumber > statEvictThreshold { + heap.Push(&h, stats[u.Root]) + for len(h) > 0 && u.BlockNumber-h[0].BlockNumber > statEvictThreshold { delete(stats, h[0].StateRoot) heap.Pop(&h) } @@ -402,7 +403,7 @@ wait: } var ( - updates = make(map[common.Hash]*stateUpdate) + updates = make(map[common.Hash]*StateUpdate) children = make(map[common.Hash][]common.Hash) done chan buildResult ) @@ -410,9 +411,9 @@ wait: for { select { case u := <-t.updateCh: - updates[u.root] = u - children[u.originRoot] = append(children[u.originRoot], u.root) - log.Debug("Received state update", "root", u.root, "blockNumber", u.blockNumber) + updates[u.Root] = u + children[u.OriginRoot] = append(children[u.OriginRoot], u.Root) + log.Debug("Received state update", "root", u.Root, "blockNumber", u.BlockNumber) case r := <-t.queryCh: r.err = errors.New("state size is not initialized yet") @@ -432,8 +433,8 @@ wait: continue } done = make(chan buildResult) - go t.build(entry.root, entry.blockNumber, done) - log.Info("Measuring persistent state size", "root", root.Hex(), "number", entry.blockNumber) + go t.build(entry.Root, entry.BlockNumber, done) + log.Info("Measuring persistent state size", "root", root.Hex(), "number", entry.BlockNumber) case result := <-done: if result.err != nil { @@ -646,8 +647,8 @@ func (t *SizeTracker) iterateTableParallel(closed chan struct{}, prefix []byte, // Notify is an async method used to send the state update to the size tracker. // It ignores empty updates (where no state changes occurred). // If the channel is full, it drops the update to avoid blocking. -func (t *SizeTracker) Notify(update *stateUpdate) { - if update == nil || update.empty() { +func (t *SizeTracker) Notify(update *StateUpdate) { + if update == nil || update.Empty() { return } select { diff --git a/core/state/state_sizer_test.go b/core/state/state_sizer_test.go index b3203afd74..539f160985 100644 --- a/core/state/state_sizer_test.go +++ b/core/state/state_sizer_test.go @@ -160,7 +160,7 @@ func TestSizeTracker(t *testing.T) { } tracker.Notify(ret) - if err := tdb.Commit(ret.root, false); err != nil { + if err := tdb.Commit(ret.Root, false); err != nil { t.Fatalf("Failed to commit trie at block %d: %v", blockNum, err) } @@ -169,7 +169,7 @@ func TestSizeTracker(t *testing.T) { t.Fatalf("Failed to calculate size stats for block %d: %v", blockNum, err) } trackedUpdates = append(trackedUpdates, diff) - currentRoot = ret.root + currentRoot = ret.Root } finalRoot := rawdb.ReadSnapshotRoot(db) diff --git a/core/state/statedb.go b/core/state/statedb.go index 956871472d..8c57edf08e 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -1045,11 +1045,11 @@ func (s *StateDB) clearJournalAndRefund() { } // deleteStorage is designed to delete the storage trie of a designated account. -func (s *StateDB) deleteStorage(addrHash common.Hash, root common.Hash) (map[common.Hash][]byte, map[common.Hash][]byte, *trienode.NodeSet, error) { +func (s *StateDB) deleteStorage(addrHash common.Hash, root common.Hash) (map[common.Hash]common.Hash, map[common.Hash]common.Hash, *trienode.NodeSet, error) { var ( - nodes = trienode.NewNodeSet(addrHash) // the set for trie node mutations (value is nil) - storages = make(map[common.Hash][]byte) // the set for storage mutations (value is nil) - storageOrigins = make(map[common.Hash][]byte) // the set for tracking the original value of slot + nodes = trienode.NewNodeSet(addrHash) // the set for trie node mutations (value is nil) + storages = make(map[common.Hash]common.Hash) // the set for storage mutations (value is nil) + storageOrigins = make(map[common.Hash]common.Hash) // the set for tracking the original value of slot ) iteratee, err := s.db.Iteratee(s.originalRoot) if err != nil { @@ -1065,19 +1065,24 @@ func (s *StateDB) deleteStorage(addrHash common.Hash, root common.Hash) (map[com nodes.AddNode(path, trienode.NewDeletedWithPrev(blob)) }) for it.Next() { - slot := common.CopyBytes(it.Slot()) - if err := it.Error(); err != nil { // error might occur after Slot function + slot := it.Slot() + // Error might occur after Slot function + if err := it.Error(); err != nil { return nil, nil, nil, err } + if slot == (common.Hash{}) { + return nil, nil, nil, fmt.Errorf("unexpected empty storage slot, addr: %x, slot: %x", addrHash, it.Hash()) + } key := it.Hash() - storages[key] = nil + storages[key] = common.Hash{} storageOrigins[key] = slot - if err := stack.Update(key.Bytes(), slot); err != nil { + if err := stack.Update(key.Bytes(), encodeSlot(slot)); err != nil { return nil, nil, nil, err } } - if err := it.Error(); err != nil { // error might occur during iteration + // Error might occur during iteration + if err := it.Error(); err != nil { return nil, nil, nil, err } if stack.Hash() != root { @@ -1104,10 +1109,10 @@ func (s *StateDB) deleteStorage(addrHash common.Hash, root common.Hash) (map[com // with their values be tracked as original value. // In case (d), **original** account along with its storages should be deleted, // with their values be tracked as original value. -func (s *StateDB) handleDestruction(noStorageWiping bool) (map[common.Hash]*accountDelete, []*trienode.NodeSet, error) { +func (s *StateDB) handleDestruction(noStorageWiping bool) (map[common.Hash]*AccountDelete, []*trienode.NodeSet, error) { var ( nodes []*trienode.NodeSet - deletes = make(map[common.Hash]*accountDelete) + deletes = make(map[common.Hash]*AccountDelete) ) for addr, prevObj := range s.stateObjectsDestruct { prev := prevObj.origin @@ -1122,9 +1127,9 @@ func (s *StateDB) handleDestruction(noStorageWiping bool) (map[common.Hash]*acco } // The account was existent, it can be either case (c) or (d). addrHash := crypto.Keccak256Hash(addr.Bytes()) - op := &accountDelete{ - address: addr, - origin: types.SlimAccountRLP(*prev), + op := &AccountDelete{ + Address: addr, + Origin: prev, } deletes[addrHash] = op @@ -1140,8 +1145,8 @@ func (s *StateDB) handleDestruction(noStorageWiping bool) (map[common.Hash]*acco if err != nil { return nil, nil, fmt.Errorf("failed to delete storage, err: %w", err) } - op.storages = storages - op.storagesOrigin = storagesOrigin + op.Storages = storages + op.StoragesOrigin = storagesOrigin // Aggregate the associated trie node changes. nodes = append(nodes, set) @@ -1156,7 +1161,7 @@ func (s *StateDB) GetTrie() Trie { // commit gathers the state mutations accumulated along with the associated // trie changes, resetting all internal flags with the new state as the base. -func (s *StateDB) commit(deleteEmptyObjects bool, noStorageWiping bool, blockNumber uint64) (*stateUpdate, error) { +func (s *StateDB) commit(deleteEmptyObjects bool, noStorageWiping bool, blockNumber uint64) (*StateUpdate, error) { // Short circuit in case any database failure occurred earlier. if s.dbErr != nil { return nil, fmt.Errorf("commit aborted due to earlier error: %v", s.dbErr) @@ -1177,7 +1182,7 @@ func (s *StateDB) commit(deleteEmptyObjects bool, noStorageWiping bool, blockNum lock sync.Mutex // protect two maps below nodes = trienode.NewMergedNodeSet() // aggregated trie nodes - updates = make(map[common.Hash]*accountUpdate, len(s.mutations)) // aggregated account updates + updates = make(map[common.Hash]*AccountUpdate, len(s.mutations)) // aggregated account updates // merge aggregates the dirty trie nodes into the global set. // @@ -1305,12 +1310,16 @@ func (s *StateDB) commit(deleteEmptyObjects bool, noStorageWiping bool, blockNum origin := s.originalRoot s.originalRoot = root - return newStateUpdate(noStorageWiping, origin, root, blockNumber, deletes, updates, nodes), nil + typ := StorageKeyHashed + if noStorageWiping { + typ = StorageKeyPlain + } + return NewStateUpdate(typ, origin, root, blockNumber, deletes, updates, nodes), nil } // commitAndFlush is a wrapper of commit which also commits the state mutations // to the configured data stores. -func (s *StateDB) commitAndFlush(block uint64, deleteEmptyObjects bool, noStorageWiping bool, deriveCodeFields bool) (*stateUpdate, error) { +func (s *StateDB) commitAndFlush(block uint64, deleteEmptyObjects bool, noStorageWiping bool, deriveCodeFields bool) (*StateUpdate, error) { ret, err := s.commit(deleteEmptyObjects, noStorageWiping, block) if err != nil { return nil, err @@ -1351,17 +1360,17 @@ func (s *StateDB) Commit(block uint64, deleteEmptyObjects bool, noStorageWiping if err != nil { return common.Hash{}, err } - return ret.root, nil + return ret.Root, nil } // CommitWithUpdate writes the state mutations and returns the state update for // external processing (e.g., live tracing hooks or size tracker). -func (s *StateDB) CommitWithUpdate(block uint64, deleteEmptyObjects bool, noStorageWiping bool) (common.Hash, *stateUpdate, error) { +func (s *StateDB) CommitWithUpdate(block uint64, deleteEmptyObjects bool, noStorageWiping bool) (common.Hash, *StateUpdate, error) { ret, err := s.commitAndFlush(block, deleteEmptyObjects, noStorageWiping, true) if err != nil { return common.Hash{}, nil, err } - return ret.root, ret, nil + return ret.Root, ret, nil } // Prepare handles the preparatory steps for executing a state transition with. diff --git a/core/state/statedb_fuzz_test.go b/core/state/statedb_fuzz_test.go index a8017e5568..c796b416a3 100644 --- a/core/state/statedb_fuzz_test.go +++ b/core/state/statedb_fuzz_test.go @@ -182,11 +182,12 @@ func (test *stateTest) run() bool { accountOrigin []map[common.Address][]byte storages []map[common.Hash]map[common.Hash][]byte storageOrigin []map[common.Address]map[common.Hash][]byte - copyUpdate = func(update *stateUpdate) { - accounts = append(accounts, maps.Clone(update.accounts)) - accountOrigin = append(accountOrigin, maps.Clone(update.accountsOrigin)) - storages = append(storages, maps.Clone(update.storages)) - storageOrigin = append(storageOrigin, maps.Clone(update.storagesOrigin)) + copyUpdate = func(update *StateUpdate) { + accts, acctOrigin, slots, slotOrigin := update.EncodeMPTState() + accounts = append(accounts, maps.Clone(accts)) + accountOrigin = append(accountOrigin, maps.Clone(acctOrigin)) + storages = append(storages, maps.Clone(slots)) + storageOrigin = append(storageOrigin, maps.Clone(slotOrigin)) } disk = rawdb.NewMemoryDatabase() tdb = triedb.NewDatabase(disk, &triedb.Config{PathDB: pathdb.Defaults}) @@ -232,11 +233,11 @@ func (test *stateTest) run() bool { if err != nil { panic(err) } - if ret.empty() { + if ret.Empty() { return true } copyUpdate(ret) - roots = append(roots, ret.root) + roots = append(roots, ret.Root) } for i := 0; i < len(test.actions); i++ { root := types.EmptyRootHash diff --git a/core/state/stateupdate.go b/core/state/stateupdate.go index 1c171cbd5e..582bcc3ec8 100644 --- a/core/state/stateupdate.go +++ b/core/state/stateupdate.go @@ -26,139 +26,143 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie/trienode" - "github.com/ethereum/go-ethereum/triedb" ) -// contractCode represents contract bytecode along with its associated metadata. -type contractCode struct { - hash common.Hash // hash is the cryptographic hash of the current contract code. - blob []byte // blob is the binary representation of the current contract code. - originHash common.Hash // originHash is the cryptographic hash of the code before mutation. +// ContractCode represents contract bytecode mutation along with its +// associated metadata. +type ContractCode struct { + Hash common.Hash // Hash is the cryptographic hash of the current contract code. + Blob []byte // Blob is the binary representation of the current contract code. + OriginHash common.Hash // OriginHash is the cryptographic hash of the code before mutation. // Derived fields, populated only when state tracking is enabled. - duplicate bool // duplicate indicates whether the updated code already exists. - originBlob []byte // originBlob is the original binary representation of the contract code. + Duplicate bool // Duplicate indicates whether the updated code already exists. + OriginBlob []byte // OriginBlob is the original binary representation of the contract code. } -// accountDelete represents an operation for deleting an Ethereum account. -type accountDelete struct { - address common.Address // address is the unique account identifier - origin []byte // origin is the original value of account data in slim-RLP encoding. +// AccountDelete represents a deletion operation for an Ethereum account. +type AccountDelete struct { + Address common.Address // Address uniquely identifies the account. + Origin *types.StateAccount // Origin is the account state prior to deletion (never be null). + Storages map[common.Hash]common.Hash // Storages contains mutated storage slots. + StoragesOrigin map[common.Hash]common.Hash // StoragesOrigin holds original values of mutated slots; keys are hashes of raw storage slot keys. +} - // storages stores mutated slots, the value should be nil. - storages map[common.Hash][]byte +// AccountUpdate represents an update operation for an Ethereum account. +type AccountUpdate struct { + Address common.Address // Address uniquely identifies the account. + Data *types.StateAccount // Data is the updated account state; nil indicates deletion. + Origin *types.StateAccount // Origin is the previous account state; nil indicates non-existence. + Code *ContractCode // Code contains updated contract code; nil if unchanged. + Storages map[common.Hash]common.Hash // Storages contains updated storage slots. - // storagesOrigin stores the original values of mutated slots in - // prefix-zero-trimmed RLP format. The map key refers to the **HASH** - // of the raw storage slot key. - storagesOrigin map[common.Hash][]byte + // StoragesOriginByKey and StoragesOriginByHash both record original values + // of mutated storage slots: + // - StoragesOriginByKey uses raw storage slot keys. + // - StoragesOriginByHash uses hashed storage slot keys. + StoragesOriginByKey map[common.Hash]common.Hash + StoragesOriginByHash map[common.Hash]common.Hash } -// accountUpdate represents an operation for updating an Ethereum account. -type accountUpdate struct { - address common.Address // address is the unique account identifier - data []byte // data is the slim-RLP encoded account data. - origin []byte // origin is the original value of account data in slim-RLP encoding. - code *contractCode // code represents mutated contract code; nil means it's not modified. - storages map[common.Hash][]byte // storages stores mutated slots in prefix-zero-trimmed RLP format. +// StorageKeyEncoding specifies the encoding scheme of a storage key. +type StorageKeyEncoding int - // storagesOriginByKey and storagesOriginByHash both store the original values - // of mutated slots in prefix-zero-trimmed RLP format. The difference is that - // storagesOriginByKey uses the **raw** storage slot key as the map ID, while - // storagesOriginByHash uses the **hash** of the storage slot key instead. - storagesOriginByKey map[common.Hash][]byte - storagesOriginByHash map[common.Hash][]byte -} +const ( + // StorageKeyHashed represents a hashed key (e.g. Keccak256). + StorageKeyHashed StorageKeyEncoding = iota + + // StorageKeyPlain represents a raw (unhashed) key. + StorageKeyPlain +) -// stateUpdate represents the difference between two states resulting from state +// StateUpdate represents the difference between two states resulting from state // execution. It contains information about mutated contract codes, accounts, // and storage slots, along with their original values. -type stateUpdate struct { - originRoot common.Hash // hash of the state before applying mutation - root common.Hash // hash of the state after applying mutation - blockNumber uint64 // Associated block number +type StateUpdate struct { + OriginRoot common.Hash // Hash of the state before applying mutation + Root common.Hash // Hash of the state after applying mutation + BlockNumber uint64 // Associated block number + + // Accounts contains mutated accounts, keyed by address hash. + Accounts map[common.Hash]*types.StateAccount - accounts map[common.Hash][]byte // accounts stores mutated accounts in 'slim RLP' encoding - accountsOrigin map[common.Address][]byte // accountsOrigin stores the original values of mutated accounts in 'slim RLP' encoding + // Storages contains mutated storage slots, keyed by address + // hash and storage slot key hash. + Storages map[common.Hash]map[common.Hash]common.Hash - // storages stores mutated slots in 'prefix-zero-trimmed' RLP format. - // The value is keyed by account hash and **storage slot key hash**. - storages map[common.Hash]map[common.Hash][]byte + // AccountsOrigin holds the original values of mutated accounts, keyed by address. + AccountsOrigin map[common.Address]*types.StateAccount - // storagesOrigin stores the original values of mutated slots in - // 'prefix-zero-trimmed' RLP format. - // (a) the value is keyed by account hash and **storage slot key** if rawStorageKey is true; - // (b) the value is keyed by account hash and **storage slot key hash** if rawStorageKey is false; - storagesOrigin map[common.Address]map[common.Hash][]byte - rawStorageKey bool + // StoragesOrigin holds the original values of mutated storage slots. + // The key format depends on StorageKeyType: + // - if StorageKeyType is plain: keyed by account address and plain storage slot key. + // - if StorageKeyType is hashed: keyed by account address and storage slot key hash. + StoragesOrigin map[common.Address]map[common.Hash]common.Hash + StorageKeyType StorageKeyEncoding - codes map[common.Address]*contractCode // codes contains the set of dirty codes - nodes *trienode.MergedNodeSet // Aggregated dirty nodes caused by state changes + Codes map[common.Address]*ContractCode // Codes contains the set of dirty codes + Nodes *trienode.MergedNodeSet // Aggregated dirty nodes caused by state changes } -// empty returns a flag indicating the state transition is empty or not. -func (sc *stateUpdate) empty() bool { - return sc.originRoot == sc.root +// Empty returns a flag indicating the state transition is empty or not. +func (sc *StateUpdate) Empty() bool { + return sc.OriginRoot == sc.Root } -// newStateUpdate constructs a state update object by identifying the differences +// NewStateUpdate constructs a state update object by identifying the differences // between two states through state execution. It combines the specified account // deletions and account updates to create a complete state update. -// -// rawStorageKey is a flag indicating whether to use the raw storage slot key or -// the hash of the slot key for constructing state update object. -func newStateUpdate(rawStorageKey bool, originRoot common.Hash, root common.Hash, blockNumber uint64, deletes map[common.Hash]*accountDelete, updates map[common.Hash]*accountUpdate, nodes *trienode.MergedNodeSet) *stateUpdate { +func NewStateUpdate(typ StorageKeyEncoding, originRoot common.Hash, root common.Hash, blockNumber uint64, deletes map[common.Hash]*AccountDelete, updates map[common.Hash]*AccountUpdate, nodes *trienode.MergedNodeSet) *StateUpdate { var ( - accounts = make(map[common.Hash][]byte) - accountsOrigin = make(map[common.Address][]byte) - storages = make(map[common.Hash]map[common.Hash][]byte) - storagesOrigin = make(map[common.Address]map[common.Hash][]byte) - codes = make(map[common.Address]*contractCode) + accounts = make(map[common.Hash]*types.StateAccount) + accountsOrigin = make(map[common.Address]*types.StateAccount) + storages = make(map[common.Hash]map[common.Hash]common.Hash) + storagesOrigin = make(map[common.Address]map[common.Hash]common.Hash) + codes = make(map[common.Address]*ContractCode) ) - // Since some accounts might be destroyed and recreated within the same + // Since some accounts might be deleted and recreated within the same // block, deletions must be aggregated first. for addrHash, op := range deletes { - addr := op.address + addr := op.Address accounts[addrHash] = nil - accountsOrigin[addr] = op.origin + accountsOrigin[addr] = op.Origin - // If storage wiping exists, the hash of the storage slot key must be used - if len(op.storages) > 0 { - storages[addrHash] = op.storages + if len(op.Storages) > 0 { + storages[addrHash] = op.Storages } - if len(op.storagesOrigin) > 0 { - storagesOrigin[addr] = op.storagesOrigin + if len(op.StoragesOrigin) > 0 { + storagesOrigin[addr] = op.StoragesOrigin } } // Aggregate account updates then. for addrHash, op := range updates { // Aggregate dirty contract codes if they are available. - addr := op.address - if op.code != nil { - codes[addr] = op.code + addr := op.Address + if op.Code != nil { + codes[addr] = op.Code } - accounts[addrHash] = op.data + accounts[addrHash] = op.Data // Aggregate the account original value. If the account is already - // present in the aggregated accountsOrigin set, skip it. + // present in the aggregated AccountsOrigin set, skip it. if _, found := accountsOrigin[addr]; !found { - accountsOrigin[addr] = op.origin + accountsOrigin[addr] = op.Origin } // Aggregate the storage mutation list. If a slot in op.storages is // already present in aggregated storages set, the value will be // overwritten. - if len(op.storages) > 0 { + if len(op.Storages) > 0 { if _, exist := storages[addrHash]; !exist { - storages[addrHash] = op.storages + storages[addrHash] = op.Storages } else { - maps.Copy(storages[addrHash], op.storages) + maps.Copy(storages[addrHash], op.Storages) } } // Aggregate the storage original values. If the slot is already present - // in aggregated storagesOrigin set, skip it. - storageOriginSet := op.storagesOriginByHash - if rawStorageKey { - storageOriginSet = op.storagesOriginByKey + // in aggregated StoragesOrigin set, skip it. + storageOriginSet := op.StoragesOriginByHash + if typ == StorageKeyPlain { + storageOriginSet = op.StoragesOriginByKey } if len(storageOriginSet) > 0 { origin, exist := storagesOrigin[addr] @@ -173,32 +177,114 @@ func newStateUpdate(rawStorageKey bool, originRoot common.Hash, root common.Hash } } } - return &stateUpdate{ - originRoot: originRoot, - root: root, - blockNumber: blockNumber, - accounts: accounts, - accountsOrigin: accountsOrigin, - storages: storages, - storagesOrigin: storagesOrigin, - rawStorageKey: rawStorageKey, - codes: codes, - nodes: nodes, + return &StateUpdate{ + OriginRoot: originRoot, + Root: root, + BlockNumber: blockNumber, + Accounts: accounts, + AccountsOrigin: accountsOrigin, + Storages: storages, + StoragesOrigin: storagesOrigin, + StorageKeyType: typ, + Codes: codes, + Nodes: nodes, } } -// stateSet converts the current stateUpdate object into a triedb.StateSet -// object. This function extracts the necessary data from the stateUpdate -// struct and formats it into the StateSet structure consumed by the triedb -// package. -func (sc *stateUpdate) stateSet() *triedb.StateSet { - return &triedb.StateSet{ - Accounts: sc.accounts, - AccountsOrigin: sc.accountsOrigin, - Storages: sc.storages, - StoragesOrigin: sc.storagesOrigin, - RawStorageKey: sc.rawStorageKey, +// encodeSlot encodes the storage slot value by trimming all leading zeros +// and then RLP-encoding the result. +func encodeSlot(value common.Hash) []byte { + if value == (common.Hash{}) { + return nil + } + blob, _ := rlp.EncodeToBytes(common.TrimLeftZeroes(value[:])) + return blob +} + +// EncodeMPTState encodes all state mutations alongside their original value +// into the Merkle-Patricia-Trie representation. +// +// It transforms account and storage updates into their corresponding MPT-encoded +// key-value mappings, using the same encoding rules as the Ethereum state trie. +func (sc *StateUpdate) EncodeMPTState() (map[common.Hash][]byte, map[common.Address][]byte, map[common.Hash]map[common.Hash][]byte, map[common.Address]map[common.Hash][]byte) { + var ( + accounts = make(map[common.Hash][]byte, len(sc.Accounts)) + storages = make(map[common.Hash]map[common.Hash][]byte, len(sc.Storages)) + accountOrigin = make(map[common.Address][]byte, len(sc.AccountsOrigin)) + storageOrigin = make(map[common.Address]map[common.Hash][]byte, len(sc.StoragesOrigin)) + ) + for addr, prev := range sc.AccountsOrigin { + if prev == nil { + accountOrigin[addr] = nil + } else { + accountOrigin[addr] = types.SlimAccountRLP(*prev) + } + } + for addrHash, data := range sc.Accounts { + if data == nil { + accounts[addrHash] = nil + } else { + accounts[addrHash] = types.SlimAccountRLP(*data) + } + } + for addr, slots := range sc.StoragesOrigin { + subset := make(map[common.Hash][]byte) + for key, val := range slots { + subset[key] = encodeSlot(val) + } + storageOrigin[addr] = subset } + for addrHash, slots := range sc.Storages { + subset := make(map[common.Hash][]byte) + for key, val := range slots { + subset[key] = encodeSlot(val) + } + storages[addrHash] = subset + } + return accounts, accountOrigin, storages, storageOrigin +} + +// EncodeUBTState encodes all state mutations alongside their original value +// into the Unified-Binary-Trie representation. +// +// It transforms account and storage updates into their corresponding UBT-encoded +// key-value mappings, using the same encoding rules as the Ethereum state trie. +func (sc *StateUpdate) EncodeUBTState() (map[common.Hash][]byte, map[common.Address][]byte, map[common.Hash]map[common.Hash][]byte, map[common.Address]map[common.Hash][]byte) { + var ( + accounts = make(map[common.Hash][]byte, len(sc.Accounts)) + storages = make(map[common.Hash]map[common.Hash][]byte, len(sc.Storages)) + accountOrigin = make(map[common.Address][]byte, len(sc.AccountsOrigin)) + storageOrigin = make(map[common.Address]map[common.Hash][]byte, len(sc.StoragesOrigin)) + ) + for addr, prev := range sc.AccountsOrigin { + if prev == nil { + accountOrigin[addr] = nil + } else { + accountOrigin[addr] = types.SlimAccountRLP(*prev) + } + } + for addrHash, data := range sc.Accounts { + if data == nil { + accounts[addrHash] = nil + } else { + accounts[addrHash] = types.SlimAccountRLP(*data) + } + } + for addr, slots := range sc.StoragesOrigin { + subset := make(map[common.Hash][]byte) + for key, val := range slots { + subset[key] = encodeSlot(val) + } + storageOrigin[addr] = subset + } + for addrHash, slots := range sc.Storages { + subset := make(map[common.Hash][]byte) + for key, val := range slots { + subset[key] = encodeSlot(val) + } + storages[addrHash] = subset + } + return accounts, accountOrigin, storages, storageOrigin } // deriveCodeFields derives the missing fields of contract code changes @@ -207,135 +293,96 @@ func (sc *stateUpdate) stateSet() *triedb.StateSet { // Note: This operation is expensive and not needed during normal state // transitions. It is only required when SizeTracker or StateUpdate hook // is enabled to produce accurate state statistics. -func (sc *stateUpdate) deriveCodeFields(reader ContractCodeReader) error { +func (sc *StateUpdate) deriveCodeFields(reader ContractCodeReader) error { cache := make(map[common.Hash]bool) - for addr, code := range sc.codes { - if code.originHash != types.EmptyCodeHash { - blob := reader.Code(addr, code.originHash) + for addr, code := range sc.Codes { + if code.OriginHash != types.EmptyCodeHash { + blob := reader.Code(addr, code.OriginHash) if len(blob) == 0 { return fmt.Errorf("original code of %x is empty", addr) } - code.originBlob = blob + code.OriginBlob = blob } - if exists, ok := cache[code.hash]; ok { - code.duplicate = exists + if exists, ok := cache[code.Hash]; ok { + code.Duplicate = exists continue } - res := reader.Has(addr, code.hash) - cache[code.hash] = res - code.duplicate = res + res := reader.Has(addr, code.Hash) + cache[code.Hash] = res + code.Duplicate = res } return nil } -// ToTracingUpdate converts the internal stateUpdate to an exported tracing.StateUpdate. -func (sc *stateUpdate) ToTracingUpdate() (*tracing.StateUpdate, error) { +// ToTracingUpdate converts the internal StateUpdate to an exported tracing.StateUpdate. +func (sc *StateUpdate) ToTracingUpdate() (*tracing.StateUpdate, error) { update := &tracing.StateUpdate{ - OriginRoot: sc.originRoot, - Root: sc.root, - BlockNumber: sc.blockNumber, - AccountChanges: make(map[common.Address]*tracing.AccountChange, len(sc.accountsOrigin)), + OriginRoot: sc.OriginRoot, + Root: sc.Root, + BlockNumber: sc.BlockNumber, + AccountChanges: make(map[common.Address]*tracing.AccountChange, len(sc.AccountsOrigin)), StorageChanges: make(map[common.Address]map[common.Hash]*tracing.StorageChange), - CodeChanges: make(map[common.Address]*tracing.CodeChange, len(sc.codes)), + CodeChanges: make(map[common.Address]*tracing.CodeChange, len(sc.Codes)), TrieChanges: make(map[common.Hash]map[string]*tracing.TrieNodeChange), } // Gather all account changes - for addr, oldData := range sc.accountsOrigin { + for addr, oldData := range sc.AccountsOrigin { addrHash := crypto.Keccak256Hash(addr.Bytes()) - newData, exists := sc.accounts[addrHash] + newData, exists := sc.Accounts[addrHash] if !exists { return nil, fmt.Errorf("account %x not found", addr) } - change := &tracing.AccountChange{} - - if len(oldData) > 0 { - acct, err := types.FullAccount(oldData) - if err != nil { - return nil, err - } - change.Prev = &types.StateAccount{ - Nonce: acct.Nonce, - Balance: acct.Balance, - Root: acct.Root, - CodeHash: acct.CodeHash, - } - } - if len(newData) > 0 { - acct, err := types.FullAccount(newData) - if err != nil { - return nil, err - } - change.New = &types.StateAccount{ - Nonce: acct.Nonce, - Balance: acct.Balance, - Root: acct.Root, - CodeHash: acct.CodeHash, - } + change := &tracing.AccountChange{ + Prev: oldData, + New: newData, } update.AccountChanges[addr] = change } // Gather all storage slot changes - for addr, slots := range sc.storagesOrigin { + for addr, slots := range sc.StoragesOrigin { addrHash := crypto.Keccak256Hash(addr.Bytes()) - subset, exists := sc.storages[addrHash] + subset, exists := sc.Storages[addrHash] if !exists { return nil, fmt.Errorf("storage %x not found", addr) } storageChanges := make(map[common.Hash]*tracing.StorageChange, len(slots)) - for key, encPrev := range slots { + for key, oldData := range slots { // Get new value - handle both raw and hashed key formats var ( exists bool - encNew []byte - decPrev []byte - decNew []byte - err error + newData common.Hash ) - if sc.rawStorageKey { - encNew, exists = subset[crypto.Keccak256Hash(key.Bytes())] + if sc.StorageKeyType == StorageKeyPlain { + newData, exists = subset[crypto.Keccak256Hash(key.Bytes())] } else { - encNew, exists = subset[key] + newData, exists = subset[key] } if !exists { return nil, fmt.Errorf("storage slot %x-%x not found", addr, key) } - - // Decode the prev and new values - if len(encPrev) > 0 { - _, decPrev, _, err = rlp.Split(encPrev) - if err != nil { - return nil, fmt.Errorf("failed to decode prevValue: %v", err) - } - } - if len(encNew) > 0 { - _, decNew, _, err = rlp.Split(encNew) - if err != nil { - return nil, fmt.Errorf("failed to decode newValue: %v", err) - } - } storageChanges[key] = &tracing.StorageChange{ - Prev: common.BytesToHash(decPrev), - New: common.BytesToHash(decNew), + Prev: oldData, + New: newData, } } update.StorageChanges[addr] = storageChanges } // Gather all contract code changes - for addr, code := range sc.codes { + for addr, code := range sc.Codes { change := &tracing.CodeChange{ New: &tracing.ContractCode{ - Hash: code.hash, - Code: code.blob, - Exists: code.duplicate, + Hash: code.Hash, + Code: code.Blob, + Exists: code.Duplicate, }, } - if code.originHash != types.EmptyCodeHash { + if code.OriginHash != types.EmptyCodeHash { change.Prev = &tracing.ContractCode{ - Hash: code.originHash, - Code: code.originBlob, + Hash: code.OriginHash, + Code: code.OriginBlob, Exists: true, } } @@ -343,8 +390,8 @@ func (sc *stateUpdate) ToTracingUpdate() (*tracing.StateUpdate, error) { } // Gather all trie node changes - if sc.nodes != nil { - for owner, subset := range sc.nodes.Sets { + if sc.Nodes != nil { + for owner, subset := range sc.Nodes.Sets { nodeChanges := make(map[string]*tracing.TrieNodeChange, len(subset.Origins)) for path, oldNode := range subset.Origins { newNode, exists := subset.Nodes[path] From e447a2696de71d9d6c56664a9e7c87ac6077f178 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20Wahrst=C3=A4tter?= <51536394+nerolation@users.noreply.github.com> Date: Tue, 21 Apr 2026 03:19:03 +0200 Subject: [PATCH 05/80] core/rawdb: clarify ReadLastPivotNumber comment (#34773) clarify that `ReadLastPivotNumber` returns `nil` only when snap sync has never been attempted, since the marker is written during snap sync and never cleared. --- core/rawdb/accessors_chain.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/rawdb/accessors_chain.go b/core/rawdb/accessors_chain.go index 0582e842c3..987b8df392 100644 --- a/core/rawdb/accessors_chain.go +++ b/core/rawdb/accessors_chain.go @@ -175,7 +175,9 @@ func WriteFinalizedBlockHash(db ethdb.KeyValueWriter, hash common.Hash) { } // ReadLastPivotNumber retrieves the number of the last pivot block. If the node -// full synced, the last pivot will always be nil. +// has never attempted snap sync, the last pivot will always be nil. The marker +// is written during snap sync and never cleared, so that a rollback past the +// pivot can re-enable snap sync. func ReadLastPivotNumber(db ethdb.KeyValueReader) *uint64 { data, _ := db.Get(lastPivotKey) if len(data) == 0 { From ac406c2fe752b0624c62f66095c9240ecec40078 Mon Sep 17 00:00:00 2001 From: Marius van der Wijden Date: Tue, 21 Apr 2026 10:20:02 +0200 Subject: [PATCH 06/80] core: implement eip-7976: Increase Calldata Floor Cost (#34748) Increases calldata floor cost from 10/40 to 64/64 --- cmd/evm/internal/t8ntool/transaction.go | 2 +- core/state_transition.go | 28 ++++++++++++++++++------- core/txpool/validation.go | 2 +- params/protocol_params.go | 1 + tests/transaction_test_util.go | 6 +++--- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/cmd/evm/internal/t8ntool/transaction.go b/cmd/evm/internal/t8ntool/transaction.go index 82c0f30b42..ad89876601 100644 --- a/cmd/evm/internal/t8ntool/transaction.go +++ b/cmd/evm/internal/t8ntool/transaction.go @@ -147,7 +147,7 @@ func Transaction(ctx *cli.Context) error { } // For Prague txs, validate the floor data gas. if rules.IsPrague { - floorDataGas, err := core.FloorDataGas(tx.Data()) + floorDataGas, err := core.FloorDataGas(rules, tx.Data()) if err != nil { r.Error = err results = append(results, r) diff --git a/core/state_transition.go b/core/state_transition.go index 297e8580ee..c3ebffd060 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -117,18 +117,32 @@ func IntrinsicGas(data []byte, accessList types.AccessList, authList []types.Set } // FloorDataGas computes the minimum gas required for a transaction based on its data tokens (EIP-7623). -func FloorDataGas(data []byte) (uint64, error) { +func FloorDataGas(rules params.Rules, data []byte) (uint64, error) { var ( - z = uint64(bytes.Count(data, []byte{0})) - nz = uint64(len(data)) - z - tokens = nz*params.TxTokenPerNonZeroByte + z + tokens uint64 + tokenCost uint64 ) + if rules.IsAmsterdam { + // EIP-7976 changes how calldata is priced. + // From 10/40 to 64/64 for zero/non-zero bytes. + tokens = uint64(len(data)) * params.TxTokenPerNonZeroByte + tokenCost = params.TxCostFloorPerToken7976 + } else { + var ( + z = uint64(bytes.Count(data, []byte{0})) + nz = uint64(len(data)) - z + ) + // Pre-Amsterdam + tokens = nz*params.TxTokenPerNonZeroByte + z + tokenCost = params.TxCostFloorPerToken + } + // Check for overflow - if (math.MaxUint64-params.TxGas)/params.TxCostFloorPerToken < tokens { + if (math.MaxUint64-params.TxGas)/tokenCost < tokens { return 0, ErrGasUintOverflow } // Minimum gas required for a transaction based on its data tokens (EIP-7623). - return params.TxGas + tokens*params.TxCostFloorPerToken, nil + return params.TxGas + tokens*tokenCost, nil } // toWordSize returns the ceiled word size required for init code payment calculation. @@ -460,7 +474,7 @@ func (st *stateTransition) execute() (*ExecutionResult, error) { } // Gas limit suffices for the floor data cost (EIP-7623) if rules.IsPrague { - floorDataGas, err = FloorDataGas(msg.Data) + floorDataGas, err = FloorDataGas(rules, msg.Data) if err != nil { return nil, err } diff --git a/core/txpool/validation.go b/core/txpool/validation.go index 3569bba08d..85bf65ac40 100644 --- a/core/txpool/validation.go +++ b/core/txpool/validation.go @@ -134,7 +134,7 @@ func ValidateTransaction(tx *types.Transaction, head *types.Header, signer types } // Ensure the transaction can cover floor data gas. if rules.IsPrague { - floorDataGas, err := core.FloorDataGas(tx.Data()) + floorDataGas, err := core.FloorDataGas(rules, tx.Data()) if err != nil { return err } diff --git a/params/protocol_params.go b/params/protocol_params.go index 652574287c..9da275c486 100644 --- a/params/protocol_params.go +++ b/params/protocol_params.go @@ -96,6 +96,7 @@ const ( TxDataNonZeroGasEIP2028 uint64 = 16 // Per byte of non zero data attached to a transaction after EIP 2028 (part in Istanbul) TxTokenPerNonZeroByte uint64 = 4 // Token cost per non-zero byte as specified by EIP-7623. TxCostFloorPerToken uint64 = 10 // Cost floor per byte of data as specified by EIP-7623. + TxCostFloorPerToken7976 uint64 = 16 // Cost floor per byte of data as specified by EIP-7976. TxAccessListAddressGas uint64 = 2400 // Per address specified in EIP 2930 access list TxAccessListStorageKeyGas uint64 = 1900 // Per storage key specified in EIP 2930 access list TxAuthTupleGas uint64 = 12500 // Per auth tuple code specified in EIP-7702 diff --git a/tests/transaction_test_util.go b/tests/transaction_test_util.go index a659a1f786..8b8d0357bf 100644 --- a/tests/transaction_test_util.go +++ b/tests/transaction_test_util.go @@ -71,7 +71,7 @@ func (tt *TransactionTest) Run() error { if err := tt.validate(); err != nil { return err } - validateTx := func(rlpData hexutil.Bytes, signer types.Signer, rules *params.Rules) (sender common.Address, hash common.Hash, requiredGas uint64, err error) { + validateTx := func(rlpData hexutil.Bytes, signer types.Signer, rules params.Rules) (sender common.Address, hash common.Hash, requiredGas uint64, err error) { tx := new(types.Transaction) if err = tx.UnmarshalBinary(rlpData); err != nil { return @@ -92,7 +92,7 @@ func (tt *TransactionTest) Run() error { if rules.IsPrague { var floorDataGas uint64 - floorDataGas, err = core.FloorDataGas(tx.Data()) + floorDataGas, err = core.FloorDataGas(rules, tx.Data()) if err != nil { return } @@ -133,7 +133,7 @@ func (tt *TransactionTest) Run() error { rules = config.Rules(new(big.Int), testcase.isMerge, 0) signer = types.MakeSigner(config, new(big.Int), 0) ) - sender, hash, gas, err := validateTx(tt.Txbytes, signer, &rules) + sender, hash, gas, err := validateTx(tt.Txbytes, signer, rules) if err != nil { if expected.Hash != nil { return fmt.Errorf("unexpected error fork %s: %v", testcase.name, err) From 077d83387a2883b571fe1570f1318005264a2009 Mon Sep 17 00:00:00 2001 From: cui Date: Tue, 21 Apr 2026 19:58:49 +0800 Subject: [PATCH 07/80] metrics: reset internal value slice in Clear (#34761) --- metrics/sample.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metrics/sample.go b/metrics/sample.go index dc8167809f..95092ffc3c 100644 --- a/metrics/sample.go +++ b/metrics/sample.go @@ -314,7 +314,7 @@ func (s *UniformSample) Clear() { s.mutex.Lock() defer s.mutex.Unlock() s.count = 0 - clear(s.values) + s.values = s.values[:0] } // Snapshot returns a read-only copy of the sample. From f568ab9931167ecfaef8f9696dfd0d52a861f2fd Mon Sep 17 00:00:00 2001 From: Barnabas Busa Date: Tue, 21 Apr 2026 14:48:21 +0200 Subject: [PATCH 08/80] internal/telemetry: add gRPC transport for OTLP trace export (#33941) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary - Add `grpc://` and `grpcs://` URL scheme support for OTLP trace export alongside existing `http://`/`https://` - The OTLP spec defines two transports: HTTP (port 4318) and gRPC (port 4317). Many observability backends (Jaeger, Tempo, Datadog) prefer gRPC for lower overhead - Both `otlptracehttp` and `otlptracegrpc` return `*otlptrace.Exporter`, so only exporter construction changes — everything downstream (batch processor, tracer provider, lifecycle) is untouched - Update flag usage strings to be transport-agnostic ## Example usage ``` geth --rpc.telemetry --rpc.telemetry.endpoint grpc://localhost:4317 geth --rpc.telemetry --rpc.telemetry.endpoint grpcs://tempo-grpc.example.com:443 ``` --------- Co-authored-by: Claude Opus 4.6 --- cmd/keeper/go.mod | 4 +-- cmd/keeper/go.sum | 12 +++---- cmd/utils/flags.go | 6 ++-- go.mod | 25 ++++++------- go.sum | 50 +++++++++++++------------- internal/telemetry/tracesetup/setup.go | 29 ++++++++++++--- 6 files changed, 75 insertions(+), 51 deletions(-) diff --git a/cmd/keeper/go.mod b/cmd/keeper/go.mod index 8303b4ab2e..2d99cb2232 100644 --- a/cmd/keeper/go.mod +++ b/cmd/keeper/go.mod @@ -38,8 +38,8 @@ require ( go.opentelemetry.io/otel v1.40.0 // indirect go.opentelemetry.io/otel/metric v1.40.0 // indirect go.opentelemetry.io/otel/trace v1.40.0 // indirect - golang.org/x/crypto v0.44.0 // indirect - golang.org/x/sync v0.18.0 // indirect + golang.org/x/crypto v0.47.0 // indirect + golang.org/x/sync v0.19.0 // indirect golang.org/x/sys v0.40.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/cmd/keeper/go.sum b/cmd/keeper/go.sum index a162537c88..09c8e55822 100644 --- a/cmd/keeper/go.sum +++ b/cmd/keeper/go.sum @@ -127,20 +127,20 @@ go.opentelemetry.io/otel/sdk v1.40.0 h1:KHW/jUzgo6wsPh9At46+h4upjtccTmuZCFAc9OJ7 go.opentelemetry.io/otel/sdk v1.40.0/go.mod h1:Ph7EFdYvxq72Y8Li9q8KebuYUr2KoeyHx0DRMKrYBUE= go.opentelemetry.io/otel/trace v1.40.0 h1:WA4etStDttCSYuhwvEa8OP8I5EWu24lkOzp+ZYblVjw= go.opentelemetry.io/otel/trace v1.40.0/go.mod h1:zeAhriXecNGP/s2SEG3+Y8X9ujcJOTqQ5RgdEJcawiA= -golang.org/x/crypto v0.44.0 h1:A97SsFvM3AIwEEmTBiaxPPTYpDC47w720rdiiUvgoAU= -golang.org/x/crypto v0.44.0/go.mod h1:013i+Nw79BMiQiMsOPcVCB5ZIJbYkerPrGnOa00tvmc= +golang.org/x/crypto v0.47.0 h1:V6e3FRj+n4dbpw86FJ8Fv7XVOql7TEwpHapKoMJ/GO8= +golang.org/x/crypto v0.47.0/go.mod h1:ff3Y9VzzKbwSSEzWqJsJVBnWmRwRSHt/6Op5n9bQc4A= golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df h1:UA2aFVmmsIlefxMk29Dp2juaUSth8Pyn3Tq5Y5mJGME= golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc= -golang.org/x/sync v0.18.0 h1:kr88TuHDroi+UVf+0hZnirlk8o8T+4MrK6mr60WkH/I= -golang.org/x/sync v0.18.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= +golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4= +golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.40.0 h1:DBZZqJ2Rkml6QMQsZywtnjnnGvHza6BTfYFWY9kjEWQ= golang.org/x/sys v0.40.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= -golang.org/x/text v0.31.0 h1:aC8ghyu4JhP8VojJ2lEHBnochRno1sgL6nEi9WGFGMM= -golang.org/x/text v0.31.0/go.mod h1:tKRAlv61yKIjGGHX/4tP1LTbc13YSec1pxVEWXzfoeM= +golang.org/x/text v0.33.0 h1:B3njUFyqtHDUI5jMn1YIr5B0IE2U0qck04r6d4KPAxE= +golang.org/x/text v0.33.0/go.mod h1:LuMebE6+rBincTi9+xWTY8TztLzKHc/9C1uBCG27+q8= google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE= google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index aff45087db..9d996f15cb 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -1067,19 +1067,19 @@ Please note that --` + MetricsHTTPFlag.Name + ` must be set to start the server. RPCTelemetryEndpointFlag = &cli.StringFlag{ Name: "rpc.telemetry.endpoint", - Usage: "Defines where RPC telemetry is sent (e.g., http://localhost:4318)", + Usage: "Defines where RPC telemetry is sent (e.g., http://localhost:4318 or grpc://localhost:4317)", Category: flags.APICategory, } RPCTelemetryUserFlag = &cli.StringFlag{ Name: "rpc.telemetry.username", - Usage: "HTTP Basic Auth username for OpenTelemetry", + Usage: "Basic Auth username for OpenTelemetry", Category: flags.APICategory, } RPCTelemetryPasswordFlag = &cli.StringFlag{ Name: "rpc.telemetry.password", - Usage: "HTTP Basic Auth password for OpenTelemetry", + Usage: "Basic Auth password for OpenTelemetry", Category: flags.APICategory, } diff --git a/go.mod b/go.mod index 37a2537dd0..a623668b89 100644 --- a/go.mod +++ b/go.mod @@ -62,19 +62,20 @@ require ( github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 github.com/urfave/cli/v2 v2.27.5 go.opentelemetry.io/otel v1.40.0 - go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.39.0 - go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.39.0 + go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.40.0 + go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.40.0 + go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.40.0 go.opentelemetry.io/otel/sdk v1.40.0 go.opentelemetry.io/otel/trace v1.40.0 go.uber.org/automaxprocs v1.5.2 go.uber.org/goleak v1.3.0 - golang.org/x/crypto v0.44.0 + golang.org/x/crypto v0.47.0 golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df - golang.org/x/sync v0.18.0 + golang.org/x/sync v0.19.0 golang.org/x/sys v0.40.0 - golang.org/x/text v0.31.0 + golang.org/x/text v0.33.0 golang.org/x/time v0.9.0 - golang.org/x/tools v0.38.0 + golang.org/x/tools v0.40.0 google.golang.org/protobuf v1.36.11 gopkg.in/natefinch/lumberjack.v2 v2.2.1 gopkg.in/yaml.v3 v3.0.1 @@ -84,13 +85,13 @@ require ( github.com/cenkalti/backoff/v5 v5.0.3 // indirect github.com/go-logr/logr v1.4.3 // indirect github.com/go-logr/stdr v1.2.2 // indirect - github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.3 // indirect + github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.7 // indirect go.opentelemetry.io/auto/sdk v1.2.1 // indirect go.opentelemetry.io/otel/metric v1.40.0 // indirect go.opentelemetry.io/proto/otlp v1.9.0 // indirect - google.golang.org/genproto/googleapis/api v0.0.0-20251222181119-0a764e51fe1b // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20251222181119-0a764e51fe1b // indirect - google.golang.org/grpc v1.77.0 // indirect + google.golang.org/genproto/googleapis/api v0.0.0-20260128011058-8636f8732409 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20260128011058-8636f8732409 // indirect + google.golang.org/grpc v1.78.0 // indirect ) require ( @@ -161,8 +162,8 @@ require ( github.com/tklauser/go-sysconf v0.3.12 // indirect github.com/tklauser/numcpus v0.6.1 // indirect github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect - golang.org/x/mod v0.29.0 // indirect - golang.org/x/net v0.47.0 // indirect + golang.org/x/mod v0.31.0 // indirect + golang.org/x/net v0.49.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index c465603242..4713387020 100644 --- a/go.sum +++ b/go.sum @@ -196,8 +196,8 @@ github.com/grafana/pyroscope-go/godeltaprof v0.1.9 h1:c1Us8i6eSmkW+Ez05d3co8kasn github.com/grafana/pyroscope-go/godeltaprof v0.1.9/go.mod h1:2+l7K7twW49Ct4wFluZD3tZ6e0SjanjcUUBPVD/UuGU= github.com/graph-gophers/graphql-go v1.3.0 h1:Eb9x/q6MFpCLz7jBCiP/WTxjSDrYLR1QY41SORZyNJ0= github.com/graph-gophers/graphql-go v1.3.0/go.mod h1:9CQHMSxwO4MprSdzoIEobiHpoLtHm77vfxsvsIN5Vuc= -github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.3 h1:NmZ1PKzSTQbuGHw9DGPFomqkkLWMC+vZCkfs+FHv1Vg= -github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.3/go.mod h1:zQrxl1YP88HQlA6i9c63DSVPFklWpGX4OWAc9bFuaH4= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.7 h1:X+2YciYSxvMQK0UZ7sg45ZVabVZBeBuvMkmuI2V3Fak= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.7/go.mod h1:lW34nIZuQ8UDPdkon5fmfp2l3+ZkQ2me/+oecHYLOII= github.com/hashicorp/go-bexpr v0.1.10 h1:9kuI5PFotCboP3dkDYFr/wi0gg0QVbSNz5oFRpxn4uE= github.com/hashicorp/go-bexpr v0.1.10/go.mod h1:oxlubA2vC/gFVfX1A6JGp7ls7uCDlfJn732ehYYg+g0= github.com/holiman/billy v0.0.0-20250707135307-f2f9b9aae7db h1:IZUYC/xb3giYwBLMnr8d0TGTzPKFGNTCGgGLoyeX330= @@ -382,10 +382,12 @@ go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y= go.opentelemetry.io/otel v1.40.0 h1:oA5YeOcpRTXq6NN7frwmwFR0Cn3RhTVZvXsP4duvCms= go.opentelemetry.io/otel v1.40.0/go.mod h1:IMb+uXZUKkMXdPddhwAHm6UfOwJyh4ct1ybIlV14J0g= -go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.39.0 h1:f0cb2XPmrqn4XMy9PNliTgRKJgS5WcL/u0/WRYGz4t0= -go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.39.0/go.mod h1:vnakAaFckOMiMtOIhFI2MNH4FYrZzXCYxmb1LlhoGz8= -go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.39.0 h1:Ckwye2FpXkYgiHX7fyVrN1uA/UYd9ounqqTuSNAv0k4= -go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.39.0/go.mod h1:teIFJh5pW2y+AN7riv6IBPX2DuesS3HgP39mwOspKwU= +go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.40.0 h1:QKdN8ly8zEMrByybbQgv8cWBcdAarwmIPZ6FThrWXJs= +go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.40.0/go.mod h1:bTdK1nhqF76qiPoCCdyFIV+N/sRHYXYCTQc+3VCi3MI= +go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.40.0 h1:DvJDOPmSWQHWywQS6lKL+pb8s3gBLOZUtw4N+mavW1I= +go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.40.0/go.mod h1:EtekO9DEJb4/jRyN4v4Qjc2yA7AtfCBuz2FynRUWTXs= +go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.40.0 h1:wVZXIWjQSeSmMoxF74LzAnpVQOAFDo3pPji9Y4SOFKc= +go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.40.0/go.mod h1:khvBS2IggMFNwZK/6lEeHg/W57h/IX6J4URh57fuI40= go.opentelemetry.io/otel/metric v1.40.0 h1:rcZe317KPftE2rstWIBitCdVp89A2HqjkxR3c11+p9g= go.opentelemetry.io/otel/metric v1.40.0/go.mod h1:ib/crwQH7N3r5kfiBZQbwrTge743UDc7DTFVZrrXnqc= go.opentelemetry.io/otel/sdk v1.40.0 h1:KHW/jUzgo6wsPh9At46+h4upjtccTmuZCFAc9OJ71f8= @@ -408,16 +410,16 @@ golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWP golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE= golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw= -golang.org/x/crypto v0.44.0 h1:A97SsFvM3AIwEEmTBiaxPPTYpDC47w720rdiiUvgoAU= -golang.org/x/crypto v0.44.0/go.mod h1:013i+Nw79BMiQiMsOPcVCB5ZIJbYkerPrGnOa00tvmc= +golang.org/x/crypto v0.47.0 h1:V6e3FRj+n4dbpw86FJ8Fv7XVOql7TEwpHapKoMJ/GO8= +golang.org/x/crypto v0.47.0/go.mod h1:ff3Y9VzzKbwSSEzWqJsJVBnWmRwRSHt/6Op5n9bQc4A= golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df h1:UA2aFVmmsIlefxMk29Dp2juaUSth8Pyn3Tq5Y5mJGME= golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/mod v0.29.0 h1:HV8lRxZC4l2cr3Zq1LvtOsi/ThTgWnUk/y64QSs8GwA= -golang.org/x/mod v0.29.0/go.mod h1:NyhrlYXJ2H4eJiRy/WDBO6HMqZQ6q9nk4JzS3NuCK+w= +golang.org/x/mod v0.31.0 h1:HaW9xtz0+kOcWKwli0ZXy79Ix+UW/vOfmWI5QVd2tgI= +golang.org/x/mod v0.31.0/go.mod h1:43JraMp9cGx1Rx3AqioxrbrhNsLl2l/iNAvuBkrezpg= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= @@ -433,8 +435,8 @@ golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI= -golang.org/x/net v0.47.0 h1:Mx+4dIFzqraBXUugkia1OOvlD6LemFo1ALMHjrXDOhY= -golang.org/x/net v0.47.0/go.mod h1:/jNxtkgq5yWUGYkaZGqo27cfGZ1c5Nen03aYrrKpVRU= +golang.org/x/net v0.49.0 h1:eeHFmOGUTtaaPSGNmjBKpbng9MulQsJURQUAfUwY++o= +golang.org/x/net v0.49.0/go.mod h1:/ysNB2EvaqvesRkuLAyjI1ycPZlQHM3q01F02UY/MV8= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -443,8 +445,8 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.18.0 h1:kr88TuHDroi+UVf+0hZnirlk8o8T+4MrK6mr60WkH/I= -golang.org/x/sync v0.18.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= +golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4= +golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -496,8 +498,8 @@ golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= -golang.org/x/text v0.31.0 h1:aC8ghyu4JhP8VojJ2lEHBnochRno1sgL6nEi9WGFGMM= -golang.org/x/text v0.31.0/go.mod h1:tKRAlv61yKIjGGHX/4tP1LTbc13YSec1pxVEWXzfoeM= +golang.org/x/text v0.33.0 h1:B3njUFyqtHDUI5jMn1YIr5B0IE2U0qck04r6d4KPAxE= +golang.org/x/text v0.33.0/go.mod h1:LuMebE6+rBincTi9+xWTY8TztLzKHc/9C1uBCG27+q8= golang.org/x/time v0.0.0-20201208040808-7e3f01d25324/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.9.0 h1:EsRrnYcQiGH+5FfbgvV4AP7qEZstoyrHB0DzarOQ4ZY= @@ -509,8 +511,8 @@ golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roY golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= -golang.org/x/tools v0.38.0 h1:Hx2Xv8hISq8Lm16jvBZ2VQf+RLmbd7wVUsALibYI/IQ= -golang.org/x/tools v0.38.0/go.mod h1:yEsQ/d/YK8cjh0L6rZlY8tgtlKiBNTL14pGDJPJpYQs= +golang.org/x/tools v0.40.0 h1:yLkxfA+Qnul4cs9QA3KnlFu0lVmd8JJfoq+E41uSutA= +golang.org/x/tools v0.40.0/go.mod h1:Ik/tzLRlbscWpqqMRjyWYDisX8bG13FrdXp3o4Sr9lc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -518,12 +520,12 @@ golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1N golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gonum.org/v1/gonum v0.16.0 h1:5+ul4Swaf3ESvrOnidPp4GZbzf0mxVQpDCYUQE7OJfk= gonum.org/v1/gonum v0.16.0/go.mod h1:fef3am4MQ93R2HHpKnLk4/Tbh/s0+wqD5nfa6Pnwy4E= -google.golang.org/genproto/googleapis/api v0.0.0-20251222181119-0a764e51fe1b h1:uA40e2M6fYRBf0+8uN5mLlqUtV192iiksiICIBkYJ1E= -google.golang.org/genproto/googleapis/api v0.0.0-20251222181119-0a764e51fe1b/go.mod h1:Xa7le7qx2vmqB/SzWUBa7KdMjpdpAHlh5QCSnjessQk= -google.golang.org/genproto/googleapis/rpc v0.0.0-20251222181119-0a764e51fe1b h1:Mv8VFug0MP9e5vUxfBcE3vUkV6CImK3cMNMIDFjmzxU= -google.golang.org/genproto/googleapis/rpc v0.0.0-20251222181119-0a764e51fe1b/go.mod h1:j9x/tPzZkyxcgEFkiKEEGxfvyumM01BEtsW8xzOahRQ= -google.golang.org/grpc v1.77.0 h1:wVVY6/8cGA6vvffn+wWK5ToddbgdU3d8MNENr4evgXM= -google.golang.org/grpc v1.77.0/go.mod h1:z0BY1iVj0q8E1uSQCjL9cppRj+gnZjzDnzV0dHhrNig= +google.golang.org/genproto/googleapis/api v0.0.0-20260128011058-8636f8732409 h1:merA0rdPeUV3YIIfHHcH4qBkiQAc1nfCKSI7lB4cV2M= +google.golang.org/genproto/googleapis/api v0.0.0-20260128011058-8636f8732409/go.mod h1:fl8J1IvUjCilwZzQowmw2b7HQB2eAuYBabMXzWurF+I= +google.golang.org/genproto/googleapis/rpc v0.0.0-20260128011058-8636f8732409 h1:H86B94AW+VfJWDqFeEbBPhEtHzJwJfTbgE2lZa54ZAQ= +google.golang.org/genproto/googleapis/rpc v0.0.0-20260128011058-8636f8732409/go.mod h1:j9x/tPzZkyxcgEFkiKEEGxfvyumM01BEtsW8xzOahRQ= +google.golang.org/grpc v1.78.0 h1:K1XZG/yGDJnzMdd/uZHAkVqJE+xIDOcmdSFZkBUicNc= +google.golang.org/grpc v1.78.0/go.mod h1:I47qjTo4OKbMkjA/aOOwxDIiPSBofUtQUI5EfpWvW7U= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= diff --git a/internal/telemetry/tracesetup/setup.go b/internal/telemetry/tracesetup/setup.go index 444416dd26..08c5f739b6 100644 --- a/internal/telemetry/tracesetup/setup.go +++ b/internal/telemetry/tracesetup/setup.go @@ -30,6 +30,7 @@ import ( "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/exporters/otlp/otlptrace" + "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" "go.opentelemetry.io/otel/propagation" "go.opentelemetry.io/otel/sdk/resource" @@ -83,6 +84,14 @@ func SetupTelemetry(cfg node.OpenTelemetryConfig, stack *node.Node) error { if err != nil { return fmt.Errorf("invalid rpc tracing endpoint URL: %w", err) } + // Build auth headers once, shared across transports. + var authHeaders map[string]string + if cfg.AuthUser != "" { + authHeaders = map[string]string{ + "Authorization": "Basic " + base64.StdEncoding.EncodeToString([]byte(cfg.AuthUser+":"+cfg.AuthPassword)), + } + } + var exporter *otlptrace.Exporter switch u.Scheme { case "http", "https": @@ -95,12 +104,24 @@ func SetupTelemetry(cfg node.OpenTelemetryConfig, stack *node.Node) error { if u.Path != "" && u.Path != "/" { opts = append(opts, otlptracehttp.WithURLPath(u.Path)) } - if cfg.AuthUser != "" { - opts = append(opts, otlptracehttp.WithHeaders(map[string]string{ - "Authorization": "Basic " + base64.StdEncoding.EncodeToString([]byte(cfg.AuthUser+":"+cfg.AuthPassword)), - })) + if authHeaders != nil { + opts = append(opts, otlptracehttp.WithHeaders(authHeaders)) } exporter = otlptracehttp.NewUnstarted(opts...) + case "grpc", "grpcs": + if u.Path != "" && u.Path != "/" { + return fmt.Errorf("gRPC endpoints do not support URL paths: %s", u.Path) + } + opts := []otlptracegrpc.Option{ + otlptracegrpc.WithEndpoint(u.Host), + } + if u.Scheme == "grpc" { + opts = append(opts, otlptracegrpc.WithInsecure()) + } + if authHeaders != nil { + opts = append(opts, otlptracegrpc.WithHeaders(authHeaders)) + } + exporter = otlptracegrpc.NewUnstarted(opts...) default: return fmt.Errorf("unsupported telemetry url scheme: %s", u.Scheme) } From c374e74ee189f1f3068cea41f676dd61dfa66209 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Tue, 21 Apr 2026 14:50:09 +0200 Subject: [PATCH 09/80] trie/bintrie: print todot path in binary (#34777) The nodes were named using the byte representation of the path, instead of the binary representation. This was confusing to other client devs trying to achieve interop. --- trie/bintrie/store_commit.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/trie/bintrie/store_commit.go b/trie/bintrie/store_commit.go index b142ecb34c..7101087b51 100644 --- a/trie/bintrie/store_commit.go +++ b/trie/bintrie/store_commit.go @@ -279,10 +279,10 @@ func (s *nodeStore) toDot(ref nodeRef, parent, path string) string { ret = fmt.Sprintf("%s %s -> %s\n", ret, parent, me) } if !node.left.IsEmpty() { - ret += s.toDot(node.left, me, fmt.Sprintf("%s%02x", path, 0)) + ret += s.toDot(node.left, me, fmt.Sprintf("%s%b", path, 0)) } if !node.right.IsEmpty() { - ret += s.toDot(node.right, me, fmt.Sprintf("%s%02x", path, 1)) + ret += s.toDot(node.right, me, fmt.Sprintf("%s%b", path, 1)) } return ret case kindStem: From d422ab39d5a34d2317d329d6407068792ae29a73 Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Wed, 22 Apr 2026 02:58:21 +0800 Subject: [PATCH 10/80] consensus, core, internal, miner: remove FinalizeAndAssemble (#34726) This PR removes `FinalizeAndAssemble` from the consensus engine interface and relocates block assembly logic outside of the consensus engine. Block assembly is consensus-agnostic. Most validations can be performed by the caller. For example: - Withdrawals must be nil prior to Shanghai - After Shanghai upgrade, withdrawals must be non-nil, even if empty. The only notable consensus-specific validation is related to uncles. In clique, the concept of uncles does not exist, and any block containing uncles should be considered invalid. Within the block production package, the policy is to produce blocks according to the latest chain specification. As a result, Clique-specific block production is no longer supported. This tradeoff is considered acceptable. --- consensus/beacon/consensus.go | 46 ----------------------------------- consensus/clique/clique.go | 19 --------------- consensus/consensus.go | 9 ------- consensus/ethash/consensus.go | 19 --------------- core/chain_makers.go | 20 +++++++++++---- core/state_processor.go | 10 ++++++++ internal/ethapi/simulate.go | 10 ++++---- miner/worker.go | 26 ++++++++++++++++---- 8 files changed, 51 insertions(+), 108 deletions(-) diff --git a/consensus/beacon/consensus.go b/consensus/beacon/consensus.go index c4a284d485..72ac75c036 100644 --- a/consensus/beacon/consensus.go +++ b/consensus/beacon/consensus.go @@ -17,7 +17,6 @@ package beacon import ( - "context" "errors" "fmt" "math/big" @@ -26,13 +25,10 @@ import ( "github.com/ethereum/go-ethereum/consensus" "github.com/ethereum/go-ethereum/consensus/misc/eip1559" "github.com/ethereum/go-ethereum/consensus/misc/eip4844" - "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" - "github.com/ethereum/go-ethereum/internal/telemetry" "github.com/ethereum/go-ethereum/params" - "github.com/ethereum/go-ethereum/trie" "github.com/holiman/uint256" ) @@ -361,48 +357,6 @@ func (beacon *Beacon) Finalize(chain consensus.ChainHeaderReader, header *types. // No block reward which is issued by consensus layer instead. } -// FinalizeAndAssemble implements consensus.Engine, setting the final state and -// assembling the block. -func (beacon *Beacon) FinalizeAndAssemble(ctx context.Context, chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body, receipts []*types.Receipt) (result *types.Block, err error) { - ctx, _, spanEnd := telemetry.StartSpan(ctx, "consensus.beacon.FinalizeAndAssemble", - telemetry.Int64Attribute("block.number", int64(header.Number.Uint64())), - telemetry.Int64Attribute("txs.count", int64(len(body.Transactions))), - telemetry.Int64Attribute("withdrawals.count", int64(len(body.Withdrawals))), - ) - defer spanEnd(&err) - - if !beacon.IsPoSHeader(header) { - block, delegateErr := beacon.ethone.FinalizeAndAssemble(ctx, chain, header, state, body, receipts) - return block, delegateErr - } - shanghai := chain.Config().IsShanghai(header.Number, header.Time) - if shanghai { - // All blocks after Shanghai must include a withdrawals root. - if body.Withdrawals == nil { - body.Withdrawals = make([]*types.Withdrawal, 0) - } - } else { - if len(body.Withdrawals) > 0 { - return nil, errors.New("withdrawals set before Shanghai activation") - } - } - // Finalize and assemble the block. - _, _, finalizeSpanEnd := telemetry.StartSpan(ctx, "consensus.beacon.Finalize") - beacon.Finalize(chain, header, state, body) - finalizeSpanEnd(nil) - - // Assign the final state root to header. - _, _, rootSpanEnd := telemetry.StartSpan(ctx, "consensus.beacon.IntermediateRoot") - header.Root = state.IntermediateRoot(true) - rootSpanEnd(nil) - - // Assemble the final block. - _, _, blockSpanEnd := telemetry.StartSpan(ctx, "consensus.beacon.NewBlock") - block := types.NewBlock(header, body, receipts, trie.NewStackTrie(nil)) - blockSpanEnd(nil) - return block, nil -} - // Seal generates a new sealing request for the given input block and pushes // the result into the given channel. // diff --git a/consensus/clique/clique.go b/consensus/clique/clique.go index 3bf79d5a62..ceaec44656 100644 --- a/consensus/clique/clique.go +++ b/consensus/clique/clique.go @@ -19,7 +19,6 @@ package clique import ( "bytes" - "context" "errors" "fmt" "io" @@ -34,7 +33,6 @@ import ( "github.com/ethereum/go-ethereum/consensus" "github.com/ethereum/go-ethereum/consensus/misc" "github.com/ethereum/go-ethereum/consensus/misc/eip1559" - "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/crypto" @@ -43,7 +41,6 @@ import ( "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" - "github.com/ethereum/go-ethereum/trie" ) const ( @@ -580,22 +577,6 @@ func (c *Clique) Finalize(chain consensus.ChainHeaderReader, header *types.Heade // No block rewards in PoA, so the state remains as is } -// FinalizeAndAssemble implements consensus.Engine, ensuring no uncles are set, -// nor block rewards given, and returns the final block. -func (c *Clique) FinalizeAndAssemble(ctx context.Context, chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body, receipts []*types.Receipt) (*types.Block, error) { - if len(body.Withdrawals) > 0 { - return nil, errors.New("clique does not support withdrawals") - } - // Finalize block - c.Finalize(chain, header, state, body) - - // Assign the final state root to header. - header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number)) - - // Assemble and return the final block for sealing. - return types.NewBlock(header, &types.Body{Transactions: body.Transactions}, receipts, trie.NewStackTrie(nil)), nil -} - // Authorize injects a private key into the consensus engine to mint new blocks // with. func (c *Clique) Authorize(signer common.Address) { diff --git a/consensus/consensus.go b/consensus/consensus.go index 094026b614..4ba389292f 100644 --- a/consensus/consensus.go +++ b/consensus/consensus.go @@ -18,11 +18,9 @@ package consensus import ( - "context" "math/big" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/params" @@ -88,13 +86,6 @@ type Engine interface { // that happen at finalization (e.g. block rewards). Finalize(chain ChainHeaderReader, header *types.Header, state vm.StateDB, body *types.Body) - // FinalizeAndAssemble runs any post-transaction state modifications (e.g. block - // rewards or process withdrawals) and assembles the final block. - // - // Note: The block header and state database might be updated to reflect any - // consensus rules that happen at finalization (e.g. block rewards). - FinalizeAndAssemble(ctx context.Context, chain ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body, receipts []*types.Receipt) (*types.Block, error) - // Seal generates a new sealing request for the given input block and pushes // the result into the given channel. // diff --git a/consensus/ethash/consensus.go b/consensus/ethash/consensus.go index 56256d1215..ee9d9d97d6 100644 --- a/consensus/ethash/consensus.go +++ b/consensus/ethash/consensus.go @@ -17,7 +17,6 @@ package ethash import ( - "context" "errors" "fmt" "math/big" @@ -28,14 +27,12 @@ import ( "github.com/ethereum/go-ethereum/consensus" "github.com/ethereum/go-ethereum/consensus/misc" "github.com/ethereum/go-ethereum/consensus/misc/eip1559" - "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/crypto/keccak" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" - "github.com/ethereum/go-ethereum/trie" "github.com/holiman/uint256" ) @@ -512,22 +509,6 @@ func (ethash *Ethash) Finalize(chain consensus.ChainHeaderReader, header *types. accumulateRewards(chain.Config(), state, header, body.Uncles) } -// FinalizeAndAssemble implements consensus.Engine, accumulating the block and -// uncle rewards, setting the final state and assembling the block. -func (ethash *Ethash) FinalizeAndAssemble(ctx context.Context, chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body, receipts []*types.Receipt) (*types.Block, error) { - if len(body.Withdrawals) > 0 { - return nil, errors.New("ethash does not support withdrawals") - } - // Finalize block - ethash.Finalize(chain, header, state, body) - - // Assign the final state root to header. - header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number)) - - // Header seems complete, assemble into a block and return - return types.NewBlock(header, &types.Body{Transactions: body.Transactions, Uncles: body.Uncles}, receipts, trie.NewStackTrie(nil)), nil -} - // SealHash returns the hash of a block prior to it being sealed. func (ethash *Ethash) SealHash(header *types.Header) (hash common.Hash) { hasher := keccak.NewLegacyKeccak256() diff --git a/core/chain_makers.go b/core/chain_makers.go index 3bc7f6528b..46cd98de61 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -17,7 +17,6 @@ package core import ( - "context" "fmt" "math/big" @@ -411,11 +410,22 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse b.header.RequestsHash = &reqHash } - body := types.Body{Transactions: b.txs, Uncles: b.uncles, Withdrawals: b.withdrawals} - block, err := b.engine.FinalizeAndAssemble(context.Background(), cm, b.header, statedb, &body, b.receipts) - if err != nil { - panic(err) + body := types.Body{ + Transactions: b.txs, + Uncles: b.uncles, + Withdrawals: b.withdrawals, + } + if !config.IsShanghai(b.header.Number, b.header.Time) { + if body.Withdrawals != nil { + panic("unexpected withdrawal before shanghai") + } + } else { + if body.Withdrawals == nil { + body.Withdrawals = make([]*types.Withdrawal, 0) + } } + // Assemble the block for delivery. + block := AssembleBlock(b.engine, cm, b.header, statedb, &body, b.receipts) // Write state changes to db root, err := statedb.Commit(b.header.Number.Uint64(), config.IsEIP158(b.header.Number), config.IsCancun(b.header.Number, b.header.Time)) diff --git a/core/state_processor.go b/core/state_processor.go index 9cb7cc73bc..1c801ec9a4 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -22,6 +22,7 @@ import ( "math/big" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/consensus" "github.com/ethereum/go-ethereum/consensus/misc" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/tracing" @@ -30,6 +31,7 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/internal/telemetry" "github.com/ethereum/go-ethereum/params" + "github.com/ethereum/go-ethereum/trie" ) // StateProcessor is a basic Processor, which takes care of transitioning @@ -372,3 +374,11 @@ func onSystemCallStart(tracer *tracing.Hooks, ctx *tracing.VMContext) { tracer.OnSystemCallStart() } } + +// AssembleBlock finalizes the state and assembles the block with provided +// body and receipts. +func AssembleBlock(engine consensus.Engine, chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body, receipts []*types.Receipt) *types.Block { + engine.Finalize(chain, header, state, body) + header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number)) + return types.NewBlock(header, body, receipts, trie.NewStackTrie(nil)) +} diff --git a/internal/ethapi/simulate.go b/internal/ethapi/simulate.go index 90e88e83ee..d18561760e 100644 --- a/internal/ethapi/simulate.go +++ b/internal/ethapi/simulate.go @@ -416,13 +416,13 @@ func (sim *simulator) processBlock(ctx context.Context, block *simBlock, header, blockBody := &types.Body{ Transactions: txes, - Withdrawals: *block.BlockOverrides.Withdrawals, + Withdrawals: *block.BlockOverrides.Withdrawals, // Withdrawal is also sanitized as non-nil } chainHeadReader := &simChainHeadReader{ctx, sim.b} - b, err := sim.b.Engine().FinalizeAndAssemble(ctx, chainHeadReader, header, sim.state, blockBody, receipts) - if err != nil { - return nil, nil, nil, err - } + + // Assemble the block + b := core.AssembleBlock(sim.b.Engine(), chainHeadReader, header, sim.state, blockBody, receipts) + repairLogs(callResults, b.Hash()) return b, callResults, senders, nil } diff --git a/miner/worker.go b/miner/worker.go index 1d648f0ee1..ae5d6c306f 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -183,7 +183,21 @@ func (miner *Miner) generateWork(ctx context.Context, genParam *generateParams, } } } - body := types.Body{Transactions: work.txs, Withdrawals: genParam.withdrawals} + // Construct the block body, the withdrawal list should never be null + // if Shanghai has been activated. + body := types.Body{ + Transactions: work.txs, + Withdrawals: genParam.withdrawals, + } + if !miner.chainConfig.IsShanghai(work.header.Number, work.header.Time) { + if body.Withdrawals != nil { + return &newPayloadResult{err: errors.New("unexpected withdrawals before shanghai")} + } + } else { + if body.Withdrawals == nil { + body.Withdrawals = make([]*types.Withdrawal, 0) + } + } allLogs := make([]*types.Log, 0) for _, r := range work.receipts { @@ -211,11 +225,11 @@ func (miner *Miner) generateWork(ctx context.Context, genParam *generateParams, reqHash := types.CalcRequestsHash(requests) work.header.RequestsHash = &reqHash } + // Assemble the block for delivery. + _, _, assembleSpanEnd := telemetry.StartSpan(ctx, "miner.AssembleBlock") + block := core.AssembleBlock(miner.engine, miner.chain, work.header, work.state, &body, work.receipts) + assembleSpanEnd(nil) - block, err := miner.engine.FinalizeAndAssemble(ctx, miner.chain, work.header, work.state, &body, work.receipts) - if err != nil { - return &newPayloadResult{err: err} - } return &newPayloadResult{ block: block, fees: totalFees(block, work.receipts), @@ -413,6 +427,7 @@ func (miner *Miner) applyTransaction(env *environment, tx *types.Transaction) (* func (miner *Miner) commitTransactions(ctx context.Context, env *environment, plainTxs, blobTxs *transactionsByPriceAndNonce, interrupt *atomic.Int32) error { ctx, _, spanEnd := telemetry.StartSpan(ctx, "miner.commitTransactions") defer spanEnd(nil) + isCancun := miner.chainConfig.IsCancun(env.header.Number, env.header.Time) for { // Check interruption signal and abort building if it's fired. @@ -529,6 +544,7 @@ func (miner *Miner) commitTransactions(ctx context.Context, env *environment, pl func (miner *Miner) fillTransactions(ctx context.Context, interrupt *atomic.Int32, env *environment) (err error) { ctx, span, spanEnd := telemetry.StartSpan(ctx, "miner.fillTransactions") defer spanEnd(&err) + miner.confMu.RLock() tip := miner.config.GasPrice prio := miner.prio From dca3cf02a22cdc6aae266f4c7e013db1c330e1a0 Mon Sep 17 00:00:00 2001 From: cui Date: Wed, 22 Apr 2026 11:02:44 +0800 Subject: [PATCH 11/80] core: pre-allocate the receipt slice (#34786) --- core/state_processor.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/state_processor.go b/core/state_processor.go index 1c801ec9a4..fda3bf8fe7 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -64,7 +64,7 @@ func (p *StateProcessor) chainConfig() *params.ChainConfig { func (p *StateProcessor) Process(ctx context.Context, block *types.Block, statedb *state.StateDB, cfg vm.Config) (*ProcessResult, error) { var ( config = p.chainConfig() - receipts types.Receipts + receipts = make(types.Receipts, 0, len(block.Transactions())) header = block.Header() blockHash = block.Hash() blockNumber = block.Number() From 3abc4cea3532e71f944a9b3e06b4341a6e338b59 Mon Sep 17 00:00:00 2001 From: cui Date: Wed, 22 Apr 2026 11:05:59 +0800 Subject: [PATCH 12/80] core/state: use address hash cache if available (#34780) --- core/state/statedb.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/state/statedb.go b/core/state/statedb.go index 8c57edf08e..a62e3c2020 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -1126,7 +1126,7 @@ func (s *StateDB) handleDestruction(noStorageWiping bool) (map[common.Hash]*Acco continue } // The account was existent, it can be either case (c) or (d). - addrHash := crypto.Keccak256Hash(addr.Bytes()) + addrHash := prevObj.addrHash() op := &AccountDelete{ Address: addr, Origin: prev, From 6f02965aab977e8e73942163a357443d43fe9139 Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Wed, 22 Apr 2026 13:42:49 +0800 Subject: [PATCH 13/80] core: track the state access footprint (#34776) This is a pre-requisite PR for landing the BAL construction --- core/state/state_object.go | 16 ++----- core/state/statedb.go | 21 ++++++++- core/state/statedb_hooked.go | 13 +++--- core/types/bal/access_list.go | 80 ++++++++++++++++++++++++++++++++++ core/vm/contracts.go | 6 +-- core/vm/contracts_fuzz_test.go | 3 +- core/vm/contracts_test.go | 9 ++-- core/vm/evm.go | 24 ++-------- core/vm/interface.go | 6 ++- 9 files changed, 130 insertions(+), 48 deletions(-) create mode 100644 core/types/bal/access_list.go diff --git a/core/state/state_object.go b/core/state/state_object.go index df54733d63..264dfd920d 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -180,6 +180,9 @@ func (s *stateObject) getState(key common.Hash) (common.Hash, common.Hash) { // GetCommittedState retrieves the value associated with the specific key // without any mutations caused in the current execution. func (s *stateObject) GetCommittedState(key common.Hash) common.Hash { + // Record slot access regardless of whether the storage slot exists. + s.db.stateReadList.AddState(s.address, key) + // If we have a pending write or clean cached, return that if value, pending := s.pendingStorage[key]; pending { return value @@ -194,19 +197,6 @@ func (s *stateObject) GetCommittedState(key common.Hash) common.Hash { // have been handles via pendingStorage above. // 2) we don't have new values, and can deliver empty response back if _, destructed := s.db.stateObjectsDestruct[s.address]; destructed { - // Invoke the reader regardless and discard the returned value. - // The returned value may not be empty, as it could belong to a - // self-destructed contract. - // - // The read operation is still essential for correctly building - // the block-level access list. - // - // TODO(rjl493456442) the reader interface can be extended with - // Touch, recording the read access without the actual disk load. - _, err := s.db.reader.Storage(s.address, key) - if err != nil { - s.db.setError(err) - } s.originStorage[key] = common.Hash{} // track the empty slot as origin value return common.Hash{} } diff --git a/core/state/statedb.go b/core/state/statedb.go index a62e3c2020..5d94d4806d 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -31,6 +31,7 @@ import ( "github.com/ethereum/go-ethereum/core/stateless" "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/core/types/bal" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/params" @@ -126,6 +127,9 @@ type StateDB struct { accessList *accessList accessEvents *AccessEvents + // Per-transaction state access footprint for EIP-7928 + stateReadList *bal.StateAccessList + // Transient storage transientStorage transientStorage @@ -317,6 +321,11 @@ func (s *StateDB) Empty(addr common.Address) bool { return so == nil || so.empty() } +// Touch accesses the specific account without returning anything. +func (s *StateDB) Touch(addr common.Address) { + s.getStateObject(addr) +} + // GetBalance retrieves the balance from the given address or 0 if object not found func (s *StateDB) GetBalance(addr common.Address) *uint256.Int { stateObject := s.getStateObject(addr) @@ -579,6 +588,9 @@ func (s *StateDB) deleteStateObject(addr common.Address) { // getStateObject retrieves a state object given by the address, returning nil if // the object is not found or was deleted in this execution context. func (s *StateDB) getStateObject(addr common.Address) *stateObject { + // Record state access regardless of whether the account exists. + s.stateReadList.AddAccount(addr) + // Prefer live objects if any is available if obj := s.stateObjects[addr]; obj != nil { return obj @@ -784,7 +796,7 @@ func (s *StateDB) LogsForBurnAccounts() []*types.Log { // Finalise finalises the state by removing the destructed objects and clears // the journal as well as the refunds. Finalise, however, will not push any updates // into the tries just yet. Only IntermediateRoot or Commit will do that. -func (s *StateDB) Finalise(deleteEmptyObjects bool) { +func (s *StateDB) Finalise(deleteEmptyObjects bool) *bal.StateAccessList { addressesToPrefetch := make([]common.Address, 0, len(s.journal.dirties)) for addr := range s.journal.dirties { obj, exist := s.stateObjects[addr] @@ -800,6 +812,7 @@ func (s *StateDB) Finalise(deleteEmptyObjects bool) { if obj.selfDestructed || (deleteEmptyObjects && obj.empty()) { delete(s.stateObjects, obj.address) s.markDelete(addr) + // We need to maintain account deletions explicitly (will remain // set indefinitely). Note only the first occurred self-destruct // event is tracked. @@ -822,6 +835,8 @@ func (s *StateDB) Finalise(deleteEmptyObjects bool) { } // Invalidate journal because reverting across transactions is not allowed. s.clearJournalAndRefund() + + return s.stateReadList } // IntermediateRoot computes the current root hash of the state trie. @@ -1415,6 +1430,10 @@ func (s *StateDB) Prepare(rules params.Rules, sender, coinbase common.Address, d } // Reset transient storage at the beginning of transaction execution s.transientStorage = newTransientStorage() + + if rules.IsAmsterdam { + s.stateReadList = bal.NewStateAccessList() + } } // AddAddressToAccessList adds the given address to the access list diff --git a/core/state/statedb_hooked.go b/core/state/statedb_hooked.go index 687c4bb52b..c5faa7c98e 100644 --- a/core/state/statedb_hooked.go +++ b/core/state/statedb_hooked.go @@ -25,6 +25,7 @@ import ( "github.com/ethereum/go-ethereum/core/stateless" "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/core/types/bal" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/params" "github.com/holiman/uint256" @@ -114,6 +115,10 @@ func (s *hookedStateDB) Exist(addr common.Address) bool { return s.inner.Exist(addr) } +func (s *hookedStateDB) Touch(addr common.Address) { + s.inner.Touch(addr) +} + func (s *hookedStateDB) Empty(addr common.Address) bool { return s.inner.Empty(addr) } @@ -229,11 +234,10 @@ func (s *hookedStateDB) LogsForBurnAccounts() []*types.Log { return s.inner.LogsForBurnAccounts() } -func (s *hookedStateDB) Finalise(deleteEmptyObjects bool) { +func (s *hookedStateDB) Finalise(deleteEmptyObjects bool) *bal.StateAccessList { if s.hooks.OnBalanceChange == nil && s.hooks.OnNonceChangeV2 == nil && s.hooks.OnNonceChange == nil && s.hooks.OnCodeChangeV2 == nil && s.hooks.OnCodeChange == nil { // Short circuit if no relevant hooks are set. - s.inner.Finalise(deleteEmptyObjects) - return + return s.inner.Finalise(deleteEmptyObjects) } // Collect all self-destructed addresses first, then sort them to ensure @@ -282,6 +286,5 @@ func (s *hookedStateDB) Finalise(deleteEmptyObjects bool) { s.hooks.OnCodeChange(addr, prevCodeHash, s.inner.GetCode(addr), types.EmptyCodeHash, nil) } } - - s.inner.Finalise(deleteEmptyObjects) + return s.inner.Finalise(deleteEmptyObjects) } diff --git a/core/types/bal/access_list.go b/core/types/bal/access_list.go new file mode 100644 index 0000000000..91da5ebcb7 --- /dev/null +++ b/core/types/bal/access_list.go @@ -0,0 +1,80 @@ +// Copyright 2026 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 + +package bal + +import ( + "maps" + + "github.com/ethereum/go-ethereum/common" +) + +// StorageAccessList represents a set of storage slots accessed within an account. +type StorageAccessList map[common.Hash]struct{} + +// StateAccessList records the set of accounts and storage slots that have been +// accessed. An entry with an empty StorageAccessList denotes an account access +// without any storage slot access. +type StateAccessList struct { + list map[common.Address]StorageAccessList +} + +// NewStateAccessList returns an empty StateAccessList ready for use. +func NewStateAccessList() *StateAccessList { + return &StateAccessList{ + list: make(map[common.Address]StorageAccessList), + } +} + +// AddAccount records an access to the given account. It is a no-op if the +// account is already present. +func (s *StateAccessList) AddAccount(addr common.Address) { + if s == nil { + return + } + if _, exists := s.list[addr]; !exists { + s.list[addr] = make(StorageAccessList) + } +} + +// AddState records an access to the given storage slot. The owning account is +// implicitly recorded as well. +func (s *StateAccessList) AddState(addr common.Address, slot common.Hash) { + if s == nil { + return + } + slots, exists := s.list[addr] + if !exists { + slots = make(StorageAccessList) + s.list[addr] = slots + } + slots[slot] = struct{}{} +} + +// Merge merges the entries from other into the receiver. +func (s *StateAccessList) Merge(other *StateAccessList) { + if s == nil || other == nil { + return + } + for addr, otherSlots := range other.list { + slots, exists := s.list[addr] + if !exists { + s.list[addr] = otherSlots + continue + } + maps.Copy(slots, otherSlots) + } +} diff --git a/core/vm/contracts.go b/core/vm/contracts.go index b1eed79282..6dadb64873 100644 --- a/core/vm/contracts.go +++ b/core/vm/contracts.go @@ -262,7 +262,7 @@ func ActivePrecompiles(rules params.Rules) []common.Address { // - the returned bytes, // - the remaining gas budget, // - any error that occurred -func RunPrecompiledContract(stateDB StateDB, p PrecompiledContract, address common.Address, input []byte, gas GasBudget, logger *tracing.Hooks) (ret []byte, remaining GasBudget, err error) { +func RunPrecompiledContract(stateDB StateDB, p PrecompiledContract, address common.Address, input []byte, gas GasBudget, logger *tracing.Hooks, rules params.Rules) (ret []byte, remaining GasBudget, err error) { gasCost := p.RequiredGas(input) prior, ok := gas.Charge(GasCosts{RegularGas: gasCost}) if !ok { @@ -274,8 +274,8 @@ func RunPrecompiledContract(stateDB StateDB, p PrecompiledContract, address comm } // Touch the precompile for block-level accessList recording once Amsterdam // fork is activated. - if stateDB != nil { - stateDB.Exist(address) + if rules.IsAmsterdam { + stateDB.Touch(address) } output, err := p.Run(input) return output, gas, err diff --git a/core/vm/contracts_fuzz_test.go b/core/vm/contracts_fuzz_test.go index 35a9bd0257..988cdb91f2 100644 --- a/core/vm/contracts_fuzz_test.go +++ b/core/vm/contracts_fuzz_test.go @@ -20,6 +20,7 @@ import ( "testing" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/params" ) func FuzzPrecompiledContracts(f *testing.F) { @@ -36,7 +37,7 @@ func FuzzPrecompiledContracts(f *testing.F) { return } inWant := string(input) - RunPrecompiledContract(nil, p, a, input, NewGasBudget(gas), nil) + RunPrecompiledContract(nil, p, a, input, NewGasBudget(gas), nil, params.Rules{}) if inHave := string(input); inWant != inHave { t.Errorf("Precompiled %v modified input data", a) } diff --git a/core/vm/contracts_test.go b/core/vm/contracts_test.go index dda753f504..e7841c8552 100644 --- a/core/vm/contracts_test.go +++ b/core/vm/contracts_test.go @@ -25,6 +25,7 @@ import ( "time" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/params" ) // precompiledTest defines the input/output pairs for precompiled contract tests. @@ -99,7 +100,7 @@ func testPrecompiled(addr string, test precompiledTest, t *testing.T) { in := common.Hex2Bytes(test.Input) gas := p.RequiredGas(in) t.Run(fmt.Sprintf("%s-Gas=%d", test.Name, gas), func(t *testing.T) { - if res, _, err := RunPrecompiledContract(nil, p, common.HexToAddress(addr), in, NewGasBudget(gas), nil); err != nil { + if res, _, err := RunPrecompiledContract(nil, p, common.HexToAddress(addr), in, NewGasBudget(gas), nil, params.Rules{}); err != nil { t.Error(err) } else if common.Bytes2Hex(res) != test.Expected { t.Errorf("Expected %v, got %v", test.Expected, common.Bytes2Hex(res)) @@ -121,7 +122,7 @@ func testPrecompiledOOG(addr string, test precompiledTest, t *testing.T) { gas := test.Gas - 1 t.Run(fmt.Sprintf("%s-Gas=%d", test.Name, gas), func(t *testing.T) { - _, _, err := RunPrecompiledContract(nil, p, common.HexToAddress(addr), in, NewGasBudget(gas), nil) + _, _, err := RunPrecompiledContract(nil, p, common.HexToAddress(addr), in, NewGasBudget(gas), nil, params.Rules{}) if err.Error() != "out of gas" { t.Errorf("Expected error [out of gas], got [%v]", err) } @@ -138,7 +139,7 @@ func testPrecompiledFailure(addr string, test precompiledFailureTest, t *testing in := common.Hex2Bytes(test.Input) gas := p.RequiredGas(in) t.Run(test.Name, func(t *testing.T) { - _, _, err := RunPrecompiledContract(nil, p, common.HexToAddress(addr), in, NewGasBudget(gas), nil) + _, _, err := RunPrecompiledContract(nil, p, common.HexToAddress(addr), in, NewGasBudget(gas), nil, params.Rules{}) if err.Error() != test.ExpectedError { t.Errorf("Expected error [%v], got [%v]", test.ExpectedError, err) } @@ -169,7 +170,7 @@ func benchmarkPrecompiled(addr string, test precompiledTest, bench *testing.B) { start := time.Now() for bench.Loop() { copy(data, in) - res, _, err = RunPrecompiledContract(nil, p, common.HexToAddress(addr), data, NewGasBudget(reqGas), nil) + res, _, err = RunPrecompiledContract(nil, p, common.HexToAddress(addr), data, NewGasBudget(reqGas), nil, params.Rules{}) } elapsed := uint64(time.Since(start)) if elapsed < 1 { diff --git a/core/vm/evm.go b/core/vm/evm.go index ca8d8967ec..59e301c0a7 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -287,11 +287,7 @@ func (evm *EVM) Call(caller common.Address, addr common.Address, input []byte, g } if isPrecompile { - var stateDB StateDB - if evm.chainRules.IsAmsterdam { - stateDB = evm.StateDB - } - ret, gas, err = RunPrecompiledContract(stateDB, p, addr, input, gas, evm.Config.Tracer) + ret, gas, err = RunPrecompiledContract(evm.StateDB, p, addr, input, gas, evm.Config.Tracer, evm.chainRules) } else { // Initialise a new contract and set the code that is to be used by the EVM. code := evm.resolveCode(addr) @@ -354,11 +350,7 @@ func (evm *EVM) CallCode(caller common.Address, addr common.Address, input []byt // It is allowed to call precompiles, even via delegatecall if p, isPrecompile := evm.precompile(addr); isPrecompile { - var stateDB StateDB - if evm.chainRules.IsAmsterdam { - stateDB = evm.StateDB - } - ret, gas, err = RunPrecompiledContract(stateDB, p, addr, input, gas, evm.Config.Tracer) + ret, gas, err = RunPrecompiledContract(evm.StateDB, p, addr, input, gas, evm.Config.Tracer, evm.chainRules) } else { // Initialise a new contract and set the code that is to be used by the EVM. // The contract is a scoped environment for this execution context only. @@ -401,11 +393,7 @@ func (evm *EVM) DelegateCall(originCaller common.Address, caller common.Address, // It is allowed to call precompiles, even via delegatecall if p, isPrecompile := evm.precompile(addr); isPrecompile { - var stateDB StateDB - if evm.chainRules.IsAmsterdam { - stateDB = evm.StateDB - } - ret, gas, err = RunPrecompiledContract(stateDB, p, addr, input, gas, evm.Config.Tracer) + ret, gas, err = RunPrecompiledContract(evm.StateDB, p, addr, input, gas, evm.Config.Tracer, evm.chainRules) } else { // Initialise a new contract and make initialise the delegate values // @@ -457,11 +445,7 @@ func (evm *EVM) StaticCall(caller common.Address, addr common.Address, input []b evm.StateDB.AddBalance(addr, new(uint256.Int), tracing.BalanceChangeTouchAccount) if p, isPrecompile := evm.precompile(addr); isPrecompile { - var stateDB StateDB - if evm.chainRules.IsAmsterdam { - stateDB = evm.StateDB - } - ret, gas, err = RunPrecompiledContract(stateDB, p, addr, input, gas, evm.Config.Tracer) + ret, gas, err = RunPrecompiledContract(evm.StateDB, p, addr, input, gas, evm.Config.Tracer, evm.chainRules) } else { // Initialise a new contract and set the code that is to be used by the EVM. // The contract is a scoped environment for this execution context only. diff --git a/core/vm/interface.go b/core/vm/interface.go index 41b52a10dc..487d8002f9 100644 --- a/core/vm/interface.go +++ b/core/vm/interface.go @@ -22,6 +22,7 @@ import ( "github.com/ethereum/go-ethereum/core/stateless" "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/core/types/bal" "github.com/ethereum/go-ethereum/params" "github.com/holiman/uint256" ) @@ -63,6 +64,9 @@ type StateDB interface { // Notably this also returns true for self-destructed accounts within the current transaction. Exist(common.Address) bool + // Touch accesses the state without returning anything. + Touch(common.Address) + // IsNewContract reports whether the contract at the given address was deployed // during the current transaction. IsNewContract(addr common.Address) bool @@ -94,5 +98,5 @@ type StateDB interface { AccessEvents() *state.AccessEvents // Finalise must be invoked at the end of a transaction - Finalise(bool) + Finalise(bool) *bal.StateAccessList } From 87b030780e0edc82a1d48969fd05a0f9a84727f2 Mon Sep 17 00:00:00 2001 From: Sina M <1591639+s1na@users.noreply.github.com> Date: Wed, 22 Apr 2026 12:18:56 +0200 Subject: [PATCH 14/80] .github: add windows runner (#34742) Difference to Appveyor: - Missing 386 build. Hit some issue because user-space memory there is around 2Gbs. Also seems generally extremely niche. - Not doing the archive step and NSIS installer and uploads (those are done on the builder). --- .github/workflows/go.yml | 41 ++++++++++++++++++++++++++++++++++++++++ rlp/rlpgen/gen_test.go | 4 ++++ 2 files changed, 45 insertions(+) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 507057afe5..3e811072ff 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -97,3 +97,44 @@ jobs: - name: Run tests run: go run build/ci.go test -p 8 + + windows: + name: Windows ${{ matrix.arch }} + needs: lint + runs-on: [self-hosted, windows, x64] + strategy: + fail-fast: false + matrix: + include: + - arch: amd64 + mingw: 'C:\msys64\mingw64' + test: true + - arch: '386' + mingw: 'C:\msys64\mingw32' + test: false + env: + GETH_MINGW: ${{ matrix.mingw }} + GETH_CC: ${{ matrix.mingw }}\bin\gcc.exe + steps: + - uses: actions/checkout@v4 + with: + submodules: true + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: '1.25' + cache: false + + - name: Build + shell: cmd + run: | + set PATH=%GETH_MINGW%\bin;%PATH% + go run build/ci.go install -arch ${{ matrix.arch }} -cc %GETH_CC% + + - name: Run tests + if: matrix.test + shell: cmd + run: | + set PATH=%GETH_MINGW%\bin;%PATH% + go run build/ci.go test -arch ${{ matrix.arch }} -cc %GETH_CC% -short -p 8 diff --git a/rlp/rlpgen/gen_test.go b/rlp/rlpgen/gen_test.go index 4bfb1b9d25..2e35ef933d 100644 --- a/rlp/rlpgen/gen_test.go +++ b/rlp/rlpgen/gen_test.go @@ -26,6 +26,7 @@ import ( "go/types" "os" "path/filepath" + "runtime" "testing" ) @@ -52,6 +53,9 @@ var tests = []string{"uints", "nil", "rawvalue", "optional", "bigint", "uint256" func TestOutput(t *testing.T) { for _, test := range tests { t.Run(test, func(t *testing.T) { + if test == "pkgclash" && runtime.GOOS == "windows" { + t.Skip("source-based importer is pathologically slow on Windows/NTFS") + } inputFile := filepath.Join("testdata", test+".in.txt") outputFile := filepath.Join("testdata", test+".out.txt") bctx, typ, err := loadTestSource(inputFile, "Test") From eb3283fb2ef8b606d35fb6e30d2e036c186be45e Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Wed, 22 Apr 2026 12:32:19 +0200 Subject: [PATCH 15/80] accounts/usbwallet: revert github.com/karalabe/hid to fix freebsd build (#34784) This PR reverts the last change to the freebsd build, and it fixes the _direct_ FreeBSD build. Here, we change the upstream of github.com/karalabe/hid to its new home, github.com/ethereum/hid. The new dependency includes a dummy.go file that makes `go mod vendor` work. ##### Origin of the problem Enrique is maintaining the FreeBSD ports, and FreeBSD ports only support vendored go modules. It turns out that `go mod vendor` will not include C files if there is no `.go` file in the directory. Since the C files were missing for `karalabe/hid`, the ports maintainer tried to use the version of `hidapi` that is provided by the ports. To do so, he had to modify the way things are included. This broke the _out of ports_ FreeBSD build. --- accounts/usbwallet/hub.go | 2 +- accounts/usbwallet/wallet.go | 2 +- go.mod | 2 +- go.sum | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/accounts/usbwallet/hub.go b/accounts/usbwallet/hub.go index 6f8ac0d8d9..cfa844b345 100644 --- a/accounts/usbwallet/hub.go +++ b/accounts/usbwallet/hub.go @@ -26,7 +26,7 @@ import ( "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/log" - "github.com/karalabe/hid" + "github.com/ethereum/hid" ) // LedgerScheme is the protocol scheme prefixing account and wallet URLs. diff --git a/accounts/usbwallet/wallet.go b/accounts/usbwallet/wallet.go index f1597ca1a7..5da58a2ec2 100644 --- a/accounts/usbwallet/wallet.go +++ b/accounts/usbwallet/wallet.go @@ -31,7 +31,7 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/log" - "github.com/karalabe/hid" + "github.com/ethereum/hid" ) // Maximum time between wallet health checks to detect USB unplugs. diff --git a/go.mod b/go.mod index a623668b89..17897a62c0 100644 --- a/go.mod +++ b/go.mod @@ -23,6 +23,7 @@ require ( github.com/dop251/goja v0.0.0-20230605162241-28ee0ee714f3 github.com/ethereum/c-kzg-4844/v2 v2.1.6 github.com/ethereum/go-bigmodexpfix v0.0.0-20250911101455-f9e208c548ab + github.com/ethereum/hid v1.0.1-0.20260421154323-c2ab8d9bf68a github.com/fatih/color v1.16.0 github.com/ferranbt/fastssz v0.1.4 github.com/fsnotify/fsnotify v1.6.0 @@ -43,7 +44,6 @@ require ( github.com/influxdata/influxdb1-client v0.0.0-20220302092344-a9ab5670611c github.com/jackpal/go-nat-pmp v1.0.2 github.com/jedisct1/go-minisign v0.0.0-20230811132847-661be99b8267 - github.com/karalabe/hid v1.0.1-0.20260315100226-f5d04adeffeb github.com/klauspost/compress v1.17.8 github.com/kylelemons/godebug v1.1.0 github.com/mattn/go-colorable v0.1.13 diff --git a/go.sum b/go.sum index 4713387020..bad8a44cfd 100644 --- a/go.sum +++ b/go.sum @@ -117,6 +117,8 @@ github.com/ethereum/c-kzg-4844/v2 v2.1.6 h1:xQymkKCT5E2Jiaoqf3v4wsNgjZLY0lRSkZn2 github.com/ethereum/c-kzg-4844/v2 v2.1.6/go.mod h1:8HMkUZ5JRv4hpw/XUrYWSQNAUzhHMg2UDb/U+5m+XNw= github.com/ethereum/go-bigmodexpfix v0.0.0-20250911101455-f9e208c548ab h1:rvv6MJhy07IMfEKuARQ9TKojGqLVNxQajaXEp/BoqSk= github.com/ethereum/go-bigmodexpfix v0.0.0-20250911101455-f9e208c548ab/go.mod h1:IuLm4IsPipXKF7CW5Lzf68PIbZ5yl7FFd74l/E0o9A8= +github.com/ethereum/hid v1.0.1-0.20260421154323-c2ab8d9bf68a h1:eIFUceK3U/z9UV0D/kAI6cxA27eH7MPqt2ks7fbzj/k= +github.com/ethereum/hid v1.0.1-0.20260421154323-c2ab8d9bf68a/go.mod h1:nABYy4hsKZpuN0mu0uybdjrIOuGb1eE7b1lci/ezUAo= github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= github.com/ferranbt/fastssz v0.1.4 h1:OCDB+dYDEQDvAgtAGnTSidK1Pe2tW3nFV40XyMkTeDY= @@ -225,8 +227,6 @@ github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9Y github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= -github.com/karalabe/hid v1.0.1-0.20260315100226-f5d04adeffeb h1:Ag83At00qa4FLkcdMgrwHVSakqky/eZczOlxd4q336E= -github.com/karalabe/hid v1.0.1-0.20260315100226-f5d04adeffeb/go.mod h1:qk1sX/IBgppQNcGCRoj90u6EGC056EBoIc1oEjCWla8= github.com/kilic/bls12-381 v0.1.0 h1:encrdjqKMEvabVQ7qYOKu1OvhqpK4s47wDYtNiPtlp4= github.com/kilic/bls12-381 v0.1.0/go.mod h1:vDTTHJONJ6G+P2R74EhnyotQDTliQDnFEwhdmfzw1ig= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= From b0ead5e17bb3e479137fb545da2fef27e1e51672 Mon Sep 17 00:00:00 2001 From: Sina M <1591639+s1na@users.noreply.github.com> Date: Wed, 22 Apr 2026 16:18:29 +0200 Subject: [PATCH 16/80] .gitea: add installer and archive steps for windows (#34793) Adds the installer + archive steps that were done on appveyor to gitea builder. --- .gitea/workflows/release.yml | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/release.yml index 41defedd00..efe76cf170 100644 --- a/.gitea/workflows/release.yml +++ b/.gitea/workflows/release.yml @@ -166,6 +166,24 @@ jobs: env: GETH_MINGW: 'C:\msys64\mingw64' + - name: "Create/upload archive (amd64)" + shell: cmd + run: | + go run build/ci.go archive -arch amd64 -type zip -signer WINDOWS_SIGNING_KEY -upload gethstore/builds + env: + WINDOWS_SIGNING_KEY: ${{ secrets.WINDOWS_SIGNING_KEY }} + AZURE_BLOBSTORE_TOKEN: ${{ secrets.AZURE_BLOBSTORE_TOKEN }} + + - name: "Create/upload NSIS installer (amd64)" + shell: cmd + run: | + set "PATH=C:\Program Files (x86)\NSIS;%PATH%" + go run build/ci.go nsis -arch amd64 -signer WINDOWS_SIGNING_KEY -upload gethstore/builds + del /Q build\bin\* + env: + WINDOWS_SIGNING_KEY: ${{ secrets.WINDOWS_SIGNING_KEY }} + AZURE_BLOBSTORE_TOKEN: ${{ secrets.AZURE_BLOBSTORE_TOKEN }} + - name: "Build (386)" shell: cmd run: | @@ -174,6 +192,24 @@ jobs: env: GETH_MINGW: 'C:\msys64\mingw32' + - name: "Create/upload archive (386)" + shell: cmd + run: | + go run build/ci.go archive -arch 386 -type zip -signer WINDOWS_SIGNING_KEY -upload gethstore/builds + env: + WINDOWS_SIGNING_KEY: ${{ secrets.WINDOWS_SIGNING_KEY }} + AZURE_BLOBSTORE_TOKEN: ${{ secrets.AZURE_BLOBSTORE_TOKEN }} + + - name: "Create/upload NSIS installer (386)" + shell: cmd + run: | + set "PATH=C:\Program Files (x86)\NSIS;%PATH%" + go run build/ci.go nsis -arch 386 -signer WINDOWS_SIGNING_KEY -upload gethstore/builds + del /Q build\bin\* + env: + WINDOWS_SIGNING_KEY: ${{ secrets.WINDOWS_SIGNING_KEY }} + AZURE_BLOBSTORE_TOKEN: ${{ secrets.AZURE_BLOBSTORE_TOKEN }} + docker: name: Docker Image runs-on: ubuntu-latest From 8e2107dc39dc9dab132150ec915e7ac299f9eb48 Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Wed, 22 Apr 2026 16:48:38 +0200 Subject: [PATCH 17/80] cmd/devp2p: fix disconnect decoding in rlpx ping (#34781) The rlpx ping command mishandled disconnect responses on two counts: the error return from rlp.DecodeBytes was ignored, so decode failures silently produced an "invalid disconnect message" error with no context; and the decoder assumed the spec-compliant list form exclusively, while older geth and some other implementations send the reason as a bare byte. Accept both wire forms (matching the legacy-tolerant behavior already in p2p.decodeDisconnectMessage), and on decode failure include the raw payload so operators can see exactly what the peer sent. Add a unit test for the decoder covering both forms plus the empty-payload error path. --- cmd/devp2p/rlpxcmd.go | 38 +++++++++++++++++-- cmd/devp2p/rlpxcmd_test.go | 75 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 cmd/devp2p/rlpxcmd_test.go diff --git a/cmd/devp2p/rlpxcmd.go b/cmd/devp2p/rlpxcmd.go index 1dc8f82460..ec73171e76 100644 --- a/cmd/devp2p/rlpxcmd.go +++ b/cmd/devp2p/rlpxcmd.go @@ -17,6 +17,7 @@ package main import ( + "bytes" "errors" "fmt" "net" @@ -30,6 +31,31 @@ import ( "github.com/urfave/cli/v2" ) +// decodeRLPxDisconnect parses a disconnect message payload. Per the RLPx spec +// the payload is a list containing a single reason, but some implementations +// (including older geth) sent the reason as a bare byte. Accept both forms. +func decodeRLPxDisconnect(data []byte) (p2p.DiscReason, error) { + s := rlp.NewStream(bytes.NewReader(data), uint64(len(data))) + k, _, err := s.Kind() + if err != nil { + return 0, err + } + var reason p2p.DiscReason + if k == rlp.List { + if _, err := s.List(); err != nil { + return 0, err + } + if err := s.Decode(&reason); err != nil { + return 0, err + } + return reason, nil + } + if err := s.Decode(&reason); err != nil { + return 0, err + } + return reason, nil +} + var ( rlpxCommand = &cli.Command{ Name: "rlpx", @@ -103,11 +129,15 @@ func rlpxPing(ctx *cli.Context) error { } fmt.Printf("%+v\n", h) case 1: - var msg []p2p.DiscReason - if rlp.DecodeBytes(data, &msg); len(msg) == 0 { - return errors.New("invalid disconnect message") + // The disconnect message is specified as a list containing the reason, + // but some implementations (including older geth) send the reason as a + // single byte. Handle both forms, and on failure include the raw payload + // so the operator can see what was actually sent. + reason, decErr := decodeRLPxDisconnect(data) + if decErr != nil { + return fmt.Errorf("invalid disconnect message: %v (raw=0x%x)", decErr, data) } - return fmt.Errorf("received disconnect message: %v", msg[0]) + return fmt.Errorf("received disconnect message: %v", reason) default: return fmt.Errorf("invalid message code %d, expected handshake (code zero) or disconnect (code one)", code) } diff --git a/cmd/devp2p/rlpxcmd_test.go b/cmd/devp2p/rlpxcmd_test.go new file mode 100644 index 0000000000..d7b374c47d --- /dev/null +++ b/cmd/devp2p/rlpxcmd_test.go @@ -0,0 +1,75 @@ +// Copyright 2026 The go-ethereum Authors +// This file is part of go-ethereum. +// +// go-ethereum is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// go-ethereum 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with go-ethereum. If not, see . + +package main + +import ( + "testing" + + "github.com/ethereum/go-ethereum/p2p" +) + +func TestDecodeRLPxDisconnect(t *testing.T) { + tests := []struct { + name string + payload []byte + want p2p.DiscReason + wantErr bool + }{ + { + name: "list form (spec-compliant)", + payload: []byte{0xc1, 0x04}, // [4] = TooManyPeers + want: p2p.DiscTooManyPeers, + }, + { + name: "list form with reason zero", + payload: []byte{0xc1, 0x80}, // [0] = Requested + want: p2p.DiscRequested, + }, + { + name: "bare byte form (legacy geth)", + payload: []byte{0x04}, // 4 = TooManyPeers + want: p2p.DiscTooManyPeers, + }, + { + name: "bare byte form zero", + payload: []byte{0x80}, // 0 = Requested + want: p2p.DiscRequested, + }, + { + name: "empty payload", + payload: []byte{}, + wantErr: true, + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + got, err := decodeRLPxDisconnect(tc.payload) + if tc.wantErr { + if err == nil { + t.Fatalf("expected error, got reason=%v", got) + } + return + } + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if got != tc.want { + t.Fatalf("got reason %v, want %v", got, tc.want) + } + }) + } +} From 2ca74d2ef9314c18a70e396858ddc6dc7de2350f Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Wed, 22 Apr 2026 17:55:43 +0000 Subject: [PATCH 18/80] eth/fetcher: lazy-allocate hashes slice in scheduleFetches scheduleFetches.func1 is the single biggest allocator in the Pyroscope profile of a busy node (~13.5 GB/hr, 8% of total alloc_space). Each peer-iteration pre-allocated 'make([]common.Hash, 0, maxTxRetrievals)' = 8 KB, even for peers that end up collecting no new hashes (all their announces were already being fetched by someone else). Defer the slice allocation to the first append. Peers that collect zero hashes now pay zero allocation, which is the common case on the timeoutTrigger path where all peers with any announces are iterated. New benchmarks BenchmarkScheduleFetches_{100peers_10new, 100peers_allFetching, 500peers_3new} (benchstat, 6 samples): scenario ns/op B/op allocs/op 100p/10new unchanged unchanged unchanged (fast path) 100p/allFetching -62% -92% -20% 500p/3new -22% -44% -7% geomean -33% -65% -9% --- eth/fetcher/tx_fetcher.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/eth/fetcher/tx_fetcher.go b/eth/fetcher/tx_fetcher.go index 5817dfbcf5..2165b7f103 100644 --- a/eth/fetcher/tx_fetcher.go +++ b/eth/fetcher/tx_fetcher.go @@ -991,8 +991,10 @@ func (f *TxFetcher) scheduleFetches(timer *mclock.Timer, timeout chan struct{}, if len(f.announces[peer]) == 0 { return // continue in the for-each } + // hashes is allocated lazily: peers that collect no new hashes + // (all announces already being fetched) skip the 8KB allocation. var ( - hashes = make([]common.Hash, 0, maxTxRetrievals) + hashes []common.Hash bytes uint64 ) f.forEachAnnounce(f.announces[peer], func(hash common.Hash, meta txMetadata) bool { @@ -1009,6 +1011,9 @@ func (f *TxFetcher) scheduleFetches(timer *mclock.Timer, timeout chan struct{}, f.alternates[hash] = f.announced[hash] delete(f.announced, hash) + if hashes == nil { + hashes = make([]common.Hash, 0, maxTxRetrievals) + } // Accumulate the hash and stop if the limit was reached hashes = append(hashes, hash) if len(hashes) >= maxTxRetrievals { From 526ad4f6f1dc79d36ae8a7bd75a3ff18ac8a0a60 Mon Sep 17 00:00:00 2001 From: Bosul Mun Date: Thu, 23 Apr 2026 15:39:07 +0200 Subject: [PATCH 19/80] crypto/kzg4844: add cell-related functions (#34766) This PR adds three cell-level kzg functions required for the sparse blobpool (eth/72). - VerifyCells: Verifies cells corresponding to proofs. This is used to verify cells received from eth/72 peers. - ComputeCells: Computes cells from blobs. This is needed because user submissions and eth/71 transaction deliveries contain blobs, while eth/72 peers expect cells. - RecoverBlobs: Recovers blobs from partial cells. This is needed to support both eth/71 and eth/72 --------- Co-authored-by: Felix Lange --- crypto/kzg4844/kzg4844.go | 92 ++++++++- crypto/kzg4844/kzg4844_ckzg_cgo.go | 89 +++++++++ crypto/kzg4844/kzg4844_ckzg_nocgo.go | 12 ++ crypto/kzg4844/kzg4844_gokzg.go | 82 ++++++++ crypto/kzg4844/kzg4844_test.go | 269 ++++++++++++++++++++++----- 5 files changed, 499 insertions(+), 45 deletions(-) diff --git a/crypto/kzg4844/kzg4844.go b/crypto/kzg4844/kzg4844.go index 3ccc204838..1e021d64a6 100644 --- a/crypto/kzg4844/kzg4844.go +++ b/crypto/kzg4844/kzg4844.go @@ -34,9 +34,27 @@ var ( blobT = reflect.TypeFor[Blob]() commitmentT = reflect.TypeFor[Commitment]() proofT = reflect.TypeFor[Proof]() + cellT = reflect.TypeFor[Cell]() ) -const CellProofsPerBlob = 128 +const ( + CellProofsPerBlob = 128 + CellsPerBlob = 128 + DataPerBlob = 64 +) + +// Cell represents a single cell in a blob. +type Cell [2048]byte + +// UnmarshalJSON parses a cell in hex syntax. +func (c *Cell) UnmarshalJSON(input []byte) error { + return hexutil.UnmarshalFixedJSON(cellT, input, c[:]) +} + +// MarshalText returns the hex representation of c. +func (c *Cell) MarshalText() ([]byte, error) { + return hexutil.Bytes(c[:]).MarshalText() +} // Blob represents a 4844 data blob. type Blob [131072]byte @@ -189,3 +207,75 @@ func CalcBlobHashV1(hasher hash.Hash, commit *Commitment) (vh [32]byte) { func IsValidVersionedHash(h []byte) bool { return len(h) == 32 && h[0] == 0x01 } + +// VerifyCells verifies a batch of proofs corresponding to the cells and blob commitments. +// +// For this function, it is sufficient to only provide some of the cells. +// +// The `cellIndices` specify which of the 128 cells of each blob are given. +// Indices must be given in ascending order. +// +// Note the list of indices is shared among all blobs, i.e. for a given list of indices +// [1, 2, 13], the cells slice must contain cells [1, 2, 13] of each blob. +// Thus, `len(cells)` must be a multiple of `len(cellIndices)`. +// +// One proof must be given for each cell. As such, `len(proofs)` must equal `len(cells)`. +func VerifyCells(cells []Cell, commitments []Commitment, proofs []Proof, cellIndices []uint64) error { + // commitments/proofs/cells validation + switch { + case len(commitments) == 0: + return errors.New("no commitments") + case len(proofs)%len(commitments) != 0: + return errors.New("len(proofs) must be a multiple of len(commitments)") + case len(cells) != len(proofs): + return errors.New("mismatched len(cellProofs) and len(cells)") + } + if err := validateCellIndices(cells, cellIndices); err != nil { + return err + } + if len(cells)/len(cellIndices) != len(commitments) { + return errors.New("invalid number of cells for blob count") + } + + if useCKZG.Load() { + return ckzgVerifyCells(cells, commitments, proofs, cellIndices) + } + return gokzgVerifyCells(cells, commitments, proofs, cellIndices) +} + +// ComputeCells computes the cells from the given blobs. +func ComputeCells(blobs []Blob) ([]Cell, error) { + if useCKZG.Load() { + return ckzgComputeCells(blobs) + } + return gokzgComputeCells(blobs) +} + +// RecoverBlobs recovers blobs from the given cells and cell indices. +// In order to successfully recover, at least DataPerBlob (64) cells must be provided. +// +// For the layout of cells and cellIndices, please see [VerifyCells]. +func RecoverBlobs(cells []Cell, cellIndices []uint64) ([]Blob, error) { + if err := validateCellIndices(cells, cellIndices); err != nil { + return nil, err + } + if useCKZG.Load() { + return ckzgRecoverBlobs(cells, cellIndices) + } + return gokzgRecoverBlobs(cells, cellIndices) +} + +func validateCellIndices(cells []Cell, cellIndices []uint64) error { + switch { + case len(cellIndices) == 0: + return errors.New("no cellIndices given") + case len(cellIndices) > len(cells): + return errors.New("less cells than cellIndices") + case len(cellIndices) > CellsPerBlob: + return errors.New("too many cellIndices") + case len(cells)%len(cellIndices) != 0: + return errors.New("len(cells) must be a multiple of len(cellIndices)") + } + // The library checks the canonical ordering of indices, so we don't have to do it here. + return nil +} diff --git a/crypto/kzg4844/kzg4844_ckzg_cgo.go b/crypto/kzg4844/kzg4844_ckzg_cgo.go index 93d5f4ff94..bacfa7095a 100644 --- a/crypto/kzg4844/kzg4844_ckzg_cgo.go +++ b/crypto/kzg4844/kzg4844_ckzg_cgo.go @@ -190,3 +190,92 @@ func ckzgVerifyCellProofBatch(blobs []Blob, commitments []Commitment, cellProofs } return nil } + +// ckzgVerifyCells verifies that the cell data corresponds to the provided commitments. +func ckzgVerifyCells(cells []Cell, commitments []Commitment, cellProofs []Proof, cellIndices []uint64) error { + ckzgIniter.Do(ckzgInit) + var ( + proofs = make([]ckzg4844.Bytes48, len(cellProofs)) + commits = make([]ckzg4844.Bytes48, 0, len(cellProofs)) + indices = make([]uint64, 0, len(cellProofs)) + kzgcells = make([]ckzg4844.Cell, 0, len(cellProofs)) + ) + for i := range cellProofs { + proofs[i] = (ckzg4844.Bytes48)(cellProofs[i]) + kzgcells = append(kzgcells, (ckzg4844.Cell)(cells[i])) + } + if len(cellProofs)%len(commitments) != 0 { + return errors.New("wrong cell proofs and commitments length") + } + cellCounts := len(cellProofs) / len(commitments) + for _, commitment := range commitments { + for j := 0; j < cellCounts; j++ { + commits = append(commits, (ckzg4844.Bytes48)(commitment)) + } + } + for j := 0; j < len(commitments); j++ { + indices = append(indices, cellIndices...) + } + + valid, err := ckzg4844.VerifyCellKZGProofBatch(commits, indices, kzgcells, proofs) + if err != nil { + return err + } + if !valid { + return errors.New("invalid proof") + } + return nil +} + +// ckzgComputeCells computes cells from blobs. +func ckzgComputeCells(blobs []Blob) ([]Cell, error) { + ckzgIniter.Do(ckzgInit) + cells := make([]Cell, 0, ckzg4844.CellsPerExtBlob*len(blobs)) + + for i := range blobs { + cellsI, err := ckzg4844.ComputeCells((*ckzg4844.Blob)(&blobs[i])) + if err != nil { + return []Cell{}, err + } + for _, c := range cellsI { + cells = append(cells, Cell(c)) + } + } + return cells, nil +} + +// ckzgRecoverBlobs recovers blobs from cells and cell indices. +func ckzgRecoverBlobs(cells []Cell, cellIndices []uint64) ([]Blob, error) { + ckzgIniter.Do(ckzgInit) + + if len(cellIndices) == 0 || len(cells)%len(cellIndices) != 0 { + return []Blob{}, errors.New("cells with wrong length") + } + + blobCount := len(cells) / len(cellIndices) + blobs := make([]Blob, 0, blobCount) + + offset := 0 + for range blobCount { + kzgcells := make([]ckzg4844.Cell, 0, len(cellIndices)) + + for _, cell := range cells[offset : offset+len(cellIndices)] { + kzgcells = append(kzgcells, ckzg4844.Cell(cell)) + } + + extCells, err := ckzg4844.RecoverCells(cellIndices, kzgcells) + if err != nil { + return []Blob{}, err + } + + var blob Blob + for i, cell := range extCells[:DataPerBlob] { + copy(blob[i*len(cell):], cell[:]) + } + blobs = append(blobs, blob) + + offset = offset + len(cellIndices) + } + + return blobs, nil +} diff --git a/crypto/kzg4844/kzg4844_ckzg_nocgo.go b/crypto/kzg4844/kzg4844_ckzg_nocgo.go index 7c552e9a18..e1a3c0af1e 100644 --- a/crypto/kzg4844/kzg4844_ckzg_nocgo.go +++ b/crypto/kzg4844/kzg4844_ckzg_nocgo.go @@ -73,3 +73,15 @@ func ckzgVerifyCellProofBatch(blobs []Blob, commitments []Commitment, proof []Pr func ckzgComputeCellProofs(blob *Blob) ([]Proof, error) { panic("unsupported platform") } + +func ckzgVerifyCells(cells []Cell, commitments []Commitment, cellProofs []Proof, cellIndices []uint64) error { + panic("unsupported platform") +} + +func ckzgComputeCells(blobs []Blob) ([]Cell, error) { + panic("unsupported platform") +} + +func ckzgRecoverBlobs(cells []Cell, cellIndices []uint64) ([]Blob, error) { + panic("unsupported platform") +} diff --git a/crypto/kzg4844/kzg4844_gokzg.go b/crypto/kzg4844/kzg4844_gokzg.go index 03627ebafb..ca45a6a560 100644 --- a/crypto/kzg4844/kzg4844_gokzg.go +++ b/crypto/kzg4844/kzg4844_gokzg.go @@ -148,3 +148,85 @@ func gokzgVerifyCellProofBatch(blobs []Blob, commitments []Commitment, cellProof } return context.VerifyCellKZGProofBatch(commits, cellIndices, cells[:], proofs) } + +// gokzgVerifyCells verifies that the cell data corresponds to the provided commitment. +func gokzgVerifyCells(cells []Cell, commitments []Commitment, cellProofs []Proof, cellIndices []uint64) error { + gokzgIniter.Do(gokzgInit) + + var ( + proofs = make([]gokzg4844.KZGProof, len(cellProofs)) + commits = make([]gokzg4844.KZGCommitment, 0, len(cellProofs)) + indices = make([]uint64, 0, len(cellProofs)) + kzgcells = make([]*gokzg4844.Cell, 0, len(cellProofs)) + ) + // Copy over the cell proofs and cells + for i := range cellProofs { + proofs[i] = gokzg4844.KZGProof(cellProofs[i]) + gc := gokzg4844.Cell(cells[i]) + kzgcells = append(kzgcells, &gc) + } + cellCounts := len(cellProofs) / len(commitments) + // Blow up the commitments to be the same length as the proofs + for _, commitment := range commitments { + for j := 0; j < cellCounts; j++ { + commits = append(commits, gokzg4844.KZGCommitment(commitment)) + } + } + for j := 0; j < len(commitments); j++ { + indices = append(indices, cellIndices...) + } + + return context.VerifyCellKZGProofBatch(commits, indices, kzgcells, proofs) +} + +// gokzgComputeCells computes cells from blobs. +func gokzgComputeCells(blobs []Blob) ([]Cell, error) { + gokzgIniter.Do(gokzgInit) + cells := make([]Cell, 0, gokzg4844.CellsPerExtBlob*len(blobs)) + + for i := range blobs { + cellsI, err := context.ComputeCells((*gokzg4844.Blob)(&blobs[i]), 2) + if err != nil { + return []Cell{}, err + } + for _, c := range cellsI { + if c != nil { + cells = append(cells, Cell(*c)) + } + } + } + return cells, nil +} + +// gokzgRecoverBlobs recovers blobs from cells and cell indices. +func gokzgRecoverBlobs(cells []Cell, cellIndices []uint64) ([]Blob, error) { + gokzgIniter.Do(gokzgInit) + + blobCount := len(cells) / len(cellIndices) + blobs := make([]Blob, 0, blobCount) + + offset := 0 + for range blobCount { + kzgcells := make([]*gokzg4844.Cell, 0, len(cellIndices)) + + for _, cell := range cells[offset : offset+len(cellIndices)] { + gc := gokzg4844.Cell(cell) + kzgcells = append(kzgcells, &gc) + } + + extCells, err := context.RecoverCells(cellIndices, kzgcells, 2) + if err != nil { + return []Blob{}, err + } + + var blob Blob + for i, cell := range extCells[:DataPerBlob] { + copy(blob[i*len(cell):], cell[:]) + } + blobs = append(blobs, blob) + + offset = offset + len(cellIndices) + } + + return blobs, nil +} diff --git a/crypto/kzg4844/kzg4844_test.go b/crypto/kzg4844/kzg4844_test.go index 743a277199..056decfb8b 100644 --- a/crypto/kzg4844/kzg4844_test.go +++ b/crypto/kzg4844/kzg4844_test.go @@ -18,6 +18,8 @@ package kzg4844 import ( "crypto/rand" + mrand "math/rand" + "slices" "testing" "github.com/consensys/gnark-crypto/ecc/bls12-381/fr" @@ -45,14 +47,20 @@ func randBlob() *Blob { return &blob } -func TestCKZGWithPoint(t *testing.T) { testKZGWithPoint(t, true) } -func TestGoKZGWithPoint(t *testing.T) { testKZGWithPoint(t, false) } -func testKZGWithPoint(t *testing.T, ckzg bool) { +func switchBackend(t testing.TB, ckzg bool) (switchBack func()) { + t.Helper() if ckzg && !ckzgAvailable { t.Skip("CKZG unavailable in this test build") } - defer func(old bool) { useCKZG.Store(old) }(useCKZG.Load()) + prev := useCKZG.Load() useCKZG.Store(ckzg) + return func() { useCKZG.Store(prev) } +} + +func TestCKZGWithPoint(t *testing.T) { testKZGWithPoint(t, true) } +func TestGoKZGWithPoint(t *testing.T) { testKZGWithPoint(t, false) } +func testKZGWithPoint(t *testing.T, ckzg bool) { + defer switchBackend(t, ckzg)() blob := randBlob() @@ -73,11 +81,7 @@ func testKZGWithPoint(t *testing.T, ckzg bool) { func TestCKZGWithBlob(t *testing.T) { testKZGWithBlob(t, true) } func TestGoKZGWithBlob(t *testing.T) { testKZGWithBlob(t, false) } func testKZGWithBlob(t *testing.T, ckzg bool) { - if ckzg && !ckzgAvailable { - t.Skip("CKZG unavailable in this test build") - } - defer func(old bool) { useCKZG.Store(old) }(useCKZG.Load()) - useCKZG.Store(ckzg) + defer switchBackend(t, ckzg)() blob := randBlob() @@ -97,11 +101,7 @@ func testKZGWithBlob(t *testing.T, ckzg bool) { func BenchmarkCKZGBlobToCommitment(b *testing.B) { benchmarkBlobToCommitment(b, true) } func BenchmarkGoKZGBlobToCommitment(b *testing.B) { benchmarkBlobToCommitment(b, false) } func benchmarkBlobToCommitment(b *testing.B, ckzg bool) { - if ckzg && !ckzgAvailable { - b.Skip("CKZG unavailable in this test build") - } - defer func(old bool) { useCKZG.Store(old) }(useCKZG.Load()) - useCKZG.Store(ckzg) + defer switchBackend(b, ckzg)() blob := randBlob() @@ -113,11 +113,7 @@ func benchmarkBlobToCommitment(b *testing.B, ckzg bool) { func BenchmarkCKZGComputeProof(b *testing.B) { benchmarkComputeProof(b, true) } func BenchmarkGoKZGComputeProof(b *testing.B) { benchmarkComputeProof(b, false) } func benchmarkComputeProof(b *testing.B, ckzg bool) { - if ckzg && !ckzgAvailable { - b.Skip("CKZG unavailable in this test build") - } - defer func(old bool) { useCKZG.Store(old) }(useCKZG.Load()) - useCKZG.Store(ckzg) + defer switchBackend(b, ckzg)() var ( blob = randBlob() @@ -132,11 +128,7 @@ func benchmarkComputeProof(b *testing.B, ckzg bool) { func BenchmarkCKZGVerifyProof(b *testing.B) { benchmarkVerifyProof(b, true) } func BenchmarkGoKZGVerifyProof(b *testing.B) { benchmarkVerifyProof(b, false) } func benchmarkVerifyProof(b *testing.B, ckzg bool) { - if ckzg && !ckzgAvailable { - b.Skip("CKZG unavailable in this test build") - } - defer func(old bool) { useCKZG.Store(old) }(useCKZG.Load()) - useCKZG.Store(ckzg) + defer switchBackend(b, ckzg)() var ( blob = randBlob() @@ -153,11 +145,7 @@ func benchmarkVerifyProof(b *testing.B, ckzg bool) { func BenchmarkCKZGComputeBlobProof(b *testing.B) { benchmarkComputeBlobProof(b, true) } func BenchmarkGoKZGComputeBlobProof(b *testing.B) { benchmarkComputeBlobProof(b, false) } func benchmarkComputeBlobProof(b *testing.B, ckzg bool) { - if ckzg && !ckzgAvailable { - b.Skip("CKZG unavailable in this test build") - } - defer func(old bool) { useCKZG.Store(old) }(useCKZG.Load()) - useCKZG.Store(ckzg) + defer switchBackend(b, ckzg)() var ( blob = randBlob() @@ -172,11 +160,7 @@ func benchmarkComputeBlobProof(b *testing.B, ckzg bool) { func BenchmarkCKZGVerifyBlobProof(b *testing.B) { benchmarkVerifyBlobProof(b, true) } func BenchmarkGoKZGVerifyBlobProof(b *testing.B) { benchmarkVerifyBlobProof(b, false) } func benchmarkVerifyBlobProof(b *testing.B, ckzg bool) { - if ckzg && !ckzgAvailable { - b.Skip("CKZG unavailable in this test build") - } - defer func(old bool) { useCKZG.Store(old) }(useCKZG.Load()) - useCKZG.Store(ckzg) + defer switchBackend(b, ckzg)() var ( blob = randBlob() @@ -192,11 +176,7 @@ func benchmarkVerifyBlobProof(b *testing.B, ckzg bool) { func TestCKZGCells(t *testing.T) { testKZGCells(t, true) } func TestGoKZGCells(t *testing.T) { testKZGCells(t, false) } func testKZGCells(t *testing.T, ckzg bool) { - if ckzg && !ckzgAvailable { - t.Skip("CKZG unavailable in this test build") - } - defer func(old bool) { useCKZG.Store(old) }(useCKZG.Load()) - useCKZG.Store(ckzg) + defer switchBackend(t, ckzg)() blob1 := randBlob() blob2 := randBlob() @@ -236,11 +216,7 @@ func BenchmarkGOKZGComputeCellProofs(b *testing.B) { benchmarkComputeCellProofs( func BenchmarkCKZGComputeCellProofs(b *testing.B) { benchmarkComputeCellProofs(b, true) } func benchmarkComputeCellProofs(b *testing.B, ckzg bool) { - if ckzg && !ckzgAvailable { - b.Skip("CKZG unavailable in this test build") - } - defer func(old bool) { useCKZG.Store(old) }(useCKZG.Load()) - useCKZG.Store(ckzg) + defer switchBackend(b, ckzg)() blob := randBlob() _, _ = ComputeCellProofs(blob) // for kzg initialization @@ -253,3 +229,208 @@ func benchmarkComputeCellProofs(b *testing.B, ckzg bool) { } } } + +// randCellIndices picks n random unique indices from [0, CellsPerBlob) in sorted order. +func randCellIndices(rng *mrand.Rand, n int) []uint64 { + perm := rng.Perm(CellsPerBlob) + indices := make([]uint64, n) + for i := 0; i < n; i++ { + indices[i] = uint64(perm[i]) + } + slices.Sort(indices) + return indices +} + +// randBlobAndProofs generates random blobs and precomputes their cells, proofs, and commitments. +type randBlobAndProofs struct { + blobs []Blob + commitments []Commitment + cells []Cell // flat: blobs[i] cells at [i*CellsPerBlob : (i+1)*CellsPerBlob] + proofs []Proof +} + +func newBlobs(t *testing.T, blobCount int) *randBlobAndProofs { + d := &randBlobAndProofs{ + blobs: make([]Blob, blobCount), + commitments: make([]Commitment, blobCount), + } + for i := range blobCount { + d.blobs[i] = *randBlob() + commitment, err := BlobToCommitment(&d.blobs[i]) + if err != nil { + t.Fatalf("failed to compute commitment: %v", err) + } + d.commitments[i] = commitment + proofs, err := ComputeCellProofs(&d.blobs[i]) + if err != nil { + t.Fatalf("failed to compute cell proofs: %v", err) + } + d.proofs = append(d.proofs, proofs...) + } + cells, err := ComputeCells(d.blobs) + if err != nil { + t.Fatalf("failed to compute cells: %v", err) + } + d.cells = cells + return d +} + +func TestCKZGVerifyPartialCells(t *testing.T) { testVerifyPartialCells(t, true) } +func TestGoKZGVerifyPartialCells(t *testing.T) { testVerifyPartialCells(t, false) } + +func testVerifyPartialCells(t *testing.T, ckzg bool) { + defer switchBackend(t, ckzg)() + + const ( + iterations = 50 + blobCount = 3 + cellsCount = 8 + ) + // Precompute blobs once, vary only cell indices per iteration + d := newBlobs(t, blobCount) + + for iter := range iterations { + rng := mrand.New(mrand.NewSource(int64(iter))) + indices := randCellIndices(rng, cellsCount) + + var partialCells []Cell + var partialProofs []Proof + for i := range blobCount { + for _, idx := range indices { + partialCells = append(partialCells, d.cells[i*CellsPerBlob+int(idx)]) + partialProofs = append(partialProofs, d.proofs[i*CellProofsPerBlob+int(idx)]) + } + } + if err := VerifyCells(partialCells, d.commitments, partialProofs, indices); err != nil { + t.Fatalf("iter %d: failed to verify partial cells: %v", iter, err) + } + } +} + +func TestCKZGVerifyCellsWithCorruptedCells(t *testing.T) { + testVerifyCellsWithCorruptedCells(t, true) +} +func TestGoKZGVerifyCellsWithCorruptedCells(t *testing.T) { + testVerifyCellsWithCorruptedCells(t, false) +} + +func testVerifyCellsWithCorruptedCells(t *testing.T, ckzg bool) { + defer switchBackend(t, ckzg)() + + const blobCount = 3 + d := newBlobs(t, blobCount) + indices := []uint64{0, 15, 63, 64, 95, 100, 120, 127} + + var partialCells []Cell + var partialProofs []Proof + for i := range blobCount { + for _, idx := range indices { + partialCells = append(partialCells, d.cells[i*CellsPerBlob+int(idx)]) + partialProofs = append(partialProofs, d.proofs[i*CellProofsPerBlob+int(idx)]) + } + } + // Corrupt the first cell + corruptedCells := make([]Cell, len(partialCells)) + copy(corruptedCells, partialCells) + corruptedCells[0][0] ^= 0xff + + if err := VerifyCells(corruptedCells, d.commitments, partialProofs, indices); err == nil { + t.Fatal("expected verification failure with corrupted cell") + } +} + +func TestCKZGVerifyCellsWithCorruptedProofs(t *testing.T) { + testVerifyCellsWithCorruptedProofs(t, true) +} +func TestGoKZGVerifyCellsWithCorruptedProofs(t *testing.T) { + testVerifyCellsWithCorruptedProofs(t, false) +} + +func testVerifyCellsWithCorruptedProofs(t *testing.T, ckzg bool) { + defer switchBackend(t, ckzg)() + + const blobCount = 3 + d := newBlobs(t, blobCount) + indices := []uint64{0, 15, 63, 64, 95, 100, 120, 127} + + var partialCells []Cell + var partialProofs []Proof + for i := range blobCount { + for _, idx := range indices { + partialCells = append(partialCells, d.cells[i*CellsPerBlob+int(idx)]) + partialProofs = append(partialProofs, d.proofs[i*CellProofsPerBlob+int(idx)]) + } + } + // Swap first and last proof + wrongProofs := make([]Proof, len(partialProofs)) + copy(wrongProofs, partialProofs) + wrongProofs[0], wrongProofs[len(wrongProofs)-1] = wrongProofs[len(wrongProofs)-1], wrongProofs[0] + + if err := VerifyCells(partialCells, d.commitments, wrongProofs, indices); err == nil { + t.Fatal("expected verification failure with swapped proofs") + } +} + +func TestCKZGRecoverBlob(t *testing.T) { testRecoverBlob(t, true) } +func TestGoKZGRecoverBlob(t *testing.T) { testRecoverBlob(t, false) } + +func testRecoverBlob(t *testing.T, ckzg bool) { + defer switchBackend(t, ckzg)() + + // Precompute blobs once, vary only cell indices per iteration + d := newBlobs(t, 3) + + for iter := range 50 { + rng := mrand.New(mrand.NewSource(int64(iter))) + numCells := DataPerBlob + rng.Intn(CellsPerBlob-DataPerBlob+1) + indices := randCellIndices(rng, numCells) + + var partialCells []Cell + for bi := range 3 { + for _, idx := range indices { + partialCells = append(partialCells, d.cells[bi*CellsPerBlob+int(idx)]) + } + } + recovered, err := RecoverBlobs(partialCells, indices) + if err != nil { + t.Fatalf("iter %d: failed to recover blob with %d cells: %v", iter, numCells, err) + } + if err := VerifyCellProofs(recovered, d.commitments, d.proofs); err != nil { + t.Fatalf("iter %d: recovered blobs failed verification: %v", iter, err) + } + for i := range d.blobs { + if recovered[i] != d.blobs[i] { + t.Fatalf("iter %d: recovered blob %d does not match original", iter, i) + } + } + } +} + +func TestCKZGRecoverBlobWithInsufficientCells(t *testing.T) { + testRecoverBlobWithInsufficientCells(t, true) +} +func TestGoKZGRecoverBlobWithInsufficientCells(t *testing.T) { + testRecoverBlobWithInsufficientCells(t, false) +} + +func testRecoverBlobWithInsufficientCells(t *testing.T, ckzg bool) { + defer switchBackend(t, ckzg)() + + const blobCount = 3 + d := newBlobs(t, blobCount) + + // Use DataPerBlob-1 cells (one short of minimum required) + indices := make([]uint64, DataPerBlob-1) + for i := range indices { + indices[i] = uint64(i) + } + var partialCells []Cell + for bi := range blobCount { + for _, idx := range indices { + partialCells = append(partialCells, d.cells[bi*CellsPerBlob+int(idx)]) + } + } + if _, err := RecoverBlobs(partialCells, indices); err == nil { + t.Fatalf("expected error with only %d cells, got none", len(indices)) + } +} From 6ece4cd1436fa5ea2380eb819e125ee125efc643 Mon Sep 17 00:00:00 2001 From: cui Date: Fri, 24 Apr 2026 10:02:34 +0800 Subject: [PATCH 20/80] crypto: fix unit test (#34811) --- crypto/crypto_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto/crypto_test.go b/crypto/crypto_test.go index d803ab27c5..0636427a4f 100644 --- a/crypto/crypto_test.go +++ b/crypto/crypto_test.go @@ -293,7 +293,7 @@ func TestPythonIntegration(t *testing.T) { sig0, _ := Sign(msg0, k0) msg1 := common.FromHex("00000000000000000000000000000000") - sig1, _ := Sign(msg0, k0) + sig1, _ := Sign(msg1, k0) t.Logf("msg: %x, privkey: %s sig: %x\n", msg0, kh, sig0) t.Logf("msg: %x, privkey: %s sig: %x\n", msg1, kh, sig1) From 33c1bd59ff23a7b22f8804a6b83833015edfc159 Mon Sep 17 00:00:00 2001 From: YQ Date: Fri, 24 Apr 2026 17:27:39 +0800 Subject: [PATCH 21/80] rpc: send WebSocket close frame on client disconnect (#33909) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When `rpc.Client.Close()` is called, the TCP connection is torn down without sending a WebSocket Close frame. The server sees `websocket: close 1006 (abnormal closure): unexpected EOF` instead of a clean 1000 (normal closure). ### Root cause `websocketCodec.close()` delegates to `jsonCodec.close()` which calls `c.conn.Close()` — gorilla/websocket's `Conn.Close` explicitly "[closes the underlying network connection without sending or waiting for a close message](https://pkg.go.dev/github.com/gorilla/websocket#Conn.Close)" (per RFC 6455). ### Fix Send a WebSocket Close control frame (opcode 0x8, status 1000) before closing the underlying connection. Uses `WriteControl` with the same `encMu` mutex pattern already used by `pingLoop` for write serialization, and reuses the existing `wsPingWriteTimeout` (5s) constant. `WriteControl` errors are safe to ignore — the connection may already be broken by the time we attempt the close frame. Fixes #30482 --- rpc/websocket.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/rpc/websocket.go b/rpc/websocket.go index 543ff617ba..ec676b9caf 100644 --- a/rpc/websocket.go +++ b/rpc/websocket.go @@ -324,6 +324,16 @@ func newWebsocketCodec(conn *websocket.Conn, host string, req http.Header, readL } func (wc *websocketCodec) close() { + // Send a WebSocket Close frame before closing the underlying connection, + // so the server sees a clean 1000 (normal closure) instead of 1006 (abnormal). + wc.jsonCodec.encMu.Lock() + wc.conn.WriteControl( + websocket.CloseMessage, + websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), + time.Now().Add(wsPingWriteTimeout), + ) + wc.jsonCodec.encMu.Unlock() + wc.jsonCodec.close() wc.wg.Wait() } From c876755839b2d6ebffa1c43538c42713e769347d Mon Sep 17 00:00:00 2001 From: Sina M <1591639+s1na@users.noreply.github.com> Date: Fri, 24 Apr 2026 12:12:26 +0200 Subject: [PATCH 22/80] Update eth/fetcher/tx_fetcher.go Co-authored-by: jwasinger --- eth/fetcher/tx_fetcher.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/eth/fetcher/tx_fetcher.go b/eth/fetcher/tx_fetcher.go index 2165b7f103..20621c531d 100644 --- a/eth/fetcher/tx_fetcher.go +++ b/eth/fetcher/tx_fetcher.go @@ -991,8 +991,6 @@ func (f *TxFetcher) scheduleFetches(timer *mclock.Timer, timeout chan struct{}, if len(f.announces[peer]) == 0 { return // continue in the for-each } - // hashes is allocated lazily: peers that collect no new hashes - // (all announces already being fetched) skip the 8KB allocation. var ( hashes []common.Hash bytes uint64 From 8091994e7b5954827ad68ccca463647b220b1621 Mon Sep 17 00:00:00 2001 From: rayoo Date: Fri, 24 Apr 2026 19:37:34 +0800 Subject: [PATCH 23/80] eth/protocols/snap: fix data race on testPeer counters (#34802) The testPeer request counters (nAccountRequests, nStorageRequests, nBytecodeRequests, nTrienodeRequests) were plain int fields incremented with ++. These increments happen in Request* methods that are invoked concurrently by the Syncer from multiple goroutines (assignBytecodeTasks, assignStorageTasks, etc.), causing a data race reliably detected by go test -race. Change the counters to atomic.Int64 so increments and reads are synchronized without introducing a mutex. Fixes races detected in TestMultiSyncManyUseless, TestMultiSyncManyUselessWithLowTimeout, TestMultiSyncManyUnresponsive, TestSyncWithStorageAndOneCappedPeer, TestSyncWithStorageAndCorruptPeer, and TestSyncWithStorageAndNonProvingPeer. --- eth/protocols/snap/sync_test.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/eth/protocols/snap/sync_test.go b/eth/protocols/snap/sync_test.go index b11ad4e78a..c506488e91 100644 --- a/eth/protocols/snap/sync_test.go +++ b/eth/protocols/snap/sync_test.go @@ -25,6 +25,7 @@ import ( mrand "math/rand" "slices" "sync" + "sync/atomic" "testing" "time" @@ -142,10 +143,10 @@ type testPeer struct { term func() // counters - nAccountRequests int - nStorageRequests int - nBytecodeRequests int - nTrienodeRequests int + nAccountRequests atomic.Int64 + nStorageRequests atomic.Int64 + nBytecodeRequests atomic.Int64 + nTrienodeRequests atomic.Int64 } func newTestPeer(id string, t *testing.T, term func()) *testPeer { @@ -179,25 +180,25 @@ func (t *testPeer) Stats() string { Storage requests: %d Bytecode requests: %d Trienode requests: %d -`, t.nAccountRequests, t.nStorageRequests, t.nBytecodeRequests, t.nTrienodeRequests) +`, t.nAccountRequests.Load(), t.nStorageRequests.Load(), t.nBytecodeRequests.Load(), t.nTrienodeRequests.Load()) } func (t *testPeer) RequestAccountRange(id uint64, root, origin, limit common.Hash, bytes int) error { t.logger.Trace("Fetching range of accounts", "reqid", id, "root", root, "origin", origin, "limit", limit, "bytes", common.StorageSize(bytes)) - t.nAccountRequests++ + t.nAccountRequests.Add(1) go t.accountRequestHandler(t, id, root, origin, limit, bytes) return nil } func (t *testPeer) RequestTrieNodes(id uint64, root common.Hash, count int, paths []TrieNodePathSet, bytes int) error { t.logger.Trace("Fetching set of trie nodes", "reqid", id, "root", root, "pathsets", len(paths), "bytes", common.StorageSize(bytes)) - t.nTrienodeRequests++ + t.nTrienodeRequests.Add(1) go t.trieRequestHandler(t, id, root, paths, bytes) return nil } func (t *testPeer) RequestStorageRanges(id uint64, root common.Hash, accounts []common.Hash, origin, limit []byte, bytes int) error { - t.nStorageRequests++ + t.nStorageRequests.Add(1) if len(accounts) == 1 && origin != nil { t.logger.Trace("Fetching range of large storage slots", "reqid", id, "root", root, "account", accounts[0], "origin", common.BytesToHash(origin), "limit", common.BytesToHash(limit), "bytes", common.StorageSize(bytes)) } else { @@ -208,7 +209,7 @@ func (t *testPeer) RequestStorageRanges(id uint64, root common.Hash, accounts [] } func (t *testPeer) RequestByteCodes(id uint64, hashes []common.Hash, bytes int) error { - t.nBytecodeRequests++ + t.nBytecodeRequests.Add(1) t.logger.Trace("Fetching set of byte codes", "reqid", id, "hashes", len(hashes), "bytes", common.StorageSize(bytes)) go t.codeRequestHandler(t, id, hashes, bytes) return nil @@ -1901,7 +1902,7 @@ func testSyncAccountPerformance(t *testing.T, scheme string) { // sync cycle starts. When popping the queue, we do not look it up again. // Doing so would bring this number down to zero in this artificial testcase, // but only add extra IO for no reason in practice. - if have, want := src.nTrienodeRequests, 1; have != want { + if have, want := src.nTrienodeRequests.Load(), int64(1); have != want { fmt.Print(src.Stats()) t.Errorf("trie node heal requests wrong, want %d, have %d", want, have) } From b70d9a4b8e409640d5f77af860864e1369dc87fd Mon Sep 17 00:00:00 2001 From: rayoo Date: Fri, 24 Apr 2026 23:30:03 +0800 Subject: [PATCH 24/80] core/state,core/types/bal: copy stateReadList in StateDB.Copy The stateReadList field introduced by #34776 to track the state access footprint for EIP-7928 was not propagated by StateDB.Copy. Every other per-transaction field that lives alongside it (accessList, transientStorage, journal, witness, accessEvents) is copied explicitly, so this field was simply missed. After Copy the copy's stateReadList is nil while the original keeps its entries, so the nil-safe guards on StateAccessList.AddAccount / AddState silently drop every access recorded on the copy. For any post-Amsterdam code path that copies a prepared state and keeps reading from the copy, the BAL footprint becomes incomplete. Add a Copy method on bal.StateAccessList and invoke it from StateDB.Copy, matching the pattern used for accessList and accessEvents. --------- Co-authored-by: jwasinger --- core/state/statedb.go | 3 +++ core/types/bal/access_list.go | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/core/state/statedb.go b/core/state/statedb.go index 5d94d4806d..1858f4758d 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -716,6 +716,9 @@ func (s *StateDB) Copy() *StateDB { if s.accessEvents != nil { state.accessEvents = s.accessEvents.Copy() } + if s.stateReadList != nil { + state.stateReadList = s.stateReadList.Copy() + } // Deep copy cached state objects. for addr, obj := range s.stateObjects { state.stateObjects[addr] = obj.deepCopy(state) diff --git a/core/types/bal/access_list.go b/core/types/bal/access_list.go index 91da5ebcb7..e563fa22e2 100644 --- a/core/types/bal/access_list.go +++ b/core/types/bal/access_list.go @@ -78,3 +78,17 @@ func (s *StateAccessList) Merge(other *StateAccessList) { maps.Copy(slots, otherSlots) } } + +// Copy returns a deep copy of the StateAccessList. +func (s *StateAccessList) Copy() *StateAccessList { + if s == nil { + return nil + } + cpy := &StateAccessList{ + list: make(map[common.Address]StorageAccessList, len(s.list)), + } + for addr, slots := range s.list { + cpy.list[addr] = maps.Clone(slots) + } + return cpy +} From b26391773d7cec44dcc52326b5fbfae9cc63f573 Mon Sep 17 00:00:00 2001 From: cui Date: Sun, 26 Apr 2026 17:54:07 +0800 Subject: [PATCH 25/80] core/state: and instead of or (#34819) --- core/state/snapshot/iterator_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/state/snapshot/iterator_test.go b/core/state/snapshot/iterator_test.go index dd6c4cf968..a95bd66dde 100644 --- a/core/state/snapshot/iterator_test.go +++ b/core/state/snapshot/iterator_test.go @@ -342,7 +342,7 @@ func TestAccountIteratorTraversalValues(t *testing.T) { if i%8 == 0 { e[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 4, i) } - if i > 50 || i < 85 { + if i > 50 && i < 85 { f[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 5, i) } if i%64 == 0 { From 2d5da603717417ffe63d238a92a8cd9dc4e02745 Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Sun, 26 Apr 2026 23:32:39 +0800 Subject: [PATCH 26/80] core/types/bal: update the BAL definition to the latest spec (#34799) This PR updates the BAL structure definition to the latest the spec, - Balance has been changed from [16]byte to uint256 - Storage key and value has been changed from [32]byte to uint256 - BlockAccessList has been changed from a struct to a slice of AccountChanges - TxIndex has been changed from uint16 to uint32 --- core/types/bal/bal.go | 34 +- core/types/bal/bal_encoding.go | 210 ++++++----- core/types/bal/bal_encoding_rlp_generated.go | 358 +++++++++---------- core/types/bal/bal_test.go | 97 ++--- eth/protocols/snap/handler_test.go | 12 +- 5 files changed, 378 insertions(+), 333 deletions(-) diff --git a/core/types/bal/bal.go b/core/types/bal/bal.go index 86dc8e5426..99ead8d6f0 100644 --- a/core/types/bal/bal.go +++ b/core/types/bal/bal.go @@ -25,13 +25,13 @@ import ( ) // ConstructionAccountAccess contains post-block account state for mutations as well as -// all storage keys that were read during execution. It is used when building block +// all storage keys that were read during execution. It is used when building block // access list during execution. type ConstructionAccountAccess struct { // StorageWrites is the post-state values of an account's storage slots // that were modified in a block, keyed by the slot key and the tx index // where the modification occurred. - StorageWrites map[common.Hash]map[uint16]common.Hash `json:"storageWrites,omitempty"` + StorageWrites map[common.Hash]map[uint32]common.Hash `json:"storageWrites,omitempty"` // StorageReads is the set of slot keys that were accessed during block // execution. @@ -42,25 +42,25 @@ type ConstructionAccountAccess struct { // BalanceChanges contains the post-transaction balances of an account, // keyed by transaction indices where it was changed. - BalanceChanges map[uint16]*uint256.Int `json:"balanceChanges,omitempty"` + BalanceChanges map[uint32]*uint256.Int `json:"balanceChanges,omitempty"` // NonceChanges contains the post-state nonce values of an account keyed // by tx index. - NonceChanges map[uint16]uint64 `json:"nonceChanges,omitempty"` + NonceChanges map[uint32]uint64 `json:"nonceChanges,omitempty"` // CodeChange contains the post-state contract code of an account keyed // by tx index. - CodeChange map[uint16][]byte `json:"codeChange,omitempty"` + CodeChange map[uint32][]byte `json:"codeChange,omitempty"` } // NewConstructionAccountAccess initializes the account access object. func NewConstructionAccountAccess() *ConstructionAccountAccess { return &ConstructionAccountAccess{ - StorageWrites: make(map[common.Hash]map[uint16]common.Hash), + StorageWrites: make(map[common.Hash]map[uint32]common.Hash), StorageReads: make(map[common.Hash]struct{}), - BalanceChanges: make(map[uint16]*uint256.Int), - NonceChanges: make(map[uint16]uint64), - CodeChange: make(map[uint16][]byte), + BalanceChanges: make(map[uint32]*uint256.Int), + NonceChanges: make(map[uint32]uint64), + CodeChange: make(map[uint32][]byte), } } @@ -97,12 +97,12 @@ func (b *ConstructionBlockAccessList) StorageRead(address common.Address, key co // StorageWrite records the post-transaction value of a mutated storage slot. // The storage slot is removed from the list of read slots. -func (b *ConstructionBlockAccessList) StorageWrite(txIdx uint16, address common.Address, key, value common.Hash) { +func (b *ConstructionBlockAccessList) StorageWrite(txIdx uint32, address common.Address, key, value common.Hash) { if _, ok := b.Accounts[address]; !ok { b.Accounts[address] = NewConstructionAccountAccess() } if _, ok := b.Accounts[address].StorageWrites[key]; !ok { - b.Accounts[address].StorageWrites[key] = make(map[uint16]common.Hash) + b.Accounts[address].StorageWrites[key] = make(map[uint32]common.Hash) } b.Accounts[address].StorageWrites[key][txIdx] = value @@ -110,7 +110,7 @@ func (b *ConstructionBlockAccessList) StorageWrite(txIdx uint16, address common. } // CodeChange records the code of a newly-created contract. -func (b *ConstructionBlockAccessList) CodeChange(address common.Address, txIndex uint16, code []byte) { +func (b *ConstructionBlockAccessList) CodeChange(address common.Address, txIndex uint32, code []byte) { if _, ok := b.Accounts[address]; !ok { b.Accounts[address] = NewConstructionAccountAccess() } @@ -120,7 +120,7 @@ func (b *ConstructionBlockAccessList) CodeChange(address common.Address, txIndex // NonceChange records tx post-state nonce of any contract-like accounts whose // nonce was incremented. -func (b *ConstructionBlockAccessList) NonceChange(address common.Address, txIdx uint16, postNonce uint64) { +func (b *ConstructionBlockAccessList) NonceChange(address common.Address, txIdx uint32, postNonce uint64) { if _, ok := b.Accounts[address]; !ok { b.Accounts[address] = NewConstructionAccountAccess() } @@ -129,7 +129,7 @@ func (b *ConstructionBlockAccessList) NonceChange(address common.Address, txIdx // BalanceChange records the post-transaction balance of an account whose // balance changed. -func (b *ConstructionBlockAccessList) BalanceChange(txIdx uint16, address common.Address, balance *uint256.Int) { +func (b *ConstructionBlockAccessList) BalanceChange(txIdx uint32, address common.Address, balance *uint256.Int) { if _, ok := b.Accounts[address]; !ok { b.Accounts[address] = NewConstructionAccountAccess() } @@ -148,21 +148,21 @@ func (b *ConstructionBlockAccessList) Copy() *ConstructionBlockAccessList { for addr, aa := range b.Accounts { var aaCopy ConstructionAccountAccess - slotWrites := make(map[common.Hash]map[uint16]common.Hash, len(aa.StorageWrites)) + slotWrites := make(map[common.Hash]map[uint32]common.Hash, len(aa.StorageWrites)) for key, m := range aa.StorageWrites { slotWrites[key] = maps.Clone(m) } aaCopy.StorageWrites = slotWrites aaCopy.StorageReads = maps.Clone(aa.StorageReads) - balances := make(map[uint16]*uint256.Int, len(aa.BalanceChanges)) + balances := make(map[uint32]*uint256.Int, len(aa.BalanceChanges)) for index, balance := range aa.BalanceChanges { balances[index] = balance.Clone() } aaCopy.BalanceChanges = balances aaCopy.NonceChanges = maps.Clone(aa.NonceChanges) - codes := make(map[uint16][]byte, len(aa.CodeChange)) + codes := make(map[uint32][]byte, len(aa.CodeChange)) for index, code := range aa.CodeChange { codes[index] = bytes.Clone(code) } diff --git a/core/types/bal/bal_encoding.go b/core/types/bal/bal_encoding.go index 6d52c17c83..03f97f3809 100644 --- a/core/types/bal/bal_encoding.go +++ b/core/types/bal/bal_encoding.go @@ -33,27 +33,59 @@ import ( "github.com/holiman/uint256" ) -//go:generate go run github.com/ethereum/go-ethereum/rlp/rlpgen -out bal_encoding_rlp_generated.go -type BlockAccessList -decoder +//go:generate go run github.com/ethereum/go-ethereum/rlp/rlpgen -out bal_encoding_rlp_generated.go -type AccountAccess -decoder // These are objects used as input for the access list encoding. They mirror // the spec format. // BlockAccessList is the encoding format of ConstructionBlockAccessList. -type BlockAccessList struct { - Accesses []AccountAccess `ssz-max:"300000"` +type BlockAccessList []AccountAccess + +// EncodeRLP implements rlp.Encoder. It encodes the access list as a single +// RLP list of AccountAccess entries. +func (e BlockAccessList) EncodeRLP(w io.Writer) error { + buf := rlp.NewEncoderBuffer(w) + l := buf.List() + for i := range e { + if err := e[i].EncodeRLP(buf); err != nil { + return err + } + } + buf.ListEnd(l) + return buf.Flush() +} + +// DecodeRLP implements rlp.Decoder. +func (e *BlockAccessList) DecodeRLP(s *rlp.Stream) error { + if _, err := s.List(); err != nil { + return err + } + var list BlockAccessList + for s.MoreDataInList() { + var a AccountAccess + if err := a.DecodeRLP(s); err != nil { + return err + } + list = append(list, a) + } + if err := s.ListEnd(); err != nil { + return err + } + *e = list + return nil } // Validate returns an error if the contents of the access list are not ordered // according to the spec or any code changes are contained which exceed protocol // max code size. -func (e *BlockAccessList) Validate() error { - if !slices.IsSortedFunc(e.Accesses, func(a, b AccountAccess) int { +func (e *BlockAccessList) Validate(rules params.Rules) error { + if !slices.IsSortedFunc(*e, func(a, b AccountAccess) int { return bytes.Compare(a.Address[:], b.Address[:]) }) { return errors.New("block access list accounts not in lexicographic order") } - for _, entry := range e.Accesses { - if err := entry.validate(); err != nil { + for _, entry := range *e { + if err := entry.validate(rules); err != nil { return err } } @@ -63,56 +95,44 @@ func (e *BlockAccessList) Validate() error { // Hash computes the keccak256 hash of the access list func (e *BlockAccessList) Hash() common.Hash { var enc bytes.Buffer - err := e.EncodeRLP(&enc) - if err != nil { - // errors here are related to BAL values exceeding maximum size defined - // by the spec. Hard-fail because these cases are not expected to be hit - // under reasonable conditions. - panic(err) + if err := e.EncodeRLP(&enc); err != nil { + // Errors here are related to BAL values exceeding maximum size defined + // by the spec. Return empty hash because these cases are not expected + // to be hit under reasonable conditions. + return common.Hash{} } return crypto.Keccak256Hash(enc.Bytes()) } -// encodeBalance encodes the provided balance into 16-bytes. -func encodeBalance(val *uint256.Int) [16]byte { - valBytes := val.Bytes() - if len(valBytes) > 16 { - panic("can't encode value that is greater than 16 bytes in size") - } - var enc [16]byte - copy(enc[16-len(valBytes):], valBytes[:]) - return enc -} - // encodingBalanceChange is the encoding format of BalanceChange. type encodingBalanceChange struct { - TxIdx uint16 `ssz-size:"2"` - Balance [16]byte `ssz-size:"16"` + TxIdx uint32 + Balance *uint256.Int } // encodingAccountNonce is the encoding format of NonceChange. type encodingAccountNonce struct { - TxIdx uint16 `ssz-size:"2"` - Nonce uint64 `ssz-size:"8"` + TxIdx uint32 + Nonce uint64 } // encodingStorageWrite is the encoding format of StorageWrites. type encodingStorageWrite struct { - TxIdx uint16 - ValueAfter [32]byte `ssz-size:"32"` + TxIdx uint32 + ValueAfter *uint256.Int } // encodingStorageWrite is the encoding format of SlotWrites. type encodingSlotWrites struct { - Slot [32]byte `ssz-size:"32"` - Accesses []encodingStorageWrite `ssz-max:"300000"` + Slot *uint256.Int + Accesses []encodingStorageWrite } // validate returns an instance of the encoding-representation slot writes in // working representation. func (e *encodingSlotWrites) validate() error { if slices.IsSortedFunc(e.Accesses, func(a, b encodingStorageWrite) int { - return cmp.Compare[uint16](a.TxIdx, b.TxIdx) + return cmp.Compare[uint32](a.TxIdx, b.TxIdx) }) { return nil } @@ -122,27 +142,27 @@ func (e *encodingSlotWrites) validate() error { // encodingCodeChange contains the runtime bytecode deployed at an address // and the transaction index where the deployment took place. type encodingCodeChange struct { - TxIndex uint16 `ssz-size:"2"` - Code []byte `ssz-max:"300000"` // TODO(rjl493456442) shall we put the limit here? The limit will be increased gradually + TxIndex uint32 + Code []byte } // AccountAccess is the encoding format of ConstructionAccountAccess. type AccountAccess struct { - Address [20]byte `ssz-size:"20"` // 20-byte Ethereum address - StorageWrites []encodingSlotWrites `ssz-max:"300000"` // Storage changes (slot -> [tx_index -> new_value]) - StorageReads [][32]byte `ssz-max:"300000"` // Read-only storage keys - BalanceChanges []encodingBalanceChange `ssz-max:"300000"` // Balance changes ([tx_index -> post_balance]) - NonceChanges []encodingAccountNonce `ssz-max:"300000"` // Nonce changes ([tx_index -> new_nonce]) - CodeChanges []encodingCodeChange `ssz-max:"300000"` // Code changes ([tx_index -> new_code]) + Address [20]byte // 20-byte Ethereum address + StorageWrites []encodingSlotWrites // Storage changes (slot -> [tx_index -> new_value]) + StorageReads []*uint256.Int // Read-only storage keys + BalanceChanges []encodingBalanceChange // Balance changes ([tx_index -> post_balance]) + NonceChanges []encodingAccountNonce // Nonce changes ([tx_index -> new_nonce]) + CodeChanges []encodingCodeChange // Code changes ([tx_index -> new_code]) } // validate converts the account accesses out of encoding format. // If any of the keys in the encoding object are not ordered according to the // spec, an error is returned. -func (e *AccountAccess) validate() error { +func (e *AccountAccess) validate(rules params.Rules) error { // Check the storage write slots are sorted in order if !slices.IsSortedFunc(e.StorageWrites, func(a, b encodingSlotWrites) int { - return bytes.Compare(a.Slot[:], b.Slot[:]) + return a.Slot.Cmp(b.Slot) }) { return errors.New("storage writes slots not in lexicographic order") } @@ -153,36 +173,41 @@ func (e *AccountAccess) validate() error { } // Check the storage read slots are sorted in order - if !slices.IsSortedFunc(e.StorageReads, func(a, b [32]byte) int { - return bytes.Compare(a[:], b[:]) + if !slices.IsSortedFunc(e.StorageReads, func(a, b *uint256.Int) int { + return a.Cmp(b) }) { return errors.New("storage read slots not in lexicographic order") } // Check the balance changes are sorted in order if !slices.IsSortedFunc(e.BalanceChanges, func(a, b encodingBalanceChange) int { - return cmp.Compare[uint16](a.TxIdx, b.TxIdx) + return cmp.Compare[uint32](a.TxIdx, b.TxIdx) }) { return errors.New("balance changes not in ascending order by tx index") } // Check the nonce changes are sorted in order if !slices.IsSortedFunc(e.NonceChanges, func(a, b encodingAccountNonce) int { - return cmp.Compare[uint16](a.TxIdx, b.TxIdx) + return cmp.Compare[uint32](a.TxIdx, b.TxIdx) }) { return errors.New("nonce changes not in ascending order by tx index") } // Check the code changes are sorted in order if !slices.IsSortedFunc(e.CodeChanges, func(a, b encodingCodeChange) int { - return cmp.Compare[uint16](a.TxIndex, b.TxIndex) + return cmp.Compare[uint32](a.TxIndex, b.TxIndex) }) { return errors.New("code changes not in ascending order by tx index") } for _, change := range e.CodeChanges { - // TODO(rjl493456442): This check should be fork-aware, since the limit may - // differ across forks. - if len(change.Code) > params.MaxCodeSize { + var sizeLimit int + switch { + case rules.IsAmsterdam: + sizeLimit = params.MaxCodeSizeAmsterdam + default: + sizeLimit = params.MaxCodeSize + } + if len(change.Code) > sizeLimit { return errors.New("code change contained oversized code") } } @@ -193,16 +218,32 @@ func (e *AccountAccess) validate() error { func (e *AccountAccess) Copy() AccountAccess { res := AccountAccess{ Address: e.Address, - StorageReads: slices.Clone(e.StorageReads), - BalanceChanges: slices.Clone(e.BalanceChanges), + StorageReads: make([]*uint256.Int, 0, len(e.StorageReads)), + BalanceChanges: make([]encodingBalanceChange, 0, len(e.BalanceChanges)), NonceChanges: slices.Clone(e.NonceChanges), StorageWrites: make([]encodingSlotWrites, 0, len(e.StorageWrites)), CodeChanges: make([]encodingCodeChange, 0, len(e.CodeChanges)), } + for _, slot := range e.StorageReads { + res.StorageReads = append(res.StorageReads, slot.Clone()) + } + for _, change := range e.BalanceChanges { + res.BalanceChanges = append(res.BalanceChanges, encodingBalanceChange{ + TxIdx: change.TxIdx, + Balance: change.Balance.Clone(), + }) + } for _, storageWrite := range e.StorageWrites { + accesses := make([]encodingStorageWrite, 0, len(storageWrite.Accesses)) + for _, w := range storageWrite.Accesses { + accesses = append(accesses, encodingStorageWrite{ + TxIdx: w.TxIdx, + ValueAfter: w.ValueAfter.Clone(), + }) + } res.StorageWrites = append(res.StorageWrites, encodingSlotWrites{ - Slot: storageWrite.Slot, - Accesses: slices.Clone(storageWrite.Accesses), + Slot: storageWrite.Slot.Clone(), + Accesses: accesses, }) } for _, codeChange := range e.CodeChanges { @@ -221,13 +262,13 @@ func (b *ConstructionBlockAccessList) EncodeRLP(wr io.Writer) error { var _ rlp.Encoder = &ConstructionBlockAccessList{} -// toEncodingObj creates an instance of the ConstructionAccountAccess of the type that is -// used as input for the encoding. +// toEncodingObj creates an instance of the ConstructionAccountAccess of the type +// that is used as input for the encoding. func (a *ConstructionAccountAccess) toEncodingObj(addr common.Address) AccountAccess { res := AccountAccess{ Address: addr, StorageWrites: make([]encodingSlotWrites, 0, len(a.StorageWrites)), - StorageReads: make([][32]byte, 0, len(a.StorageReads)), + StorageReads: make([]*uint256.Int, 0, len(a.StorageReads)), BalanceChanges: make([]encodingBalanceChange, 0, len(a.BalanceChanges)), NonceChanges: make([]encodingAccountNonce, 0, len(a.NonceChanges)), CodeChanges: make([]encodingCodeChange, 0, len(a.CodeChange)), @@ -237,18 +278,19 @@ func (a *ConstructionAccountAccess) toEncodingObj(addr common.Address) AccountAc writeSlots := slices.Collect(maps.Keys(a.StorageWrites)) slices.SortFunc(writeSlots, common.Hash.Cmp) for _, slot := range writeSlots { - var obj encodingSlotWrites - obj.Slot = slot - + obj := encodingSlotWrites{ + Slot: new(uint256.Int).SetBytes(slot[:]), + } slotWrites := a.StorageWrites[slot] obj.Accesses = make([]encodingStorageWrite, 0, len(slotWrites)) indices := slices.Collect(maps.Keys(slotWrites)) - slices.SortFunc(indices, cmp.Compare[uint16]) + slices.SortFunc(indices, cmp.Compare[uint32]) for _, index := range indices { + val := slotWrites[index] obj.Accesses = append(obj.Accesses, encodingStorageWrite{ TxIdx: index, - ValueAfter: slotWrites[index], + ValueAfter: new(uint256.Int).SetBytes(val[:]), }) } res.StorageWrites = append(res.StorageWrites, obj) @@ -258,22 +300,22 @@ func (a *ConstructionAccountAccess) toEncodingObj(addr common.Address) AccountAc readSlots := slices.Collect(maps.Keys(a.StorageReads)) slices.SortFunc(readSlots, common.Hash.Cmp) for _, slot := range readSlots { - res.StorageReads = append(res.StorageReads, slot) + res.StorageReads = append(res.StorageReads, new(uint256.Int).SetBytes(slot[:])) } // Convert balance changes balanceIndices := slices.Collect(maps.Keys(a.BalanceChanges)) - slices.SortFunc(balanceIndices, cmp.Compare[uint16]) + slices.SortFunc(balanceIndices, cmp.Compare[uint32]) for _, idx := range balanceIndices { res.BalanceChanges = append(res.BalanceChanges, encodingBalanceChange{ TxIdx: idx, - Balance: encodeBalance(a.BalanceChanges[idx]), + Balance: a.BalanceChanges[idx].Clone(), }) } // Convert nonce changes nonceIndices := slices.Collect(maps.Keys(a.NonceChanges)) - slices.SortFunc(nonceIndices, cmp.Compare[uint16]) + slices.SortFunc(nonceIndices, cmp.Compare[uint32]) for _, idx := range nonceIndices { res.NonceChanges = append(res.NonceChanges, encodingAccountNonce{ TxIdx: idx, @@ -283,11 +325,16 @@ func (a *ConstructionAccountAccess) toEncodingObj(addr common.Address) AccountAc // Convert code change codeIndices := slices.Collect(maps.Keys(a.CodeChange)) - slices.SortFunc(codeIndices, cmp.Compare[uint16]) + slices.SortFunc(codeIndices, cmp.Compare[uint32]) for _, idx := range codeIndices { res.CodeChanges = append(res.CodeChanges, encodingCodeChange{ TxIndex: idx, - Code: a.CodeChange[idx], + + // TODO(rjl493456442) the contract code is not deep-copied. + // In theory the deep-copy is unnecessary, the semantics of + // the function should be probably changed that the returned + // AccessList is unsafe for modification. + Code: a.CodeChange[idx], }) } return res @@ -302,9 +349,9 @@ func (b *ConstructionBlockAccessList) toEncodingObj() *BlockAccessList { } slices.SortFunc(addresses, common.Address.Cmp) - var res BlockAccessList + res := make(BlockAccessList, 0, len(addresses)) for _, addr := range addresses { - res.Accesses = append(res.Accesses, b.Accounts[addr].toEncodingObj(addr)) + res = append(res, b.Accounts[addr].toEncodingObj(addr)) } return &res } @@ -314,26 +361,25 @@ func (e *BlockAccessList) PrettyPrint() string { printWithIndent := func(indent int, text string) { fmt.Fprintf(&res, "%s%s\n", strings.Repeat(" ", indent), text) } - for _, accountDiff := range e.Accesses { + for _, accountDiff := range *e { printWithIndent(0, fmt.Sprintf("%x:", accountDiff.Address)) printWithIndent(1, "storage writes:") for _, sWrite := range accountDiff.StorageWrites { - printWithIndent(2, fmt.Sprintf("%x:", sWrite.Slot)) + printWithIndent(2, fmt.Sprintf("%s:", sWrite.Slot.Hex())) for _, access := range sWrite.Accesses { - printWithIndent(3, fmt.Sprintf("%d: %x", access.TxIdx, access.ValueAfter)) + printWithIndent(3, fmt.Sprintf("%d: %s", access.TxIdx, access.ValueAfter.Hex())) } } printWithIndent(1, "storage reads:") for _, slot := range accountDiff.StorageReads { - printWithIndent(2, fmt.Sprintf("%x", slot)) + printWithIndent(2, slot.Hex()) } printWithIndent(1, "balance changes:") for _, change := range accountDiff.BalanceChanges { - balance := new(uint256.Int).SetBytes(change.Balance[:]).String() - printWithIndent(2, fmt.Sprintf("%d: %s", change.TxIdx, balance)) + printWithIndent(2, fmt.Sprintf("%d: %s", change.TxIdx, change.Balance)) } printWithIndent(1, "nonce changes:") @@ -351,11 +397,9 @@ func (e *BlockAccessList) PrettyPrint() string { // Copy returns a deep copy of the access list func (e *BlockAccessList) Copy() *BlockAccessList { - cpy := &BlockAccessList{ - Accesses: make([]AccountAccess, 0, len(e.Accesses)), - } - for _, accountAccess := range e.Accesses { - cpy.Accesses = append(cpy.Accesses, accountAccess.Copy()) + cpy := make(BlockAccessList, 0, len(*e)) + for _, accountAccess := range *e { + cpy = append(cpy, accountAccess.Copy()) } - return cpy + return &cpy } diff --git a/core/types/bal/bal_encoding_rlp_generated.go b/core/types/bal/bal_encoding_rlp_generated.go index 640035e30e..540987c076 100644 --- a/core/types/bal/bal_encoding_rlp_generated.go +++ b/core/types/bal/bal_encoding_rlp_generated.go @@ -3,274 +3,264 @@ package bal import "github.com/ethereum/go-ethereum/rlp" +import "github.com/holiman/uint256" import "io" -func (obj *BlockAccessList) EncodeRLP(_w io.Writer) error { +func (obj *AccountAccess) EncodeRLP(_w io.Writer) error { w := rlp.NewEncoderBuffer(_w) _tmp0 := w.List() + w.WriteBytes(obj.Address[:]) _tmp1 := w.List() - for _, _tmp2 := range obj.Accesses { + for _, _tmp2 := range obj.StorageWrites { _tmp3 := w.List() - w.WriteBytes(_tmp2.Address[:]) + if _tmp2.Slot == nil { + w.Write(rlp.EmptyString) + } else { + w.WriteUint256(_tmp2.Slot) + } _tmp4 := w.List() - for _, _tmp5 := range _tmp2.StorageWrites { + for _, _tmp5 := range _tmp2.Accesses { _tmp6 := w.List() - w.WriteBytes(_tmp5.Slot[:]) - _tmp7 := w.List() - for _, _tmp8 := range _tmp5.Accesses { - _tmp9 := w.List() - w.WriteUint64(uint64(_tmp8.TxIdx)) - w.WriteBytes(_tmp8.ValueAfter[:]) - w.ListEnd(_tmp9) + w.WriteUint64(uint64(_tmp5.TxIdx)) + if _tmp5.ValueAfter == nil { + w.Write(rlp.EmptyString) + } else { + w.WriteUint256(_tmp5.ValueAfter) } - w.ListEnd(_tmp7) w.ListEnd(_tmp6) } w.ListEnd(_tmp4) - _tmp10 := w.List() - for _, _tmp11 := range _tmp2.StorageReads { - w.WriteBytes(_tmp11[:]) - } - w.ListEnd(_tmp10) - _tmp12 := w.List() - for _, _tmp13 := range _tmp2.BalanceChanges { - _tmp14 := w.List() - w.WriteUint64(uint64(_tmp13.TxIdx)) - w.WriteBytes(_tmp13.Balance[:]) - w.ListEnd(_tmp14) - } - w.ListEnd(_tmp12) - _tmp15 := w.List() - for _, _tmp16 := range _tmp2.NonceChanges { - _tmp17 := w.List() - w.WriteUint64(uint64(_tmp16.TxIdx)) - w.WriteUint64(_tmp16.Nonce) - w.ListEnd(_tmp17) - } - w.ListEnd(_tmp15) - _tmp18 := w.List() - for _, _tmp19 := range _tmp2.CodeChanges { - _tmp20 := w.List() - w.WriteUint64(uint64(_tmp19.TxIndex)) - w.WriteBytes(_tmp19.Code) - w.ListEnd(_tmp20) - } - w.ListEnd(_tmp18) w.ListEnd(_tmp3) } w.ListEnd(_tmp1) + _tmp7 := w.List() + for _, _tmp8 := range obj.StorageReads { + if _tmp8 == nil { + w.Write(rlp.EmptyString) + } else { + w.WriteUint256(_tmp8) + } + } + w.ListEnd(_tmp7) + _tmp9 := w.List() + for _, _tmp10 := range obj.BalanceChanges { + _tmp11 := w.List() + w.WriteUint64(uint64(_tmp10.TxIdx)) + if _tmp10.Balance == nil { + w.Write(rlp.EmptyString) + } else { + w.WriteUint256(_tmp10.Balance) + } + w.ListEnd(_tmp11) + } + w.ListEnd(_tmp9) + _tmp12 := w.List() + for _, _tmp13 := range obj.NonceChanges { + _tmp14 := w.List() + w.WriteUint64(uint64(_tmp13.TxIdx)) + w.WriteUint64(_tmp13.Nonce) + w.ListEnd(_tmp14) + } + w.ListEnd(_tmp12) + _tmp15 := w.List() + for _, _tmp16 := range obj.CodeChanges { + _tmp17 := w.List() + w.WriteUint64(uint64(_tmp16.TxIndex)) + w.WriteBytes(_tmp16.Code) + w.ListEnd(_tmp17) + } + w.ListEnd(_tmp15) w.ListEnd(_tmp0) return w.Flush() } -func (obj *BlockAccessList) DecodeRLP(dec *rlp.Stream) error { - var _tmp0 BlockAccessList +func (obj *AccountAccess) DecodeRLP(dec *rlp.Stream) error { + var _tmp0 AccountAccess { if _, err := dec.List(); err != nil { return err } - // Accesses: - var _tmp1 []AccountAccess + // Address: + var _tmp1 [20]byte + if err := dec.ReadBytes(_tmp1[:]); err != nil { + return err + } + _tmp0.Address = _tmp1 + // StorageWrites: + var _tmp2 []encodingSlotWrites if _, err := dec.List(); err != nil { return err } for dec.MoreDataInList() { - var _tmp2 AccountAccess + var _tmp3 encodingSlotWrites { if _, err := dec.List(); err != nil { return err } - // Address: - var _tmp3 [20]byte - if err := dec.ReadBytes(_tmp3[:]); err != nil { + // Slot: + var _tmp4 uint256.Int + if err := dec.ReadUint256(&_tmp4); err != nil { return err } - _tmp2.Address = _tmp3 - // StorageWrites: - var _tmp4 []encodingSlotWrites + _tmp3.Slot = &_tmp4 + // Accesses: + var _tmp5 []encodingStorageWrite if _, err := dec.List(); err != nil { return err } for dec.MoreDataInList() { - var _tmp5 encodingSlotWrites + var _tmp6 encodingStorageWrite { if _, err := dec.List(); err != nil { return err } - // Slot: - var _tmp6 [32]byte - if err := dec.ReadBytes(_tmp6[:]); err != nil { - return err - } - _tmp5.Slot = _tmp6 - // Accesses: - var _tmp7 []encodingStorageWrite - if _, err := dec.List(); err != nil { + // TxIdx: + _tmp7, err := dec.Uint32() + if err != nil { return err } - for dec.MoreDataInList() { - var _tmp8 encodingStorageWrite - { - if _, err := dec.List(); err != nil { - return err - } - // TxIdx: - _tmp9, err := dec.Uint16() - if err != nil { - return err - } - _tmp8.TxIdx = _tmp9 - // ValueAfter: - var _tmp10 [32]byte - if err := dec.ReadBytes(_tmp10[:]); err != nil { - return err - } - _tmp8.ValueAfter = _tmp10 - if err := dec.ListEnd(); err != nil { - return err - } - } - _tmp7 = append(_tmp7, _tmp8) - } - if err := dec.ListEnd(); err != nil { + _tmp6.TxIdx = _tmp7 + // ValueAfter: + var _tmp8 uint256.Int + if err := dec.ReadUint256(&_tmp8); err != nil { return err } - _tmp5.Accesses = _tmp7 + _tmp6.ValueAfter = &_tmp8 if err := dec.ListEnd(); err != nil { return err } } - _tmp4 = append(_tmp4, _tmp5) + _tmp5 = append(_tmp5, _tmp6) } if err := dec.ListEnd(); err != nil { return err } - _tmp2.StorageWrites = _tmp4 - // StorageReads: - var _tmp11 [][32]byte - if _, err := dec.List(); err != nil { - return err - } - for dec.MoreDataInList() { - var _tmp12 [32]byte - if err := dec.ReadBytes(_tmp12[:]); err != nil { - return err - } - _tmp11 = append(_tmp11, _tmp12) - } + _tmp3.Accesses = _tmp5 if err := dec.ListEnd(); err != nil { return err } - _tmp2.StorageReads = _tmp11 - // BalanceChanges: - var _tmp13 []encodingBalanceChange + } + _tmp2 = append(_tmp2, _tmp3) + } + if err := dec.ListEnd(); err != nil { + return err + } + _tmp0.StorageWrites = _tmp2 + // StorageReads: + var _tmp9 []*uint256.Int + if _, err := dec.List(); err != nil { + return err + } + for dec.MoreDataInList() { + var _tmp10 uint256.Int + if err := dec.ReadUint256(&_tmp10); err != nil { + return err + } + _tmp9 = append(_tmp9, &_tmp10) + } + if err := dec.ListEnd(); err != nil { + return err + } + _tmp0.StorageReads = _tmp9 + // BalanceChanges: + var _tmp11 []encodingBalanceChange + if _, err := dec.List(); err != nil { + return err + } + for dec.MoreDataInList() { + var _tmp12 encodingBalanceChange + { if _, err := dec.List(); err != nil { return err } - for dec.MoreDataInList() { - var _tmp14 encodingBalanceChange - { - if _, err := dec.List(); err != nil { - return err - } - // TxIdx: - _tmp15, err := dec.Uint16() - if err != nil { - return err - } - _tmp14.TxIdx = _tmp15 - // Balance: - var _tmp16 [16]byte - if err := dec.ReadBytes(_tmp16[:]); err != nil { - return err - } - _tmp14.Balance = _tmp16 - if err := dec.ListEnd(); err != nil { - return err - } - } - _tmp13 = append(_tmp13, _tmp14) + // TxIdx: + _tmp13, err := dec.Uint32() + if err != nil { + return err + } + _tmp12.TxIdx = _tmp13 + // Balance: + var _tmp14 uint256.Int + if err := dec.ReadUint256(&_tmp14); err != nil { + return err } + _tmp12.Balance = &_tmp14 if err := dec.ListEnd(); err != nil { return err } - _tmp2.BalanceChanges = _tmp13 - // NonceChanges: - var _tmp17 []encodingAccountNonce + } + _tmp11 = append(_tmp11, _tmp12) + } + if err := dec.ListEnd(); err != nil { + return err + } + _tmp0.BalanceChanges = _tmp11 + // NonceChanges: + var _tmp15 []encodingAccountNonce + if _, err := dec.List(); err != nil { + return err + } + for dec.MoreDataInList() { + var _tmp16 encodingAccountNonce + { if _, err := dec.List(); err != nil { return err } - for dec.MoreDataInList() { - var _tmp18 encodingAccountNonce - { - if _, err := dec.List(); err != nil { - return err - } - // TxIdx: - _tmp19, err := dec.Uint16() - if err != nil { - return err - } - _tmp18.TxIdx = _tmp19 - // Nonce: - _tmp20, err := dec.Uint64() - if err != nil { - return err - } - _tmp18.Nonce = _tmp20 - if err := dec.ListEnd(); err != nil { - return err - } - } - _tmp17 = append(_tmp17, _tmp18) + // TxIdx: + _tmp17, err := dec.Uint32() + if err != nil { + return err } + _tmp16.TxIdx = _tmp17 + // Nonce: + _tmp18, err := dec.Uint64() + if err != nil { + return err + } + _tmp16.Nonce = _tmp18 if err := dec.ListEnd(); err != nil { return err } - _tmp2.NonceChanges = _tmp17 - // CodeChanges: - var _tmp21 []encodingCodeChange + } + _tmp15 = append(_tmp15, _tmp16) + } + if err := dec.ListEnd(); err != nil { + return err + } + _tmp0.NonceChanges = _tmp15 + // CodeChanges: + var _tmp19 []encodingCodeChange + if _, err := dec.List(); err != nil { + return err + } + for dec.MoreDataInList() { + var _tmp20 encodingCodeChange + { if _, err := dec.List(); err != nil { return err } - for dec.MoreDataInList() { - var _tmp22 encodingCodeChange - { - if _, err := dec.List(); err != nil { - return err - } - // TxIndex: - _tmp23, err := dec.Uint16() - if err != nil { - return err - } - _tmp22.TxIndex = _tmp23 - // Code: - _tmp24, err := dec.Bytes() - if err != nil { - return err - } - _tmp22.Code = _tmp24 - if err := dec.ListEnd(); err != nil { - return err - } - } - _tmp21 = append(_tmp21, _tmp22) + // TxIndex: + _tmp21, err := dec.Uint32() + if err != nil { + return err } - if err := dec.ListEnd(); err != nil { + _tmp20.TxIndex = _tmp21 + // Code: + _tmp22, err := dec.Bytes() + if err != nil { return err } - _tmp2.CodeChanges = _tmp21 + _tmp20.Code = _tmp22 if err := dec.ListEnd(); err != nil { return err } } - _tmp1 = append(_tmp1, _tmp2) + _tmp19 = append(_tmp19, _tmp20) } if err := dec.ListEnd(); err != nil { return err } - _tmp0.Accesses = _tmp1 + _tmp0.CodeChanges = _tmp19 if err := dec.ListEnd(); err != nil { return err } diff --git a/core/types/bal/bal_test.go b/core/types/bal/bal_test.go index 58ba639ff0..32a0292f2e 100644 --- a/core/types/bal/bal_test.go +++ b/core/types/bal/bal_test.go @@ -25,22 +25,16 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/internal/testrand" + "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" "github.com/holiman/uint256" ) -func equalBALs(a *BlockAccessList, b *BlockAccessList) bool { - if !reflect.DeepEqual(a, b) { - return false - } - return true -} - func makeTestConstructionBAL() *ConstructionBlockAccessList { return &ConstructionBlockAccessList{ map[common.Address]*ConstructionAccountAccess{ common.BytesToAddress([]byte{0xff, 0xff}): { - StorageWrites: map[common.Hash]map[uint16]common.Hash{ + StorageWrites: map[common.Hash]map[uint32]common.Hash{ common.BytesToHash([]byte{0x01}): { 1: common.BytesToHash([]byte{1, 2, 3, 4}), 2: common.BytesToHash([]byte{1, 2, 3, 4, 5, 6}), @@ -52,20 +46,20 @@ func makeTestConstructionBAL() *ConstructionBlockAccessList { StorageReads: map[common.Hash]struct{}{ common.BytesToHash([]byte{1, 2, 3, 4, 5, 6, 7}): {}, }, - BalanceChanges: map[uint16]*uint256.Int{ + BalanceChanges: map[uint32]*uint256.Int{ 1: uint256.NewInt(100), 2: uint256.NewInt(500), }, - NonceChanges: map[uint16]uint64{ + NonceChanges: map[uint32]uint64{ 1: 2, 2: 6, }, - CodeChange: map[uint16][]byte{ + CodeChange: map[uint32][]byte{ 0: common.Hex2Bytes("deadbeef"), }, }, common.BytesToAddress([]byte{0xff, 0xff, 0xff}): { - StorageWrites: map[common.Hash]map[uint16]common.Hash{ + StorageWrites: map[common.Hash]map[uint32]common.Hash{ common.BytesToHash([]byte{0x01}): { 2: common.BytesToHash([]byte{1, 2, 3, 4, 5, 6}), 3: common.BytesToHash([]byte{1, 2, 3, 4, 5, 6, 7, 8}), @@ -77,14 +71,14 @@ func makeTestConstructionBAL() *ConstructionBlockAccessList { StorageReads: map[common.Hash]struct{}{ common.BytesToHash([]byte{1, 2, 3, 4, 5, 6, 7, 8}): {}, }, - BalanceChanges: map[uint16]*uint256.Int{ + BalanceChanges: map[uint32]*uint256.Int{ 2: uint256.NewInt(100), 3: uint256.NewInt(500), }, - NonceChanges: map[uint16]uint64{ + NonceChanges: map[uint32]uint64{ 1: 2, }, - CodeChange: map[uint16][]byte{ + CodeChange: map[uint32][]byte{ 0: common.Hex2Bytes("deadbeef"), }, }, @@ -101,13 +95,13 @@ func TestBALEncoding(t *testing.T) { t.Fatalf("encoding failed: %v\n", err) } var dec BlockAccessList - if err := dec.DecodeRLP(rlp.NewStream(bytes.NewReader(buf.Bytes()), 10000000)); err != nil { + if err := dec.DecodeRLP(rlp.NewStream(bytes.NewReader(buf.Bytes()), 0)); err != nil { t.Fatalf("decoding failed: %v\n", err) } if dec.Hash() != bal.toEncodingObj().Hash() { t.Fatalf("encoded block hash doesn't match decoded") } - if !equalBALs(bal.toEncodingObj(), &dec) { + if !reflect.DeepEqual(bal.toEncodingObj(), &dec) { t.Fatal("decoded BAL doesn't match") } } @@ -115,63 +109,79 @@ func TestBALEncoding(t *testing.T) { func makeTestAccountAccess(sort bool) AccountAccess { var ( storageWrites []encodingSlotWrites - storageReads [][32]byte + storageReads []*uint256.Int balances []encodingBalanceChange nonces []encodingAccountNonce + codes []encodingCodeChange ) + randSlot := func() *uint256.Int { + return new(uint256.Int).SetBytes(testrand.Bytes(32)) + } for i := 0; i < 5; i++ { slot := encodingSlotWrites{ - Slot: testrand.Hash(), + Slot: randSlot(), } for j := 0; j < 3; j++ { slot.Accesses = append(slot.Accesses, encodingStorageWrite{ - TxIdx: uint16(2 * j), - ValueAfter: testrand.Hash(), + TxIdx: uint32(2 * j), + ValueAfter: randSlot(), }) } if sort { slices.SortFunc(slot.Accesses, func(a, b encodingStorageWrite) int { - return cmp.Compare[uint16](a.TxIdx, b.TxIdx) + return cmp.Compare[uint32](a.TxIdx, b.TxIdx) }) } storageWrites = append(storageWrites, slot) } if sort { slices.SortFunc(storageWrites, func(a, b encodingSlotWrites) int { - return bytes.Compare(a.Slot[:], b.Slot[:]) + return a.Slot.Cmp(b.Slot) }) } for i := 0; i < 5; i++ { - storageReads = append(storageReads, testrand.Hash()) + storageReads = append(storageReads, randSlot()) } if sort { - slices.SortFunc(storageReads, func(a, b [32]byte) int { - return bytes.Compare(a[:], b[:]) + slices.SortFunc(storageReads, func(a, b *uint256.Int) int { + return a.Cmp(b) }) } for i := 0; i < 5; i++ { balances = append(balances, encodingBalanceChange{ - TxIdx: uint16(2 * i), - Balance: [16]byte(testrand.Bytes(16)), + TxIdx: uint32(2 * i), + Balance: new(uint256.Int).SetBytes(testrand.Bytes(16)), }) } if sort { slices.SortFunc(balances, func(a, b encodingBalanceChange) int { - return cmp.Compare[uint16](a.TxIdx, b.TxIdx) + return cmp.Compare[uint32](a.TxIdx, b.TxIdx) }) } for i := 0; i < 5; i++ { nonces = append(nonces, encodingAccountNonce{ - TxIdx: uint16(2 * i), + TxIdx: uint32(2 * i), Nonce: uint64(i + 100), }) } if sort { slices.SortFunc(nonces, func(a, b encodingAccountNonce) int { - return cmp.Compare[uint16](a.TxIdx, b.TxIdx) + return cmp.Compare[uint32](a.TxIdx, b.TxIdx) + }) + } + + for i := 0; i < 5; i++ { + codes = append(codes, encodingCodeChange{ + TxIndex: uint32(2 * i), + Code: testrand.Bytes(256), + }) + } + if sort { + slices.SortFunc(codes, func(a, b encodingCodeChange) int { + return cmp.Compare[uint32](a.TxIndex, b.TxIndex) }) } @@ -181,26 +191,21 @@ func makeTestAccountAccess(sort bool) AccountAccess { StorageReads: storageReads, BalanceChanges: balances, NonceChanges: nonces, - CodeChanges: []encodingCodeChange{ - { - TxIndex: 100, - Code: testrand.Bytes(256), - }, - }, + CodeChanges: codes, } } func makeTestBAL(sort bool) *BlockAccessList { - list := &BlockAccessList{} + list := make(BlockAccessList, 0, 5) for i := 0; i < 5; i++ { - list.Accesses = append(list.Accesses, makeTestAccountAccess(sort)) + list = append(list, makeTestAccountAccess(sort)) } if sort { - slices.SortFunc(list.Accesses, func(a, b AccountAccess) int { + slices.SortFunc(list, func(a, b AccountAccess) int { return bytes.Compare(a.Address[:], b.Address[:]) }) } - return list + return &list } func TestBlockAccessListCopy(t *testing.T) { @@ -216,9 +221,9 @@ func TestBlockAccessListCopy(t *testing.T) { } // Make sure the mutations on copy won't affect the origin - for _, aa := range cpyCpy.Accesses { + for _, aa := range *cpyCpy { for i := 0; i < len(aa.StorageReads); i++ { - aa.StorageReads[i] = [32]byte(testrand.Bytes(32)) + aa.StorageReads[i] = new(uint256.Int).SetBytes(testrand.Bytes(32)) } } if !reflect.DeepEqual(list, cpy) { @@ -229,7 +234,7 @@ func TestBlockAccessListCopy(t *testing.T) { func TestBlockAccessListValidation(t *testing.T) { // Validate the block access list after RLP decoding enc := makeTestBAL(true) - if err := enc.Validate(); err != nil { + if err := enc.Validate(params.Rules{}); err != nil { t.Fatalf("Unexpected validation error: %v", err) } var buf bytes.Buffer @@ -241,14 +246,14 @@ func TestBlockAccessListValidation(t *testing.T) { if err := dec.DecodeRLP(rlp.NewStream(bytes.NewReader(buf.Bytes()), 0)); err != nil { t.Fatalf("Unexpected RLP-decode error: %v", err) } - if err := dec.Validate(); err != nil { + if err := dec.Validate(params.Rules{}); err != nil { t.Fatalf("Unexpected validation error: %v", err) } // Validate the derived block access list cBAL := makeTestConstructionBAL() listB := cBAL.toEncodingObj() - if err := listB.Validate(); err != nil { + if err := listB.Validate(params.Rules{}); err != nil { t.Fatalf("Unexpected validation error: %v", err) } } diff --git a/eth/protocols/snap/handler_test.go b/eth/protocols/snap/handler_test.go index 3f6a43a059..b0522c20bb 100644 --- a/eth/protocols/snap/handler_test.go +++ b/eth/protocols/snap/handler_test.go @@ -31,18 +31,24 @@ import ( "github.com/ethereum/go-ethereum/core/types/bal" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" + "github.com/holiman/uint256" ) func makeTestBAL(minSize int) *bal.BlockAccessList { n := minSize/33 + 1 // 33 bytes per storage read slot in RLP access := bal.AccountAccess{ Address: common.HexToAddress("0x01"), - StorageReads: make([][32]byte, n), + StorageReads: make([]*uint256.Int, n), } + // Use a full-width 32-byte value (top byte 0xff) so each slot still + // encodes to 33 RLP bytes regardless of the index. for i := range access.StorageReads { - binary.BigEndian.PutUint64(access.StorageReads[i][24:], uint64(i)) + var b [32]byte + b[0] = 0xff + binary.BigEndian.PutUint64(b[24:], uint64(i)) + access.StorageReads[i] = new(uint256.Int).SetBytes(b[:]) } - return &bal.BlockAccessList{Accesses: []bal.AccountAccess{access}} + return &bal.BlockAccessList{access} } // getChainWithBALs creates a minimal test chain with BALs stored for each block. From a06558042299eba2e19ca656d3f46edb85df1420 Mon Sep 17 00:00:00 2001 From: Rahman Date: Mon, 27 Apr 2026 01:25:57 -0600 Subject: [PATCH 27/80] triedb/pathdb: compute size in StateSetWithOrigin.decode (#34828) `StateSetWithOrigin.decode()` was missing size computation after deserializing origin data, causing `size` to remain zero after journal reload. Added the same calculation logic used in `NewStateSetWithOrigin()`. --- triedb/pathdb/states.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/triedb/pathdb/states.go b/triedb/pathdb/states.go index c54d8b1136..27a6c1d422 100644 --- a/triedb/pathdb/states.go +++ b/triedb/pathdb/states.go @@ -583,6 +583,18 @@ func (s *StateSetWithOrigin) decode(r *rlp.Stream) error { } } s.storageOrigin = storageSet + + // Compute the size of origin data, keeping consistent with NewStateSetWithOrigin + var size int + for _, data := range s.accountOrigin { + size += common.HashLength + len(data) + } + for _, slots := range s.storageOrigin { + for _, data := range slots { + size += 2*common.HashLength + len(data) + } + } + s.size = s.stateSet.size + uint64(size) return nil } From 442bd28b0bad76674348a67b4c9f8689170bfcdb Mon Sep 17 00:00:00 2001 From: felipe Date: Mon, 27 Apr 2026 14:35:49 +0200 Subject: [PATCH 28/80] cmd/evm/internal/t8ntool: stream t8n alloc to ease heavy memory cases (#34785) --- cmd/evm/internal/t8ntool/execution.go | 90 ++++++++++++++++++- cmd/evm/internal/t8ntool/transition.go | 119 ++++++++++++++++++++++--- 2 files changed, 193 insertions(+), 16 deletions(-) diff --git a/cmd/evm/internal/t8ntool/execution.go b/cmd/evm/internal/t8ntool/execution.go index f17829ec53..253ebe1111 100644 --- a/cmd/evm/internal/t8ntool/execution.go +++ b/cmd/evm/internal/t8ntool/execution.go @@ -17,9 +17,11 @@ package t8ntool import ( + "encoding/json" "fmt" stdmath "math" "math/big" + "os" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" @@ -47,6 +49,9 @@ type Prestate struct { Env stEnv `json:"env"` Pre types.GenesisAlloc `json:"pre"` TreeLeaves map[common.Hash]hexutil.Bytes `json:"vkt,omitempty"` + // AllocPath, when non-empty, causes Apply to stream the alloc from disk + // instead of reading Pre, so the full map never materializes in memory. + AllocPath string `json:"-"` } //go:generate go run github.com/fjl/gencodec -type ExecutionResult -field-override executionResultMarshaling -out gen_execresult.go @@ -146,8 +151,19 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig, return h } var ( - isEIP4762 = chainConfig.IsUBT(big.NewInt(int64(pre.Env.Number)), pre.Env.Timestamp) - statedb = MakePreState(rawdb.NewMemoryDatabase(), pre.Pre, isEIP4762) + isEIP4762 = chainConfig.IsUBT(big.NewInt(int64(pre.Env.Number)), pre.Env.Timestamp) + statedb *state.StateDB + ) + if pre.AllocPath != "" { + var err error + statedb, err = MakePreStateStreaming(rawdb.NewMemoryDatabase(), pre.AllocPath, isEIP4762) + if err != nil { + return nil, nil, nil, err + } + } else { + statedb = MakePreState(rawdb.NewMemoryDatabase(), pre.Pre, isEIP4762) + } + var ( signer = types.MakeSigner(chainConfig, new(big.Int).SetUint64(pre.Env.Number), pre.Env.Timestamp) gaspool = core.NewGasPool(pre.Env.GasLimit) blockHash = common.Hash{0x13, 0x37} @@ -414,6 +430,76 @@ func MakePreState(db ethdb.Database, accounts types.GenesisAlloc, isBintrie bool return statedb } +// MakePreStateStreaming is like MakePreState, but decodes the alloc from disk +// one account at a time so the full map is never held in memory. +func MakePreStateStreaming(db ethdb.Database, allocPath string, isBintrie bool) (*state.StateDB, error) { + tdb := triedb.NewDatabase(db, &triedb.Config{Preimages: true, IsUBT: isBintrie}) + sdb := state.NewDatabase(tdb, nil) + + root := types.EmptyRootHash + if isBintrie { + root = types.EmptyBinaryHash + } + statedb, err := state.New(root, sdb) + if err != nil { + return nil, NewError(ErrorEVM, fmt.Errorf("failed to create initial statedb: %v", err)) + } + + f, err := os.Open(allocPath) + if err != nil { + return nil, NewError(ErrorIO, fmt.Errorf("failed reading alloc file: %v", err)) + } + defer f.Close() + + dec := json.NewDecoder(f) + tok, err := dec.Token() + if err != nil { + return nil, NewError(ErrorJson, fmt.Errorf("failed reading alloc opening token: %v", err)) + } + if d, ok := tok.(json.Delim); !ok || d != '{' { + return nil, NewError(ErrorJson, fmt.Errorf("expected alloc object, got %v", tok)) + } + for dec.More() { + keyTok, err := dec.Token() + if err != nil { + return nil, NewError(ErrorJson, fmt.Errorf("failed reading alloc key: %v", err)) + } + keyStr, ok := keyTok.(string) + if !ok { + return nil, NewError(ErrorJson, fmt.Errorf("alloc key not a string: %v", keyTok)) + } + addr := common.HexToAddress(keyStr) + var acct types.Account + if err := dec.Decode(&acct); err != nil { + return nil, NewError(ErrorJson, fmt.Errorf("failed decoding account %s: %v", keyStr, err)) + } + statedb.SetCode(addr, acct.Code, tracing.CodeChangeUnspecified) + statedb.SetNonce(addr, acct.Nonce, tracing.NonceChangeGenesis) + if acct.Balance != nil { + statedb.SetBalance(addr, uint256.MustFromBig(acct.Balance), tracing.BalanceIncreaseGenesisBalance) + } + for k, v := range acct.Storage { + statedb.SetState(addr, k, v) + } + } + if _, err := dec.Token(); err != nil { + return nil, NewError(ErrorJson, fmt.Errorf("failed reading alloc closing token: %v", err)) + } + + root, err = statedb.Commit(0, false, false) + if err != nil { + return nil, NewError(ErrorEVM, fmt.Errorf("failed to commit initial state: %v", err)) + } + if isBintrie { + return statedb, nil + } + statedb, err = state.New(root, sdb) + if err != nil { + return nil, NewError(ErrorEVM, fmt.Errorf("failed to reopen state after commit: %v", err)) + } + return statedb, nil +} + func rlpHash(x any) (h common.Hash) { hw := keccak.NewLegacyKeccak256() rlp.Encode(hw, x) diff --git a/cmd/evm/internal/t8ntool/transition.go b/cmd/evm/internal/t8ntool/transition.go index 6a23e9dc70..e0bb3a449d 100644 --- a/cmd/evm/internal/t8ntool/transition.go +++ b/cmd/evm/internal/t8ntool/transition.go @@ -17,6 +17,7 @@ package t8ntool import ( + "bufio" "encoding/json" "errors" "fmt" @@ -115,11 +116,10 @@ func Transition(ctx *cli.Context) error { } } if allocStr != stdinSelector { - if err := readFile(allocStr, "alloc", &inputData.Alloc); err != nil { - return err - } + prestate.AllocPath = allocStr + } else { + prestate.Pre = inputData.Alloc } - prestate.Pre = inputData.Alloc if btStr != stdinSelector && btStr != "" { if err := readFile(btStr, "BT", &inputData.BT); err != nil { @@ -223,22 +223,57 @@ func Transition(ctx *cli.Context) error { return err } } - // Dump the execution result + // Dump the execution result. var ( - collector = make(Alloc) + collector Alloc btleaves map[common.Hash]hexutil.Bytes ) isBinary := chainConfig.IsUBT(big.NewInt(int64(prestate.Env.Number)), prestate.Env.Timestamp) - if !isBinary { + allocOutput := ctx.String(OutputAllocFlag.Name) + switch { + case !isBinary && allocOutput != "" && allocOutput != "stdout" && allocOutput != "stderr": + // Stream directly to the output file to avoid materializing the + // whole post-state in memory. dispatchOutput is told to skip alloc + // by clearing the output name. + if err := writeStreamedAlloc(filepath.Join(baseDir, allocOutput), s); err != nil { + return err + } + allocOutput = "" + case !isBinary: + collector = make(Alloc) s.DumpToCollector(collector, nil) - } else { + default: btleaves = make(map[common.Hash]hexutil.Bytes) if err := s.DumpBinTrieLeaves(btleaves); err != nil { return err } } + return dispatchOutput(ctx, baseDir, result, collector, allocOutput, body, btleaves) +} - return dispatchOutput(ctx, baseDir, result, collector, body, btleaves) +// writeStreamedAlloc writes the post-state alloc to path one account at a +// time, producing the same JSON shape as saveFile on an Alloc map. +func writeStreamedAlloc(path string, s *state.StateDB) error { + f, err := os.Create(path) + if err != nil { + return NewError(ErrorIO, fmt.Errorf("failed creating alloc output file: %v", err)) + } + bw := bufio.NewWriter(f) + sa := newStreamingAlloc(bw) + s.DumpToCollector(sa, nil) + if err := sa.Close(); err != nil { + f.Close() + return NewError(ErrorIO, fmt.Errorf("failed writing alloc output: %v", err)) + } + if err := bw.Flush(); err != nil { + f.Close() + return NewError(ErrorIO, fmt.Errorf("failed flushing alloc output: %v", err)) + } + if err := f.Close(); err != nil { + return NewError(ErrorIO, fmt.Errorf("failed closing alloc output file: %v", err)) + } + log.Info("Wrote file", "file", path) + return nil } func applyLondonChecks(env *stEnv, chainConfig *params.ChainConfig) error { @@ -327,6 +362,10 @@ func (g Alloc) OnAccount(addr *common.Address, dumpAccount state.DumpAccount) { if addr == nil { return } + g[*addr] = dumpAccountToTypesAccount(dumpAccount) +} + +func dumpAccountToTypesAccount(dumpAccount state.DumpAccount) types.Account { balance, _ := new(big.Int).SetString(dumpAccount.Balance, 0) var storage map[common.Hash]common.Hash if dumpAccount.Storage != nil { @@ -335,13 +374,64 @@ func (g Alloc) OnAccount(addr *common.Address, dumpAccount state.DumpAccount) { storage[k] = common.HexToHash(v) } } - genesisAccount := types.Account{ + return types.Account{ Code: dumpAccount.Code, Storage: storage, Balance: balance, Nonce: dumpAccount.Nonce, } - g[*addr] = genesisAccount +} + +// streamingAlloc is a DumpCollector that writes each account to w as it is +// visited, emitting a single JSON object keyed by address. Close must be +// called to emit the closing brace. +type streamingAlloc struct { + w io.Writer + wroteOne bool + err error +} + +func newStreamingAlloc(w io.Writer) *streamingAlloc { + return &streamingAlloc{w: w} +} + +func (s *streamingAlloc) write(b []byte) { + if s.err != nil { + return + } + _, s.err = s.w.Write(b) +} + +func (s *streamingAlloc) OnRoot(common.Hash) { + s.write([]byte{'{'}) +} + +func (s *streamingAlloc) OnAccount(addr *common.Address, dumpAccount state.DumpAccount) { + if s.err != nil || addr == nil { + return + } + keyJSON, err := json.Marshal(*addr) + if err != nil { + s.err = err + return + } + valueJSON, err := json.Marshal(dumpAccountToTypesAccount(dumpAccount)) + if err != nil { + s.err = err + return + } + if s.wroteOne { + s.write([]byte{','}) + } + s.write(keyJSON) + s.write([]byte{':'}) + s.write(valueJSON) + s.wroteOne = true +} + +func (s *streamingAlloc) Close() error { + s.write([]byte{'}'}) + return s.err } // saveFile marshals the object to the given file @@ -359,8 +449,9 @@ func saveFile(baseDir, filename string, data interface{}) error { } // dispatchOutput writes the output data to either stderr or stdout, or to the specified -// files -func dispatchOutput(ctx *cli.Context, baseDir string, result *ExecutionResult, alloc Alloc, body hexutil.Bytes, bt map[common.Hash]hexutil.Bytes) error { +// files. An empty allocOutput skips the alloc dispatch, which is used when the +// alloc has already been streamed to disk by the caller. +func dispatchOutput(ctx *cli.Context, baseDir string, result *ExecutionResult, alloc Alloc, allocOutput string, body hexutil.Bytes, bt map[common.Hash]hexutil.Bytes) error { stdOutObject := make(map[string]interface{}) stdErrObject := make(map[string]interface{}) dispatch := func(baseDir, fName, name string, obj interface{}) error { @@ -378,7 +469,7 @@ func dispatchOutput(ctx *cli.Context, baseDir string, result *ExecutionResult, a } return nil } - if err := dispatch(baseDir, ctx.String(OutputAllocFlag.Name), "alloc", alloc); err != nil { + if err := dispatch(baseDir, allocOutput, "alloc", alloc); err != nil { return err } if err := dispatch(baseDir, ctx.String(OutputResultFlag.Name), "result", result); err != nil { From 822e7c648618a43e7f8326868b9334b9976185c8 Mon Sep 17 00:00:00 2001 From: cui Date: Mon, 27 Apr 2026 22:13:42 +0800 Subject: [PATCH 29/80] accounts/scwallet: truncate before write (#34815) Co-authored-by: Guillaume Ballet <3272758+gballet@users.noreply.github.com> --- accounts/scwallet/hub.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/accounts/scwallet/hub.go b/accounts/scwallet/hub.go index 1b1899dc8e..185815365e 100644 --- a/accounts/scwallet/hub.go +++ b/accounts/scwallet/hub.go @@ -113,7 +113,7 @@ func (hub *Hub) readPairings() error { } func (hub *Hub) writePairings() error { - pairingFile, err := os.OpenFile(filepath.Join(hub.datadir, "smartcards.json"), os.O_RDWR|os.O_CREATE, 0755) + pairingFile, err := os.OpenFile(filepath.Join(hub.datadir, "smartcards.json"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755) if err != nil { return err } @@ -129,11 +129,8 @@ func (hub *Hub) writePairings() error { return err } - if _, err := pairingFile.Write(pairingData); err != nil { - return err - } - - return nil + _, err = pairingFile.Write(pairingData) + return err } func (hub *Hub) pairing(wallet *Wallet) *smartcardPairing { From 51c97216c5f1c512781de46a85e756c15ed2c888 Mon Sep 17 00:00:00 2001 From: Rahman Date: Tue, 28 Apr 2026 02:57:58 -0600 Subject: [PATCH 30/80] p2p/discover: fix timeout loop early exit when removing expired matchers (#34743) Save `el.Next()` before calling `plist.Remove(el)` so iteration continues correctly. Previously the loop exited after removing the first expired matcher because `Remove` invalidates the element's links. --------- Co-authored-by: Felix Lange --- p2p/discover/common.go | 15 +++++++++++++++ p2p/discover/v4_udp.go | 11 ++++------- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/p2p/discover/common.go b/p2p/discover/common.go index 767cc23b92..5513afd54d 100644 --- a/p2p/discover/common.go +++ b/p2p/discover/common.go @@ -17,9 +17,11 @@ package discover import ( + "container/list" "crypto/ecdsa" crand "crypto/rand" "encoding/binary" + "iter" "math/rand" "net" "net/netip" @@ -143,3 +145,16 @@ func (r *reseedingRandom) Shuffle(n int, swap func(i, j int)) { defer r.mu.Unlock() r.cur.Shuffle(n, swap) } + +// iterList iterates over the elements of the given list. +func iterList[T any](l *list.List) iter.Seq2[T, *list.Element] { + return func(yield func(T, *list.Element) bool) { + for el := l.Front(); el != nil; { + next := el.Next() + if !yield(el.Value.(T), el) { + return + } + el = next + } + } +} diff --git a/p2p/discover/v4_udp.go b/p2p/discover/v4_udp.go index dd3f363f7e..9e824dae18 100644 --- a/p2p/discover/v4_udp.go +++ b/p2p/discover/v4_udp.go @@ -446,9 +446,8 @@ func (t *UDPv4) loop() { } // Start the timer so it fires when the next pending reply has expired. now := time.Now() - for el := plist.Front(); el != nil; el = el.Next() { - nextTimeout = el.Value.(*replyMatcher) - if dist := nextTimeout.deadline.Sub(now); dist < 2*respTimeout { + for p, el := range iterList[*replyMatcher](plist) { + if dist := p.deadline.Sub(now); dist < 2*respTimeout { timeout.Reset(dist) return } @@ -478,8 +477,7 @@ func (t *UDPv4) loop() { case r := <-t.gotreply: var matched bool // whether any replyMatcher considered the reply acceptable. - for el := plist.Front(); el != nil; el = el.Next() { - p := el.Value.(*replyMatcher) + for p, el := range iterList[*replyMatcher](plist) { if p.from == r.from && p.ptype == r.data.Kind() && p.ip == r.ip { ok, requestDone := p.callback(r.data) matched = matched || ok @@ -499,8 +497,7 @@ func (t *UDPv4) loop() { nextTimeout = nil // Notify and remove callbacks whose deadline is in the past. - for el := plist.Front(); el != nil; el = el.Next() { - p := el.Value.(*replyMatcher) + for p, el := range iterList[*replyMatcher](plist) { if now.After(p.deadline) || now.Equal(p.deadline) { p.errc <- errTimeout plist.Remove(el) From 4dc7d461556a2ccde3cd6bbe816bd448755a0e07 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Tue, 28 Apr 2026 11:10:44 +0200 Subject: [PATCH 31/80] core/vm: implement stack arena (#33960) Here, we change the EVM stack implementation to use an 'arena', i.e. a shared allocation pool for sub-call stacks. The stack is now more GC-friendly, since it is a slice of uint256 values instead of a slice of pointers. Code that pushes an item to the stack has been changed to get() the top item, then overwrite it. The PR is a rewrite/rebase of #30362. --------- Co-authored-by: Martin Holst Swende Co-authored-by: Marius van der Wijden --- core/state_prefetcher.go | 1 + core/state_processor.go | 1 + core/vm/eips.go | 28 ++--- core/vm/evm.go | 9 ++ core/vm/gas_table.go | 30 +++--- core/vm/instructions.go | 74 ++++++------- core/vm/instructions_test.go | 77 +++++++++----- core/vm/interpreter.go | 6 +- core/vm/interpreter_test.go | 2 +- core/vm/memory_table.go | 46 ++++---- core/vm/operations_acl.go | 10 +- core/vm/operations_verkle.go | 8 +- core/vm/runtime/runtime_test.go | 60 +++++++++++ core/vm/stack.go | 177 ++++++++++++++++++++----------- eth/gasestimator/gasestimator.go | 1 + eth/state_accessor.go | 1 + eth/tracers/api.go | 9 +- internal/ethapi/api.go | 2 + internal/ethapi/simulate.go | 1 + miner/worker.go | 3 + 20 files changed, 350 insertions(+), 196 deletions(-) diff --git a/core/state_prefetcher.go b/core/state_prefetcher.go index c91d40d94f..ed292d0beb 100644 --- a/core/state_prefetcher.go +++ b/core/state_prefetcher.go @@ -93,6 +93,7 @@ func (p *statePrefetcher) Prefetch(block *types.Block, statedb *state.StateDB, c } // Execute the message to preload the implicit touched states evm := vm.NewEVM(NewEVMBlockContext(header, p.chain, nil), stateCpy, p.config, cfg) + defer evm.Release() // Convert the transaction into an executable message and pre-cache its sender msg, err := TransactionToMessage(tx, signer, header.BaseFee) diff --git a/core/state_processor.go b/core/state_processor.go index fda3bf8fe7..54ebbd047b 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -88,6 +88,7 @@ func (p *StateProcessor) Process(ctx context.Context, block *types.Block, stated // Apply pre-execution system calls. context = NewEVMBlockContext(header, p.chain, nil) evm := vm.NewEVM(context, tracingStateDB, config, cfg) + defer evm.Release() if beaconRoot := block.BeaconRoot(); beaconRoot != nil { ProcessBeaconBlockRoot(*beaconRoot, evm) diff --git a/core/vm/eips.go b/core/vm/eips.go index 54e5cb0c60..33af8fd4fd 100644 --- a/core/vm/eips.go +++ b/core/vm/eips.go @@ -24,7 +24,6 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/params" - "github.com/holiman/uint256" ) var activators = map[int]func(*JumpTable){ @@ -92,8 +91,7 @@ func enable1884(jt *JumpTable) { } func opSelfBalance(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { - balance := evm.StateDB.GetBalance(scope.Contract.Address()) - scope.Stack.push(balance) + scope.Stack.get().Set(evm.StateDB.GetBalance(scope.Contract.Address())) return nil, nil } @@ -111,8 +109,7 @@ func enable1344(jt *JumpTable) { // opChainID implements CHAINID opcode func opChainID(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { - chainId, _ := uint256.FromBig(evm.chainConfig.ChainID) - scope.Stack.push(chainId) + scope.Stack.get().SetFromBig(evm.chainConfig.ChainID) return nil, nil } @@ -222,8 +219,7 @@ func opTstore(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { // opBaseFee implements BASEFEE opcode func opBaseFee(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { - baseFee, _ := uint256.FromBig(evm.Context.BaseFee) - scope.Stack.push(baseFee) + scope.Stack.get().SetFromBig(evm.Context.BaseFee) return nil, nil } @@ -240,7 +236,7 @@ func enable3855(jt *JumpTable) { // opPush0 implements the PUSH0 opcode func opPush0(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { - scope.Stack.push(new(uint256.Int)) + scope.Stack.get().Clear() return nil, nil } @@ -291,8 +287,7 @@ func opBlobHash(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { // opBlobBaseFee implements BLOBBASEFEE opcode func opBlobBaseFee(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { - blobBaseFee, _ := uint256.FromBig(evm.Context.BlobBaseFee) - scope.Stack.push(blobBaseFee) + scope.Stack.get().SetFromBig(evm.Context.BlobBaseFee) return nil, nil } @@ -397,11 +392,11 @@ func opExtCodeCopyEIP4762(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, er func opPush1EIP4762(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { var ( codeLen = uint64(len(scope.Contract.Code)) - integer = new(uint256.Int) + elem = scope.Stack.get() ) *pc += 1 if *pc < codeLen { - scope.Stack.push(integer.SetUint64(uint64(scope.Contract.Code[*pc]))) + elem.SetUint64(uint64(scope.Contract.Code[*pc])) if !scope.Contract.IsDeployment && !scope.Contract.IsSystemCall && *pc%31 == 0 { // touch next chunk if PUSH1 is at the boundary. if so, *pc has @@ -414,7 +409,7 @@ func opPush1EIP4762(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { } } } else { - scope.Stack.push(integer.Clear()) + elem.Clear() } return nil, nil } @@ -426,12 +421,11 @@ func makePushEIP4762(size uint64, pushByteSize int) executionFunc { start = min(codeLen, int(*pc+1)) end = min(codeLen, start+pushByteSize) ) - scope.Stack.push(new(uint256.Int).SetBytes( + scope.Stack.get().SetBytes( common.RightPadBytes( scope.Contract.Code[start:end], pushByteSize, - )), - ) + )) if !scope.Contract.IsDeployment && !scope.Contract.IsSystemCall { contractAddr := scope.Contract.Address() @@ -583,7 +577,7 @@ func enable7702(jt *JumpTable) { // opSlotNum enables the SLOTNUM opcode func opSlotNum(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { - scope.Stack.push(uint256.NewInt(evm.Context.SlotNum)) + scope.Stack.get().SetUint64(evm.Context.SlotNum) return nil, nil } diff --git a/core/vm/evm.go b/core/vm/evm.go index 59e301c0a7..26b2f73a00 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -127,6 +127,8 @@ type EVM struct { readOnly bool // Whether to throw on stateful modifications returnData []byte // Last CALL's return data for subsequent reuse + + arena *stackArena } // NewEVM constructs an EVM instance with the supplied block context, state @@ -141,6 +143,7 @@ func NewEVM(blockCtx BlockContext, statedb StateDB, chainConfig *params.ChainCon chainConfig: chainConfig, chainRules: chainConfig.Rules(blockCtx.BlockNumber, blockCtx.Random != nil, blockCtx.Time), jumpDests: newMapJumpDests(), + arena: newArena(), } evm.precompiles = activePrecompiledContracts(evm.chainRules) @@ -223,6 +226,12 @@ func (evm *EVM) Cancel() { evm.abort.Store(true) } +// Release returns some memory allocated by the EVM, should be called after the EVM was used +// for the last time. Not necessary, but an improvement. +func (evm *EVM) Release() { + returnStack(evm.arena) +} + // Cancelled returns true if Cancel has been called func (evm *EVM) Cancelled() bool { return evm.abort.Load() diff --git a/core/vm/gas_table.go b/core/vm/gas_table.go index b3259b2ec7..046311f9cc 100644 --- a/core/vm/gas_table.go +++ b/core/vm/gas_table.go @@ -71,7 +71,7 @@ func memoryCopierGas(stackpos int) gasFunc { return GasCosts{}, err } // And gas for copying data, charged per word at param.CopyGas - words, overflow := stack.Back(stackpos).Uint64WithOverflow() + words, overflow := stack.back(stackpos).Uint64WithOverflow() if overflow { return GasCosts{}, ErrGasUintOverflow } @@ -100,7 +100,7 @@ func gasSStore(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySi return GasCosts{}, ErrWriteProtection } var ( - y, x = stack.Back(1), stack.Back(0) + y, x = stack.back(1), stack.back(0) current, original = evm.StateDB.GetStateAndCommittedState(contract.Address(), x.Bytes32()) ) // The legacy gas metering only takes into consideration the current state @@ -192,7 +192,7 @@ func gasSStoreEIP2200(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m } // Gas sentry honoured, do the actual gas calculation based on the stored value var ( - y, x = stack.Back(1), stack.Back(0) + y, x = stack.back(1), stack.back(0) current, original = evm.StateDB.GetStateAndCommittedState(contract.Address(), x.Bytes32()) ) value := common.Hash(y.Bytes32()) @@ -228,7 +228,7 @@ func gasSStoreEIP2200(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m func makeGasLog(n uint64) gasFunc { return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { - requestedSize, overflow := stack.Back(1).Uint64WithOverflow() + requestedSize, overflow := stack.back(1).Uint64WithOverflow() if overflow { return GasCosts{}, ErrGasUintOverflow } @@ -261,7 +261,7 @@ func gasKeccak256(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memor if err != nil { return GasCosts{}, err } - wordGas, overflow := stack.Back(1).Uint64WithOverflow() + wordGas, overflow := stack.back(1).Uint64WithOverflow() if overflow { return GasCosts{}, ErrGasUintOverflow } @@ -299,7 +299,7 @@ func gasCreate2(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memoryS if err != nil { return GasCosts{}, err } - wordGas, overflow := stack.Back(2).Uint64WithOverflow() + wordGas, overflow := stack.back(2).Uint64WithOverflow() if overflow { return GasCosts{}, ErrGasUintOverflow } @@ -317,7 +317,7 @@ func gasCreateEip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m if err != nil { return GasCosts{}, err } - size, overflow := stack.Back(2).Uint64WithOverflow() + size, overflow := stack.back(2).Uint64WithOverflow() if overflow { return GasCosts{}, ErrGasUintOverflow } @@ -336,7 +336,7 @@ func gasCreate2Eip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, if err != nil { return GasCosts{}, err } - size, overflow := stack.Back(2).Uint64WithOverflow() + size, overflow := stack.back(2).Uint64WithOverflow() if overflow { return GasCosts{}, ErrGasUintOverflow } @@ -352,7 +352,7 @@ func gasCreate2Eip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, } func gasExpFrontier(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { - expByteLen := uint64((stack.data[stack.len()-2].BitLen() + 7) / 8) + expByteLen := uint64((stack.back(1).BitLen() + 7) / 8) var ( gas = expByteLen * params.ExpByteFrontier // no overflow check required. Max is 256 * ExpByte gas @@ -365,7 +365,7 @@ func gasExpFrontier(evm *EVM, contract *Contract, stack *Stack, mem *Memory, mem } func gasExpEIP158(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { - expByteLen := uint64((stack.data[stack.len()-2].BitLen() + 7) / 8) + expByteLen := uint64((stack.back(1).BitLen() + 7) / 8) var ( gas = expByteLen * params.ExpByteEIP158 // no overflow check required. Max is 256 * ExpByte gas @@ -390,7 +390,7 @@ func makeCallVariantGasCost(intrinsicFunc gasFunc) gasFunc { if err != nil { return GasCosts{}, err } - evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas.RegularGas, intrinsic.RegularGas, stack.Back(0)) + evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas.RegularGas, intrinsic.RegularGas, stack.back(0)) if err != nil { return GasCosts{}, err } @@ -405,8 +405,8 @@ func makeCallVariantGasCost(intrinsicFunc gasFunc) gasFunc { func gasCallIntrinsic(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { var ( gas uint64 - transfersValue = !stack.Back(2).IsZero() - address = common.Address(stack.Back(1).Bytes20()) + transfersValue = !stack.back(2).IsZero() + address = common.Address(stack.back(1).Bytes20()) ) if evm.readOnly && transfersValue { return GasCosts{}, ErrWriteProtection @@ -453,7 +453,7 @@ func gasCallCodeIntrinsic(evm *EVM, contract *Contract, stack *Stack, mem *Memor gas uint64 overflow bool ) - if stack.Back(2).Sign() != 0 && !evm.chainRules.IsEIP4762 { + if stack.back(2).Sign() != 0 && !evm.chainRules.IsEIP4762 { gas += params.CallValueTransferGas } if gas, overflow = math.SafeAdd(gas, memoryGas); overflow { @@ -487,7 +487,7 @@ func gasSelfdestruct(evm *EVM, contract *Contract, stack *Stack, mem *Memory, me // EIP150 homestead gas reprice fork: if evm.chainRules.IsEIP150 { gas = params.SelfdestructGasEIP150 - var address = common.Address(stack.Back(0).Bytes20()) + var address = common.Address(stack.back(0).Bytes20()) if evm.chainRules.IsEIP158 { // if empty and transfers value diff --git a/core/vm/instructions.go b/core/vm/instructions.go index 3311af0d22..4b05092cc7 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -24,7 +24,6 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/params" - "github.com/holiman/uint256" ) func opAdd(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { @@ -244,7 +243,7 @@ func opKeccak256(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { } func opAddress(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { - scope.Stack.push(new(uint256.Int).SetBytes(scope.Contract.Address().Bytes())) + scope.Stack.get().SetBytes(scope.Contract.Address().Bytes()) return nil, nil } @@ -256,17 +255,17 @@ func opBalance(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { } func opOrigin(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { - scope.Stack.push(new(uint256.Int).SetBytes(evm.Origin.Bytes())) + scope.Stack.get().SetBytes(evm.Origin.Bytes()) return nil, nil } func opCaller(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { - scope.Stack.push(new(uint256.Int).SetBytes(scope.Contract.Caller().Bytes())) + scope.Stack.get().SetBytes(scope.Contract.Caller().Bytes()) return nil, nil } func opCallValue(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { - scope.Stack.push(scope.Contract.value) + scope.Stack.get().Set(scope.Contract.value) return nil, nil } @@ -282,7 +281,7 @@ func opCallDataLoad(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { } func opCallDataSize(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { - scope.Stack.push(new(uint256.Int).SetUint64(uint64(len(scope.Contract.Input)))) + scope.Stack.get().SetUint64(uint64(len(scope.Contract.Input))) return nil, nil } @@ -305,7 +304,7 @@ func opCallDataCopy(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { } func opReturnDataSize(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { - scope.Stack.push(new(uint256.Int).SetUint64(uint64(len(evm.returnData)))) + scope.Stack.get().SetUint64(uint64(len(evm.returnData))) return nil, nil } @@ -338,7 +337,7 @@ func opExtCodeSize(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { } func opCodeSize(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { - scope.Stack.push(new(uint256.Int).SetUint64(uint64(len(scope.Contract.Code)))) + scope.Stack.get().SetUint64(uint64(len(scope.Contract.Code))) return nil, nil } @@ -416,7 +415,7 @@ func opExtCodeHash(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { } func opGasprice(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { - scope.Stack.push(evm.GasPrice.Clone()) + scope.Stack.get().Set(evm.GasPrice) return nil, nil } @@ -451,35 +450,32 @@ func opBlockhash(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { } func opCoinbase(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { - scope.Stack.push(new(uint256.Int).SetBytes(evm.Context.Coinbase.Bytes())) + scope.Stack.get().SetBytes(evm.Context.Coinbase.Bytes()) return nil, nil } func opTimestamp(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { - scope.Stack.push(new(uint256.Int).SetUint64(evm.Context.Time)) + scope.Stack.get().SetUint64(evm.Context.Time) return nil, nil } func opNumber(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { - v, _ := uint256.FromBig(evm.Context.BlockNumber) - scope.Stack.push(v) + scope.Stack.get().SetFromBig(evm.Context.BlockNumber) return nil, nil } func opDifficulty(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { - v, _ := uint256.FromBig(evm.Context.Difficulty) - scope.Stack.push(v) + scope.Stack.get().SetFromBig(evm.Context.Difficulty) return nil, nil } func opRandom(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { - v := new(uint256.Int).SetBytes(evm.Context.Random.Bytes()) - scope.Stack.push(v) + scope.Stack.get().SetBytes(evm.Context.Random.Bytes()) return nil, nil } func opGasLimit(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { - scope.Stack.push(new(uint256.Int).SetUint64(evm.Context.GasLimit)) + scope.Stack.get().SetUint64(evm.Context.GasLimit) return nil, nil } @@ -556,17 +552,17 @@ func opJumpdest(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { } func opPc(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { - scope.Stack.push(new(uint256.Int).SetUint64(*pc)) + scope.Stack.get().SetUint64(*pc) return nil, nil } func opMsize(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { - scope.Stack.push(new(uint256.Int).SetUint64(uint64(scope.Memory.Len()))) + scope.Stack.get().SetUint64(uint64(scope.Memory.Len())) return nil, nil } func opGas(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { - scope.Stack.push(new(uint256.Int).SetUint64(scope.Contract.Gas.RegularGas)) + scope.Stack.get().SetUint64(scope.Contract.Gas.RegularGas) return nil, nil } @@ -1007,7 +1003,7 @@ func opDupN(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { } //The n‘th stack item is duplicated at the top of the stack. - scope.Stack.push(scope.Stack.Back(n - 1)) + scope.Stack.push(scope.Stack.back(n - 1)) *pc += 1 return nil, nil } @@ -1034,10 +1030,10 @@ func opSwapN(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { return nil, &ErrStackUnderflow{stackLen: scope.Stack.len(), required: n + 1} } - // The (n+1)‘th stack item is swapped with the top of the stack. - indexTop := scope.Stack.len() - 1 - indexN := scope.Stack.len() - 1 - n - scope.Stack.data[indexTop], scope.Stack.data[indexN] = scope.Stack.data[indexN], scope.Stack.data[indexTop] + // The (n+1)’th stack item is swapped with the top of the stack. + top := scope.Stack.peek() + nth := scope.Stack.back(n) + *top, *nth = *nth, *top *pc += 1 return nil, nil } @@ -1067,10 +1063,10 @@ func opExchange(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { return nil, &ErrStackUnderflow{stackLen: scope.Stack.len(), required: need} } - // The (n+1)‘th stack item is swapped with the (m+1)‘th stack item. - indexN := scope.Stack.len() - 1 - n - indexM := scope.Stack.len() - 1 - m - scope.Stack.data[indexN], scope.Stack.data[indexM] = scope.Stack.data[indexM], scope.Stack.data[indexN] + // The (n+1)’th stack item is swapped with the (m+1)’th stack item. + nth := scope.Stack.back(n) + mth := scope.Stack.back(m) + *nth, *mth = *mth, *nth *pc += 1 return nil, nil } @@ -1106,13 +1102,13 @@ func makeLog(size int) executionFunc { func opPush1(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { var ( codeLen = uint64(len(scope.Contract.Code)) - integer = new(uint256.Int) + elem = scope.Stack.get() ) *pc += 1 if *pc < codeLen { - scope.Stack.push(integer.SetUint64(uint64(scope.Contract.Code[*pc]))) + elem.SetUint64(uint64(scope.Contract.Code[*pc])) } else { - scope.Stack.push(integer.Clear()) + elem.Clear() } return nil, nil } @@ -1121,14 +1117,14 @@ func opPush1(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { func opPush2(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { var ( codeLen = uint64(len(scope.Contract.Code)) - integer = new(uint256.Int) + elem = scope.Stack.get() ) if *pc+2 < codeLen { - scope.Stack.push(integer.SetBytes2(scope.Contract.Code[*pc+1 : *pc+3])) + elem.SetBytes2(scope.Contract.Code[*pc+1 : *pc+3]) } else if *pc+1 < codeLen { - scope.Stack.push(integer.SetUint64(uint64(scope.Contract.Code[*pc+1]) << 8)) + elem.SetUint64(uint64(scope.Contract.Code[*pc+1]) << 8) } else { - scope.Stack.push(integer.Clear()) + elem.Clear() } *pc += 2 return nil, nil @@ -1142,13 +1138,13 @@ func makePush(size uint64, pushByteSize int) executionFunc { start = min(codeLen, int(*pc+1)) end = min(codeLen, start+pushByteSize) ) - a := new(uint256.Int).SetBytes(scope.Contract.Code[start:end]) + a := scope.Stack.get() + a.SetBytes(scope.Contract.Code[start:end]) // Missing bytes: pushByteSize - len(pushData) if missing := pushByteSize - (end - start); missing > 0 { a.Lsh(a, uint(8*missing)) } - scope.Stack.push(a) *pc += size return nil, nil } diff --git a/core/vm/instructions_test.go b/core/vm/instructions_test.go index dbe055f2ac..354d2ce5ab 100644 --- a/core/vm/instructions_test.go +++ b/core/vm/instructions_test.go @@ -25,6 +25,7 @@ import ( "os" "strings" "testing" + "time" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/math" @@ -98,7 +99,7 @@ func init() { func testTwoOperandOp(t *testing.T, tests []TwoOperandTestcase, opFn executionFunc, name string) { var ( evm = NewEVM(BlockContext{}, nil, params.TestChainConfig, Config{}) - stack = newstack() + stack = newStackForTesting() pc = uint64(0) ) @@ -109,8 +110,8 @@ func testTwoOperandOp(t *testing.T, tests []TwoOperandTestcase, opFn executionFu stack.push(x) stack.push(y) opFn(&pc, evm, &ScopeContext{nil, stack, nil}) - if len(stack.data) != 1 { - t.Errorf("Expected one item on stack after %v, got %d: ", name, len(stack.data)) + if stack.len() != 1 { + t.Errorf("Expected one item on stack after %v, got %d: ", name, stack.len()) } actual := stack.pop() @@ -196,7 +197,7 @@ func TestSAR(t *testing.T) { func TestAddMod(t *testing.T) { var ( evm = NewEVM(BlockContext{}, nil, params.TestChainConfig, Config{}) - stack = newstack() + stack = newStackForTesting() pc = uint64(0) ) tests := []struct { @@ -239,7 +240,7 @@ func TestWriteExpectedValues(t *testing.T) { getResult := func(args []*twoOperandParams, opFn executionFunc) []TwoOperandTestcase { var ( evm = NewEVM(BlockContext{}, nil, params.TestChainConfig, Config{}) - stack = newstack() + stack = newStackForTesting() pc = uint64(0) ) result := make([]TwoOperandTestcase, len(args)) @@ -282,23 +283,40 @@ func TestJsonTestcases(t *testing.T) { func opBenchmark(bench *testing.B, op executionFunc, args ...string) { var ( - evm = NewEVM(BlockContext{}, nil, params.TestChainConfig, Config{}) - stack = newstack() - scope = &ScopeContext{nil, stack, nil} + evm = NewEVM(BlockContext{}, nil, params.TestChainConfig, Config{}) + stack = newStackForTesting() + code = []byte{} + opPush32 = makePush(32, 32) ) // convert args intArgs := make([]*uint256.Int, len(args)) for i, arg := range args { + code = append(code, common.LeftPadBytes(common.Hex2Bytes(arg), 32)...) intArgs[i] = new(uint256.Int).SetBytes(common.Hex2Bytes(arg)) } pc := uint64(0) - for bench.Loop() { - for _, arg := range intArgs { - stack.push(arg) + scope := &ScopeContext{nil, stack, &Contract{Code: code}} + start := time.Now() + bench.ResetTimer() + for i := 0; i < bench.N; i++ { + for range len(args) { + opPush32(&pc, evm, scope) + pc += 32 } op(&pc, evm, scope) - stack.pop() + opPop(&pc, evm, scope) + } + bench.StopTimer() + elapsed := uint64(time.Since(start)) + if elapsed < 1 { + elapsed = 1 } + reqGas := uint64(len(args))*GasFastestStep + GasFastestStep + GasQuickStep + gasUsed := reqGas * uint64(bench.N) + bench.ReportMetric(float64(reqGas), "gas/op") + // Keep it as uint64, multiply 100 to get two digit float later + mgasps := (100 * 1000 * gasUsed) / elapsed + bench.ReportMetric(float64(mgasps)/100, "mgas/s") for i, arg := range args { want := new(uint256.Int).SetBytes(common.Hex2Bytes(arg)) @@ -519,7 +537,7 @@ func BenchmarkOpIsZero(b *testing.B) { func TestOpMstore(t *testing.T) { var ( evm = NewEVM(BlockContext{}, nil, params.TestChainConfig, Config{}) - stack = newstack() + stack = newStackForTesting() mem = NewMemory() ) mem.Resize(64) @@ -542,7 +560,7 @@ func TestOpMstore(t *testing.T) { func BenchmarkOpMstore(bench *testing.B) { var ( evm = NewEVM(BlockContext{}, nil, params.TestChainConfig, Config{}) - stack = newstack() + stack = newStackForTesting() mem = NewMemory() ) mem.Resize(64) @@ -561,7 +579,7 @@ func TestOpTstore(t *testing.T) { var ( statedb, _ = state.New(types.EmptyRootHash, state.NewDatabaseForTesting()) evm = NewEVM(BlockContext{}, statedb, params.TestChainConfig, Config{}) - stack = newstack() + stack = newStackForTesting() mem = NewMemory() caller = common.Address{} to = common.Address{1} @@ -600,7 +618,7 @@ func TestOpTstore(t *testing.T) { func BenchmarkOpKeccak256(bench *testing.B) { var ( evm = NewEVM(BlockContext{}, nil, params.TestChainConfig, Config{}) - stack = newstack() + stack = newStackForTesting() mem = NewMemory() ) mem.Resize(32) @@ -672,7 +690,7 @@ func TestCreate2Addresses(t *testing.T) { codeHash := crypto.Keccak256(code) address := crypto.CreateAddress2(origin, salt, codeHash) /* - stack := newstack() + stack := newStackForTesting() // salt, but we don't need that for this test stack.push(big.NewInt(int64(len(code)))) //size stack.push(big.NewInt(0)) // memstart @@ -701,12 +719,12 @@ func TestRandom(t *testing.T) { } { var ( evm = NewEVM(BlockContext{Random: &tt.random}, nil, params.TestChainConfig, Config{}) - stack = newstack() + stack = newStackForTesting() pc = uint64(0) ) opRandom(&pc, evm, &ScopeContext{nil, stack, nil}) - if len(stack.data) != 1 { - t.Errorf("Expected one item on stack after %v, got %d: ", tt.name, len(stack.data)) + if have, want := stack.len(), 1; have != want { + t.Errorf("test '%v': want %d item(s) on stack, have %d: ", tt.name, have, want) } actual := stack.pop() expected, overflow := uint256.FromBig(new(big.Int).SetBytes(tt.random.Bytes())) @@ -741,14 +759,14 @@ func TestBlobHash(t *testing.T) { } { var ( evm = NewEVM(BlockContext{}, nil, params.TestChainConfig, Config{}) - stack = newstack() + stack = newStackForTesting() pc = uint64(0) ) evm.SetTxContext(TxContext{BlobHashes: tt.hashes}) stack.push(uint256.NewInt(tt.idx)) opBlobHash(&pc, evm, &ScopeContext{nil, stack, nil}) - if len(stack.data) != 1 { - t.Errorf("Expected one item on stack after %v, got %d: ", tt.name, len(stack.data)) + if have, want := stack.len(), 1; have != want { + t.Errorf("test '%v': want %d item(s) on stack, have %d: ", tt.name, have, want) } actual := stack.pop() expected, overflow := uint256.FromBig(new(big.Int).SetBytes(tt.expect.Bytes())) @@ -844,7 +862,7 @@ func TestOpMCopy(t *testing.T) { } { var ( evm = NewEVM(BlockContext{}, nil, params.TestChainConfig, Config{}) - stack = newstack() + stack = newStackForTesting() pc = uint64(0) ) data := common.FromHex(strings.ReplaceAll(tc.pre, " ", "")) @@ -907,7 +925,7 @@ func TestPush(t *testing.T) { scope := &ScopeContext{ Memory: nil, - Stack: newstack(), + Stack: newStackForTesting(), Contract: &Contract{ Code: code, }, @@ -988,7 +1006,7 @@ func TestOpCLZ(t *testing.T) { } for _, tc := range tests { // prepare a fresh stack and PC - stack := newstack() + stack := newStackForTesting() pc := uint64(0) // parse input @@ -1111,7 +1129,7 @@ func TestEIP8024_Execution(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { code := common.FromHex(tc.codeHex) - stack := newstack() + stack := newStackForTesting() pc := uint64(0) scope := &ScopeContext{Stack: stack, Contract: &Contract{Code: code}} var err error @@ -1189,8 +1207,9 @@ func TestEIP8024_Execution(t *testing.T) { t.Fatalf("unexpected error: %v", err) } got := make([]uint64, 0, stack.len()) - for i := stack.len() - 1; i >= 0; i-- { - got = append(got, stack.data[i].Uint64()) + data := stack.Data() + for i := len(data) - 1; i >= 0; i-- { + got = append(got, data[i].Uint64()) } if len(got) != len(tc.wantVals) { t.Fatalf("stack len=%d; want %d", len(got), len(tc.wantVals)) diff --git a/core/vm/interpreter.go b/core/vm/interpreter.go index 9dc0a0b787..4c278fc857 100644 --- a/core/vm/interpreter.go +++ b/core/vm/interpreter.go @@ -116,8 +116,8 @@ func (evm *EVM) Run(contract *Contract, input []byte, readOnly bool) (ret []byte var ( op OpCode // current opcode jumpTable *JumpTable = evm.table - mem = NewMemory() // bound memory - stack = newstack() // local stack + mem = NewMemory() // bound memory + stack = evm.arena.stack() // local stack callContext = &ScopeContext{ Memory: mem, Stack: stack, @@ -140,7 +140,7 @@ func (evm *EVM) Run(contract *Contract, input []byte, readOnly bool) (ret []byte // so that it gets executed _after_: the OnOpcode needs the stacks before // they are returned to the pools defer func() { - returnStack(stack) + stack.release() mem.Free() }() contract.Input = input diff --git a/core/vm/interpreter_test.go b/core/vm/interpreter_test.go index 69c2316907..868cb12d04 100644 --- a/core/vm/interpreter_test.go +++ b/core/vm/interpreter_test.go @@ -83,7 +83,7 @@ func BenchmarkInterpreter(b *testing.B) { evm = NewEVM(BlockContext{BlockNumber: big.NewInt(1), Time: 1, Random: &common.Hash{}}, statedb, params.MergedTestChainConfig, Config{}) startGas uint64 = 100_000_000 value = uint256.NewInt(0) - stack = newstack() + stack = newStackForTesting() mem = NewMemory() contract = NewContract(common.Address{}, common.Address{}, value, NewGasBudget(startGas), nil) ) diff --git a/core/vm/memory_table.go b/core/vm/memory_table.go index 63ad967850..8f30cbeee6 100644 --- a/core/vm/memory_table.go +++ b/core/vm/memory_table.go @@ -17,59 +17,59 @@ package vm func memoryKeccak256(stack *Stack) (uint64, bool) { - return calcMemSize64(stack.Back(0), stack.Back(1)) + return calcMemSize64(stack.back(0), stack.back(1)) } func memoryCallDataCopy(stack *Stack) (uint64, bool) { - return calcMemSize64(stack.Back(0), stack.Back(2)) + return calcMemSize64(stack.back(0), stack.back(2)) } func memoryReturnDataCopy(stack *Stack) (uint64, bool) { - return calcMemSize64(stack.Back(0), stack.Back(2)) + return calcMemSize64(stack.back(0), stack.back(2)) } func memoryCodeCopy(stack *Stack) (uint64, bool) { - return calcMemSize64(stack.Back(0), stack.Back(2)) + return calcMemSize64(stack.back(0), stack.back(2)) } func memoryExtCodeCopy(stack *Stack) (uint64, bool) { - return calcMemSize64(stack.Back(1), stack.Back(3)) + return calcMemSize64(stack.back(1), stack.back(3)) } func memoryMLoad(stack *Stack) (uint64, bool) { - return calcMemSize64WithUint(stack.Back(0), 32) + return calcMemSize64WithUint(stack.back(0), 32) } func memoryMStore8(stack *Stack) (uint64, bool) { - return calcMemSize64WithUint(stack.Back(0), 1) + return calcMemSize64WithUint(stack.back(0), 1) } func memoryMStore(stack *Stack) (uint64, bool) { - return calcMemSize64WithUint(stack.Back(0), 32) + return calcMemSize64WithUint(stack.back(0), 32) } func memoryMcopy(stack *Stack) (uint64, bool) { - mStart := stack.Back(0) // stack[0]: dest - if stack.Back(1).Gt(mStart) { - mStart = stack.Back(1) // stack[1]: source + mStart := stack.back(0) // stack[0]: dest + if stack.back(1).Gt(mStart) { + mStart = stack.back(1) // stack[1]: source } - return calcMemSize64(mStart, stack.Back(2)) // stack[2]: length + return calcMemSize64(mStart, stack.back(2)) // stack[2]: length } func memoryCreate(stack *Stack) (uint64, bool) { - return calcMemSize64(stack.Back(1), stack.Back(2)) + return calcMemSize64(stack.back(1), stack.back(2)) } func memoryCreate2(stack *Stack) (uint64, bool) { - return calcMemSize64(stack.Back(1), stack.Back(2)) + return calcMemSize64(stack.back(1), stack.back(2)) } func memoryCall(stack *Stack) (uint64, bool) { - x, overflow := calcMemSize64(stack.Back(5), stack.Back(6)) + x, overflow := calcMemSize64(stack.back(5), stack.back(6)) if overflow { return 0, true } - y, overflow := calcMemSize64(stack.Back(3), stack.Back(4)) + y, overflow := calcMemSize64(stack.back(3), stack.back(4)) if overflow { return 0, true } @@ -80,11 +80,11 @@ func memoryCall(stack *Stack) (uint64, bool) { } func memoryDelegateCall(stack *Stack) (uint64, bool) { - x, overflow := calcMemSize64(stack.Back(4), stack.Back(5)) + x, overflow := calcMemSize64(stack.back(4), stack.back(5)) if overflow { return 0, true } - y, overflow := calcMemSize64(stack.Back(2), stack.Back(3)) + y, overflow := calcMemSize64(stack.back(2), stack.back(3)) if overflow { return 0, true } @@ -95,11 +95,11 @@ func memoryDelegateCall(stack *Stack) (uint64, bool) { } func memoryStaticCall(stack *Stack) (uint64, bool) { - x, overflow := calcMemSize64(stack.Back(4), stack.Back(5)) + x, overflow := calcMemSize64(stack.back(4), stack.back(5)) if overflow { return 0, true } - y, overflow := calcMemSize64(stack.Back(2), stack.Back(3)) + y, overflow := calcMemSize64(stack.back(2), stack.back(3)) if overflow { return 0, true } @@ -110,13 +110,13 @@ func memoryStaticCall(stack *Stack) (uint64, bool) { } func memoryReturn(stack *Stack) (uint64, bool) { - return calcMemSize64(stack.Back(0), stack.Back(1)) + return calcMemSize64(stack.back(0), stack.back(1)) } func memoryRevert(stack *Stack) (uint64, bool) { - return calcMemSize64(stack.Back(0), stack.Back(1)) + return calcMemSize64(stack.back(0), stack.back(1)) } func memoryLog(stack *Stack) (uint64, bool) { - return calcMemSize64(stack.Back(0), stack.Back(1)) + return calcMemSize64(stack.back(0), stack.back(1)) } diff --git a/core/vm/operations_acl.go b/core/vm/operations_acl.go index 313d03819e..86ac262a93 100644 --- a/core/vm/operations_acl.go +++ b/core/vm/operations_acl.go @@ -37,7 +37,7 @@ func makeGasSStoreFunc(clearingRefund uint64) gasFunc { } // Gas sentry honoured, do the actual gas calculation based on the stored value var ( - y, x = stack.Back(1), stack.peek() + y, x = stack.back(1), stack.peek() slot = common.Hash(x.Bytes32()) current, original = evm.StateDB.GetStateAndCommittedState(contract.Address(), slot) cost = uint64(0) @@ -158,7 +158,7 @@ func gasEip2929AccountCheck(evm *EVM, contract *Contract, stack *Stack, mem *Mem func makeCallVariantGasCallEIP2929(oldCalculator gasFunc, addressPosition int) gasFunc { return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { - addr := common.Address(stack.Back(addressPosition).Bytes20()) + addr := common.Address(stack.back(addressPosition).Bytes20()) // Check slot presence in the access list warmAccess := evm.StateDB.AddressInAccessList(addr) // The WarmStorageReadCostEIP2929 (100) is already deducted in the form of a constant cost, so @@ -269,7 +269,7 @@ func gasCallEIP7702(evm *EVM, contract *Contract, stack *Stack, mem *Memory, mem // Although it's checked in `gasCall`, EIP-7702 loads the target's code before // to determine if it is resolving a delegation. This could incorrectly record // the target in the block access list (BAL) if the call later fails. - transfersValue := !stack.Back(2).IsZero() + transfersValue := !stack.back(2).IsZero() if evm.readOnly && transfersValue { return GasCosts{}, ErrWriteProtection } @@ -281,7 +281,7 @@ func makeCallVariantGasCallEIP7702(intrinsicFunc gasFunc) gasFunc { var ( eip2929Cost uint64 eip7702Cost uint64 - addr = common.Address(stack.Back(1).Bytes20()) + addr = common.Address(stack.back(1).Bytes20()) ) // Perform EIP-2929 checks (stateless), checking address presence // in the accessList and charge the cold access accordingly. @@ -330,7 +330,7 @@ func makeCallVariantGasCallEIP7702(intrinsicFunc gasFunc) gasFunc { } // Calculate the gas budget for the nested call. The costs defined by // EIP-2929 and EIP-7702 have already been applied. - evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas.RegularGas, intrinsicCost.RegularGas, stack.Back(0)) + evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas.RegularGas, intrinsicCost.RegularGas, stack.back(0)) if err != nil { return GasCosts{}, err } diff --git a/core/vm/operations_verkle.go b/core/vm/operations_verkle.go index d57f2c4dcf..4d3960a174 100644 --- a/core/vm/operations_verkle.go +++ b/core/vm/operations_verkle.go @@ -56,7 +56,7 @@ func gasExtCodeHash4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, func makeCallVariantGasEIP4762(oldCalculator gasFunc, withTransferCosts bool) gasFunc { return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { var ( - target = common.Address(stack.Back(1).Bytes20()) + target = common.Address(stack.back(1).Bytes20()) witnessGas uint64 _, isPrecompile = evm.precompile(target) isSystemContract = target == params.HistoryStorageAddress @@ -64,7 +64,7 @@ func makeCallVariantGasEIP4762(oldCalculator gasFunc, withTransferCosts bool) ga // If value is transferred, it is charged before 1/64th // is subtracted from the available gas pool. - if withTransferCosts && !stack.Back(2).IsZero() { + if withTransferCosts && !stack.back(2).IsZero() { wantedValueTransferWitnessGas := evm.AccessEvents.ValueTransferGas(contract.Address(), target, contract.Gas.RegularGas) if wantedValueTransferWitnessGas > contract.Gas.RegularGas { return GasCosts{RegularGas: wantedValueTransferWitnessGas}, nil @@ -168,8 +168,8 @@ func gasCodeCopyEip4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, gas := gasCost.RegularGas if !contract.IsDeployment && !contract.IsSystemCall { var ( - codeOffset = stack.Back(1) - length = stack.Back(2) + codeOffset = stack.back(1) + length = stack.back(2) ) uint64CodeOffset, overflow := codeOffset.Uint64WithOverflow() if overflow { diff --git a/core/vm/runtime/runtime_test.go b/core/vm/runtime/runtime_test.go index 40fb770454..bc2ffd622d 100644 --- a/core/vm/runtime/runtime_test.go +++ b/core/vm/runtime/runtime_test.go @@ -959,3 +959,63 @@ func TestDelegatedAccountAccessCost(t *testing.T) { } } } + +func TestManyLargeStacks(t *testing.T) { + // This piece of code will push 512 items to the stack, and then call itself + // recursively. + code := make([]byte, 10) + for i := range code { + code[i] = byte(vm.PUSH0) + } + code = append(code, []byte{ + byte(vm.ADDRESS), // address to call + byte(vm.GAS), + byte(vm.CALL), + }...) + + main := common.HexToAddress("0xbb") + statedb, _ := state.New(types.EmptyRootHash, state.NewDatabaseForTesting()) + statedb.SetCode(main, code, tracing.CodeChangeUnspecified) + + //tracer := logger.NewJSONLogger(nil, os.Stdout) + var tracer *tracing.Hooks + _, _, err := Call(main, nil, &Config{ + GasLimit: 10_000_000, + State: statedb, + EVMConfig: vm.Config{ + Tracer: tracer, + }}) + if err != nil { + t.Fatal("didn't expect error", err) + } +} + +func BenchmarkLargeDeepStacks(b *testing.B) { + // This piece of code will push 512 items to the stack, and then call itself + // recursively. + code := make([]byte, 512) + for i := range code { + code[i] = byte(vm.PUSH0) + } + code = append(code, []byte{ + byte(vm.ADDRESS), // address to call + byte(vm.GAS), + byte(vm.CALL), + }...) + benchmarkNonModifyingCode(10_000_000, code, "deep-large-stacks-10M", "", b) +} + +func BenchmarkShortDeepStacks(b *testing.B) { + // This piece of code will push a few items to the stack, and then call itself + // recursively. + code := make([]byte, 8) + for i := range code { + code[i] = byte(vm.PUSH0) + } + code = append(code, []byte{ + byte(vm.ADDRESS), // address to call + byte(vm.GAS), + byte(vm.CALL), + }...) + benchmarkNonModifyingCode(10_000_000, code, "deep-short-stacks-10M", "", b) +} diff --git a/core/vm/stack.go b/core/vm/stack.go index 879dc9aa6d..d8000bc86d 100644 --- a/core/vm/stack.go +++ b/core/vm/stack.go @@ -17,111 +17,170 @@ package vm import ( + "slices" "sync" "github.com/holiman/uint256" ) +// stackArena is an arena which actual evm stacks use for data storage +type stackArena struct { + data []uint256.Int + top int // first free slot +} + +func newArena() *stackArena { + return stackPool.Get().(*stackArena) +} + +// 1025, because in stack() there is a condition check +// for the stack size that would fail if it was set to +// 1024. +const initialStackSize = 1025 + var stackPool = sync.Pool{ - New: func() interface{} { - return &Stack{data: make([]uint256.Int, 0, 16)} + New: func() any { + return &stackArena{ + data: make([]uint256.Int, initialStackSize), + } }, } +func returnStack(arena *stackArena) { + arena.top = 0 // defensive, not strictly needed as s.inner.top = s.bottom in release() + stackPool.Put(arena) +} + +// stack returns an instance of a stack which uses the underlying arena. The instance +// must be released by invoking the (*Stack).release() method +func (sa *stackArena) stack() *Stack { + // make sure every substack has at least 1024 elements + if len(sa.data) <= sa.top+1024 { + // we need to grow the arena + sa.data = slices.Grow(sa.data, 1024) + sa.data = sa.data[:cap(sa.data)] + } + return &Stack{ + bottom: sa.top, + size: 0, + inner: sa, + } +} + +// newStackForTesting is meant to be used solely for testing. It creates a stack +// backed by a newly allocated arena. +func newStackForTesting() *Stack { + arena := &stackArena{ + data: make([]uint256.Int, 1025), + } + return arena.stack() +} + // Stack is an object for basic stack operations. Items popped to the stack are // expected to be changed and modified. stack does not take care of adding newly // initialized objects. type Stack struct { - data []uint256.Int + bottom int // bottom is the index of the first element of this stack + size int // size is the number of elements in this stack + inner *stackArena } -func newstack() *Stack { - return stackPool.Get().(*Stack) +// release un-claims the area of the arena which was claimed by the stack. +func (s *Stack) release() { + // When the stack is returned, need to notify the arena that the new 'top' is + // the returned stack's bottom. + s.inner.top = s.bottom } -func returnStack(s *Stack) { - s.data = s.data[:0] - stackPool.Put(s) +// Data returns the underlying uint256.Int array. +func (s *Stack) Data() []uint256.Int { + return s.inner.data[s.bottom : s.bottom+s.size] } -// Data returns the underlying uint256.Int array. -func (st *Stack) Data() []uint256.Int { - return st.data +func (s *Stack) push(d *uint256.Int) { + elem := s.get() + *elem = *d } -func (st *Stack) push(d *uint256.Int) { - // NOTE push limit (1024) is checked in baseCheck - st.data = append(st.data, *d) +// get returns a pointer to a newly created element +// on top of the stack +func (s *Stack) get() *uint256.Int { + elem := &s.inner.data[s.inner.top] + s.inner.top++ + s.size++ + return elem } -func (st *Stack) pop() (ret uint256.Int) { - ret = st.data[len(st.data)-1] - st.data = st.data[:len(st.data)-1] - return +func (s *Stack) pop() uint256.Int { + s.inner.top-- + s.size-- + return s.inner.data[s.inner.top] } -func (st *Stack) len() int { - return len(st.data) +func (s *Stack) len() int { + return s.size } -func (st *Stack) swap1() { - st.data[st.len()-2], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-2] +func (s *Stack) swap1() { + s.inner.data[s.bottom+s.size-2], s.inner.data[s.bottom+s.size-1] = s.inner.data[s.bottom+s.size-1], s.inner.data[s.bottom+s.size-2] } -func (st *Stack) swap2() { - st.data[st.len()-3], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-3] +func (s *Stack) swap2() { + s.inner.data[s.bottom+s.size-3], s.inner.data[s.bottom+s.size-1] = s.inner.data[s.bottom+s.size-1], s.inner.data[s.bottom+s.size-3] } -func (st *Stack) swap3() { - st.data[st.len()-4], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-4] +func (s *Stack) swap3() { + s.inner.data[s.bottom+s.size-4], s.inner.data[s.bottom+s.size-1] = s.inner.data[s.bottom+s.size-1], s.inner.data[s.bottom+s.size-4] } -func (st *Stack) swap4() { - st.data[st.len()-5], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-5] +func (s *Stack) swap4() { + s.inner.data[s.bottom+s.size-5], s.inner.data[s.bottom+s.size-1] = s.inner.data[s.bottom+s.size-1], s.inner.data[s.bottom+s.size-5] } -func (st *Stack) swap5() { - st.data[st.len()-6], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-6] +func (s *Stack) swap5() { + s.inner.data[s.bottom+s.size-6], s.inner.data[s.bottom+s.size-1] = s.inner.data[s.bottom+s.size-1], s.inner.data[s.bottom+s.size-6] } -func (st *Stack) swap6() { - st.data[st.len()-7], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-7] +func (s *Stack) swap6() { + s.inner.data[s.bottom+s.size-7], s.inner.data[s.bottom+s.size-1] = s.inner.data[s.bottom+s.size-1], s.inner.data[s.bottom+s.size-7] } -func (st *Stack) swap7() { - st.data[st.len()-8], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-8] +func (s *Stack) swap7() { + s.inner.data[s.bottom+s.size-8], s.inner.data[s.bottom+s.size-1] = s.inner.data[s.bottom+s.size-1], s.inner.data[s.bottom+s.size-8] } -func (st *Stack) swap8() { - st.data[st.len()-9], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-9] +func (s *Stack) swap8() { + s.inner.data[s.bottom+s.size-9], s.inner.data[s.bottom+s.size-1] = s.inner.data[s.bottom+s.size-1], s.inner.data[s.bottom+s.size-9] } -func (st *Stack) swap9() { - st.data[st.len()-10], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-10] +func (s *Stack) swap9() { + s.inner.data[s.bottom+s.size-10], s.inner.data[s.bottom+s.size-1] = s.inner.data[s.bottom+s.size-1], s.inner.data[s.bottom+s.size-10] } -func (st *Stack) swap10() { - st.data[st.len()-11], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-11] +func (s *Stack) swap10() { + s.inner.data[s.bottom+s.size-11], s.inner.data[s.bottom+s.size-1] = s.inner.data[s.bottom+s.size-1], s.inner.data[s.bottom+s.size-11] } -func (st *Stack) swap11() { - st.data[st.len()-12], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-12] +func (s *Stack) swap11() { + s.inner.data[s.bottom+s.size-12], s.inner.data[s.bottom+s.size-1] = s.inner.data[s.bottom+s.size-1], s.inner.data[s.bottom+s.size-12] } -func (st *Stack) swap12() { - st.data[st.len()-13], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-13] +func (s *Stack) swap12() { + s.inner.data[s.bottom+s.size-13], s.inner.data[s.bottom+s.size-1] = s.inner.data[s.bottom+s.size-1], s.inner.data[s.bottom+s.size-13] } -func (st *Stack) swap13() { - st.data[st.len()-14], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-14] +func (s *Stack) swap13() { + s.inner.data[s.bottom+s.size-14], s.inner.data[s.bottom+s.size-1] = s.inner.data[s.bottom+s.size-1], s.inner.data[s.bottom+s.size-14] } -func (st *Stack) swap14() { - st.data[st.len()-15], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-15] +func (s *Stack) swap14() { + s.inner.data[s.bottom+s.size-15], s.inner.data[s.bottom+s.size-1] = s.inner.data[s.bottom+s.size-1], s.inner.data[s.bottom+s.size-15] } -func (st *Stack) swap15() { - st.data[st.len()-16], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-16] +func (s *Stack) swap15() { + s.inner.data[s.bottom+s.size-16], s.inner.data[s.bottom+s.size-1] = s.inner.data[s.bottom+s.size-1], s.inner.data[s.bottom+s.size-16] } -func (st *Stack) swap16() { - st.data[st.len()-17], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-17] +func (s *Stack) swap16() { + s.inner.data[s.bottom+s.size-17], s.inner.data[s.bottom+s.size-1] = s.inner.data[s.bottom+s.size-1], s.inner.data[s.bottom+s.size-17] } -func (st *Stack) dup(n int) { - st.push(&st.data[st.len()-n]) +func (s *Stack) dup(n int) { + s.inner.data[s.bottom+s.size] = s.inner.data[s.bottom+s.size-n] + s.size++ + s.inner.top++ } -func (st *Stack) peek() *uint256.Int { - return &st.data[st.len()-1] +func (s *Stack) peek() *uint256.Int { + return &s.inner.data[s.bottom+s.size-1] } -// Back returns the n'th item in stack -func (st *Stack) Back(n int) *uint256.Int { - return &st.data[st.len()-n-1] +// back returns the n'th item in stack +func (s *Stack) back(n int) *uint256.Int { + return &s.inner.data[s.bottom+s.size-n-1] } diff --git a/eth/gasestimator/gasestimator.go b/eth/gasestimator/gasestimator.go index ad6491fd93..efb089d456 100644 --- a/eth/gasestimator/gasestimator.go +++ b/eth/gasestimator/gasestimator.go @@ -244,6 +244,7 @@ func run(ctx context.Context, call *core.Message, opts *Options) (*core.Executio evmContext.BlobBaseFee = new(big.Int) } evm := vm.NewEVM(evmContext, dirtyState, opts.Config, vm.Config{NoBaseFee: true}) + defer evm.Release() // Monitor the outer context and interrupt the EVM upon cancellation. To avoid // a dangling goroutine until the outer estimation finishes, create an internal diff --git a/eth/state_accessor.go b/eth/state_accessor.go index 7467e1e590..a806a4fc56 100644 --- a/eth/state_accessor.go +++ b/eth/state_accessor.go @@ -247,6 +247,7 @@ func (eth *Ethereum) stateAtTransaction(ctx context.Context, block *types.Block, // Insert parent beacon block root in the state as per EIP-4788. context := core.NewEVMBlockContext(block.Header(), eth.blockchain, nil) evm := vm.NewEVM(context, statedb, eth.blockchain.Config(), vm.Config{}) + defer evm.Release() if beaconRoot := block.BeaconRoot(); beaconRoot != nil { core.ProcessBeaconBlockRoot(*beaconRoot, evm) } diff --git a/eth/tracers/api.go b/eth/tracers/api.go index b5ddc78a10..dae11b81de 100644 --- a/eth/tracers/api.go +++ b/eth/tracers/api.go @@ -379,6 +379,7 @@ func (api *API) traceChain(start, end *types.Block, config *TraceConfig, closed if api.backend.ChainConfig().IsPrague(next.Number(), next.Time()) { core.ProcessParentBlockHash(next.ParentHash(), evm) } + evm.Release() // Clean out any pending release functions of trace state. Note this // step must be done after constructing tracing state, because the // tracing state of block next depends on the parent state and construction @@ -524,6 +525,7 @@ func (api *API) IntermediateRoots(ctx context.Context, hash common.Hash, config deleteEmptyObjects = chainConfig.IsEIP158(block.Number()) ) evm := vm.NewEVM(vmctx, statedb, chainConfig, vm.Config{}) + defer evm.Release() if beaconRoot := block.BeaconRoot(); beaconRoot != nil { core.ProcessBeaconBlockRoot(*beaconRoot, evm) } @@ -584,6 +586,7 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac blockCtx := core.NewEVMBlockContext(block.Header(), api.chainContext(ctx), nil) evm := vm.NewEVM(blockCtx, statedb, api.backend.ChainConfig(), vm.Config{}) + defer evm.Release() if beaconRoot := block.BeaconRoot(); beaconRoot != nil { core.ProcessBeaconBlockRoot(*beaconRoot, evm) } @@ -673,6 +676,7 @@ func (api *API) traceBlockParallel(ctx context.Context, block *types.Block, stat var failed error blockCtx := core.NewEVMBlockContext(block.Header(), api.chainContext(ctx), nil) evm := vm.NewEVM(blockCtx, statedb, api.backend.ChainConfig(), vm.Config{}) + defer evm.Release() txloop: for i, tx := range txs { @@ -758,6 +762,7 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block } evm := vm.NewEVM(vmctx, statedb, chainConfig, vm.Config{}) + defer evm.Release() if beaconRoot := block.BeaconRoot(); beaconRoot != nil { core.ProcessBeaconBlockRoot(*beaconRoot, evm) } @@ -805,6 +810,7 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block tracer.OnTxStart(evm.GetVMContext(), tx, msg.From) } _, err = core.ApplyMessage(evm, msg, nil) + evm.Release() if writer != nil { writer.Flush() } @@ -817,7 +823,7 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block } // Finalize the state so any modifications are written to the trie // Only delete empty objects if EIP158/161 (a.k.a Spurious Dragon) is in effect - statedb.Finalise(evm.ChainConfig().IsEIP158(block.Number())) + statedb.Finalise(chainConfig.IsEIP158(block.Number())) // If we've traced the transaction we were looking for, abort if tx.Hash() == txHash { @@ -999,6 +1005,7 @@ func (api *API) traceTx(ctx context.Context, tx *types.Transaction, message *cor } tracingStateDB := state.NewHookedState(statedb, tracer.Hooks) evm := vm.NewEVM(vmctx, tracingStateDB, api.backend.ChainConfig(), vm.Config{Tracer: tracer.Hooks, NoBaseFee: true}) + defer evm.Release() if precompiles != nil { evm.SetPrecompiles(precompiles) } diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 149e12c5b8..e8669b86c6 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -775,6 +775,7 @@ func applyMessage(ctx context.Context, b Backend, args TransactionArgs, state *s blockContext.BlobBaseFee = new(big.Int) } evm := b.GetEVM(ctx, state, header, vmConfig, blockContext) + defer evm.Release() if precompiles != nil { evm.SetPrecompiles(precompiles) } @@ -1390,6 +1391,7 @@ func AccessList(ctx context.Context, b Backend, blockNrOrHash rpc.BlockNumberOrH evm.Context.BlobBaseFee = new(big.Int) } res, err := core.ApplyMessage(evm, msg, nil) + evm.Release() if err != nil { return nil, 0, nil, fmt.Errorf("failed to apply transaction: %v err: %v", args.ToTransaction(types.LegacyTxType).Hash(), err) } diff --git a/internal/ethapi/simulate.go b/internal/ethapi/simulate.go index d18561760e..e3a14bf5d6 100644 --- a/internal/ethapi/simulate.go +++ b/internal/ethapi/simulate.go @@ -312,6 +312,7 @@ func (sim *simulator) processBlock(ctx context.Context, block *simBlock, header, tracingStateDB = state.NewHookedState(sim.state, hooks) } evm := vm.NewEVM(blockContext, tracingStateDB, sim.chainConfig, *vmConfig) + defer evm.Release() // It is possible to override precompiles with EVM bytecode, or // move them to another address. if precompiles != nil { diff --git a/miner/worker.go b/miner/worker.go index ae5d6c306f..158f1b26c0 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -83,6 +83,9 @@ func (env *environment) txFitsSize(tx *types.Transaction) bool { // discard terminates the background threads before discarding it. func (env *environment) discard() { env.state.StopPrefetcher() + if env.evm != nil { + env.evm.Release() + } } const ( From b5d9c8d1c2745256b6fb45e1108758720cc4868b Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Tue, 28 Apr 2026 19:10:15 +0800 Subject: [PATCH 32/80] core: implement BAL reader for prefetching (#33737) --- core/state/reader_eip_7928.go | 247 +++++++++++++++++++++++++++++ core/state/reader_eip_7928_test.go | 145 +++++++++++++++++ 2 files changed, 392 insertions(+) create mode 100644 core/state/reader_eip_7928.go create mode 100644 core/state/reader_eip_7928_test.go diff --git a/core/state/reader_eip_7928.go b/core/state/reader_eip_7928.go new file mode 100644 index 0000000000..ff315ac5eb --- /dev/null +++ b/core/state/reader_eip_7928.go @@ -0,0 +1,247 @@ +// Copyright 2026 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 . + +package state + +import ( + "sync" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/core/types/bal" +) + +// The EIP27928 reader utilizes a hierarchical architecture to optimize state +// access during block execution: +// +// - Base layer: The reader is initialized with the pre-transition state root, +// providing the access of the state. +// +// - Prefetching Layer: This base reader is wrapped by newPrefetchStateReader. +// Using an Access List hint, it asynchronously fetches required state data +// in the background, minimizing I/O blocking during transaction processing. +// +// - Execution Layer: To support parallel transaction execution within the EIP +// 7928 context, readers are wrapped in ReaderWithBlockLevelAccessList. +// This layer provides a "unified view" by merging the pre-transition state +// with mutated states from preceding transactions in the block. +// +// - Tracking Layer: Finally, the readerTracker wraps the execution reader to +// capture all state reads made during a specific transaction. These individual +// reads are subsequently merged to construct a comprehensive access list +// for the entire block. +// +// The architecture can be illustrated by the diagram below: +// +// ┌──────────────┴──────────────┐ ┌──────────────┴──────────────┐ +// │ ReaderWithBlockLevelAL │ │ ReaderWithBlockLevelAL │ +// │ (Pre-state + Mutations) │ │ (Pre-state + Mutations) │ +// └──────────────┬──────────────┘ └──────────────┬──────────────┘ +// │ │ +// └────────────────┬─────────────────┘ +// │ +// ┌──────────────┴──────────────┐ +// │ newPrefetchStateReader │ (Async I/O) +// │ (Access List Hint driven) │ +// └──────────────┬──────────────┘ +// │ +// ┌──────────────┴──────────────┐ +// │ Base Reader │ (State Root) +// │ (State & Contract Code) │ +// └─────────────────────────────┘ + +// Note: The block producer, which is responsible for generating the block +// along with the block-level access list, does not maintain the internal +// hierarchy (e.g., PrefetchStateReader or ReaderWithBlockLevelAL). +// Instead, it directly utilizes the readerTracker, wrapped around the +// base reader, to construct the access list. + +type fetchTask struct { + addr common.Address + slots []common.Hash +} + +func (t *fetchTask) weight() int { return 1 + len(t.slots) } + +type prefetchStateReader struct { + StateReader + + tasks []*fetchTask + nThreads int + done chan struct{} + term chan struct{} + closeOnce sync.Once +} + +// nolint:unused +func newPrefetchStateReader(reader StateReader, accessList map[common.Address][]common.Hash, nThreads int) *prefetchStateReader { + tasks := make([]*fetchTask, 0, len(accessList)) + for addr, slots := range accessList { + tasks = append(tasks, &fetchTask{ + addr: addr, + slots: slots, + }) + } + return newPrefetchStateReaderInternal(reader, tasks, nThreads) +} + +func newPrefetchStateReaderInternal(reader StateReader, tasks []*fetchTask, nThreads int) *prefetchStateReader { + r := &prefetchStateReader{ + StateReader: reader, + tasks: tasks, + nThreads: nThreads, + done: make(chan struct{}), + term: make(chan struct{}), + } + go r.prefetch() + return r +} + +func (r *prefetchStateReader) Close() { + r.closeOnce.Do(func() { + close(r.term) + <-r.done + }) +} + +func (r *prefetchStateReader) Wait() error { + select { + case <-r.term: + return nil + case <-r.done: + return nil + } +} + +func (r *prefetchStateReader) prefetch() { + defer close(r.done) + + if len(r.tasks) == 0 { + return + } + var total int + for _, t := range r.tasks { + total += t.weight() + } + var ( + wg sync.WaitGroup + unit = (total + r.nThreads - 1) / r.nThreads // round-up the per worker unit + ) + for i := 0; i < r.nThreads; i++ { + start := i * unit + if start >= total { + break + } + limit := (i + 1) * unit + if i == r.nThreads-1 { + limit = total + } + // Schedule the worker for prefetching, the items on the range [start, limit) + // is exclusively assigned for this worker. + wg.Add(1) + go func(workerID, startW, endW int) { + r.process(startW, endW) + wg.Done() + }(i, start, limit) + } + wg.Wait() +} + +func (r *prefetchStateReader) process(start, limit int) { + var total = 0 + for _, t := range r.tasks { + tw := t.weight() + if total+tw > start { + s := 0 + if start > total { + s = start - total + } + l := tw + if limit < total+tw { + l = limit - total + } + for j := s; j < l; j++ { + select { + case <-r.term: + return + default: + if j == 0 { + r.StateReader.Account(t.addr) + } else { + r.StateReader.Storage(t.addr, t.slots[j-1]) + } + } + } + } + total += tw + if total >= limit { + return + } + } +} + +// ReaderWithBlockLevelAccessList provides state access that reflects the +// pre-transition state combined with the mutations made by transactions +// prior to TxIndex. +type ReaderWithBlockLevelAccessList struct { + Reader + AccessList *bal.ConstructionBlockAccessList + TxIndex int +} + +// NewReaderWithBlockLevelAccessList constructs a reader for accessing states +// with the mutations made by transactions prior to txIndex. +// +// The txIndex refers to the call frame as such: +// - 0 for pre‑execution system contract calls. +// - 1 … n for transactions (in block order). +// - n + 1 for post‑execution system contract calls. +func NewReaderWithBlockLevelAccessList(base Reader, accessList *bal.ConstructionBlockAccessList, txIndex int) *ReaderWithBlockLevelAccessList { + return &ReaderWithBlockLevelAccessList{ + Reader: base, + AccessList: accessList, + TxIndex: txIndex, + } +} + +// Account implements Reader, returning the account with the specific address. +func (r *ReaderWithBlockLevelAccessList) Account(addr common.Address) (*types.StateAccount, error) { + panic("implement me") +} + +// Storage implements Reader, returning the storage slot with the specific +// address and slot key. +func (r *ReaderWithBlockLevelAccessList) Storage(addr common.Address, slot common.Hash) (common.Hash, error) { + panic("implement me") +} + +// Has implements Reader, returning the flag indicating whether the contract +// code with specified address and hash exists or not. +func (r *ReaderWithBlockLevelAccessList) Has(addr common.Address, codeHash common.Hash) bool { + panic("implement me") +} + +// Code implements Reader, returning the contract code with specified address +// and hash. +func (r *ReaderWithBlockLevelAccessList) Code(addr common.Address, codeHash common.Hash) ([]byte, error) { + panic("implement me") +} + +// CodeSize implements Reader, returning the contract code size with specified +// address and hash. +func (r *ReaderWithBlockLevelAccessList) CodeSize(addr common.Address, codeHash common.Hash) (int, error) { + panic("implement me") +} diff --git a/core/state/reader_eip_7928_test.go b/core/state/reader_eip_7928_test.go new file mode 100644 index 0000000000..b2d432258c --- /dev/null +++ b/core/state/reader_eip_7928_test.go @@ -0,0 +1,145 @@ +// Copyright 2026 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 . + +package state + +import ( + "fmt" + "math/rand" + "sync" + "testing" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/internal/testrand" +) + +type countingStateReader struct { + accounts map[common.Address]int + storages map[common.Address]map[common.Hash]int + lock sync.Mutex +} + +func newRefStateReader() *countingStateReader { + return &countingStateReader{ + accounts: make(map[common.Address]int), + storages: make(map[common.Address]map[common.Hash]int), + } +} + +func (r *countingStateReader) validate(total int) error { + var sum int + for addr, n := range r.accounts { + if n != 1 { + return fmt.Errorf("duplicated account access: %x-%d", addr, n) + } + sum += 1 + + slots, exists := r.storages[addr] + if !exists { + continue + } + for key, n := range slots { + if n != 1 { + return fmt.Errorf("duplicated storage access: %x-%x-%d", addr, key, n) + } + sum += 1 + } + } + for addr := range r.storages { + _, exists := r.accounts[addr] + if !exists { + return fmt.Errorf("dangling storage access: %x", addr) + } + } + if sum != total { + return fmt.Errorf("unexpected number of access, want: %d, got: %d", total, sum) + } + return nil +} + +func (r *countingStateReader) Account(addr common.Address) (*types.StateAccount, error) { + r.lock.Lock() + defer r.lock.Unlock() + + r.accounts[addr] += 1 + return nil, nil +} +func (r *countingStateReader) Storage(addr common.Address, slot common.Hash) (common.Hash, error) { + r.lock.Lock() + defer r.lock.Unlock() + + slots, exists := r.storages[addr] + if !exists { + slots = make(map[common.Hash]int) + r.storages[addr] = slots + } + slots[slot] += 1 + return common.Hash{}, nil +} + +func makeFetchTasks(n int) ([]*fetchTask, int) { + var ( + total int + tasks []*fetchTask + ) + for i := 0; i < n; i++ { + var slots []common.Hash + if rand.Intn(3) != 0 { + for j := 0; j < rand.Intn(100); j++ { + slots = append(slots, testrand.Hash()) + } + } + tasks = append(tasks, &fetchTask{ + addr: testrand.Address(), + slots: slots, + }) + total += len(slots) + 1 + } + return tasks, total +} + +func TestPrefetchReader(t *testing.T) { + type suite struct { + tasks []*fetchTask + threads int + total int + } + var suites []suite + for i := 0; i < 100; i++ { + tasks, total := makeFetchTasks(100) + suites = append(suites, suite{ + tasks: tasks, + threads: rand.Intn(30) + 1, + total: total, + }) + } + // num(tasks) < num(threads) + tasks, total := makeFetchTasks(1) + suites = append(suites, suite{ + tasks: tasks, + threads: 100, + total: total, + }) + for _, s := range suites { + r := newRefStateReader() + pr := newPrefetchStateReaderInternal(r, s.tasks, s.threads) + pr.Wait() + if err := r.validate(s.total); err != nil { + t.Fatal(err) + } + } +} From 0c0d299c52c84141ad1f8ee5ea56df0003739afb Mon Sep 17 00:00:00 2001 From: cui Date: Tue, 28 Apr 2026 20:33:40 +0800 Subject: [PATCH 33/80] core/state: opt stateObject.GetState (#34825) --- core/state/state_object.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/core/state/state_object.go b/core/state/state_object.go index 264dfd920d..8e72486825 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -162,8 +162,11 @@ func (s *stateObject) getPrefetchedTrie() Trie { // GetState retrieves a value associated with the given storage key. func (s *stateObject) GetState(key common.Hash) common.Hash { - value, _ := s.getState(key) - return value + value, dirty := s.dirtyStorage[key] + if dirty { + return value + } + return s.GetCommittedState(key) } // getState retrieves a value associated with the given storage key, along with From db8d6abced13e81b9a493d5a315164d3cbc9872f Mon Sep 17 00:00:00 2001 From: Miki Noir Date: Tue, 28 Apr 2026 14:36:01 +0100 Subject: [PATCH 34/80] accounts/keystore: enable fsnotify watcher on linux/arm64 (#34834) --- accounts/keystore/watch.go | 4 ++-- accounts/keystore/watch_fallback.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/accounts/keystore/watch.go b/accounts/keystore/watch.go index 1bef321cd1..e2fcd1871a 100644 --- a/accounts/keystore/watch.go +++ b/accounts/keystore/watch.go @@ -14,8 +14,8 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . -//go:build (darwin && !ios && cgo) || freebsd || (linux && !arm64) || netbsd || solaris -// +build darwin,!ios,cgo freebsd linux,!arm64 netbsd solaris +//go:build (darwin && !ios && cgo) || freebsd || linux || netbsd || solaris +// +build darwin,!ios,cgo freebsd linux netbsd solaris package keystore diff --git a/accounts/keystore/watch_fallback.go b/accounts/keystore/watch_fallback.go index e3c133b3f6..ee1b989e63 100644 --- a/accounts/keystore/watch_fallback.go +++ b/accounts/keystore/watch_fallback.go @@ -14,8 +14,8 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . -//go:build (darwin && !cgo) || ios || (linux && arm64) || windows || (!darwin && !freebsd && !linux && !netbsd && !solaris) -// +build darwin,!cgo ios linux,arm64 windows !darwin,!freebsd,!linux,!netbsd,!solaris +//go:build (darwin && !cgo) || ios || windows || (!darwin && !freebsd && !linux && !netbsd && !solaris) +// +build darwin,!cgo ios windows !darwin,!freebsd,!linux,!netbsd,!solaris // This is the fallback implementation of directory watching. // It is used on unsupported platforms. From 01036bed831186d892a3e3e35b32fcf304d4cc29 Mon Sep 17 00:00:00 2001 From: Giulio rebuffo Date: Tue, 28 Apr 2026 17:25:16 +0200 Subject: [PATCH 35/80] core: skip tx gas cap after Amsterdam (#34841) EIP-7825 caps the transaction gas limit at `MaxTxGas`, but after Amsterdam/EIP-8037 the transaction gas limit can include state gas reservoir in addition to the regular gas dimension. Applying the Osaka cap to the full `tx.Gas()` rejects otherwise valid Amsterdam transactions that need more than `MaxTxGas` total gas because of state gas, while their regular gas use remains within the intended limit. This changes geth to stop applying the full transaction gas cap once Amsterdam is active: - txpool stateless validation no longer rejects `tx.Gas() > MaxTxGas` under Amsterdam - legacy pool reorg cleanup does not purge high-total-gas transactions at the Osaka transition if Amsterdam is also active - execution precheck mirrors the txpool behavior and does not reject high-total-gas messages under Amsterdam The block gas limit check remains in place, so transactions still cannot request more total gas than the current block gas limit. Validation run: ``` go test ./core/txpool ./core/txpool/legacypool go test ./core -run TestStateProcessorErrors ``` --------- Co-authored-by: Gary Rong --- cmd/evm/internal/t8ntool/transaction.go | 4 +++- core/state_transition.go | 3 ++- core/txpool/legacypool/legacypool.go | 6 ++++-- core/txpool/validation.go | 2 +- eth/gasestimator/gasestimator.go | 8 ++++---- miner/worker.go | 2 +- 6 files changed, 15 insertions(+), 10 deletions(-) diff --git a/cmd/evm/internal/t8ntool/transaction.go b/cmd/evm/internal/t8ntool/transaction.go index ad89876601..7e068c06af 100644 --- a/cmd/evm/internal/t8ntool/transaction.go +++ b/cmd/evm/internal/t8ntool/transaction.go @@ -185,7 +185,9 @@ func Transaction(ctx *cli.Context) error { } } - if chainConfig.IsOsaka(new(big.Int), 0) && tx.Gas() > params.MaxTxGas { + isOsaka := chainConfig.IsOsaka(new(big.Int), 0) + isAmsterdam := chainConfig.IsAmsterdam(new(big.Int), 0) + if isOsaka && !isAmsterdam && tx.Gas() > params.MaxTxGas { r.Error = errors.New("gas limit exceeds maximum") } results = append(results, r) diff --git a/core/state_transition.go b/core/state_transition.go index c3ebffd060..c7b0593857 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -344,9 +344,10 @@ func (st *stateTransition) preCheck() error { } } isOsaka := st.evm.ChainConfig().IsOsaka(st.evm.Context.BlockNumber, st.evm.Context.Time) + isAmsterdam := st.evm.ChainConfig().IsAmsterdam(st.evm.Context.BlockNumber, st.evm.Context.Time) if !msg.SkipTransactionChecks { // Verify tx gas limit does not exceed EIP-7825 cap. - if isOsaka && msg.GasLimit > params.MaxTxGas { + if isOsaka && !isAmsterdam && msg.GasLimit > params.MaxTxGas { return fmt.Errorf("%w (cap: %d, tx: %d)", ErrGasLimitTooHigh, params.MaxTxGas, msg.GasLimit) } // Make sure the sender is an EOA diff --git a/core/txpool/legacypool/legacypool.go b/core/txpool/legacypool/legacypool.go index 78a0161c41..00630de04c 100644 --- a/core/txpool/legacypool/legacypool.go +++ b/core/txpool/legacypool/legacypool.go @@ -1222,8 +1222,10 @@ func (pool *LegacyPool) runReorg(done chan struct{}, reset *txpoolResetRequest, pool.mu.Lock() if reset != nil { if reset.newHead != nil && reset.oldHead != nil { - // Discard the transactions with the gas limit higher than the cap. - if pool.chainconfig.IsOsaka(reset.newHead.Number, reset.newHead.Time) && !pool.chainconfig.IsOsaka(reset.oldHead.Number, reset.oldHead.Time) { + // Discard the transactions with the gas limit higher than the cap at the + // Osaka fork boundary. + if pool.chainconfig.IsOsaka(reset.newHead.Number, reset.newHead.Time) && + !pool.chainconfig.IsOsaka(reset.oldHead.Number, reset.oldHead.Time) { var hashes []common.Hash pool.all.Range(func(hash common.Hash, tx *types.Transaction) bool { if tx.Gas() > params.MaxTxGas { diff --git a/core/txpool/validation.go b/core/txpool/validation.go index 85bf65ac40..6891dc94d2 100644 --- a/core/txpool/validation.go +++ b/core/txpool/validation.go @@ -92,7 +92,7 @@ func ValidateTransaction(tx *types.Transaction, head *types.Header, signer types return err } } - if rules.IsOsaka && tx.Gas() > params.MaxTxGas { + if rules.IsOsaka && !rules.IsAmsterdam && tx.Gas() > params.MaxTxGas { return fmt.Errorf("%w (cap: %d, tx: %d)", core.ErrGasLimitTooHigh, params.MaxTxGas, tx.Gas()) } // Transactions can't be negative. This may never happen using RLP decoded diff --git a/eth/gasestimator/gasestimator.go b/eth/gasestimator/gasestimator.go index efb089d456..ace0752037 100644 --- a/eth/gasestimator/gasestimator.go +++ b/eth/gasestimator/gasestimator.go @@ -63,10 +63,10 @@ func Estimate(ctx context.Context, call *core.Message, opts *Options, gasCap uin } // Cap the maximum gas allowance according to EIP-7825 if the estimation targets Osaka - if hi > params.MaxTxGas { - if opts.Config.IsOsaka(opts.Header.Number, opts.Header.Time) { - hi = params.MaxTxGas - } + isOsaka := opts.Config.IsOsaka(opts.Header.Number, opts.Header.Time) + isAmsterdam := opts.Config.IsAmsterdam(opts.Header.Number, opts.Header.Time) + if hi > params.MaxTxGas && isOsaka && !isAmsterdam { + hi = params.MaxTxGas } // Normalize the max fee per gas the call is willing to spend. diff --git a/miner/worker.go b/miner/worker.go index 158f1b26c0..42e3695025 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -563,7 +563,7 @@ func (miner *Miner) fillTransactions(ctx context.Context, interrupt *atomic.Int3 if env.header.ExcessBlobGas != nil { filter.BlobFee = uint256.MustFromBig(eip4844.CalcBlobFee(miner.chainConfig, env.header)) } - if miner.chainConfig.IsOsaka(env.header.Number, env.header.Time) { + if miner.chainConfig.IsOsaka(env.header.Number, env.header.Time) && !miner.chainConfig.IsAmsterdam(env.header.Number, env.header.Time) { filter.GasLimitCap = params.MaxTxGas } filter.BlobTxs = false From 75a64ee3413ef324641b9306efdb5d57e8a01cf8 Mon Sep 17 00:00:00 2001 From: Bosul Mun Date: Thu, 30 Apr 2026 17:55:26 +0200 Subject: [PATCH 36/80] eth/downloader: drop peers sending invalid bodies or receipts (#34745) - Fixes an error shadowing issue in the deliver() function, where a stale result from GetDeliverySlot caused the original failure to be overwritten by errStaleDelivery. - Adds errInvalidBody and errInvalidReceipt to the downloader error checks to properly drop peers who sent invalid responses. --------- Co-authored-by: Felix Lange --- eth/downloader/downloader_test.go | 35 ++++++++++++++++++- eth/downloader/fetchers_concurrent.go | 49 +++++++++++++++++---------- eth/downloader/queue.go | 29 ++++++++-------- 3 files changed, 80 insertions(+), 33 deletions(-) diff --git a/eth/downloader/downloader_test.go b/eth/downloader/downloader_test.go index 9280d455fb..6d5d159631 100644 --- a/eth/downloader/downloader_test.go +++ b/eth/downloader/downloader_test.go @@ -96,6 +96,7 @@ func (dl *downloadTester) newPeer(id string, version uint, blocks []*types.Block id: id, chain: newTestBlockchain(blocks), withholdBodies: make(map[common.Hash]struct{}), + dropped: make(chan error, 1), } dl.peers[id] = peer @@ -121,8 +122,11 @@ func (dl *downloadTester) dropPeer(id string) { type downloadTesterPeer struct { dl *downloadTester withholdBodies map[common.Hash]struct{} + corruptBodies bool // if set, the peer serves incorrect blocks id string chain *core.BlockChain + + dropped chan error // signaled when res.Done receives an error } func unmarshalRlpHeaders(rlpdata []rlp.RawValue) []*types.Header { @@ -236,6 +240,11 @@ func (dlp *downloadTesterPeer) RequestBodies(hashes []common.Hash, sink chan *et txsHashes[i] = hash uncleHashes[i] = types.CalcUncleHash(body.Uncles) } + if dlp.corruptBodies { + for i := range txsHashes { + txsHashes[i] = common.Hash{0xff} + } + } req := ð.Request{ Peer: dlp.id, } @@ -248,10 +257,16 @@ func (dlp *downloadTesterPeer) RequestBodies(hashes []common.Hash, sink chan *et WithdrawalRoots: withdrawalHashes, }, Time: 1, - Done: make(chan error, 1), // Ignore the returned status + Done: make(chan error), } go func() { sink <- res + if err := <-res.Done; err != nil { + select { + case dlp.dropped <- err: + default: + } + } }() return req, nil } @@ -704,3 +719,21 @@ func testSyncProgress(t *testing.T, protocol uint, mode SyncMode) { t.Fatalf("Failed to sync chain in three seconds") } } + +func TestInvalidBodyPeerDrop(t *testing.T) { + tester := newTester(t, FullSync) + defer tester.terminate() + + chain := testChainBase.shorten(blockCacheMaxItems - 15) + peer := tester.newPeer("corrupt", eth.ETH69, chain.blocks[1:]) + peer.corruptBodies = true + + if err := tester.downloader.BeaconSync(chain.blocks[len(chain.blocks)-1].Header(), nil); err != nil { + t.Fatalf("failed to beacon-sync chain: %v", err) + } + select { + case <-peer.dropped: + case <-time.After(1 * time.Minute): + t.Fatal("peer was not dropped") + } +} diff --git a/eth/downloader/fetchers_concurrent.go b/eth/downloader/fetchers_concurrent.go index 9d8cd114c1..51bf3404bd 100644 --- a/eth/downloader/fetchers_concurrent.go +++ b/eth/downloader/fetchers_concurrent.go @@ -323,25 +323,32 @@ func (d *Downloader) concurrentFetch(queue typedQueue) error { delete(pending, res.Req.Peer) delete(stales, res.Req.Peer) - // Signal the dispatcher that the round trip is done. We'll drop the - // peer if the data turns out to be junk. - res.Done <- nil - res.Req.Close() - // If the peer was previously banned and failed to deliver its pack // in a reasonable time frame, ignore its message. - if peer := d.peers.Peer(res.Req.Peer); peer != nil { - // Deliver the received chunk of data and check chain validity - accepted, err := queue.deliver(peer, res) - if errors.Is(err, errInvalidChain) { - return err - } - // Unless a peer delivered something completely else than requested (usually - // caused by a timed out request which came through in the end), set it to - // idle. If the delivery's stale, the peer should have already been idled. - if !errors.Is(err, errStaleDelivery) { - queue.updateCapacity(peer, accepted, res.Time) - } + peer := d.peers.Peer(res.Req.Peer) + if peer == nil { + res.Done <- nil + res.Req.Close() + continue + } + // Deliver the received chunk of data and check chain validity + accepted, err := queue.deliver(peer, res) + // Unless a peer delivered something completely else than requested (usually + // caused by a timed out request which came through in the end), set it to + // idle. If the delivery's stale, the peer should have already been idled. + if !errors.Is(err, errStaleDelivery) { + queue.updateCapacity(peer, accepted, res.Time) + } + res.Done <- validityErrorOfRequest(err) + res.Req.Close() + + if errors.Is(err, errInvalidChain) { + // errInvalidChain is the signal that processing of items failed internally, + // even though the items were validly encoded. + // + // This can be due to invalid blocks, or a database error. + // The sync cycle should be aborted for such errors, so we return it here. + return err } case cont := <-queue.waker(): @@ -352,3 +359,11 @@ func (d *Downloader) concurrentFetch(queue typedQueue) error { } } } + +// validityErrorOfRequest returns err if it is related to block validity, and nil otherwise. +func validityErrorOfRequest(err error) error { + if errors.Is(err, errInvalidBody) || errors.Is(err, errInvalidReceipt) { + return err + } + return nil +} diff --git a/eth/downloader/queue.go b/eth/downloader/queue.go index c0cb9b174a..dd17b7f1ed 100644 --- a/eth/downloader/queue.go +++ b/eth/downloader/queue.go @@ -671,10 +671,10 @@ func (q *queue) deliver(id string, taskPool map[common.Hash]*types.Header, } // Assemble each of the results with their headers and retrieved data parts var ( - accepted int - failure error - i int - hashes []common.Hash + accepted int + failure error + i int + foundStale bool ) for _, header := range request.Headers { // Short circuit assembly if no more fetch results are found @@ -686,42 +686,41 @@ func (q *queue) deliver(id string, taskPool map[common.Hash]*types.Header, failure = err break } - hashes = append(hashes, header.Hash()) i++ } for _, header := range request.Headers[:i] { if res, stale, err := q.resultCache.GetDeliverySlot(header.Number.Uint64()); err == nil && !stale { reconstruct(accepted, res) + accepted++ } else { - // else: between here and above, some other peer filled this result, + // Between here and above, some other peer filled this result, // or it was indeed a no-op. This should not happen, but if it does it's // not something to panic about log.Error("Delivery stale", "stale", stale, "number", header.Number.Uint64(), "err", err) - failure = errStaleDelivery + foundStale = true } // Clean up a successful fetch - delete(taskPool, hashes[accepted]) - accepted++ + delete(taskPool, header.Hash()) } resDropMeter.Mark(int64(results - accepted)) // Return all failed or missing fetches to the queue - for _, header := range request.Headers[accepted:] { + for _, header := range request.Headers[i:] { taskQueue.Push(header, -int64(header.Number.Uint64())) } // Wake up Results if accepted > 0 { q.active.Signal() } - if failure == nil { - return accepted, nil + if failure != nil { + return accepted, failure } // If none of the data was good, it's a stale delivery - if accepted > 0 { - return accepted, fmt.Errorf("partial failure: %v", failure) + if foundStale { + return accepted, errStaleDelivery } - return accepted, fmt.Errorf("%w: %v", failure, errStaleDelivery) + return accepted, nil } // Prepare configures the result cache to allow accepting and caching inbound From 19dc690af86278b4e84fd3d7eee9fd810bcbac25 Mon Sep 17 00:00:00 2001 From: cui Date: Fri, 1 May 2026 00:24:22 +0800 Subject: [PATCH 37/80] triedb/pathdb: fix layer 5 key range in account iterator traversal test (#34639) The layer-5 diff condition used `i > 50 || i < 85`, which is true for almost all keys in the 0..255 loop. Use `i > 50 && i < 85` so layer 5 only covers the intended band (51..84), consistent with the snapshot iterator test fix. --- triedb/pathdb/iterator_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/triedb/pathdb/iterator_test.go b/triedb/pathdb/iterator_test.go index adb534f47d..2197e85272 100644 --- a/triedb/pathdb/iterator_test.go +++ b/triedb/pathdb/iterator_test.go @@ -369,7 +369,7 @@ func TestAccountIteratorTraversalValues(t *testing.T) { if i%8 == 0 { e[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 4, i) } - if i > 50 || i < 85 { + if i > 50 && i < 85 { f[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 5, i) } if i%64 == 0 { From 68646229a0275acb18fceef83bf1733ced88ef00 Mon Sep 17 00:00:00 2001 From: cui Date: Fri, 1 May 2026 20:10:41 +0800 Subject: [PATCH 38/80] internal/era/onedb: return false if err (#34816) Next() function in RawIterator returned true on decompression errors.Now it returns false on those cases. Redundant error check on cmd/era/main.go is also removed. --------- Co-authored-by: Bosul Mun --- cmd/era/main.go | 3 --- internal/era/onedb/iterator.go | 8 ++++---- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/cmd/era/main.go b/cmd/era/main.go index 1c26f44ad4..43279e7001 100644 --- a/cmd/era/main.go +++ b/cmd/era/main.go @@ -337,9 +337,6 @@ func checkAccumulator(e era.Era) error { // accumulation across the entire set and are verified at the end. for it.Next() { // 1) next() walks the block index, so we're able to implicitly verify it. - if it.Error() != nil { - return fmt.Errorf("error reading block %d: %w", it.Number(), it.Error()) - } block, receipts, err := it.BlockAndReceipts() if err != nil { return fmt.Errorf("error reading block %d: %w", it.Number(), err) diff --git a/internal/era/onedb/iterator.go b/internal/era/onedb/iterator.go index b80fbabbc5..21dc5acbe0 100644 --- a/internal/era/onedb/iterator.go +++ b/internal/era/onedb/iterator.go @@ -156,22 +156,22 @@ func (it *RawIterator) Next() bool { var n int64 if it.Header, n, it.err = newSnappyReader(it.e.s, era.TypeCompressedHeader, off); it.err != nil { it.clear() - return true + return false } off += n if it.Body, n, it.err = newSnappyReader(it.e.s, era.TypeCompressedBody, off); it.err != nil { it.clear() - return true + return false } off += n if it.Receipts, n, it.err = newSnappyReader(it.e.s, era.TypeCompressedReceipts, off); it.err != nil { it.clear() - return true + return false } off += n if it.TotalDifficulty, _, it.err = it.e.s.ReaderAt(era.TypeTotalDifficulty, off); it.err != nil { it.clear() - return true + return false } it.next += 1 return true From a15778c52f3f5e5922d6ecd29bf62df500733c6b Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Fri, 1 May 2026 15:28:19 +0200 Subject: [PATCH 39/80] trie: group 2^N binary trie nodes in serialization (#34794) This PR addresses one of the biggest performance issue with binary tries: storing each internal node individually bloats the index, the disk, and triggers a lot of write amplifications. To fix this issue, this PR serializes groups of nodes together. Because we are still looking for the ideal group size, the "depth" of the group tree is made a parameter, but that will be removed in the future, once the perfect size is known. This is a rebase of #33658 --------- Co-authored-by: Copilot --- cmd/evm/internal/t8ntool/transition.go | 8 +- cmd/geth/bintrie_convert.go | 4 +- cmd/geth/bintrie_convert_test.go | 8 +- cmd/geth/main.go | 1 + cmd/utils/flags.go | 10 + core/bintrie_witness_test.go | 2 + core/blockchain.go | 12 +- core/genesis.go | 5 +- core/genesis_test.go | 29 +-- core/state/database_ubt.go | 2 +- core/state/reader.go | 2 +- eth/backend.go | 1 + eth/ethconfig/config.go | 7 + eth/ethconfig/gen_config.go | 6 + trie/bintrie/binary_node.go | 11 + trie/bintrie/binary_node_test.go | 48 +++-- trie/bintrie/hashed_node_test.go | 2 +- trie/bintrie/internal_node_test.go | 7 +- trie/bintrie/iterator.go | 2 +- trie/bintrie/stem_node_test.go | 5 +- trie/bintrie/store_commit.go | 279 ++++++++++++++++++++----- trie/bintrie/trie.go | 38 ++-- trie/bintrie/trie_test.go | 5 +- triedb/database.go | 24 ++- triedb/pathdb/journal.go | 14 +- 25 files changed, 399 insertions(+), 133 deletions(-) diff --git a/cmd/evm/internal/t8ntool/transition.go b/cmd/evm/internal/t8ntool/transition.go index e0bb3a449d..89b703d3b8 100644 --- a/cmd/evm/internal/t8ntool/transition.go +++ b/cmd/evm/internal/t8ntool/transition.go @@ -546,7 +546,7 @@ func BinKeys(ctx *cli.Context) error { db := triedb.NewDatabase(rawdb.NewMemoryDatabase(), triedb.UBTDefaults) defer db.Close() - bt, err := genBinTrieFromAlloc(alloc, db) + bt, err := genBinTrieFromAlloc(alloc, db, triedb.UBTDefaults.BinTrieGroupDepth) if err != nil { return fmt.Errorf("error generating bt: %w", err) } @@ -590,7 +590,7 @@ func BinTrieRoot(ctx *cli.Context) error { db := triedb.NewDatabase(rawdb.NewMemoryDatabase(), triedb.UBTDefaults) defer db.Close() - bt, err := genBinTrieFromAlloc(alloc, db) + bt, err := genBinTrieFromAlloc(alloc, db, triedb.UBTDefaults.BinTrieGroupDepth) if err != nil { return fmt.Errorf("error generating bt: %w", err) } @@ -600,8 +600,8 @@ func BinTrieRoot(ctx *cli.Context) error { } // TODO(@CPerezz): Should this go to `bintrie` module? -func genBinTrieFromAlloc(alloc core.GenesisAlloc, db database.NodeDatabase) (*bintrie.BinaryTrie, error) { - bt, err := bintrie.NewBinaryTrie(types.EmptyBinaryHash, db) +func genBinTrieFromAlloc(alloc core.GenesisAlloc, db database.NodeDatabase, groupDepth int) (*bintrie.BinaryTrie, error) { + bt, err := bintrie.NewBinaryTrie(types.EmptyBinaryHash, db, groupDepth) if err != nil { return nil, err } diff --git a/cmd/geth/bintrie_convert.go b/cmd/geth/bintrie_convert.go index 43d2e629ac..46cb3aa7e4 100644 --- a/cmd/geth/bintrie_convert.go +++ b/cmd/geth/bintrie_convert.go @@ -151,7 +151,7 @@ func convertToBinaryTrie(ctx *cli.Context) error { }) defer destTriedb.Close() - binTrie, err := bintrie.NewBinaryTrie(types.EmptyBinaryHash, destTriedb) + binTrie, err := bintrie.NewBinaryTrie(types.EmptyBinaryHash, destTriedb, ctx.Int(utils.BinTrieGroupDepthFlag.Name)) if err != nil { return fmt.Errorf("failed to create binary trie: %w", err) } @@ -319,7 +319,7 @@ func commitBinaryTrie(bt *bintrie.BinaryTrie, currentRoot common.Hash, destDB *t runtime.GC() debug.FreeOSMemory() - bt, err := bintrie.NewBinaryTrie(newRoot, destDB) + bt, err := bintrie.NewBinaryTrie(newRoot, destDB, bt.GroupDepth()) if err != nil { return nil, common.Hash{}, fmt.Errorf("failed to reload binary trie: %w", err) } diff --git a/cmd/geth/bintrie_convert_test.go b/cmd/geth/bintrie_convert_test.go index 50ae752358..32e8c7e55b 100644 --- a/cmd/geth/bintrie_convert_test.go +++ b/cmd/geth/bintrie_convert_test.go @@ -87,7 +87,7 @@ func TestBintrieConvert(t *testing.T) { }) defer destTriedb.Close() - bt, err := bintrie.NewBinaryTrie(types.EmptyBinaryHash, destTriedb) + bt, err := bintrie.NewBinaryTrie(types.EmptyBinaryHash, destTriedb, 8) if err != nil { t.Fatalf("failed to create binary trie: %v", err) } @@ -98,7 +98,7 @@ func TestBintrieConvert(t *testing.T) { } t.Logf("Binary trie root: %x", currentRoot) - bt2, err := bintrie.NewBinaryTrie(currentRoot, destTriedb) + bt2, err := bintrie.NewBinaryTrie(currentRoot, destTriedb, 8) if err != nil { t.Fatalf("failed to reload binary trie: %v", err) } @@ -194,7 +194,7 @@ func TestBintrieConvertDeleteSource(t *testing.T) { PathDB: pathdb.Defaults, }) - bt, err := bintrie.NewBinaryTrie(types.EmptyBinaryHash, destTriedb) + bt, err := bintrie.NewBinaryTrie(types.EmptyBinaryHash, destTriedb, 8) if err != nil { t.Fatalf("failed to create binary trie: %v", err) } @@ -209,7 +209,7 @@ func TestBintrieConvertDeleteSource(t *testing.T) { } srcTriedb2.Close() - bt2, err := bintrie.NewBinaryTrie(newRoot, destTriedb) + bt2, err := bintrie.NewBinaryTrie(newRoot, destTriedb, 8) if err != nil { t.Fatalf("failed to reload binary trie after deletion: %v", err) } diff --git a/cmd/geth/main.go b/cmd/geth/main.go index ae869ec970..c8d7abc65b 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -95,6 +95,7 @@ var ( utils.StateHistoryFlag, utils.TrienodeHistoryFlag, utils.TrienodeHistoryFullValueCheckpointFlag, + utils.BinTrieGroupDepthFlag, utils.LightKDFFlag, utils.EthRequiredBlocksFlag, utils.LegacyWhitelistFlag, // deprecated diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 9d996f15cb..cc4c3bff5c 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -297,6 +297,12 @@ var ( Value: ethconfig.Defaults.EnableStateSizeTracking, Category: flags.StateCategory, } + BinTrieGroupDepthFlag = &cli.IntFlag{ + Name: "bintrie.groupdepth", + Usage: "Number of levels per serialized group in binary trie (1-8, default 5). Lower values create smaller groups with more nodes.", + Value: 5, + Category: flags.StateCategory, + } StateHistoryFlag = &cli.Uint64Flag{ Name: "history.state", Usage: "Number of recent blocks to retain state history for, only relevant in state.scheme=path (default = 90,000 blocks, 0 = entire chain)", @@ -1817,6 +1823,9 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) { if ctx.IsSet(TrienodeHistoryFullValueCheckpointFlag.Name) { cfg.NodeFullValueCheckpoint = uint32(ctx.Uint(TrienodeHistoryFullValueCheckpointFlag.Name)) } + if ctx.IsSet(BinTrieGroupDepthFlag.Name) { + cfg.BinTrieGroupDepth = ctx.Int(BinTrieGroupDepthFlag.Name) + } if ctx.IsSet(StateSchemeFlag.Name) { cfg.StateScheme = ctx.String(StateSchemeFlag.Name) } @@ -2433,6 +2442,7 @@ func MakeChain(ctx *cli.Context, stack *node.Node, readonly bool) (*core.BlockCh StateHistory: ctx.Uint64(StateHistoryFlag.Name), TrienodeHistory: ctx.Int64(TrienodeHistoryFlag.Name), NodeFullValueCheckpoint: uint32(ctx.Uint(TrienodeHistoryFullValueCheckpointFlag.Name)), + BinTrieGroupDepth: ctx.Int(BinTrieGroupDepthFlag.Name), // Disable transaction indexing/unindexing. TxLookupLimit: -1, diff --git a/core/bintrie_witness_test.go b/core/bintrie_witness_test.go index 1b033151d3..66feef0675 100644 --- a/core/bintrie_witness_test.go +++ b/core/bintrie_witness_test.go @@ -92,6 +92,7 @@ func TestProcessUBT(t *testing.T) { // genesis := gspec.MustCommit(bcdb, triedb) options := DefaultConfig().WithStateScheme(rawdb.PathScheme) options.SnapshotLimit = 0 + options.BinTrieGroupDepth = triedb.DefaultBinTrieGroupDepth blockchain, _ := NewBlockChain(bcdb, gspec, beacon.New(ethash.NewFaker()), options) defer blockchain.Stop() @@ -218,6 +219,7 @@ func TestProcessParentBlockHash(t *testing.T) { t.Run("UBT", func(t *testing.T) { db := rawdb.NewMemoryDatabase() cacheConfig := DefaultConfig().WithStateScheme(rawdb.PathScheme) + cacheConfig.BinTrieGroupDepth = triedb.DefaultBinTrieGroupDepth cacheConfig.SnapshotLimit = 0 triedb := triedb.NewDatabase(db, cacheConfig.triedbConfig(true)) statedb, _ := state.New(types.EmptyBinaryHash, state.NewDatabase(triedb, nil)) diff --git a/core/blockchain.go b/core/blockchain.go index 296ef6bc16..f21a1462ea 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -170,9 +170,10 @@ type BlockChainConfig struct { TrieNoAsyncFlush bool // Whether the asynchronous buffer flushing is disallowed TrieJournalDirectory string // Directory path to the journal used for persisting trie data across node restarts - Preimages bool // Whether to store preimage of trie key to the disk - StateScheme string // Scheme used to store ethereum states and merkle tree nodes on top - ArchiveMode bool // Whether to enable the archive mode + Preimages bool // Whether to store preimage of trie key to the disk + StateScheme string // Scheme used to store ethereum states and merkle tree nodes on top + ArchiveMode bool // Whether to enable the archive mode + BinTrieGroupDepth int // Number of levels per serialized group in binary trie (1-8) // Number of blocks from the chain head for which state histories are retained. // If set to 0, all state histories across the entire chain will be retained; @@ -260,8 +261,9 @@ func (cfg BlockChainConfig) WithNoAsyncFlush(on bool) *BlockChainConfig { // triedbConfig derives the configures for trie database. func (cfg *BlockChainConfig) triedbConfig(isUBT bool) *triedb.Config { config := &triedb.Config{ - Preimages: cfg.Preimages, - IsUBT: isUBT, + Preimages: cfg.Preimages, + IsUBT: isUBT, + BinTrieGroupDepth: cfg.BinTrieGroupDepth, } if cfg.StateScheme == rawdb.HashScheme { config.HashDB = &hashdb.Config{ diff --git a/core/genesis.go b/core/genesis.go index d77ea10d8c..6a0affa52e 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -136,8 +136,9 @@ func hashAlloc(ga *types.GenesisAlloc, isUBT bool) (common.Hash, error) { var config *triedb.Config if isUBT { config = &triedb.Config{ - PathDB: pathdb.Defaults, - IsUBT: true, + PathDB: pathdb.Defaults, + IsUBT: true, + BinTrieGroupDepth: triedb.UBTDefaults.BinTrieGroupDepth, } } // Create an ephemeral in-memory database for computing hash, diff --git a/core/genesis_test.go b/core/genesis_test.go index e15ad00222..94f1b3a4fd 100644 --- a/core/genesis_test.go +++ b/core/genesis_test.go @@ -261,9 +261,9 @@ func newDbConfig(scheme string) *triedb.Config { return &triedb.Config{PathDB: &config} } -func TestVerkleGenesisCommit(t *testing.T) { - var verkleTime uint64 = 0 - verkleConfig := ¶ms.ChainConfig{ +func TestBinaryGenesisCommit(t *testing.T) { + var ubtTime uint64 = 0 + ubtConfig := ¶ms.ChainConfig{ ChainID: big.NewInt(1), HomesteadBlock: big.NewInt(0), DAOForkBlock: nil, @@ -281,11 +281,11 @@ func TestVerkleGenesisCommit(t *testing.T) { ArrowGlacierBlock: big.NewInt(0), GrayGlacierBlock: big.NewInt(0), MergeNetsplitBlock: nil, - ShanghaiTime: &verkleTime, - CancunTime: &verkleTime, - PragueTime: &verkleTime, - OsakaTime: &verkleTime, - UBTTime: &verkleTime, + ShanghaiTime: &ubtTime, + CancunTime: &ubtTime, + PragueTime: &ubtTime, + OsakaTime: &ubtTime, + UBTTime: &ubtTime, TerminalTotalDifficulty: big.NewInt(0), EnableUBTAtGenesis: true, Ethash: nil, @@ -300,8 +300,8 @@ func TestVerkleGenesisCommit(t *testing.T) { genesis := &Genesis{ BaseFee: big.NewInt(params.InitialBaseFee), - Config: verkleConfig, - Timestamp: verkleTime, + Config: ubtConfig, + Timestamp: ubtTime, Difficulty: big.NewInt(0), Alloc: types.GenesisAlloc{ {1}: {Balance: big.NewInt(1), Storage: map[common.Hash]common.Hash{{1}: {1}}}, @@ -320,17 +320,18 @@ func TestVerkleGenesisCommit(t *testing.T) { config.NoAsyncFlush = true triedb := triedb.NewDatabase(db, &triedb.Config{ - IsUBT: true, - PathDB: &config, + IsUBT: true, + PathDB: &config, + BinTrieGroupDepth: triedb.DefaultBinTrieGroupDepth, }) block := genesis.MustCommit(db, triedb) if !bytes.Equal(block.Root().Bytes(), expected) { t.Fatalf("invalid genesis state root, expected %x, got %x", expected, block.Root()) } - // Test that the trie is verkle + // Test that the trie is a unified binary trie if !triedb.IsUBT() { - t.Fatalf("expected trie to be verkle") + t.Fatalf("expected trie to be a unified binary trie") } vdb := rawdb.NewTable(db, string(rawdb.VerklePrefix)) if !rawdb.HasAccountTrieNode(vdb, nil) { diff --git a/core/state/database_ubt.go b/core/state/database_ubt.go index 718d93df87..16579f6d6a 100644 --- a/core/state/database_ubt.go +++ b/core/state/database_ubt.go @@ -96,7 +96,7 @@ func (db *UBTDatabase) ReadersWithCacheStats(stateRoot common.Hash) (Reader, Rea // OpenTrie opens the main account trie at a specific root hash. func (db *UBTDatabase) OpenTrie(root common.Hash) (Trie, error) { - return bintrie.NewBinaryTrie(root, db.triedb) + return bintrie.NewBinaryTrie(root, db.triedb, db.triedb.BinTrieGroupDepth()) } // OpenStorageTrie opens the storage trie of an account. In binary trie mode, diff --git a/core/state/reader.go b/core/state/reader.go index 5df0acbb9b..be07cec0f9 100644 --- a/core/state/reader.go +++ b/core/state/reader.go @@ -255,7 +255,7 @@ type ubtTrieReader struct { // newUBTTrieReader constructs a Unified-binary-trie reader of the specific state. // An error will be returned if the associated trie specified by root is not existent. func newUBTTrieReader(root common.Hash, db *triedb.Database) (*ubtTrieReader, error) { - binTrie, binErr := bintrie.NewBinaryTrie(root, db) + binTrie, binErr := bintrie.NewBinaryTrie(root, db, db.BinTrieGroupDepth()) if binErr != nil { return nil, binErr } diff --git a/eth/backend.go b/eth/backend.go index 08a3c70c9d..6cfd1f6fa0 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -237,6 +237,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { StateHistory: config.StateHistory, TrienodeHistory: config.TrienodeHistory, NodeFullValueCheckpoint: config.NodeFullValueCheckpoint, + BinTrieGroupDepth: config.BinTrieGroupDepth, StateScheme: scheme, HistoryPolicy: histPolicy, TxLookupLimit: int64(min(config.TransactionHistory, math.MaxInt64)), diff --git a/eth/ethconfig/config.go b/eth/ethconfig/config.go index dd7436bf52..b51b78e199 100644 --- a/eth/ethconfig/config.go +++ b/eth/ethconfig/config.go @@ -35,6 +35,7 @@ import ( "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/miner" "github.com/ethereum/go-ethereum/params" + "github.com/ethereum/go-ethereum/triedb" "github.com/ethereum/go-ethereum/triedb/pathdb" ) @@ -59,6 +60,7 @@ var Defaults = Config{ StateHistory: pathdb.Defaults.StateHistory, TrienodeHistory: pathdb.Defaults.TrienodeHistory, NodeFullValueCheckpoint: pathdb.Defaults.FullValueCheckpoint, + BinTrieGroupDepth: triedb.DefaultBinTrieGroupDepth, DatabaseCache: 2048, TrieCleanCache: 614, TrieDirtyCache: 1024, @@ -125,6 +127,11 @@ type Config struct { // consistent with persistent state. StateScheme string `toml:",omitempty"` + // BinTrieGroupDepth is the number of levels per serialized group in binary trie. + // Valid values are 1-8, with 8 being the default (byte-aligned groups). + // Lower values create smaller groups with more nodes. + BinTrieGroupDepth int `toml:",omitempty"` + // RequiredBlocks is a set of block number -> hash mappings which must be in the // canonical chain of all remote peers. Setting the option makes geth verify the // presence of these blocks for every new peer connection. diff --git a/eth/ethconfig/gen_config.go b/eth/ethconfig/gen_config.go index ed85562f44..c5e45348be 100644 --- a/eth/ethconfig/gen_config.go +++ b/eth/ethconfig/gen_config.go @@ -34,6 +34,7 @@ func (c Config) MarshalTOML() (interface{}, error) { TrienodeHistory int64 `toml:",omitempty"` NodeFullValueCheckpoint uint32 `toml:",omitempty"` StateScheme string `toml:",omitempty"` + BinTrieGroupDepth int `toml:",omitempty"` RequiredBlocks map[uint64]common.Hash `toml:"-"` SlowBlockThreshold time.Duration `toml:",omitempty"` SkipBcVersionCheck bool `toml:"-"` @@ -87,6 +88,7 @@ func (c Config) MarshalTOML() (interface{}, error) { enc.TrienodeHistory = c.TrienodeHistory enc.NodeFullValueCheckpoint = c.NodeFullValueCheckpoint enc.StateScheme = c.StateScheme + enc.BinTrieGroupDepth = c.BinTrieGroupDepth enc.RequiredBlocks = c.RequiredBlocks enc.SlowBlockThreshold = c.SlowBlockThreshold enc.SkipBcVersionCheck = c.SkipBcVersionCheck @@ -144,6 +146,7 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error { TrienodeHistory *int64 `toml:",omitempty"` NodeFullValueCheckpoint *uint32 `toml:",omitempty"` StateScheme *string `toml:",omitempty"` + BinTrieGroupDepth *int `toml:",omitempty"` RequiredBlocks map[uint64]common.Hash `toml:"-"` SlowBlockThreshold *time.Duration `toml:",omitempty"` SkipBcVersionCheck *bool `toml:"-"` @@ -234,6 +237,9 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error { if dec.StateScheme != nil { c.StateScheme = *dec.StateScheme } + if dec.BinTrieGroupDepth != nil { + c.BinTrieGroupDepth = *dec.BinTrieGroupDepth + } if dec.RequiredBlocks != nil { c.RequiredBlocks = dec.RequiredBlocks } diff --git a/trie/bintrie/binary_node.go b/trie/bintrie/binary_node.go index e7f57d45a2..3516bf6bd5 100644 --- a/trie/bintrie/binary_node.go +++ b/trie/bintrie/binary_node.go @@ -27,8 +27,19 @@ const ( NodeTypeBytes = 1 // Size of node type prefix in serialization HashSize = 32 // Size of a hash in bytes StemBitmapSize = 32 // Size of the bitmap in a stem node (256 values = 32 bytes) + + MaxGroupDepth = 8 ) +// bitmapSizeForDepth returns the bitmap size in bytes for a given group depth. +// For depths 1-3, returns 1 byte. For depths 4-8, returns 2^(depth-3) bytes. +func bitmapSizeForDepth(groupDepth int) int { + if groupDepth <= 3 { + return 1 + } + return 1 << (groupDepth - 3) +} + const ( nodeTypeStem = iota + 1 nodeTypeInternal diff --git a/trie/bintrie/binary_node_test.go b/trie/bintrie/binary_node_test.go index 12ac199903..857060a0c0 100644 --- a/trie/bintrie/binary_node_test.go +++ b/trie/bintrie/binary_node_test.go @@ -23,8 +23,8 @@ import ( "github.com/ethereum/go-ethereum/common" ) -// TestSerializeDeserializeInternalNode tests flat 65-byte serialization and -// deserialization of InternalNode through nodeStore. +// TestSerializeDeserializeInternalNode tests grouped serialization and +// deserialization of InternalNode through nodeStore at groupDepth=1. func TestSerializeDeserializeInternalNode(t *testing.T) { leftHash := common.HexToHash("0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef") rightHash := common.HexToHash("0xfedcba0987654321fedcba0987654321fedcba0987654321fedcba0987654321") @@ -39,24 +39,32 @@ func TestSerializeDeserializeInternalNode(t *testing.T) { rootNode.right = rightRef s.root = rootRef - // Serialize the node — flat 65-byte format - serialized := s.serializeNode(rootRef) + // Serialize the node — grouped format at groupDepth=1: + // [type(1)][groupDepth(1)][bitmap(1)][leftHash(32)][rightHash(32)] = 67 bytes + serialized := s.serializeNode(rootRef, 1) - // Check the serialized format: [type(1)][leftHash(32)][rightHash(32)] if serialized[0] != nodeTypeInternal { t.Errorf("Expected type byte to be %d, got %d", nodeTypeInternal, serialized[0]) } + if serialized[1] != 1 { + t.Errorf("Expected groupDepth byte to be 1, got %d", serialized[1]) + } - expectedLen := NodeTypeBytes + 2*HashSize // 1 + 64 = 65 + expectedLen := NodeTypeBytes + 1 + 1 + 2*HashSize // type + groupDepth + bitmap + 2 hashes = 67 if len(serialized) != expectedLen { t.Errorf("Expected serialized length to be %d, got %d", expectedLen, len(serialized)) } - // Check that left and right hashes are embedded directly - if !bytes.Equal(serialized[NodeTypeBytes:NodeTypeBytes+HashSize], leftHash[:]) { + // Both children present at a 1-level group → bitmap byte = 0b11000000. + if serialized[2] != 0xc0 { + t.Errorf("Expected bitmap byte 0xc0, got 0x%02x", serialized[2]) + } + + hashesStart := NodeTypeBytes + 1 + 1 + if !bytes.Equal(serialized[hashesStart:hashesStart+HashSize], leftHash[:]) { t.Error("Left hash not found at expected position") } - if !bytes.Equal(serialized[NodeTypeBytes+HashSize:], rightHash[:]) { + if !bytes.Equal(serialized[hashesStart+HashSize:], rightHash[:]) { t.Error("Right hash not found at expected position") } @@ -116,7 +124,7 @@ func TestSerializeDeserializeStemNode(t *testing.T) { } // Serialize the node - serialized := s.serializeNode(ref) + serialized := s.serializeNode(ref, 8) // Check the serialized format if serialized[0] != nodeTypeStem { @@ -195,8 +203,9 @@ func TestDeserializeInvalidType(t *testing.T) { // TestDeserializeInvalidLength tests deserialization with invalid data length. func TestDeserializeInvalidLength(t *testing.T) { s := newNodeStore() - // InternalNode with valid type byte but wrong length (needs exactly 65 bytes) - invalidData := []byte{nodeTypeInternal, 0, 0, 0} + // InternalNode group header with groupDepth=1 (valid) and a 1-byte bitmap + // announcing two present hashes, but the hash payload is missing. + invalidData := []byte{nodeTypeInternal, 1, 0xc0} _, err := s.deserializeNode(invalidData, 0) if err == nil { @@ -208,6 +217,21 @@ func TestDeserializeInvalidLength(t *testing.T) { } } +// TestDeserializeInvalidGroupDepth tests deserialization when the group depth +// byte is out of the supported 1..MaxGroupDepth range. +func TestDeserializeInvalidGroupDepth(t *testing.T) { + s := newNodeStore() + invalidData := []byte{nodeTypeInternal, 0, 0, 0} + + _, err := s.deserializeNode(invalidData, 0) + if err == nil { + t.Fatal("Expected error for invalid group depth, got nil") + } + if err.Error() != "invalid group depth" { + t.Errorf("Expected 'invalid group depth' error, got: %v", err) + } +} + // TestKeyToPath tests the keyToPath function. func TestKeyToPath(t *testing.T) { tests := []struct { diff --git a/trie/bintrie/hashed_node_test.go b/trie/bintrie/hashed_node_test.go index ae77b7c570..2e12bfba5e 100644 --- a/trie/bintrie/hashed_node_test.go +++ b/trie/bintrie/hashed_node_test.go @@ -95,7 +95,7 @@ func TestHashedNodeInsertValuesAtStem(t *testing.T) { sn.setValue(byte(i), v) } } - serialized := rs.serializeNode(ref) + serialized := rs.serializeNode(ref, 8) validResolver := func(path []byte, hash common.Hash) ([]byte, error) { return serialized, nil diff --git a/trie/bintrie/internal_node_test.go b/trie/bintrie/internal_node_test.go index 8d5a75de8c..4d8da8af37 100644 --- a/trie/bintrie/internal_node_test.go +++ b/trie/bintrie/internal_node_test.go @@ -90,7 +90,7 @@ func TestInternalNodeGetWithResolver(t *testing.T) { ref := rs.newStemRef(stem, 1) sn := rs.getStem(ref.Index()) sn.setValue(5, common.HexToHash("0xabcd").Bytes()) - return rs.serializeNode(ref), nil + return rs.serializeNode(ref, 8), nil } return nil, errors.New("node not found") } @@ -290,10 +290,7 @@ func TestInternalNodeCollectNodes(t *testing.T) { collectedPaths = append(collectedPaths, pathCopy) } - err := s.collectNodes(s.root, []byte{1}, flushFn) - if err != nil { - t.Fatalf("Failed to collect nodes: %v", err) - } + s.collectNodes(s.root, []byte{1}, flushFn, 8) // Should have collected 3 nodes: left stem, right stem, and the internal node itself if len(collectedPaths) != 3 { diff --git a/trie/bintrie/iterator.go b/trie/bintrie/iterator.go index 31645430c3..a920f91378 100644 --- a/trie/bintrie/iterator.go +++ b/trie/bintrie/iterator.go @@ -205,7 +205,7 @@ func (it *binaryNodeIterator) Path() []byte { } func (it *binaryNodeIterator) NodeBlob() []byte { - return it.store.serializeNode(it.current) + return it.store.serializeNode(it.current, it.trie.groupDepth) } // Leaf reports whether the iterator is currently positioned at a leaf value. diff --git a/trie/bintrie/stem_node_test.go b/trie/bintrie/stem_node_test.go index 5faf903fba..ae6b57ab34 100644 --- a/trie/bintrie/stem_node_test.go +++ b/trie/bintrie/stem_node_test.go @@ -320,10 +320,7 @@ func TestStemNodeCollectNodes(t *testing.T) { collectedPaths = append(collectedPaths, pathCopy) } - err := s.collectNodes(s.root, []byte{0, 1, 0}, flushFn) - if err != nil { - t.Fatalf("Failed to collect nodes: %v", err) - } + s.collectNodes(s.root, []byte{0, 1, 0}, flushFn, 8) // Should have collected one node (itself) if len(collectedPaths) != 1 { diff --git a/trie/bintrie/store_commit.go b/trie/bintrie/store_commit.go index 7101087b51..b14bffbc6c 100644 --- a/trie/bintrie/store_commit.go +++ b/trie/bintrie/store_commit.go @@ -107,18 +107,83 @@ func (s *nodeStore) hashInternal(idx uint32) common.Hash { return node.hash } +// serializeSubtree recursively collects child hashes from a subtree of InternalNodes. +// It traverses up to `remainingDepth` levels, storing hashes of bottom-layer children. +// position tracks the current index (0 to 2^groupDepth - 1) for bitmap placement. +// hashes collects the hashes of present children, bitmap tracks which positions are present. +func (s *nodeStore) serializeSubtree(ref nodeRef, remainingDepth int, position int, absoluteDepth int, bitmap []byte, hashes *[]common.Hash) { + if remainingDepth == 0 { + // Bottom layer: store hash if not empty + switch ref.Kind() { + case kindEmpty: + // Leave bitmap bit unset, don't add hash + return + default: + // StemNode, HashedNode, or InternalNode at boundary: store hash + bitmap[position/8] |= 1 << (7 - (position % 8)) + *hashes = append(*hashes, s.computeHash(ref)) + } + return + } + + switch ref.Kind() { + case kindInternal: + leftPos := position * 2 + rightPos := position*2 + 1 + s.serializeSubtree(s.getInternal(ref.Index()).left, remainingDepth-1, leftPos, absoluteDepth+1, bitmap, hashes) + s.serializeSubtree(s.getInternal(ref.Index()).right, remainingDepth-1, rightPos, absoluteDepth+1, bitmap, hashes) + case kindEmpty: + return + default: + // StemNode or HashedNode encountered before reaching the group's bottom + // layer. Compute the leaf bitmap position where this node's hash will + // be stored. + leafPos := position + switch ref.Kind() { + case kindStem: + sn := s.getStem(ref.Index()) + // Extend position using the stem's key bits so that + // GetValuesAtStem traversal (which follows key bits) finds the hash. + for d := 0; d < remainingDepth; d++ { + bit := sn.Stem[(absoluteDepth+d)/8] >> (7 - ((absoluteDepth + d) % 8)) & 1 + leafPos = leafPos*2 + int(bit) + } + default: + // HashedNode or unknown: extend all-left (no key bits available). + // This matches the all-zero path that resolveNode would follow. + leafPos = position << remainingDepth + } + bitmap[leafPos/8] |= 1 << (7 - (leafPos % 8)) + *hashes = append(*hashes, s.computeHash(ref)) + } +} + // SerializeNode serializes a node into the flat on-disk format. -func (s *nodeStore) serializeNode(ref nodeRef) []byte { +func (s *nodeStore) serializeNode(ref nodeRef, groupDepth int) []byte { switch ref.Kind() { case kindInternal: + // InternalNode group: 1 byte type + 1 byte group depth + variable bitmap + N×32 byte hashes + bitmapSize := bitmapSizeForDepth(groupDepth) + bitmap := make([]byte, bitmapSize) + var hashes []common.Hash + node := s.getInternal(ref.Index()) - var serialized [NodeTypeBytes + HashSize + HashSize]byte + s.serializeSubtree(ref, groupDepth, 0, int(node.depth), bitmap, &hashes) + + // Build serialized output + serializedLen := NodeTypeBytes + 1 + bitmapSize + len(hashes)*HashSize + serialized := make([]byte, serializedLen) serialized[0] = nodeTypeInternal - lh := s.computeHash(node.left) - rh := s.computeHash(node.right) - copy(serialized[NodeTypeBytes:NodeTypeBytes+HashSize], lh[:]) - copy(serialized[NodeTypeBytes+HashSize:], rh[:]) - return serialized[:] + serialized[1] = byte(groupDepth) // group depth => bitmap size for a sparse group + copy(serialized[2:2+bitmapSize], bitmap) + + offset := NodeTypeBytes + 1 + bitmapSize + for _, h := range hashes { + copy(serialized[offset:offset+HashSize], h.Bytes()) + offset += HashSize + } + + return serialized case kindStem: sn := s.getStem(ref.Index()) @@ -163,6 +228,59 @@ func (s *nodeStore) deserializeNodeWithHash(serialized []byte, depth int, hn com return s.decodeNode(serialized, depth, hn, false, false) } +// deserializeSubtree reconstructs an InternalNode subtree from grouped serialization. +// remainingDepth is how many more levels to build, position is current index in the bitmap, +// nodeDepth is the actual trie depth for the node being created. +// hashIdx tracks the current position in the hash data (incremented as hashes are consumed). +func (s *nodeStore) deserializeSubtree(hn common.Hash, remainingDepth int, position int, nodeDepth int, bitmap []byte, hashData []byte, hashIdx *int, mustRecompute bool, dirty bool) (nodeRef, error) { + if remainingDepth == 0 { + // Bottom layer: check bitmap and return HashedNode or Empty + if bitmap[position/8]>>(7-(position%8))&1 == 1 { + if len(hashData) < (*hashIdx+1)*HashSize { + return emptyRef, errInvalidSerializedLength + } + hash := common.BytesToHash(hashData[*hashIdx*HashSize : (*hashIdx+1)*HashSize]) + *hashIdx++ + return s.newHashedRef(hash), nil + } + return emptyRef, nil + } + + // Check if this entire subtree is empty by examining all relevant bitmap bits + leftPos := position * 2 + rightPos := position*2 + 1 + + // note that the parent might not need root computations, but the children + // do, because their hash isn't saved. Hence `mustRecompute` is set to `true`. + left, err := s.deserializeSubtree(common.Hash{}, remainingDepth-1, leftPos, nodeDepth+1, bitmap, hashData, hashIdx, true, dirty) + if err != nil { + return emptyRef, err + } + right, err := s.deserializeSubtree(common.Hash{}, remainingDepth-1, rightPos, nodeDepth+1, bitmap, hashData, hashIdx, true, dirty) + if err != nil { + return emptyRef, err + } + + // If both children are empty, return Empty + if left.IsEmpty() && right.IsEmpty() { + return emptyRef, nil + } + + ref := s.newInternalRef(nodeDepth) + node := s.getInternal(ref.Index()) + node.left = left + node.right = right + node.mustRecompute = mustRecompute + if !mustRecompute { + // mustRecompute will only be false for the root of the subtree, + // for which we already know the hash. + node.hash = hn + node.mustRecompute = false + } + node.dirty = dirty + return ref, nil +} + func (s *nodeStore) decodeNode(serialized []byte, depth int, hn common.Hash, mustRecompute, dirty bool) (nodeRef, error) { if len(serialized) == 0 { return emptyRef, nil @@ -170,31 +288,23 @@ func (s *nodeStore) decodeNode(serialized []byte, depth int, hn common.Hash, mus switch serialized[0] { case nodeTypeInternal: - if len(serialized) != NodeTypeBytes+2*HashSize { + // Grouped format: 1 byte type + 1 byte group depth + variable bitmap + N×32 byte hashes + if len(serialized) < NodeTypeBytes+1 { return emptyRef, errInvalidSerializedLength } - var leftHash, rightHash common.Hash - copy(leftHash[:], serialized[NodeTypeBytes:NodeTypeBytes+HashSize]) - copy(rightHash[:], serialized[NodeTypeBytes+HashSize:]) - - var leftRef, rightRef nodeRef - if leftHash != (common.Hash{}) { - leftRef = s.newHashedRef(leftHash) + groupDepth := int(serialized[1]) + if groupDepth < 1 || groupDepth > MaxGroupDepth { + return 0, errors.New("invalid group depth") } - if rightHash != (common.Hash{}) { - rightRef = s.newHashedRef(rightHash) + bitmapSize := bitmapSizeForDepth(groupDepth) + if len(serialized) < NodeTypeBytes+1+bitmapSize { + return 0, errInvalidSerializedLength } + bitmap := serialized[2 : 2+bitmapSize] + hashData := serialized[2+bitmapSize:] - ref := s.newInternalRef(depth) - node := s.getInternal(ref.Index()) - node.left = leftRef - node.right = rightRef - if !mustRecompute { - node.hash = hn - node.mustRecompute = false - } - node.dirty = dirty - return ref, nil + hashIdx := 0 + return s.deserializeSubtree(hn, groupDepth, 0, depth, bitmap, hashData, &hashIdx, mustRecompute, dirty) case nodeTypeStem: if len(serialized) < NodeTypeBytes+StemSize+StemBitmapSize { @@ -230,43 +340,110 @@ func (s *nodeStore) decodeNode(serialized []byte, depth int, hn common.Hash, mus // CollectNodes flushes every node that needs flushing via flushfn in post-order. // Invariant: any ancestor of a node that needs flushing is itself marked, so a // clean root means the whole subtree is clean. -func (s *nodeStore) collectNodes(ref nodeRef, path []byte, flushfn nodeFlushFn) error { +func (s *nodeStore) collectNodes(ref nodeRef, path []byte, flushfn nodeFlushFn, groupDepth int) { switch ref.Kind() { - case kindEmpty: - return nil case kindInternal: node := s.getInternal(ref.Index()) if !node.dirty { - return nil + return } - // Reuse path buffer across children: flushfn consumers - // (NodeSet.AddNode, tracer.Get) clone via string(path), so in-place - // mutation is safe. - path = append(path, 0) - if err := s.collectNodes(node.left, path, flushfn); err != nil { - return err + // Only flush at group boundaries (depth % groupDepth == 0) + if int(node.depth)%groupDepth == 0 { + // We're at a group boundary - first collect any nodes in deeper groups, + // then flush this group + s.collectChildGroups(node, path, flushfn, groupDepth, groupDepth-1) + flushfn(path, s.computeHash(ref), s.serializeNode(ref, groupDepth)) + node.dirty = false + return } - path[len(path)-1] = 1 - if err := s.collectNodes(node.right, path, flushfn); err != nil { - return err - } - path = path[:len(path)-1] - flushfn(path, s.computeHash(ref), s.serializeNode(ref)) - node.dirty = false - return nil + // Not at a group boundary - this shouldn't happen if we're called correctly from root + // but handle it by continuing to traverse + s.collectChildGroups(node, path, flushfn, groupDepth, groupDepth-(int(node.depth)%groupDepth)-1) case kindStem: sn := s.getStem(ref.Index()) if !sn.dirty { - return nil + return } - flushfn(path, s.computeHash(ref), s.serializeNode(ref)) + flushfn(path, s.computeHash(ref), s.serializeNode(ref, groupDepth)) sn.dirty = false - return nil - case kindHashed: - return nil // Already committed + case kindHashed, kindEmpty: default: - return fmt.Errorf("CollectNodes: unexpected kind %d", ref.Kind()) + panic(fmt.Sprintf("CollectNodes: unexpected kind %d", ref.Kind())) + } +} + +// collectChildGroups traverses within a group to find and collect nodes in the next group. +// remainingLevels is how many more levels below the current node until we reach the group boundary. +// When remainingLevels=0, the current node's children are at the next group boundary. +func (s *nodeStore) collectChildGroups(node *InternalNode, path []byte, flushfn nodeFlushFn, groupDepth int, remainingLevels int) error { + if remainingLevels == 0 { + // Current node is at depth (groupBoundary - 1), its children are at the next group boundary + if !node.left.IsEmpty() { + s.collectNodes(node.left, appendBit(path, 0), flushfn, groupDepth) + } + if !node.right.IsEmpty() { + s.collectNodes(node.right, appendBit(path, 1), flushfn, groupDepth) + } + return nil + } + + if !node.left.IsEmpty() { + switch node.left.Kind() { + case kindInternal: + n := s.getInternal(node.left.Index()) + if err := s.collectChildGroups(n, appendBit(path, 0), flushfn, groupDepth, remainingLevels-1); err != nil { + return err + } + default: + extPath := s.extendPathToGroupLeaf(appendBit(path, 0), node.left, remainingLevels) + s.collectNodes(node.left, extPath, flushfn, groupDepth) + } + } + if !node.right.IsEmpty() { + switch node.right.Kind() { + case kindInternal: + n := s.getInternal(node.right.Index()) + if err := s.collectChildGroups(n, appendBit(path, 1), flushfn, groupDepth, remainingLevels-1); err != nil { + return err + } + default: + extPath := s.extendPathToGroupLeaf(appendBit(path, 1), node.right, remainingLevels) + s.collectNodes(node.right, extPath, flushfn, groupDepth) + } } + return nil +} + +// extendPathToGroupLeaf extends a storage path to the group's leaf boundary, +// matching the projection done by serializeSubtree. For StemNodes, the path +// is extended using the stem's key bits (same as serializeSubtree). For other +// node types, the path is extended with all-zero (left) bits. +func (s *nodeStore) extendPathToGroupLeaf(path []byte, node nodeRef, remainingLevels int) []byte { + if remainingLevels <= 0 { + return path + } + if node.Kind() == kindStem { + sn := s.getStem(node.Index()) + for _ = range remainingLevels { + bit := sn.Stem[len(path)/8] >> (7 - (len(path) % 8)) & 1 + path = appendBit(path, bit) + } + } else { + // HashedNode or other: all-left extension (matches serializeSubtree's + // position << remainingDepth behavior). + for _ = range remainingLevels { + path = appendBit(path, 0) + } + } + return path +} + +// appendBit appends a bit to a path, returning a new slice +func appendBit(path []byte, bit byte) []byte { + var p [256]byte + copy(p[:], path) + result := p[:len(path)] + return append(result, bit) } func (s *nodeStore) toDot(ref nodeRef, parent, path string) string { diff --git a/trie/bintrie/trie.go b/trie/bintrie/trie.go index 8c69e0aa00..e3436e3df1 100644 --- a/trie/bintrie/trie.go +++ b/trie/bintrie/trie.go @@ -107,9 +107,14 @@ func ChunkifyCode(code []byte) ChunkedCode { // BinaryTrie is the implementation of https://eips.ethereum.org/EIPS/eip-7864. type BinaryTrie struct { - store *nodeStore - reader *trie.Reader - tracer *trie.PrevalueTracer + store *nodeStore + reader *trie.Reader + tracer *trie.PrevalueTracer + groupDepth int // Number of levels per serialized group (1-8, default 8) +} + +func (t *BinaryTrie) GroupDepth() int { + return t.groupDepth } // ToDot converts the binary trie to a DOT language representation. Useful for debugging. @@ -119,15 +124,20 @@ func (t *BinaryTrie) ToDot() string { } // NewBinaryTrie creates a new binary trie. -func NewBinaryTrie(root common.Hash, db database.NodeDatabase) (*BinaryTrie, error) { +// groupDepth specifies the number of levels per serialized group (1-8). +func NewBinaryTrie(root common.Hash, db database.NodeDatabase, groupDepth int) (*BinaryTrie, error) { + if groupDepth < 1 || groupDepth > MaxGroupDepth { + panic("invalid group depth size") + } reader, err := trie.NewReader(root, common.Hash{}, db) if err != nil { return nil, err } t := &BinaryTrie{ - store: newNodeStore(), - reader: reader, - tracer: trie.NewPrevalueTracer(), + store: newNodeStore(), + reader: reader, + tracer: trie.NewPrevalueTracer(), + groupDepth: groupDepth, } // Parse the root node if it's not empty if root != types.EmptyBinaryHash && root != types.EmptyRootHash { @@ -312,12 +322,9 @@ func (t *BinaryTrie) Commit(_ bool) (common.Hash, *trienode.NodeSet) { // Pre-size the path buffer: collectNodes reuses it in-place via // append/truncate; 32 covers typical binary-trie depth without regrowth. pathBuf := make([]byte, 0, 32) - err := t.store.collectNodes(t.store.root, pathBuf, func(path []byte, hash common.Hash, serialized []byte) { + t.store.collectNodes(t.store.root, pathBuf, func(path []byte, hash common.Hash, serialized []byte) { nodeset.AddNode(path, trienode.NewNodeWithPrev(hash, serialized, t.tracer.Get(path))) - }) - if err != nil { - panic(fmt.Errorf("CollectNodes failed: %v", err)) - } + }, t.groupDepth) return t.Hash(), nodeset } @@ -341,9 +348,10 @@ func (t *BinaryTrie) Prove(key []byte, proofDb ethdb.KeyValueWriter) error { // Copy creates a deep copy of the trie. func (t *BinaryTrie) Copy() *BinaryTrie { return &BinaryTrie{ - store: t.store.Copy(), - reader: t.reader, - tracer: t.tracer.Copy(), + store: t.store.Copy(), + reader: t.reader, + tracer: t.tracer.Copy(), + groupDepth: t.groupDepth, } } diff --git a/trie/bintrie/trie_test.go b/trie/bintrie/trie_test.go index 73aacb76c4..8b7d9e46d6 100644 --- a/trie/bintrie/trie_test.go +++ b/trie/bintrie/trie_test.go @@ -768,8 +768,9 @@ func TestGetStorageNonMembershipInternalRoot(t *testing.T) { // flushes only the root-to-leaf path. func TestCommitSkipCleanSubtrees(t *testing.T) { tr := &BinaryTrie{ - store: newNodeStore(), - tracer: trie.NewPrevalueTracer(), + store: newNodeStore(), + tracer: trie.NewPrevalueTracer(), + groupDepth: 1, } const n = 200 key := func(i int) [HashSize]byte { diff --git a/triedb/database.go b/triedb/database.go index 533097c9e3..0fd3e1aa91 100644 --- a/triedb/database.go +++ b/triedb/database.go @@ -31,12 +31,15 @@ import ( // Config defines all necessary options for database. type Config struct { - Preimages bool // Flag whether the preimage of node key is recorded - IsUBT bool // Flag whether the db is holding a verkle tree - HashDB *hashdb.Config // Configs for hash-based scheme - PathDB *pathdb.Config // Configs for experimental path-based scheme + Preimages bool // Flag whether the preimage of node key is recorded + IsUBT bool // Flag whether the db is holding a unified binary tree + BinTrieGroupDepth int // Number of levels per serialized group in binary trie (1-8, default 8) + HashDB *hashdb.Config // Configs for hash-based scheme + PathDB *pathdb.Config // Configs for experimental path-based scheme } +const DefaultBinTrieGroupDepth = 5 + // HashDefaults represents a config for using hash-based scheme with // default settings. var HashDefaults = &Config{ @@ -45,12 +48,13 @@ var HashDefaults = &Config{ HashDB: hashdb.Defaults, } -// UBTDefaults represents a config for holding verkle trie data +// UBTDefaults represents a config for holding unified binary trie data // using path-based scheme with default settings. var UBTDefaults = &Config{ - Preimages: false, - IsUBT: true, - PathDB: pathdb.Defaults, + Preimages: false, + IsUBT: true, + BinTrieGroupDepth: DefaultBinTrieGroupDepth, + PathDB: pathdb.Defaults, } // backend defines the methods needed to access/update trie nodes in different @@ -393,3 +397,7 @@ func (db *Database) SnapshotCompleted() bool { } return pdb.SnapshotCompleted() } + +func (db *Database) BinTrieGroupDepth() int { + return db.config.BinTrieGroupDepth +} diff --git a/triedb/pathdb/journal.go b/triedb/pathdb/journal.go index efcc3f2549..657fbbff27 100644 --- a/triedb/pathdb/journal.go +++ b/triedb/pathdb/journal.go @@ -161,7 +161,19 @@ func loadGenerator(db ethdb.KeyValueReader, hash nodeHasher) (*journalGenerator, // loadLayers loads a pre-existing state layer backed by a key-value store. func (db *Database) loadLayers() layer { // Retrieve the root node of persistent state. - root, err := db.hasher(rawdb.ReadAccountTrieNode(db.diskdb, nil)) + var ( + root common.Hash + err error + ) + if db.isUBT { + root = rawdb.ReadSnapshotRoot(db.diskdb) + if root == (common.Hash{}) { + root = types.EmptyBinaryHash + } + } else { + blob := rawdb.ReadAccountTrieNode(db.diskdb, nil) + root, err = db.hasher(blob) + } if err != nil { log.Crit("Failed to compute node hash", "err", err) } From b9c5fe6d26342d625c9a393bef9ccd5209c6d888 Mon Sep 17 00:00:00 2001 From: felipe Date: Fri, 1 May 2026 16:38:33 +0200 Subject: [PATCH 40/80] eth/tracers: fix evm trace for t8n (#34862) The mux tracer fanned out every standard hook to its children but never forwarded OnSystemCall{Start,End}. Tracers that rely on these - like `logger.jsonLogger`, which uses the start hook to silence its opcode hook for the duration of a system call - never got the signal when wrapped behind a mux. In evm t8n, combining `--trace` with `--opcode-count` (default for geth with exec specs) produces exactly that wrapping. The first system call (e.g. `ProcessBeaconBlockRoot`) then fires `OnOpcode` on the json logger before any `OnTxStart` has run, dereferencing a nil env and crashing t8n. Forward both hooks through the mux. The V2 fan-out falls back to V1 for children that only implement the legacy hook, mirroring the precedence already used in `core/state_processor.go`. --- eth/tracers/native/mux.go | 44 ++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/eth/tracers/native/mux.go b/eth/tracers/native/mux.go index b7d6f29a6a..362fb24297 100644 --- a/eth/tracers/native/mux.go +++ b/eth/tracers/native/mux.go @@ -67,18 +67,20 @@ func NewMuxTracer(names []string, objects []*tracers.Tracer) (*tracers.Tracer, e t := &muxTracer{names: names, tracers: objects} return &tracers.Tracer{ Hooks: &tracing.Hooks{ - OnTxStart: t.OnTxStart, - OnTxEnd: t.OnTxEnd, - OnEnter: t.OnEnter, - OnExit: t.OnExit, - OnOpcode: t.OnOpcode, - OnFault: t.OnFault, - OnGasChange: t.OnGasChange, - OnBalanceChange: t.OnBalanceChange, - OnNonceChange: t.OnNonceChange, - OnCodeChange: t.OnCodeChange, - OnStorageChange: t.OnStorageChange, - OnLog: t.OnLog, + OnTxStart: t.OnTxStart, + OnTxEnd: t.OnTxEnd, + OnEnter: t.OnEnter, + OnExit: t.OnExit, + OnOpcode: t.OnOpcode, + OnFault: t.OnFault, + OnGasChange: t.OnGasChange, + OnBalanceChange: t.OnBalanceChange, + OnNonceChange: t.OnNonceChange, + OnCodeChange: t.OnCodeChange, + OnStorageChange: t.OnStorageChange, + OnLog: t.OnLog, + OnSystemCallStartV2: t.OnSystemCallStart, + OnSystemCallEnd: t.OnSystemCallEnd, }, GetResult: t.GetResult, Stop: t.Stop, @@ -189,6 +191,24 @@ func (t *muxTracer) OnLog(log *types.Log) { } } +func (t *muxTracer) OnSystemCallStart(vm *tracing.VMContext) { + for _, t := range t.tracers { + if t.OnSystemCallStartV2 != nil { + t.OnSystemCallStartV2(vm) + } else if t.OnSystemCallStart != nil { + t.OnSystemCallStart() + } + } +} + +func (t *muxTracer) OnSystemCallEnd() { + for _, t := range t.tracers { + if t.OnSystemCallEnd != nil { + t.OnSystemCallEnd() + } + } +} + // GetResult returns an empty json object. func (t *muxTracer) GetResult() (json.RawMessage, error) { resObject := make(map[string]json.RawMessage) From 41b856d472153296c14b145514f202ad3e67c683 Mon Sep 17 00:00:00 2001 From: Richard Creighton Date: Fri, 1 May 2026 19:18:14 +0100 Subject: [PATCH 41/80] ethclient: add maxUsedGas to simulate call results (#34820) This updates the typed `ethclient` model for `eth_simulateV1` call results to include `maxUsedGas`, matching the field already returned by the server-side RPC response. Follow-up to #32789. --- ethclient/ethclient.go | 2 ++ ethclient/ethclient_test.go | 6 ++++++ ethclient/gen_simulate_call_result.go | 6 ++++++ 3 files changed, 14 insertions(+) diff --git a/ethclient/ethclient.go b/ethclient/ethclient.go index 412f8955ba..1d8573f982 100644 --- a/ethclient/ethclient.go +++ b/ethclient/ethclient.go @@ -914,6 +914,7 @@ type SimulateCallResult struct { ReturnValue []byte `json:"returnData"` Logs []*types.Log `json:"logs"` GasUsed uint64 `json:"gasUsed"` + MaxUsedGas uint64 `json:"maxUsedGas"` Status uint64 `json:"status"` Error *CallError `json:"error,omitempty"` } @@ -921,6 +922,7 @@ type SimulateCallResult struct { type simulateCallResultMarshaling struct { ReturnValue hexutil.Bytes GasUsed hexutil.Uint64 + MaxUsedGas hexutil.Uint64 Status hexutil.Uint64 } diff --git a/ethclient/ethclient_test.go b/ethclient/ethclient_test.go index f9e761e412..fb04d77669 100644 --- a/ethclient/ethclient_test.go +++ b/ethclient/ethclient_test.go @@ -861,6 +861,12 @@ func TestSimulateV1(t *testing.T) { if results[0].Calls[0].Error != nil { t.Errorf("expected no error, got %v", results[0].Calls[0].Error) } + if results[0].Calls[0].MaxUsedGas == 0 { + t.Error("expected maxUsedGas to be set") + } + if results[0].Calls[0].MaxUsedGas < results[0].Calls[0].GasUsed { + t.Errorf("expected maxUsedGas >= gasUsed, got %d < %d", results[0].Calls[0].MaxUsedGas, results[0].Calls[0].GasUsed) + } } func TestSimulateV1WithBlockOverrides(t *testing.T) { diff --git a/ethclient/gen_simulate_call_result.go b/ethclient/gen_simulate_call_result.go index 55e14cd697..18373bbb88 100644 --- a/ethclient/gen_simulate_call_result.go +++ b/ethclient/gen_simulate_call_result.go @@ -17,6 +17,7 @@ func (s SimulateCallResult) MarshalJSON() ([]byte, error) { ReturnValue hexutil.Bytes `json:"returnData"` Logs []*types.Log `json:"logs"` GasUsed hexutil.Uint64 `json:"gasUsed"` + MaxUsedGas hexutil.Uint64 `json:"maxUsedGas"` Status hexutil.Uint64 `json:"status"` Error *CallError `json:"error,omitempty"` } @@ -24,6 +25,7 @@ func (s SimulateCallResult) MarshalJSON() ([]byte, error) { enc.ReturnValue = s.ReturnValue enc.Logs = s.Logs enc.GasUsed = hexutil.Uint64(s.GasUsed) + enc.MaxUsedGas = hexutil.Uint64(s.MaxUsedGas) enc.Status = hexutil.Uint64(s.Status) enc.Error = s.Error return json.Marshal(&enc) @@ -35,6 +37,7 @@ func (s *SimulateCallResult) UnmarshalJSON(input []byte) error { ReturnValue *hexutil.Bytes `json:"returnData"` Logs []*types.Log `json:"logs"` GasUsed *hexutil.Uint64 `json:"gasUsed"` + MaxUsedGas *hexutil.Uint64 `json:"maxUsedGas"` Status *hexutil.Uint64 `json:"status"` Error *CallError `json:"error,omitempty"` } @@ -51,6 +54,9 @@ func (s *SimulateCallResult) UnmarshalJSON(input []byte) error { if dec.GasUsed != nil { s.GasUsed = uint64(*dec.GasUsed) } + if dec.MaxUsedGas != nil { + s.MaxUsedGas = uint64(*dec.MaxUsedGas) + } if dec.Status != nil { s.Status = uint64(*dec.Status) } From d270e211d1bfdee7e1a4f9f03b0898327636f85a Mon Sep 17 00:00:00 2001 From: cui Date: Sat, 2 May 2026 02:22:48 +0800 Subject: [PATCH 42/80] core/state/snapshot: fix condition in iterator traversal test (#34638) Fixes a condition in a snapshot-related test. --- core/state/snapshot/iterator_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/state/snapshot/iterator_test.go b/core/state/snapshot/iterator_test.go index a95bd66dde..8e473aa312 100644 --- a/core/state/snapshot/iterator_test.go +++ b/core/state/snapshot/iterator_test.go @@ -441,7 +441,7 @@ func TestStorageIteratorTraversalValues(t *testing.T) { if i%8 == 0 { e[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 4, i) } - if i > 50 || i < 85 { + if i > 50 && i < 85 { f[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 5, i) } if i%64 == 0 { From 8656efcf5b2bf1e377fe162489d52dd09afac2e4 Mon Sep 17 00:00:00 2001 From: hero5512 Date: Fri, 1 May 2026 14:27:57 -0400 Subject: [PATCH 43/80] ethclient/simulated: disable log indexing by default (#32594) Disables the recently added log indexer from a simulated backend. In most cases the log indexer is not required and unindexed search should be fast enough. Fixes https://github.com/ethereum/go-ethereum/issues/32552. --- ethclient/simulated/backend.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ethclient/simulated/backend.go b/ethclient/simulated/backend.go index d573c7e750..160ad924bf 100644 --- a/ethclient/simulated/backend.go +++ b/ethclient/simulated/backend.go @@ -86,6 +86,8 @@ func NewBackend(alloc types.GenesisAlloc, options ...func(nodeConf *node.Config, } ethConf.SyncMode = ethconfig.FullSync ethConf.TxPool.NoLocals = true + // Disable log indexing to force unindexed log search + ethConf.LogNoHistory = true for _, option := range options { option(&nodeConf, ðConf) From 7155c65abbeec23981ea10ecdbeb1a661426e934 Mon Sep 17 00:00:00 2001 From: rayoo Date: Sat, 2 May 2026 02:32:49 +0800 Subject: [PATCH 44/80] internal/ethapi: apply block overrides to header in eth_call (#34842) Apply block overrides to header in eth_call so EIP-1559 fee fields use the correct overridden basefee. --- internal/ethapi/api.go | 4 ++++ internal/ethapi/api_test.go | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index e8669b86c6..6fc43a370b 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -734,6 +734,10 @@ func doCall(ctx context.Context, b Backend, args TransactionArgs, state *state.S if err := blockOverrides.Apply(&blockCtx); err != nil { return nil, err } + // Override the header so callers that compute gas price from 1559 fee + // fields see the overridden basefee. Otherwise GASPRICE/effectiveTip + // would be derived from the pre-override basefee. + header = blockOverrides.MakeHeader(header) } rules := b.ChainConfig().Rules(blockCtx.BlockNumber, blockCtx.Random != nil, blockCtx.Time) precompiles := vm.ActivePrecompiledContracts(rules) diff --git a/internal/ethapi/api_test.go b/internal/ethapi/api_test.go index 6cf52d636a..161d97b4eb 100644 --- a/internal/ethapi/api_test.go +++ b/internal/ethapi/api_test.go @@ -1315,6 +1315,27 @@ func TestCall(t *testing.T) { }, expectErr: errors.New(`block override "withdrawals" is not supported for this RPC method`), }, + // Verify that an overridden basefee is honored when computing gasPrice + // from the 1559 fee fields. Returning GASPRICE opcode; expected value + // is min(MaxFeePerGas, MaxPriorityFeePerGas + overridden BaseFee). + // + // BaseFee override = 0xa (10); MaxFeePerGas = 0x64 (100); + // MaxPriorityFeePerGas = 0x2 (2); expected GASPRICE = 12. + { + name: "basefee-override-used-in-gasprice", + blockNumber: rpc.LatestBlockNumber, + call: TransactionArgs{ + From: &accounts[0].addr, + // Contract: GASPRICE; PUSH1 0; MSTORE; PUSH1 32; PUSH1 0; RETURN + Input: hex2Bytes("3a60005260206000f3"), + MaxFeePerGas: (*hexutil.Big)(big.NewInt(100)), + MaxPriorityFeePerGas: (*hexutil.Big)(big.NewInt(2)), + }, + blockOverrides: override.BlockOverrides{ + BaseFeePerGas: (*hexutil.Big)(big.NewInt(10)), + }, + want: "0x000000000000000000000000000000000000000000000000000000000000000c", + }, } for _, tc := range testSuite { result, err := api.Call(context.Background(), tc.call, &rpc.BlockNumberOrHash{BlockNumber: &tc.blockNumber}, &tc.overrides, &tc.blockOverrides) From f0b21fa11061d3f46de621e38cbbc82c1dc2f819 Mon Sep 17 00:00:00 2001 From: Rahman Date: Sat, 2 May 2026 05:29:21 -0600 Subject: [PATCH 45/80] core/txpool/blobpool: fix gapped queue size cap (#34831) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit the gapped queue cap was effectively per-sender rather than total — a sender pool spread across enough distinct addresses could grow `p.gapped` well past `maxGapped`, defeating the resource bound. `maxGapped` was being compared against `len(p.gapped)`, which is a `map[address][]tx` and counts unique senders, not queued txs. Switched the check to `len(p.gappedSource)` (keyed by tx hash, so its length is the real total). Also wired up a `blobpool/gapped/count` gauge plus `promoted`, `evicted`, and `gappedfull` meters so queue size and churn are actually observable in prod. --- core/txpool/blobpool/blobpool.go | 12 +++++++++--- core/txpool/blobpool/metrics.go | 6 ++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/core/txpool/blobpool/blobpool.go b/core/txpool/blobpool/blobpool.go index 4030a0c339..efa41a0649 100644 --- a/core/txpool/blobpool/blobpool.go +++ b/core/txpool/blobpool/blobpool.go @@ -1596,9 +1596,10 @@ func (p *BlobPool) addLocked(tx *types.Transaction, checkGapped bool) (err error // Store the tx in memory, and revalidate later from, _ := types.Sender(p.signer, tx) allowance := p.gappedAllowance(from) - if allowance >= 1 && len(p.gapped) < maxGapped { + if allowance >= 1 && len(p.gappedSource) < maxGapped { p.gapped[from] = append(p.gapped[from], tx) p.gappedSource[tx.Hash()] = from + gappedGauge.Update(int64(len(p.gappedSource))) log.Trace("added tx to gapped blob queue", "allowance", allowance, "hash", tx.Hash(), "from", from, "nonce", tx.Nonce(), "qlen", len(p.gapped[from])) return nil } else { @@ -1606,6 +1607,7 @@ func (p *BlobPool) addLocked(tx *types.Transaction, checkGapped bool) (err error // transactions by keeping the old and dropping this one. // Thus replacing a gapped transaction with another gapped transaction // is discouraged. + addGappedFullMeter.Mark(1) log.Trace("no gapped blob queue allowance", "allowance", allowance, "hash", tx.Hash(), "from", from, "nonce", tx.Nonce(), "qlen", len(p.gapped[from])) } case errors.Is(err, core.ErrInsufficientFunds): @@ -1791,6 +1793,7 @@ func (p *BlobPool) addLocked(tx *types.Transaction, checkGapped bool) (err error // We do not recurse here, but continue to loop instead. // We are under lock, so we can add the transaction directly. if err := p.addLocked(tx, false); err == nil { + gappedPromotedMeter.Mark(1) log.Trace("Gapped blob transaction added to pool", "hash", tx.Hash(), "from", from, "nonce", tx.Nonce(), "qlen", len(p.gapped[from])) } else { log.Trace("Gapped blob transaction not accepted", "hash", tx.Hash(), "from", from, "nonce", tx.Nonce(), "err", err) @@ -1802,6 +1805,7 @@ func (p *BlobPool) addLocked(tx *types.Transaction, checkGapped bool) (err error } else { p.gapped[from] = gtxs } + gappedGauge.Update(int64(len(p.gappedSource))) } return nil } @@ -2069,8 +2073,9 @@ func (p *BlobPool) evictGapped() { keep = append(keep, gtx) } } - if len(keep) < len(txs) { - log.Trace("Evicting old gapped blob transactions", "count", len(txs)-len(keep), "from", from) + if evicted := len(txs) - len(keep); evicted > 0 { + gappedEvictedMeter.Mark(int64(evicted)) + log.Trace("Evicting old gapped blob transactions", "count", evicted, "from", from) } if len(keep) == 0 { delete(p.gapped, from) @@ -2078,6 +2083,7 @@ func (p *BlobPool) evictGapped() { p.gapped[from] = keep } } + gappedGauge.Update(int64(len(p.gappedSource))) } // isAnnouncable checks whether a transaction is announcable based on its diff --git a/core/txpool/blobpool/metrics.go b/core/txpool/blobpool/metrics.go index 52419ade09..44e2098b22 100644 --- a/core/txpool/blobpool/metrics.go +++ b/core/txpool/blobpool/metrics.go @@ -97,9 +97,15 @@ var ( addUnderpricedMeter = metrics.NewRegisteredMeter("blobpool/add/underpriced", nil) // Gas tip too low, neutral addStaleMeter = metrics.NewRegisteredMeter("blobpool/add/stale", nil) // Nonce already filled, reject, bad-ish addGappedMeter = metrics.NewRegisteredMeter("blobpool/add/gapped", nil) // Nonce gapped, reject, bad-ish + addGappedFullMeter = metrics.NewRegisteredMeter("blobpool/add/gappedfull", nil) // Gapped queue full, reject, neutral addOverdraftedMeter = metrics.NewRegisteredMeter("blobpool/add/overdrafted", nil) // Balance exceeded, reject, neutral addOvercappedMeter = metrics.NewRegisteredMeter("blobpool/add/overcapped", nil) // Per-account cap exceeded, reject, neutral addNoreplaceMeter = metrics.NewRegisteredMeter("blobpool/add/noreplace", nil) // Replacement fees or tips too low, neutral addNonExclusiveMeter = metrics.NewRegisteredMeter("blobpool/add/nonexclusive", nil) // Plain transaction from same account exists, reject, neutral addValidMeter = metrics.NewRegisteredMeter("blobpool/add/valid", nil) // Valid transaction, add, neutral + + // Gapped queue metrics for observability + gappedGauge = metrics.NewRegisteredGauge("blobpool/gapped/count", nil) // Current gapped queue size + gappedPromotedMeter = metrics.NewRegisteredMeter("blobpool/gapped/promoted", nil) // Gapped txs successfully promoted to pool + gappedEvictedMeter = metrics.NewRegisteredMeter("blobpool/gapped/evicted", nil) // Gapped txs evicted due to timeout/stale ) From efd6cdcff111c37bf49aefba0a7376997780a8c8 Mon Sep 17 00:00:00 2001 From: rayoo Date: Tue, 5 May 2026 03:36:26 +0800 Subject: [PATCH 46/80] eth/tracers: forward V2 state hooks through mux tracer (#34869) Fixes the muxTracer to correctly forward events to v2 state hooks, i.e. `OnCodeChangeV2` and `OnNonceChangeV2`. --- eth/tracers/native/mux.go | 26 +++++----- eth/tracers/native/mux_test.go | 87 ++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 12 deletions(-) create mode 100644 eth/tracers/native/mux_test.go diff --git a/eth/tracers/native/mux.go b/eth/tracers/native/mux.go index 362fb24297..bb2101deec 100644 --- a/eth/tracers/native/mux.go +++ b/eth/tracers/native/mux.go @@ -63,6 +63,12 @@ func newMuxTracerFromConfig(ctx *tracers.Context, cfg json.RawMessage, chainConf // // The names parameter associates a label with each tracer, used as keys in // the aggregated JSON result returned by GetResult. +// +// For hooks that have both a V1 and V2 form (OnCodeChange / OnCodeChangeV2, +// OnNonceChange / OnNonceChangeV2, OnSystemCallStart / OnSystemCallStartV2), +// the mux exposes only the V2 variant upward. The fanout then prefers each +// child's V2 hook and falls back to V1 if only V1 is set, mirroring the +// precedence already used in core/state_processor.go. func NewMuxTracer(names []string, objects []*tracers.Tracer) (*tracers.Tracer, error) { t := &muxTracer{names: names, tracers: objects} return &tracers.Tracer{ @@ -75,8 +81,8 @@ func NewMuxTracer(names []string, objects []*tracers.Tracer) (*tracers.Tracer, e OnFault: t.OnFault, OnGasChange: t.OnGasChange, OnBalanceChange: t.OnBalanceChange, - OnNonceChange: t.OnNonceChange, - OnCodeChange: t.OnCodeChange, + OnNonceChangeV2: t.OnNonceChangeV2, + OnCodeChangeV2: t.OnCodeChangeV2, OnStorageChange: t.OnStorageChange, OnLog: t.OnLog, OnSystemCallStartV2: t.OnSystemCallStart, @@ -151,26 +157,22 @@ func (t *muxTracer) OnBalanceChange(a common.Address, prev, new *big.Int, reason } } -func (t *muxTracer) OnNonceChange(a common.Address, prev, new uint64) { +func (t *muxTracer) OnNonceChangeV2(a common.Address, prev, new uint64, reason tracing.NonceChangeReason) { for _, t := range t.tracers { - if t.OnNonceChange != nil { + if t.OnNonceChangeV2 != nil { + t.OnNonceChangeV2(a, prev, new, reason) + } else if t.OnNonceChange != nil { t.OnNonceChange(a, prev, new) } } } -func (t *muxTracer) OnCodeChange(a common.Address, prevCodeHash common.Hash, prev []byte, codeHash common.Hash, code []byte) { - for _, t := range t.tracers { - if t.OnCodeChange != nil { - t.OnCodeChange(a, prevCodeHash, prev, codeHash, code) - } - } -} - func (t *muxTracer) OnCodeChangeV2(a common.Address, prevCodeHash common.Hash, prev []byte, codeHash common.Hash, code []byte, reason tracing.CodeChangeReason) { for _, t := range t.tracers { if t.OnCodeChangeV2 != nil { t.OnCodeChangeV2(a, prevCodeHash, prev, codeHash, code, reason) + } else if t.OnCodeChange != nil { + t.OnCodeChange(a, prevCodeHash, prev, codeHash, code) } } } diff --git a/eth/tracers/native/mux_test.go b/eth/tracers/native/mux_test.go new file mode 100644 index 0000000000..902b7a026a --- /dev/null +++ b/eth/tracers/native/mux_test.go @@ -0,0 +1,87 @@ +// Copyright 2026 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 . + +package native + +import ( + "testing" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/tracing" + "github.com/ethereum/go-ethereum/eth/tracers" +) + +// TestMuxForwardsV2StateHooks verifies that the mux tracer fans out the V2 +// variants of state-change hooks to child tracers. A child tracer that only +// implements OnCodeChangeV2 / OnNonceChangeV2 must still receive events when +// wrapped behind the mux. The mux must also fall back to the V1 hook when a +// child only implements V1, mirroring the precedence used in +// core/state_processor.go. +func TestMuxForwardsV2StateHooks(t *testing.T) { + var ( + codeV2Calls int + nonceV2Calls int + codeV1Calls int + nonceV1Calls int + ) + v2Child := &tracers.Tracer{ + Hooks: &tracing.Hooks{ + OnCodeChangeV2: func(addr common.Address, prevCodeHash common.Hash, prevCode []byte, codeHash common.Hash, code []byte, reason tracing.CodeChangeReason) { + codeV2Calls++ + }, + OnNonceChangeV2: func(addr common.Address, prev, new uint64, reason tracing.NonceChangeReason) { + nonceV2Calls++ + }, + }, + } + v1Child := &tracers.Tracer{ + Hooks: &tracing.Hooks{ + OnCodeChange: func(addr common.Address, prevCodeHash common.Hash, prevCode []byte, codeHash common.Hash, code []byte) { + codeV1Calls++ + }, + OnNonceChange: func(addr common.Address, prev, new uint64) { + nonceV1Calls++ + }, + }, + } + mux, err := NewMuxTracer([]string{"v2", "v1"}, []*tracers.Tracer{v2Child, v1Child}) + if err != nil { + t.Fatalf("NewMuxTracer: %v", err) + } + + if mux.Hooks.OnCodeChangeV2 == nil { + t.Fatal("mux does not expose OnCodeChangeV2; V2-only child tracers will miss code changes") + } + if mux.Hooks.OnNonceChangeV2 == nil { + t.Fatal("mux does not expose OnNonceChangeV2; V2-only child tracers will miss nonce changes") + } + + mux.Hooks.OnCodeChangeV2(common.Address{}, common.Hash{}, nil, common.Hash{}, nil, tracing.CodeChangeContractCreation) + mux.Hooks.OnNonceChangeV2(common.Address{}, 0, 1, tracing.NonceChangeEoACall) + + if codeV2Calls != 1 { + t.Fatalf("V2 child OnCodeChangeV2 got %d calls, want 1", codeV2Calls) + } + if nonceV2Calls != 1 { + t.Fatalf("V2 child OnNonceChangeV2 got %d calls, want 1", nonceV2Calls) + } + if codeV1Calls != 1 { + t.Fatalf("V1 child OnCodeChange got %d calls, want 1 (mux should fall back from V2 to V1)", codeV1Calls) + } + if nonceV1Calls != 1 { + t.Fatalf("V1 child OnNonceChange got %d calls, want 1 (mux should fall back from V2 to V1)", nonceV1Calls) + } +} From d5edb8043824333459a7bd02bcd05868b0d0d997 Mon Sep 17 00:00:00 2001 From: TenderDeve Date: Tue, 5 May 2026 14:59:26 +0530 Subject: [PATCH 47/80] accounts/abi/bind: re-export event signature errors (#34868) Re-exports errors in bind package. --------- Co-authored-by: Marius van der Wijden --- accounts/abi/bind/old.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/accounts/abi/bind/old.go b/accounts/abi/bind/old.go index b09f5f3c7a..1fe1b1cca5 100644 --- a/accounts/abi/bind/old.go +++ b/accounts/abi/bind/old.go @@ -176,6 +176,13 @@ var ( // ErrNoCodeAfterDeploy is returned by WaitDeployed if contract creation leaves // an empty contract behind. ErrNoCodeAfterDeploy = bind2.ErrNoCodeAfterDeploy + + // ErrNoEventSignature is returned when a log entry has no topics. + ErrNoEventSignature = bind2.ErrNoEventSignature + + // ErrEventSignatureMismatch is returned when a log's topic[0] does not match + // the expected event signature. + ErrEventSignatureMismatch = bind2.ErrEventSignatureMismatch ) // ContractCaller defines the methods needed to allow operating with a contract on a read From 5b837e578689e7db5679b25d0a159956563f6a93 Mon Sep 17 00:00:00 2001 From: cui Date: Tue, 5 May 2026 18:41:22 +0800 Subject: [PATCH 48/80] eth/downloader: use batch index in deliver reconstruct (#34870) The reconstruct callback indexes parallel response slices (bodies, receipts). Passing the accept counter used the wrong element when an earlier header in the same batch hit a stale slot. --- eth/downloader/queue.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/eth/downloader/queue.go b/eth/downloader/queue.go index dd17b7f1ed..585906b8bd 100644 --- a/eth/downloader/queue.go +++ b/eth/downloader/queue.go @@ -689,9 +689,9 @@ func (q *queue) deliver(id string, taskPool map[common.Hash]*types.Header, i++ } - for _, header := range request.Headers[:i] { + for k, header := range request.Headers[:i] { if res, stale, err := q.resultCache.GetDeliverySlot(header.Number.Uint64()); err == nil && !stale { - reconstruct(accepted, res) + reconstruct(k, res) accepted++ } else { // Between here and above, some other peer filled this result, From 60db25b070d5060a379231e6e0f69c047d0ffcbb Mon Sep 17 00:00:00 2001 From: rayoo Date: Tue, 5 May 2026 21:28:28 +0800 Subject: [PATCH 49/80] p2p/discover: restore nextTimeout update in UDPv4 resetTimeout loop (#34878) The refactor from `for el := plist.Front(); ...; el = el.Next()` to the new `iterList` iterator in #34743 silently dropped two things needed by resetTimeout: 1. `nextTimeout = el.Value.(*replyMatcher)` at the top of the loop. This assignment is what gives `nextTimeout` its documented meaning ("head of plist when timeout was last reset"), and what makes the early-return optimization at the top of resetTimeout work. Without it, nextTimeout is only ever written to nil, so `nextTimeout == plist.Front().Value` is always false and the optimization is dead. 2. `nextTimeout.errc <- errClockWarp` in the clock-warp branch now reads a stale or nil pointer. Prior to the refactor, the inner assignment kept nextTimeout pointing at the current matcher so its errc was the right channel to receive the errClockWarp signal. After the refactor, on first entry into the clock-warp branch nextTimeout is nil, which panics the UDPv4 loop goroutine with a nil pointer deref and takes discv4 down. Re-assign `nextTimeout = p` at the head of the loop (restoring the documented invariant) and send the clock-warp error on `p.errc` rather than the now-stale `nextTimeout.errc`. The clock-warp branch triggers only when the system clock jumps backward after a deadline is assigned (deadline - time.Now() >= 2*respTimeout, i.e. at least ~500ms backward jump), which is why this regression slipped past CI - it is not exercised by any existing unit test, and writing one would require plumbing a clock through the loop. --- p2p/discover/v4_udp.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/p2p/discover/v4_udp.go b/p2p/discover/v4_udp.go index 9e824dae18..b06db4bdb2 100644 --- a/p2p/discover/v4_udp.go +++ b/p2p/discover/v4_udp.go @@ -447,6 +447,7 @@ func (t *UDPv4) loop() { // Start the timer so it fires when the next pending reply has expired. now := time.Now() for p, el := range iterList[*replyMatcher](plist) { + nextTimeout = p if dist := p.deadline.Sub(now); dist < 2*respTimeout { timeout.Reset(dist) return @@ -454,7 +455,7 @@ func (t *UDPv4) loop() { // Remove pending replies whose deadline is too far in the // future. These can occur if the system clock jumped // backwards after the deadline was assigned. - nextTimeout.errc <- errClockWarp + p.errc <- errClockWarp plist.Remove(el) } nextTimeout = nil From 4ff33ba1b6ecfff7087136136ed13dd8c3a3ac38 Mon Sep 17 00:00:00 2001 From: Andrii Furmanets Date: Tue, 5 May 2026 23:17:09 +0300 Subject: [PATCH 50/80] internal/download: only report stale existing downloads (#34849) ## Summary Fixes #31917. `geth era-download` now only prints `is stale` when an existing downloaded file fails checksum verification. Missing files are still downloaded normally, but no longer get mislabeled as stale. ## Why `DownloadFile` used `verifyHash` for both missing files and checksum mismatches, then printed `is stale` for any error. This made first-time downloads look like corrupt or outdated files. ## Validation - `make all` - `go run ./build/ci.go test` - `go run ./build/ci.go lint` - `go run ./build/ci.go check_generate` - `go run ./build/ci.go check_baddeps` --------- Co-authored-by: Felix Lange --- internal/download/download.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/internal/download/download.go b/internal/download/download.go index c59c8a90c3..79cf27a56b 100644 --- a/internal/download/download.go +++ b/internal/download/download.go @@ -22,6 +22,7 @@ import ( "bytes" "crypto/sha256" "encoding/hex" + "errors" "fmt" "io" "iter" @@ -180,12 +181,13 @@ func (db *ChecksumDB) DownloadFile(url, dstPath string) error { return fmt.Errorf("no known hash for file %q", basename) } // Shortcut if already downloaded. - if verifyHash(dstPath, hash) == nil { + if err := verifyHash(dstPath, hash); err == nil { fmt.Printf("%s is up-to-date\n", dstPath) return nil + } else if !errors.Is(err, os.ErrNotExist) { + fmt.Printf("%s is stale\n", dstPath) } - fmt.Printf("%s is stale\n", dstPath) fmt.Printf("downloading from %s\n", url) resp, err := http.Get(url) if err != nil { From 84949107ce4592f901ef77ab082121432bf4b5f6 Mon Sep 17 00:00:00 2001 From: cui Date: Wed, 6 May 2026 15:18:12 +0800 Subject: [PATCH 51/80] beacon/engine: fix wrong presize bound (#34860) --- beacon/engine/types.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beacon/engine/types.go b/beacon/engine/types.go index a312fee88a..9b0b186df7 100644 --- a/beacon/engine/types.go +++ b/beacon/engine/types.go @@ -276,7 +276,7 @@ func ExecutableDataToBlockNoHash(data ExecutableData, versionedHashes []common.H if data.BaseFeePerGas != nil && (data.BaseFeePerGas.Sign() == -1 || data.BaseFeePerGas.BitLen() > 256) { return nil, fmt.Errorf("invalid baseFeePerGas: %v", data.BaseFeePerGas) } - var blobHashes = make([]common.Hash, 0, len(txs)) + var blobHashes = make([]common.Hash, 0, len(versionedHashes)) for _, tx := range txs { blobHashes = append(blobHashes, tx.BlobHashes()...) } From 4d2af275e1fe34eeafc50bbb61ea5bb96f11b10c Mon Sep 17 00:00:00 2001 From: Marius van der Wijden Date: Wed, 6 May 2026 09:59:51 +0200 Subject: [PATCH 52/80] eth/catalyst: allow reorging the head block to a parent (#34767) Implements https://github.com/ethereum/execution-apis/pull/786/changes as discussed on standup today --------- Co-authored-by: Gary Rong --- beacon/engine/errors.go | 1 + core/blockchain.go | 9 +++++++-- eth/catalyst/api.go | 25 +++++++++++++++++++++---- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/beacon/engine/errors.go b/beacon/engine/errors.go index 62773a0ea9..80e13b11b9 100644 --- a/beacon/engine/errors.go +++ b/beacon/engine/errors.go @@ -81,6 +81,7 @@ var ( TooLargeRequest = &EngineAPIError{code: -38004, msg: "Too large request"} InvalidParams = &EngineAPIError{code: -32602, msg: "Invalid parameters"} UnsupportedFork = &EngineAPIError{code: -38005, msg: "Unsupported fork"} + TooDeepReorg = &EngineAPIError{code: -38006, msg: "Too deep reorg"} STATUS_INVALID = ForkChoiceResponse{PayloadStatus: PayloadStatusV1{Status: INVALID}, PayloadID: nil} STATUS_SYNCING = ForkChoiceResponse{PayloadStatus: PayloadStatusV1{Status: SYNCING}, PayloadID: nil} diff --git a/core/blockchain.go b/core/blockchain.go index f21a1462ea..2b49111121 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -2596,8 +2596,13 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Header) error blockReorgAddMeter.Mark(int64(len(newChain))) } else { // len(newChain) == 0 && len(oldChain) > 0 - // rewind the canonical chain to a lower point. - log.Error("Impossible reorg, please file an issue", "oldnum", oldHead.Number, "oldhash", oldHead.Hash(), "oldblocks", len(oldChain), "newnum", newHead.Number, "newhash", newHead.Hash(), "newblocks", len(newChain)) + // Rewind the canonical chain to a lower point. In EPBs we can reorg to + // a parent of the head within 32 blocks. + if len(oldChain) > 32 { + log.Error("Impossible reorg, please file an issue", "oldnum", oldHead.Number, "oldhash", oldHead.Hash(), "oldblocks", len(oldChain)) + } else { + log.Info("Shorten chain", "del", len(oldChain), "number", oldHead.Number, "hash", oldHead.Hash()) + } } // Acquire the tx-lookup lock before mutation. This step is essential // as the txlookups should be changed atomically, and all subsequent diff --git a/eth/catalyst/api.go b/eth/catalyst/api.go index 8a4aced04b..b81ed57a2c 100644 --- a/eth/catalyst/api.go +++ b/eth/catalyst/api.go @@ -82,6 +82,9 @@ const ( // beaconUpdateWarnFrequency is the frequency at which to warn the user that // the beacon client is offline. beaconUpdateWarnFrequency = 5 * time.Minute + + // maxReorgDepth is the maximum reorg depth accepted via forkchoiceUpdated. + maxReorgDepth = 32 ) type ConsensusAPI struct { @@ -237,6 +240,7 @@ func (api *ConsensusAPI) ForkchoiceUpdatedV4(ctx context.Context, update engine. func (api *ConsensusAPI) forkchoiceUpdated(ctx context.Context, update engine.ForkchoiceStateV1, payloadAttributes *engine.PayloadAttributes, payloadVersion engine.PayloadVersion, payloadWitness bool) (result engine.ForkChoiceResponse, err error) { ctx, _, spanEnd := telemetry.StartSpan(ctx, "engine.forkchoiceUpdated") defer spanEnd(&err) + api.forkchoiceLock.Lock() defer api.forkchoiceLock.Unlock() @@ -321,10 +325,23 @@ func (api *ConsensusAPI) forkchoiceUpdated(ctx context.Context, update engine.Fo // generating the payload. It's a special corner case that a few slots are // missing and we are requested to generate the payload in slot. } else { - // If the head block is already in our canonical chain, the beacon client is - // probably resyncing. Ignore the update. - log.Info("Ignoring beacon update to old head", "number", block.NumberU64(), "hash", update.HeadBlockHash, "age", common.PrettyAge(time.Unix(int64(block.Time()), 0)), "have", api.eth.BlockChain().CurrentBlock().Number) - return valid(nil), nil + if finalized := api.eth.BlockChain().CurrentFinalBlock(); finalized != nil && block.NumberU64() <= finalized.Number.Uint64() { + log.Info("Skipping beacon update to finalized ancestor", "number", block.NumberU64(), "hash", update.HeadBlockHash) + return valid(nil), nil + } + depth := api.eth.BlockChain().CurrentBlock().Number.Uint64() - block.NumberU64() + if depth >= maxReorgDepth { + log.Warn("Refusing too deep reorg", "depth", depth, "head", update.HeadBlockHash) + return engine.STATUS_INVALID, engine.TooDeepReorg.With(fmt.Errorf("reorg depth %d exceeds limit %d", depth, maxReorgDepth)) + } + if !api.eth.Synced() { + log.Info("Ignoring beacon update to old head while syncing", "number", block.NumberU64(), "hash", update.HeadBlockHash) + return valid(nil), nil + } + if latestValid, err := api.eth.BlockChain().SetCanonical(block); err != nil { + log.Error("Error setting canonical", "number", block.NumberU64(), "hash", update.HeadBlockHash, "error", err) + return engine.ForkChoiceResponse{PayloadStatus: engine.PayloadStatusV1{Status: engine.INVALID, LatestValidHash: &latestValid}}, err + } } api.eth.SetSynced() From aaa2b662856dcd8527dfb452ceee605467967fb1 Mon Sep 17 00:00:00 2001 From: Marius van der Wijden Date: Wed, 6 May 2026 12:03:11 +0200 Subject: [PATCH 53/80] core: implement eip-7981: Increase Access List Cost (#34755) based on: https://github.com/ethereum/go-ethereum/pull/34748 spec: https://eips.ethereum.org/EIPS/eip-7981 --- cmd/evm/internal/t8ntool/transaction.go | 4 +- core/bench_test.go | 2 +- core/bintrie_witness_test.go | 4 +- core/state_transition.go | 66 +++++- core/state_transition_test.go | 287 ++++++++++++++++++++++++ core/txpool/validation.go | 4 +- tests/transaction_test_util.go | 4 +- 7 files changed, 354 insertions(+), 17 deletions(-) create mode 100644 core/state_transition_test.go diff --git a/cmd/evm/internal/t8ntool/transaction.go b/cmd/evm/internal/t8ntool/transaction.go index 7e068c06af..ca19ae3386 100644 --- a/cmd/evm/internal/t8ntool/transaction.go +++ b/cmd/evm/internal/t8ntool/transaction.go @@ -133,7 +133,7 @@ func Transaction(ctx *cli.Context) error { } // Check intrinsic gas rules := chainConfig.Rules(common.Big0, true, 0) - cost, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.SetCodeAuthorizations(), tx.To() == nil, rules.IsHomestead, rules.IsIstanbul, rules.IsShanghai) + cost, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.SetCodeAuthorizations(), tx.To() == nil, rules.IsHomestead, rules.IsIstanbul, rules.IsShanghai, rules.IsAmsterdam) if err != nil { r.Error = err results = append(results, r) @@ -147,7 +147,7 @@ func Transaction(ctx *cli.Context) error { } // For Prague txs, validate the floor data gas. if rules.IsPrague { - floorDataGas, err := core.FloorDataGas(rules, tx.Data()) + floorDataGas, err := core.FloorDataGas(rules, tx.Data(), tx.AccessList()) if err != nil { r.Error = err results = append(results, r) diff --git a/core/bench_test.go b/core/bench_test.go index 20d1a7794b..65179c54d4 100644 --- a/core/bench_test.go +++ b/core/bench_test.go @@ -89,7 +89,7 @@ func genValueTx(nbytes int) func(int, *BlockGen) { data := make([]byte, nbytes) return func(i int, gen *BlockGen) { toaddr := common.Address{} - cost, _ := IntrinsicGas(data, nil, nil, false, false, false, false) + cost, _ := IntrinsicGas(data, nil, nil, false, false, false, false, false) signer := gen.Signer() gasPrice := big.NewInt(0) if gen.header.BaseFee != nil { diff --git a/core/bintrie_witness_test.go b/core/bintrie_witness_test.go index 66feef0675..5f6239e4fa 100644 --- a/core/bintrie_witness_test.go +++ b/core/bintrie_witness_test.go @@ -63,12 +63,12 @@ var ( func TestProcessUBT(t *testing.T) { var ( code = common.FromHex(`6060604052600a8060106000396000f360606040526008565b00`) - intrinsicContractCreationGas, _ = IntrinsicGas(code, nil, nil, true, true, true, true) + intrinsicContractCreationGas, _ = IntrinsicGas(code, nil, nil, true, true, true, true, false) // A contract creation that calls EXTCODECOPY in the constructor. Used to ensure that the witness // will not contain that copied data. // Source: https://gist.github.com/gballet/a23db1e1cb4ed105616b5920feb75985 codeWithExtCodeCopy = common.FromHex(`0x60806040526040516100109061017b565b604051809103906000f08015801561002c573d6000803e3d6000fd5b506000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555034801561007857600080fd5b5060008067ffffffffffffffff8111156100955761009461024a565b5b6040519080825280601f01601f1916602001820160405280156100c75781602001600182028036833780820191505090505b50905060008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1690506020600083833c81610101906101e3565b60405161010d90610187565b61011791906101a3565b604051809103906000f080158015610133573d6000803e3d6000fd5b50600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550505061029b565b60d58061046783390190565b6102068061053c83390190565b61019d816101d9565b82525050565b60006020820190506101b86000830184610194565b92915050565b6000819050602082019050919050565b600081519050919050565b6000819050919050565b60006101ee826101ce565b826101f8846101be565b905061020381610279565b925060208210156102435761023e7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8360200360080261028e565b831692505b5050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b600061028582516101d9565b80915050919050565b600082821b905092915050565b6101bd806102aa6000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063f566852414610030575b600080fd5b61003861004e565b6040516100459190610146565b60405180910390f35b6000600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166381ca91d36040518163ffffffff1660e01b815260040160206040518083038186803b1580156100b857600080fd5b505afa1580156100cc573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906100f0919061010a565b905090565b60008151905061010481610170565b92915050565b6000602082840312156101205761011f61016b565b5b600061012e848285016100f5565b91505092915050565b61014081610161565b82525050565b600060208201905061015b6000830184610137565b92915050565b6000819050919050565b600080fd5b61017981610161565b811461018457600080fd5b5056fea2646970667358221220a6a0e11af79f176f9c421b7b12f441356b25f6489b83d38cc828a701720b41f164736f6c63430008070033608060405234801561001057600080fd5b5060b68061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063ab5ed15014602d575b600080fd5b60336047565b604051603e9190605d565b60405180910390f35b60006001905090565b6057816076565b82525050565b6000602082019050607060008301846050565b92915050565b600081905091905056fea26469706673582212203a14eb0d5cd07c277d3e24912f110ddda3e553245a99afc4eeefb2fbae5327aa64736f6c63430008070033608060405234801561001057600080fd5b5060405161020638038061020683398181016040528101906100329190610063565b60018160001c6100429190610090565b60008190555050610145565b60008151905061005d8161012e565b92915050565b60006020828403121561007957610078610129565b5b60006100878482850161004e565b91505092915050565b600061009b826100f0565b91506100a6836100f0565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff038211156100db576100da6100fa565b5b828201905092915050565b6000819050919050565b6000819050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b600080fd5b610137816100e6565b811461014257600080fd5b50565b60b3806101536000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c806381ca91d314602d575b600080fd5b60336047565b604051603e9190605a565b60405180910390f35b60005481565b6054816073565b82525050565b6000602082019050606d6000830184604d565b92915050565b600081905091905056fea26469706673582212209bff7098a2f526de1ad499866f27d6d0d6f17b74a413036d6063ca6a0998ca4264736f6c63430008070033`) - intrinsicCodeWithExtCodeCopyGas, _ = IntrinsicGas(codeWithExtCodeCopy, nil, nil, true, true, true, true) + intrinsicCodeWithExtCodeCopyGas, _ = IntrinsicGas(codeWithExtCodeCopy, nil, nil, true, true, true, true, false) signer = types.LatestSigner(testUBTChainConfig) testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") bcdb = rawdb.NewMemoryDatabase() // Database for the blockchain diff --git a/core/state_transition.go b/core/state_transition.go index c7b0593857..b5b8b22155 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -68,7 +68,7 @@ func (result *ExecutionResult) Revert() []byte { } // IntrinsicGas computes the 'intrinsic gas' for a message with the given data. -func IntrinsicGas(data []byte, accessList types.AccessList, authList []types.SetCodeAuthorization, isContractCreation, isHomestead, isEIP2028, isEIP3860 bool) (vm.GasCosts, error) { +func IntrinsicGas(data []byte, accessList types.AccessList, authList []types.SetCodeAuthorization, isContractCreation, isHomestead, isEIP2028, isEIP3860, isAmsterdam bool) (vm.GasCosts, error) { // Set the starting gas for the raw transaction var gas uint64 if isContractCreation && isHomestead { @@ -107,8 +107,32 @@ func IntrinsicGas(data []byte, accessList types.AccessList, authList []types.Set } } if accessList != nil { - gas += uint64(len(accessList)) * params.TxAccessListAddressGas - gas += uint64(accessList.StorageKeys()) * params.TxAccessListStorageKeyGas + addresses := uint64(len(accessList)) + storageKeys := uint64(accessList.StorageKeys()) + if (math.MaxUint64-gas)/params.TxAccessListAddressGas < addresses { + return vm.GasCosts{}, ErrGasUintOverflow + } + gas += addresses * params.TxAccessListAddressGas + if (math.MaxUint64-gas)/params.TxAccessListStorageKeyGas < storageKeys { + return vm.GasCosts{}, ErrGasUintOverflow + } + gas += storageKeys * params.TxAccessListStorageKeyGas + + // EIP-7981: access list data is charged in addition to the base charge. + if isAmsterdam { + const ( + addressCost = common.AddressLength * params.TxCostFloorPerToken7976 * params.TxTokenPerNonZeroByte + storageKeyCost = common.HashLength * params.TxCostFloorPerToken7976 * params.TxTokenPerNonZeroByte + ) + if (math.MaxUint64-gas)/addressCost < addresses { + return vm.GasCosts{}, ErrGasUintOverflow + } + gas += addresses * addressCost + if (math.MaxUint64-gas)/storageKeyCost < storageKeys { + return vm.GasCosts{}, ErrGasUintOverflow + } + gas += storageKeys * storageKeyCost + } } if authList != nil { gas += uint64(len(authList)) * params.CallNewAccountGas @@ -117,7 +141,7 @@ func IntrinsicGas(data []byte, accessList types.AccessList, authList []types.Set } // FloorDataGas computes the minimum gas required for a transaction based on its data tokens (EIP-7623). -func FloorDataGas(rules params.Rules, data []byte) (uint64, error) { +func FloorDataGas(rules params.Rules, data []byte, accessList types.AccessList) (uint64, error) { var ( tokens uint64 tokenCost uint64 @@ -125,15 +149,41 @@ func FloorDataGas(rules params.Rules, data []byte) (uint64, error) { if rules.IsAmsterdam { // EIP-7976 changes how calldata is priced. // From 10/40 to 64/64 for zero/non-zero bytes. - tokens = uint64(len(data)) * params.TxTokenPerNonZeroByte tokenCost = params.TxCostFloorPerToken7976 + dataLen := uint64(len(data)) + if math.MaxUint64/params.TxTokenPerNonZeroByte < dataLen { + return 0, ErrGasUintOverflow + } + tokens = dataLen * params.TxTokenPerNonZeroByte + + // EIP-7981 adds additional tokens for every entry in the accesslist + const addressTokenCost = uint64(common.AddressLength) * params.TxTokenPerNonZeroByte + addresses := uint64(len(accessList)) + if (math.MaxUint64-tokens)/addressTokenCost < addresses { + return 0, ErrGasUintOverflow + } + tokens += addresses * addressTokenCost + + const storageKeyTokenCost = uint64(common.HashLength) * params.TxTokenPerNonZeroByte + storageKeys := uint64(accessList.StorageKeys()) + if (math.MaxUint64-tokens)/storageKeyTokenCost < storageKeys { + return 0, ErrGasUintOverflow + } + tokens += storageKeys * storageKeyTokenCost } else { var ( z = uint64(bytes.Count(data, []byte{0})) nz = uint64(len(data)) - z ) // Pre-Amsterdam - tokens = nz*params.TxTokenPerNonZeroByte + z + if math.MaxUint64/params.TxTokenPerNonZeroByte < nz { + return 0, ErrGasUintOverflow + } + tokens = nz * params.TxTokenPerNonZeroByte + if math.MaxUint64-tokens < z { + return 0, ErrGasUintOverflow + } + tokens += z tokenCost = params.TxCostFloorPerToken } @@ -462,7 +512,7 @@ func (st *stateTransition) execute() (*ExecutionResult, error) { floorDataGas uint64 ) // Check clauses 4-5, subtract intrinsic gas if everything is correct - cost, err := IntrinsicGas(msg.Data, msg.AccessList, msg.SetCodeAuthorizations, contractCreation, rules.IsHomestead, rules.IsIstanbul, rules.IsShanghai) + cost, err := IntrinsicGas(msg.Data, msg.AccessList, msg.SetCodeAuthorizations, contractCreation, rules.IsHomestead, rules.IsIstanbul, rules.IsShanghai, rules.IsAmsterdam) if err != nil { return nil, err } @@ -475,7 +525,7 @@ func (st *stateTransition) execute() (*ExecutionResult, error) { } // Gas limit suffices for the floor data cost (EIP-7623) if rules.IsPrague { - floorDataGas, err = FloorDataGas(rules, msg.Data) + floorDataGas, err = FloorDataGas(rules, msg.Data, msg.AccessList) if err != nil { return nil, err } diff --git a/core/state_transition_test.go b/core/state_transition_test.go new file mode 100644 index 0000000000..8aab016123 --- /dev/null +++ b/core/state_transition_test.go @@ -0,0 +1,287 @@ +// Copyright 2026 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 . + +package core + +import ( + "bytes" + "testing" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/core/vm" + "github.com/ethereum/go-ethereum/params" +) + +func TestFloorDataGas(t *testing.T) { + addr1 := common.HexToAddress("0x1111111111111111111111111111111111111111") + addr2 := common.HexToAddress("0x2222222222222222222222222222222222222222") + key1 := common.HexToHash("0xaa") + key2 := common.HexToHash("0xbb") + + tests := []struct { + name string + amsterdam bool + data []byte + accessList types.AccessList + want uint64 + }{ + { + name: "pre-amsterdam/empty", + want: params.TxGas, + }, + { + name: "pre-amsterdam/zero-bytes-only", + data: bytes.Repeat([]byte{0x00}, 100), + // 100 zero tokens * 10 cost = 1000 + want: params.TxGas + 100*params.TxCostFloorPerToken, + }, + { + name: "pre-amsterdam/non-zero-bytes-only", + data: bytes.Repeat([]byte{0xff}, 100), + // 100 nz * 4 tokens * 10 cost = 4000 + want: params.TxGas + 100*params.TxTokenPerNonZeroByte*params.TxCostFloorPerToken, + }, + { + name: "pre-amsterdam/mixed", + data: append(bytes.Repeat([]byte{0x00}, 50), bytes.Repeat([]byte{0xff}, 50)...), + // 50 zero + 50*4 nz = 250 tokens * 10 = 2500 + want: params.TxGas + (50+50*params.TxTokenPerNonZeroByte)*params.TxCostFloorPerToken, + }, + { + name: "pre-amsterdam/access-list-ignored", + data: bytes.Repeat([]byte{0xff}, 10), + accessList: types.AccessList{ + {Address: addr1, StorageKeys: []common.Hash{key1, key2}}, + }, + // pre-amsterdam: floor calculation does not include access list + want: params.TxGas + 10*params.TxTokenPerNonZeroByte*params.TxCostFloorPerToken, + }, + { + name: "amsterdam/empty", + amsterdam: true, + want: params.TxGas, + }, + { + name: "amsterdam/data-only", + amsterdam: true, + data: bytes.Repeat([]byte{0x00}, 1024), + // post-amsterdam: every byte = 4 tokens regardless of value + want: params.TxGas + 1024*params.TxTokenPerNonZeroByte*params.TxCostFloorPerToken7976, + }, + { + name: "amsterdam/data-non-zero", + amsterdam: true, + data: bytes.Repeat([]byte{0xff}, 1024), + // same as zero data post-amsterdam + want: params.TxGas + 1024*params.TxTokenPerNonZeroByte*params.TxCostFloorPerToken7976, + }, + { + name: "amsterdam/access-list-addresses-only", + amsterdam: true, + accessList: types.AccessList{ + {Address: addr1}, + {Address: addr2}, + }, + // 2 * 20 bytes * 4 tokens/byte * 16 cost/token + want: params.TxGas + 2*common.AddressLength*params.TxTokenPerNonZeroByte*params.TxCostFloorPerToken7976, + }, + { + name: "amsterdam/access-list-with-storage-keys", + amsterdam: true, + accessList: types.AccessList{ + {Address: addr1, StorageKeys: []common.Hash{key1, key2}}, + }, + // 1 addr * 20 * 4 + 2 keys * 32 * 4 = 80 + 256 = 336 tokens * 16 + want: params.TxGas + (1*common.AddressLength+2*common.HashLength)*params.TxTokenPerNonZeroByte*params.TxCostFloorPerToken7976, + }, + { + name: "amsterdam/mixed", + amsterdam: true, + data: bytes.Repeat([]byte{0xff}, 100), + accessList: types.AccessList{ + {Address: addr1, StorageKeys: []common.Hash{key1}}, + {Address: addr2, StorageKeys: []common.Hash{key1, key2}}, + }, + // data: 100*4 = 400; addrs: 2*20*4 = 160; keys: 3*32*4 = 384; total = 944 * 16 + want: params.TxGas + (100*params.TxTokenPerNonZeroByte+2*common.AddressLength*params.TxTokenPerNonZeroByte+3*common.HashLength*params.TxTokenPerNonZeroByte)*params.TxCostFloorPerToken7976, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + rules := params.Rules{IsAmsterdam: tt.amsterdam} + got, err := FloorDataGas(rules, tt.data, tt.accessList) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if got != tt.want { + t.Fatalf("gas mismatch: got %d, want %d", got, tt.want) + } + }) + } +} + +func TestIntrinsicGas(t *testing.T) { + addr1 := common.HexToAddress("0x1111111111111111111111111111111111111111") + addr2 := common.HexToAddress("0x2222222222222222222222222222222222222222") + key1 := common.HexToHash("0xaa") + key2 := common.HexToHash("0xbb") + + const ( + amsterdamAddressCost = uint64(common.AddressLength) * params.TxCostFloorPerToken7976 * params.TxTokenPerNonZeroByte // 1280 + amsterdamStorageKeyCost = uint64(common.HashLength) * params.TxCostFloorPerToken7976 * params.TxTokenPerNonZeroByte // 2048 + ) + + tests := []struct { + name string + data []byte + accessList types.AccessList + authList []types.SetCodeAuthorization + creation bool + isHomestead bool + isEIP2028 bool + isEIP3860 bool + isAmsterdam bool + want uint64 + }{ + { + name: "frontier/empty-call", + want: params.TxGas, + }, + { + name: "frontier/contract-creation-pre-homestead", + creation: true, + isHomestead: false, + // pre-homestead, contract creation still uses TxGas + want: params.TxGas, + }, + { + name: "homestead/contract-creation", + creation: true, + isHomestead: true, + want: params.TxGasContractCreation, + }, + { + name: "frontier/non-zero-data", + data: bytes.Repeat([]byte{0xff}, 100), + // 100 nz bytes * 68 (frontier) + want: params.TxGas + 100*params.TxDataNonZeroGasFrontier, + }, + { + name: "istanbul/non-zero-data", + data: bytes.Repeat([]byte{0xff}, 100), + isEIP2028: true, + // 100 nz bytes * 16 (post-EIP2028) + want: params.TxGas + 100*params.TxDataNonZeroGasEIP2028, + }, + { + name: "istanbul/zero-data", + data: bytes.Repeat([]byte{0x00}, 100), + isEIP2028: true, + // 100 zero bytes * 4 + want: params.TxGas + 100*params.TxDataZeroGas, + }, + { + name: "istanbul/mixed-data", + data: append(bytes.Repeat([]byte{0x00}, 50), bytes.Repeat([]byte{0xff}, 50)...), + isEIP2028: true, + want: params.TxGas + 50*params.TxDataZeroGas + 50*params.TxDataNonZeroGasEIP2028, + }, + { + name: "shanghai/init-code-word-gas", + data: bytes.Repeat([]byte{0x00}, 64), // 2 words + creation: true, + isHomestead: true, + isEIP2028: true, + isEIP3860: true, + // TxGasContractCreation + 64 zero bytes * 4 + 2 words * 2 + want: params.TxGasContractCreation + 64*params.TxDataZeroGas + 2*params.InitCodeWordGas, + }, + { + name: "shanghai/init-code-non-multiple-of-32", + data: bytes.Repeat([]byte{0x00}, 33), // 2 words (rounded up) + creation: true, + isHomestead: true, + isEIP2028: true, + isEIP3860: true, + want: params.TxGasContractCreation + 33*params.TxDataZeroGas + 2*params.InitCodeWordGas, + }, + { + name: "berlin/access-list", + accessList: types.AccessList{ + {Address: addr1, StorageKeys: []common.Hash{key1, key2}}, + {Address: addr2, StorageKeys: []common.Hash{key1}}, + }, + isEIP2028: true, + // 2 addrs * 2400 + 3 keys * 1900 + want: params.TxGas + 2*params.TxAccessListAddressGas + 3*params.TxAccessListStorageKeyGas, + }, + { + name: "amsterdam/access-list-extra-cost", + accessList: types.AccessList{ + {Address: addr1, StorageKeys: []common.Hash{key1, key2}}, + {Address: addr2, StorageKeys: []common.Hash{key1}}, + }, + isEIP2028: true, + isAmsterdam: true, + // base access-list charge + EIP-7981 extra + want: params.TxGas + + 2*params.TxAccessListAddressGas + 3*params.TxAccessListStorageKeyGas + + 2*amsterdamAddressCost + 3*amsterdamStorageKeyCost, + }, + { + name: "prague/auth-list", + authList: []types.SetCodeAuthorization{ + {Address: addr1}, + {Address: addr2}, + {Address: addr1}, + }, + isEIP2028: true, + // 3 auths * 25000 + want: params.TxGas + 3*params.CallNewAccountGas, + }, + { + name: "amsterdam/combined", + data: bytes.Repeat([]byte{0xff}, 100), + accessList: types.AccessList{ + {Address: addr1, StorageKeys: []common.Hash{key1}}, + }, + authList: []types.SetCodeAuthorization{ + {Address: addr2}, + }, + isEIP2028: true, + isAmsterdam: true, + want: params.TxGas + + 100*params.TxDataNonZeroGasEIP2028 + + 1*params.TxAccessListAddressGas + 1*params.TxAccessListStorageKeyGas + + 1*amsterdamAddressCost + 1*amsterdamStorageKeyCost + + 1*params.CallNewAccountGas, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := IntrinsicGas(tt.data, tt.accessList, tt.authList, + tt.creation, tt.isHomestead, tt.isEIP2028, tt.isEIP3860, tt.isAmsterdam) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + want := vm.GasCosts{RegularGas: tt.want} + if got != want { + t.Fatalf("gas mismatch: got %+v, want %+v", got, want) + } + }) + } +} diff --git a/core/txpool/validation.go b/core/txpool/validation.go index 6891dc94d2..c87bba31ac 100644 --- a/core/txpool/validation.go +++ b/core/txpool/validation.go @@ -125,7 +125,7 @@ func ValidateTransaction(tx *types.Transaction, head *types.Header, signer types } // Ensure the transaction has more gas than the bare minimum needed to cover // the transaction metadata - intrGas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.SetCodeAuthorizations(), tx.To() == nil, true, rules.IsIstanbul, rules.IsShanghai) + intrGas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.SetCodeAuthorizations(), tx.To() == nil, true, rules.IsIstanbul, rules.IsShanghai, rules.IsAmsterdam) if err != nil { return err } @@ -134,7 +134,7 @@ func ValidateTransaction(tx *types.Transaction, head *types.Header, signer types } // Ensure the transaction can cover floor data gas. if rules.IsPrague { - floorDataGas, err := core.FloorDataGas(rules, tx.Data()) + floorDataGas, err := core.FloorDataGas(rules, tx.Data(), tx.AccessList()) if err != nil { return err } diff --git a/tests/transaction_test_util.go b/tests/transaction_test_util.go index 8b8d0357bf..572c109f1e 100644 --- a/tests/transaction_test_util.go +++ b/tests/transaction_test_util.go @@ -81,7 +81,7 @@ func (tt *TransactionTest) Run() error { return } // Intrinsic gas - cost, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.SetCodeAuthorizations(), tx.To() == nil, rules.IsHomestead, rules.IsIstanbul, rules.IsShanghai) + cost, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.SetCodeAuthorizations(), tx.To() == nil, rules.IsHomestead, rules.IsIstanbul, rules.IsShanghai, rules.IsAmsterdam) if err != nil { return } @@ -92,7 +92,7 @@ func (tt *TransactionTest) Run() error { if rules.IsPrague { var floorDataGas uint64 - floorDataGas, err = core.FloorDataGas(rules, tx.Data()) + floorDataGas, err = core.FloorDataGas(rules, tx.Data(), tx.AccessList()) if err != nil { return } From b92c86deb79dff0338545b0a530442b2c7abf36c Mon Sep 17 00:00:00 2001 From: Rahman Date: Wed, 6 May 2026 07:02:03 -0600 Subject: [PATCH 54/80] internal/download: don't discard `dst.Close` error (#34866) When `io.Copy` succeeds but the buffered `Close` fails (e.g. disk full on `Flush`), the error was swallowed and verification reported a misleading hash mismatch instead of the real I/O failure. Keep the `Close` error when `io.Copy` didn't already produce one. --------- Co-authored-by: Jared Wasinger --- internal/download/download.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/internal/download/download.go b/internal/download/download.go index 79cf27a56b..27d3732731 100644 --- a/internal/download/download.go +++ b/internal/download/download.go @@ -211,9 +211,11 @@ func (db *ChecksumDB) DownloadFile(url, dstPath string) error { if resp.ContentLength > 0 { dst = newDownloadWriter(fd, resp.ContentLength) } - _, err = io.Copy(dst, resp.Body) - dst.Close() - if err != nil { + if _, err = io.Copy(dst, resp.Body); err != nil { + os.Remove(tmpfile) + return err + } + if err = dst.Close(); err != nil { os.Remove(tmpfile) return err } From 06c30cc7e113a639499c2c5d669901bbfc8c189d Mon Sep 17 00:00:00 2001 From: Jonny Rhea <5555162+jrhea@users.noreply.github.com> Date: Wed, 6 May 2026 08:08:15 -0500 Subject: [PATCH 55/80] triedb/pathdb: add AdoptSyncedState for snap/2 completion path (#34874) This PR adds `AdoptSyncedState()` alongside `Enable()`. It does the same pathdb bookkeeping (now factored into a shared `resetForReactivation()` helper), but skips the regeneration. The wiring/calling code lands in #34626 --------- Co-authored-by: Gary Rong --- triedb/database.go | 10 +++++ triedb/pathdb/database.go | 81 ++++++++++++++++++++++++++-------- triedb/pathdb/database_test.go | 78 ++++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+), 19 deletions(-) diff --git a/triedb/database.go b/triedb/database.go index 0fd3e1aa91..ef95169df1 100644 --- a/triedb/database.go +++ b/triedb/database.go @@ -327,6 +327,16 @@ func (db *Database) Enable(root common.Hash) error { return pdb.Enable(root) } +// AdoptSyncedState activates the database after a snap/2 sync and adopts the +// flat state populated during sync as-is, skipping regeneration. +func (db *Database) AdoptSyncedState(root common.Hash) error { + pdb, ok := db.backend.(*pathdb.Database) + if !ok { + return errors.New("not supported") + } + return pdb.AdoptSyncedState(root) +} + // Journal commits an entire diff hierarchy to disk into a single journal entry. // This is meant to be used during shutdown to persist the snapshot without // flattening everything down (bad for reorgs). It's only supported by path-based diff --git a/triedb/pathdb/database.go b/triedb/pathdb/database.go index 98975d7fa5..e52949c93e 100644 --- a/triedb/pathdb/database.go +++ b/triedb/pathdb/database.go @@ -365,16 +365,9 @@ func (db *Database) Disable() error { return nil } -// Enable activates database and resets the state tree with the provided persistent -// state root once the state sync is finished. -func (db *Database) Enable(root common.Hash) error { - db.lock.Lock() - defer db.lock.Unlock() - - // Short circuit if the database is in read only mode. - if db.readOnly { - return errDatabaseReadOnly - } +// resetForReactivation performs the pathdb-side bookkeeping shared by both +// Enable and AdoptSyncedState. +func (db *Database) resetForReactivation(root common.Hash) error { // Ensure the provided state root matches the stored one. stored, err := db.hasher(rawdb.ReadAccountTrieNode(db.diskdb, nil)) if err != nil { @@ -383,27 +376,40 @@ func (db *Database) Enable(root common.Hash) error { if stored != root { return fmt.Errorf("state root mismatch: stored %x, synced %x", stored, root) } - // Drop the stale state journal in persistent database and - // reset the persistent state id back to zero. + // Drop the stale state journal marker and reset the persistent state id + // back to zero. batch := db.diskdb.NewBatch() rawdb.DeleteSnapshotRoot(batch) rawdb.WritePersistentStateID(batch, 0) if err := batch.Write(); err != nil { return err } - // Clean up all state histories in freezer. Theoretically - // all root->id mappings should be removed as well. Since - // mappings can be huge and might take a while to clear - // them, just leave them in disk and wait for overwriting. + // Clean up all state histories in the freezer. Theoretically all root->id + // mappings should be removed as well; since those can be huge, leave them + // on disk and let them be overwritten. purgeHistory(db.stateFreezer, db.diskdb, typeStateHistory) purgeHistory(db.trienodeFreezer, db.diskdb, typeTrienodeHistory) - // Re-enable the database as the final step. + // Re-enable the database as the final bookkeeping step. db.waitSync = false rawdb.WriteSnapSyncStatusFlag(db.diskdb, rawdb.StateSyncFinished) + return nil +} - // Re-construct a new disk layer backed by persistent state - // and schedule the state snapshot generation if it's permitted. +// Enable activates the database after a snap/1 sync and schedules background +// regeneration of the snapshot from the trie. +func (db *Database) Enable(root common.Hash) error { + db.lock.Lock() + defer db.lock.Unlock() + + if db.readOnly { + return errDatabaseReadOnly + } + if err := db.resetForReactivation(root); err != nil { + return err + } + // Re-construct a new disk layer backed by persistent state and schedule + // the state snapshot generation if it's permitted. db.tree.init(generateSnapshot(db, root, db.isUBT || db.config.SnapshotNoBuild)) // After snap sync, the state of the database may have changed completely. @@ -416,6 +422,43 @@ func (db *Database) Enable(root common.Hash) error { return nil } +// AdoptSyncedState reactivates the database after a snap/2 sync. The syncer +// already wrote a consistent flat state, so we take it as-is instead of +// rebuilding it from the trie. The new disk layer has no generator attached, +// and a "done" marker is written so future boots know the snapshot is +// already complete. +func (db *Database) AdoptSyncedState(root common.Hash) error { + db.lock.Lock() + defer db.lock.Unlock() + + if db.readOnly { + return errDatabaseReadOnly + } + if err := db.resetForReactivation(root); err != nil { + return err + } + + // Tell the snapshot subsystem the flat state is good by writing the new root + // and a "done" marker (nil journal) so the next boot doesn't try to rebuild it. + batch := db.diskdb.NewBatch() + rawdb.WriteSnapshotRoot(batch, root) + journalProgress(batch, nil, nil) + if err := batch.Write(); err != nil { + return err + } + + // New disk layer, no generator attached. Nothing to rebuild, and reads + // can serve the flat state right away without waiting on a generator to + // scan past every key. + dl := newDiskLayer(root, 0, db, nil, nil, newBuffer(db.config.WriteBufferSize, nil, nil, 0), nil) + db.tree.init(dl) + + db.setHistoryIndexer() + + log.Info("Adopted synced state", "root", root) + return nil +} + // Recover rollbacks the database to a specified historical point. // The state is supported as the rollback destination only if it's // canonical state and the corresponding trie histories are existent. diff --git a/triedb/pathdb/database_test.go b/triedb/pathdb/database_test.go index 8ceb22eaba..41212dc9d0 100644 --- a/triedb/pathdb/database_test.go +++ b/triedb/pathdb/database_test.go @@ -748,6 +748,84 @@ func TestDisable(t *testing.T) { } } +// TestAdoptSyncedState verifies that AdoptSyncedState rejects a wrong root, +// writes the on-disk markers that say the snapshot is already complete, +// leaves a single fresh disk layer with no generator attached, and clears +// out stale state histories. +func TestAdoptSyncedState(t *testing.T) { + maxDiffLayers = 4 + defer func() { + maxDiffLayers = 128 + }() + + tester := newTester(t, &testerConfig{layers: 12}) + defer tester.release() + + // Push everything down to disk so the trie root is the persistent root. + if err := tester.db.Commit(tester.lastHash(), false); err != nil { + t.Fatalf("Failed to commit, err: %v", err) + } + stored := crypto.Keccak256Hash(rawdb.ReadAccountTrieNode(tester.db.diskdb, nil)) + + // Mimic the snap-syncing state. + if err := tester.db.Disable(); err != nil { + t.Fatalf("Failed to disable database: %v", err) + } + // Mismatched root must be rejected. + if err := tester.db.AdoptSyncedState(types.EmptyRootHash); err == nil { + t.Fatal("Mismatched root should be rejected") + } + if err := tester.db.AdoptSyncedState(stored); err != nil { + t.Fatalf("AdoptSyncedState failed: %v", err) + } + + // On-disk markers reflect a completed snapshot. + if got := rawdb.ReadSnapshotRoot(tester.db.diskdb); got != stored { + t.Fatalf("SnapshotRoot mismatch: got %x want %x", got, stored) + } + if blob := rawdb.ReadSnapshotGenerator(tester.db.diskdb); len(blob) == 0 { + t.Fatal("Generator journal not written") + } else { + var entry journalGenerator + if err := rlp.DecodeBytes(blob, &entry); err != nil { + t.Fatalf("Failed to decode generator journal: %v", err) + } + if !entry.Done { + t.Fatal("Generator journal should be marked Done") + } + // RLP turns a nil slice into an empty one on decode, so check length. + if len(entry.Marker) != 0 { + t.Fatalf("Generator marker should be empty, got %x", entry.Marker) + } + } + if rawdb.ReadSnapSyncStatusFlag(tester.db.diskdb) != rawdb.StateSyncFinished { + t.Fatal("Sync-status flag should be StateSyncFinished") + } + if tester.db.waitSync { + t.Fatal("waitSync should be false after adopt") + } + + // State histories are purged. + if n, err := tester.db.stateFreezer.Ancients(); err != nil || n != 0 { + t.Fatalf("State histories not purged: count=%d err=%v", n, err) + } + + // Layer tree has a single disk layer with no generator attached. + if got := tester.db.tree.len(); got != 1 { + t.Fatalf("Expected single layer, got %d", got) + } + dl := tester.db.tree.bottom() + if dl.rootHash() != stored { + t.Fatalf("Disk layer root mismatch: got %x want %x", dl.rootHash(), stored) + } + if dl.generator != nil { + t.Fatal("Disk layer should have no generator after adopt") + } + if dl.genMarker() != nil { + t.Fatal("genMarker should be nil after adopt") + } +} + func TestCommit(t *testing.T) { // Redefine the diff layer depth allowance for faster testing. maxDiffLayers = 4 From ea1cf7bf5ee07562284f9d050a6def9704d258e7 Mon Sep 17 00:00:00 2001 From: Bosul Mun Date: Wed, 6 May 2026 15:36:54 +0200 Subject: [PATCH 56/80] eth/protocols/eth: stop serving on unavailable responses (#34787) This is an alternative PR for https://github.com/ethereum/go-ethereum/pull/34746. This PR implements the second approach among the two possible solutions mentioned in the above PR. Requests for unavailable items are possible when the peer is following a different fork from us. However this is not expected to happen frequently. Considering the amount of complexity added to the codebase, the simpler approach (this PR) can be preferred. --- eth/protocols/eth/handler_test.go | 14 +++++++++----- eth/protocols/eth/handlers.go | 21 ++++++++++++--------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/eth/protocols/eth/handler_test.go b/eth/protocols/eth/handler_test.go index a45abc90eb..d056d121d9 100644 --- a/eth/protocols/eth/handler_test.go +++ b/eth/protocols/eth/handler_test.go @@ -424,16 +424,20 @@ func testGetBlockBodies(t *testing.T, protocol uint) { {0, []common.Hash{backend.chain.CurrentBlock().Hash()}, []bool{true}, 1}, // The chains head block should be retrievable {0, []common.Hash{{}}, []bool{false}, 0}, // A non existent block should not be returned - // Existing and non-existing blocks interleaved should not cause problems + // Existing blocks followed by a non-existing one should stop at the gap {0, []common.Hash{ - {}, backend.chain.GetBlockByNumber(1).Hash(), - {}, backend.chain.GetBlockByNumber(10).Hash(), - {}, backend.chain.GetBlockByNumber(100).Hash(), {}, - }, []bool{false, true, false, true, false, true, false}, 3}, + }, []bool{true, true, true, false}, 3}, + + // A non-existing block at the start should return nothing + {0, []common.Hash{ + {}, + backend.chain.GetBlockByNumber(1).Hash(), + backend.chain.GetBlockByNumber(10).Hash(), + }, []bool{false, true, true}, 0}, } // Run each of the tests and verify the results against the chain for i, tt := range tests { diff --git a/eth/protocols/eth/handlers.go b/eth/protocols/eth/handlers.go index 7556df9af2..3254a0abc2 100644 --- a/eth/protocols/eth/handlers.go +++ b/eth/protocols/eth/handlers.go @@ -238,10 +238,12 @@ func ServiceGetBlockBodiesQuery(chain *core.BlockChain, query GetBlockBodiesRequ lookups >= 2*maxBodiesServe { break } - if data := chain.GetBodyRLP(hash); len(data) != 0 { - bodies = append(bodies, data) - bytes += len(data) + data := chain.GetBodyRLP(hash) + if len(data) == 0 { + break // If we don't have this block's body, stop serving. } + bodies = append(bodies, data) + bytes += len(data) } return bodies } @@ -281,16 +283,16 @@ func ServiceGetReceiptsQuery69(chain *core.BlockChain, query GetReceiptsRequest) // Retrieve the requested block's receipts results := chain.GetReceiptsRLP(hash) if results == nil { - continue // Can't retrieve the receipts, so we just skip this block. + break // Don't have this block's receipts, stop serving. } body := chain.GetBodyRLP(hash) if body == nil { - continue // The block body is missing, we also have to skip. + break // The block body is missing, stop serving. } results, _, err := blockReceiptsToNetwork(results, body, receiptQueryParams{}) if err != nil { log.Error("Error in block receipts conversion", "hash", hash, "err", err) - continue + break } receipts.AppendRaw(results) bytes += len(results) @@ -312,12 +314,13 @@ func serviceGetReceiptsQuery70(chain *core.BlockChain, query GetReceiptsRequest, break } results := chain.GetReceiptsRLP(hash) + // If we don't have this block's receipts or body, stop serving. if results == nil { - continue // Can't retrieve the receipts, so we just skip this block. + break } body := chain.GetBodyRLP(hash) if body == nil { - continue // The block body is missing, we also have to skip. + break } q := receiptQueryParams{sizeLimit: uint64(maxPacketSize - bytes)} if i == 0 { @@ -326,7 +329,7 @@ func serviceGetReceiptsQuery70(chain *core.BlockChain, query GetReceiptsRequest, results, incomplete, err := blockReceiptsToNetwork(results, body, q) if err != nil { log.Error("Error in block receipts conversion", "hash", hash, "err", err) - continue + break } if results == nil { // This case triggers when the first receipt of the block receipts list doesn't From c5598fe9588be9d19ab210ebf53e760c7e195d95 Mon Sep 17 00:00:00 2001 From: Roshan <48975233+pythonberg1997@users.noreply.github.com> Date: Thu, 7 May 2026 16:44:26 +0800 Subject: [PATCH 57/80] core/txpool: change lock in Pending method of legacy pool to read lock (#32924) This PR makes a small update to the `Pending()` method in the legacy pool. By changing the lock from exclusive to read-only, it aims to improve concurrency performance. --------- Co-authored-by: rjl493456442 --- core/txpool/legacypool/legacypool.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/txpool/legacypool/legacypool.go b/core/txpool/legacypool/legacypool.go index 00630de04c..3d66803fd7 100644 --- a/core/txpool/legacypool/legacypool.go +++ b/core/txpool/legacypool/legacypool.go @@ -467,8 +467,8 @@ func (pool *LegacyPool) stats() (int, int) { // Content retrieves the data content of the transaction pool, returning all the // pending as well as queued transactions, grouped by account and sorted by nonce. func (pool *LegacyPool) Content() (map[common.Address][]*types.Transaction, map[common.Address][]*types.Transaction) { - pool.mu.Lock() - defer pool.mu.Unlock() + pool.mu.RLock() + defer pool.mu.RUnlock() pending := make(map[common.Address][]*types.Transaction, len(pool.pending)) for addr, list := range pool.pending { @@ -503,8 +503,8 @@ func (pool *LegacyPool) Pending(filter txpool.PendingFilter) (map[common.Address if filter.BlobTxs { return nil, 0 } - pool.mu.Lock() - defer pool.mu.Unlock() + pool.mu.RLock() + defer pool.mu.RUnlock() var count int pending := make(map[common.Address][]*txpool.LazyTransaction, len(pool.pending)) From f7b7d4c7e5cd9cd3b596c7a7430942cea8f5a755 Mon Sep 17 00:00:00 2001 From: cui Date: Thu, 7 May 2026 20:03:32 +0800 Subject: [PATCH 58/80] eth/tracers/logger: fix exclude address list (#34887) Fixes the exclusion list of the accessListTracer. --------- Co-authored-by: Sina M <1591639+s1na@users.noreply.github.com> --- eth/tracers/logger/access_list_tracer.go | 5 ++- eth/tracers/logger/access_list_tracer_test.go | 39 +++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 eth/tracers/logger/access_list_tracer_test.go diff --git a/eth/tracers/logger/access_list_tracer.go b/eth/tracers/logger/access_list_tracer.go index 749aade61b..31c3ebde93 100644 --- a/eth/tracers/logger/access_list_tracer.go +++ b/eth/tracers/logger/access_list_tracer.go @@ -112,9 +112,10 @@ type AccessListTracer struct { func NewAccessListTracer(acl types.AccessList, addressesToExclude map[common.Address]struct{}) *AccessListTracer { list := newAccessList() for _, al := range acl { - if _, ok := addressesToExclude[al.Address]; !ok { - list.addAddress(al.Address) + if _, ok := addressesToExclude[al.Address]; ok { + continue } + list.addAddress(al.Address) for _, slot := range al.StorageKeys { list.addSlot(al.Address, slot) } diff --git a/eth/tracers/logger/access_list_tracer_test.go b/eth/tracers/logger/access_list_tracer_test.go new file mode 100644 index 0000000000..04b2b4b31b --- /dev/null +++ b/eth/tracers/logger/access_list_tracer_test.go @@ -0,0 +1,39 @@ +// Copyright 2026 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 . + +package logger + +import ( + "testing" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" +) + +func TestNewAccessListTracerExcludedAddress(t *testing.T) { + excluded := common.HexToAddress("0x2222222222222222222222222222222222222222") + slot := common.HexToHash("0x01") + prelude := types.AccessList{{ + Address: excluded, + StorageKeys: []common.Hash{slot}, + }} + excl := map[common.Address]struct{}{excluded: {}} + tracer := NewAccessListTracer(prelude, excl) + got := tracer.AccessList() + if len(got) != 0 { + t.Fatalf("excluded prelude address must not contribute tuples, got %+v", got) + } +} From e1e3eaa38140c2ba1ed1eefafc126ae08d352ce9 Mon Sep 17 00:00:00 2001 From: cui Date: Thu, 7 May 2026 21:18:04 +0800 Subject: [PATCH 59/80] p2p/discover: copy buffer before sending read errors to unhandled (#34888) This fixes an issue where packets send to the `Unhandled` channel configured on discv4 could be corrupted when the packet buffer gets reused. --------- Co-authored-by: Felix Lange --- p2p/discover/v4_udp.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/p2p/discover/v4_udp.go b/p2p/discover/v4_udp.go index b06db4bdb2..ae8cbec3e2 100644 --- a/p2p/discover/v4_udp.go +++ b/p2p/discover/v4_udp.go @@ -555,8 +555,9 @@ func (t *UDPv4) readLoop(unhandled chan<- ReadPacket) { if err := t.handlePacket(from, buf[:nbytes]); err != nil && unhandled == nil { t.log.Debug("Bad discv4 packet", "addr", from, "err", err) } else if err != nil && unhandled != nil { + p := ReadPacket{bytes.Clone(buf[:nbytes]), from} select { - case unhandled <- ReadPacket{buf[:nbytes], from}: + case unhandled <- p: default: } } From e71098ba4e0814f2ced7165d518f63b281ad0697 Mon Sep 17 00:00:00 2001 From: cui Date: Fri, 8 May 2026 06:40:26 +0800 Subject: [PATCH 60/80] core/state: fix StateDB Reader Error Discard After Commit (#34899) --- core/state/statedb.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/state/statedb.go b/core/state/statedb.go index 1858f4758d..e6d8b5bffc 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -1355,7 +1355,7 @@ func (s *StateDB) commitAndFlush(block uint64, deleteEmptyObjects bool, noStorag // The reader update must be performed as the final step, otherwise, // the new state would not be visible before db.commit. - s.reader, _ = s.db.Reader(s.originalRoot) + s.reader, err = s.db.Reader(s.originalRoot) return ret, err } From 1abbae239d9a9f5797a5967350023c0f6b6aabb9 Mon Sep 17 00:00:00 2001 From: Delweng Date: Fri, 8 May 2026 10:12:46 +0800 Subject: [PATCH 61/80] eth,node: replace the deprecated TypeMux with Feed (#32585) replace the not used event.Typemux to event.Feed --------- Co-authored-by: Felix Lange --- cmd/geth/config.go | 10 ++- cmd/geth/main.go | 27 -------- cmd/utils/flags.go | 8 +-- eth/backend.go | 7 +- eth/downloader/api.go | 22 +++---- eth/downloader/downloader.go | 28 +++++--- eth/downloader/downloader_test.go | 3 +- eth/downloader/events.go | 24 +++++-- eth/handler.go | 35 ++++------ eth/syncer/syncer.go | 106 +++++++++++++++++++----------- node/node.go | 9 --- 11 files changed, 139 insertions(+), 140 deletions(-) diff --git a/cmd/geth/config.go b/cmd/geth/config.go index 8e2db32d76..40458186f4 100644 --- a/cmd/geth/config.go +++ b/cmd/geth/config.go @@ -38,6 +38,7 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/eth/catalyst" "github.com/ethereum/go-ethereum/eth/ethconfig" + "github.com/ethereum/go-ethereum/eth/syncer" "github.com/ethereum/go-ethereum/internal/flags" "github.com/ethereum/go-ethereum/internal/telemetry/tracesetup" "github.com/ethereum/go-ethereum/internal/version" @@ -276,16 +277,19 @@ func makeFullNode(ctx *cli.Context) *node.Node { if cfg.Ethstats.URL != "" { utils.RegisterEthStatsService(stack, backend, cfg.Ethstats.URL) } + // Configure synchronization override service - var synctarget common.Hash + syncConfig := syncer.Config{ + ExitWhenSynced: ctx.Bool(utils.ExitWhenSyncedFlag.Name), + } if ctx.IsSet(utils.SyncTargetFlag.Name) { target := ctx.String(utils.SyncTargetFlag.Name) if !common.IsHexHash(target) { utils.Fatalf("sync target hash is not a valid hex hash: %s", target) } - synctarget = common.HexToHash(target) + syncConfig.TargetBlock = common.HexToHash(target) } - utils.RegisterSyncOverrideService(stack, eth, synctarget, ctx.Bool(utils.ExitWhenSyncedFlag.Name)) + utils.RegisterSyncOverrideService(stack, eth, syncConfig) if ctx.IsSet(utils.DeveloperFlag.Name) { // Start dev mode. diff --git a/cmd/geth/main.go b/cmd/geth/main.go index c8d7abc65b..850e26d161 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -22,13 +22,10 @@ import ( "os" "slices" "sort" - "time" "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/cmd/utils" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/console/prompt" - "github.com/ethereum/go-ethereum/eth/downloader" "github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/internal/debug" "github.com/ethereum/go-ethereum/internal/flags" @@ -387,28 +384,4 @@ func startNode(ctx *cli.Context, stack *node.Node, isConsole bool) { } } }() - - // Spawn a standalone goroutine for status synchronization monitoring, - // close the node when synchronization is complete if user required. - if ctx.Bool(utils.ExitWhenSyncedFlag.Name) { - go func() { - sub := stack.EventMux().Subscribe(downloader.DoneEvent{}) - defer sub.Unsubscribe() - for { - event := <-sub.Chan() - if event == nil { - continue - } - done, ok := event.Data.(downloader.DoneEvent) - if !ok { - continue - } - if timestamp := time.Unix(int64(done.Latest.Time), 0); time.Since(timestamp) < 10*time.Minute { - log.Info("Synchronisation completed", "latestnum", done.Latest.Number, "latesthash", done.Latest.Hash(), - "age", common.PrettyAge(timestamp)) - stack.Close() - } - } - }() - } } diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index cc4c3bff5c..ea0f6f5ee4 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -2237,13 +2237,13 @@ func RegisterFilterAPI(stack *node.Node, backend ethapi.Backend, ethcfg *ethconf } // RegisterSyncOverrideService adds the synchronization override service into node. -func RegisterSyncOverrideService(stack *node.Node, eth *eth.Ethereum, target common.Hash, exitWhenSynced bool) { - if target != (common.Hash{}) { - log.Info("Registered sync override service", "hash", target, "exitWhenSynced", exitWhenSynced) +func RegisterSyncOverrideService(stack *node.Node, eth *eth.Ethereum, config syncer.Config) { + if config.TargetBlock != (common.Hash{}) { + log.Info("Registered sync override service", "hash", config.TargetBlock, "exitWhenSynced", config.ExitWhenSynced) } else { log.Info("Registered sync override service") } - syncer.Register(stack, eth, target, exitWhenSynced) + syncer.Register(stack, eth, config) } // SetupMetrics configures the metrics system. diff --git a/eth/backend.go b/eth/backend.go index 6cfd1f6fa0..af8b04bda6 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -49,7 +49,6 @@ import ( "github.com/ethereum/go-ethereum/eth/protocols/snap" "github.com/ethereum/go-ethereum/eth/tracers" "github.com/ethereum/go-ethereum/ethdb" - "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/internal/ethapi" "github.com/ethereum/go-ethereum/internal/shutdowncheck" "github.com/ethereum/go-ethereum/internal/version" @@ -105,7 +104,6 @@ type Ethereum struct { // DB interfaces chainDb ethdb.Database // Block chain database - eventMux *event.TypeMux engine consensus.Engine accountManager *accounts.Manager @@ -194,7 +192,6 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { eth := &Ethereum{ config: config, chainDb: chainDb, - eventMux: stack.EventMux(), accountManager: stack.AccountManager(), engine: engine, networkID: networkID, @@ -344,7 +341,6 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { Network: networkID, Sync: config.SyncMode, BloomCache: uint64(cacheLimit), - EventMux: eth.eventMux, RequiredBlocks: config.RequiredBlocks, }); err != nil { return nil, err @@ -405,7 +401,7 @@ func (s *Ethereum) APIs() []rpc.API { Service: NewMinerAPI(s), }, { Namespace: "eth", - Service: downloader.NewDownloaderAPI(s.handler.downloader, s.blockchain, s.eventMux), + Service: downloader.NewDownloaderAPI(s.handler.downloader, s.blockchain), }, { Namespace: "admin", Service: NewAdminAPI(s), @@ -600,7 +596,6 @@ func (s *Ethereum) Stop() error { s.shutdownTracker.Stop() s.chainDb.Close() - s.eventMux.Stop() return nil } diff --git a/eth/downloader/api.go b/eth/downloader/api.go index 1fea35775e..6033e44474 100644 --- a/eth/downloader/api.go +++ b/eth/downloader/api.go @@ -23,7 +23,6 @@ import ( "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/core" - "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/rpc" ) @@ -33,20 +32,18 @@ import ( type DownloaderAPI struct { d *Downloader chain *core.BlockChain - mux *event.TypeMux installSyncSubscription chan chan interface{} uninstallSyncSubscription chan *uninstallSyncSubscriptionRequest } // NewDownloaderAPI creates a new DownloaderAPI. The API has an internal event loop that -// listens for events from the downloader through the global event mux. In case it receives one of +// listens for events from the downloader through the event feed. In case it receives one of // these events it broadcasts it to all syncing subscriptions that are installed through the // installSyncSubscription channel. -func NewDownloaderAPI(d *Downloader, chain *core.BlockChain, m *event.TypeMux) *DownloaderAPI { +func NewDownloaderAPI(d *Downloader, chain *core.BlockChain) *DownloaderAPI { api := &DownloaderAPI{ d: d, chain: chain, - mux: m, installSyncSubscription: make(chan chan interface{}), uninstallSyncSubscription: make(chan *uninstallSyncSubscriptionRequest), } @@ -66,7 +63,8 @@ func NewDownloaderAPI(d *Downloader, chain *core.BlockChain, m *event.TypeMux) * // receive is {false}. func (api *DownloaderAPI) eventLoop() { var ( - sub = api.mux.Subscribe(StartEvent{}) + events = make(chan SyncEvent, 16) + sub = api.d.SubscribeSyncEvents(events) syncSubscriptions = make(map[chan interface{}]struct{}) checkInterval = time.Second * 60 checkTimer = time.NewTimer(checkInterval) @@ -90,6 +88,7 @@ func (api *DownloaderAPI) eventLoop() { } ) defer checkTimer.Stop() + defer sub.Unsubscribe() for { select { @@ -101,14 +100,13 @@ func (api *DownloaderAPI) eventLoop() { case u := <-api.uninstallSyncSubscription: delete(syncSubscriptions, u.c) close(u.uninstalled) - case event := <-sub.Chan(): - if event == nil { - return - } - switch event.Data.(type) { - case StartEvent: + case ev := <-events: + if ev.Type == SyncStarted { started = true } + case <-sub.Err(): + // The downloader is terminated or other internal error occurs + return case <-checkTimer.C: if !started { checkTimer.Reset(checkInterval) diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index 1de0933842..4a575d6856 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -97,9 +97,12 @@ type headerTask struct { } type Downloader struct { - mode atomic.Uint32 // Synchronisation mode defining the strategy used (per sync cycle), use d.getMode() to get the SyncMode - moder *syncModer // Sync mode management, deliver the appropriate sync mode choice for each cycle - mux *event.TypeMux // Event multiplexer to announce sync operation events + mode atomic.Uint32 // Synchronisation mode defining the strategy used (per sync cycle), use d.getMode() to get the SyncMode + moder *syncModer // Sync mode management, deliver the appropriate sync mode choice for each cycle + + // Event feed for downloader events + feed event.FeedOf[SyncEvent] + scope event.SubscriptionScope queue *queue // Scheduler for selecting the hashes to download peers *peerSet // Set of active peers from which download can proceed @@ -229,12 +232,11 @@ type BlockChain interface { } // New creates a new downloader to fetch hashes and blocks from remote peers. -func New(stateDb ethdb.Database, mode ethconfig.SyncMode, mux *event.TypeMux, chain BlockChain, dropPeer peerDropFn, success func()) *Downloader { +func New(stateDb ethdb.Database, mode ethconfig.SyncMode, chain BlockChain, dropPeer peerDropFn, success func()) *Downloader { cutoffNumber, cutoffHash := chain.HistoryPruningCutoff() dl := &Downloader{ stateDB: stateDb, moder: newSyncModer(mode, chain, stateDb), - mux: mux, queue: newQueue(blockCacheMaxItems, blockCacheInitialItems), peers: newPeerSet(), blockchain: chain, @@ -427,20 +429,25 @@ func (d *Downloader) ConfigSyncMode() SyncMode { return d.moder.get(false) } +// SubscribeSyncEvents creates a subscription for downloader sync events +func (d *Downloader) SubscribeSyncEvents(ch chan<- SyncEvent) event.Subscription { + return d.scope.Track(d.feed.Subscribe(ch)) +} + // syncToHead starts a block synchronization based on the hash chain from // the specified head hash. func (d *Downloader) syncToHead() (err error) { - d.mux.Post(StartEvent{}) + mode := d.getMode() + d.feed.Send(SyncEvent{Type: SyncStarted, Mode: mode}) defer func() { // reset on error if err != nil { - d.mux.Post(FailedEvent{err}) + d.feed.Send(SyncEvent{Type: SyncFailed, Mode: mode, Err: err}) } else { latest := d.blockchain.CurrentHeader() - d.mux.Post(DoneEvent{latest}) + d.feed.Send(SyncEvent{Type: SyncCompleted, Mode: mode, Latest: latest}) } }() - mode := d.getMode() log.Debug("Backfilling with the network", "mode", mode) defer func(start time.Time) { @@ -662,6 +669,9 @@ func (d *Downloader) Cancel() { // Terminate interrupts the downloader, canceling all pending operations. // The downloader cannot be reused after calling Terminate. func (d *Downloader) Terminate() { + // Unsubscribe all subscriptions registered from downloader + d.scope.Close() + // Close the termination channel (make sure double close is allowed) d.quitLock.Lock() select { diff --git a/eth/downloader/downloader_test.go b/eth/downloader/downloader_test.go index 6d5d159631..e6c477cd33 100644 --- a/eth/downloader/downloader_test.go +++ b/eth/downloader/downloader_test.go @@ -32,7 +32,6 @@ import ( "github.com/ethereum/go-ethereum/eth/ethconfig" "github.com/ethereum/go-ethereum/eth/protocols/eth" "github.com/ethereum/go-ethereum/eth/protocols/snap" - "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" @@ -75,7 +74,7 @@ func newTesterWithNotification(t *testing.T, mode ethconfig.SyncMode, success fu chain: chain, peers: make(map[string]*downloadTesterPeer), } - tester.downloader = New(db, mode, new(event.TypeMux), tester.chain, tester.dropPeer, success) + tester.downloader = New(db, mode, tester.chain, tester.dropPeer, success) return tester } diff --git a/eth/downloader/events.go b/eth/downloader/events.go index 25255a3a72..0fb380a857 100644 --- a/eth/downloader/events.go +++ b/eth/downloader/events.go @@ -16,10 +16,24 @@ package downloader -import "github.com/ethereum/go-ethereum/core/types" +import ( + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/eth/ethconfig" +) -type DoneEvent struct { - Latest *types.Header +// SyncEventType represents the type of sync event +type SyncEventType int + +const ( + SyncStarted SyncEventType = iota + SyncFailed + SyncCompleted +) + +// SyncEvent represents a downloader synchronization event +type SyncEvent struct { + Type SyncEventType + Mode ethconfig.SyncMode + Err error // Set when Type is SyncFailed + Latest *types.Header // Set when Type is SyncCompleted } -type StartEvent struct{} -type FailedEvent struct{ Err error } diff --git a/eth/handler.go b/eth/handler.go index 27b5e60697..76df635fb0 100644 --- a/eth/handler.go +++ b/eth/handler.go @@ -107,7 +107,6 @@ type handlerConfig struct { Network uint64 // Network identifier to advertise Sync ethconfig.SyncMode // Whether to snap or full sync BloomCache uint64 // Megabytes to alloc for snap sync bloom - EventMux *event.TypeMux // Legacy event mux, deprecate for `feed` RequiredBlocks map[uint64]common.Hash // Hard coded map of required block hashes for sync challenges } @@ -126,7 +125,6 @@ type handler struct { peers *peerSet txBroadcastKey [16]byte - eventMux *event.TypeMux txsCh chan core.NewTxsEvent txsSub event.Subscription blockRange *blockRangeState @@ -144,14 +142,9 @@ type handler struct { // newHandler returns a handler for all Ethereum chain management protocol. func newHandler(config *handlerConfig) (*handler, error) { - // Create the protocol manager with the base fields - if config.EventMux == nil { - config.EventMux = new(event.TypeMux) // Nicety initialization for tests - } h := &handler{ nodeID: config.NodeID, networkID: config.Network, - eventMux: config.EventMux, database: config.Database, txpool: config.TxPool, chain: config.Chain, @@ -163,7 +156,7 @@ func newHandler(config *handlerConfig) (*handler, error) { handlerStartCh: make(chan struct{}), } // Construct the downloader (long sync) - h.downloader = downloader.New(config.Database, config.Sync, h.eventMux, h.chain, h.removePeer, h.enableSyncedFeatures) + h.downloader = downloader.New(config.Database, config.Sync, h.chain, h.removePeer, h.enableSyncedFeatures) // If snap sync is requested but snapshots are disabled, fail loudly if h.downloader.ConfigSyncMode() == ethconfig.SnapSync && (config.Chain.Snapshots() == nil && config.Chain.TrieDB().Scheme() == rawdb.HashScheme) { @@ -420,7 +413,7 @@ func (h *handler) Start(maxPeers int) { // broadcast block range h.wg.Add(1) - h.blockRange = newBlockRangeState(h.chain, h.eventMux) + h.blockRange = newBlockRangeState(h.chain, h.downloader) go h.blockRangeLoop(h.blockRange) // start sync handlers @@ -536,16 +529,19 @@ type blockRangeState struct { next atomic.Pointer[eth.BlockRangeUpdatePacket] headCh chan core.ChainHeadEvent headSub event.Subscription - syncSub *event.TypeMuxSubscription + syncCh chan downloader.SyncEvent + syncSub event.Subscription } -func newBlockRangeState(chain *core.BlockChain, typeMux *event.TypeMux) *blockRangeState { +func newBlockRangeState(chain *core.BlockChain, dl *downloader.Downloader) *blockRangeState { headCh := make(chan core.ChainHeadEvent, chainHeadChanSize) headSub := chain.SubscribeChainHeadEvent(headCh) - syncSub := typeMux.Subscribe(downloader.StartEvent{}, downloader.DoneEvent{}, downloader.FailedEvent{}) + syncCh := make(chan downloader.SyncEvent, 16) + syncSub := dl.SubscribeSyncEvents(syncCh) st := &blockRangeState{ headCh: headCh, headSub: headSub, + syncCh: syncCh, syncSub: syncSub, } st.update(chain, chain.CurrentBlock()) @@ -561,11 +557,8 @@ func (h *handler) blockRangeLoop(st *blockRangeState) { for { select { - case ev := <-st.syncSub.Chan(): - if ev == nil { - continue - } - if _, ok := ev.Data.(downloader.StartEvent); ok && h.downloader.ConfigSyncMode() == ethconfig.SnapSync { + case ev := <-st.syncCh: + if ev.Type == downloader.SyncStarted && ev.Mode == ethconfig.SnapSync { h.blockRangeWhileSnapSyncing(st) } case <-st.headCh: @@ -593,12 +586,8 @@ func (h *handler) blockRangeWhileSnapSyncing(st *blockRangeState) { h.broadcastBlockRange(st) } // back to processing head block updates when sync is done - case ev := <-st.syncSub.Chan(): - if ev == nil { - continue - } - switch ev.Data.(type) { - case downloader.FailedEvent, downloader.DoneEvent: + case ev := <-st.syncCh: + if ev.Type == downloader.SyncFailed || ev.Type == downloader.SyncCompleted { return } // ignore head updates, but exit when the subscription ends diff --git a/eth/syncer/syncer.go b/eth/syncer/syncer.go index c0d54b953b..b04d8f22e8 100644 --- a/eth/syncer/syncer.go +++ b/eth/syncer/syncer.go @@ -26,6 +26,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/eth" + "github.com/ethereum/go-ethereum/eth/downloader" "github.com/ethereum/go-ethereum/eth/ethconfig" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/node" @@ -37,32 +38,40 @@ type syncReq struct { errc chan error } +type Config struct { + TargetBlock common.Hash // if set, sync is triggered at startup + ExitWhenSynced bool // if true, the node shuts down after sync has finished +} + // Syncer is an auxiliary service that allows Geth to perform full sync // alone without consensus-layer attached. Users must specify a valid block hash // as the sync target. // +// Additionally, the syncer can be used to monitor state synchronization. +// It will exit once the specified target has been reached or when the +// most recent chain head is caught up. +// // This tool can be applied to different networks, no matter it's pre-merge or // post-merge, but only for full-sync. type Syncer struct { - stack *node.Node - backend *eth.Ethereum - target common.Hash - request chan *syncReq - closed chan struct{} - wg sync.WaitGroup - exitWhenSynced bool + stack *node.Node + backend *eth.Ethereum + request chan *syncReq + closed chan struct{} + wg sync.WaitGroup + + config Config } // Register registers the synchronization override service into the node // stack for launching and stopping the service controlled by node. -func Register(stack *node.Node, backend *eth.Ethereum, target common.Hash, exitWhenSynced bool) (*Syncer, error) { +func Register(stack *node.Node, backend *eth.Ethereum, cfg Config) (*Syncer, error) { s := &Syncer{ - stack: stack, - backend: backend, - target: target, - request: make(chan *syncReq), - closed: make(chan struct{}), - exitWhenSynced: exitWhenSynced, + stack: stack, + backend: backend, + request: make(chan *syncReq), + closed: make(chan struct{}), + config: cfg, } stack.RegisterAPIs(s.APIs()) stack.RegisterLifecycle(s) @@ -88,9 +97,11 @@ func (s *Syncer) run() { var ( target *types.Header - ticker = time.NewTicker(time.Second * 5) + syncCh = make(chan downloader.SyncEvent, 10) ) - defer ticker.Stop() + sub := s.backend.Downloader().SubscribeSyncEvents(syncCh) + defer sub.Unsubscribe() + for { select { case req := <-s.request: @@ -137,35 +148,50 @@ func (s *Syncer) run() { } } - case <-ticker.C: - if target == nil { + case ev := <-syncCh: + if ev.Type == downloader.SyncStarted { + log.Debug("Synchronization started") continue } - - // Terminate the node if the target has been reached - if s.exitWhenSynced { - if block := s.backend.BlockChain().GetBlockByHash(target.Hash()); block != nil { - log.Info("Sync target reached", "number", block.NumberU64(), "hash", block.Hash()) - go s.stack.Close() // async since we need to close ourselves - return - } + if ev.Type == downloader.SyncFailed { + log.Debug("Synchronization failed", "err", ev.Err) + continue } - // Set the finalized and safe markers relative to the current head. - // The finalized marker is set two epochs behind the target, - // and the safe marker is set one epoch behind the target. head := s.backend.BlockChain().CurrentHeader() - if head == nil { - continue - } - if header := s.backend.BlockChain().GetHeaderByNumber(head.Number.Uint64() - params.EpochLength*2); header != nil { - if final := s.backend.BlockChain().CurrentFinalBlock(); final == nil || final.Number.Cmp(header.Number) < 0 { - s.backend.BlockChain().SetFinalized(header) + if head != nil { + // Set the finalized and safe markers relative to the current head. + // The finalized marker is set two epochs behind the target, + // and the safe marker is set one epoch behind the target. + if header := s.backend.BlockChain().GetHeaderByNumber(head.Number.Uint64() - params.EpochLength*2); header != nil { + if final := s.backend.BlockChain().CurrentFinalBlock(); final == nil || final.Number.Cmp(header.Number) < 0 { + s.backend.BlockChain().SetFinalized(header) + } + } + if header := s.backend.BlockChain().GetHeaderByNumber(head.Number.Uint64() - params.EpochLength); header != nil { + if safe := s.backend.BlockChain().CurrentSafeBlock(); safe == nil || safe.Number.Cmp(header.Number) < 0 { + s.backend.BlockChain().SetSafe(header) + } } } - if header := s.backend.BlockChain().GetHeaderByNumber(head.Number.Uint64() - params.EpochLength); header != nil { - if safe := s.backend.BlockChain().CurrentSafeBlock(); safe == nil || safe.Number.Cmp(header.Number) < 0 { - s.backend.BlockChain().SetSafe(header) + + // Terminate the node if the target has been reached + if s.config.ExitWhenSynced { + var synced bool + var block *types.Header + if target != nil { + tb := s.backend.BlockChain().GetBlockByHash(target.Hash()) + synced = tb != nil + block = tb.Header() + } else { + timestamp := time.Unix(int64(ev.Latest.Time), 0) + synced = time.Since(timestamp) < 10*time.Minute + block = ev.Latest + } + + if synced { + log.Info("Sync target reached", "number", block.Number.Uint64(), "hash", block.Hash()) + go s.stack.Close() // async since we need to close ourselves } } @@ -179,10 +205,10 @@ func (s *Syncer) run() { func (s *Syncer) Start() error { s.wg.Add(1) go s.run() - if s.target == (common.Hash{}) { + if s.config.TargetBlock == (common.Hash{}) { return nil } - return s.Sync(s.target) + return s.Sync(s.config.TargetBlock) } // Stop terminates the synchronization service and stop all background activities. diff --git a/node/node.go b/node/node.go index 01318881d4..56ecd7d522 100644 --- a/node/node.go +++ b/node/node.go @@ -35,7 +35,6 @@ import ( "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb/memorydb" - "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/rpc" @@ -44,7 +43,6 @@ import ( // Node is a container on which services can be registered. type Node struct { - eventmux *event.TypeMux config *Config accman *accounts.Manager log log.Logger @@ -108,7 +106,6 @@ func New(conf *Config) (*Node, error) { node := &Node{ config: conf, inprocHandler: server, - eventmux: new(event.TypeMux), log: conf.Logger, stop: make(chan struct{}), server: &p2p.Server{Config: conf.P2P}, @@ -692,12 +689,6 @@ func (n *Node) WSAuthEndpoint() string { return "ws://" + n.wsAuth.listenAddr() + n.wsAuth.wsConfig.prefix } -// EventMux retrieves the event multiplexer used by all the network services in -// the current protocol stack. -func (n *Node) EventMux() *event.TypeMux { - return n.eventmux -} - // OpenDatabaseWithOptions opens an existing database with the given name (or creates one if no // previous can be found) from within the node's instance directory. If the node has no // data directory, an in-memory database is returned. From 281dc4c2091400ea1388b030300e5adc7672ffa0 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Fri, 8 May 2026 11:33:19 +0200 Subject: [PATCH 62/80] p2p/discover: decouple nodeFeed from Table mutex in waitForNodes (#34898) Fixes #34881 This fixes a hang in `Table.waitForNodes`. It is a replacement for PRs #34890, #33665 which tried to fix the same issue in a different way. - #34890 doesn't really fix the issue, just makes it less likely - #33665 tries to fix it by moving the feed send outside of the lock I created this PR because I want to keep the synchronous node feed sending in `Table.nodeAdded`. --------- Co-authored-by: Csaba Kiraly --- p2p/discover/table.go | 55 +++++++++++++++++++++++++++++--------- p2p/discover/table_test.go | 40 +++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 12 deletions(-) diff --git a/p2p/discover/table.go b/p2p/discover/table.go index 721cd7b589..016a2d1af3 100644 --- a/p2p/discover/table.go +++ b/p2p/discover/table.go @@ -753,6 +753,41 @@ func (tab *Table) deleteNode(n *enode.Node) { // waitForNodes blocks until the table contains at least n nodes. func (tab *Table) waitForNodes(ctx context.Context, n int) error { + // Wrap ctx so the forwarder goroutine exits when waitForNodes returns, + // regardless of whether the caller's ctx is canceled. + ctx, cancel := context.WithCancel(ctx) + defer cancel() + + // Set up a notification channel that gets unblocked when there was any activity on + // the table. Ultimately this reads from the table's nodeFeed, but can't use the feed + // directly on the same goroutine that takes Table.mutex, it would deadlock. + var notify chan struct{} + var notifyErr error + initsub := func() event.Subscription { + notify = make(chan struct{}, 1) + newnode := make(chan *enode.Node, 1) + sub := tab.nodeFeed.Subscribe(newnode) + go func() { + defer close(notify) + for { + select { + case <-newnode: + select { + case notify <- struct{}{}: + default: + } + case <-ctx.Done(): + notifyErr = ctx.Err() + return + case <-tab.closeReq: + notifyErr = errClosed + return + } + } + }() + return sub + } + getlength := func() (count int) { for _, b := range &tab.buckets { count += len(b.entries) @@ -760,28 +795,24 @@ func (tab *Table) waitForNodes(ctx context.Context, n int) error { return count } - var ch chan *enode.Node for { tab.mutex.Lock() if getlength() >= n { tab.mutex.Unlock() return nil } - if ch == nil { - // Init subscription. - ch = make(chan *enode.Node) - sub := tab.nodeFeed.Subscribe(ch) + if notify == nil { + // Lazily init the subscription. Do this while holding the + // lock so we don't miss any events that change the node count. + sub := initsub() defer sub.Unsubscribe() } tab.mutex.Unlock() - // Wait for a node add event. - select { - case <-ch: - case <-ctx.Done(): - return ctx.Err() - case <-tab.closeReq: - return errClosed + // Wait for table event. + if _, ok := <-notify; !ok { + break } } + return notifyErr } diff --git a/p2p/discover/table_test.go b/p2p/discover/table_test.go index c3b71ea5a6..a16b4d9cab 100644 --- a/p2p/discover/table_test.go +++ b/p2p/discover/table_test.go @@ -17,6 +17,7 @@ package discover import ( + "context" "crypto/ecdsa" "fmt" "math/rand" @@ -550,6 +551,45 @@ func TestSetFallbackNodes_DNSHostname(t *testing.T) { t.Logf("resolved localhost to %v", resolved.IPAddr()) } +// This test checks that waitForNodes does not block addFoundNode. +// See https://github.com/ethereum/go-ethereum/issues/34881. +func TestTable_waitForNodesLocking(t *testing.T) { + transport := newPingRecorder() + tab, db := newTestTable(transport, Config{}) + defer db.Close() + defer tab.close() + <-tab.initDone + + // waitForNodes will never reach this count, so it stays subscribed + // to nodeFeed and looping for the duration of the test. + waitCtx, cancelWait := context.WithCancel(context.Background()) + defer cancelWait() + waitDone := make(chan struct{}) + go func() { + defer close(waitDone) + tab.waitForNodes(waitCtx, 1<<20) + }() + + // Call addFoundNode in loop to send to the feed. + addDone := make(chan struct{}) + go func() { + defer close(addDone) + for i := range 10000 { + d := 240 + (i % 17) + n := nodeAtDistance(tab.self().ID(), d, intIP(i)) + tab.addFoundNode(n, true) + } + }() + + select { + case <-addDone: + cancelWait() + <-waitDone + case <-time.After(10 * time.Second): + t.Fatal("deadlock detected: add loop did not finish within 10s") + } +} + func newkey() *ecdsa.PrivateKey { key, err := crypto.GenerateKey() if err != nil { From 0ad890e3af967f1d2d3bec3b972f76fc4c208929 Mon Sep 17 00:00:00 2001 From: Miki Noir Date: Fri, 8 May 2026 11:43:31 +0100 Subject: [PATCH 63/80] core/txpool/blobpool: continue on cell proof error in `GetBlobs` (#34891) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `GetBlobs` returned early when `CellProofsAt` reported corrupted/out-of-bounds proofs, dropping every blob already collected and aborting the remaining hashes — a single bad sidecar killed the whole Engine API batch for consensus clients. Replaced the `return nil, nil, nil, err` with `log.Error + continue` so the slot stays `nil` per the sparse-array contract, matching the store/RLP/nil-sidecar branches a few lines above. --- core/txpool/blobpool/blobpool.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/txpool/blobpool/blobpool.go b/core/txpool/blobpool/blobpool.go index efa41a0649..f2e0d5f9d2 100644 --- a/core/txpool/blobpool/blobpool.go +++ b/core/txpool/blobpool/blobpool.go @@ -1517,7 +1517,8 @@ func (p *BlobPool) GetBlobs(vhashes []common.Hash, version byte) ([]*kzg4844.Blo case types.BlobSidecarVersion1: cellProofs, err := sidecar.CellProofsAt(i) if err != nil { - return nil, nil, nil, err + log.Error("Failed to get cell proofs", "id", txID, "err", err) + continue } pf = cellProofs } From 592209c0ee99bd9c3046d27ecea5b970c64a8730 Mon Sep 17 00:00:00 2001 From: Sina M <1591639+s1na@users.noreply.github.com> Date: Fri, 8 May 2026 15:18:24 +0200 Subject: [PATCH 64/80] .gitea, build: cross-compile windows binaries (#34889) --- .gitea/workflows/release.yml | 35 ++++------ build/ci.go | 125 ++++++++++++++++++++++------------- internal/build/gotool.go | 13 +++- 3 files changed, 100 insertions(+), 73 deletions(-) diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/release.yml index efe76cf170..a3fa4a2ea7 100644 --- a/.gitea/workflows/release.yml +++ b/.gitea/workflows/release.yml @@ -145,7 +145,7 @@ jobs: windows: name: Windows Build - runs-on: "win-11" + runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -155,57 +155,46 @@ jobs: go-version: 1.24 cache: false - # Note: gcc.exe only works properly if the corresponding bin/ directory is - # contained in PATH. + - name: Install cross toolchain + run: | + apt-get update + apt-get -yq --no-install-suggests --no-install-recommends install \ + gcc-mingw-w64-x86-64 gcc-mingw-w64-i686 nsis - name: "Build (amd64)" - shell: cmd run: | - set PATH=%GETH_MINGW%\bin;%PATH% - go run build/ci.go install -dlgo -arch amd64 -cc %GETH_MINGW%\bin\gcc.exe - env: - GETH_MINGW: 'C:\msys64\mingw64' + go run build/ci.go install -dlgo -os windows -arch amd64 -cc x86_64-w64-mingw32-gcc - name: "Create/upload archive (amd64)" - shell: cmd run: | - go run build/ci.go archive -arch amd64 -type zip -signer WINDOWS_SIGNING_KEY -upload gethstore/builds + go run build/ci.go archive -os windows -arch amd64 -type zip -signer WINDOWS_SIGNING_KEY -upload gethstore/builds env: WINDOWS_SIGNING_KEY: ${{ secrets.WINDOWS_SIGNING_KEY }} AZURE_BLOBSTORE_TOKEN: ${{ secrets.AZURE_BLOBSTORE_TOKEN }} - name: "Create/upload NSIS installer (amd64)" - shell: cmd run: | - set "PATH=C:\Program Files (x86)\NSIS;%PATH%" go run build/ci.go nsis -arch amd64 -signer WINDOWS_SIGNING_KEY -upload gethstore/builds - del /Q build\bin\* + rm -f build/bin/* env: WINDOWS_SIGNING_KEY: ${{ secrets.WINDOWS_SIGNING_KEY }} AZURE_BLOBSTORE_TOKEN: ${{ secrets.AZURE_BLOBSTORE_TOKEN }} - name: "Build (386)" - shell: cmd run: | - set PATH=%GETH_MINGW%\bin;%PATH% - go run build/ci.go install -dlgo -arch 386 -cc %GETH_MINGW%\bin\gcc.exe - env: - GETH_MINGW: 'C:\msys64\mingw32' + go run build/ci.go install -dlgo -os windows -arch 386 -cc i686-w64-mingw32-gcc - name: "Create/upload archive (386)" - shell: cmd run: | - go run build/ci.go archive -arch 386 -type zip -signer WINDOWS_SIGNING_KEY -upload gethstore/builds + go run build/ci.go archive -os windows -arch 386 -type zip -signer WINDOWS_SIGNING_KEY -upload gethstore/builds env: WINDOWS_SIGNING_KEY: ${{ secrets.WINDOWS_SIGNING_KEY }} AZURE_BLOBSTORE_TOKEN: ${{ secrets.AZURE_BLOBSTORE_TOKEN }} - name: "Create/upload NSIS installer (386)" - shell: cmd run: | - set "PATH=C:\Program Files (x86)\NSIS;%PATH%" go run build/ci.go nsis -arch 386 -signer WINDOWS_SIGNING_KEY -upload gethstore/builds - del /Q build\bin\* + rm -f build/bin/* env: WINDOWS_SIGNING_KEY: ${{ secrets.WINDOWS_SIGNING_KEY }} AZURE_BLOBSTORE_TOKEN: ${{ secrets.AZURE_BLOBSTORE_TOKEN }} diff --git a/build/ci.go b/build/ci.go index 173288bcdc..173a3280ce 100644 --- a/build/ci.go +++ b/build/ci.go @@ -73,21 +73,9 @@ var ( "./cmd/keeper", } - // Files that end up in the geth*.zip archive. - gethArchiveFiles = []string{ - "COPYING", - executablePath("geth"), - } - - // Files that end up in the geth-alltools*.zip archive. - allToolsArchiveFiles = []string{ - "COPYING", - executablePath("abigen"), - executablePath("evm"), - executablePath("geth"), - executablePath("rlpdump"), - executablePath("clef"), - } + // Files that end up in the geth-alltools*.zip archive (and the NSIS installer + // dev-tools section). Order matches the historical layout produced by ci.go. + allToolsBinaries = []string{"abigen", "evm", "geth", "rlpdump", "clef"} // Keeper build targets with their configurations keeperTargets = []struct { @@ -180,13 +168,35 @@ var ( var GOBIN, _ = filepath.Abs(filepath.Join("build", "bin")) -func executablePath(name string) string { - if runtime.GOOS == "windows" { +// executablePath returns the path to a built binary in GOBIN, applying the +// platform-specific extension for the given target OS. +func executablePath(name, targetOS string) string { + if targetOS == "windows" { name += ".exe" } return filepath.Join(GOBIN, name) } +// gethArchiveFiles returns the file list for the geth-{platform}-{ver}.zip +// archive, with binary paths resolved for the target OS. +func gethArchiveFiles(targetOS string) []string { + return []string{ + "COPYING", + executablePath("geth", targetOS), + } +} + +// allToolsArchiveFiles returns the file list for the +// geth-alltools-{platform}-{ver}.zip archive, with binary paths resolved for +// the target OS. +func allToolsArchiveFiles(targetOS string) []string { + files := []string{"COPYING"} + for _, name := range allToolsBinaries { + files = append(files, executablePath(name, targetOS)) + } + return files +} + func main() { log.SetFlags(log.Lshortfile) @@ -233,6 +243,7 @@ func main() { func doInstall(cmdline []string) { var ( dlgo = flag.Bool("dlgo", false, "Download Go and build with it") + targetOS = flag.String("os", runtime.GOOS, "Target OS to cross build for") arch = flag.String("arch", "", "Architecture to cross build for") cc = flag.String("cc", "", "C compiler to cross build with") staticlink = flag.Bool("static", false, "Create statically-linked executable") @@ -241,7 +252,7 @@ func doInstall(cmdline []string) { env := build.Env() // Configure the toolchain. - tc := build.GoToolchain{GOARCH: *arch, CC: *cc} + tc := build.GoToolchain{GOOS: *targetOS, GOARCH: *arch, CC: *cc} if *dlgo { csdb := download.MustLoadChecksums("build/checksums.txt") tc.Root = build.DownloadGo(csdb) @@ -255,7 +266,7 @@ func doInstall(cmdline []string) { } // Configure the build. - gobuild := tc.Go("build", buildFlags(env, *staticlink, buildTags)...) + gobuild := tc.Go("build", buildFlags(env, *staticlink, buildTags, *targetOS)...) // Show packages during build. gobuild.Args = append(gobuild.Args, "-v") @@ -270,7 +281,7 @@ func doInstall(cmdline []string) { // Do the build! for _, pkg := range packages { args := slices.Clone(gobuild.Args) - args = append(args, "-o", executablePath(path.Base(pkg))) + args = append(args, "-o", executablePath(path.Base(pkg), *targetOS)) args = append(args, pkg) build.MustRun(&exec.Cmd{Path: gobuild.Path, Args: args, Env: gobuild.Env}) } @@ -297,7 +308,13 @@ func doInstallKeeper(cmdline []string) { tc.GOARCH = target.GOARCH tc.GOOS = target.GOOS tc.CC = target.CC - gobuild := tc.Go("build", buildFlags(env, true, []string{target.Tags})...) + // An empty GOOS means "build for the host OS"; thread that through to + // buildFlags so platform-specific linker flags are picked correctly. + targetOS := target.GOOS + if targetOS == "" { + targetOS = runtime.GOOS + } + gobuild := tc.Go("build", buildFlags(env, true, []string{target.Tags}, targetOS)...) gobuild.Dir = "./cmd/keeper" gobuild.Args = append(gobuild.Args, "-v") @@ -307,14 +324,15 @@ func doInstallKeeper(cmdline []string) { outputName := fmt.Sprintf("keeper-%s", target.Name) args := slices.Clone(gobuild.Args) - args = append(args, "-o", executablePath(outputName)) + args = append(args, "-o", executablePath(outputName, targetOS)) args = append(args, ".") build.MustRun(&exec.Cmd{Path: gobuild.Path, Args: args, Env: gobuild.Env, Dir: gobuild.Dir}) } } -// buildFlags returns the go tool flags for building. -func buildFlags(env build.Environment, staticLinking bool, buildTags []string) (flags []string) { +// buildFlags returns the go tool flags for building. targetOS is the OS we +// are producing binaries for. +func buildFlags(env build.Environment, staticLinking bool, buildTags []string, targetOS string) (flags []string) { var ld []string // See https://github.com/golang/go/issues/33772#issuecomment-528176001 // We need to set --buildid to the linker here, and also pass --build-id to the @@ -326,10 +344,10 @@ func buildFlags(env build.Environment, staticLinking bool, buildTags []string) ( } // Strip DWARF on darwin. This used to be required for certain things, // and there is no downside to this, so we just keep doing it. - if runtime.GOOS == "darwin" { + if targetOS == "darwin" { ld = append(ld, "-s") } - if runtime.GOOS == "linux" { + if targetOS == "linux" { // Enforce the stacksize to 8M, which is the case on most platforms apart from // alpine Linux. // See https://sourceware.org/binutils/docs-2.23.1/ld/Options.html#Options @@ -682,12 +700,13 @@ func downloadProtoc(cachedir string) string { // Release Packaging func doArchive(cmdline []string) { var ( - arch = flag.String("arch", runtime.GOARCH, "Architecture cross packaging") - atype = flag.String("type", "zip", "Type of archive to write (zip|tar)") - signer = flag.String("signer", "", `Environment variable holding the signing key (e.g. LINUX_SIGNING_KEY)`) - signify = flag.String("signify", "", `Environment variable holding the signify key (e.g. LINUX_SIGNIFY_KEY)`) - upload = flag.String("upload", "", `Destination to upload the archives (usually "gethstore/builds")`) - ext string + targetOS = flag.String("os", runtime.GOOS, "Target OS the binaries were built for") + arch = flag.String("arch", runtime.GOARCH, "Architecture cross packaging") + atype = flag.String("type", "zip", "Type of archive to write (zip|tar)") + signer = flag.String("signer", "", `Environment variable holding the signing key (e.g. LINUX_SIGNING_KEY)`) + signify = flag.String("signify", "", `Environment variable holding the signify key (e.g. LINUX_SIGNIFY_KEY)`) + upload = flag.String("upload", "", `Destination to upload the archives (usually "gethstore/builds")`) + ext string ) flag.CommandLine.Parse(cmdline) switch *atype { @@ -701,15 +720,15 @@ func doArchive(cmdline []string) { var ( env = build.Env() - basegeth = archiveBasename(*arch, version.Archive(env.Commit)) + basegeth = archiveBasename(*targetOS, *arch, version.Archive(env.Commit)) geth = "geth-" + basegeth + ext alltools = "geth-alltools-" + basegeth + ext ) maybeSkipArchive(env) - if err := build.WriteArchive(geth, gethArchiveFiles); err != nil { + if err := build.WriteArchive(geth, gethArchiveFiles(*targetOS)); err != nil { log.Fatal(err) } - if err := build.WriteArchive(alltools, allToolsArchiveFiles); err != nil { + if err := build.WriteArchive(alltools, allToolsArchiveFiles(*targetOS)); err != nil { log.Fatal(err) } for _, archive := range []string{geth, alltools} { @@ -735,7 +754,11 @@ func doKeeperArchive(cmdline []string) { maybeSkipArchive(env) files := []string{"COPYING"} for _, target := range keeperTargets { - files = append(files, executablePath(fmt.Sprintf("keeper-%s", target.Name))) + targetOS := target.GOOS + if targetOS == "" { + targetOS = runtime.GOOS + } + files = append(files, executablePath(fmt.Sprintf("keeper-%s", target.Name), targetOS)) } if err := build.WriteArchive(keeper, files); err != nil { log.Fatal(err) @@ -745,8 +768,8 @@ func doKeeperArchive(cmdline []string) { } } -func archiveBasename(arch string, archiveVersion string) string { - platform := runtime.GOOS + "-" + arch +func archiveBasename(targetOS, arch, archiveVersion string) string { + platform := targetOS + "-" + arch if arch == "arm" { platform += os.Getenv("GOARM") } @@ -1209,13 +1232,13 @@ func doWindowsInstaller(cmdline []string) { env := build.Env() maybeSkipArchive(env) - // Aggregate binaries that are included in the installer + // Aggregate binaries that are included in the installer. var ( devTools []string allTools []string gethTool string ) - for _, file := range allToolsArchiveFiles { + for _, file := range allToolsArchiveFiles("windows") { if file == "COPYING" { // license, copied later continue } @@ -1252,16 +1275,24 @@ func doWindowsInstaller(cmdline []string) { if env.Commit != "" { ver[2] += "-" + env.Commit[:8] } - installer, err := filepath.Abs("geth-" + archiveBasename(*arch, version.Archive(env.Commit)) + ".exe") + installer, err := filepath.Abs("geth-" + archiveBasename("windows", *arch, version.Archive(env.Commit)) + ".exe") if err != nil { log.Fatalf("Failed to convert installer file path: %v", err) } - build.MustRunCommand("makensis.exe", - "/DOUTPUTFILE="+installer, - "/DMAJORVERSION="+ver[0], - "/DMINORVERSION="+ver[1], - "/DBUILDVERSION="+ver[2], - "/DARCH="+*arch, + // makensis on Windows is "makensis.exe" with /D-style defines; on Linux + // (and other Unixes) the binary is "makensis" and accepts -D. + makensisCmd := "makensis" + defineFlag := "-D" + if runtime.GOOS == "windows" { + makensisCmd = "makensis.exe" + defineFlag = "/D" + } + build.MustRunCommand(makensisCmd, + defineFlag+"OUTPUTFILE="+installer, + defineFlag+"MAJORVERSION="+ver[0], + defineFlag+"MINORVERSION="+ver[1], + defineFlag+"BUILDVERSION="+ver[2], + defineFlag+"ARCH="+*arch, filepath.Join(*workdir, "geth.nsi"), ) // Sign and publish installer. diff --git a/internal/build/gotool.go b/internal/build/gotool.go index 172fa13464..00aa9d6f02 100644 --- a/internal/build/gotool.go +++ b/internal/build/gotool.go @@ -41,12 +41,19 @@ type GoToolchain struct { func (g *GoToolchain) Go(command string, args ...string) *exec.Cmd { tool := g.goTool(command, args...) - // Configure environment for cross build. - if g.GOARCH != "" && g.GOARCH != runtime.GOARCH { + // Configure environment for cross build. Force CGO_ENABLED=1 whenever + // either GOOS or GOARCH differs from the host: Go's default is + // CGO_ENABLED=0 for any cross-compile, but geth's release builds rely + // on cgo (c-kzg-4844, secp256k1) regardless of which axis is crossing. + crossArch := g.GOARCH != "" && g.GOARCH != runtime.GOARCH + crossOS := g.GOOS != "" && g.GOOS != runtime.GOOS + if crossArch || crossOS { tool.Env = append(tool.Env, "CGO_ENABLED=1") + } + if crossArch { tool.Env = append(tool.Env, "GOARCH="+g.GOARCH) } - if g.GOOS != "" && g.GOOS != runtime.GOOS { + if crossOS { tool.Env = append(tool.Env, "GOOS="+g.GOOS) } // Configure C compiler. From 1f3989dc708fcba783eae5e10932b1102de608d8 Mon Sep 17 00:00:00 2001 From: cui Date: Sat, 9 May 2026 08:51:12 +0800 Subject: [PATCH 65/80] signer/core: avoid mutating the input (#34908) --- signer/core/signed_data.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/signer/core/signed_data.go b/signer/core/signed_data.go index c62b513145..d8b6ef0674 100644 --- a/signer/core/signed_data.go +++ b/signer/core/signed_data.go @@ -17,6 +17,7 @@ package core import ( + "bytes" "context" "encoding/json" "errors" @@ -309,7 +310,8 @@ func (api *SignerAPI) EcRecover(ctx context.Context, data hexutil.Bytes, sig hex if sig[64] != 27 && sig[64] != 28 { return common.Address{}, errors.New("invalid Ethereum signature (V is not 27 or 28)") } - sig[64] -= 27 // Transform yellow paper V from 27/28 to 0/1 + sig = bytes.Clone(sig) // Avoid mutating the input + sig[64] -= 27 // Transform yellow paper V from 27/28 to 0/1 hash := accounts.TextHash(data) rpk, err := crypto.SigToPub(hash, sig) if err != nil { From b927ff8b53c070e8b369324fd0f63ffa28aea83a Mon Sep 17 00:00:00 2001 From: cui Date: Sat, 9 May 2026 21:59:03 +0800 Subject: [PATCH 66/80] cmd/devp2p: fix typo in ENR IP printing code (#34909) --- cmd/devp2p/enrcmd.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/devp2p/enrcmd.go b/cmd/devp2p/enrcmd.go index c9b692612f..af2cf90a81 100644 --- a/cmd/devp2p/enrcmd.go +++ b/cmd/devp2p/enrcmd.go @@ -194,7 +194,7 @@ func formatAttrString(v rlp.RawValue) (string, bool) { func formatAttrIP(v rlp.RawValue) (string, bool) { content, _, err := rlp.SplitString(v) - if err != nil || len(content) != 4 && len(content) != 6 { + if err != nil || len(content) != 4 && len(content) != 16 { return "", false } return net.IP(content).String(), true From 2ca3a6447d5f0bbea325cd99acad6537d0de8e9b Mon Sep 17 00:00:00 2001 From: Richard Creighton Date: Sat, 9 May 2026 15:07:12 +0100 Subject: [PATCH 67/80] cmd/geth: respect --graphql=false (#34914) Passing `--graphql=false` currently still registers the GraphQL handler because the startup path checks whether the flag was set, not its boolean value. This switches the registration condition to use `ctx.Bool`, so explicit false disables GraphQL while the default behavior remains unchanged. --- cmd/geth/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/geth/config.go b/cmd/geth/config.go index 40458186f4..31f19e7a32 100644 --- a/cmd/geth/config.go +++ b/cmd/geth/config.go @@ -270,7 +270,7 @@ func makeFullNode(ctx *cli.Context) *node.Node { filterSystem := utils.RegisterFilterAPI(stack, backend, &cfg.Eth) // Configure GraphQL if requested. - if ctx.IsSet(utils.GraphQLEnabledFlag.Name) { + if ctx.Bool(utils.GraphQLEnabledFlag.Name) { utils.RegisterGraphQLService(stack, backend, filterSystem, &cfg.Node) } // Add the Ethereum Stats daemon if requested. From 2ba9be9c0efcb640860cfedc4a43b6f6b2d68c7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20Kj=C3=A6rstad?= Date: Sun, 10 May 2026 11:45:23 +0200 Subject: [PATCH 68/80] build: upgrade -dlgo version to Go 1.25.10 (#34911) New security fix: https://groups.google.com/g/golang-announce/c/qcCIEXso47M --- build/checksums.txt | 84 ++++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/build/checksums.txt b/build/checksums.txt index 1832ce41dd..454efa93c4 100644 --- a/build/checksums.txt +++ b/build/checksums.txt @@ -5,49 +5,49 @@ # https://github.com/ethereum/execution-spec-tests/releases/download/v5.1.0 a3192784375acec7eaec492799d5c5d0c47a2909a3cc40178898e4ecd20cc416 fixtures_develop.tar.gz -# version:golang 1.25.9 +# version:golang 1.25.10 # https://go.dev/dl/ -0ec9ef8ebcea097aac37decae9f09a7218b451cd96be7d6ed513d8e4bcf909cf go1.25.9.src.tar.gz -b9ede6378a8f8d3d22bf52e68beb69ef7abdb65929ab2456020383002da15846 go1.25.9.aix-ppc64.tar.gz -92cb78fba4796e218c1accb0ea0a214ef2094c382049a244ad6505505d015fbe go1.25.9.darwin-amd64.tar.gz -9528be7329b9770631a6bd09ca2f3a73ed7332bec01d87435e75e92d8f130363 go1.25.9.darwin-arm64.tar.gz -918e44a471c5524caa52f74185064240d5eb343aa8023d604776511fc7adffa6 go1.25.9.dragonfly-amd64.tar.gz -2d67dbdfd09c6fcaa0e64485367ef43b8837ea200c663d6417183237bcddf83d go1.25.9.freebsd-386.tar.gz -9152d0c0badbfeb0c0e148e47c12bec28099d8cf2db60958810c879e0b679d07 go1.25.9.freebsd-amd64.tar.gz -437dca59604ad4a806a6a88e3d7ec1cd98ac9b402a3671629f4e553dd8b9888f go1.25.9.freebsd-arm.tar.gz -4c0fe53977412036fc8081e8d0992bbaabe4d3e1926137271ba11c2f5753300f go1.25.9.freebsd-arm64.tar.gz -d6087cdd1c084bd186132f29e0d032852a745f3c7619003d0fd5612c1fa58c8a go1.25.9.freebsd-riscv64.tar.gz -f82e49037e195cb62beae6a6ad83497157b2af5a01bad2f1dcb65df41080aabb go1.25.9.illumos-amd64.tar.gz -1e14a73bc2b19e370e0d4c57ba87aabfe8aef1e435e14d246742d48a13254f36 go1.25.9.linux-386.tar.gz -00859d7bd6defe8bf84d9db9e57b9a4467b2887c18cd93ae7460e713db774bc1 go1.25.9.linux-amd64.tar.gz -ec342e7389b7f489564ed5463c63b16cf8040023dabc7861256677165a8c0e2b go1.25.9.linux-arm64.tar.gz -7d4f0d266d871301e08ef4ac31c56e66048688893b2848392e5c600276351ee8 go1.25.9.linux-armv6l.tar.gz -f3460d901a14496bc609636e4accf9110ee1869d41c64af7e29cd567cffcf49b go1.25.9.linux-loong64.tar.gz -1da96ea449382ff96c09c55cee74815324e01d687d5ac6d2ade58244b8574306 go1.25.9.linux-mips.tar.gz -311a7f5f01f9a4bd51288b575eb619dc8e28e1fbc0cd78256a428b3ca668ff01 go1.25.9.linux-mips64.tar.gz -0b4edaf9e2ba3f0a079547effda70ec6a4b51a6ca3271a1147652c87ebcf3735 go1.25.9.linux-mips64le.tar.gz -42667340df264896f20b12261429d954e736e9772ab83ba289e68c30cf6f9628 go1.25.9.linux-mipsle.tar.gz -b9cbb3a4894b5aca6966c23452608435e8535278ef019b18d8898fbbfab67e74 go1.25.9.linux-ppc64.tar.gz -b0c41c7da1fc8d39020d65296a0dc54167afd9f76d67064e22c31ce3d839a739 go1.25.9.linux-ppc64le.tar.gz -2a630be8f854177c13e5fa75f7812c721369ecb9bd6e4c0fb1bd1c708d08b37c go1.25.9.linux-riscv64.tar.gz -0cf55136ac7eaccfc36d849054f849510ea289c2d959ffbed7b3866b4f484d17 go1.25.9.linux-s390x.tar.gz -eaf8167ff10a6a3e5dd304ef5f2e020b3a7379e76fa1011dc49c895800bf367c go1.25.9.netbsd-386.tar.gz -3cc6a861e62e23feae660984e0f2f14a2efb5d1f655900afee1d51af98919ae4 go1.25.9.netbsd-amd64.tar.gz -c2c44dca10e882c30553f4aa2ab8f6722b670fb12882378c8f461a9105d40188 go1.25.9.netbsd-arm.tar.gz -f301b71a8ec448053a5d2597df2e178120204bc9a33266c81600dd5d020a61b4 go1.25.9.netbsd-arm64.tar.gz -c4543b7fdef9707b4896810c69b4160a43ecec210af45c300f3abd78aa0c9e72 go1.25.9.openbsd-386.tar.gz -37275325e314f5ab7cf8ae65c4efc7cbfdaf20b41c6849549739b57a3ac97544 go1.25.9.openbsd-amd64.tar.gz -f9c05b6b315e979ecdd47354dd287c01708d6a88dc6ae7af74c84df8fa00df94 go1.25.9.openbsd-arm.tar.gz -4e999f42cf959ff95ca84af1ea1db3771000f5e57e157904bc2ffc72c75e29a2 go1.25.9.openbsd-arm64.tar.gz -0c7fa6c7c2b1cc13ad32fa94fc31273b4adf39c1e0f0e5dcedac158ff526af3f go1.25.9.openbsd-ppc64.tar.gz -347b33953a4b6e8df17719296f360f60878fe48a2d482ceb3637a3dfd4950065 go1.25.9.openbsd-riscv64.tar.gz -889f77d567c06832e0d332fe2458653dc66d43cded7ddbca6f72ce0ca60029cc go1.25.9.plan9-386.tar.gz -978b1f931fadec2f2516237d2649ee845d93c8eaf47dd196cfd8d26c7b2706a1 go1.25.9.plan9-amd64.tar.gz -30b9565e5ad0a212fe00990ead700c751b416eb2ef8d7c91a204945a7ff83a48 go1.25.9.plan9-arm.tar.gz -9e9125ff84ab3c3522ec758cab9540a17e9cba12bfcc34b6bf556cb89b522591 go1.25.9.solaris-amd64.tar.gz -bf40515f5f4d834fa9ead31ff75581e61a38ac27bf49840b95c5c998d321c0f6 go1.25.9.windows-386.zip -a7a710e225467b34e9e09fb432b829c86c9b2da5821ee5418f7eb2e8ae1a22cc go1.25.9.windows-amd64.zip -33cd73cf1b3ceee655ef71bc96e94006c02ae3c617fdd67ac9be3dfae3957449 go1.25.9.windows-arm64.zip +20cf04a92e5af99748e341bc8996fa28090c9ac98765fa115ec5ddf41d7af41d go1.25.10.src.tar.gz +a194e767c2ab4216a60acc068b9dbe6bf4fae05c14bb52d6bbdcb5b3ea521308 go1.25.10.aix-ppc64.tar.gz +52321165a3146cd91865ef98371506a846ed4dc4f9f1c9323e5ad90d2a411e06 go1.25.10.darwin-amd64.tar.gz +795691a425de7e7cdba3544f354dcd2cebcf52e87dc6898193878f34eb6d634f go1.25.10.darwin-arm64.tar.gz +e37b4544ba9e9e9a7ab2ed3116b3fc4d39a88da854baa5a566d9d6d3a9de7d4c go1.25.10.dragonfly-amd64.tar.gz +2a70d1fdabab637aa442ca94599a56e381238efa20cb995d5433b8579bfe482c go1.25.10.freebsd-386.tar.gz +9cdf522d87d47d82fec4a313cc4f8c3c94a7770426e8d443e4150a1f330cba71 go1.25.10.freebsd-amd64.tar.gz +6da6183633e9e59ffd9edefab68b5059c89b605596d94aaba650b1681fccd35f go1.25.10.freebsd-arm.tar.gz +7adcefeebdd05331f4d45f1ad2dddb5c53537cff6552e82f6595b3b833b95371 go1.25.10.freebsd-arm64.tar.gz +285f80a1ace21a7d94035cd753196eeada8cacd48e6396fd116ad5eb67aea957 go1.25.10.freebsd-riscv64.tar.gz +de7461bf0e5068a4f6e7f8713026d70516be6dbd5de5d21f9ced1c182f2f326e go1.25.10.illumos-amd64.tar.gz +2f574f2e2e19ead5b280fec0e7af5c81b76632685f03b6ac42dfa34c4b773c52 go1.25.10.linux-386.tar.gz +42d4f7a32316aa66591eca7e89867256057a4264451aca10570a715b3637ba70 go1.25.10.linux-amd64.tar.gz +654da1f9b50a5d1c2a85ccf8ed405aa89c06e94d18384628bf186f7712677b08 go1.25.10.linux-arm64.tar.gz +39f168f158e693887d3ad006168af1b1a3007b19c5993cae4d9d57f82f52aaf8 go1.25.10.linux-armv6l.tar.gz +05401fe5ea50ad2bafb9c797ef9bf21574b0661f19ef4d0dd66af8a0fb7323f3 go1.25.10.linux-loong64.tar.gz +d5bc2d6155d394a3aae41f21eb7c60da5595a6147aa0f30ed6b27da25e06c3f7 go1.25.10.linux-mips.tar.gz +8c64e7493e5953c3ba3153487d2fddd7f8ed142392c77f138e6792a6c1930db4 go1.25.10.linux-mips64.tar.gz +bd53aa2d558b7c1eadfc6bf01132e1859203a92f458ed7ba75b7f3230f14b095 go1.25.10.linux-mips64le.tar.gz +120b254e2e2980bb06687175db5c4064a85696c53001dc9f59934ad18f74a6bc go1.25.10.linux-mipsle.tar.gz +8a6acb21295b0ec974a44608361920ea8dbff5666631a6f556bd7d5f1d56535f go1.25.10.linux-ppc64.tar.gz +778925fdcdf9a272f823d147fad51545c3334b7ccd8652b2ccaaf2b01800280a go1.25.10.linux-ppc64le.tar.gz +b4f04ad0db48bcfea946db5323919cd21034e0bd2821a557dacd29c1b1013a4b go1.25.10.linux-riscv64.tar.gz +936b953e43921a64c12da871f76871ebbeb6d2092a7b8bdc307f5246f3c662cc go1.25.10.linux-s390x.tar.gz +061470e0bc7132146a5925a3cc28d5bc498eb1b1ff09dedcfaae10f781ff2274 go1.25.10.netbsd-386.tar.gz +63b2d50d7f8f269a9c82d42a4060e90cffb7f9102299818bb071b067aac8da8f go1.25.10.netbsd-amd64.tar.gz +c35129f68796526aa4dc4b6f481e2d995ef312aedadc88b659b945cc00e1f8f0 go1.25.10.netbsd-arm.tar.gz +2f541da4e2b298154d992d1f11bbb38c89d0821d91cc50a46776d42bb5e63bca go1.25.10.netbsd-arm64.tar.gz +2d42e569b07f1b99fdbfd008e7c22f967d165e2ce02464f46818fbed2aec43f5 go1.25.10.openbsd-386.tar.gz +0ad05960e8c9f867328151308c87f938433bec8f22f6a9437a896e22169fc840 go1.25.10.openbsd-amd64.tar.gz +099cc11473f99461c77161912740945308f08f6834980afb262c72bdc915f2d7 go1.25.10.openbsd-arm.tar.gz +bdf3335d5008c1ddc81fa94892283e4f1fee22566f5351d4e726d9f55a67c838 go1.25.10.openbsd-arm64.tar.gz +0933d418da0a61e0f29de717a77498f16b9b5b50dbe2205e20b2ed7fd4067f75 go1.25.10.openbsd-ppc64.tar.gz +191e6f3e75712f8c13d189d53b668e2cac6449f26474c1d86fbd04f6e9846f9c go1.25.10.openbsd-riscv64.tar.gz +68c053c8acd76c50fc430e92f4a86110ec3d97dd03d27b9339b4eaf793caff5f go1.25.10.plan9-386.tar.gz +42e2c46638ae22d93402e79efb40faee5c42cf7c56a01bb3ab47c6bb2512b745 go1.25.10.plan9-amd64.tar.gz +3ef1d5838b1648da16724a07b72e839ccbd7cb8899c3e0426afd6b79d494b91c go1.25.10.plan9-arm.tar.gz +631e3716017fbec06500a628d97e1155daec3593f0a7812c2ebfe8fc8c96b2ab go1.25.10.solaris-amd64.tar.gz +ddc693d2d9d7cc671ebb72d1d50aa05670f95b059b7d90440611af57976871d5 go1.25.10.windows-386.zip +ca37af2dadd8544464f1a9ca7c3886499d1cdfcb263855d0a1d71f194b2bd222 go1.25.10.windows-amd64.zip +38be57e0398bd93673d65bcae6dc7ee3cf151d7038d0dba5c60a5153022872da go1.25.10.windows-arm64.zip # version:golangci 2.10.1 # https://github.com/golangci/golangci-lint/releases/ From 8581125a21ee4592631abe127749d6ca3f1020fa Mon Sep 17 00:00:00 2001 From: vickkkkkyy Date: Sun, 10 May 2026 17:49:17 +0800 Subject: [PATCH 69/80] crypto: add hash length check in nocgo VerifySignature (#33839) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I was tracing a signature verification issue in a nocgo build and found that `VerifySignature` doesn't validate hash length. #33104 added the check to `Sign` and `sigToPub` but missed this one. The cgo path in `secp256k1/secp256.go` already rejects non-32-byte hashes, so the nocgo path should do the same — otherwise a wrong-length hash gets passed to decred's `Verify` and silently gives a bogus result. --- crypto/signature_nocgo.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto/signature_nocgo.go b/crypto/signature_nocgo.go index 0aab7180d3..bf273612e9 100644 --- a/crypto/signature_nocgo.go +++ b/crypto/signature_nocgo.go @@ -103,7 +103,7 @@ func Sign(hash []byte, prv *ecdsa.PrivateKey) ([]byte, error) { // The public key should be in compressed (33 bytes) or uncompressed (65 bytes) format. // The signature should have the 64 byte [R || S] format. func VerifySignature(pubkey, hash, signature []byte) bool { - if len(signature) != 64 { + if len(signature) != 64 || len(hash) != DigestLength { return false } var r, s secp256k1.ModNScalar From bcb68d23b3e78855be7d825ede37815d83b3142b Mon Sep 17 00:00:00 2001 From: cui Date: Sun, 10 May 2026 19:02:46 +0800 Subject: [PATCH 70/80] p2p: handle return false from TCPEndpoint (#34916) --- p2p/dial.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/p2p/dial.go b/p2p/dial.go index f9463d6d89..0ffcd10497 100644 --- a/p2p/dial.go +++ b/p2p/dial.go @@ -67,7 +67,10 @@ type tcpDialer struct { } func (t tcpDialer) Dial(ctx context.Context, dest *enode.Node) (net.Conn, error) { - addr, _ := dest.TCPEndpoint() + addr, ok := dest.TCPEndpoint() + if !ok { + return nil, errNoPort + } return t.d.DialContext(ctx, "tcp", addr.String()) } From 7facf9c1292d3783cb079a16ba797def4738e1c8 Mon Sep 17 00:00:00 2001 From: cui Date: Sun, 10 May 2026 19:03:57 +0800 Subject: [PATCH 71/80] core/txpool: use cmp.Compare instead of subtraction (#34918) This fixes a theoretical overflow condition if an account has an impossibly high nonce. --- core/txpool/locals/tx_tracker.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/txpool/locals/tx_tracker.go b/core/txpool/locals/tx_tracker.go index bb178f175e..66f3248105 100644 --- a/core/txpool/locals/tx_tracker.go +++ b/core/txpool/locals/tx_tracker.go @@ -18,6 +18,7 @@ package locals import ( + "cmp" "slices" "sync" "time" @@ -151,7 +152,7 @@ func (tracker *TxTracker) recheck(journalCheck bool) []*types.Transaction { for _, list := range rejournal { // cmp(a, b) should return a negative number when a < b, slices.SortFunc(list, func(a, b *types.Transaction) int { - return int(a.Nonce() - b.Nonce()) + return cmp.Compare(a.Nonce(), b.Nonce()) }) } // Rejournal the tracker while holding the lock. No new transactions will From f63c26509298ecfdf7e50d7b08f96809b2235ddd Mon Sep 17 00:00:00 2001 From: rayoo Date: Sun, 10 May 2026 19:43:40 +0800 Subject: [PATCH 72/80] internal/download: close dst on io.Copy error (#34910) --- internal/download/download.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/download/download.go b/internal/download/download.go index 27d3732731..94517166f5 100644 --- a/internal/download/download.go +++ b/internal/download/download.go @@ -212,6 +212,7 @@ func (db *ChecksumDB) DownloadFile(url, dstPath string) error { dst = newDownloadWriter(fd, resp.ContentLength) } if _, err = io.Copy(dst, resp.Body); err != nil { + dst.Close() os.Remove(tmpfile) return err } From 18becee8cb01f6d35224a6bac3e45d49b2074857 Mon Sep 17 00:00:00 2001 From: Marius van der Wijden Date: Sun, 10 May 2026 22:54:57 +0200 Subject: [PATCH 73/80] appveyor.yml: remove appveyor configuration (#34720) Removes the appveyor.yml since we moved to github runners. --------- Co-authored-by: Sina Mahmoodi Co-authored-by: Felix Lange --- appveyor.yml | 39 --------------------------------------- 1 file changed, 39 deletions(-) delete mode 100644 appveyor.yml diff --git a/appveyor.yml b/appveyor.yml deleted file mode 100644 index aeafcfc838..0000000000 --- a/appveyor.yml +++ /dev/null @@ -1,39 +0,0 @@ -clone_depth: 5 -version: "{branch}.{build}" - -image: - - Visual Studio 2019 - -environment: - matrix: - - GETH_ARCH: amd64 - GETH_MINGW: 'C:\msys64\mingw64' - - GETH_ARCH: 386 - GETH_MINGW: 'C:\msys64\mingw32' - -install: - - git submodule update --init --depth 1 --recursive - - go version - -for: - # Windows builds for amd64 + 386. - - matrix: - only: - - image: Visual Studio 2019 - environment: - # We use gcc from MSYS2 because it is the most recent compiler version available on - # AppVeyor. Note: gcc.exe only works properly if the corresponding bin/ directory is - # contained in PATH. - GETH_CC: '%GETH_MINGW%\bin\gcc.exe' - PATH: '%GETH_MINGW%\bin;C:\Program Files (x86)\NSIS\;%PATH%' - build_script: - - 'echo %GETH_ARCH%' - - 'echo %GETH_CC%' - - '%GETH_CC% --version' - - go run build/ci.go install -dlgo -arch %GETH_ARCH% -cc %GETH_CC% - after_build: - # Upload builds. Note that ci.go makes this a no-op PR builds. - - go run build/ci.go archive -arch %GETH_ARCH% -type zip -signer WINDOWS_SIGNING_KEY -upload gethstore/builds - - go run build/ci.go nsis -arch %GETH_ARCH% -signer WINDOWS_SIGNING_KEY -upload gethstore/builds - test_script: - - go run build/ci.go test -dlgo -arch %GETH_ARCH% -cc %GETH_CC% -short From 934a0091fa66b474832b3664854e979fe8bf76b2 Mon Sep 17 00:00:00 2001 From: rayoo Date: Mon, 11 May 2026 10:23:58 +0800 Subject: [PATCH 74/80] triedb/pathdb: fix layer 5 key range in storage iterator traversal test (#34883) --- triedb/pathdb/iterator_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/triedb/pathdb/iterator_test.go b/triedb/pathdb/iterator_test.go index 2197e85272..191c2fadf5 100644 --- a/triedb/pathdb/iterator_test.go +++ b/triedb/pathdb/iterator_test.go @@ -489,7 +489,7 @@ func TestStorageIteratorTraversalValues(t *testing.T) { if i%8 == 0 { e[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 4, i) } - if i > 50 || i < 85 { + if i > 50 && i < 85 { f[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 5, i) } if i%64 == 0 { From 2f11dccca00bc66b68f348193e98dee4b6e2206d Mon Sep 17 00:00:00 2001 From: Richard Creighton Date: Mon, 11 May 2026 15:08:55 +0100 Subject: [PATCH 75/80] cmd/geth: respect --dev=false (#34920) Passing `--dev=false` currently still enters the dev-mode startup path because a couple of branches check whether the flag was set, not its boolean value. This switches those branches to use `ctx.Bool`, so explicit false does not start dev mode or emit a dev genesis, while `--dev` keeps its existing behavior. --- cmd/geth/chaincmd.go | 2 +- cmd/geth/config.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/geth/chaincmd.go b/cmd/geth/chaincmd.go index 0aacb0878a..98ed348d8c 100644 --- a/cmd/geth/chaincmd.go +++ b/cmd/geth/chaincmd.go @@ -325,7 +325,7 @@ func dumpGenesis(ctx *cli.Context) error { var genesis *core.Genesis if utils.IsNetworkPreset(ctx) { genesis = utils.MakeGenesis(ctx) - } else if ctx.IsSet(utils.DeveloperFlag.Name) && !ctx.IsSet(utils.DataDirFlag.Name) { + } else if ctx.Bool(utils.DeveloperFlag.Name) && !ctx.IsSet(utils.DataDirFlag.Name) { genesis = core.DeveloperGenesisBlock(11_500_000, nil) } diff --git a/cmd/geth/config.go b/cmd/geth/config.go index 31f19e7a32..c02e307bdc 100644 --- a/cmd/geth/config.go +++ b/cmd/geth/config.go @@ -291,7 +291,7 @@ func makeFullNode(ctx *cli.Context) *node.Node { } utils.RegisterSyncOverrideService(stack, eth, syncConfig) - if ctx.IsSet(utils.DeveloperFlag.Name) { + if ctx.Bool(utils.DeveloperFlag.Name) { // Start dev mode. simBeacon, err := catalyst.NewSimulatedBeacon(ctx.Uint64(utils.DeveloperPeriodFlag.Name), cfg.Eth.Miner.PendingFeeRecipient, eth) if err != nil { From e1047b9c8489ed2e26845498b58e3e30dad66f1c Mon Sep 17 00:00:00 2001 From: Marius van der Wijden Date: Mon, 11 May 2026 16:25:57 +0200 Subject: [PATCH 76/80] core: use uint256 in core.Message (#34934) Changes core.Message to use Uint256 which is faster --------- Co-authored-by: Gary Rong --- common/hexutil/json.go | 4 + core/evm.go | 2 +- core/state_processor.go | 19 ++-- core/state_transition.go | 151 ++++++++++++++++++---------- eth/gasestimator/gasestimator.go | 16 +-- internal/ethapi/transaction_args.go | 24 +++-- tests/state_test.go | 3 +- tests/state_test_util.go | 10 +- 8 files changed, 142 insertions(+), 87 deletions(-) diff --git a/common/hexutil/json.go b/common/hexutil/json.go index 6b9f412078..c00cd879c8 100644 --- a/common/hexutil/json.go +++ b/common/hexutil/json.go @@ -204,6 +204,10 @@ func (b *Big) ToInt() *big.Int { return (*big.Int)(b) } +func (b *Big) ToUint256() (*uint256.Int, bool) { + return uint256.FromBig((*big.Int)(b)) +} + // String returns the hex encoding of b. func (b *Big) String() string { return EncodeBig(b.ToInt()) diff --git a/core/evm.go b/core/evm.go index 818b23bee5..73e4c01a99 100644 --- a/core/evm.go +++ b/core/evm.go @@ -87,7 +87,7 @@ func NewEVMBlockContext(header *types.Header, chain ChainContext, author *common func NewEVMTxContext(msg *Message) vm.TxContext { ctx := vm.TxContext{ Origin: msg.From, - GasPrice: uint256.MustFromBig(msg.GasPrice), + GasPrice: msg.GasPrice, BlobHashes: msg.BlobHashes, } return ctx diff --git a/core/state_processor.go b/core/state_processor.go index 54ebbd047b..9dcb4cf07c 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -32,6 +32,7 @@ import ( "github.com/ethereum/go-ethereum/internal/telemetry" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/trie" + "github.com/holiman/uint256" ) // StateProcessor is a basic Processor, which takes care of transitioning @@ -254,9 +255,9 @@ func ProcessBeaconBlockRoot(beaconRoot common.Hash, evm *vm.EVM) { msg := &Message{ From: params.SystemAddress, GasLimit: 30_000_000, - GasPrice: common.Big0, - GasFeeCap: common.Big0, - GasTipCap: common.Big0, + GasPrice: uint256.NewInt(0), + GasFeeCap: uint256.NewInt(0), + GasTipCap: uint256.NewInt(0), To: ¶ms.BeaconRootsAddress, Data: beaconRoot[:], } @@ -281,9 +282,9 @@ func ProcessParentBlockHash(prevHash common.Hash, evm *vm.EVM) { msg := &Message{ From: params.SystemAddress, GasLimit: 30_000_000, - GasPrice: common.Big0, - GasFeeCap: common.Big0, - GasTipCap: common.Big0, + GasPrice: uint256.NewInt(0), + GasFeeCap: uint256.NewInt(0), + GasTipCap: uint256.NewInt(0), To: ¶ms.HistoryStorageAddress, Data: prevHash.Bytes(), } @@ -321,9 +322,9 @@ func processRequestsSystemCall(requests *[][]byte, evm *vm.EVM, requestType byte msg := &Message{ From: params.SystemAddress, GasLimit: 30_000_000, - GasPrice: common.Big0, - GasFeeCap: common.Big0, - GasTipCap: common.Big0, + GasPrice: uint256.NewInt(0), + GasFeeCap: uint256.NewInt(0), + GasTipCap: uint256.NewInt(0), To: &addr, } evm.SetTxContext(NewEVMTxContext(msg)) diff --git a/core/state_transition.go b/core/state_transition.go index b5b8b22155..fcd483eeb7 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -210,14 +210,14 @@ type Message struct { To *common.Address From common.Address Nonce uint64 - Value *big.Int + Value *uint256.Int GasLimit uint64 - GasPrice *big.Int - GasFeeCap *big.Int - GasTipCap *big.Int + GasPrice *uint256.Int + GasFeeCap *uint256.Int + GasTipCap *uint256.Int Data []byte AccessList types.AccessList - BlobGasFeeCap *big.Int + BlobGasFeeCap *uint256.Int BlobHashes []common.Hash SetCodeAuthorizations []types.SetCodeAuthorization @@ -238,32 +238,64 @@ type Message struct { // TransactionToMessage converts a transaction into a Message. func TransactionToMessage(tx *types.Transaction, s types.Signer, baseFee *big.Int) (*Message, error) { + from, err := types.Sender(s, tx) + if err != nil { + return nil, err + } + gasPrice, overflow := uint256.FromBig(tx.GasPrice()) + if overflow { + return nil, fmt.Errorf("%w: address %v, maxFeePerGas bit length: %d", ErrFeeCapVeryHigh, + from.Hex(), tx.GasPrice().BitLen()) + } + txGasFeeCap := tx.GasFeeCap() + gasFeeCap, overflow := uint256.FromBig(txGasFeeCap) + if overflow { + return nil, fmt.Errorf("%w: address %v, maxFeePerGas bit length: %d", ErrFeeCapVeryHigh, + from.Hex(), tx.GasFeeCap().BitLen()) + } + txGasTipCap := tx.GasTipCap() + gasTipCap, overflow := uint256.FromBig(txGasTipCap) + if overflow { + return nil, fmt.Errorf("%w: address %v, maxPriorityFeePerGas bit length: %d", ErrTipVeryHigh, + from.Hex(), tx.GasTipCap().BitLen()) + } + value, overflow := uint256.FromBig(tx.Value()) + if overflow { + return nil, fmt.Errorf("value exceeds 256 bits: address %v", from.Hex()) + } + blobGasFeeCap, overflow := uint256.FromBig(tx.BlobGasFeeCap()) + if overflow { + return nil, fmt.Errorf("blobGasFeeCap exceeds 256 bits: address %v", from.Hex()) + } + msg := &Message{ + From: from, Nonce: tx.Nonce(), GasLimit: tx.Gas(), - GasPrice: tx.GasPrice(), - GasFeeCap: tx.GasFeeCap(), - GasTipCap: tx.GasTipCap(), + GasPrice: gasPrice, + GasFeeCap: gasFeeCap, + GasTipCap: gasTipCap, To: tx.To(), - Value: tx.Value(), + Value: value, Data: tx.Data(), AccessList: tx.AccessList(), SetCodeAuthorizations: tx.SetCodeAuthorizations(), SkipNonceChecks: false, SkipTransactionChecks: false, BlobHashes: tx.BlobHashes(), - BlobGasFeeCap: tx.BlobGasFeeCap(), + BlobGasFeeCap: blobGasFeeCap, } // If baseFee provided, set gasPrice to effectiveGasPrice. if baseFee != nil { - msg.GasPrice = msg.GasPrice.Add(msg.GasTipCap, baseFee) - if msg.GasPrice.Cmp(msg.GasFeeCap) > 0 { - msg.GasPrice = msg.GasFeeCap + effectiveGasPrice := new(big.Int).Add(baseFee, txGasTipCap) + if effectiveGasPrice.Cmp(txGasFeeCap) > 0 { + effectiveGasPrice = txGasFeeCap } + // EffectiveGasPrice is already capped by txGasFeeCap, therefore + // the overflow check is not required. + msg.GasPrice = uint256.MustFromBig(effectiveGasPrice) } - var err error - msg.From, err = types.Sender(s, tx) - return msg, err + return msg, nil } // ApplyMessage computes the new state by applying the given message @@ -333,32 +365,55 @@ func (st *stateTransition) to() common.Address { } func (st *stateTransition) buyGas() error { - mgval := new(big.Int).SetUint64(st.msg.GasLimit) - mgval.Mul(mgval, st.msg.GasPrice) - balanceCheck := new(big.Int).Set(mgval) + mgval := new(uint256.Int).SetUint64(st.msg.GasLimit) + _, overflow := mgval.MulOverflow(mgval, st.msg.GasPrice) + if overflow { + return fmt.Errorf("%w: address %v required balance exceeds 256 bits", ErrInsufficientFunds, st.msg.From.Hex()) + } + balanceCheck := new(uint256.Int).Set(mgval) if st.msg.GasFeeCap != nil { balanceCheck.SetUint64(st.msg.GasLimit) - balanceCheck = balanceCheck.Mul(balanceCheck, st.msg.GasFeeCap) + if _, overflow := balanceCheck.MulOverflow(balanceCheck, st.msg.GasFeeCap); overflow { + return fmt.Errorf("%w: address %v required balance exceeds 256 bits", ErrInsufficientFunds, st.msg.From.Hex()) + } + } + if st.msg.Value != nil { + if _, overflow := balanceCheck.AddOverflow(balanceCheck, st.msg.Value); overflow { + return fmt.Errorf("%w: address %v required balance exceeds 256 bits", ErrInsufficientFunds, st.msg.From.Hex()) + } } - balanceCheck.Add(balanceCheck, st.msg.Value) if st.evm.ChainConfig().IsCancun(st.evm.Context.BlockNumber, st.evm.Context.Time) { if blobGas := st.blobGasUsed(); blobGas > 0 { // Check that the user has enough funds to cover blobGasUsed * tx.BlobGasFeeCap - blobBalanceCheck := new(big.Int).SetUint64(blobGas) - blobBalanceCheck.Mul(blobBalanceCheck, st.msg.BlobGasFeeCap) - balanceCheck.Add(balanceCheck, blobBalanceCheck) + blobBalanceCheck := new(uint256.Int).SetUint64(blobGas) + if _, overflow := blobBalanceCheck.MulOverflow(blobBalanceCheck, st.msg.BlobGasFeeCap); overflow { + return fmt.Errorf("%w: address %v required balance exceeds 256 bits", ErrInsufficientFunds, st.msg.From.Hex()) + } + if _, overflow := balanceCheck.AddOverflow(balanceCheck, blobBalanceCheck); overflow { + return fmt.Errorf("%w: address %v required balance exceeds 256 bits", ErrInsufficientFunds, st.msg.From.Hex()) + } // Pay for blobGasUsed * actual blob fee - blobFee := new(big.Int).SetUint64(blobGas) - blobFee.Mul(blobFee, st.evm.Context.BlobBaseFee) - mgval.Add(mgval, blobFee) + blobBaseFee, overflow := uint256.FromBig(st.evm.Context.BlobBaseFee) + if overflow { + return fmt.Errorf("invalid blobBaseFee: %v", st.evm.Context.BlobBaseFee) + } + blobFee := new(uint256.Int).SetUint64(blobGas) + + // In practice, overflow checking is unnecessary, as blobBaseFee cannot exceed + // BlobGasFeeCap. However, in eth_call it is still possible for users to specify + // an excessively large blob base fee and bypass the blob base fee validation. + _, overflow = blobFee.MulOverflow(blobFee, blobBaseFee) + if overflow { + return fmt.Errorf("%w: address %v required balance exceeds 256 bits", ErrInsufficientFunds, st.msg.From.Hex()) + } + _, overflow = mgval.AddOverflow(mgval, blobFee) + if overflow { + return fmt.Errorf("%w: address %v required balance exceeds 256 bits", ErrInsufficientFunds, st.msg.From.Hex()) + } } } - balanceCheckU256, overflow := uint256.FromBig(balanceCheck) - if overflow { - return fmt.Errorf("%w: address %v required balance exceeds 256 bits", ErrInsufficientFunds, st.msg.From.Hex()) - } - if have, want := st.state.GetBalance(st.msg.From), balanceCheckU256; have.Cmp(want) < 0 { + if have, want := st.state.GetBalance(st.msg.From), balanceCheck; have.Cmp(want) < 0 { return fmt.Errorf("%w: address %v have %v want %v", ErrInsufficientFunds, st.msg.From.Hex(), have, want) } if err := st.gp.SubGas(st.msg.GasLimit); err != nil { @@ -371,8 +426,7 @@ func (st *stateTransition) buyGas() error { st.gasRemaining = vm.NewGasBudget(st.msg.GasLimit) st.initialBudget = st.gasRemaining.Copy() - mgvalU256, _ := uint256.FromBig(mgval) - st.state.SubBalance(st.msg.From, mgvalU256, tracing.BalanceDecreaseGasBuy) + st.state.SubBalance(st.msg.From, mgval, tracing.BalanceDecreaseGasBuy) return nil } @@ -412,21 +466,13 @@ func (st *stateTransition) preCheck() error { // Skip the checks if gas fields are zero and baseFee was explicitly disabled (eth_call) skipCheck := st.evm.Config.NoBaseFee && msg.GasFeeCap.BitLen() == 0 && msg.GasTipCap.BitLen() == 0 if !skipCheck { - if l := msg.GasFeeCap.BitLen(); l > 256 { - return fmt.Errorf("%w: address %v, maxFeePerGas bit length: %d", ErrFeeCapVeryHigh, - msg.From.Hex(), l) - } - if l := msg.GasTipCap.BitLen(); l > 256 { - return fmt.Errorf("%w: address %v, maxPriorityFeePerGas bit length: %d", ErrTipVeryHigh, - msg.From.Hex(), l) - } if msg.GasFeeCap.Cmp(msg.GasTipCap) < 0 { return fmt.Errorf("%w: address %v, maxPriorityFeePerGas: %s, maxFeePerGas: %s", ErrTipAboveFeeCap, msg.From.Hex(), msg.GasTipCap, msg.GasFeeCap) } // This will panic if baseFee is nil, but basefee presence is verified // as part of header validation. - if msg.GasFeeCap.Cmp(st.evm.Context.BaseFee) < 0 { + if msg.GasFeeCap.CmpBig(st.evm.Context.BaseFee) < 0 { return fmt.Errorf("%w: address %v, maxFeePerGas: %s, baseFee: %s", ErrFeeCapTooLow, msg.From.Hex(), msg.GasFeeCap, st.evm.Context.BaseFee) } @@ -460,7 +506,7 @@ func (st *stateTransition) preCheck() error { if !skipCheck { // This will panic if blobBaseFee is nil, but blobBaseFee presence // is verified as part of header validation. - if msg.BlobGasFeeCap.Cmp(st.evm.Context.BlobBaseFee) < 0 { + if msg.BlobGasFeeCap.CmpBig(st.evm.Context.BlobBaseFee) < 0 { return fmt.Errorf("%w: address %v blobGasFeeCap: %v, blobBaseFee: %v", ErrBlobFeeCapTooLow, msg.From.Hex(), msg.BlobGasFeeCap, st.evm.Context.BlobBaseFee) } @@ -543,9 +589,9 @@ func (st *stateTransition) execute() (*ExecutionResult, error) { } // Check clause 6 - value, overflow := uint256.FromBig(msg.Value) - if overflow { - return nil, fmt.Errorf("%w: address %v", ErrInsufficientFundsForTransfer, msg.From.Hex()) + value := msg.Value + if value == nil { + value = new(uint256.Int) } if !value.IsZero() && !st.evm.Context.CanTransfer(st.state, msg.From, value) { return nil, fmt.Errorf("%w: address %v", ErrInsufficientFundsForTransfer, msg.From.Hex()) @@ -629,9 +675,12 @@ func (st *stateTransition) execute() (*ExecutionResult, error) { } effectiveTip := msg.GasPrice if rules.IsLondon { - effectiveTip = new(big.Int).Sub(msg.GasPrice, st.evm.Context.BaseFee) + baseFee, overflow := uint256.FromBig(st.evm.Context.BaseFee) + if overflow { + return nil, fmt.Errorf("invalid baseFee: %v", st.evm.Context.BaseFee) + } + effectiveTip = new(uint256.Int).Sub(msg.GasPrice, baseFee) } - effectiveTipU256, _ := uint256.FromBig(effectiveTip) if st.evm.Config.NoBaseFee && msg.GasFeeCap.Sign() == 0 && msg.GasTipCap.Sign() == 0 { // Skip fee payment when NoBaseFee is set and the fee fields @@ -639,7 +688,7 @@ func (st *stateTransition) execute() (*ExecutionResult, error) { // the coinbase when simulating calls. } else { fee := new(uint256.Int).SetUint64(st.gasUsed()) - fee.Mul(fee, effectiveTipU256) + fee.Mul(fee, effectiveTip) st.state.AddBalance(st.evm.Context.Coinbase, fee, tracing.BalanceIncreaseRewardTransactionFee) // add the coinbase to the witness iff the fee is greater than 0 @@ -741,7 +790,7 @@ func (st *stateTransition) calcRefund() vm.GasBudget { // exchanged at the original rate. func (st *stateTransition) returnGas() { remaining := uint256.NewInt(st.gasRemaining.RegularGas) - remaining.Mul(remaining, uint256.MustFromBig(st.msg.GasPrice)) + remaining.Mul(remaining, st.msg.GasPrice) st.state.AddBalance(st.msg.From, remaining, tracing.BalanceIncreaseGasReturn) if st.evm.Config.Tracer != nil && st.evm.Config.Tracer.OnGasChange != nil && st.gasRemaining.RegularGas > 0 { diff --git a/eth/gasestimator/gasestimator.go b/eth/gasestimator/gasestimator.go index ace0752037..f45fc0d8c9 100644 --- a/eth/gasestimator/gasestimator.go +++ b/eth/gasestimator/gasestimator.go @@ -22,13 +22,13 @@ import ( "fmt" "math/big" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/params" + "github.com/holiman/uint256" ) // Options are the contextual parameters to execute the requested call. @@ -70,17 +70,17 @@ func Estimate(ctx context.Context, call *core.Message, opts *Options, gasCap uin } // Normalize the max fee per gas the call is willing to spend. - var feeCap *big.Int + var feeCap *uint256.Int if call.GasFeeCap != nil { feeCap = call.GasFeeCap } else if call.GasPrice != nil { feeCap = call.GasPrice } else { - feeCap = common.Big0 + feeCap = uint256.NewInt(0) } // Recap the highest gas limit with account's available balance. if feeCap.BitLen() != 0 { - balance := opts.State.GetBalance(call.From).ToBig() + balance := opts.State.GetBalance(call.From).Clone() available := balance if call.Value != nil { @@ -90,8 +90,8 @@ func Estimate(ctx context.Context, call *core.Message, opts *Options, gasCap uin available.Sub(available, call.Value) } if opts.Config.IsCancun(opts.Header.Number, opts.Header.Time) && len(call.BlobHashes) > 0 { - blobGasPerBlob := new(big.Int).SetInt64(params.BlobTxBlobGasPerBlob) - blobBalanceUsage := new(big.Int).SetInt64(int64(len(call.BlobHashes))) + blobGasPerBlob := uint256.NewInt(params.BlobTxBlobGasPerBlob) + blobBalanceUsage := uint256.NewInt(uint64(len(call.BlobHashes))) blobBalanceUsage.Mul(blobBalanceUsage, blobGasPerBlob) blobBalanceUsage.Mul(blobBalanceUsage, call.BlobGasFeeCap) if blobBalanceUsage.Cmp(available) >= 0 { @@ -99,13 +99,13 @@ func Estimate(ctx context.Context, call *core.Message, opts *Options, gasCap uin } available.Sub(available, blobBalanceUsage) } - allowance := new(big.Int).Div(available, feeCap) + allowance := new(uint256.Int).Div(available, feeCap) // If the allowance is larger than maximum uint64, skip checking if allowance.IsUint64() && hi > allowance.Uint64() { transfer := call.Value if transfer == nil { - transfer = new(big.Int) + transfer = new(uint256.Int) } log.Debug("Gas estimation capped by limited funds", "original", hi, "balance", balance, "sent", transfer, "maxFeePerGas", feeCap, "fundable", allowance) diff --git a/internal/ethapi/transaction_args.go b/internal/ethapi/transaction_args.go index 4fb30e6289..1032d067f1 100644 --- a/internal/ethapi/transaction_args.go +++ b/internal/ethapi/transaction_args.go @@ -446,27 +446,27 @@ func (args *TransactionArgs) CallDefaults(globalGasCap uint64, baseFee *big.Int, // Assumes that fields are not nil, i.e. setDefaults or CallDefaults has been called. func (args *TransactionArgs) ToMessage(baseFee *big.Int, skipNonceCheck bool) *core.Message { var ( - gasPrice *big.Int - gasFeeCap *big.Int - gasTipCap *big.Int + gasPrice *uint256.Int + gasFeeCap *uint256.Int + gasTipCap *uint256.Int ) if baseFee == nil { - gasPrice = args.GasPrice.ToInt() + gasPrice, _ = args.GasPrice.ToUint256() gasFeeCap, gasTipCap = gasPrice, gasPrice } else { // A basefee is provided, necessitating 1559-type execution if args.GasPrice != nil { // User specified the legacy gas field, convert to 1559 gas typing - gasPrice = args.GasPrice.ToInt() + gasPrice, _ = args.GasPrice.ToUint256() gasFeeCap, gasTipCap = gasPrice, gasPrice } else { // User specified 1559 gas fields (or none), use those - gasFeeCap = args.MaxFeePerGas.ToInt() - gasTipCap = args.MaxPriorityFeePerGas.ToInt() + gasFeeCap, _ = args.MaxFeePerGas.ToUint256() + gasTipCap, _ = args.MaxPriorityFeePerGas.ToUint256() // Backfill the legacy gasPrice for EVM execution, unless we're all zeroes - gasPrice = new(big.Int) + gasPrice = uint256.NewInt(0) if gasFeeCap.BitLen() > 0 || gasTipCap.BitLen() > 0 { - gasPrice = gasPrice.Add(gasTipCap, baseFee) + gasPrice = gasPrice.Add(gasTipCap, uint256.MustFromBig(baseFee)) if gasPrice.Cmp(gasFeeCap) > 0 { gasPrice = gasFeeCap } @@ -477,10 +477,12 @@ func (args *TransactionArgs) ToMessage(baseFee *big.Int, skipNonceCheck bool) *c if args.AccessList != nil { accessList = *args.AccessList } + value, _ := args.Value.ToUint256() + blobFeeCap, _ := args.BlobFeeCap.ToUint256() return &core.Message{ From: args.from(), To: args.To, - Value: (*big.Int)(args.Value), + Value: value, Nonce: uint64(*args.Nonce), GasLimit: uint64(*args.Gas), GasPrice: gasPrice, @@ -488,7 +490,7 @@ func (args *TransactionArgs) ToMessage(baseFee *big.Int, skipNonceCheck bool) *c GasTipCap: gasTipCap, Data: args.data(), AccessList: accessList, - BlobGasFeeCap: (*big.Int)(args.BlobFeeCap), + BlobGasFeeCap: blobFeeCap, BlobHashes: args.BlobHashes, SetCodeAuthorizations: args.AuthorizationList, SkipNonceChecks: skipNonceCheck, diff --git a/tests/state_test.go b/tests/state_test.go index 8444d211cf..cf1d4bce4c 100644 --- a/tests/state_test.go +++ b/tests/state_test.go @@ -35,7 +35,6 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/eth/tracers/logger" - "github.com/holiman/uint256" ) func initMatcher(st *testMatcher) { @@ -329,7 +328,7 @@ func runBenchmark(b *testing.B, t *StateTest) { initialGas := vm.NewGasBudget(msg.GasLimit) // Execute the message. - _, leftOverGas, err := evm.Call(sender.Address(), *msg.To, msg.Data, initialGas.Copy(), uint256.MustFromBig(msg.Value)) + _, leftOverGas, err := evm.Call(sender.Address(), *msg.To, msg.Data, initialGas.Copy(), msg.Value) if err != nil { b.Error(err) return diff --git a/tests/state_test_util.go b/tests/state_test_util.go index f7cf1c0697..e33e15fc8c 100644 --- a/tests/state_test_util.go +++ b/tests/state_test_util.go @@ -479,15 +479,15 @@ func (tx *stTransaction) toMessage(ps stPostState, baseFee *big.Int) (*core.Mess From: from, To: to, Nonce: tx.Nonce, - Value: value, + Value: uint256.MustFromBig(value), GasLimit: gasLimit, - GasPrice: gasPrice, - GasFeeCap: tx.MaxFeePerGas, - GasTipCap: tx.MaxPriorityFeePerGas, + GasPrice: uint256.MustFromBig(gasPrice), + GasFeeCap: uint256.MustFromBig(tx.MaxFeePerGas), + GasTipCap: uint256.MustFromBig(tx.MaxPriorityFeePerGas), Data: data, AccessList: accessList, BlobHashes: tx.BlobVersionedHashes, - BlobGasFeeCap: tx.BlobGasFeeCap, + BlobGasFeeCap: uint256.MustFromBig(tx.BlobGasFeeCap), SetCodeAuthorizations: authList, } return msg, nil From 117e067f0f0bae1a17082321f224dedb6765b10f Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Mon, 11 May 2026 23:19:24 +0800 Subject: [PATCH 77/80] version: release go-ethereum v1.17.3 stable (#34937) --- version/version.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/version/version.go b/version/version.go index ea1f5fc632..4b64b58bb7 100644 --- a/version/version.go +++ b/version/version.go @@ -17,8 +17,8 @@ package version const ( - Major = 1 // Major version component of the current release - Minor = 17 // Minor version component of the current release - Patch = 3 // Patch version component of the current release - Meta = "unstable" // Version metadata to append to the version string + Major = 1 // Major version component of the current release + Minor = 17 // Minor version component of the current release + Patch = 3 // Patch version component of the current release + Meta = "stable" // Version metadata to append to the version string ) From df1e813aa15123104bc8d40e18db0bb4a8c7f4a8 Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Tue, 4 Aug 2026 11:31:54 +0530 Subject: [PATCH 78/80] docs: record the v1.17.3 milestone of the go-ethereum v1.17.4 sync Documentation only; no code changes. Covers batches 20 through 26 plus the two hand-written adoption commits, closing the v1.17.3 milestone. The ledger gains a section per batch, each recording what upstream changed, how it interacts with Bor's divergences, and why every non-trivial conflict was resolved the way it was. Three decisions carry most of the weight. The consensus interface keeps finalize-and-assemble. Upstream removed it on the premise that block assembly is consensus-agnostic, which does not hold for Bor: finalization performs the state-sync commits and therefore produces receipts, while upstream's replacement takes receipts as an input. The EVM stack keeps Bor's storage layout while adopting upstream's call-site API. Upstream's arena and Bor's fixed-array port pursue the same goal with opposite trade-offs, and no measurement exists for Bor's workload, so the hot path was left alone and only the accessor surface converged, which is where future conflicts actually come from. The event mux migration is deferred, because it is entangled with an already deferred sync-mode relocation and with Bor's forked downloader at the same time. The fork register gains three Amsterdam-gated pricing changes: the calldata floor increase, the lifting of the per-transaction gas cap, and the access list cost increase. All three are inert while Amsterdam is dormant, and all three need reviewing together before it is scheduled, since Bor's gates are wider than upstream's in two of the three cases and the gas-cap lift only makes sense alongside a state-gas dimension Bor has not adopted. The needs-wiring backlog gains twelve rows across the milestone, including two that are explicitly not for the merge to decide: the binary-trie reader is invisible to Bor's reader dispatch, which would silently collect no witness data if that trie type were ever enabled, and the effective tip must stay signed because Bor removed the guard that keeps it non-negative. The version file is intentionally unchanged. Checking the earlier milestone tips before deciding showed all of them still on the same version constants, and the only commits in this sync that ever touched the file were auto-merges of upstream's release-cycle commits. Bor does not track upstream's release version here, so the batch that ended on upstream's release tag kept Bor's values, and this commit does not bump them either. What that file should say for a tree based on the final sync target is a Bor release decision, and it is recorded as an open question rather than answered here. --- docs/upstream-merges/v1.17.4/fork-register.md | 8 +- docs/upstream-merges/v1.17.4/ledger.md | 988 ++++++++++++++++++ docs/upstream-merges/v1.17.4/needs-wiring.md | 19 + docs/upstream-merges/v1.17.4/plan.md | 54 +- 4 files changed, 1058 insertions(+), 11 deletions(-) diff --git a/docs/upstream-merges/v1.17.4/fork-register.md b/docs/upstream-merges/v1.17.4/fork-register.md index c3baf91bb3..faaf0f73dc 100644 --- a/docs/upstream-merges/v1.17.4/fork-register.md +++ b/docs/upstream-merges/v1.17.4/fork-register.md @@ -12,7 +12,8 @@ gate-guarded (not ungated / unconditional). | EIP / fork | upstream PR | batch | touches | Bor gate (value) | decision | verified-dormant | | ---------- | ----------- | ----- | ------- | ---------------- | -------- | ---------------- | -| Verkle / binary-trie transition | verkle/UBT groundwork (`cf93077fa`); go-verkle removal `#33461` (`710008450`) | 3 (v1.17.0 2/12) intro → removed batch 6 (`b66d3f157`) | `core/state`, `core/vm`, `core/types/block.go`, `trie/*`, `cmd/*` | `VerkleBlock = nil` (all Bor presets); `IsVerkle(num)` block-based — **fork gate KEPT dormant** | **go-verkle impl REMOVED** (2026-07-23, `b66d3f157`, mirroring #33461); binary tree kept; fork gate kept dormant — see Notes | yes — go-verkle/go-ipa deleted; `VerkleTrie`/`PointCache`/verkle `ExecutionWitness` gone; `AccessEvents` key derivation now `bintrie`. `VerkleBlock: nil` on every preset so `IsVerkle()` never activates; upstream's follow-up verkle stub/rename PRs to be mirrored in later batches. | +| Verkle / binary-trie transition | verkle/UBT groundwork (`cf93077fa`); go-verkle removal `#33461` (`710008450`) | 3 (v1.17.0 2/12) intro → removed batch 6 (`b66d3f157`) | `core/state`, `core/vm`, `core/types/block.go`, `trie/*`, `cmd/*` | `VerkleBlock = nil` (all Bor presets); `IsVerkle(num)` block-based — **fork gate KEPT dormant** | **go-verkle impl REMOVED** (2026-07-23, `b66d3f157`, mirroring #33461); binary tree kept; fork gate kept dormant — see Notes | yes — go-verkle/go-ipa deleted; `VerkleTrie`/`PointCache`/verkle `ExecutionWitness` gone; `AccessEvents` key derivation now `bintrie`. `VerkleBlock: nil` on every preset so `IsVerkle()` never activates. **Batch 22 (#34700, `ba215fd92`):** upstream renamed the whole vocabulary to UBT (`VerkleTime`→`UBTTime`, `IsVerkle`→`IsUBT`, `BlobScheduleConfig.Verkle`→`.UBT`) and split `CachingDB` into `MPTDatabase`+`UBTDatabase`. **Deferred in the batch, then adopted on branch `ppatil-upstream-mptubt` before batch 23** — the type split only, not the runtime fork-boundary selection; see the two `needs-wiring.md` rows. In `params/config.go` the rename is inapplicable regardless: Bor deleted the timestamp fork fields in favour of block-based ones, so there is no `VerkleTime` to rename and `VerkleBlock`/`IsVerkle(num)`/`Rules.IsVerkle`/`BlobScheduleConfig.Verkle` are all unaffected. After the adoption the vocabulary is split by design: `core`, `trie` and `triedb` say UBT, `params` and the `--override.verkle` CLI flag still say Verkle, because that is where the fork *schedule* lives. Gate still dormant — `UBTDatabase` is never constructed on any Bor preset. | +| EIP-7610 reworked (contract-creation rejection) | #34718 (`eb67d6193`) | 22 (v1.17.3 3/7) | `core/vm/eip7610.go` (new), `core/vm/evm.go`, `core/vm/interface.go`, `core/state/statedb.go`, `core/state/statedb_hooked.go`, `cmd/geth/snapshot.go` | n/a — not a fork gate; keyed on **chain ID** via `isEIP7610RejectedAccount`, which returns false for chains absent from `eip7610Accounts` | **adopt** | yes, and **provably a no-op for Bor**. Upstream drops the storage-emptiness check on contract creation (the storage root is unavailable once block-level access lists land) and replaces it with a hardcoded per-chain set of rejectable accounts. Bor's chains (137, 80002) are absent from that map. Upstream's stated invariant is that only networks adopting **EIP-158 after genesis** need an entry, because the pathological class — zero nonce, empty code, non-empty storage — is only producible by pre-EIP-158 creation semantics; **Bor mainnet and Amoy both set `EIP158Block: 0`** against Ethereum mainnet's 2,675,000, which is why upstream needs 28 addresses and Bor needs none. Residual: a Bor genesis-alloc account with storage, empty code and zero nonce would formerly have been rejected as a deployment target and now would not — reaching it needs a keccak address collision, upstream's own basis for the change. Answerable on a live db via `geth snapshot list-eip7610-eligible-accounts`, shipped in the same commit. | | EIP-8024 (DUPN, SWAPN, EXCHANGE opcodes) | boundary `a122dbe45` | 4 (v1.17.0 3/12) | `core/vm/eips.go` (activator), `core/vm/jump_table.go` | registered in the `activators` map only; **not wired into any fork instruction set**; reachable solely via `ChainConfig.ExtraEips`, which is unset on every Bor preset | **defer** — dormant, not adopted | yes — `enable8024` is never invoked by any `new*InstructionSet()` (0 refs in `jump_table.go`); no Bor preset sets `ExtraEips`, so the DUPN/SWAPN/EXCHANGE opcodes are absent from every Bor network's jump table. Not ungated. | | EIP-8024 PC-increment fix (#33361) | boundary `228933a66` | 5 (v1.17.0 4/12) | `core/vm/instructions.go`, `core/vm/instructions_test.go` | same as above — no gate change; corrects the opcode implementation only | **defer** (unchanged) | yes — re-confirmed `enable8024` still unreferenced by any instruction set and no preset sets `ExtraEips`. The fix corrects the (still-dormant) DUPN/SWAPN/EXCHANGE PC handling; it does not enable them. Auto-merged clean. | | EIP-8024 missing-immediate-byte update (#33614) | boundary `845009f68` | 10 (v1.17.0 9/12) | `core/vm/instructions.go`, `core/vm/instructions_test.go` | same as above — no gate change; spec-clarification only (missing immediate byte after DUPN/SWAPN/EXCHANGE now treated as `0x00`, matching PUSHn, instead of `ErrInvalidOpCode`) | **defer** (unchanged) | yes — re-confirmed `enable8024` registered only in the `activators` map (`eips.go:46`), never invoked by any `new*InstructionSet()`; no Bor preset sets `ExtraEips`. The opcodes stay absent from every Bor network's jump table; the behavior change is unreachable. Auto-merged clean; `TestEIP8024_Execution` (incl. new `DUPN_MISSING_IMMEDIATE`) passes. | @@ -25,7 +26,10 @@ gate-guarded (not ungated / unconditional). | Amsterdam jump-table dispatch in `LookupInstructionSet` | #33947 (`fe3a74e61`) | 16 (v1.17.2 1/4) | `core/vm/jump_table_export.go` | `case rules.IsAmsterdam → newAmsterdamInstructionSet()`, ordered below Bor's Chicago/LisovoPro/Lisovo/MadhugiriPro/Madhugiri cases and above Osaka, matching the `core/vm/evm.go` dispatch wired in batch 15 | **enable-by-block** — dormant | yes — the case is unreachable while `AmsterdamBlock` is nil; the exported lookup is the RPC/tooling twin of the `evm.go` dispatch, so both agree. | | EIP-8024 branchless normalization + extended EXCHANGE | #33869 (`814edc530`) | 16 (v1.17.2 1/4) | `core/vm/instructions.go`, `core/vm/instructions_test.go` | no gate change — updates the `enable8024` opcode implementations to the latest spec (EIPs PR 11306) | **defer** (unchanged) | yes — reachable only through `enable8024`, invoked only by `newAmsterdamInstructionSet`. | | EIP-7954 (increase maximum contract size) | #33832 (`95b9a2ed7`) | 17 (v1.17.2 2/4) | `params/protocol_params.go`, `core/vm/{common,evm,gas_table}.go`, `core/state_transition.go`, `core/txpool/validation.go`, `cmd/evm/internal/t8ntool/transaction.go` | `MaxCodeSizeAmsterdam` (32768) / `MaxInitCodeSizeAmsterdam` (65536) + `vm.CheckMaxCodeSize` / `vm.CheckMaxInitCodeSize`, both branching on `rules.IsAmsterdam` | **enable-by-block** — dormant | yes — `IsAmsterdam` false on every preset → the helpers fall through to the pre-existing `IsEIP158` (24576) / `IsShanghai` (49152) limits. **Overlaps a live Bor fork:** Bor's Ahmedabad already sets the same 32768 code cap (`MaxCodeSizePostAhmedabad`, `Bor.IsAhmedabad`, mainnet `62278656` / Amoy `11865856`), and `initNewContract` checks Ahmedabad first, so on Bor the Amsterdam code-size branch is unreachable in practice. **Enable-time decision:** Ahmedabad does *not* raise the initcode cap, so enabling Amsterdam would move initcode 49152 → 65536 — a separate consensus change needing its own N-1/N/N+1 tests and Erigon parity. Bor also keeps its assign-err-and-continue flow in `initNewContract` (deployment gas still charged) versus upstream's early return. | -| EIP-7708 (ETH transfers as logs) | #33645 (`b87340a85`) | 19 (v1.17.2 4/4) | `params/protocol_params.go`, `core/types/log.go`, `core/evm.go`, `core/state_transition.go`, `core/state/{statedb,statedb_hooked,parallel_statedb}.go`, `core/vm/{evm,instructions,interface}.go` | `EthTransferLogEvent` / `EthBurnLogEvent` topic constants + `types.EthTransferLog` / `types.EthBurnLog`; `Transfer`/`TransferFunc` gain `*params.Rules`; emission sites in `core.Transfer`, `core.EthereumTransfer`, `opSelfdestruct6780`, and `StateDB.EmitLogsForBurnAccounts` (called from `stateTransition.execute`), each behind `rules.IsAmsterdam` | **enable-by-block** — dormant | yes — `IsAmsterdam` false on every preset → no system log is ever emitted and receipts are byte-identical to pre-merge. **Three enable-time consequences, all Bor-specific.** (1) **Duplicate transfer logs:** Bor already emits its own `LogTransfer` from the 0x1010 fee address for every `core.Transfer`, so with Amsterdam active a plain value transfer produces *both* that and the EIP-7708 system log — a bloom and receipt-size change, and a product decision to take before enabling. (2) **V1/V2 log order:** the emission is deliberately placed at the same point relative to the balance change on Bor's serial path, its V2 BlockSTM fast path, and `EthereumTransfer`, because V2 defers the 0x1010 log to settlement — any other placement orders the pair differently under V1 vs V2 and diverges the receipt root. (3) **Selfdestruct depends on a declined PR:** upstream's burn/transfer branch reads `StateDB.IsNewContract` from the deferred #32919 rework; Bor instead takes the `wasNewContract` bool that its own `SelfDestruct6780` already returns. If #32919 is ever adopted, that adaptation must be revisited rather than blindly replaced. Needs N-1/N/N+1 boundary tests and Erigon parity at enable time. | +| EIP-7708 (ETH transfers as logs) | #33645 (`b87340a85`) | 19 (v1.17.2 4/4) | `params/protocol_params.go`, `core/types/log.go`, `core/evm.go`, `core/state_transition.go`, `core/state/{statedb,statedb_hooked,parallel_statedb}.go`, `core/vm/{evm,instructions,interface}.go` | `EthTransferLogEvent` / `EthBurnLogEvent` topic constants + `types.EthTransferLog` / `types.EthBurnLog`; `Transfer`/`TransferFunc` gain `*params.Rules`; emission sites in `core.Transfer`, `core.EthereumTransfer`, `opSelfdestruct6780`, and `StateDB.LogsForBurnAccounts` (called from `stateTransition.execute`), each behind `rules.IsAmsterdam` | **enable-by-block** — dormant | yes — `IsAmsterdam` false on every preset → no system log is ever emitted and receipts are byte-identical to pre-merge. **Three enable-time consequences, all Bor-specific.** (1) **Duplicate transfer logs:** Bor already emits its own `LogTransfer` from the 0x1010 fee address for every `core.Transfer`, so with Amsterdam active a plain value transfer produces *both* that and the EIP-7708 system log — a bloom and receipt-size change, and a product decision to take before enabling. (2) **V1/V2 log order:** the emission is deliberately placed at the same point relative to the balance change on Bor's serial path, its V2 BlockSTM fast path, and `EthereumTransfer`, because V2 defers the 0x1010 log to settlement — any other placement orders the pair differently under V1 vs V2 and diverges the receipt root. (3) **Selfdestruct depends on a declined PR:** upstream's burn/transfer branch reads `StateDB.IsNewContract` from the deferred #32919 rework; Bor instead takes the `wasNewContract` bool that its own `SelfDestruct6780` already returns. If #32919 is ever adopted, that adaptation must be revisited rather than blindly replaced. **Batch 21 follow-up (#34688, `21b19362c`):** upstream turned `EmitLogsForBurnAccounts()` (which added the logs itself) into `LogsForBurnAccounts() []*types.Log`, so the caller adds them and the tracer observes them in order. Bor's BlockSTM-only `ParallelStateDB` carries a hand-written mirror of that method and needed the same rename — it raised no conflict and surfaced only as an unimplemented `vm.StateDB` at build time. Both sides return an address-sorted list, which is what keeps V1 and V2 log order identical; consequence (2) above still governs. Still dormant. Needs N-1/N/N+1 boundary tests and Erigon parity at enable time. | +| EIP-7976 (increase calldata floor cost) | #34748 (`ac406c2fe`) | 23 (v1.17.3 4/7) | `params/protocol_params.go`, `core/state_transition.go`, `core/txpool/validation.go` | `params.TxCostFloorPerToken7976 = 16`; `FloorDataGas` gains a `params.Rules` parameter and branches internally on `rules.IsAmsterdam`, pricing calldata at 64/64 tokens per zero/non-zero byte instead of EIP-7623's 10/40 | **enable-by-block** — dormant | yes — `IsAmsterdam` is false on every Bor preset, so `FloorDataGas` always takes the pre-Amsterdam branch and the floor is byte-identical to pre-merge. **No new fork gate was needed**: the EIP rides the existing Amsterdam gate, which is already wired block-based-nil, so `params/config.go` is untouched by this batch and both meta-guards are unaffected. Two Bor-specific notes for enable time. (1) **Bor's txpool gate is narrower than upstream's.** Upstream guards the floor check with `rules.IsPrague`; Bor guards it with `opts.Config.IsPrague(head.Number) && opts.Config.Bor != nil`, so the check only applies on Bor chains. That gate was kept and only the `FloorDataGas` signature adopted — meaning the *floor value* becomes Amsterdam-dependent while the *decision to check it at all* stays Prague+Bor. Both conditions must be reviewed together when Amsterdam is scheduled. (2) **Raising the floor is a fee change, not just a gas-schedule change**: at 64/64 the minimum cost of calldata-heavy transactions rises, which affects data-availability-style traffic on Bor more than on L1. Treat it as an operator- and ecosystem-facing decision, not an internal one. | +| EIP-7825 tx gas cap lifted at Amsterdam | #34841 (`01036bed8`) | 24 (v1.17.3 5/7) | `core/state_transition.go`, `core/txpool/validation.go`, `eth/gasestimator/gasestimator.go`, `cmd/evm/internal/t8ntool/transaction.go`, `miner/worker.go` | EIP-7825 caps a single transaction's gas at `params.MaxTxGas`. Under Amsterdam/EIP-8037 the transaction gas limit also covers the state-gas reservoir, so applying the cap to the full `tx.Gas()` would reject otherwise-valid transactions whose *regular* gas stays within the limit. Upstream therefore stops applying the cap once Amsterdam is active | **enable-by-block** — dormant | yes — `IsAmsterdam` is false on every Bor preset, so every site keeps applying the cap exactly as before. Three Bor-specific notes for enable time. (1) **Bor's gate is wider than upstream's.** Upstream applies the cap on `IsOsaka`; Bor applies it on `IsOsaka || Bor.IsMadhugiri`, because Bor shipped EIP-7825 at Madhugiri rather than at Osaka. Every site was combined as `(isOsaka || isMadhugiri) && !isAmsterdam`, so enabling Amsterdam lifts the cap for **both** activation paths — that is the intended reading, but it means the Madhugiri path changes behaviour at the Amsterdam block too, and both conditions must be reviewed together. (2) **Five sites, one of them Bor-only.** Execution precheck, txpool stateless validation, gas estimation and t8n all conflicted and were combined; `miner/worker.go`'s `PendingFilter` construction is a Bor-only twin that raised **no conflict** and was patched by the twin scan. A sixth site, `core/txpool/legacypool/legacypool.go`, kept Bor's version — upstream's change there is only a line-wrap and comment, with no Amsterdam gate, despite what the PR description says. (3) **Lifting the cap is a policy decision, not a mechanical one**: it only makes sense together with EIP-8037's state-gas dimension, which Bor has not adopted (`vm.GasBudget.StateGas` is still unused groundwork from batch 23). Enabling Amsterdam without EIP-8037 would remove the per-transaction gas ceiling while nothing replaces it — the block gas limit would be the only remaining bound. | +| EIP-7981 (increase access list cost) | #34755 (`aaa2b6628`) | 25 (v1.17.3 6/7) | `core/state_transition.go`, `core/txpool/validation.go`, `cmd/evm/internal/t8ntool/transaction.go`, `tests/transaction_test_util.go`, `eth/tracers/parity.go` | `IntrinsicGas` gains an `isAmsterdam` parameter and `FloorDataGas` gains an `accessList` parameter. Under Amsterdam each access-list address is additionally charged `common.AddressLength * TxCostFloorPerToken7976 * TxTokenPerNonZeroByte` and each storage key the `common.HashLength` equivalent, on top of the existing `TxAccessListAddressGas` / `TxAccessListStorageKeyGas`; the floor-data-gas calculation gains the same access-list tokens. Every arithmetic step gained an explicit overflow guard returning `ErrGasUintOverflow` | **enable-by-block** — dormant | yes — `IsAmsterdam` is false on every Bor preset, so both the intrinsic-gas and floor paths take the pre-Amsterdam branch and pricing is byte-identical to pre-merge. **No new fork gate was needed**: like EIP-7976 it rides the existing Amsterdam gate, so `params/config.go`, the chain presets, the packaged genesis files and `forkid` are all untouched by this batch and both meta-guards are unaffected. Two notes for enable time. (1) **Review with EIP-7976 and the EIP-7825 cap row together** — all three are Amsterdam-gated changes to transaction pricing, and Bor's txpool gate for the floor path stays the narrower `IsPrague(head.Number) && Config.Bor != nil`, so the floor *value* becomes Amsterdam-dependent while the *decision to check* stays Prague+Bor. (2) **This is a fee increase for access-list-heavy traffic**, which is common in bots and MEV paths on Bor; treat the activation as an ecosystem-facing decision, not an internal one. | | EIP-7928 (block access lists) — storage + header field | #34064 (`dc3794e3d`), #34636 (`bcb0efd75`), #34651 (`0bafb2949`) | 20 (v1.17.3 1/7) | `core/rawdb/{schema,accessors_chain}.go` (`accessListPrefix` = `"j"`), `core/types/block.go` (`BlockAccessListHash` carried through `CopyHeader`/`WithSeal`/`WithBody`), `core/types/bal/*`, `consensus/beacon/consensus.go` | `AmsterdamBlock = nil`; the header check is on Bor's **block-based** `IsAmsterdam(header.Number)` | **enable-by-block** — dormant | yes — with `IsAmsterdam` false the verifier asserts `BlockAccessListHash == nil`, which is what every Bor header carries; the rawdb prefix is inert because nothing writes a BAL. The Amsterdam header check was **combined** at resolution: upstream added the BAL-hash arm next to the existing EIP-7843 `SlotNumber` arm, and Bor keeps both on its block-based gate so they activate together. Enable-time work: producing a BAL at all, plus N-1/N/N+1 tests and Erigon parity. | | EIP-7928 BAL serving (snap/2 protocol) | #34083 (`00da4f51f`) | 20 (v1.17.3 1/7) | `eth/protocols/snap/{handler,handlers,protocol}.go`, `core/blockchain_reader.go` | n/a — **whole feature deferred**, not merged | **defer** | yes — feature not present. snap/2 exists to *serve* block access lists, which Bor cannot produce while Amsterdam is dormant, so it buys nothing today and would cost a rewrite of Bor's diverged snap serving path (upstream moves 571 lines of `handler.go` into a new `handlers.go`). Upstream itself ships the snap/2 dispatch commented out (`//case SNAP2:`) in this commit. Revisit **with** the Amsterdam/BAL enable decision, not before. | | EIP-7975 (eth/70 partial block receipt lists) | #33153 (`965bd6b6a`) | 20 (v1.17.3 1/7) | `eth/protocols/eth/*`, `eth/downloader/*`, `cmd/devp2p/internal/ethtest/*` | n/a — wire-protocol version, not a state-transition fork; Bor now advertises `ProtocolVersions = [ETH70, ETH69, ETH68]` | **adopt** | n/a — no gate involved; eth/70 is negotiated per peer, not activated by block. **Adopted** on branch `ppatil-upstream-eth70`. The one fork-gated line it does introduce is the reduced minimum transaction gas used to bound a truncated response: upstream keys it off `AmsterdamTime`, Bor keys it off block-based `IsAmsterdam(number)` with `AmsterdamBlock` nil, so the pre-fork 21000 bound is what applies today and the Amsterdam branch is **verified dormant** by `TestValidateLastBlockReceiptAmsterdamGate`, which pins both sides of the gate. The Madhugiri-gated state-sync exclusion is untouched: it runs in the downloader queue on the reassembled list, never on a partial one. See `needs-wiring.md` for what the adoption actually required. | diff --git a/docs/upstream-merges/v1.17.4/ledger.md b/docs/upstream-merges/v1.17.4/ledger.md index 34ed9181a6..ace7c13d2e 100644 --- a/docs/upstream-merges/v1.17.4/ledger.md +++ b/docs/upstream-merges/v1.17.4/ledger.md @@ -1224,3 +1224,991 @@ behavior is exercised by unit tests over synthetic size limits, not end to end. Upstream's `cmd/devp2p/internal/ethtest` eth/70 suite and its regenerated testdata were not ported (geth-chain-specific; Bor's ethtest is already a known-failing surface). + +## v1.17.3 batch 2/7 (`c453b99a5`, plan row 21) — merged `582a825ae` — MILESTONE-CONTINUATION + +Branch `ppatil-upstream-v1.17.3-part2`, cut from the eth/70 tip `36d3268c4`. +This branch carries the rest of the milestone (batches 21–26 + chores), so +v1.17.3 spans two PRs with the eth/70 PR stacked between them. 20 upstream +commits, **12 conflicts**, 59 files, +2097/−569. + +The batch is dominated by #34691, which turns gas into a vector; 26 of the 30 +conflict hunks are that one change. + +### Consensus surface + +| Area | PR | Decision | +| ---- | -- | -------- | +| Gas becomes `` | #34691 | `Contract.Gas` is now a `GasCosts` struct and every `gasFunc` returns `GasCosts`. `StateGas` is unused — upstream calls this a pre-refactor to land EIP-8037 (batch 32) in chunks. Adopted wholesale; the change is mechanical on the regular-gas path and no gas value moves. **The risk was not the conflicts but the silent fallout** — see the twin scan below. | +| EIP-7708 burn logs, tracer hook | #34688 | `StateDB.EmitLogsForBurnAccounts()` becomes `LogsForBurnAccounts() []*types.Log`, returning the logs so the caller adds them and the tracer sees them in order. Ported the same rename onto Bor's BlockSTM-only `ParallelStateDB`, keeping its address sort so V1 and V2 log order stay identical. The call site is `rules.IsAmsterdam`-gated, so dormant. `fork-register.md`'s EIP-7708 row was updated rather than duplicated. | + +No fork gate was flipped. Amsterdam and the binary trie remain dormant; the +bintrie spec fixes (#34670, #34690, #34676) and the new offline conversion +subcommand (#33740) land inert. + +### Conflict resolutions + +Ten of the twelve conflicted files diverged from upstream **only by a blank line +before a return** — Bor carries a `wsl`-style formatting layer that upstream does +not, and `whitespace` is the only whitespace linter Bor actually enables, so +these are cosmetic. They were resolved by a script that asserts +`ours == [''] + base` per hunk and refuses to touch anything else, so a real +divergence hiding among them would have stopped the run rather than being +silently flattened. Two hunks did not match and were done by hand. + +| File | PR | Class | Decision | +| ---- | -- | ----- | -------- | +| `core/vm/{contract,interpreter,gas_table}.go` | #34691 | 1 | Blank-line pattern, 20 hunks; took upstream's `GasCosts` wrapping. | +| `core/vm/operations_acl.go` | #34691 | 3 | Six blank-line hunks plus two by hand: one where upstream introduced `gas := gasCost.RegularGas`, and one whose HEAD side had **swallowed Bor's entire `gasSLoadPIP88` function** because it shares a return line with its sibling. Reconstructed by hand — this is the append-collision hazard, and pattern-matching a previous splice would have deleted a live PIP-88 gas function. | +| `core/stateless/encoding.go` | #34683 | 2 | Combined: upstream's empty-headers guard placed before Bor's `w.context = ext.Context` assignment, so the validation runs first. | +| `eth/filters/filter.go`, `filter_test.go` | #34647 | 3 | Kept Bor's deletion. Bor enforces the range limit one layer up in `eth/filters/api.go` (`checkBlockRangeLimit`, covering `eth_getLogs` and `bor_getLogs`), per the #33163 `wontfix` row — **verified before accepting the empty HEAD side, since taking it blind would have looked like dropping a DoS guard.** Upstream's -32602 refinement has no corresponding line; recorded in `needs-wiring.md`. | +| `miner/payload_building.go` | #34704 | 2 | Kept Bor's deletion of `BuildTestingPayload`/`updateSpanForDelivery` (declined with `testing_buildBlockV1`, #33656, batch 12). The changed line (`new(big.Int)` → `res.fees`) has no home; recorded as a coverage gap alongside the existing #34094 row. | +| `accounts/keystore/account_cache_test.go` | #34084 | 1 | Took upstream's rewrite. It fixes a flaky test by polling "accounts appeared" and "notification received" independently instead of requiring both at the same instant; Bor had no divergence beyond blank lines. | +| `signer/core/apitypes/types.go` | #33702 | 1 | Blank-line pattern; took upstream's guard around the `Truncate`. | +| `cmd/devp2p/internal/v5test/framework.go` | #34043 | 1 | Four blank-line hunks plus one by hand for the `write`/`read` → `writeTo`/`readFrom` conversion. | +| `cmd/devp2p/internal/v5test/discv5tests.go` | #34043 | 1 | Took upstream's whole file. Confirmed first that Bor's divergence was blank-line-only (a name-only diff of the file between the merge base and HEAD yields no non-blank lines), so `--theirs` discarded nothing semantic. | + +### Bor-only twin scan (§4.6) — four findings, none of which conflicted + +This is the batch the step was written for. #34691 changed a type that Bor mirrors +in four places, and **not one of them raised a conflict**; every one surfaced at +build or vet time. + +1. **`makeGasSStoreFuncPIP88`** — the PIP-88 SSTORE twin. Its sibling + `makeGasSStoreFunc` auto-merged to `GasCosts` while the twin kept + `(uint64, error)`. Mirrored exactly what upstream did to the sibling: + signature, the two guard returns, and the four value returns. Nothing more — + no guard the sibling lacks. +2. **`gasSLoadPIP88`** — same treatment. This one did appear inside a conflict, + but only because it shares a return line with its sibling; the signature + change itself was invisible to the merge. +3. **`core/vm/interpreter_dispatch.go`** — a **generated** file (`gen_dispatch`) + whose `runSwitch` mirrors upstream's `Run()` loop, including its gas + deduction. Fixed the generator (`core/vm/gen_dispatch/main.go`) and + regenerated, per that package's own documented upstream-merge SOP; never + hand-edited the output. **This is a twin class the step does not yet name: a + code generator whose emitted output mirrors an upstream function.** +4. **`ParallelStateDB.EmitLogsForBurnAccounts`** — Bor's BlockSTM mirror of + `StateDB`, broken by #34688's interface rename. + +Both PIP-88 twins were then verified back in lockstep with a comment-stripped, +constant-normalised diff of the two function bodies (exit 0). + +Two further signature dependencies in Bor-only code, same silent class: +`cmd/geth/bintrie_convert.go` (a **new upstream file** calling Bor's diverged +`utils.MakeChainDatabase`, which carries an extra `disableFreeze` parameter) and +the Bor-only pathdb test `TestLookupZeroBaseRootFallback` (upstream added a +`stateReservation` parameter to `newBuffer`). Plus the PIP-88 gas tests, which +compare against `gas.RegularGas` now. + +### Verification + +`go build ./...`, `go vet ./...`, `gofmt` clean apart from the two documented +`//nolint` copylocks; `go mod tidy` a no-op. The generator's mandatory +differential suite (`TestDispatch|TestPreShanghai|TestStackOverflow|TestDefaultFallback|TestInterrupt|TestAbort`) +passes — it runs every case through both `runSwitch` and the standard +interpreter and asserts identical gas, return data, errors and logs, which is +the real evidence that the regenerated gas flush is correct. + +Green: `core` (170 s), `core/vm/{program,runtime}`, `core/state`, +`core/state/snapshot`, `core/stateless`, `core/filtermaps`, `eth/filters`, +`miner` (196 s), `accounts/keystore`, `signer/core`, `signer/core/apitypes`, +`triedb`, `triedb/pathdb`, `trie`, `trie/bintrie`, `trie/trienode`, all +`eth/tracers/...`. Both fork meta-guards pass +(`TestReinforceMultiClientPreCompilesTest`, `TestV2ForkParity`), as do all +`TestPIP88*`. + +Pre-existing failures, re-baselined in a detached worktree at `36d3268c4` rather +than assumed: `core/vm`'s `TestInterruptDuringExecution` and +`TestAbortDuringJump` fail identically before the merge, with the same +non-deterministic subtest pattern. Worth stating explicitly because this batch +edits the dispatch generator those two tests cover, so "known flaky" was not +good enough on its own. `cmd/geth` and `cmd/devp2p/internal/ethtest` were +re-baselined the same way. + +## v1.17.3 batch 3/7 (`5af5510b1`, plan row 22) — merged `e15a2b63b` + +Branch `ppatil-upstream-v1.17.3-part2`. 20 upstream commits, **34 conflicts**, of +which **31 came from a single commit that was deferred**, leaving 38 files and ++999/−174 in the batch itself. + +### The batch's one decision: #34700 deferred + +`ba215fd92` (#34700) splits `CachingDB` into `MerkleDB` + `UBTDB`, adds a +`DatabaseType`, and renames the Verkle vocabulary to UBT (`VerkleTime`→`UBTTime`, +`IsVerkle`→`IsUBT`, `BlobScheduleConfig.Verkle`→`.UBT`). Upstream states it is +prerequisite groundwork for #34004, the UBT state transition. + +**Deferred, and scheduled for immediate adoption on its own branch** — the same +pattern as eth/70 and the `core/vm` catch-up, chosen deliberately rather than as +a permanent decline. Reasoning: + +- **#34004 is not in this sync's range at all.** The only reference to it anywhere + in the fetched upstream history is #34700's own commit message, so the feature + the groundwork serves is out of scope here. +- **Zero functional value to Bor today.** Verkle/UBT is dormant (`VerkleBlock` + nil on every preset) and go-verkle was removed in batch 6. +- **But declining it permanently would be costly**, because it renames the central + type of `core/state` and **the sequel lands in the very next batch**: #34763 + (`7e388fd09`, batch 23) applies the same MPT/UBT split to `core/state/reader.go`, + a file that carries Bor's prefetch-attribution instrumentation. #34843 follows in + batch 30. Declining the family would fork `core/state` on its primary type name + for the rest of the sync — the compounding-divergence trap already three deep on + the eth-protocol surface. +- **Adoption is tractable and belongs in its own review**, not inside a 20-commit + merge: every Bor divergence (`Snapshot()`, the removal of `Commit` in favour of + `CommitWithUpdate`, the `Iteratee()` impls, the prefetch instrumentation) lands on + `MerkleDB`, because Bor only ever runs MPT; `UBTDB` becomes dormant code Bor never + constructs, exactly like the bintrie it already carries. + +Reverted the full 67-file footprint to Bor HEAD, `git rm`'d the two new files +(`core/state/database_mpt.go`, `database_ubt.go`) and kept Bor's deletion of +`core/bintrie_witness_test.go` (the batch-2 coverage-gap row). + +**The revert was not applied blindly.** Nine files in that footprint are also +touched by *other* in-range commits, so a wholesale revert would have silently +discarded adopted work — the `core/blockchain_reader.go` mistake from batch 20. +Each was reverted and then had the other commits' changes re-applied in upstream +chronological order: `cmd/utils/flags.go` (#34729), `core/state/database.go` +(#34758), `core/state/state_object.go` (#34723), `core/state/statedb.go` (#34723, +#34718), `core/state/statedb_test.go` (#33695, #34718, #34758), +`core/state/trie_prefetcher_test.go` (#34718), `core/vm/evm.go` (#34718), +`tests/state_test_util.go` (#34750), `triedb/pathdb/reader.go` (#34762). + +### Consensus surface: EIP-7610 reworked (#34718) — adopted, provable no-op + +`eb67d6193` removes the storage-emptiness check from contract creation and +replaces it with a hardcoded per-chain set of accounts eligible for rejection +(`core/vm/eip7610.go`, new). Upstream's motivation is that with block-level access +lists the storage root is no longer available at creation time. + +This is a change to contract-creation rejection semantics, so it needs a real +argument rather than a shrug. **It is a no-op for Bor, provably:** + +- `isEIP7610RejectedAccount` is keyed on chain ID and returns false for chains + absent from the map. Bor's chains (137, 80002) are absent. +- Upstream's own invariant is that *only networks which adopted EIP-158 after + genesis* need an entry, because the pathological account class — zero nonce, + empty code, non-empty storage — can only be produced by pre-EIP-158 creation + semantics. **Bor mainnet and Amoy both have `EIP158Block: 0`**, versus Ethereum + mainnet's 2,675,000, which is exactly why upstream needs its 28-address list and + Bor needs none. + +Residual, recorded rather than hand-waved: a Bor genesis-alloc account with +storage, empty code and zero nonce would previously have been rejected as a +deployment target and now would not. Reaching it requires a keccak address +collision, which is upstream's own stated basis for the change. Upstream also +shipped `geth snapshot list-eip7610-eligible-accounts` in the same commit, so the +question is answerable against a live Bor database if we ever want certainty. + +### Other resolutions + +| File | PR | Class | Decision | +| ---- | -- | ----- | -------- | +| `params/config.go` (16 hunks) | #34700 | 3 (hardfork surface) | **Kept Bor's side throughout.** Bor replaced timestamp fork scheduling with block-based fields and deleted the timestamp fields, so upstream's `VerkleTime`→`UBTTime` rename targets fields Bor does not have. Verified the upstream diff for this file contains *only* that rename, so take-ours discards nothing. `VerkleBlock` and `IsVerkle(num)` stay per the fork-wiring principle; `BlobScheduleConfig.Verkle` likewise. | +| `core/state/transient_storage.go` | #33695 | 1 | Took upstream's flattening of `map[Address]Storage` into `map[transientStorageKey]Hash`. Confirmed first that Bor's divergence in the file was blank-line-only, and that Bor's only consumers (`ParallelStateDB` via `newTransientStorage`/`Get`/`Set`/`clear`) are unaffected by the representation change. | +| `core/state/statedb_test.go` | #33695 | 2 | Bor's own `EqualTS` test helper existed solely to compare the nested map and would no longer compile; removed in favour of upstream's `maps.Equal`. A Bor-only helper broken by an upstream type change — the twin-scan class. | +| `core/state/statedb_test.go` | #34758 | 2 | Ported upstream's new UBT copy test as `TestStateDBCopyBinaryTrie`, reaching the binary tree via Bor's `triedb.VerkleDefaults` (upstream renamed it `UBTDefaults` in the deferred #34700). It guards the `mustCopyTrie` bintrie case adopted from the same commit, and passes. | +| `core/history/historymode.go` | #34714 | 2 | Added the hoodi Prague prune point to Bor's `PraguePrunePoints`. Bor restructured this file into two top-level maps instead of upstream's mode-keyed map, and retains `HoodiGenesisHash`, so the data ports cleanly. Inert for Bor's own networks. | +| `eth/catalyst/api_testing.go` | #34722 | 3 (DU) | Kept Bor's deletion — the file went with `testing_buildBlockV1` (#33656, batch 12). **Third orphaned change on that declined feature** (after #34094 and #34704); recorded again rather than left implicit. | +| `eth/tracers/.../eip7702_deauth.json` | #34675 | 2 | Adopted the prestate codehash fix but **removed its new fixture.** The fixture configures forks by timestamp (`cancunTime`/`pragueTime`), which Bor's block-based `ChainConfig` silently drops on unmarshal, leaving no blob-capable fork; its non-nil `excessBlobGas` then panics `CalcBlobFee` with "calculating blob fee on unsupported fork". Same root cause as batch 20's `tests/init.go`, and it predicts every future upstream fixture that pairs timestamp forks with blobs. | +| `cmd/geth/snapshot.go` | #34718 | 2 | New upstream file calling Bor's `MakeChainDatabase`, which carries an extra `disableFreeze` parameter. **Second occurrence of this exact pattern** after batch 21's `bintrie_convert.go`; no conflict either time, caught at build. | + +### Bor-only twin scan (§4.6) + +- PIP-88 twins verified in lockstep (comment-stripped, constant-normalised body + diff, exit 0). This batch does not touch `core/vm` gas code at all — its + `core/vm` footprint is the EIP-7610 rework plus the interface change. +- Two signature-dependency hits, both surfaced at build rather than as conflicts: + Bor's `EqualTS` helper (broken by the transient-storage flattening) and + `cmd/geth/snapshot.go` (Bor's extra `MakeChainDatabase` parameter). The latter is + now a **predictable, recurring class**: every new upstream caller of + `MakeChainDatabase` will break until Bor's signature converges. Worth a standing + watch item rather than rediscovering it each batch. + +### Verification + +`go build ./...`, `go vet ./...`, `gofmt`, `go mod tidy` and **`make lint` (0 +issues)** all clean, apart from the two documented `//nolint` copylocks. Both fork +meta-guards pass. + +Green: `core` (172.9 s), `eth` (47.5 s), `core/state`, `core/vm/{program,runtime}`, +`core/rawdb`, `core/rawdb/eradb`, `core/types`, `core/types/bal`, `core/history`, +`triedb`, `triedb/pathdb`, `trie`, `trie/bintrie`, `trie/trienode`, all +`eth/tracers/...`, `cmd/utils`, `log`, `tests`. + +Pre-existing failures, **re-baselined in a detached worktree at `9a7efacaa`** +rather than assumed, because this batch touches both packages: +`cmd/evm`'s `TestT8n`/`TestEVMTracing`/`TestEvmRun`/`TestEvmRunRegEx` fail +identically before the merge (the recorded t8n golden drift), and `core/vm`'s +`TestInterruptDuringExecution`/`TestAbortDuringJump` fail there too under repeats, +with the same non-deterministic subtest pattern. + +## v1.17.3 out-of-band adoption — `CachingDB` split (#34700, `ba215fd92`) — committed `b0ea85ca0` + +Branch `ppatil-upstream-mptubt`, cut from `e15a2b63b` (batch 22 tip). Deferred out +of batch 22 (31 of its 34 conflicts) and adopted here before batch 23, whose +#34763 applies the same split to `core/state/reader.go`. 35 files. + +Upstream splits `CachingDB` into `MPTDatabase` + `UBTDatabase`, adds a +`DatabaseType` (`TypeMPT` / `TypeUBT`) with a `Type()` method on the `Database` +interface, and renames the Verkle vocabulary to UBT. It is groundwork for #34004, +the UBT state transition, which is not in this sync's range. + +### The scope line: type split adopted, fork-boundary plumbing declined + +#34700 does two separable things. Only the first is taken. + +1. **The type split** — two implementations of `state.Database`, selected by the + trie type, plus the `IsVerkle()` → `IsUBT()` / `IsVerkle` → `IsUBT` renames + down through `trie` and `triedb`. This is where the future-merge value is: + `core/state` stops forking from upstream on the name of its primary type, and + #34763 / #34843 land against a split tree. +2. **Runtime fork-boundary selection** — upstream rewrites `BlockChain.StateAt` + to take a `*types.Header` instead of a root, adds `StateAtForkBoundary`, makes + `HistoricState` refuse UBT, and has `ProcessBlock` pick MPT or UBT per block + via `chainConfig.IsUBT(number, time)`. **Declined**: it is keyed on the + timestamp fork fields Bor deleted in favour of block-based ones, it ripples + `StateAt`'s signature through every caller across `eth`, `internal`, `miner` + and the tracers, and it buys Bor nothing while `VerkleBlock` is nil on every + preset. Recorded in `needs-wiring.md`. + +The same reasoning excludes upstream's `params/config.go` rename (already +declined in batch 22 — see that section), and `ChainOverrides.OverrideVerkle` → +`OverrideUBT`, which would rename the operator-facing `--override.verkle` flag +and its `ethconfig` TOML key for a dormant fork. + +**Rule applied, so the boundary is not ad hoc:** rename identifiers whose type or +value is part of the split (the state database, the trie interface, the triedb +config flag); keep identifiers that mean *the Verkle hardfork* — Bor's fork +register, chain presets, CLI flags and `params` surface all still call that fork +Verkle, and renaming them is a hardfork-surface and operator-facing change with +no conflict payoff, because the sequels touch `core/state`, not `params`. The +visible consequence is a few mixed-vocabulary lines, e.g. `EnableUBTAtGenesis` +returning `genesis.Config.EnableVerkleAtGenesis`, and `tests/block_test_util.go` +setting `IsUBT: gspec.Config.IsVerkleGenesis()`. That is the boundary showing +through, not an oversight. + +### Where Bor's divergences landed + +Every Bor-only member of `CachingDB` moved onto `MPTDatabase`, because Bor only +ever runs MPT and `UBTDatabase` is dormant code Bor never constructs outside +tooling and tests: `DisableSnapInReader` / `EnableSnapInReader` and the +`useSnapInReader` guard, `ReaderTrieOnly`, `ReadersWithCacheStats` and +`ReadersWithCacheStatsTriple` (returning Bor's `ReaderWithStats`, not upstream's +`Reader`), `ContractCodeWithPrefix`, and `Snapshot()` — which Bor carries on the +`Database` interface where upstream does not. `UBTDatabase` implements +`Snapshot()` as `return nil`: a unified binary trie has no snapshot layer. + +Two constructor deviations from upstream, both forced and both pre-existing: + +- Bor has no `CodeDB`. Upstream's constructors take `(triedb, codedb)`; Bor's take + `(triedb, snap)` for MPT and `(triedb)` for UBT, and each owns the inline + `codeCache` / `codeSizeCache` pair that Bor's `newCachingCodeReader` needs. +- Upstream's `NewDatabaseForTesting` returns the `Database` interface. Bor's + returns `*MPTDatabase`, because `core/state/reader_test.go` calls + `ReadersWithCacheStats`, which is Bor-only and therefore not on the interface. + +`NewDatabase` becomes upstream's deprecated dispatcher, returning `Database` and +picking the implementation from `tdb.IsUBT()`. All ~60 Bor call sites pass the +result straight to `state.New`, so only three needed the concrete constructor: +`core/blockchain.go`, `core/blockchain_sethead_test.go` and +`core/state/reader_test.go`. + +Bor's footprint is 35 files against upstream's 67, because Bor's callers already +held `state.Database` rather than `*state.CachingDB` — the concrete type appeared +in exactly one production declaration (`BlockChain.statedb`). + +### Behaviour-preserving check on `OpenTrie` + +Bor's `CachingDB.OpenTrie` consulted the overlay transition state when the triedb +was verkle: panic if `InTransition()`, `BinaryTrie` if `Transitioned()`, else fall +through to a `StateTrie`. The split drops that: `MPTDatabase.OpenTrie` always +opens a `StateTrie`, `UBTDatabase.OpenTrie` always a `BinaryTrie`. + +Verified this is not a behaviour change on the dispatcher path rather than +assuming it: `overlay.LoadTransitionState(db, root, isVerkle)` with no stored +state returns `&TransitionState{Ended: isVerkle}`, so for a UBT triedb +`Transitioned()` is true and the old code already returned a `BinaryTrie`. +`OpenStorageTrie` likewise returned `self`, which is what `UBTDatabase` does. + +The one place the narrowing is real is `core/blockchain.go`, which now builds +`NewMPTDatabase` unconditionally where it previously built the dual-mode +`CachingDB`. That only matters for a hand-written genesis setting +`enableVerkleAtGenesis: true` — a configuration no Bor preset uses and no test +drives through `NewBlockChain` — and it is the same gap as the declined +fork-boundary plumbing, so it is recorded in that `needs-wiring.md` row rather +than papered over with invented dispatch. + +### Dead field removed + +`CachingDB.TransitionStatePerRoot` (an `lru.Cache` of `*overlay.TransitionState`, +sized 1000) was declared and initialised and **never read** — the only two +references in the tree were its declaration and its initialiser. Upstream's +`MPTDatabase` has no equivalent. Dropped rather than carried into a newly written +file, where it would have pulled the `overlay` import in for nothing. + +### Verification + +`go build ./...`, `go vet ./...`, `gofmt -l`, `go mod tidy` clean apart from the +two documented `//nolint` copylocks. **`make lint` (golangci-lint v2.11.4): 0 +issues.** **`tests/bor`: 622.3 s, exit 0.** Both fork meta-guards pass — no `Rules` +field name moved, since the `params` rename is declined. + +Green: `core` (171.0 s), `eth` (46.8 s), `core/state`, `core/state/snapshot`, +`core/types`, `core/types/bal`, `trie`, `trie/bintrie`, `trie/trienode`, `triedb`, +`triedb/pathdb` (44.5 s), all `eth/...` including every tracer package, `tests`, +`internal/ethapi`, `miner` (199.0 s), all `consensus/...` including +`consensus/bor` (41.7 s), `cmd/utils`. + +Pre-existing failures **re-baselined in a detached worktree at `e15a2b63b`**, +because this change touches both packages: `cmd/evm`'s `TestT8n` / +`TestEVMTracing` / `TestEvmRun` / `TestEvmRunRegEx` and `cmd/geth`'s +`TestConsoleWelcome` / `TestCustomGenesis` / `TestCustomBackend` / `TestExport` +all fail identically before the change. `TestCustomGenesis` was worth confirming +specifically, since this change renames `Genesis.IsVerkle` and `hashAlloc`'s +parameter. + +`UBTDatabase` is exercised, not merely compiled: `TestVerklePrefetcher` and +`TestStateDBCopyBinaryTrie` both open a triedb with `triedb.UBTDefaults`, so the +`NewDatabase` dispatcher hands them a `UBTDatabase` and they cover `OpenTrie`'s +binary branch, `OpenStorageTrie` returning self, the prefetcher's `isUBT` path and +the `AccessEvents` allocation in `NewWithReader`. + +Bor-only twin scan: `ParallelStateDB` carries no verkle/UBT branch and does not +mirror `IntermediateRoot` or `handleDestruction`, so the five `statedb.go` +conversions to `Type().Is(...)` have no V2 counterpart. No Bor-only implementor of +`state.Database` or `state.Trie` exists beyond `HistoricDB` (given `Type()` +returning `TypeMPT`, matching upstream) and the three trie types. + +## v1.17.3 batch 4/7 (`33c1bd59f`, plan row 23) — merged `7b7b9f352` + +Branch `ppatil-upstream-v1.17.3-part3`, cut from `b0ea85ca0` (the #34700 adoption +tip). 20 upstream commits, **37 conflicts** across 12 upstream PRs — the largest +conflict count of the sync so far, and the only batch carrying three separate +refactors. + +### Standing check: witness logic untouched + +Operator-directed, sync-wide (see `plan.md` risk flags). Verified rather than +asserted, because two of this batch's commits land in files that hold witness +code: + +- #34724's footprint contains **no** `core/stateless/` or `eth/protocols/wit/` + file, and nothing in Bor's witness path consumes `stateUpdate`. +- `core/state/reader.go` holds `(r *trieReader) CollectStateWitness`, and #34763 + renames that type. After resolution the method body is **byte-identical to + `b0ea85ca0` apart from the receiver name**, and Bor's concurrent-reader + machinery is unchanged by occurrence count (`accountCache` 11, `storageCache` + 16, `concurrentEnabled` 4, `subTrieConcurrent` 3, `EnableConcurrentReads` 5, + `subTrieFor` 3, `resolveSubRoot` 4 — identical before and after). +- The three `case *trieReader:` sites in `statedb.go` (witness collection, + storage-cache finder, concurrent-reads enabler) took a **type-name-only** + update. One consequence is recorded rather than fixed — see the `ubtTrieReader` + row in `needs-wiring.md`. + +### Consensus surface: #34726 `FinalizeAndAssemble` removal — DECLINED + +`d422ab39d` removes `FinalizeAndAssemble` from the `consensus.Engine` interface, +deletes it from beacon/clique/ethash, and replaces it with a consensus-agnostic +`core.AssembleBlock(engine, chain, header, state, body, receipts) *types.Block` +that calls `Finalize`, sets the root and builds the block. Upstream's stated +premise is that block assembly is consensus-agnostic. + +**That premise does not hold for Bor, so the removal is declined.** +`(*Bor).finalizeAndAssemble` does three things upstream's helper cannot express: + +- it calls `commitSprintWork` at sprint start, which performs the **state-sync + commits** and returns `stateSyncData`, so finalization *produces receipts* that + must land in the block. Upstream's helper takes `receipts` as an input and + returns only a block; +- it calls `changeContractCodeIfNeeded`; +- it returns `(*types.Block, []*types.Receipt, time.Duration, error)` — the extra + receipts plus a duration Bor feeds to metrics. + +Bor also carries a second entry point, `FinalizeAndAssembleForSimulation`, used by +`eth_simulateV1` to skip the sprint-start span and state-sync commits, since a +simulated block's parent may be a phantom header. There is nowhere for that to go +in upstream's model. + +Kept Bor's method on the interface and kept every implementation. The two failure +modes of a partial decline both materialised and were caught: + +- **Orphaned helper.** `core.AssembleBlock` auto-merged into + `core/state_processor.go` *outside any conflict*. With the removal declined it + has no caller and referenced a `consensus` import Bor does not take; removed, + along with the then-unused `trie` import. +- **Append-collision in `core/chain_makers.go`.** Upstream's new withdrawal + normalisation plus its `AssembleBlock` call landed *after* Bor's retained call, + producing two `block` assignments — and used `config.IsShanghai(num, time)`, + which Bor does not have (block-based forks). Removed; Bor's engine already + rejects withdrawals with `ErrUnexpectedWithdrawals`. +- Also restored `consensus/ethash`'s `core/state` import, dropped by taking ours + on its import hunk. + +`miner/worker.go` needed no upstream content at all: all four conflicts had an +empty ours side, because Bor's worker is a different structure (`w *worker`, +BlockSTM, no telemetry spans) and keeps assembly in the engine. + +### `core/state`: #34724 exports `StateUpdate` — adopted, byte-equivalent + +`acbf699c3` is titled "export StateUpdate struct" but also **changes the in-memory +representation**: `Accounts` moves from slim-RLP `[]byte` to `*types.StateAccount`, +storage from prefix-zero-trimmed RLP to `common.Hash`, `rawStorageKey bool` becomes +a typed `StorageKeyEncoding`, and `stateSet()` is replaced by `EncodeMPTState()` / +`EncodeUBTState()` so encoding happens at the consumer. + +Adopted, on the strength of two checks made before touching consensus-critical +code: + +- **Byte-equivalence for MPT.** `EncodeMPTState` emits exactly + `types.SlimAccountRLP` and the same trimmed-leading-zero slot RLP that Bor + produced at construction time. Same bytes reach the snapshot tree and the + pathdb state set, so the state root and history are unchanged; only the moment + of encoding moves. +- **The two-origin-map design is upstream's, not Bor's.** + `StoragesOriginByKey`/`StoragesOriginByHash` exist on upstream's `AccountUpdate` + too, and the enum is the old boolean typed. Nothing is lost. + +Bor's divergence in this file is narrower than the line count suggests (192 lines +against upstream's 369): it is confined to `contractCode` lacking +`OriginHash`/`Duplicate`/`OriginBlob` and the two functions Bor declined, +`deriveCodeFields` and `ToTracingUpdate` (the live-tracer state-update hook). The +new file is upstream's minus exactly those, so the decline stays coherent — +confirmed by `state_object.go`'s second conflict, where Bor's side was already +empty where the base set `op.code.originHash`. + +The substantive port is `StateDB.commitAndFlush`, which in Bor calls +`triedb.Update` directly (Bor replaced upstream's `Database.Commit` with +`CommitWithUpdate`). It now calls `EncodeMPTState()` **once** and feeds both +consumers that previously took pre-encoded maps — `snap.Update(...)` and the +`triedb.StateSet` — which is what preserves the byte-equivalence above. +`commitAndFlush` keeps Bor's parameter list (no `deriveCodeFields` flag). + +`state_sizer.go` kept **Bor's algorithm** with field renames only: upstream's new +`calSizeStats` skips duplicate code via `code.Duplicate`, a field Bor declined, so +Bor retains its documented code-dedup size inaccuracy rather than silently +gaining a half-ported dedup. Recorded in `needs-wiring.md`. + +### `core/state/reader.go`: #34763 mptReader/ubtReader — the #34700 payoff + +`7e388fd09` splits `trieReader` into `mptTrieReader` + a new `ubtTrieReader`. This +is why #34700 was adopted first, and the benefit was visible in the merge output: +the `newMPTTrieReader` / `newUBTTrieReader` constructors **auto-merged cleanly into +`database_mpt.go` and `database_ubt.go`**. Against an unsplit tree those would have +been further conflicts inside a single `CachingDB`. + +Bor's `trieReader` is heavily diverged (`sync.Map` account/storage caches, +`concurrentEnabled`, `subTrieConcurrent`/`subTrieFor`/`resolveSubRoot` — the +BlockSTM V2 parallel-read work), so rather than picking sides on a 4-conflict file +all four were resolved to ours, upstream's `ubtTrieReader` was inserted as a +**verbatim block lifted from the boundary version**, and the rename applied +mechanically. Taking ours on the last conflict also avoided an append-collision: +upstream's side began with the tail of *its* `Storage` method, which would have +duplicated Bor's own tail. + +`MPTDatabase.ReaderTrieOnly` (Bor-only) still called `newTrieReader` and was caught +at build — no conflict. + +### Fork/EIP scan + +`params/config.go` is **untouched**: no new fork gate, no new `Rules` field, so +both meta-guards are unaffected (and pass). The batch's one new EIP rides an +existing gate: + +- **EIP-7976 (#34748), calldata floor cost 10/40 → 64/64.** Adds + `params.TxCostFloorPerToken7976 = 16`, consumed by `FloorDataGas(rules, data)` + under `rules.IsAmsterdam`. Amsterdam is dormant on every Bor preset, so the EIP + is inert and needs no new block-based wiring. In `core/txpool/validation.go` the + resolution combined **Bor's** gate (`IsPrague(head.Number) && Config.Bor != nil`, + the EIP-7623 row) with upstream's rules-aware signature. +- **#34712 gas budget** introduces `vm.GasBudget` (`RegularGas`/`StateGas`) with + `Charge`/`Refund`/`Exhaust`/`Used`. No fork gating; `StateGas` remains unused + groundwork for EIP-8037 in batch 32. + +### Other resolutions + +| File | PR | Class | Decision | +| ---- | -- | ----- | -------- | +| `core/vm/contract.go` | #34712 | 1 | **Took theirs.** Bor's side held `c.Gas.RegularGas -= gas`, a stale duplicate deduction: the auto-merged `Charge()` already deducts and `gas` is no longer in scope. Keeping ours would not have compiled. | +| `core/vm/interface.go` | #34776 | 1 | Combined: upstream's `Finalise(bool) *bal.StateAccessList` plus Bor's `Inner() *state.StateDB`. | +| `core/vm/evm.go` (4 hunks) | #34776 | 2 | Kept ours at all four sites — Bor routes precompiles through its own `runPrecompile` (ecrecover cache). The work was adapting that Bor-only helper and `runEcrecoverWithCache`, which raised no conflict: `uint64`→`GasBudget`, thread `evm.chainRules`, and mirror the sibling's `Charge`/`Exhaust` and `Exist`→`Touch`. Checked the base version of `RunPrecompiledContract` first so exactly three deltas were mirrored, no more. | +| `core/state/state_object.go` | #34776 | 1 | Combined upstream's `stateReadList.AddState` with Bor's `storageMutex`. **`bal.StateAccessList` is an unsynchronised map and Bor reads stateObjects concurrently under V2**, so this is only safe because the list is allocated exclusively under `rules.IsAmsterdam` (dormant), leaving `AddState` on its nil-receiver guard. Recorded. | +| `core/state/statedb.go` | #34776 | 1 | Combined Bor's blockstm path constants with upstream's new `Touch`; added `AddAccount` before Bor's `MVRead` wrapper rather than inside the closure, where `s` shadows the receiver. | +| `core/state/statedb.go` | #34780 | 1 | One-liner, auto-merged: `crypto.Keccak256Hash(addr.Bytes())` → `prevObj.addrHash()`. No interaction with Bor's address-cache preload work despite the similar name. | +| `crypto/kzg4844/kzg4844_test.go` (7 hunks) | #34766 | 4 (converge) | **Took theirs throughout.** Upstream's new `switchBackend(t, ckzg)` helper already contains Bor's `ckzgAvailable` skip and `t.Helper()`, so Bor's divergence disappears. | +| `cmd/utils/flags.go`, `internal/telemetry/tracesetup/setup.go`, `go.mod`/`go.sum` | #33941 | 3 (DU) | Kept Bor's deletions. **Bor has no `internal/telemetry` tree at all** — the OpenTelemetry feature was declined earlier in the sync — so #33941's gRPC OTLP transport is an orphan on that decline. Verified first that neither `RPCTelemetry*` nor `EraFormatFlag` appears anywhere else in `flags.go`, so the empty ours side is intentional, not a swallowed hunk. | +| `accounts/usbwallet/{hub,wallet}.go` | #34784 | 1 | **Adopted** upstream's `karalabe/hid` → `ethereum/hid` move. It is a freebsd build fix, and batch 22 landed the freebsd CI job (#34078), so declining would leave that new job broken. Bor keeps hid in its own import group, which is why the conflict showed an empty ours side. | +| `.github/workflows/go.yml` | #34742 | 3 (DU) | Kept Bor's deletion (removed in Bor's own #1723). | +| `core/bintrie_witness_test.go` | #34712 | 3 (DU) | Kept Bor's deletion — the batch-2 coverage-gap row. | + +### Bor-only twin scan (§4.6) + +PIP-88 gas twins verified in lockstep (comment-stripped, constant-normalised body +diff): `makeGasSStoreFunc` ↔ `makeGasSStoreFuncPIP88` and `gasSLoadEIP2929` ↔ +`gasSLoadPIP88`. `go generate ./core/vm/` produced a **byte-identical** +`interpreter_dispatch.go`, so unlike batch 21 the generated dispatch needed no +change. + +Four Bor-only dependents surfaced only at build or via Bor's own guard tests, none +as conflicts: + +- **`ParallelStateDB.Finalise`** returned nothing and stopped satisfying + `vm.StateDB`. Caught by Bor's own compile-time assertion in + `core/vm/statedb_impl_test.go`. Now returns `*bal.StateAccessList` (nil — V2 does + not build BALs; the serial StateDB it settles onto accumulates them), plus a new + `Touch` implemented via `Exist`, the V2 read-registration equivalent. +- **`consensus/bor/statefull/processor.go`** — Bor's system-contract / state-sync + call path on `uint64` gas; adapted to `vm.NewGasBudget(msg.Gas())` and + `gasLeft.Used(...)`. +- **`eth/tracers/parity.go`** — `core.IntrinsicGas` now returns `GasCosts`; the + same Bor-only Parity tracer that batch 20 broke via the `reexec` removal. +- **`MPTDatabase.ReaderTrieOnly`** — stale `newTrieReader` call. + +Plus Bor-only test call sites adapted to `GasBudget` in `core/vm` +(`dispatch_test.go`, `dispatch_bench_test.go`, `evm_precompile_cache_test.go`, +`contracts_test.go`), all found by compiling the test binary rather than one vet +run at a time. + +### Verification + +`go build ./...`, `go vet ./...`, `gofmt -l`, `go mod tidy` clean apart from the +two documented `//nolint` copylocks. **`make lint` (golangci-lint v2.11.4): 0 +issues.** **`tests/bor`: 620.3 s, exit 0.** Both fork meta-guards pass. + +Green: 41 packages across `core`, `eth/...`, `internal/...`, `tests`, `trie/...`, +`triedb/...`, `core/types/...`; plus `core/state/...`, `core/vm/{program,runtime}`, +`core/txpool/...`, all `consensus/...` including `consensus/bor` (44.8 s) and +`consensus/bor/statefull`, `crypto/kzg4844`, `miner` (201.7 s), `cmd/utils`. + +**Two pre-existing failure sets re-baselined, and one of them re-characterised:** + +- `core/vm`'s `TestInterruptDuringExecution` / `TestAbortDuringJump` failed on the + first full run with a *gas* mismatch (`fast: out of gas`, `slow: `), which + did not match the recorded "load-dependent flake" description, so it was treated + as a suspected regression. A single baseline run passed — which proves nothing — + so both revisions were run repeatedly: **1 failure in 5 on the merge, 1 failure + in 6 at `b0ea85ca0`, same failure text.** Pre-existing. The mechanism is now + understood and worth recording: both tests run an infinite JUMP loop with 10M + gas and fire the abort after a 5 ms sleep, so the abort races gas exhaustion of + a ~1M-iteration loop; on the switch-dispatch path the loop sometimes finishes + first and reports out-of-gas. It is a racy test, not a load-dependent one. +- `cmd/evm`'s `TestT8n` / `TestEVMTracing` / `TestEvmRun` / `TestEvmRunRegEx`: + because this batch changes gas accounting and t8n golden output contains gas + fields, name-matching was not enough. Captured the full failure text at both + revisions and diffed: **the only differences are temp-directory names, timings + and allocation counts — no gas field differs.** `cmd/geth`'s four and + `cmd/devp2p/internal/ethtest` also match the recorded set. + +## v1.17.3 batch 5/7 (`41b856d47`, plan row 24) — merged `9c16c4244` + +20 upstream first-parent commits, **29 conflicts** across 9 upstream PRs, 73 files. +Three clusters dominate: the `core/vm` stack arena (#33960, 11 files), the +post-Amsterdam gas-cap skip (#34841, 6 files), and the t8n alloc streaming plus +binary-trie serialization pair (#34785 / #34794, 4 files). + +### Standing check: witness logic untouched + +Two of this batch's commits land in the downloader, which is a witness-adjacent +surface, so this was verified rather than asserted. + +`75a64ee34` (#34745) rewrites the delivery path in `concurrentFetch` and +`queue.deliver` — the generic machinery Bor's **witness** fetcher +(`bor_fetchers_concurrent_witnesses.go`) also flows through. The new +`validityErrorOfRequest` reports `errInvalidBody` / `errInvalidReceipt` back +through `res.Done`, which drops the peer. Both sentinels are produced **only** by +body/receipt validation (`queue.go` validators and `bor_downloader.go:2393`); +`witnessQueue.deliver` returns neither, so `validityErrorOfRequest` yields nil on +the witness path and `res.Done <- nil` is byte-for-byte today's behaviour. +Witness generation, propagation and import are unchanged. + +No file under `core/stateless/` or `eth/protocols/wit/` is in this batch's +footprint, and the `s.witness` blocks in `core/state/statedb.go` are untouched. + +### `core/vm`: #33960 stack arena — API adopted, storage layout kept (operator decision) + +Upstream replaces the EVM stack with a shared **arena**: one growable +`[]uint256.Int` per EVM, each call frame claiming a window (`bottom`, `size`, +`inner`), with `evm.Release()` returning the arena to the pool. + +Bor already carries its **own** optimisation of the same hot path — a fixed +`[1024]uint256.Int` embedded in `Stack` with a `top` index, documented as +"Ported from GEVM" and deliberately ordering `top` first so it shares a cache +line with `data[0]`. The two designs pursue the same goal (no append, no slice +header, GC-friendly) with opposite trade-offs: upstream amortises allocation +across call depth but pays a pointer hop (`s.inner.data[...]`) on every +push/pop/peek; Bor pays a pooled 32 KiB object per frame but indexes directly. + +The decisive fact is that **no call site depends on arena internals** — the whole +8-file rewrite (`push(new(uint256.Int).SetX(...))` → `get().SetX(...)`, and +`Back` → unexported `back`) only needs a stack exposing `get()` and `back()`. + +Resolved on the operator's call: **keep Bor's storage, adopt upstream's API.** + +- `core/vm/stack.go` keeps Bor's struct and pool, and gains `get()` (5 lines, + same semantics as upstream's), `newStackForTesting()` (returns an off-pool + stack, as upstream's does), and the `Back` → `back` rename. +- `instructions.go`, `gas_table.go`, `memory_table.go`, `eips.go`, + `instructions_test.go` take upstream's side verbatim — these are the files + future `core/vm` batches will keep touching, so they are the ones worth + converging. +- **Declined:** `evm.arena`, `EVM.Release()`, and its **17** auto-merged call + sites across `core/state_prefetcher.go`, `core/state_processor.go`, + `internal/ethapi/{api,simulate}.go`, `eth/state_accessor.go`, + `eth/gasestimator`, `eth/tracers/api.go` and `miner/worker.go`. `interpreter.go` + keeps `newstack()` / `returnStack(stack)`. + +Recorded in `needs-wiring.md`, including the recurring cost: every future +upstream commit that adds a `defer evm.Release()` will auto-merge into Bor and +break the build until removed. + +### #34841: skip the tx gas cap after Amsterdam + +EIP-7825 caps `tx.Gas()` at `params.MaxTxGas`; after Amsterdam/EIP-8037 the limit +also covers the state-gas reservoir, so upstream stops applying the cap once +Amsterdam is active. Bor's gate is **wider** than upstream's — it fires on +`IsOsaka || Bor.IsMadhugiri` because Bor shipped EIP-7825 at Madhugiri — so every +site combines as `(isOsaka || isMadhugiri) && !isAmsterdam`, block-based: + +`core/state_transition.go`, `core/txpool/validation.go`, +`eth/gasestimator/gasestimator.go`, `cmd/evm/internal/t8ntool/transaction.go`, +and the **Bor-only twin** in `miner/worker.go` that raised no conflict (see the +twin scan). `core/txpool/legacypool/legacypool.go` kept Bor's side: upstream's +change there is only a line-wrap and comment, with no Amsterdam gate, despite +what the PR description claims. + +Amsterdam is dormant on every Bor preset, so all five sites are inert today. + +### Other resolutions + +- **#34745 downloader** — adopted. Upstream's `queue.deliver` fix is substantive: + `accepted` now counts only reconstructed items, `taskPool` is keyed by + `header.Hash()` instead of the removed `hashes` slice, failed fetches requeue + from `[i:]` rather than `[accepted:]`, and errors return unwrapped so + `errors.Is` works in the new peer-drop check. Bor's heavy `log.Trace` + instrumentation and its index-based validation loop were re-seated on top. Note + the `hashes` slice was **deleted by the auto-merged `var` block** while its uses + sat inside conflicts — a build break waiting to happen if the conflicts had been + resolved to Bor's side. +- **#34743 p2p/discover** — adopted verbatim, including upstream's own latent bug: + the new `iterList` loop no longer assigns `nextTimeout`, so the clock-warp branch + can nil-deref. Upstream fixes this later **inside this sync's range** + (`60db25b07`, #34878, batch 27), so converging now and letting the fix arrive + with its own commit is preferable to diverging. +- **#34785 t8n alloc streaming** — adopted, keeping Bor's block-based + `IsVerkle(num)` where upstream calls `IsUBT(num, time)` (the batch-23 boundary + rule: `params` identifiers meaning the Verkle hardfork stay). Bor's `path.Join` + was converged to upstream's `filepath.Join` at both sites in `transition.go` — + identical on Linux, correct on Windows, one less divergence. +- **#34794 binary-trie grouping** — `eth/ethconfig/gen_config.go` is generated and + gencodec is not vendored, so the two struct declarations were hand-extended with + `BinTrieGroupDepth` exactly as the generator would emit it. The + Marshal/Unmarshal **bodies** had already auto-merged the field references, so + taking Bor's side on the declarations alone would not have compiled. + `core/genesis_test.go` converged its local names (`ubtTime`, `ubtConfig`, + `TestBinaryGenesisCommit`) because the auto-merged body already used them, while + keeping Bor's block-based fork fields and its skip. +- **#34802 snap sync_test** — combine: upstream's `atomic.Int64` counters adopted, + Bor's `bytes uint64` signatures on `RequestStorageRanges` / `RequestByteCodes` + kept. +- **#34819 snapshot iterator_test** — converge. Bor carried `if i > 50 || i < 85` + (always true); upstream fixed it to `&&`. +- **#34799 BAL spec** — `core/types/bal/bal_encoding_rlp_generated.go` took + upstream's new `uint256` import, re-grouped into Bor's gofmt'd import block. + +### DU deletions kept + +`core/bintrie_witness_test.go` (batch-2 coverage gap), +`eth/downloader/downloader_test.go` (Bor's long-standing rename to +`bor_downloader_test.go`; re-`rm`'d in batches 12, 15 and the eth/70 PR), and +`eth/protocols/snap/handler_test.go` (belongs to the snap/2 + BAL serving feature +deferred in batch 20). + +`downloader_test.go` carried #34745's new coverage for the peer-drop path. Bor's +`bor_downloader_test.go` has diverged too far to host it — no `withholdBodies`, a +different `RequestBodies` shape, and the test needs an unbuffered `Done` channel — +so the behaviour is adopted without its test. Recorded as a coverage gap. + +### Fork/EIP scan + +`params/config.go`, `internal/cli/server/chains/`, `builder/files/` and +`core/forkid/` are **all untouched** by this batch. No new fork gate, no new +`Rules` field; both meta-guards +(`TestReinforceMultiClientPreCompilesTest`, `TestV2ForkParity`) pass unchanged. +The one fork-sensitive change, #34841, rides the existing dormant Amsterdam gate. + +### Bor-only twin scan (§4.6) + +`go generate ./core/vm/` produced a **byte-identical** `interpreter_dispatch.go`, +so the switch-dispatch fast path needed no change despite the push→get rewrite. + +Four Bor-only dependents broke with **no conflict**, three of them from the +`Back` → `back` rename that upstream applied only to its own files: + +- `core/vm/operations_acl.go:111` — `makeGasSStoreFuncPIP88`, the Bor-only PIP-88 + twin of upstream's `makeGasSStoreFunc`, still called `stack.Back(1)`. +- `core/vm/stack_test.go` — Bor's own `BenchmarkStackBack` (part of the GEVM port). +- `core/vm/instructions.go` — `uint256` import left unused once every + `push(new(uint256.Int)...)` became `get().SetX(...)`; Bor keeps that import in + its own group, so upstream's removal did not apply. Same in `core/vm/eips.go`. +- `miner/worker.go:2095` — Bor's own `PendingFilter` construction, the twin of the + block upstream gated on `!IsAmsterdam`. + +`operations_verkle.go` auto-merged its `back()` renames cleanly. + +### Observation, not a change + +`core/vm/interpreter.go`'s deferred cleanup calls `returnStack(stack)` but **not** +`mem.Free()`, which upstream has called since #30137. This is pre-existing on +`origin/develop`, not introduced by this sync — Bor's `Memory` pool therefore +never receives returns and `NewMemory()` always allocates. Flagged for the +`core/vm` owner; deliberately not "fixed" inside a merge batch. + +### Verification + +`go build`, `go vet` (clean apart from the two documented `//nolint` copylocks), +`gofmt -l`, `go mod tidy` and `make lint` (**0 issues**) all clean. Both fork +meta-guards pass. Broad sweep across `core/...`, `eth/...`, `miner/...`, +`consensus/...`, `internal/...`, `trie/...`, `triedb/...`, `p2p/...` and +`cmd/utils`: **82 packages, 0 failures** — notably `eth/downloader` 90.4 s (the +reworked delivery path), `eth/protocols/snap` 24.5 s, `miner` 197.4 s, +`consensus/bor` 39.1 s. `tests/bor`: **620.5 s, exit 0**. + +`core/vm`'s `TestAbortDuringJump` failed once on the first targeted run with the +familiar `fast: out of gas / slow: ` text. Because this batch changes +`core/vm` gas call sites that was re-checked rather than assumed: **2 failures in +6 repeat runs**, matching the rate and text established at the base revision in +batch 23, and it passed in the full sweep. The known racy test, not a regression. + +## v1.17.3 batch 6/7 (`1abbae239`, plan row 25) — merged `90cbef62b` + +20 upstream first-parent commits, **17 conflicts** across 10 upstream PRs, 33 files. +Two of them are direct follow-ups to batch 24's own resolutions, and the largest +single PR is declined. + +### Standing check: witness logic untouched + +`core/state/statedb.go` conflicted, which is on the no-go list, so this was +verified: the conflict is #34899's one-line fix in `commitAndFlush`, and the +resolved diff for that file contains **zero** lines mentioning `witness`. The +`s.witness` blocks, `collectStateWitnessFromReader`, `findStorageCache` and +`enableConcurrentOnReader` are all untouched. No `core/stateless/` or +`eth/protocols/wit/` file is in the footprint. + +### #32585 TypeMux → Feed — DECLINED + +Upstream replaces the deprecated `event.TypeMux` with `event.Feed` across +`eth`, `node`, `eth/downloader`, `eth/syncer` and `cmd/geth`. Declined, for the +same reason as #33157, #33835 and #33511 before it: it is entangled with two +Bor divergences at once. + +- The auto-merge replaced Bor's `StartEvent` / `DoneEvent` / `FailedEvent` in + `eth/downloader/events.go` with upstream's Feed-based `SyncEvent`, which + imports `ethconfig.SyncMode` — the very type relocation Bor declined in + **#33157** (Bor keeps `SyncMode` in the `downloader` package). +- `node/node.go` loses `EventMux()`, while Bor's `cmd/geth/main.go` still + subscribes through it for `--exitwhensynced`, and Bor's forked + `bor_downloader.go` posts to the mux in five places. Upstream's matching + changes land in `downloader.go`, which Bor does not have. + +Adopting it means rewriting Bor's downloader event plumbing on top of a +still-declined type relocation. Reverted the full footprint to Bor HEAD +(`cmd/geth/{config,main}.go`, `cmd/utils/flags.go`, `eth/backend.go`, +`eth/handler.go`, `eth/syncer/syncer.go`, `eth/downloader/{api,events}.go`, +`node/node.go`), `git rm` on the DU `eth/downloader/{downloader,downloader_test}.go`. +Verified first that **no other in-range commit touches any file in that set**, so +no chronological re-application was needed. + +### Two follow-ups to batch 24's own resolutions + +Both were predicted in the batch-24 report and arrived exactly as expected: + +- **#34878 (`60db25b07`)** restores the `nextTimeout` update in `resetTimeout`, + fixing the latent nil-deref that batch 24 adopted verbatim from #34743 rather + than diverging over. It now reads `p.errc <- errClockWarp`. Adopting #34743 + as-is and waiting one batch was the right call — the repair arrived with its + own commit and no Bor divergence was created. +- **#34870 (`5b837e578`)** switches `reconstruct(accepted, res)` to + `reconstruct(k, res)`, keyed on the batch index. This matters *because* of + batch 24: #34745 made `accepted` count only successfully reconstructed items, + so once a stale slot appears, `accepted` and the request-batch index diverge + and `reconstruct` writes to the wrong slot. Adopted, with Bor's trace fields + renamed from `acceptedIndex` to `batchIndex` to match. + +### EIP-7981 (#34755) — access list cost + +`IntrinsicGas` gains an `isAmsterdam` parameter and `FloorDataGas` gains an +`accessList` parameter; under Amsterdam each access-list address and storage key +is charged additional calldata-token cost on top of the existing per-entry gas, +and every arithmetic step gains an overflow guard. Rides the **existing dormant +Amsterdam gate** — `params/config.go`, the chain presets, packaged genesis and +`forkid` are all untouched, and both meta-guards pass. + +`core/txpool/validation.go` combined as usual: Bor's narrower +`IsPrague(head.Number) && Config.Bor != nil` gate kept, upstream's new +`FloorDataGas(rules, tx.Data(), tx.AccessList())` signature adopted. + +### #34787: stop serving on unavailable responses + +Upstream changes the body and receipt serving loops from `continue` (skip the +missing item) to `break` (stop serving), so a response is always a contiguous +prefix of the request. Adopted into Bor's diverged handlers rather than deferred, +because the requester side makes it matter more here than upstream: Bor's +`queue.deliver` validates by position, and #34870 in this same batch keys +`reconstruct` on the batch index. Serving holes to a position-validating +requester is exactly the misalignment both fixes exist to prevent. + +Bor's structure was kept and only the control flow converted — including its +eth/68 `rlp.EmptyList` branch and its eth/69 `gatherBlockReceipts` / +`blockReceiptsToNetwork69` state-sync path, neither of which upstream has. + +### Other resolutions + +- **#34899 `core/state`** — adopted. `s.reader, _ =` becomes `s.reader, err =`, + so a Reader error after commit propagates instead of being discarded; `err` is + the named return and `return ret, err` follows. Bor's `commitAndFlush` body + from batch 23 kept, upstream's explanatory comment restored. +- **#32924 legacypool** — adopted `pool.mu.RLock()` in `Pending`, keeping Bor's + lock-wait metric. Checked before accepting: `Pending` calls `list.Flatten()`, + which caches, but Bor's `sortedMap` carries its own `cacheMu sync.RWMutex`, so + the cache write is internally synchronised and a read lock is safe. +- **#34874 pathdb `AdoptSyncedState`** — reverted as an **orphan on a declined + feature**: it is the snap/2 completion path, and snap/2 was deferred in batch 20. + It also failed to compile against Bor's diverged `newBuffer` signature, which is + how it surfaced. Same class as batch 23's gRPC OTLP orphan. +- **#34887 access-list tracer**, **#34638 snapshot iterator test**, **#34767 + catalyst** — adopted; the catalyst conflict was only Bor's declined + telemetry/ctx signature, and #34767's actual reorg change auto-merged cleanly. + +### DU deletions kept + +`core/bintrie_witness_test.go` (batch-2 coverage gap) and +`eth/downloader/{downloader,downloader_test}.go` (Bor's rename to +`bor_downloader.go`; re-`rm`'d in batches 12, 15, eth/70 and 24). + +### Bor-only twin scan (§4.6) + +One dependent broke with no conflict: **`eth/tracers/parity.go`**, Bor's own +Parity tracer, needed `rules.IsAmsterdam` threading through its `IntrinsicGas` +call. That is the **third** time this one file has broken in this sync — batch 20 +(`reexec` removal), batch 23 (`GasCosts` return type), and now EIP-7981's +signature. It has no upstream counterpart, so it will never appear as a conflict; +it is worth adding to the standing per-batch check list. + +### Verification + +`go build`, `go vet` (clean apart from the two documented `//nolint` copylocks), +`gofmt -l`, `go mod tidy` and `make lint` (**0 issues**) all clean. Both fork +meta-guards pass. Broad sweep: **82 packages green**, with one failure — +`core/vm`'s `TestInterruptDuringExecution`, the known racy test. This batch +touches **no file under `core/vm`** (the diff for that directory is empty), and +the test still failed 2 of 3 repeat runs, which settles it as pre-existing +without needing a re-baseline worktree. `tests/bor`: **623.1 s, exit 0**. + +## v1.17.3 batch 7/7 (`117e067f0`, plan row 26) — merged `96945e338` + +16 upstream first-parent commits ending at the **go-ethereum v1.17.3 release +tag**, 7 conflicts, 33 files. Small in conflicts, large in blast radius: #34934 +converts `core.Message`'s fee and value fields to `uint256.Int`, which auto-merged +almost everywhere and surfaced as build breaks in Bor-only callers instead. + +### Standing check: witness logic untouched + +No file under `core/stateless/` or `eth/protocols/wit/` is in the footprint, and +`core/state/statedb.go` is not in this batch at all. Witness generation, +propagation and import are untouched. + +### The regression this batch nearly shipped + +`internal/ethapi`'s `TestSimulateV1/basefee-non-validation` failed after the +merge. It passed in both batch 24 and batch 25's sweeps, which settled it as a +**regression of this batch** without needing a re-baseline worktree. + +The cause is a silent signedness change on a Bor-only path. At the merge base, +Bor computed + +```go +effectiveTip := msg.GasPrice // *big.Int +effectiveTip = new(big.Int).Sub(msg.GasPrice, st.evm.Context.BaseFee) +``` + +`big.Int` is **signed**, so when `GasPrice < BaseFee` the tip goes negative, and +Bor's fee-transfer log (`output1.Sub(output1, amount)` / +`output2.Add(output2, amount)`) consumed that negative value coherently. + +Upstream's #34934 rewrote the same lines in `uint256`, where `Sub` **wraps** on +underflow — producing the `0x5226ffff…` garbage visible in the failing log. This +does not hurt upstream, because upstream guards the whole fee payment with + +```go +if st.evm.Config.NoBaseFee && msg.GasFeeCap.Sign() == 0 && msg.GasTipCap.Sign() == 0 { /* skip */ } +``` + +which **Bor has commented out** (the long-standing `TODO(raneet10)` block), so Bor +always pays the tip and therefore always evaluates the subtraction. + +Resolved by keeping `effectiveTip` on signed `big.Int` and converting at the +boundary instead, with a comment recording *why* the type cannot follow upstream +here. `amount`, `burnAmount`, `ExecutionResult.FeeBurnt` / `FeeTipped` and +`AddFeeTransferLog` all stay `big.Int` — converting them would ripple through +Bor's public execution-result surface for no benefit. + +This is worth remembering as a class: **an upstream big.Int → uint256 conversion +is not type-neutral on any path where Bor removed upstream's guard**, because the +guard is what makes the value non-negative. + +### `version/version.go` — deliberately not bumped + +The batch ends at upstream's release commit, which sets `Patch = 3` and +`Meta = "stable"`. Took Bor's side, and **not** because the bump was deferred to +the chores commit: it is not taken at all. Checked against the earlier milestone +tips before deciding — `ppatil-upstream-v1.17.1`, `ppatil-upstream-v1.17.2` and +`ppatil-upstream-v1.17.3-part2` all still read `Patch = 0`, `Meta = "unstable"`. +The only two commits in this whole sync that touched the file are batch 1 of +v1.16.9 and batch 7 of v1.17.0, both auto-merges of upstream's *release-cycle* +commits rather than release tags. + +So the established behaviour is that Bor does not track upstream's geth release +version here, and this batch matches it. The `version/version.go` gotcha is real +but its correct handling is "keep Bor's", not "bump it later". Worth deciding +explicitly at some point what this file should say for a Bor tree based on geth +v1.17.4 — but that is a Bor release decision, not a merge one. + +### Other resolutions + +- **`eth/gasestimator`** — combine: Bor's block-based `IsCancun(opts.Header.Number)` + kept, upstream's `uint256.NewInt` blob-gas arithmetic adopted. +- **`core/state_transition.go` `TransactionToMessage`** — theirs. Upstream now + computes `from` at the top and returns `msg, nil`; the overflow checks that + replaced Bor's `BitLen() > 256` guards live inside the new `uint256.FromBig` + conversions, so the guards are redundant, not lost. +- **`core/state_processor.go`** — took only the `uint256` import; the `trie` import + stays dropped, as it has since batch 23 declined `FinalizeAndAssemble`. +- **`internal/build/gotool.go`**, **`internal/ethapi/transaction_args.go`**, + **`signer/core/signed_data.go`** — adopted (the last is #34908, avoiding + mutation of the caller's signature buffer). + +### Bor-only twin scan (§4.6) + +Five Bor-only dependents broke at build with **no conflict**, all from the +`core.Message` conversion: + +- `internal/ethapi/api.go` — Bor's access-list creation zeroes the three fee + fields; now `new(uint256.Int)`. +- `eth/tracers/parity_call.go` — Bor's Parity `trace_call`, which clamps the + effective gas price to the base fee. +- `accounts/abi/bind/backends/simulated.go` — the simulated backend upstream + deleted long ago and Bor still carries. +- `consensus/bor/statefull/processor_test.go` — the state-sync system-contract + test helper. +- `eth/tracers/parity.go` was **not** hit this time, but see its needs-wiring row + from batch 25. + +### Verification + +`go build`, `go vet` (clean apart from the two documented `//nolint` copylocks), +`gofmt -l`, `go mod tidy` and `make lint` (**0 issues**) all clean. Both fork +meta-guards pass; `params/config.go`, the chain presets, the packaged genesis +files and `forkid` are untouched. Broad sweep after the `effectiveTip` fix: +**92 packages, 0 failures** — including `core/vm`, whose racy interrupt/abort +tests happened to pass this round. `tests/bor`: **620.5 s, exit 0**. diff --git a/docs/upstream-merges/v1.17.4/needs-wiring.md b/docs/upstream-merges/v1.17.4/needs-wiring.md index 799e3a94dd..4f031821c8 100644 --- a/docs/upstream-merges/v1.17.4/needs-wiring.md +++ b/docs/upstream-merges/v1.17.4/needs-wiring.md @@ -66,6 +66,25 @@ a future adoption would require. | snap/2 protocol + BAL serving (#34083) | batch 20 (v1.17.3 1/7, `04e40995d`) | deferred | The snap/2 protocol exists to **serve block access lists**. Bor cannot produce a BAL while Amsterdam is dormant (`AmsterdamBlock` nil), so adopting it buys nothing today while costing a rewrite of Bor's diverged snap serving path — upstream moves 571 lines out of `eth/protocols/snap/handler.go` into a new `handlers.go` behind a version-dispatch map. Upstream itself ships the snap/2 dispatch **commented out** in this commit (`//case SNAP2:`), so even upstream is not serving it yet. Reverted `eth/protocols/snap/{handler,handler_fuzzing_test,protocol}.go` and `core/blockchain_reader.go` to HEAD; `git rm` on the new `handlers.go` + `handler_test.go`. | Revisit **together with the Amsterdam/BAL enable decision**, not before — serving a BAL is meaningless until Bor produces one. Then: adopt upstream's `handler.go`→`handlers.go` split, port Bor's snap divergences (timing/metrics in the inline switch) into the new per-message handlers, add `SNAP2` to the version dispatch, and re-add `BlockChain.GetAccessListRLP` (dropped here — see below). | upstream boundary `04e40995d`; commit `00da4f51f`; gated on the EIP-7928 rows in `fork-register.md` | | Amsterdam statetests (#34671) | batch 20 (v1.17.3 1/7, `04e40995d`) | coverage-gap | Upstream added an `"Amsterdam"` entry to `tests/init.go`'s config table so the Amsterdam statetests execute. It keys on `AmsterdamTime: u64(0)` and sets `BlobScheduleConfig{Amsterdam: params.DefaultBPO4BlobConfig}` — Bor is **block-based** (`AmsterdamBlock`) and carries no BPO blob configs (it deleted the `BPO1`/`OsakaToBPO1AtTime15k` entries), so upstream's config would not compile as written. Kept Bor's deletion. | Add a block-based Amsterdam statetest config (`AmsterdamBlock: big.NewInt(0)`) once Bor takes a blob-schedule decision for Amsterdam; until then the Amsterdam statetests do not run on Bor. Pairs with the EIP-7928 / Amsterdam rows in `fork-register.md`. | upstream boundary `04e40995d`; commit `44257950f` | | `BlockChain.GetAccessListRLP` (#34083 fragment) | batch 20 (v1.17.3 1/7, `04e40995d`) | deferred | Dropped as part of the #34083 deferral above. Worth its own line because it is the *only* piece of #34083 that landed outside `eth/protocols/snap/` — a `core/blockchain_reader.go` accessor reading `rawdb.ReadAccessListRLP`. During resolution `blockchain_reader.go` was reverted wholesale, which also discarded #34633's `StateIndexProgress` widening; that was re-applied, this was intentionally not. | Re-add alongside snap/2 (it exists to feed the BAL serving path). Trivial once `rawdb.ReadAccessListRLP` has a producer. | upstream boundary `04e40995d`; commit `00da4f51f`; `core/blockchain_reader.go` | +| `res.fees` in the testing payload envelope (#34704) | batch 21 (v1.17.3 2/7, `c453b99a5`) | coverage-gap | Upstream changed `BuildTestingPayload`'s last line from `engine.BlockToExecutableData(res.block, new(big.Int), ...)` to pass `res.fees`. Bor deleted `BuildTestingPayload` together with the `testing_buildBlockV1` RPC (#33656, declined in batch 12), so the changed line has no home. The same commit's `updateSpanForDelivery` is inside that deleted region too. Kept Bor's `miner/payload_building.go`. | Re-apply automatically when the #33656 row is adopted; it is one argument. | upstream boundary `c453b99a5`; commit `ecae51997` (#34704); depends on the batch-12 #33656 row, same as the #34094 row | +| `eth_getLogs` range-limit error should be -32602 (#34647) | batch 21 (v1.17.3 2/7, `c453b99a5`) | deferred | Upstream made the block-range-limit rejection an `invalidParamsErr` so JSON-RPC clients see **-32602 invalid params** instead of a generic server error, and rewrote `TestRangeLimit` to assert the code and message. Bor does not enforce the limit in `eth/filters/filter.go` at all — it enforces it one layer up in `eth/filters/api.go` (`checkBlockRangeLimit`, covering `eth_getLogs` **and** `bor_getLogs`, per the #33163 `wontfix` row above), returning a plain `errExceedBlockRangeLimit`. So upstream's change has no corresponding line, and Bor's equivalent still surfaces as a generic error. Kept Bor's `filter.go` and its deletion of `TestRangeLimit`. **No DoS protection was dropped** — Bor's guard is broader than upstream's. | RPC-conformance nicety, not a bug: wrap Bor's `errExceedBlockRangeLimit` in an `rpc.Error` carrying -32602 at the two `checkBlockRangeLimit` call sites in `eth/filters/api.go`, and add a test asserting the code. Note this changes a public RPC error code, so it wants a release note. | upstream boundary `c453b99a5`; commit `a8ea6319f` (#34647); relates to the #33163 row | +| `CachingDB` split into `MPTDatabase` + `UBTDatabase` (#34700) | batch 22 (v1.17.3 3/7, `5af5510b1`) | adopted | Deferred out of batch 22 (31 of its 34 conflicts) so a hand-written restructure of consensus-critical `core/state` would not ride inside a 20-commit merge commit, then **adopted on branch `ppatil-upstream-mptubt`, cut from `e15a2b63b`, before batch 23** — its sequel #34763 (`7e388fd09`, batch 23) applies the same split to `core/state/reader.go`, and #34843 (`61342e9c0`) follows in batch 30. | Adopted: the type split (`MPTDatabase` / `UBTDatabase`, `DatabaseType`, `Type()` on the `Database` interface, `Trie.IsVerkle()`→`IsUBT()`, `triedb.Config.IsVerkle`→`IsUBT`, `triedb.VerkleDefaults`→`UBTDefaults`, `pathdb` `isVerkle`→`isUBT`, `types.EmptyVerkleHash` folded into `EmptyBinaryHash`, and the `core/state` conversions from `TrieDB().IsVerkle()` to `db.Type().Is(...)`). Every Bor divergence landed on `MPTDatabase`; `UBTDatabase` is dormant code Bor never constructs outside tooling and tests. **Not adopted: the runtime fork-boundary plumbing — see the row below.** | upstream boundary `5af5510b1`; commit `ba215fd92`; ledger section *v1.17.3 out-of-band adoption — CachingDB split* | +| UBT runtime fork-boundary selection (#34700, part 2) | batch 22 adoption (`ba215fd92`) | deferred | The half of #34700 that decides MPT vs UBT **per block**: `BlockChain.StateAt(root)` becomes `StateAt(header *types.Header)`, a new `StateAtForkBoundary(parent, header)` handles the last-MPT/first-UBT boundary, `HistoricState` takes a header and refuses UBT, and `ProcessBlock` selects the implementation via `chainConfig.IsUBT(number, time)` behind a local `prewarmReader` type assertion. Declined because it is keyed on the **timestamp** fork fields Bor deleted in favour of block-based ones, it ripples `StateAt`'s signature through every caller across `eth`, `internal`, `miner` and the tracers, and it buys nothing while `VerkleBlock` is nil on every Bor preset. | Required before Bor could ever enable UBT, together with: `params` UBT fields (see `fork-register.md`), UBT support in BlockSTM V2 (`ParallelStateDB` has no binary-trie path), and `AccessEvents` wiring (`statedb.go` allocates them only when `db.Type().Is(TypeUBT)`). **Concrete consequence today:** `core/blockchain.go` builds `NewMPTDatabase` unconditionally, so a hand-written genesis with `enableVerkleAtGenesis: true` would now get Merkle tries where the dual-mode `CachingDB` gave binary ones. No Bor preset sets it and no test drives it through `NewBlockChain`; fixing it properly means this row, not invented dispatch. | upstream `ba215fd92` hunks in `core/blockchain.go` / `core/blockchain_reader.go`; relates to the batch-22 `params/config.go` decision | +| `ChainOverrides.OverrideVerkle` → `OverrideUBT` (#34700) | batch 22 adoption (`ba215fd92`) | wontfix | Pure vocabulary rename, but it renames the operator-facing `--override.verkle` CLI flag and the `OverrideVerkle` `ethconfig` TOML key, and requires regenerating `eth/ethconfig/gen_config.go` — an operator-visible change to a dormant fork's override with no merge-conflict payoff. | Take it only if Bor also takes the `params` Verkle→UBT rename, so the two surfaces stay consistent. | upstream `ba215fd92` hunks in `cmd/geth/config.go`, `cmd/geth/chaincmd.go`, `cmd/utils/flags.go`, `core/genesis.go`, `eth/backend.go`, `eth/ethconfig/*` | +| Slot number in `testing_buildBlockV1` payload attributes (#34722) | batch 22 (v1.17.3 3/7, `5af5510b1`) | coverage-gap | One-line change inside `eth/catalyst/api_testing.go`, a file Bor deleted with the `testing_buildBlockV1` RPC (#33656, batch 12). **Third orphaned change on that declined feature**, after #34094 (batch 19) and #34704 (batch 21). | Re-apply automatically when the #33656 row is adopted. | upstream boundary `5af5510b1`; commit `c9fea4461`; depends on the batch-12 #33656 row | +| Upstream test fixtures that pair timestamp forks with blobs | batch 22 (v1.17.3 3/7, `5af5510b1`) | coverage-gap | #34675's prestate codehash fix was adopted, but its new fixture `eth/tracers/internal/tracetest/testdata/prestate_tracer_with_diff_mode/eip7702_deauth.json` was removed. The fixture configures forks by timestamp (`cancunTime`/`pragueTime`), which Bor's block-based `ChainConfig` silently drops on unmarshal, so no blob-capable fork is configured; the fixture's non-nil `excessBlobGas` then panics `CalcBlobFee` with "calculating blob fee on unsupported fork". Same root cause as the batch-20 `tests/init.go` row. | **This is a recurring class, not a one-off** — any future upstream fixture combining `*Time` fork fields with blob fields will fail the same way. Options: (a) translate such fixtures to Bor's block-based fields as they arrive; (b) teach the tracetest/statetest fixture loaders to map `*Time` fork keys onto Bor's `*Block` fields once, centrally. (b) is the durable fix and would also recover the batch-20 `tests/init.go` gap. | upstream boundary `5af5510b1`; commit `f63e9f3a8`; relates to the batch-20 `tests/init.go` row | +| `FinalizeAndAssemble` removal (#34726) | batch 23 (v1.17.3 4/7, `33c1bd59f`) | wontfix | Upstream removed `FinalizeAndAssemble` from `consensus.Engine` and replaced it with a consensus-agnostic `core.AssembleBlock`, on the premise that block assembly carries no consensus-specific logic. **That premise does not hold for Bor:** `(*Bor).finalizeAndAssemble` calls `commitSprintWork` at sprint start, performing the **state-sync commits** and therefore *producing receipts during finalization*, and it returns `(*types.Block, []*types.Receipt, time.Duration, error)`. Upstream's helper takes receipts as an input and returns only a block, so it cannot express this. Bor additionally carries `FinalizeAndAssembleForSimulation` for `eth_simulateV1`, which skips the sprint span and state-sync commits because a simulated block's parent may be a phantom header. | Revisit only if Bor's state-sync commits move out of finalization — e.g. as part of the batch 27–28 block-assembly rework around block access lists, which is already flagged in `plan.md`. Any future adoption must keep a path that returns finalization-produced receipts. Note the removal's fallout is load-bearing knowledge: `core.AssembleBlock` auto-merges into `core/state_processor.go` outside any conflict, and `core/chain_makers.go` gains a duplicate assembly block using `IsShanghai(num, time)` which Bor lacks. | upstream boundary `33c1bd59f`; commit `d422ab39d`; relates to the batch 27–28 assembly rework | +| `ubtTrieReader` is invisible to Bor's reader dispatch (#34763) | batch 23 (v1.17.3 4/7, `33c1bd59f`) | coverage-gap | The reader split means `UBTDatabase.Reader` now builds a `*ubtTrieReader`, but Bor's three Bor-only type switches in `core/state/statedb.go` — witness collection (`collectStateWitnessFromReader`), the storage-cache finder (`findStorageCache`) and the concurrent-reads enabler (`enableConcurrentOnReader`) — only have a case for `*mptTrieReader`. Before the split a single `trieReader` served both trie types. Under UBT, witness collection would therefore silently collect nothing, and V2 concurrent reads and the shared storage cache would not engage. **Inert today**: UBT is dormant and `UBTDatabase` is never constructed on any Bor network. | **Not resolved autonomously by operator instruction** — closing it means writing new witness logic (a `CollectStateWitness` on `ubtTrieReader`), and witness generation/propagation/import is off-limits for the sync (see `plan.md` risk flags). Required before UBT could be enabled, together with the other UBT rows. | upstream boundary `33c1bd59f`; commit `7e388fd09`; depends on the #34700 adoption | +| Block-level access lists are not built under BlockSTM V2 (#34776) | batch 23 (v1.17.3 4/7, `33c1bd59f`) | deferred | Three separate places where BAL accumulation is adopted but inert or absent on Bor's parallel path: (a) `stateObject.GetCommittedState` now calls `s.db.stateReadList.AddState(...)`, and **`bal.StateAccessList` is an unsynchronised map** while Bor reads stateObjects concurrently under V2 — safe only because the list is allocated exclusively under `rules.IsAmsterdam`, leaving the call on its nil-receiver guard; (b) `StateDB.getStateObject` records `AddAccount` before Bor's `MVRead` wrapper; (c) `ParallelStateDB.Finalise` returns a nil access list and `ParallelStateDB.Touch` only registers a V2 read. | Enabling Amsterdam requires deciding how BALs are constructed under parallel execution: either give `bal.StateAccessList` its own synchronisation, or accumulate per-worker lists and merge them at settlement (upstream provides `StateAccessList.Merge`). Until then Amsterdam must not be scheduled on a network running V2. | upstream boundary `33c1bd59f`; commit `6f02965aa`; relates to the Amsterdam row in `fork-register.md` | +| Contract-code size dedup in `SizeTracker` (#34724) | batch 23 (v1.17.3 4/7, `33c1bd59f`) | coverage-gap | Upstream's new `calSizeStats` skips already-present code via `ContractCode.Duplicate`, one of the code-origin fields Bor declined along with `deriveCodeFields` and `ToTracingUpdate`. Bor therefore keeps its own loop and its documented inaccuracy: reported contract-code size can overcount because code is stored by hash and deduplicated by the database. | Adopt together with the declined code-origin/tracing feature, which is what populates `Duplicate` and `OriginBlob`. Partial adoption is not useful: without `deriveCodeFields` the flags are never set. | upstream boundary `33c1bd59f`; commit `acbf699c3` | +| gRPC transport for OTLP trace export (#33941) | batch 23 (v1.17.3 4/7, `33c1bd59f`) | wontfix | Adds a gRPC transport alongside the HTTP one for OpenTelemetry trace export, touching `internal/telemetry/tracesetup/setup.go`, the `rpc.telemetry.*` CLI flags and the otel dependencies. **Bor has no `internal/telemetry` tree at all** — the whole OpenTelemetry feature was declined earlier in this sync — so this is an orphan on that decline, in the same way the `testing_buildBlockV1` rows are orphans on #33656. | Re-apply automatically if the OpenTelemetry feature is ever adopted; the otel and grpc module requirements come with it. | upstream boundary `33c1bd59f`; commit `f568ab993`; depends on the earlier telemetry decline | +| EVM stack arena (#33960) | batch 24 (v1.17.3 5/7, `41b856d47`) | deferred | Upstream replaced the EVM stack with a shared **arena** — one growable `[]uint256.Int` per EVM, each call frame claiming a window (`bottom`/`size`/`inner`), released via a new `EVM.Release()`. Bor already carries its own optimisation of the same hot path: a fixed `[1024]uint256.Int` embedded in `Stack` with a `top` index, ported from GEVM, with `top` ordered first so it shares a cache line with `data[0]`. **By operator decision the storage layout stayed Bor's and only the call-site API was adopted** (`get()`, `back()`, `newStackForTesting()`), because no call site depends on arena internals and the arena adds a pointer hop (`s.inner.data[...]`) to every push/pop/peek that nobody has measured on Bor's workload. Declined with it: `evm.arena`, `EVM.Release()`, and its **17** call sites. | Two things to revisit. (1) **Benchmark before reconsidering**: the arena amortises allocation across call depth while Bor pools a 32 KiB object per frame — which wins depends on call-depth distribution and, under BlockSTM V2, on `sync.Pool` contention across workers. Adopt only with numbers. (2) **Recurring merge cost**: every future upstream commit adding a `defer evm.Release()` auto-merges into Bor outside any conflict and breaks the build until removed. If that churn becomes routine, the cheap alternative is a documented no-op `EVM.Release()` on Bor's per-frame model. | upstream boundary `41b856d47`; commit `4dc7d4615`; `core/vm/{stack,evm,interpreter}.go` | +| Peer-drop coverage for invalid bodies/receipts (#34745) | batch 24 (v1.17.3 5/7, `41b856d47`) | coverage-gap | The delivery-path fix was adopted in `eth/downloader/{bor_fetchers_concurrent,queue}.go` — `accepted` now counts only reconstructed items, `taskPool` is keyed by `header.Hash()`, failed fetches requeue from `[i:]`, and errors return unwrapped so `validityErrorOfRequest` can drop peers that served `errInvalidBody` / `errInvalidReceipt`. Upstream's matching tests live in `eth/downloader/downloader_test.go`, which Bor deleted long ago in favour of `bor_downloader_test.go`. That fork has diverged too far to host them: no `withholdBodies` map, a different `RequestBodies` body-construction path, and the new tests need an unbuffered `Done` channel plus a `dropped` channel on the test peer. | Port `TestInvalidBodies`-style coverage onto `bor_downloader_test.go` by adding `corruptBodies` + `dropped` to Bor's `downloadTesterPeer` and switching its `Done` channel to unbuffered. Worth doing — this is peer-eviction behaviour on the sync path, and it is currently untested in Bor. | upstream boundary `41b856d47`; commit `75a64ee34`; relates to the witness-fetcher path in `bor_fetchers_concurrent_witnesses.go` | +| `Memory` instances are never returned to the pool | batch 24 (v1.17.3 5/7, `41b856d47`) | coverage-gap | `(*EVM).Run`'s deferred cleanup in `core/vm/interpreter.go` calls `returnStack(stack)` but not `mem.Free()`, which upstream has called since #30137. **Pre-existing on `origin/develop`, not introduced by this sync** — confirmed by checking every branch in the stack plus `origin/develop`, all of which lack the call. The consequence is that Bor's `memoryPool` never receives returns, so `NewMemory()` allocates on every call frame and the pool is dead weight. Surfaced here because #33960 rewrote exactly this deferred block, making the omission visible in the conflict. | Decide with the `core/vm` owner whether the omission is deliberate (e.g. dropped for a BlockSTM-related reason) or an old merge casualty. If the latter, restoring `mem.Free()` is a one-line change that should be benchmarked, not folded into a merge batch. | upstream boundary `41b856d47`; `core/vm/interpreter.go`; unrelated to any upstream commit in range | +| TypeMux -> Feed migration (#32585) | batch 25 (v1.17.3 6/7, `1abbae239`) | deferred | Upstream replaced the deprecated `event.TypeMux` with `event.Feed` across `eth`, `node`, `eth/downloader`, `eth/syncer` and `cmd/geth`, turning the downloader's `StartEvent`/`DoneEvent`/`FailedEvent` into a single Feed-published `SyncEvent`. Entangled with **two** Bor divergences at once: the new `events.go` imports `ethconfig.SyncMode`, which is exactly the type relocation Bor declined in **#33157** (Bor keeps `SyncMode` in the `downloader` package); and `node/node.go` loses `EventMux()` while Bor's `cmd/geth/main.go` still subscribes through it for `--exitwhensynced` and Bor's forked `bor_downloader.go` posts to the mux in five places, with upstream's matching changes landing in a `downloader.go` Bor does not have. Reverted the full footprint to HEAD; removed the two DU files `eth/downloader/{downloader,downloader_test}.go`. | Adopt **together with #33157**, not before — the SyncMode relocation is a prerequisite, since the Feed event type carries `ethconfig.SyncMode`. Then: publish Bor's sync events from `bor_downloader.go` through the new Feed, migrate `cmd/geth/main.go`'s `--exitwhensynced` subscription, and drop `EventMux()` from `node`. Groups naturally with the other deferred protocol-layer rows (#33835, #33511). | upstream boundary `1abbae239`; commit `1abbae239`; depends on the batch-5 #33157 row | +| pathdb `AdoptSyncedState` (#34874) | batch 25 (v1.17.3 6/7, `1abbae239`) | wontfix | Adds `triedb/pathdb.AdoptSyncedState` plus a `triedb` wrapper, the completion path for a **snap/2** sync — the protocol Bor deferred in batch 20 because snap/2 exists to serve block access lists, which Bor cannot produce while Amsterdam is dormant. An orphan on that decline, in the same way #33941's gRPC OTLP transport was an orphan on the declined telemetry tree. It surfaced at build time rather than as a conflict, because it calls Bor's diverged `newBuffer` with upstream's argument list. Reverted `triedb/database.go`, `triedb/pathdb/database.go` and `triedb/pathdb/database_test.go` to HEAD. | Re-apply automatically whenever the snap/2 + BAL serving row is adopted; it is that feature's completion path and is meaningless without it. | upstream boundary `1abbae239`; commit `06c30cc7e`; depends on the batch-20 snap/2 row | +| `eth/tracers/parity.go` breaks on every `core` signature change | batch 25 (v1.17.3 6/7, `1abbae239`) | coverage-gap | Bor's Parity-format tracer is Bor-only and calls `core.IntrinsicGas` directly, so it has now broken **three times in this sync with no conflict to warn of it**: batch 20 (the `reexec` removal), batch 23 (`IntrinsicGas` returning `vm.GasCosts`), and batch 25 (the new `isAmsterdam` parameter from EIP-7981). Each time it surfaced only at build. | Add `eth/tracers/parity.go` to the standing per-batch twin-scan check list alongside the PIP-88 gas twins, since it has no upstream counterpart and will keep tracking `core`'s intrinsic-gas signature. Longer term, consider having it consume a stable helper rather than `IntrinsicGas` directly. | upstream boundary `1abbae239`; commit `aaa2b6628`; recurs from batches 20 and 23 | +| Bor pays the tip unconditionally on the `NoBaseFee` path | batch 26 (v1.17.3 7/7, `117e067f0`) | coverage-gap | Upstream skips fee payment entirely when `Config.NoBaseFee` is set and both fee caps are zero, so a simulated call never applies a negative effective tip to the coinbase. Bor has that guard **commented out** (the long-standing `TODO(raneet10)` block in `core/state_transition.go`) and always pays, which means `effectiveTip = GasPrice - BaseFee` can legitimately go negative on Bor. That is why #34934's `big.Int` to `uint256` conversion could not be taken here: `uint256.Sub` wraps, and the resulting garbage showed up as a corrupted fee-transfer log in `TestSimulateV1/basefee-non-validation`. `effectiveTip` is therefore pinned to signed `big.Int` with a comment, while the rest of `core.Message` moved to `uint256`. | Decide with the execution owner whether Bor should adopt upstream's skip. If it should, the pin can be removed and `effectiveTip` follows upstream to `uint256`. If it should not, the `TODO(raneet10)` comment should be replaced with a statement of the intended Bor behaviour, because the current comment reads as unfinished work rather than a decision. Either way the signedness constraint must be restated wherever this value is touched. | upstream boundary `117e067f0`; commit `e1047b9c8`; `core/state_transition.go` | ## Notes diff --git a/docs/upstream-merges/v1.17.4/plan.md b/docs/upstream-merges/v1.17.4/plan.md index a964809c15..133e747a96 100644 --- a/docs/upstream-merges/v1.17.4/plan.md +++ b/docs/upstream-merges/v1.17.4/plan.md @@ -42,12 +42,12 @@ Status legend: `pending` / `in-progress` / `merged` / `skipped`. | 18 | v1.17.2 | 3/4 | `e23b0cbc2` | 20 | stateless codedb fix, **call-variant gas measurement rework (`core/vm`)**, bintrie parallel hash | merged (`1abb57b8f`) | | 19 | v1.17.2 | 4/4 | `be4dc0c4b` | 17 | **EIP-7708**, simulateV1/getProofs limits, **v1.17.2 release** | merged (`682b4c380`) | | 20 | v1.17.3 | 1/7 | `04e40995d` | 20 | **eth/70 partial receipts**, BAL storage layer + snap/2 BAL serving | merged `d9a3a0bc9` (snap/2 deferred; **eth/70 adopted separately**, see below) | -| 21 | v1.17.3 | 2/7 | `c453b99a5` | 20 | **gas becomes vector (`core`)**, bintrie fixes | pending | -| 22 | v1.17.3 | 3/7 | `5af5510b1` | 20 | EIP-7610 rework, CachingDB split (merkle/binary), freezer fsync fix | pending | -| 23 | v1.17.3 | 4/7 | `33c1bd59f` | 20 | **gas budget (`core/vm`)**, **EIP-7976 calldata floor**, **FinalizeAndAssemble removed (consensus iface)** | pending | -| 24 | v1.17.3 | 5/7 | `41b856d47` | 20 | BAL spec update, **stack arena (`core/vm`)**, skip tx gas cap post-Amsterdam | pending | -| 25 | v1.17.3 | 6/7 | `1abbae239` | 20 | **EIP-7981 access-list cost**, eth_call block overrides, TypeMux removal | pending | -| 26 | v1.17.3 | 7/7 | `117e067f0` | 16 | misc fixes, **core.Message moves to uint256**, **v1.17.3 release** | pending | +| 21 | v1.17.3 | 2/7 | `c453b99a5` | 20 | **gas becomes vector (`core`)**, bintrie fixes | merged (`582a825ae`) | +| 22 | v1.17.3 | 3/7 | `5af5510b1` | 20 | EIP-7610 rework, CachingDB split (merkle/binary), freezer fsync fix | merged (`e15a2b63b`) — **CachingDB split (#34700) deferred**, adopted separately in `b0ea85ca0`, see below | +| 23 | v1.17.3 | 4/7 | `33c1bd59f` | 20 | **gas budget (`core/vm`)**, **EIP-7976 calldata floor**, **FinalizeAndAssemble removed (consensus iface)** | merged (`7b7b9f352`) — 37 conflicts, **#34726 FinalizeAndAssemble removal DECLINED** (Bor's finalization produces state-sync receipts) | +| 24 | v1.17.3 | 5/7 | `41b856d47` | 20 | BAL spec update, **stack arena (`core/vm`)**, skip tx gas cap post-Amsterdam | merged (`9c16c4244`) — 29 conflicts, **#33960 stack arena: API adopted, Bor's GEVM storage layout kept** (operator decision; `EVM.Release()` + 17 call sites declined) | +| 25 | v1.17.3 | 6/7 | `1abbae239` | 20 | **EIP-7981 access-list cost**, eth_call block overrides, TypeMux removal | merged (`90cbef62b`) — 17 conflicts, **#32585 TypeMux→Feed DECLINED** (entangled with the deferred #33157 SyncMode relocation + Bor's forked downloader) | +| 26 | v1.17.3 | 7/7 | `117e067f0` | 16 | misc fixes, **core.Message moves to uint256**, **v1.17.3 release** | merged (`96945e338`) — 7 conflicts; caught and fixed a real regression (Bor's signed `effectiveTip` wrapped as uint256 on the NoBaseFee path). `version/version.go` deliberately not bumped — belongs in the milestone chores commit | | 27 | v1.17.4 | 1/7 | `1149f76dc` | 20 | pre/post-execution wrap (`core`+miner), GasChangeHook v2, block-level accessList | pending | | 28 | v1.17.4 | 2/7 | `ca1a027fa` | 20 | **block accessList construction (consensus/miner)**, eth71 BAL response, engine_hasBlobs | pending | | 29 | v1.17.4 | 3/7 | `4017efe34` | 20 | **jumpdest bitmap global cache (`core/vm`)**, RPC hardening, deprecated CLI flag removal | pending | @@ -67,13 +67,32 @@ upstream's commit expected. | ------- | ----------- | ------ | -------- | | `core/vm` catch-up | batches 4–19 | `ppatil-corevm-catchup` | `ppatil-upstream-v1.17.2` tip | | EIP-7975 / eth/70 partial receipts (#33153) | batch 20 | `ppatil-upstream-eth70` | `d9a3a0bc9` (batch 20 tip) | +| `CachingDB` split into `MPTDatabase` + `UBTDatabase` (#34700) | batch 22 | `ppatil-upstream-mptubt` — `b0ea85ca0` | `e15a2b63b` (batch 22 tip) | -Milestone v1.17.3 is therefore split across two PRs, with the eth/70 PR stacked -between them: batch 20 alone, then eth/70, then batches 21–26 plus the milestone -chores. +Milestone v1.17.3 is therefore split across four PRs, with two adoption PRs +stacked between the merge PRs: batch 20 alone (#2340), then eth/70 (#2341), then +batches 21–22 (#2342), then the `CachingDB` split (#2343), then batches 23–26 plus +the milestone chores on `ppatil-upstream-v1.17.3-part3`. + +The #34700 adoption was **scheduled before batch 23**, not open-ended: its sequel +#34763 applies the same MPT/UBT split to `core/state/reader.go` inside batch 23, +so batch 23 must not be attempted against a non-split tree. Adopted partially — +the type split, not the runtime fork-boundary plumbing; see the `needs-wiring.md` +row for what was declined and why. ## Bor-specific risk flags (advance warning, not a substitute for per-batch review) +- **Witness logic is off-limits for the whole sync (operator-directed).** Bor's + witness generation, propagation and import are a deliberate divergence, so no + batch may alter them as a side effect of an upstream merge. Treat as no-go: + `core/stateless/`, `eth/protocols/wit/`, the `s.witness` collection blocks in + `core/state/statedb.go`, and witness handling in `core/blockchain.go`, + `miner/worker.go` and the downloader. A *file* overlapping is not the feature + overlapping — `statedb.go` in particular holds witness blocks next to + unrelated state code, so resolve around them. If an upstream change genuinely + requires changing witness behaviour, stop and surface it; record it in + `needs-wiring.md` rather than resolving it autonomously. State the check in + each batch report, the same way fork gates are stated. - **Consensus interface churn:** batch 23 removes `FinalizeAndAssemble` from the consensus interface and batches 27–28 rework block assembly around block access lists — `consensus/bor` implements this interface, so expect class-3 @@ -103,3 +122,20 @@ chores. from `upstream-merge-v1.17.4` on the milestone's first batch. - Full first-parent logs per milestone captured in the planning run directory: `runs/pos-merge-upstream/2026-07-06T09-56-56Z-claude-v1.17.4-plan/log-.txt`. + +### Milestone v1.17.3 — complete + +Batches 20–26 plus two hand-written adoption commits are merged on four stacked +branches. The milestone deliberately spans four PRs rather than one: + +| PR | Head | Contents | +| -- | ---- | -------- | +| #2340 | `ppatil-upstream-v1.17.3` | batch 20 | +| #2341 | `ppatil-upstream-eth70` | eth/70 adoption | +| #2342 | `ppatil-upstream-v1.17.3-part2` | batches 21–22 | +| #2343 | `ppatil-upstream-mptubt` | #34700 `CachingDB` split adoption | +| #2345 | `ppatil-upstream-v1.17.3-part3` | batches 23–26 | + +Next: milestone v1.17.4 (rows 27–33). The risk flags below still apply — batches +27–28 rework block assembly around block access lists, which is where the +`FinalizeAndAssemble` decline from batch 23 will resurface. From c0878608b8581a5a071825f40f0ce3d10563c71c Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Tue, 4 Aug 2026 14:37:08 +0530 Subject: [PATCH 79/80] docs: record the v1.17.3 milestone verification suite Documentation only; no code changes. Adds the per-milestone verification tier the skill requires alongside the per-batch gates already recorded above it: two devnets, govulncheck and diffguard. The first devnet covers chain health on a build made from the milestone tip and verified by GitCommit from inside the container. It also pins two of the three Amsterdam-gated pricing EIPs added by this milestone as inert by exact measurement rather than by reading the gates, using the figures Bor reports in its own error messages, taken after Madhugiri activated so the PIP-88 rules were live. The access-list intrinsic cost and the calldata floor both come back at their pre-Amsterdam values. The third, the per-transaction gas cap lift, is not reachable from a call because the check sits behind the skip-transaction-checks flag that calls set; an initial probe was accepted for that reason, which is an instrument artifact and is recorded as such so it is not misread later. That one is argued by inspection instead, soundly, since with Amsterdam dormant the merged condition reduces identically to the pre-merge form. The same devnet validates the effective-tip pin from the last batch end to end. Nearly six thousand fee-transfer logs were scanned with no wrapped values and arithmetic coherent to the wei, which is exactly the corruption the uint256 conversion would have shipped. A second devnet exists because the first one covered none of the witness path, which is the divergence this sync guards most carefully and the one three changes in this milestone touch. It runs a producer, a control node with the witness protocol disabled, and a node syncing statelessly from witnesses. All three agree on block hash and state root, at the finalized block and at a fixed height, with the stateless node at zero lag. Since a stateless node reconstructs state from witnesses, that converts three code-reading arguments into live evidence: the renamed reader type still resolves through the runtime dispatch, the rewritten delivery path still returns nil on the witness queue, and the batch-index reconstruction is correct in the queue that also carries witness tasks. The witness store counts confirm the path was loaded rather than merely enabled, since every witness the producer generated was transferred and retained. Both devnets' error output was checked rather than assumed. The only errors are a bounded startup cluster on freshly-joining RPC nodes, identical on the control node that has the witness protocol disabled, so they are unrelated to witnesses. govulncheck reports one called vulnerability, and the earlier recorded set of three is gone. That was verified rather than reported as an improvement: the relevant dependency versions are byte-identical between the previous milestone tip and this one, so nothing here moved them. The earlier entries cleared because the local toolchain advanced past one of them and the other two were already beyond their affected ranges. The remaining entry is a newly published advisory against an unmoved dependency, and bumping it mid-sync would diverge from upstream's module file, so it is a team follow-up rather than a merge decision. diffguard ran structurally and was then re-run at the pull request base in a worktree, so every finding is baselined instead of assumed. Its single failure, a self-referential package cycle, is present identically at the base. Its dead-code hit is a function that arrived with the block-access-list prefetch reader and is byte-identical to upstream, which has no production caller either, so it is folded into the existing row about access lists not being built under parallel execution. That hit is worth noting as a method point, since an unused private function compiles and passes vet, so only this gate could have surfaced it. Mutation testing remains a CI and operator gate, matching the decision recorded at the first milestone. Also records what the verification does not cover, so the gaps are explicit: the binary-trie reader gap cannot be exercised while that trie type is dormant, parallel stateless import was deliberately left off so a failure would isolate, and long-range stateless sync was not exercised because the node joined near genesis. --- docs/upstream-merges/v1.17.4/ledger.md | 147 +++++++++++++++++++++++++ 1 file changed, 147 insertions(+) diff --git a/docs/upstream-merges/v1.17.4/ledger.md b/docs/upstream-merges/v1.17.4/ledger.md index ace7c13d2e..c27725c9e5 100644 --- a/docs/upstream-merges/v1.17.4/ledger.md +++ b/docs/upstream-merges/v1.17.4/ledger.md @@ -2212,3 +2212,150 @@ meta-guards pass; `params/config.go`, the chain presets, the packaged genesis files and `forkid` are untouched. Broad sweep after the `effectiveTip` fix: **92 packages, 0 failures** — including `core/vm`, whose racy interrupt/abort tests happened to pass this round. `tests/bor`: **620.5 s, exit 0**. + +## v1.17.3 milestone verification — full suite + +The per-batch tier (build, vet, lint, unit, `tests/bor`) is recorded per batch +above. This is the per-milestone tier required by the skill's invariant 8: +kurtosis devnet, govulncheck, diffguard. + +| Gate | Result | +| ---- | ------ | +| kurtosis devnet (health) | **8/8 probes PASS** + two exact Amsterdam-dormancy proofs + fee-log validation | +| kurtosis devnet (witness) | **stateless node agrees on state root** with two full-sync nodes; 189 witnesses transferred | +| govulncheck | 1 called vulnerability, **not introduced by this milestone** | +| diffguard (structural) | 1 FAIL, **proven pre-existing**; 1 dead-code hit, **identical to upstream** | +| diffguard (mutation) | CI/operator gate — not reproduced in-session, same as the v1.17.0 milestone | + +### Devnet — `bor:v1173-sync`, GitCommit-verified + +Built from the milestone tip `df1e813aa` and verified from inside the container +(`bor version` → GitCommit `df1e813aa15123104bc8d40e18db0bb4a8c7f4a8`). Small +preset, 13 services, zero Bor-side log errors. Both nodes 205→233 with +**leader-lag 0** and an **identical finalized hash**; Heimdall 488 on both; +milestones 232→**621**; checkpoints ack_count 4→**17**; state-sync clerk records +ids 1..**113** accumulating from L1. Full table: +`runs/pos-spawn-devnet/2026-08-04T04-39-49Z-claude-pos-v1173-sync/summary.md`. + +**Two of the three Amsterdam-gated pricing EIPs are now proven inert exactly**, not +merely by reading the gates — measured at head 392, i.e. after Madhugiri and +MadhugiriPro activated, so PIP-88 was live during the measurements. The instrument +is the exact figure Bor reports in its own error messages, which needs no funded +account: + +- **EIP-7981** — intrinsic gas for a tx with one access-list address and one + storage key: `want 25300`. Pre-7981 is 25300; post-7981 would be 28628. +- **EIP-7976** — floor data gas for 1000 zero bytes: `want 31000`. Pre-7976 + (10/40) is 31000; post-7976 (64/64) would be 85000. + +The running chain config carries `pragueBlock 0`, `bor.madhugiriBlock 256`, +`bor.madhugiriProBlock 256`, and **no `amsterdamBlock` / `osakaBlock` / +`verkleBlock` at all**. + +**The EIP-7825 cap lift is not devnet-provable**, and the first probe was an +instrument artifact worth recording so it is not repeated: the cap check sits +inside `if !msg.SkipTransactionChecks`, and `eth_call` sets that flag, so a call +with `gas = 20,000,000` was *accepted* — which says nothing about the cap. It is +provable by inspection instead: with `isAmsterdam` false, the merged condition +`(isOsaka || isMadhugiri) && !isAmsterdam && msg.GasLimit > params.MaxTxGas` +reduces identically to Bor's pre-merge form. + +**Batch 26's `effectiveTip` pin validated end to end.** 5907 `LogFeeTransfer` +events on `0x1010` scanned from block 100 to head: **zero** words above 2^200, and +the arithmetic exactly coherent on the sample (`input1 − amount = output1`, +`input2 + amount = output2`, to the wei). The corrupted-log failure mode is +precisely what the uint256 wrap would have shipped, so this is the live signal the +unit tier could not give. + +### Witness path — proven on a live chain with a stateless node + +The first devnet covered chain health and the Amsterdam proofs but gave **no +coverage of the witness path**, which is the divergence this sync guards most +carefully and the one three changes in this milestone touch. A second devnet +(`pos-v1173-witness`) closed that gap with three L2 nodes: + +| Node | `syncmode` | `[witness] enable` | `syncwithwitnesses` | Role | +| ---- | ---------- | ------------------ | ------------------- | ---- | +| validator | `full` | true | false | generates + serves witnesses | +| rpc | `full` | false | false | control — wit off | +| rpc | **`stateless`** | true | **true** | imports blocks from witnesses | + +Rendered config was verified inside each container rather than assumed. Both +witness-participating nodes needed kurtosis-pos's `el_bor_produce_witness`, because +that key drives `[witness] enable` and Bor registers `wit.MakeProtocols` only under +`WitnessProtocol` — without it the stateless node would have had no wire protocol +to fetch over. + +**Result: all three nodes agree on block hash *and* state root**, at the finalized +block (`0xcf`, stateRoot `0x9c56c6…9c3fae`) and at a fixed height (block 202), with +the stateless node at **lag 0**. Since a stateless node reconstructs state *from +witnesses*, that agreement is end-to-end evidence for all three of this milestone's +witness-adjacent changes, each of which was previously supported only by reading +the code: + +- batch 23's rename of the type carrying `CollectStateWitness` — occurrence counting + showed the body unchanged, but only this shows the runtime dispatch still resolves; +- batch 24's rewrite of `queue.deliver` / `concurrentFetch`, which Bor's witness + fetcher flows through; +- batch 24/25's switch of `reconstruct` to a batch index, in the same queue that + carries `witnessTaskPool` / `SetWitnessDone`. + +The witness-store counts confirm the path was loaded rather than merely enabled: +the stateless node held 169 → 178 → **189** witness files as the head advanced, +**equal to the producer's 189**, so every witness generated was transferred and +retained. + +Log errors were checked rather than waved past: the validator had **0**; both RPC +nodes had 22, all of them the same `error handling milestone ws event +err="chain out of sync"` line inside a 4-second startup window, with none in the +following 4 minutes. The count is identical on the control node with the witness +protocol disabled, so it is startup noise on any freshly-joining RPC. + +**Not covered, and stated so the record is honest:** the `ubtTrieReader` gap remains +open — it only manifests under UBT, which cannot be enabled on a devnet without +scheduling the fork, so this validates the **MPT** dispatch only. Parallel stateless +import was deliberately left off to keep a failure isolable, and long-range +stateless sync / witness fast-forward was not exercised, since the node joined near +genesis and followed live. + +Full table: +`runs/pos-spawn-devnet/2026-08-04T04-39-49Z-claude-pos-v1173-witness/summary.md`. + +### govulncheck — one called vulnerability, and it predates this milestone + +`GO-2026-6061` (`google.golang.org/grpc@v1.79.3`, fixed in `1.82.1`). The v1.17.2 +baseline recorded three *different* ones, so this was verified rather than reported +as an improvement: **grpc, `golang.org/x/text` and `golang.org/x/crypto` are +byte-identical at `ppatil-upstream-v1.17.3-part2` and at the milestone tip** — this +milestone moved no dependency. The earlier three cleared because the local +toolchain is now go1.26.5 (the stated fix for the `crypto/tls` entry) and x/text / +x/crypto were already past their affected ranges before part-2. So this is a newly +**published** advisory against an unmoved dependency. + +Bumping grpc mid-sync would diverge from upstream's `go.mod`; it belongs as a team +follow-up, not a merge decision. + +### diffguard — one FAIL, proven pre-existing + +Run structurally (`-skip-mutation`), then re-run **at the PR base in a worktree** +to baseline every finding rather than assuming: + +- **`core -> core` circular dependency (FAIL)** — present identically at the base + (`ppatil-upstream-mptubt`). Pre-existing. +- 21 SDP warnings and the churn-weighted complexity list (`SetEthConfig`, + `BlockChain.reorg`, `EVM.Run`, …) — pre-existing architecture, untouched by this + milestone. +- **Unused func `newPrefetchStateReader`** in `core/state/reader_eip_7928.go` — + flagged as dead code *in changed code*, because the file arrived in batch 24 with + #33737. Both that file and its test are **byte-identical to upstream**, and + upstream has no production caller either: #33737 ships only the reader plus a + test that calls `newPrefetchStateReaderInternal`. It is groundwork for BAL-driven + prefetching, so it needs Amsterdam — folded into the existing "BALs are not built + under BlockSTM V2" row rather than given its own. + +Worth recording as a method point: an unused private function compiles and passes +`go vet`, so neither the build nor the test tier could have surfaced this. diffguard +was the only gate that would. + +Mutation testing was not reproduced in-session, matching the v1.17.0 milestone's +recorded decision that diffguard-mutation and the soak ladder are CI/operator gates. From d011053ead3e5fa7a133122bced7c59732980bf0 Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Tue, 4 Aug 2026 15:20:00 +0530 Subject: [PATCH 80/80] tests: drop the now-unused uint256 import The previous batch moved the message struct's value field to uint256, which correctly reduced a conversion at the only call site in this file from wrapping the value to passing it through. That was the file's single reference to the package, so the import was left behind and the package no longer compiles under the integration build tag. The change is confined to the import block; the call site is already correct. Worth recording why the per-batch gates missed it: this file is behind a build tag, so it is compiled by neither the ordinary build of all packages nor the untagged vet, and the integration suite that does compile it splits per package, so the Bor-specific integration tests passed while the package next to them failed to build. A vet run with the tag enabled reproduces it in seconds, since vet type-checks test files, and that is the gate this belongs in from now on. --- tests/state_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/state_test.go b/tests/state_test.go index f016a08ad0..c360d57e43 100644 --- a/tests/state_test.go +++ b/tests/state_test.go @@ -32,8 +32,6 @@ import ( "testing" "time" - "github.com/holiman/uint256" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/rawdb"