From e58e2aa0df2fc15097d801a048b9f460f19d1402 Mon Sep 17 00:00:00 2001 From: Mete Karasakal <32202283+karasakalmt@users.noreply.github.com> Date: Tue, 12 Dec 2023 16:55:51 +0200 Subject: [PATCH] feat: status req res (#492) --- .../fork_choice/helpers.ex | 17 +++++++++++++++++ .../fork_choice/store.ex | 10 ++++++++++ .../p2p/incoming_requests/handler.ex | 18 +++++------------- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/lib/lambda_ethereum_consensus/fork_choice/helpers.ex b/lib/lambda_ethereum_consensus/fork_choice/helpers.ex index 1ac8c5d93..bf3cec72c 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(Store.t()) :: + {:ok, SszTypes.StatusMessage.t()} | {:error, any} + def current_status_message(store) do + with {:ok, head_root} <- get_head(store), + {:ok, state} <- Map.fetch(store.block_states, head_root) do + {:ok, + %SszTypes.StatusMessage{ + fork_digest: + Misc.compute_fork_digest(state.fork.current_version, state.genesis_validators_root), + finalized_root: state.finalized_checkpoint.root, + finalized_epoch: state.finalized_checkpoint.epoch, + head_root: head_root, + head_slot: state.slot + }} + end + end + @spec get_forkchoice_store(BeaconState.t(), BeaconBlock.t()) :: {:ok, Store.t()} | {:error, any} def get_forkchoice_store(%BeaconState{} = anchor_state, %BeaconBlock{} = anchor_block) do anchor_state_root = Ssz.hash_tree_root!(anchor_state) diff --git a/lib/lambda_ethereum_consensus/fork_choice/store.ex b/lib/lambda_ethereum_consensus/fork_choice/store.ex index ed4ea85e4..56c8e942f 100644 --- a/lib/lambda_ethereum_consensus/fork_choice/store.ex +++ b/lib/lambda_ethereum_consensus/fork_choice/store.ex @@ -36,6 +36,11 @@ defmodule LambdaEthereumConsensus.ForkChoice.Store do div(time - genesis_time, ChainSpec.get("SECONDS_PER_SLOT")) end + @spec get_current_status_message() :: {:ok, SszTypes.StatusMessage.t()} | {:error, any} + def get_current_status_message do + GenServer.call(__MODULE__, :get_current_status_message, @default_timeout) + end + @spec has_block?(SszTypes.root()) :: boolean() def has_block?(block_root) do block = get_block(block_root) @@ -89,6 +94,11 @@ defmodule LambdaEthereumConsensus.ForkChoice.Store do {:reply, values, state} end + @impl GenServer + 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 {:reply, Map.get(state.blocks, block_root), state} end diff --git a/lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex b/lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex index bb8614183..493952182 100644 --- a/lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex +++ b/lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex @@ -2,9 +2,11 @@ defmodule LambdaEthereumConsensus.P2P.IncomingRequests.Handler do @moduledoc """ This module handles Req/Resp domain requests. """ - require Logger - alias LambdaEthereumConsensus.{Libp2pPort, P2P} + + alias LambdaEthereumConsensus.ForkChoice 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 @@ -28,18 +30,8 @@ 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, + {: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