2018-03-05 22:33:44 +03:00
|
|
|
// Copyright 2018 The Grin Developers
|
2018-02-22 16:45:13 +03:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
//! Implementation of the persistent Backend for the prunable MMR tree.
|
|
|
|
|
2018-07-06 20:40:43 +03:00
|
|
|
use std::{fs, io, marker};
|
2018-02-22 16:45:13 +03:00
|
|
|
|
2018-06-18 18:18:38 +03:00
|
|
|
use croaring::Bitmap;
|
|
|
|
|
|
|
|
use core::core::hash::{Hash, Hashed};
|
2018-10-15 19:16:34 +03:00
|
|
|
use core::core::pmmr::{self, family, Backend, HashOnlyBackend};
|
2018-06-29 03:53:00 +03:00
|
|
|
use core::core::BlockHeader;
|
2018-06-14 15:16:14 +03:00
|
|
|
use core::ser::{self, PMMRable};
|
2018-06-18 18:18:38 +03:00
|
|
|
use leaf_set::LeafSet;
|
2018-06-29 03:53:00 +03:00
|
|
|
use prune_list::PruneList;
|
2018-10-15 19:16:34 +03:00
|
|
|
use types::{prune_noop, AppendOnlyFile, HashFile};
|
2018-02-22 16:45:13 +03:00
|
|
|
|
|
|
|
const PMMR_HASH_FILE: &'static str = "pmmr_hash.bin";
|
|
|
|
const PMMR_DATA_FILE: &'static str = "pmmr_data.bin";
|
2018-06-18 18:18:38 +03:00
|
|
|
const PMMR_LEAF_FILE: &'static str = "pmmr_leaf.bin";
|
2018-06-29 03:53:00 +03:00
|
|
|
const PMMR_PRUN_FILE: &'static str = "pmmr_prun.bin";
|
|
|
|
|
2018-08-03 05:16:16 +03:00
|
|
|
/// The list of PMMR_Files for internal purposes
|
|
|
|
pub const PMMR_FILES: [&str; 4] = [
|
|
|
|
PMMR_HASH_FILE,
|
|
|
|
PMMR_DATA_FILE,
|
|
|
|
PMMR_LEAF_FILE,
|
|
|
|
PMMR_PRUN_FILE,
|
|
|
|
];
|
|
|
|
|
2018-02-22 16:45:13 +03:00
|
|
|
/// PMMR persistent backend implementation. Relies on multiple facilities to
|
|
|
|
/// handle writing, reading and pruning.
|
|
|
|
///
|
2018-06-18 18:18:38 +03:00
|
|
|
/// * A main storage file appends Hash instances as they come.
|
|
|
|
/// This AppendOnlyFile is also backed by a mmap for reads.
|
2018-02-22 16:45:13 +03:00
|
|
|
/// * 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.
|
2018-06-18 18:18:38 +03:00
|
|
|
/// * 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.
|
2018-02-22 16:45:13 +03:00
|
|
|
pub struct PMMRBackend<T>
|
|
|
|
where
|
|
|
|
T: PMMRable,
|
|
|
|
{
|
|
|
|
data_dir: String,
|
2018-07-05 05:31:08 +03:00
|
|
|
prunable: bool,
|
2018-02-22 16:45:13 +03:00
|
|
|
hash_file: AppendOnlyFile,
|
|
|
|
data_file: AppendOnlyFile,
|
2018-06-18 18:18:38 +03:00
|
|
|
leaf_set: LeafSet,
|
2018-06-29 03:53:00 +03:00
|
|
|
prune_list: PruneList,
|
2018-04-22 00:03:45 +03:00
|
|
|
_marker: marker::PhantomData<T>,
|
2018-02-22 16:45:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Backend<T> for PMMRBackend<T>
|
|
|
|
where
|
2018-03-15 19:53:40 +03:00
|
|
|
T: PMMRable + ::std::fmt::Debug,
|
2018-02-22 16:45:13 +03:00
|
|
|
{
|
2018-10-15 19:16:34 +03:00
|
|
|
/// 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.
|
2018-02-22 16:45:13 +03:00
|
|
|
#[allow(unused_variables)]
|
2018-10-15 19:16:34 +03:00
|
|
|
fn append(&mut self, data: T, hashes: Vec<Hash>) -> Result<(), String> {
|
|
|
|
if self.prunable {
|
|
|
|
let record_len = Hash::SIZE 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);
|
|
|
|
}
|
|
|
|
self.data_file.append(&mut ser::ser_vec(&data).unwrap());
|
|
|
|
for ref h in hashes {
|
|
|
|
self.hash_file.append(&mut ser::ser_vec(h).unwrap());
|
2018-02-22 16:45:13 +03:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-03-02 23:47:27 +03:00
|
|
|
fn get_from_file(&self, position: u64) -> Option<Hash> {
|
2018-06-29 03:53:00 +03:00
|
|
|
if self.is_compacted(position) {
|
2018-02-22 16:45:13 +03:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2018-06-29 03:53:00 +03:00
|
|
|
let shift = self.prune_list.get_shift(position);
|
|
|
|
|
2018-02-22 16:45:13 +03:00
|
|
|
// Read PMMR
|
|
|
|
// The MMR starts at 1, our binary backend starts at 0
|
|
|
|
let pos = position - 1;
|
|
|
|
|
|
|
|
// Must be on disk, doing a read at the correct position
|
2018-10-15 19:16:34 +03:00
|
|
|
let hash_record_len = Hash::SIZE;
|
2018-06-29 03:53:00 +03:00
|
|
|
let file_offset = ((pos - shift) as usize) * hash_record_len;
|
2018-02-22 16:45:13 +03:00
|
|
|
let data = self.hash_file.read(file_offset, hash_record_len);
|
2018-03-02 23:47:27 +03:00
|
|
|
match ser::deserialize(&mut &data[..]) {
|
|
|
|
Ok(h) => Some(h),
|
2018-02-22 16:45:13 +03:00
|
|
|
Err(e) => {
|
|
|
|
error!(
|
2018-10-21 23:30:56 +03:00
|
|
|
"Corrupted storage, could not read an entry from hash store: {:?}",
|
|
|
|
e
|
2018-02-22 16:45:13 +03:00
|
|
|
);
|
|
|
|
return None;
|
|
|
|
}
|
2018-03-02 23:47:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-15 20:12:30 +03:00
|
|
|
fn get_data_from_file(&self, position: u64) -> Option<T> {
|
2018-06-29 03:53:00 +03:00
|
|
|
if self.is_compacted(position) {
|
2018-03-15 20:12:30 +03:00
|
|
|
return None;
|
|
|
|
}
|
2018-06-29 03:53:00 +03:00
|
|
|
let shift = self.prune_list.get_leaf_shift(position);
|
2018-03-15 20:12:30 +03:00
|
|
|
let pos = pmmr::n_leaves(position) - 1;
|
|
|
|
|
|
|
|
// Must be on disk, doing a read at the correct position
|
|
|
|
let record_len = T::len();
|
2018-06-29 03:53:00 +03:00
|
|
|
let file_offset = ((pos - shift) as usize) * record_len;
|
2018-03-15 20:12:30 +03:00
|
|
|
let data = self.data_file.read(file_offset, record_len);
|
|
|
|
match ser::deserialize(&mut &data[..]) {
|
|
|
|
Ok(h) => Some(h),
|
|
|
|
Err(e) => {
|
|
|
|
error!(
|
2018-10-21 23:30:56 +03:00
|
|
|
"Corrupted storage, could not read an entry from data store: {:?}",
|
|
|
|
e
|
2018-03-15 20:12:30 +03:00
|
|
|
);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-24 02:33:59 +03:00
|
|
|
/// Get the hash at pos.
|
2018-06-18 18:18:38 +03:00
|
|
|
/// Return None if pos is a leaf and it has been removed (or pruned or
|
|
|
|
/// compacted).
|
2018-03-24 02:33:59 +03:00
|
|
|
fn get_hash(&self, pos: u64) -> Option<(Hash)> {
|
2018-07-05 05:31:08 +03:00
|
|
|
if self.prunable && pmmr::is_leaf(pos) && !self.leaf_set.includes(pos) {
|
2018-06-18 18:18:38 +03:00
|
|
|
return None;
|
2018-02-22 16:45:13 +03:00
|
|
|
}
|
2018-06-18 18:18:38 +03:00
|
|
|
self.get_from_file(pos)
|
2018-03-24 02:33:59 +03:00
|
|
|
}
|
2018-02-22 16:45:13 +03:00
|
|
|
|
2018-03-24 02:33:59 +03:00
|
|
|
/// Get the data at pos.
|
|
|
|
/// Return None if it has been removed or if pos is not a leaf node.
|
|
|
|
fn get_data(&self, pos: u64) -> Option<(T)> {
|
2018-06-18 18:18:38 +03:00
|
|
|
if !pmmr::is_leaf(pos) {
|
|
|
|
return None;
|
2018-03-13 21:22:34 +03:00
|
|
|
}
|
2018-07-05 05:31:08 +03:00
|
|
|
if self.prunable && !self.leaf_set.includes(pos) {
|
2018-06-18 18:18:38 +03:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
self.get_data_from_file(pos)
|
2018-02-22 16:45:13 +03:00
|
|
|
}
|
|
|
|
|
2018-04-24 22:53:01 +03:00
|
|
|
/// Rewind the PMMR backend to the given position.
|
2018-09-12 10:19:05 +03:00
|
|
|
fn rewind(&mut self, position: u64, rewind_rm_pos: &Bitmap) -> Result<(), String> {
|
2018-06-18 18:18:38 +03:00
|
|
|
// First rewind the leaf_set with the necessary added and removed positions.
|
2018-07-05 05:31:08 +03:00
|
|
|
if self.prunable {
|
2018-07-08 19:37:09 +03:00
|
|
|
self.leaf_set.rewind(position, rewind_rm_pos);
|
2018-07-05 05:31:08 +03:00
|
|
|
}
|
2018-02-22 16:45:13 +03:00
|
|
|
|
2018-04-24 22:53:01 +03:00
|
|
|
// Rewind the hash file accounting for pruned/compacted pos
|
2018-06-29 03:53:00 +03:00
|
|
|
let shift = self.prune_list.get_shift(position);
|
2018-10-15 19:16:34 +03:00
|
|
|
let record_len = Hash::SIZE as u64;
|
2018-04-24 22:53:01 +03:00
|
|
|
let file_pos = (position - shift) * record_len;
|
2018-02-22 16:45:13 +03:00
|
|
|
self.hash_file.rewind(file_pos);
|
|
|
|
|
2018-04-24 22:53:01 +03:00
|
|
|
// Rewind the data file accounting for pruned/compacted pos
|
2018-06-29 03:53:00 +03:00
|
|
|
let leaf_shift = self.prune_list.get_leaf_shift(position);
|
2018-04-19 21:52:46 +03:00
|
|
|
let flatfile_pos = pmmr::n_leaves(position);
|
2018-04-24 22:53:01 +03:00
|
|
|
let record_len = T::len() as u64;
|
|
|
|
let file_pos = (flatfile_pos - leaf_shift) * record_len;
|
|
|
|
self.data_file.rewind(file_pos);
|
|
|
|
|
2018-02-22 16:45:13 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-06-18 18:18:38 +03:00
|
|
|
/// Remove by insertion position.
|
|
|
|
fn remove(&mut self, pos: u64) -> Result<(), String> {
|
2018-07-05 05:31:08 +03:00
|
|
|
assert!(self.prunable, "Remove on non-prunable MMR");
|
2018-06-18 18:18:38 +03:00
|
|
|
self.leaf_set.remove(pos);
|
|
|
|
Ok(())
|
2018-02-22 16:45:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return data file path
|
|
|
|
fn get_data_file_path(&self) -> String {
|
|
|
|
self.data_file.path()
|
|
|
|
}
|
2018-03-15 19:53:40 +03:00
|
|
|
|
2018-06-18 18:18:38 +03:00
|
|
|
fn snapshot(&self, header: &BlockHeader) -> Result<(), String> {
|
|
|
|
self.leaf_set
|
|
|
|
.snapshot(header)
|
|
|
|
.map_err(|_| format!("Failed to save copy of leaf_set for {}", header.hash()))?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-03-15 19:53:40 +03:00
|
|
|
fn dump_stats(&self) {
|
|
|
|
debug!(
|
2018-06-18 18:18:38 +03:00
|
|
|
"pmmr backend: unpruned: {}, hashes: {}, data: {}, leaf_set: {}, prune_list: {}",
|
2018-03-15 19:53:40 +03:00
|
|
|
self.unpruned_size().unwrap_or(0),
|
|
|
|
self.hash_size().unwrap_or(0),
|
2018-03-20 04:31:57 +03:00
|
|
|
self.data_size().unwrap_or(0),
|
2018-06-18 18:18:38 +03:00
|
|
|
self.leaf_set.len(),
|
2018-06-29 03:53:00 +03:00
|
|
|
self.prune_list.len(),
|
2018-03-15 19:53:40 +03:00
|
|
|
);
|
|
|
|
}
|
2018-02-22 16:45:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> PMMRBackend<T>
|
|
|
|
where
|
2018-03-13 21:22:34 +03:00
|
|
|
T: PMMRable + ::std::fmt::Debug,
|
2018-02-22 16:45:13 +03:00
|
|
|
{
|
2018-04-24 22:53:01 +03:00
|
|
|
/// Instantiates a new PMMR backend.
|
|
|
|
/// Use the provided dir to store its files.
|
2018-07-05 05:31:08 +03:00
|
|
|
pub fn new(
|
|
|
|
data_dir: String,
|
|
|
|
prunable: bool,
|
2018-09-12 10:19:05 +03:00
|
|
|
header: Option<&BlockHeader>,
|
2018-07-05 05:31:08 +03:00
|
|
|
) -> io::Result<PMMRBackend<T>> {
|
2018-04-24 22:53:01 +03:00
|
|
|
let hash_file = AppendOnlyFile::open(format!("{}/{}", data_dir, PMMR_HASH_FILE))?;
|
|
|
|
let data_file = AppendOnlyFile::open(format!("{}/{}", data_dir, PMMR_DATA_FILE))?;
|
2018-02-22 16:45:13 +03:00
|
|
|
|
2018-06-18 18:18:38 +03:00
|
|
|
let leaf_set_path = format!("{}/{}", data_dir, PMMR_LEAF_FILE);
|
|
|
|
|
2018-07-05 05:31:08 +03:00
|
|
|
// If we received a rewound "snapshot" leaf_set file move it into
|
|
|
|
// place so we use it.
|
2018-06-18 18:18:38 +03:00
|
|
|
if let Some(header) = header {
|
|
|
|
let leaf_snapshot_path = format!("{}/{}.{}", data_dir, PMMR_LEAF_FILE, header.hash());
|
|
|
|
LeafSet::copy_snapshot(leaf_set_path.clone(), leaf_snapshot_path.clone())?;
|
|
|
|
}
|
|
|
|
|
2018-07-05 05:31:08 +03:00
|
|
|
let leaf_set = LeafSet::open(leaf_set_path.clone())?;
|
2018-09-12 10:19:05 +03:00
|
|
|
let prune_list = PruneList::open(format!("{}/{}", data_dir, PMMR_PRUN_FILE))?;
|
2018-06-18 18:18:38 +03:00
|
|
|
|
2018-02-22 16:45:13 +03:00
|
|
|
Ok(PMMRBackend {
|
2018-04-24 22:53:01 +03:00
|
|
|
data_dir,
|
2018-07-05 05:31:08 +03:00
|
|
|
prunable,
|
2018-04-24 22:53:01 +03:00
|
|
|
hash_file,
|
|
|
|
data_file,
|
2018-06-18 18:18:38 +03:00
|
|
|
leaf_set,
|
2018-06-29 03:53:00 +03:00
|
|
|
prune_list,
|
2018-04-22 00:03:45 +03:00
|
|
|
_marker: marker::PhantomData,
|
2018-02-22 16:45:13 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-06-18 18:18:38 +03:00
|
|
|
fn is_pruned(&self, pos: u64) -> bool {
|
2018-06-29 03:53:00 +03:00
|
|
|
self.prune_list.is_pruned(pos)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_pruned_root(&self, pos: u64) -> bool {
|
|
|
|
self.prune_list.is_pruned_root(pos)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_compacted(&self, pos: u64) -> bool {
|
|
|
|
self.is_pruned(pos) && !self.is_pruned_root(pos)
|
2018-06-18 18:18:38 +03:00
|
|
|
}
|
|
|
|
|
2018-03-01 00:15:29 +03:00
|
|
|
/// Number of elements in the PMMR stored by this backend. Only produces the
|
|
|
|
/// fully sync'd size.
|
2018-02-22 16:45:13 +03:00
|
|
|
pub fn unpruned_size(&self) -> io::Result<u64> {
|
2018-06-29 03:53:00 +03:00
|
|
|
let total_shift = self.prune_list.get_total_shift();
|
2018-03-13 21:22:34 +03:00
|
|
|
|
2018-10-15 19:16:34 +03:00
|
|
|
let record_len = Hash::SIZE as u64;
|
2018-02-22 16:45:13 +03:00
|
|
|
let sz = self.hash_file.size()?;
|
|
|
|
Ok(sz / record_len + total_shift)
|
|
|
|
}
|
|
|
|
|
2018-03-01 00:15:29 +03:00
|
|
|
/// 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;
|
|
|
|
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> {
|
2018-10-15 19:16:34 +03:00
|
|
|
self.hash_file.size().map(|sz| sz / Hash::SIZE as u64)
|
2018-03-01 00:15:29 +03:00
|
|
|
}
|
|
|
|
|
2018-02-22 16:45:13 +03:00
|
|
|
/// Syncs all files to disk. A call to sync is required to ensure all the
|
|
|
|
/// data has been successfully written to disk.
|
|
|
|
pub fn sync(&mut self) -> io::Result<()> {
|
|
|
|
if let Err(e) = self.hash_file.flush() {
|
|
|
|
return Err(io::Error::new(
|
2018-03-04 03:19:54 +03:00
|
|
|
io::ErrorKind::Interrupted,
|
|
|
|
format!("Could not write to log hash storage, disk full? {:?}", e),
|
|
|
|
));
|
2018-02-22 16:45:13 +03:00
|
|
|
}
|
|
|
|
if let Err(e) = self.data_file.flush() {
|
|
|
|
return Err(io::Error::new(
|
2018-03-04 03:19:54 +03:00
|
|
|
io::ErrorKind::Interrupted,
|
|
|
|
format!("Could not write to log data storage, disk full? {:?}", e),
|
|
|
|
));
|
2018-02-22 16:45:13 +03:00
|
|
|
}
|
2018-09-12 10:19:05 +03:00
|
|
|
|
|
|
|
// Flush the leaf_set to disk.
|
2018-06-18 18:18:38 +03:00
|
|
|
self.leaf_set.flush()?;
|
2018-03-13 21:22:34 +03:00
|
|
|
|
2018-02-22 16:45:13 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Discard the current, non synced state of the backend.
|
|
|
|
pub fn discard(&mut self) {
|
|
|
|
self.hash_file.discard();
|
2018-06-18 18:18:38 +03:00
|
|
|
self.leaf_set.discard();
|
2018-02-22 16:45:13 +03:00
|
|
|
self.data_file.discard();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the data file path
|
|
|
|
pub fn data_file_path(&self) -> String {
|
|
|
|
self.get_data_file_path()
|
|
|
|
}
|
|
|
|
|
2018-06-18 18:18:38 +03:00
|
|
|
/// 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.
|
2018-02-22 16:45:13 +03:00
|
|
|
///
|
2018-06-18 18:18:38 +03:00
|
|
|
/// 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.
|
2018-03-06 20:58:33 +03:00
|
|
|
pub fn check_compact<P>(
|
|
|
|
&mut self,
|
2018-06-18 18:18:38 +03:00
|
|
|
cutoff_pos: u64,
|
|
|
|
rewind_rm_pos: &Bitmap,
|
2018-03-06 20:58:33 +03:00
|
|
|
prune_cb: P,
|
|
|
|
) -> io::Result<bool>
|
|
|
|
where
|
|
|
|
P: Fn(&[u8]),
|
|
|
|
{
|
2018-07-05 05:31:08 +03:00
|
|
|
assert!(self.prunable, "Trying to compact a non-prunable PMMR");
|
|
|
|
|
2018-03-13 21:22:34 +03:00
|
|
|
// Paths for tmp hash and data files.
|
|
|
|
let tmp_prune_file_hash = format!("{}/{}.hashprune", self.data_dir, PMMR_HASH_FILE);
|
2018-03-15 20:12:30 +03:00
|
|
|
let tmp_prune_file_data = format!("{}/{}.dataprune", self.data_dir, PMMR_DATA_FILE);
|
2018-03-13 21:22:34 +03:00
|
|
|
|
2018-06-18 18:18:38 +03:00
|
|
|
// Calculate the sets of leaf positions and node positions to remove based
|
|
|
|
// on the cutoff_pos provided.
|
2018-07-08 19:37:09 +03:00
|
|
|
let (leaves_removed, pos_to_rm) = self.pos_to_rm(cutoff_pos, rewind_rm_pos);
|
2018-03-13 21:22:34 +03:00
|
|
|
|
|
|
|
// 1. Save compact copy of the hash file, skipping removed data.
|
|
|
|
{
|
2018-10-15 19:16:34 +03:00
|
|
|
let record_len = Hash::SIZE as u64;
|
2018-03-13 21:22:34 +03:00
|
|
|
|
2018-06-18 18:18:38 +03:00
|
|
|
let off_to_rm = map_vec!(pos_to_rm, |pos| {
|
2018-06-29 03:53:00 +03:00
|
|
|
let shift = self.prune_list.get_shift(pos.into());
|
2018-06-18 18:18:38 +03:00
|
|
|
((pos as u64) - 1 - shift) * record_len
|
2018-04-02 23:49:35 +03:00
|
|
|
});
|
2018-03-13 21:22:34 +03:00
|
|
|
|
|
|
|
self.hash_file.save_prune(
|
|
|
|
tmp_prune_file_hash.clone(),
|
|
|
|
off_to_rm,
|
|
|
|
record_len,
|
|
|
|
&prune_noop,
|
|
|
|
)?;
|
2018-02-22 16:45:13 +03:00
|
|
|
}
|
|
|
|
|
2018-03-13 21:22:34 +03:00
|
|
|
// 2. Save compact copy of the data file, skipping removed leaves.
|
2018-03-15 20:12:30 +03:00
|
|
|
{
|
|
|
|
let record_len = T::len() as u64;
|
|
|
|
|
2018-06-18 18:18:38 +03:00
|
|
|
let leaf_pos_to_rm = pos_to_rm
|
|
|
|
.iter()
|
|
|
|
.filter(|&x| pmmr::is_leaf(x.into()))
|
|
|
|
.map(|x| x as u64)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let off_to_rm = map_vec!(leaf_pos_to_rm, |&pos| {
|
|
|
|
let flat_pos = pmmr::n_leaves(pos);
|
2018-06-29 03:53:00 +03:00
|
|
|
let shift = self.prune_list.get_leaf_shift(pos);
|
2018-06-07 00:46:21 +03:00
|
|
|
(flat_pos - 1 - shift) * record_len
|
2018-04-02 23:49:35 +03:00
|
|
|
});
|
2018-03-15 20:12:30 +03:00
|
|
|
|
|
|
|
self.data_file.save_prune(
|
|
|
|
tmp_prune_file_data.clone(),
|
|
|
|
off_to_rm,
|
|
|
|
record_len,
|
|
|
|
prune_cb,
|
|
|
|
)?;
|
|
|
|
}
|
2018-03-13 21:22:34 +03:00
|
|
|
|
2018-06-29 03:53:00 +03:00
|
|
|
// 3. Update the prune list and write to disk.
|
2018-03-13 21:22:34 +03:00
|
|
|
{
|
2018-06-18 18:18:38 +03:00
|
|
|
for pos in leaves_removed.iter() {
|
2018-06-29 03:53:00 +03:00
|
|
|
self.prune_list.add(pos.into());
|
2018-03-01 00:15:29 +03:00
|
|
|
}
|
2018-06-29 03:53:00 +03:00
|
|
|
self.prune_list.flush()?;
|
2018-02-22 16:45:13 +03:00
|
|
|
}
|
|
|
|
|
2018-03-13 21:22:34 +03:00
|
|
|
// 4. Rename the compact copy of hash file and reopen it.
|
2018-02-22 16:45:13 +03:00
|
|
|
fs::rename(
|
|
|
|
tmp_prune_file_hash.clone(),
|
|
|
|
format!("{}/{}", self.data_dir, PMMR_HASH_FILE),
|
|
|
|
)?;
|
2018-04-24 22:53:01 +03:00
|
|
|
self.hash_file = AppendOnlyFile::open(format!("{}/{}", self.data_dir, PMMR_HASH_FILE))?;
|
2018-02-22 16:45:13 +03:00
|
|
|
|
2018-03-13 21:22:34 +03:00
|
|
|
// 5. Rename the compact copy of the data file and reopen it.
|
2018-03-15 20:12:30 +03:00
|
|
|
fs::rename(
|
|
|
|
tmp_prune_file_data.clone(),
|
|
|
|
format!("{}/{}", self.data_dir, PMMR_DATA_FILE),
|
|
|
|
)?;
|
2018-04-24 22:53:01 +03:00
|
|
|
self.data_file = AppendOnlyFile::open(format!("{}/{}", self.data_dir, PMMR_DATA_FILE))?;
|
2018-02-22 16:45:13 +03:00
|
|
|
|
2018-06-18 18:18:38 +03:00
|
|
|
// 6. Write the leaf_set to disk.
|
|
|
|
// Optimize the bitmap storage in the process.
|
|
|
|
self.leaf_set.flush()?;
|
2018-02-22 16:45:13 +03:00
|
|
|
|
2018-03-06 20:58:33 +03:00
|
|
|
Ok(true)
|
2018-02-22 16:45:13 +03:00
|
|
|
}
|
2018-06-18 18:18:38 +03:00
|
|
|
|
2018-09-12 10:19:05 +03:00
|
|
|
fn pos_to_rm(&self, cutoff_pos: u64, rewind_rm_pos: &Bitmap) -> (Bitmap, Bitmap) {
|
2018-06-18 18:18:38 +03:00
|
|
|
let mut expanded = Bitmap::create();
|
|
|
|
|
2018-09-12 10:19:05 +03:00
|
|
|
let leaf_pos_to_rm =
|
|
|
|
self.leaf_set
|
|
|
|
.removed_pre_cutoff(cutoff_pos, rewind_rm_pos, &self.prune_list);
|
2018-06-18 18:18:38 +03:00
|
|
|
|
|
|
|
for x in leaf_pos_to_rm.iter() {
|
|
|
|
expanded.add(x);
|
|
|
|
let mut current = x as u64;
|
|
|
|
loop {
|
|
|
|
let (parent, sibling) = family(current);
|
2018-06-29 03:53:00 +03:00
|
|
|
let sibling_pruned = self.is_pruned_root(sibling);
|
2018-06-18 18:18:38 +03:00
|
|
|
|
|
|
|
// if sibling previously pruned
|
|
|
|
// push it back onto list of pos to remove
|
|
|
|
// so we can remove it and traverse up to parent
|
|
|
|
if sibling_pruned {
|
|
|
|
expanded.add(sibling as u32);
|
|
|
|
}
|
|
|
|
|
|
|
|
if sibling_pruned || expanded.contains(sibling as u32) {
|
|
|
|
expanded.add(parent as u32);
|
|
|
|
current = parent;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(leaf_pos_to_rm, removed_excl_roots(expanded))
|
|
|
|
}
|
2018-02-22 16:45:13 +03:00
|
|
|
}
|
2018-03-13 21:22:34 +03:00
|
|
|
|
2018-10-15 19:16:34 +03:00
|
|
|
/// Simple MMR Backend for hashes only (data maintained in the db).
|
|
|
|
pub struct HashOnlyMMRBackend {
|
|
|
|
/// The hash file underlying this MMR backend.
|
|
|
|
hash_file: HashFile,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HashOnlyBackend for HashOnlyMMRBackend {
|
|
|
|
fn append(&mut self, hashes: Vec<Hash>) -> Result<(), String> {
|
|
|
|
for ref h in hashes {
|
|
|
|
self.hash_file
|
|
|
|
.append(h)
|
|
|
|
.map_err(|e| format!("Failed to append to backend, {:?}", e))?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rewind(&mut self, position: u64) -> Result<(), String> {
|
|
|
|
self.hash_file
|
|
|
|
.rewind(position)
|
|
|
|
.map_err(|e| format!("Failed to rewind backend, {:?}", e))?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_hash(&self, position: u64) -> Option<Hash> {
|
|
|
|
self.hash_file.read(position)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HashOnlyMMRBackend {
|
|
|
|
/// Instantiates a new PMMR backend.
|
|
|
|
/// Use the provided dir to store its files.
|
|
|
|
pub fn new(data_dir: String) -> io::Result<HashOnlyMMRBackend> {
|
|
|
|
let hash_file = HashFile::open(format!("{}/{}", data_dir, PMMR_HASH_FILE))?;
|
|
|
|
Ok(HashOnlyMMRBackend { hash_file })
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Discard any pending changes to this MMR backend.
|
|
|
|
pub fn discard(&mut self) {
|
|
|
|
self.hash_file.discard();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sync pending changes to the backend file on disk.
|
|
|
|
pub fn sync(&mut self) -> io::Result<()> {
|
|
|
|
if let Err(e) = self.hash_file.flush() {
|
|
|
|
return Err(io::Error::new(
|
|
|
|
io::ErrorKind::Interrupted,
|
|
|
|
format!("Could not write to hash storage, disk full? {:?}", e),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-13 21:22:34 +03:00
|
|
|
/// Filter remove list to exclude roots.
|
|
|
|
/// We want to keep roots around so we have hashes for Merkle proofs.
|
2018-06-18 18:18:38 +03:00
|
|
|
fn removed_excl_roots(removed: Bitmap) -> Bitmap {
|
2018-03-13 21:22:34 +03:00
|
|
|
removed
|
|
|
|
.iter()
|
2018-06-18 18:18:38 +03:00
|
|
|
.filter(|pos| {
|
|
|
|
let (parent_pos, _) = family(*pos as u64);
|
|
|
|
removed.contains(parent_pos as u32)
|
2018-10-15 19:16:34 +03:00
|
|
|
}).collect()
|
2018-03-13 21:22:34 +03:00
|
|
|
}
|