Skip to content

Commit bff7882

Browse files
committed
refactor: simplify block ID usage by implementing the From trait and avoiding reference passing
1 parent bb13ab5 commit bff7882

File tree

7 files changed

+34
-32
lines changed

7 files changed

+34
-32
lines changed

Diff for: src/clients/beacon/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ pub struct Config {
3535
#[async_trait]
3636
#[cfg_attr(test, automock)]
3737
pub trait CommonBeaconClient: Send + Sync + Debug {
38-
async fn get_block(&self, block_id: &BlockId) -> ClientResult<Option<Block>>;
39-
async fn get_block_header(&self, block_id: &BlockId) -> ClientResult<Option<BlockHeader>>;
40-
async fn get_blobs(&self, block_id: &BlockId) -> ClientResult<Option<Vec<Blob>>>;
38+
async fn get_block(&self, block_id: BlockId) -> ClientResult<Option<Block>>;
39+
async fn get_block_header(&self, block_id: BlockId) -> ClientResult<Option<BlockHeader>>;
40+
async fn get_blobs(&self, block_id: BlockId) -> ClientResult<Option<Vec<Blob>>>;
4141
fn subscribe_to_events(&self, topics: &[Topic]) -> ClientResult<EventSource>;
4242
}
4343

@@ -57,7 +57,7 @@ impl BeaconClient {
5757

5858
#[async_trait]
5959
impl CommonBeaconClient for BeaconClient {
60-
async fn get_block(&self, block_id: &BlockId) -> ClientResult<Option<Block>> {
60+
async fn get_block(&self, block_id: BlockId) -> ClientResult<Option<Block>> {
6161
let path = format!("v2/beacon/blocks/{}", { block_id.to_detailed_string() });
6262
let url = self.base_url.join(path.as_str())?;
6363

@@ -67,7 +67,7 @@ impl CommonBeaconClient for BeaconClient {
6767
})
6868
}
6969

70-
async fn get_block_header(&self, block_id: &BlockId) -> ClientResult<Option<BlockHeader>> {
70+
async fn get_block_header(&self, block_id: BlockId) -> ClientResult<Option<BlockHeader>> {
7171
let path = format!("v1/beacon/headers/{}", { block_id.to_detailed_string() });
7272
let url = self.base_url.join(path.as_str())?;
7373

@@ -83,7 +83,7 @@ impl CommonBeaconClient for BeaconClient {
8383
})
8484
}
8585

86-
async fn get_blobs(&self, block_id: &BlockId) -> ClientResult<Option<Vec<Blob>>> {
86+
async fn get_blobs(&self, block_id: BlockId) -> ClientResult<Option<Vec<Blob>>> {
8787
let path = format!("v1/beacon/blob_sidecars/{}", {
8888
block_id.to_detailed_string()
8989
});

Diff for: src/clients/beacon/types.rs

+6
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ impl From<B256> for BlockId {
170170
}
171171
}
172172

173+
impl From<u32> for BlockId {
174+
fn from(value: u32) -> Self {
175+
BlockId::Slot(value)
176+
}
177+
}
178+
173179
impl From<BlockHeaderResponse> for BlockHeader {
174180
fn from(response: BlockHeaderResponse) -> Self {
175181
BlockHeader {

Diff for: src/indexer/event_handlers/finalized_checkpoint.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ use tracing::info;
33

44
use crate::{
55
clients::{
6-
beacon::types::{BlockId, FinalizedCheckpointEventData},
7-
blobscan::types::BlockchainSyncState,
6+
beacon::types::FinalizedCheckpointEventData, blobscan::types::BlockchainSyncState,
87
common::ClientError,
98
},
109
context::CommonContext,
@@ -46,7 +45,7 @@ where
4645
let last_finalized_block_number = match self
4746
.context
4847
.beacon_client()
49-
.get_block(&BlockId::Hash(block_hash))
48+
.get_block(block_hash.into())
5049
.await
5150
.map_err(|err| {
5251
FinalizedCheckpointEventHandlerError::BlockRetrievalError(

Diff for: src/indexer/event_handlers/head.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ where
6666
head_block_id.clone()
6767
};
6868

69-
let head_block_header = self.get_block_header(&head_block_id).await?;
69+
let head_block_header = self.get_block_header(head_block_id).await?;
7070

7171
if let Some(last_block_hash) = self.last_block_hash {
7272
if last_block_hash != head_block_header.parent_root {
7373
let parent_block_header = self
74-
.get_block_header(&head_block_header.parent_root.into())
74+
.get_block_header(head_block_header.parent_root.into())
7575
.await?;
7676
let parent_block_slot = parent_block_header.slot;
7777
let reorg_start_slot = parent_block_slot + 1;
@@ -91,8 +91,8 @@ where
9191
// Re-index parent block as it may be mark as reorged and not indexed
9292
self.synchronizer
9393
.run(
94-
&BlockId::Slot(parent_block_slot),
95-
&BlockId::Slot(parent_block_slot + 1),
94+
parent_block_slot.into(),
95+
(parent_block_slot + 1).into(),
9696
)
9797
.await?;
9898

@@ -118,7 +118,7 @@ where
118118
}
119119

120120
self.synchronizer
121-
.run(&initial_block_id, &BlockId::Slot(head_block_slot + 1))
121+
.run(initial_block_id, (head_block_slot + 1).into())
122122
.await?;
123123

124124
self.last_block_hash = Some(head_block_hash);
@@ -128,12 +128,12 @@ where
128128

129129
async fn get_block_header(
130130
&self,
131-
block_id: &BlockId,
131+
block_id: BlockId,
132132
) -> Result<BlockHeader, HeadEventHandlerError> {
133133
match self
134134
.context
135135
.beacon_client()
136-
.get_block_header(block_id)
136+
.get_block_header(block_id.clone())
137137
.await
138138
.map_err(|err| {
139139
HeadEventHandlerError::BlockHeaderRetrievalError(block_id.clone(), err)

Diff for: src/indexer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl Indexer<ReqwestTransport> {
184184
let historical_syc_thread_span = tracing::info_span!("indexer:historical");
185185

186186
let result: Result<(), IndexerError> = async move {
187-
let result = synchronizer.run(&start_block_id, &end_block_id).await;
187+
let result = synchronizer.run(start_block_id, end_block_id).await;
188188

189189
if let Err(error) = result {
190190
tx.send(IndexerTaskMessage::Error(

Diff for: src/slots_processor/mod.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ use anyhow::{anyhow, Context as AnyhowContext, Result};
44
use tracing::{debug, info};
55

66
use crate::{
7-
clients::{
8-
beacon::types::BlockId,
9-
blobscan::types::{Blob, Block, Transaction},
10-
},
7+
clients::blobscan::types::{Blob, Block, Transaction},
118
context::CommonContext,
129
};
1310

@@ -59,7 +56,7 @@ impl SlotsProcessor<ReqwestTransport> {
5956
let blobscan_client = self.context.blobscan_client();
6057
let provider = self.context.provider();
6158

62-
let beacon_block = match beacon_client.get_block(&BlockId::Slot(slot)).await? {
59+
let beacon_block = match beacon_client.get_block(slot.into()).await? {
6360
Some(block) => block,
6461
None => {
6562
debug!(slot = slot, "Skipping as there is no beacon block");
@@ -113,7 +110,7 @@ impl SlotsProcessor<ReqwestTransport> {
113110
// Fetch blobs and perform some checks
114111

115112
let blobs = match beacon_client
116-
.get_blobs(&BlockId::Slot(slot))
113+
.get_blobs(slot.into())
117114
.await
118115
.map_err(SlotProcessingError::ClientError)?
119116
{

Diff for: src/synchronizer/mod.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ pub mod error;
2525
pub trait CommonSynchronizer: Send + Sync {
2626
async fn run(
2727
&self,
28-
initial_block_id: &BlockId,
29-
final_block_id: &BlockId,
28+
initial_block_id: BlockId,
29+
final_block_id: BlockId,
3030
) -> Result<(), SynchronizerError>;
3131
}
3232

@@ -286,12 +286,12 @@ impl Synchronizer<ReqwestTransport> {
286286
Ok(())
287287
}
288288

289-
async fn resolve_to_slot(&self, block_id: &BlockId) -> Result<u32, SynchronizerError> {
289+
async fn resolve_to_slot(&self, block_id: BlockId) -> Result<u32, SynchronizerError> {
290290
let beacon_client = self.context.beacon_client();
291291

292292
let resolved_block_id: Result<u32, ClientError> = match block_id {
293-
BlockId::Slot(slot) => Ok(*slot),
294-
_ => match beacon_client.get_block_header(block_id).await {
293+
BlockId::Slot(slot) => Ok(slot),
294+
_ => match beacon_client.get_block_header(block_id.clone()).await {
295295
Ok(None) => {
296296
let err = anyhow!("Block ID {} not found", block_id);
297297

@@ -316,11 +316,11 @@ impl Synchronizer<ReqwestTransport> {
316316
impl CommonSynchronizer for Synchronizer<ReqwestTransport> {
317317
async fn run(
318318
&self,
319-
initial_block_id: &BlockId,
320-
final_block_id: &BlockId,
319+
initial_block_id: BlockId,
320+
final_block_id: BlockId,
321321
) -> Result<(), SynchronizerError> {
322322
let initial_slot = self.resolve_to_slot(initial_block_id).await?;
323-
let mut final_slot = self.resolve_to_slot(final_block_id).await?;
323+
let mut final_slot = self.resolve_to_slot(final_block_id.clone()).await?;
324324

325325
if initial_slot == final_slot {
326326
return Ok(());
@@ -330,7 +330,7 @@ impl CommonSynchronizer for Synchronizer<ReqwestTransport> {
330330
self.sync_slots_by_checkpoints(initial_slot, final_slot)
331331
.await?;
332332

333-
let latest_final_slot = self.resolve_to_slot(final_block_id).await?;
333+
let latest_final_slot = self.resolve_to_slot(final_block_id.clone()).await?;
334334

335335
if final_slot == latest_final_slot {
336336
return Ok(());

0 commit comments

Comments
 (0)