Skip to content

Commit

Permalink
fix conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaubennassar committed Nov 15, 2024
2 parents 294dfc1 + 06f8aa6 commit d36822e
Show file tree
Hide file tree
Showing 11 changed files with 324 additions and 45 deletions.
150 changes: 149 additions & 1 deletion agglayer/mock_agglayer_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"test_name": "L1InfoRootIncorrect",
"certificate_header": "{\"network_id\":1,\"height\":6,\"epoch_number\":null,\"certificate_index\":null,\"certificate_id\":\"0xa80cd4abb016bbb3c3058f923a88be1ad49d68277366c55554d6f13d62428a1f\",\"new_local_exit_root\":\"0x566244fbf813b6926f6895142979f61ed6706184909cb8c819cd0216202a8aa9\",\"metadata\":\"0x000000000000000000000000000000000000000000000000000000000000001f\",\"status\":{\"InError\":{\"error\":{\"TypeConversionError\":{\"L1InfoRootIncorrect\":{\"leaf_count\":11,\"declared\":\"0x566244fbf813b6926f6895142979f61ed6706184909cb8c819cd0216202a8aa9\",\"retrieved\":\"0xa80cd4abb016bbb3c3058f923a88be1ad49d68277366c55554d6f13d62428a1f\"}}}}}}"
},
{
"test_name": "L1InfoRootIncorrect - unmarshal error",
"expected_error": "value of key leaf_count is not of type uint32",
"certificate_header": "{\"network_id\":1,\"height\":6,\"epoch_number\":null,\"certificate_index\":null,\"certificate_id\":\"0xa80cd4abb016bbb3c3058f923a88be1ad49d68277366c55554d6f13d62428a1f\",\"new_local_exit_root\":\"0x566244fbf813b6926f6895142979f61ed6706184909cb8c819cd0216202a8aa9\",\"metadata\":\"0x000000000000000000000000000000000000000000000000000000000000001f\",\"status\":{\"InError\":{\"error\":{\"TypeConversionError\":{\"L1InfoRootIncorrect\":{\"leaf_count\":\"invalid\",\"declared\":\"0x566244fbf813b6926f6895142979f61ed6706184909cb8c819cd0216202a8aa9\",\"retrieved\":\"0xa80cd4abb016bbb3c3058f923a88be1ad49d68277366c55554d6f13d62428a1f\"}}}}}}"
}
]
51 changes: 51 additions & 0 deletions agglayer/type_conversion_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package agglayer
import (
"errors"
"fmt"

"github.com/ethereum/go-ethereum/common"
)

const (
Expand All @@ -12,6 +14,7 @@ const (
BalanceUnderflowErrorType = "BalanceUnderflow"
BalanceProofGenerationFailedErrorType = "BalanceProofGenerationFailed"
NullifierPathGenerationFailedErrorType = "NullifierPathGenerationFailed"
L1InfoRootIncorrectErrorType = "L1InfoRootIncorrect"
)

// TypeConversionError is an error that is returned when verifying a certficate
Expand Down Expand Up @@ -57,6 +60,12 @@ func (p *TypeConversionError) Unmarshal(data interface{}) error {
return nil, err
}
return nullifierPathGenerationFailed, nil
case L1InfoRootIncorrectErrorType:
l1InfoRootIncorrect := &L1InfoRootIncorrect{}
if err := l1InfoRootIncorrect.Unmarshal(value); err != nil {
return nil, err
}
return l1InfoRootIncorrect, nil
default:
return nil, fmt.Errorf("unknown type conversion error type: %v", key)
}
Expand Down Expand Up @@ -253,3 +262,45 @@ func (e *NullifierPathGenerationFailed) UnmarshalFromMap(data interface{}) error
e.GlobalIndex = &GlobalIndex{}
return e.GlobalIndex.UnmarshalFromMap(globalIndexMap)
}

// L1InfoRootIncorrect is an error that is returned when the L1 Info Root is invalid or unsettled
type L1InfoRootIncorrect struct {
Declared common.Hash `json:"declared"`
Retrieved common.Hash `json:"retrieved"`
LeafCount uint32 `json:"leaf_count"`
}

// String is the implementation of the Error interface
func (e *L1InfoRootIncorrect) String() string {
return fmt.Sprintf("%s: The L1 Info Root is incorrect. Declared: %s, Retrieved: %s, LeafCount: %d",
L1InfoRootIncorrectErrorType, e.Declared.String(), e.Retrieved.String(), e.LeafCount)
}

// Unmarshal unmarshals the data from a map into a L1InfoRootIncorrect struct.
func (e *L1InfoRootIncorrect) Unmarshal(data interface{}) error {
dataMap, ok := data.(map[string]interface{})
if !ok {
return errNotMap
}

declared, err := convertMapValue[string](dataMap, "declared")
if err != nil {
return err
}

retrieved, err := convertMapValue[string](dataMap, "retrieved")
if err != nil {
return err
}

leafCount, err := convertMapValue[uint32](dataMap, "leaf_count")
if err != nil {
return err
}

e.Declared = common.HexToHash(declared)
e.Retrieved = common.HexToHash(retrieved)
e.LeafCount = leafCount

return nil
}
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
43 changes: 23 additions & 20 deletions aggsender/aggsender.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func New(
cfg Config,
aggLayerClient agglayer.AgglayerClientInterface,
l1InfoTreeSyncer *l1infotreesync.L1InfoTreeSync,
l2Syncer *bridgesync.BridgeSync,
l2Syncer types.L2BridgeSyncer,
epochNotifier types.EpochNotifier) (*AggSender, error) {
storage, err := db.NewAggSenderSQLStorage(logger, cfg.StoragePath)
if err != nil {
Expand Down Expand Up @@ -93,14 +93,14 @@ func (a *AggSender) sendCertificates(ctx context.Context) {
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 @@ func (a *AggSender) sendCertificate(ctx context.Context) (*agglayer.SignedCertif
}

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,49 @@ func (a *AggSender) signCertificate(certificate *agglayer.Certificate) (*agglaye
// 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
thereArePendingCerts := false
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(),
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",
certificateHeader.String(), certificate.Status, certificateHeader.Status, elapsedTime)

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) {
a.log.Infof("certificate %s is still pending, elapsed time:%s ",
certificateHeader.String(), elapsedTime)
thereArePendingCerts = true
}
}
return thereArePendingCertificates, nil
return thereArePendingCerts
}

// shouldSendCertificate checks if a certificate should be sent at given time
Expand Down
Loading

0 comments on commit d36822e

Please sign in to comment.