Skip to content

Commit

Permalink
fix(p2pk-tests): fix p2pk tests post merge (#2119)
Browse files Browse the repository at this point in the history
  • Loading branch information
shamardy committed May 17, 2024
1 parent df6ab98 commit 2bbb75c
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 40 deletions.
4 changes: 3 additions & 1 deletion mm2src/coins/rpc_command/tendermint/ibc_transfer_channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ pub async fn ibc_transfer_channels(ctx: MmArc, req: IBCTransferChannelsRequest)
.map(|t| t.as_str().unwrap_or_default().to_owned());

let Some(destination_registry_name) = destination_registry_name else {
return MmError::err(IBCTransferChannelsRequestError::RegistryNameIsMissing(req.destination_coin));
return MmError::err(IBCTransferChannelsRequestError::RegistryNameIsMissing(
req.destination_coin,
));
};

get_ibc_transfer_channels(source_registry_name, destination_registry_name).await
Expand Down
30 changes: 13 additions & 17 deletions mm2src/coins/tendermint/tendermint_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -902,20 +902,18 @@ impl TendermintCoin {
memo: String,
withdraw_fee: Option<WithdrawFee>,
) -> MmResult<Fee, TendermintCoinRpcError> {
let Ok(activated_priv_key) = self
.activation_policy
.activated_key_or_err() else {
let (gas_price, gas_limit) = self.gas_info_for_withdraw(&withdraw_fee, GAS_LIMIT_DEFAULT);
let amount = ((GAS_WANTED_BASE_VALUE * 1.5) * gas_price).ceil();

let fee_amount = Coin {
denom: self.platform_denom().clone(),
amount: (amount as u64).into(),
};
let Ok(activated_priv_key) = self.activation_policy.activated_key_or_err() else {
let (gas_price, gas_limit) = self.gas_info_for_withdraw(&withdraw_fee, GAS_LIMIT_DEFAULT);
let amount = ((GAS_WANTED_BASE_VALUE * 1.5) * gas_price).ceil();

return Ok(Fee::from_amount_and_gas(fee_amount, gas_limit));
let fee_amount = Coin {
denom: self.platform_denom().clone(),
amount: (amount as u64).into(),
};

return Ok(Fee::from_amount_and_gas(fee_amount, gas_limit));
};

let (response, raw_response) = loop {
let account_info = self.account_info(&self.account_id).await?;
let tx_bytes = self
Expand Down Expand Up @@ -986,12 +984,10 @@ impl TendermintCoin {
withdraw_fee: Option<WithdrawFee>,
) -> MmResult<u64, TendermintCoinRpcError> {
let account_id = &self.account_id;
let Ok(priv_key) = self
.activation_policy
.activated_key_or_err() else {
let (gas_price, _) = self.gas_info_for_withdraw(&withdraw_fee, 0);
return Ok(((GAS_WANTED_BASE_VALUE * 1.5) * gas_price).ceil() as u64);
};
let Ok(priv_key) = self.activation_policy.activated_key_or_err() else {
let (gas_price, _) = self.gas_info_for_withdraw(&withdraw_fee, 0);
return Ok(((GAS_WANTED_BASE_VALUE * 1.5) * gas_price).ceil() as u64);
};

let (response, raw_response) = loop {
let account_info = self.account_info(account_id).await?;
Expand Down
6 changes: 4 additions & 2 deletions mm2src/coins/tx_history_storage/sql_tx_history_storage_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,10 @@ impl TxHistoryStorage for SqliteTxHistoryStorage {
let sql_transaction = conn.transaction()?;

for tx in transactions {
let Some(tx_hash) = tx.tx.tx_hash() else {continue};
let Some(tx_hex) = tx.tx.tx_hex().cloned() else {continue};
let Some(tx_hash) = tx.tx.tx_hash() else { continue };
let Some(tx_hex) = tx.tx.tx_hex().cloned() else {
continue;
};
let tx_hex = format!("{:02x}", tx_hex);

let internal_id = format!("{:02x}", tx.internal_id);
Expand Down
28 changes: 17 additions & 11 deletions mm2src/coins/utxo/utxo_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,18 +679,24 @@ impl<'a, T: AsRef<UtxoCoinFields> + UtxoTxGenerationOps> UtxoTxBuilder<'a, T> {
None
};

for utxo in self.available_inputs.clone() {
if self.update_fee_and_check_completeness(from.addr_format(), &actual_tx_fee) {
break;
}
// The function `update_fee_and_check_completeness` checks if the total value of the current inputs
// (added using add_required_inputs or directly) is enough to cover the transaction outputs and fees.
// If it returns `true`, it indicates that no additional inputs are needed from the available inputs,
// and we can skip the loop that adds these additional inputs.
if !self.update_fee_and_check_completeness(from.addr_format(), &actual_tx_fee) {
for utxo in self.available_inputs.clone() {
self.tx.inputs.push(UnsignedTransactionInput {
previous_output: utxo.outpoint,
prev_script: utxo.script,
sequence: SEQUENCE_FINAL,
amount: utxo.value,
});
self.sum_inputs += utxo.value;

self.tx.inputs.push(UnsignedTransactionInput {
previous_output: utxo.outpoint,
prev_script: utxo.script,
sequence: SEQUENCE_FINAL,
amount: utxo.value,
});
self.sum_inputs += utxo.value;
if self.update_fee_and_check_completeness(from.addr_format(), &actual_tx_fee) {
break;
}
}
}

match self.fee_policy {
Expand Down
4 changes: 3 additions & 1 deletion mm2src/coins/utxo/utxo_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3187,6 +3187,7 @@ fn test_withdraw_to_p2pk_fails() {
max: false,
fee: None,
memo: None,
ibc_source_channel: None,
};

assert!(matches!(
Expand Down Expand Up @@ -3403,9 +3404,10 @@ fn test_withdraw_p2pk_balance() {
max: false,
fee: None,
memo: None,
ibc_source_channel: None,
};
let tx_details = coin.withdraw(withdraw_req).wait().unwrap();
let transaction: UtxoTx = deserialize(tx_details.tx_hex.as_slice()).unwrap();
let transaction: UtxoTx = deserialize(tx_details.tx.tx_hex().unwrap().as_slice()).unwrap();

// The change should be in a p2pkh script.
let output_script: Script = transaction.outputs[1].script_pubkey.clone().into();
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/utxo/utxo_tx_history_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ where

let txs_with_height: HashMap<H256Json, u64> = self.all_tx_ids_with_height.clone().into_iter().collect();
for mut tx in unconfirmed {
let Some(tx_hash) = tx.tx.tx_hash() else {continue};
let Some(tx_hash) = tx.tx.tx_hash() else { continue };

let found = match H256Json::from_str(tx_hash) {
Ok(unconfirmed_tx_hash) => txs_with_height.get(&unconfirmed_tx_hash),
Expand Down
14 changes: 7 additions & 7 deletions mm2src/mm2_main/tests/mm2_tests/mm2_tests_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3730,12 +3730,12 @@ fn test_tx_history_segwit() {
let (_dump_log, _dump_dashboard) = mm.mm_dump();
log!("log path: {}", mm.log_path.display());

// enable tBTC to see that to/from segwit addresses are displayed correctly in tx_history
// enable tBTC-Segwit to see that to/from segwit addresses are displayed correctly in tx_history
// and that tx_history is retrieved for the segwit address instead of legacy
let electrum = block_on(enable_electrum(&mm, "tBTC-Segwit", false, TBTC_ELECTRUMS));
let electrum = block_on(enable_electrum(&mm, "tBTC-Segwit", true, TBTC_ELECTRUMS));
assert_eq!(&electrum.address, "tb1qdkwjk42dw6pryvs9sl0ht3pn3mxghuma64jst5");

block_on(wait_till_history_has_records(&mm, "tBTC", 13));
block_on(wait_till_history_has_records(&mm, "tBTC-Segwit", 13));

let tx_history = block_on(mm.rpc(&json!({
"userpass": mm.userpass,
Expand Down Expand Up @@ -3811,7 +3811,7 @@ fn test_tx_history_segwit() {
#[cfg(not(target_arch = "wasm32"))]
fn test_tx_history_tbtc_non_segwit() {
let passphrase = "also shoot benefit prefer juice shell elder veteran woman mimic image kidney";
let coins = json!([tbtc_segwit_conf(),]);
let coins = json!([tbtc_conf(),]);

let conf = Mm2TestConf::seednode(passphrase, &coins);
let mm = MarketMakerIt::start(conf.conf, conf.rpc_password, None).unwrap();
Expand All @@ -3820,7 +3820,7 @@ fn test_tx_history_tbtc_non_segwit() {
log!("log path: {}", mm.log_path.display());

// enable tBTC in legacy first to see that to/from segwit addresses are displayed correctly in tx_history
let electrum = block_on(enable_electrum(&mm, "tBTC-Segwit", false, TBTC_ELECTRUMS));
let electrum = block_on(enable_electrum(&mm, "tBTC", true, TBTC_ELECTRUMS));
assert_eq!(&electrum.address, "mqWYEGxLeK843n3xMTe8EWTFPyoSZjtUXb");

let expected = vec![
Expand Down Expand Up @@ -3872,12 +3872,12 @@ fn test_tx_history_tbtc_non_segwit() {
"649d514d76702a0925a917d830e407f4f1b52d78832520e486c140ce8d0b879f",
];

block_on(wait_till_history_has_records(&mm, "tBTC-Segwit", expected.len()));
block_on(wait_till_history_has_records(&mm, "tBTC", expected.len()));

let tx_history = block_on(mm.rpc(&json!({
"userpass": mm.userpass,
"method": "my_tx_history",
"coin": "tBTC-Segwit",
"coin": "tBTC",
"limit": 100,
})))
.unwrap();
Expand Down

0 comments on commit 2bbb75c

Please sign in to comment.