Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(types): vbyte -> storage score #1398

Closed
wants to merge 12 commits into from
6 changes: 3 additions & 3 deletions bindings/core/src/method/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
use derivative::Derivative;
use iota_sdk::types::block::{
address::{Bech32Address, Hrp},
output::{dto::OutputDto, AccountId, NftId, OutputId, RentStructure},
output::{dto::OutputDto, AccountId, NftId, OutputId},
payload::transaction::{
dto::{TransactionEssenceDto, TransactionPayloadDto},
TransactionId,
},
protocol::ProtocolParameters,
protocol::{ProtocolParameters, RentParameters},
signature::Ed25519Signature,
slot::SlotCommitment,
BlockWrapperDto,
Expand Down Expand Up @@ -131,7 +131,7 @@ pub enum UtilsMethod {
/// Computes the input commitment from the output objects that are used as inputs to fund the transaction.
ComputeInputsCommitment { inputs: Vec<OutputDto> },
/// Computes the required storage deposit of an output.
ComputeStorageDeposit { output: OutputDto, rent: RentStructure },
ComputeStorageDeposit { output: OutputDto, rent: RentParameters },
/// Checks if the given mnemonic is valid.
/// Expected response: [`Ok`](crate::Response::Ok)
VerifyMnemonic {
Expand Down
14 changes: 7 additions & 7 deletions bindings/core/src/method_handler/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ use iota_sdk::{
api::core::response::OutputWithMetadataResponse,
block::{
output::{
dto::OutputDto, AccountOutput, BasicOutput, FoundryOutput, NftOutput, Output, OutputBuilderAmount, Rent,
dto::OutputDto, AccountOutput, BasicOutput, FoundryOutput, NftOutput, Output, OutputBuilderAmount,
},
payload::Payload,
protocol::{RentStructure, StorageScore},
BlockWrapper, BlockWrapperDto,
},
TryFromDto,
Expand Down Expand Up @@ -71,7 +72,7 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
if let Some(amount) = amount {
OutputBuilderAmount::Amount(amount)
} else {
OutputBuilderAmount::MinimumStorageDeposit(client.get_rent_structure().await?)
OutputBuilderAmount::MinimumStorageDeposit(client.get_rent_parameters().await?.into())
},
mana,
native_tokens,
Expand All @@ -98,7 +99,7 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
if let Some(amount) = amount {
OutputBuilderAmount::Amount(amount)
} else {
OutputBuilderAmount::MinimumStorageDeposit(client.get_rent_structure().await?)
OutputBuilderAmount::MinimumStorageDeposit(client.get_rent_parameters().await?.into())
},
mana,
native_tokens,
Expand All @@ -122,7 +123,7 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
if let Some(amount) = amount {
OutputBuilderAmount::Amount(amount)
} else {
OutputBuilderAmount::MinimumStorageDeposit(client.get_rent_structure().await?)
OutputBuilderAmount::MinimumStorageDeposit(client.get_rent_parameters().await?.into())
},
native_tokens,
serial_number,
Expand All @@ -148,7 +149,7 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
if let Some(amount) = amount {
OutputBuilderAmount::Amount(amount)
} else {
OutputBuilderAmount::MinimumStorageDeposit(client.get_rent_structure().await?)
OutputBuilderAmount::MinimumStorageDeposit(client.get_rent_parameters().await?.into())
},
mana,
native_tokens,
Expand Down Expand Up @@ -296,8 +297,7 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
}
ClientMethod::MinimumRequiredStorageDeposit { output } => {
let output = Output::try_from_dto_with_params(output, client.get_token_supply().await?)?;
let rent_structure = client.get_rent_structure().await?;

let rent_structure = client.get_rent_parameters().await?.into();
let minimum_storage_deposit = output.rent_cost(rent_structure);

Response::MinimumRequiredStorageDeposit(minimum_storage_deposit.to_string())
Expand Down
5 changes: 3 additions & 2 deletions bindings/core/src/method_handler/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ use iota_sdk::{
block::{
address::{AccountAddress, Address, ToBech32Ext},
input::UtxoInput,
output::{AccountId, FoundryId, InputsCommitment, NftId, Output, OutputId, Rent, TokenId},
output::{AccountId, FoundryId, InputsCommitment, NftId, Output, OutputId, TokenId},
payload::{transaction::TransactionEssence, TransactionPayload},
protocol::StorageScore,
BlockWrapper,
},
TryFromDto,
Expand Down Expand Up @@ -80,7 +81,7 @@ pub(crate) fn call_utils_method_internal(method: UtilsMethod) -> Result<Response
}
UtilsMethod::ComputeStorageDeposit { output, rent } => {
let out = Output::try_from_dto(output)?;
Response::MinimumRequiredStorageDeposit(out.rent_cost(rent).to_string())
Response::MinimumRequiredStorageDeposit(out.rent_cost(rent.into()).to_string())
}
UtilsMethod::VerifyMnemonic { mnemonic } => {
let mnemonic = Mnemonic::from(mnemonic);
Expand Down
2 changes: 1 addition & 1 deletion cli/src/command/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ pub async fn send_native_token_command(
let address = address.convert()?;
let transaction = if gift_storage_deposit.unwrap_or(false) {
// Send native tokens together with the required storage deposit
let rent_structure = account.client().get_rent_structure().await?;
let rent_structure = account.client().get_rent_parameters().await?.into();
let token_supply = account.client().get_token_supply().await?;

account.client().bech32_hrp_matches(address.hrp()).await?;
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/client/output/build_account_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async fn main() -> Result<()> {
.await?;

let token_supply = client.get_token_supply().await?;
let rent_structure = client.get_rent_structure().await?;
let rent_structure = client.get_rent_parameters().await?.into();

let address = std::env::args()
.nth(1)
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/client/output/build_nft_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async fn main() -> Result<()> {
.await?;

let token_supply = client.get_token_supply().await?;
let rent_structure = client.get_rent_structure().await?;
let rent_structure = client.get_rent_parameters().await?.into();

let address = std::env::args()
.nth(1)
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/how_tos/account/state_transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async fn main() -> Result<()> {
);

let token_supply = account.client().get_token_supply().await?;
let rent_structure = account.client().get_rent_structure().await?;
let rent_structure = account.client().get_rent_parameters().await?.into();

let account_output = account_output_data.output.as_account();
let updated_account_output = AccountOutputBuilder::from(account_output)
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/how_tos/outputs/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async fn main() -> Result<()> {
let client = Client::builder().with_node(&node_url)?.finish().await?;

let token_supply = client.get_token_supply().await?;
let rent_structure = client.get_rent_structure().await?;
let rent_structure = client.get_rent_parameters().await?.into();

let address = Address::try_from_bech32("rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy")?;

Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/how_tos/outputs/unlock_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async fn main() -> Result<()> {
let client = Client::builder().with_node(&node_url)?.finish().await?;

let token_supply = client.get_token_supply().await?;
let rent_structure = client.get_rent_structure().await?;
let rent_structure = client.get_rent_parameters().await?.into();

let address = Address::try_from_bech32("rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy")?;
let account_address = Address::try_from_bech32("rms1pr59qm43mjtvhcajfmupqf23x29llam88yecn6pyul80rx099krmv2fnnux")?;
Expand Down
5 changes: 3 additions & 2 deletions sdk/src/client/api/block_builder/input_selection/remainder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::{
types::block::{
address::{Address, Ed25519Address},
output::{unlock_condition::AddressUnlockCondition, BasicOutputBuilder, NativeTokensBuilder, Output},
protocol::RentStructure,
},
};

Expand Down Expand Up @@ -65,7 +66,7 @@ impl InputSelection {
let native_tokens_remainder = native_tokens_diff.is_some();

let mut remainder_builder =
BasicOutputBuilder::new_with_minimum_storage_deposit(self.protocol_parameters.rent_structure())
BasicOutputBuilder::new_with_minimum_storage_deposit(self.protocol_parameters.rent_parameters().into())
.add_unlock_condition(AddressUnlockCondition::new(Address::from(Ed25519Address::from(
[0; 32],
))));
Expand Down Expand Up @@ -145,7 +146,7 @@ impl InputSelection {
log::debug!("Created remainder output of {diff} for {remainder_address:?}");

remainder.verify_storage_deposit(
self.protocol_parameters.rent_structure(),
self.protocol_parameters.rent_parameters().into(),
self.protocol_parameters.token_supply(),
)?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ use crate::{
input::INPUT_COUNT_MAX,
output::{
unlock_condition::StorageDepositReturnUnlockCondition, AccountOutputBuilder, AccountTransition,
FoundryOutputBuilder, NftOutputBuilder, Output, OutputId, Rent,
FoundryOutputBuilder, NftOutputBuilder, Output, OutputId,
},
protocol::{RentStructure, StorageScore},
slot::SlotIndex,
},
};
Expand Down Expand Up @@ -240,7 +241,7 @@ impl InputSelection {
for output in outputs {
let diff = amount_selection.missing_amount();
let amount = output.amount();
let rent = output.rent_cost(self.protocol_parameters.rent_structure());
let rent = output.rent_cost(self.protocol_parameters.rent_parameters().into());

let new_amount = if amount >= diff + rent { amount - diff } else { rent };

Expand Down
9 changes: 6 additions & 3 deletions sdk/src/client/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ use crate::{
node_manager::NodeManager,
Error,
},
types::block::{address::Hrp, output::RentStructure, protocol::ProtocolParameters},
types::block::{
address::Hrp,
protocol::{ProtocolParameters, RentParameters},
},
};

/// An IOTA node client.
Expand Down Expand Up @@ -153,8 +156,8 @@ impl ClientInner {
}

/// Gets the rent structure of the node we're connecting to.
pub async fn get_rent_structure(&self) -> Result<RentStructure> {
Ok(self.get_network_info().await?.protocol_parameters.rent_structure())
pub async fn get_rent_parameters(&self) -> Result<RentParameters> {
Ok(self.get_network_info().await?.protocol_parameters.rent_parameters())
}

/// Gets the token supply of the node we're connecting to.
Expand Down
12 changes: 11 additions & 1 deletion sdk/src/types/block/address/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ use core::str::FromStr;

use derive_more::{AsRef, Deref, From};

use crate::types::block::{output::AccountId, Error};
use crate::types::block::{
output::AccountId,
protocol::{RentStructure, StorageScore},
Error,
};

/// An account address.
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, From, AsRef, Deref, packable::Packable)]
Expand Down Expand Up @@ -57,6 +61,12 @@ impl core::fmt::Debug for AccountAddress {
}
}

impl StorageScore for AccountAddress {
fn storage_score(&self, _rent_struct: RentStructure) -> u64 {
0
}
}

#[cfg(feature = "serde")]
mod dto {
use serde::{Deserialize, Serialize};
Expand Down
18 changes: 17 additions & 1 deletion sdk/src/types/block/address/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use crypto::{
use derive_more::{AsRef, Deref, From};
use packable::Packable;

use crate::types::block::Error;
use crate::types::block::{
protocol::{RentStructure, StorageScore},
Error,
};

/// An Ed25519 address.
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, From, AsRef, Deref, Packable)]
Expand All @@ -33,6 +36,12 @@ impl Ed25519Address {
pub fn from_public_key_bytes(public_key_bytes: [u8; PublicKey::LENGTH]) -> Result<Self, Error> {
Ok(Self::new(Blake2b256::digest(public_key_bytes).try_into()?))
}

/// Creates an empty [`Ed25519Address`].
/// This is used internally to calculate a storage score for implicit account addresses.
pub(crate) fn null() -> Self {
Self([0; Self::LENGTH])
}
}

impl FromStr for Ed25519Address {
Expand All @@ -55,6 +64,13 @@ impl core::fmt::Debug for Ed25519Address {
}
}

impl StorageScore for Ed25519Address {
fn storage_score(&self, rent_structure: RentStructure) -> u64 {
// TODO: wait for TIP for this to change?!
0
}
}

#[cfg(feature = "serde")]
pub(crate) mod dto {
use serde::{Deserialize, Serialize};
Expand Down
11 changes: 10 additions & 1 deletion sdk/src/types/block/address/implicit_account_creation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
use derive_more::{AsRef, Deref, Display, From, FromStr};
use packable::Packable;

use crate::types::block::address::Ed25519Address;
use crate::types::block::{
address::Ed25519Address,
protocol::{RentStructure, StorageScore},
};

/// An implicit account creation address that can be used to transition an account.
#[derive(Copy, Clone, Debug, Display, Eq, PartialEq, Ord, PartialOrd, Hash, FromStr, AsRef, Deref, From, Packable)]
Expand All @@ -24,6 +27,12 @@ impl ImplicitAccountCreationAddress {
}
}

impl StorageScore for ImplicitAccountCreationAddress {
fn storage_score(&self, rent_structure: RentStructure) -> u64 {
self.0.storage_score(rent_structure)
}
}

#[cfg(feature = "serde")]
pub(crate) mod dto {
use serde::{Deserialize, Serialize};
Expand Down
28 changes: 22 additions & 6 deletions sdk/src/types/block/address/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ pub use self::{
nft::NftAddress,
restricted::{AddressCapabilities, AddressCapabilityFlag, RestrictedAddress},
};
use crate::types::block::{
output::{Output, OutputId},
semantic::{TransactionFailureReason, ValidationContext},
signature::Signature,
unlock::Unlock,
ConvertTo, Error,
use crate::{
types::block::{
output::{Output, OutputId},
protocol::{RentParameters, RentStructure, StorageScore},
semantic::{TransactionFailureReason, ValidationContext},
signature::Signature,
unlock::Unlock,
ConvertTo, Error,
},
wallet::storage::Storage,
};

/// A generic address supporting different address kinds.
Expand Down Expand Up @@ -224,3 +228,15 @@ impl<T: Into<Address>> ToBech32Ext for T {
Bech32Address::new(hrp.convert_unchecked(), self)
}
}

impl StorageScore for Address {
fn storage_score(&self, rent_structure: RentStructure) -> u64 {
match self {
Self::Account(account) => account.storage_score(rent_structure),
Self::Ed25519(ed25519) => ed25519.storage_score(rent_structure),
Self::Nft(nft) => nft.storage_score(rent_structure),
Self::ImplicitAccountCreation(implicit) => implicit.storage_score(rent_structure),
Self::Restricted(restricted) => restricted.storage_score(rent_structure),
}
}
}
12 changes: 11 additions & 1 deletion sdk/src/types/block/address/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ use core::str::FromStr;

use derive_more::{AsRef, Deref, From};

use crate::types::block::{output::NftId, Error};
use crate::types::block::{
output::NftId,
protocol::{RentStructure, StorageScore},
Error,
};

/// An NFT address.
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, From, AsRef, Deref, packable::Packable)]
Expand Down Expand Up @@ -57,6 +61,12 @@ impl core::fmt::Debug for NftAddress {
}
}

impl StorageScore for NftAddress {
fn storage_score(&self, _rent_struct: RentStructure) -> u64 {
0
}
}

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