Skip to content

Commit

Permalink
Update protocol params (#1271)
Browse files Browse the repository at this point in the history
* Update protocol params

* More updates

* Fixes
  • Loading branch information
thibault-martinez authored Sep 22, 2023
1 parent 64e6b85 commit f3c819f
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 92 deletions.
40 changes: 20 additions & 20 deletions sdk/src/types/block/mana/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,52 +16,52 @@ use crate::types::block::{slot::EpochIndex, Error};
#[getset(get_copy = "pub")]
pub struct ManaStructure {
/// The number of bits used to represent Mana.
pub(crate) mana_bits_count: u8,
pub(crate) bits_count: u8,
/// The amount of potential Mana generated by 1 IOTA in 1 slot.
pub(crate) mana_generation_rate: u8,
pub(crate) generation_rate: u8,
/// The scaling of `mana_generation_rate` expressed as an exponent of 2.
pub(crate) mana_generation_rate_exponent: u8,
pub(crate) generation_rate_exponent: u8,
/// A lookup table of epoch index diff to mana decay factor.
#[packable(unpack_error_with = |_| Error::InvalidManaDecayFactors)]
#[cfg_attr(feature = "serde", serde(with = "crate::utils::serde::boxed_slice_prefix"))]
#[getset(skip)]
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: u8,
pub(crate) decay_factors: BoxedSlicePrefix<u32, u16>,
/// The scaling of `decay_factors` expressed as an exponent of 2.
pub(crate) decay_factors_exponent: u8,
/// An integer approximation of the sum of decay over epochs.
pub(crate) mana_decay_factor_epochs_sum: u32,
pub(crate) 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: u8,
pub(crate) decay_factor_epochs_sum_exponent: u8,
}

impl ManaStructure {
/// Returns the mana decay factors slice.
pub fn mana_decay_factors(&self) -> &[u32] {
&self.mana_decay_factors
pub fn decay_factors(&self) -> &[u32] {
&self.decay_factors
}

/// Returns the mana decay factor for the given epoch index.
pub fn mana_decay_factor_at(&self, epoch_index: EpochIndex) -> Option<u32> {
self.mana_decay_factors.get(*epoch_index as usize).copied()
pub fn decay_factor_at(&self, epoch_index: EpochIndex) -> Option<u32> {
self.decay_factors.get(*epoch_index as usize).copied()
}

/// Returns the max mana that can exist with the mana bits defined.
pub fn max_mana(&self) -> u64 {
(1 << self.mana_bits_count) - 1
(1 << self.bits_count) - 1
}
}

impl Default for ManaStructure {
fn default() -> Self {
// TODO: use actual values
Self {
mana_bits_count: 10,
mana_generation_rate: Default::default(),
mana_generation_rate_exponent: Default::default(),
mana_decay_factors: Default::default(),
mana_decay_factors_exponent: Default::default(),
mana_decay_factor_epochs_sum: Default::default(),
mana_decay_factor_epochs_sum_exponent: Default::default(),
bits_count: 10,
generation_rate: Default::default(),
generation_rate_exponent: Default::default(),
decay_factors: Default::default(),
decay_factors_exponent: Default::default(),
decay_factor_epochs_sum: Default::default(),
decay_factor_epochs_sum_exponent: Default::default(),
}
}
}
19 changes: 13 additions & 6 deletions sdk/src/types/block/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ pub struct ProtocolParameters {
pub(crate) staking_unbonding_period: EpochIndex,
/// The number of validation blocks that each validator should issue each slot.
pub(crate) validation_blocks_per_slot: u16,
/// The number of epochs worth of Mana that a node is punished with for each additional validation block it issues.
#[cfg_attr(feature = "serde", serde(with = "crate::utils::serde::string"))]
pub(crate) punishment_epochs: u64,
/// 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,
Expand Down Expand Up @@ -100,6 +103,7 @@ impl Default for ProtocolParameters {
mana_structure: Default::default(),
staking_unbonding_period: 10.into(),
validation_blocks_per_slot: 10,
punishment_epochs: 9,
liveness_threshold: 5.into(),
min_committable_age: 10.into(),
max_committable_age: 20.into(),
Expand Down Expand Up @@ -180,7 +184,7 @@ impl ProtocolParameters {
#[getset(get_copy = "pub")]
pub struct WorkScoreStructure {
/// Modifier for network traffic per byte.
data_kilobyte: u32,
data_byte: u32,
/// Modifier for work done to process a block.
block: u32,
/// Modifier for slashing when there are insufficient strong tips.
Expand Down Expand Up @@ -208,7 +212,7 @@ pub struct WorkScoreStructure {
impl Default for WorkScoreStructure {
fn default() -> Self {
Self {
data_kilobyte: 0,
data_byte: 0,
block: 100,
missing_parent: 500,
input: 20,
Expand All @@ -234,9 +238,9 @@ impl Default for WorkScoreStructure {
#[packable(unpack_error = Error)]
#[getset(get_copy = "pub")]
pub struct CongestionControlParameters {
/// Minimum value of the RMC.
/// Minimum value of the reference Mana cost.
#[cfg_attr(feature = "serde", serde(with = "crate::utils::serde::string"))]
rmc_min: u64,
min_reference_mana_cost: u64,
/// Increase step size of the RMC.
#[cfg_attr(feature = "serde", serde(with = "crate::utils::serde::string"))]
increase: u64,
Expand All @@ -252,21 +256,24 @@ pub struct CongestionControlParameters {
/// Minimum amount of Mana that an account must have to schedule a block.
#[cfg_attr(feature = "serde", serde(with = "crate::utils::serde::string"))]
min_mana: u64,
/// Maximum size of the buffer. TODO what buffer?
/// Maximum size of the buffer in the scheduler.
max_buffer_size: u32,
/// Maximum number of blocks in the validation buffer.
max_validation_buffer_size: u32,
}

impl Default for CongestionControlParameters {
fn default() -> Self {
Self {
rmc_min: 500,
min_reference_mana_cost: 500,
increase: 500,
decrease: 500,
increase_threshold: 800000,
decrease_threshold: 500000,
scheduler_rate: 100000,
min_mana: 1,
max_buffer_size: 3276800,
max_validation_buffer_size: 100,
}
}
}
Expand Down
1 change: 0 additions & 1 deletion sdk/src/types/block/slot/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use crate::types::block::slot::{RootsId, SlotCommitmentId, SlotIndex};
)]
pub struct SlotCommitment {
// The version of the protocol running.
#[cfg_attr(feature = "serde", serde(rename = "version"))]
protocol_version: u8,
/// The slot index of this commitment.
/// It is calculated based on genesis timestamp and the duration of a slot.
Expand Down
146 changes: 82 additions & 64 deletions sdk/tests/types/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,97 @@
// SPDX-License-Identifier: Apache-2.0

use iota_sdk::types::block::protocol::ProtocolParameters;
use packable::PackableExt;

#[test]
fn params_serde_hash() {
// Data from https://github.com/iotaledger/tips-draft/blob/tip49/tips/TIP-0049/tip-0049.md#protocol-parameter-example
// Test from https://github.com/iotaledger/tips-draft/blob/tip49/tips/TIP-0049/tip-0049.md#protocol-parameter-example
let protocol_params_json = serde_json::json!(
{
"type": 0,
"version": 3,
"networkName": "xxxNetwork",
"bech32Hrp": "xxx",
"rentStructure": {
"vByteCost": 6,
"vByteFactorData": 7,
"vByteFactorKey": 8,
"vByteFactorBlockIssuerKey": 9,
"vByteFactorStakingFeature": 10,
"vByteFactorDelegation": 10
},
"workScoreStructure": {
"dataKilobyte": 1,
"block": 2,
"missingParent": 3,
"input": 4,
"contextInput": 5,
"output": 6,
"nativeToken": 7,
"staking": 8,
"blockIssuer": 9,
"allotment": 10,
"signatureEd25519": 11,
"minStrongParentsThreshold": 12
},
"tokenSupply": "1234567890987654321",
"genesisUnixTimestamp": "1681373293",
"slotDurationInSeconds": 10,
"slotsPerEpochExponent": 13,
"manaStructure": {
"manaBitsCount": 1,
"manaGenerationRate": 1,
"manaGenerationRateExponent": 27,
"manaDecayFactors": [ 10, 20 ],
"manaDecayFactorsExponent": 32,
"manaDecayFactorEpochsSum": 1337,
"manaDecayFactorEpochsSumExponent": 20
},
"stakingUnbondingPeriod": "11",
"validationBlocksPerSlot": 10,
"livenessThreshold": "3",
"minCommittableAge": "10",
"maxCommittableAge": "20",
"epochNearingThreshold": "24",
"congestionControlParameters": {
"rmcMin": "500",
"increase": "500",
"decrease": "500",
"increaseThreshold": 800000,
"decreaseThreshold": 500000,
"schedulerRate": 100000,
"minMana": "1",
"maxBufferSize": 3276800
},
"versionSignaling": {
"windowSize": 3,
"windowTargetRatio": 4,
"activationOffset": 1
}
}
{
"type":0,
"version":3,
"networkName":"xxxNetwork",
"bech32Hrp":"xxx",
"rentStructure": {
"vByteCost":6,
"vByteFactorData":7,
"vByteFactorKey":8,
"vByteFactorBlockIssuerKey":9,
"vByteFactorStakingFeature":10,
"vByteFactorDelegation":10
},
"workScoreStructure":{
"dataByte":1,
"block":2,
"missingParent":3,
"input":4,
"contextInput":5,
"output":6,
"nativeToken":7,
"staking":8,
"blockIssuer":9,
"allotment":10,
"signatureEd25519":11,
"minStrongParentsThreshold":12
},
"tokenSupply":"1234567890987654321",
"genesisUnixTimestamp":"1681373293",
"slotDurationInSeconds":10,
"slotsPerEpochExponent":13,
"manaStructure": {
"bitsCount":1,
"generationRate":1,
"generationRateExponent":27,
"decayFactors":[10,20],
"decayFactorsExponent":32,
"decayFactorEpochsSum":1337,
"decayFactorEpochsSumExponent":20
},
"stakingUnbondingPeriod":"11",
"validationBlocksPerSlot":10,
"punishmentEpochs":"9",
"livenessThreshold":"3",
"minCommittableAge":"10",
"maxCommittableAge":"20",
"epochNearingThreshold":"24",
"congestionControlParameters": {
"minReferenceManaCost":"500",
"increase":"500",
"decrease":"500",
"increaseThreshold":800000,
"decreaseThreshold":500000,
"schedulerRate":100000,
"minMana":"1",
"maxBufferSize":1000,
"maxValidationBufferSize":100
},
"versionSignaling": {
"windowSize":3,
"windowTargetRatio":4,
"activationOffset":1
}
}
);
let protocol_params = serde_json::from_value::<ProtocolParameters>(protocol_params_json).unwrap();
let protocol_params_bytes = protocol_params.pack_to_vec();

assert_eq!(
protocol_params_bytes,
[
0, 3, 10, 120, 120, 120, 78, 101, 116, 119, 111, 114, 107, 3, 120, 120, 120, 6, 0, 0, 0, 7, 8, 9, 10, 10,
1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5, 0, 0, 0, 6, 0, 0, 0, 7, 0, 0, 0, 8, 0, 0, 0, 9, 0, 0, 0,
10, 0, 0, 0, 11, 0, 0, 0, 12, 177, 28, 108, 177, 244, 16, 34, 17, 109, 184, 55, 100, 0, 0, 0, 0, 10, 13, 1,
1, 27, 2, 0, 10, 0, 0, 0, 20, 0, 0, 0, 32, 57, 5, 0, 0, 20, 11, 0, 0, 0, 0, 0, 0, 0, 10, 0, 9, 0, 0, 0, 0,
0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0,
244, 1, 0, 0, 0, 0, 0, 0, 244, 1, 0, 0, 0, 0, 0, 0, 244, 1, 0, 0, 0, 0, 0, 0, 0, 53, 12, 0, 32, 161, 7, 0,
160, 134, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 232, 3, 0, 0, 100, 0, 0, 0, 3, 4, 1
]
);

let hash = protocol_params.hash();

assert_eq!(
hash.to_string(),
"0xd379bdceb68aa77dada50ae7e3493b8f0b6ed28d26813620ce893afad541eb29"
"0x9d3e39699e38db1d6e6777a40f82b1b5030e645cffb08dba3a157105bd6bfac8"
);
}
2 changes: 1 addition & 1 deletion sdk/tests/types/slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn slot_commitment_id() {
// Test from https://github.com/iotaledger/tips-draft/blob/tip46/tips/TIP-0046/tip-0046.md#slot-commitment-id-1

let slot_commitment_json = serde_json::json!({
"version": 3,
"protocolVersion": 3,
"index": "10",
"previousCommitmentId": "0x4b024b3e47280d05272a7d136f0c464e4e136b734e6c427749413e286162077560652c007e37241a",
"rootsId": "0x75614402763f5f045c040334631b791b4d755d626d504b134a505c001c516549",
Expand Down

0 comments on commit f3c819f

Please sign in to comment.