@@ -10,7 +10,7 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do
1010 alias LambdaEthereumConsensus.Metrics
1111 alias LambdaEthereumConsensus.P2P.BlobDownloader
1212 alias LambdaEthereumConsensus.P2P.BlockDownloader
13- alias LambdaEthereumConsensus.Store.BlobDb
13+ alias LambdaEthereumConsensus.Store.Blobs
1414 alias LambdaEthereumConsensus.Store.Blocks
1515 alias LambdaEthereumConsensus.Utils
1616 alias Types.BlockInfo
@@ -46,15 +46,20 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do
4646
4747 # If the block is new or was to be downloaded, we store it.
4848 if is_nil ( loaded_block ) or loaded_block . status == :download do
49- missing_blobs = missing_blobs ( block_info )
49+ missing_blobs = Blobs . missing_for_block ( block_info )
5050
5151 if Enum . empty? ( missing_blobs ) do
5252 Logger . debug ( "[PendingBlocks] No missing blobs for block, process it" , log_md )
5353 Blocks . new_block_info ( block_info )
5454 process_block_and_check_children ( store , block_info )
5555 else
5656 Logger . debug ( "[PendingBlocks] Missing blobs for block, scheduling download" , log_md )
57- BlobDownloader . request_blobs_by_root ( missing_blobs , & process_blobs / 2 , @ download_retries )
57+
58+ BlobDownloader . request_blobs_by_root (
59+ missing_blobs ,
60+ & process_blobs / 2 ,
61+ @ download_retries
62+ )
5863
5964 block_info
6065 |> BlockInfo . change_status ( :download_blobs )
@@ -72,6 +77,7 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do
7277 module after receiving a new block, but there are some other cases like at node startup, as there
7378 may be pending blocks from prior executions.
7479 """
80+ @ spec process_blocks ( Store . t ( ) ) :: Store . t ( )
7581 def process_blocks ( store ) do
7682 case Blocks . get_blocks_with_status ( :pending ) do
7783 { :ok , blocks } ->
@@ -92,6 +98,34 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do
9298 end
9399 end
94100
101+ @ doc """
102+ Process incoming blobs if the block can be processed does so immediately.
103+ """
104+ @ spec process_blobs ( Store . t ( ) , { :ok , [ Types.BlobSidecar . t ( ) ] } ) :: { :ok , Store . t ( ) }
105+ def process_blobs ( store , { :ok , blobs } ) do
106+ blobs
107+ |> Blobs . add_blobs ( )
108+ |> Enum . reduce ( store , fn root , store ->
109+ with % BlockInfo { status: :download_blobs } = block_info <- Blocks . get_block_info ( root ) ,
110+ [ ] <- Blobs . missing_for_block ( block_info ) do
111+ block_info
112+ |> Blocks . change_status ( :pending )
113+ |> then ( & process_block_and_check_children ( store , & 1 ) )
114+
115+ { :ok , store }
116+ else
117+ _ -> { :ok , store }
118+ end
119+ end )
120+ end
121+
122+ @ spec process_blobs ( Store . t ( ) , { :error , any ( ) } ) :: { :ok , Store . t ( ) }
123+ def process_blobs ( store , { :error , reason } ) do
124+ # We might want to declare a block invalid here.
125+ Logger . error ( "[PendingBlocks] Error downloading blobs: #{ inspect ( reason ) } " )
126+ { :ok , store }
127+ end
128+
95129 ##########################
96130 ### Private Functions
97131 ##########################
@@ -184,51 +218,4 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do
184218 Logger . error ( "[PendingBlocks] Error downloading block: #{ inspect ( reason ) } " )
185219 { :ok , store }
186220 end
187-
188- def process_blobs ( store , { :ok , blobs } ) , do: { :ok , add_blobs ( store , blobs ) }
189-
190- def process_blobs ( store , { :error , reason } ) do
191- # We might want to declare a block invalid here.
192- Logger . error ( "[PendingBlocks] Error downloading blobs: #{ inspect ( reason ) } " )
193- { :ok , store }
194- end
195-
196- def add_blob ( store , blob ) , do: add_blobs ( store , [ blob ] )
197-
198- # To be used when a series of blobs are downloaded. Stores each blob.
199- # If there are blocks that can be processed, does so immediately.
200- defp add_blobs ( store , blobs ) do
201- blobs
202- |> Enum . map ( & BlobDb . store_blob / 1 )
203- |> Enum . uniq ( )
204- |> Enum . reduce ( store , fn root , store ->
205- with % BlockInfo { status: :download_blobs } = block_info <- Blocks . get_block_info ( root ) ,
206- [ ] <- missing_blobs ( block_info ) do
207- block_info
208- |> Blocks . change_status ( :pending )
209- |> then ( & process_block_and_check_children ( store , & 1 ) )
210- else
211- _ ->
212- store
213- end
214- end )
215- end
216-
217- @ spec missing_blobs ( BlockInfo . t ( ) ) :: [ Types.BlobIdentifier . t ( ) ]
218- def missing_blobs ( % BlockInfo { root: root , signed_block: signed_block } ) do
219- signed_block . message . body . blob_kzg_commitments
220- |> Stream . with_index ( )
221- |> Enum . filter ( & blob_needs_download? ( & 1 , root ) )
222- |> Enum . map ( & % Types.BlobIdentifier { block_root: root , index: elem ( & 1 , 1 ) } )
223- end
224-
225- defp blob_needs_download? ( { commitment , index } , block_root ) do
226- case BlobDb . get_blob_sidecar ( block_root , index ) do
227- { :ok , % { kzg_commitment: ^ commitment } } ->
228- false
229-
230- _ ->
231- true
232- end
233- end
234221end
0 commit comments