Skip to content

Commit

Permalink
Merge branch 'main' into security-update
Browse files Browse the repository at this point in the history
  • Loading branch information
mt-polygon-technology authored Apr 4, 2024
2 parents 6800405 + 4e76ab1 commit 2fce604
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 187 deletions.
10 changes: 3 additions & 7 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,7 @@ func start(cliCtx *cli.Context) error {

var cancelFuncs []context.CancelFunc

sequencerTracker, err := sequencer.NewTracker(c.L1, etm)
if err != nil {
log.Fatal(err)
}
sequencerTracker := sequencer.NewTracker(c.L1, etm)
go sequencerTracker.Start(cliCtx.Context)
cancelFuncs = append(cancelFuncs, sequencerTracker.Stop)

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

err = detector.Start()
if err != nil {
if err = detector.Start(); err != nil {
log.Fatal(err)
}

Expand Down Expand Up @@ -157,7 +153,7 @@ func start(cliCtx *cli.Context) error {
)

// Run!
if err := server.Start(); err != nil {
if err = server.Start(); err != nil {
log.Fatal(err)
}

Expand Down
18 changes: 12 additions & 6 deletions etherman/etherman.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ type Etherman interface {
GetCurrentDataCommittee() (*DataCommittee, error)
GetCurrentDataCommitteeMembers() ([]DataCommitteeMember, error)
GetTx(ctx context.Context, txHash common.Hash) (*types.Transaction, bool, error)
TrustedSequencer() (common.Address, error)
TrustedSequencer(ctx context.Context) (common.Address, error)
WatchSetTrustedSequencer(
ctx context.Context,
events chan *polygonvalidium.PolygonvalidiumSetTrustedSequencer,
) (event.Subscription, error)
TrustedSequencerURL() (string, error)
TrustedSequencerURL(ctx context.Context) (string, error)
WatchSetTrustedSequencerURL(
ctx context.Context,
events chan *polygonvalidium.PolygonvalidiumSetTrustedSequencerURL,
Expand Down Expand Up @@ -98,8 +98,11 @@ func (e *etherman) GetTx(ctx context.Context, txHash common.Hash) (*types.Transa
}

// TrustedSequencer gets trusted sequencer address
func (e *etherman) TrustedSequencer() (common.Address, error) {
return e.CDKValidium.TrustedSequencer(&bind.CallOpts{Pending: false})
func (e *etherman) TrustedSequencer(ctx context.Context) (common.Address, error) {
return e.CDKValidium.TrustedSequencer(&bind.CallOpts{
Context: ctx,
Pending: false,
})
}

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

// TrustedSequencerURL gets trusted sequencer's RPC url
func (e *etherman) TrustedSequencerURL() (string, error) {
return e.CDKValidium.TrustedSequencerURL(&bind.CallOpts{Pending: false})
func (e *etherman) TrustedSequencerURL(ctx context.Context) (string, error) {
return e.CDKValidium.TrustedSequencerURL(&bind.CallOpts{
Context: ctx,
Pending: false,
})
}

// WatchSetTrustedSequencerURL watches trusted sequencer's RPC url
Expand Down
58 changes: 30 additions & 28 deletions mocks/etherman.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions pkg/backoff/backoff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package backoff

import "time"

// Exponential performs exponential backoff attempts on a given action
func Exponential(action func() error, max uint, wait time.Duration) error {
var err error
for i := uint(0); i < max; i++ {
if err = action(); err == nil {
return nil
}
time.Sleep(wait)
wait *= 2
}
return err
}
46 changes: 46 additions & 0 deletions pkg/backoff/backoff_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package backoff

import (
"errors"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestExponential(t *testing.T) {
t.Run("success", func(t *testing.T) {
i := 0
outcomes := []bool{false, false, true}
t0 := time.Now()
err := Exponential(func() error {
outcome := outcomes[i]
i++
if outcome {
return nil
}
return errors.New("bad")
}, 3, 150*time.Millisecond)

elapsed := time.Since(t0)

require.NoError(t, err)
require.Equal(t, i, 3)
require.True(t, elapsed >= 450*time.Millisecond)
})

t.Run("failed", func(t *testing.T) {
i := 0
t0 := time.Now()
err := Exponential(func() error {
i++
return errors.New("bad")
}, 3, 100*time.Millisecond)

elapsed := time.Since(t0)

require.Error(t, err)
require.Equal(t, i, 3)
require.True(t, elapsed >= 600*time.Millisecond)
})
}
Loading

0 comments on commit 2fce604

Please sign in to comment.