Skip to content

Commit ce09e95

Browse files
authored
feat: add blocks pruning (#1092)
1 parent 7a12460 commit ce09e95

File tree

3 files changed

+26
-28
lines changed

3 files changed

+26
-28
lines changed

lib/lambda_ethereum_consensus/fork_choice/fork_choice.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ defmodule LambdaEthereumConsensus.ForkChoice do
1111
alias LambdaEthereumConsensus.ForkChoice.Handlers
1212
alias LambdaEthereumConsensus.ForkChoice.Head
1313
alias LambdaEthereumConsensus.P2P.Gossip.OperationsCollector
14+
alias LambdaEthereumConsensus.Store.BlockDb
1415
alias LambdaEthereumConsensus.Store.Blocks
1516
alias LambdaEthereumConsensus.Store.StateDb
1617
alias LambdaEthereumConsensus.Store.StoreDb
@@ -157,6 +158,7 @@ defmodule LambdaEthereumConsensus.ForkChoice do
157158
new_finalized_epoch * ChainSpec.get("SLOTS_PER_EPOCH")
158159

159160
Task.async(StateDb, :prune_states_older_than, [new_finalized_slot])
161+
Task.async(BlockDb, :prune_blocks_older_than, [new_finalized_slot])
160162
end
161163
end
162164

lib/lambda_ethereum_consensus/store/block_db.ex

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ defmodule LambdaEthereumConsensus.Store.BlockDb do
22
@moduledoc """
33
Storage and retrieval of blocks.
44
"""
5+
require Logger
56
alias LambdaEthereumConsensus.Store.Db
67
alias LambdaEthereumConsensus.Store.Utils
78
alias Types.SignedBeaconBlock
@@ -58,36 +59,37 @@ defmodule LambdaEthereumConsensus.Store.BlockDb do
5859
end
5960
end
6061

61-
def stream_missing_blocks_desc() do
62-
Stream.resource(
63-
fn -> 0xFFFFFFFFFFFFFFFF |> block_root_by_slot_key() |> init_keycursor() end,
64-
&next_slot(&1, :prev),
65-
&close_cursor/1
66-
)
67-
# TODO: we should remove this when we have a genesis block
68-
|> Stream.concat([-1])
69-
|> Stream.transform(nil, &get_missing_desc/2)
70-
end
62+
@spec prune_blocks_older_than(non_neg_integer()) :: :ok | {:error, String.t()} | :not_found
63+
def prune_blocks_older_than(slot) do
64+
Logger.info("[BlockDb] Pruning started.", slot: slot)
65+
initial_key = slot |> block_root_by_slot_key()
7166

72-
def stream_missing_blocks_asc(starting_slot) do
73-
initial_key = block_root_by_slot_key(starting_slot)
74-
75-
[starting_slot - 1]
76-
|> Stream.concat(
67+
slots_to_remove =
7768
Stream.resource(
7869
fn -> init_keycursor(initial_key) end,
79-
&next_slot(&1, :next),
70+
&next_slot(&1, :prev),
8071
&close_cursor/1
8172
)
82-
)
83-
|> Stream.transform(nil, &get_missing_asc/2)
73+
|> Enum.to_list()
74+
75+
slots_to_remove |> Enum.each(&remove_block_by_slot/1)
76+
Logger.info("[BlockDb] Pruning finished. #{Enum.count(slots_to_remove)} blocks removed.")
77+
end
78+
79+
@spec remove_block_by_slot(non_neg_integer()) :: :ok | :not_found
80+
defp remove_block_by_slot(slot) do
81+
slothash_key = block_root_by_slot_key(slot)
82+
83+
with {:ok, block_root} <- Db.get(slothash_key) do
84+
key_block = block_key(block_root)
85+
Db.delete(slothash_key)
86+
Db.delete(key_block)
87+
end
8488
end
8589

8690
defp init_keycursor(initial_key) do
8791
with {:ok, it} <- Db.iterate_keys(),
88-
{:ok, key} <- Exleveldb.iterator_move(it, initial_key),
89-
{:ok, _} <-
90-
if(key == initial_key, do: Exleveldb.iterator_move(it, :prev), else: {:ok, nil}) do
92+
{:ok, _key} <- Exleveldb.iterator_move(it, initial_key) do
9193
it
9294
else
9395
# DB is empty
@@ -110,12 +112,6 @@ defmodule LambdaEthereumConsensus.Store.BlockDb do
110112
defp close_cursor(nil), do: :ok
111113
defp close_cursor(it), do: :ok = Exleveldb.iterator_close(it)
112114

113-
def get_missing_desc(slot, nil), do: {[], slot}
114-
def get_missing_desc(slot, prev), do: {(prev - 1)..(slot + 1)//-1, slot}
115-
116-
def get_missing_asc(slot, nil), do: {[], slot}
117-
def get_missing_asc(slot, prev), do: {(prev + 1)..(slot - 1)//1, slot}
118-
119115
defp block_key(root), do: Utils.get_key(@block_prefix, root)
120116
defp block_root_by_slot_key(slot), do: Utils.get_key(@blockslot_prefix, slot)
121117

lib/lambda_ethereum_consensus/store/state_db.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ defmodule LambdaEthereumConsensus.Store.StateDb do
4242
{:ok, slots_to_remove} <- get_slots_to_remove(it),
4343
:ok <- Exleveldb.iterator_close(it) do
4444
slots_to_remove |> Enum.each(&remove_state_by_slot/1)
45-
Logger.info("[StateDb] Pruning finished. #{length(slots_to_remove)} slots removed.")
45+
Logger.info("[StateDb] Pruning finished. #{length(slots_to_remove)} states removed.")
4646
end
4747
end
4848

0 commit comments

Comments
 (0)