Skip to content

Commit

Permalink
[r2r] Retry tx wait confirmation if not on chain (#1474)
Browse files Browse the repository at this point in the history
* enable retry if tx is not available onchain

* fixed review notes

* fixed failing tests

* pr review fixes

* set test timeout to 1
  • Loading branch information
borngraced authored Sep 27, 2022
1 parent 67ede86 commit b7169e4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
18 changes: 17 additions & 1 deletion mm2src/coins/utxo/rpc_clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ cfg_native! {
}

pub const NO_TX_ERROR_CODE: &str = "'code': -5";
const TX_NOT_FOUND_RETRIES: u8 = 10;

pub type AddressesByLabelResult = HashMap<String, AddressPurpose>;
pub type JsonRpcPendingRequestsShared = Arc<AsyncMutex<JsonRpcPendingRequests>>;
Expand Down Expand Up @@ -147,6 +148,7 @@ impl UtxoRpcClientEnum {
check_every: u64,
) -> Box<dyn Future<Item = (), Error = String> + Send> {
let selfi = self.clone();
let mut tx_not_found_retries = TX_NOT_FOUND_RETRIES;
let fut = async move {
loop {
if now_ms() / 1000 > wait_until {
Expand Down Expand Up @@ -176,7 +178,21 @@ impl UtxoRpcClientEnum {
},
Err(e) => {
if e.get_inner().is_tx_not_found_error() {
return ERR!("Tx {} is not on chain anymore", tx_hash);
if tx_not_found_retries == 0 {
return ERR!(
"Tx {} was not found on chain after {} tries, error: {}",
tx_hash,
TX_NOT_FOUND_RETRIES,
e,
);
}
error!(
"Tx {} not found on chain, error: {}, retrying in 10 seconds. Retries left: {}",
tx_hash, e, tx_not_found_retries
);
tx_not_found_retries -= 1;
Timer::sleep(check_every as f64).await;
continue;
};

if expiry_height > 0 {
Expand Down
8 changes: 3 additions & 5 deletions mm2src/coins/utxo/utxo_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4054,16 +4054,14 @@ fn test_for_non_existent_tx_hex_utxo_electrum() {
);
// bad transaction hex
let tx = hex::decode("0400008085202f8902bf17bf7d1daace52e08f732a6b8771743ca4b1cb765a187e72fd091a0aabfd52000000006a47304402203eaaa3c4da101240f80f9c5e9de716a22b1ec6d66080de6a0cca32011cd77223022040d9082b6242d6acf9a1a8e658779e1c655d708379862f235e8ba7b8ca4e69c6012102031d4256c4bc9f99ac88bf3dba21773132281f65f9bf23a59928bce08961e2f3ffffffffff023ca13c0e9e085dd13f481f193e8a3e8fd609020936e98b5587342d994f4d020000006b483045022100c0ba56adb8de923975052312467347d83238bd8d480ce66e8b709a7997373994022048507bcac921fdb2302fa5224ce86e41b7efc1a2e20ae63aa738dfa99b7be826012102031d4256c4bc9f99ac88bf3dba21773132281f65f9bf23a59928bce08961e2f3ffffffff0300e1f5050000000017a9141ee6d4c38a3c078eab87ad1a5e4b00f21259b10d87000000000000000016611400000000000000000000000000000000000000001b94d736000000001976a91405aab5342166f8594baf17a7d9bef5d56744332788ac2d08e35e000000000000000000000000000000").unwrap();
let expected_timeout = (now_ms() / 1000) + 10;
let actual = coin
.wait_for_confirmations(&tx, 1, false, timeout, 1)
.wait()
.err()
.unwrap();
assert!(
actual.contains("Tx d342ff9da528a2e262bddf2b6f9a27d1beb7aeb03f0fc8d9eac2987266447e44 is not on chain anymore")
);
assert!((now_ms() / 1000) < expected_timeout);
assert!(actual.contains(
"Tx d342ff9da528a2e262bddf2b6f9a27d1beb7aeb03f0fc8d9eac2987266447e44 was not found on chain after 10 tries"
));
}

#[test]
Expand Down
7 changes: 3 additions & 4 deletions mm2src/mm2_main/src/docker_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,15 +436,14 @@ mod docker_tests {
let (_ctx, coin, _) = generate_utxo_coin_with_random_privkey("MYCOIN", 1000u64.into());
// bad transaction hex
let tx = hex::decode("0400008085202f8902bf17bf7d1daace52e08f732a6b8771743ca4b1cb765a187e72fd091a0aabfd52000000006a47304402203eaaa3c4da101240f80f9c5e9de716a22b1ec6d66080de6a0cca32011cd77223022040d9082b6242d6acf9a1a8e658779e1c655d708379862f235e8ba7b8ca4e69c6012102031d4256c4bc9f99ac88bf3dba21773132281f65f9bf23a59928bce08961e2f3ffffffffff023ca13c0e9e085dd13f481f193e8a3e8fd609020936e98b5587342d994f4d020000006b483045022100c0ba56adb8de923975052312467347d83238bd8d480ce66e8b709a7997373994022048507bcac921fdb2302fa5224ce86e41b7efc1a2e20ae63aa738dfa99b7be826012102031d4256c4bc9f99ac88bf3dba21773132281f65f9bf23a59928bce08961e2f3ffffffff0300e1f5050000000017a9141ee6d4c38a3c078eab87ad1a5e4b00f21259b10d87000000000000000016611400000000000000000000000000000000000000001b94d736000000001976a91405aab5342166f8594baf17a7d9bef5d56744332788ac2d08e35e000000000000000000000000000000").unwrap();
let expected_timeout = (now_ms() / 1000) + 10;
let actual = coin
.wait_for_confirmations(&tx, 1, false, timeout, 1)
.wait()
.err()
.unwrap();
assert!(actual
.contains("Tx d342ff9da528a2e262bddf2b6f9a27d1beb7aeb03f0fc8d9eac2987266447e44 is not on chain anymore"));
assert!((now_ms() / 1000) < expected_timeout);
assert!(actual.contains(
"Tx d342ff9da528a2e262bddf2b6f9a27d1beb7aeb03f0fc8d9eac2987266447e44 was not found on chain after 10 tries"
));
}

#[test]
Expand Down

0 comments on commit b7169e4

Please sign in to comment.