mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-04-28 03:00:18 -04:00
drk: mark spent coins on block scanning
This commit is contained in:
@@ -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);",
|
||||
|
||||
@@ -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!(
|
||||
|
||||
Reference in New Issue
Block a user