Skip to content

Commit 9cb0752

Browse files
committed
Changes per CR.
1 parent 8b47bde commit 9cb0752

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

lib/lambda_ethereum_consensus/beacon/pending_blocks.ex

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do
4747
@impl true
4848
def handle_cast({:add_block, %SignedBeaconBlock{message: block} = signed_block}, state) do
4949
block_root = Ssz.hash_tree_root!(block)
50-
{:noreply, Map.put(state, block_root, {signed_block, :pending})}
50+
51+
if state |> Map.get(block_root) do
52+
{:noreply, state}
53+
else
54+
{:noreply, state |> Map.put(block_root, {signed_block, :pending})}
55+
end
5156
end
5257

5358
@impl true
@@ -77,22 +82,23 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do
7782
|> Enum.sort_by(fn {_, signed_block} -> signed_block.message.slot end)
7883
|> Enum.reduce(state, fn {block_root, signed_block}, state ->
7984
parent_root = signed_block.message.parent_root
85+
parent_status = get_block_status(state, parent_root)
8086

8187
cond do
8288
# If already processed, remove it
8389
Blocks.get_block(block_root) ->
8490
state |> Map.delete(block_root)
8591

8692
# If parent is invalid, block is invalid
87-
get_block_status(state, parent_root) == :invalid ->
93+
parent_status == :invalid ->
8894
state |> Map.put(block_root, {nil, :invalid})
8995

9096
# If parent is processing, block is pending
91-
get_block_status(state, parent_root) == :processing ->
97+
parent_status == :processing ->
9298
state
9399

94100
# If parent is pending, block is pending
95-
get_block_status(state, parent_root) == :pending ->
101+
parent_status == :pending ->
96102
state
97103

98104
# If parent is not in fork choice, download parent
@@ -117,20 +123,16 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do
117123
blocks_to_download = state |> Map.filter(fn {_, {_, s}} -> s == :download end) |> Map.keys()
118124

119125
downloaded_blocks =
120-
if Enum.empty?(blocks_to_download) do
121-
[]
122-
else
123-
blocks_to_download
124-
|> Enum.take(16)
125-
|> BlockDownloader.request_blocks_by_root()
126-
|> case do
127-
{:ok, signed_blocks} ->
128-
signed_blocks
129-
130-
{:error, reason} ->
131-
Logger.debug("Block download failed: '#{reason}'")
132-
[]
133-
end
126+
blocks_to_download
127+
|> Enum.take(16)
128+
|> BlockDownloader.request_blocks_by_root()
129+
|> case do
130+
{:ok, signed_blocks} ->
131+
signed_blocks
132+
133+
{:error, reason} ->
134+
Logger.debug("Block download failed: '#{reason}'")
135+
[]
134136
end
135137

136138
new_state =

lib/lambda_ethereum_consensus/p2p/block_downloader.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,13 @@ defmodule LambdaEthereumConsensus.P2P.BlockDownloader do
7979

8080
@spec request_blocks_by_root([Types.root()], integer()) ::
8181
{:ok, [Types.SignedBeaconBlock.t()]} | {:error, binary()}
82-
def request_blocks_by_root(roots, retries \\ @default_retries) do
82+
def request_blocks_by_root(roots, retries \\ @default_retries)
83+
84+
def request_blocks_by_root([], _retries) do
85+
{:ok, []}
86+
end
87+
88+
def request_blocks_by_root(roots, retries) do
8389
Logger.debug("Requesting block for roots #{Enum.map_join(roots, ", ", &Base.encode16/1)}")
8490

8591
peer_id = get_some_peer()

0 commit comments

Comments
 (0)