Skip to content

Commit

Permalink
chore: replace BlockStore and StateStore with BlockDb and `Stat…
Browse files Browse the repository at this point in the history
…eDb` (#750)
  • Loading branch information
manojkgorle authored Feb 15, 2024
1 parent 63a9c27 commit 4e081b4
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 41 deletions.
12 changes: 6 additions & 6 deletions bench/block_processing.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ alias LambdaEthereumConsensus.ForkChoice
alias LambdaEthereumConsensus.ForkChoice.Handlers
alias LambdaEthereumConsensus.StateTransition.Cache
alias LambdaEthereumConsensus.Store
alias LambdaEthereumConsensus.Store.BlockStore
alias LambdaEthereumConsensus.Store.StateStore
alias LambdaEthereumConsensus.Store.BlockDb
alias LambdaEthereumConsensus.Store.StateDb
alias Types.{BeaconState, SignedBeaconBlock}

Logger.configure(level: :warning)
Expand All @@ -17,9 +17,9 @@ Cache.initialize_cache()
slot = 4_213_280

IO.puts("fetching blocks...")
{:ok, %BeaconState{} = state} = StateStore.get_state_by_slot(slot)
{:ok, %SignedBeaconBlock{} = block} = BlockStore.get_block_by_slot(slot)
{:ok, %SignedBeaconBlock{} = new_block} = BlockStore.get_block_by_slot(slot + 1)
{:ok, %BeaconState{} = state} = StateDb.get_state_by_slot(slot)
{:ok, %SignedBeaconBlock{} = block} = BlockDb.get_block_by_slot(slot)
{:ok, %SignedBeaconBlock{} = new_block} = BlockDb.get_block_by_slot(slot + 1)

IO.puts("initializing store...")
{:ok, store} = Types.Store.get_forkchoice_store(state, block)
Expand All @@ -28,7 +28,7 @@ store = Handlers.on_tick(store, store.time + 30)
attestations = new_block.message.body.attestations
attester_slashings = new_block.message.body.attester_slashings

{:ok, root} = BlockStore.get_block_root_by_slot(slot)
{:ok, root} = BlockDb.get_block_root_by_slot(slot)

IO.puts("about to process block: #{slot + 1}, with root: #{Base.encode16(root)}...")
IO.puts("#{length(attestations)} attestations ; #{length(attester_slashings)} attester slashings")
Expand Down
10 changes: 5 additions & 5 deletions bench/multiple_blocks_processing.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ alias LambdaEthereumConsensus.ForkChoice
alias LambdaEthereumConsensus.ForkChoice.Handlers
alias LambdaEthereumConsensus.StateTransition.Cache
alias LambdaEthereumConsensus.Store
alias LambdaEthereumConsensus.Store.BlockStore
alias LambdaEthereumConsensus.Store.StateStore
alias LambdaEthereumConsensus.Store.BlockDb
alias LambdaEthereumConsensus.Store.StateDb
alias Types.{BeaconState, SignedBeaconBlock}

Logger.configure(level: :warning)
Expand All @@ -19,14 +19,14 @@ count = 10
end_slot = start_slot + count

IO.puts("fetching blocks...")
{:ok, %BeaconState{} = state} = StateStore.get_state_by_slot(start_slot)
{:ok, %SignedBeaconBlock{} = signed_block} = BlockStore.get_block_by_slot(state.slot)
{:ok, %BeaconState{} = state} = StateDb.get_state_by_slot(start_slot)
{:ok, %SignedBeaconBlock{} = signed_block} = BlockDb.get_block_by_slot(state.slot)

blocks =
(start_slot + 1)..end_slot
# NOTE: we have to consider empty slots
|> Enum.flat_map(fn slot ->
case BlockStore.get_block_by_slot(slot) do
case BlockDb.get_block_by_slot(slot) do
{:ok, block} -> [block]
:not_found -> []
end
Expand Down
4 changes: 2 additions & 2 deletions lib/beacon_api/controllers/v1/beacon_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ defmodule BeaconApi.V1.BeaconController do
alias BeaconApi.ErrorController

alias LambdaEthereumConsensus.ForkChoice
alias LambdaEthereumConsensus.Store.BlockDb
alias LambdaEthereumConsensus.Store.Blocks
alias LambdaEthereumConsensus.Store.BlockStore
use BeaconApi, :controller

plug(OpenApiSpex.Plug.CastAndValidate, json_render_error_v2: true)
Expand Down Expand Up @@ -76,7 +76,7 @@ defmodule BeaconApi.V1.BeaconController do

def get_block_root(conn, %{block_id: block_id}) do
with {slot, ""} when slot >= 0 <- Integer.parse(block_id),
{:ok, block_root} <- BlockStore.get_block_root_by_slot(slot) do
{:ok, block_root} <- BlockDb.get_block_root_by_slot(slot) do
conn |> root_response(block_root, true, false)
else
:not_found ->
Expand Down
4 changes: 2 additions & 2 deletions lib/beacon_api/controllers/v2/beacon_controller.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
defmodule BeaconApi.V2.BeaconController do
alias BeaconApi.ApiSpec
alias BeaconApi.ErrorController
alias LambdaEthereumConsensus.Store.BlockDb
alias LambdaEthereumConsensus.Store.Blocks
alias LambdaEthereumConsensus.Store.BlockStore
use BeaconApi, :controller

plug(OpenApiSpex.Plug.CastAndValidate, json_render_error_v2: true)
Expand Down Expand Up @@ -48,7 +48,7 @@ defmodule BeaconApi.V2.BeaconController do

def get_block(conn, %{block_id: block_id}) do
with {slot, ""} when slot >= 0 <- Integer.parse(block_id),
{:ok, block} <- BlockStore.get_block_by_slot(slot) do
{:ok, block} <- BlockDb.get_block_by_slot(slot) do
conn |> block_response(block)
else
:not_found ->
Expand Down
14 changes: 7 additions & 7 deletions lib/lambda_ethereum_consensus/fork_choice/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule LambdaEthereumConsensus.ForkChoice.Helpers do
alias LambdaEthereumConsensus.StateTransition.{Accessors, Misc}
alias LambdaEthereumConsensus.Store.Blocks
alias LambdaEthereumConsensus.Store.BlockStates
alias LambdaEthereumConsensus.Store.{BlockStore, StateStore}
alias LambdaEthereumConsensus.Store.{BlockDb, StateDb}

alias Types.Store

Expand Down Expand Up @@ -244,7 +244,7 @@ defmodule LambdaEthereumConsensus.ForkChoice.Helpers do

def block_root_by_block_id(slot) when is_integer(slot) do
with :ok <- check_valid_slot(slot, BeaconChain.get_current_slot()),
{:ok, root} <- BlockStore.get_block_root_by_slot(slot) do
{:ok, root} <- BlockDb.get_block_root_by_slot(slot) do
# TODO compute is_optimistic_or_invalid() and is_finalized()
execution_optimistic = true
finalized = false
Expand All @@ -259,15 +259,15 @@ defmodule LambdaEthereumConsensus.ForkChoice.Helpers do
execution_optimistic = true
finalized = false

case StateStore.get_state_by_state_root(hex_root) do
case StateDb.get_state_by_state_root(hex_root) do
{:ok, _state} -> {:ok, {hex_root, execution_optimistic, finalized}}
_ -> :not_found
end
end

def state_root_by_state_id(id) do
with {:ok, {block_root, optimistic, finalized}} <- block_root_by_block_id(id),
{:ok, block} <- BlockStore.get_block(block_root) do
{:ok, block} <- BlockDb.get_block(block_root) do
%{message: %{state_root: state_root}} = block
{:ok, {state_root, optimistic, finalized}}
end
Expand All @@ -281,7 +281,7 @@ defmodule LambdaEthereumConsensus.ForkChoice.Helpers do
| :invalid_id
def block_by_block_id(block_id) do
with {:ok, {root, optimistic, finalized}} <- block_root_by_block_id(block_id),
{:ok, block} <- BlockStore.get_block(root) do
{:ok, block} <- BlockDb.get_block(root) do
{:ok, {block, optimistic, finalized}}
end
end
Expand All @@ -297,7 +297,7 @@ defmodule LambdaEthereumConsensus.ForkChoice.Helpers do
execution_optimistic = true
finalized = false

case StateStore.get_state_by_state_root(hex_root) do
case StateDb.get_state_by_state_root(hex_root) do
{:ok, state} -> {:ok, {state, execution_optimistic, finalized}}
_ -> :not_found
end
Expand All @@ -307,7 +307,7 @@ defmodule LambdaEthereumConsensus.ForkChoice.Helpers do
with {:ok, {%{message: %{state_root: state_root}}, optimistic, finalized}} <-
block_by_block_id(id),
{:ok, state} <-
StateStore.get_state_by_state_root(state_root) do
StateDb.get_state_by_state_root(state_root) do
{:ok, {state, optimistic, finalized}}
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ defmodule LambdaEthereumConsensus.P2P.IncomingRequests.Handler do

alias LambdaEthereumConsensus.Beacon.BeaconChain
alias LambdaEthereumConsensus.{Libp2pPort, P2P}
alias LambdaEthereumConsensus.Store.BlockDb
alias LambdaEthereumConsensus.Store.Blocks
alias LambdaEthereumConsensus.Store.BlockStore

require Logger

Expand Down Expand Up @@ -113,7 +113,7 @@ defmodule LambdaEthereumConsensus.P2P.IncomingRequests.Handler do

blocks =
start_slot..slot_coverage
|> Enum.map(&BlockStore.get_block_by_slot/1)
|> Enum.map(&BlockDb.get_block_by_slot/1)

response_chunk =
blocks
Expand Down
6 changes: 3 additions & 3 deletions lib/lambda_ethereum_consensus/store/block_states.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule LambdaEthereumConsensus.Store.BlockStates do
Interface to `Store.block_states`.
"""
alias LambdaEthereumConsensus.Store.LRUCache
alias LambdaEthereumConsensus.Store.StateStore
alias LambdaEthereumConsensus.Store.StateDb
alias Types.BeaconState

@table :states_by_block_hash
Expand All @@ -20,7 +20,7 @@ defmodule LambdaEthereumConsensus.Store.BlockStates do
table: @table,
max_entries: @max_entries,
batch_prune_size: @batch_prune_size,
store_func: &StateStore.store_state(&2, &1)
store_func: &StateDb.store_state(&2, &1)
)
end

Expand Down Expand Up @@ -50,7 +50,7 @@ defmodule LambdaEthereumConsensus.Store.BlockStates do
##########################

defp fetch_state(key) do
case StateStore.get_state_by_block_root(key) do
case StateDb.get_state_by_block_root(key) do
{:ok, value} -> value
:not_found -> nil
# TODO: handle this somehow?
Expand Down
2 changes: 1 addition & 1 deletion lib/lambda_ethereum_consensus/store/block_store.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule LambdaEthereumConsensus.Store.BlockStore do
defmodule LambdaEthereumConsensus.Store.BlockDb do
@moduledoc """
Storage and retrieval of blocks.
"""
Expand Down
6 changes: 3 additions & 3 deletions lib/lambda_ethereum_consensus/store/blocks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule LambdaEthereumConsensus.Store.Blocks do
@moduledoc """
Interface to `Store.blocks`.
"""
alias LambdaEthereumConsensus.Store.BlockStore
alias LambdaEthereumConsensus.Store.BlockDb
alias LambdaEthereumConsensus.Store.LRUCache
alias Types.BeaconBlock
alias Types.SignedBeaconBlock
Expand All @@ -21,7 +21,7 @@ defmodule LambdaEthereumConsensus.Store.Blocks do
table: @table,
max_entries: @max_entries,
batch_prune_size: @batch_prune_size,
store_func: &BlockStore.store_block(&2, &1)
store_func: &BlockDb.store_block(&2, &1)
)
end

Expand Down Expand Up @@ -66,7 +66,7 @@ defmodule LambdaEthereumConsensus.Store.Blocks do
##########################

defp fetch_block(key) do
case BlockStore.get_block(key) do
case BlockDb.get_block(key) do
{:ok, value} -> value
:not_found -> nil
# TODO: handle this somehow?
Expand Down
2 changes: 1 addition & 1 deletion lib/lambda_ethereum_consensus/store/state_store.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule LambdaEthereumConsensus.Store.StateStore do
defmodule LambdaEthereumConsensus.Store.StateDb do
@moduledoc """
Beacon node state storage.
"""
Expand Down
10 changes: 5 additions & 5 deletions test/integration/fork_choice/handlers_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ defmodule Integration.ForkChoice.HandlersTest do

alias LambdaEthereumConsensus.ForkChoice.Handlers
alias LambdaEthereumConsensus.StateTransition.Cache
alias LambdaEthereumConsensus.Store.BlockDb
alias LambdaEthereumConsensus.Store.Blocks
alias LambdaEthereumConsensus.Store.BlockStore
alias LambdaEthereumConsensus.Store.Db
alias LambdaEthereumConsensus.Store.StateStore
alias LambdaEthereumConsensus.Store.StateDb

setup_all do
start_supervised!(Db)
Expand All @@ -21,10 +21,10 @@ defmodule Integration.ForkChoice.HandlersTest do
test "on_block w/data from DB" do
# NOTE: this test requires a DB with a state, and blocks for the state's slot and the next slot.
# WARN: sometimes fails with "OffsetOutOfBounds" errors. Re-run the test in those cases.
{:ok, state} = StateStore.get_latest_state()
{:ok, state} = StateDb.get_latest_state()

{:ok, signed_block} = BlockStore.get_block_by_slot(state.slot)
{:ok, new_signed_block} = BlockStore.get_block_by_slot(state.slot + 1)
{:ok, signed_block} = BlockDb.get_block_by_slot(state.slot)
{:ok, new_signed_block} = BlockDb.get_block_by_slot(state.slot + 1)

assert {:ok, store} = Types.Store.get_forkchoice_store(state, signed_block)
new_store = Handlers.on_tick(store, :os.system_time(:second))
Expand Down
8 changes: 4 additions & 4 deletions test/unit/beacon_api_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule BeaconApiTest do
use Plug.Test
use Patch
alias BeaconApi.Router
alias LambdaEthereumConsensus.Store.BlockStore
alias LambdaEthereumConsensus.Store.BlockDb
alias LambdaEthereumConsensus.Store.Db

@moduletag :beacon_api_case
Expand Down Expand Up @@ -42,7 +42,7 @@ defmodule BeaconApiTest do
0, 0>>

signed_block = Fixtures.Block.signed_beacon_block()
BlockStore.store_block(signed_block, head_root)
BlockDb.store_block(signed_block, head_root)

resp_body = %{
data: %{root: "0x" <> Base.encode16(signed_block.message.state_root, case: :lower)},
Expand Down Expand Up @@ -86,11 +86,11 @@ defmodule BeaconApiTest do
0, 0>>

signed_block = Fixtures.Block.signed_beacon_block()
BlockStore.store_block(signed_block, head_root)
BlockDb.store_block(signed_block, head_root)
beacon_state = Fixtures.Block.beacon_state()

patch(
LambdaEthereumConsensus.Store.StateStore,
LambdaEthereumConsensus.Store.StateDb,
:get_state_by_state_root,
{:ok, beacon_state}
)
Expand Down

0 comments on commit 4e081b4

Please sign in to comment.