Skip to content
Permalink
Browse files

Merge pull request #680 from input-output-hk/nicolasdp/updates

small changes in APIs linked to time
  • Loading branch information...
NicolasDP committed May 13, 2019
2 parents e0616f1 + a2db307 commit 0ac5ac6327a648679dd2157592cf6e910125953b
@@ -25,7 +25,7 @@ impl BlockDate {

/// Get the slot following this one.
pub fn next(&self, era: &TimeEra) -> BlockDate {
let epoch_duration = era.slots_per_epoch;
let epoch_duration = era.slots_per_epoch();
assert!(self.slot_id < epoch_duration);
if self.slot_id + 1 == epoch_duration {
BlockDate {
@@ -145,6 +145,18 @@ impl Leadership {
}
}

/// get the epoch associated to the `Leadership`
#[inline]
pub fn epoch(&self) -> Epoch {
self.epoch
}

/// get the TimeEra associated to the `Leadership`
#[inline]
pub fn era(&self) -> &TimeEra {
&self.era
}

/// Verify whether this header has been produced by a leader that fits with the leadership
///
pub fn verify(&self, block_header: &Header) -> Verification {
@@ -68,6 +68,7 @@ pub enum Block0Error {
InitialMessageDuplicatePraosActiveSlotsCoeff,
InitialMessageNoDate,
InitialMessageNoSlotDuration,
InitialMessageNoSlotsPerEpoch,
InitialMessageNoDiscrimination,
InitialMessageNoConsensusVersion,
InitialMessageNoConsensusLeaderId,
@@ -190,6 +191,7 @@ impl Ledger {
let mut block0_start_time = None;
let mut slot_duration = None;
let mut discrimination = None;
let mut slots_per_epoch = None;

for param in init_ents.iter() {
match param {
@@ -202,6 +204,9 @@ impl Ledger {
ConfigParam::SlotDuration(d) => {
slot_duration = Some(*d);
}
ConfigParam::SlotsPerEpoch(n) => {
slots_per_epoch = Some(*n);
}
_ => regular_ents.push(param.clone()),
}
}
@@ -213,6 +218,8 @@ impl Ledger {
discrimination.ok_or(Error::Block0(Block0Error::InitialMessageNoDiscrimination))?;
let slot_duration =
slot_duration.ok_or(Error::Block0(Block0Error::InitialMessageNoSlotDuration))?;
let slots_per_epoch =
slots_per_epoch.ok_or(Error::Block0(Block0Error::InitialMessageNoSlotsPerEpoch))?;

let static_params = LedgerStaticParameters {
block0_initial_hash,
@@ -225,8 +232,7 @@ impl Ledger {
let tf = TimeFrame::new(timeline, SlotDuration::from_secs(slot_duration as u32));
let slot0 = tf.slot0();

// TODO -- configurable slots per epoch
let era = TimeEra::new_era(slot0, Epoch(0), 21600);
let era = TimeEra::new(slot0, Epoch(0), slots_per_epoch);

let settings = setting::Settings::new(era).apply(&regular_ents)?;

@@ -828,6 +834,7 @@ pub mod test {
ie.push(ConfigParam::ConsensusGenesisPraosActiveSlotsCoeff(
Milli::HALF,
));
ie.push(ConfigParam::SlotsPerEpoch(21600));

let mut rng = rand::thread_rng();
let (sk1, _pk1, user1_address) = make_key(&mut rng, &discrimination);
@@ -291,14 +291,15 @@ mod test {

#[test]
pub fn multiverse() {
const NUM_BLOCK_PER_EPOCH: u32 = 1000;
let mut multiverse = Multiverse::new();

let system_time = SystemTime::UNIX_EPOCH;
let timeline = Timeline::new(system_time);
let tf = TimeFrame::new(timeline, SlotDuration::from_secs(10));

let slot0 = tf.slot0();
let era = TimeEra::new_era(slot0, Epoch(0), 1000);
let era = TimeEra::new(slot0, Epoch(0), NUM_BLOCK_PER_EPOCH);

let mut g = StdGen::new(rand::thread_rng(), 10);
let leader_key = Arbitrary::arbitrary(&mut g);
@@ -316,6 +317,7 @@ mod test {
ents.push(ConfigParam::ConsensusGenesisPraosActiveSlotsCoeff(
Milli::HALF,
));
ents.push(ConfigParam::SlotsPerEpoch(NUM_BLOCK_PER_EPOCH));
genesis_block.message(Message::Initial(ents));
let genesis_block = genesis_block.make_genesis_block();
let mut date = genesis_block.date();
@@ -23,19 +23,24 @@ pub struct EpochPosition {
pub struct TimeEra {
epoch_start: Epoch,
slot_start: Slot,
pub slots_per_epoch: u32,
slots_per_epoch: u32,
}

impl TimeEra {
/// Set a new era to start on slot_start at epoch_start for a given slots per epoch.
pub fn new_era(slot_start: Slot, epoch_start: Epoch, slots_per_epoch: u32) -> Self {
pub fn new(slot_start: Slot, epoch_start: Epoch, slots_per_epoch: u32) -> Self {
TimeEra {
epoch_start,
slot_start,
slots_per_epoch,
}
}

/// retrieve the number of slots in an epoch during a given Epoch
pub fn slots_per_epoch(&self) -> u32 {
self.slots_per_epoch
}

/// Try to return the epoch/inner-epoch-slot associated.
///
/// If the slot in parameter is before the beginning of this era, then
@@ -92,7 +97,7 @@ mod test {
assert_eq!(slot2, Slot(4));
assert_eq!(slot3, Slot(20));

let era = TimeEra::new_era(slot1, Epoch(2), 4);
let era = TimeEra::new(slot1, Epoch(2), 4);

let p1 = era.from_slot_to_era(slot1).unwrap();
let p2 = era.from_slot_to_era(slot2).unwrap();

0 comments on commit 0ac5ac6

Please sign in to comment.
You can’t perform that action at this time.