Commit 4349d08f authored by OptimismBot's avatar OptimismBot Committed by GitHub

Merge pull request #6965 from ethereum-optimism/aj/delete-trace-files

op-challenger: Delete game files when complete
parents ffcff575 e18a86cd
...@@ -13,6 +13,7 @@ import ( ...@@ -13,6 +13,7 @@ import (
type gamePlayer interface { type gamePlayer interface {
ProgressGame(ctx context.Context) bool ProgressGame(ctx context.Context) bool
Cleanup() error
} }
type playerCreator func(address common.Address) (gamePlayer, error) type playerCreator func(address common.Address) (gamePlayer, error)
...@@ -92,7 +93,15 @@ func (m *gameMonitor) progressGames(ctx context.Context) error { ...@@ -92,7 +93,15 @@ func (m *gameMonitor) progressGames(ctx context.Context) error {
m.logger.Error("Error while progressing game", "game", game.Proxy, "err", err) m.logger.Error("Error while progressing game", "game", game.Proxy, "err", err)
continue continue
} }
player.ProgressGame(ctx) done := player.ProgressGame(ctx)
if done {
// Remove resources on disk as soon as the game is complete to save disk space.
// We keep the player in memory to avoid recreating it on every update but will no longer
// need the resources on disk because there are no further actions required on the game.
if err := player.Cleanup(); err != nil {
m.logger.Error("Unable to cleanup player data", "err", err)
}
}
} }
// Remove the player for any game that's no longer being returned from the list of active games // Remove the player for any game that's no longer being returned from the list of active games
for addr := range m.players { for addr := range m.players {
......
...@@ -146,6 +146,27 @@ func TestDeletePlayersWhenNoLongerInListOfGames(t *testing.T) { ...@@ -146,6 +146,27 @@ func TestDeletePlayersWhenNoLongerInListOfGames(t *testing.T) {
require.Equal(t, 1, games.created[addr1].progressCount) require.Equal(t, 1, games.created[addr1].progressCount)
} }
func TestCleanupResourcesOfCompletedGames(t *testing.T) {
monitor, source, games := setupMonitorTest(t, []common.Address{})
games.createCompleted = true
addr1 := common.Address{0xaa}
source.games = []FaultDisputeGame{
{
Proxy: addr1,
Timestamp: 9999,
},
}
err := monitor.progressGames(context.Background())
require.NoError(t, err)
require.Len(t, games.created, 1, "should create game agents")
require.Contains(t, games.created, addr1)
require.Equal(t, 1, games.created[addr1].progressCount)
require.Equal(t, 1, games.created[addr1].cleanupCount, "should clean up once game is done")
}
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{}
...@@ -171,6 +192,7 @@ func (s *stubGameSource) FetchAllGamesAtBlock(ctx context.Context, earliest uint ...@@ -171,6 +192,7 @@ func (s *stubGameSource) FetchAllGamesAtBlock(ctx context.Context, earliest uint
type stubGame struct { type stubGame struct {
addr common.Address addr common.Address
progressCount int progressCount int
cleanupCount int
done bool done bool
} }
...@@ -179,16 +201,22 @@ func (g *stubGame) ProgressGame(ctx context.Context) bool { ...@@ -179,16 +201,22 @@ func (g *stubGame) ProgressGame(ctx context.Context) bool {
return g.done return g.done
} }
func (g *stubGame) Cleanup() error {
g.cleanupCount++
return nil
}
type createdGames struct { type createdGames struct {
t *testing.T t *testing.T
created map[common.Address]*stubGame createCompleted bool
created map[common.Address]*stubGame
} }
func (c *createdGames) CreateGame(addr common.Address) (gamePlayer, error) { func (c *createdGames) CreateGame(addr common.Address) (gamePlayer, error) {
if _, exists := c.created[addr]; exists { if _, exists := c.created[addr]; exists {
c.t.Fatalf("game %v already exists", addr) c.t.Fatalf("game %v already exists", addr)
} }
game := &stubGame{addr: addr} game := &stubGame{addr: addr, done: c.createCompleted}
c.created[addr] = game c.created[addr] = game
return game, nil return game, nil
} }
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