state: remove spent UTXOs in commit_finalized_direct

This commit is contained in:
Henry de Valence 2020-10-27 14:55:04 -07:00
parent c8a97f94b2
commit 1573e0e4ae
1 changed files with 19 additions and 2 deletions

View File

@ -13,7 +13,7 @@ use crate::{BoxError, Config, HashOrHeight, QueuedBlock};
mod sled_format; mod sled_format;
use sled_format::{FromSled, SledDeserialize, SledSerialize}; use sled_format::{FromSled, IntoSled, SledDeserialize, SledSerialize};
/// The finalized part of the chain state, stored in sled. /// The finalized part of the chain state, stored in sled.
/// ///
@ -236,23 +236,40 @@ impl FinalizedState {
)| { )| {
// TODO: check highest entry of hash_by_height as in RFC // TODO: check highest entry of hash_by_height as in RFC
// Index the block
hash_by_height.zs_insert(height, hash)?; hash_by_height.zs_insert(height, hash)?;
height_by_hash.zs_insert(hash, height)?; height_by_hash.zs_insert(hash, height)?;
block_by_height.zs_insert(height, &*block)?; block_by_height.zs_insert(height, &*block)?;
// TODO: sprout and sapling anchors (per block)
// Index each transaction
for transaction in block.transactions.iter() { for transaction in block.transactions.iter() {
let transaction_hash = transaction.hash(); let transaction_hash = transaction.hash();
tx_by_hash.zs_insert(transaction_hash, transaction)?; tx_by_hash.zs_insert(transaction_hash, transaction)?;
// Mark all transparent inputs as spent
for input in transaction.inputs() {
match input {
transparent::Input::PrevOut { outpoint, .. } => {
utxo_by_outpoint.remove(outpoint.as_bytes())?;
}
// Coinbase inputs represent new coins,
// so there are no UTXOs to mark as spent.
transparent::Input::Coinbase { .. } => {}
}
}
// Index all new transparent outputs
for (index, output) in transaction.outputs().iter().enumerate() { for (index, output) in transaction.outputs().iter().enumerate() {
let outpoint = transparent::OutPoint { let outpoint = transparent::OutPoint {
hash: transaction_hash, hash: transaction_hash,
index: index as _, index: index as _,
}; };
utxo_by_outpoint.zs_insert(outpoint, output)?; utxo_by_outpoint.zs_insert(outpoint, output)?;
} }
// Mark sprout and sapling nullifiers as spent
for sprout_nullifier in transaction.sprout_nullifiers() { for sprout_nullifier in transaction.sprout_nullifiers() {
sprout_nullifiers.zs_insert(sprout_nullifier, ())?; sprout_nullifiers.zs_insert(sprout_nullifier, ())?;
} }