Skip to content

Commit dbae6e1

Browse files
committed
Query finalized root in storage too when performing peer status check
1 parent 846e07e commit dbae6e1

File tree

3 files changed

+75
-16
lines changed

3 files changed

+75
-16
lines changed

fork_choice_control/src/queries.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,11 @@ where
158158
}
159159
}
160160

161-
// TODO(Grandine Team): This will incorrectly return `None` for archived slots.
162-
#[must_use]
163-
pub fn finalized_block_root_before_or_at(&self, slot: Slot) -> Option<H256> {
164-
self.store_snapshot()
165-
.finalized_before_or_at(slot)
166-
.map(|chain_link| chain_link.block_root)
161+
pub fn finalized_block_root_before_or_at(&self, slot: Slot) -> Result<Option<H256>> {
162+
match self.store_snapshot().finalized_before_or_at(slot) {
163+
Some(chain_link) => Ok(Some(chain_link.block_root)),
164+
None => self.storage().block_root_before_or_at_slot(slot),
165+
}
167166
}
168167

169168
pub fn checkpoint_state(&self, checkpoint: Checkpoint) -> Result<Option<Arc<BeaconState<P>>>> {

fork_choice_control/src/storage.rs

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,21 @@ impl<P: Preset> Storage<P> {
534534
self.block_root_by_slot(slot)
535535
}
536536

537+
pub(crate) fn block_root_before_or_at_slot(&self, slot: Slot) -> Result<Option<H256>> {
538+
let results = self
539+
.database
540+
.iterator_descending(..=BlockRootBySlot(slot).to_string())?;
541+
542+
itertools::process_results(results, |pairs| {
543+
pairs
544+
.take_while(|(key_bytes, _)| BlockRootBySlot::has_prefix(key_bytes))
545+
.map(|(_, value_bytes)| H256::from_ssz_default(value_bytes))
546+
.next()
547+
.transpose()
548+
})?
549+
.map_err(Into::into)
550+
}
551+
537552
pub(crate) fn finalized_block_by_slot(
538553
&self,
539554
slot: Slot,
@@ -798,10 +813,8 @@ impl<P: Preset> Storage<P> {
798813

799814
itertools::process_results(results, |pairs| {
800815
pairs
801-
.take_while(|(key_bytes, _)| {
802-
FinalizedBlockByRoot::has_prefix(key_bytes)
803-
&& !UnfinalizedBlockByRoot::has_prefix(key_bytes)
804-
})
816+
.take_while(|(key_bytes, _)| FinalizedBlockByRoot::has_prefix(key_bytes))
817+
.filter(|(key_bytes, _)| !UnfinalizedBlockByRoot::has_prefix(key_bytes))
805818
.count()
806819
})
807820
}
@@ -1213,4 +1226,41 @@ mod tests {
12131226

12141227
Ok(())
12151228
}
1229+
1230+
#[test]
1231+
fn test_block_root_before_or_at_slot() -> Result<()> {
1232+
let database = Database::in_memory();
1233+
1234+
database.put_batch(vec![
1235+
serialize(BlockRootBySlot(2), H256::repeat_byte(2))?,
1236+
serialize(BlockRootBySlot(6), H256::repeat_byte(6))?,
1237+
])?;
1238+
1239+
let storage = Storage::<Mainnet>::new(
1240+
Arc::new(Config::mainnet()),
1241+
database,
1242+
nonzero!(64_u64),
1243+
StorageMode::Standard,
1244+
);
1245+
1246+
assert_eq!(storage.block_root_before_or_at_slot(1)?, None);
1247+
assert_eq!(
1248+
storage.block_root_before_or_at_slot(2)?,
1249+
Some(H256::repeat_byte(2)),
1250+
);
1251+
assert_eq!(
1252+
storage.block_root_before_or_at_slot(3)?,
1253+
Some(H256::repeat_byte(2)),
1254+
);
1255+
assert_eq!(
1256+
storage.block_root_before_or_at_slot(6)?,
1257+
Some(H256::repeat_byte(6)),
1258+
);
1259+
assert_eq!(
1260+
storage.block_root_before_or_at_slot(9)?,
1261+
Some(H256::repeat_byte(6)),
1262+
);
1263+
1264+
Ok(())
1265+
}
12161266
}

p2p/src/network.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,11 +1581,18 @@ impl<P: Preset> Network<P> {
15811581
Ordering::Greater => {
15821582
let remote_finalized_slot = Self::start_of_epoch(remote.finalized_epoch);
15831583

1584-
(
1585-
self.controller
1586-
.finalized_block_root_before_or_at(remote_finalized_slot),
1587-
SyncStatus::Behind { info },
1588-
)
1584+
let finalized_root_at_slot = match self
1585+
.controller
1586+
.finalized_block_root_before_or_at(remote_finalized_slot)
1587+
{
1588+
Ok(root) => root,
1589+
Err(error) => {
1590+
warn!("failed to query for finalized block root: {error:?}");
1591+
None
1592+
}
1593+
};
1594+
1595+
(finalized_root_at_slot, SyncStatus::Behind { info })
15891596
}
15901597
};
15911598

@@ -1606,7 +1613,10 @@ impl<P: Preset> Network<P> {
16061613

16071614
return;
16081615
}
1609-
} else if matches!(sync_status, SyncStatus::Behind { .. }) {
1616+
} else if matches!(sync_status, SyncStatus::Behind { .. })
1617+
&& local.finalized_root != H256::zero()
1618+
&& remote.finalized_root != H256::zero()
1619+
{
16101620
debug!(
16111621
"disconnecting peer {peer_id} due to missing historical data \
16121622
required to validate finalized root {:?} at epoch {}",

0 commit comments

Comments
 (0)