Struct grin_store::pmmr::PMMRBackend

source ·
pub struct PMMRBackend<T: PMMRable> { /* private fields */ }
Expand description

PMMR persistent backend implementation. Relies on multiple facilities to handle writing, reading and pruning.

  • A main storage file appends Hash instances as they come. This AppendOnlyFile is also backed by a mmap for reads.
  • An in-memory backend buffers the latest batch of writes to ensure the PMMR can always read recent values even if they haven’t been flushed to disk yet.
  • A leaf_set tracks unpruned (unremoved) leaf positions in the MMR..
  • A prune_list tracks the positions of pruned (and compacted) roots in the MMR.

Implementations§

source§

impl<T: PMMRable> PMMRBackend<T>

source

pub fn new<P: AsRef<Path>>( data_dir: P, prunable: bool, version: ProtocolVersion, header: Option<&BlockHeader> ) -> Result<PMMRBackend<T>>

Instantiates a new PMMR backend. If optional size is provided then treat as “fixed” size otherwise “variable” size backend. Use the provided dir to store its files.

source

pub fn unpruned_size(&self) -> u64

Number of hashes in the PMMR stored by this backend. Only produces the fully sync’d size.

source

pub fn data_size(&self) -> u64

Number of elements in the underlying stored data. Extremely dependent on pruning and compaction.

source

pub fn hash_size(&self) -> u64

Size of the underlying hashed data. Extremely dependent on pruning and compaction.

source

pub fn sync(&mut self) -> Result<()>

Syncs all files to disk. A call to sync is required to ensure all the data has been successfully written to disk.

source

pub fn discard(&mut self)

Discard the current, non synced state of the backend.

source

pub fn check_compact( &mut self, cutoff_pos: u64, rewind_rm_pos: &Bitmap ) -> Result<bool>

Takes the leaf_set at a given cutoff_pos and generates an updated prune_list. Saves the updated prune_list to disk, compacts the hash and data files based on the prune_list and saves both to disk.

A cutoff position limits compaction on recent data. This will be the last position of a particular block to keep things aligned. The block_marker in the db/index for the particular block will have a suitable output_pos. This is used to enforce a horizon after which the local node should have all the data to allow rewinding.

Trait Implementations§

source§

impl<T: PMMRable> Backend<T> for PMMRBackend<T>

source§

fn append(&mut self, data: &T, hashes: &[Hash]) -> Result<(), String>

Append the provided data and hashes to the backend storage. Add the new leaf pos to our leaf_set if this is a prunable MMR.

source§

fn get_hash(&self, pos0: u64) -> Option<Hash>

Get the hash at pos. Return None if pos is a leaf and it has been removed (or pruned or compacted).

source§

fn get_data(&self, pos0: u64) -> Option<T::E>

Get the data at pos. Return None if it has been removed or if pos is not a leaf node.

source§

fn remove_from_leaf_set(&mut self, pos0: u64)

Remove leaf from leaf set

source§

fn leaf_pos_iter(&self) -> Box<dyn Iterator<Item = u64> + '_>

Returns an iterator over all the leaf positions. for a prunable PMMR this is an iterator over the leaf_set bitmap. For a non-prunable PMMR this is all leaves (this is not yet implemented).

source§

fn leaf_idx_iter(&self, from_idx: u64) -> Box<dyn Iterator<Item = u64> + '_>

Returns an iterator over all the leaf insertion indices (0-indexed). If our pos are [1,2,4,5,8] (first 5 leaf pos) then our insertion indices are [0,1,2,3,4]

source§

fn rewind( &mut self, position: u64, rewind_rm_pos: &Bitmap ) -> Result<(), String>

Rewind the PMMR backend to the given position.

source§

fn remove(&mut self, pos0: u64) -> Result<(), String>

Remove by insertion position.

source§

fn release_files(&mut self)

Release underlying data files

source§

fn append_pruned_subtree(&mut self, hash: Hash, pos0: u64) -> Result<(), String>

Rebuilding a PMMR locally from PIBD segments requires pruned subtree support. This allows us to append an existing pruned subtree directly without the underlying leaf nodes.
source§

fn append_hash(&mut self, hash: Hash) -> Result<(), String>

Append a single hash to the pmmr
source§

fn get_from_file(&self, pos0: u64) -> Option<Hash>

Get a Hash by original insertion position (ignoring the remove log).
source§

fn get_peak_from_file(&self, pos0: u64) -> Option<Hash>

Get hash for peak pos. Optimized for reading peak hashes rather than arbitrary pos hashes. Peaks can be assumed to not be compacted.
source§

fn get_data_from_file(&self, pos0: u64) -> Option<T::E>

Get a Data Element by original insertion position (ignoring the remove log).
source§

fn n_unpruned_leaves(&self) -> u64

Number of leaves
source§

fn n_unpruned_leaves_to_index(&self, to_index: u64) -> u64

Number of leaves up to the given leaf index
source§

fn reset_prune_list(&mut self)

Reset prune list, used when PIBD is reset
source§

fn snapshot(&self, header: &BlockHeader) -> Result<(), String>

Saves a snapshot of the rewound utxo file with the block hash as filename suffix. We need this when sending a txhashset zip file to a node for fast sync.
source§

fn dump_stats(&self)

For debugging purposes so we can see how compaction is doing.

Auto Trait Implementations§

§

impl<T> Freeze for PMMRBackend<T>

§

impl<T> RefUnwindSafe for PMMRBackend<T>
where <T as PMMRable>::E: RefUnwindSafe,

§

impl<T> Send for PMMRBackend<T>
where <T as PMMRable>::E: Send,

§

impl<T> Sync for PMMRBackend<T>
where <T as PMMRable>::E: Sync,

§

impl<T> Unpin for PMMRBackend<T>
where <T as PMMRable>::E: Unpin,

§

impl<T> UnwindSafe for PMMRBackend<T>
where <T as PMMRable>::E: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> SafeBorrow<T> for T
where T: ?Sized,

source§

fn borrow_replacement(ptr: &T) -> &T

Given ptr, which was obtained from a prior call to Self::borrow(), return a value with the same nominal lifetime which is guaranteed to survive mutations to Self. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> UnsafeAny for T
where T: Any,