Skip to content

Commit

Permalink
feat(aggsender): add more data to certificate metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
vcastellm committed Nov 26, 2024
1 parent e93e1b2 commit 5f3fc94
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
2 changes: 1 addition & 1 deletion agglayer/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ type Certificate struct {
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[:]))
common.Bytes2Hex(c.NewLocalExitRoot[:]), c.Metadata.String())

if c.BridgeExits == nil {
res += " BridgeExits: nil\n"
Expand Down
40 changes: 33 additions & 7 deletions aggsender/aggsender.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package aggsender

import (
"bytes"
"compress/gzip"
"context"
"crypto/ecdsa"
"encoding/json"
"errors"
"fmt"
"math/big"
"os"
"time"

Expand Down Expand Up @@ -190,7 +191,8 @@ func (a *AggSender) sendCertificate(ctx context.Context) (*agglayer.SignedCertif

a.log.Infof("building certificate for block: %d to block: %d", fromBlock, toBlock)

certificate, err := a.buildCertificate(ctx, bridges, claims, *lastSentCertificateInfo, toBlock)
createdTime := time.Now().UTC().UnixMilli()
certificate, err := a.buildCertificate(ctx, bridges, claims, *lastSentCertificateInfo, fromBlock, toBlock, createdTime)
if err != nil {
return nil, fmt.Errorf("error building certificate: %w", err)
}
Expand All @@ -215,7 +217,6 @@ func (a *AggSender) sendCertificate(ctx context.Context) (*agglayer.SignedCertif
return nil, fmt.Errorf("error marshalling signed certificate: %w", err)
}

createdTime := time.Now().UTC().UnixMilli()
certInfo := types.CertificateInfo{
Height: certificate.Height,
CertificateID: certificateHash,
Expand Down Expand Up @@ -304,7 +305,9 @@ func (a *AggSender) buildCertificate(ctx context.Context,
bridges []bridgesync.Bridge,
claims []bridgesync.Claim,
lastSentCertificateInfo types.CertificateInfo,
toBlock uint64) (*agglayer.Certificate, error) {
fromBlock, toBlock uint64,
createdAt int64,
) (*agglayer.Certificate, error) {
if len(bridges) == 0 && len(claims) == 0 {
return nil, errNoBridgesAndClaims
}
Expand Down Expand Up @@ -335,7 +338,7 @@ func (a *AggSender) buildCertificate(ctx context.Context,
BridgeExits: bridgeExits,
ImportedBridgeExits: importedBridgeExits,
Height: height,
Metadata: createCertificateMetadata(toBlock),
Metadata: createCertificateMetadata(fromBlock, toBlock, createdAt),
}, nil
}

Expand Down Expand Up @@ -692,8 +695,31 @@ func extractSignatureData(signature []byte) (r, s common.Hash, isOddParity bool,
}

// createCertificateMetadata creates a certificate metadata from given input
func createCertificateMetadata(toBlock uint64) common.Hash {
return common.BigToHash(new(big.Int).SetUint64(toBlock))
func createCertificateMetadata(fromBlock, toBlock uint64, createdAt int64) common.Hash {
meta := &types.CertificateMetadata{
FromBlock: fromBlock,
ToBlock: toBlock,
CreatedAt: createdAt,
}

// marshal the metadata to json and gzip it
metaJSON, err := json.Marshal(meta)
if err != nil {
panic(err)
}

buf := bytes.NewBuffer(make([]byte, 0, 32))

Check failure on line 711 in aggsender/aggsender.go

View workflow job for this annotation

GitHub Actions / lint

Magic number: 32, in <argument> detected (mnd)
zw := gzip.NewWriter(buf)

if _, err := zw.Write(metaJSON); err != nil {
log.Fatal(err)
}

if err := zw.Close(); err != nil {
log.Fatal(err)
}

return common.BytesToHash(buf.Bytes())
}

func extractFromCertificateMetadataToBlock(metadata common.Hash) uint64 {
Expand Down
22 changes: 17 additions & 5 deletions aggsender/aggsender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,7 @@ func TestBuildCertificate(t *testing.T) {
bridges []bridgesync.Bridge
claims []bridgesync.Claim
lastSentCertificateInfo aggsendertypes.CertificateInfo
fromBlock uint64
toBlock uint64
mockFn func()
expectedCert *agglayer.Certificate
Expand Down Expand Up @@ -628,12 +629,13 @@ func TestBuildCertificate(t *testing.T) {
NewLocalExitRoot: common.HexToHash("0x123"),
Height: 1,
},
toBlock: 10,
fromBlock: 0,
toBlock: 10,
expectedCert: &agglayer.Certificate{
NetworkID: 1,
PrevLocalExitRoot: common.HexToHash("0x123"),
NewLocalExitRoot: common.HexToHash("0x789"),
Metadata: createCertificateMetadata(10),
Metadata: createCertificateMetadata(0, 10, time.Now().UTC().UnixMilli()),
BridgeExits: []*agglayer.BridgeExit{
{
LeafType: agglayer.LeafTypeAsset,
Expand Down Expand Up @@ -784,7 +786,15 @@ func TestBuildCertificate(t *testing.T) {
l1infoTreeSyncer: mockL1InfoTreeSyncer,
log: log.WithFields("test", "unittest"),
}
cert, err := aggSender.buildCertificate(context.Background(), tt.bridges, tt.claims, tt.lastSentCertificateInfo, tt.toBlock)
cert, err := aggSender.buildCertificate(
context.Background(),
tt.bridges,
tt.claims,
tt.lastSentCertificateInfo,
tt.fromBlock,
tt.toBlock,
0,
)

if tt.expectedError {
require.Error(t, err)
Expand Down Expand Up @@ -1609,8 +1619,10 @@ func TestSendCertificate_NoClaims(t *testing.T) {
}

func TestMetadataConversions(t *testing.T) {
fromBlock := uint64(123567890)
toBlock := uint64(123567890)
c := createCertificateMetadata(toBlock)
createdAt := int64(123567890)
c := createCertificateMetadata(fromBlock, toBlock, createdAt)
extractBlock := extractFromCertificateMetadataToBlock(c)
require.Equal(t, toBlock, extractBlock)
}
Expand Down Expand Up @@ -1816,7 +1828,7 @@ func certInfoToCertHeader(certInfo *aggsendertypes.CertificateInfo, networkID ui
CertificateID: certInfo.CertificateID,
NewLocalExitRoot: certInfo.NewLocalExitRoot,
Status: agglayer.Pending,
Metadata: createCertificateMetadata(certInfo.ToBlock),
Metadata: createCertificateMetadata(certInfo.FromBlock, certInfo.ToBlock, certInfo.CreatedAt),
}
}

Expand Down
6 changes: 6 additions & 0 deletions aggsender/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,9 @@ func (c *CertificateInfo) ElapsedTimeSinceCreation() time.Duration {
}
return time.Now().UTC().Sub(time.UnixMilli(c.CreatedAt))
}

type CertificateMetadata struct {
FromBlock uint64 `json:"fromBlock"`
ToBlock uint64 `json:"toBlock"`
CreatedAt int64 `json:"createdAt"`
}

0 comments on commit 5f3fc94

Please sign in to comment.