diff --git a/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex b/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex index 9fa4bf8d7..79a9b1d9f 100644 --- a/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex +++ b/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex @@ -44,7 +44,7 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do if Enum.empty?(missing_blobs) do block_info else - BlobDownloader.request_blobs_by_root(missing_blobs) + BlobDownloader.request_blobs_by_root(missing_blobs, &process_blobs/1) block_info |> BlockInfo.change_status(:download_blobs) end |> Blocks.new_block_info() @@ -132,6 +132,12 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do end end + defp process_blobs({:ok, blobs}), do: add_blobs(blobs) + + defp process_blobs({:error, reason}) do + Logger.error("Error downloading blobs: #{inspect(reason)}") + end + @spec missing_blobs(BlockInfo.t()) :: [Types.BlobIdentifier.t()] defp missing_blobs(%BlockInfo{root: root, signed_block: signed_block}) do signed_block.message.body.blob_kzg_commitments diff --git a/lib/lambda_ethereum_consensus/p2p/blob_downloader.ex b/lib/lambda_ethereum_consensus/p2p/blob_downloader.ex index 70848b581..0e0291495 100644 --- a/lib/lambda_ethereum_consensus/p2p/blob_downloader.ex +++ b/lib/lambda_ethereum_consensus/p2p/blob_downloader.ex @@ -13,6 +13,7 @@ defmodule LambdaEthereumConsensus.P2P.BlobDownloader do @blobs_by_root_protocol_id "/eth2/beacon_chain/req/blob_sidecars_by_root/1/ssz_snappy" @type on_blobs :: ({:ok, [BlobSidecar.t()]} | {:error, any()} -> :ok) + @type on_blob :: ({:ok, BlobSidecar.t()} | {:error, any()} -> :ok) # Requests to peers might fail for various reasons, # for example they might not support the protocol or might not reply @@ -59,18 +60,22 @@ defmodule LambdaEthereumConsensus.P2P.BlobDownloader do end end - @spec request_blob_by_root(Types.BlobIdentifier.t(), non_neg_integer()) :: - {:ok, BlobSidecar.t()} | {:error, binary()} - def request_blob_by_root(identifier, retries \\ @default_retries) do - with {:ok, [blob]} <- request_blobs_by_root([identifier], retries) do - {:ok, blob} - end + @spec request_blob_by_root(Types.BlobIdentifier.t(), on_blob(), non_neg_integer()) :: :ok + def request_blob_by_root(identifier, on_blob, retries \\ @default_retries) do + request_blobs_by_root( + [identifier], + fn + {:ok, [blob]} -> on_blob.({:ok, blob}) + other -> on_blob.(other) + end, + retries + ) end @spec request_blobs_by_root([Types.BlobIdentifier.t()], on_blobs(), non_neg_integer()) :: :ok def request_blobs_by_root(identifiers, on_blobs, retries \\ @default_retries) - def request_blobs_by_root([], on_blobs, _retries), do: {:ok, []} + def request_blobs_by_root([], _on_blobs, _retries), do: {:ok, []} def request_blobs_by_root(identifiers, on_blobs, retries) do Logger.debug("Requesting #{length(identifiers)} blobs.")