Skip to content

Commit 4e5424e

Browse files
committed
[persist_redesign] Remove Loadable trait
It is not needed. Also moved `tracker_example_cli::RemoteChain` into it's own file.
1 parent f31eac5 commit 4e5424e

File tree

7 files changed

+217
-184
lines changed

7 files changed

+217
-184
lines changed

crates/chain/src/local_chain.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use core::convert::Infallible;
33
use alloc::collections::{BTreeMap, BTreeSet};
44
use bitcoin::BlockHash;
55

6-
use crate::{BlockId, ChainOracle, Loadable};
6+
use crate::{BlockId, ChainOracle};
77

88
/// This is a local implementation of [`ChainOracle`].
99
///
@@ -43,14 +43,6 @@ impl ChainOracle for LocalChain {
4343
}
4444
}
4545

46-
impl Loadable for LocalChain {
47-
type ChangeSet = ChangeSet;
48-
49-
fn load_changeset(&mut self, changeset: Self::ChangeSet) {
50-
self.apply_changeset(changeset)
51-
}
52-
}
53-
5446
impl AsRef<BTreeMap<u32, BlockHash>> for LocalChain {
5547
fn as_ref(&self) -> &BTreeMap<u32, BlockHash> {
5648
&self.blocks

crates/chain/src/persist.rs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ use crate::Append;
88
/// Not all changes to the tracker, which is an in-memory representation of wallet/blockchain
99
/// data, needs to be written to disk right away, so [`Persist::stage`] can be used to *stage*
1010
/// changes first and then [`Persist::commit`] can be used to write changes to disk.
11-
pub struct Persist<T, C, B> {
11+
pub struct Persist<B, T, C> {
1212
backend: B,
1313
stage: C,
1414
marker: PhantomData<T>,
1515
}
1616

17-
impl<T, B> Persist<T, T::ChangeSet, B>
17+
impl<B, T, C> Persist<B, T, C>
1818
where
19-
T: Loadable,
20-
B: PersistBackend<T>,
19+
B: PersistBackend<T, C>,
20+
C: Default + Append,
2121
{
2222
/// Create a new [`Persist`] from [`PersistBackend`].
2323
pub fn new(backend: B) -> Self {
@@ -31,20 +31,20 @@ where
3131
/// Stage a `changeset` to be commited later with [`commit`].
3232
///
3333
/// [`commit`]: Self::commit
34-
pub fn stage(&mut self, changeset: T::ChangeSet) {
34+
pub fn stage(&mut self, changeset: C) {
3535
self.stage.append(changeset)
3636
}
3737

3838
/// Get the changes that have not been commited yet.
39-
pub fn staged(&self) -> &T::ChangeSet {
39+
pub fn staged(&self) -> &C {
4040
&self.stage
4141
}
4242

4343
/// Commit the staged changes to the underlying persistance backend.
4444
///
4545
/// Returns a backend-defined error if this fails.
4646
pub fn commit(&mut self) -> Result<(), B::WriteError> {
47-
let mut temp = T::ChangeSet::default();
47+
let mut temp = C::default();
4848
core::mem::swap(&mut temp, &mut self.stage);
4949
self.backend.write_changes(&temp)
5050
}
@@ -53,7 +53,7 @@ where
5353
/// A persistence backend for [`Persist`].
5454
///
5555
/// `T` represents the tracker, the in-memory data structure which we wish to persist.
56-
pub trait PersistBackend<T: Loadable> {
56+
pub trait PersistBackend<T, C> {
5757
/// The error the backend returns when it fails to write.
5858
type WriteError: core::fmt::Debug;
5959

@@ -68,29 +68,21 @@ pub trait PersistBackend<T: Loadable> {
6868
/// changesets had been applied sequentially.
6969
///
7070
/// [`load_into_tracker`]: Self::load_into_tracker
71-
fn write_changes(&mut self, changeset: &T::ChangeSet) -> Result<(), Self::WriteError>;
71+
fn write_changes(&mut self, changeset: &C) -> Result<(), Self::WriteError>;
7272

7373
/// Loads all data from the persistence backend into `tracker`.
7474
fn load_into_tracker(&mut self, tracker: &mut T) -> Result<(), Self::LoadError>;
7575
}
7676

77-
impl<T: Loadable> PersistBackend<T> for () {
77+
impl<T, C> PersistBackend<T, C> for () {
7878
type WriteError = ();
7979
type LoadError = ();
8080

81-
fn write_changes(&mut self, _changeset: &T::ChangeSet) -> Result<(), Self::WriteError> {
81+
fn write_changes(&mut self, _changeset: &C) -> Result<(), Self::WriteError> {
8282
Ok(())
8383
}
8484

8585
fn load_into_tracker(&mut self, _tracker: &mut T) -> Result<(), Self::LoadError> {
8686
Ok(())
8787
}
8888
}
89-
90-
/// A trait that represents a structure which can be loaded with changesets.
91-
pub trait Loadable {
92-
/// The changeset to be loaded into `self`.
93-
type ChangeSet: Default + Append;
94-
/// Loads the `changeset` into `self`.
95-
fn load_changeset(&mut self, changeset: Self::ChangeSet);
96-
}

crates/file_store/src/store.rs

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55
path::Path,
66
};
77

8-
use bdk_chain::{Append, Loadable, PersistBackend};
8+
use bdk_chain::Append;
99
use bincode::Options;
1010

1111
use crate::{bincode_options, EntryIter, FileError, IterError};
@@ -14,16 +14,15 @@ use crate::{bincode_options, EntryIter, FileError, IterError};
1414
///
1515
/// The changesets are the results of altering a tracker implementation (`T`).
1616
#[derive(Debug)]
17-
pub struct Store<T> {
17+
pub struct Store<T, C> {
1818
magic: &'static [u8],
1919
db_file: File,
20-
marker: PhantomData<T>,
20+
marker: PhantomData<(T, C)>,
2121
}
2222

23-
impl<T> Store<T>
23+
impl<T, C> Store<T, C>
2424
where
25-
T: Loadable,
26-
T::ChangeSet: serde::Serialize + serde::de::DeserializeOwned,
25+
C: Default + Append + serde::Serialize + serde::de::DeserializeOwned,
2726
{
2827
/// Creates a new store from a [`File`].
2928
///
@@ -81,7 +80,7 @@ where
8180
/// **WARNING**: This method changes the write position in the underlying file. You should
8281
/// always iterate over all entries until `None` is returned if you want your next write to go
8382
/// at the end; otherwise, you will write over existing entries.
84-
pub fn iter_changesets(&mut self) -> Result<EntryIter<'_, T::ChangeSet>, io::Error> {
83+
pub fn iter_changesets(&mut self) -> Result<EntryIter<'_, C>, io::Error> {
8584
self.db_file
8685
.seek(io::SeekFrom::Start(self.magic.len() as _))?;
8786

@@ -100,8 +99,8 @@ where
10099
///
101100
/// **WARNING**: This method changes the write position of the underlying file. The next
102101
/// changeset will be written over the erroring entry (or the end of the file if none existed).
103-
pub fn aggregate_changesets(&mut self) -> (T::ChangeSet, Result<(), IterError>) {
104-
let mut changeset = T::ChangeSet::default();
102+
pub fn aggregate_changesets(&mut self) -> (C, Result<(), IterError>) {
103+
let mut changeset = C::default();
105104
let result = (|| {
106105
let iter_changeset = self.iter_changesets()?;
107106
for next_changeset in iter_changeset {
@@ -121,7 +120,7 @@ where
121120
///
122121
/// **WARNING**: This method does not detect whether the changeset is empty or not, and will
123122
/// append an empty changeset to the file (not catastrophic, just a waste of space).
124-
pub fn append_changeset(&mut self, changeset: &T::ChangeSet) -> Result<(), io::Error> {
123+
pub fn append_changeset(&mut self, changeset: &C) -> Result<(), io::Error> {
125124
bincode_options()
126125
.serialize_into(&mut self.db_file, changeset)
127126
.map_err(|e| match *e {
@@ -139,25 +138,6 @@ where
139138
}
140139
}
141140

142-
impl<T> PersistBackend<T> for Store<T>
143-
where
144-
T: Loadable,
145-
T::ChangeSet: serde::de::DeserializeOwned + serde::Serialize,
146-
{
147-
type WriteError = std::io::Error;
148-
type LoadError = IterError;
149-
150-
fn write_changes(&mut self, changeset: &T::ChangeSet) -> Result<(), Self::WriteError> {
151-
Store::append_changeset(self, changeset)
152-
}
153-
154-
fn load_into_tracker(&mut self, tracker: &mut T) -> Result<(), Self::LoadError> {
155-
let (changeset, result) = self.aggregate_changesets();
156-
tracker.load_changeset(changeset);
157-
result
158-
}
159-
}
160-
161141
#[cfg(test)]
162142
mod test {
163143
use super::*;
@@ -213,19 +193,13 @@ mod test {
213193
#[derive(Debug)]
214194
struct TestTracker;
215195

216-
impl Loadable for TestTracker {
217-
type ChangeSet = TestChangeSet;
218-
219-
fn load_changeset(&mut self, _changeset: Self::ChangeSet) {}
220-
}
221-
222196
#[test]
223197
fn new_fails_if_file_is_too_short() {
224198
let mut file = NamedTempFile::new().unwrap();
225199
file.write_all(&TEST_MAGIC_BYTES[..TEST_MAGIC_BYTES_LEN - 1])
226200
.expect("should write");
227201

228-
match Store::<TestTracker>::new(&TEST_MAGIC_BYTES, file.reopen().unwrap()) {
202+
match Store::<TestTracker, TestChangeSet>::new(&TEST_MAGIC_BYTES, file.reopen().unwrap()) {
229203
Err(FileError::Io(e)) => assert_eq!(e.kind(), std::io::ErrorKind::UnexpectedEof),
230204
unexpected => panic!("unexpected result: {:?}", unexpected),
231205
};
@@ -239,7 +213,7 @@ mod test {
239213
file.write_all(invalid_magic_bytes.as_bytes())
240214
.expect("should write");
241215

242-
match Store::<TestTracker>::new(&TEST_MAGIC_BYTES, file.reopen().unwrap()) {
216+
match Store::<TestTracker, TestChangeSet>::new(&TEST_MAGIC_BYTES, file.reopen().unwrap()) {
243217
Err(FileError::InvalidMagicBytes { got, .. }) => {
244218
assert_eq!(got, invalid_magic_bytes.as_bytes())
245219
}
@@ -260,8 +234,9 @@ mod test {
260234
let mut file = NamedTempFile::new().unwrap();
261235
file.write_all(&data).expect("should write");
262236

263-
let mut store = Store::<TestTracker>::new(&TEST_MAGIC_BYTES, file.reopen().unwrap())
264-
.expect("should open");
237+
let mut store =
238+
Store::<TestTracker, TestChangeSet>::new(&TEST_MAGIC_BYTES, file.reopen().unwrap())
239+
.expect("should open");
265240
match store.iter_changesets().expect("seek should succeed").next() {
266241
Some(Err(IterError::Bincode(_))) => {}
267242
unexpected_res => panic!("unexpected result: {:?}", unexpected_res),

example-crates/tracker_electrum/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub struct ScanOptions {
5858
}
5959

6060
fn main() -> anyhow::Result<()> {
61-
let (args, keymap, tracker, db) = cli::init::<ElectrumCommands, ConfirmationHeightAnchor, _>(
61+
let (args, keymap, tracker, db) = cli::init::<ElectrumCommands, ConfirmationHeightAnchor, _, _>(
6262
DB_MAGIC,
6363
DB_PATH,
6464
cli::Tracker::new_local(),

0 commit comments

Comments
 (0)