Skip to content

Commit 2002ac3

Browse files
authored
Added polling mechanism to sequencer tracker (#77)
1 parent 4e76ab1 commit 2002ac3

21 files changed

+431
-454
lines changed

.mockery.yaml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,3 @@ packages:
3636
SequencerTracker:
3737
config:
3838
filename: sequencer_tracker.generated.go
39-
github.com/0xPolygon/cdk-data-availability/types:
40-
config:
41-
interfaces:
42-
EthClient:
43-
config:
44-
filename: eth_client.generated.go
45-
EthClientFactory:
46-
config:
47-
filename: eth_client_factory.generated.go

cmd/main.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"github.com/0xPolygon/cdk-data-availability/services/status"
2020
"github.com/0xPolygon/cdk-data-availability/services/sync"
2121
"github.com/0xPolygon/cdk-data-availability/synchronizer"
22-
"github.com/0xPolygon/cdk-data-availability/types"
22+
"github.com/ethereum/go-ethereum/common"
2323
"github.com/ethereum/go-ethereum/crypto"
2424
_ "github.com/lib/pq"
2525
"github.com/urfave/cli/v2"
@@ -99,11 +99,13 @@ func start(cliCtx *cli.Context) error {
9999
log.Fatal(err)
100100
}
101101

102-
// derive address
103-
selfAddr := crypto.PubkeyToAddress(pk.PublicKey)
104-
105102
// ensure synchro/reorg start block is set
106-
err = synchronizer.InitStartBlock(storage, types.NewEthClientFactory(), c.L1)
103+
err = synchronizer.InitStartBlock(
104+
storage,
105+
etm,
106+
c.L1.GenesisBlock,
107+
common.HexToAddress(c.L1.PolygonValidiumAddress),
108+
)
107109
if err != nil {
108110
log.Fatal(err)
109111
}
@@ -114,7 +116,7 @@ func start(cliCtx *cli.Context) error {
114116
go sequencerTracker.Start(cliCtx.Context)
115117
cancelFuncs = append(cancelFuncs, sequencerTracker.Stop)
116118

117-
detector, err := synchronizer.NewReorgDetector(c.L1.RpcURL, 1*time.Second)
119+
detector, err := synchronizer.NewReorgDetector(c.L1.RpcURL, time.Second)
118120
if err != nil {
119121
log.Fatal(err)
120122
}
@@ -125,8 +127,15 @@ func start(cliCtx *cli.Context) error {
125127

126128
cancelFuncs = append(cancelFuncs, detector.Stop)
127129

128-
batchSynchronizer, err := synchronizer.NewBatchSynchronizer(c.L1, selfAddr,
129-
storage, detector.Subscribe(), etm, sequencerTracker, client.NewFactory())
130+
batchSynchronizer, err := synchronizer.NewBatchSynchronizer(
131+
c.L1,
132+
crypto.PubkeyToAddress(pk.PublicKey),
133+
storage,
134+
detector.Subscribe(),
135+
etm,
136+
sequencerTracker,
137+
client.NewFactory(),
138+
)
130139
if err != nil {
131140
log.Fatal(err)
132141
}

config/config.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ type Config struct {
3232

3333
// L1Config is a struct that defines L1 contract and service settings
3434
type L1Config struct {
35-
WsURL string `mapstructure:"WsURL"`
36-
RpcURL string `mapstructure:"RpcURL"`
37-
PolygonValidiumAddress string `mapstructure:"PolygonValidiumAddress"`
38-
DataCommitteeAddress string `mapstructure:"DataCommitteeAddress"`
39-
Timeout types.Duration `mapstructure:"Timeout"`
40-
RetryPeriod types.Duration `mapstructure:"RetryPeriod"`
41-
BlockBatchSize uint `mapstructure:"BlockBatchSize"`
42-
TrackSequencer bool `mapstructure:"TrackSequencer"`
35+
RpcURL string `mapstructure:"RpcURL"`
36+
PolygonValidiumAddress string `mapstructure:"PolygonValidiumAddress"`
37+
DataCommitteeAddress string `mapstructure:"DataCommitteeAddress"`
38+
Timeout types.Duration `mapstructure:"Timeout"`
39+
RetryPeriod types.Duration `mapstructure:"RetryPeriod"`
40+
BlockBatchSize uint `mapstructure:"BlockBatchSize"`
41+
TrackSequencer bool `mapstructure:"TrackSequencer"`
42+
TrackSequencerPollInterval types.Duration `mapstructure:"TrackSequencerPollInterval"`
4343

4444
// GenesisBlock represents the block number where PolygonValidium contract is deployed on L1
4545
GenesisBlock uint64 `mapstructure:"GenesisBlock"`

config/config_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,9 @@ func Test_Defaults(t *testing.T) {
1919
path string
2020
expectedValue interface{}
2121
}{
22-
{
23-
path: "L1.WsURL",
24-
expectedValue: "ws://127.0.0.1:8546",
25-
},
2622
{
2723
path: "L1.RpcURL",
28-
expectedValue: "http://127.0.0.1:8545",
24+
expectedValue: "ws://127.0.0.1:8546",
2925
},
3026
{
3127
path: "L1.PolygonValidiumAddress",

config/default.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ const DefaultValues = `
1212
PrivateKey = {Path = "/pk/test-member.keystore", Password = "testonly"}
1313
1414
[L1]
15-
WsURL = "ws://127.0.0.1:8546"
16-
RpcURL = "http://127.0.0.1:8545"
15+
RpcURL = "ws://127.0.0.1:8546"
1716
PolygonValidiumAddress = "0x8dAF17A20c9DBA35f005b6324F493785D239719d"
1817
DataCommitteeAddress = "0x68B1D87F95878fE05B998F19b66F4baba5De1aed"
1918
Timeout = "1m"
2019
RetryPeriod = "5s"
2120
BlockBatchSize = "64"
2221
GenesisBlock = "0"
2322
TrackSequencer = true
23+
TrackSequencerPollInterval = "1m"
2424
2525
[Log]
2626
Environment = "development" # "production" or "development"

docs/running.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ services:
7676
PrivateKey = {Path = "/pk/test-member.keystore", Password = "testonly"} # CHANGE THIS (the password): according to the private key file password
7777
7878
[L1]
79-
WsURL = "ws://URLofYourL1Node:8546" # CHANGE THIS: use the URL of your L1 node
80-
RpcURL = "http://URLofYourL1Node:8545" # CHANGE THIS: use the URL of your L1 node
79+
RpcURL = "http://URLofYourL1Node:8545" # CHANGE THIS: use the URL of your L1 node, can be http(s) or ws(s)
8180
PolygonValidiumAddress = "0x8dAF17A20c9DBA35f005b6324F493785D239719d" # CHANGE THIS: Address of the Validium smart contract
8281
DataCommitteeAddress = "0x68B1D87F95878fE05B998F19b66F4baba5De1aed" # CHANGE THIS: Address of the data availability committee smart contract
8382
Timeout = "3m"
8483
RetryPeriod = "5s"
8584
BlockBatchSize = 32
8685
TrackSequencer = true
86+
TrackSequencerPollInterval = "1m"
8787
8888
[Log]
8989
Environment = "development" # "production" or "development"

etherman/etherman.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ type DataCommittee struct {
3131

3232
// Etherman defines functions that should be implemented by Etherman
3333
type Etherman interface {
34+
GetTx(ctx context.Context, txHash common.Hash) (*types.Transaction, bool, error)
35+
HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
36+
BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error)
37+
CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error)
38+
3439
GetCurrentDataCommittee() (*DataCommittee, error)
3540
GetCurrentDataCommitteeMembers() ([]DataCommitteeMember, error)
36-
GetTx(ctx context.Context, txHash common.Hash) (*types.Transaction, bool, error)
3741
TrustedSequencer(ctx context.Context) (common.Address, error)
3842
WatchSetTrustedSequencer(
3943
ctx context.Context,
@@ -44,7 +48,6 @@ type Etherman interface {
4448
ctx context.Context,
4549
events chan *polygonvalidium.PolygonvalidiumSetTrustedSequencerURL,
4650
) (event.Subscription, error)
47-
HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
4851
FilterSequenceBatches(
4952
opts *bind.FilterOpts,
5053
numBatch []uint64,
@@ -63,9 +66,9 @@ func New(ctx context.Context, cfg config.L1Config) (Etherman, error) {
6366
ctx, cancel := context.WithTimeout(ctx, cfg.Timeout.Duration)
6467
defer cancel()
6568

66-
ethClient, err := ethclient.DialContext(ctx, cfg.WsURL)
69+
ethClient, err := ethclient.DialContext(ctx, cfg.RpcURL)
6770
if err != nil {
68-
log.Errorf("error connecting to %s: %+v", cfg.WsURL, err)
71+
log.Errorf("error connecting to %s: %+v", cfg.RpcURL, err)
6972
return nil, err
7073
}
7174

@@ -97,6 +100,21 @@ func (e *etherman) GetTx(ctx context.Context, txHash common.Hash) (*types.Transa
97100
return e.EthClient.TransactionByHash(ctx, txHash)
98101
}
99102

103+
// HeaderByNumber returns header by number from the eth client
104+
func (e *etherman) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
105+
return e.EthClient.HeaderByNumber(ctx, number)
106+
}
107+
108+
// BlockByNumber returns a block by the given number
109+
func (e *etherman) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) {
110+
return e.EthClient.BlockByNumber(ctx, number)
111+
}
112+
113+
// CodeAt returns the contract code of the given account.
114+
func (e *etherman) CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error) {
115+
return e.EthClient.CodeAt(ctx, account, blockNumber)
116+
}
117+
100118
// TrustedSequencer gets trusted sequencer address
101119
func (e *etherman) TrustedSequencer(ctx context.Context) (common.Address, error) {
102120
return e.CDKValidium.TrustedSequencer(&bind.CallOpts{
@@ -129,11 +147,6 @@ func (e *etherman) WatchSetTrustedSequencerURL(
129147
return e.CDKValidium.WatchSetTrustedSequencerURL(&bind.WatchOpts{Context: ctx}, events)
130148
}
131149

132-
// HeaderByNumber returns header by number from the eth client
133-
func (e *etherman) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
134-
return e.EthClient.HeaderByNumber(ctx, number)
135-
}
136-
137150
// FilterSequenceBatches retrieves filtered batches on CDK validium
138151
func (e *etherman) FilterSequenceBatches(opts *bind.FilterOpts,
139152
numBatch []uint64) (*polygonvalidium.PolygonvalidiumSequenceBatchesIterator, error) {

mocks/eth_client.generated.go

Lines changed: 0 additions & 161 deletions
This file was deleted.

0 commit comments

Comments
 (0)