Skip to content

Commit

Permalink
Blob addition lacked a check about already transitioned states, this …
Browse files Browse the repository at this point in the history
…may fix the issue
  • Loading branch information
rodrigo-o committed Oct 4, 2024
1 parent 26e73e1 commit 9cae57f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 29 deletions.
5 changes: 4 additions & 1 deletion .iex.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ head_root = fn ->
store = LambdaEthereumConsensus.Store.StoreDb.fetch_store() |> elem(1)

{fn -> store end,
store |> LambdaEthereumConsensus.ForkChoice.Head.get_head() |> elem(1) |> LambdaEthereumConsensus.Utils.format_shorten_binary()}
store |> LambdaEthereumConsensus.ForkChoice.Head.get_head() |> elem(1) |> LambdaEthereumConsensus.Utils.format_binary()}
end


block_info = fn "0x"<>root -> LambdaEthereumConsensus.Store.Blocks.get_block_info(root |> Base.decode16(case: :lower) |> elem(1)) end
51 changes: 26 additions & 25 deletions lib/lambda_ethereum_consensus/beacon/pending_blocks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do
alias Types.SignedBeaconBlock
alias Types.Store

@type block_status :: :pending | :invalid | :download | :download_blobs | :unknown
@type block_status :: :transitioned | :pending | :invalid | :download | :download_blobs | :unknown
@type block_info ::
{SignedBeaconBlock.t(), :pending | :download_blobs}
| {nil, :invalid | :download}
Expand Down Expand Up @@ -103,9 +103,8 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do

defp process_block(store, block_info) do
if block_info.status != :pending do
Logger.error("Called process block for a block that's not ready: #{block_info}")
Logger.error("[PendingBlocks] Called process block for a block that's not ready: #{block_info}")
end

Logger.info("[PendingBlocks] Processing block #{inspect(block_info.root |> LambdaEthereumConsensus.Utils.format_shorten_binary())}, with parent: #{inspect(block_info.signed_block.message.parent_root |> LambdaEthereumConsensus.Utils.format_shorten_binary())}")
parent_root = block_info.signed_block.message.parent_root

Expand All @@ -128,32 +127,28 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do
{store, :download_pending}

%BlockInfo{status: :invalid} ->
Logger.info("[PendingBlocks] Parent block is invalid: #{inspect(parent_root |> LambdaEthereumConsensus.Utils.format_shorten_binary())}, making it invalid also")
Blocks.change_status(block_info, :invalid)
{store, :invalid}

%BlockInfo{status: :transitioned} ->
case Blocks.get_block_info(block_info.root) do
nil ->
case ForkChoice.on_block(store, block_info) do
{:ok, store} ->
Blocks.change_status(block_info, :transitioned)
{store, :transitioned}

{:error, reason, store} ->
Logger.error("[PendingBlocks] Saving block as invalid #{reason}",
slot: block_info.signed_block.message.slot,
root: block_info.root
)

Blocks.change_status(block_info, :invalid)
{store, :invalid}
end
%BlockInfo{} ->
Logger.error("[PendingBlocks] Block already processed!!!!!!!!!!!: #{block_info.root}")
{store, :ok}
case ForkChoice.on_block(store, block_info) do
{:ok, store} ->
Blocks.change_status(block_info, :transitioned)
{store, :transitioned}

{:error, reason, store} ->
Logger.error("[PendingBlocks] Saving block as invalid #{reason}",
slot: block_info.signed_block.message.slot,
root: block_info.root
)

Blocks.change_status(block_info, :invalid)
{store, :invalid}
end

_other ->
other ->
Logger.info("[PendingBlocks] Parent block is not ready: #{inspect(other, pretty: true)}")
{store, :ok}
end
end
Expand Down Expand Up @@ -185,13 +180,19 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do
|> Enum.map(&BlobDb.store_blob/1)
|> Enum.uniq()
|> Enum.reduce(store, fn root, store ->
with %BlockInfo{} = block_info <- Blocks.get_block_info(root),
with %BlockInfo{status: status} = block_info <- Blocks.get_block_info(root),
# Maybe just transitioned?? should we try to reprocess invalid blocks?
false <- status in [:transitioned, :invalid],
[] <- missing_blobs(block_info) do
block_info
|> Blocks.change_status(:pending)
|> then(&process_block_and_check_children(store, &1))
else
_ -> store
true ->
Logger.warning("[PendingBlocks] Blob added to block #{root |> LambdaEthereumConsensus.Utils.format_shorten_binary()} but its status is: #{Blocks.get_block_info(root).status}")
store
_ ->
store
end
end)
end
Expand Down
8 changes: 6 additions & 2 deletions lib/lambda_ethereum_consensus/fork_choice/fork_choice.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,12 @@ defmodule LambdaEthereumConsensus.ForkChoice do
|> prune_old_states(last_finalized_checkpoint.epoch)
|> tap(fn store ->
StoreDb.persist_store(store)
Logger.info("[Fork choice] Added new block", slot: slot, root: block_root)
Logger.info("[Fork choice] Recomputed head", slot: store.head_slot, root: store.head_root)
Logger.info("[Fork choice] Added new block: #{LambdaEthereumConsensus.Utils.format_shorten_binary(block_root)}", slot: slot, root: block_root)
Logger.info("[Fork choice] Recomputed head: #{LambdaEthereumConsensus.Utils.format_shorten_binary(store.head_root)}", slot: store.head_slot, root: store.head_root)

if block_root != store.head_root do
Logger.info("WTFFFFFFFFFFFFFFFFFFF", slot: store.head_slot, root: store.head_root)
end
end)
|> then(&{:ok, &1})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ defmodule CustomConsoleLogger do
end

def format_metadata_value(:root, root) do
Utils.format_shorten_binary(root)
Utils.format_binary(root)
end

def format_metadata_value(:bits, slot) do
Expand Down
8 changes: 8 additions & 0 deletions lib/lambda_ethereum_consensus/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ defmodule LambdaEthereumConsensus.Utils do
"0x#{String.slice(encoded, 0, 3)}..#{String.slice(encoded, -4, 4)}"
end

@doc """
Format a binary to its hexadecimal representation.
"""
@spec format_binary(binary) :: String.t()
def format_binary(binary) do
"0x#{binary |> Base.encode16(case: :lower)}"
end

@doc """
Format a bitstring to a base 2 representation.
"""
Expand Down

0 comments on commit 9cae57f

Please sign in to comment.