From 14b2455d844d0561c6d8ab9b0804943753dd8845 Mon Sep 17 00:00:00 2001 From: b-yap <2826165+b-yap@users.noreply.github.com> Date: Tue, 7 Nov 2023 21:37:42 +0800 Subject: [PATCH 1/3] first iteration --- clients/vault/tests/helper/helper.rs | 67 +++++++++++++++---- .../vault/tests/vault_integration_tests.rs | 53 ++++++--------- 2 files changed, 74 insertions(+), 46 deletions(-) diff --git a/clients/vault/tests/helper/helper.rs b/clients/vault/tests/helper/helper.rs index d8264127b..3e31398a5 100644 --- a/clients/vault/tests/helper/helper.rs +++ b/clients/vault/tests/helper/helper.rs @@ -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::{CurrencyId, StellarStroops, H256, stellar::Asset as StellarAsset }; use runtime::{ integration::{ assert_event, get_required_vault_collateral_for_issue, setup_provider, SubxtClient, @@ -12,9 +12,11 @@ use runtime::{ use sp_keyring::AccountKeyring; use sp_runtime::traits::StaticLookup; use std::{sync::Arc, time::Duration}; +use tokio::sync::RwLockWriteGuard; use stellar_relay_lib::sdk::PublicKey; use vault::{oracle::OracleAgent, ArcRwLock}; -use wallet::StellarWallet; +use wallet::error::Error; +use wallet::{StellarWallet, TransactionResponse}; pub fn default_destination() -> SecretKey { SecretKey::from_encoding(crate::helper::DEFAULT_TESTNET_DEST_SECRET_KEY).expect("Should work") @@ -111,6 +113,46 @@ pub async fn assert_execute_redeem_event( .await } + +pub async fn send_payment_to_address( + wallet:ArcRwLock, + destination_address: PublicKey, + asset: StellarAsset, + stroop_amount: StellarStroops, + request_id: [u8; 32], + stroop_fee_per_operation: u32, + is_payment_for_redeem_request: bool, +) -> Result { + 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, @@ -127,20 +169,17 @@ 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; diff --git a/clients/vault/tests/vault_integration_tests.rs b/clients/vault/tests/vault_integration_tests.rs index 355ff7cac..ff3982f39 100644 --- a/clients/vault/tests/vault_integration_tests.rs +++ b/clients/vault/tests/vault_integration_tests.rs @@ -591,10 +591,8 @@ 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( + let transaction_response = send_payment_to_address( + user_wallet, destination_public_key, stellar_asset, stroop_amount.try_into().unwrap(), @@ -681,21 +679,18 @@ 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, + 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"); - drop(wallet_write); - tracing::warn!("Sent payment successfully: {:?}", result); // Sleep 5 seconds to give other thread some time to receive the RequestIssue event @@ -821,9 +816,8 @@ 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( + let result = send_payment_to_address( + user_wallet, destination_public_key, stellar_asset, stroop_amount, @@ -833,7 +827,6 @@ async fn test_automatic_issue_execution_succeeds_for_other_vault() { ) .await; assert!(result.is_ok()); - drop(wallet_write); tracing::info!("Sent payment to address. Ledger is {:?}", result.unwrap().ledger); @@ -974,21 +967,17 @@ 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; From dd209cc322791a2593f4e19b61b5401bccb10ec3 Mon Sep 17 00:00:00 2001 From: b-yap <2826165+b-yap@users.noreply.github.com> Date: Tue, 7 Nov 2023 23:18:35 +0800 Subject: [PATCH 2/3] extend timeout, similar to `test_automatic_issue_execution_succeeds_for_other_vault` --- clients/vault/tests/helper/helper.rs | 1 - clients/vault/tests/vault_integration_tests.rs | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/clients/vault/tests/helper/helper.rs b/clients/vault/tests/helper/helper.rs index 3e31398a5..32d779518 100644 --- a/clients/vault/tests/helper/helper.rs +++ b/clients/vault/tests/helper/helper.rs @@ -12,7 +12,6 @@ use runtime::{ use sp_keyring::AccountKeyring; use sp_runtime::traits::StaticLookup; use std::{sync::Arc, time::Duration}; -use tokio::sync::RwLockWriteGuard; use stellar_relay_lib::sdk::PublicKey; use vault::{oracle::OracleAgent, ArcRwLock}; use wallet::error::Error; diff --git a/clients/vault/tests/vault_integration_tests.rs b/clients/vault/tests/vault_integration_tests.rs index ff3982f39..7920cebbc 100644 --- a/clients/vault/tests/vault_integration_tests.rs +++ b/clients/vault/tests/vault_integration_tests.rs @@ -995,10 +995,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; }, From ca3229a482f2ea77035c1ccaf4301ef463f225f9 Mon Sep 17 00:00:00 2001 From: b-yap <2826165+b-yap@users.noreply.github.com> Date: Wed, 8 Nov 2023 13:03:47 +0800 Subject: [PATCH 3/3] https://github.com/pendulum-chain/spacewalk/actions/runs/6786558158/job/18447348918#step:11:12 --- clients/vault/tests/helper/helper.rs | 29 +++++++------ .../vault/tests/vault_integration_tests.rs | 43 ++++++++++--------- 2 files changed, 37 insertions(+), 35 deletions(-) diff --git a/clients/vault/tests/helper/helper.rs b/clients/vault/tests/helper/helper.rs index 32d779518..ca9f2bece 100644 --- a/clients/vault/tests/helper/helper.rs +++ b/clients/vault/tests/helper/helper.rs @@ -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, stellar::Asset as StellarAsset }; +use primitives::{stellar::Asset as StellarAsset, CurrencyId, StellarStroops, H256}; use runtime::{ integration::{ assert_event, get_required_vault_collateral_for_issue, setup_provider, SubxtClient, @@ -14,8 +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::error::Error; -use wallet::{StellarWallet, TransactionResponse}; +use wallet::{error::Error, StellarWallet, TransactionResponse}; pub fn default_destination() -> SecretKey { SecretKey::from_encoding(crate::helper::DEFAULT_TESTNET_DEST_SECRET_KEY).expect("Should work") @@ -112,19 +111,20 @@ pub async fn assert_execute_redeem_event( .await } - pub async fn send_payment_to_address( - wallet:ArcRwLock, + wallet: ArcRwLock, destination_address: PublicKey, asset: StellarAsset, stroop_amount: StellarStroops, request_id: [u8; 32], stroop_fee_per_operation: u32, is_payment_for_redeem_request: bool, -) -> Result { +) -> Result { let response; loop { - let result = wallet.write().await + let result = wallet + .write() + .await .send_payment_to_address( destination_address.clone(), asset.clone(), @@ -137,13 +137,12 @@ pub async fn send_payment_to_address( match &result { // if the error is `tx_bad_seq` perform the process again - Err(Error::HorizonSubmissionError { reason, .. }) if reason.contains("tx_bad_seq") => { - continue - }, + Err(Error::HorizonSubmissionError { reason, .. }) if reason.contains("tx_bad_seq") => + continue, _ => { response = result; - break; - } + break + }, } } @@ -177,8 +176,10 @@ pub async fn assert_issue( stroop_amount, issue.issue_id.0, 300, - false - ).await.expect("should return ok"); + false, + ) + .await + .expect("should return ok"); let slot = response.ledger as u64; diff --git a/clients/vault/tests/vault_integration_tests.rs b/clients/vault/tests/vault_integration_tests.rs index 7920cebbc..5aea98813 100644 --- a/clients/vault/tests/vault_integration_tests.rs +++ b/clients/vault/tests/vault_integration_tests.rs @@ -593,15 +593,15 @@ async fn test_issue_overpayment_succeeds() { 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"); + 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); @@ -687,9 +687,9 @@ async fn test_automatic_issue_execution_succeeds() { issue.issue_id.0, 300, false, - ) - .await - .expect("should return a result"); + ) + .await + .expect("should return a result"); tracing::warn!("Sent payment successfully: {:?}", result); @@ -818,14 +818,14 @@ async fn test_automatic_issue_execution_succeeds_for_other_vault() { let result = send_payment_to_address( user_wallet, - destination_public_key, - stellar_asset, - stroop_amount, - issue.issue_id.0, - 300, - false, - ) - .await; + destination_public_key, + stellar_asset, + stroop_amount, + issue.issue_id.0, + 300, + false, + ) + .await; assert!(result.is_ok()); tracing::info!("Sent payment to address. Ledger is {:?}", result.unwrap().ledger); @@ -976,7 +976,8 @@ async fn test_execute_open_requests_succeeds() { redeem_ids[0].0, 300, false - ).await + ) + .await ); // Sleep 3 seconds to give other thread some time to receive the RequestIssue event and // add it to the set