Commit 715732e7 authored by Adrian Sutton's avatar Adrian Sutton Committed by GitHub

op-node: Extract contract binding for SystemConfig (#11227)

parent 3c1d3b87
......@@ -4,12 +4,10 @@ import (
"context"
"errors"
"fmt"
"math/big"
"github.com/ethereum-optimism/optimism/op-service/sources/batching"
"github.com/urfave/cli/v2"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
......@@ -165,18 +163,12 @@ var Subcommands = cli.Commands{
return fmt.Errorf("cannot dial %s: %w", l1RPC, err)
}
funcDef := "startBlock()"
funcHash := crypto.Keccak256Hash([]byte(funcDef))
msg := ethereum.CallMsg{
To: &config.SystemConfigProxy,
Data: funcHash[:4], // hardcode the 4byte sig
}
result, err := client.CallContract(context.Background(), msg, nil)
caller := batching.NewMultiCaller(client.Client(), batching.DefaultBatchSize)
sysCfg := NewSystemConfigContract(caller, config.SystemConfigProxy)
l1StartBlockNum, err := sysCfg.StartBlock(context.Background())
if err != nil {
return fmt.Errorf("failed to fetch startBlock from SystemConfig contract: %w", err)
return fmt.Errorf("failed to fetch startBlock from SystemConfig: %w", err)
}
l1StartBlockNum := new(big.Int).SetBytes(result)
l1StartBlock, err := client.BlockByNumber(context.Background(), l1StartBlockNum)
if err != nil {
return fmt.Errorf("cannot fetch block by number: %w", err)
......
package genesis
import (
"context"
"fmt"
"math/big"
"github.com/ethereum-optimism/optimism/op-service/sources/batching"
"github.com/ethereum-optimism/optimism/op-service/sources/batching/rpcblock"
"github.com/ethereum-optimism/optimism/packages/contracts-bedrock/snapshots"
"github.com/ethereum/go-ethereum/common"
)
var (
methodStartBlock = "startBlock"
)
type SystemConfigContract struct {
caller *batching.MultiCaller
contract *batching.BoundContract
}
func NewSystemConfigContract(caller *batching.MultiCaller, addr common.Address) *SystemConfigContract {
return &SystemConfigContract{
caller: caller,
contract: batching.NewBoundContract(snapshots.LoadSystemConfigABI(), addr),
}
}
func (c *SystemConfigContract) StartBlock(ctx context.Context) (*big.Int, error) {
result, err := c.caller.SingleCall(ctx, rpcblock.Latest, c.contract.Call(methodStartBlock))
if err != nil {
return nil, fmt.Errorf("failed to call startBlock: %w", err)
}
return result.GetBigInt(0), nil
}
package genesis
import (
"context"
"math/big"
"testing"
"github.com/ethereum-optimism/optimism/op-service/sources/batching"
"github.com/ethereum-optimism/optimism/op-service/sources/batching/rpcblock"
batchingTest "github.com/ethereum-optimism/optimism/op-service/sources/batching/test"
"github.com/ethereum-optimism/optimism/packages/contracts-bedrock/snapshots"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
)
func TestSystemConfigContract_StartBlock(t *testing.T) {
addr := common.Address{0xaa}
sysCfgAbi := snapshots.LoadSystemConfigABI()
stubRpc := batchingTest.NewAbiBasedRpc(t, addr, sysCfgAbi)
caller := batching.NewMultiCaller(stubRpc, batching.DefaultBatchSize)
sysCfg := NewSystemConfigContract(caller, addr)
expected := big.NewInt(56)
stubRpc.SetResponse(addr, methodStartBlock, rpcblock.Latest, nil, []interface{}{expected})
result, err := sysCfg.StartBlock(context.Background())
require.NoError(t, err)
require.Truef(t, result.Cmp(expected) == 0, "expected %v, got %v", expected, result)
}
......@@ -22,6 +22,9 @@ var mips []byte
//go:embed abi/DelayedWETH.json
var delayedWETH []byte
//go:embed abi/SystemConfig.json
var systemConfig []byte
func LoadDisputeGameFactoryABI() *abi.ABI {
return loadABI(disputeGameFactory)
}
......@@ -38,6 +41,10 @@ func LoadDelayedWETHABI() *abi.ABI {
return loadABI(delayedWETH)
}
func LoadSystemConfigABI() *abi.ABI {
return loadABI(systemConfig)
}
func loadABI(json []byte) *abi.ABI {
if parsed, err := abi.JSON(bytes.NewReader(json)); err != nil {
panic(err)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment