From 7222e27f0f34eefe90f80dd700de964d5acae476 Mon Sep 17 00:00:00 2001 From: Gregory Hill Date: Mon, 13 Feb 2023 08:21:40 +0000 Subject: [PATCH] feat: add opt-out config to execute all redeem and replace requests Signed-off-by: Gregory Hill --- Cargo.lock | 10 ++--- runtime/src/rpc.rs | 93 +++++++++++++++++++++++++-------------- vault/src/cancellation.rs | 2 +- vault/src/execution.rs | 9 ++-- vault/src/metrics.rs | 10 ++--- vault/src/replace.rs | 2 +- vault/src/system.rs | 6 +++ 7 files changed, 83 insertions(+), 49 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5db0d51fa..65b172e10 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15238,12 +15238,12 @@ dependencies = [ "pkg-config", ] -[[patch.unused]] -name = "orml-xcm" -version = "0.4.1-dev" -source = "git+https://github.com/open-web3-stack//open-runtime-module-library?rev=24f0a8b6e04e1078f70d0437fb816337cdf4f64c#24f0a8b6e04e1078f70d0437fb816337cdf4f64c" - [[patch.unused]] name = "sp-serializer" version = "4.0.0-dev" source = "git+https://github.com/paritytech//substrate?branch=polkadot-v0.9.36#cb4f2491b00af7d7817f3a54209c26b20faa1f51" + +[[patch.unused]] +name = "orml-xcm" +version = "0.4.1-dev" +source = "git+https://github.com/open-web3-stack//open-runtime-module-library?rev=24f0a8b6e04e1078f70d0437fb816337cdf4f64c#24f0a8b6e04e1078f70d0437fb816337cdf4f64c" diff --git a/runtime/src/rpc.rs b/runtime/src/rpc.rs index 636459537..721f9b3dc 100644 --- a/runtime/src/rpc.rs +++ b/runtime/src/rpc.rs @@ -791,7 +791,7 @@ pub trait ReplacePallet { /// Get all replace requests made by the given vault async fn get_old_vault_replace_requests( &self, - account_id: AccountId, + maybe_account_id: Option, ) -> Result, Error>; /// Get the time difference in number of blocks between when a replace @@ -886,22 +886,35 @@ impl ReplacePallet for InterBtcParachain { /// Get all replace requests made by the given vault async fn get_old_vault_replace_requests( &self, - account_id: AccountId, + maybe_account_id: Option, ) -> Result, Error> { let head = self.get_finalized_block_hash().await?; - let result: Vec = self - .api - .rpc() - .request("replace_getOldVaultReplaceRequests", rpc_params![account_id, head]) - .await?; - join_all( - result - .into_iter() - .map(|key| async move { self.get_replace_request(key).await.map(|value| (key, value)) }), - ) - .await - .into_iter() - .collect() + if let Some(account_id) = maybe_account_id { + let result: Vec = self + .api + .rpc() + .request("replace_getOldVaultReplaceRequests", rpc_params![account_id, head]) + .await?; + join_all( + result + .into_iter() + .map(|key| async move { self.get_replace_request(key).await.map(|value| (key, value)) }), + ) + .await + .into_iter() + .collect() + } else { + 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 { @@ -1184,10 +1197,11 @@ pub trait RedeemPallet { async fn get_redeem_request(&self, redeem_id: H256) -> Result; - /// Get all redeem requests requested of the given vault - async fn get_vault_redeem_requests( + /// Get all redeem requests, if the account is specified + /// only return those for that Vault + async fn get_redeem_requests( &self, - account_id: AccountId, + maybe_account_id: Option, ) -> Result, Error>; async fn get_redeem_period(&self) -> Result; @@ -1229,24 +1243,37 @@ impl RedeemPallet for InterBtcParachain { .await } - async fn get_vault_redeem_requests( + async fn get_redeem_requests( &self, - account_id: AccountId, + maybe_account_id: Option, ) -> Result, Error> { let head = self.get_finalized_block_hash().await?; - let result: Vec = self - .api - .rpc() - .request("redeem_getVaultRedeemRequests", rpc_params![account_id, head]) - .await?; - join_all( - result - .into_iter() - .map(|key| async move { self.get_redeem_request(key).await.map(|value| (key, value)) }), - ) - .await - .into_iter() - .collect() + if let Some(account_id) = maybe_account_id { + let result: Vec = self + .api + .rpc() + .request("redeem_getVaultRedeemRequests", rpc_params![account_id, head]) + .await?; + join_all( + result + .into_iter() + .map(|key| async move { self.get_redeem_request(key).await.map(|value| (key, value)) }), + ) + .await + .into_iter() + .collect() + } else { + 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 { diff --git a/vault/src/cancellation.rs b/vault/src/cancellation.rs index 18c800e1d..39bbb8d5b 100644 --- a/vault/src/cancellation.rs +++ b/vault/src/cancellation.rs @@ -358,7 +358,7 @@ mod tests { async fn execute_replace(&self, replace_id: H256, merkle_proof: &[u8], raw_tx: &[u8]) -> Result<(), RuntimeError>; 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_old_vault_replace_requests(&self, maybe_account_id: Option) -> 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 70bdb791f..bdd5f5fb3 100644 --- a/vault/src/execution.rs +++ b/vault/src/execution.rs @@ -429,14 +429,15 @@ pub async fn execute_open_requests( num_confirmations: u32, payment_margin: Duration, auto_rbf: bool, + no_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()), + parachain_rpc.get_redeem_requests(no_execute_all.then_some(vault_id.clone())), + parachain_rpc.get_old_vault_replace_requests(no_execute_all.then_some(vault_id.clone())), )?; let open_redeems = redeem_requests @@ -667,7 +668,7 @@ mod tests { async fn execute_redeem(&self, redeem_id: H256, merkle_proof: &[u8], raw_tx: &[u8]) -> Result<(), RuntimeError>; 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_redeem_requests(&self, maybe_account_id: Option) -> Result, RuntimeError>; async fn get_redeem_period(&self) -> Result; } @@ -679,7 +680,7 @@ mod tests { async fn execute_replace(&self, replace_id: H256, merkle_proof: &[u8], raw_tx: &[u8]) -> Result<(), RuntimeError>; 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_old_vault_replace_requests(&self, maybe_account_id: Option) -> 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 c5844aedb..bc1c8f9e9 100644 --- a/vault/src/metrics.rs +++ b/vault/src/metrics.rs @@ -224,8 +224,8 @@ impl PerCurrencyMetrics { let vault_id = &vault.vault_id; // update fee surplus if let Ok((redeem_requests, replace_requests)) = try_join!( - parachain_rpc.get_vault_redeem_requests(vault_id.account_id.clone()), - parachain_rpc.get_old_vault_replace_requests(vault_id.account_id.clone()) + parachain_rpc.get_redeem_requests(Some(vault_id.account_id.clone())), + parachain_rpc.get_old_vault_replace_requests(Some(vault_id.account_id.clone())) ) { let redeems = redeem_requests .iter() @@ -612,7 +612,7 @@ pub async fn poll_metrics Result<(), RuntimeError>; 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_redeem_requests(&self, maybe_account_id: Option) -> Result, RuntimeError>; async fn get_redeem_period(&self) -> Result; } @@ -759,7 +759,7 @@ mod tests { async fn execute_replace(&self, replace_id: H256, merkle_proof: &[u8], raw_tx: &[u8]) -> Result<(), RuntimeError>; 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_old_vault_replace_requests(&self, maybe_account_id: Option) -> 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 a3f1b7c9d..6289cc0c0 100644 --- a/vault/src/replace.rs +++ b/vault/src/replace.rs @@ -329,7 +329,7 @@ mod tests { async fn execute_replace(&self, replace_id: H256, merkle_proof: &[u8], raw_tx: &[u8]) -> Result<(), RuntimeError>; 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_old_vault_replace_requests(&self, maybe_account_id: Option) -> 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 090078ea1..eebfbe406 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, + /// Don't try to execute **all** redeem and replace requests, + /// the Vault will still submit proofs for their own + #[clap(long)] + pub no_execute_all: bool, + /// Don't run the RPC API. #[clap(long)] pub no_api: bool, @@ -568,6 +573,7 @@ impl VaultService { num_confirmations, self.config.payment_margin_minutes, self.config.auto_rbf, + self.config.no_execute_all, ); let shutdown_clone = self.shutdown.clone();