Skip to content

Commit

Permalink
feat: improve logs (#204)
Browse files Browse the repository at this point in the history
- Avoid to log full certificate because it's too long
- Add logs to `bridgesync` creation
- Reduce log for `l1infotreesync/processor.go:401`  `block 7157878 processed with 0 events` in case of 0 events
---------

Co-authored-by: Stefan Negovanović <[email protected]>
  • Loading branch information
joanestebanr and Stefan-Ethernal committed Nov 28, 2024
1 parent a642291 commit f1be601
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 47 deletions.
33 changes: 10 additions & 23 deletions agglayer/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,27 +112,14 @@ type Certificate struct {
Metadata common.Hash `json:"metadata"`
}

func (c *Certificate) String() string {
res := fmt.Sprintf("NetworkID: %d, Height: %d, PrevLocalExitRoot: %s, NewLocalExitRoot: %s, Metadata: %s\n",
c.NetworkID, c.Height, common.Bytes2Hex(c.PrevLocalExitRoot[:]),
common.Bytes2Hex(c.NewLocalExitRoot[:]), common.Bytes2Hex(c.Metadata[:]))

if c.BridgeExits == nil {
res += " BridgeExits: nil\n"
} else {
for i, bridgeExit := range c.BridgeExits {
res += fmt.Sprintf(", BridgeExit[%d]: %s\n", i, bridgeExit.String())
}
}

if c.ImportedBridgeExits == nil {
res += " ImportedBridgeExits: nil\n"
} else {
for i, importedBridgeExit := range c.ImportedBridgeExits {
res += fmt.Sprintf(" ImportedBridgeExit[%d]: %s\n", i, importedBridgeExit.String())
}
// Brief returns a string with a brief cert
func (c *Certificate) Brief() string {
if c == nil {
return nilStr
}

res := fmt.Sprintf("agglayer.Cert {height: %d prevLER: %s newLER: %s exits: %d imported_exits: %d}", c.Height,
common.Bytes2Hex(c.PrevLocalExitRoot[:]), common.Bytes2Hex(c.NewLocalExitRoot[:]),
len(c.BridgeExits), len(c.ImportedBridgeExits))
return res
}

Expand Down Expand Up @@ -181,8 +168,8 @@ type SignedCertificate struct {
Signature *Signature `json:"signature"`
}

func (s *SignedCertificate) String() string {
return fmt.Sprintf("Certificate:%s,\nSignature: %s", s.Certificate.String(), s.Signature.String())
func (s *SignedCertificate) Brief() string {
return fmt.Sprintf("Certificate:%s,\nSignature: %s", s.Certificate.Brief(), s.Signature.String())
}

// CopyWithDefaulting returns a shallow copy of the signed certificate
Expand Down Expand Up @@ -604,7 +591,7 @@ func (c *CertificateHeader) String() string {
if c.Error != nil {
errors = c.Error.String()
}
previousLocalExitRoot := "nil"
previousLocalExitRoot := nilStr
if c.PreviousLocalExitRoot != nil {
previousLocalExitRoot = c.PreviousLocalExitRoot.String()
}
Expand Down
14 changes: 8 additions & 6 deletions aggsender/aggsender.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,17 @@ func (a *AggSender) sendCertificate(ctx context.Context) (*agglayer.SignedCertif
}

a.saveCertificateToFile(signedCertificate)
a.log.Infof("certificate ready to be send to AggLayer: %s", signedCertificate.String())
a.log.Infof("certificate ready to be send to AggLayer: %s", signedCertificate.Brief())
certificateHash, err := a.aggLayerClient.SendCertificate(signedCertificate)
if err != nil {
return nil, fmt.Errorf("error sending certificate: %w", err)
}

a.log.Debugf("certificate send: Height: %d hash: %s", signedCertificate.Height, certificateHash.String())
a.log.Debugf("certificate send: Height: %d cert: %s", signedCertificate.Height, signedCertificate.Brief())

raw, err := json.Marshal(signedCertificate)
if err != nil {
return nil, fmt.Errorf("error marshalling signed certificate: %w", err)
return nil, fmt.Errorf("error marshalling signed certificate. Cert:%s. Err: %w", signedCertificate.Brief(), err)
}

createdTime := time.Now().UTC().UnixMilli()
Expand All @@ -228,12 +228,12 @@ func (a *AggSender) sendCertificate(ctx context.Context) (*agglayer.SignedCertif
// TODO: Improve this case, if a cert is not save in the storage, we are going to settle a unknown certificate
err = a.saveCertificateToStorage(ctx, certInfo, a.cfg.MaxRetriesStoreCertificate)
if err != nil {
a.log.Errorf("error saving certificate to storage: %w", err)
a.log.Errorf("error saving certificate to storage. Cert:%s Err: %w", certInfo.String(), err)
return nil, fmt.Errorf("error saving last sent certificate %s in db: %w", certInfo.String(), err)
}

a.log.Infof("certificate: %s sent successfully for range of l2 blocks (from block: %d, to block: %d) cert:%s",
certificateHash, fromBlock, toBlock, signedCertificate.String())
certInfo.ID(), fromBlock, toBlock, signedCertificate.Brief())

return signedCertificate, nil
}
Expand Down Expand Up @@ -454,7 +454,9 @@ func (a *AggSender) getImportedBridgeExits(
for i, claim := range claims {
l1Info := claimL1Info[i]

a.log.Debugf("claim[%d]: destAddr: %s GER:%s", i, claim.DestinationAddress.String(), claim.GlobalExitRoot.String())
a.log.Debugf("claim[%d]: destAddr: %s GER: %s Block: %d Pos: %d GlobalIndex: 0x%x",
i, claim.DestinationAddress.String(), claim.GlobalExitRoot.String(),
claim.BlockNum, claim.BlockPos, claim.GlobalIndex)
ibe, err := a.convertClaimToImportedBridgeExit(claim)
if err != nil {
return nil, fmt.Errorf("error converting claim to imported bridge exit: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion aggsender/db/migrations/0001.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CREATE TABLE certificate_info (
height INTEGER NOT NULL,
certificate_id VARCHAR NOT NULL PRIMARY KEY,
status INTEGER NOT NULL,
previous_local_exit_root VARCHAR ,
previous_local_exit_root VARCHAR,
new_local_exit_root VARCHAR NOT NULL,
from_block INTEGER NOT NULL,
to_block INTEGER NOT NULL,
Expand Down
18 changes: 9 additions & 9 deletions aggsender/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,16 @@ func (c *CertificateInfo) String() string {
if c.PreviousLocalExitRoot != nil {
previousLocalExitRoot = c.PreviousLocalExitRoot.String()
}
return fmt.Sprintf(
return fmt.Sprintf("aggsender.CertificateInfo: "+
"Height: %d "+
"CertificateID: %s "+
"PreviousLocalExitRoot: %s "+
"NewLocalExitRoot: %s "+
"Status: %s "+
"FromBlock: %d "+
"ToBlock: %d "+
"CreatedAt: %s "+
"UpdatedAt: %s",
"CertificateID: %s "+
"PreviousLocalExitRoot: %s "+
"NewLocalExitRoot: %s "+
"Status: %s "+
"FromBlock: %d "+
"ToBlock: %d "+
"CreatedAt: %s "+
"UpdatedAt: %s",
c.Height,
c.CertificateID.String(),
previousLocalExitRoot,
Expand Down
11 changes: 10 additions & 1 deletion bridgesync/bridgesync.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/0xPolygon/cdk/etherman"
"github.com/0xPolygon/cdk/log"
"github.com/0xPolygon/cdk/sync"
tree "github.com/0xPolygon/cdk/tree/types"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -111,7 +112,8 @@ func newBridgeSync(
originNetwork uint32,
syncFullClaims bool,
) (*BridgeSync, error) {
processor, err := newProcessor(dbPath, l1OrL2ID)
logger := log.WithFields("bridge-syncer", l1OrL2ID)
processor, err := newProcessor(dbPath, logger)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -156,6 +158,13 @@ func newBridgeSync(
if err != nil {
return nil, err
}
logger.Infof("BridgeSyncer [%s] created: dbPath: %s initialBlock: %d bridgeAddr: %s, syncFullClaims: %d,"+
" maxRetryAttemptsAfterError: %d RetryAfterErrorPeriod: %s"+
"syncBlockChunkSize: %d, blockFinalityType: %s waitForNewBlocksPeriod: %s",
l1OrL2ID,
dbPath, initialBlock, bridge.String(), syncFullClaims,
maxRetryAttemptsAfterError, retryAfterErrorPeriod.String(),
syncBlockChunkSize, blockFinalityType, waitForNewBlocksPeriod.String())

return &BridgeSync{
processor: processor,
Expand Down
4 changes: 2 additions & 2 deletions bridgesync/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ type processor struct {
bridgeContract BridgeContractor
}

func newProcessor(dbPath, loggerPrefix string) (*processor, error) {
func newProcessor(dbPath string, logger *log.Logger) (*processor, error) {
err := migrations.RunMigrations(dbPath)
if err != nil {
return nil, err
Expand All @@ -118,7 +118,7 @@ func newProcessor(dbPath, loggerPrefix string) (*processor, error) {
if err != nil {
return nil, err
}
logger := log.WithFields("bridge-syncer", loggerPrefix)

exitTree := tree.NewAppendOnlyTree(db, "")
return &processor{
db: db,
Expand Down
9 changes: 6 additions & 3 deletions bridgesync/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ func TestProceessor(t *testing.T) {
log.Debugf("sqlite path: %s", path)
err := migrationsBridge.RunMigrations(path)
require.NoError(t, err)
p, err := newProcessor(path, "foo")
logger := log.WithFields("bridge-syncer", "foo")
p, err := newProcessor(path, logger)
require.NoError(t, err)
actions := []processAction{
// processed: ~
Expand Down Expand Up @@ -735,7 +736,8 @@ func TestInsertAndGetClaim(t *testing.T) {
log.Debugf("sqlite path: %s", path)
err := migrationsBridge.RunMigrations(path)
require.NoError(t, err)
p, err := newProcessor(path, "foo")
logger := log.WithFields("bridge-syncer", "foo")
p, err := newProcessor(path, logger)
require.NoError(t, err)

tx, err := p.db.BeginTx(context.Background(), nil)
Expand Down Expand Up @@ -828,7 +830,8 @@ func TestGetBridgesPublished(t *testing.T) {

path := path.Join(t.TempDir(), "file::memory:?cache=shared")
require.NoError(t, migrationsBridge.RunMigrations(path))
p, err := newProcessor(path, "foo")
logger := log.WithFields("bridge-syncer", "foo")
p, err := newProcessor(path, logger)
require.NoError(t, err)

tx, err := p.db.BeginTx(context.Background(), nil)
Expand Down
7 changes: 5 additions & 2 deletions l1infotreesync/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,11 @@ func (p *processor) ProcessBlock(ctx context.Context, block sync.Block) error {
return fmt.Errorf("err: %w", err)
}
shouldRollback = false

log.Infof("block %d processed with %d events", block.Num, len(block.Events))
logFunc := log.Debugf
if len(block.Events) > 0 {
logFunc = log.Infof
}
logFunc("block %d processed with %d events", block.Num, len(block.Events))
return nil
}

Expand Down

0 comments on commit f1be601

Please sign in to comment.