Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions src/beacon/drand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use backon::{ExponentialBuilder, Retryable};
use bls_signatures::Serialize as _;
use nonzero_ext::nonzero;
use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize};
use smallvec::SmallVec;
use tracing::debug;
use url::Url;

Expand Down Expand Up @@ -88,10 +89,11 @@ impl BeaconSchedule {
if cb_epoch != pb_epoch {
// Fork logic, take entries from the last two rounds of the new beacon.
let round = curr_beacon.max_beacon_round_for_epoch(network_version, epoch);
let mut entries = Vec::with_capacity(2);
entries.push(curr_beacon.entry(round - 1).await?);
entries.push(curr_beacon.entry(round).await?);
return Ok(entries);

let mut out: SmallVec::<[BeaconEntry; 2]> = SmallVec::new();
out.push(curr_beacon.entry(round - 1).await?);
out.push(curr_beacon.entry(round).await?);
return Ok(out.to_vec());
}
}

Expand All @@ -113,21 +115,30 @@ impl BeaconSchedule {
prev.round()
};

// We only ever need one entry after drand quicknet upgrade (FIP-0063)
let mut out: SmallVec::<[BeaconEntry; 2]> = SmallVec::new();
Comment thread
EclesioMeloJunior marked this conversation as resolved.
Outdated
if curr_beacon.network().is_unchained() {
let entry = curr_beacon.entry(max_round).await?;
Ok(vec![entry])
for covered_epoch in (parent_epoch + 1)..=epoch {
let round = curr_beacon.max_beacon_round_for_epoch(network_version, covered_epoch);
out.push(
curr_beacon
.entry(round)
.await
.context(format!(
Comment thread
EclesioMeloJunior marked this conversation as resolved.
Outdated
"failed to fetch beacon entry for epoch {covered_epoch}, round {round}"
))?,
);
}
Ok(out.to_vec())
Comment thread
EclesioMeloJunior marked this conversation as resolved.
Outdated
} else {
let mut cur = max_round;
let mut out = Vec::new();
while cur > prev_round {
// Push all entries from rounds elapsed since the last chain epoch.
let entry = curr_beacon.entry(cur).await?;
cur = entry.round() - 1;
out.push(entry);
}
out.reverse();
Ok(out)
Ok(out.to_vec())
Comment thread
EclesioMeloJunior marked this conversation as resolved.
Outdated
Comment thread
EclesioMeloJunior marked this conversation as resolved.
Outdated
}
}

Expand Down
37 changes: 36 additions & 1 deletion src/beacon/tests/drand.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
// Copyright 2019-2026 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use itertools::Itertools;

use crate::{
beacon::{Beacon, ChainInfo, DrandBeacon, DrandConfig, DrandNetwork},
beacon::{
Beacon, BeaconEntry, BeaconPoint, BeaconSchedule, ChainInfo, DrandBeacon, DrandConfig,
DrandNetwork,
},
shim::version::NetworkVersion,
};
use std::borrow::Cow;
Expand Down Expand Up @@ -144,3 +149,33 @@ fn test_max_beacon_round_for_epoch_quicknet() {
((1598306400 + 3547000 * 30) - 1692803367 - 30) / 3 + 1
);
}

#[tokio::test]
async fn beacon_entries_for_block_covers_null_rounds_quicknet() {
// (parent epoch, its beacon round, block epoch, expected rounds)
let cases = [
// Null round at 6216199: entries for both 6216199 and 6216200.
(6216198, 30662982, 6216200, vec![30662992, 30663002]),
// No null round in between: only 6216200's entry.
(6216199, 30662992, 6216200, vec![30663002]),
];

let schedule = BeaconSchedule(vec![BeaconPoint::new(0, new_beacon_quicknet())]);

for (prev_epoch, prev_epoch_round, epoch, expected_rounds) in cases {
let (_, prev_beacon) = schedule.beacon_for_epoch(prev_epoch).unwrap();
let prev_beacon_entry = prev_beacon.entry(prev_epoch_round).await.unwrap();

let entries = schedule
.beacon_entries_for_block(NetworkVersion::V22, epoch, prev_epoch, &prev_beacon_entry)
.await
.unwrap();

let rounds: Vec<u64> = entries.iter().map(BeaconEntry::round).collect_vec();
Comment thread
EclesioMeloJunior marked this conversation as resolved.
Outdated

assert_eq!(
rounds, expected_rounds,
"epoch {epoch}, parent {prev_epoch}"
);
}
}
Loading