Commit 98ab92e8 authored by Andreas Bigger's avatar Andreas Bigger

Modify the monitor component to only progress games on new L1 block.

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