Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor file store #1684

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ The project is split up into several crates in the `/crates` directory:
- [`wallet`](./crates/wallet): Contains the central high level `Wallet` type that is built from the low-level mechanisms provided by the other components
- [`chain`](./crates/chain): Tools for storing and indexing chain data
- [`persist`](./crates/persist): Types that define data persistence of a BDK wallet
- [`file_store`](./crates/file_store): A (experimental) persistence backend for storing chain data in a single file.
- [`file_store`](./crates/file_store): Persistence backend for storing chain data in a single file. Intended for testing and development purposes, not for production.
- [`esplora`](./crates/esplora): Extends the [`esplora-client`] crate with methods to fetch chain data from an esplora HTTP server in the form that [`bdk_chain`] and `Wallet` can consume.
- [`electrum`](./crates/electrum): Extends the [`electrum-client`] crate with methods to fetch chain data from an electrum server in the form that [`bdk_chain`] and `Wallet` can consume.

Expand Down
31 changes: 3 additions & 28 deletions crates/file_store/src/entry_iter.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::StoreError;
use bincode::Options;
use std::{
fs::File,
Expand Down Expand Up @@ -37,7 +38,7 @@ impl<'t, T> Iterator for EntryIter<'t, T>
where
T: serde::de::DeserializeOwned,
{
type Item = Result<T, IterError>;
type Item = Result<T, StoreError>;

fn next(&mut self) -> Option<Self::Item> {
if self.finished {
Expand All @@ -63,7 +64,7 @@ where
}
}
self.db_file.seek(io::SeekFrom::Start(pos_before_read))?;
Err(IterError::Bincode(*e))
Err(StoreError::Bincode(*e))
}
}
})()
Expand All @@ -80,29 +81,3 @@ impl<'t, T> Drop for EntryIter<'t, T> {
}
}
}

/// Error type for [`EntryIter`].
#[derive(Debug)]
pub enum IterError {
/// Failure to read from the file.
Io(io::Error),
/// Failure to decode data from the file.
Bincode(bincode::ErrorKind),
}

impl core::fmt::Display for IterError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
IterError::Io(e) => write!(f, "io error trying to read entry {}", e),
IterError::Bincode(e) => write!(f, "bincode error while reading entry {}", e),
}
}
}

impl From<io::Error> for IterError {
fn from(value: io::Error) -> Self {
IterError::Io(value)
}
}

impl std::error::Error for IterError {}
11 changes: 7 additions & 4 deletions crates/file_store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ pub(crate) fn bincode_options() -> impl bincode::Options {

/// Error that occurs due to problems encountered with the file.
#[derive(Debug)]
pub enum FileError {
pub enum StoreError {
/// IO error, this may mean that the file is too short.
Io(io::Error),
/// Magic bytes do not match what is expected.
InvalidMagicBytes { got: Vec<u8>, expected: Vec<u8> },
/// Failure to decode data from the file.
Bincode(bincode::ErrorKind),
}

impl core::fmt::Display for FileError {
impl core::fmt::Display for StoreError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Io(e) => write!(f, "io error trying to read file: {}", e),
Expand All @@ -29,14 +31,15 @@ impl core::fmt::Display for FileError {
"file has invalid magic bytes: expected={:?} got={:?}",
expected, got,
),
Self::Bincode(e) => write!(f, "bincode error while reading entry {}", e),
}
}
}

impl From<io::Error> for FileError {
impl From<io::Error> for StoreError {
fn from(value: io::Error) -> Self {
Self::Io(value)
}
}

impl std::error::Error for FileError {}
impl std::error::Error for StoreError {}
Loading
Loading