-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mana structure and fix max mana checks (#1145)
* add mana structure and fix max mana checks * fix block ID test * suggestions * feature gate * renames --------- Co-authored-by: Thibault Martinez <[email protected]>
- Loading branch information
1 parent
bba0f9d
commit 01e0286
Showing
18 changed files
with
232 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2023 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use getset::CopyGetters; | ||
use packable::{prefix::BoxedSlicePrefix, Packable}; | ||
|
||
use crate::types::block::{slot::EpochIndex, Error}; | ||
|
||
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Packable, CopyGetters)] | ||
#[cfg_attr( | ||
feature = "serde", | ||
derive(serde::Serialize, serde::Deserialize), | ||
serde(rename_all = "camelCase") | ||
)] | ||
#[packable(unpack_error = Error)] | ||
#[getset(get_copy = "pub")] | ||
pub struct ManaStructure { | ||
/// The number of bits used to represent Mana. | ||
pub(crate) mana_bits_count: u8, | ||
/// The amount of potential Mana generated by 1 IOTA in 1 slot. | ||
pub(crate) mana_generation_rate: u8, | ||
/// The scaling of `mana_generation_rate` expressed as an exponent of 2. | ||
pub(crate) mana_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, | ||
/// 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: u8, | ||
} | ||
|
||
impl ManaStructure { | ||
/// Returns the mana decay factors slice. | ||
pub fn mana_decay_factors(&self) -> &[u32] { | ||
&self.mana_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() | ||
} | ||
|
||
/// Returns the max mana that can exist with the mana bits defined. | ||
pub fn max_mana(&self) -> u64 { | ||
(1 << self.mana_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(), | ||
} | ||
} | ||
} |
Oops, something went wrong.