Commit 0e1330b2 authored by OptimismBot's avatar OptimismBot Committed by GitHub

Merge pull request #6974 from ethereum-optimism/refcell/progress-games-on-block

feat(op-challenger): Progress Games on New L1 Block
parents 94d05480 f7fd62de
......@@ -87,11 +87,7 @@ func (m *gameMonitor) minGameTimestamp() uint64 {
return 0
}
func (m *gameMonitor) progressGames(ctx context.Context) error {
blockNum, err := m.fetchBlockNumber(ctx)
if err != nil {
return fmt.Errorf("failed to load current block number: %w", err)
}
func (m *gameMonitor) progressGames(ctx context.Context, blockNum uint64) error {
games, err := m.source.FetchAllGamesAtBlock(ctx, m.minGameTimestamp(), new(big.Int).SetUint64(blockNum))
if err != nil {
return fmt.Errorf("failed to load games: %w", err)
......@@ -147,13 +143,26 @@ func (m *gameMonitor) fetchOrCreateGamePlayer(gameData FaultDisputeGame) (gamePl
func (m *gameMonitor) MonitorGames(ctx context.Context) error {
m.logger.Info("Monitoring fault dispute games")
blockNum := uint64(0)
for {
err := m.progressGames(ctx)
select {
case <-ctx.Done():
return ctx.Err()
default:
nextBlockNum, err := m.fetchBlockNumber(ctx)
if err != nil {
m.logger.Error("Failed to load current block number", "err", err)
continue
}
if nextBlockNum > blockNum {
blockNum = nextBlockNum
if err := m.progressGames(ctx, nextBlockNum); err != nil {
m.logger.Error("Failed to progress games", "err", err)
}
if err := m.clock.SleepCtx(ctx, 300*time.Millisecond); err != nil {
}
if err := m.clock.SleepCtx(ctx, time.Second); err != nil {
return err
}
}
}
}
......@@ -65,8 +65,7 @@ func TestMonitorCreateAndProgressGameAgents(t *testing.T) {
},
}
err := monitor.progressGames(context.Background())
require.NoError(t, err)
require.NoError(t, monitor.progressGames(context.Background(), uint64(1)))
require.Len(t, games.created, 2, "should create game agents")
require.Contains(t, games.created, addr1)
......@@ -75,7 +74,7 @@ func TestMonitorCreateAndProgressGameAgents(t *testing.T) {
require.Equal(t, 1, games.created[addr2].progressCount)
// The stub will fail the test if a game is created with the same address multiple times
require.NoError(t, monitor.progressGames(context.Background()), "should only create games once")
require.NoError(t, monitor.progressGames(context.Background(), uint64(2)), "should only create games once")
require.Equal(t, 2, games.created[addr1].progressCount)
require.Equal(t, 2, games.created[addr2].progressCount)
}
......@@ -96,8 +95,7 @@ func TestMonitorOnlyCreateSpecifiedGame(t *testing.T) {
},
}
err := monitor.progressGames(context.Background())
require.NoError(t, err)
require.NoError(t, monitor.progressGames(context.Background(), uint64(1)))
require.Len(t, games.created, 1, "should only create allowed game")
require.Contains(t, games.created, addr2)
......@@ -122,14 +120,14 @@ func TestDeletePlayersWhenNoLongerInListOfGames(t *testing.T) {
}
source.games = allGames
require.NoError(t, monitor.progressGames(context.Background()))
require.NoError(t, monitor.progressGames(context.Background(), uint64(1)))
require.Len(t, games.created, 2)
require.Contains(t, games.created, addr1)
require.Contains(t, games.created, addr2)
// First game is now old enough it's not returned in the list of active games
source.games = source.games[1:]
require.NoError(t, monitor.progressGames(context.Background()))
require.NoError(t, monitor.progressGames(context.Background(), uint64(2)))
require.Len(t, games.created, 2)
require.Contains(t, games.created, addr1)
require.Contains(t, games.created, addr2)
......@@ -139,7 +137,7 @@ func TestDeletePlayersWhenNoLongerInListOfGames(t *testing.T) {
// First game now reappears (inexplicably but usefully for our testing)
source.games = allGames
require.NoError(t, monitor.progressGames(context.Background()))
require.NoError(t, monitor.progressGames(context.Background(), uint64(3)))
// A new player is created for it because the original was deleted
require.Len(t, games.created, 2)
require.Contains(t, games.created, addr1)
......@@ -165,7 +163,7 @@ func TestCleanupResourcesOfCompletedGames(t *testing.T) {
},
}
err := monitor.progressGames(context.Background())
err := monitor.progressGames(context.Background(), uint64(1))
require.NoError(t, err)
require.Len(t, games.created, 2, "should create game agents")
......@@ -187,8 +185,10 @@ func setupMonitorTest(t *testing.T, allowedGames []common.Address) (*gameMonitor
t: t,
created: make(map[common.Address]*stubGame),
}
i := uint64(1)
fetchBlockNum := func(ctx context.Context) (uint64, error) {
return 1234, nil
i++
return i, nil
}
disk := &stubDiskManager{
gameDirExists: make(map[common.Address]bool),
......
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