Skip to content

Commit 2fce604

Browse files
Merge branch 'main' into security-update
2 parents 6800405 + 4e76ab1 commit 2fce604

File tree

9 files changed

+196
-187
lines changed

9 files changed

+196
-187
lines changed

cmd/main.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,7 @@ func start(cliCtx *cli.Context) error {
110110

111111
var cancelFuncs []context.CancelFunc
112112

113-
sequencerTracker, err := sequencer.NewTracker(c.L1, etm)
114-
if err != nil {
115-
log.Fatal(err)
116-
}
113+
sequencerTracker := sequencer.NewTracker(c.L1, etm)
117114
go sequencerTracker.Start(cliCtx.Context)
118115
cancelFuncs = append(cancelFuncs, sequencerTracker.Stop)
119116

@@ -122,8 +119,7 @@ func start(cliCtx *cli.Context) error {
122119
log.Fatal(err)
123120
}
124121

125-
err = detector.Start()
126-
if err != nil {
122+
if err = detector.Start(); err != nil {
127123
log.Fatal(err)
128124
}
129125

@@ -157,7 +153,7 @@ func start(cliCtx *cli.Context) error {
157153
)
158154

159155
// Run!
160-
if err := server.Start(); err != nil {
156+
if err = server.Start(); err != nil {
161157
log.Fatal(err)
162158
}
163159

etherman/etherman.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ type Etherman interface {
3434
GetCurrentDataCommittee() (*DataCommittee, error)
3535
GetCurrentDataCommitteeMembers() ([]DataCommitteeMember, error)
3636
GetTx(ctx context.Context, txHash common.Hash) (*types.Transaction, bool, error)
37-
TrustedSequencer() (common.Address, error)
37+
TrustedSequencer(ctx context.Context) (common.Address, error)
3838
WatchSetTrustedSequencer(
3939
ctx context.Context,
4040
events chan *polygonvalidium.PolygonvalidiumSetTrustedSequencer,
4141
) (event.Subscription, error)
42-
TrustedSequencerURL() (string, error)
42+
TrustedSequencerURL(ctx context.Context) (string, error)
4343
WatchSetTrustedSequencerURL(
4444
ctx context.Context,
4545
events chan *polygonvalidium.PolygonvalidiumSetTrustedSequencerURL,
@@ -98,8 +98,11 @@ func (e *etherman) GetTx(ctx context.Context, txHash common.Hash) (*types.Transa
9898
}
9999

100100
// TrustedSequencer gets trusted sequencer address
101-
func (e *etherman) TrustedSequencer() (common.Address, error) {
102-
return e.CDKValidium.TrustedSequencer(&bind.CallOpts{Pending: false})
101+
func (e *etherman) TrustedSequencer(ctx context.Context) (common.Address, error) {
102+
return e.CDKValidium.TrustedSequencer(&bind.CallOpts{
103+
Context: ctx,
104+
Pending: false,
105+
})
103106
}
104107

105108
// WatchSetTrustedSequencer watches trusted sequencer address
@@ -111,8 +114,11 @@ func (e *etherman) WatchSetTrustedSequencer(
111114
}
112115

113116
// TrustedSequencerURL gets trusted sequencer's RPC url
114-
func (e *etherman) TrustedSequencerURL() (string, error) {
115-
return e.CDKValidium.TrustedSequencerURL(&bind.CallOpts{Pending: false})
117+
func (e *etherman) TrustedSequencerURL(ctx context.Context) (string, error) {
118+
return e.CDKValidium.TrustedSequencerURL(&bind.CallOpts{
119+
Context: ctx,
120+
Pending: false,
121+
})
116122
}
117123

118124
// WatchSetTrustedSequencerURL watches trusted sequencer's RPC url

mocks/etherman.generated.go

Lines changed: 30 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/backoff/backoff.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package backoff
2+
3+
import "time"
4+
5+
// Exponential performs exponential backoff attempts on a given action
6+
func Exponential(action func() error, max uint, wait time.Duration) error {
7+
var err error
8+
for i := uint(0); i < max; i++ {
9+
if err = action(); err == nil {
10+
return nil
11+
}
12+
time.Sleep(wait)
13+
wait *= 2
14+
}
15+
return err
16+
}

pkg/backoff/backoff_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package backoff
2+
3+
import (
4+
"errors"
5+
"testing"
6+
"time"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestExponential(t *testing.T) {
12+
t.Run("success", func(t *testing.T) {
13+
i := 0
14+
outcomes := []bool{false, false, true}
15+
t0 := time.Now()
16+
err := Exponential(func() error {
17+
outcome := outcomes[i]
18+
i++
19+
if outcome {
20+
return nil
21+
}
22+
return errors.New("bad")
23+
}, 3, 150*time.Millisecond)
24+
25+
elapsed := time.Since(t0)
26+
27+
require.NoError(t, err)
28+
require.Equal(t, i, 3)
29+
require.True(t, elapsed >= 450*time.Millisecond)
30+
})
31+
32+
t.Run("failed", func(t *testing.T) {
33+
i := 0
34+
t0 := time.Now()
35+
err := Exponential(func() error {
36+
i++
37+
return errors.New("bad")
38+
}, 3, 100*time.Millisecond)
39+
40+
elapsed := time.Since(t0)
41+
42+
require.Error(t, err)
43+
require.Equal(t, i, 3)
44+
require.True(t, elapsed >= 600*time.Millisecond)
45+
})
46+
}

0 commit comments

Comments
 (0)