Skip to content

Commit

Permalink
refactor: split redeem / replace methods
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 51c28e8 commit 684b207
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 75 deletions.
122 changes: 64 additions & 58 deletions runtime/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -939,9 +939,11 @@ pub trait ReplacePallet {
/// Get all replace requests made by the given vault
async fn get_old_vault_replace_requests(
&self,
maybe_account_id: Option<AccountId>,
account_id: AccountId,
) -> Result<Vec<(H256, InterBtcReplaceRequest)>, Error>;

async fn get_all_replace_requests(&self) -> Result<Vec<(H256, InterBtcReplaceRequest)>, 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<u32, Error>;
Expand Down Expand Up @@ -1035,35 +1037,36 @@ impl ReplacePallet for InterBtcParachain {
/// Get all replace requests made by the given vault
async fn get_old_vault_replace_requests(
&self,
maybe_account_id: Option<AccountId>,
account_id: AccountId,
) -> Result<Vec<(H256, InterBtcReplaceRequest)>, Error> {
let head = self.get_finalized_block_hash().await?;
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)
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()
}

async fn get_all_replace_requests(&self) -> Result<Vec<(H256, InterBtcReplaceRequest)>, 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<u32, Error> {
Expand Down Expand Up @@ -1353,13 +1356,15 @@ pub trait RedeemPallet {

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

/// Get all redeem requests, if the account is specified
/// only return those for that Vault
async fn get_redeem_requests(
/// Get all redeem requests for a vault
async fn get_vault_redeem_requests(
&self,
maybe_account_id: Option<AccountId>,
account_id: AccountId,
) -> Result<Vec<(H256, InterBtcRedeemRequest)>, Error>;

/// Get all redeem requests
async fn get_all_redeem_requests(&self) -> Result<Vec<(H256, InterBtcRedeemRequest)>, Error>;

async fn get_redeem_period(&self) -> Result<BlockNumber, Error>;
}

Expand Down Expand Up @@ -1400,37 +1405,38 @@ impl RedeemPallet for InterBtcParachain {
.await
}

async fn get_redeem_requests(
async fn get_vault_redeem_requests(
&self,
maybe_account_id: Option<AccountId>,
account_id: AccountId,
) -> Result<Vec<(H256, InterBtcRedeemRequest)>, Error> {
let head = self.get_finalized_block_hash().await?;
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)
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()
}

async fn get_all_redeem_requests(&self) -> Result<Vec<(H256, InterBtcRedeemRequest)>, 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<BlockNumber, Error> {
Expand Down
3 changes: 2 additions & 1 deletion vault/src/cancellation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,8 @@ 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, maybe_account_id: Option<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_all_replace_requests(&self) -> 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
23 changes: 16 additions & 7 deletions vault/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,16 +471,23 @@ pub async fn execute_open_requests(
num_confirmations: u32,
payment_margin: Duration,
auto_rbf: bool,
no_execute_all: bool,
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_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 (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()
Expand Down Expand Up @@ -723,7 +730,8 @@ 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_redeem_requests(&self, maybe_account_id: Option<AccountId>) -> Result<Vec<(H256, InterBtcRedeemRequest)>, RuntimeError>;
async fn get_vault_redeem_requests(&self, account_id: AccountId) -> Result<Vec<(H256, InterBtcRedeemRequest)>, RuntimeError>;
async fn get_all_redeem_requests(&self) -> Result<Vec<(H256, InterBtcRedeemRequest)>, RuntimeError>;
async fn get_redeem_period(&self) -> Result<BlockNumber, RuntimeError>;
}

Expand All @@ -735,7 +743,8 @@ 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, maybe_account_id: Option<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_all_replace_requests(&self) -> 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
12 changes: 7 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_redeem_requests(Some(vault_id.account_id.clone())),
parachain_rpc.get_old_vault_replace_requests(Some(vault_id.account_id.clone()))
parachain_rpc.get_vault_redeem_requests(vault_id.account_id.clone()),
parachain_rpc.get_old_vault_replace_requests(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_redeem_requests(Some(parachain_rpc.get_account_id().clone()))
.get_vault_redeem_requests(parachain_rpc.get_account_id().clone())
.await
{
publish_redeem_count(vault_id_manager, &redeems).await;
Expand Down Expand Up @@ -720,7 +720,8 @@ 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_redeem_requests(&self, maybe_account_id: Option<AccountId>) -> Result<Vec<(H256, InterBtcRedeemRequest)>, RuntimeError>;
async fn get_vault_redeem_requests(&self, account_id: AccountId) -> Result<Vec<(H256, InterBtcRedeemRequest)>, RuntimeError>;
async fn get_all_redeem_requests(&self) -> Result<Vec<(H256, InterBtcRedeemRequest)>, RuntimeError>;
async fn get_redeem_period(&self) -> Result<BlockNumber, RuntimeError>;
}

Expand Down Expand Up @@ -759,7 +760,8 @@ 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, maybe_account_id: Option<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_all_replace_requests(&self) -> 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
3 changes: 2 additions & 1 deletion vault/src/replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,8 @@ 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, maybe_account_id: Option<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_all_replace_requests(&self) -> 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: 3 additions & 3 deletions vault/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ pub struct VaultServiceConfig {
#[clap(long)]
pub no_issue_execution: bool,

/// Don't try to execute **all** redeem and replace requests,
/// Try to execute **all** redeem and replace requests,
/// the Vault will still submit proofs for their own
#[clap(long)]
pub no_execute_all: bool,
pub execute_all: bool,

/// Don't run the RPC API.
#[clap(long)]
Expand Down Expand Up @@ -637,7 +637,7 @@ impl VaultService {
num_confirmations,
self.config.payment_margin_minutes,
self.config.auto_rbf,
self.config.no_execute_all,
self.config.execute_all,
);

let shutdown_clone = self.shutdown.clone();
Expand Down

0 comments on commit 684b207

Please sign in to comment.