Skip to content

Commit

Permalink
Fix clippy warnings (#2157)
Browse files Browse the repository at this point in the history
* fix clippy

* nit

---------

Co-authored-by: Thibault Martinez <[email protected]>
  • Loading branch information
DaughterOfMars and thibault-martinez authored Mar 7, 2024
1 parent b0baecd commit ee51d3e
Show file tree
Hide file tree
Showing 32 changed files with 169 additions and 131 deletions.
4 changes: 2 additions & 2 deletions bindings/core/src/method/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use std::path::PathBuf;

use crypto::keys::bip44::Bip44;
use derivative::Derivative;
use iota_sdk::client::api::options::TransactionOptions;
#[cfg(feature = "events")]
use iota_sdk::wallet::events::types::{WalletEvent, WalletEventType};
use iota_sdk::{client::api::options::TransactionOptions, utils::serde::string};
// #[cfg(feature = "participation")]
// use iota_sdk::{
// client::node_manager::node::Node,
Expand All @@ -22,7 +22,7 @@ use iota_sdk::{
secret::GenerateAddressOptions,
},
types::block::{
address::{Bech32Address, Hrp},
address::Hrp,
output::{AccountId, DelegationId, Output, OutputId, TokenId},
payload::signed_transaction::TransactionId,
},
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/client/send_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let native_token_return =
BasicOutputBuilder::new_with_minimum_amount(protocol_parameters.storage_score_parameters())
.add_unlock_condition(AddressUnlockCondition::new(from_address.clone()))
.with_native_token(native_token.clone())
.with_native_token(*native_token)
.finish_output()?;
total_amount -= native_token_return.amount();
outputs.push(native_token_return);
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/client/api/block_builder/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl SignedTransactionData {
.collect::<Vec<(&OutputId, &Output)>>();

let context = SemanticValidationContext::new(
&self.payload.transaction(),
self.payload.transaction(),
&inputs,
Some(self.payload.unlocks()),
Some(&self.mana_rewards),
Expand Down
10 changes: 5 additions & 5 deletions sdk/src/client/api/block_builder/transaction_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ impl Client {
/// Builds a transaction using the given inputs, outputs, addresses, and options.
pub async fn build_transaction(
&self,
addresses: impl IntoIterator<Item = (Address, Bip44)>,
outputs: impl IntoIterator<Item = Output>,
addresses: impl IntoIterator<Item = (Address, Bip44)> + Send,
outputs: impl IntoIterator<Item = Output> + Send,
options: TransactionOptions,
) -> Result<PreparedTransactionData, ClientError> {
let addresses = addresses.into_iter().collect::<HashMap<_, _>>();
Expand Down Expand Up @@ -93,9 +93,9 @@ impl Client {
/// Builds a transaction using the given inputs, outputs, addresses, and options.
pub(crate) async fn build_transaction_inner(
&self,
addresses: impl IntoIterator<Item = Address>,
available_inputs: impl IntoIterator<Item = InputSigningData>,
outputs: impl IntoIterator<Item = Output>,
addresses: impl IntoIterator<Item = Address> + Send,
available_inputs: impl IntoIterator<Item = InputSigningData> + Send,
outputs: impl IntoIterator<Item = Output> + Send,
options: TransactionOptions,
slot_commitment_id: SlotCommitmentId,
protocol_parameters: ProtocolParameters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ impl TransactionBuilder {
let mut selected_mana = 0;
let include_generated = include_generated
.into()
.unwrap_or(self.burn.as_ref().map_or(true, |b| !b.generated_mana()));
.unwrap_or_else(|| self.burn.as_ref().map_or(true, |b| !b.generated_mana()));

for input in &self.selected_inputs {
selected_mana += self.total_mana(input, include_generated)?;
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/client/api/wait_for_tx_acceptance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ impl Client {
tokio::time::sleep(duration).await;
}

Err(ClientError::TransactionAcceptance(transaction_id.to_string()).into())
Err(ClientError::TransactionAcceptance(transaction_id.to_string()))
}
}
9 changes: 8 additions & 1 deletion sdk/src/client/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ pub enum ClientError {
#[cfg(feature = "ledger_nano")]
#[cfg_attr(docsrs, doc(cfg(feature = "ledger_nano")))]
#[error("{0}")]
Ledger(#[from] crate::client::secret::ledger_nano::Error),
Ledger(Box<crate::client::secret::ledger_nano::Error>),

/// MQTT error
#[cfg(feature = "mqtt")]
Expand Down Expand Up @@ -236,3 +236,10 @@ crate::impl_from_error_via!(ClientError via BlockError:
UnlockError,
SignatureError,
);

#[cfg(feature = "ledger_nano")]
impl From<crate::client::secret::ledger_nano::Error> for ClientError {
fn from(value: crate::client::secret::ledger_nano::Error) -> Self {
Self::Ledger(value.into())
}
}
2 changes: 1 addition & 1 deletion sdk/src/types/block/output/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ fn verify_unlock_conditions(unlock_conditions: &UnlockConditions, account_id: &A
fn verify_staking(amount: u64, features: &Features) -> Result<(), OutputError> {
if let Some(staking) = features.staking() {
if features.block_issuer().is_none() {
return Err(FeatureError::StakingBlockIssuerMissing)?;
return Err(FeatureError::StakingBlockIssuerMissing.into());
}
if amount < staking.staked_amount() {
return Err(OutputError::InvalidStakedAmount);
Expand Down
69 changes: 50 additions & 19 deletions sdk/src/types/block/semantic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ impl<'a> SemanticValidationContext<'a> {
input_amount: 0,
input_mana: 0,
mana_rewards,
commitment_context_input: None,
commitment_context_input: transaction
.context_inputs()
.commitment()
.map(|c| c.slot_commitment_id()),
reward_context_inputs: Default::default(),
input_native_tokens: BTreeMap::<TokenId, U256>::new(),
input_chains,
Expand All @@ -107,28 +110,39 @@ impl<'a> SemanticValidationContext<'a> {

///
pub fn validate(mut self) -> Result<(), TransactionFailureReason> {
self.commitment_context_input = self
.transaction
.context_inputs()
.commitment()
.map(|c| c.slot_commitment_id());
self.validate_reward_context_inputs()?;

let bic_context_inputs = self
.transaction
.context_inputs()
.block_issuance_credits()
.map(|bic| *bic.account_id())
.collect::<HashSet<_>>();
self.validate_inputs()?;

self.validate_outputs()?;

self.validate_storage_deposit_returns()?;

self.validate_balances()?;

self.validate_transitions()?;

Ok(())
}

fn validate_reward_context_inputs(&mut self) -> Result<(), TransactionFailureReason> {
for reward_context_input in self.transaction.context_inputs().rewards() {
if let Some(output_id) = self.inputs.get(reward_context_input.index() as usize).map(|v| v.0) {
self.reward_context_inputs.insert(*output_id, *reward_context_input);
} else {
return Err(TransactionFailureReason::RewardInputReferenceInvalid);
}
}
Ok(())
}

// Validation of inputs.
fn validate_inputs(&mut self) -> Result<(), TransactionFailureReason> {
let bic_context_inputs = self
.transaction
.context_inputs()
.block_issuance_credits()
.map(|bic| *bic.account_id())
.collect::<HashSet<_>>();

let mut has_implicit_account_creation_address = false;

Expand All @@ -155,7 +169,7 @@ impl<'a> SemanticValidationContext<'a> {
.checked_add(
consumed_output
.available_mana(
&self.protocol_parameters,
self.protocol_parameters,
output_id.transaction_id().slot_index(),
self.transaction.creation_slot(),
)
Expand Down Expand Up @@ -230,7 +244,7 @@ impl<'a> SemanticValidationContext<'a> {
.checked_add(
consumed_output
.available_mana(
&self.protocol_parameters,
self.protocol_parameters,
output_id.transaction_id().slot_index(),
self.transaction.creation_slot(),
)
Expand Down Expand Up @@ -266,6 +280,17 @@ impl<'a> SemanticValidationContext<'a> {
}
}

Ok(())
}

fn validate_outputs(&mut self) -> Result<(), TransactionFailureReason> {
let bic_context_inputs = self
.transaction
.context_inputs()
.block_issuance_credits()
.map(|bic| *bic.account_id())
.collect::<HashSet<_>>();

// Add allotted mana
for mana_allotment in self.transaction.allotments().iter() {
self.output_mana = self
Expand All @@ -274,7 +299,6 @@ impl<'a> SemanticValidationContext<'a> {
.ok_or(TransactionFailureReason::ManaOverflow)?;
}

// Validation of outputs.
for (index, created_output) in self.transaction.outputs().iter().enumerate() {
let (amount, mana, created_native_token, features) = match created_output {
Output::Basic(output) => {
Expand Down Expand Up @@ -377,8 +401,10 @@ impl<'a> SemanticValidationContext<'a> {
.ok_or(TransactionFailureReason::SemanticValidationFailed)?;
}
}
Ok(())
}

// Validation of storage deposit returns.
fn validate_storage_deposit_returns(&mut self) -> Result<(), TransactionFailureReason> {
for (return_address, return_amount) in self.storage_deposit_returns.iter() {
if let Some(deposit_amount) = self.simple_deposits.get(return_address) {
if deposit_amount < return_amount {
Expand All @@ -388,8 +414,11 @@ impl<'a> SemanticValidationContext<'a> {
return Err(TransactionFailureReason::ReturnAmountNotFulFilled);
}
}
Ok(())
}

// Validation of amounts.
fn validate_balances(&mut self) -> Result<(), TransactionFailureReason> {
// Validation of amounts
if self.input_amount != self.output_amount {
return Err(TransactionFailureReason::InputOutputBaseTokenMismatch);
}
Expand Down Expand Up @@ -422,7 +451,10 @@ impl<'a> SemanticValidationContext<'a> {
return Err(TransactionFailureReason::NativeTokenSumUnbalanced);
}
}
Ok(())
}

fn validate_transitions(&mut self) -> Result<(), TransactionFailureReason> {
// Validation of state transitions and destructions.
for (chain_id, current_state) in self.input_chains.iter() {
self.verify_state_transition(
Expand All @@ -437,7 +469,6 @@ impl<'a> SemanticValidationContext<'a> {
self.verify_state_transition(None, Some((&next_state.0, next_state.1)))?;
}
}

Ok(())
}
}
2 changes: 1 addition & 1 deletion sdk/src/wallet/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ impl From<crate::client::stronghold::Error> for WalletError {
#[cfg(feature = "ledger_nano")]
impl From<crate::client::secret::ledger_nano::Error> for WalletError {
fn from(error: crate::client::secret::ledger_nano::Error) -> Self {
Self::Client(ClientError::Ledger(error))
Self::Client(ClientError::from(error))
}
}

Expand Down
14 changes: 7 additions & 7 deletions sdk/tests/client/signing/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async fn sign_account_state_transition() -> Result<(), Box<dyn std::error::Error
Account {
amount: 1_000_000,
mana: 0,
account_id: account_id,
account_id,
address: address.clone(),
sender: None,
issuer: None,
Expand All @@ -68,7 +68,7 @@ async fn sign_account_state_transition() -> Result<(), Box<dyn std::error::Error
let outputs = build_outputs([Account {
amount: 1_000_000,
mana: 0,
account_id: account_id,
account_id,
address: address.clone(),
sender: None,
issuer: None,
Expand All @@ -94,7 +94,7 @@ async fn sign_account_state_transition() -> Result<(), Box<dyn std::error::Error
};

let unlocks = secret_manager
.transaction_unlocks(&prepared_transaction_data, &protocol_parameters)
.transaction_unlocks(&prepared_transaction_data, protocol_parameters)
.await?;

assert_eq!(unlocks.len(), 1);
Expand Down Expand Up @@ -134,7 +134,7 @@ async fn account_reference_unlocks() -> Result<(), Box<dyn std::error::Error>> {
Account {
amount: 1_000_000,
mana: 0,
account_id: account_id,
account_id,
address: address.clone(),
sender: None,
issuer: None,
Expand Down Expand Up @@ -175,8 +175,8 @@ async fn account_reference_unlocks() -> Result<(), Box<dyn std::error::Error>> {
Account {
amount: 1_000_000,
mana: 0,
account_id: account_id,
address: address,
account_id,
address,
sender: None,
issuer: None,
},
Expand Down Expand Up @@ -212,7 +212,7 @@ async fn account_reference_unlocks() -> Result<(), Box<dyn std::error::Error>> {
};

let unlocks = secret_manager
.transaction_unlocks(&prepared_transaction_data, &protocol_parameters)
.transaction_unlocks(&prepared_transaction_data, protocol_parameters)
.await?;

assert_eq!(unlocks.len(), 3);
Expand Down
6 changes: 3 additions & 3 deletions sdk/tests/client/signing/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ async fn single_ed25519_unlock() -> Result<(), Box<dyn std::error::Error>> {
};

let unlocks = secret_manager
.transaction_unlocks(&prepared_transaction_data, &protocol_parameters)
.transaction_unlocks(&prepared_transaction_data, protocol_parameters)
.await?;

assert_eq!(unlocks.len(), 1);
Expand Down Expand Up @@ -197,7 +197,7 @@ async fn ed25519_reference_unlocks() -> Result<(), Box<dyn std::error::Error>> {
};

let unlocks = secret_manager
.transaction_unlocks(&prepared_transaction_data, &protocol_parameters)
.transaction_unlocks(&prepared_transaction_data, protocol_parameters)
.await?;

assert_eq!(unlocks.len(), 3);
Expand Down Expand Up @@ -313,7 +313,7 @@ async fn two_signature_unlocks() -> Result<(), Box<dyn std::error::Error>> {
};

let unlocks = secret_manager
.transaction_unlocks(&prepared_transaction_data, &protocol_parameters)
.transaction_unlocks(&prepared_transaction_data, protocol_parameters)
.await?;

assert_eq!(unlocks.len(), 2);
Expand Down
Loading

0 comments on commit ee51d3e

Please sign in to comment.