Never delete spent utxos from the database

A `is_spent` field is added to LocalUtxo; when a txo is spent we set
this field to true instead of deleting the entire utxo from the
database.
This allows us to create txs double-spending txs already in blockchain.
Listunspent won't return spent utxos, effectively excluding them from the
coin selection and balance calculation
This commit is contained in:
Daniela Brozzoni committed 2022-03-10 11:58:23 +01:00
1 parent 3e4678d8e3
commit f2f0efc0b3
13 files changed
+155 -66

No files matched your search

+3
View File
@@ -569,6 +569,7 @@ mod test {
script_pubkey: Script::new(),
},
keychain: KeychainKind::External,
is_spent: false,
}),
}
}
@@ -596,6 +597,7 @@ mod test {
script_pubkey: Script::new(),
},
keychain: KeychainKind::External,
is_spent: false,
}),
});
}
@@ -615,6 +617,7 @@ mod test {
script_pubkey: Script::new(),
},
keychain: KeychainKind::External,
is_spent: false,
}),
};
vec![utxo; utxos_number]
+8 -1
View File
@@ -387,7 +387,13 @@ where
/// Note that this method only operates on the internal database, which first needs to be
/// [`Wallet::sync`] manually.
pub fn list_unspent(&self) -> Result<Vec<LocalUtxo>, Error> {
self.database.borrow().iter_utxos()
Ok(self
.database
.borrow()
.iter_utxos()?
.into_iter()
.filter(|l| !l.is_spent)
.collect())
}
/// Returns the `UTXO` owned by this wallet corresponding to `outpoint` if it exists in the
@@ -879,6 +885,7 @@ where
outpoint: txin.previous_output,
txout,
keychain,
is_spent: true,
};
Ok(WeightedUtxo {
+2
View File
@@ -838,6 +838,7 @@ mod test {
},
txout: Default::default(),
keychain: KeychainKind::External,
is_spent: false,
},
LocalUtxo {
outpoint: OutPoint {
@@ -846,6 +847,7 @@ mod test {
},
txout: Default::default(),
keychain: KeychainKind::Internal,
is_spent: false,
},
]
}