PMMRable cleanup (#1910)

* cleanup pmmrable and len()
introduce FixedLength trait with a const LEN
make Hash impl FixedLength for consistency

* rustfmt

* store tests cleanup

* rustfmt

* whats going on with those comments and rustfmt?
This commit is contained in:
Antioch Peverell
2018-11-01 20:14:46 +00:00
committed by GitHub
parent 9cebbf24b8
commit d23dec73d0
14 changed files with 81 additions and 112 deletions
+15 -24
View File
@@ -20,7 +20,7 @@ use croaring::Bitmap;
use core::core::hash::{Hash, Hashed};
use core::core::pmmr::{self, family, Backend, HashOnlyBackend};
use core::core::BlockHeader;
use core::ser::{self, PMMRable};
use core::ser::{self, FixedLength, PMMRable};
use leaf_set::LeafSet;
use prune_list::PruneList;
use types::{prune_noop, AppendOnlyFile, HashFile};
@@ -49,10 +49,7 @@ pub const PMMR_FILES: [&str; 4] = [
/// * 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.
pub struct PMMRBackend<T>
where
T: PMMRable,
{
pub struct PMMRBackend<T: PMMRable> {
data_dir: String,
prunable: bool,
hash_file: AppendOnlyFile,
@@ -62,16 +59,13 @@ where
_marker: marker::PhantomData<T>,
}
impl<T> Backend<T> for PMMRBackend<T>
where
T: PMMRable + ::std::fmt::Debug,
{
impl<T: PMMRable> Backend<T> for PMMRBackend<T> {
/// 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.
#[allow(unused_variables)]
fn append(&mut self, data: T, hashes: Vec<Hash>) -> Result<(), String> {
if self.prunable {
let record_len = Hash::SIZE as u64;
let record_len = Hash::LEN as u64;
let shift = self.prune_list.get_total_shift();
let position = (self.hash_file.size_unsync() / record_len) + shift + 1;
self.leaf_set.add(position);
@@ -95,7 +89,7 @@ where
let pos = position - 1;
// Must be on disk, doing a read at the correct position
let hash_record_len = Hash::SIZE;
let hash_record_len = Hash::LEN;
let file_offset = ((pos - shift) as usize) * hash_record_len;
let data = self.hash_file.read(file_offset, hash_record_len);
match ser::deserialize(&mut &data[..]) {
@@ -118,7 +112,7 @@ where
let pos = pmmr::n_leaves(position) - 1;
// Must be on disk, doing a read at the correct position
let record_len = T::len();
let record_len = T::LEN;
let file_offset = ((pos - shift) as usize) * record_len;
let data = self.data_file.read(file_offset, record_len);
match ser::deserialize(&mut &data[..]) {
@@ -164,14 +158,14 @@ where
// Rewind the hash file accounting for pruned/compacted pos
let shift = self.prune_list.get_shift(position);
let record_len = Hash::SIZE as u64;
let record_len = Hash::LEN as u64;
let file_pos = (position - shift) * record_len;
self.hash_file.rewind(file_pos);
// Rewind the data file accounting for pruned/compacted pos
let leaf_shift = self.prune_list.get_leaf_shift(position);
let flatfile_pos = pmmr::n_leaves(position);
let record_len = T::len() as u64;
let record_len = T::LEN as u64;
let file_pos = (flatfile_pos - leaf_shift) * record_len;
self.data_file.rewind(file_pos);
@@ -209,10 +203,7 @@ where
}
}
impl<T> PMMRBackend<T>
where
T: PMMRable + ::std::fmt::Debug,
{
impl<T: PMMRable> PMMRBackend<T> {
/// Instantiates a new PMMR backend.
/// Use the provided dir to store its files.
pub fn new(
@@ -263,7 +254,7 @@ where
pub fn unpruned_size(&self) -> io::Result<u64> {
let total_shift = self.prune_list.get_total_shift();
let record_len = Hash::SIZE as u64;
let record_len = Hash::LEN as u64;
let sz = self.hash_file.size()?;
Ok(sz / record_len + total_shift)
}
@@ -271,14 +262,14 @@ where
/// Number of elements in the underlying stored data. Extremely dependent on
/// pruning and compaction.
pub fn data_size(&self) -> io::Result<u64> {
let record_len = T::len() as u64;
let record_len = T::LEN as u64;
self.data_file.size().map(|sz| sz / record_len)
}
/// Size of the underlying hashed data. Extremely dependent on pruning
/// and compaction.
pub fn hash_size(&self) -> io::Result<u64> {
self.hash_file.size().map(|sz| sz / Hash::SIZE as u64)
self.hash_file.size().map(|sz| sz / Hash::LEN as u64)
}
/// Syncs all files to disk. A call to sync is required to ensure all the
@@ -348,7 +339,7 @@ where
// 1. Save compact copy of the hash file, skipping removed data.
{
let record_len = Hash::SIZE as u64;
let record_len = Hash::LEN as u64;
let off_to_rm = map_vec!(pos_to_rm, |pos| {
let shift = self.prune_list.get_shift(pos.into());
@@ -365,7 +356,7 @@ where
// 2. Save compact copy of the data file, skipping removed leaves.
{
let record_len = T::len() as u64;
let record_len = T::LEN as u64;
let leaf_pos_to_rm = pos_to_rm
.iter()
@@ -488,7 +479,7 @@ impl HashOnlyMMRBackend {
/// The unpruned size of this MMR backend.
pub fn unpruned_size(&self) -> io::Result<u64> {
let sz = self.hash_file.size()?;
Ok(sz / Hash::SIZE as u64)
Ok(sz / Hash::LEN as u64)
}
/// Discard any pending changes to this MMR backend.
+4 -4
View File
@@ -26,7 +26,7 @@ use libc::{ftruncate as ftruncate64, off_t as off64_t};
use libc::{ftruncate64, off64_t};
use core::core::hash::Hash;
use core::ser;
use core::ser::{self, FixedLength};
/// A no-op function for doing nothing with some pruned data.
pub fn prune_noop(_pruned_data: &[u8]) {}
@@ -58,8 +58,8 @@ impl HashFile {
let pos = position - 1;
// Must be on disk, doing a read at the correct position
let file_offset = (pos as usize) * Hash::SIZE;
let data = self.file.read(file_offset, Hash::SIZE);
let file_offset = (pos as usize) * Hash::LEN;
let data = self.file.read(file_offset, Hash::LEN);
match ser::deserialize(&mut &data[..]) {
Ok(h) => Some(h),
Err(e) => {
@@ -74,7 +74,7 @@ impl HashFile {
/// Rewind the backend file to the specified position.
pub fn rewind(&mut self, position: u64) -> io::Result<()> {
self.file.rewind(position * Hash::SIZE as u64);
self.file.rewind(position * Hash::LEN as u64);
Ok(())
}