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

fix: check max timestamp before send to L1 #17

Open
wants to merge 10 commits into
base: dev
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 Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ build-go:
$(GOENVVARS) go build -ldflags "all=$(LDFLAGS)" -o $(GOBIN)/$(GOBINARY) $(GOCMD)

.PHONY: build-docker
build-docker: build-mock-signer-docker ## Builds a docker image with the cdk binary
build-docker: ## Builds a docker image with the cdk binary
docker build -t cdk -f ./Dockerfile .

.PHONY: build-mock-signer-docker
Expand Down
7 changes: 7 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,12 @@ func newTxBuilder(
switch contracts.VersionType(cfg.Common.ContractVersions) {
case contracts.VersionBanana:
if cfg.Common.IsValidiumMode {
// For X Layer
var l2RpcClient txbuilder.RPCInterface
if cfg.SequenceSender.CheckSendBatch {
l2RpcClient = rpc.NewBatchEndpoints(cfg.SequenceSender.RPCURL, cfg.SequenceSender.RPCTimeout.Duration)
}

txBuilder = txbuilder.NewTxBuilderBananaValidium(
logger,
ethman.Contracts.Banana.Rollup,
Expand All @@ -317,6 +323,7 @@ func newTxBuilder(
l1InfoTreeSync,
l1Client,
blockFinality,
l2RpcClient,
)
} else {
txBuilder = txbuilder.NewTxBuilderBananaZKEVM(
Expand Down
1 change: 1 addition & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ MaxBatchesForL1 = 300
BlockFinality = "FinalizedBlock"
RPCURL = "{{L2URL}}"
RPCTimeout = "60s"
CheckSendBatch = false
GetBatchWaitInterval = "10s"
[SequenceSender.EthTxManager]
FrequencyToMonitorTxs = "1s"
Expand Down
9 changes: 8 additions & 1 deletion rpc/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,22 @@ func (b *BatchEndpoints) GetBatch(batchNumber uint64) (*types.RPCBatch, error) {

if len(zkEVMBatchData.Blocks) > 0 {
lastL2BlockTimestamp, err := b.GetL2BlockTimestamp(zkEVMBatchData.Blocks[len(zkEVMBatchData.Blocks)-1])
log.Infof("Getting the last l2 block timestamp from the rpc:%v,%v", batchNumber, lastL2BlockTimestamp)
if err != nil {
return nil, fmt.Errorf("error getting the last l2 block timestamp from the rpc: %w", err)
}
rpcBatch.SetLastL2BLockTimestamp(lastL2BlockTimestamp)
} else {
log.Infof("No blocks in the batch, setting the last l2 block timestamp from the batch data")
log.Infof("No blocks in the batch, setting the last l2 block timestamp from the batch data:%v,%v",
batchNumber, zkEVMBatchData.Timestamp)
rpcBatch.SetLastL2BLockTimestamp(new(big.Int).SetBytes(common.FromHex(zkEVMBatchData.Timestamp)).Uint64())
}

if rpcBatch.IsClosed() && rpcBatch.LastL2BLockTimestamp() == 0 {
log.Infof("last L2 block timestamp is 0, cannot send sequence, %v, %v", rpcBatch.BatchNumber(), rpcBatch.String())
time.Sleep(10 * time.Hour) //nolint:mnd
}

return rpcBatch, nil
}

Expand Down
3 changes: 3 additions & 0 deletions sequencesender/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ type Config struct {
// RPCTimeout is the timeout for the L2 RPC calls
RPCTimeout types.Duration `mapstructure:"RPCTimeout"`

// CheckSendBatch is the configuration for the Batch before sending it to L1
CheckSendBatch bool `mapstructure:"CheckSendBatch"`

// GetBatchWaitInterval is the time to wait to query for a new batch when there are no more batches available
GetBatchWaitInterval types.Duration `mapstructure:"GetBatchWaitInterval"`
}
11 changes: 11 additions & 0 deletions sequencesender/sequencesender.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ func (s *SequenceSender) batchRetrieval(ctx context.Context) error {
continue
}

if rpcBatch.LastL2BLockTimestamp() == 0 {
s.logger.Error(fmt.Sprintf("last L2 block timestamp is 0, cannot send sequence, %v", rpcBatch.BatchNumber()))
time.Sleep(10 * time.Hour) //nolint:mnd
}

// Process and decode the batch
if err := s.populateSequenceData(rpcBatch, currentBatchNumber); err != nil {
return err
Expand Down Expand Up @@ -246,6 +251,7 @@ func (s *SequenceSender) populateSequenceData(rpcBatch *types.RPCBatch, batchNum
batchRaw: batchRaw,
}

log.Infof("Populate rpc batch data: %v", rpcBatch.String())
return nil
}

Expand Down Expand Up @@ -343,6 +349,11 @@ func (s *SequenceSender) tryToSendSequence(ctx context.Context) {

s.logger.Debugf(sequence.String())
s.logger.Infof("sending sequences to L1. From batch %d to batch %d", firstBatch.BatchNumber(), lastBatch.BatchNumber())
if lastBatch.LastL2BLockTimestamp() == 0 {
s.logger.Error(fmt.Sprintf("last L2 block timestamp is 0, cannot send sequence, %v", sequence.String()))
time.Sleep(10 * time.Hour) //nolint:mnd
return
}

// Wait until last L1 block timestamp is L1BlockTimestampMargin seconds above the timestamp
// of the last L2 block in the sequence
Expand Down
3 changes: 3 additions & 0 deletions sequencesender/txbuilder/banana_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type TxBuilderBananaBase struct {
ethClient l1Client
blockFinality *big.Int
opts bind.TransactOpts
l2RpcClient RPCInterface
}

func NewTxBuilderBananaBase(
Expand All @@ -53,6 +54,7 @@ func NewTxBuilderBananaBase(
ethClient l1Client,
blockFinality *big.Int,
opts bind.TransactOpts,
l2RpcClient RPCInterface,
) *TxBuilderBananaBase {
return &TxBuilderBananaBase{
logger: logger,
Expand All @@ -62,6 +64,7 @@ func NewTxBuilderBananaBase(
ethClient: ethClient,
blockFinality: blockFinality,
opts: opts,
l2RpcClient: l2RpcClient,
}
}

Expand Down
1 change: 1 addition & 0 deletions sequencesender/txbuilder/banana_base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ func newBananaBaseTestData(t *testing.T) *testDataBananaBase {
zkevmContractMock,
gerContractMock,
l1InfoSyncer, l1Client, big.NewInt(0), opts,
nil,
)
require.NotNil(t, sut)
return &testDataBananaBase{
Expand Down
10 changes: 9 additions & 1 deletion sequencesender/txbuilder/banana_validium.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ func NewTxBuilderBananaValidium(
l1InfoTree l1InfoSyncer,
ethClient l1Client,
blockFinality *big.Int,
l2RpcClient RPCInterface,
) *TxBuilderBananaValidium {
txBuilderBase := *NewTxBuilderBananaBase(logger, rollupContract,
gerContract, l1InfoTree, ethClient, blockFinality, opts)
gerContract, l1InfoTree, ethClient, blockFinality, opts,
l2RpcClient)

return &TxBuilderBananaValidium{
TxBuilderBananaBase: txBuilderBase,
Expand Down Expand Up @@ -93,6 +95,12 @@ func (t *TxBuilderBananaValidium) BuildSequenceBatchesTx(
return nil, err
}

// For X Layer
err = t.checkMaxTimestamp(ethseq)
if err != nil {
return nil, err
}

// Build sequence data
tx, err := t.internalBuildSequenceBatchesTx(ethseq, dataAvailabilityMessage)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions sequencesender/txbuilder/banana_validium_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func newBananaValidiumTestData(t *testing.T, maxBatchesForL1 uint64) *testDataBa
l1InfoSyncer,
l1Client,
big.NewInt(0),
nil,
)
require.NotNil(t, sut)
sut.SetCondNewSeq(condMock)
Expand Down
36 changes: 36 additions & 0 deletions sequencesender/txbuilder/banana_validium_xlayer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package txbuilder

import (
"errors"
"fmt"

"github.com/0xPolygon/cdk/etherman"
rpctypes "github.com/0xPolygon/cdk/rpc/types"
)

// RPCInterface represents the RPC interface
type RPCInterface interface {
GetBatch(batchNumber uint64) (*rpctypes.RPCBatch, error)
}

func (t *TxBuilderBananaValidium) checkMaxTimestamp(sequence etherman.SequenceBanana) error {
if t.l2RpcClient == nil {
return nil
}
var maxBatchNumber uint64
for _, batch := range sequence.Batches {
maxBatchNumber = max(maxBatchNumber, batch.BatchNumber)
}
rpcBatch, err := t.l2RpcClient.GetBatch(maxBatchNumber)
if err != nil {
t.logger.Error("check rpc max timestamp error: ", err)
return err
}
if rpcBatch.LastL2BLockTimestamp() != sequence.MaxSequenceTimestamp {
t.logger.Error("max timestamp mismatch: ", rpcBatch.LastL2BLockTimestamp(), sequence.MaxSequenceTimestamp)
return errors.New(fmt.Sprintf("max timestamp mismatch: %v, %v",
rpcBatch.LastL2BLockTimestamp(), sequence.MaxSequenceTimestamp))
}
t.logger.Infof("max timestamp check passed:%v,%v", maxBatchNumber, sequence.MaxSequenceTimestamp)
return nil
}
2 changes: 1 addition & 1 deletion sequencesender/txbuilder/banana_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func NewTxBuilderBananaZKEVM(
blockFinality *big.Int,
) *TxBuilderBananaZKEVM {
txBuilderBase := *NewTxBuilderBananaBase(logger, rollupContract,
gerContract, l1InfoTree, ethClient, blockFinality, opts)
gerContract, l1InfoTree, ethClient, blockFinality, opts, nil)

return &TxBuilderBananaZKEVM{
TxBuilderBananaBase: txBuilderBase,
Expand Down
1 change: 1 addition & 0 deletions test/config/test.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ WaitPeriodPurgeTxFile = "60m"
MaxPendingTx = 1
RPCURL = "http://127.0.0.1:8123"
GetBatchWaitInterval = "10s"
CheckSendBatch = false
[SequenceSender.EthTxManager]
FrequencyToMonitorTxs = "1s"
WaitTxToBeMined = "2m"
Expand Down
Loading