Skip to content

Commit 473a987

Browse files
authored
feat: add graph for on_block, attestations and attester_slashings at a high level (#1241)
1 parent 7f7b945 commit 473a987

File tree

2 files changed

+434
-368
lines changed

2 files changed

+434
-368
lines changed

lib/lambda_ethereum_consensus/fork_choice/fork_choice.ex

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -198,46 +198,57 @@ defmodule LambdaEthereumConsensus.ForkChoice do
198198
end
199199
end
200200

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}
208206
end)
209207
end
210208

211209
@spec process_block(BlockInfo.t(), Store.t()) :: Store.t()
212210
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
221217
{:ok, new_store}
222218
end
223219
end
224220

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+
225231
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
238240

241+
defp prefetch_states(attestations) do
239242
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()
241252
end
242253

243254
@spec recompute_head(Store.t()) :: :ok

0 commit comments

Comments
 (0)