From 332fe7f9e29dad6e16459a160f5cf069463ebc5e Mon Sep 17 00:00:00 2001 From: Goran Rojovic Date: Mon, 9 Dec 2024 14:04:38 +0100 Subject: [PATCH] fix: revert underflow fix --- aggsender/aggsender.go | 36 +++++++-------------- aggsender/aggsender_test.go | 62 ------------------------------------- 2 files changed, 11 insertions(+), 87 deletions(-) diff --git a/aggsender/aggsender.go b/aggsender/aggsender.go index 27ef1434..29b6b06d 100644 --- a/aggsender/aggsender.go +++ b/aggsender/aggsender.go @@ -155,8 +155,17 @@ func (a *AggSender) sendCertificate(ctx context.Context) (*agglayer.SignedCertif if err != nil { return nil, err } - - previousToBlock, retryCount := getLastSentBlockAndRetryCount(lastSentCertificateInfo) + previousToBlock := uint64(0) + retryCount := 0 + if lastSentCertificateInfo != nil { + previousToBlock = lastSentCertificateInfo.ToBlock + if lastSentCertificateInfo.Status == agglayer.InError { + // if the last certificate was in error, we need to resend it + // from the block before the error + previousToBlock = lastSentCertificateInfo.FromBlock - 1 + retryCount = lastSentCertificateInfo.RetryCount + 1 + } + } if previousToBlock >= lasL2BlockSynced { a.log.Infof("no new blocks to send a certificate, last certificate block: %d, last L2 block: %d", @@ -807,26 +816,3 @@ func NewCertificateInfoFromAgglayerCertHeader(c *agglayer.CertificateHeader) *ty } return res } - -// getLastSentBlockAndRetryCount returns the last sent block of the last sent certificate -// if there is no previosly sent certificate, it returns 0 and 0 -func getLastSentBlockAndRetryCount(lastSentCertificateInfo *types.CertificateInfo) (uint64, int) { - if lastSentCertificateInfo == nil { - return 0, 0 - } - - retryCount := 0 - previousToBlock := lastSentCertificateInfo.ToBlock - - if lastSentCertificateInfo.Status == agglayer.InError { - // if the last certificate was in error, we need to resend it - // from the block before the error - if lastSentCertificateInfo.FromBlock > 0 { - previousToBlock = lastSentCertificateInfo.FromBlock - 1 - } - - retryCount = lastSentCertificateInfo.RetryCount + 1 - } - - return previousToBlock, retryCount -} diff --git a/aggsender/aggsender_test.go b/aggsender/aggsender_test.go index 806faf19..01d12019 100644 --- a/aggsender/aggsender_test.go +++ b/aggsender/aggsender_test.go @@ -1971,68 +1971,6 @@ func TestLimitSize_MinNumBlocks(t *testing.T) { require.Equal(t, uint64(1), newCert.ToBlock) } -func TestGetLastSentBlockAndRetryCount(t *testing.T) { - t.Parallel() - - tests := []struct { - name string - lastSentCertificateInfo *aggsendertypes.CertificateInfo - expectedBlock uint64 - expectedRetryCount int - }{ - { - name: "No last sent certificate", - lastSentCertificateInfo: nil, - expectedBlock: 0, - expectedRetryCount: 0, - }, - { - name: "Last sent certificate with no error", - lastSentCertificateInfo: &aggsendertypes.CertificateInfo{ - ToBlock: 10, - Status: agglayer.Settled, - }, - expectedBlock: 10, - expectedRetryCount: 0, - }, - { - name: "Last sent certificate with error and non-zero FromBlock", - lastSentCertificateInfo: &aggsendertypes.CertificateInfo{ - FromBlock: 5, - ToBlock: 10, - Status: agglayer.InError, - RetryCount: 1, - }, - expectedBlock: 4, - expectedRetryCount: 2, - }, - { - name: "Last sent certificate with error and zero FromBlock", - lastSentCertificateInfo: &aggsendertypes.CertificateInfo{ - FromBlock: 0, - ToBlock: 10, - Status: agglayer.InError, - RetryCount: 1, - }, - expectedBlock: 10, - expectedRetryCount: 2, - }, - } - - for _, tt := range tests { - tt := tt - - t.Run(tt.name, func(t *testing.T) { - t.Parallel() - - block, retryCount := getLastSentBlockAndRetryCount(tt.lastSentCertificateInfo) - - require.Equal(t, tt.expectedBlock, block) - require.Equal(t, tt.expectedRetryCount, retryCount) - }) - } -} - type testDataFlags = int const (