From 060748cc7285b0e9b3e7fc8845da70f5bf16c38a Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Thu, 15 Aug 2024 15:36:11 -0300 Subject: [PATCH 1/3] 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 From 3fc42913fa7c628eece868341ef4abc02b5c49e8 Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Thu, 15 Aug 2024 16:54:50 -0300 Subject: [PATCH 2/3] feat: download only missing blocks --- lib/lambda_ethereum_consensus/beacon/sync_blocks.ex | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/lambda_ethereum_consensus/beacon/sync_blocks.ex b/lib/lambda_ethereum_consensus/beacon/sync_blocks.ex index c4f22af1e..3657c608f 100644 --- a/lib/lambda_ethereum_consensus/beacon/sync_blocks.ex +++ b/lib/lambda_ethereum_consensus/beacon/sync_blocks.ex @@ -8,7 +8,6 @@ defmodule LambdaEthereumConsensus.Beacon.SyncBlocks do alias LambdaEthereumConsensus.ForkChoice alias LambdaEthereumConsensus.Libp2pPort alias LambdaEthereumConsensus.P2P.BlockDownloader - alias LambdaEthereumConsensus.StateTransition.Misc @blocks_per_chunk 16 @retries 50 @@ -23,9 +22,8 @@ defmodule LambdaEthereumConsensus.Beacon.SyncBlocks do """ @spec run() :: non_neg_integer() def run() do - # Initial sleep for faster app start - checkpoint = ForkChoice.get_finalized_checkpoint() - initial_slot = Misc.compute_start_slot_at_epoch(checkpoint.epoch) + 1 + %{head_slot: head_slot} = ForkChoice.get_current_status_message() + initial_slot = head_slot + 1 last_slot = ForkChoice.get_current_chain_slot() # If we're around genesis, we consider ourselves synced From 8bae6b772baaa29f0e272b817b823404bf01e985 Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Fri, 16 Aug 2024 11:35:52 -0300 Subject: [PATCH 3/3] refactor: restore sync delay --- lib/libp2p_port.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/libp2p_port.ex b/lib/libp2p_port.ex index a4ab2b73a..48fb52651 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 20_000 + @sync_delay_millis 10_000 ###################### ### API