Commit 5ba697a2 authored by Adrian Sutton's avatar Adrian Sutton

op-challenger: Remove unused players from memory.

parent 7a21469e
...@@ -66,11 +66,13 @@ func (m *gameMonitor) progressGames(ctx context.Context) error { ...@@ -66,11 +66,13 @@ func (m *gameMonitor) progressGames(ctx context.Context) error {
if err != nil { if err != nil {
return fmt.Errorf("failed to load games: %w", err) return fmt.Errorf("failed to load games: %w", err)
} }
requiredGames := make(map[common.Address]bool)
for _, game := range games { for _, game := range games {
if !m.allowedGame(game.Proxy) { if !m.allowedGame(game.Proxy) {
m.logger.Debug("Skipping game not on allow list", "game", game.Proxy) m.logger.Debug("Skipping game not on allow list", "game", game.Proxy)
continue continue
} }
requiredGames[game.Proxy] = true
player, err := m.fetchOrCreateGamePlayer(game) player, err := m.fetchOrCreateGamePlayer(game)
if err != nil { if 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)
...@@ -78,6 +80,14 @@ func (m *gameMonitor) progressGames(ctx context.Context) error { ...@@ -78,6 +80,14 @@ func (m *gameMonitor) progressGames(ctx context.Context) error {
} }
player.ProgressGame(ctx) player.ProgressGame(ctx)
} }
// Remove the player for any game that's no longer being returned from the list of active games
for addr := range m.players {
if _, ok := requiredGames[addr]; ok {
// Game still required
continue
}
delete(m.players, addr)
}
return nil return nil
} }
......
...@@ -77,6 +77,48 @@ func TestMonitorOnlyCreateSpecifiedGame(t *testing.T) { ...@@ -77,6 +77,48 @@ func TestMonitorOnlyCreateSpecifiedGame(t *testing.T) {
require.Equal(t, 1, games.created[addr2].progressCount) require.Equal(t, 1, games.created[addr2].progressCount)
} }
func TestDeletePlayersWhenNoLongerInListOfGames(t *testing.T) {
addr1 := common.Address{0xaa}
addr2 := common.Address{0xbb}
monitor, source, games := setupMonitorTest(t, nil)
allGames := []FaultDisputeGame{
{
Proxy: addr1,
Timestamp: 9999,
},
{
Proxy: addr2,
Timestamp: 9999,
},
}
source.games = allGames
require.NoError(t, monitor.progressGames(context.Background()))
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.Len(t, games.created, 2)
require.Contains(t, games.created, addr1)
require.Contains(t, games.created, addr2)
// Forget that we created the first game so it can be recreated if needed
delete(games.created, addr1)
// First game now reappears (inexplicably but usefully for our testing)
source.games = allGames
require.NoError(t, monitor.progressGames(context.Background()))
// A new player is created for it because the original was deleted
require.Len(t, games.created, 2)
require.Contains(t, games.created, addr1)
require.Contains(t, games.created, addr2)
require.Equal(t, 1, games.created[addr1].progressCount)
}
func setupMonitorTest(t *testing.T, allowedGames []common.Address) (*gameMonitor, *stubGameSource, *createdGames) { func setupMonitorTest(t *testing.T, allowedGames []common.Address) (*gameMonitor, *stubGameSource, *createdGames) {
logger := testlog.Logger(t, log.LvlDebug) logger := testlog.Logger(t, log.LvlDebug)
source := &stubGameSource{} source := &stubGameSource{}
......
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