Skip to content

Commit

Permalink
refactor: flatten block response from beacon client
Browse files Browse the repository at this point in the history
  • Loading branch information
PJColombo committed Feb 11, 2025
1 parent f3ea424 commit 01f2e97
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/clients/beacon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl CommonBeaconClient for BeaconClient {
let url = self.base_url.join(path.as_str())?;

json_get!(&self.client, url, BlockResponse, self.exp_backoff.clone()).map(|res| match res {
Some(r) => Some(r.data),
Some(r) => Some(r.into()),
None => None,
})
}
Expand Down
24 changes: 22 additions & 2 deletions src/clients/beacon/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ pub enum Topic {
FinalizedCheckpoint,
}

#[derive(Deserialize, Debug)]
pub struct Block {
pub blob_kzg_commitments: Option<Vec<String>>,
pub execution_payload: Option<ExecutionPayload>,
pub parent_root: B256,
#[serde(deserialize_with = "deserialize_number")]
pub slot: u32,
}

#[derive(Deserialize, Debug)]
pub struct ExecutionPayload {
pub block_hash: B256,
Expand All @@ -43,13 +52,13 @@ pub struct BlockMessage {
}

#[derive(Deserialize, Debug)]
pub struct Block {
pub struct BlockData {
pub message: BlockMessage,
}

#[derive(Deserialize, Debug)]
pub struct BlockResponse {
pub data: Block,
pub data: BlockData,
}

#[derive(Deserialize, Debug)]
Expand Down Expand Up @@ -194,6 +203,17 @@ impl From<BlockHeaderResponse> for BlockHeader {
}
}

impl From<BlockResponse> for Block {
fn from(response: BlockResponse) -> Self {
Block {
blob_kzg_commitments: response.data.message.body.blob_kzg_commitments,
execution_payload: response.data.message.body.execution_payload,
parent_root: response.data.message.parent_root,
slot: response.data.message.slot,
}
}
}

#[derive(Debug, thiserror::Error)]
pub enum BlockIdResolutionError {
#[error("Block with id '{0}' not found")]
Expand Down
2 changes: 1 addition & 1 deletion src/indexer/event_handlers/finalized_checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ where
err,
)
})? {
Some(block) => match block.message.body.execution_payload {
Some(block) => match block.execution_payload {
Some(execution_payload) => execution_payload.block_number,
None => {
return Err(FinalizedCheckpointEventHandlerError::BlockNotFound(
Expand Down
16 changes: 8 additions & 8 deletions src/slots_processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl SlotsProcessor<ReqwestTransport> {
}
};

let execution_payload = match beacon_block.message.body.execution_payload {
let execution_payload = match beacon_block.execution_payload {
Some(payload) => payload,
None => {
debug!(
Expand All @@ -157,7 +157,7 @@ impl SlotsProcessor<ReqwestTransport> {
}
};

let has_kzg_blob_commitments = match beacon_block.message.body.blob_kzg_commitments {
let has_kzg_blob_commitments = match beacon_block.blob_kzg_commitments {
Some(commitments) => !commitments.is_empty(),
None => false,
};
Expand Down Expand Up @@ -350,30 +350,30 @@ impl SlotsProcessor<ReqwestTransport> {
}
};

if let Some(execution_payload) = &canonical_block.message.body.execution_payload {
if let Some(execution_payload) = &canonical_block.execution_payload {
if execution_payload.block_hash == blobscan_block.hash {
return Ok(vec![]);
}
}

let mut current_canonical_block_root = head_block_root;

while canonical_block.message.parent_root != B256::ZERO {
let canonical_block_parent_root = canonical_block.message.parent_root;
while canonical_block.parent_root != B256::ZERO {
let canonical_block_parent_root = canonical_block.parent_root;

if canonical_block.message.slot < blobscan_block.slot {
if canonical_block.slot < blobscan_block.slot {
return Ok(vec![]);
}

if let Some(execution_payload) = &canonical_block.message.body.execution_payload {
if let Some(execution_payload) = &canonical_block.execution_payload {
if execution_payload.block_hash == blobscan_block.hash {
return Ok(canonical_execution_blocks);
}

canonical_execution_blocks.push(BlockData {
root: current_canonical_block_root,
parent_root: canonical_block_parent_root,
slot: canonical_block.message.slot,
slot: canonical_block.slot,
execution_block_hash: execution_payload.block_hash,
});
}
Expand Down

0 comments on commit 01f2e97

Please sign in to comment.