Skip to content

Commit

Permalink
refactor!(sqlite): revert DbCommitment and transform logic
Browse files Browse the repository at this point in the history
Update Store to use persist::changesets::CombinedChangeSet directly instead
of trying to convert it to a DbCommitment.
  • Loading branch information
notmandatory committed May 22, 2024
1 parent a0084dc commit 0280923
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 194 deletions.
8 changes: 4 additions & 4 deletions crates/sqlite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ readme = "README.md"

[dependencies]
anyhow = { version = "1", default-features = false }
bdk_chain = { path = "../chain", version = "0.14.0", features = [ "serde", "miniscript" ] }
bdk_persist = { path = "../persist", version = "0.2.0" }
rusqlite = { version = "0.31.0", features = ["bundled"]}
bdk_chain = { path = "../chain", version = "0.14.0", features = ["serde", "miniscript"] }
bdk_persist = { path = "../persist", version = "0.2.0", features = ["serde"] }
rusqlite = { version = "0.31.0", features = ["bundled"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde_json = "1"

# optional
bdk_wallet = { path = "../wallet", version = "1.0.0-alpha.11", optional = true }
Expand Down
123 changes: 0 additions & 123 deletions crates/sqlite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,132 +6,9 @@ mod schema;
mod store;

use bdk_chain::bitcoin::Network;
use bdk_chain::{indexed_tx_graph, keychain, local_chain, Anchor, Append};
pub use rusqlite;
pub use store::Store;

/// Structure representing changes to be committed to the SQLite database.
#[derive(Clone, Debug, PartialEq)]
pub struct DbCommitment<K, A> {
/// Used to save [`Network`] type of the wallet.
pub network: Option<Network>,
/// Changes to [`local_chain::LocalChain`].
pub chain: local_chain::ChangeSet,
/// Changes to [`indexed_tx_graph::IndexedTxGraph`].
pub tx_graph: indexed_tx_graph::ChangeSet<A, keychain::ChangeSet<K>>,
}

impl<K, A> Default for DbCommitment<K, A> {
fn default() -> Self {
DbCommitment {
network: None,
chain: Default::default(),
tx_graph: indexed_tx_graph::ChangeSet::default(),
}
}
}

impl<K: Ord, A: Anchor> Append for DbCommitment<K, A> {
fn append(&mut self, mut other: Self) {
match (self.network, other.network) {
// if current network is Some it can never be changed
(Some(net), Some(other_net)) => assert_eq!(net, other_net),
// if current network is None it can be changed to other
(None, Some(other_net)) => self.network = Some(other_net),
// if other is None then no change
(_, None) => (),
};
self.chain.append(&mut other.chain);
self.tx_graph.append(other.tx_graph);
}

fn is_empty(&self) -> bool {
self.chain.is_empty() && self.tx_graph.is_empty()
}
}

/// Tuple of combined [`bdk_chain`] change-sets.
pub type ChangeSet<K, A> = (
Option<Network>,
local_chain::ChangeSet,
indexed_tx_graph::ChangeSet<A, keychain::ChangeSet<K>>,
);

impl<K, A> From<DbCommitment<K, A>> for ChangeSet<K, A> {
fn from(db_commit: DbCommitment<K, A>) -> Self {
(db_commit.network, db_commit.chain, db_commit.tx_graph)
}
}

impl<K, A> From<ChangeSet<K, A>> for DbCommitment<K, A> {
fn from(changeset: ChangeSet<K, A>) -> Self {
let (network, chain, tx_graph) = changeset;
Self {
network,
chain,
tx_graph,
}
}
}

/// Tuple of combined [`bdk_chain`] change-sets without [`Network`].
pub type ChangeSetWithoutNetwork<K, A> = (
local_chain::ChangeSet,
indexed_tx_graph::ChangeSet<A, keychain::ChangeSet<K>>,
);

impl<K, A> From<DbCommitment<K, A>> for ChangeSetWithoutNetwork<K, A> {
fn from(db_commit: DbCommitment<K, A>) -> Self {
assert_eq!(
db_commit.network, None,
"changeset does not contain `Network`"
);
(db_commit.chain, db_commit.tx_graph)
}
}

impl<K, A> From<ChangeSetWithoutNetwork<K, A>> for DbCommitment<K, A> {
fn from(changeset: ChangeSetWithoutNetwork<K, A>) -> Self {
let (chain, tx_graph) = changeset;
let network = None;
Self {
network,
chain,
tx_graph,
}
}
}

#[cfg(feature = "wallet")]
#[cfg_attr(docsrs, doc(cfg(feature = "wallet")))]
impl From<bdk_wallet::wallet::ChangeSet>
for DbCommitment<bdk_wallet::KeychainKind, bdk_chain::ConfirmationTimeHeightAnchor>
{
fn from(changeset: bdk_wallet::wallet::ChangeSet) -> Self {
Self {
network: changeset.network,
chain: changeset.chain,
tx_graph: changeset.indexed_tx_graph,
}
}
}

#[cfg(feature = "wallet")]
#[cfg_attr(docsrs, doc(cfg(feature = "wallet")))]
impl From<DbCommitment<bdk_wallet::KeychainKind, bdk_chain::ConfirmationTimeHeightAnchor>>
for bdk_wallet::wallet::ChangeSet
{
fn from(
db_commit: DbCommitment<bdk_wallet::KeychainKind, bdk_chain::ConfirmationTimeHeightAnchor>,
) -> Self {
Self {
chain: db_commit.chain,
indexed_tx_graph: db_commit.tx_graph,
network: db_commit.network,
}
}
}

/// Error that occurs while reading or writing change sets with the SQLite database.
#[derive(Debug)]
pub enum Error {
Expand Down
Loading

0 comments on commit 0280923

Please sign in to comment.