Skip to content

Commit

Permalink
Fix protocol parameters integer types (#1094)
Browse files Browse the repository at this point in the history
* Fix protocol parameters integer types

* Fix block ID test

* Update sdk/src/types/block/protocol.rs

Co-authored-by: Alexandcoats <[email protected]>

* Add doc

* Update sdk/src/types/block/protocol.rs

* Update sdk/src/types/block/protocol.rs

Co-authored-by: Alexandcoats <[email protected]>

* Update sdk/src/types/block/protocol.rs

Co-authored-by: Alexandcoats <[email protected]>

* Update sdk/src/types/block/protocol.rs

Co-authored-by: Alexandcoats <[email protected]>

* Nits

* Apply suggestions from code review

Co-authored-by: Alexandcoats <[email protected]>

* sad format noises

---------

Co-authored-by: Alexandcoats <[email protected]>
  • Loading branch information
thibault-martinez and Alexandcoats authored Aug 29, 2023
1 parent 144959d commit 766f040
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
40 changes: 23 additions & 17 deletions sdk/src/types/block/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,39 +45,41 @@ pub struct ProtocolParameters {
pub(crate) token_supply: u64,
/// Genesis timestamp at which the slots start to count.
#[serde(with = "crate::utils::serde::string")]
pub(crate) genesis_unix_timestamp: u32,
pub(crate) genesis_unix_timestamp: u64,
/// Duration of each slot in seconds.
pub(crate) slot_duration_in_seconds: u8,
/// The number of slots in an epoch expressed as an exponent of 2.
pub(crate) slots_per_epoch_exponent: u32,
pub(crate) slots_per_epoch_exponent: u8,
/// The amount of potential Mana generated by 1 IOTA in 1 slot.
pub(crate) mana_generation_rate: u32,
pub(crate) mana_generation_rate: u8,
/// The scaling of `mana_generation_rate` expressed as an exponent of 2.
pub(crate) mana_generation_rate_exponent: u32,
pub(crate) mana_generation_rate_exponent: u8,
#[packable(unpack_error_with = |_| Error::InvalidManaDecayFactors)]
#[getset(skip)]
/// A lookup table of epoch index diff to mana decay factor.
pub(crate) mana_decay_factors: BoxedSlicePrefix<u32, u8>,
pub(crate) mana_decay_factors: BoxedSlicePrefix<u32, u16>,
/// The scaling of `mana_decay_factors` expressed as an exponent of 2.
pub(crate) mana_decay_factors_exponent: u32,
pub(crate) mana_decay_factors_exponent: u8,
/// An integer approximation of the sum of decay over epochs.
pub(crate) mana_decay_factor_epochs_sum: u32,
/// The scaling of `mana_decay_factor_epochs_sum` expressed as an exponent of 2.
pub(crate) mana_decay_factor_epochs_sum_exponent: u32,
pub(crate) mana_decay_factor_epochs_sum_exponent: u8,
/// The unbonding period in epochs before an account can stop staking.
pub(crate) staking_unbonding_period: EpochIndex,
/// TODO
/// The slot index used by tip-selection to determine if a block is eligible by evaluating issuing times
/// and commitments in its past-cone against accepted tangle time and last committed slot respectively.
pub(crate) liveness_threshold: SlotIndex,
/// Minimum age relative to the accepted tangle time slot index that a slot can be committed.
pub(crate) min_committable_age: SlotIndex,
/// Maximum age for a slot commitment to be included in a block relative to the slot index of the block issuing
/// time.
pub(crate) max_committable_age: SlotIndex,
/// TODO
/// The slot index used by the epoch orchestrator to detect the slot that should trigger a new
/// committee selection for the next and upcoming epoch.
pub(crate) epoch_nearing_threshold: SlotIndex,
/// Parameters used to calculate the Reference Mana Cost (RMC).
pub(crate) congestion_control_parameters: CongestionControlParameters,
/// TODO
/// Defines the parameters used to signal a protocol parameters upgrade.
pub(crate) version_signaling: VersionSignalingParameters,
}

Expand Down Expand Up @@ -127,7 +129,7 @@ impl ProtocolParameters {
bech32_hrp: impl ConvertTo<Hrp>,
rent_structure: RentStructure,
token_supply: u64,
genesis_unix_timestamp: u32,
genesis_unix_timestamp: u64,
slot_duration_in_seconds: u8,
epoch_nearing_threshold: impl Into<SlotIndex>,
) -> Result<Self, Error> {
Expand Down Expand Up @@ -166,7 +168,7 @@ impl ProtocolParameters {

/// Returns the slots per epoch of the [`ProtocolParameters`].
pub fn slots_per_epoch(&self) -> u64 {
2_u64.pow(self.slots_per_epoch_exponent())
2_u64.pow(self.slots_per_epoch_exponent() as u32)
}

/// Gets a [`SlotIndex`] from a unix timestamp.
Expand Down Expand Up @@ -216,7 +218,7 @@ pub struct WorkScoreStructure {
/// Modifier for the block signature check.
signature_ed25519: u32,
/// The minimum count of strong parents in a basic block.
min_strong_parents_threshold: u32,
min_strong_parents_threshold: u8,
}

impl Default for WorkScoreStructure {
Expand Down Expand Up @@ -285,7 +287,7 @@ impl Default for CongestionControlParameters {
}
}

// TODO docs
/// Defines the parameters used to signal a protocol parameters upgrade.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Packable, CopyGetters)]
#[cfg_attr(
feature = "serde",
Expand All @@ -295,9 +297,13 @@ impl Default for CongestionControlParameters {
#[packable(unpack_error = Error)]
#[getset(get_copy = "pub")]
pub struct VersionSignalingParameters {
window_size: u32,
window_target_ratio: u32,
activation_offset: u32,
/// The size of the window in epochs that is used to find which version of protocol parameters was
/// most signaled, from `current_epoch - window_size` to `current_epoch`.
window_size: u8,
/// The number of supporters required for a version to win within a `window_size`.
window_target_ratio: u8,
/// The offset in epochs required to activate the new version of protocol parameters.
activation_offset: u8,
}

impl Default for VersionSignalingParameters {
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/slot/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl EpochIndex {
/// Gets the epoch index given a [`SlotIndex`].
pub fn from_slot_index(
slot_index: SlotIndex,
slots_per_epoch_exponent_iter: impl Iterator<Item = (EpochIndex, u32)>,
slots_per_epoch_exponent_iter: impl Iterator<Item = (EpochIndex, u8)>,
) -> Result<Self, Error> {
let mut slot_index = *slot_index;
let mut res = 0;
Expand Down
6 changes: 3 additions & 3 deletions sdk/src/types/block/slot/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ impl SlotIndex {
/// Gets the [`EpochIndex`] of this slot.
pub fn to_epoch_index(
self,
slots_per_epoch_exponent_iter: impl Iterator<Item = (EpochIndex, u32)>,
slots_per_epoch_exponent_iter: impl Iterator<Item = (EpochIndex, u8)>,
) -> Result<EpochIndex, Error> {
EpochIndex::from_slot_index(self, slots_per_epoch_exponent_iter)
}

/// Gets the slot index of a unix timestamp.
/// Slots are counted starting from `1` with `0` being reserved for times before the genesis.
pub fn from_timestamp(timestamp: u64, genesis_unix_timestamp: u32, slot_duration_in_seconds: u8) -> SlotIndex {
pub fn from_timestamp(timestamp: u64, genesis_unix_timestamp: u64, slot_duration_in_seconds: u8) -> SlotIndex {
timestamp
.checked_sub(genesis_unix_timestamp as u64)
.map(|diff| (diff / slot_duration_in_seconds as u64) + 1)
Expand All @@ -54,7 +54,7 @@ impl SlotIndex {

/// Converts the slot index into the unix timestamp representing the beginning of the slot.
/// Slot `0` will return the unix epoch.
pub fn to_timestamp(self, genesis_unix_timestamp: u32, slot_duration_in_seconds: u8) -> u64 {
pub fn to_timestamp(self, genesis_unix_timestamp: u64, slot_duration_in_seconds: u8) -> u64 {
self.0
.checked_sub(1)
.map(|adjusted_slot| (adjusted_slot * slot_duration_in_seconds as u64) + genesis_unix_timestamp as u64)
Expand Down
4 changes: 2 additions & 2 deletions sdk/tests/types/block_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ fn compute() {
// TODO: Independently verify this value
assert_eq!(
block_id.to_string(),
"0x0cf6544579470043791cadc15284fd231b577d63bb780f9f615e54761c2eb8b30b00000000000000"
"0xf0bd00c717f4a11d3a6381c4c06954b873c7bf1052a0b10465d04ba2de38bd420b00000000000000"
);
assert_eq!(
block_id.hash().to_string(),
"0x0cf6544579470043791cadc15284fd231b577d63bb780f9f615e54761c2eb8b3"
"0xf0bd00c717f4a11d3a6381c4c06954b873c7bf1052a0b10465d04ba2de38bd42"
);
assert_eq!(block_id.slot_index(), slot_index);
}

0 comments on commit 766f040

Please sign in to comment.