Skip to content

Commit

Permalink
fix: update head_root correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
avilagaston9 committed Aug 15, 2024
1 parent 52f5e0f commit 060748c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 32 deletions.
2 changes: 1 addition & 1 deletion lib/lambda_ethereum_consensus/beacon/store_setup.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ defmodule LambdaEthereumConsensus.Beacon.StoreSetup do
end

@doc """
Gets a {store, genesis_validators_root} tuple with the configured strategy.
Gets a store with the configured strategy.
"""
@spec setup!() :: Store.t()
def setup!(), do: setup!(get_strategy!())
Expand Down
41 changes: 11 additions & 30 deletions lib/lambda_ethereum_consensus/fork_choice/fork_choice.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ defmodule LambdaEthereumConsensus.ForkChoice do
alias LambdaEthereumConsensus.Store.StoreDb
alias Types.Attestation
alias Types.BlockInfo
alias Types.Checkpoint
alias Types.Store

##########################
Expand Down Expand Up @@ -61,9 +60,10 @@ defmodule LambdaEthereumConsensus.ForkChoice do
:telemetry.execute([:sync, :on_block], %{slot: slot})
Logger.info("[Fork choice] Added new block", slot: slot, root: block_root)

:telemetry.span([:fork_choice, :recompute_head], %{}, fn ->
{recompute_head(new_store), %{}}
end)
new_store =
:telemetry.span([:fork_choice, :recompute_head], %{}, fn ->
{recompute_head(new_store), %{}}
end)

%Store{finalized_checkpoint: new_finalized_checkpoint} = new_store

Expand Down Expand Up @@ -178,7 +178,7 @@ defmodule LambdaEthereumConsensus.ForkChoice do
defp prune_old_states(last_finalized_epoch, new_finalized_epoch) do
if last_finalized_epoch < new_finalized_epoch do
new_finalized_slot =
new_finalized_epoch * ChainSpec.get("SLOTS_PER_EPOCH")
Misc.compute_start_slot_at_epoch(new_finalized_epoch)

Task.Supervisor.start_child(
PruneStatesSupervisor,
Expand Down Expand Up @@ -259,7 +259,7 @@ defmodule LambdaEthereumConsensus.ForkChoice do
end)
end

@spec recompute_head(Store.t()) :: :ok
@spec recompute_head(Store.t()) :: Store.t()
def recompute_head(store) do
{:ok, head_root} = Head.get_head(store)
head_block = Blocks.get_block!(head_root)
Expand All @@ -272,16 +272,13 @@ defmodule LambdaEthereumConsensus.ForkChoice do
Libp2pPort.notify_new_head(slot, head_root)
ExecutionChain.notify_new_block(slot, body.eth1_data, body.execution_payload)

update_fork_choice_data(
head_root,
slot,
store.justified_checkpoint,
store.finalized_checkpoint
)

Logger.debug("[Fork choice] Updated fork choice cache", slot: slot)

:ok
%{
store
| head_root: head_root,
head_slot: slot
}
end

defp persist_store(store) do
Expand All @@ -302,20 +299,4 @@ defmodule LambdaEthereumConsensus.ForkChoice do
|> ChainSpec.get_fork_version_for_epoch()
|> Misc.compute_fork_digest(genesis_validators_root)
end

@spec update_fork_choice_data(Types.root(), Types.slot(), Checkpoint.t(), Checkpoint.t()) ::
:ok
defp update_fork_choice_data(head_root, head_slot, justified, finalized) do
store = fetch_store!()

new_store = %{
store
| head_root: head_root,
head_slot: head_slot,
justified_checkpoint: justified,
finalized_checkpoint: finalized
}

persist_store(new_store)
end
end
2 changes: 1 addition & 1 deletion lib/libp2p_port.ex
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
discovery_addresses: [String.t()]
}

@sync_delay_millis 10_000
@sync_delay_millis 20_000

######################
### API
Expand Down

0 comments on commit 060748c

Please sign in to comment.