chain: split IndexedTxGraph::insert_tx
into 3 methods
Instead of inserting anchors and seen_at timestamp in the same method, we have three separate methods. This makes the API easier to understand and makes `IndexedTxGraph` more consistent with the `TxGraph` API.
This commit is contained in:
parent
4f5695d43a
commit
6d4b33ef91
@ -738,7 +738,16 @@ impl<D> Wallet<D> {
|
|||||||
ConfirmationTime::Unconfirmed { last_seen } => (None, Some(last_seen)),
|
ConfirmationTime::Unconfirmed { last_seen } => (None, Some(last_seen)),
|
||||||
};
|
};
|
||||||
|
|
||||||
let changeset: ChangeSet = self.indexed_graph.insert_tx(&tx, anchor, last_seen).into();
|
let mut changeset = ChangeSet::default();
|
||||||
|
let txid = tx.txid();
|
||||||
|
changeset.append(self.indexed_graph.insert_tx(tx).into());
|
||||||
|
if let Some(anchor) = anchor {
|
||||||
|
changeset.append(self.indexed_graph.insert_anchor(txid, anchor).into());
|
||||||
|
}
|
||||||
|
if let Some(last_seen) = last_seen {
|
||||||
|
changeset.append(self.indexed_graph.insert_seen_at(txid, last_seen).into());
|
||||||
|
}
|
||||||
|
|
||||||
let changed = !changeset.is_empty();
|
let changed = !changeset.is_empty();
|
||||||
self.persist.stage(changeset);
|
self.persist.stage(changeset);
|
||||||
Ok(changed)
|
Ok(changed)
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
//! This is essentially a [`TxGraph`] combined with an indexer.
|
//! This is essentially a [`TxGraph`] combined with an indexer.
|
||||||
|
|
||||||
use alloc::vec::Vec;
|
use alloc::vec::Vec;
|
||||||
use bitcoin::{Block, OutPoint, Transaction, TxOut};
|
use bitcoin::{Block, OutPoint, Transaction, TxOut, Txid};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
keychain,
|
keychain,
|
||||||
@ -103,32 +103,25 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Insert and index a transaction into the graph.
|
/// Insert and index a transaction into the graph.
|
||||||
///
|
pub fn insert_tx(&mut self, tx: Transaction) -> ChangeSet<A, I::ChangeSet> {
|
||||||
/// `anchors` can be provided to anchor the transaction to various blocks. `seen_at` is a
|
let graph = self.graph.insert_tx(tx);
|
||||||
/// unix timestamp of when the transaction is last seen.
|
|
||||||
pub fn insert_tx(
|
|
||||||
&mut self,
|
|
||||||
tx: &Transaction,
|
|
||||||
anchors: impl IntoIterator<Item = A>,
|
|
||||||
seen_at: Option<u64>,
|
|
||||||
) -> ChangeSet<A, I::ChangeSet> {
|
|
||||||
let txid = tx.txid();
|
|
||||||
|
|
||||||
let mut graph = tx_graph::ChangeSet::default();
|
|
||||||
if self.graph.get_tx(txid).is_none() {
|
|
||||||
graph.append(self.graph.insert_tx(tx.clone()));
|
|
||||||
}
|
|
||||||
for anchor in anchors.into_iter() {
|
|
||||||
graph.append(self.graph.insert_anchor(txid, anchor));
|
|
||||||
}
|
|
||||||
if let Some(seen_at) = seen_at {
|
|
||||||
graph.append(self.graph.insert_seen_at(txid, seen_at));
|
|
||||||
}
|
|
||||||
|
|
||||||
let indexer = self.index_tx_graph_changeset(&graph);
|
let indexer = self.index_tx_graph_changeset(&graph);
|
||||||
ChangeSet { graph, indexer }
|
ChangeSet { graph, indexer }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Insert an `anchor` for a given transaction.
|
||||||
|
pub fn insert_anchor(&mut self, txid: Txid, anchor: A) -> ChangeSet<A, I::ChangeSet> {
|
||||||
|
self.graph.insert_anchor(txid, anchor).into()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Insert a unix timestamp of when a transaction is seen in the mempool.
|
||||||
|
///
|
||||||
|
/// This is used for transaction conflict resolution in [`TxGraph`] where the transaction with
|
||||||
|
/// the later last-seen is prioritized.
|
||||||
|
pub fn insert_seen_at(&mut self, txid: Txid, seen_at: u64) -> ChangeSet<A, I::ChangeSet> {
|
||||||
|
self.graph.insert_seen_at(txid, seen_at).into()
|
||||||
|
}
|
||||||
|
|
||||||
/// Batch insert transactions, filtering out those that are irrelevant.
|
/// Batch insert transactions, filtering out those that are irrelevant.
|
||||||
///
|
///
|
||||||
/// Relevancy is determined by the [`Indexer::is_tx_relevant`] implementation of `I`. Irrelevant
|
/// Relevancy is determined by the [`Indexer::is_tx_relevant`] implementation of `I`. Irrelevant
|
||||||
|
@ -624,8 +624,7 @@ where
|
|||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
println!("Broadcasted Tx : {}", transaction.txid());
|
println!("Broadcasted Tx : {}", transaction.txid());
|
||||||
|
|
||||||
let keychain_changeset =
|
let keychain_changeset = graph.lock().unwrap().insert_tx(transaction);
|
||||||
graph.lock().unwrap().insert_tx(&transaction, None, None);
|
|
||||||
|
|
||||||
// We know the tx is at least unconfirmed now. Note if persisting here fails,
|
// We know the tx is at least unconfirmed now. Note if persisting here fails,
|
||||||
// it's not a big deal since we can always find it again form
|
// it's not a big deal since we can always find it again form
|
||||||
|
Loading…
x
Reference in New Issue
Block a user