From 3260e1e077b987cf70bc66f101b392b23fd4f49c Mon Sep 17 00:00:00 2001 From: Paolo Grisoli Date: Thu, 12 Jan 2023 09:54:46 +0100 Subject: [PATCH] [Sequencer] Check if enough time passed to close a sequence (#1509) Closes #1508. This PR adds a new MinTimeToCloseBatch value in the sequencer configuration. This value is then checked inside shouldCloseDueToNewDeposits against the open sequence timestamp to determine if the sequence can be closed. --- config/default.go | 1 + config/environments/local/local.node.config.toml | 1 + sequencer/closingchecker.go | 13 +++++++++---- sequencer/config.go | 3 +++ test/config/debug.node.config.toml | 1 + test/config/test.node.config.toml | 1 + 6 files changed, 16 insertions(+), 4 deletions(-) diff --git a/config/default.go b/config/default.go index 667db92cd3..ba1657912b 100644 --- a/config/default.go +++ b/config/default.go @@ -78,6 +78,7 @@ LastBatchVirtualizationTimeMaxWaitPeriod = "300s" WaitBlocksToUpdateGER = 10 WaitBlocksToConsiderGerFinal = 10 ElapsedTimeToCloseBatchWithoutTxsDueToNewGER = "60s" +MinTimeToCloseBatch = "60s" MaxTimeForBatchToBeOpen = "15s" BlocksAmountForTxsToBeDeleted = 100 FrequencyToCheckTxsForDelete = "12h" diff --git a/config/environments/local/local.node.config.toml b/config/environments/local/local.node.config.toml index a2e6f7718e..32224d4780 100644 --- a/config/environments/local/local.node.config.toml +++ b/config/environments/local/local.node.config.toml @@ -76,6 +76,7 @@ LastBatchVirtualizationTimeMaxWaitPeriod = "300s" WaitBlocksToUpdateGER = 10 WaitBlocksToConsiderGerFinal = 10 ElapsedTimeToCloseBatchWithoutTxsDueToNewGER = "60s" +MinTimeToCloseBatch = "60s" MaxTimeForBatchToBeOpen = "15s" BlocksAmountForTxsToBeDeleted = 100 FrequencyToCheckTxsForDelete = "12h" diff --git a/sequencer/closingchecker.go b/sequencer/closingchecker.go index 6f436595d8..651bf02e3b 100644 --- a/sequencer/closingchecker.go +++ b/sequencer/closingchecker.go @@ -33,8 +33,10 @@ func (s *Sequencer) shouldCloseSequenceInProgress(ctx context.Context) bool { return false } -// shouldCloseDueToNewDeposits return true if there has been new deposits on L1 for more than WaitBlocksToUpdateGER -// and the sequence is profitable (if profitability check is enabled) +// shouldCloseDueToNewDeposits return true if there has been new deposits on L1 +// for more than WaitBlocksToUpdateGER, enough time has passed since the +// sequence was opened and the sequence is profitable (if profitability check +// is enabled). func (s *Sequencer) shouldCloseDueToNewDeposits(ctx context.Context) (bool, error) { lastGer, gerReceivedAt, err := s.getLatestGer(ctx, nil) if err != nil && err != state.ErrNotFound { @@ -56,9 +58,12 @@ func (s *Sequencer) shouldCloseDueToNewDeposits(ctx context.Context) (bool, erro return false, err } + sequenceInProgressTimestamp := time.Unix(s.sequenceInProgress.Timestamp, 0) + if latestBlockNumber-blockNum > s.cfg.WaitBlocksToUpdateGER && - gerReceivedAt.Before(time.Now().Add(-s.cfg.ElapsedTimeToCloseBatchWithoutTxsDueToNewGER.Duration)) { - log.Info("current sequence should be closed because blocks have been mined since last GER") + gerReceivedAt.Before(time.Now().Add(-s.cfg.ElapsedTimeToCloseBatchWithoutTxsDueToNewGER.Duration)) && + sequenceInProgressTimestamp.Before(time.Now().Add(-s.cfg.MinTimeToCloseBatch.Duration)) { + log.Info("current sequence should be closed because blocks have been mined since last GER and enough time has passed") return true, nil } } diff --git a/sequencer/config.go b/sequencer/config.go index 2734a5ebc1..004d2ddd7e 100644 --- a/sequencer/config.go +++ b/sequencer/config.go @@ -30,6 +30,9 @@ type Config struct { // ElapsedTimeToCloseBatchWithoutTxsDueToNewGER it's time to close a batch bcs new GER appeared ElapsedTimeToCloseBatchWithoutTxsDueToNewGER types.Duration `mapstructure:"ElapsedTimeToCloseBatchWithoutTxsDueToNewGER"` + // MinTimeToCloseBatch enough time passed to close a batch. + MinTimeToCloseBatch types.Duration `mapstructure:"MinTimeToCloseBatch"` + // MaxTimeForBatchToBeOpen is time after which new batch should be closed MaxTimeForBatchToBeOpen types.Duration `mapstructure:"MaxTimeForBatchToBeOpen"` diff --git a/test/config/debug.node.config.toml b/test/config/debug.node.config.toml index 0885e07dd9..29538cda02 100644 --- a/test/config/debug.node.config.toml +++ b/test/config/debug.node.config.toml @@ -76,6 +76,7 @@ LastBatchVirtualizationTimeMaxWaitPeriod = "300s" WaitBlocksToUpdateGER = 10 WaitBlocksToConsiderGerFinal = 10 ElapsedTimeToCloseBatchWithoutTxsDueToNewGER = "60s" +MinTimeToCloseBatch = "60s" MaxTimeForBatchToBeOpen = "15s" BlocksAmountForTxsToBeDeleted = 100 FrequencyToCheckTxsForDelete = "12h" diff --git a/test/config/test.node.config.toml b/test/config/test.node.config.toml index 21c22acbab..dc6c5005a1 100644 --- a/test/config/test.node.config.toml +++ b/test/config/test.node.config.toml @@ -73,6 +73,7 @@ LastBatchVirtualizationTimeMaxWaitPeriod = "10s" WaitBlocksToUpdateGER = 10 WaitBlocksToConsiderGerFinal = 10 ElapsedTimeToCloseBatchWithoutTxsDueToNewGER = "60s" +MinTimeToCloseBatch = "60s" MaxTimeForBatchToBeOpen = "15s" BlocksAmountForTxsToBeDeleted = 100 FrequencyToCheckTxsForDelete = "12h"