Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move ForkChoice reads to BeaconChain #677

Merged
merged 5 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 35 additions & 24 deletions lib/lambda_ethereum_consensus/beacon/beacon_chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule LambdaEthereumConsensus.Beacon.BeaconChain do
alias LambdaEthereumConsensus.ForkChoice
alias LambdaEthereumConsensus.StateTransition.Misc
alias Types.BeaconState
alias Types.Checkpoint

defmodule BeaconChainState do
@moduledoc false
Expand All @@ -24,8 +25,8 @@ defmodule LambdaEthereumConsensus.Beacon.BeaconChain do
cached_fork_choice: %{
head_root: Types.root(),
head_slot: Types.slot(),
finalized_root: Types.root(),
finalized_epoch: Types.epoch()
justified: Types.Checkpoint.t(),
finalized: Types.Checkpoint.t()
}
}
end
Expand All @@ -40,14 +41,27 @@ defmodule LambdaEthereumConsensus.Beacon.BeaconChain do
GenServer.call(__MODULE__, :get_current_slot)
end

@spec update_fork_choice_cache(Types.root(), Types.slot(), Types.root(), Types.epoch()) :: :ok
def update_fork_choice_cache(head_root, head_slot, finalized_root, finalized_epoch) do
@spec update_fork_choice_cache(Types.root(), Types.slot(), Checkpoint.t(), Checkpoint.t()) ::
:ok
def update_fork_choice_cache(head_root, head_slot, justified, finalized) do
GenServer.cast(
__MODULE__,
{:update_fork_choice_cache, head_root, head_slot, finalized_root, finalized_epoch}
{:update_fork_choice_cache, head_root, head_slot, justified, finalized}
)
end

@spec get_finalized_checkpoint() :: Types.Checkpoint.t()
def get_finalized_checkpoint do
%{finalized: finalized} = GenServer.call(__MODULE__, :get_fork_choice_cache)
finalized
end

@spec get_justified_checkpoint() :: Types.Checkpoint.t()
def get_justified_checkpoint do
%{justified: justified} = GenServer.call(__MODULE__, :get_fork_choice_cache)
justified
end

@spec get_current_epoch() :: integer()
def get_current_epoch do
Misc.compute_epoch_at_slot(get_current_slot())
Expand Down Expand Up @@ -82,13 +96,13 @@ defmodule LambdaEthereumConsensus.Beacon.BeaconChain do
%BeaconChainState{
genesis_time: anchor_state.genesis_time,
genesis_validators_root: anchor_state.genesis_validators_root,
time: time,
cached_fork_choice: %{
head_root: <<0::256>>,
head_slot: anchor_state.slot,
finalized_root: anchor_state.finalized_checkpoint.root,
finalized_epoch: anchor_state.finalized_checkpoint.epoch
},
time: time
justified: anchor_state.finalized_checkpoint,
finalized: anchor_state.finalized_checkpoint
}
}}
end

Expand All @@ -97,6 +111,11 @@ defmodule LambdaEthereumConsensus.Beacon.BeaconChain do
{:reply, compute_current_slot(state), state}
end

@impl true
def handle_call(:get_fork_choice_cache, _, %{cached_fork_choice: cached} = state) do
{:reply, cached, state}
end

@impl true
def handle_call({:get_fork_digest, slot}, _from, state) do
fork_digest =
Expand All @@ -116,8 +135,7 @@ defmodule LambdaEthereumConsensus.Beacon.BeaconChain do
%{
head_root: head_root,
head_slot: head_slot,
finalized_root: finalized_root,
finalized_epoch: finalized_epoch
finalized: %{root: finalized_root, epoch: finalized_epoch}
} = state.cached_fork_choice

status_message = %Types.StatusMessage{
Expand All @@ -143,17 +161,14 @@ defmodule LambdaEthereumConsensus.Beacon.BeaconChain do
end

@impl true
def handle_cast(
{:update_fork_choice_cache, head_root, head_slot, finalized_root, finalized_epoch},
state
) do
def handle_cast({:update_fork_choice_cache, head_root, head_slot, justified, finalized}, state) do
{:noreply,
state
|> Map.put(:cached_fork_choice, %{
head_root: head_root,
head_slot: head_slot,
finalized_root: finalized_root,
finalized_epoch: finalized_epoch
justified: justified,
finalized: finalized
})}
end

Expand All @@ -168,12 +183,8 @@ defmodule LambdaEthereumConsensus.Beacon.BeaconChain do
end

defp compute_fork_digest(slot, genesis_validators_root) do
current_fork_version =
slot |> Misc.compute_epoch_at_slot() |> ChainSpec.get_fork_version_for_epoch()

Misc.compute_fork_digest(
current_fork_version,
genesis_validators_root
)
Misc.compute_epoch_at_slot(slot)
|> ChainSpec.get_fork_version_for_epoch()
|> Misc.compute_fork_digest(genesis_validators_root)
end
end
4 changes: 1 addition & 3 deletions lib/lambda_ethereum_consensus/beacon/sync_blocks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ defmodule LambdaEthereumConsensus.Beacon.SyncBlocks do

alias LambdaEthereumConsensus.Beacon.BeaconChain
alias LambdaEthereumConsensus.Beacon.PendingBlocks
alias LambdaEthereumConsensus.ForkChoice
alias LambdaEthereumConsensus.P2P.BlockDownloader
alias LambdaEthereumConsensus.StateTransition.Misc

Expand All @@ -24,8 +23,7 @@ defmodule LambdaEthereumConsensus.Beacon.SyncBlocks do
def run(_opts) do
# Initial sleep for faster app start
Process.sleep(1000)
{:ok, checkpoint} = ForkChoice.get_finalized_checkpoint()

checkpoint = BeaconChain.get_finalized_checkpoint()
initial_slot = Misc.compute_start_slot_at_epoch(checkpoint.epoch)
last_slot = BeaconChain.get_current_slot()

Expand Down
26 changes: 2 additions & 24 deletions lib/lambda_ethereum_consensus/fork_choice/fork_choice.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,6 @@ defmodule LambdaEthereumConsensus.ForkChoice do
GenServer.start_link(__MODULE__, opts, name: __MODULE__)
end

@spec get_finalized_checkpoint() :: {:ok, Types.Checkpoint.t()}
def get_finalized_checkpoint do
[finalized_checkpoint] = get_store_attrs([:finalized_checkpoint])
{:ok, finalized_checkpoint}
end

@spec get_justified_checkpoint() :: {:ok, Types.Checkpoint.t()}
def get_justified_checkpoint do
[justified_checkpoint] = get_store_attrs([:justified_checkpoint])
{:ok, justified_checkpoint}
end

@spec has_block?(Types.root()) :: boolean()
def has_block?(block_root) do
GenServer.call(__MODULE__, {:has_block?, block_root}, @default_timeout)
Expand Down Expand Up @@ -92,11 +80,6 @@ defmodule LambdaEthereumConsensus.ForkChoice do
{:reply, values, state}
end

@impl GenServer
def handle_call(:get_current_status_message, _from, state) do
{:reply, Helpers.current_status_message(state), state}
end

def handle_call({:has_block?, block_root}, _from, state) do
{:reply, Store.has_block?(state, block_root), state}
end
Expand Down Expand Up @@ -170,11 +153,6 @@ defmodule LambdaEthereumConsensus.ForkChoice do
### Private Functions
##########################

@spec get_store_attrs([atom()]) :: [any()]
defp get_store_attrs(attrs) do
GenServer.call(__MODULE__, {:get_store_attrs, attrs}, @default_timeout)
end

@spec apply_handler(any(), any(), any()) :: any()
def apply_handler(iter, state, handler) do
iter
Expand Down Expand Up @@ -220,8 +198,8 @@ defmodule LambdaEthereumConsensus.ForkChoice do
BeaconChain.update_fork_choice_cache(
head_root,
head_block.slot,
finalized_checkpoint.root,
finalized_checkpoint.epoch
store.justified_checkpoint,
finalized_checkpoint
)

:ok
Expand Down
19 changes: 8 additions & 11 deletions lib/lambda_ethereum_consensus/fork_choice/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ defmodule LambdaEthereumConsensus.ForkChoice.Helpers do
Utility functions for the fork choice.
"""
alias LambdaEthereumConsensus.Beacon.BeaconChain
alias LambdaEthereumConsensus.ForkChoice
alias LambdaEthereumConsensus.StateTransition.{Accessors, Misc}
alias LambdaEthereumConsensus.Store.BlockStore
alias Types.Store
Expand Down Expand Up @@ -192,19 +191,17 @@ defmodule LambdaEthereumConsensus.ForkChoice.Helpers do
def root_by_id(:genesis), do: :not_found

def root_by_id(:justified) do
with {:ok, justified_checkpoint} <- ForkChoice.get_justified_checkpoint() do
# TODO compute is_optimistic_or_invalid
execution_optimistic = true
{:ok, {justified_checkpoint.root, execution_optimistic, false}}
end
justified_checkpoint = BeaconChain.get_justified_checkpoint()
# TODO compute is_optimistic_or_invalid
execution_optimistic = true
{:ok, {justified_checkpoint.root, execution_optimistic, false}}
end

def root_by_id(:finalized) do
with {:ok, finalized_checkpoint} <- ForkChoice.get_finalized_checkpoint() do
# TODO compute is_optimistic_or_invalid
execution_optimistic = true
{:ok, {finalized_checkpoint.root, execution_optimistic, true}}
end
finalized_checkpoint = BeaconChain.get_finalized_checkpoint()
# TODO compute is_optimistic_or_invalid
execution_optimistic = true
{:ok, {finalized_checkpoint.root, execution_optimistic, true}}
end

def root_by_id(:invalid_id), do: :invalid_id
Expand Down
Loading