Skip to content

Commit

Permalink
Remove ManaAllotment
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-martinez committed Dec 11, 2023
1 parent 65791c7 commit e4b5c77
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 59 deletions.
54 changes: 6 additions & 48 deletions sdk/src/types/block/mana/allotment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ use crate::types::block::{
/// in the form of Block Issuance Credits to the account.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Packable)]
#[packable(unpack_error = Error)]
#[packable(unpack_visitor = ProtocolParameters)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ManaAllotment {
pub(crate) account_id: AccountId,
#[packable(verify_with = verify_mana)]
#[serde(with = "crate::utils::serde::string")]
pub(crate) mana: u64,
}

impl ManaAllotment {
pub fn new(account_id: AccountId, mana: u64, protocol_params: &ProtocolParameters) -> Result<Self, Error> {
verify_mana::<true>(&mana, protocol_params)?;
pub fn new(account_id: AccountId, mana: u64) -> Result<Self, Error> {
verify_mana::<true>(&mana)?;

Ok(Self { account_id, mana })
}
Expand Down Expand Up @@ -59,8 +60,8 @@ impl WorkScore for ManaAllotment {
}
}

fn verify_mana<const VERIFY: bool>(mana: &u64, params: &ProtocolParameters) -> Result<(), Error> {
if VERIFY && *mana > params.mana_parameters().max_mana() {
fn verify_mana<const VERIFY: bool>(mana: &u64) -> Result<(), Error> {
if VERIFY && *mana == 0 {
return Err(Error::InvalidManaValue(*mana));
}

Expand Down Expand Up @@ -187,46 +188,3 @@ impl IntoIterator for ManaAllotments {
Vec::from(Into::<Box<[ManaAllotment]>>::into(self.0)).into_iter()
}
}

#[cfg(feature = "serde")]
pub(super) mod dto {
use serde::{Deserialize, Serialize};

use super::*;
use crate::{types::TryFromDto, utils::serde::string};

#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ManaAllotmentDto {
pub account_id: AccountId,
#[serde(with = "string")]
pub mana: u64,
}

impl From<&ManaAllotment> for ManaAllotmentDto {
fn from(value: &ManaAllotment) -> Self {
Self {
account_id: value.account_id,
mana: value.mana,
}
}
}

impl TryFromDto<ManaAllotmentDto> for ManaAllotment {
type Error = Error;

fn try_from_dto_with_params_inner(
dto: ManaAllotmentDto,
params: Option<&ProtocolParameters>,
) -> Result<Self, Self::Error> {
Ok(if let Some(params) = params {
Self::new(dto.account_id, dto.mana, params)?
} else {
Self {
account_id: dto.account_id,
mana: dto.mana,
}
})
}
}
}
2 changes: 1 addition & 1 deletion sdk/src/types/block/mana/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod allotment;
mod parameters;
mod rewards;

pub(crate) use self::allotment::{dto::ManaAllotmentDto, verify_mana_allotments_sum, ManaAllotmentCount};
pub(crate) use self::allotment::{verify_mana_allotments_sum, ManaAllotmentCount};
pub use self::{
allotment::{ManaAllotment, ManaAllotments},
parameters::ManaParameters,
Expand Down
13 changes: 4 additions & 9 deletions sdk/src/types/block/payload/signed_transaction/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ pub(crate) mod dto {

use super::*;
use crate::types::{
block::{mana::ManaAllotmentDto, payload::dto::PayloadDto, Error},
block::{payload::dto::PayloadDto, Error},
TryFromDto,
};

Expand All @@ -555,7 +555,7 @@ pub(crate) mod dto {
pub context_inputs: Vec<ContextInput>,
pub inputs: Vec<Input>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub allotments: Vec<ManaAllotmentDto>,
pub allotments: Vec<ManaAllotment>,
#[serde(default, skip_serializing_if = "TransactionCapabilities::is_none")]
pub capabilities: TransactionCapabilities,
#[serde(default, skip_serializing_if = "Option::is_none")]
Expand All @@ -570,7 +570,7 @@ pub(crate) mod dto {
creation_slot: value.creation_slot(),
context_inputs: value.context_inputs().to_vec(),
inputs: value.inputs().to_vec(),
allotments: value.allotments().iter().map(Into::into).collect(),
allotments: value.allotments().to_vec(),
capabilities: value.capabilities().clone(),
payload: match value.payload() {
Some(p @ Payload::TaggedData(_)) => Some(p.into()),
Expand All @@ -593,18 +593,13 @@ pub(crate) mod dto {
.network_id
.parse::<u64>()
.map_err(|_| Error::InvalidField("network_id"))?;
let mana_allotments = dto
.allotments
.into_iter()
.map(|o| ManaAllotment::try_from_dto_with_params_inner(o, params))
.collect::<Result<Vec<ManaAllotment>, Error>>()?;
let outputs = dto.outputs;

let mut builder = Self::builder(network_id)
.with_creation_slot(dto.creation_slot)
.with_context_inputs(dto.context_inputs)
.with_inputs(dto.inputs)
.with_mana_allotments(mana_allotments)
.with_mana_allotments(dto.allotments)
.with_capabilities(dto.capabilities)
.with_outputs(outputs);

Expand Down
1 change: 0 additions & 1 deletion sdk/src/types/block/rand/mana.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ pub fn rand_mana_allotment(params: &ProtocolParameters) -> ManaAllotment {
ManaAllotment::new(
rand_account_id(),
rand_number_range(0..params.mana_parameters().max_mana()),
params,
)
.unwrap()
}

0 comments on commit e4b5c77

Please sign in to comment.