Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: optimize the request RPC timeout config #250

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func New(
aggLayerClient: aggLayerClient,
sequencerPrivateKey: sequencerPrivateKey,
witnessRetrievalChan: make(chan state.DBBatch),
rpcClient: rpc.NewBatchEndpoints(cfg.RPCURL),
rpcClient: rpc.NewBatchEndpoints(cfg.RPCURL, cfg.RPCTimeout.Duration),
}

if a.ctx == nil {
Expand Down
3 changes: 3 additions & 0 deletions aggregator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ type Config struct {
// RPCURL is the URL of the RPC server
RPCURL string `mapstructure:"RPCURL"`

// RPCTimeout is the timeout for the L2 RPC calls
RPCTimeout types.Duration `mapstructure:"RPCTimeout"`

// WitnessURL is the URL of the witness server
WitnessURL string `mapstructure:"WitnessURL"`

Expand Down
2 changes: 2 additions & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ MaxPendingTx = 1
MaxBatchesForL1 = 300
BlockFinality = "FinalizedBlock"
RPCURL = "{{L2URL}}"
RPCTimeout = "2m"
GetBatchWaitInterval = "10s"
[SequenceSender.EthTxManager]
FrequencyToMonitorTxs = "1s"
Expand Down Expand Up @@ -131,6 +132,7 @@ CleanupLockedProofsInterval = "2m"
GeneratingProofCleanupThreshold = "10m"
GasOffset = 0
RPCURL = "{{L2URL}}"
RPCTimeout = "2m"
WitnessURL = "{{WitnessURL}}"
UseFullWitness = false
SettlementBackend = "l1"
Expand Down
24 changes: 18 additions & 6 deletions rpc/batch.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package rpc

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

"github.com/0xPolygon/cdk-rpc/rpc"
"github.com/0xPolygon/cdk/log"
Expand All @@ -21,11 +23,15 @@ var (
const busyResponse = "busy"

type BatchEndpoints struct {
url string
url string
readTimeout time.Duration
}

func NewBatchEndpoints(url string) *BatchEndpoints {
return &BatchEndpoints{url: url}
func NewBatchEndpoints(url string, readTimeout time.Duration) *BatchEndpoints {
return &BatchEndpoints{
url: url,
readTimeout: readTimeout,
}
}

func (b *BatchEndpoints) GetBatch(batchNumber uint64) (*types.RPCBatch, error) {
Expand All @@ -45,7 +51,9 @@ func (b *BatchEndpoints) GetBatch(batchNumber uint64) (*types.RPCBatch, error) {

log.Infof("Getting batch %d from RPC", batchNumber)

response, err := rpc.JSONRPCCall(b.url, "zkevm_getBatchByNumber", batchNumber)
ctx, cancel := context.WithTimeout(context.Background(), b.readTimeout)
defer cancel()
response, err := rpc.JSONRPCCallWithContext(ctx, b.url, "zkevm_getBatchByNumber", batchNumber)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -92,7 +100,9 @@ func (b *BatchEndpoints) GetL2BlockTimestamp(blockHash string) (uint64, error) {

log.Infof("Getting l2 block timestamp from RPC. Block hash: %s", blockHash)

response, err := rpc.JSONRPCCall(b.url, "eth_getBlockByHash", blockHash, false)
ctx, cancel := context.WithTimeout(context.Background(), b.readTimeout)
defer cancel()
response, err := rpc.JSONRPCCallWithContext(ctx, b.url, "eth_getBlockByHash", blockHash, false)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -126,7 +136,9 @@ func (b *BatchEndpoints) GetWitness(batchNumber uint64, fullWitness bool) ([]byt

log.Infof("Requesting witness for batch %d of type %s", batchNumber, witnessType)

response, err = rpc.JSONRPCCall(b.url, "zkevm_getBatchWitness", batchNumber, witnessType)
ctx, cancel := context.WithTimeout(context.Background(), b.readTimeout)
defer cancel()
response, err = rpc.JSONRPCCallWithContext(ctx, b.url, "zkevm_getBatchWitness", batchNumber, witnessType)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions rpc/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func Test_getBatchFromRPC(t *testing.T) {
}))
defer srv.Close()

rcpBatchClient := NewBatchEndpoints(srv.URL)
rcpBatchClient := NewBatchEndpoints(srv.URL, 0)
rpcBatch, err := rcpBatchClient.GetBatch(tt.batch)
if rpcBatch != nil {
copiedrpcBatch := rpcBatch.DeepCopy()
Expand Down Expand Up @@ -187,7 +187,7 @@ func Test_getBatchWitnessRPC(t *testing.T) {
}))
defer srv.Close()

rcpBatchClient := NewBatchEndpoints(srv.URL)
rcpBatchClient := NewBatchEndpoints(srv.URL, 0)
witness, err := rcpBatchClient.GetWitness(tt.batch, false)
if tt.expectErr != nil {
require.Equal(t, tt.expectErr.Error(), err.Error())
Expand Down Expand Up @@ -252,7 +252,7 @@ func Test_getGetL2BlockTimestamp(t *testing.T) {
}))
defer srv.Close()

rcpBatchClient := NewBatchEndpoints(srv.URL)
rcpBatchClient := NewBatchEndpoints(srv.URL, 0)
timestamp, err := rcpBatchClient.GetL2BlockTimestamp(string(tt.blockHash))
if tt.expectErr != nil {
require.Equal(t, tt.expectErr.Error(), err.Error())
Expand Down
3 changes: 3 additions & 0 deletions sequencesender/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ type Config struct {
// RPCURL is the URL of the RPC server
RPCURL string `mapstructure:"RPCURL"`

// RPCTimeout is the timeout for the L2 RPC calls
RPCTimeout types.Duration `mapstructure:"RPCTimeout"`

// GetBatchWaitInterval is the time to wait to query for a new batch when there are no more batches available
GetBatchWaitInterval types.Duration `mapstructure:"GetBatchWaitInterval"`
}
2 changes: 1 addition & 1 deletion sequencesender/sequencesender.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func New(cfg Config, logger *log.Logger,
sequenceData: make(map[uint64]*sequenceData),
validStream: false,
TxBuilder: txBuilder,
rpcClient: rpc.NewBatchEndpoints(cfg.RPCURL),
rpcClient: rpc.NewBatchEndpoints(cfg.RPCURL, cfg.RPCTimeout.Duration),
}

logger.Infof("TxBuilder configuration: %s", txBuilder.String())
Expand Down
Loading