From 13010845fbbac79f313afb37cbbe2918d606ad3e Mon Sep 17 00:00:00 2001 From: laruh Date: Thu, 7 Nov 2024 19:28:10 +0700 Subject: [PATCH] review: remove redundant validation --- mm2src/coins/eth/eth_swap_v2/eth_maker_swap_v2.rs | 13 +++++++------ mm2src/coins/eth/eth_swap_v2/eth_taker_swap_v2.rs | 13 +++++++------ mm2src/coins/eth/eth_swap_v2/mod.rs | 12 +----------- 3 files changed, 15 insertions(+), 23 deletions(-) diff --git a/mm2src/coins/eth/eth_swap_v2/eth_maker_swap_v2.rs b/mm2src/coins/eth/eth_swap_v2/eth_maker_swap_v2.rs index 8837b8ce0d..3089604ede 100644 --- a/mm2src/coins/eth/eth_swap_v2/eth_maker_swap_v2.rs +++ b/mm2src/coins/eth/eth_swap_v2/eth_maker_swap_v2.rs @@ -1,4 +1,4 @@ -use super::{validate_from_to_and_status, validate_payment_args, EthPaymentType, PaymentMethod, PrepareTxDataError, +use super::{validate_amount, validate_from_to_and_status, EthPaymentType, PaymentMethod, PrepareTxDataError, ZERO_VALUE}; use crate::coin_errors::{ValidatePaymentError, ValidatePaymentResult}; use crate::eth::{decode_contract_call, get_function_input_data, wei_from_big_decimal, EthCoin, EthCoinType, @@ -132,9 +132,9 @@ impl EthCoin { ValidatePaymentError::InternalError("Expected swap_v2_contracts to be Some, but found None".to_string()) })? .maker_swap_v2_contract; - validate_payment_args(args.taker_secret_hash, args.maker_secret_hash, &args.amount) - .map_to_mm(ValidatePaymentError::InternalError)?; - let maker_address = public_to_address(args.maker_pub); + let taker_secret_hash = args.taker_secret_hash.try_into()?; + let maker_secret_hash = args.maker_secret_hash.try_into()?; + validate_amount(&args.amount).map_to_mm(ValidatePaymentError::InternalError)?; let swap_id = self.etomic_swap_id_v2(args.time_lock, args.maker_secret_hash); let maker_status = self .payment_status_v2( @@ -155,6 +155,7 @@ impl EthCoin { args.maker_payment_tx.tx_hash() )) })?; + let maker_address = public_to_address(args.maker_pub); validate_from_to_and_status( tx_from_rpc, maker_address, @@ -169,8 +170,8 @@ impl EthCoin { swap_id, amount, taker: self.my_addr().await, - taker_secret_hash: args.taker_secret_hash.try_into()?, - maker_secret_hash: args.maker_secret_hash.try_into()?, + taker_secret_hash, + maker_secret_hash, payment_time_lock: args.time_lock, } }; diff --git a/mm2src/coins/eth/eth_swap_v2/eth_taker_swap_v2.rs b/mm2src/coins/eth/eth_swap_v2/eth_taker_swap_v2.rs index 8dd686e064..fea91b0408 100644 --- a/mm2src/coins/eth/eth_swap_v2/eth_taker_swap_v2.rs +++ b/mm2src/coins/eth/eth_swap_v2/eth_taker_swap_v2.rs @@ -1,4 +1,4 @@ -use super::{check_decoded_length, validate_from_to_and_status, validate_payment_args, validate_payment_state, +use super::{check_decoded_length, validate_amount, validate_from_to_and_status, validate_payment_state, EthPaymentType, PaymentMethod, PrepareTxDataError, ZERO_VALUE}; use crate::eth::{decode_contract_call, get_function_input_data, wei_from_big_decimal, EthCoin, EthCoinType, ParseCoinAssocTypes, RefundFundingSecretArgs, RefundTakerPaymentArgs, SendTakerFundingArgs, @@ -154,9 +154,9 @@ impl EthCoin { ValidateSwapV2TxError::Internal("Expected swap_v2_contracts to be Some, but found None".to_string()) })? .taker_swap_v2_contract; - validate_payment_args(args.taker_secret_hash, args.maker_secret_hash, &args.trading_amount) - .map_err(ValidateSwapV2TxError::Internal)?; - let taker_address = public_to_address(args.taker_pub); + let taker_secret_hash = args.taker_secret_hash.try_into()?; + let maker_secret_hash = args.maker_secret_hash.try_into()?; + validate_amount(&args.trading_amount).map_err(ValidateSwapV2TxError::Internal)?; let swap_id = self.etomic_swap_id_v2(args.payment_time_lock, args.maker_secret_hash); let taker_status = self .payment_status_v2( @@ -175,6 +175,7 @@ impl EthCoin { args.funding_tx.tx_hash() )) })?; + let taker_address = public_to_address(args.taker_pub); validate_from_to_and_status( tx_from_rpc, taker_address, @@ -191,8 +192,8 @@ impl EthCoin { amount: payment_amount, dex_fee, receiver: self.my_addr().await, - taker_secret_hash: args.taker_secret_hash.try_into()?, - maker_secret_hash: args.maker_secret_hash.try_into()?, + taker_secret_hash, + maker_secret_hash, funding_time_lock: args.funding_time_lock, payment_time_lock: args.payment_time_lock, } diff --git a/mm2src/coins/eth/eth_swap_v2/mod.rs b/mm2src/coins/eth/eth_swap_v2/mod.rs index ff31330ce3..798a232d56 100644 --- a/mm2src/coins/eth/eth_swap_v2/mod.rs +++ b/mm2src/coins/eth/eth_swap_v2/mod.rs @@ -154,20 +154,10 @@ pub(crate) fn validate_from_to_and_status( } // TODO validate premium when add its support in swap_v2 -fn validate_payment_args<'a>( - taker_secret_hash: &'a [u8], - maker_secret_hash: &'a [u8], - trading_amount: &BigDecimal, -) -> Result<(), String> { +fn validate_amount(trading_amount: &BigDecimal) -> Result<(), String> { if !trading_amount.is_positive() { return Err("trading_amount must be a positive value".to_string()); } - if taker_secret_hash.len() != 32 { - return Err("taker_secret_hash must be 32 bytes".to_string()); - } - if maker_secret_hash.len() != 32 { - return Err("maker_secret_hash must be 32 bytes".to_string()); - } Ok(()) }