drk: mark spent coins on block scanning

This commit is contained in:
aggstam
2022-12-10 18:50:54 +02:00
parent fcf66b78c4
commit b1ca9721c8
2 changed files with 27 additions and 0 deletions

View File

@@ -132,6 +132,7 @@ impl Drk {
async fn scan_block(&self, block: &BlockInfo) -> Result<()> {
eprintln!("Iterating over {} transactions", block.txs.len());
let mut nullifiers: Vec<Nullifier> = vec![];
let mut outputs: Vec<Output> = vec![];
// TODO: FIXME: This shouldn't be hardcoded here obviously.
@@ -143,6 +144,9 @@ impl Drk {
{
eprintln!("Found Money::Transfer in call {} in tx {}", j, i);
let params: MoneyTransferParams = deserialize(&call.data[1..])?;
for input in params.inputs {
nullifiers.push(input.nullifier);
}
for output in params.outputs {
outputs.push(output);
}
@@ -152,6 +156,9 @@ impl Drk {
if call.contract_id == contract_id && call.data[0] == MoneyFunction::OtcSwap as u8 {
eprintln!("Found Money::OtcSwap in call {} in tx {}", j, i);
let params: MoneyTransferParams = deserialize(&call.data[1..])?;
for input in params.inputs {
nullifiers.push(input.nullifier);
}
for output in params.outputs {
outputs.push(output);
}
@@ -205,6 +212,10 @@ impl Drk {
self.put_tree(&tree).await?;
eprintln!("Merkle tree written successfully");
eprintln!("Marking spent coins");
self.mark_spent_coins(nullifiers).await?;
eprintln!("Spent coins marked successfully");
// This is the SQL query we'll be executing to insert coins into the wallet
let query = format!(
"INSERT INTO {} ({}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12);",

View File

@@ -446,6 +446,22 @@ impl Drk {
Ok(())
}
/// Marks all coins in the wallet as spent, if their nullifier is
/// in the provided set
pub async fn mark_spent_coins(&self, nullifiers: Vec<Nullifier>) -> Result<()> {
if nullifiers.is_empty() {
return Ok(())
}
for (coin, _) in self.wallet_coins(false).await? {
if nullifiers.contains(&coin.nullifier) {
self.mark_spent_coin(&coin.coin).await?;
}
}
Ok(())
}
/// Mark a given coin in the wallet as unspent
pub async fn unspend_coin(&self, coin: &Coin) -> Result<()> {
let query = format!(