From 5e95fb62618b842ea56aa220277da9704091f274 Mon Sep 17 00:00:00 2001 From: Goran Rojovic Date: Fri, 8 Nov 2024 13:13:32 +0100 Subject: [PATCH 1/4] feat: created and updated timestamps --- aggsender/aggsender.go | 4 ++++ aggsender/db/aggsender_db_storage_test.go | 27 +++++++++++++++++++++++ aggsender/db/migrations/0001.sql | 4 +++- aggsender/types/types.go | 23 +++++++++++++++++-- 4 files changed, 55 insertions(+), 3 deletions(-) diff --git a/aggsender/aggsender.go b/aggsender/aggsender.go index 73953633..d380845b 100644 --- a/aggsender/aggsender.go +++ b/aggsender/aggsender.go @@ -177,12 +177,15 @@ func (a *AggSender) sendCertificate(ctx context.Context) (*agglayer.SignedCertif a.log.Debugf("certificate send: Height: %d hash: %s", signedCertificate.Height, certificateHash.String()) + createdTime := time.Now().UTC().UnixMilli() certInfo := aggsendertypes.CertificateInfo{ Height: certificate.Height, CertificateID: certificateHash, NewLocalExitRoot: certificate.NewLocalExitRoot, FromBlock: fromBlock, ToBlock: toBlock, + CreatedAt: createdTime, + UpdatedAt: createdTime, } if err := a.storage.SaveLastSentCertificate(ctx, certInfo); err != nil { @@ -504,6 +507,7 @@ func (a *AggSender) checkPendingCertificatesStatus(ctx context.Context) { certificateHeader.String(), certificate.Status, certificateHeader.Status) certificate.Status = certificateHeader.Status + certificate.UpdatedAt = time.Now().UTC().UnixMilli() if err := a.storage.UpdateCertificateStatus(ctx, *certificate); err != nil { a.log.Errorf("error updating certificate %s status in storage: %w", certificateHeader.String(), err) diff --git a/aggsender/db/aggsender_db_storage_test.go b/aggsender/db/aggsender_db_storage_test.go index 6a656a95..4c4c389b 100644 --- a/aggsender/db/aggsender_db_storage_test.go +++ b/aggsender/db/aggsender_db_storage_test.go @@ -4,6 +4,7 @@ import ( "context" "path" "testing" + "time" "github.com/0xPolygon/cdk/agglayer" "github.com/0xPolygon/cdk/aggsender/db/migrations" @@ -24,6 +25,8 @@ func Test_Storage(t *testing.T) { storage, err := NewAggSenderSQLStorage(log.WithFields("aggsender-db"), path) require.NoError(t, err) + updateTime := time.Now().UTC().UnixMilli() + t.Run("SaveLastSentCertificate", func(t *testing.T) { certificate := types.CertificateInfo{ Height: 1, @@ -32,6 +35,8 @@ func Test_Storage(t *testing.T) { FromBlock: 1, ToBlock: 2, Status: agglayer.Settled, + CreatedAt: updateTime, + UpdatedAt: updateTime, } require.NoError(t, storage.SaveLastSentCertificate(ctx, certificate)) @@ -50,6 +55,8 @@ func Test_Storage(t *testing.T) { FromBlock: 3, ToBlock: 4, Status: agglayer.Settled, + CreatedAt: updateTime, + UpdatedAt: updateTime, } require.NoError(t, storage.SaveLastSentCertificate(ctx, certificate)) @@ -75,6 +82,8 @@ func Test_Storage(t *testing.T) { FromBlock: 5, ToBlock: 6, Status: agglayer.Pending, + CreatedAt: updateTime, + UpdatedAt: updateTime, } require.NoError(t, storage.SaveLastSentCertificate(ctx, certificate)) @@ -104,6 +113,8 @@ func Test_Storage(t *testing.T) { FromBlock: 17, ToBlock: 18, Status: agglayer.Pending, + CreatedAt: updateTime, + UpdatedAt: updateTime, } require.NoError(t, storage.SaveLastSentCertificate(ctx, certificate)) @@ -124,6 +135,8 @@ func Test_Storage(t *testing.T) { FromBlock: 7, ToBlock: 8, Status: agglayer.Settled, + CreatedAt: updateTime, + UpdatedAt: updateTime, }, { Height: 9, @@ -132,6 +145,8 @@ func Test_Storage(t *testing.T) { FromBlock: 9, ToBlock: 10, Status: agglayer.Pending, + CreatedAt: updateTime, + UpdatedAt: updateTime, }, { Height: 11, @@ -140,6 +155,8 @@ func Test_Storage(t *testing.T) { FromBlock: 11, ToBlock: 12, Status: agglayer.InError, + CreatedAt: updateTime, + UpdatedAt: updateTime, }, } @@ -187,6 +204,8 @@ func Test_Storage(t *testing.T) { FromBlock: 13, ToBlock: 14, Status: agglayer.Pending, + CreatedAt: updateTime, + UpdatedAt: updateTime, } require.NoError(t, storage.SaveLastSentCertificate(ctx, certificate)) @@ -213,6 +232,8 @@ func Test_SaveLastSentCertificate(t *testing.T) { storage, err := NewAggSenderSQLStorage(log.WithFields("aggsender-db"), path) require.NoError(t, err) + updateTime := time.Now().UTC().UnixMilli() + t.Run("SaveNewCertificate", func(t *testing.T) { certificate := types.CertificateInfo{ Height: 1, @@ -221,6 +242,8 @@ func Test_SaveLastSentCertificate(t *testing.T) { FromBlock: 1, ToBlock: 2, Status: agglayer.Settled, + CreatedAt: updateTime, + UpdatedAt: updateTime, } require.NoError(t, storage.SaveLastSentCertificate(ctx, certificate)) @@ -238,6 +261,8 @@ func Test_SaveLastSentCertificate(t *testing.T) { FromBlock: 3, ToBlock: 4, Status: agglayer.InError, + CreatedAt: updateTime, + UpdatedAt: updateTime, } require.NoError(t, storage.SaveLastSentCertificate(ctx, certificate)) @@ -267,6 +292,8 @@ func Test_SaveLastSentCertificate(t *testing.T) { FromBlock: 7, ToBlock: 8, Status: agglayer.Settled, + CreatedAt: updateTime, + UpdatedAt: updateTime, } // Close the database to force an error diff --git a/aggsender/db/migrations/0001.sql b/aggsender/db/migrations/0001.sql index 3ed7f997..2b8ab6b0 100644 --- a/aggsender/db/migrations/0001.sql +++ b/aggsender/db/migrations/0001.sql @@ -8,5 +8,7 @@ CREATE TABLE certificate_info ( status INTEGER NOT NULL, new_local_exit_root VARCHAR NOT NULL, from_block INTEGER NOT NULL, - to_block INTEGER NOT NULL + to_block INTEGER NOT NULL, + created_at INTEGER NOT NULL, + updated_at INTEGER NOT NULL ); \ No newline at end of file diff --git a/aggsender/types/types.go b/aggsender/types/types.go index ffdf4d24..382d80b6 100644 --- a/aggsender/types/types.go +++ b/aggsender/types/types.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "math/big" + "time" "github.com/0xPolygon/cdk/agglayer" "github.com/0xPolygon/cdk/bridgesync" @@ -57,9 +58,27 @@ type CertificateInfo struct { FromBlock uint64 `meddler:"from_block"` ToBlock uint64 `meddler:"to_block"` Status agglayer.CertificateStatus `meddler:"status"` + CreatedAt int64 `meddler:"created_at"` + UpdatedAt int64 `meddler:"updated_at"` } func (c CertificateInfo) String() string { - return fmt.Sprintf("Height: %d, CertificateID: %s, FromBlock: %d, ToBlock: %d, NewLocalExitRoot: %s", - c.Height, c.CertificateID.String(), c.FromBlock, c.ToBlock, c.NewLocalExitRoot.String()) + return fmt.Sprintf( + "Height: %d\n"+ + "CertificateID: %s\n"+ + "FromBlock: %d\n"+ + "ToBlock: %d\n"+ + "NewLocalExitRoot: %s\n"+ + "Status: %s\n"+ + "CreatedAt: %s\n"+ + "UpdatedAt: %s\n", + c.Height, + c.CertificateID.String(), + c.FromBlock, + c.ToBlock, + c.NewLocalExitRoot.String(), + c.Status.String(), + time.UnixMilli(c.CreatedAt), + time.UnixMilli(c.UpdatedAt), + ) } From 732d0f052fe29b9076189b83b2d42502e817cba2 Mon Sep 17 00:00:00 2001 From: Goran Rojovic Date: Fri, 8 Nov 2024 13:24:48 +0100 Subject: [PATCH 2/4] feat: save raw certificate to db --- aggsender/aggsender.go | 6 +++ aggsender/db/aggsender_db_storage_test.go | 56 +++++++++++++++++++++++ aggsender/db/migrations/0001.sql | 3 +- aggsender/types/types.go | 1 + 4 files changed, 65 insertions(+), 1 deletion(-) diff --git a/aggsender/aggsender.go b/aggsender/aggsender.go index d380845b..303d6fec 100644 --- a/aggsender/aggsender.go +++ b/aggsender/aggsender.go @@ -177,6 +177,11 @@ func (a *AggSender) sendCertificate(ctx context.Context) (*agglayer.SignedCertif a.log.Debugf("certificate send: Height: %d hash: %s", signedCertificate.Height, certificateHash.String()) + raw, err := json.Marshal(signedCertificate) + if err != nil { + return nil, fmt.Errorf("error marshalling signed certificate: %w", err) + } + createdTime := time.Now().UTC().UnixMilli() certInfo := aggsendertypes.CertificateInfo{ Height: certificate.Height, @@ -186,6 +191,7 @@ func (a *AggSender) sendCertificate(ctx context.Context) (*agglayer.SignedCertif ToBlock: toBlock, CreatedAt: createdTime, UpdatedAt: createdTime, + Raw: string(raw), } if err := a.storage.SaveLastSentCertificate(ctx, certInfo); err != nil { diff --git a/aggsender/db/aggsender_db_storage_test.go b/aggsender/db/aggsender_db_storage_test.go index 4c4c389b..a2fb8a89 100644 --- a/aggsender/db/aggsender_db_storage_test.go +++ b/aggsender/db/aggsender_db_storage_test.go @@ -2,6 +2,8 @@ package db import ( "context" + "encoding/json" + "math/big" "path" "testing" "time" @@ -311,4 +313,58 @@ func Test_SaveLastSentCertificate(t *testing.T) { require.Equal(t, types.CertificateInfo{}, certificateFromDB) require.NoError(t, storage.clean()) }) + + t.Run("SaveCertificate with raw data", func(t *testing.T) { + certfiicate := &agglayer.SignedCertificate{ + Certificate: &agglayer.Certificate{ + NetworkID: 1, + Height: 1, + PrevLocalExitRoot: common.HexToHash("0x1"), + NewLocalExitRoot: common.HexToHash("0x2"), + Metadata: common.HexToHash("0x3"), + BridgeExits: []*agglayer.BridgeExit{ + { + LeafType: agglayer.LeafTypeAsset, + TokenInfo: &agglayer.TokenInfo{ + OriginNetwork: 1, + OriginTokenAddress: common.HexToAddress("0x1"), + }, + DestinationNetwork: 2, + DestinationAddress: common.HexToAddress("0x2"), + Amount: big.NewInt(100), + Metadata: []byte("metadata"), + }, + }, + ImportedBridgeExits: []*agglayer.ImportedBridgeExit{}, + }, + Signature: &agglayer.Signature{ + R: common.HexToHash("0x4"), + S: common.HexToHash("0x5"), + OddParity: false, + }, + } + + raw, err := json.Marshal(certfiicate) + require.NoError(t, err) + + certificate := types.CertificateInfo{ + Height: 1, + CertificateID: common.HexToHash("0x9"), + NewLocalExitRoot: common.HexToHash("0x2"), + FromBlock: 1, + ToBlock: 10, + Status: agglayer.Pending, + CreatedAt: updateTime, + UpdatedAt: updateTime, + Raw: string(raw), + } + require.NoError(t, storage.SaveLastSentCertificate(ctx, certificate)) + + certificateFromDB, err := storage.GetCertificateByHeight(certificate.Height) + require.NoError(t, err) + require.Equal(t, certificate, certificateFromDB) + require.Equal(t, raw, []byte(certificateFromDB.Raw)) + + require.NoError(t, storage.clean()) + }) } diff --git a/aggsender/db/migrations/0001.sql b/aggsender/db/migrations/0001.sql index 2b8ab6b0..0bf2a773 100644 --- a/aggsender/db/migrations/0001.sql +++ b/aggsender/db/migrations/0001.sql @@ -10,5 +10,6 @@ CREATE TABLE certificate_info ( from_block INTEGER NOT NULL, to_block INTEGER NOT NULL, created_at INTEGER NOT NULL, - updated_at INTEGER NOT NULL + updated_at INTEGER NOT NULL, + raw TEXT ); \ No newline at end of file diff --git a/aggsender/types/types.go b/aggsender/types/types.go index 382d80b6..1689d85f 100644 --- a/aggsender/types/types.go +++ b/aggsender/types/types.go @@ -60,6 +60,7 @@ type CertificateInfo struct { Status agglayer.CertificateStatus `meddler:"status"` CreatedAt int64 `meddler:"created_at"` UpdatedAt int64 `meddler:"updated_at"` + Raw string `meddler:"raw"` } func (c CertificateInfo) String() string { From 2274bd982b36e378b1d496b693ae827451453639 Mon Sep 17 00:00:00 2001 From: Goran Rojovic Date: Fri, 8 Nov 2024 13:57:38 +0100 Subject: [PATCH 3/4] fix: raw to signed_certificate --- aggsender/aggsender.go | 16 ++++++++-------- aggsender/db/aggsender_db_storage_test.go | 20 ++++++++++---------- aggsender/db/migrations/0001.sql | 2 +- aggsender/types/types.go | 18 +++++++++--------- 4 files changed, 28 insertions(+), 28 deletions(-) diff --git a/aggsender/aggsender.go b/aggsender/aggsender.go index 303d6fec..e3242bdf 100644 --- a/aggsender/aggsender.go +++ b/aggsender/aggsender.go @@ -184,14 +184,14 @@ func (a *AggSender) sendCertificate(ctx context.Context) (*agglayer.SignedCertif createdTime := time.Now().UTC().UnixMilli() certInfo := aggsendertypes.CertificateInfo{ - Height: certificate.Height, - CertificateID: certificateHash, - NewLocalExitRoot: certificate.NewLocalExitRoot, - FromBlock: fromBlock, - ToBlock: toBlock, - CreatedAt: createdTime, - UpdatedAt: createdTime, - Raw: string(raw), + Height: certificate.Height, + CertificateID: certificateHash, + NewLocalExitRoot: certificate.NewLocalExitRoot, + FromBlock: fromBlock, + ToBlock: toBlock, + CreatedAt: createdTime, + UpdatedAt: createdTime, + SignedCertificate: string(raw), } if err := a.storage.SaveLastSentCertificate(ctx, certInfo); err != nil { diff --git a/aggsender/db/aggsender_db_storage_test.go b/aggsender/db/aggsender_db_storage_test.go index a2fb8a89..a0a20894 100644 --- a/aggsender/db/aggsender_db_storage_test.go +++ b/aggsender/db/aggsender_db_storage_test.go @@ -348,22 +348,22 @@ func Test_SaveLastSentCertificate(t *testing.T) { require.NoError(t, err) certificate := types.CertificateInfo{ - Height: 1, - CertificateID: common.HexToHash("0x9"), - NewLocalExitRoot: common.HexToHash("0x2"), - FromBlock: 1, - ToBlock: 10, - Status: agglayer.Pending, - CreatedAt: updateTime, - UpdatedAt: updateTime, - Raw: string(raw), + Height: 1, + CertificateID: common.HexToHash("0x9"), + NewLocalExitRoot: common.HexToHash("0x2"), + FromBlock: 1, + ToBlock: 10, + Status: agglayer.Pending, + CreatedAt: updateTime, + UpdatedAt: updateTime, + SignedCertificate: string(raw), } require.NoError(t, storage.SaveLastSentCertificate(ctx, certificate)) certificateFromDB, err := storage.GetCertificateByHeight(certificate.Height) require.NoError(t, err) require.Equal(t, certificate, certificateFromDB) - require.Equal(t, raw, []byte(certificateFromDB.Raw)) + require.Equal(t, raw, []byte(certificateFromDB.SignedCertificate)) require.NoError(t, storage.clean()) }) diff --git a/aggsender/db/migrations/0001.sql b/aggsender/db/migrations/0001.sql index 0bf2a773..69e4896e 100644 --- a/aggsender/db/migrations/0001.sql +++ b/aggsender/db/migrations/0001.sql @@ -11,5 +11,5 @@ CREATE TABLE certificate_info ( to_block INTEGER NOT NULL, created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL, - raw TEXT + signed_certificate TEXT ); \ No newline at end of file diff --git a/aggsender/types/types.go b/aggsender/types/types.go index 1689d85f..46d31176 100644 --- a/aggsender/types/types.go +++ b/aggsender/types/types.go @@ -52,15 +52,15 @@ type Logger interface { } type CertificateInfo struct { - Height uint64 `meddler:"height"` - CertificateID common.Hash `meddler:"certificate_id,hash"` - NewLocalExitRoot common.Hash `meddler:"new_local_exit_root,hash"` - FromBlock uint64 `meddler:"from_block"` - ToBlock uint64 `meddler:"to_block"` - Status agglayer.CertificateStatus `meddler:"status"` - CreatedAt int64 `meddler:"created_at"` - UpdatedAt int64 `meddler:"updated_at"` - Raw string `meddler:"raw"` + Height uint64 `meddler:"height"` + CertificateID common.Hash `meddler:"certificate_id,hash"` + NewLocalExitRoot common.Hash `meddler:"new_local_exit_root,hash"` + FromBlock uint64 `meddler:"from_block"` + ToBlock uint64 `meddler:"to_block"` + Status agglayer.CertificateStatus `meddler:"status"` + CreatedAt int64 `meddler:"created_at"` + UpdatedAt int64 `meddler:"updated_at"` + SignedCertificate string `meddler:"signed_certificate"` } func (c CertificateInfo) String() string { From f5f86ffc7c0eeae58e6a7c2b1979af6f06ca40cf Mon Sep 17 00:00:00 2001 From: Goran Rojovic Date: Fri, 8 Nov 2024 14:45:16 +0100 Subject: [PATCH 4/4] fix: indentation --- aggsender/db/migrations/0001.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aggsender/db/migrations/0001.sql b/aggsender/db/migrations/0001.sql index 69e4896e..b2d600b8 100644 --- a/aggsender/db/migrations/0001.sql +++ b/aggsender/db/migrations/0001.sql @@ -10,6 +10,6 @@ CREATE TABLE certificate_info ( from_block INTEGER NOT NULL, to_block INTEGER NOT NULL, created_at INTEGER NOT NULL, - updated_at INTEGER NOT NULL, + updated_at INTEGER NOT NULL, signed_certificate TEXT ); \ No newline at end of file