Commit dc5cf529 authored by Adrian Sutton's avatar Adrian Sutton Committed by GitHub

op-challenger: Release agent resources once game is complete (#11820)

parent 21161265
...@@ -64,6 +64,10 @@ type GameContract interface { ...@@ -64,6 +64,10 @@ type GameContract interface {
GetL1Head(ctx context.Context) (common.Hash, error) GetL1Head(ctx context.Context) (common.Hash, error)
} }
var actNoop = func(ctx context.Context) error {
return nil
}
type resourceCreator func(ctx context.Context, logger log.Logger, gameDepth types.Depth, dir string) (types.TraceAccessor, error) type resourceCreator func(ctx context.Context, logger log.Logger, gameDepth types.Depth, dir string) (types.TraceAccessor, error)
func NewGamePlayer( func NewGamePlayer(
...@@ -98,9 +102,7 @@ func NewGamePlayer( ...@@ -98,9 +102,7 @@ func NewGamePlayer(
prestateValidators: validators, prestateValidators: validators,
status: status, status: status,
// Act function does nothing because the game is already complete // Act function does nothing because the game is already complete
act: func(ctx context.Context) error { act: actNoop,
return nil
},
}, nil }, nil
} }
...@@ -195,6 +197,10 @@ func (g *GamePlayer) ProgressGame(ctx context.Context) gameTypes.GameStatus { ...@@ -195,6 +197,10 @@ func (g *GamePlayer) ProgressGame(ctx context.Context) gameTypes.GameStatus {
} }
g.logGameStatus(ctx, status) g.logGameStatus(ctx, status)
g.status = status g.status = status
if status != gameTypes.GameStatusInProgress {
// Release the agent as we will no longer need to act on this game.
g.act = actNoop
}
return status return status
} }
......
...@@ -90,6 +90,11 @@ func TestDoNotActOnCompleteGame(t *testing.T) { ...@@ -90,6 +90,11 @@ func TestDoNotActOnCompleteGame(t *testing.T) {
fetched = game.ProgressGame(context.Background()) fetched = game.ProgressGame(context.Background())
require.Equal(t, 1, gameState.callCount, "does not act after game is complete") require.Equal(t, 1, gameState.callCount, "does not act after game is complete")
require.Equal(t, status, fetched) require.Equal(t, status, fetched)
// Should have replaced the act function with a noop so callCount doesn't update even when called directly
// This allows the agent resources to be GC'd
require.NoError(t, game.act(context.Background()))
require.Equal(t, 1, gameState.callCount)
}) })
} }
} }
......
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