-
Notifications
You must be signed in to change notification settings - Fork 35
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: persist forkchoice state #1125
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,11 +65,13 @@ defmodule LambdaEthereumConsensus.ForkChoice do | |
:telemetry.execute([:sync, :store], %{slot: Store.get_current_slot(store)}) | ||
:telemetry.execute([:sync, :on_block], %{slot: head_slot}) | ||
|
||
persist_fork_choice_store(store) | ||
{:ok, store} | ||
end | ||
|
||
@impl GenServer | ||
def handle_cast({:on_block, block_root, %SignedBeaconBlock{} = signed_block, from}, store) do | ||
def handle_cast({:on_block, block_root, %SignedBeaconBlock{} = signed_block, from}, _store) do | ||
store = fetch_fork_choice_store!() | ||
slot = signed_block.message.slot | ||
|
||
Logger.info("[Fork choice] Adding new block", root: block_root, slot: slot) | ||
|
@@ -93,6 +95,7 @@ defmodule LambdaEthereumConsensus.ForkChoice do | |
prune_old_states(last_finalized_checkpoint.epoch, new_finalized_checkpoint.epoch) | ||
|
||
GenServer.cast(from, {:block_processed, block_root, true}) | ||
persist_fork_choice_store(new_store) | ||
{:noreply, new_store} | ||
|
||
{:error, reason} -> | ||
|
@@ -103,7 +106,8 @@ defmodule LambdaEthereumConsensus.ForkChoice do | |
end | ||
|
||
@impl GenServer | ||
def handle_cast({:on_attestation, %Attestation{} = attestation}, %Store{} = state) do | ||
def handle_cast({:on_attestation, %Attestation{} = attestation}, %Store{} = _state) do | ||
state = fetch_fork_choice_store!() | ||
id = attestation.signature |> Base.encode16() |> String.slice(0, 8) | ||
Logger.debug("[Fork choice] Adding attestation #{id} to the store") | ||
|
||
|
@@ -113,12 +117,14 @@ defmodule LambdaEthereumConsensus.ForkChoice do | |
_ -> state | ||
end | ||
|
||
persist_fork_choice_store(state) | ||
{:noreply, state} | ||
end | ||
|
||
@impl GenServer | ||
def handle_cast({:attester_slashing, attester_slashing}, state) do | ||
def handle_cast({:attester_slashing, attester_slashing}, _state) do | ||
Logger.info("[Fork choice] Adding attester slashing to the store") | ||
state = fetch_fork_choice_store!() | ||
|
||
state = | ||
case Handlers.on_attester_slashing(state, attester_slashing) do | ||
|
@@ -130,16 +136,19 @@ defmodule LambdaEthereumConsensus.ForkChoice do | |
state | ||
end | ||
|
||
persist_fork_choice_store(state) | ||
{:noreply, state} | ||
end | ||
|
||
@impl GenServer | ||
def handle_cast({:on_tick, time}, store) do | ||
def handle_cast({:on_tick, time}, _store) do | ||
store = fetch_fork_choice_store!() | ||
%Store{finalized_checkpoint: last_finalized_checkpoint} = store | ||
|
||
new_store = Handlers.on_tick(store, time) | ||
%Store{finalized_checkpoint: new_finalized_checkpoint} = new_store | ||
prune_old_states(last_finalized_checkpoint.epoch, new_finalized_checkpoint.epoch) | ||
persist_fork_choice_store(new_store) | ||
{:noreply, new_store} | ||
end | ||
|
||
|
@@ -219,4 +228,19 @@ defmodule LambdaEthereumConsensus.ForkChoice do | |
Logger.debug("[Fork choice] Store persisted") | ||
end) | ||
end | ||
|
||
defp persist_fork_choice_store(store) do | ||
:telemetry.span([:fork_choice, :persist], %{}, fn -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's have the telemetry call on the db methods directly, so they are called no matter who is storing/fetching the store. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
{StoreDb.persist_fork_choice_store(store), %{}} | ||
end) | ||
end | ||
|
||
defp fetch_fork_choice_store!() do | ||
{:ok, store} = | ||
:telemetry.span([:fork_choice, :fetch], %{}, fn -> | ||
{StoreDb.fetch_fork_choice_store(), %{}} | ||
end) | ||
|
||
store | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,16 +5,25 @@ defmodule LambdaEthereumConsensus.Store.StoreDb do | |
alias LambdaEthereumConsensus.Store.Db | ||
|
||
@store_prefix "store" | ||
@fork_choice_store_prefix "fork_choice_store" | ||
@snapshot_prefix "snapshot" | ||
|
||
@spec fetch_store() :: {:ok, Types.Store.t()} | :not_found | ||
def fetch_store(), do: get(@store_prefix) | ||
|
||
@spec fetch_fork_choice_store() :: {:ok, Types.Store.t()} | :not_found | ||
def fetch_fork_choice_store(), do: get(@fork_choice_store_prefix) | ||
|
||
@spec persist_store(Types.Store.t()) :: :ok | ||
def persist_store(%Types.Store{} = store) do | ||
put(@store_prefix, store) | ||
end | ||
|
||
@spec persist_fork_choice_store(Types.Store.t()) :: :ok | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need a separate table? Is it not the same store we're saving? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Merged into a single table! |
||
def persist_fork_choice_store(%Types.Store{} = store) do | ||
put(@fork_choice_store_prefix, store) | ||
end | ||
|
||
@spec fetch_deposits_snapshot() :: {:ok, Types.DepositTreeSnapshot.t()} | :not_found | ||
def fetch_deposits_snapshot(), do: get(@snapshot_prefix) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would persist before calling the cast. As a general rule it's useful that the state is stored and consistent before others are made aware of it. As a matter of fact, if we persist it before launching the recompute_head async task, we can remove the persist call in recompute-head.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true. Done!