-
Notifications
You must be signed in to change notification settings - Fork 331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(wallet)!: rework persistence, changeset, and construction #1514
Changes from all commits
6b43001
eb73f06
22d02ed
af4ee0f
3aed4cf
892b97d
93f9b83
2cf07d6
8875c92
64eb576
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,5 +1,7 @@ | ||||||
//! Contains the [`IndexedTxGraph`] and associated types. Refer to the | ||||||
//! [`IndexedTxGraph`] documentation for more. | ||||||
use core::fmt::Debug; | ||||||
|
||||||
use alloc::vec::Vec; | ||||||
use bitcoin::{Block, OutPoint, Transaction, TxOut, Txid}; | ||||||
|
||||||
|
@@ -47,21 +49,24 @@ impl<A: Anchor, I: Indexer> IndexedTxGraph<A, I> { | |||||
pub fn apply_changeset(&mut self, changeset: ChangeSet<A, I::ChangeSet>) { | ||||||
self.index.apply_changeset(changeset.indexer); | ||||||
|
||||||
for tx in &changeset.graph.txs { | ||||||
for tx in &changeset.tx_graph.txs { | ||||||
self.index.index_tx(tx); | ||||||
} | ||||||
for (&outpoint, txout) in &changeset.graph.txouts { | ||||||
for (&outpoint, txout) in &changeset.tx_graph.txouts { | ||||||
self.index.index_txout(outpoint, txout); | ||||||
} | ||||||
|
||||||
self.graph.apply_changeset(changeset.graph); | ||||||
self.graph.apply_changeset(changeset.tx_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 } | ||||||
ChangeSet { | ||||||
tx_graph: graph, | ||||||
indexer, | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
|
@@ -89,21 +94,30 @@ where | |||||
pub fn apply_update(&mut self, update: TxGraph<A>) -> ChangeSet<A, I::ChangeSet> { | ||||||
let graph = self.graph.apply_update(update); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⛏️ same small nit.
Suggested change
|
||||||
let indexer = self.index_tx_graph_changeset(&graph); | ||||||
ChangeSet { graph, indexer } | ||||||
ChangeSet { | ||||||
tx_graph: graph, | ||||||
indexer, | ||||||
} | ||||||
} | ||||||
|
||||||
/// Insert a floating `txout` of given `outpoint`. | ||||||
pub fn insert_txout(&mut self, outpoint: OutPoint, txout: TxOut) -> ChangeSet<A, I::ChangeSet> { | ||||||
let graph = self.graph.insert_txout(outpoint, txout); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⛏️ same nit.
Suggested change
|
||||||
let indexer = self.index_tx_graph_changeset(&graph); | ||||||
ChangeSet { graph, indexer } | ||||||
ChangeSet { | ||||||
tx_graph: graph, | ||||||
indexer, | ||||||
} | ||||||
} | ||||||
|
||||||
/// Insert and index a transaction into the graph. | ||||||
pub fn insert_tx(&mut self, tx: Transaction) -> ChangeSet<A, I::ChangeSet> { | ||||||
let graph = self.graph.insert_tx(tx); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⛏️ nit and same for rest of file.
Suggested change
|
||||||
let indexer = self.index_tx_graph_changeset(&graph); | ||||||
ChangeSet { graph, indexer } | ||||||
ChangeSet { | ||||||
tx_graph: graph, | ||||||
indexer, | ||||||
} | ||||||
} | ||||||
|
||||||
/// Insert an `anchor` for a given transaction. | ||||||
|
@@ -151,7 +165,10 @@ where | |||||
} | ||||||
} | ||||||
|
||||||
ChangeSet { graph, indexer } | ||||||
ChangeSet { | ||||||
tx_graph: graph, | ||||||
indexer, | ||||||
} | ||||||
} | ||||||
|
||||||
/// Batch insert unconfirmed transactions, filtering out those that are irrelevant. | ||||||
|
@@ -185,7 +202,10 @@ where | |||||
.map(|(tx, seen_at)| (tx.clone(), seen_at)), | ||||||
); | ||||||
|
||||||
ChangeSet { graph, indexer } | ||||||
ChangeSet { | ||||||
tx_graph: graph, | ||||||
indexer, | ||||||
} | ||||||
} | ||||||
|
||||||
/// Batch insert unconfirmed transactions. | ||||||
|
@@ -203,7 +223,10 @@ where | |||||
) -> ChangeSet<A, I::ChangeSet> { | ||||||
let graph = self.graph.batch_insert_unconfirmed(txs); | ||||||
let indexer = self.index_tx_graph_changeset(&graph); | ||||||
ChangeSet { graph, indexer } | ||||||
ChangeSet { | ||||||
tx_graph: graph, | ||||||
indexer, | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
|
@@ -236,9 +259,9 @@ where | |||||
if self.index.is_tx_relevant(tx) { | ||||||
let txid = tx.compute_txid(); | ||||||
let anchor = A::from_block_position(block, block_id, tx_pos); | ||||||
changeset.graph.merge(self.graph.insert_tx(tx.clone())); | ||||||
changeset.tx_graph.merge(self.graph.insert_tx(tx.clone())); | ||||||
changeset | ||||||
.graph | ||||||
.tx_graph | ||||||
.merge(self.graph.insert_anchor(txid, anchor)); | ||||||
} | ||||||
} | ||||||
|
@@ -265,7 +288,16 @@ where | |||||
graph.merge(self.graph.insert_tx(tx.clone())); | ||||||
} | ||||||
let indexer = self.index_tx_graph_changeset(&graph); | ||||||
ChangeSet { graph, indexer } | ||||||
ChangeSet { | ||||||
tx_graph: graph, | ||||||
indexer, | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
impl<A, I> AsRef<TxGraph<A>> for IndexedTxGraph<A, I> { | ||||||
fn as_ref(&self) -> &TxGraph<A> { | ||||||
&self.graph | ||||||
} | ||||||
} | ||||||
|
||||||
|
@@ -285,54 +317,46 @@ where | |||||
#[must_use] | ||||||
pub struct ChangeSet<A, IA> { | ||||||
/// [`TxGraph`] changeset. | ||||||
pub graph: tx_graph::ChangeSet<A>, | ||||||
pub tx_graph: tx_graph::ChangeSet<A>, | ||||||
/// [`Indexer`] changeset. | ||||||
pub indexer: IA, | ||||||
} | ||||||
|
||||||
impl<A, IA: Default> Default for ChangeSet<A, IA> { | ||||||
fn default() -> Self { | ||||||
Self { | ||||||
graph: Default::default(), | ||||||
tx_graph: Default::default(), | ||||||
indexer: Default::default(), | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
impl<A: Anchor, IA: Merge> Merge for ChangeSet<A, IA> { | ||||||
fn merge(&mut self, other: Self) { | ||||||
self.graph.merge(other.graph); | ||||||
self.tx_graph.merge(other.tx_graph); | ||||||
self.indexer.merge(other.indexer); | ||||||
} | ||||||
|
||||||
fn is_empty(&self) -> bool { | ||||||
self.graph.is_empty() && self.indexer.is_empty() | ||||||
self.tx_graph.is_empty() && self.indexer.is_empty() | ||||||
} | ||||||
} | ||||||
|
||||||
impl<A, IA: Default> From<tx_graph::ChangeSet<A>> for ChangeSet<A, IA> { | ||||||
fn from(graph: tx_graph::ChangeSet<A>) -> Self { | ||||||
Self { | ||||||
graph, | ||||||
tx_graph: graph, | ||||||
..Default::default() | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
#[cfg(feature = "miniscript")] | ||||||
impl<A, K> From<crate::indexer::keychain_txout::ChangeSet<K>> | ||||||
for ChangeSet<A, crate::indexer::keychain_txout::ChangeSet<K>> | ||||||
{ | ||||||
fn from(indexer: crate::indexer::keychain_txout::ChangeSet<K>) -> Self { | ||||||
impl<A> From<crate::keychain_txout::ChangeSet> for ChangeSet<A, crate::keychain_txout::ChangeSet> { | ||||||
fn from(indexer: crate::keychain_txout::ChangeSet) -> Self { | ||||||
Self { | ||||||
graph: Default::default(), | ||||||
tx_graph: Default::default(), | ||||||
indexer, | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
impl<A, I> AsRef<TxGraph<A>> for IndexedTxGraph<A, I> { | ||||||
fn as_ref(&self) -> &TxGraph<A> { | ||||||
&self.graph | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⛏️ small nit.