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

Commit

Permalink
Merge branch 'main' into fix/simplify_code_2
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtaikocha committed Jan 21, 2024
2 parents bbefc07 + 5f9d843 commit bb301c1
Show file tree
Hide file tree
Showing 22 changed files with 186 additions and 262 deletions.
1 change: 1 addition & 0 deletions cmd/flags/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ var (
Name: "rpc.timeout",
Usage: "Timeout in seconds for RPC calls",
Category: commonCategory,
Value: 1 * time.Minute,
}
WaitReceiptTimeout = &cli.DurationFlag{
Name: "rpc.waitReceiptTimeout",
Expand Down
17 changes: 10 additions & 7 deletions driver/chain_syncer/calldata/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/taikoxyz/taiko-client/driver/state"
"github.com/taikoxyz/taiko-client/internal/testutils"
"github.com/taikoxyz/taiko-client/internal/utils"
"github.com/taikoxyz/taiko-client/pkg/rpc"
"github.com/taikoxyz/taiko-client/proposer"
)

Expand Down Expand Up @@ -47,12 +48,14 @@ func (s *CalldataSyncerTestSuite) SetupTest() {
s.Nil(err)
proposeInterval := 1024 * time.Hour // No need to periodically propose transactions list in unit tests

s.Nil(proposer.InitFromConfig(context.Background(), prop, (&proposer.Config{
L1Endpoint: os.Getenv("L1_NODE_WS_ENDPOINT"),
L2Endpoint: os.Getenv("L2_EXECUTION_ENGINE_WS_ENDPOINT"),
TaikoL1Address: common.HexToAddress(os.Getenv("TAIKO_L1_ADDRESS")),
TaikoL2Address: common.HexToAddress(os.Getenv("TAIKO_L2_ADDRESS")),
TaikoTokenAddress: common.HexToAddress(os.Getenv("TAIKO_TOKEN_ADDRESS")),
s.Nil(prop.InitFromConfig(context.Background(), &proposer.Config{
ClientConfig: &rpc.ClientConfig{
L1Endpoint: os.Getenv("L1_NODE_WS_ENDPOINT"),
L2Endpoint: os.Getenv("L2_EXECUTION_ENGINE_WS_ENDPOINT"),
TaikoL1Address: common.HexToAddress(os.Getenv("TAIKO_L1_ADDRESS")),
TaikoL2Address: common.HexToAddress(os.Getenv("TAIKO_L2_ADDRESS")),
TaikoTokenAddress: common.HexToAddress(os.Getenv("TAIKO_TOKEN_ADDRESS")),
},
AssignmentHookAddress: common.HexToAddress(os.Getenv("ASSIGNMENT_HOOK_ADDRESS")),
L1ProposerPrivKey: l1ProposerPrivKey,
ProposeInterval: &proposeInterval,
Expand All @@ -65,7 +68,7 @@ func (s *CalldataSyncerTestSuite) SetupTest() {
SgxAndPseZkevmTierFee: common.Big256,
MaxTierFeePriceBumps: 3,
TierFeePriceBump: common.Big2,
})))
}))

s.p = prop
}
Expand Down
16 changes: 9 additions & 7 deletions driver/chain_syncer/chain_syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ func (s *ChainSyncerTestSuite) SetupTest() {
s.Nil(err)
proposeInterval := 1024 * time.Hour // No need to periodically propose transactions list in unit tests

s.Nil(proposer.InitFromConfig(context.Background(), prop, (&proposer.Config{
L1Endpoint: os.Getenv("L1_NODE_WS_ENDPOINT"),
L2Endpoint: os.Getenv("L2_EXECUTION_ENGINE_WS_ENDPOINT"),
TaikoL1Address: common.HexToAddress(os.Getenv("TAIKO_L1_ADDRESS")),
TaikoL2Address: common.HexToAddress(os.Getenv("TAIKO_L2_ADDRESS")),
TaikoTokenAddress: common.HexToAddress(os.Getenv("TAIKO_TOKEN_ADDRESS")),
s.Nil(prop.InitFromConfig(context.Background(), &proposer.Config{
ClientConfig: &rpc.ClientConfig{
L1Endpoint: os.Getenv("L1_NODE_WS_ENDPOINT"),
L2Endpoint: os.Getenv("L2_EXECUTION_ENGINE_WS_ENDPOINT"),
TaikoL1Address: common.HexToAddress(os.Getenv("TAIKO_L1_ADDRESS")),
TaikoL2Address: common.HexToAddress(os.Getenv("TAIKO_L2_ADDRESS")),
TaikoTokenAddress: common.HexToAddress(os.Getenv("TAIKO_TOKEN_ADDRESS")),
},
AssignmentHookAddress: common.HexToAddress(os.Getenv("ASSIGNMENT_HOOK_ADDRESS")),
L1ProposerPrivKey: l1ProposerPrivKey,
ProposeInterval: &proposeInterval,
Expand All @@ -68,7 +70,7 @@ func (s *ChainSyncerTestSuite) SetupTest() {
MaxTierFeePriceBumps: 3,
TierFeePriceBump: common.Big2,
ExtraData: "test",
})))
}))

s.p = prop
}
Expand Down
39 changes: 15 additions & 24 deletions driver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,15 @@ import (

"github.com/taikoxyz/taiko-client/cmd/flags"
"github.com/taikoxyz/taiko-client/pkg/jwt"
"github.com/taikoxyz/taiko-client/pkg/rpc"
)

// Config contains the configurations to initialize a Taiko driver.
type Config struct {
L1Endpoint string
L2Endpoint string
L2EngineEndpoint string
L2CheckPoint string
TaikoL1Address common.Address
TaikoL2Address common.Address
JwtSecret string
*rpc.ClientConfig
P2PSyncVerifiedBlocks bool
P2PSyncTimeout time.Duration
BackOffRetryInterval time.Duration
RPCTimeout *time.Duration
RPCTimeout time.Duration
}

// NewConfigFromCliContext creates a new config instance from
Expand All @@ -44,24 +38,21 @@ func NewConfigFromCliContext(c *cli.Context) (*Config, error) {
return nil, errors.New("empty L2 check point URL")
}

var timeout *time.Duration

if c.IsSet(flags.RPCTimeout.Name) {
duration := c.Duration(flags.RPCTimeout.Name)
timeout = &duration
}

var timeout = c.Duration(flags.RPCTimeout.Name)
return &Config{
L1Endpoint: c.String(flags.L1WSEndpoint.Name),
L2Endpoint: c.String(flags.L2WSEndpoint.Name),
L2EngineEndpoint: c.String(flags.L2AuthEndpoint.Name),
L2CheckPoint: l2CheckPoint,
TaikoL1Address: common.HexToAddress(c.String(flags.TaikoL1Address.Name)),
TaikoL2Address: common.HexToAddress(c.String(flags.TaikoL2Address.Name)),
JwtSecret: string(jwtSecret),
ClientConfig: &rpc.ClientConfig{
L1Endpoint: c.String(flags.L1WSEndpoint.Name),
L2Endpoint: c.String(flags.L2WSEndpoint.Name),
L2CheckPoint: l2CheckPoint,
TaikoL1Address: common.HexToAddress(c.String(flags.TaikoL1Address.Name)),
TaikoL2Address: common.HexToAddress(c.String(flags.TaikoL2Address.Name)),
L2EngineEndpoint: c.String(flags.L2AuthEndpoint.Name),
JwtSecret: string(jwtSecret),
RetryInterval: c.Duration(flags.BackOffRetryInterval.Name),
Timeout: timeout,
},
P2PSyncVerifiedBlocks: p2pSyncVerifiedBlocks,
P2PSyncTimeout: c.Duration(flags.P2PSyncTimeout.Name),
BackOffRetryInterval: c.Duration(flags.BackOffRetryInterval.Name),
RPCTimeout: timeout,
}, nil
}
2 changes: 1 addition & 1 deletion driver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (s *DriverTestSuite) TestNewConfigFromCliContext() {
s.Equal(taikoL1, c.TaikoL1Address.String())
s.Equal(taikoL2, c.TaikoL2Address.String())
s.Equal(120*time.Second, c.P2PSyncTimeout)
s.Equal(rpcTimeout, *c.RPCTimeout)
s.Equal(rpcTimeout, c.RPCTimeout)
s.NotEmpty(c.JwtSecret)
s.Nil(new(Driver).InitFromCli(context.Background(), ctx))
s.True(c.P2PSyncVerifiedBlocks)
Expand Down
30 changes: 10 additions & 20 deletions driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
// Driver keeps the L2 execution engine's local block chain in sync with the TaikoL1
// contract.
type Driver struct {
*Config
rpc *rpc.Client
l2ChainSyncer *chainSyncer.L2ChainSyncer
state *state.State
Expand All @@ -36,9 +37,8 @@ type Driver struct {
l1HeadSub event.Subscription
syncNotify chan struct{}

backOffRetryInterval time.Duration
ctx context.Context
wg sync.WaitGroup
ctx context.Context
wg sync.WaitGroup
}

// InitFromCli New initializes the given driver instance based on the command line flags.
Expand All @@ -48,28 +48,18 @@ func (d *Driver) InitFromCli(ctx context.Context, c *cli.Context) error {
return err
}

return InitFromConfig(ctx, d, cfg)
return d.InitFromConfig(ctx, cfg)
}

// InitFromConfig initializes the driver instance based on the given configurations.
func InitFromConfig(ctx context.Context, d *Driver, cfg *Config) (err error) {
func (d *Driver) InitFromConfig(ctx context.Context, cfg *Config) (err error) {
d.l1HeadCh = make(chan *types.Header, 1024)
d.wg = sync.WaitGroup{}
d.syncNotify = make(chan struct{}, 1)
d.ctx = ctx
d.backOffRetryInterval = cfg.BackOffRetryInterval

if d.rpc, err = rpc.NewClient(d.ctx, &rpc.ClientConfig{
L1Endpoint: cfg.L1Endpoint,
L2Endpoint: cfg.L2Endpoint,
L2CheckPoint: cfg.L2CheckPoint,
TaikoL1Address: cfg.TaikoL1Address,
TaikoL2Address: cfg.TaikoL2Address,
L2EngineEndpoint: cfg.L2EngineEndpoint,
JwtSecret: cfg.JwtSecret,
RetryInterval: cfg.BackOffRetryInterval,
Timeout: cfg.RPCTimeout,
}); err != nil {
d.Config = cfg

if d.rpc, err = rpc.NewClient(d.ctx, cfg.ClientConfig); err != nil {
return err
}

Expand Down Expand Up @@ -143,7 +133,7 @@ func (d *Driver) eventLoop() {

// doSyncWithBackoff performs a synchronising operation with a backoff strategy.
doSyncWithBackoff := func() {
if err := backoff.Retry(d.doSync, backoff.NewConstantBackOff(d.backOffRetryInterval)); err != nil {
if err := backoff.Retry(d.doSync, backoff.NewConstantBackOff(d.RetryInterval)); err != nil {
log.Error("Sync L2 execution engine's block chain error", "error", err)
}
}
Expand Down Expand Up @@ -210,7 +200,7 @@ func (d *Driver) reportProtocolStatus() {
maxNumBlocks = configs.BlockMaxProposals
return nil
},
backoff.NewConstantBackOff(d.backOffRetryInterval),
backoff.NewConstantBackOff(d.RetryInterval),
); err != nil {
log.Error("Failed to get protocol state variables", "error", err)
return
Expand Down
35 changes: 20 additions & 15 deletions driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/taikoxyz/taiko-client/bindings/encoding"
"github.com/taikoxyz/taiko-client/internal/testutils"
"github.com/taikoxyz/taiko-client/pkg/jwt"
"github.com/taikoxyz/taiko-client/pkg/rpc"
"github.com/taikoxyz/taiko-client/proposer"
)

Expand All @@ -27,37 +28,41 @@ type DriverTestSuite struct {
func (s *DriverTestSuite) SetupTest() {
s.ClientTestSuite.SetupTest()

// Init driver
// InitFromConfig driver
jwtSecret, err := jwt.ParseSecretFromFile(os.Getenv("JWT_SECRET"))
s.Nil(err)
s.NotEmpty(jwtSecret)

d := new(Driver)
ctx, cancel := context.WithCancel(context.Background())
s.Nil(InitFromConfig(ctx, d, &Config{
L1Endpoint: os.Getenv("L1_NODE_WS_ENDPOINT"),
L2Endpoint: os.Getenv("L2_EXECUTION_ENGINE_WS_ENDPOINT"),
L2EngineEndpoint: os.Getenv("L2_EXECUTION_ENGINE_AUTH_ENDPOINT"),
TaikoL1Address: common.HexToAddress(os.Getenv("TAIKO_L1_ADDRESS")),
TaikoL2Address: common.HexToAddress(os.Getenv("TAIKO_L2_ADDRESS")),
JwtSecret: string(jwtSecret),
s.Nil(d.InitFromConfig(ctx, &Config{
ClientConfig: &rpc.ClientConfig{
L1Endpoint: os.Getenv("L1_NODE_WS_ENDPOINT"),
L2Endpoint: os.Getenv("L2_EXECUTION_ENGINE_WS_ENDPOINT"),
L2EngineEndpoint: os.Getenv("L2_EXECUTION_ENGINE_AUTH_ENDPOINT"),
TaikoL1Address: common.HexToAddress(os.Getenv("TAIKO_L1_ADDRESS")),
TaikoL2Address: common.HexToAddress(os.Getenv("TAIKO_L2_ADDRESS")),
JwtSecret: string(jwtSecret),
},
}))
s.d = d
s.cancel = cancel

// Init proposer
// InitFromConfig proposer
p := new(proposer.Proposer)

l1ProposerPrivKey, err := crypto.ToECDSA(common.FromHex(os.Getenv("L1_PROPOSER_PRIVATE_KEY")))
s.Nil(err)

proposeInterval := 1024 * time.Hour // No need to periodically propose transactions list in unit tests
s.Nil(proposer.InitFromConfig(context.Background(), p, &proposer.Config{
L1Endpoint: os.Getenv("L1_NODE_WS_ENDPOINT"),
L2Endpoint: os.Getenv("L2_EXECUTION_ENGINE_WS_ENDPOINT"),
TaikoL1Address: common.HexToAddress(os.Getenv("TAIKO_L1_ADDRESS")),
TaikoL2Address: common.HexToAddress(os.Getenv("TAIKO_L2_ADDRESS")),
TaikoTokenAddress: common.HexToAddress(os.Getenv("TAIKO_TOKEN_ADDRESS")),
s.Nil(p.InitFromConfig(context.Background(), &proposer.Config{
ClientConfig: &rpc.ClientConfig{
L1Endpoint: os.Getenv("L1_NODE_WS_ENDPOINT"),
L2Endpoint: os.Getenv("L2_EXECUTION_ENGINE_WS_ENDPOINT"),
TaikoL1Address: common.HexToAddress(os.Getenv("TAIKO_L1_ADDRESS")),
TaikoL2Address: common.HexToAddress(os.Getenv("TAIKO_L2_ADDRESS")),
TaikoTokenAddress: common.HexToAddress(os.Getenv("TAIKO_TOKEN_ADDRESS")),
},
AssignmentHookAddress: common.HexToAddress(os.Getenv("ASSIGNMENT_HOOK_ADDRESS")),
L1ProposerPrivKey: l1ProposerPrivKey,
ProposeInterval: &proposeInterval,
Expand Down
33 changes: 11 additions & 22 deletions pkg/rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,25 @@ type ClientConfig struct {
L2EngineEndpoint string
JwtSecret string
RetryInterval time.Duration
Timeout *time.Duration
BackOffMaxRetrys *big.Int
Timeout time.Duration
BackOffMaxRetries uint64
}

// NewClient initializes all RPC clients used by Taiko client software.
func NewClient(ctx context.Context, cfg *ClientConfig) (*Client, error) {
ctxWithTimeout, cancel := ctxWithTimeoutOrDefault(ctx, defaultTimeout)
defer cancel()

if cfg.BackOffMaxRetrys == nil {
defaultRetrys := new(big.Int).SetInt64(10)
cfg.BackOffMaxRetrys = defaultRetrys
if cfg.BackOffMaxRetries == 0 {
cfg.BackOffMaxRetries = 10
}

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

l2EthClient, err := DialClientWithBackoff(ctxWithTimeout, cfg.L2Endpoint, cfg.RetryInterval, cfg.BackOffMaxRetrys)
l2EthClient, err := DialClientWithBackoff(ctxWithTimeout, cfg.L2Endpoint, cfg.RetryInterval, cfg.BackOffMaxRetries)
if err != nil {
return nil, err
}
Expand All @@ -84,13 +83,8 @@ func NewClient(ctx context.Context, cfg *ClientConfig) (*Client, error) {
l1RPC *EthClient
l2RPC *EthClient
)
if cfg.Timeout != nil {
l1RPC = NewEthClientWithTimeout(l1EthClient, *cfg.Timeout)
l2RPC = NewEthClientWithTimeout(l2EthClient, *cfg.Timeout)
} else {
l1RPC = NewEthClientWithDefaultTimeout(l1EthClient)
l2RPC = NewEthClientWithDefaultTimeout(l2EthClient)
}
l1RPC = NewEthClientWithTimeout(l1EthClient, cfg.Timeout)
l2RPC = NewEthClientWithTimeout(l2EthClient, cfg.Timeout)

taikoL1, err := bindings.NewTaikoL1Client(cfg.TaikoL1Address, l1RPC)
if err != nil {
Expand Down Expand Up @@ -160,7 +154,7 @@ func NewClient(ctx context.Context, cfg *ClientConfig) (*Client, error) {
cfg.L2EngineEndpoint,
cfg.JwtSecret,
cfg.RetryInterval,
cfg.BackOffMaxRetrys,
cfg.BackOffMaxRetries,
); err != nil {
return nil, err
}
Expand All @@ -172,16 +166,11 @@ func NewClient(ctx context.Context, cfg *ClientConfig) (*Client, error) {
ctxWithTimeout,
cfg.L2CheckPoint,
cfg.RetryInterval,
cfg.BackOffMaxRetrys)
cfg.BackOffMaxRetries)
if err != nil {
return nil, err
}

if cfg.Timeout != nil {
l2CheckPoint = NewEthClientWithTimeout(l2CheckPointEthClient, *cfg.Timeout)
} else {
l2CheckPoint = NewEthClientWithDefaultTimeout(l2CheckPointEthClient)
}
l2CheckPoint = NewEthClientWithTimeout(l2CheckPointEthClient, cfg.Timeout)
}

client := &Client{
Expand Down
3 changes: 1 addition & 2 deletions pkg/rpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func newTestClient(t *testing.T) *Client {
}

func newTestClientWithTimeout(t *testing.T) *Client {
timeout := 5 * time.Second
client, err := NewClient(context.Background(), &ClientConfig{
L1Endpoint: os.Getenv("L1_NODE_WS_ENDPOINT"),
L2Endpoint: os.Getenv("L2_EXECUTION_ENGINE_WS_ENDPOINT"),
Expand All @@ -40,7 +39,7 @@ func newTestClientWithTimeout(t *testing.T) *Client {
L2EngineEndpoint: os.Getenv("L2_EXECUTION_ENGINE_AUTH_ENDPOINT"),
JwtSecret: os.Getenv("JWT_SECRET"),
RetryInterval: backoff.DefaultMaxInterval,
Timeout: &timeout,
Timeout: 5 * time.Second,
})

require.Nil(t, err)
Expand Down
Loading

0 comments on commit bb301c1

Please sign in to comment.