Remove transaction-based type parameters and traits

This commit is contained in:
志宇
2023-03-08 11:39:25 +13:00
parent de9457fce6
commit 0505cd7242
12 changed files with 180 additions and 336 deletions

View File

@@ -3,9 +3,8 @@
//! The star of the show is [`KeychainStore`] which maintains an append-only file of
//! [`KeychainChangeSet`]s which can be used to restore a [`KeychainTracker`].
use bdk_chain::{
bitcoin::Transaction,
keychain::{KeychainChangeSet, KeychainTracker},
sparse_chain, AsTransaction,
sparse_chain,
};
use bincode::{DefaultOptions, Options};
use core::marker::PhantomData;
@@ -24,21 +23,20 @@ const MAGIC_BYTES: [u8; MAGIC_BYTES_LEN] = [98, 100, 107, 102, 115, 48, 48, 48,
/// Persists an append only list of `KeychainChangeSet<K,P>` to a single file.
/// [`KeychainChangeSet<K,P>`] record the changes made to a [`KeychainTracker<K,P>`].
#[derive(Debug)]
pub struct KeychainStore<K, P, T = Transaction> {
pub struct KeychainStore<K, P> {
db_file: File,
changeset_type_params: core::marker::PhantomData<(K, P, T)>,
changeset_type_params: core::marker::PhantomData<(K, P)>,
}
fn bincode() -> impl bincode::Options {
DefaultOptions::new().with_varint_encoding()
}
impl<K, P, T> KeychainStore<K, P, T>
impl<K, P> KeychainStore<K, P>
where
K: Ord + Clone + core::fmt::Debug,
P: sparse_chain::ChainPosition,
T: Ord + AsTransaction + Clone,
KeychainChangeSet<K, P, T>: serde::Serialize + serde::de::DeserializeOwned,
KeychainChangeSet<K, P>: serde::Serialize + serde::de::DeserializeOwned,
{
/// Creates a new store from a [`File`].
///
@@ -87,9 +85,7 @@ where
/// **WARNING**: This method changes the write position in the underlying file. You should
/// always iterate over all entries until `None` is returned if you want your next write to go
/// at the end, otherwise you will write over existing enties.
pub fn iter_changesets(
&mut self,
) -> Result<EntryIter<'_, KeychainChangeSet<K, P, T>>, io::Error> {
pub fn iter_changesets(&mut self) -> Result<EntryIter<'_, KeychainChangeSet<K, P>>, io::Error> {
self.db_file
.seek(io::SeekFrom::Start(MAGIC_BYTES_LEN as _))?;
@@ -108,7 +104,7 @@ where
///
/// **WARNING**: This method changes the write position of the underlying file. The next
/// changeset will be written over the erroring entry (or the end of the file if none existed).
pub fn aggregate_changeset(&mut self) -> (KeychainChangeSet<K, P, T>, Result<(), IterError>) {
pub fn aggregate_changeset(&mut self) -> (KeychainChangeSet<K, P>, Result<(), IterError>) {
let mut changeset = KeychainChangeSet::default();
let result = (|| {
let iter_changeset = self.iter_changesets()?;
@@ -128,7 +124,7 @@ where
/// changeset will be written over the erroring entry (or the end of the file if none existed).
pub fn load_into_keychain_tracker(
&mut self,
tracker: &mut KeychainTracker<K, P, T>,
tracker: &mut KeychainTracker<K, P>,
) -> Result<(), IterError> {
for changeset in self.iter_changesets()? {
tracker.apply_changeset(changeset?)
@@ -142,7 +138,7 @@ where
/// directly after the appended changeset.
pub fn append_changeset(
&mut self,
changeset: &KeychainChangeSet<K, P, T>,
changeset: &KeychainChangeSet<K, P>,
) -> Result<(), io::Error> {
if changeset.is_empty() {
return Ok(());
@@ -291,7 +287,6 @@ impl From<io::Error> for IterError {
mod test {
use super::*;
use bdk_chain::{
bitcoin::Transaction,
keychain::{DerivationAdditions, KeychainChangeSet},
TxHeight,
};
@@ -337,7 +332,7 @@ mod test {
file.write_all(&MAGIC_BYTES[..MAGIC_BYTES_LEN - 1])
.expect("should write");
match KeychainStore::<TestKeychain, TxHeight, Transaction>::new(file.reopen().unwrap()) {
match KeychainStore::<TestKeychain, TxHeight>::new(file.reopen().unwrap()) {
Err(FileError::Io(e)) => assert_eq!(e.kind(), std::io::ErrorKind::UnexpectedEof),
unexpected => panic!("unexpected result: {:?}", unexpected),
};
@@ -351,7 +346,7 @@ mod test {
file.write_all(invalid_magic_bytes.as_bytes())
.expect("should write");
match KeychainStore::<TestKeychain, TxHeight, Transaction>::new(file.reopen().unwrap()) {
match KeychainStore::<TestKeychain, TxHeight>::new(file.reopen().unwrap()) {
Err(FileError::InvalidMagicBytes(b)) => {
assert_eq!(b, invalid_magic_bytes.as_bytes())
}
@@ -375,9 +370,8 @@ mod test {
let mut file = NamedTempFile::new().unwrap();
file.write_all(&data).expect("should write");
let mut store =
KeychainStore::<TestKeychain, TxHeight, Transaction>::new(file.reopen().unwrap())
.expect("should open");
let mut store = KeychainStore::<TestKeychain, TxHeight>::new(file.reopen().unwrap())
.expect("should open");
match store.iter_changesets().expect("seek should succeed").next() {
Some(Err(IterError::Bincode(_))) => {}
unexpected_res => panic!("unexpected result: {:?}", unexpected_res),

View File

@@ -1,6 +1,5 @@
mod file_store;
use bdk_chain::{
bitcoin::Transaction,
keychain::{KeychainChangeSet, KeychainTracker, PersistBackend},
sparse_chain::ChainPosition,
};
@@ -10,7 +9,7 @@ impl<K, P> PersistBackend<K, P> for KeychainStore<K, P>
where
K: Ord + Clone + core::fmt::Debug,
P: ChainPosition,
KeychainChangeSet<K, P, Transaction>: serde::Serialize + serde::de::DeserializeOwned,
KeychainChangeSet<K, P>: serde::Serialize + serde::de::DeserializeOwned,
{
type WriteError = std::io::Error;