Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

313 - retry integration tests for tx_bad_seq errors #447

Merged
merged 3 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 53 additions & 14 deletions clients/vault/tests/helper/helper.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::helper::DEFAULT_TESTING_CURRENCY;
use async_trait::async_trait;
use frame_support::assert_ok;
use primitives::{CurrencyId, StellarStroops, H256};
use primitives::{stellar::Asset as StellarAsset, CurrencyId, StellarStroops, H256};
use runtime::{
integration::{
assert_event, get_required_vault_collateral_for_issue, setup_provider, SubxtClient,
Expand All @@ -14,7 +14,7 @@ use sp_runtime::traits::StaticLookup;
use std::{sync::Arc, time::Duration};
use stellar_relay_lib::sdk::PublicKey;
use vault::{oracle::OracleAgent, ArcRwLock};
use wallet::StellarWallet;
use wallet::{error::Error, StellarWallet, TransactionResponse};

pub fn default_destination() -> SecretKey {
SecretKey::from_encoding(crate::helper::DEFAULT_TESTNET_DEST_SECRET_KEY).expect("Should work")
Expand Down Expand Up @@ -111,6 +111,46 @@ pub async fn assert_execute_redeem_event(
.await
}

pub async fn send_payment_to_address(
wallet: ArcRwLock<StellarWallet>,
destination_address: PublicKey,
asset: StellarAsset,
stroop_amount: StellarStroops,
request_id: [u8; 32],
stroop_fee_per_operation: u32,
is_payment_for_redeem_request: bool,
) -> Result<TransactionResponse, Error> {
let response;
loop {
let result = wallet
.write()
.await
.send_payment_to_address(
destination_address.clone(),
asset.clone(),
stroop_amount,
request_id,
stroop_fee_per_operation,
is_payment_for_redeem_request,
)
.await;

match &result {
// if the error is `tx_bad_seq` perform the process again
Err(Error::HorizonSubmissionError { reason, .. }) if reason.contains("tx_bad_seq") =>
continue,
_ => {
response = result;
break
},
}
}

drop(wallet);

response
}

/// request, pay and execute an issue
pub async fn assert_issue(
parachain_rpc: &SpacewalkParachain,
Expand All @@ -127,20 +167,19 @@ pub async fn assert_issue(
let asset = primitives::AssetConversion::lookup(issue.asset).expect("Invalid asset");
let stroop_amount = primitives::BalanceConversion::lookup(amount).expect("Invalid amount");

let mut wallet_write = wallet.write().await;
let destination_public_key = PublicKey::from_binary(issue.vault_stellar_public_key);

let response = wallet_write
.send_payment_to_address(
destination_public_key,
asset,
stroop_amount,
issue.issue_id.0,
300,
false,
)
.await
.expect("Failed to send payment");
let response = send_payment_to_address(
wallet,
destination_public_key,
asset,
stroop_amount,
issue.issue_id.0,
300,
false,
)
.await
.expect("should return ok");

let slot = response.ledger as u64;

Expand Down
100 changes: 45 additions & 55 deletions clients/vault/tests/vault_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,19 +591,17 @@ async fn test_issue_overpayment_succeeds() {
let stellar_asset =
primitives::AssetConversion::lookup(issue.asset).expect("Asset not found");

let transaction_response = user_wallet
.write()
.await
.send_payment_to_address(
destination_public_key,
stellar_asset,
stroop_amount.try_into().unwrap(),
issue.issue_id.0,
300,
false,
)
.await
.expect("Sending payment failed");
let transaction_response = send_payment_to_address(
user_wallet,
destination_public_key,
stellar_asset,
stroop_amount.try_into().unwrap(),
issue.issue_id.0,
300,
false,
)
.await
.expect("Sending payment failed");

assert!(transaction_response.successful);

Expand Down Expand Up @@ -681,20 +679,17 @@ async fn test_automatic_issue_execution_succeeds() {
let stellar_asset =
primitives::AssetConversion::lookup(issue.asset).expect("Asset not found");

let mut wallet_write = user_wallet.write().await;
let result = wallet_write
.send_payment_to_address(
destination_public_key,
stellar_asset,
stroop_amount,
issue.issue_id.0,
300,
false,
)
.await
.expect("should return a result");

drop(wallet_write);
let result = send_payment_to_address(
user_wallet,
destination_public_key,
stellar_asset,
stroop_amount,
issue.issue_id.0,
300,
false,
)
.await
.expect("should return a result");

tracing::warn!("Sent payment successfully: {:?}", result);

Expand Down Expand Up @@ -821,19 +816,17 @@ async fn test_automatic_issue_execution_succeeds_for_other_vault() {
drop(issue_set);
assert!(!memos_to_issue_ids.read().await.is_empty());

let mut wallet_write = user_wallet.write().await;
let result = wallet_write
.send_payment_to_address(
destination_public_key,
stellar_asset,
stroop_amount,
issue.issue_id.0,
300,
false,
)
.await;
let result = send_payment_to_address(
user_wallet,
destination_public_key,
stellar_asset,
stroop_amount,
issue.issue_id.0,
300,
false,
)
.await;
assert!(result.is_ok());
drop(wallet_write);

tracing::info!("Sent payment to address. Ledger is {:?}", result.unwrap().ledger);

Expand Down Expand Up @@ -974,21 +967,18 @@ async fn test_execute_open_requests_succeeds() {
primitives::AssetConversion::lookup(redeems[0].asset).expect("Invalid asset");

// do stellar transfer for redeem 0
let mut wallet_write = vault_wallet.write().await;
assert_ok!(
wallet_write
.send_payment_to_address(
address,
asset,
stroop_amount,
redeem_ids[0].0,
300,
false
)
.await
send_payment_to_address(
vault_wallet.clone(),
address,
asset,
stroop_amount,
redeem_ids[0].0,
300,
false
)
.await
);
drop(wallet_write);

// Sleep 3 seconds to give other thread some time to receive the RequestIssue event and
// add it to the set
sleep(Duration::from_secs(5)).await;
Expand All @@ -1006,10 +996,10 @@ async fn test_execute_open_requests_succeeds() {
.map(Result::unwrap),
// Redeem 0 should be executed without creating an extra payment since we already
// sent one just before
assert_execute_redeem_event(TIMEOUT, user_provider.clone(), redeem_ids[0]),
assert_execute_redeem_event(TIMEOUT * 3, user_provider.clone(), redeem_ids[0]),
// Redeem 1 and 2 should be executed after creating an extra payment
assert_execute_redeem_event(TIMEOUT, user_provider.clone(), redeem_ids[1]),
assert_execute_redeem_event(TIMEOUT, user_provider.clone(), redeem_ids[2]),
assert_execute_redeem_event(TIMEOUT * 3, user_provider.clone(), redeem_ids[1]),
assert_execute_redeem_event(TIMEOUT * 3, user_provider.clone(), redeem_ids[2]),
)
.await;
},
Expand Down
Loading