Commit 3f7933d2 authored by Adrian Sutton's avatar Adrian Sutton

op-challenger: Only play game specified with --game-address when specified

parent cbedb215
...@@ -27,15 +27,17 @@ type gameMonitor struct { ...@@ -27,15 +27,17 @@ type gameMonitor struct {
source gameSource source gameSource
createGame gameCreator createGame gameCreator
fetchBlockNumber blockNumberFetcher fetchBlockNumber blockNumberFetcher
allowedGame common.Address
games map[common.Address]gameAgent games map[common.Address]gameAgent
} }
func newGameMonitor(logger log.Logger, fetchBlockNumber blockNumberFetcher, source gameSource, createGame gameCreator) *gameMonitor { func newGameMonitor(logger log.Logger, fetchBlockNumber blockNumberFetcher, allowedGame common.Address, source gameSource, createGame gameCreator) *gameMonitor {
return &gameMonitor{ return &gameMonitor{
logger: logger, logger: logger,
source: source, source: source,
createGame: createGame, createGame: createGame,
fetchBlockNumber: fetchBlockNumber, fetchBlockNumber: fetchBlockNumber,
allowedGame: allowedGame,
games: make(map[common.Address]gameAgent), games: make(map[common.Address]gameAgent),
} }
} }
...@@ -50,6 +52,10 @@ func (m *gameMonitor) progressGames(ctx context.Context) error { ...@@ -50,6 +52,10 @@ func (m *gameMonitor) progressGames(ctx context.Context) error {
return fmt.Errorf("failed to load games: %w", err) return fmt.Errorf("failed to load games: %w", err)
} }
for _, game := range games { for _, game := range games {
if m.allowedGame != (common.Address{}) && m.allowedGame != game.Proxy {
m.logger.Debug("Skipping game not on allow list", "game", game.Proxy)
continue
}
if err := m.progressGame(ctx, game); err != nil { if err := m.progressGame(ctx, game); err != nil {
m.logger.Error("Error while progressing game", "game", game.Proxy, "err", err) m.logger.Error("Error while progressing game", "game", game.Proxy, "err", err)
} }
......
...@@ -13,7 +13,7 @@ import ( ...@@ -13,7 +13,7 @@ import (
) )
func TestMonitorExitsWhenContextDone(t *testing.T) { func TestMonitorExitsWhenContextDone(t *testing.T) {
monitor, _, _ := setupMonitorTest(t) monitor, _, _ := setupMonitorTest(t, common.Address{})
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
cancel() cancel()
err := monitor.MonitorGames(ctx) err := monitor.MonitorGames(ctx)
...@@ -21,7 +21,7 @@ func TestMonitorExitsWhenContextDone(t *testing.T) { ...@@ -21,7 +21,7 @@ func TestMonitorExitsWhenContextDone(t *testing.T) {
} }
func TestMonitorCreateAndProgressGameAgents(t *testing.T) { func TestMonitorCreateAndProgressGameAgents(t *testing.T) {
monitor, source, games := setupMonitorTest(t) monitor, source, games := setupMonitorTest(t, common.Address{})
addr1 := common.Address{0xaa} addr1 := common.Address{0xaa}
addr2 := common.Address{0xbb} addr2 := common.Address{0xbb}
...@@ -51,7 +51,32 @@ func TestMonitorCreateAndProgressGameAgents(t *testing.T) { ...@@ -51,7 +51,32 @@ func TestMonitorCreateAndProgressGameAgents(t *testing.T) {
require.Equal(t, 2, games.created[addr2].progressCount) require.Equal(t, 2, games.created[addr2].progressCount)
} }
func setupMonitorTest(t *testing.T) (*gameMonitor, *stubGameSource, *createdGames) { func TestMonitorOnlyCreateSpecifiedGame(t *testing.T) {
addr1 := common.Address{0xaa}
addr2 := common.Address{0xbb}
monitor, source, games := setupMonitorTest(t, addr2)
source.games = []FaultDisputeGame{
{
Proxy: addr1,
Timestamp: big.NewInt(9999),
},
{
Proxy: addr2,
Timestamp: big.NewInt(9999),
},
}
err := monitor.progressGames(context.Background())
require.NoError(t, err)
require.Len(t, games.created, 1, "should only create allowed game")
require.Contains(t, games.created, addr2)
require.NotContains(t, games.created, addr1)
require.Equal(t, 1, games.created[addr2].progressCount)
}
func setupMonitorTest(t *testing.T, allowedGame common.Address) (*gameMonitor, *stubGameSource, *createdGames) {
logger := testlog.Logger(t, log.LvlDebug) logger := testlog.Logger(t, log.LvlDebug)
source := &stubGameSource{} source := &stubGameSource{}
games := &createdGames{ games := &createdGames{
...@@ -61,7 +86,7 @@ func setupMonitorTest(t *testing.T) (*gameMonitor, *stubGameSource, *createdGame ...@@ -61,7 +86,7 @@ func setupMonitorTest(t *testing.T) (*gameMonitor, *stubGameSource, *createdGame
fetchBlockNum := func(ctx context.Context) (uint64, error) { fetchBlockNum := func(ctx context.Context) (uint64, error) {
return 1234, nil return 1234, nil
} }
monitor := newGameMonitor(logger, fetchBlockNum, source, games.CreateGame) monitor := newGameMonitor(logger, fetchBlockNum, allowedGame, source, games.CreateGame)
return monitor, source, games return monitor, source, games
} }
......
...@@ -46,7 +46,7 @@ func NewService(ctx context.Context, logger log.Logger, cfg *config.Config) (*se ...@@ -46,7 +46,7 @@ func NewService(ctx context.Context, logger log.Logger, cfg *config.Config) (*se
} }
loader := NewGameLoader(factory) loader := NewGameLoader(factory)
monitor := newGameMonitor(logger, client.BlockNumber, loader, func(addr common.Address) (gameAgent, error) { monitor := newGameMonitor(logger, client.BlockNumber, cfg.GameAddress, loader, func(addr common.Address) (gameAgent, error) {
return NewGame(ctx, logger, cfg, addr, txMgr, client) return NewGame(ctx, logger, cfg, addr, txMgr, client)
}) })
return &service{ return &service{
...@@ -74,5 +74,5 @@ func ValidateAbsolutePrestate(ctx context.Context, trace types.TraceProvider, lo ...@@ -74,5 +74,5 @@ func ValidateAbsolutePrestate(ctx context.Context, trace types.TraceProvider, lo
// MonitorGame monitors the fault dispute game and attempts to progress it. // MonitorGame monitors the fault dispute game and attempts to progress it.
func (s *service) MonitorGame(ctx context.Context) error { func (s *service) MonitorGame(ctx context.Context) error {
return s.monitor.MonitorGames(ctx) // TODO MonitorGame(ctx, s.logger, s.agreeWithProposedOutput, s.agent, s.caller) return s.monitor.MonitorGames(ctx)
} }
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