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

Commit

Permalink
feat(pkg): make chainID part of rpc.EthClient (#563)
Browse files Browse the repository at this point in the history
  • Loading branch information
mask-pp committed Feb 23, 2024
1 parent bacedb0 commit f5d1146
Show file tree
Hide file tree
Showing 20 changed files with 41 additions and 61 deletions.
2 changes: 1 addition & 1 deletion driver/anchor_tx_constructor/anchor_tx_constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (c *AnchorTxConstructor) transactOpts(
baseFee *big.Int,
) (*bind.TransactOpts, error) {
var (
signer = types.LatestSignerForChainID(c.rpc.L2ChainID)
signer = types.LatestSignerForChainID(c.rpc.L2.ChainID)
parentHeight = new(big.Int).Sub(l2Height, common.Big1)
)

Expand Down
2 changes: 1 addition & 1 deletion driver/chain_syncer/calldata/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func NewSyncer(
uint64(configs.BlockMaxGasLimit),
defaultMaxTxPerBlock,
configs.BlockMaxTxListBytes.Uint64(),
rpc.L2ChainID,
rpc.L2.ChainID,
),
}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion driver/chain_syncer/chain_syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (s *ChainSyncerTestSuite) TestAheadOfProtocolVerifiedHead2() {
// generate transactopts to interact with TaikoL1 contract with.
privKey, err := crypto.ToECDSA(common.FromHex(os.Getenv("L1_PROVER_PRIVATE_KEY")))
s.Nil(err)
opts, err := bind.NewKeyedTransactorWithChainID(privKey, s.RPCClient.L1ChainID)
opts, err := bind.NewKeyedTransactorWithChainID(privKey, s.RPCClient.L1.ChainID)
s.Nil(err)

head, err := s.RPCClient.L1.HeaderByNumber(context.Background(), nil)
Expand Down
5 changes: 3 additions & 2 deletions driver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
var (
l1Endpoint = os.Getenv("L1_NODE_WS_ENDPOINT")
l2Endpoint = os.Getenv("L2_EXECUTION_ENGINE_WS_ENDPOINT")
l2CheckPoint = os.Getenv("L2_EXECUTION_ENGINE_HTTP_ENDPOINT")
l2EngineEndpoint = os.Getenv("L2_EXECUTION_ENGINE_AUTH_ENDPOINT")
taikoL1 = os.Getenv("TAIKO_L1_ADDRESS")
taikoL2 = os.Getenv("TAIKO_L2_ADDRESS")
Expand All @@ -35,7 +36,7 @@ func (s *DriverTestSuite) TestNewConfigFromCliContext() {
s.NotEmpty(c.JwtSecret)
s.Nil(new(Driver).InitFromCli(context.Background(), ctx))
s.True(c.P2PSyncVerifiedBlocks)
s.Equal("http://localhost:8545", c.L2CheckPoint)
s.Equal(l2CheckPoint, c.L2CheckPoint)

return err
}
Expand All @@ -51,7 +52,7 @@ func (s *DriverTestSuite) TestNewConfigFromCliContext() {
"--" + flags.P2PSyncTimeout.Name, "120s",
"--" + flags.RPCTimeout.Name, "5s",
"--" + flags.P2PSyncVerifiedBlocks.Name,
"--" + flags.CheckPointSyncURL.Name, "http://localhost:8545",
"--" + flags.CheckPointSyncURL.Name, l2CheckPoint,
}))
}

Expand Down
2 changes: 1 addition & 1 deletion internal/testutils/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func ProposeAndInsertValidBlock(
baseFee,
[]byte{},
)
signedTx, err := types.SignTx(tx, types.LatestSignerForChainID(s.RPCClient.L2ChainID), s.TestAddrPrivKey)
signedTx, err := types.SignTx(tx, types.LatestSignerForChainID(s.RPCClient.L2.ChainID), s.TestAddrPrivKey)
s.Nil(err)
s.Nil(s.RPCClient.L2.SendTransaction(context.Background(), signedTx))

Expand Down
4 changes: 2 additions & 2 deletions internal/testutils/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (s *ClientTestSuite) SetupTest() {
s.Nil(err)
s.Greater(balance.Cmp(common.Big0), 0)

opts, err := bind.NewKeyedTransactorWithChainID(ownerPrivKey, rpcCli.L1ChainID)
opts, err := bind.NewKeyedTransactorWithChainID(ownerPrivKey, rpcCli.L1.ChainID)
s.Nil(err)
proverBalance := new(big.Int).Div(balance, common.Big2)
s.Greater(proverBalance.Cmp(common.Big0), 0)
Expand All @@ -102,7 +102,7 @@ func (s *ClientTestSuite) setAllowance(key *ecdsa.PrivateKey) {

bigInt := new(big.Int).Exp(big.NewInt(1_000_000_000), new(big.Int).SetUint64(uint64(decimal)), nil)

opts, err := bind.NewKeyedTransactorWithChainID(key, s.RPCClient.L1ChainID)
opts, err := bind.NewKeyedTransactorWithChainID(key, s.RPCClient.L1.ChainID)
s.Nil(err)

_, err = s.RPCClient.TaikoToken.Approve(
Expand Down
7 changes: 1 addition & 6 deletions pkg/chain_iterator/block_batch_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,6 @@ func NewBlockBatchIterator(ctx context.Context, cfg *BlockBatchIteratorConfig) (
return nil, errors.New("invalid callback")
}

chainID, err := cfg.Client.ChainID(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get chain ID, error: %w", err)
}

if cfg.StartHeight == nil {
return nil, errors.New("invalid start height")
}
Expand All @@ -102,7 +97,7 @@ func NewBlockBatchIterator(ctx context.Context, cfg *BlockBatchIteratorConfig) (
iterator := &BlockBatchIterator{
ctx: ctx,
client: cfg.Client,
chainID: chainID,
chainID: cfg.Client.ChainID,
startHeight: cfg.StartHeight.Uint64(),
onBlocks: cfg.OnBlocks,
current: startHeader,
Expand Down
17 changes: 1 addition & 16 deletions pkg/rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package rpc
import (
"context"
"fmt"
"math/big"
"time"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/prysmaticlabs/prysm/v4/api/client"
"github.com/prysmaticlabs/prysm/v4/api/client/beacon"

"github.com/taikoxyz/taiko-client/bindings"
)

Expand All @@ -32,9 +32,6 @@ type Client struct {
TaikoL2 *bindings.TaikoL2Client
TaikoToken *bindings.TaikoToken
GuardianProver *bindings.GuardianProver
// Chain IDs
L1ChainID *big.Int
L2ChainID *big.Int
}

// ClientConfig contains all configs which will be used to initializing an
Expand Down Expand Up @@ -71,16 +68,6 @@ func NewClient(ctx context.Context, cfg *ClientConfig) (*Client, error) {
return nil, err
}

l1ChainID, err := l1Client.ChainID(ctxWithTimeout)
if err != nil {
return nil, err
}

l2ChainID, err := l2Client.ChainID(ctxWithTimeout)
if err != nil {
return nil, err
}

taikoL1, err := bindings.NewTaikoL1Client(cfg.TaikoL1Address, l1Client)
if err != nil {
return nil, err
Expand Down Expand Up @@ -153,8 +140,6 @@ func NewClient(ctx context.Context, cfg *ClientConfig) (*Client, error) {
TaikoL2: taikoL2,
TaikoToken: taikoToken,
GuardianProver: guardianProver,
L1ChainID: l1ChainID,
L2ChainID: l2ChainID,
}

if err := client.ensureGenesisMatched(ctxWithTimeout); err != nil {
Expand Down
20 changes: 11 additions & 9 deletions pkg/rpc/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type ethClient struct {

// EthClient is a wrapper for go-ethereum eth client with a timeout attached.
type EthClient struct {
ChainID *big.Int

*rpc.Client
*gethClient
*ethClient
Expand All @@ -42,22 +44,22 @@ func NewEthClient(ctx context.Context, url string, timeout time.Duration) (*EthC
return nil, err
}

ethClient := &ethClient{ethclient.NewClient(client)}
// Get chainID.
chainID, err := ethClient.ChainID(ctx)
if err != nil {
return nil, err
}

return &EthClient{
ChainID: chainID,
Client: client,
gethClient: &gethClient{gethclient.New(client)},
ethClient: &ethClient{ethclient.NewClient(client)},
ethClient: ethClient,
timeout: timeoutVal,
}, nil
}

// ChainID retrieves the current chain ID for transaction replay protection.
func (c *EthClient) ChainID(ctx context.Context) (*big.Int, error) {
ctxWithTimeout, cancel := ctxWithTimeoutOrDefault(ctx, c.timeout)
defer cancel()

return c.ethClient.ChainID(ctxWithTimeout)
}

// BlockByHash returns the given full block.
//
// Note that loading full blocks requires two requests. Use HeaderByHash
Expand Down
2 changes: 1 addition & 1 deletion pkg/rpc/methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func TestGetSyncedL1SnippetFromAnchor(t *testing.T) {
testAddrPrivKey, err := crypto.ToECDSA(common.Hex2Bytes(encoding.GoldenTouchPrivKey))
require.Nil(t, err)

opts, err := bind.NewKeyedTransactorWithChainID(testAddrPrivKey, client.L2ChainID)
opts, err := bind.NewKeyedTransactorWithChainID(testAddrPrivKey, client.L2.ChainID)
require.Nil(t, err)

opts.NoSend = true
Expand Down
5 changes: 1 addition & 4 deletions pkg/rpc/tx_blob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ func TestBlockTx(t *testing.T) {
sk, err := crypto.ToECDSA(common.FromHex(priv))
assert.NoError(t, err)

chainID, err := l1Client.ChainID(ctx)
assert.NoError(t, err)

opts, err := bind.NewKeyedTransactorWithChainID(sk, chainID)
opts, err := bind.NewKeyedTransactorWithChainID(sk, l1Client.ChainID)
assert.NoError(t, err)
opts.Context = ctx
//opts.NoSend = true
Expand Down
2 changes: 1 addition & 1 deletion pkg/rpc/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestL1ContentFrom(t *testing.T) {
baseFee,
[]byte{},
)
signedTx, err := types.SignTx(tx, types.LatestSignerForChainID(client.L2ChainID), testAddrPrivKey)
signedTx, err := types.SignTx(tx, types.LatestSignerForChainID(client.L2.ChainID), testAddrPrivKey)
require.Nil(t, err)
require.Nil(t, client.L2.SendTransaction(context.Background(), signedTx))

Expand Down
6 changes: 3 additions & 3 deletions proposer/proposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (p *Proposer) ProposeOp(ctx context.Context) error {
if p.LocalAddressesOnly {
var (
localTxsLists []types.Transactions
signer = types.LatestSignerForChainID(p.rpc.L2ChainID)
signer = types.LatestSignerForChainID(p.rpc.L2.ChainID)
)
for _, txs := range txLists {
var filtered types.Transactions
Expand Down Expand Up @@ -329,7 +329,7 @@ func (p *Proposer) sendProposeBlockTxWithBlobHash(
}

// Propose the transactions list
opts, err := getTxOpts(ctx, p.rpc.L1, p.L1ProposerPrivKey, p.rpc.L1ChainID, maxFee)
opts, err := getTxOpts(ctx, p.rpc.L1, p.L1ProposerPrivKey, p.rpc.L1.ChainID, maxFee)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -433,7 +433,7 @@ func (p *Proposer) sendProposeBlockTx(
}

// Propose the transactions list
opts, err := getTxOpts(ctx, p.rpc.L1, p.L1ProposerPrivKey, p.rpc.L1ChainID, maxFee)
opts, err := getTxOpts(ctx, p.rpc.L1, p.L1ProposerPrivKey, p.rpc.L1.ChainID, maxFee)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions proposer/proposer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (s *ProposerTestSuite) TestProposeOp() {

to := common.BytesToAddress(testutils.RandomBytes(32))
tx := types.NewTx(&types.DynamicFeeTx{
ChainID: s.RPCClient.L2ChainID,
ChainID: s.RPCClient.L2.ChainID,
Nonce: nonce,
GasTipCap: common.Big0,
GasFeeCap: new(big.Int).SetUint64(baseFee.Uint64() * 2),
Expand All @@ -102,7 +102,7 @@ func (s *ProposerTestSuite) TestProposeOp() {
Value: common.Big1,
})

signedTx, err := types.SignTx(tx, types.LatestSignerForChainID(s.p.rpc.L2ChainID), s.TestAddrPrivKey)
signedTx, err := types.SignTx(tx, types.LatestSignerForChainID(s.p.rpc.L2.ChainID), s.TestAddrPrivKey)
s.Nil(err)
s.Nil(s.p.rpc.L2.SendTransaction(context.Background(), signedTx))

Expand Down Expand Up @@ -160,7 +160,7 @@ func (s *ProposerTestSuite) TestSendProposeBlockTx() {
context.Background(),
s.p.rpc.L1,
s.p.L1ProposerPrivKey,
s.RPCClient.L1ChainID,
s.RPCClient.L1.ChainID,
fee,
)
s.Nil(err)
Expand All @@ -181,7 +181,7 @@ func (s *ProposerTestSuite) TestSendProposeBlockTx() {
s.SetL1Automine(false)
defer s.SetL1Automine(true)

signedTx, err := types.SignTx(tx, types.LatestSignerForChainID(s.RPCClient.L1ChainID), s.p.L1ProposerPrivKey)
signedTx, err := types.SignTx(tx, types.LatestSignerForChainID(s.RPCClient.L1.ChainID), s.p.L1ProposerPrivKey)
s.Nil(err)
s.Nil(s.RPCClient.L1.SendTransaction(context.Background(), signedTx))

Expand Down
6 changes: 3 additions & 3 deletions prover/anchor_tx_validator/anchor_tx_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type AnchorTxValidatorTestSuite struct {
func (s *AnchorTxValidatorTestSuite) SetupTest() {
s.ClientTestSuite.SetupTest()

validator, err := New(common.HexToAddress(os.Getenv("TAIKO_L2_ADDRESS")), s.RPCClient.L2ChainID, s.RPCClient)
validator, err := New(common.HexToAddress(os.Getenv("TAIKO_L2_ADDRESS")), s.RPCClient.L2.ChainID, s.RPCClient)
s.Nil(err)
s.v = validator
}
Expand All @@ -44,7 +44,7 @@ func (s *AnchorTxValidatorTestSuite) TestValidateAnchorTx() {

// invalid sender
dynamicFeeTxTx := &types.DynamicFeeTx{
ChainID: s.v.rpc.L2ChainID,
ChainID: s.v.rpc.L2.ChainID,
Nonce: 0,
GasTipCap: common.Big1,
GasFeeCap: common.Big1,
Expand All @@ -55,7 +55,7 @@ func (s *AnchorTxValidatorTestSuite) TestValidateAnchorTx() {
AccessList: types.AccessList{},
}

signer := types.LatestSignerForChainID(s.v.rpc.L2ChainID)
signer := types.LatestSignerForChainID(s.v.rpc.L2.ChainID)
tx = types.MustSignNewTx(wrongPrivKey, signer, dynamicFeeTxTx)

s.ErrorContains(
Expand Down
2 changes: 1 addition & 1 deletion prover/proof_submitter/proof_submitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func New(
txReplacementTipMultiplier uint64,
proveBlockMaxTxGasTipCap *big.Int,
) (*ProofSubmitter, error) {
anchorValidator, err := validator.New(taikoL2Address, rpcClient.L2ChainID, rpcClient)
anchorValidator, err := validator.New(taikoL2Address, rpcClient.L2.ChainID, rpcClient)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion prover/proof_submitter/transaction/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (a *ProveBlockTxBuilder) Build(
a.mutex.Lock()
defer a.mutex.Unlock()

txOpts, err := getProveBlocksTxOpts(ctx, a.rpc.L1, a.rpc.L1ChainID, a.proverPrivateKey)
txOpts, err := getProveBlocksTxOpts(ctx, a.rpc.L1, a.rpc.L1.ChainID, a.proverPrivateKey)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions prover/proof_submitter/transaction/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
)

func (s *TransactionTestSuite) TestGetProveBlocksTxOpts() {
optsL1, err := getProveBlocksTxOpts(context.Background(), s.RPCClient.L1, s.RPCClient.L1ChainID, s.TestAddrPrivKey)
optsL1, err := getProveBlocksTxOpts(context.Background(), s.RPCClient.L1, s.RPCClient.L1.ChainID, s.TestAddrPrivKey)
s.Nil(err)
s.Greater(optsL1.GasTipCap.Uint64(), uint64(0))

optsL2, err := getProveBlocksTxOpts(context.Background(), s.RPCClient.L2, s.RPCClient.L2ChainID, s.TestAddrPrivKey)
optsL2, err := getProveBlocksTxOpts(context.Background(), s.RPCClient.L2, s.RPCClient.L2.ChainID, s.TestAddrPrivKey)
s.Nil(err)
s.Greater(optsL2.GasTipCap.Uint64(), uint64(0))
}
Expand Down
2 changes: 1 addition & 1 deletion prover/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func (p *Prover) setApprovalAmount(ctx context.Context, contract common.Address)

opts, err := bind.NewKeyedTransactorWithChainID(
p.cfg.L1ProverPrivKey,
p.rpc.L1ChainID,
p.rpc.L1.ChainID,
)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion prover/prover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ func (s *ProverTestSuite) TestProveOp() {
}

func (s *ProverTestSuite) TestSetApprovalAmount() {
opts, err := bind.NewKeyedTransactorWithChainID(s.p.proverPrivateKey, s.p.rpc.L1ChainID)
opts, err := bind.NewKeyedTransactorWithChainID(s.p.proverPrivateKey, s.p.rpc.L1.ChainID)
s.Nil(err)

tx, err := s.p.rpc.TaikoToken.Approve(opts, s.p.cfg.AssignmentHookAddress, common.Big0)
Expand Down

0 comments on commit f5d1146

Please sign in to comment.