From 3f2d737acbec6e19b8234af6736d0f7891b8419f Mon Sep 17 00:00:00 2001 From: /alex/ Date: Wed, 29 Nov 2023 17:09:27 +0100 Subject: [PATCH] Calculate and set `max_burned_mana` field (#1610) * 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 * is_signature * remove todo * first impl * review 1 * review 2 * review 3 * fix copyright year Co-authored-by: Thibault Martinez * StorageScore :heart: 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 * 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 <46689931+Thoralf-M@users.noreply.github.com> * remove builder method * pr comments * improve convenience * simplify --------- Co-authored-by: DaughterOfMars Co-authored-by: Thibault Martinez Co-authored-by: Thibault Martinez Co-authored-by: Thoralf-M <46689931+Thoralf-M@users.noreply.github.com> --- sdk/src/client/api/block_builder/mod.rs | 19 +++++---- sdk/src/types/block/core/basic.rs | 54 +++++++++++++++++++------ sdk/src/types/block/core/mod.rs | 6 ++- sdk/src/types/block/rand/block.rs | 2 +- 4 files changed, 59 insertions(+), 22 deletions(-) diff --git a/sdk/src/client/api/block_builder/mod.rs b/sdk/src/client/api/block_builder/mod.rs index ea3137424c..9c87e324b9 100644 --- a/sdk/src/client/api/block_builder/mod.rs +++ b/sdk/src/client/api/block_builder/mod.rs @@ -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, @@ -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()?, )) } } diff --git a/sdk/src/types/block/core/basic.rs b/sdk/src/types/block/core/basic.rs index 5ff0a65743..6cee82987c 100644 --- a/sdk/src/types/block/core/basic.rs +++ b/sdk/src/types/block/core/basic.rs @@ -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 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) -> Self { + let max_burned_mana = max_burned_mana.into(); Self { strong_parents, weak_parents: WeakParents::default(), @@ -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 { 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`]. @@ -97,7 +125,7 @@ impl From 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), } } } diff --git a/sdk/src/types/block/core/mod.rs b/sdk/src/types/block/core/mod.rs index 5b16fb9b0a..b8e6b96cd1 100644 --- a/sdk/src/types/block/core/mod.rs +++ b/sdk/src/types/block/core/mod.rs @@ -19,6 +19,7 @@ pub use self::{ validation::{ValidationBlockBody, ValidationBlockBodyBuilder}, }; use crate::types::block::{ + core::basic::MaxBurnedManaAmount, protocol::{ProtocolParameters, ProtocolParametersHash, WorkScore, WorkScoreParameters}, Error, }; @@ -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, + ) -> BasicBlockBodyBuilder { BasicBlockBodyBuilder::new(strong_parents, max_burned_mana) } diff --git a/sdk/src/types/block/rand/block.rs b/sdk/src/types/block/rand/block.rs index 7eb3e3d840..d462810c8f 100644 --- a/sdk/src/types/block/rand/block.rs +++ b/sdk/src/types/block/rand/block.rs @@ -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::()) } /// Generates a random block with given block body.