Skip to content
Permalink
Browse files

add ticking speed in seconds

  • Loading branch information...
vincenthz committed May 15, 2019
1 parent 323c2e4 commit 978a8cf4f6fef770ef81adfd97029ee724f6d27b
@@ -58,15 +58,16 @@ pub enum ConfigParam {
AllowAccountCreation(bool),
LinearFee(LinearFee),
ProposalExpiration(u32),
KESUpdateSpeed(u32),
}

// Discriminants can NEVER be 1024 or higher
#[derive(AsRefStr, Clone, Copy, Debug, EnumIter, EnumString, FromPrimitive, PartialEq)]
enum Tag {
#[strum(to_string = "block0-date")]
Block0Date = 1,
#[strum(to_string = "discrimination")]
Discrimination = 2,
Discrimination = 1,
#[strum(to_string = "block0-date")]
Block0Date = 2,
#[strum(to_string = "block0-consensus")]
ConsensusVersion = 3,
#[strum(to_string = "slots-per-epoch")]
@@ -91,6 +92,8 @@ enum Tag {
LinearFee = 14,
#[strum(to_string = "proposal-expiration")]
ProposalExpiration = 15,
#[strum(to_string = "kes-update-speed")]
KESUpdateSpeed = 16,
}

impl<'a> From<&'a ConfigParam> for Tag {
@@ -112,6 +115,7 @@ impl<'a> From<&'a ConfigParam> for Tag {
ConfigParam::AllowAccountCreation(_) => Tag::AllowAccountCreation,
ConfigParam::LinearFee(_) => Tag::LinearFee,
ConfigParam::ProposalExpiration(_) => Tag::ProposalExpiration,
ConfigParam::KESUpdateSpeed(_) => Tag::KESUpdateSpeed,
}
}
}
@@ -157,6 +161,9 @@ impl Readable for ConfigParam {
Tag::ProposalExpiration => {
ConfigParamVariant::from_payload(bytes).map(ConfigParam::ProposalExpiration)
}
Tag::KESUpdateSpeed => {
ConfigParamVariant::from_payload(bytes).map(ConfigParam::KESUpdateSpeed)
}
}
.map_err(Into::into)
}
@@ -182,6 +189,7 @@ impl property::Serialize for ConfigParam {
ConfigParam::AllowAccountCreation(data) => data.to_payload(),
ConfigParam::LinearFee(data) => data.to_payload(),
ConfigParam::ProposalExpiration(data) => data.to_payload(),
ConfigParam::KESUpdateSpeed(data) => data.to_payload(),
};
let taglen = TagLen::new(tag, bytes.len()).ok_or_else(|| {
io::Error::new(
@@ -22,6 +22,7 @@ pub struct LedgerStaticParameters {
pub block0_initial_hash: HeaderHash,
pub block0_start_time: config::Block0Date,
pub discrimination: Discrimination,
pub kes_update_speed: u32,
}

// parameters to validate ledger
@@ -73,6 +74,7 @@ pub enum Block0Error {
InitialMessageNoConsensusVersion,
InitialMessageNoConsensusLeaderId,
InitialMessageNoPraosActiveSlotsCoeff,
InitialMessageNoKesUpdateSpeed,
UtxoTotalValueTooBig,
HasUpdateProposal,
HasUpdateVote,
@@ -192,6 +194,7 @@ impl Ledger {
let mut slot_duration = None;
let mut discrimination = None;
let mut slots_per_epoch = None;
let mut kes_update_speed = None;

for param in init_ents.iter() {
match param {
@@ -207,6 +210,9 @@ impl Ledger {
ConfigParam::SlotsPerEpoch(n) => {
slots_per_epoch = Some(*n);
}
ConfigParam::KESUpdateSpeed(n) => {
kes_update_speed = Some(*n);
}
_ => regular_ents.push(param.clone()),
}
}
@@ -220,11 +226,14 @@ impl Ledger {
slot_duration.ok_or(Error::Block0(Block0Error::InitialMessageNoSlotDuration))?;
let slots_per_epoch =
slots_per_epoch.ok_or(Error::Block0(Block0Error::InitialMessageNoSlotsPerEpoch))?;
let kes_update_speed =
kes_update_speed.ok_or(Error::Block0(Block0Error::InitialMessageNoKesUpdateSpeed))?;

let static_params = LedgerStaticParameters {
block0_initial_hash,
block0_start_time: block0_start_time,
discrimination: discrimination,
kes_update_speed: kes_update_speed,
};

let system_time = SystemTime::UNIX_EPOCH + Duration::from_secs(block0_start_time.0);
@@ -820,13 +829,18 @@ pub mod test {
};
}

#[test]
pub fn utxo() {
// create an initial fake ledger with the non-optional parameter setup
pub fn create_initial_fake_ledger(
discrimination: Discrimination,
initial_msgs: &[Message],
) -> (HeaderHash, Ledger) {
let block0_hash = HeaderHash::hash_bytes(&[1, 2, 3]);
let discrimination = Discrimination::Test;

let mut ie = config::ConfigParams::new();
ie.push(ConfigParam::Discrimination(Discrimination::Test));
ie.push(ConfigParam::Discrimination(discrimination));
ie.push(ConfigParam::ConsensusVersion(ConsensusVersion::Bft));

// TODO remove rng: make this creation deterministic
let leader_pub_key = SecretKey::generate(rand::thread_rng()).to_public();
ie.push(ConfigParam::AddBftLeader(leader_pub_key.into()));
ie.push(ConfigParam::Block0Date(crate::config::Block0Date(0)));
@@ -835,6 +849,21 @@ pub mod test {
Milli::HALF,
));
ie.push(ConfigParam::SlotsPerEpoch(21600));
ie.push(ConfigParam::KESUpdateSpeed(3600 * 12));

let mut messages = Vec::new();
messages.push(Message::Initial(ie));
messages.extend_from_slice(initial_msgs);

let ledger =
Ledger::new(block0_hash, &messages).expect("create initial fake ledger failed");

(block0_hash, ledger)
}

#[test]
pub fn utxo() {
let discrimination = Discrimination::Test;

let mut rng = rand::thread_rng();
let (sk1, _pk1, user1_address) = make_key(&mut rng, &discrimination);
@@ -862,8 +891,9 @@ pub mod test {
value: value,
};

let messages = [Message::Initial(ie), Message::Transaction(first_trans)];
let ledger = Ledger::new(block0_hash, &messages).unwrap();
let messages = [Message::Transaction(first_trans)];
let (block0_hash, ledger) = create_initial_fake_ledger(discrimination, &messages);

let dyn_params = ledger.get_ledger_parameters();

{
@@ -314,6 +314,7 @@ mod test {
ents.push(ConfigParam::AddBftLeader(LeaderId::from(leader_pub_key)));
ents.push(ConfigParam::Block0Date(Block0Date(0)));
ents.push(ConfigParam::SlotDuration(10));
ents.push(ConfigParam::KESUpdateSpeed(12 * 3600));
ents.push(ConfigParam::ConsensusGenesisPraosActiveSlotsCoeff(
Milli::HALF,
));
@@ -63,7 +63,9 @@ impl Settings {

for param in changes.iter() {
match param {
ConfigParam::Block0Date(_) | ConfigParam::Discrimination(_) => {
ConfigParam::Block0Date(_)
| ConfigParam::Discrimination(_)
| ConfigParam::KESUpdateSpeed(_) => {
return Err(Error::ReadOnlySetting);
}
ConfigParam::ConsensusVersion(d) => {

0 comments on commit 978a8cf

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