From 6b61268b2261c253e34c141bbdf457893add6742 Mon Sep 17 00:00:00 2001 From: karasakalmt Date: Thu, 30 Nov 2023 12:35:47 +0200 Subject: [PATCH 01/18] current_status_message --- .../fork_choice/helpers.ex | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/lambda_ethereum_consensus/fork_choice/helpers.ex b/lib/lambda_ethereum_consensus/fork_choice/helpers.ex index 9cf51a1f3..b6a0f662d 100644 --- a/lib/lambda_ethereum_consensus/fork_choice/helpers.ex +++ b/lib/lambda_ethereum_consensus/fork_choice/helpers.ex @@ -8,6 +8,23 @@ defmodule LambdaEthereumConsensus.ForkChoice.Helpers do alias SszTypes.Checkpoint alias SszTypes.Store + @spec current_status_message(BeaconState.t(), Store.t()) :: {:ok, SszTypes.StatusMessage.t()} | {:error, any} + def current_status_message(state, store) do + with {:ok, head_root} <- get_head(store), + {:ok, signed_head_block} <- LambdaEthereumConsensus.Store.BlockStore.get_block(head_root), + {:ok, finalized_checkpoint} <- LambdaEthereumConsensus.ForkChoice.Store.get_finalized_checkpoint() do + {:ok, %SszTypes.StatusMessage{ + fork_digest: Misc.compute_fork_digest(state.fork.current_version, state.genesis_validators_root), + finalized_root: finalized_checkpoint.root, + finalized_epoch: finalized_checkpoint.epoch, + head_root: head_root, + head_slot: signed_head_block.message.slot + }} + else + {:error, msg} -> {:error, msg} + end + end + @spec get_forkchoice_store(BeaconState.t(), BeaconBlock.t()) :: {:ok, Store.t()} | {:error, any} def get_forkchoice_store(anchor_state, anchor_block) do anchor_state_root = Ssz.hash_tree_root!(anchor_state) From 428245210a30680c01aa79a1a1e1cf53761354fc Mon Sep 17 00:00:00 2001 From: karasakalmt Date: Fri, 1 Dec 2023 16:02:01 +0200 Subject: [PATCH 02/18] current storage --- .../fork_choice/helpers.ex | 31 ++++++----- .../fork_choice/store.ex | 51 +++++++++++++++++++ .../p2p/incoming_requests/handler.ex | 15 ++---- 3 files changed, 73 insertions(+), 24 deletions(-) diff --git a/lib/lambda_ethereum_consensus/fork_choice/helpers.ex b/lib/lambda_ethereum_consensus/fork_choice/helpers.ex index b6a0f662d..9407d4d87 100644 --- a/lib/lambda_ethereum_consensus/fork_choice/helpers.ex +++ b/lib/lambda_ethereum_consensus/fork_choice/helpers.ex @@ -8,18 +8,25 @@ defmodule LambdaEthereumConsensus.ForkChoice.Helpers do alias SszTypes.Checkpoint alias SszTypes.Store - @spec current_status_message(BeaconState.t(), Store.t()) :: {:ok, SszTypes.StatusMessage.t()} | {:error, any} - def current_status_message(state, store) do - with {:ok, head_root} <- get_head(store), - {:ok, signed_head_block} <- LambdaEthereumConsensus.Store.BlockStore.get_block(head_root), - {:ok, finalized_checkpoint} <- LambdaEthereumConsensus.ForkChoice.Store.get_finalized_checkpoint() do - {:ok, %SszTypes.StatusMessage{ - fork_digest: Misc.compute_fork_digest(state.fork.current_version, state.genesis_validators_root), - finalized_root: finalized_checkpoint.root, - finalized_epoch: finalized_checkpoint.epoch, - head_root: head_root, - head_slot: signed_head_block.message.slot - }} + @spec current_status_message() :: + {:ok, SszTypes.StatusMessage.t()} | {:error, any} + def current_status_message(store) do + with {:ok, store} <- LambdaEthereumConsensus.ForkChoice.Store.get_store(), + {:ok, head_root} <- get_head(store), + {:ok, state} <- LambdaEthereumConsensus.Store.StateStore.get_state(head_root), + {:ok, signed_head_block} <- + LambdaEthereumConsensus.Store.BlockStore.get_block(head_root), + {:ok, finalized_checkpoint} <- + LambdaEthereumConsensus.ForkChoice.Store.get_finalized_checkpoint() do + {:ok, + %SszTypes.StatusMessage{ + fork_digest: + Misc.compute_fork_digest(state.fork.current_version, state.genesis_validators_root), + finalized_root: finalized_checkpoint.root, + finalized_epoch: finalized_checkpoint.epoch, + head_root: head_root, + head_slot: signed_head_block.message.slot + }} else {:error, msg} -> {:error, msg} end diff --git a/lib/lambda_ethereum_consensus/fork_choice/store.ex b/lib/lambda_ethereum_consensus/fork_choice/store.ex index ed4ea85e4..848325eaf 100644 --- a/lib/lambda_ethereum_consensus/fork_choice/store.ex +++ b/lib/lambda_ethereum_consensus/fork_choice/store.ex @@ -36,6 +36,57 @@ defmodule LambdaEthereumConsensus.ForkChoice.Store do div(time - genesis_time, ChainSpec.get("SECONDS_PER_SLOT")) end + @spec get_store() :: {:ok, SszTypes.Store.t()} + def get_store() do + [ + time, + genesis_time, + justified_checkpoint, + finalized_checkpoint, + unrealized_justified_checkpoint, + unrealized_finalized_checkpoint, + proposer_boost_root, + equivocating_indices, + blocks, + block_states, + checkpoint_states, + latest_messages, + unrealized_justifications + ] = + get_store_attrs([ + :time, + :genesis_time, + :justified_checkpoint, + :finalized_checkpoint, + :unrealized_justified_checkpoint, + :unrealized_finalized_checkpoint, + :proposer_boost_root, + :equivocating_indices, + :blocks, + :block_states, + :checkpoint_states, + :latest_messages, + :unrealized_justifications + ]) + + {:ok, + %SszTypes.Store{ + time: time, + genesis_time: genesis_time, + justified_checkpoint: justified_checkpoint, + finalized_checkpoint: finalized_checkpoint, + unrealized_justified_checkpoint: unrealized_justified_checkpoint, + unrealized_finalized_checkpoint: unrealized_finalized_checkpoint, + proposer_boost_root: proposer_boost_root, + equivocating_indices: equivocating_indices, + blocks: blocks, + block_states: block_states, + checkpoint_states: checkpoint_states, + latest_messages: latest_messages, + unrealized_justifications: unrealized_justifications + }} + end + @spec has_block?(SszTypes.root()) :: boolean() def has_block?(block_root) do block = get_block(block_root) diff --git a/lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex b/lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex index bb8614183..c71edf729 100644 --- a/lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex +++ b/lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex @@ -28,18 +28,9 @@ defmodule LambdaEthereumConsensus.P2P.IncomingRequests.Handler do @spec handle_req(String.t(), String.t(), binary()) :: :ok | :not_implemented | {:error, binary()} defp handle_req("status/1/ssz_snappy", message_id, message) do - # hardcoded response from random peer - current_status = %SszTypes.StatusMessage{ - fork_digest: Base.decode16!("BBA4DA96"), - finalized_root: - Base.decode16!("7715794499C07D9954DD223EC2C6B846D3BAB27956D093000FADC1B8219F74D4"), - finalized_epoch: 228_168, - head_root: - Base.decode16!("D62A74AE0F933224133C5E6E1827A2835A1E705F0CDFEE3AD25808DDEA5572DB"), - head_slot: 7_301_450 - } - - with <<84, snappy_status::binary>> <- message, + with {:ok, current_status} <- + LambdaEthereumConsensus.ForkChoice.Helpers.current_status_message(), + <<84, snappy_status::binary>> <- message, {:ok, ssz_status} <- Snappy.decompress(snappy_status), {:ok, status} <- Ssz.from_ssz(ssz_status, SszTypes.StatusMessage), status From f27c9112a88495560feed98a4c952c1fc4e204e9 Mon Sep 17 00:00:00 2001 From: karasakalmt Date: Fri, 1 Dec 2023 16:21:06 +0200 Subject: [PATCH 03/18] lint --- lib/lambda_ethereum_consensus/fork_choice/helpers.ex | 2 +- lib/lambda_ethereum_consensus/fork_choice/store.ex | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/lambda_ethereum_consensus/fork_choice/helpers.ex b/lib/lambda_ethereum_consensus/fork_choice/helpers.ex index ecae831b0..74e4224b7 100644 --- a/lib/lambda_ethereum_consensus/fork_choice/helpers.ex +++ b/lib/lambda_ethereum_consensus/fork_choice/helpers.ex @@ -10,7 +10,7 @@ defmodule LambdaEthereumConsensus.ForkChoice.Helpers do @spec current_status_message() :: {:ok, SszTypes.StatusMessage.t()} | {:error, any} - def current_status_message(store) do + def current_status_message do with {:ok, store} <- LambdaEthereumConsensus.ForkChoice.Store.get_store(), {:ok, head_root} <- get_head(store), {:ok, state} <- LambdaEthereumConsensus.Store.StateStore.get_state(head_root), diff --git a/lib/lambda_ethereum_consensus/fork_choice/store.ex b/lib/lambda_ethereum_consensus/fork_choice/store.ex index 848325eaf..2c60285b3 100644 --- a/lib/lambda_ethereum_consensus/fork_choice/store.ex +++ b/lib/lambda_ethereum_consensus/fork_choice/store.ex @@ -37,7 +37,7 @@ defmodule LambdaEthereumConsensus.ForkChoice.Store do end @spec get_store() :: {:ok, SszTypes.Store.t()} - def get_store() do + def get_store do [ time, genesis_time, From c825fd08c63c1fe42d7ba63af5864ee72ec3ef43 Mon Sep 17 00:00:00 2001 From: karasakalmt Date: Mon, 4 Dec 2023 16:33:19 +0200 Subject: [PATCH 04/18] related changes are done --- .../fork_choice/helpers.ex | 3 +- .../fork_choice/store.ex | 62 ++++--------------- 2 files changed, 14 insertions(+), 51 deletions(-) diff --git a/lib/lambda_ethereum_consensus/fork_choice/helpers.ex b/lib/lambda_ethereum_consensus/fork_choice/helpers.ex index 74e4224b7..52232cbe1 100644 --- a/lib/lambda_ethereum_consensus/fork_choice/helpers.ex +++ b/lib/lambda_ethereum_consensus/fork_choice/helpers.ex @@ -11,8 +11,7 @@ defmodule LambdaEthereumConsensus.ForkChoice.Helpers do @spec current_status_message() :: {:ok, SszTypes.StatusMessage.t()} | {:error, any} def current_status_message do - with {:ok, store} <- LambdaEthereumConsensus.ForkChoice.Store.get_store(), - {:ok, head_root} <- get_head(store), + with head_root <- LambdaEthereumConsensus.ForkChoice.Store.get_head(store), {:ok, state} <- LambdaEthereumConsensus.Store.StateStore.get_state(head_root), {:ok, signed_head_block} <- LambdaEthereumConsensus.Store.BlockStore.get_block(head_root), diff --git a/lib/lambda_ethereum_consensus/fork_choice/store.ex b/lib/lambda_ethereum_consensus/fork_choice/store.ex index 2c60285b3..8e01a1d2e 100644 --- a/lib/lambda_ethereum_consensus/fork_choice/store.ex +++ b/lib/lambda_ethereum_consensus/fork_choice/store.ex @@ -36,55 +36,9 @@ defmodule LambdaEthereumConsensus.ForkChoice.Store do div(time - genesis_time, ChainSpec.get("SECONDS_PER_SLOT")) end - @spec get_store() :: {:ok, SszTypes.Store.t()} - def get_store do - [ - time, - genesis_time, - justified_checkpoint, - finalized_checkpoint, - unrealized_justified_checkpoint, - unrealized_finalized_checkpoint, - proposer_boost_root, - equivocating_indices, - blocks, - block_states, - checkpoint_states, - latest_messages, - unrealized_justifications - ] = - get_store_attrs([ - :time, - :genesis_time, - :justified_checkpoint, - :finalized_checkpoint, - :unrealized_justified_checkpoint, - :unrealized_finalized_checkpoint, - :proposer_boost_root, - :equivocating_indices, - :blocks, - :block_states, - :checkpoint_states, - :latest_messages, - :unrealized_justifications - ]) - - {:ok, - %SszTypes.Store{ - time: time, - genesis_time: genesis_time, - justified_checkpoint: justified_checkpoint, - finalized_checkpoint: finalized_checkpoint, - unrealized_justified_checkpoint: unrealized_justified_checkpoint, - unrealized_finalized_checkpoint: unrealized_finalized_checkpoint, - proposer_boost_root: proposer_boost_root, - equivocating_indices: equivocating_indices, - blocks: blocks, - block_states: block_states, - checkpoint_states: checkpoint_states, - latest_messages: latest_messages, - unrealized_justifications: unrealized_justifications - }} + @spec get_head_root() :: SszTypes.root() + def get_head_root do + get_head() end @spec has_block?(SszTypes.root()) :: boolean() @@ -140,6 +94,11 @@ defmodule LambdaEthereumConsensus.ForkChoice.Store do {:reply, values, state} end + @impl GenServer + def handle_call({:get_head}, _from, state) do + {:reply, Helpers.get_head(state)} + end + def handle_call({:get_block, block_root}, _from, state) do {:reply, Map.get(state.blocks, block_root), state} end @@ -236,6 +195,11 @@ defmodule LambdaEthereumConsensus.ForkChoice.Store do GenServer.call(__MODULE__, {:get_store_attrs, attrs}, @default_timeout) end + @spec get_head() :: SszTypes.root() + defp get_head() do + GenServer.call(__MODULE__, {:get_head}, @default_timeout) + end + @spec on_tick_now(Store.t()) :: Store.t() defp on_tick_now(store) do new_store = Handlers.on_tick(store, :os.system_time(:second)) From 4289e7976cc11bd7cab91fe8b60c6a415b91e420 Mon Sep 17 00:00:00 2001 From: karasakalmt Date: Mon, 4 Dec 2023 16:37:36 +0200 Subject: [PATCH 05/18] lint --- lib/lambda_ethereum_consensus/fork_choice/store.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lambda_ethereum_consensus/fork_choice/store.ex b/lib/lambda_ethereum_consensus/fork_choice/store.ex index 8e01a1d2e..67412413b 100644 --- a/lib/lambda_ethereum_consensus/fork_choice/store.ex +++ b/lib/lambda_ethereum_consensus/fork_choice/store.ex @@ -196,7 +196,7 @@ defmodule LambdaEthereumConsensus.ForkChoice.Store do end @spec get_head() :: SszTypes.root() - defp get_head() do + defp get_head do GenServer.call(__MODULE__, {:get_head}, @default_timeout) end From 1a54d48cca0ab1d7cabecaef27832897a4ccc2a1 Mon Sep 17 00:00:00 2001 From: karasakalmt Date: Mon, 4 Dec 2023 16:41:19 +0200 Subject: [PATCH 06/18] build issue resolved --- lib/lambda_ethereum_consensus/fork_choice/helpers.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lambda_ethereum_consensus/fork_choice/helpers.ex b/lib/lambda_ethereum_consensus/fork_choice/helpers.ex index 52232cbe1..40931310f 100644 --- a/lib/lambda_ethereum_consensus/fork_choice/helpers.ex +++ b/lib/lambda_ethereum_consensus/fork_choice/helpers.ex @@ -11,7 +11,7 @@ defmodule LambdaEthereumConsensus.ForkChoice.Helpers do @spec current_status_message() :: {:ok, SszTypes.StatusMessage.t()} | {:error, any} def current_status_message do - with head_root <- LambdaEthereumConsensus.ForkChoice.Store.get_head(store), + with head_root <- LambdaEthereumConsensus.ForkChoice.Store.get_head(), {:ok, state} <- LambdaEthereumConsensus.Store.StateStore.get_state(head_root), {:ok, signed_head_block} <- LambdaEthereumConsensus.Store.BlockStore.get_block(head_root), From 918bed4b26bccbc6e4174c804bf590d61a7a4001 Mon Sep 17 00:00:00 2001 From: karasakalmt Date: Mon, 4 Dec 2023 16:53:01 +0200 Subject: [PATCH 07/18] build issue resolved --- lib/lambda_ethereum_consensus/fork_choice/store.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lambda_ethereum_consensus/fork_choice/store.ex b/lib/lambda_ethereum_consensus/fork_choice/store.ex index 67412413b..bf3e7bde5 100644 --- a/lib/lambda_ethereum_consensus/fork_choice/store.ex +++ b/lib/lambda_ethereum_consensus/fork_choice/store.ex @@ -196,7 +196,7 @@ defmodule LambdaEthereumConsensus.ForkChoice.Store do end @spec get_head() :: SszTypes.root() - defp get_head do + def get_head do GenServer.call(__MODULE__, {:get_head}, @default_timeout) end From 762597b0ce8e228dc3509d4f83b32017934c0715 Mon Sep 17 00:00:00 2001 From: karasakalmt Date: Wed, 6 Dec 2023 15:38:01 +0200 Subject: [PATCH 08/18] refactor commands --- .../fork_choice/helpers.ex | 6 +++--- .../fork_choice/store.ex | 16 ++++++++-------- .../p2p/incoming_requests/handler.ex | 6 +++--- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/lambda_ethereum_consensus/fork_choice/helpers.ex b/lib/lambda_ethereum_consensus/fork_choice/helpers.ex index 893dbf26e..9cc6cac77 100644 --- a/lib/lambda_ethereum_consensus/fork_choice/helpers.ex +++ b/lib/lambda_ethereum_consensus/fork_choice/helpers.ex @@ -8,10 +8,10 @@ defmodule LambdaEthereumConsensus.ForkChoice.Helpers do alias SszTypes.Checkpoint alias SszTypes.Store - @spec current_status_message() :: + @spec current_status_message(Store.t()) :: {:ok, SszTypes.StatusMessage.t()} | {:error, any} - def current_status_message do - with head_root <- LambdaEthereumConsensus.ForkChoice.Store.get_head(), + def current_status_message(store) do + with {:ok, head_root} <- get_head(store), {:ok, state} <- LambdaEthereumConsensus.Store.StateStore.get_state(head_root), {:ok, signed_head_block} <- LambdaEthereumConsensus.Store.BlockStore.get_block(head_root), diff --git a/lib/lambda_ethereum_consensus/fork_choice/store.ex b/lib/lambda_ethereum_consensus/fork_choice/store.ex index bf3e7bde5..76d8edec2 100644 --- a/lib/lambda_ethereum_consensus/fork_choice/store.ex +++ b/lib/lambda_ethereum_consensus/fork_choice/store.ex @@ -36,9 +36,9 @@ defmodule LambdaEthereumConsensus.ForkChoice.Store do div(time - genesis_time, ChainSpec.get("SECONDS_PER_SLOT")) end - @spec get_head_root() :: SszTypes.root() - def get_head_root do - get_head() + @spec get_current_status_message() :: {:ok, SszTypes.StatusMessage.t()} | {:error, any} + def get_current_status_message do + get_current_status_message_from_store() end @spec has_block?(SszTypes.root()) :: boolean() @@ -95,8 +95,8 @@ defmodule LambdaEthereumConsensus.ForkChoice.Store do end @impl GenServer - def handle_call({:get_head}, _from, state) do - {:reply, Helpers.get_head(state)} + def handle_call({:get_current_status_message}, _from, state) do + {:reply, Helpers.current_status_message(state)} end def handle_call({:get_block, block_root}, _from, state) do @@ -195,9 +195,9 @@ defmodule LambdaEthereumConsensus.ForkChoice.Store do GenServer.call(__MODULE__, {:get_store_attrs, attrs}, @default_timeout) end - @spec get_head() :: SszTypes.root() - def get_head do - GenServer.call(__MODULE__, {:get_head}, @default_timeout) + @spec get_current_status_message_from_store() :: SszTypes.root() + def get_current_status_message_from_store do + GenServer.call(__MODULE__, {:get_current_status_message}, @default_timeout) end @spec on_tick_now(Store.t()) :: Store.t() diff --git a/lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex b/lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex index c71edf729..bee80476a 100644 --- a/lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex +++ b/lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex @@ -28,9 +28,9 @@ defmodule LambdaEthereumConsensus.P2P.IncomingRequests.Handler do @spec handle_req(String.t(), String.t(), binary()) :: :ok | :not_implemented | {:error, binary()} defp handle_req("status/1/ssz_snappy", message_id, message) do - with {:ok, current_status} <- - LambdaEthereumConsensus.ForkChoice.Helpers.current_status_message(), - <<84, snappy_status::binary>> <- message, + with <<84, snappy_status::binary>> <- message, + {:ok, current_status} <- + LambdaEthereumConsensus.ForkChoice.Store.current_status_message(), {:ok, ssz_status} <- Snappy.decompress(snappy_status), {:ok, status} <- Ssz.from_ssz(ssz_status, SszTypes.StatusMessage), status From 4104d669c4ea90a5a80659cd74e207fb92a5116a Mon Sep 17 00:00:00 2001 From: karasakalmt Date: Wed, 6 Dec 2023 15:45:04 +0200 Subject: [PATCH 09/18] refactor comments --- lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex b/lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex index bee80476a..101517d09 100644 --- a/lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex +++ b/lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex @@ -30,7 +30,7 @@ defmodule LambdaEthereumConsensus.P2P.IncomingRequests.Handler do defp handle_req("status/1/ssz_snappy", message_id, message) do with <<84, snappy_status::binary>> <- message, {:ok, current_status} <- - LambdaEthereumConsensus.ForkChoice.Store.current_status_message(), + LambdaEthereumConsensus.ForkChoice.Store.get_current_status_message(), {:ok, ssz_status} <- Snappy.decompress(snappy_status), {:ok, status} <- Ssz.from_ssz(ssz_status, SszTypes.StatusMessage), status From 168eb3de83411af86e56b45efb85157adc914047 Mon Sep 17 00:00:00 2001 From: karasakalmt Date: Wed, 6 Dec 2023 15:50:11 +0200 Subject: [PATCH 10/18] refactor comments --- lib/lambda_ethereum_consensus/fork_choice/helpers.ex | 11 +++++------ lib/lambda_ethereum_consensus/fork_choice/store.ex | 4 ++-- .../p2p/incoming_requests/handler.ex | 3 ++- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/lambda_ethereum_consensus/fork_choice/helpers.ex b/lib/lambda_ethereum_consensus/fork_choice/helpers.ex index 9cc6cac77..fc4e742a1 100644 --- a/lib/lambda_ethereum_consensus/fork_choice/helpers.ex +++ b/lib/lambda_ethereum_consensus/fork_choice/helpers.ex @@ -3,6 +3,8 @@ defmodule LambdaEthereumConsensus.ForkChoice.Helpers do Utility functions for the fork choice. """ alias LambdaEthereumConsensus.StateTransition.{Accessors, Misc} + alias LambdaEthereumConsensus.Store.{StateStore, BlockStore} + alias LambdaEthereumConsensus.ForkChoice alias SszTypes.BeaconBlock alias SszTypes.BeaconState alias SszTypes.Checkpoint @@ -12,11 +14,11 @@ defmodule LambdaEthereumConsensus.ForkChoice.Helpers do {:ok, SszTypes.StatusMessage.t()} | {:error, any} def current_status_message(store) do with {:ok, head_root} <- get_head(store), - {:ok, state} <- LambdaEthereumConsensus.Store.StateStore.get_state(head_root), + {:ok, state} <- StateStore.get_state(head_root), {:ok, signed_head_block} <- - LambdaEthereumConsensus.Store.BlockStore.get_block(head_root), + BlockStore.get_block(head_root), {:ok, finalized_checkpoint} <- - LambdaEthereumConsensus.ForkChoice.Store.get_finalized_checkpoint() do + ForkChoice.Store.get_finalized_checkpoint() do {:ok, %SszTypes.StatusMessage{ fork_digest: @@ -26,9 +28,6 @@ defmodule LambdaEthereumConsensus.ForkChoice.Helpers do head_root: head_root, head_slot: signed_head_block.message.slot }} - else - {:error, msg} -> {:error, msg} - end end @spec get_forkchoice_store(BeaconState.t(), BeaconBlock.t()) :: {:ok, Store.t()} | {:error, any} diff --git a/lib/lambda_ethereum_consensus/fork_choice/store.ex b/lib/lambda_ethereum_consensus/fork_choice/store.ex index 76d8edec2..9349dab92 100644 --- a/lib/lambda_ethereum_consensus/fork_choice/store.ex +++ b/lib/lambda_ethereum_consensus/fork_choice/store.ex @@ -95,7 +95,7 @@ defmodule LambdaEthereumConsensus.ForkChoice.Store do end @impl GenServer - def handle_call({:get_current_status_message}, _from, state) do + def handle_call(:get_current_status_message, _from, state) do {:reply, Helpers.current_status_message(state)} end @@ -197,7 +197,7 @@ defmodule LambdaEthereumConsensus.ForkChoice.Store do @spec get_current_status_message_from_store() :: SszTypes.root() def get_current_status_message_from_store do - GenServer.call(__MODULE__, {:get_current_status_message}, @default_timeout) + GenServer.call(__MODULE__, :get_current_status_message, @default_timeout) end @spec on_tick_now(Store.t()) :: Store.t() diff --git a/lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex b/lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex index 101517d09..2462bda4c 100644 --- a/lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex +++ b/lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex @@ -5,6 +5,7 @@ defmodule LambdaEthereumConsensus.P2P.IncomingRequests.Handler do require Logger alias LambdaEthereumConsensus.{Libp2pPort, P2P} alias LambdaEthereumConsensus.Store.BlockStore + alias LambdaEthereumConsensus.ForkChoice.Store # This is the `ForkDigest` for mainnet in the capella fork # TODO: compute this at runtime @@ -30,7 +31,7 @@ defmodule LambdaEthereumConsensus.P2P.IncomingRequests.Handler do defp handle_req("status/1/ssz_snappy", message_id, message) do with <<84, snappy_status::binary>> <- message, {:ok, current_status} <- - LambdaEthereumConsensus.ForkChoice.Store.get_current_status_message(), + Store.get_current_status_message(), {:ok, ssz_status} <- Snappy.decompress(snappy_status), {:ok, status} <- Ssz.from_ssz(ssz_status, SszTypes.StatusMessage), status From dd8a1ecb1eb5852767fa0d558c69545ef4e32fc2 Mon Sep 17 00:00:00 2001 From: karasakalmt Date: Wed, 6 Dec 2023 15:51:15 +0200 Subject: [PATCH 11/18] fix: end --- lib/lambda_ethereum_consensus/fork_choice/helpers.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/lambda_ethereum_consensus/fork_choice/helpers.ex b/lib/lambda_ethereum_consensus/fork_choice/helpers.ex index fc4e742a1..01c2acee2 100644 --- a/lib/lambda_ethereum_consensus/fork_choice/helpers.ex +++ b/lib/lambda_ethereum_consensus/fork_choice/helpers.ex @@ -28,6 +28,7 @@ defmodule LambdaEthereumConsensus.ForkChoice.Helpers do head_root: head_root, head_slot: signed_head_block.message.slot }} + end end @spec get_forkchoice_store(BeaconState.t(), BeaconBlock.t()) :: {:ok, Store.t()} | {:error, any} From efa5a7db1856f4fd5aa76349ff49e5828d68fa0d Mon Sep 17 00:00:00 2001 From: karasakalmt Date: Wed, 6 Dec 2023 16:01:29 +0200 Subject: [PATCH 12/18] fix: build issue --- .../p2p/incoming_requests/handler.ex | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex b/lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex index 2462bda4c..0c875de78 100644 --- a/lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex +++ b/lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex @@ -2,10 +2,11 @@ defmodule LambdaEthereumConsensus.P2P.IncomingRequests.Handler do @moduledoc """ This module handles Req/Resp domain requests. """ - require Logger - alias LambdaEthereumConsensus.{Libp2pPort, P2P} - alias LambdaEthereumConsensus.Store.BlockStore + alias LambdaEthereumConsensus.ForkChoice.Store + alias LambdaEthereumConsensus.Store.BlockStore + alias LambdaEthereumConsensus.{Libp2pPort, P2P} + require Logger # This is the `ForkDigest` for mainnet in the capella fork # TODO: compute this at runtime @@ -30,8 +31,7 @@ defmodule LambdaEthereumConsensus.P2P.IncomingRequests.Handler do :ok | :not_implemented | {:error, binary()} defp handle_req("status/1/ssz_snappy", message_id, message) do with <<84, snappy_status::binary>> <- message, - {:ok, current_status} <- - Store.get_current_status_message(), + {:ok, current_status} <- Store.get_current_status_message(), {:ok, ssz_status} <- Snappy.decompress(snappy_status), {:ok, status} <- Ssz.from_ssz(ssz_status, SszTypes.StatusMessage), status From 1f16690e7dc38814ae7e5662e053c17804cb7a51 Mon Sep 17 00:00:00 2001 From: karasakalmt Date: Wed, 6 Dec 2023 16:08:42 +0200 Subject: [PATCH 13/18] fix: build issue --- .../p2p/incoming_requests/handler.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex b/lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex index 0c875de78..493952182 100644 --- a/lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex +++ b/lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex @@ -3,7 +3,7 @@ defmodule LambdaEthereumConsensus.P2P.IncomingRequests.Handler do This module handles Req/Resp domain requests. """ - alias LambdaEthereumConsensus.ForkChoice.Store + alias LambdaEthereumConsensus.ForkChoice alias LambdaEthereumConsensus.Store.BlockStore alias LambdaEthereumConsensus.{Libp2pPort, P2P} require Logger @@ -31,7 +31,7 @@ defmodule LambdaEthereumConsensus.P2P.IncomingRequests.Handler do :ok | :not_implemented | {:error, binary()} defp handle_req("status/1/ssz_snappy", message_id, message) do with <<84, snappy_status::binary>> <- message, - {:ok, current_status} <- Store.get_current_status_message(), + {:ok, current_status} <- ForkChoice.Store.get_current_status_message(), {:ok, ssz_status} <- Snappy.decompress(snappy_status), {:ok, status} <- Ssz.from_ssz(ssz_status, SszTypes.StatusMessage), status From ad99a8016c3efcbdf15bd2e5efac46579ae3b1e3 Mon Sep 17 00:00:00 2001 From: karasakalmt Date: Wed, 6 Dec 2023 18:46:39 +0200 Subject: [PATCH 14/18] refactor: related changes --- lib/lambda_ethereum_consensus/fork_choice/helpers.ex | 4 ++-- lib/lambda_ethereum_consensus/fork_choice/store.ex | 7 +------ 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/lib/lambda_ethereum_consensus/fork_choice/helpers.ex b/lib/lambda_ethereum_consensus/fork_choice/helpers.ex index 01c2acee2..be27b8744 100644 --- a/lib/lambda_ethereum_consensus/fork_choice/helpers.ex +++ b/lib/lambda_ethereum_consensus/fork_choice/helpers.ex @@ -2,9 +2,9 @@ defmodule LambdaEthereumConsensus.ForkChoice.Helpers do @moduledoc """ Utility functions for the fork choice. """ - alias LambdaEthereumConsensus.StateTransition.{Accessors, Misc} - alias LambdaEthereumConsensus.Store.{StateStore, BlockStore} alias LambdaEthereumConsensus.ForkChoice + alias LambdaEthereumConsensus.StateTransition.{Accessors, Misc} + alias LambdaEthereumConsensus.Store.{BlockStore, StateStore} alias SszTypes.BeaconBlock alias SszTypes.BeaconState alias SszTypes.Checkpoint diff --git a/lib/lambda_ethereum_consensus/fork_choice/store.ex b/lib/lambda_ethereum_consensus/fork_choice/store.ex index 9349dab92..56c8e942f 100644 --- a/lib/lambda_ethereum_consensus/fork_choice/store.ex +++ b/lib/lambda_ethereum_consensus/fork_choice/store.ex @@ -38,7 +38,7 @@ defmodule LambdaEthereumConsensus.ForkChoice.Store do @spec get_current_status_message() :: {:ok, SszTypes.StatusMessage.t()} | {:error, any} def get_current_status_message do - get_current_status_message_from_store() + GenServer.call(__MODULE__, :get_current_status_message, @default_timeout) end @spec has_block?(SszTypes.root()) :: boolean() @@ -195,11 +195,6 @@ defmodule LambdaEthereumConsensus.ForkChoice.Store do GenServer.call(__MODULE__, {:get_store_attrs, attrs}, @default_timeout) end - @spec get_current_status_message_from_store() :: SszTypes.root() - def get_current_status_message_from_store do - GenServer.call(__MODULE__, :get_current_status_message, @default_timeout) - end - @spec on_tick_now(Store.t()) :: Store.t() defp on_tick_now(store) do new_store = Handlers.on_tick(store, :os.system_time(:second)) From f9e1eca19dd1a13e9c1f9cf70d7db4adb85e98aa Mon Sep 17 00:00:00 2001 From: karasakalmt Date: Mon, 11 Dec 2023 16:06:48 +0200 Subject: [PATCH 15/18] comment fix --- lib/lambda_ethereum_consensus/fork_choice/helpers.ex | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/lambda_ethereum_consensus/fork_choice/helpers.ex b/lib/lambda_ethereum_consensus/fork_choice/helpers.ex index 4eb4481ef..c82241b95 100644 --- a/lib/lambda_ethereum_consensus/fork_choice/helpers.ex +++ b/lib/lambda_ethereum_consensus/fork_choice/helpers.ex @@ -14,19 +14,15 @@ defmodule LambdaEthereumConsensus.ForkChoice.Helpers do {:ok, SszTypes.StatusMessage.t()} | {:error, any} def current_status_message(store) do with {:ok, head_root} <- get_head(store), - {:ok, state} <- StateStore.get_state(head_root), - {:ok, signed_head_block} <- - BlockStore.get_block(head_root), - {:ok, finalized_checkpoint} <- - ForkChoice.Store.get_finalized_checkpoint() do + {:ok, state} <- StateStore.get_state(head_root) do {:ok, %SszTypes.StatusMessage{ fork_digest: Misc.compute_fork_digest(state.fork.current_version, state.genesis_validators_root), - finalized_root: finalized_checkpoint.root, - finalized_epoch: finalized_checkpoint.epoch, + finalized_root: state.finalized_checkpoint.root, + finalized_epoch: state.finalized_checkpoint.epoch, head_root: head_root, - head_slot: signed_head_block.message.slot + head_slot: state.slot }} end end From 3767db8185faa89b9fe8daafaa1bd19129ca8e1e Mon Sep 17 00:00:00 2001 From: karasakalmt Date: Mon, 11 Dec 2023 16:19:30 +0200 Subject: [PATCH 16/18] build issue fixed --- lib/lambda_ethereum_consensus/fork_choice/helpers.ex | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/lambda_ethereum_consensus/fork_choice/helpers.ex b/lib/lambda_ethereum_consensus/fork_choice/helpers.ex index c82241b95..daa75f1ba 100644 --- a/lib/lambda_ethereum_consensus/fork_choice/helpers.ex +++ b/lib/lambda_ethereum_consensus/fork_choice/helpers.ex @@ -2,9 +2,8 @@ defmodule LambdaEthereumConsensus.ForkChoice.Helpers do @moduledoc """ Utility functions for the fork choice. """ - alias LambdaEthereumConsensus.ForkChoice alias LambdaEthereumConsensus.StateTransition.{Accessors, Misc} - alias LambdaEthereumConsensus.Store.{BlockStore, StateStore} + alias LambdaEthereumConsensus.Store.StateStore alias SszTypes.BeaconBlock alias SszTypes.BeaconState alias SszTypes.Checkpoint From 7c1f64c382ecd6ccc7a03c80544ea7bb5e17f300 Mon Sep 17 00:00:00 2001 From: karasakalmt Date: Mon, 11 Dec 2023 21:26:22 +0200 Subject: [PATCH 17/18] state fetch changed --- lib/lambda_ethereum_consensus/fork_choice/helpers.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lambda_ethereum_consensus/fork_choice/helpers.ex b/lib/lambda_ethereum_consensus/fork_choice/helpers.ex index daa75f1ba..3b52d22c8 100644 --- a/lib/lambda_ethereum_consensus/fork_choice/helpers.ex +++ b/lib/lambda_ethereum_consensus/fork_choice/helpers.ex @@ -13,7 +13,7 @@ defmodule LambdaEthereumConsensus.ForkChoice.Helpers do {:ok, SszTypes.StatusMessage.t()} | {:error, any} def current_status_message(store) do with {:ok, head_root} <- get_head(store), - {:ok, state} <- StateStore.get_state(head_root) do + {:ok, state} <- Map.fetch(store.block_states, head_root) do {:ok, %SszTypes.StatusMessage{ fork_digest: From 91b6ca2598ae0f3af57b7cba308714ef00b11d60 Mon Sep 17 00:00:00 2001 From: karasakalmt Date: Mon, 11 Dec 2023 21:36:37 +0200 Subject: [PATCH 18/18] build issue --- lib/lambda_ethereum_consensus/fork_choice/helpers.ex | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/lambda_ethereum_consensus/fork_choice/helpers.ex b/lib/lambda_ethereum_consensus/fork_choice/helpers.ex index 3b52d22c8..bf3cec72c 100644 --- a/lib/lambda_ethereum_consensus/fork_choice/helpers.ex +++ b/lib/lambda_ethereum_consensus/fork_choice/helpers.ex @@ -3,7 +3,6 @@ defmodule LambdaEthereumConsensus.ForkChoice.Helpers do Utility functions for the fork choice. """ alias LambdaEthereumConsensus.StateTransition.{Accessors, Misc} - alias LambdaEthereumConsensus.Store.StateStore alias SszTypes.BeaconBlock alias SszTypes.BeaconState alias SszTypes.Checkpoint