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

feat: improve aggsender logs #186

Merged
merged 9 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion agglayer/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ func (c CertificateHeader) String() string {
errors = c.Error.String()
}

return fmt.Sprintf("Height: %d, CertificateID: %s, NewLocalExitRoot: %s. Status: %s. Errors: %s",
return fmt.Sprintf("Height: %d, CertificateID: %s, NewLocalExitRoot: %s. Status: %s. Errors: [%s]",
c.Height, c.CertificateID.String(), c.NewLocalExitRoot.String(), c.Status.String(), errors)
}

Expand Down
45 changes: 26 additions & 19 deletions aggsender/aggsender.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@
select {
case epoch := <-chEpoch:
a.log.Infof("Epoch received: %s", epoch.String())
thereArePendingCerts, err := a.checkPendingCertificatesStatus(ctx)
if err == nil && !thereArePendingCerts {
thereArePendingCerts := a.checkPendingCertificatesStatus(ctx)
if !thereArePendingCerts {
if _, err := a.sendCertificate(ctx); err != nil {
log.Error(err)
}
} else {
log.Warnf("Skipping epoch %s because there are pending certificates %v or error: %w",
epoch.String(), thereArePendingCerts, err)
log.Infof("Skipping epoch %s because there are pending certificates",
epoch.String())
}
case <-ctx.Done():
a.log.Info("AggSender stopped")
Expand Down Expand Up @@ -177,7 +177,7 @@
}

a.saveCertificateToFile(signedCertificate)
a.log.Debugf("certificate ready to be send to AggLayer: %s", signedCertificate.String())
a.log.Infof("certificate ready to be send to AggLayer: %s", signedCertificate.String())

certificateHash, err := a.aggLayerClient.SendCertificate(signedCertificate)
if err != nil {
Expand Down Expand Up @@ -488,46 +488,53 @@
// and updates in the storage if it changed on agglayer
// It returns:
// bool -> if there are pending certificates
// error -> if there was an error
func (a *AggSender) checkPendingCertificatesStatus(ctx context.Context) (bool, error) {
func (a *AggSender) checkPendingCertificatesStatus(ctx context.Context) bool {
pendingCertificates, err := a.storage.GetCertificatesByStatus(nonSettledStatuses)
if err != nil {
err = fmt.Errorf("error getting pending certificates: %w", err)
a.log.Error(err)
return true, err
return true
}
thereArePendingCertificates := false
logErrMsg := ""
a.log.Debugf("checkPendingCertificatesStatus num of pendingCertificates: %d", len(pendingCertificates))
for _, certificate := range pendingCertificates {
certificateHeader, err := a.aggLayerClient.GetCertificateHeader(certificate.CertificateID)
if err != nil {
err = fmt.Errorf("error getting certificate header of %d/%s from agglayer: %w",
certificate.Height, certificate.String(), err)
a.log.Error(err)
return true, err
return true
}
if slices.Contains(nonSettledStatuses, certificateHeader.Status) {
thereArePendingCertificates = true
}
a.log.Debugf("aggLayerClient.GetCertificateHeader status [%s] of certificate %s ",
elapsedTime := time.Now().UTC().Sub(time.UnixMilli(certificate.CreatedAt))
a.log.Debugf("aggLayerClient.GetCertificateHeader status [%s] of certificate %s elapsed_time:%s",
certificateHeader.Status,
certificateHeader.String())
certificateHeader.String(),
Dismissed Show dismissed Hide dismissed
elapsedTime)

if certificateHeader.Status != certificate.Status {
a.log.Infof("certificate %s changed status from [%s] to [%s]",
certificateHeader.String(), certificate.Status, certificateHeader.Status)
a.log.Infof("certificate %s changed status from [%s] to [%s] elapsed_time: %s",
joanestebanr marked this conversation as resolved.
Show resolved Hide resolved
certificateHeader.String(), certificate.Status, certificateHeader.Status, elapsedTime)
Dismissed Show dismissed Hide dismissed

certificate.Status = certificateHeader.Status
certificate.UpdatedAt = time.Now().UTC().UnixMilli()

if err := a.storage.UpdateCertificateStatus(ctx, *certificate); err != nil {
err = fmt.Errorf("error updating certificate %s status in storage: %w", certificateHeader.String(), err)
a.log.Error(err)
return true, err
return true
}
}
if slices.Contains(nonSettledStatuses, certificateHeader.Status) {

Check failure on line 527 in aggsender/aggsender.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary leading newline (whitespace)

logErrMsg += fmt.Sprintf("certificate %s is still pending, elapsed_time:%s ", certificateHeader.String(), elapsedTime)
}
}
if logErrMsg != "" {
a.log.Infof(logErrMsg)
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
return true

}

Check failure on line 536 in aggsender/aggsender.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)
return thereArePendingCertificates, nil
return false
}

// shouldSendCertificate checks if a certificate should be sent at given time
Expand Down
50 changes: 31 additions & 19 deletions aggsender/aggsender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,23 @@
}
}

func TestNewAggSender(t *testing.T) {
AggLayerMock := agglayer.NewAgglayerClientMock(t)
joanestebanr marked this conversation as resolved.
Show resolved Hide resolved
epochNotifierMock := mocks.NewEpochNotifier(t)

aggSender, err := New(
context.TODO(),
log.WithFields("test", "unittest"),
Config{},
AggLayerMock,
nil,
nil,
epochNotifierMock)
require.NoError(t, err)
require.NotNil(t, aggSender)

}

Check failure on line 298 in aggsender/aggsender_test.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)

//nolint:dupl
func TestGetImportedBridgeExits(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -751,16 +768,15 @@

func TestCheckIfCertificatesAreSettled(t *testing.T) {
tests := []struct {
name string
pendingCertificates []*aggsendertypes.CertificateInfo
certificateHeaders map[common.Hash]*agglayer.CertificateHeader
getFromDBError error
clientError error
updateDBError error
expectedErrorLogMessages []string
expectedInfoMessages []string
expectedThereArePendingCerts bool
expectedError bool
name string
pendingCertificates []*aggsendertypes.CertificateInfo
certificateHeaders map[common.Hash]*agglayer.CertificateHeader
getFromDBError error
clientError error
updateDBError error
expectedErrorLogMessages []string
expectedInfoMessages []string
expectedError bool
}{
{
name: "All certificates settled - update successful",
Expand Down Expand Up @@ -796,8 +812,7 @@
expectedErrorLogMessages: []string{
"error getting pending certificates: %w",
},
expectedThereArePendingCerts: true,
expectedError: true,
expectedError: true,
},
{
name: "Error getting certificate header",
Expand All @@ -811,8 +826,7 @@
expectedErrorLogMessages: []string{
"error getting header of certificate %s with height: %d from agglayer: %w",
},
expectedThereArePendingCerts: true,
expectedError: true,
expectedError: true,
},
{
name: "Error updating certificate status",
Expand All @@ -829,8 +843,7 @@
expectedInfoMessages: []string{
"certificate %s changed status to %s",
},
expectedThereArePendingCerts: true,
expectedError: true,
expectedError: true,
},
}

Expand Down Expand Up @@ -864,9 +877,8 @@
}

ctx := context.TODO()
thereArePendingCerts, err := aggSender.checkPendingCertificatesStatus(ctx)
require.Equal(t, tt.expectedThereArePendingCerts, thereArePendingCerts)
require.Equal(t, tt.expectedError, err != nil)
thereArePendingCerts := aggSender.checkPendingCertificatesStatus(ctx)
require.Equal(t, tt.expectedError, thereArePendingCerts)
mockAggLayerClient.AssertExpectations(t)
mockStorage.AssertExpectations(t)
})
Expand Down
2 changes: 1 addition & 1 deletion config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,5 +343,5 @@ URLRPCL2="{{L2URL}}"
CheckSettledInterval = "2s"
BlockFinality = "LatestBlock"
EpochNotificationPercentage = 50
SaveCertificatesToFiles = false
SaveCertificatesToFilesPath = ""
`
Loading