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

feat(client): simplify client #521

Merged
merged 11 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ linters:
- gofmt
- ineffassign
- importas
- lll
- makezero
- misspell
- misspell
Expand Down
2 changes: 1 addition & 1 deletion cmd/utils/sub_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func SubcommandAction(app SubcommandApplication) cli.ActionFunc {
logger.InitLogger(c)

ctx, ctxClose := context.WithCancel(context.Background())
defer func() { ctxClose() }()
defer ctxClose()

if err := app.InitFromCli(ctx, c); err != nil {
return err
Expand Down
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 @@ -68,7 +68,7 @@ func (c *AnchorTxConstructor) AssembleAnchorTx(
return nil, err
}

signalRoot, err := c.rpc.GetStorageRoot(ctx, c.rpc.L1GethClient, c.signalServiceAddress, l1Height)
signalRoot, err := c.rpc.GetStorageRoot(ctx, c.rpc.L1, c.signalServiceAddress, l1Height)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions driver/chain_syncer/chain_syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,15 @@ func TestChainSyncerTestSuite(t *testing.T) {

func (s *ChainSyncerTestSuite) TakeSnapshot() {
// record snapshot state to revert to before changes
s.Nil(s.RPCClient.L1RawRPC.CallContext(context.Background(), &s.snapshotID, "evm_snapshot"))
s.Nil(s.RPCClient.L1.CallContext(context.Background(), &s.snapshotID, "evm_snapshot"))
}

func (s *ChainSyncerTestSuite) RevertSnapshot() {
// revert to the snapshot state so protocol configs are unaffected
var revertRes bool
s.Nil(s.RPCClient.L1RawRPC.CallContext(context.Background(), &revertRes, "evm_revert", s.snapshotID))
s.Nil(s.RPCClient.L1.CallContext(context.Background(), &revertRes, "evm_revert", s.snapshotID))
s.True(revertRes)
s.Nil(rpc.SetHead(context.Background(), s.RPCClient.L2RawRPC, common.Big0))
s.Nil(rpc.SetHead(context.Background(), s.RPCClient.L2, common.Big0))
}

func (s *ChainSyncerTestSuite) TestAheadOfProtocolVerifiedHead() {
Expand Down
12 changes: 6 additions & 6 deletions driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (s *DriverTestSuite) TestProcessL1Blocks() {

func (s *DriverTestSuite) TestCheckL1ReorgToHigherFork() {
var testnetL1SnapshotID string
s.Nil(s.RPCClient.L1RawRPC.CallContext(context.Background(), &testnetL1SnapshotID, "evm_snapshot"))
s.Nil(s.RPCClient.L1.CallContext(context.Background(), &testnetL1SnapshotID, "evm_snapshot"))
s.NotEmpty(testnetL1SnapshotID)

l1Head1, err := s.d.rpc.L1.HeaderByNumber(context.Background(), nil)
Expand Down Expand Up @@ -158,7 +158,7 @@ func (s *DriverTestSuite) TestCheckL1ReorgToHigherFork() {

// Reorg back to l2Head1
var revertRes bool
s.Nil(s.RPCClient.L1RawRPC.CallContext(context.Background(), &revertRes, "evm_revert", testnetL1SnapshotID))
s.Nil(s.RPCClient.L1.CallContext(context.Background(), &revertRes, "evm_revert", testnetL1SnapshotID))
s.True(revertRes)

l1Head3, err := s.d.rpc.L1.HeaderByNumber(context.Background(), nil)
Expand Down Expand Up @@ -191,7 +191,7 @@ func (s *DriverTestSuite) TestCheckL1ReorgToHigherFork() {

func (s *DriverTestSuite) TestCheckL1ReorgToLowerFork() {
var testnetL1SnapshotID string
s.Nil(s.RPCClient.L1RawRPC.CallContext(context.Background(), &testnetL1SnapshotID, "evm_snapshot"))
s.Nil(s.RPCClient.L1.CallContext(context.Background(), &testnetL1SnapshotID, "evm_snapshot"))
s.NotEmpty(testnetL1SnapshotID)

l1Head1, err := s.d.rpc.L1.HeaderByNumber(context.Background(), nil)
Expand Down Expand Up @@ -221,7 +221,7 @@ func (s *DriverTestSuite) TestCheckL1ReorgToLowerFork() {

// Reorg back to l2Head1
var revertRes bool
s.Nil(s.RPCClient.L1RawRPC.CallContext(context.Background(), &revertRes, "evm_revert", testnetL1SnapshotID))
s.Nil(s.RPCClient.L1.CallContext(context.Background(), &revertRes, "evm_revert", testnetL1SnapshotID))
s.True(revertRes)

l1Head3, err := s.d.rpc.L1.HeaderByNumber(context.Background(), nil)
Expand Down Expand Up @@ -251,7 +251,7 @@ func (s *DriverTestSuite) TestCheckL1ReorgToLowerFork() {

func (s *DriverTestSuite) TestCheckL1ReorgToSameHeightFork() {
var testnetL1SnapshotID string
s.Nil(s.RPCClient.L1RawRPC.CallContext(context.Background(), &testnetL1SnapshotID, "evm_snapshot"))
s.Nil(s.RPCClient.L1.CallContext(context.Background(), &testnetL1SnapshotID, "evm_snapshot"))
s.NotEmpty(testnetL1SnapshotID)

l1Head1, err := s.d.rpc.L1.HeaderByNumber(context.Background(), nil)
Expand Down Expand Up @@ -281,7 +281,7 @@ func (s *DriverTestSuite) TestCheckL1ReorgToSameHeightFork() {

// Reorg back to l2Head1
var revertRes bool
s.Nil(s.RPCClient.L1RawRPC.CallContext(context.Background(), &revertRes, "evm_revert", testnetL1SnapshotID))
s.Nil(s.RPCClient.L1.CallContext(context.Background(), &revertRes, "evm_revert", testnetL1SnapshotID))
s.True(revertRes)

l1Head3, err := s.d.rpc.L1.HeaderByNumber(context.Background(), nil)
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set/v2 v2.1.0 // indirect
github.com/ethereum/c-kzg-4844 v0.4.0 // indirect
github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gballet/go-verkle v0.1.1-0.20231031103413-a67434b50f46 // indirect
github.com/getsentry/sentry-go v0.18.0 // indirect
Expand All @@ -56,6 +57,7 @@ require (
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/go-bexpr v0.1.10 // indirect
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
github.com/holiman/uint256 v1.2.4 // indirect
github.com/huin/goupnp v1.3.0 // indirect
Expand All @@ -73,6 +75,7 @@ require (
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/minio/sha256-simd v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/mitchellh/pointerstructure v1.2.0 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
Expand All @@ -86,6 +89,7 @@ require (
github.com/prysmaticlabs/gohashtree v0.0.3-alpha // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/rs/cors v1.7.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
Expand All @@ -107,6 +111,7 @@ require (
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.15.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/AndreasBriese/bbloom v0.0.0-20190306092124-e2d15f34fcf9/go.mod h1:bOvUY6CB00SOBii9/FifXqc0awNKxLFCL/+pkDPuyl8=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53/go.mod h1:+3IMCy2vIlbG1XG/0ggNQv0SvxCAIpPM5b1nCz56Xno=
github.com/CloudyKit/jet/v3 v3.0.0/go.mod h1:HKQPgSJmdK8hdoAbKUUWajkHyHo4RaU5rMdUywE7VMo=
github.com/DataDog/zstd v1.5.2 h1:vUG4lAyuPCXO0TLbXvPv7EB7cNK1QV/luu55UHLrrn8=
Expand Down
10 changes: 5 additions & 5 deletions internal/testutils/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (s *ClientTestSuite) SetupTest() {
_, err = rpc.WaitReceipt(context.Background(), rpcCli.L1, tx)
s.Nil(err)
}
s.Nil(rpcCli.L1RawRPC.CallContext(context.Background(), &s.testnetL1SnapshotID, "evm_snapshot"))
s.Nil(rpcCli.L1.CallContext(context.Background(), &s.testnetL1SnapshotID, "evm_snapshot"))
s.NotEmpty(s.testnetL1SnapshotID)
}

Expand Down Expand Up @@ -175,19 +175,19 @@ func (s *ClientTestSuite) setAddress(ownerPrivKey *ecdsa.PrivateKey, name [32]by

func (s *ClientTestSuite) TearDownTest() {
var revertRes bool
s.Nil(s.RPCClient.L1RawRPC.CallContext(context.Background(), &revertRes, "evm_revert", s.testnetL1SnapshotID))
s.Nil(s.RPCClient.L1.CallContext(context.Background(), &revertRes, "evm_revert", s.testnetL1SnapshotID))
s.True(revertRes)

s.Nil(rpc.SetHead(context.Background(), s.RPCClient.L2RawRPC, common.Big0))
s.Nil(rpc.SetHead(context.Background(), s.RPCClient.L2, common.Big0))
s.Nil(s.proverServer.Shutdown(context.Background()))
}

func (s *ClientTestSuite) SetL1Automine(automine bool) {
s.Nil(s.RPCClient.L1RawRPC.CallContext(context.Background(), nil, "evm_setAutomine", automine))
s.Nil(s.RPCClient.L1.CallContext(context.Background(), nil, "evm_setAutomine", automine))
}

func (s *ClientTestSuite) IncreaseTime(time uint64) {
var result uint64
s.Nil(s.RPCClient.L1RawRPC.CallContext(context.Background(), &result, "evm_increaseTime", time))
s.Nil(s.RPCClient.L1.CallContext(context.Background(), &result, "evm_increaseTime", time))
s.NotNil(result)
}
90 changes: 22 additions & 68 deletions pkg/rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import (

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient/gethclient"
"github.com/ethereum/go-ethereum/rpc"

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

Expand All @@ -24,12 +21,6 @@ type Client struct {
L1 *EthClient
L2 *EthClient
L2CheckPoint *EthClient
// Geth gethclient clients
L1GethClient *gethclient.Client
L2GethClient *gethclient.Client
// Geth raw RPC clients
L1RawRPC *rpc.Client
L2RawRPC *rpc.Client
// Geth Engine API clients
L2Engine *EngineClient
// Protocol contracts clients
Expand Down Expand Up @@ -65,33 +56,32 @@ func NewClient(ctx context.Context, cfg *ClientConfig) (*Client, error) {
ctxWithTimeout, cancel := ctxWithTimeoutOrDefault(ctx, defaultTimeout)
defer cancel()

if cfg.BackOffMaxRetries == 0 {
cfg.BackOffMaxRetries = 10
l1Client, err := NewEthClient(ctxWithTimeout, cfg.L1Endpoint, cfg.Timeout)
if err != nil {
return nil, err
}

l1EthClient, err := DialClientWithBackoff(ctxWithTimeout, cfg.L1Endpoint, cfg.RetryInterval, cfg.BackOffMaxRetries)
l2Client, err := NewEthClient(ctxWithTimeout, cfg.L2Endpoint, cfg.Timeout)
if err != nil {
return nil, err
}

l2EthClient, err := DialClientWithBackoff(ctxWithTimeout, cfg.L2Endpoint, cfg.RetryInterval, cfg.BackOffMaxRetries)
l1ChainID, err := l1Client.ChainID(ctxWithTimeout)
if err != nil {
return nil, err
}

var (
l1RPC *EthClient
l2RPC *EthClient
)
l1RPC = NewEthClientWithTimeout(l1EthClient, cfg.Timeout)
l2RPC = NewEthClientWithTimeout(l2EthClient, cfg.Timeout)
l2ChainID, err := l2Client.ChainID(ctxWithTimeout)
if err != nil {
return nil, err
}

taikoL1, err := bindings.NewTaikoL1Client(cfg.TaikoL1Address, l1RPC)
taikoL1, err := bindings.NewTaikoL1Client(cfg.TaikoL1Address, l1Client)
if err != nil {
return nil, err
}

taikoL2, err := bindings.NewTaikoL2Client(cfg.TaikoL2Address, l2RPC)
taikoL2, err := bindings.NewTaikoL2Client(cfg.TaikoL2Address, l2Client)
if err != nil {
return nil, err
}
Expand All @@ -101,12 +91,12 @@ func NewClient(ctx context.Context, cfg *ClientConfig) (*Client, error) {
guardianProver *bindings.GuardianProver
)
if cfg.TaikoTokenAddress.Hex() != ZeroAddress.Hex() {
if taikoToken, err = bindings.NewTaikoToken(cfg.TaikoTokenAddress, l1RPC); err != nil {
if taikoToken, err = bindings.NewTaikoToken(cfg.TaikoTokenAddress, l1Client); err != nil {
return nil, err
}
}
if cfg.GuardianProverAddress.Hex() != ZeroAddress.Hex() {
if guardianProver, err = bindings.NewGuardianProver(cfg.GuardianProverAddress, l1RPC); err != nil {
if guardianProver, err = bindings.NewGuardianProver(cfg.GuardianProverAddress, l1Client); err != nil {
return nil, err
}
}
Expand All @@ -115,73 +105,37 @@ func NewClient(ctx context.Context, cfg *ClientConfig) (*Client, error) {
if err != nil {
return nil, err
}

isArchive, err := IsArchiveNode(ctxWithTimeout, l1RPC, stateVars.A.GenesisHeight)
isArchive, err := IsArchiveNode(ctxWithTimeout, l1Client, stateVars.A.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
}

l2RawRPC, err := rpc.Dial(cfg.L2Endpoint)
if err != nil {
return nil, err
}

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

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

// If not providing L2EngineEndpoint or JwtSecret, then the L2Engine client
// won't be initialized.
var l2AuthRPC *EngineClient
var l2AuthClient *EngineClient
if len(cfg.L2EngineEndpoint) != 0 && len(cfg.JwtSecret) != 0 {
if l2AuthRPC, err = DialEngineClientWithBackoff(
ctxWithTimeout,
cfg.L2EngineEndpoint,
cfg.JwtSecret,
cfg.RetryInterval,
cfg.BackOffMaxRetries,
); err != nil {
l2AuthClient, err = NewJWTEngineClient(cfg.L2EngineEndpoint, cfg.JwtSecret)
if err != nil {
return nil, err
}
}

var l2CheckPoint *EthClient
if len(cfg.L2CheckPoint) != 0 {
l2CheckPointEthClient, err := DialClientWithBackoff(
ctxWithTimeout,
cfg.L2CheckPoint,
cfg.RetryInterval,
cfg.BackOffMaxRetries)
if cfg.L2CheckPoint != "" {
l2CheckPoint, err = NewEthClient(ctxWithTimeout, cfg.L2CheckPoint, cfg.Timeout)
if err != nil {
return nil, err
}
l2CheckPoint = NewEthClientWithTimeout(l2CheckPointEthClient, cfg.Timeout)
}

client := &Client{
L1: l1RPC,
L2: l2RPC,
L1: l1Client,
L2: l2Client,
L2CheckPoint: l2CheckPoint,
L1RawRPC: l1RawRPC,
L2RawRPC: l2RawRPC,
L1GethClient: gethclient.New(l1RawRPC),
L2GethClient: gethclient.New(l2RawRPC),
L2Engine: l2AuthRPC,
L2Engine: l2AuthClient,
TaikoL1: taikoL1,
TaikoL2: taikoL2,
TaikoToken: taikoToken,
Expand Down
Loading