From 678fc17e3e048b057e353dca6e2ccb59371d484c Mon Sep 17 00:00:00 2001 From: Goran Rojovic <100121253+goran-ethernal@users.noreply.github.com> Date: Fri, 6 Dec 2024 16:37:50 +0100 Subject: [PATCH] fix: last sent certificate block (#234) --- aggsender/aggsender.go | 36 ++++++++++++++------- aggsender/aggsender_test.go | 62 +++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 11 deletions(-) diff --git a/aggsender/aggsender.go b/aggsender/aggsender.go index 366e6950..cb3319d0 100644 --- a/aggsender/aggsender.go +++ b/aggsender/aggsender.go @@ -155,17 +155,8 @@ func (a *AggSender) sendCertificate(ctx context.Context) (*agglayer.SignedCertif if err != nil { return nil, err } - 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 - } - } + + previousToBlock, retryCount := getLastSentBlockAndRetryCount(lastSentCertificateInfo) if previousToBlock >= lasL2BlockSynced { a.log.Infof("no new blocks to send a certificate, last certificate block: %d, last L2 block: %d", @@ -823,3 +814,26 @@ 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 7102c745..6d82604c 100644 --- a/aggsender/aggsender_test.go +++ b/aggsender/aggsender_test.go @@ -1959,6 +1959,68 @@ 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 (