pub struct FsBlockDb { /* private fields */ }
unstable
only.Expand description
A block source that reads block data from disk and block metadata from a SQLite database.
This block source expects each compact block to be stored on disk in the blocks
subdirectory
of the blockstore_root
path provided at the time of construction. Each block should be
written, as the serialized bytes of its protobuf representation, where the path for each block
has the pattern:
<blockstore_root>/blocks/<block_height>-<block_hash>-compactblock
where <block_height>
is the decimal value of the height at which the block was mined, and
<block_hash>
is the hexadecimal representation of the block hash, as produced by the
fmt::Display
implementation for zcash_primitives::block::BlockHash
.
This block source is intended to be used with the following data flow:
- When the cache is being filled:
- The caller requests the current maximum height at which cached data is available
using
FsBlockDb::get_max_cached_height
. If no cached data is available, the caller can use the wallet’s synced-to height for the following operations instead. - (recommended for privacy) the caller should round the returned height down to some 100- / 1000-block boundary.
- The caller uses the lightwalletd’s
getblock
gRPC method to obtain a stream of blocks. For each block returned, the caller writes the compact block toblocks_dir
using the path format specified above. It is fine to overwrite an existing block, since block hashes are immutable and collision-resistant. - Once a caller-determined number of blocks have been successfully written to disk, the
caller should invoke
FsBlockDb::write_block_metadata
with the metadata for each block written to disk.
- The caller requests the current maximum height at which cached data is available
using
- The cache can then be scanned using the
BlockSource
implementation, providing the wallet’s synced-to-height as a starting point. - When part of the cache is no longer needed:
- The caller determines some height
H
that is the earliest block data it needs to preserve. This might be determined based on where the wallet is fully-synced to, or other heuristics. - The caller searches the defined filesystem folder for all files beginning in
HEIGHT-*
whereHEIGHT < H
, and deletes those files.
- The caller determines some height
Note: This API is unstable, and may change in the future. In particular, the BlockSource
API and the above description currently assume that scanning is performed in linear block
order; this assumption is likely to be weakened and/or removed in a future update.
Implementations§
Source§impl FsBlockDb
impl FsBlockDb
Sourcepub fn for_path<P: AsRef<Path>>(
fsblockdb_root: P,
) -> Result<Self, FsBlockDbError>
pub fn for_path<P: AsRef<Path>>( fsblockdb_root: P, ) -> Result<Self, FsBlockDbError>
Creates a filesystem-backed block store at the given path.
This will construct or open a SQLite database at the path
<fsblockdb_root>/blockmeta.sqlite
and will ensure that a directory exists at
<fsblockdb_root>/blocks
where this block store will expect to find serialized block
files as described for FsBlockDb
.
An application using this constructor should ensure that they call
crate::chain::init::init_blockmeta_db
at application startup to ensure
that the resulting metadata database is properly initialized and has had all required
migrations applied before use.
Sourcepub fn get_max_cached_height(
&self,
) -> Result<Option<BlockHeight>, FsBlockDbError>
pub fn get_max_cached_height( &self, ) -> Result<Option<BlockHeight>, FsBlockDbError>
Returns the maximum height of blocks known to the block metadata database.
Sourcepub fn write_block_metadata(
&self,
block_meta: &[BlockMeta],
) -> Result<(), FsBlockDbError>
pub fn write_block_metadata( &self, block_meta: &[BlockMeta], ) -> Result<(), FsBlockDbError>
Adds a set of block metadata entries to the metadata database, overwriting any existing entries at the given block heights.
This will return an error if any block file corresponding to one of these metadata records is absent from the blocks directory.
Sourcepub fn find_block(
&self,
height: BlockHeight,
) -> Result<Option<BlockMeta>, FsBlockDbError>
pub fn find_block( &self, height: BlockHeight, ) -> Result<Option<BlockMeta>, FsBlockDbError>
Returns the metadata for the block with the given height, if it exists in the database.
Sourcepub fn truncate_to_height(
&self,
block_height: BlockHeight,
) -> Result<(), FsBlockDbError>
pub fn truncate_to_height( &self, block_height: BlockHeight, ) -> Result<(), FsBlockDbError>
Rewinds the BlockMeta Db to the block_height
provided.
This doesn’t delete any files referenced by the records stored in BlockMeta.
If the requested height is greater than or equal to the height of the last scanned block, or if the DB is empty, this function does nothing.
Trait Implementations§
Source§impl BlockSource for FsBlockDb
impl BlockSource for FsBlockDb
type Error = FsBlockDbError
Source§fn with_blocks<F, DbErrT>(
&self,
from_height: Option<BlockHeight>,
limit: Option<usize>,
with_row: F,
) -> Result<(), Error<DbErrT, Self::Error>>
fn with_blocks<F, DbErrT>( &self, from_height: Option<BlockHeight>, limit: Option<usize>, with_row: F, ) -> Result<(), Error<DbErrT, Self::Error>>
limit
number of blocks from the blockchain, starting at
from_height
, applying the provided callback to each block. If from_height
is None
then scanning will begin at the first available block. Read moreAuto Trait Implementations§
impl !Freeze for FsBlockDb
impl !RefUnwindSafe for FsBlockDb
impl Send for FsBlockDb
impl !Sync for FsBlockDb
impl Unpin for FsBlockDb
impl !UnwindSafe for FsBlockDb
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request
§impl<L> LayerExt<L> for L
impl<L> LayerExt<L> for L
§fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
Layered
].§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.