Skip to content

Commit c09cd2a

Browse files
committed
[bdk_chain_redesign] Added methods to LocalChain
Also made the `IndexedTxGraph::index` field public (`index()` and `index_mut()` methods are no longer needed).
1 parent 7810059 commit c09cd2a

2 files changed

Lines changed: 49 additions & 14 deletions

File tree

crates/chain/src/indexed_tx_graph.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ impl<A: BlockAnchor, IA: AddAssign> AddAssign for IndexedAdditions<A, IA> {
5858
}
5959

6060
pub struct IndexedTxGraph<A, I> {
61+
/// Transaction index.
62+
pub index: I,
6163
graph: TxGraph<A>,
62-
index: I, // [TODO] Make public
6364
last_height: u32,
6465
}
6566

@@ -79,16 +80,6 @@ impl<A: BlockAnchor, I: TxIndex> IndexedTxGraph<A, I> {
7980
&self.graph
8081
}
8182

82-
/// Get a reference of the internal transaction index.
83-
pub fn index(&self) -> &I {
84-
&self.index
85-
}
86-
87-
/// Get a mutable reference to the internal transaction index.
88-
pub fn index_mut(&mut self) -> &mut I {
89-
&mut self.index
90-
}
91-
9283
/// Applies the [`IndexedAdditions`] to the [`IndexedTxGraph`].
9384
pub fn apply_additions(&mut self, additions: IndexedAdditions<A, I::Additions>) {
9485
let IndexedAdditions {

crates/chain/src/local_chain.rs

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
use core::convert::Infallible;
1+
use core::{convert::Infallible, ops::Deref};
22

3-
use alloc::{collections::BTreeMap, vec::Vec};
3+
use alloc::{
4+
collections::{BTreeMap, BTreeSet},
5+
vec::Vec,
6+
};
47
use bitcoin::BlockHash;
58

69
use crate::{BlockId, ChainOracle};
@@ -104,11 +107,52 @@ impl LocalChain {
104107
}
105108
Ok(ChangeSet(changeset))
106109
}
110+
111+
/// Applies the given `changeset`.
112+
pub fn apply_changeset(&mut self, mut changeset: ChangeSet) {
113+
self.blocks.append(&mut changeset.0)
114+
}
115+
116+
/// Updates [`LocalChain`] with an update [`LocalChain`].
117+
///
118+
/// This is equivilant to calling [`determine_changeset`] and [`apply_changeset`] in sequence.
119+
///
120+
/// [`determine_changeset`]: Self::determine_changeset
121+
/// [`apply_changeset`]: Self::apply_changeset
122+
pub fn apply_update(&mut self, update: Self) -> Result<ChangeSet, UpdateError> {
123+
let changeset = self.determine_changeset(&update)?;
124+
self.apply_changeset(changeset.clone());
125+
Ok(changeset)
126+
}
127+
128+
pub fn initial_changeset(&self) -> ChangeSet {
129+
ChangeSet(self.blocks.clone())
130+
}
131+
132+
pub fn heights(&self) -> BTreeSet<u32> {
133+
self.blocks.keys().cloned().collect()
134+
}
107135
}
108136

109-
#[derive(Debug, Default)]
137+
/// This is the return value of [`determine_changeset`] and represents changes to [`LocalChain`].
138+
///
139+
/// [`determine_changeset`]: LocalChain::determine_changeset
140+
#[derive(Debug, Default, Clone, PartialEq)]
141+
#[cfg_attr(
142+
feature = "serde",
143+
derive(serde::Deserialize, serde::Serialize),
144+
serde(crate = "serde_crate")
145+
)]
110146
pub struct ChangeSet(pub BTreeMap<u32, BlockHash>);
111147

148+
impl Deref for ChangeSet {
149+
type Target = BTreeMap<u32, BlockHash>;
150+
151+
fn deref(&self) -> &Self::Target {
152+
&self.0
153+
}
154+
}
155+
112156
/// Represents an update failure of [`LocalChain`].j
113157
#[derive(Clone, Debug, PartialEq)]
114158
pub enum UpdateError {

0 commit comments

Comments
 (0)