Commit 360fe67d authored by Adrian Sutton's avatar Adrian Sutton Committed by GitHub

op-challenger: Fix a race condition in challenger tests (#9850)

* op-challenger: Fix a race condition in challenger tests now that actions are performed in parallel.

* op-dispute-mon: Fix flaky bond enricher test
parent efdf5a1f
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"context" "context"
"errors" "errors"
"math/big" "math/big"
"sync"
"testing" "testing"
"github.com/ethereum-optimism/optimism/op-challenger/game/fault/trace" "github.com/ethereum-optimism/optimism/op-challenger/game/fault/trace"
...@@ -172,6 +173,7 @@ func (s *stubClaimLoader) GetAllClaims(_ context.Context, _ rpcblock.Block) ([]t ...@@ -172,6 +173,7 @@ func (s *stubClaimLoader) GetAllClaims(_ context.Context, _ rpcblock.Block) ([]t
} }
type stubResponder struct { type stubResponder struct {
l sync.Mutex
callResolveCount int callResolveCount int
callResolveStatus gameTypes.GameStatus callResolveStatus gameTypes.GameStatus
callResolveErr error callResolveErr error
...@@ -185,21 +187,29 @@ type stubResponder struct { ...@@ -185,21 +187,29 @@ type stubResponder struct {
} }
func (s *stubResponder) CallResolve(ctx context.Context) (gameTypes.GameStatus, error) { func (s *stubResponder) CallResolve(ctx context.Context) (gameTypes.GameStatus, error) {
s.l.Lock()
defer s.l.Unlock()
s.callResolveCount++ s.callResolveCount++
return s.callResolveStatus, s.callResolveErr return s.callResolveStatus, s.callResolveErr
} }
func (s *stubResponder) Resolve() error { func (s *stubResponder) Resolve() error {
s.l.Lock()
defer s.l.Unlock()
s.resolveCount++ s.resolveCount++
return s.resolveErr return s.resolveErr
} }
func (s *stubResponder) CallResolveClaim(ctx context.Context, clainIdx uint64) error { func (s *stubResponder) CallResolveClaim(ctx context.Context, clainIdx uint64) error {
s.l.Lock()
defer s.l.Unlock()
s.callResolveClaimCount++ s.callResolveClaimCount++
return s.callResolveClaimErr return s.callResolveClaimErr
} }
func (s *stubResponder) ResolveClaim(clainIdx uint64) error { func (s *stubResponder) ResolveClaim(clainIdx uint64) error {
s.l.Lock()
defer s.l.Unlock()
s.resolveClaimCount++ s.resolveClaimCount++
return nil return nil
} }
......
...@@ -52,7 +52,7 @@ func TestBondEnricher(t *testing.T) { ...@@ -52,7 +52,7 @@ func TestBondEnricher(t *testing.T) {
t.Run("GetCreditsWrongNumberOfResults", func(t *testing.T) { t.Run("GetCreditsWrongNumberOfResults", func(t *testing.T) {
enricher := NewBondEnricher() enricher := NewBondEnricher()
caller := &mockGameCaller{credits: []*big.Int{big.NewInt(4)}} caller := &mockGameCaller{extraCredit: []*big.Int{big.NewInt(4)}}
game := makeGame() game := makeGame()
err := enricher.Enrich(context.Background(), rpcblock.Latest, caller, game) err := enricher.Enrich(context.Background(), rpcblock.Latest, caller, game)
require.ErrorIs(t, err, ErrIncorrectCreditCount) require.ErrorIs(t, err, ErrIncorrectCreditCount)
...@@ -69,16 +69,18 @@ func TestBondEnricher(t *testing.T) { ...@@ -69,16 +69,18 @@ func TestBondEnricher(t *testing.T) {
// Claim 2 CounteredBy is unset // Claim 2 CounteredBy is unset
} }
enricher := NewBondEnricher() enricher := NewBondEnricher()
credits := []*big.Int{big.NewInt(10), big.NewInt(20), big.NewInt(30)} expectedCredits := map[common.Address]*big.Int{
caller := &mockGameCaller{credits: credits} expectedRecipients[0]: big.NewInt(10),
expectedRecipients[1]: big.NewInt(20),
expectedRecipients[2]: big.NewInt(30),
}
caller := &mockGameCaller{credits: expectedCredits}
err := enricher.Enrich(context.Background(), rpcblock.Latest, caller, game) err := enricher.Enrich(context.Background(), rpcblock.Latest, caller, game)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, expectedRecipients, caller.requestedCredits) require.Equal(t, len(expectedRecipients), len(caller.requestedCredits))
expectedCredits := map[common.Address]*big.Int{ for _, recipient := range expectedRecipients {
expectedRecipients[0]: credits[0], require.Contains(t, caller.requestedCredits, recipient)
expectedRecipients[1]: credits[1],
expectedRecipients[2]: credits[2],
} }
require.Equal(t, expectedCredits, game.Credits) require.Equal(t, expectedCredits, game.Credits)
}) })
......
...@@ -184,7 +184,8 @@ type mockGameCaller struct { ...@@ -184,7 +184,8 @@ type mockGameCaller struct {
claims []faultTypes.Claim claims []faultTypes.Claim
requestedCredits []common.Address requestedCredits []common.Address
creditsErr error creditsErr error
credits []*big.Int credits map[common.Address]*big.Int
extraCredit []*big.Int
balanceErr error balanceErr error
balance *big.Int balance *big.Int
balanceAddr common.Address balanceAddr common.Address
...@@ -211,7 +212,16 @@ func (m *mockGameCaller) GetCredits(_ context.Context, _ rpcblock.Block, recipie ...@@ -211,7 +212,16 @@ func (m *mockGameCaller) GetCredits(_ context.Context, _ rpcblock.Block, recipie
if m.creditsErr != nil { if m.creditsErr != nil {
return nil, m.creditsErr return nil, m.creditsErr
} }
return m.credits, nil response := make([]*big.Int, 0, len(recipients))
for _, recipient := range recipients {
credit, ok := m.credits[recipient]
if !ok {
credit = big.NewInt(0)
}
response = append(response, credit)
}
response = append(response, m.extraCredit...)
return response, nil
} }
func (m *mockGameCaller) GetBalance(_ context.Context, _ rpcblock.Block) (*big.Int, common.Address, error) { func (m *mockGameCaller) GetBalance(_ context.Context, _ rpcblock.Block) (*big.Int, common.Address, error) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment