@@ -198,46 +198,57 @@ defmodule LambdaEthereumConsensus.ForkChoice do
198
198
end
199
199
end
200
200
201
- def apply_handler ( iter , name , state , handler ) do
202
- Metrics . span_operation ( name , nil , nil , fn ->
203
- iter
204
- |> Enum . reduce_while ( { :ok , state } , fn
205
- x , { :ok , st } -> { :cont , handler . ( st , x ) }
206
- _ , { :error , _ } = err -> { :halt , err }
207
- end )
201
+ def apply_handler ( iter , state , handler ) do
202
+ iter
203
+ |> Enum . reduce_while ( { :ok , state } , fn
204
+ x , { :ok , st } -> { :cont , handler . ( st , x ) }
205
+ _ , { :error , _ } = err -> { :halt , err }
208
206
end )
209
207
end
210
208
211
209
@ spec process_block ( BlockInfo . t ( ) , Store . t ( ) ) :: Store . t ( )
212
210
def process_block ( % BlockInfo { signed_block: signed_block } = block_info , store ) do
213
- with { :ok , new_store } <- Handlers . on_block ( store , block_info ) ,
214
- # process block attestations
215
- { :ok , new_store } <-
216
- process_attestations ( new_store , signed_block . message . body . attestations ) ,
217
- # process block attester slashings
218
- { :ok , new_store } <-
219
- signed_block . message . body . attester_slashings
220
- |> apply_handler ( :attester_slashings , new_store , & Handlers . on_attester_slashing / 2 ) do
211
+ attestations = signed_block . message . body . attestations
212
+ attester_slashings = signed_block . message . body . attester_slashings
213
+
214
+ with { :ok , new_store } <- apply_on_block ( store , block_info ) ,
215
+ { :ok , new_store } <- process_attestations ( new_store , attestations ) ,
216
+ { :ok , new_store } <- process_attester_slashings ( new_store , attester_slashings ) do
221
217
{ :ok , new_store }
222
218
end
223
219
end
224
220
221
+ defp apply_on_block ( store , block_info ) do
222
+ Metrics . span_operation ( :on_block , nil , nil , fn -> Handlers . on_block ( store , block_info ) end )
223
+ end
224
+
225
+ defp process_attester_slashings ( store , attester_slashings ) do
226
+ Metrics . span_operation ( :attester_slashings , nil , nil , fn ->
227
+ apply_handler ( attester_slashings , store , & Handlers . on_attester_slashing / 2 )
228
+ end )
229
+ end
230
+
225
231
defp process_attestations ( store , attestations ) do
226
- # prefetch states:
227
- states =
228
- attestations
229
- |> Enum . map ( & & 1 . data . target )
230
- |> Enum . uniq ( )
231
- |> Enum . flat_map ( fn ch ->
232
- case CheckpointStates . get_checkpoint_state ( ch ) do
233
- { :ok , state } -> [ { ch , state } ]
234
- _other -> [ ]
235
- end
236
- end )
237
- |> Map . new ( )
232
+ Metrics . span_operation ( :attestations , nil , nil , fn ->
233
+ apply_handler (
234
+ attestations ,
235
+ store ,
236
+ & Handlers . on_attestation ( & 1 , & 2 , true , prefetch_states ( attestations ) )
237
+ )
238
+ end )
239
+ end
238
240
241
+ defp prefetch_states ( attestations ) do
239
242
attestations
240
- |> apply_handler ( :attestations , store , & Handlers . on_attestation ( & 1 , & 2 , true , states ) )
243
+ |> Enum . map ( & & 1 . data . target )
244
+ |> Enum . uniq ( )
245
+ |> Enum . flat_map ( fn ch ->
246
+ case CheckpointStates . get_checkpoint_state ( ch ) do
247
+ { :ok , state } -> [ { ch , state } ]
248
+ _other -> [ ]
249
+ end
250
+ end )
251
+ |> Map . new ( )
241
252
end
242
253
243
254
@ spec recompute_head ( Store . t ( ) ) :: :ok
0 commit comments