Skip to content

Commit

Permalink
feat: add timestamps to certificate (#175)
Browse files Browse the repository at this point in the history
* feat: created and updated timestamps

* feat: save raw certificate to db

* fix: raw to signed_certificate

* fix: indentation
  • Loading branch information
goran-ethernal authored Nov 8, 2024
1 parent d4cf2db commit c1d0f13
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 14 deletions.
20 changes: 15 additions & 5 deletions aggsender/aggsender.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,21 @@ 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,
CertificateID: certificateHash,
NewLocalExitRoot: certificate.NewLocalExitRoot,
FromBlock: fromBlock,
ToBlock: toBlock,
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 {
Expand Down Expand Up @@ -504,6 +513,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)
Expand Down
83 changes: 83 additions & 0 deletions aggsender/db/aggsender_db_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package db

import (
"context"
"encoding/json"
"math/big"
"path"
"testing"
"time"

"github.com/0xPolygon/cdk/agglayer"
"github.com/0xPolygon/cdk/aggsender/db/migrations"
Expand All @@ -24,6 +27,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,
Expand All @@ -32,6 +37,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))

Expand All @@ -50,6 +57,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))

Expand All @@ -75,6 +84,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))

Expand Down Expand Up @@ -104,6 +115,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))

Expand All @@ -124,6 +137,8 @@ func Test_Storage(t *testing.T) {
FromBlock: 7,
ToBlock: 8,
Status: agglayer.Settled,
CreatedAt: updateTime,
UpdatedAt: updateTime,
},
{
Height: 9,
Expand All @@ -132,6 +147,8 @@ func Test_Storage(t *testing.T) {
FromBlock: 9,
ToBlock: 10,
Status: agglayer.Pending,
CreatedAt: updateTime,
UpdatedAt: updateTime,
},
{
Height: 11,
Expand All @@ -140,6 +157,8 @@ func Test_Storage(t *testing.T) {
FromBlock: 11,
ToBlock: 12,
Status: agglayer.InError,
CreatedAt: updateTime,
UpdatedAt: updateTime,
},
}

Expand Down Expand Up @@ -187,6 +206,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))

Expand All @@ -213,6 +234,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,
Expand All @@ -221,6 +244,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))

Expand All @@ -238,6 +263,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))

Expand Down Expand Up @@ -267,6 +294,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
Expand All @@ -284,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,
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.SignedCertificate))

require.NoError(t, storage.clean())
})
}
5 changes: 4 additions & 1 deletion aggsender/db/migrations/0001.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ 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,
signed_certificate TEXT
);
36 changes: 28 additions & 8 deletions aggsender/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"math/big"
"time"

"github.com/0xPolygon/cdk/agglayer"
"github.com/0xPolygon/cdk/bridgesync"
Expand Down Expand Up @@ -51,15 +52,34 @@ 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"`
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 {
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),
)
}

0 comments on commit c1d0f13

Please sign in to comment.