Skip to content
This repository has been archived by the owner on Jun 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #688 from input-output-hk/kes-speed
Browse files Browse the repository at this point in the history
add ticking speed in seconds
  • Loading branch information
vincenthz committed May 15, 2019
2 parents c84a2a5 + c482f17 commit 740f972
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 12 deletions.
5 changes: 3 additions & 2 deletions chain-impl-mockchain/doc/format.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ The following parameter types exist:

| tag | name | value type | description |
|:-------|:-------|:------------|:--------------|
| 1 | block0-date | u64 | the official start time of the blockchain, in seconds since the Unix epoch |
| 2 | discrimination | u8 | address discrimination; 1 for production, 2 for testing |
| 1 | discrimination | u8 | address discrimination; 1 for production, 2 for testing |
| 2 | block0-date | u64 | the official start time of the blockchain, in seconds since the Unix epoch |
| 3 | consensus| u16 | consensus version; 1 for BFT, 2 for Genesis Praos |
| 4 | slots-per-epoch | u32 | number of slots in an epoch |
| 5 | slot-duration | u8 | slot duration in seconds |
Expand All @@ -203,6 +203,7 @@ The following parameter types exist:
| 13 | allow-account-creation | bool (u8) | 0 to enable account creation, 1 to disable |
| 14 | linear-fee | LinearFee | coefficients for fee calculations |
| 15 | proposal-expiration | u32 | number of epochs until an update proposal expires |
| 16 | kes-update-speed | u32 | maximum number of seconds per update for KES keys known by the system after start time |

`Milli` is a 64-bit entity that encoded a non-negative, fixed-point
number with a scaling factor of 1000. That is, the number 1.234 is
Expand Down
14 changes: 11 additions & 3 deletions chain-impl-mockchain/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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 {
Expand All @@ -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,
}
}
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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(
Expand Down
42 changes: 36 additions & 6 deletions chain-impl-mockchain/src/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -73,6 +74,7 @@ pub enum Block0Error {
InitialMessageNoConsensusVersion,
InitialMessageNoConsensusLeaderId,
InitialMessageNoPraosActiveSlotsCoeff,
InitialMessageNoKesUpdateSpeed,
UtxoTotalValueTooBig,
HasUpdateProposal,
HasUpdateVote,
Expand Down Expand Up @@ -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 {
Expand All @@ -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()),
}
}
Expand All @@ -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);
Expand Down Expand Up @@ -824,13 +833,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)));
Expand All @@ -839,6 +853,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);
Expand Down Expand Up @@ -866,8 +895,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();

{
Expand Down
1 change: 1 addition & 0 deletions chain-impl-mockchain/src/multiverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
));
Expand Down
4 changes: 3 additions & 1 deletion chain-impl-mockchain/src/setting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 740f972

Please sign in to comment.