Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add opt-in config to execute all redeem and replace requests #449

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion runtime/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,8 @@ pub trait ReplacePallet {
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 @@ -1053,6 +1055,20 @@ impl ReplacePallet for InterBtcParachain {
.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> {
self.query_finalized_or_error(metadata::storage().replace().replace_period())
.await
Expand Down Expand Up @@ -1340,12 +1356,15 @@ pub trait RedeemPallet {

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

/// 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<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 @@ -1406,6 +1425,20 @@ impl RedeemPallet for InterBtcParachain {
.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> {
self.query_finalized_or_error(metadata::storage().redeem().redeem_period())
.await
Expand Down
1 change: 1 addition & 0 deletions vault/src/cancellation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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
18 changes: 14 additions & 4 deletions vault/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,15 +471,23 @@ pub async fn execute_open_requests(
num_confirmations: u32,
payment_margin: Duration,
auto_rbf: 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_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()
Expand Down Expand Up @@ -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<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,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<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
2 changes: 2 additions & 0 deletions vault/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 @@ -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<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
1 change: 1 addition & 0 deletions vault/src/replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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: 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,

/// 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,
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions vault/tests/vault_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
Expand Down