Skip to content

Commit

Permalink
Merge pull request #6 from sander2/fix/rescan-btc-chain-on-restart
Browse files Browse the repository at this point in the history
fix: rescan bitcoin chain on restart to add any missed wallet addresses
  • Loading branch information
sander2 authored Jun 8, 2021
2 parents 6f6154b + 6ba4f78 commit 5b1c584
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions bitcoin/src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ mod tests {
where
P: Into<[u8; PUBLIC_KEY_SIZE]> + From<[u8; PUBLIC_KEY_SIZE]> + Clone + PartialEq + Send + Sync + 'static;
async fn import_private_key(&self, privkey: PrivateKey) -> Result<(), Error>;
async fn rescan_blockchain(&self, start_height: usize) -> Result<(), Error>;
}
}

Expand Down
7 changes: 7 additions & 0 deletions bitcoin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ pub trait BitcoinCoreApi {
P: Into<[u8; PUBLIC_KEY_SIZE]> + From<[u8; PUBLIC_KEY_SIZE]> + Clone + PartialEq + Send + Sync + 'static;

async fn import_private_key(&self, privkey: PrivateKey) -> Result<(), Error>;

async fn rescan_blockchain(&self, start_height: usize) -> Result<(), Error>;
}

pub struct LockedTransaction {
Expand Down Expand Up @@ -661,6 +663,11 @@ impl BitcoinCoreApi for BitcoinCore {
self.with_wallet(|| async { Ok(self.rpc.import_private_key(&privkey, None, None)?) })
.await
}

async fn rescan_blockchain(&self, start_height: usize) -> Result<(), Error> {
self.rpc.rescan_blockchain(Some(start_height), None)?;
Ok(())
}
}

/// Extension trait for transaction, adding methods to help to match the Transaction to Replace/Redeem requests
Expand Down
3 changes: 3 additions & 0 deletions runtime/src/integration/bitcoin_simulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,4 +428,7 @@ impl BitcoinCoreApi for MockBitcoinCore {
async fn import_private_key(&self, _privkey: PrivateKey) -> Result<(), BitcoinError> {
Ok(())
}
async fn rescan_blockchain(&self, start_height: usize) -> Result<(), BitcoinError> {
Ok(())
}
}
1 change: 1 addition & 0 deletions staked-relayer/src/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ mod tests {
where
P: Into<[u8; PUBLIC_KEY_SIZE]> + From<[u8; PUBLIC_KEY_SIZE]> + Clone + PartialEq + Send + Sync + 'static;
async fn import_private_key(&self, privkey: PrivateKey) -> Result<(), BitcoinError>;
async fn rescan_blockchain(&self, start_height: usize) -> Result<(), BitcoinError>;
}
}

Expand Down
1 change: 1 addition & 0 deletions vault/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ mod tests {
where
P: Into<[u8; PUBLIC_KEY_SIZE]> + From<[u8; PUBLIC_KEY_SIZE]> + Clone + PartialEq + Send + Sync + 'static;
async fn import_private_key(&self, privkey: PrivateKey) -> Result<(), BitcoinError>;
async fn rescan_blockchain(&self, start_height: usize) -> Result<(), BitcoinError>;
}
}

Expand Down
17 changes: 13 additions & 4 deletions vault/src/issue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,24 @@ pub async fn add_keys_from_past_issue_request<B: BitcoinCoreApi + Clone + Send +
bitcoin_core: &B,
btc_parachain: &InterBtcParachain,
) -> Result<(), Error> {
for (issue_id, request) in btc_parachain
let issue_requests = btc_parachain
.get_vault_issue_requests(btc_parachain.get_account_id().clone())
.await?
.into_iter()
{
.await?;

let btc_start_height = match issue_requests.iter().map(|(_, request)| request.btc_height).min() {
Some(x) => x as usize,
None => return Ok(()), // the iterator is empty so we have nothing to do
};

for (issue_id, request) in issue_requests.into_iter() {
if let Err(e) = add_new_deposit_key(bitcoin_core, issue_id, request.btc_public_key).await {
tracing::error!("Failed to add deposit key #{}: {}", issue_id, e.to_string());
}
}

tracing::info!("Rescanning bitcoin chain from height {}...", btc_start_height);
bitcoin_core.rescan_blockchain(btc_start_height).await?;

Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions vault/src/replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ mod tests {
where
P: Into<[u8; PUBLIC_KEY_SIZE]> + From<[u8; PUBLIC_KEY_SIZE]> + Clone + PartialEq + Send + Sync + 'static;
async fn import_private_key(&self, privkey: PrivateKey) -> Result<(), BitcoinError>;
async fn rescan_blockchain(&self, start_height: usize) -> Result<(), BitcoinError>;
}
}

Expand Down

0 comments on commit 5b1c584

Please sign in to comment.