Skip to content

Commit 87ba656

Browse files
authored
fix: trigger pruning on tick (#1086)
1 parent b737dff commit 87ba656

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

lib/lambda_ethereum_consensus/fork_choice/fork_choice.ex

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,7 @@ defmodule LambdaEthereumConsensus.ForkChoice do
8989

9090
%Store{finalized_checkpoint: new_finalized_checkpoint} = new_store
9191

92-
if last_finalized_checkpoint.epoch < new_finalized_checkpoint.epoch do
93-
new_finalized_slot =
94-
new_finalized_checkpoint.epoch * ChainSpec.get("SLOTS_PER_EPOCH")
95-
96-
Task.async(StateDb, :prune_states_older_than, [new_finalized_slot])
97-
end
92+
prune_old_states(last_finalized_checkpoint.epoch, new_finalized_checkpoint.epoch)
9893

9994
GenServer.cast(from, {:block_processed, block_root, true})
10095
{:noreply, new_store}
@@ -139,7 +134,11 @@ defmodule LambdaEthereumConsensus.ForkChoice do
139134

140135
@impl GenServer
141136
def handle_cast({:on_tick, time}, store) do
137+
%Store{finalized_checkpoint: last_finalized_checkpoint} = store
138+
142139
new_store = Handlers.on_tick(store, time)
140+
%Store{finalized_checkpoint: new_finalized_checkpoint} = new_store
141+
prune_old_states(last_finalized_checkpoint.epoch, new_finalized_checkpoint.epoch)
143142
{:noreply, new_store}
144143
end
145144

@@ -152,6 +151,15 @@ defmodule LambdaEthereumConsensus.ForkChoice do
152151
### Private Functions
153152
##########################
154153

154+
def prune_old_states(last_finalized_epoch, new_finalized_epoch) do
155+
if last_finalized_epoch < new_finalized_epoch do
156+
new_finalized_slot =
157+
new_finalized_epoch * ChainSpec.get("SLOTS_PER_EPOCH")
158+
159+
Task.async(StateDb, :prune_states_older_than, [new_finalized_slot])
160+
end
161+
end
162+
155163
@spec apply_handler(any(), any(), any()) :: any()
156164
def apply_handler(iter, state, handler) do
157165
iter

lib/lambda_ethereum_consensus/store/state_db.ex

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ defmodule LambdaEthereumConsensus.Store.StateDb do
22
@moduledoc """
33
Beacon node state storage.
44
"""
5+
require Logger
56
alias LambdaEthereumConsensus.Store.Db
67
alias LambdaEthereumConsensus.Store.Utils
78
alias Types.BeaconState
@@ -30,30 +31,35 @@ defmodule LambdaEthereumConsensus.Store.StateDb do
3031
Db.put(slothash_key_block, block_root)
3132
end
3233

34+
@spec prune_states_older_than(non_neg_integer()) :: :ok | {:error, String.t()} | :not_found
3335
def prune_states_older_than(slot) do
36+
Logger.info("[StateDb] Pruning started.", slot: slot)
3437
last_finalized_key = slot |> root_by_slot_key()
3538

3639
with {:ok, it} <- Db.iterate(),
3740
{:ok, @stateslot_prefix <> _slot, _value} <-
3841
Exleveldb.iterator_move(it, last_finalized_key),
3942
{:ok, slots_to_remove} <- get_slots_to_remove(it),
4043
:ok <- Exleveldb.iterator_close(it) do
41-
slots_to_remove |> Enum.map(&remove_by_slot/1)
44+
slots_to_remove |> Enum.each(&remove_state_by_slot/1)
45+
Logger.info("[StateDb] Pruning finished. #{length(slots_to_remove)} slots removed.")
4246
end
4347
end
4448

49+
@spec get_slots_to_remove(list(non_neg_integer()), :eleveldb.itr_ref()) ::
50+
{:ok, list(non_neg_integer())}
4551
defp get_slots_to_remove(slots_to_remove \\ [], iterator) do
4652
case Exleveldb.iterator_move(iterator, :prev) do
47-
{:ok, @stateslot_prefix <> slot, _root} ->
53+
{:ok, @stateslot_prefix <> <<slot::unsigned-size(64)>>, _root} ->
4854
[slot | slots_to_remove] |> get_slots_to_remove(iterator)
4955

5056
_ ->
5157
{:ok, slots_to_remove}
5258
end
5359
end
5460

55-
defp remove_by_slot(binary_slot) do
56-
slot = :binary.decode_unsigned(binary_slot)
61+
@spec remove_state_by_slot(non_neg_integer()) :: :ok | :not_found
62+
defp remove_state_by_slot(slot) do
5763
key_slot = root_by_slot_key(slot)
5864

5965
with {:ok, block_root} <- Db.get(key_slot),

0 commit comments

Comments
 (0)