Skip to content

Commit

Permalink
feat: status req res (lambdaclass#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
karasakalmt authored and h3lio5 committed Jan 5, 2024
1 parent 9cc2be1 commit 321fe39
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
17 changes: 17 additions & 0 deletions lib/lambda_ethereum_consensus/fork_choice/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions lib/lambda_ethereum_consensus/fork_choice/store.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
18 changes: 5 additions & 13 deletions lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 321fe39

Please sign in to comment.