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: last sent certificate block underflow #234

Merged
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
36 changes: 25 additions & 11 deletions aggsender/aggsender.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
}
62 changes: 62 additions & 0 deletions aggsender/aggsender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
Loading