From d4cd646fef9c8d9a1a0d94f5361608efe7c6aaa3 Mon Sep 17 00:00:00 2001 From: mralj Date: Fri, 11 Oct 2024 12:16:24 +0200 Subject: [PATCH] rollup precompile config is glob. variable I decided to implement it this way after trying to integrate code with Arbitrum and having a better understanding of the calls that are made to the NewEvm This approach makes it easier to both override the default config, and to have the option to "not to think about it" --- cmd/geth/config.go | 2 +- cmd/geth/config_rollup.go | 20 ----------- cmd/utils/flags_rollup.go | 35 ++++++++++++------- core/vm/contracts_rollup.go | 17 ++++----- core/vm/contracts_rollup_overrides.go | 14 ++++++-- eth/api_backend_rollup.go | 7 ---- eth/backend.go | 2 +- eth/tracers/api.go | 11 +----- eth/tracers/api_rollup_test.go | 7 ---- internal/ethapi/api.go | 10 ++---- internal/ethapi/backend.go | 3 -- internal/ethapi/simulate.go | 10 ++---- .../ethapi/transaction_args_rollup_test.go | 7 ---- 13 files changed, 52 insertions(+), 93 deletions(-) delete mode 100644 cmd/geth/config_rollup.go delete mode 100644 eth/api_backend_rollup.go delete mode 100644 eth/tracers/api_rollup_test.go delete mode 100644 internal/ethapi/transaction_args_rollup_test.go diff --git a/cmd/geth/config.go b/cmd/geth/config.go index 9a245c171426..92dc9f8a2152 100644 --- a/cmd/geth/config.go +++ b/cmd/geth/config.go @@ -178,7 +178,7 @@ func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) { applyMetricConfig(ctx, &cfg) //[rollup-geth] - activateL1RPCEndpoint(ctx, stack) + utils.ActivateL1RPCEndpoint(ctx, stack) return stack, cfg } diff --git a/cmd/geth/config_rollup.go b/cmd/geth/config_rollup.go deleted file mode 100644 index 3f19b8b1c4f3..000000000000 --- a/cmd/geth/config_rollup.go +++ /dev/null @@ -1,20 +0,0 @@ -package main - -import ( - "github.com/ethereum/go-ethereum/cmd/utils" - "github.com/ethereum/go-ethereum/log" - "github.com/ethereum/go-ethereum/node" - "github.com/urfave/cli/v2" -) - -// TODO: when we have clearer picture of how do we want rollup "features" (EIPs/RIPs) to be activated -// make this "rule" activated (ie. if not "rule activated" then L1 client can simply be nil) -func activateL1RPCEndpoint(ctx *cli.Context, stack *node.Node) { - if !ctx.IsSet(utils.L1NodeRPCEndpointFlag.Name) { - log.Error("L1 node RPC endpoint URL not set", "flag", utils.L1NodeRPCEndpointFlag.Name) - return - } - - l1RPCEndpoint := ctx.String(utils.L1NodeRPCEndpointFlag.Name) - stack.RegisterEthClient(l1RPCEndpoint) -} diff --git a/cmd/utils/flags_rollup.go b/cmd/utils/flags_rollup.go index 241505d1a1fb..bfb6ee3cb538 100644 --- a/cmd/utils/flags_rollup.go +++ b/cmd/utils/flags_rollup.go @@ -1,21 +1,32 @@ package utils import ( + "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/internal/flags" + "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/node" "github.com/urfave/cli/v2" ) -var ( - L1NodeRPCEndpointFlag = &cli.StringFlag{ - Name: "rollup.l1.rpc_endpoint", - Usage: "L1 node RPC endpoint eg. http://0.0.0.0:8545", - Category: flags.RollupCategory, - Required: true, - } -) +var L1NodeRPCEndpointFlag = &cli.StringFlag{ + Name: "rollup.l1.rpc_endpoint", + Usage: "L1 node RPC endpoint eg. http://0.0.0.0:8545", + Category: flags.RollupCategory, +} + +var RollupFlags = []cli.Flag{ + L1NodeRPCEndpointFlag, +} -var ( - RollupFlags = []cli.Flag{ - L1NodeRPCEndpointFlag, +// TODO: when we have clearer picture of how do we want rollup "features" (EIPs/RIPs) to be activated +// make this "rule" activated (ie. if not "rule activated" then L1 client can simply be nil) +func ActivateL1RPCEndpoint(ctx *cli.Context, stack *node.Node) { + if !ctx.IsSet(L1NodeRPCEndpointFlag.Name) { + log.Error("L1 node RPC endpoint URL not set", "flag", L1NodeRPCEndpointFlag.Name) + return } -) + + l1RPCEndpoint := ctx.String(L1NodeRPCEndpointFlag.Name) + stack.RegisterEthClient(l1RPCEndpoint) + vm.SetVmL1RpcClient(stack.EthClient()) +} diff --git a/core/vm/contracts_rollup.go b/core/vm/contracts_rollup.go index caf1b757a978..3d2dfe7d5551 100644 --- a/core/vm/contracts_rollup.go +++ b/core/vm/contracts_rollup.go @@ -39,11 +39,7 @@ func activeRollupPrecompiledContracts(rules params.Rules) PrecompiledContracts { } // ActivateRollupPrecompiledContracts activates rollup-specific precompiles -func (pc PrecompiledContracts) ActivateRollupPrecompiledContracts(rules params.Rules, config *RollupPrecompileActivationConfig) { - if config == nil { - return - } - +func (pc PrecompiledContracts) ActivateRollupPrecompiledContracts(rules params.Rules, config RollupPrecompileActivationConfig) { activeRollupPrecompiles := activeRollupPrecompiledContracts(rules) for k, v := range activeRollupPrecompiles { pc[k] = v @@ -131,13 +127,18 @@ func (c *L1SLoad) isL1SLoadActive() bool { } func (pc PrecompiledContracts) activateL1SLoad(l1RpcClient L1RpcClient, getLatestL1BlockNumber func() *big.Int) { - if paramsAreNil := l1RpcClient == nil || getLatestL1BlockNumber == nil; paramsAreNil { - return - } if precompileNotRuleActivated := pc[rollupL1SloadAddress] == nil; precompileNotRuleActivated { return } + if rpcClientNotOverridenUseDefaultOne := l1RpcClient == nil; rpcClientNotOverridenUseDefaultOne { + l1RpcClient = defaultRollupPrecompilesConfig.L1RpcClient + } + + if latestBlockGetterNotOverridenUseDefaultOne := getLatestL1BlockNumber == nil; latestBlockGetterNotOverridenUseDefaultOne { + getLatestL1BlockNumber = defaultRollupPrecompilesConfig.GetLatestL1BlockNumber + } + pc[rollupL1SloadAddress] = &L1SLoad{ L1RpcClient: l1RpcClient, GetLatestL1BlockNumber: getLatestL1BlockNumber, diff --git a/core/vm/contracts_rollup_overrides.go b/core/vm/contracts_rollup_overrides.go index 81f4464c0d2a..2ae57ada18d3 100644 --- a/core/vm/contracts_rollup_overrides.go +++ b/core/vm/contracts_rollup_overrides.go @@ -7,9 +7,19 @@ import ( "math/big" ) +var defaultRollupPrecompilesConfig RollupPrecompileActivationConfig = RollupPrecompileActivationConfig{ + L1SLoad: L1SLoad{ + GetLatestL1BlockNumber: LetRPCDecideLatestL1Number, + }, +} + +func SetVmL1RpcClient(c L1RpcClient) { + defaultRollupPrecompilesConfig.L1RpcClient = c +} + // generateRollupPrecompiledContractsOverrides generates rollup precompile config including L2 specific overrides -func generateRollupPrecompiledContractsOverrides(evm *EVM) *RollupPrecompileActivationConfig { - return &RollupPrecompileActivationConfig{ +func generateRollupPrecompiledContractsOverrides(evm *EVM) RollupPrecompileActivationConfig { + return RollupPrecompileActivationConfig{ L1SLoad{ L1RpcClient: evm.Config.L1RpcClient, GetLatestL1BlockNumber: LetRPCDecideLatestL1Number, diff --git a/eth/api_backend_rollup.go b/eth/api_backend_rollup.go deleted file mode 100644 index 42548f2ab164..000000000000 --- a/eth/api_backend_rollup.go +++ /dev/null @@ -1,7 +0,0 @@ -package eth - -import "github.com/ethereum/go-ethereum/core/vm" - -func (b *EthAPIBackend) GetL1RpcClient() vm.L1RpcClient { - return b.eth.BlockChain().GetVMConfig().L1RpcClient -} diff --git a/eth/backend.go b/eth/backend.go index 6ca118777cc0..7483f4646e5b 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -217,7 +217,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { } // [rollup-geth] - vmConfig.L1RpcClient = stack.EthClient() + vm.SetVmL1RpcClient(stack.EthClient()) eth.blockchain, err = core.NewBlockChain(chainDb, cacheConfig, config.Genesis, &overrides, eth.engine, vmConfig, &config.TransactionHistory) if err != nil { diff --git a/eth/tracers/api.go b/eth/tracers/api.go index 91babc6ede8b..ec7a48d39add 100644 --- a/eth/tracers/api.go +++ b/eth/tracers/api.go @@ -88,9 +88,6 @@ type Backend interface { ChainDb() ethdb.Database StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, readOnly bool, preferDisk bool) (*state.StateDB, StateReleaseFunc, error) StateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (*types.Transaction, vm.BlockContext, *state.StateDB, StateReleaseFunc, error) - - // [rollup-geth] - GetL1RpcClient() vm.L1RpcClient } // API is the collection of tracing APIs exposed over the private debugging endpoint. @@ -965,13 +962,7 @@ func (api *API) TraceCall(ctx context.Context, args ethapi.TransactionArgs, bloc precompiles := vm.ActivePrecompiledContracts(rules) //[rollup-geth] - rollupsConfig := vm.RollupPrecompileActivationConfig{ - L1SLoad: vm.L1SLoad{ - L1RpcClient: api.backend.GetL1RpcClient(), - GetLatestL1BlockNumber: vm.LetRPCDecideLatestL1Number, - }, - } - precompiles.ActivateRollupPrecompiledContracts(rules, &rollupsConfig) + precompiles.ActivateRollupPrecompiledContracts(rules, vm.RollupPrecompileActivationConfig{}) if err := config.StateOverrides.Apply(statedb, precompiles); err != nil { return nil, err diff --git a/eth/tracers/api_rollup_test.go b/eth/tracers/api_rollup_test.go deleted file mode 100644 index 21581a015aef..000000000000 --- a/eth/tracers/api_rollup_test.go +++ /dev/null @@ -1,7 +0,0 @@ -package tracers - -import "github.com/ethereum/go-ethereum/core/vm" - -func (b *testBackend) GetL1RpcClient() vm.L1RpcClient { - return nil -} diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index b652bdf29a9a..37044b6f37a9 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -1166,14 +1166,10 @@ func doCall(ctx context.Context, b Backend, args TransactionArgs, state *state.S } rules := b.ChainConfig().Rules(blockCtx.BlockNumber, blockCtx.Random != nil, blockCtx.Time) precompiles := maps.Clone(vm.ActivePrecompiledContracts(rules)) + //[rollup-geth] - rollupConfig := vm.RollupPrecompileActivationConfig{ - L1SLoad: vm.L1SLoad{ - L1RpcClient: b.GetL1RpcClient(), - GetLatestL1BlockNumber: vm.LetRPCDecideLatestL1Number, - }, - } - precompiles.ActivateRollupPrecompiledContracts(rules, &rollupConfig) + rollupConfigOverrides := vm.RollupPrecompileActivationConfig{} + precompiles.ActivateRollupPrecompiledContracts(rules, rollupConfigOverrides) if err := overrides.Apply(state, precompiles); err != nil { return nil, err diff --git a/internal/ethapi/backend.go b/internal/ethapi/backend.go index 671e5017ac05..0e991592b4b3 100644 --- a/internal/ethapi/backend.go +++ b/internal/ethapi/backend.go @@ -96,9 +96,6 @@ type Backend interface { SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription BloomStatus() (uint64, uint64) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) - - //[rollup-geth] - GetL1RpcClient() vm.L1RpcClient } func GetAPIs(apiBackend Backend) []rpc.API { diff --git a/internal/ethapi/simulate.go b/internal/ethapi/simulate.go index 0b53dda8f3da..d8e5ba12453b 100644 --- a/internal/ethapi/simulate.go +++ b/internal/ethapi/simulate.go @@ -287,15 +287,9 @@ func (sim *simulator) activePrecompiles(base *types.Header) vm.PrecompiledContra rules = sim.chainConfig.Rules(base.Number, isMerge, base.Time) ) - //[rollup-geth] - rollupsConfig := vm.RollupPrecompileActivationConfig{ - L1SLoad: vm.L1SLoad{ - L1RpcClient: sim.b.GetL1RpcClient(), - GetLatestL1BlockNumber: vm.LetRPCDecideLatestL1Number, - }, - } precompiles := vm.ActivePrecompiledContracts(rules) - precompiles.ActivateRollupPrecompiledContracts(rules, &rollupsConfig) + //[rollup-geth] + precompiles.ActivateRollupPrecompiledContracts(rules, vm.RollupPrecompileActivationConfig{}) return maps.Clone(precompiles) } diff --git a/internal/ethapi/transaction_args_rollup_test.go b/internal/ethapi/transaction_args_rollup_test.go deleted file mode 100644 index 5004799d10a9..000000000000 --- a/internal/ethapi/transaction_args_rollup_test.go +++ /dev/null @@ -1,7 +0,0 @@ -package ethapi - -import "github.com/ethereum/go-ethereum/core/vm" - -func (b *backendMock) GetL1RpcClient() vm.L1RpcClient { - return nil -}