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 204ef24 commit 759dda7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 44 deletions.
58 changes: 30 additions & 28 deletions core/vm/contracts_rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@ import (
"github.com/ethereum/go-ethereum/params"
)

var rollupL1SloadAddress = common.BytesToAddress([]byte{0x10, 0x01})
type RollupPrecompiledContractsOverrides struct {
l1SLoadGetLatestL1Block func() *big.Int
}

var PrecompiledContractsRollupR0 = PrecompiledContracts{
rollupL1SloadAddress: &L1SLoad{},
func GenerateRollupPrecompiledContractsOverrides(evm *EVM) RollupPrecompiledContractsOverrides {
return RollupPrecompiledContractsOverrides{
l1SLoadGetLatestL1Block: getLatestL1BlockNumber(evm),
}
}

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

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

func activeRollupPrecompiledContracts(rules params.Rules) PrecompiledContracts {
Expand All @@ -30,44 +36,40 @@ func activeRollupPrecompiledContracts(rules params.Rules) PrecompiledContracts {
}
}

func (pc *PrecompiledContracts) ActivateRollupPrecompiledContracts(config RollupPrecompileActivationConfig) {
// NOTE: if L1SLoad was not activated via chain rules this is no-op
pc.activateL1SLoad(config.L1RpcClient, config.GetLatestL1BlockNumber)
}

func (evm *EVM) activateRollupPrecompiledContracts() {
evm.precompiles.ActivateRollupPrecompiledContracts(RollupPrecompileActivationConfig{
L1SLoad{L1RpcClient: evm.Config.L1RpcClient, GetLatestL1BlockNumber: evm.rollupPrecompileOverrides.l1SLoadGetLatestL1Block},
})
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 L1RpcClient
GetLatestL1BlockNumber func() *big.Int
type l1SLoad struct {
l1RpcClient L1Client
getLatestL1BlockNumber func() *big.Int
}

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

func (c *L1SLoad) Run(input []byte) ([]byte, error) {
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 (c *l1SLoad) isL1SLoadActive() bool {
return c.getLatestL1BlockNumber != nil && c.l1RpcClient != nil
}

func (pc *PrecompiledContracts) activateL1SLoad(l1RpcClient L1RpcClient, getLatestL1BlockNumber func() *big.Int) {
rulesSayContractShouldBeActive := (*pc)[rollupL1SloadAddress] != nil
paramsNotNil := l1RpcClient != nil && getLatestL1BlockNumber != nil

if shouldActivateL1SLoad := rulesSayContractShouldBeActive && paramsNotNil; shouldActivateL1SLoad {
(*pc)[rollupL1SloadAddress] = &L1SLoad{
L1RpcClient: l1RpcClient,
GetLatestL1BlockNumber: getLatestL1BlockNumber,
func (pc *PrecompiledContracts) activateL1SLoad(l1RpcClient L1Client, getLatestL1BlockNumber func() *big.Int) {
if (*pc)[rollupL1SloadAddress] != nil {
(*pc)[rollupL1SloadAddress] = &l1SLoad{
l1RpcClient: l1RpcClient,
getLatestL1BlockNumber: getLatestL1BlockNumber,
}
}
}
17 changes: 1 addition & 16 deletions core/vm/contracts_rollup_overrides.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

package vm

import (
"math/big"
)
import "math/big"

type RollupPrecompiledContractsOverrides struct {
l1SLoadGetLatestL1Block func() *big.Int
Expand All @@ -25,16 +23,3 @@ func getLatestL1BlockNumber(evm *EVM) func() *big.Int {
return evm.Context.BlockNumber
}
}

// [OVERRIDE] getLatestL1BlockNumber
// Each rollup should override this function so that it returns
// correct latest L1 block number
//
// EXAMPLE 2
// func getLatestL1BlockNumber(evm *EVM) func() *big.Int {
// return func() *big.Int {
// addressOfL1BlockContract := common.Address{}
// slotInContractRepresentingL1BlockNumber := common.Hash{}
// return evm.StateDB.GetState(addressOfL1BlockContract, slotInContractRepresentingL1BlockNumber).Big()
// }
// }

0 comments on commit 759dda7

Please sign in to comment.