Skip to content

Commit

Permalink
simplified the code
Browse files Browse the repository at this point in the history
  • Loading branch information
mralj committed Oct 7, 2024
1 parent 0f74390 commit 1ccbc95
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 40 deletions.
27 changes: 18 additions & 9 deletions core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"maps"
"math/big"
"slices"

"github.com/consensys/gnark-crypto/ecc"
bls12381 "github.com/consensys/gnark-crypto/ecc/bls12-381"
Expand Down Expand Up @@ -173,7 +174,7 @@ func init() {
}
}

func activePrecompiledContracts(rules params.Rules) PrecompiledContracts {
func activePrecompiledContracts(rules params.Rules, config *RollupPrecompileActivationConfig) PrecompiledContracts {
var activePrecompiles PrecompiledContracts
switch {
case rules.IsVerkle:
Expand All @@ -197,31 +198,39 @@ func activePrecompiledContracts(rules params.Rules) PrecompiledContracts {
for k, v := range activeRollupPrecompiles {
activePrecompiles[k] = v
}
activePrecompiles.ActivateRollupPrecompiledContracts(config)

return activePrecompiles
}

// ActivePrecompiledContracts returns a copy of precompiled contracts enabled with the current configuration.
func ActivePrecompiledContracts(rules params.Rules) PrecompiledContracts {
return maps.Clone(activePrecompiledContracts(rules))
func ActivePrecompiledContracts(rules params.Rules, rollupConfig *RollupPrecompileActivationConfig) PrecompiledContracts {
return maps.Clone(activePrecompiledContracts(rules, rollupConfig))
}

// ActivePrecompiles returns the precompile addresses enabled with the current configuration.
func ActivePrecompiles(rules params.Rules) []common.Address {
var activePrecompileAddresses []common.Address
switch {
case rules.IsPrague:
return PrecompiledAddressesPrague
activePrecompileAddresses = PrecompiledAddressesPrague
case rules.IsCancun:
return PrecompiledAddressesCancun
activePrecompileAddresses = PrecompiledAddressesCancun
case rules.IsBerlin:
return PrecompiledAddressesBerlin
activePrecompileAddresses = PrecompiledAddressesBerlin
case rules.IsIstanbul:
return PrecompiledAddressesIstanbul
activePrecompileAddresses = PrecompiledAddressesIstanbul
case rules.IsByzantium:
return PrecompiledAddressesByzantium
activePrecompileAddresses = PrecompiledAddressesByzantium
default:
return PrecompiledAddressesHomestead
activePrecompileAddresses = PrecompiledAddressesHomestead
}

// [rollup-geth]
activePrecompileAddresses =
append(activePrecompileAddresses, slices.Collect(maps.Keys(activeRollupPrecompiledContracts(rules)))...)

return activePrecompileAddresses
}

// RunPrecompiledContract runs and evaluates the output of a precompiled contract.
Expand Down
14 changes: 7 additions & 7 deletions core/vm/contracts_rollup_overrides.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ package vm

import "math/big"

type RollupPrecompiledContractsOverrides struct {
l1SLoadGetLatestL1Block func() *big.Int
}

func GenerateRollupPrecompiledContractsOverrides(evm *EVM) RollupPrecompiledContractsOverrides {
return RollupPrecompiledContractsOverrides{
l1SLoadGetLatestL1Block: getLatestL1BlockNumber(evm),
// generateRollupPrecompiledContractsOverrides generates rollup precompile config inlucing L2 specific overrides
func generateRollupPrecompiledContractsOverrides(evm *EVM) *RollupPrecompileActivationConfig {
return &RollupPrecompileActivationConfig{
L1SLoad{
L1RpcClient: evm.Config.L1RpcClient,
GetLatestL1BlockNumber: getLatestL1BlockNumber(evm),
},
}
}

Expand Down
8 changes: 1 addition & 7 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,6 @@ type EVM struct {
callGasTemp uint64
// precompiles holds the precompiled contracts for the current epoch
precompiles PrecompiledContracts

//[rollup-geth]
// Overrides specific to precompiled contracts for rollups
rollupPrecompileOverrides RollupPrecompiledContractsOverrides
}

// NewEVM returns a new EVM. The returned EVM is not thread safe and should
Expand All @@ -131,11 +127,9 @@ func NewEVM(blockCtx BlockContext, txCtx TxContext, statedb StateDB, chainConfig
chainConfig: chainConfig,
chainRules: chainConfig.Rules(blockCtx.BlockNumber, blockCtx.Random != nil, blockCtx.Time),
}
evm.precompiles = activePrecompiledContracts(evm.chainRules)

//[rollup-geth]
evm.rollupPrecompileOverrides = GenerateRollupPrecompiledContractsOverrides(evm)
evm.activateRollupPrecompiledContracts()
evm.precompiles = activePrecompiledContracts(evm.chainRules, generateRollupPrecompiledContractsOverrides(evm))

evm.interpreter = NewEVMInterpreter(evm)
return evm
Expand Down
15 changes: 9 additions & 6 deletions eth/tracers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -962,12 +962,15 @@ func (api *API) TraceCall(ctx context.Context, args ethapi.TransactionArgs, bloc
config.BlockOverrides.Apply(&vmctx)
rules := api.backend.ChainConfig().Rules(vmctx.BlockNumber, vmctx.Random != nil, vmctx.Time)

precompiles := vm.ActivePrecompiledContracts(rules)

//[rollup-geth] This is optional for rollups
precompiles.ActivateRollupPrecompiledContracts(vm.RollupPrecompileActivationConfig{
vm.L1SLoad{L1RpcClient: api.backend.GetL1RpcClient(), GetLatestL1BlockNumber: func() *big.Int { return vmctx.BlockNumber }},
})
//[rollup-geth] This is optional for rollups, instead we can simply do
// rollupsConfig := nil
rollupsConfig := vm.RollupPrecompileActivationConfig{
L1SLoad: vm.L1SLoad{
L1RpcClient: api.backend.GetL1RpcClient(),
GetLatestL1BlockNumber: func() *big.Int { return vmctx.BlockNumber }
},
}
precompiles := vm.ActivePrecompiledContracts(rules, &rollupsConfig)

if err := config.StateOverrides.Apply(statedb, precompiles); err != nil {
return nil, err
Expand Down
13 changes: 8 additions & 5 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1165,12 +1165,15 @@ func doCall(ctx context.Context, b Backend, args TransactionArgs, state *state.S
blockOverrides.Apply(&blockCtx)
}
rules := b.ChainConfig().Rules(blockCtx.BlockNumber, blockCtx.Random != nil, blockCtx.Time)
precompiles := maps.Clone(vm.ActivePrecompiledContracts(rules))

//[rollup-geth] This is optional for rollups
precompiles.ActivateRollupPrecompiledContracts(vm.RollupPrecompileActivationConfig{
vm.L1SLoad{L1RpcClient: b.GetL1RpcClient(), GetLatestL1BlockNumber: func() *big.Int { return blockCtx.BlockNumber }},
})
//[rollup-geth] This is optional for rollups, instead we can simply do
// rollupsConfig := nil
rollupConfig := vm.RollupPrecompileActivationConfig{
L1SLoad: vm.L1SLoad{
L1RpcClient: b.GetL1RpcClient(),
GetLatestL1BlockNumber: func() *big.Int { return blockCtx.BlockNumber }},
}
precompiles := maps.Clone(vm.ActivePrecompiledContracts(rules, &rollupConfig))

if err := overrides.Apply(state, precompiles); err != nil {
return nil, err
Expand Down
15 changes: 9 additions & 6 deletions internal/ethapi/simulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,15 @@ func (sim *simulator) activePrecompiles(base *types.Header) vm.PrecompiledContra
isMerge = base.Difficulty.Sign() == 0
rules = sim.chainConfig.Rules(base.Number, isMerge, base.Time)
)
precompiles := vm.ActivePrecompiledContracts(rules)

//[rollup-geth] This is optional for rollups
precompiles.ActivateRollupPrecompiledContracts(vm.RollupPrecompileActivationConfig{
L1SLoad: vm.L1SLoad{L1RpcClient: sim.b.GetL1RpcClient(), GetLatestL1BlockNumber: func() *big.Int { return base.Number }},
})
//[rollup-geth] This is optional for rollups, instead we can simply do
// rollupsConfig := nil
rollupsConfig := vm.RollupPrecompileActivationConfig{
L1SLoad: vm.L1SLoad{
L1RpcClient: sim.b.GetL1RpcClient(),
GetLatestL1BlockNumber: func() *big.Int { return base.Number },
},
}
precompiles := vm.ActivePrecompiledContracts(rules, &rollupsConfig)

return maps.Clone(precompiles)
}
Expand Down

0 comments on commit 1ccbc95

Please sign in to comment.