Skip to content

Commit

Permalink
feat(chain): implement default index_tx method
Browse files Browse the repository at this point in the history
Co-authored-by: 志宇 <hello@evanlinjin.me>
  • Loading branch information
vladimirfomene and evanlinjin committed Jan 31, 2024
1 parent ba76247 commit 680fe94
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
35 changes: 22 additions & 13 deletions crates/chain/src/indexed_tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ impl<A, I> IndexedTxGraph<A, I> {
}

impl<A: Anchor, I: Indexer> IndexedTxGraph<A, I> {
/// Determines the [`ChangeSet`] between `self` and an empty [`IndexedTxGraph`].
pub fn initial_changeset(&self) -> ChangeSet<A, I::ChangeSet> {
let graph = self.graph.initial_changeset();
let indexer = self.index.initial_changeset();
ChangeSet { graph, indexer }
}
}

impl<A: Anchor, I: Indexer> IndexedTxGraph<A, I>
where
I::ChangeSet: Default + Append,
{
/// Applies the [`ChangeSet`] to the [`IndexedTxGraph`].
pub fn apply_changeset(&mut self, changeset: ChangeSet<A, I::ChangeSet>) {
self.index.apply_changeset(changeset.indexer);
Expand All @@ -58,18 +70,6 @@ impl<A: Anchor, I: Indexer> IndexedTxGraph<A, I> {
self.graph.apply_changeset(changeset.graph);
}

/// Determines the [`ChangeSet`] between `self` and an empty [`IndexedTxGraph`].
pub fn initial_changeset(&self) -> ChangeSet<A, I::ChangeSet> {
let graph = self.graph.initial_changeset();
let indexer = self.index.initial_changeset();
ChangeSet { graph, indexer }
}
}

impl<A: Anchor, I: Indexer> IndexedTxGraph<A, I>
where
I::ChangeSet: Default + Append,
{
fn index_tx_graph_changeset(
&mut self,
tx_graph_changeset: &tx_graph::ChangeSet<A>,
Expand Down Expand Up @@ -341,7 +341,16 @@ pub trait Indexer {
fn index_txout(&mut self, outpoint: OutPoint, txout: &TxOut) -> Self::ChangeSet;

/// Scans a transaction for relevant outpoints, which are stored and indexed internally.
fn index_tx(&mut self, tx: &Transaction) -> Self::ChangeSet;
fn index_tx(&mut self, tx: &Transaction) -> Self::ChangeSet
where
Self::ChangeSet: Default + Append,
{
let mut changeset = Self::ChangeSet::default();
for (op, txout) in tx.output.iter().enumerate() {
changeset.append(self.index_txout(OutPoint::new(tx.txid(), op as u32), txout));
}
changeset
}

/// Apply changeset to itself.
fn apply_changeset(&mut self, changeset: Self::ChangeSet);
Expand Down
8 changes: 0 additions & 8 deletions crates/chain/src/keychain/txout_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,6 @@ impl<K: Clone + Ord + Debug> Indexer for KeychainTxOutIndex<K> {
}
}

fn index_tx(&mut self, tx: &bitcoin::Transaction) -> Self::ChangeSet {
let mut changeset = super::ChangeSet::<K>::default();
for (op, txout) in tx.output.iter().enumerate() {
changeset.append(self.index_txout(OutPoint::new(tx.txid(), op as u32), txout));
}
changeset
}

fn initial_changeset(&self) -> Self::ChangeSet {
super::ChangeSet(self.last_revealed.clone())
}
Expand Down

0 comments on commit 680fe94

Please sign in to comment.