]> Untitled Git - bdk/commitdiff
[bdk_chain_redesign] Added methods to `LocalChain`
author志宇 <hello@evanlinjin.me>
Fri, 31 Mar 2023 14:42:47 +0000 (22:42 +0800)
committer志宇 <hello@evanlinjin.me>
Fri, 31 Mar 2023 14:42:47 +0000 (22:42 +0800)
Also made the `IndexedTxGraph::index` field public (`index()` and
`index_mut()` methods are no longer needed).

crates/chain/src/indexed_tx_graph.rs
crates/chain/src/local_chain.rs

index 0b27150c6837892df44e1c0cc8d9a1cb2309fdc0..79e5105ce5ddbb20bfa6aabf711d81036cee2ff0 100644 (file)
@@ -58,8 +58,9 @@ impl<A: BlockAnchor, IA: AddAssign> AddAssign for IndexedAdditions<A, IA> {
 }
 
 pub struct IndexedTxGraph<A, I> {
+    /// Transaction index.
+    pub index: I,
     graph: TxGraph<A>,
-    index: I, // [TODO] Make public
     last_height: u32,
 }
 
@@ -79,16 +80,6 @@ impl<A: BlockAnchor, I: TxIndex> IndexedTxGraph<A, I> {
         &self.graph
     }
 
-    /// Get a reference of the internal transaction index.
-    pub fn index(&self) -> &I {
-        &self.index
-    }
-
-    /// Get a mutable reference to the internal transaction index.
-    pub fn index_mut(&mut self) -> &mut I {
-        &mut self.index
-    }
-
     /// Applies the [`IndexedAdditions`] to the [`IndexedTxGraph`].
     pub fn apply_additions(&mut self, additions: IndexedAdditions<A, I::Additions>) {
         let IndexedAdditions {
index 5bcb524f3a17ba8a1245ccd53fcc3a869ce6939d..5d459a15fd0779526daa4622ecc2a9aef99607a0 100644 (file)
@@ -1,6 +1,9 @@
-use core::convert::Infallible;
+use core::{convert::Infallible, ops::Deref};
 
-use alloc::{collections::BTreeMap, vec::Vec};
+use alloc::{
+    collections::{BTreeMap, BTreeSet},
+    vec::Vec,
+};
 use bitcoin::BlockHash;
 
 use crate::{BlockId, ChainOracle};
@@ -104,11 +107,52 @@ impl LocalChain {
         }
         Ok(ChangeSet(changeset))
     }
+
+    /// Applies the given `changeset`.
+    pub fn apply_changeset(&mut self, mut changeset: ChangeSet) {
+        self.blocks.append(&mut changeset.0)
+    }
+
+    /// Updates [`LocalChain`] with an update [`LocalChain`].
+    ///
+    /// This is equivilant to calling [`determine_changeset`] and [`apply_changeset`] in sequence.
+    ///
+    /// [`determine_changeset`]: Self::determine_changeset
+    /// [`apply_changeset`]: Self::apply_changeset
+    pub fn apply_update(&mut self, update: Self) -> Result<ChangeSet, UpdateError> {
+        let changeset = self.determine_changeset(&update)?;
+        self.apply_changeset(changeset.clone());
+        Ok(changeset)
+    }
+
+    pub fn initial_changeset(&self) -> ChangeSet {
+        ChangeSet(self.blocks.clone())
+    }
+
+    pub fn heights(&self) -> BTreeSet<u32> {
+        self.blocks.keys().cloned().collect()
+    }
 }
 
-#[derive(Debug, Default)]
+/// This is the return value of [`determine_changeset`] and represents changes to [`LocalChain`].
+///
+/// [`determine_changeset`]: LocalChain::determine_changeset
+#[derive(Debug, Default, Clone, PartialEq)]
+#[cfg_attr(
+    feature = "serde",
+    derive(serde::Deserialize, serde::Serialize),
+    serde(crate = "serde_crate")
+)]
 pub struct ChangeSet(pub BTreeMap<u32, BlockHash>);
 
+impl Deref for ChangeSet {
+    type Target = BTreeMap<u32, BlockHash>;
+
+    fn deref(&self) -> &Self::Target {
+        &self.0
+    }
+}
+
 /// Represents an update failure of [`LocalChain`].j
 #[derive(Clone, Debug, PartialEq)]
 pub enum UpdateError {