Skip to content

Commit

Permalink
Calculate and set max_burned_mana field (#1610)
Browse files Browse the repository at this point in the history
* add workscore calc

* fix merge

* finish calculations

* nits

* merge import

* nits

* rename trait fn

* update work score computations

* nit

* move work score types into own module

* add and impl mana cost fn

* PR suggestions 1

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

* is_signature

* remove todo

* first impl

* review 1

* review 2

* review 3

* fix copyright year

Co-authored-by: Thibault Martinez <[email protected]>

* StorageScore ❤️ WorkScore

* Fix def_is_as_opt panic message (#1659)

* Remove native tokens from AnchorOutput (#1660)

* Python: add multi address (#1658)

* add weighted address and multi address

* fmt

* fmt

* align + more impls

* nit

* default impl for all features

* default impl for all unlocks

* self

* rm WorkScore impl for NativeTokens type

* review suggestions

* align and improve

* cleanup

* cleanup 2

* forward work score in NativeTokenFeature

* cleanup 3

* forward 2

* let's see if we're faster than thibault

* unnecessary import

* underscore

Co-authored-by: Thibault Martinez <[email protected]>

* final touches

* remove todo

* rm mana_cost fn from work score trait

* set max burned mana to minimum

* use builder

* remnant

* use with_ instead of ctro to set mana

* nit

* another remnant

* ctor enum approach

* rand

Co-authored-by: Thoralf-M <[email protected]>

* remove builder method

* pr comments

* improve convenience

* simplify

---------

Co-authored-by: DaughterOfMars <[email protected]>
Co-authored-by: Thibault Martinez <[email protected]>
Co-authored-by: Thibault Martinez <[email protected]>
Co-authored-by: Thoralf-M <[email protected]>
  • Loading branch information
5 people authored Nov 29, 2023
1 parent c32fe8c commit 3f2d737
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 22 deletions.
19 changes: 12 additions & 7 deletions sdk/src/client/api/block_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub use self::transaction::verify_semantic;
use crate::{
client::{ClientInner, Result},
types::block::{
core::{BlockHeader, UnsignedBlock},
core::{basic::MaxBurnedManaAmount, BlockHeader, UnsignedBlock},
output::AccountId,
payload::Payload,
BlockBody,
Expand Down Expand Up @@ -43,12 +43,17 @@ impl ClientInner {
issuance.latest_finalized_slot,
issuer_id,
),
// TODO: burned mana calculation
BlockBody::build_basic(issuance.strong_parents()?, 0)
.with_weak_parents(issuance.weak_parents()?)
.with_shallow_like_parents(issuance.shallow_like_parents()?)
.with_payload(payload)
.finish_block_body()?,
BlockBody::build_basic(
issuance.strong_parents()?,
(
protocol_params.work_score_parameters,
issuance.commitment.reference_mana_cost(),
),
)
.with_weak_parents(issuance.weak_parents()?)
.with_shallow_like_parents(issuance.shallow_like_parents()?)
.with_payload(payload)
.finish_block_body()?,
))
}
}
54 changes: 41 additions & 13 deletions sdk/src/types/block/core/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,44 @@ pub type StrongParents = Parents<1, 8>;
pub type WeakParents = Parents<0, 8>;
pub type ShallowLikeParents = Parents<0, 8>;

#[derive(Copy, Clone)]
pub enum MaxBurnedManaAmount {
Amount(u64),
MinimumAmount {
params: WorkScoreParameters,
reference_mana_cost: u64,
},
}

impl From<u64> for MaxBurnedManaAmount {
fn from(value: u64) -> Self {
Self::Amount(value)
}
}

impl From<(WorkScoreParameters, u64)> for MaxBurnedManaAmount {
fn from(value: (WorkScoreParameters, u64)) -> Self {
Self::MinimumAmount {
params: value.0,
reference_mana_cost: value.1,
}
}
}

/// A builder for a [`BasicBlockBody`].
pub struct BasicBlockBodyBuilder {
strong_parents: StrongParents,
weak_parents: WeakParents,
shallow_like_parents: ShallowLikeParents,
payload: OptionalPayload,
max_burned_mana: u64,
max_burned_mana: MaxBurnedManaAmount,
}

impl BasicBlockBodyBuilder {
/// Creates a new [`BasicBlockBodyBuilder`].
#[inline(always)]
pub fn new(strong_parents: StrongParents, max_burned_mana: u64) -> Self {
pub fn new(strong_parents: StrongParents, max_burned_mana: impl Into<MaxBurnedManaAmount>) -> Self {
let max_burned_mana = max_burned_mana.into();
Self {
strong_parents,
weak_parents: WeakParents::default(),
Expand Down Expand Up @@ -64,24 +89,27 @@ impl BasicBlockBodyBuilder {
self
}

/// Adds max burned mana to a [`BasicBlockBodyBuilder`].
#[inline(always)]
pub fn with_max_burned_mana(mut self, max_burned_mana: u64) -> Self {
self.max_burned_mana = max_burned_mana;
self
}

/// Finishes the builder into a [`BasicBlockBody`].
pub fn finish(self) -> Result<BasicBlockBody, Error> {
verify_parents_sets(&self.strong_parents, &self.weak_parents, &self.shallow_like_parents)?;

Ok(BasicBlockBody {
let mut body = BasicBlockBody {
strong_parents: self.strong_parents,
weak_parents: self.weak_parents,
shallow_like_parents: self.shallow_like_parents,
payload: self.payload,
max_burned_mana: self.max_burned_mana,
})
max_burned_mana: 0,
};

body.max_burned_mana = match self.max_burned_mana {
MaxBurnedManaAmount::Amount(amount) => amount,
MaxBurnedManaAmount::MinimumAmount {
params,
reference_mana_cost,
} => body.work_score(params) as u64 * reference_mana_cost,
};

Ok(body)
}

/// Finishes the builder into a [`BlockBody`].
Expand All @@ -97,7 +125,7 @@ impl From<BasicBlockBody> for BasicBlockBodyBuilder {
weak_parents: value.weak_parents,
shallow_like_parents: value.shallow_like_parents,
payload: value.payload,
max_burned_mana: value.max_burned_mana,
max_burned_mana: MaxBurnedManaAmount::Amount(value.max_burned_mana),
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion sdk/src/types/block/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub use self::{
validation::{ValidationBlockBody, ValidationBlockBodyBuilder},
};
use crate::types::block::{
core::basic::MaxBurnedManaAmount,
protocol::{ProtocolParameters, ProtocolParametersHash, WorkScore, WorkScoreParameters},
Error,
};
Expand Down Expand Up @@ -81,7 +82,10 @@ impl BlockBody {

/// Creates a new [`BasicBlockBodyBuilder`].
#[inline(always)]
pub fn build_basic(strong_parents: self::basic::StrongParents, max_burned_mana: u64) -> BasicBlockBodyBuilder {
pub fn build_basic(
strong_parents: self::basic::StrongParents,
max_burned_mana: impl Into<MaxBurnedManaAmount>,
) -> BasicBlockBodyBuilder {
BasicBlockBodyBuilder::new(strong_parents, max_burned_mana)
}

Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/rand/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn rand_basic_block_body_with_strong_parents(strong_parents: basic::StrongPa
pub fn rand_basic_block_body_builder_with_strong_parents(
strong_parents: basic::StrongParents,
) -> BasicBlockBodyBuilder {
BlockBody::build_basic(strong_parents, rand_number())
BlockBody::build_basic(strong_parents, rand_number::<u64>())
}

/// Generates a random block with given block body.
Expand Down

0 comments on commit 3f2d737

Please sign in to comment.