[bdk_chain_redesign] Added methods to LocalChain

Also made the `IndexedTxGraph::index` field public (`index()` and
`index_mut()` methods are no longer needed).
This commit is contained in:
志宇 2023-03-31 22:42:47 +08:00
parent 7810059ed0
commit c09cd2afce
No known key found for this signature in database
GPG Key ID: F6345C9837C2BDE8
2 changed files with 49 additions and 14 deletions

View 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 {

View 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 {