Skip to content

Commit d17c24b

Browse files
authored
Merge pull request #83 from Blobscan/refactor/simplify-beacon-block-response
refactor: flatten block response from beacon client
2 parents 358d584 + 7ff8fec commit d17c24b

File tree

6 files changed

+50
-40
lines changed

6 files changed

+50
-40
lines changed

src/clients/beacon/mod.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,8 @@ impl CommonBeaconClient for BeaconClient {
6161
let path = format!("v2/beacon/blocks/{}", { block_id.to_detailed_string() });
6262
let url = self.base_url.join(path.as_str())?;
6363

64-
json_get!(&self.client, url, BlockResponse, self.exp_backoff.clone()).map(|res| match res {
65-
Some(r) => Some(r.data),
66-
None => None,
67-
})
64+
json_get!(&self.client, url, BlockResponse, self.exp_backoff.clone())
65+
.map(|res| res.map(|r| r.into()))
6866
}
6967

7068
async fn get_block_header(&self, block_id: BlockId) -> ClientResult<Option<BlockHeader>> {
@@ -77,10 +75,7 @@ impl CommonBeaconClient for BeaconClient {
7775
BlockHeaderResponse,
7876
self.exp_backoff.clone()
7977
)
80-
.map(|res| match res {
81-
Some(r) => Some(r.into()),
82-
None => None,
83-
})
78+
.map(|res| res.map(|r| r.into()))
8479
}
8580

8681
async fn get_blobs(&self, block_id: BlockId) -> ClientResult<Option<Vec<Blob>>> {

src/clients/beacon/types.rs

+23-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ pub enum Topic {
2222
FinalizedCheckpoint,
2323
}
2424

25+
#[derive(Deserialize, Debug)]
26+
pub struct Block {
27+
pub blob_kzg_commitments: Option<Vec<String>>,
28+
pub execution_payload: Option<ExecutionPayload>,
29+
pub parent_root: B256,
30+
#[serde(deserialize_with = "deserialize_number")]
31+
pub slot: u32,
32+
}
33+
2534
#[derive(Deserialize, Debug)]
2635
pub struct ExecutionPayload {
2736
pub block_hash: B256,
@@ -43,13 +52,13 @@ pub struct BlockMessage {
4352
}
4453

4554
#[derive(Deserialize, Debug)]
46-
pub struct Block {
55+
pub struct BlockData {
4756
pub message: BlockMessage,
4857
}
4958

5059
#[derive(Deserialize, Debug)]
5160
pub struct BlockResponse {
52-
pub data: Block,
61+
pub data: BlockData,
5362
}
5463

5564
#[derive(Deserialize, Debug)]
@@ -194,6 +203,17 @@ impl From<BlockHeaderResponse> for BlockHeader {
194203
}
195204
}
196205

206+
impl From<BlockResponse> for Block {
207+
fn from(response: BlockResponse) -> Self {
208+
Block {
209+
blob_kzg_commitments: response.data.message.body.blob_kzg_commitments,
210+
execution_payload: response.data.message.body.execution_payload,
211+
parent_root: response.data.message.parent_root,
212+
slot: response.data.message.slot,
213+
}
214+
}
215+
}
216+
197217
#[derive(Debug, thiserror::Error)]
198218
pub enum BlockIdResolutionError {
199219
#[error("Block with id '{0}' not found")]
@@ -221,7 +241,7 @@ impl BlockIdResolution for BlockId {
221241
match self {
222242
BlockId::Slot(slot) => Ok(*slot),
223243
_ => match beacon_client
224-
.get_block_header(self.clone().into())
244+
.get_block_header(self.clone())
225245
.await
226246
.map_err(|err| BlockIdResolutionError::FailedBlockIdResolution {
227247
block_id: self.clone(),

src/indexer/event_handlers/finalized_checkpoint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ where
5353
err,
5454
)
5555
})? {
56-
Some(block) => match block.message.body.execution_payload {
56+
Some(block) => match block.execution_payload {
5757
Some(execution_payload) => execution_payload.block_number,
5858
None => {
5959
return Err(FinalizedCheckpointEventHandlerError::BlockNotFound(

src/indexer/mod.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,15 @@ impl Indexer<ReqwestTransport> {
111111
},
112112
};
113113

114-
let last_synced_block = sync_state
115-
.map(|state| {
116-
state
117-
.last_upper_synced_slot
118-
.map(|last_upper_synced_slot| BlockHeader {
119-
slot: last_upper_synced_slot,
120-
root: B256::ZERO,
121-
parent_root: B256::ZERO,
122-
})
123-
})
124-
.flatten();
114+
let last_synced_block = sync_state.and_then(|state| {
115+
state
116+
.last_upper_synced_slot
117+
.map(|last_upper_synced_slot| BlockHeader {
118+
slot: last_upper_synced_slot,
119+
root: B256::ZERO,
120+
parent_root: B256::ZERO,
121+
})
122+
});
125123

126124
let upper_indexed_block_id = match &last_synced_block {
127125
Some(block) => block.slot.into(),

src/slots_processor/helpers.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,11 @@ pub fn create_tx_hash_versioned_hashes_mapping(
1212
let mut tx_to_versioned_hashes = HashMap::new();
1313

1414
if let Some(transactions) = block.transactions.as_transactions() {
15-
transactions
16-
.iter()
17-
.for_each(|tx| match &tx.blob_versioned_hashes {
18-
Some(versioned_hashes) => {
19-
tx_to_versioned_hashes.insert(tx.hash, versioned_hashes.clone());
20-
}
21-
None => {}
22-
});
15+
transactions.iter().for_each(|tx| {
16+
if let Some(versioned_hashes) = tx.blob_versioned_hashes.as_ref() {
17+
tx_to_versioned_hashes.insert(tx.hash, versioned_hashes.clone());
18+
}
19+
});
2320
}
2421

2522
Ok(tx_to_versioned_hashes)

src/slots_processor/mod.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl SlotsProcessor<ReqwestTransport> {
145145
}
146146
};
147147

148-
let execution_payload = match beacon_block.message.body.execution_payload {
148+
let execution_payload = match beacon_block.execution_payload {
149149
Some(payload) => payload,
150150
None => {
151151
debug!(
@@ -157,7 +157,7 @@ impl SlotsProcessor<ReqwestTransport> {
157157
}
158158
};
159159

160-
let has_kzg_blob_commitments = match beacon_block.message.body.blob_kzg_commitments {
160+
let has_kzg_blob_commitments = match beacon_block.blob_kzg_commitments {
161161
Some(commitments) => !commitments.is_empty(),
162162
None => false,
163163
};
@@ -324,7 +324,7 @@ impl SlotsProcessor<ReqwestTransport> {
324324
reorg_depth += 1;
325325
}
326326

327-
Err(anyhow!("No common block found").into())
327+
Err(anyhow!("No common block found"))
328328
}
329329

330330
/// Returns the path of blocks with execution payload from the head block to the provided block.
@@ -343,30 +343,30 @@ impl SlotsProcessor<ReqwestTransport> {
343343
}
344344
};
345345

346-
if let Some(execution_payload) = &canonical_block.message.body.execution_payload {
346+
if let Some(execution_payload) = &canonical_block.execution_payload {
347347
if execution_payload.block_hash == blobscan_block.hash {
348348
return Ok(vec![]);
349349
}
350350
}
351351

352352
let mut current_canonical_block_root = head_block_root;
353353

354-
while canonical_block.message.parent_root != B256::ZERO {
355-
let canonical_block_parent_root = canonical_block.message.parent_root;
354+
while canonical_block.parent_root != B256::ZERO {
355+
let canonical_block_parent_root = canonical_block.parent_root;
356356

357-
if canonical_block.message.slot < blobscan_block.slot {
357+
if canonical_block.slot < blobscan_block.slot {
358358
return Ok(vec![]);
359359
}
360360

361-
if let Some(execution_payload) = &canonical_block.message.body.execution_payload {
361+
if let Some(execution_payload) = &canonical_block.execution_payload {
362362
if execution_payload.block_hash == blobscan_block.hash {
363363
return Ok(canonical_execution_blocks);
364364
}
365365

366366
canonical_execution_blocks.push(BlockData {
367367
root: current_canonical_block_root,
368368
parent_root: canonical_block_parent_root,
369-
slot: canonical_block.message.slot,
369+
slot: canonical_block.slot,
370370
execution_block_hash: execution_payload.block_hash,
371371
});
372372
}

0 commit comments

Comments
 (0)