Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanwhit authored and atodorov committed Jul 17, 2023
1 parent b0874d3 commit 0ebb5cc
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 26 deletions.
1 change: 1 addition & 0 deletions bin/node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ mod tests {
&parent_hash,
parent_number,
slot.into(),
0.into(),
)
.unwrap()
.unwrap();
Expand Down
27 changes: 20 additions & 7 deletions client/consensus/babe/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl Environment<TestBlock> for DummyFactory {
type Error = Error;

fn init(&mut self, parent_header: &<TestBlock as BlockT>::Header) -> Self::CreateProposer {
let parent_slot = crate::find_pre_digest::<TestBlock>(parent_header)
let parent_slot = crate::find_pre_digest::<TestBlock>(parent_header, &0.into())
.expect("parent header has a pre-digest")
.slot();

Expand Down Expand Up @@ -131,7 +131,7 @@ impl DummyProposer {
Err(e) => return future::ready(Err(e)),
};

let this_slot = crate::find_pre_digest::<TestBlock>(block.header())
let this_slot = crate::find_pre_digest::<TestBlock>(block.header(), &0.into())
.expect("baked block has valid pre-digest")
.slot();

Expand All @@ -145,6 +145,7 @@ impl DummyProposer {
self.parent_number,
this_slot,
|slot| Epoch::genesis(&self.factory.config, slot),
0.into(),
)
.expect("client has data to find epoch")
.expect("can compute epoch for baked block");
Expand Down Expand Up @@ -450,7 +451,7 @@ async fn run_one_test(mutator: impl Fn(&mut TestHeader, Stage) + Send + Sync + '
// this test fail.
let parent_header = client_clone.header(parent).ok().flatten().unwrap();
let slot = Slot::from(
find_pre_digest::<TestBlock>(&parent_header).unwrap().slot() + 1,
find_pre_digest::<TestBlock>(&parent_header, &0.into()).unwrap().slot() + 1,
);

async move { Ok((InherentDataProvider::new(slot),)) }
Expand Down Expand Up @@ -693,7 +694,7 @@ async fn propose_and_import_block<Transaction: Send + 'static>(
let mut proposer = proposer_factory.init(parent).await.unwrap();

let slot = slot.unwrap_or_else(|| {
let parent_pre_digest = find_pre_digest::<TestBlock>(parent).unwrap();
let parent_pre_digest = find_pre_digest::<TestBlock>(parent, &0.into()).unwrap();
parent_pre_digest.slot() + 1
});

Expand All @@ -716,6 +717,7 @@ async fn propose_and_import_block<Transaction: Send + 'static>(
&parent_hash,
*parent.number(),
slot,
0.into(),
)
.unwrap()
.unwrap();
Expand Down Expand Up @@ -806,9 +808,14 @@ async fn importing_block_one_sets_genesis_epoch() {

let epoch_changes = data.link.epoch_changes.shared_data();
let epoch_for_second_block = epoch_changes
.epoch_data_for_child_of(descendent_query(&*client), &block_hash, 1, 1000.into(), |slot| {
Epoch::genesis(&data.link.config, slot)
})
.epoch_data_for_child_of(
descendent_query(&*client),
&block_hash,
1,
1000.into(),
|slot| Epoch::genesis(&data.link.config, slot),
0.into(),
)
.unwrap()
.unwrap();

Expand Down Expand Up @@ -1307,6 +1314,7 @@ async fn allows_skipping_epochs() {
epoch_length + 2,
(epoch_length * 3 + 2).into(),
|slot| Epoch::genesis(&data.link.config, slot),
0.into(),
)
.unwrap()
.unwrap();
Expand All @@ -1323,6 +1331,7 @@ async fn allows_skipping_epochs() {
epoch_length + 2,
(epoch_length * 4 + 1).into(),
|slot| Epoch::genesis(&data.link.config, slot),
0.into(),
)
.unwrap()
.unwrap();
Expand Down Expand Up @@ -1383,6 +1392,7 @@ async fn allows_skipping_epochs_on_some_forks() {
epoch_length + 2,
(epoch_length * 2 + 2).into(),
|slot| Epoch::genesis(&data.link.config, slot),
0.into(),
)
.unwrap()
.unwrap();
Expand All @@ -1407,6 +1417,7 @@ async fn allows_skipping_epochs_on_some_forks() {
epoch_length + 2,
(epoch_length * 3 + 2).into(),
|slot| Epoch::genesis(&data.link.config, slot),
0.into(),
)
.unwrap()
.unwrap();
Expand All @@ -1425,6 +1436,7 @@ async fn allows_skipping_epochs_on_some_forks() {
epoch_length + 2,
(epoch_length * 4 + 1).into(),
|slot| Epoch::genesis(&data.link.config, slot),
0.into(),
)
.unwrap()
.unwrap();
Expand All @@ -1442,6 +1454,7 @@ async fn allows_skipping_epochs_on_some_forks() {
epoch_length + 2,
(epoch_length * 4 + 1).into(),
|slot| Epoch::genesis(&data.link.config, slot),
0.into(),
)
.unwrap()
.unwrap();
Expand Down
51 changes: 39 additions & 12 deletions client/consensus/epochs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ mod tests {

let epoch_changes = EpochChanges::<_, _, Epoch>::new();
let genesis_epoch = epoch_changes
.epoch_descriptor_for_child_of(&is_descendent_of, b"0", 0, 10101)
.epoch_descriptor_for_child_of(&is_descendent_of, b"0", 0, 10101, 0.into())
.unwrap()
.unwrap();

Expand All @@ -807,7 +807,7 @@ mod tests {
};

let genesis_epoch_2 = epoch_changes
.epoch_descriptor_for_child_of(&is_descendent_of, b"0", 0, 10102)
.epoch_descriptor_for_child_of(&is_descendent_of, b"0", 0, 10102, 0.into())
.unwrap()
.unwrap();

Expand Down Expand Up @@ -839,7 +839,7 @@ mod tests {

let mut epoch_changes = EpochChanges::<_, _, Epoch>::new();
let genesis_epoch = epoch_changes
.epoch_descriptor_for_child_of(&is_descendent_of, b"0", 0, 100)
.epoch_descriptor_for_child_of(&is_descendent_of, b"0", 0, 100, 0.into())
.unwrap()
.unwrap();

Expand All @@ -862,7 +862,14 @@ mod tests {
{
// x is still within the genesis epoch.
let x = epoch_changes
.epoch_data_for_child_of(&is_descendent_of, b"A", 1, end_slot - 1, &make_genesis)
.epoch_data_for_child_of(
&is_descendent_of,
b"A",
1,
end_slot - 1,
&make_genesis,
0.into(),
)
.unwrap()
.unwrap();

Expand All @@ -873,7 +880,14 @@ mod tests {
// x is now at the next epoch, because the block is now at the
// start slot of epoch 1.
let x = epoch_changes
.epoch_data_for_child_of(&is_descendent_of, b"A", 1, end_slot, &make_genesis)
.epoch_data_for_child_of(
&is_descendent_of,
b"A",
1,
end_slot,
&make_genesis,
0.into(),
)
.unwrap()
.unwrap();

Expand All @@ -890,6 +904,7 @@ mod tests {
1,
epoch_1.end_slot() - 1,
&make_genesis,
0.into(),
)
.unwrap()
.unwrap();
Expand Down Expand Up @@ -923,7 +938,7 @@ mod tests {
// insert genesis epoch for A
{
let genesis_epoch_a_descriptor = epoch_changes
.epoch_descriptor_for_child_of(&is_descendent_of, b"0", 0, 100)
.epoch_descriptor_for_child_of(&is_descendent_of, b"0", 0, 100, 0.into())
.unwrap()
.unwrap();

Expand All @@ -940,7 +955,7 @@ mod tests {
// insert genesis epoch for X
{
let genesis_epoch_x_descriptor = epoch_changes
.epoch_descriptor_for_child_of(&is_descendent_of, b"0", 0, 1000)
.epoch_descriptor_for_child_of(&is_descendent_of, b"0", 0, 1000, 0.into())
.unwrap()
.unwrap();

Expand All @@ -958,21 +973,21 @@ mod tests {
// respect the chain structure.
{
let epoch_for_a_child = epoch_changes
.epoch_data_for_child_of(&is_descendent_of, b"A", 1, 101, &make_genesis)
.epoch_data_for_child_of(&is_descendent_of, b"A", 1, 101, &make_genesis, 0.into())
.unwrap()
.unwrap();

assert_eq!(epoch_for_a_child, make_genesis(100));

let epoch_for_x_child = epoch_changes
.epoch_data_for_child_of(&is_descendent_of, b"X", 1, 1001, &make_genesis)
.epoch_data_for_child_of(&is_descendent_of, b"X", 1, 1001, &make_genesis, 0.into())
.unwrap()
.unwrap();

assert_eq!(epoch_for_x_child, make_genesis(1000));

let epoch_for_x_child_before_genesis = epoch_changes
.epoch_data_for_child_of(&is_descendent_of, b"X", 1, 101, &make_genesis)
.epoch_data_for_child_of(&is_descendent_of, b"X", 1, 101, &make_genesis, 0.into())
.unwrap();

// even though there is a genesis epoch at that slot, it's not in
Expand Down Expand Up @@ -1014,7 +1029,13 @@ mod tests {
let make_genesis = |slot| Epoch { start_slot: slot, duration: 100 };
// Get epoch descriptor valid for 'slot'
let epoch_descriptor = epoch_changes
.epoch_descriptor_for_child_of(&is_descendent_of, parent_hash, parent_number, slot)
.epoch_descriptor_for_child_of(
&is_descendent_of,
parent_hash,
parent_number,
slot,
0.into(),
)
.unwrap()
.unwrap();
// Increment it
Expand Down Expand Up @@ -1111,7 +1132,13 @@ mod tests {
let make_genesis = |slot| Epoch { start_slot: slot, duration: 5 };
// Get epoch descriptor valid for 'slot'
let epoch_descriptor = epoch_changes
.epoch_descriptor_for_child_of(&is_descendent_of, parent_hash, parent_number, slot)
.epoch_descriptor_for_child_of(
&is_descendent_of,
parent_hash,
parent_number,
slot,
0.into(),
)
.unwrap()
.unwrap();
// Increment it
Expand Down
7 changes: 4 additions & 3 deletions client/consensus/manual-seal/src/consensus/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ impl SlotTimestampProvider {
let slot_duration = sc_consensus_babe::configuration(&*client)?.slot_duration();

let time = Self::with_header(&client, slot_duration, |header| {
let slot_number = *sc_consensus_babe::find_pre_digest::<B>(&header, &sp_core::U256::zero())
.map_err(|err| format!("{}", err))?
.slot();
let slot_number =
*sc_consensus_babe::find_pre_digest::<B>(&header, &sp_core::U256::zero())
.map_err(|err| format!("{}", err))?
.slot();
Ok(slot_number)
})?;

Expand Down
5 changes: 1 addition & 4 deletions frame/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ impl From<&pallet::GenesisConfig> for InitConfig {
}
}


#[frame_support::pallet]
pub mod pallet {
use super::*;
Expand Down Expand Up @@ -905,9 +904,7 @@ impl<T: Config> Pallet<T> {
pub fn genesis_init(config: crate::InitConfig) {
SegmentIndex::<T>::put(0);
Pallet::<T>::initialize_genesis_authorities(&config.authorities);
EpochConfig::<T>::put(
config.epoch_config.clone().expect("epoch_config must not be None"),
);
EpochConfig::<T>::put(config.epoch_config.clone().expect("epoch_config must not be None"));
}
}

Expand Down

0 comments on commit 0ebb5cc

Please sign in to comment.