diff --git a/sdk/src/types/block/core/basic.rs b/sdk/src/types/block/core/basic.rs index b6cde73360..4ad42748f2 100644 --- a/sdk/src/types/block/core/basic.rs +++ b/sdk/src/types/block/core/basic.rs @@ -157,18 +157,22 @@ impl BasicBlock { } impl WorkScore for BasicBlock { - fn work_score(&self, work_score_struct: WorkScoreStructure) -> u32 { + fn work_score(&self, work_score_params: WorkScoreStructure) -> u32 { let missing_parent_score = { - let min_strong_parents_threshold = work_score_struct.min_strong_parents_threshold as usize; + let min_strong_parents_threshold = work_score_params.min_strong_parents_threshold as usize; if self.strong_parents.len() < min_strong_parents_threshold { let missing_parents_count = min_strong_parents_threshold - self.strong_parents.len(); - work_score_struct.missing_parent * missing_parents_count as u32 + work_score_params.missing_parent * missing_parents_count as u32 } else { 0 } }; - let payload_score = self.payload.as_ref().map(|p| p.work_score(work_score_struct)).unwrap_or(0); - work_score_struct.block + missing_parent_score + payload_score + let payload_score = self + .payload + .as_ref() + .map(|p| p.work_score(work_score_params)) + .unwrap_or(0); + work_score_params.block + missing_parent_score + payload_score } } diff --git a/sdk/src/types/block/core/mod.rs b/sdk/src/types/block/core/mod.rs index b38d338d0d..76ad5984d1 100644 --- a/sdk/src/types/block/core/mod.rs +++ b/sdk/src/types/block/core/mod.rs @@ -132,10 +132,10 @@ impl Block { } impl WorkScore for Block { - fn work_score(&self, work_score_struct: WorkScoreStructure) -> u32 { + fn work_score(&self, work_score_params: WorkScoreStructure) -> u32 { match self { - Self::Basic(basic) => basic.work_score(work_score_struct), - Self::Validation(validation) => validation.work_score(work_score_struct), + Self::Basic(basic) => basic.work_score(work_score_params), + Self::Validation(validation) => 0, } } } diff --git a/sdk/src/types/block/core/validation.rs b/sdk/src/types/block/core/validation.rs index 246e61e59e..e04d6ae266 100644 --- a/sdk/src/types/block/core/validation.rs +++ b/sdk/src/types/block/core/validation.rs @@ -161,13 +161,6 @@ impl ValidationBlock { } } -impl WorkScore for ValidationBlock { - fn work_score(&self, _: WorkScoreStructure) -> u32 { - // The work score of a validation block is `0`. - 0 - } -} - impl Packable for ValidationBlock { type UnpackError = Error; type UnpackVisitor = ProtocolParameters; diff --git a/sdk/src/types/block/core/wrapper.rs b/sdk/src/types/block/core/wrapper.rs index 95b62f9356..4d1ea8c7bf 100644 --- a/sdk/src/types/block/core/wrapper.rs +++ b/sdk/src/types/block/core/wrapper.rs @@ -109,13 +109,6 @@ impl BlockHeader { } } -impl WorkScore for BlockHeader { - fn work_score(&self, _: WorkScoreStructure) -> u32 { - // The work score of a block header is `0`. - 0 - } -} - impl Packable for BlockHeader { type UnpackError = Error; type UnpackVisitor = ProtocolParameters; @@ -298,12 +291,10 @@ impl BlockWrapper { } impl WorkScore for BlockWrapper { - fn work_score(&self, work_score_struct: WorkScoreStructure) -> u32 { - let mut score = work_score_struct.data_byte * self.packed_len() as u32 / 1024; - score += self.header.work_score(work_score_struct); - score += self.block.work_score(work_score_struct); - score += self.signature.work_score(work_score_struct); - score + fn work_score(&self, work_score_params: WorkScoreStructure) -> u32 { + let block_score = self.block.work_score(work_score_params); + let signature_score = self.signature.work_score(work_score_params); + block_score + signature_score } } diff --git a/sdk/src/types/block/mana/computation.rs b/sdk/src/types/block/mana/computation.rs deleted file mode 100644 index 6636ead80c..0000000000 --- a/sdk/src/types/block/mana/computation.rs +++ /dev/null @@ -1,7 +0,0 @@ -// Copyright 2023 IOTA Stiftung -// SPDX-License-Identifier: Apache-2.0 - -/// Computes the necessary mana amount to fulfill the work score requirement. -pub fn compute_necessary_mana_amount(work_score: u32) -> u64 { - todo!("compute necessary mana amount") -} diff --git a/sdk/src/types/block/mana/mod.rs b/sdk/src/types/block/mana/mod.rs index 2c69d05cab..80320ac623 100644 --- a/sdk/src/types/block/mana/mod.rs +++ b/sdk/src/types/block/mana/mod.rs @@ -2,7 +2,6 @@ // SPDX-License-Identifier: Apache-2.0 mod allotment; -mod computation; mod rewards; mod structure; @@ -15,10 +14,7 @@ use packable::{bounded::BoundedU16, prefix::BoxedSlicePrefix, Packable}; #[cfg(feature = "serde")] pub use self::allotment::dto::ManaAllotmentDto; -pub use self::{ - allotment::ManaAllotment, computation::compute_necessary_mana_amount, rewards::RewardsParameters, - structure::ManaStructure, -}; +pub use self::{allotment::ManaAllotment, rewards::RewardsParameters, structure::ManaStructure}; use super::{output::AccountId, protocol::ProtocolParameters, Error}; pub(crate) type ManaAllotmentCount = diff --git a/sdk/src/types/block/protocol/work_score.rs b/sdk/src/types/block/protocol/work_score.rs index 84982ac7e6..cd049233fa 100644 --- a/sdk/src/types/block/protocol/work_score.rs +++ b/sdk/src/types/block/protocol/work_score.rs @@ -4,7 +4,7 @@ use getset::CopyGetters; use packable::Packable; -use crate::types::block::{Error, mana::compute_necessary_mana_amount}; +use crate::types::block::Error; #[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Packable, CopyGetters)] #[cfg_attr( @@ -62,12 +62,12 @@ impl Default for WorkScoreStructure { /// A trait to facilitate the computation of the work score of a block, which is central to mana cost calculation. pub trait WorkScore { - /// Returns the work score. + /// Returns its work score. fn work_score(&self, work_score_params: WorkScoreStructure) -> u32; - /// Calculates the Mana cost. - fn mana_cost(&self, work_score_params: WorkScoreStructure) -> u64 { - compute_necessary_mana_amount(self.work_score(work_score_params)) + /// Returns the Mana cost given its work score. + fn mana_cost(&self, work_score_params: WorkScoreStructure, reference_mana_cost: u64) -> u64 { + reference_mana_cost * self.work_score(work_score_params) as u64 } }