use std::{
collections::{BTreeMap, BTreeSet, HashMap},
ops::RangeInclusive,
};
use mset::MultiSet;
use zebra_chain::{
amount::{Amount, NegativeAllowed},
block::Height,
transaction, transparent,
};
use crate::{OutputLocation, TransactionLocation, ValidateContextError};
use super::{RevertPosition, UpdateWith};
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TransparentTransfers {
balance: Amount<NegativeAllowed>,
tx_ids: MultiSet<transaction::Hash>,
created_utxos: BTreeMap<OutputLocation, transparent::Output>,
spent_utxos: BTreeSet<OutputLocation>,
}
impl
UpdateWith<(
// The location of the UTXO
&transparent::OutPoint,
// The UTXO data
// Includes the location of the transaction that created the output
&transparent::OrderedUtxo,
)> for TransparentTransfers
{
#[allow(clippy::unwrap_in_result)]
fn update_chain_tip_with(
&mut self,
&(outpoint, created_utxo): &(&transparent::OutPoint, &transparent::OrderedUtxo),
) -> Result<(), ValidateContextError> {
self.balance = (self.balance
+ created_utxo
.utxo
.output
.value()
.constrain()
.expect("NonNegative values are always valid NegativeAllowed values"))
.expect("total UTXO value has already been checked");
let transaction_location = transaction_location(created_utxo);
let output_location = OutputLocation::from_outpoint(transaction_location, outpoint);
let previous_entry = self
.created_utxos
.insert(output_location, created_utxo.utxo.output.clone());
assert_eq!(
previous_entry, None,
"unexpected created output: duplicate update or duplicate UTXO",
);
self.tx_ids.insert(outpoint.hash);
Ok(())
}
fn revert_chain_with(
&mut self,
&(outpoint, created_utxo): &(&transparent::OutPoint, &transparent::OrderedUtxo),
_position: RevertPosition,
) {
self.balance = (self.balance
- created_utxo
.utxo
.output
.value()
.constrain()
.expect("NonNegative values are always valid NegativeAllowed values"))
.expect("reversing previous balance changes is always valid");
let transaction_location = transaction_location(created_utxo);
let output_location = OutputLocation::from_outpoint(transaction_location, outpoint);
let removed_entry = self.created_utxos.remove(&output_location);
assert!(
removed_entry.is_some(),
"unexpected revert of created output: duplicate update or duplicate UTXO",
);
let tx_id_was_removed = self.tx_ids.remove(&outpoint.hash);
assert!(
tx_id_was_removed,
"unexpected revert of created output transaction: \
duplicate revert, or revert of an output that was never updated",
);
}
}
impl
UpdateWith<(
// The transparent input data
&transparent::Input,
// The hash of the transaction the input is from
// (not the transaction the spent output was created by)
&transaction::Hash,
// The output spent by the input
// Includes the location of the transaction that created the output
&transparent::OrderedUtxo,
)> for TransparentTransfers
{
#[allow(clippy::unwrap_in_result)]
fn update_chain_tip_with(
&mut self,
&(spending_input, spending_tx_hash, spent_output): &(
&transparent::Input,
&transaction::Hash,
&transparent::OrderedUtxo,
),
) -> Result<(), ValidateContextError> {
self.balance = (self.balance
- spent_output
.utxo
.output
.value()
.constrain()
.expect("NonNegative values are always valid NegativeAllowed values"))
.expect("total UTXO value has already been checked");
let spent_outpoint = spending_input.outpoint().expect("checked by caller");
let spent_output_tx_loc = transaction_location(spent_output);
let output_location = OutputLocation::from_outpoint(spent_output_tx_loc, &spent_outpoint);
let spend_was_inserted = self.spent_utxos.insert(output_location);
assert!(
spend_was_inserted,
"unexpected spent output: duplicate update or duplicate spend",
);
self.tx_ids.insert(*spending_tx_hash);
Ok(())
}
fn revert_chain_with(
&mut self,
&(spending_input, spending_tx_hash, spent_output): &(
&transparent::Input,
&transaction::Hash,
&transparent::OrderedUtxo,
),
_position: RevertPosition,
) {
self.balance = (self.balance
+ spent_output
.utxo
.output
.value()
.constrain()
.expect("NonNegative values are always valid NegativeAllowed values"))
.expect("reversing previous balance changes is always valid");
let spent_outpoint = spending_input.outpoint().expect("checked by caller");
let spent_output_tx_loc = transaction_location(spent_output);
let output_location = OutputLocation::from_outpoint(spent_output_tx_loc, &spent_outpoint);
let spend_was_removed = self.spent_utxos.remove(&output_location);
assert!(
spend_was_removed,
"unexpected revert of spent output: \
duplicate revert, or revert of a spent output that was never updated",
);
let tx_id_was_removed = self.tx_ids.remove(spending_tx_hash);
assert!(
tx_id_was_removed,
"unexpected revert of spending input transaction: \
duplicate revert, or revert of an input that was never updated",
);
}
}
impl TransparentTransfers {
pub fn is_empty(&self) -> bool {
self.balance == Amount::<NegativeAllowed>::zero()
&& self.tx_ids.is_empty()
&& self.created_utxos.is_empty()
&& self.spent_utxos.is_empty()
}
#[allow(dead_code)]
pub fn balance(&self) -> Amount<NegativeAllowed> {
self.balance
}
pub fn received(&self) -> u64 {
let received_utxos = self.created_utxos.values();
received_utxos.map(|out| out.value()).map(u64::from).sum()
}
pub fn tx_ids(
&self,
chain_tx_loc_by_hash: &HashMap<transaction::Hash, TransactionLocation>,
query_height_range: RangeInclusive<Height>,
) -> BTreeMap<TransactionLocation, transaction::Hash> {
self.tx_ids
.distinct_elements()
.filter_map(|tx_hash| {
let tx_loc = *chain_tx_loc_by_hash
.get(tx_hash)
.expect("all hashes are indexed");
if query_height_range.contains(&tx_loc.height) {
Some((tx_loc, *tx_hash))
} else {
None
}
})
.collect()
}
#[allow(dead_code)]
pub fn created_utxos(&self) -> &BTreeMap<OutputLocation, transparent::Output> {
&self.created_utxos
}
#[allow(dead_code)]
pub fn spent_utxos(&self) -> &BTreeSet<OutputLocation> {
&self.spent_utxos
}
}
impl Default for TransparentTransfers {
fn default() -> Self {
Self {
balance: Amount::zero(),
tx_ids: Default::default(),
created_utxos: Default::default(),
spent_utxos: Default::default(),
}
}
}
pub fn transaction_location(ordered_utxo: &transparent::OrderedUtxo) -> TransactionLocation {
TransactionLocation::from_usize(ordered_utxo.utxo.height, ordered_utxo.tx_index_in_block)
}