Skip to content

Commit

Permalink
feat: save raw certificate to db
Browse files Browse the repository at this point in the history
  • Loading branch information
goran-ethernal committed Nov 8, 2024
1 parent 534135d commit fdb0138
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
6 changes: 6 additions & 0 deletions aggsender/aggsender.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
56 changes: 56 additions & 0 deletions aggsender/db/aggsender_db_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package db

import (
"context"
"encoding/json"
"math/big"
"path"
"testing"
"time"
Expand Down Expand Up @@ -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())
})
}
3 changes: 2 additions & 1 deletion aggsender/db/migrations/0001.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
1 change: 1 addition & 0 deletions aggsender/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit fdb0138

Please sign in to comment.