Skip to content
This repository has been archived by the owner on May 11, 2024. It is now read-only.

feat(pkg): improve archive node check #334

Merged
merged 2 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions pkg/rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@ package rpc

import (
"context"
"errors"
"fmt"
"math/big"
"time"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/ethclient/gethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/taikoxyz/taiko-client/bindings"
)

var (
errNotArchiveNode = errors.New("error with rpc: node must be archive node")
)

// Client contains all L1/L2 RPC clients that a driver needs.
type Client struct {
// Geth ethclient clients
Expand Down Expand Up @@ -62,15 +59,6 @@ func NewClient(ctx context.Context, cfg *ClientConfig) (*Client, error) {
return nil, err
}

isArchive, err := IsArchiveNode(ctx, l1RPC)
if err != nil {
return nil, err
}

if !isArchive {
return nil, errNotArchiveNode
}

taikoL1, err := bindings.NewTaikoL1Client(cfg.TaikoL1Address, l1RPC)
if err != nil {
return nil, err
Expand All @@ -94,6 +82,20 @@ func NewClient(ctx context.Context, cfg *ClientConfig) (*Client, error) {
return nil, err
}

stateVars, err := taikoL1.GetStateVariables(&bind.CallOpts{Context: ctx})
if err != nil {
return nil, err
}

isArchive, err := IsArchiveNode(ctx, l1RPC, stateVars.GenesisHeight)
if err != nil {
return nil, err
}

if !isArchive {
return nil, fmt.Errorf("error with RPC endpoint: node (%s) must be archive node", cfg.L1Endpoint)
}

l1RawRPC, err := rpc.Dial(cfg.L1Endpoint)
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions pkg/rpc/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ func StringToBytes32(str string) [32]byte {
return b
}

func IsArchiveNode(ctx context.Context, client *ethclient.Client) (bool, error) {
_, err := client.BalanceAt(ctx, zeroAddress, big.NewInt(1))
if err != nil {
// IsArchiveNode checks if the given node is an archive node.
func IsArchiveNode(ctx context.Context, client *ethclient.Client, l2GenesisHeight uint64) (bool, error) {
if _, err := client.BalanceAt(ctx, zeroAddress, new(big.Int).SetUint64(l2GenesisHeight)); err != nil {
if strings.Contains(err.Error(), "missing trie node") {
return false, nil
}
Expand Down
Loading