Skip to content

Commit d41a594

Browse files
authored
Merge pull request #84 from Blobscan/feat/store-last-synced-block-data
feat: use last synced block slot and root
2 parents 01ac237 + 699eac3 commit d41a594

File tree

4 files changed

+47
-25
lines changed

4 files changed

+47
-25
lines changed

src/clients/blobscan/types.rs

+14
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ pub struct BlockchainSyncStateRequest {
6868
pub last_upper_synced_slot: Option<u32>,
6969
#[serde(default, skip_serializing_if = "Option::is_none")]
7070
pub last_finalized_block: Option<u32>,
71+
#[serde(default, skip_serializing_if = "Option::is_none")]
72+
pub last_upper_synced_block_root: Option<B256>,
73+
#[serde(default, skip_serializing_if = "Option::is_none")]
74+
pub last_upper_synced_block_slot: Option<u32>,
7175
}
7276

7377
#[derive(Deserialize, Debug)]
@@ -77,13 +81,19 @@ pub struct BlockchainSyncStateResponse {
7781
pub last_lower_synced_slot: Option<u32>,
7882
#[serde(default, skip_serializing_if = "Option::is_none")]
7983
pub last_upper_synced_slot: Option<u32>,
84+
#[serde(default, skip_serializing_if = "Option::is_none")]
85+
pub last_upper_synced_block_root: Option<B256>,
86+
#[serde(default, skip_serializing_if = "Option::is_none")]
87+
pub last_upper_synced_block_slot: Option<u32>,
8088
}
8189

8290
#[derive(Debug, PartialEq)]
8391
pub struct BlockchainSyncState {
8492
pub last_finalized_block: Option<u32>,
8593
pub last_lower_synced_slot: Option<u32>,
8694
pub last_upper_synced_slot: Option<u32>,
95+
pub last_upper_synced_block_root: Option<B256>,
96+
pub last_upper_synced_block_slot: Option<u32>,
8797
}
8898

8999
#[derive(Serialize, Debug)]
@@ -253,6 +263,8 @@ impl From<BlockchainSyncStateResponse> for BlockchainSyncState {
253263
last_finalized_block: None,
254264
last_lower_synced_slot: response.last_lower_synced_slot,
255265
last_upper_synced_slot: response.last_upper_synced_slot,
266+
last_upper_synced_block_root: response.last_upper_synced_block_root,
267+
last_upper_synced_block_slot: response.last_upper_synced_block_slot,
256268
}
257269
}
258270
}
@@ -263,6 +275,8 @@ impl From<BlockchainSyncState> for BlockchainSyncStateRequest {
263275
last_lower_synced_slot: sync_state.last_lower_synced_slot,
264276
last_upper_synced_slot: sync_state.last_upper_synced_slot,
265277
last_finalized_block: sync_state.last_finalized_block,
278+
last_upper_synced_block_root: sync_state.last_upper_synced_block_root,
279+
last_upper_synced_block_slot: sync_state.last_upper_synced_block_slot,
266280
}
267281
}
268282
}

src/indexer/event_handlers/finalized_checkpoint.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@ where
7171
self.context
7272
.blobscan_client()
7373
.update_sync_state(BlockchainSyncState {
74+
last_finalized_block: Some(last_finalized_block_number),
7475
last_lower_synced_slot: None,
7576
last_upper_synced_slot: None,
76-
last_finalized_block: Some(last_finalized_block_number),
77+
last_upper_synced_block_root: None,
78+
last_upper_synced_block_slot: None,
7779
})
7880
.await
7981
.map_err(FinalizedCheckpointEventHandlerError::BlobscanFinalizedBlockUpdateFailure)?;

src/indexer/mod.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -111,24 +111,24 @@ impl Indexer<ReqwestTransport> {
111111
},
112112
};
113113

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,
114+
let last_synced_block = sync_state.as_ref().and_then(|state| {
115+
match (
116+
state.last_upper_synced_block_root,
117+
state.last_upper_synced_slot,
118+
) {
119+
(Some(root), Some(slot)) => Some(BlockHeader {
120120
parent_root: B256::ZERO,
121-
})
121+
root,
122+
slot,
123+
}),
124+
_ => None,
125+
}
122126
});
123127

124-
let upper_indexed_block_id = match &last_synced_block {
125-
Some(block) => block.slot.into(),
126-
None => BlockId::Head,
127-
};
128-
129128
info!(
130-
lower_indexed_block_id = current_lower_block_id.to_string(),
131-
upper_indexed_block_id = upper_indexed_block_id.to_string(),
129+
last_lowest_synced_slot = ?current_lower_block_id,
130+
last_upper_synced_block_slot = ?last_synced_block.as_ref().map(|block| block.slot),
131+
last_upper_synced_block_root = ?last_synced_block.as_ref().map(|block| block.root),
132132
"Starting indexer…",
133133
);
134134

src/synchronizer/mod.rs

+16-10
Original file line numberDiff line numberDiff line change
@@ -262,16 +262,20 @@ impl Synchronizer<ReqwestTransport> {
262262
});
263263

264264
if self.checkpoint_type != CheckpointType::Disabled {
265-
let last_lower_synced_slot = if self.checkpoint_type == CheckpointType::Lower {
266-
last_slot
267-
} else {
268-
None
269-
};
270-
let last_upper_synced_slot = if self.checkpoint_type == CheckpointType::Upper {
271-
last_slot
272-
} else {
273-
None
274-
};
265+
let mut last_lower_synced_slot = None;
266+
let mut last_upper_synced_slot = None;
267+
let mut last_upper_synced_block_root = None;
268+
let mut last_upper_synced_block_slot = None;
269+
270+
if self.checkpoint_type == CheckpointType::Lower {
271+
last_lower_synced_slot = last_slot;
272+
} else if self.checkpoint_type == CheckpointType::Upper {
273+
last_upper_synced_slot = last_slot;
274+
last_upper_synced_block_root =
275+
self.last_synced_block.as_ref().map(|block| block.root);
276+
last_upper_synced_block_slot =
277+
self.last_synced_block.as_ref().map(|block| block.slot);
278+
}
275279

276280
if let Err(error) = self
277281
.context
@@ -280,6 +284,8 @@ impl Synchronizer<ReqwestTransport> {
280284
last_finalized_block: None,
281285
last_lower_synced_slot,
282286
last_upper_synced_slot,
287+
last_upper_synced_block_root,
288+
last_upper_synced_block_slot,
283289
})
284290
.await
285291
{

0 commit comments

Comments
 (0)