Skip to content

Commit

Permalink
add and impl mana cost fn
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex6323 committed Oct 9, 2023
1 parent 7e8a204 commit f917fd9
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 45 deletions.
14 changes: 9 additions & 5 deletions sdk/src/types/block/core/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
6 changes: 3 additions & 3 deletions sdk/src/types/block/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}
Expand Down
7 changes: 0 additions & 7 deletions sdk/src/types/block/core/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 4 additions & 13 deletions sdk/src/types/block/core/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}
}

Expand Down
7 changes: 0 additions & 7 deletions sdk/src/types/block/mana/computation.rs

This file was deleted.

6 changes: 1 addition & 5 deletions sdk/src/types/block/mana/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

mod allotment;
mod computation;
mod rewards;
mod structure;

Expand All @@ -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 =
Expand Down
10 changes: 5 additions & 5 deletions sdk/src/types/block/protocol/work_score.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
}
}

Expand Down

0 comments on commit f917fd9

Please sign in to comment.