diff --git a/runtime/src/rpc.rs b/runtime/src/rpc.rs index bb08f2fd0..a2218bd94 100644 --- a/runtime/src/rpc.rs +++ b/runtime/src/rpc.rs @@ -942,6 +942,8 @@ pub trait ReplacePallet { account_id: AccountId, ) -> Result, Error>; + async fn get_all_replace_requests(&self) -> Result, Error>; + /// Get the time difference in number of blocks between when a replace /// request is created and required completion time by a vault async fn get_replace_period(&self) -> Result; @@ -1053,6 +1055,20 @@ impl ReplacePallet for InterBtcParachain { .collect() } + async fn get_all_replace_requests(&self) -> Result, Error> { + let head = self.get_finalized_block_hash().await?; + let key_addr = metadata::storage().replace().replace_requests_root(); + let mut iter = self.api.storage().iter(key_addr, DEFAULT_PAGE_SIZE, head).await?; + let mut replace_requests = Vec::new(); + while let Some((replace_id, request)) = iter.next().await? { + let key_hash = replace_id.0.as_slice(); + // last bytes are the raw key + let key = &key_hash[key_hash.len() - 32..]; + replace_requests.push((H256::from_slice(key), request)); + } + Ok(replace_requests) + } + async fn get_replace_period(&self) -> Result { self.query_finalized_or_error(metadata::storage().replace().replace_period()) .await @@ -1340,12 +1356,15 @@ pub trait RedeemPallet { async fn get_redeem_request(&self, redeem_id: H256) -> Result; - /// Get all redeem requests requested of the given vault + /// Get all redeem requests for a vault async fn get_vault_redeem_requests( &self, account_id: AccountId, ) -> Result, Error>; + /// Get all redeem requests + async fn get_all_redeem_requests(&self) -> Result, Error>; + async fn get_redeem_period(&self) -> Result; } @@ -1406,6 +1425,20 @@ impl RedeemPallet for InterBtcParachain { .collect() } + async fn get_all_redeem_requests(&self) -> Result, Error> { + let head = self.get_finalized_block_hash().await?; + let key_addr = metadata::storage().redeem().redeem_requests_root(); + let mut iter = self.api.storage().iter(key_addr, DEFAULT_PAGE_SIZE, head).await?; + let mut redeem_requests = Vec::new(); + while let Some((redeem_id, request)) = iter.next().await? { + let key_hash = redeem_id.0.as_slice(); + // last bytes are the raw key + let key = &key_hash[key_hash.len() - 32..]; + redeem_requests.push((H256::from_slice(key), request)); + } + Ok(redeem_requests) + } + async fn get_redeem_period(&self) -> Result { self.query_finalized_or_error(metadata::storage().redeem().redeem_period()) .await diff --git a/vault/src/cancellation.rs b/vault/src/cancellation.rs index 81f2f4ba7..36449d094 100644 --- a/vault/src/cancellation.rs +++ b/vault/src/cancellation.rs @@ -359,6 +359,7 @@ mod tests { async fn cancel_replace(&self, replace_id: H256) -> Result<(), RuntimeError>; async fn get_new_vault_replace_requests(&self, account_id: AccountId) -> Result, RuntimeError>; async fn get_old_vault_replace_requests(&self, account_id: AccountId) -> Result, RuntimeError>; + async fn get_all_replace_requests(&self) -> Result, RuntimeError>; async fn get_replace_period(&self) -> Result; async fn get_replace_request(&self, replace_id: H256) -> Result; async fn get_replace_dust_amount(&self) -> Result; diff --git a/vault/src/execution.rs b/vault/src/execution.rs index 07763f22d..82aa4149b 100644 --- a/vault/src/execution.rs +++ b/vault/src/execution.rs @@ -471,15 +471,23 @@ pub async fn execute_open_requests( num_confirmations: u32, payment_margin: Duration, auto_rbf: bool, + execute_all: bool, ) -> Result<(), ServiceError> { let parachain_rpc = ¶chain_rpc; let vault_id = parachain_rpc.get_account_id().clone(); // get all redeem and replace requests - let (redeem_requests, replace_requests) = try_join!( - parachain_rpc.get_vault_redeem_requests(vault_id.clone()), - parachain_rpc.get_old_vault_replace_requests(vault_id.clone()), - )?; + let (redeem_requests, replace_requests) = if execute_all { + try_join!( + parachain_rpc.get_all_redeem_requests(), + parachain_rpc.get_all_replace_requests(), + )? + } else { + try_join!( + parachain_rpc.get_vault_redeem_requests(vault_id.clone()), + parachain_rpc.get_old_vault_replace_requests(vault_id.clone()), + )? + }; let open_redeems = redeem_requests .into_iter() @@ -723,6 +731,7 @@ mod tests { async fn cancel_redeem(&self, redeem_id: H256, reimburse: bool) -> Result<(), RuntimeError>; async fn get_redeem_request(&self, redeem_id: H256) -> Result; async fn get_vault_redeem_requests(&self, account_id: AccountId) -> Result, RuntimeError>; + async fn get_all_redeem_requests(&self) -> Result, RuntimeError>; async fn get_redeem_period(&self) -> Result; } @@ -735,6 +744,7 @@ mod tests { async fn cancel_replace(&self, replace_id: H256) -> Result<(), RuntimeError>; async fn get_new_vault_replace_requests(&self, account_id: AccountId) -> Result, RuntimeError>; async fn get_old_vault_replace_requests(&self, account_id: AccountId) -> Result, RuntimeError>; + async fn get_all_replace_requests(&self) -> Result, RuntimeError>; async fn get_replace_period(&self) -> Result; async fn get_replace_request(&self, replace_id: H256) -> Result; async fn get_replace_dust_amount(&self) -> Result; diff --git a/vault/src/metrics.rs b/vault/src/metrics.rs index ca407687c..d10a22043 100644 --- a/vault/src/metrics.rs +++ b/vault/src/metrics.rs @@ -721,6 +721,7 @@ mod tests { async fn cancel_redeem(&self, redeem_id: H256, reimburse: bool) -> Result<(), RuntimeError>; async fn get_redeem_request(&self, redeem_id: H256) -> Result; async fn get_vault_redeem_requests(&self, account_id: AccountId) -> Result, RuntimeError>; + async fn get_all_redeem_requests(&self) -> Result, RuntimeError>; async fn get_redeem_period(&self) -> Result; } @@ -760,6 +761,7 @@ mod tests { async fn cancel_replace(&self, replace_id: H256) -> Result<(), RuntimeError>; async fn get_new_vault_replace_requests(&self, account_id: AccountId) -> Result, RuntimeError>; async fn get_old_vault_replace_requests(&self, account_id: AccountId) -> Result, RuntimeError>; + async fn get_all_replace_requests(&self) -> Result, RuntimeError>; async fn get_replace_period(&self) -> Result; async fn get_replace_request(&self, replace_id: H256) -> Result; async fn get_replace_dust_amount(&self) -> Result; diff --git a/vault/src/replace.rs b/vault/src/replace.rs index d36527f11..73ba8b59d 100644 --- a/vault/src/replace.rs +++ b/vault/src/replace.rs @@ -332,6 +332,7 @@ mod tests { async fn cancel_replace(&self, replace_id: H256) -> Result<(), RuntimeError>; async fn get_new_vault_replace_requests(&self, account_id: AccountId) -> Result, RuntimeError>; async fn get_old_vault_replace_requests(&self, account_id: AccountId) -> Result, RuntimeError>; + async fn get_all_replace_requests(&self) -> Result, RuntimeError>; async fn get_replace_period(&self) -> Result; async fn get_replace_request(&self, replace_id: H256) -> Result; async fn get_replace_dust_amount(&self) -> Result; diff --git a/vault/src/system.rs b/vault/src/system.rs index 8941117d0..a6580399a 100644 --- a/vault/src/system.rs +++ b/vault/src/system.rs @@ -69,6 +69,11 @@ pub struct VaultServiceConfig { #[clap(long)] pub no_issue_execution: bool, + /// Try to execute **all** redeem and replace requests, + /// the Vault will still submit proofs for their own + #[clap(long)] + pub execute_all: bool, + /// Don't run the RPC API. #[clap(long)] pub no_api: bool, @@ -632,6 +637,7 @@ impl VaultService { num_confirmations, self.config.payment_margin_minutes, self.config.auto_rbf, + self.config.execute_all, ); let shutdown_clone = self.shutdown.clone(); diff --git a/vault/tests/vault_integration_tests.rs b/vault/tests/vault_integration_tests.rs index 20e50388f..f08b24ddb 100644 --- a/vault/tests/vault_integration_tests.rs +++ b/vault/tests/vault_integration_tests.rs @@ -840,6 +840,7 @@ async fn test_execute_open_requests_succeeds() { 0, Duration::from_secs(0), true, + false, ) .map(Result::unwrap), assert_redeem_event(TIMEOUT, user_provider.clone(), redeem_ids[0]),