Skip to content

Commit

Permalink
feat: add opt-out config to execute all redeem and replace requests
Browse files Browse the repository at this point in the history
Signed-off-by: Gregory Hill <[email protected]>
  • Loading branch information
gregdhill committed Jun 28, 2023
1 parent e0ce3fe commit 51c28e8
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 44 deletions.
93 changes: 60 additions & 33 deletions runtime/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,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<AccountId>,
) -> Result<Vec<(H256, InterBtcReplaceRequest)>, Error>;

/// Get the time difference in number of blocks between when a replace
Expand Down Expand Up @@ -1035,22 +1035,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<AccountId>,
) -> Result<Vec<(H256, InterBtcReplaceRequest)>, Error> {
let head = self.get_finalized_block_hash().await?;
let result: Vec<H256> = 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<H256> = 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<u32, Error> {
Expand Down Expand Up @@ -1340,10 +1353,11 @@ pub trait RedeemPallet {

async fn get_redeem_request(&self, redeem_id: H256) -> Result<InterBtcRedeemRequest, Error>;

/// 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<AccountId>,
) -> Result<Vec<(H256, InterBtcRedeemRequest)>, Error>;

async fn get_redeem_period(&self) -> Result<BlockNumber, Error>;
Expand Down Expand Up @@ -1386,24 +1400,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<AccountId>,
) -> Result<Vec<(H256, InterBtcRedeemRequest)>, Error> {
let head = self.get_finalized_block_hash().await?;
let result: Vec<H256> = 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<H256> = 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<BlockNumber, Error> {
Expand Down
2 changes: 1 addition & 1 deletion vault/src/cancellation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<(H256, InterBtcReplaceRequest)>, RuntimeError>;
async fn get_old_vault_replace_requests(&self, account_id: AccountId) -> Result<Vec<(H256, InterBtcReplaceRequest)>, RuntimeError>;
async fn get_old_vault_replace_requests(&self, maybe_account_id: Option<AccountId>) -> Result<Vec<(H256, InterBtcReplaceRequest)>, RuntimeError>;
async fn get_replace_period(&self) -> Result<u32, RuntimeError>;
async fn get_replace_request(&self, replace_id: H256) -> Result<InterBtcReplaceRequest, RuntimeError>;
async fn get_replace_dust_amount(&self) -> Result<u128, RuntimeError>;
Expand Down
9 changes: 5 additions & 4 deletions vault/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,14 +471,15 @@ pub async fn execute_open_requests(
num_confirmations: u32,
payment_margin: Duration,
auto_rbf: bool,
no_execute_all: bool,
) -> Result<(), ServiceError<Error>> {
let parachain_rpc = &parachain_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
Expand Down Expand Up @@ -722,7 +723,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<InterBtcRedeemRequest, RuntimeError>;
async fn get_vault_redeem_requests(&self, account_id: AccountId) -> Result<Vec<(H256, InterBtcRedeemRequest)>, RuntimeError>;
async fn get_redeem_requests(&self, maybe_account_id: Option<AccountId>) -> Result<Vec<(H256, InterBtcRedeemRequest)>, RuntimeError>;
async fn get_redeem_period(&self) -> Result<BlockNumber, RuntimeError>;
}

Expand All @@ -734,7 +735,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<Vec<(H256, InterBtcReplaceRequest)>, RuntimeError>;
async fn get_old_vault_replace_requests(&self, account_id: AccountId) -> Result<Vec<(H256, InterBtcReplaceRequest)>, RuntimeError>;
async fn get_old_vault_replace_requests(&self, maybe_account_id: Option<AccountId>) -> Result<Vec<(H256, InterBtcReplaceRequest)>, RuntimeError>;
async fn get_replace_period(&self) -> Result<u32, RuntimeError>;
async fn get_replace_request(&self, replace_id: H256) -> Result<InterBtcReplaceRequest, RuntimeError>;
async fn get_replace_dust_amount(&self) -> Result<u128, RuntimeError>;
Expand Down
10 changes: 5 additions & 5 deletions vault/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,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()
Expand Down Expand Up @@ -608,7 +608,7 @@ pub async fn poll_metrics<P: CollateralBalancesPallet + RedeemPallet + IssuePall
publish_native_currency_balance(parachain_rpc).await?;
publish_issue_count(parachain_rpc, vault_id_manager).await;
if let Ok(redeems) = parachain_rpc
.get_vault_redeem_requests(parachain_rpc.get_account_id().clone())
.get_redeem_requests(Some(parachain_rpc.get_account_id().clone()))
.await
{
publish_redeem_count(vault_id_manager, &redeems).await;
Expand Down Expand Up @@ -720,7 +720,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<InterBtcRedeemRequest, RuntimeError>;
async fn get_vault_redeem_requests(&self, account_id: AccountId) -> Result<Vec<(H256, InterBtcRedeemRequest)>, RuntimeError>;
async fn get_redeem_requests(&self, maybe_account_id: Option<AccountId>) -> Result<Vec<(H256, InterBtcRedeemRequest)>, RuntimeError>;
async fn get_redeem_period(&self) -> Result<BlockNumber, RuntimeError>;
}

Expand Down Expand Up @@ -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<Vec<(H256, InterBtcReplaceRequest)>, RuntimeError>;
async fn get_old_vault_replace_requests(&self, account_id: AccountId) -> Result<Vec<(H256, InterBtcReplaceRequest)>, RuntimeError>;
async fn get_old_vault_replace_requests(&self, maybe_account_id: Option<AccountId>) -> Result<Vec<(H256, InterBtcReplaceRequest)>, RuntimeError>;
async fn get_replace_period(&self) -> Result<u32, RuntimeError>;
async fn get_replace_request(&self, replace_id: H256) -> Result<InterBtcReplaceRequest, RuntimeError>;
async fn get_replace_dust_amount(&self) -> Result<u128, RuntimeError>;
Expand Down
2 changes: 1 addition & 1 deletion vault/src/replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,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<Vec<(H256, InterBtcReplaceRequest)>, RuntimeError>;
async fn get_old_vault_replace_requests(&self, account_id: AccountId) -> Result<Vec<(H256, InterBtcReplaceRequest)>, RuntimeError>;
async fn get_old_vault_replace_requests(&self, maybe_account_id: Option<AccountId>) -> Result<Vec<(H256, InterBtcReplaceRequest)>, RuntimeError>;
async fn get_replace_period(&self) -> Result<u32, RuntimeError>;
async fn get_replace_request(&self, replace_id: H256) -> Result<InterBtcReplaceRequest, RuntimeError>;
async fn get_replace_dust_amount(&self) -> Result<u128, RuntimeError>;
Expand Down
6 changes: 6 additions & 0 deletions vault/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -632,6 +637,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();
Expand Down

0 comments on commit 51c28e8

Please sign in to comment.