@@ -2,6 +2,7 @@ defmodule LambdaEthereumConsensus.Store.BlockDb do
2
2
@ moduledoc """
3
3
Storage and retrieval of blocks.
4
4
"""
5
+ require Logger
5
6
alias LambdaEthereumConsensus.Store.Db
6
7
alias LambdaEthereumConsensus.Store.Utils
7
8
alias Types.SignedBeaconBlock
@@ -58,36 +59,37 @@ defmodule LambdaEthereumConsensus.Store.BlockDb do
58
59
end
59
60
end
60
61
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 ( )
71
66
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 =
77
68
Stream . resource (
78
69
fn -> init_keycursor ( initial_key ) end ,
79
- & next_slot ( & 1 , :next ) ,
70
+ & next_slot ( & 1 , :prev ) ,
80
71
& close_cursor / 1
81
72
)
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
84
88
end
85
89
86
90
defp init_keycursor ( initial_key ) do
87
91
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
91
93
it
92
94
else
93
95
# DB is empty
@@ -110,12 +112,6 @@ defmodule LambdaEthereumConsensus.Store.BlockDb do
110
112
defp close_cursor ( nil ) , do: :ok
111
113
defp close_cursor ( it ) , do: :ok = Exleveldb . iterator_close ( it )
112
114
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
-
119
115
defp block_key ( root ) , do: Utils . get_key ( @ block_prefix , root )
120
116
defp block_root_by_slot_key ( slot ) , do: Utils . get_key ( @ blockslot_prefix , slot )
121
117
0 commit comments