Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add graph for on_block, attestations and attester_slashings at a high level #1241

Merged
merged 2 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 39 additions & 28 deletions lib/lambda_ethereum_consensus/fork_choice/fork_choice.ex
Original file line number Diff line number Diff line change
Expand Up @@ -198,46 +198,57 @@ defmodule LambdaEthereumConsensus.ForkChoice do
end
end

def apply_handler(iter, name, state, handler) do
Metrics.span_operation(name, nil, nil, fn ->
iter
|> Enum.reduce_while({:ok, state}, fn
x, {:ok, st} -> {:cont, handler.(st, x)}
_, {:error, _} = err -> {:halt, err}
end)
def apply_handler(iter, state, handler) do
iter
|> Enum.reduce_while({:ok, state}, fn
x, {:ok, st} -> {:cont, handler.(st, x)}
_, {:error, _} = err -> {:halt, err}
end)
end

@spec process_block(BlockInfo.t(), Store.t()) :: Store.t()
def process_block(%BlockInfo{signed_block: signed_block} = block_info, store) do
with {:ok, new_store} <- Handlers.on_block(store, block_info),
# process block attestations
{:ok, new_store} <-
process_attestations(new_store, signed_block.message.body.attestations),
# process block attester slashings
{:ok, new_store} <-
signed_block.message.body.attester_slashings
|> apply_handler(:attester_slashings, new_store, &Handlers.on_attester_slashing/2) do
attestations = signed_block.message.body.attestations
attester_slashings = signed_block.message.body.attester_slashings

with {:ok, new_store} <- apply_on_block(store, block_info),
{:ok, new_store} <- process_attestations(new_store, attestations),
{:ok, new_store} <- process_attester_slashings(new_store, attester_slashings) do
{:ok, new_store}
end
end

defp apply_on_block(store, block_info) do
Metrics.span_operation(:on_block, nil, nil, fn -> Handlers.on_block(store, block_info) end)
end

defp process_attester_slashings(store, attester_slashings) do
Metrics.span_operation(:attester_slashings, nil, nil, fn ->
apply_handler(attester_slashings, store, &Handlers.on_attester_slashing/2)
end)
end

defp process_attestations(store, attestations) do
# prefetch states:
states =
attestations
|> Enum.map(& &1.data.target)
|> Enum.uniq()
|> Enum.flat_map(fn ch ->
case CheckpointStates.get_checkpoint_state(ch) do
{:ok, state} -> [{ch, state}]
_other -> []
end
end)
|> Map.new()
Metrics.span_operation(:attestations, nil, nil, fn ->
apply_handler(
attestations,
store,
&Handlers.on_attestation(&1, &2, true, prefetch_states(attestations))
)
end)
end

defp prefetch_states(attestations) do
attestations
|> apply_handler(:attestations, store, &Handlers.on_attestation(&1, &2, true, states))
|> Enum.map(& &1.data.target)
|> Enum.uniq()
|> Enum.flat_map(fn ch ->
case CheckpointStates.get_checkpoint_state(ch) do
{:ok, state} -> [{ch, state}]
_other -> []
end
end)
|> Map.new()
end

@spec recompute_head(Store.t()) :: :ok
Expand Down
Loading
Loading