From 060748cc7285b0e9b3e7fc8845da70f5bf16c38a Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Thu, 15 Aug 2024 15:36:11 -0300 Subject: [PATCH] fix: update head_root correctly --- .../beacon/store_setup.ex | 2 +- .../fork_choice/fork_choice.ex | 41 +++++-------------- lib/libp2p_port.ex | 2 +- 3 files changed, 13 insertions(+), 32 deletions(-) diff --git a/lib/lambda_ethereum_consensus/beacon/store_setup.ex b/lib/lambda_ethereum_consensus/beacon/store_setup.ex index f9c1f17d0..154392966 100644 --- a/lib/lambda_ethereum_consensus/beacon/store_setup.ex +++ b/lib/lambda_ethereum_consensus/beacon/store_setup.ex @@ -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!()) diff --git a/lib/lambda_ethereum_consensus/fork_choice/fork_choice.ex b/lib/lambda_ethereum_consensus/fork_choice/fork_choice.ex index 7f2b19123..b2e30a0a5 100644 --- a/lib/lambda_ethereum_consensus/fork_choice/fork_choice.ex +++ b/lib/lambda_ethereum_consensus/fork_choice/fork_choice.ex @@ -20,7 +20,6 @@ defmodule LambdaEthereumConsensus.ForkChoice do alias LambdaEthereumConsensus.Store.StoreDb alias Types.Attestation alias Types.BlockInfo - alias Types.Checkpoint alias Types.Store ########################## @@ -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 @@ -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, @@ -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) @@ -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 @@ -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 diff --git a/lib/libp2p_port.ex b/lib/libp2p_port.ex index afc7d12dd..c770649d3 100644 --- a/lib/libp2p_port.ex +++ b/lib/libp2p_port.ex @@ -84,7 +84,7 @@ defmodule LambdaEthereumConsensus.Libp2pPort do discovery_addresses: [String.t()] } - @sync_delay_millis 10_000 + @sync_delay_millis 20_000 ###################### ### API