Skip to content

Commit

Permalink
added L1SLoad sekelton
Browse files Browse the repository at this point in the history
  • Loading branch information
mralj committed Oct 4, 2024
1 parent 0ae7e7b commit 1974d92
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 1 deletion.
75 changes: 75 additions & 0 deletions core/vm/contracts_rollup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//[rollup-geth]
// These are rollup-geth specific precompiled contracts

package vm

import (
"errors"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/params"
)

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

func GenerateRollupPrecompiledContractsOverrides(evm *EVM) RollupPrecompiledContractsOverrides {
return RollupPrecompiledContractsOverrides{
l1SLoadGetLatestL1Block: getLatestL1BlockNumber(evm),
}
}

var rollupL1SloadAddress = common.BytesToAddress([]byte{0x10, 0x01})

var PrecompiledContractsRollupR0 = PrecompiledContracts{
rollupL1SloadAddress: &l1SLoad{},
}

func activeRollupPrecompiledContracts(rules params.Rules) PrecompiledContracts {
switch rules.IsR0 {
case rules.IsR0:
return PrecompiledContractsRollupR0
default:
return nil
}
}

func (evm *EVM) activateRollupPrecompiledContracts() {
activeRollupPrecompiles := activeRollupPrecompiledContracts(evm.chainRules)
for k, v := range activeRollupPrecompiles {
evm.precompiles[k] = v
}

// NOTE: if L1SLoad was not activated via chain rules this is no-op
evm.precompiles.activateL1SLoad(evm.Config.L1RpcClient, evm.rollupPrecompileOverrides.l1SLoadGetLatestL1Block)
}

type l1SLoad struct {
l1RpcClient L1Client
getLatestL1BlockNumber func() *big.Int
}

func (c *l1SLoad) RequiredGas(input []byte) uint64 { return 0 }

func (c *l1SLoad) Run(input []byte) ([]byte, error) {
if !c.isL1SLoadActive() {
return nil, errors.New("L1SLoad precompile not active")
}

return nil, nil
}

func (c *l1SLoad) isL1SLoadActive() bool {
return c.getLatestL1BlockNumber != nil && c.l1RpcClient != nil
}

func (pc *PrecompiledContracts) activateL1SLoad(l1RpcClient L1Client, getLatestL1BlockNumber func() *big.Int) {
if (*pc)[rollupL1SloadAddress] != nil {
(*pc)[rollupL1SloadAddress] = &l1SLoad{
l1RpcClient: l1RpcClient,
getLatestL1BlockNumber: getLatestL1BlockNumber,
}
}
}
25 changes: 25 additions & 0 deletions core/vm/contracts_rollup_overrides.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//[rollup-geth]
// These are rollup-geth specific precompiled contracts

package vm

import "math/big"

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

func GenerateRollupPrecompiledContractsOverrides(evm *EVM) RollupPrecompiledContractsOverrides {
return RollupPrecompiledContractsOverrides{
l1SLoadGetLatestL1Block: getLatestL1BlockNumber(evm),
}
}

// [OVERRIDE] getLatestL1BlockNumber
// Each rollup should override this function so that it returns
// correct latest L1 block number
func getLatestL1BlockNumber(evm *EVM) func() *big.Int {
return func() *big.Int {
return evm.Context.BlockNumber
}
}
11 changes: 10 additions & 1 deletion core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ type EVM struct {
// applied in opCall*.
callGasTemp uint64
// precompiles holds the precompiled contracts for the current epoch
precompiles map[common.Address]PrecompiledContract
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 @@ -128,6 +132,11 @@ func NewEVM(blockCtx BlockContext, txCtx TxContext, statedb StateDB, 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.interpreter = NewEVMInterpreter(evm)
return evm
}
Expand Down
2 changes: 2 additions & 0 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,7 @@ type Rules struct {
IsBerlin, IsLondon bool
IsMerge, IsShanghai, IsCancun, IsPrague bool
IsVerkle bool
IsR0 bool // [rollup-geth]
}

// Rules ensures c's ChainID is not nil.
Expand Down Expand Up @@ -924,5 +925,6 @@ func (c *ChainConfig) Rules(num *big.Int, isMerge bool, timestamp uint64) Rules
IsPrague: isMerge && c.IsPrague(num, timestamp),
IsVerkle: isVerkle,
IsEIP4762: isVerkle,
IsR0: true, // [rollup-geth]
}
}

0 comments on commit 1974d92

Please sign in to comment.