Skip to content

Commit

Permalink
complete localbridgesync implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaubennassar committed Jul 12, 2024
1 parent 153b976 commit de6d2ac
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
8 changes: 4 additions & 4 deletions localbridgesync/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const (
)

type driver struct {
reorgDetector reorgDetectorInterface
reorgDetector ReorgDetectorInterface
reorgSub *reorgdetector.Subscription
processor processorInterface
downloader downloaderInterface
Expand All @@ -26,15 +26,15 @@ type processorInterface interface {
reorg(firstReorgedBlock uint64) error
}

type reorgDetectorInterface interface {
type ReorgDetectorInterface interface {
Subscribe(id string) *reorgdetector.Subscription
AddBlockToTrack(ctx context.Context, id string, blockNum uint64, blockHash common.Hash) error
}

type downloadFn func(ctx context.Context, d downloaderInterface, fromBlock, syncBlockChunkSize uint64, downloadedCh chan block)

func newDriver(
reorgDetector reorgDetectorInterface,
reorgDetector ReorgDetectorInterface,
processor processorInterface,
downloader downloaderInterface,
) (*driver, error) {
Expand All @@ -47,7 +47,7 @@ func newDriver(
}, nil
}

func (d *driver) sync(ctx context.Context, syncBlockChunkSize uint64, download downloadFn) {
func (d *driver) Sync(ctx context.Context, syncBlockChunkSize uint64, download downloadFn) {
reset:
var (
lastProcessedBlock uint64
Expand Down
2 changes: 1 addition & 1 deletion localbridgesync/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestSync(t *testing.T) {
Return(nil)
pm.On("storeBridgeEvents", expectedBlock2.Num, expectedBlock2.Events).
Return(nil)
go driver.sync(ctx, syncBlockChunck, mockDownload)
go driver.Sync(ctx, syncBlockChunck, mockDownload)
time.Sleep(time.Millisecond * 200) // time to download expectedBlock1

// Trigger reorg 1
Expand Down
25 changes: 21 additions & 4 deletions localbridgesync/localbridgesync.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package localbridgesync

import (
"errors"
"time"

"github.com/0xPolygon/cdk/log"
"github.com/ethereum/go-ethereum/common"
)

var (
Expand All @@ -14,11 +14,28 @@ var (

type LocalBridgeSync struct {
*processor
*driver
}

func New() (*LocalBridgeSync, error) {
// init driver, processor and downloader
return &LocalBridgeSync{}, errors.New("not implemented")
func New(
dbPath string,
bridge common.Address,
rd ReorgDetectorInterface,
l2Client EthClienter,
) (*LocalBridgeSync, error) {
p, err := newProcessor(dbPath)
if err != nil {
return nil, err
}
dwn, err := newDownloader(bridge, l2Client)
if err != nil {
return nil, err
}
dri, err := newDriver(rd, p, dwn)
if err != nil {
return nil, err
}
return &LocalBridgeSync{p, dri}, nil
}

func retryHandler(funcName string, attempts int) {
Expand Down

0 comments on commit de6d2ac

Please sign in to comment.