Skip to content

Commit

Permalink
op-challenger: Release agent resources once game is complete (#11820)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajsutton authored Sep 10, 2024
1 parent 2116126 commit dc5cf52
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
12 changes: 9 additions & 3 deletions op-challenger/game/fault/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ type GameContract interface {
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)

func NewGamePlayer(
Expand Down Expand Up @@ -98,9 +102,7 @@ func NewGamePlayer(
prestateValidators: validators,
status: status,
// Act function does nothing because the game is already complete
act: func(ctx context.Context) error {
return nil
},
act: actNoop,
}, nil
}

Expand Down Expand Up @@ -195,6 +197,10 @@ func (g *GamePlayer) ProgressGame(ctx context.Context) gameTypes.GameStatus {
}
g.logGameStatus(ctx, 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
}

Expand Down
5 changes: 5 additions & 0 deletions op-challenger/game/fault/player_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ func TestDoNotActOnCompleteGame(t *testing.T) {
fetched = game.ProgressGame(context.Background())
require.Equal(t, 1, gameState.callCount, "does not act after game is complete")
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)
})
}
}
Expand Down

0 comments on commit dc5cf52

Please sign in to comment.