Skip to content

Commit

Permalink
fix: rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
goran-ethernal committed Aug 23, 2024
1 parent a663963 commit f121e17
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 72 deletions.
58 changes: 28 additions & 30 deletions aggsender/aggsender.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/0xPolygon/cdk/bridgesync"
cdkcommon "github.com/0xPolygon/cdk/common"
"github.com/0xPolygon/cdk/config/types"
"github.com/0xPolygon/cdk/etherman"
"github.com/0xPolygon/cdk/log"
"github.com/0xPolygon/cdk/sync"
"github.com/ethereum/go-ethereum/common"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/mdbx"
Expand Down Expand Up @@ -41,30 +41,39 @@ func (n network) getTable() string {
}
}

func (n network) getCfg(a *AggSender) bridgesync.Config {
func (n network) getClient(a *AggSender) bridgesync.EthClienter {
switch n {
case L1:
return a.l1Cfg
return a.l1Client
default:
return a.l2Cfg
return a.l2Client
}
}

func (n network) getClient(a *AggSender) bridgesync.EthClienter {
func (n network) getSyncer(a *AggSender) *bridgesync.BridgeSync {
switch n {
case L1:
return a.l1Client
return a.l1Syncer
default:
return a.l2Client
return a.l2Syncer
}
}

func (n network) getSyncer(a *AggSender) *bridgesync.LocalBridgeSync {
func (n network) getOriginNetwork(a *AggSender) uint32 {
switch n {
case L1:
return a.l1Syncer
return a.l1Syncer.OriginNetwork()
default:
return a.l2Syncer
return a.l2Syncer.OriginNetwork()
}
}

func (n network) getBlockFinality(a *AggSender) etherman.BlockNumberFinality {
switch n {
case L1:
return a.l1Syncer.BlockFinality()
default:
return a.l2Syncer.BlockFinality()
}
}

Expand All @@ -86,13 +95,11 @@ func tableCfgFunc(defaultBuckets kv.TableCfg) kv.TableCfg {

// AggSender is a component that will send certificates to the aggLayer
type AggSender struct {
l2Syncer *bridgesync.LocalBridgeSync
l2Syncer *bridgesync.BridgeSync
l2Client bridgesync.EthClienter
l2Cfg bridgesync.Config

l1Syncer *bridgesync.LocalBridgeSync
l1Syncer *bridgesync.BridgeSync
l1Client bridgesync.EthClienter
l1Cfg bridgesync.Config

db kv.RwDB
aggLayerClient agglayer.AggLayerClient
Expand All @@ -106,21 +113,11 @@ type AggSender struct {
func New(
ctx context.Context,
cfg Config,
l1ReorgDetector sync.ReorgDetector,
l2ReorgDetector sync.ReorgDetector,
aggLayerClient agglayer.AggLayerClient,
l1Syncer *bridgesync.BridgeSync,
l2Syncer *bridgesync.BridgeSync,
l1Client bridgesync.EthClienter,
l2Client bridgesync.EthClienter) (*AggSender, error) {
l1Syncer, err := bridgesync.NewL1(ctx, cfg.L1BridgeSyncer, l1ReorgDetector, l1Client)
if err != nil {
return nil, err
}

l2Syncer, err := bridgesync.NewL2(ctx, cfg.L2BridgeSyncer, l2ReorgDetector, l2Client)
if err != nil {
return nil, err
}

db, err := mdbx.NewMDBX(nil).
Path(filepath.Join(cfg.DBPath, aggSenderDBFolder)).
WithTableCfg(tableCfgFunc).
Expand Down Expand Up @@ -175,7 +172,7 @@ func (a *AggSender) sendCertificates(ctx context.Context) {
}

// buildCertificate builds a certificate from the bridge events
func (a *AggSender) buildCertificate(ctx context.Context, syncer *bridgesync.LocalBridgeSync,
func (a *AggSender) buildCertificate(ctx context.Context, syncer *bridgesync.BridgeSync,
bridgeEvents []bridgesync.Event, originNetwork uint32, previousExitRoot common.Hash) (*agglayer.Certificate, error) {
bridgeExits := make([]*bridgesync.Bridge, 0, len(bridgeEvents))
importedBridgeExits := make([]*bridgesync.Claim, 0, len(bridgeEvents))
Expand Down Expand Up @@ -203,7 +200,8 @@ func (a *AggSender) buildCertificate(ctx context.Context, syncer *bridgesync.Loc

// sendCertificatesForNetwork sends certificates for a network
func (a *AggSender) sendCertificatesForNetwork(ctx context.Context, n network) error {
cfg := n.getCfg(a)
blockFinalityType := n.getBlockFinality(a)
originNetwork := n.getOriginNetwork(a)
client := n.getClient(a)
syncer := n.getSyncer(a)

Expand All @@ -212,7 +210,7 @@ func (a *AggSender) sendCertificatesForNetwork(ctx context.Context, n network) e
return fmt.Errorf("error getting last sent certificate: %w. Network: %s", err, n)
}

blockFinality, err := cfg.BlockFinalityType.ToBlockNum()
blockFinality, err := blockFinalityType.ToBlockNum()
if err != nil {
return fmt.Errorf("error getting block finality: %w. Network: %s", err, n)
}
Expand All @@ -237,7 +235,7 @@ func (a *AggSender) sendCertificatesForNetwork(ctx context.Context, n network) e
previousExitRoot = lastSentCertificate.NewLocalExitRoot
}

certificate, err := a.buildCertificate(ctx, syncer, bridgeEvents, cfg.OriginNetwork, previousExitRoot)
certificate, err := a.buildCertificate(ctx, syncer, bridgeEvents, originNetwork, previousExitRoot)
if err != nil {
return fmt.Errorf("error building certificate: %w. Network: %s", err, n)
}
Expand Down
6 changes: 0 additions & 6 deletions aggsender/config.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
package aggsender

import (
"github.com/0xPolygon/cdk/bridgesync"
"github.com/0xPolygon/cdk/config/types"
"github.com/0xPolygon/cdk/reorgdetector"
)

// Config is the configuration for the AggSender
type Config struct {
DBPath string `mapstructure:"DBPath"`
AggLayerUrl string `mapstructure:"AggLayerUrl"`
CertificateSendInterval types.Duration `mapstructure:"CertificateSendInterval"`
L1BridgeSyncer bridgesync.Config `mapstructure:"L1BridgeSyncer"`
L2BridgeSyncer bridgesync.Config `mapstructure:"L2BridgeSyncer"`
SequencerPrivateKey types.KeystoreFileConfig `mapstructure:"SequencerPrivateKey"`
ReorgDetectorL1 reorgdetector.Config `mapstructure:"ReorgDetectorL1"`
ReorgDetectorL2 reorgdetector.Config `mapstructure:"ReorgDetectorL2"`
}
61 changes: 48 additions & 13 deletions bridgesync/bridgesync.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const (
type BridgeSync struct {
processor *processor
driver *sync.EVMDriver

originNetwork uint32
blockFinality etherman.BlockNumberFinality
}

// NewL1 creates a bridge syncer that synchronizes the mainnet exit tree
Expand All @@ -33,63 +36,83 @@ func NewL1(
waitForNewBlocksPeriod time.Duration,
retryAfterErrorPeriod time.Duration,
maxRetryAttemptsAfterError int,
originNetwork uint32,
) (*BridgeSync, error) {
return newBridgeSync(
ctx,
cfg,
dbPath,
bridge,
syncBlockChunkSize,
blockFinalityType,
rd,
ethClient,
initialBlock,
bridgeSyncL1,
waitForNewBlocksPeriod,
retryAfterErrorPeriod,
maxRetryAttemptsAfterError,
originNetwork,
)
}

// NewL2 creates a bridge syncer that synchronizes the local exit tree
func NewL2(
ctx context.Context,
cfg Config,
dbPath string,
bridge common.Address,
syncBlockChunkSize uint64,
blockFinalityType etherman.BlockNumberFinality,
rd sync.ReorgDetector,
ethClient EthClienter,
initialBlock uint64,
waitForNewBlocksPeriod time.Duration,
retryAfterErrorPeriod time.Duration,
maxRetryAttemptsAfterError int,
originNetwork uint32,
) (*BridgeSync, error) {
return newBridgeSync(
ctx,
cfg,
dbPath,
bridge,
syncBlockChunkSize,
blockFinalityType,
rd,
ethClient,
initialBlock,
bridgeSyncL2,
waitForNewBlocksPeriod,
retryAfterErrorPeriod,
maxRetryAttemptsAfterError,
originNetwork,
)
}

func newBridgeSync(
ctx context.Context,
cfg Config,
dbPath string,
bridge common.Address,
syncBlockChunkSize uint64,
blockFinalityType etherman.BlockNumberFinality,
rd sync.ReorgDetector,
ethClient EthClienter,
initialBlock uint64,
l1OrL2ID string,
waitForNewBlocksPeriod time.Duration,
retryAfterErrorPeriod time.Duration,
maxRetryAttemptsAfterError int,
originNetwork uint32,
) (*BridgeSync, error) {
processor, err := newProcessor(ctx, cfg.DBPath, l1OrL2ID)
processor, err := newProcessor(ctx, dbPath, l1OrL2ID)
if err != nil {
return nil, err
}
lastProcessedBlock, err := processor.GetLastProcessedBlock(ctx)
if err != nil {
return nil, err
}
if lastProcessedBlock < cfg.InitialBlock {
if lastProcessedBlock < initialBlock {
err = processor.ProcessBlock(ctx, sync.Block{
Num: cfg.InitialBlock,
Num: initialBlock,
})
if err != nil {
return nil, err
Expand All @@ -100,18 +123,18 @@ func newBridgeSync(
RetryAfterErrorPeriod: retryAfterErrorPeriod,
}

appender, err := buildAppender(ethClient, cfg.BridgeAddr)
appender, err := buildAppender(ethClient, bridge)
if err != nil {
return nil, err
}
downloader, err := sync.NewEVMDownloader(
l1OrL2ID,
ethClient,
cfg.SyncBlockChunkSize,
cfg.BlockFinalityType,
syncBlockChunkSize,
blockFinalityType,
waitForNewBlocksPeriod,
appender,
[]common.Address{cfg.BridgeAddr},
[]common.Address{bridge},
rh,
)
if err != nil {
Expand All @@ -123,8 +146,10 @@ func newBridgeSync(
return nil, err
}
return &BridgeSync{
processor: processor,
driver: driver,
processor: processor,
driver: driver,
originNetwork: originNetwork,
blockFinality: blockFinalityType,
}, nil
}

Expand Down Expand Up @@ -159,3 +184,13 @@ func (s *BridgeSync) GetExitRootByIndex(ctx context.Context, index uint32) (comm

return s.processor.exitTree.GetRootByIndex(tx, index)
}

// OriginNetwork returns the network ID of the origin chain
func (s *BridgeSync) OriginNetwork() uint32 {
return s.originNetwork
}

// BlockFinality returns the block finality type
func (s *BridgeSync) BlockFinality() etherman.BlockNumberFinality {
return s.blockFinality
}
2 changes: 2 additions & 0 deletions bridgesync/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ type Config struct {
MaxRetryAttemptsAfterError int `mapstructure:"MaxRetryAttemptsAfterError"`
// WaitForNewBlocksPeriod time that will be waited when the synchronizer has reached the latest block
WaitForNewBlocksPeriod types.Duration `mapstructure:"WaitForNewBlocksPeriod"`
// OriginNetwork is the id of the network where the bridge is deployed
OriginNetwork uint32 `mapstructure:"OriginNetwork"`
}
5 changes: 3 additions & 2 deletions bridgesync/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (
)

var (
bridgeEventSignature = crypto.Keccak256Hash([]byte("BridgeEvent(uint8,uint32,address,uint32,address,uint256,bytes,uint32)"))
claimEventSignature = crypto.Keccak256Hash([]byte("ClaimEvent(uint256,uint32,address,address,uint256)"))
bridgeEventSignature = crypto.Keccak256Hash([]byte("BridgeEvent(uint8,uint32,address,uint32,address,uint256,bytes,uint32)"))
claimEventSignature = crypto.Keccak256Hash([]byte("ClaimEvent(uint256,uint32,address,address,uint256)"))
claimEventSignaturePreEtrog = crypto.Keccak256Hash([]byte("ClaimEvent(uint32,uint32,address,address,uint256)"))
)

type EthClienter interface {
Expand Down
2 changes: 1 addition & 1 deletion bridgesync/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestBridgeEventE2E(t *testing.T) {
require.NoError(t, err)
go rd.Start(ctx)

syncer, err := bridgesync.NewL1(ctx, dbPathSyncer, bridgeAddr, 10, etherman.LatestBlock, rd, client.Client(), 0, time.Millisecond*10, 0, 0)
syncer, err := bridgesync.NewL1(ctx, dbPathSyncer, bridgeAddr, 10, etherman.LatestBlock, rd, client.Client(), 0, time.Millisecond*10, 0, 0, 1)
require.NoError(t, err)
go syncer.Start(ctx)

Expand Down
2 changes: 1 addition & 1 deletion claimsponsor/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestE2EL1toEVML2(t *testing.T) {
ctx := context.Background()
env := helpers.SetupAggoracleWithEVMChain(t)
dbPathBridgeSyncL1 := t.TempDir()
bridgeSyncL1, err := bridgesync.NewL1(ctx, dbPathBridgeSyncL1, env.BridgeL1Addr, 10, etherman.LatestBlock, env.ReorgDetector, env.L1Client.Client(), 0, time.Millisecond*10, 0, 0)
bridgeSyncL1, err := bridgesync.NewL1(ctx, dbPathBridgeSyncL1, env.BridgeL1Addr, 10, etherman.LatestBlock, env.ReorgDetector, env.L1Client.Client(), 0, time.Millisecond*10, 0, 0, 1)
require.NoError(t, err)
go bridgeSyncL1.Start(ctx)

Expand Down
2 changes: 2 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ func runBridgeSyncL1IfNeeded(
cfg.WaitForNewBlocksPeriod.Duration,
cfg.RetryAfterErrorPeriod.Duration,
cfg.MaxRetryAttemptsAfterError,
cfg.OriginNetwork,
)
if err != nil {
log.Fatalf("error creating bridgeSyncL1: %s", err)
Expand Down Expand Up @@ -650,6 +651,7 @@ func runBridgeSyncL2IfNeeded(
cfg.WaitForNewBlocksPeriod.Duration,
cfg.RetryAfterErrorPeriod.Duration,
cfg.MaxRetryAttemptsAfterError,
cfg.OriginNetwork,
)
if err != nil {
log.Fatalf("error creating bridgeSyncL2: %s", err)
Expand Down
18 changes: 0 additions & 18 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,22 +254,4 @@ DBPath = "/tmp/aggsender"
AggLayerURL = "http://zkevm-agglayer"
SequencerPrivateKey = {Path = "/pk/sequencer.keystore", Password = "testonly"}
CertificateSendInterval = "1m"
[ReorgDetectorL1]
DBPath = "/tmp/aggsender/reorgdetectorL1"
[ReorgDetectorL2]
DBPath = "/tmp/aggsender/reorgdetectorL2"
[AggSender.L1BridgeSyncer]
OriginNetwork = 0
DBPath = "/tmp/aggsender/l1"
BridgeAddr = ""
SyncBlockChunkSize = "10"
InitialBlock = "0"
BlockFinalityType = "latest"
[AggSender.L2BridgeSyncer]
OriginNetwork = 1
DBPath = "/tmp/aggsender/l2"
BridgeAddr = ""
SyncBlockChunkSize = "10"
InitialBlock = "0"
BlockFinalityType = "latest"
`
2 changes: 1 addition & 1 deletion l1bridge2infoindexsync/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func TestE2E(t *testing.T) {
rd, err := reorgdetector.New(ctx, client.Client(), dbPathReorg)
go rd.Start(ctx)

bridgeSync, err := bridgesync.NewL1(ctx, dbPathBridgeSync, bridgeAddr, 10, etherman.LatestBlock, rd, client.Client(), 0, time.Millisecond*10, 0, 0)
bridgeSync, err := bridgesync.NewL1(ctx, dbPathBridgeSync, bridgeAddr, 10, etherman.LatestBlock, rd, client.Client(), 0, time.Millisecond*10, 0, 0, 1)
require.NoError(t, err)
go bridgeSync.Start(ctx)

Expand Down

0 comments on commit f121e17

Please sign in to comment.