Skip to content

Commit

Permalink
fix: last sent certificate block (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
goran-ethernal authored Dec 6, 2024
1 parent 9eec22f commit 678fc17
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 11 deletions.
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

0 comments on commit 678fc17

Please sign in to comment.