Skip to content

Commit

Permalink
Make DAC ws config tracking optional (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
begmaroman authored and christophercampbell committed Apr 17, 2024
1 parent 9ed8d9a commit 1d9c9a9
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 81 deletions.
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type L1Config struct {
Timeout types.Duration `mapstructure:"Timeout"`
RetryPeriod types.Duration `mapstructure:"RetryPeriod"`
BlockBatchSize uint `mapstructure:"BlockBatchSize"`
TrackSequencer bool `mapstructure:"TrackSequencer"`

// GenesisBlock represents the block number where PolygonValidium contract is deployed on L1
GenesisBlock uint64 `mapstructure:"GenesisBlock"`
Expand Down
1 change: 1 addition & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Timeout = "1m"
RetryPeriod = "5s"
BlockBatchSize = "64"
GenesisBlock = "0"
TrackSequencer = true
[Log]
Environment = "development" # "production" or "development"
Expand Down
1 change: 1 addition & 0 deletions docs/running.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ DataCommitteeAddress = "0x68B1D87F95878fE05B998F19b66F4baba5De1aed" # CHANGE
Timeout = "3m"
RetryPeriod = "5s"
BlockBatchSize = 32
TrackSequencer = true
[Log]
Environment = "development" # "production" or "development"
Expand Down
35 changes: 21 additions & 14 deletions sequencer/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ import (

// Tracker watches the contract for relevant changes to the sequencer
type Tracker struct {
client etherman.Etherman
stop chan struct{}
timeout time.Duration
retry time.Duration
addr common.Address
url string
lock sync.Mutex
startOnce sync.Once
client etherman.Etherman
stop chan struct{}
timeout time.Duration
retry time.Duration
addr common.Address
url string
trackChanges bool
lock sync.Mutex
startOnce sync.Once
}

// NewTracker creates a new Tracker
Expand All @@ -40,12 +41,13 @@ func NewTracker(cfg config.L1Config, ethClient etherman.Etherman) (*Tracker, err

log.Infof("current sequencer url: %s", url)
w := &Tracker{
client: ethClient,
stop: make(chan struct{}),
timeout: cfg.Timeout.Duration,
retry: cfg.RetryPeriod.Duration,
addr: addr,
url: url,
client: ethClient,
stop: make(chan struct{}),
timeout: cfg.Timeout.Duration,
retry: cfg.RetryPeriod.Duration,
addr: addr,
url: url,
trackChanges: cfg.TrackSequencer,
}

return w, nil
Expand Down Expand Up @@ -80,6 +82,11 @@ func (st *Tracker) setUrl(url string) {
// Start starts the SequencerTracker
func (st *Tracker) Start(ctx context.Context) {
st.startOnce.Do(func() {
if !st.trackChanges {
log.Info("sequencer tracking disabled")
return
}

go st.trackAddrChanges(ctx)
go st.trackUrlChanges(ctx)
})
Expand Down
166 changes: 100 additions & 66 deletions sequencer/tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,77 +84,111 @@ func Test_NewTracker(t *testing.T) {
}

func TestTracker(t *testing.T) {
var (
addressesChan chan *polygonvalidium.PolygonvalidiumSetTrustedSequencer
urlsChan chan *polygonvalidium.PolygonvalidiumSetTrustedSequencerURL
)

ctx := context.Background()

etherman := mocks.NewEtherman(t)
defer etherman.AssertExpectations(t)

etherman.On("TrustedSequencer").Return(common.Address{}, nil)
etherman.On("TrustedSequencerURL").Return("127.0.0.1:8585", nil)

addressesSubscription := mocks.NewSubscription(t)
defer addressesSubscription.AssertExpectations(t)

addressesSubscription.On("Err").Return(make(<-chan error))
addressesSubscription.On("Unsubscribe").Return()

etherman.On("WatchSetTrustedSequencer", mock.Anything, mock.Anything).
Run(func(args mock.Arguments) {
var ok bool
addressesChan, ok = args[1].(chan *polygonvalidium.PolygonvalidiumSetTrustedSequencer)
require.True(t, ok)
}).
Return(addressesSubscription, nil)

urlsSubscription := mocks.NewSubscription(t)
defer urlsSubscription.AssertExpectations(t)

urlsSubscription.On("Err").Return(make(<-chan error))
urlsSubscription.On("Unsubscribe").Return()

etherman.On("WatchSetTrustedSequencerURL", mock.Anything, mock.Anything).
Run(func(args mock.Arguments) {
var ok bool
urlsChan, ok = args[1].(chan *polygonvalidium.PolygonvalidiumSetTrustedSequencerURL)
require.True(t, ok)
}).
Return(urlsSubscription, nil)

tracker, err := sequencer.NewTracker(config.L1Config{
Timeout: types.NewDuration(time.Second * 10),
RetryPeriod: types.NewDuration(time.Millisecond),
}, etherman)
require.NoError(t, err)

tracker.Start(ctx)

var (
updatedAddress = common.BytesToAddress([]byte("updated"))
updatedURL = "127.0.0.1:9585"
)

eventually(t, 10, func() bool {
return addressesChan != nil && urlsChan != nil
t.Run("with enabled tracker", func(t *testing.T) {
var (
addressesChan chan *polygonvalidium.PolygonvalidiumSetTrustedSequencer
urlsChan chan *polygonvalidium.PolygonvalidiumSetTrustedSequencerURL
)

ctx := context.Background()

etherman := mocks.NewEtherman(t)
defer etherman.AssertExpectations(t)

etherman.On("TrustedSequencer").Return(common.Address{}, nil)
etherman.On("TrustedSequencerURL").Return("127.0.0.1:8585", nil)

addressesSubscription := mocks.NewSubscription(t)
defer addressesSubscription.AssertExpectations(t)

addressesSubscription.On("Err").Return(make(<-chan error))
addressesSubscription.On("Unsubscribe").Return()

etherman.On("WatchSetTrustedSequencer", mock.Anything, mock.Anything).
Run(func(args mock.Arguments) {
var ok bool
addressesChan, ok = args[1].(chan *polygonvalidium.PolygonvalidiumSetTrustedSequencer)
require.True(t, ok)
}).
Return(addressesSubscription, nil)

urlsSubscription := mocks.NewSubscription(t)
defer urlsSubscription.AssertExpectations(t)

urlsSubscription.On("Err").Return(make(<-chan error))
urlsSubscription.On("Unsubscribe").Return()

etherman.On("WatchSetTrustedSequencerURL", mock.Anything, mock.Anything).
Run(func(args mock.Arguments) {
var ok bool
urlsChan, ok = args[1].(chan *polygonvalidium.PolygonvalidiumSetTrustedSequencerURL)
require.True(t, ok)
}).
Return(urlsSubscription, nil)

tracker, err := sequencer.NewTracker(config.L1Config{
Timeout: types.NewDuration(time.Second * 10),
RetryPeriod: types.NewDuration(time.Millisecond),
TrackSequencer: true,
}, etherman)
require.NoError(t, err)

require.Equal(t, common.Address{}, tracker.GetAddr())
require.Equal(t, "127.0.0.1:8585", tracker.GetUrl())

tracker.Start(ctx)

var (
updatedAddress = common.BytesToAddress([]byte("updated"))
updatedURL = "127.0.0.1:9585"
)

eventually(t, 10, func() bool {
return addressesChan != nil && urlsChan != nil
})

addressesChan <- &polygonvalidium.PolygonvalidiumSetTrustedSequencer{
NewTrustedSequencer: updatedAddress,
}

urlsChan <- &polygonvalidium.PolygonvalidiumSetTrustedSequencerURL{
NewTrustedSequencerURL: updatedURL,
}

tracker.Stop()

// Wait for values to be updated
eventually(t, 10, func() bool {
return tracker.GetAddr() == updatedAddress && tracker.GetUrl() == updatedURL
})
})

addressesChan <- &polygonvalidium.PolygonvalidiumSetTrustedSequencer{
NewTrustedSequencer: updatedAddress,
}
t.Run("with disabled tracker", func(t *testing.T) {
ctx := context.Background()

urlsChan <- &polygonvalidium.PolygonvalidiumSetTrustedSequencerURL{
NewTrustedSequencerURL: updatedURL,
}
etherman := mocks.NewEtherman(t)
defer etherman.AssertExpectations(t)

etherman.On("TrustedSequencer").Return(common.Address{}, nil)
etherman.On("TrustedSequencerURL").Return("127.0.0.1:8585", nil)

tracker, err := sequencer.NewTracker(config.L1Config{
Timeout: types.NewDuration(time.Second * 10),
RetryPeriod: types.NewDuration(time.Millisecond),
}, etherman)
require.NoError(t, err)

require.Equal(t, common.Address{}, tracker.GetAddr())
require.Equal(t, "127.0.0.1:8585", tracker.GetUrl())

tracker.Start(ctx)

time.Sleep(time.Second)

tracker.Stop()
tracker.Stop()

// Wait for values to be updated
eventually(t, 10, func() bool {
return tracker.GetAddr() == updatedAddress && tracker.GetUrl() == updatedURL
require.Equal(t, common.Address{}, tracker.GetAddr())
require.Equal(t, "127.0.0.1:8585", tracker.GetUrl())
})
}

Expand Down
2 changes: 1 addition & 1 deletion test/config/test.dev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ PolygonValidiumAddress = "0x0775AAFB6dD38417581F7C583053Fa3B78FD4FD1"
DataCommitteeAddress = "0xE660928f13F51bEbb553063A1317EDC0e7038949"
Timeout = "1m"
RetryPeriod = "5s"

TrackSequencer = true

[Log]
Environment = "development" # "production" or "development"
Expand Down
1 change: 1 addition & 0 deletions test/config/test.docker.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ DataCommitteeAddress = "0x68B1D87F95878fE05B998F19b66F4baba5De1aed"
Timeout = "3m"
RetryPeriod = "5s"
BlockBatchSize = 32
TrackSequencer = true

[Log]
Environment = "development" # "production" or "development"
Expand Down
1 change: 1 addition & 0 deletions test/config/test.local.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ DataCommitteeAddress = "0x68B1D87F95878fE05B998F19b66F4baba5De1aed"
Timeout = "3m"
RetryPeriod = "5s"
BlockBatchSize = 8
TrackSequencer = true

[Log]
Environment = "development" # "production" or "development"
Expand Down

0 comments on commit 1d9c9a9

Please sign in to comment.