Skip to content

Commit

Permalink
uplift and rename trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex6323 committed Oct 6, 2023
1 parent 616f60e commit d6f12e5
Show file tree
Hide file tree
Showing 47 changed files with 406 additions and 326 deletions.
5 changes: 3 additions & 2 deletions bindings/core/src/method/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
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,
rent::RentParameters,
signature::Ed25519Signature,
slot::SlotCommitment,
BlockWrapperDto,
Expand Down Expand Up @@ -131,7 +132,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 @@ -10,9 +10,9 @@ use iota_sdk::{
block::{
output::{
dto::OutputDto, AccountOutput, BasicOutput, FoundryOutput, NftOutput, Output, OutputBuilderAmount,
StorageScore,
},
payload::Payload,
rent::StorageCost,
BlockWrapper, BlockWrapperDto,
},
TryFromDto,
Expand Down Expand Up @@ -72,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?)
},
mana,
native_tokens,
Expand All @@ -99,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?)
},
mana,
native_tokens,
Expand All @@ -123,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?)
},
native_tokens,
serial_number,
Expand All @@ -149,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?)
},
mana,
native_tokens,
Expand Down Expand Up @@ -296,9 +296,9 @@ 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_params = client.get_rent_parameters().await?;

let minimum_storage_deposit = output.storage_score(rent_structure);
let minimum_storage_deposit = output.storage_cost(rent_params);

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, StorageScore, TokenId},
output::{AccountId, FoundryId, InputsCommitment, NftId, Output, OutputId, TokenId},
payload::{transaction::TransactionEssence, TransactionPayload},
rent::StorageCost,
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.storage_score(rent).to_string())
Response::MinimumRequiredStorageDeposit(out.storage_cost(rent).to_string())
}
UtilsMethod::VerifyMnemonic { mnemonic } => {
let mnemonic = Mnemonic::from(mnemonic);
Expand Down
4 changes: 2 additions & 2 deletions cli/src/command/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,12 +721,12 @@ 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_params = account.client().get_rent_parameters().await?;
let token_supply = account.client().get_token_supply().await?;

account.client().bech32_hrp_matches(address.hrp()).await?;

let outputs = [BasicOutputBuilder::new_with_minimum_storage_deposit(rent_structure)
let outputs = [BasicOutputBuilder::new_with_minimum_storage_deposit(rent_params)
.add_unlock_condition(AddressUnlockCondition::new(address))
.with_native_tokens([NativeToken::new(
TokenId::from_str(&token_id)?,
Expand Down
4 changes: 2 additions & 2 deletions sdk/examples/client/output/build_account_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ async fn main() -> Result<()> {
.await?;

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

let address = std::env::args()
.nth(1)
.unwrap_or("rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy".to_string());
let address = Address::try_from_bech32(address)?;

// Account id needs to be null the first time
let account_output = AccountOutputBuilder::new_with_minimum_storage_deposit(rent_structure, AccountId::null())
let account_output = AccountOutputBuilder::new_with_minimum_storage_deposit(rent_params, AccountId::null())
.with_state_metadata(metadata)
.add_feature(SenderFeature::new(address))
.add_feature(MetadataFeature::new(metadata)?)
Expand Down
4 changes: 2 additions & 2 deletions 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_params = client.get_rent_parameters().await?;

let address = std::env::args()
.nth(1)
Expand All @@ -56,7 +56,7 @@ async fn main() -> Result<()> {
.to_string();

// NftId needs to be null the first time
let nft_output = NftOutputBuilder::new_with_minimum_storage_deposit(rent_structure, NftId::null())
let nft_output = NftOutputBuilder::new_with_minimum_storage_deposit(rent_params, NftId::null())
.add_unlock_condition(AddressUnlockCondition::new(address))
.add_feature(SenderFeature::new(address))
.add_feature(MetadataFeature::new(MUTABLE_METADATA)?)
Expand Down
4 changes: 2 additions & 2 deletions sdk/examples/how_tos/account/state_transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ async fn main() -> Result<()> {
);

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

let account_output = account_output_data.output.as_account();
let updated_account_output = AccountOutputBuilder::from(account_output)
// Minimum required storage deposit will change if the new metadata has a different size, so we will update
// the amount
.with_minimum_storage_deposit(rent_structure)
.with_minimum_storage_deposit(rent_params)
.with_state_metadata(NEW_STATE_METADATA.as_bytes().to_vec())
.with_state_index(account_output.state_index() + 1)
.finish_output(token_supply)?;
Expand Down
4 changes: 2 additions & 2 deletions sdk/examples/how_tos/outputs/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ 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_params = client.get_rent_parameters().await?;

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

let nft_output_builder = NftOutputBuilder::new_with_minimum_storage_deposit(rent_structure, NftId::null())
let nft_output_builder = NftOutputBuilder::new_with_minimum_storage_deposit(rent_params, NftId::null())
.add_unlock_condition(AddressUnlockCondition::new(address));

let outputs = [
Expand Down
10 changes: 4 additions & 6 deletions sdk/examples/how_tos/outputs/unlock_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,17 @@ 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_params = client.get_rent_parameters().await?;

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

let token_scheme = TokenScheme::Simple(SimpleTokenScheme::new(50, 0, 100)?);

let basic_output_builder = BasicOutputBuilder::new_with_minimum_storage_deposit(rent_structure)
let basic_output_builder = BasicOutputBuilder::new_with_minimum_storage_deposit(rent_params)
.add_unlock_condition(AddressUnlockCondition::new(address));
let account_output_builder =
AccountOutputBuilder::new_with_minimum_storage_deposit(rent_structure, AccountId::null());
let foundry_output_builder =
FoundryOutputBuilder::new_with_minimum_storage_deposit(rent_structure, 1, token_scheme);
let account_output_builder = AccountOutputBuilder::new_with_minimum_storage_deposit(rent_params, AccountId::null());
let foundry_output_builder = FoundryOutputBuilder::new_with_minimum_storage_deposit(rent_params, 1, token_scheme);

let outputs = [
//// most simple output
Expand Down
4 changes: 2 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 @@ -65,7 +65,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())
.add_unlock_condition(AddressUnlockCondition::new(Address::from(Ed25519Address::from(
[0; 32],
))));
Expand Down Expand Up @@ -144,7 +144,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(),
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, StorageScore,
FoundryOutputBuilder, NftOutputBuilder, Output, OutputId,
},
rent::StorageCost,
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.storage_score(self.protocol_parameters.rent_structure());
let rent = output.storage_cost(self.protocol_parameters.rent_parameters());

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

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

/// An IOTA node client.
Expand Down Expand Up @@ -153,8 +153,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
33 changes: 27 additions & 6 deletions sdk/src/types/block/address/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ pub use self::{
ed25519::Ed25519Address,
nft::NftAddress,
};
use crate::types::block::{
output::{Output, OutputId},
semantic::{TransactionFailureReason, ValidationContext},
signature::Signature,
unlock::Unlock,
ConvertTo, Error,
use crate::{
types::block::{
output::{Output, OutputId},
rent::{RentParameters, RentStructure, StorageCost},
semantic::{TransactionFailureReason, ValidationContext},
signature::Signature,
unlock::Unlock,
ConvertTo, Error,
},
wallet::storage::Storage,
};

/// A generic address supporting different address kinds.
Expand Down Expand Up @@ -210,3 +214,20 @@ impl From<&Self> for Address {
*value
}
}

/// A trait to facilitate the rent cost computation for addresses, which is central to dust protection.
pub trait AddressStorageCost {
/// Computes the storage score of an address given a [`RentStructure`].
fn storage_cost(&self, rent_structure: RentStructure) -> u64;
}

impl AddressStorageCost for Address {
fn storage_cost(&self, rent_structure: RentStructure) -> u64 {
match self {
Self::Account(_) | Self::Ed25519(_) | Self::Nft(_) => 0,
// TODO: implicit account address and restricted address
// Address::ImplicitAccountCreation(_) =>
// rent_stucture.storage_score_offset_implicit_account_creation_address
}
}
}
2 changes: 2 additions & 0 deletions sdk/src/types/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ pub mod protocol;
/// A module that provides utilities for random generation of types.
#[cfg(feature = "rand")]
pub mod rand;
/// A module that provides types and rules for rent cost computation.
pub mod rent;
/// A module that provides types and rules for semantic validation.
pub mod semantic;
/// A module that provides types and syntactic validations of signatures.
Expand Down
26 changes: 13 additions & 13 deletions sdk/src/types/block/output/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ use crate::types::{
verify_allowed_unlock_conditions, UnlockCondition, UnlockConditionFlags, UnlockConditions,
},
verify_output_amount_min, verify_output_amount_packable, verify_output_amount_supply, ChainId, NativeToken,
NativeTokens, Output, OutputBuilderAmount, OutputId, RentStructure, StateTransitionError,
StateTransitionVerifier, StorageScore,
NativeTokens, Output, OutputBuilderAmount, OutputId, StateTransitionError, StateTransitionVerifier,
},
protocol::ProtocolParameters,
rent::{RentParameters, StorageCost},
semantic::{TransactionFailureReason, ValidationContext},
unlock::Unlock,
Error,
Expand Down Expand Up @@ -112,8 +112,8 @@ impl AccountOutputBuilder {

/// Creates an [`AccountOutputBuilder`] with a provided rent structure.
/// The amount will be set to the minimum storage deposit.
pub fn new_with_minimum_storage_deposit(rent_structure: RentStructure, account_id: AccountId) -> Self {
Self::new(OutputBuilderAmount::MinimumStorageDeposit(rent_structure), account_id)
pub fn new_with_minimum_storage_deposit(rent_params: RentParameters, account_id: AccountId) -> Self {
Self::new(OutputBuilderAmount::MinimumStorageDeposit(rent_params), account_id)
}

fn new(amount: OutputBuilderAmount, account_id: AccountId) -> Self {
Expand All @@ -140,8 +140,8 @@ impl AccountOutputBuilder {

/// Sets the amount to the minimum storage deposit.
#[inline(always)]
pub fn with_minimum_storage_deposit(mut self, rent_structure: RentStructure) -> Self {
self.amount = OutputBuilderAmount::MinimumStorageDeposit(rent_structure);
pub fn with_minimum_storage_deposit(mut self, rent_params: RentParameters) -> Self {
self.amount = OutputBuilderAmount::MinimumStorageDeposit(rent_params);
self
}

Expand Down Expand Up @@ -318,8 +318,8 @@ impl AccountOutputBuilder {

output.amount = match self.amount {
OutputBuilderAmount::Amount(amount) => amount,
OutputBuilderAmount::MinimumStorageDeposit(rent_structure) => {
Output::Account(output.clone()).storage_score(rent_structure)
OutputBuilderAmount::MinimumStorageDeposit(rent_params) => {
Output::Account(output.clone()).storage_cost(rent_params)
}
};

Expand Down Expand Up @@ -413,10 +413,10 @@ impl AccountOutput {
/// The amount will be set to the minimum storage deposit.
#[inline(always)]
pub fn build_with_minimum_storage_deposit(
rent_structure: RentStructure,
rent_params: RentParameters,
account_id: AccountId,
) -> AccountOutputBuilder {
AccountOutputBuilder::new_with_minimum_storage_deposit(rent_structure, account_id)
AccountOutputBuilder::new_with_minimum_storage_deposit(rent_params, account_id)
}

///
Expand Down Expand Up @@ -856,8 +856,8 @@ pub(crate) mod dto {
let params = params.into();
let mut builder = match amount {
OutputBuilderAmount::Amount(amount) => AccountOutputBuilder::new_with_amount(amount, *account_id),
OutputBuilderAmount::MinimumStorageDeposit(rent_structure) => {
AccountOutputBuilder::new_with_minimum_storage_deposit(rent_structure, *account_id)
OutputBuilderAmount::MinimumStorageDeposit(rent_params) => {
AccountOutputBuilder::new_with_minimum_storage_deposit(rent_params, *account_id)
}
}
.with_mana(mana);
Expand Down Expand Up @@ -978,7 +978,7 @@ mod tests {
test_split_dto(builder);

let builder =
AccountOutput::build_with_minimum_storage_deposit(protocol_parameters.rent_structure(), account_id)
AccountOutput::build_with_minimum_storage_deposit(protocol_parameters.rent_parameters(), account_id)
.add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000).unwrap())
.add_unlock_condition(gov_address)
.add_unlock_condition(state_address)
Expand Down
Loading

0 comments on commit d6f12e5

Please sign in to comment.