2023-03-31 12:39:00 +08:00
|
|
|
use core::{convert::Infallible, ops::AddAssign};
|
2023-03-27 14:21:10 +08:00
|
|
|
|
2023-03-31 12:39:00 +08:00
|
|
|
use bitcoin::{OutPoint, Script, Transaction, TxOut};
|
2023-03-27 14:21:10 +08:00
|
|
|
|
|
|
|
use crate::{
|
2023-03-27 19:55:57 +08:00
|
|
|
keychain::Balance,
|
2023-03-27 14:21:10 +08:00
|
|
|
sparse_chain::ChainPosition,
|
2023-03-30 18:33:53 +08:00
|
|
|
tx_graph::{Additions, TxGraph, TxNode},
|
2023-03-31 12:39:00 +08:00
|
|
|
BlockAnchor, ChainOracle, FullTxOut, ObservedIn, TxIndex,
|
2023-03-27 14:21:10 +08:00
|
|
|
};
|
|
|
|
|
2023-03-28 10:58:23 +08:00
|
|
|
/// An outwards-facing view of a transaction that is part of the *best chain*'s history.
|
2023-03-27 14:21:10 +08:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
pub struct TxInChain<'a, T, A> {
|
2023-03-28 10:58:23 +08:00
|
|
|
/// Where the transaction is observed (in a block or in mempool).
|
2023-03-27 14:21:10 +08:00
|
|
|
pub observed_in: ObservedIn<&'a A>,
|
2023-03-28 10:58:23 +08:00
|
|
|
/// The transaction with anchors and last seen timestamp.
|
2023-03-30 18:33:53 +08:00
|
|
|
pub tx: TxNode<'a, T, A>,
|
2023-03-27 14:21:10 +08:00
|
|
|
}
|
|
|
|
|
2023-03-28 10:58:23 +08:00
|
|
|
/// A structure that represents changes to an [`IndexedTxGraph`].
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2023-03-27 21:51:11 +08:00
|
|
|
#[must_use]
|
2023-03-31 12:39:00 +08:00
|
|
|
pub struct IndexedAdditions<A, IA> {
|
2023-03-28 10:58:23 +08:00
|
|
|
/// [`TxGraph`] additions.
|
2023-03-27 14:21:10 +08:00
|
|
|
pub graph_additions: Additions<A>,
|
2023-03-28 10:58:23 +08:00
|
|
|
/// [`TxIndex`] additions.
|
2023-03-31 12:39:00 +08:00
|
|
|
pub index_additions: IA,
|
2023-03-28 10:58:23 +08:00
|
|
|
/// Last block height witnessed (if any).
|
2023-03-27 22:42:39 +08:00
|
|
|
pub last_height: Option<u32>,
|
2023-03-27 14:21:10 +08:00
|
|
|
}
|
|
|
|
|
2023-03-31 12:39:00 +08:00
|
|
|
impl<A, IA: Default> Default for IndexedAdditions<A, IA> {
|
2023-03-27 14:21:10 +08:00
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
graph_additions: Default::default(),
|
2023-03-31 12:39:00 +08:00
|
|
|
index_additions: Default::default(),
|
2023-03-27 22:42:39 +08:00
|
|
|
last_height: None,
|
2023-03-27 14:21:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 12:39:00 +08:00
|
|
|
impl<A: BlockAnchor, IA: AddAssign> AddAssign for IndexedAdditions<A, IA> {
|
|
|
|
fn add_assign(&mut self, rhs: Self) {
|
2023-03-27 14:21:10 +08:00
|
|
|
let Self {
|
|
|
|
graph_additions,
|
2023-03-31 12:39:00 +08:00
|
|
|
index_additions: index_delta,
|
2023-03-27 22:42:39 +08:00
|
|
|
last_height,
|
2023-03-31 12:39:00 +08:00
|
|
|
} = rhs;
|
2023-03-27 14:21:10 +08:00
|
|
|
self.graph_additions.append(graph_additions);
|
2023-03-31 12:39:00 +08:00
|
|
|
self.index_additions += index_delta;
|
2023-03-27 22:42:39 +08:00
|
|
|
if self.last_height < last_height {
|
|
|
|
let last_height =
|
|
|
|
last_height.expect("must exist as it is larger than self.last_height");
|
|
|
|
self.last_height.replace(last_height);
|
|
|
|
}
|
2023-03-27 14:21:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct IndexedTxGraph<A, I> {
|
2023-03-31 22:42:47 +08:00
|
|
|
/// Transaction index.
|
|
|
|
pub index: I,
|
2023-03-27 14:21:10 +08:00
|
|
|
graph: TxGraph<A>,
|
2023-03-27 22:42:39 +08:00
|
|
|
last_height: u32,
|
2023-03-27 14:21:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, I: Default> Default for IndexedTxGraph<A, I> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
graph: Default::default(),
|
|
|
|
index: Default::default(),
|
2023-03-27 22:42:39 +08:00
|
|
|
last_height: u32::MIN,
|
2023-03-27 14:21:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A: BlockAnchor, I: TxIndex> IndexedTxGraph<A, I> {
|
|
|
|
/// Get a reference of the internal transaction graph.
|
|
|
|
pub fn graph(&self) -> &TxGraph<A> {
|
|
|
|
&self.graph
|
|
|
|
}
|
|
|
|
|
2023-03-27 15:36:37 +08:00
|
|
|
/// Applies the [`IndexedAdditions`] to the [`IndexedTxGraph`].
|
|
|
|
pub fn apply_additions(&mut self, additions: IndexedAdditions<A, I::Additions>) {
|
|
|
|
let IndexedAdditions {
|
|
|
|
graph_additions,
|
2023-03-31 12:39:00 +08:00
|
|
|
index_additions,
|
2023-03-27 22:42:39 +08:00
|
|
|
last_height,
|
2023-03-27 15:36:37 +08:00
|
|
|
} = additions;
|
2023-03-27 21:51:11 +08:00
|
|
|
|
2023-03-31 12:39:00 +08:00
|
|
|
self.index.apply_additions(index_additions);
|
2023-03-27 21:51:11 +08:00
|
|
|
|
|
|
|
for tx in &graph_additions.tx {
|
|
|
|
self.index.index_tx(tx);
|
|
|
|
}
|
|
|
|
for (&outpoint, txout) in &graph_additions.txout {
|
|
|
|
self.index.index_txout(outpoint, txout);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.graph.apply_additions(graph_additions);
|
2023-03-27 22:42:39 +08:00
|
|
|
|
|
|
|
if let Some(height) = last_height {
|
|
|
|
self.last_height = height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 12:39:00 +08:00
|
|
|
fn insert_height_internal(&mut self, tip: u32) -> Option<u32> {
|
2023-03-27 22:42:39 +08:00
|
|
|
if self.last_height < tip {
|
|
|
|
self.last_height = tip;
|
2023-03-31 12:39:00 +08:00
|
|
|
Some(tip)
|
2023-03-27 22:42:39 +08:00
|
|
|
} else {
|
2023-03-31 12:39:00 +08:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Insert a block height that the chain source has scanned up to.
|
|
|
|
pub fn insert_height(&mut self, tip: u32) -> IndexedAdditions<A, I::Additions>
|
|
|
|
where
|
|
|
|
I::Additions: Default,
|
|
|
|
{
|
|
|
|
IndexedAdditions {
|
|
|
|
last_height: self.insert_height_internal(tip),
|
|
|
|
..Default::default()
|
2023-03-27 22:42:39 +08:00
|
|
|
}
|
2023-03-27 15:36:37 +08:00
|
|
|
}
|
|
|
|
|
2023-03-27 14:21:10 +08:00
|
|
|
/// Insert a `txout` that exists in `outpoint` with the given `observation`.
|
|
|
|
pub fn insert_txout(
|
|
|
|
&mut self,
|
|
|
|
outpoint: OutPoint,
|
|
|
|
txout: &TxOut,
|
|
|
|
observation: ObservedIn<A>,
|
|
|
|
) -> IndexedAdditions<A, I::Additions> {
|
2023-03-31 12:39:00 +08:00
|
|
|
let last_height = match &observation {
|
|
|
|
ObservedIn::Block(anchor) => self.insert_height_internal(anchor.anchor_block().height),
|
|
|
|
ObservedIn::Mempool(_) => None,
|
2023-03-27 22:42:39 +08:00
|
|
|
};
|
|
|
|
|
2023-03-31 12:39:00 +08:00
|
|
|
IndexedAdditions {
|
2023-03-27 14:21:10 +08:00
|
|
|
graph_additions: {
|
|
|
|
let mut graph_additions = self.graph.insert_txout(outpoint, txout.clone());
|
|
|
|
graph_additions.append(match observation {
|
|
|
|
ObservedIn::Block(anchor) => self.graph.insert_anchor(outpoint.txid, anchor),
|
|
|
|
ObservedIn::Mempool(seen_at) => {
|
|
|
|
self.graph.insert_seen_at(outpoint.txid, seen_at)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
graph_additions
|
|
|
|
},
|
2023-03-31 12:39:00 +08:00
|
|
|
index_additions: <I as TxIndex>::index_txout(&mut self.index, outpoint, txout),
|
|
|
|
last_height,
|
|
|
|
}
|
2023-03-27 14:21:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn insert_tx(
|
|
|
|
&mut self,
|
|
|
|
tx: &Transaction,
|
|
|
|
observation: ObservedIn<A>,
|
|
|
|
) -> IndexedAdditions<A, I::Additions> {
|
|
|
|
let txid = tx.txid();
|
2023-03-27 22:42:39 +08:00
|
|
|
|
2023-03-31 12:39:00 +08:00
|
|
|
let last_height = match &observation {
|
|
|
|
ObservedIn::Block(anchor) => self.insert_height_internal(anchor.anchor_block().height),
|
|
|
|
ObservedIn::Mempool(_) => None,
|
2023-03-27 22:42:39 +08:00
|
|
|
};
|
|
|
|
|
2023-03-31 12:39:00 +08:00
|
|
|
IndexedAdditions {
|
2023-03-27 14:21:10 +08:00
|
|
|
graph_additions: {
|
|
|
|
let mut graph_additions = self.graph.insert_tx(tx.clone());
|
|
|
|
graph_additions.append(match observation {
|
|
|
|
ObservedIn::Block(anchor) => self.graph.insert_anchor(txid, anchor),
|
|
|
|
ObservedIn::Mempool(seen_at) => self.graph.insert_seen_at(txid, seen_at),
|
|
|
|
});
|
|
|
|
graph_additions
|
|
|
|
},
|
2023-03-31 12:39:00 +08:00
|
|
|
index_additions: <I as TxIndex>::index_tx(&mut self.index, tx),
|
|
|
|
last_height,
|
|
|
|
}
|
2023-03-27 14:21:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn filter_and_insert_txs<'t, T>(
|
|
|
|
&mut self,
|
|
|
|
txs: T,
|
|
|
|
observation: ObservedIn<A>,
|
|
|
|
) -> IndexedAdditions<A, I::Additions>
|
|
|
|
where
|
|
|
|
T: Iterator<Item = &'t Transaction>,
|
2023-03-31 12:39:00 +08:00
|
|
|
I::Additions: Default + AddAssign,
|
2023-03-27 14:21:10 +08:00
|
|
|
{
|
|
|
|
txs.filter_map(|tx| {
|
|
|
|
if self.index.is_tx_relevant(tx) {
|
|
|
|
Some(self.insert_tx(tx, observation.clone()))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.fold(IndexedAdditions::default(), |mut acc, other| {
|
2023-03-31 12:39:00 +08:00
|
|
|
acc += other;
|
2023-03-27 14:21:10 +08:00
|
|
|
acc
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-27 22:42:39 +08:00
|
|
|
/// Get the last block height that we are synced up to.
|
|
|
|
pub fn last_height(&self) -> u32 {
|
|
|
|
self.last_height
|
2023-03-27 14:21:10 +08:00
|
|
|
}
|
|
|
|
|
2023-03-29 22:45:01 +08:00
|
|
|
// [TODO] Have to methods, one for relevant-only, and one for any. Have one in `TxGraph`.
|
2023-03-27 14:21:10 +08:00
|
|
|
pub fn try_list_chain_txs<'a, C>(
|
|
|
|
&'a self,
|
|
|
|
chain: C,
|
|
|
|
) -> impl Iterator<Item = Result<TxInChain<'a, Transaction, A>, C::Error>>
|
|
|
|
where
|
|
|
|
C: ChainOracle + 'a,
|
|
|
|
{
|
|
|
|
self.graph
|
|
|
|
.full_transactions()
|
|
|
|
.filter(|tx| self.index.is_tx_relevant(tx))
|
|
|
|
.filter_map(move |tx| {
|
|
|
|
self.graph
|
|
|
|
.try_get_chain_position(&chain, tx.txid)
|
|
|
|
.map(|v| v.map(|observed_in| TxInChain { observed_in, tx }))
|
|
|
|
.transpose()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn list_chain_txs<'a, C>(
|
|
|
|
&'a self,
|
|
|
|
chain: C,
|
|
|
|
) -> impl Iterator<Item = TxInChain<'a, Transaction, A>>
|
|
|
|
where
|
|
|
|
C: ChainOracle<Error = Infallible> + 'a,
|
|
|
|
{
|
|
|
|
self.try_list_chain_txs(chain)
|
|
|
|
.map(|r| r.expect("error is infallible"))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn try_list_chain_txouts<'a, C>(
|
|
|
|
&'a self,
|
|
|
|
chain: C,
|
2023-03-31 12:39:00 +08:00
|
|
|
) -> impl Iterator<Item = Result<FullTxOut<ObservedIn<A>>, C::Error>> + 'a
|
2023-03-27 14:21:10 +08:00
|
|
|
where
|
|
|
|
C: ChainOracle + 'a,
|
|
|
|
ObservedIn<A>: ChainPosition,
|
|
|
|
{
|
2023-03-31 12:39:00 +08:00
|
|
|
self.graph
|
|
|
|
.all_txouts()
|
|
|
|
.filter(|(_, txo)| self.index.is_spk_owned(&txo.script_pubkey))
|
|
|
|
.filter_map(move |(op, txout)| -> Option<Result<_, C::Error>> {
|
2023-03-27 14:21:10 +08:00
|
|
|
let graph_tx = self.graph.get_tx(op.txid)?;
|
|
|
|
|
|
|
|
let is_on_coinbase = graph_tx.is_coin_base();
|
|
|
|
|
|
|
|
let chain_position = match self.graph.try_get_chain_position(&chain, op.txid) {
|
2023-03-31 12:39:00 +08:00
|
|
|
Ok(Some(observed_at)) => observed_at.into_owned(),
|
2023-03-27 14:21:10 +08:00
|
|
|
Ok(None) => return None,
|
|
|
|
Err(err) => return Some(Err(err)),
|
|
|
|
};
|
|
|
|
|
2023-03-31 12:39:00 +08:00
|
|
|
let spent_by = match self.graph.try_get_spend_in_chain(&chain, op) {
|
|
|
|
Ok(Some((obs, txid))) => Some((obs.into_owned(), txid)),
|
|
|
|
Ok(None) => None,
|
2023-03-27 14:21:10 +08:00
|
|
|
Err(err) => return Some(Err(err)),
|
|
|
|
};
|
|
|
|
|
|
|
|
let full_txout = FullTxOut {
|
2023-03-31 12:39:00 +08:00
|
|
|
outpoint: op,
|
2023-03-27 14:21:10 +08:00
|
|
|
txout: txout.clone(),
|
|
|
|
chain_position,
|
|
|
|
spent_by,
|
|
|
|
is_on_coinbase,
|
|
|
|
};
|
|
|
|
|
2023-03-31 12:39:00 +08:00
|
|
|
Some(Ok(full_txout))
|
|
|
|
})
|
2023-03-27 14:21:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn list_chain_txouts<'a, C>(
|
|
|
|
&'a self,
|
|
|
|
chain: C,
|
2023-03-31 12:39:00 +08:00
|
|
|
) -> impl Iterator<Item = FullTxOut<ObservedIn<A>>> + 'a
|
2023-03-27 14:21:10 +08:00
|
|
|
where
|
|
|
|
C: ChainOracle<Error = Infallible> + 'a,
|
|
|
|
ObservedIn<A>: ChainPosition,
|
|
|
|
{
|
|
|
|
self.try_list_chain_txouts(chain)
|
|
|
|
.map(|r| r.expect("error in infallible"))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return relevant unspents.
|
|
|
|
pub fn try_list_chain_utxos<'a, C>(
|
|
|
|
&'a self,
|
|
|
|
chain: C,
|
2023-03-31 12:39:00 +08:00
|
|
|
) -> impl Iterator<Item = Result<FullTxOut<ObservedIn<A>>, C::Error>> + 'a
|
2023-03-27 14:21:10 +08:00
|
|
|
where
|
|
|
|
C: ChainOracle + 'a,
|
|
|
|
ObservedIn<A>: ChainPosition,
|
|
|
|
{
|
|
|
|
self.try_list_chain_txouts(chain)
|
2023-03-31 12:39:00 +08:00
|
|
|
.filter(|r| !matches!(r, Ok(txo) if txo.spent_by.is_none()))
|
2023-03-27 14:21:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn list_chain_utxos<'a, C>(
|
|
|
|
&'a self,
|
|
|
|
chain: C,
|
2023-03-31 12:39:00 +08:00
|
|
|
) -> impl Iterator<Item = FullTxOut<ObservedIn<A>>> + 'a
|
2023-03-27 14:21:10 +08:00
|
|
|
where
|
|
|
|
C: ChainOracle<Error = Infallible> + 'a,
|
|
|
|
ObservedIn<A>: ChainPosition,
|
|
|
|
{
|
|
|
|
self.try_list_chain_utxos(chain)
|
|
|
|
.map(|r| r.expect("error is infallible"))
|
|
|
|
}
|
2023-03-27 19:55:57 +08:00
|
|
|
|
|
|
|
pub fn try_balance<C, F>(
|
|
|
|
&self,
|
|
|
|
chain: C,
|
|
|
|
tip: u32,
|
|
|
|
mut should_trust: F,
|
|
|
|
) -> Result<Balance, C::Error>
|
|
|
|
where
|
|
|
|
C: ChainOracle,
|
|
|
|
ObservedIn<A>: ChainPosition + Clone,
|
2023-03-31 12:39:00 +08:00
|
|
|
F: FnMut(&Script) -> bool,
|
2023-03-27 19:55:57 +08:00
|
|
|
{
|
|
|
|
let mut immature = 0;
|
|
|
|
let mut trusted_pending = 0;
|
|
|
|
let mut untrusted_pending = 0;
|
|
|
|
let mut confirmed = 0;
|
|
|
|
|
|
|
|
for res in self.try_list_chain_txouts(&chain) {
|
2023-03-31 12:39:00 +08:00
|
|
|
let txout = res?;
|
2023-03-27 19:55:57 +08:00
|
|
|
|
|
|
|
match &txout.chain_position {
|
|
|
|
ObservedIn::Block(_) => {
|
|
|
|
if txout.is_on_coinbase {
|
|
|
|
if txout.is_mature(tip) {
|
|
|
|
confirmed += txout.txout.value;
|
|
|
|
} else {
|
|
|
|
immature += txout.txout.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ObservedIn::Mempool(_) => {
|
2023-03-31 12:39:00 +08:00
|
|
|
if should_trust(&txout.txout.script_pubkey) {
|
2023-03-27 19:55:57 +08:00
|
|
|
trusted_pending += txout.txout.value;
|
|
|
|
} else {
|
|
|
|
untrusted_pending += txout.txout.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Balance {
|
|
|
|
immature,
|
|
|
|
trusted_pending,
|
|
|
|
untrusted_pending,
|
|
|
|
confirmed,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn balance<C, F>(&self, chain: C, tip: u32, should_trust: F) -> Balance
|
|
|
|
where
|
|
|
|
C: ChainOracle<Error = Infallible>,
|
|
|
|
ObservedIn<A>: ChainPosition + Clone,
|
2023-03-31 12:39:00 +08:00
|
|
|
F: FnMut(&Script) -> bool,
|
2023-03-27 19:55:57 +08:00
|
|
|
{
|
|
|
|
self.try_balance(chain, tip, should_trust)
|
|
|
|
.expect("error is infallible")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn try_balance_at<C>(&self, chain: C, height: u32) -> Result<u64, C::Error>
|
|
|
|
where
|
|
|
|
C: ChainOracle,
|
|
|
|
ObservedIn<A>: ChainPosition + Clone,
|
|
|
|
{
|
|
|
|
let mut sum = 0;
|
2023-03-31 12:39:00 +08:00
|
|
|
for txo_res in self.try_list_chain_txouts(chain) {
|
|
|
|
let txo = txo_res?;
|
2023-03-27 19:55:57 +08:00
|
|
|
if txo.is_spendable_at(height) {
|
|
|
|
sum += txo.txout.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(sum)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn balance_at<C>(&self, chain: C, height: u32) -> u64
|
|
|
|
where
|
|
|
|
C: ChainOracle<Error = Infallible>,
|
|
|
|
ObservedIn<A>: ChainPosition + Clone,
|
|
|
|
{
|
|
|
|
self.try_balance_at(chain, height)
|
|
|
|
.expect("error is infallible")
|
|
|
|
}
|
2023-03-27 14:21:10 +08:00
|
|
|
}
|