2018-03-05 22:33:44 +03:00
|
|
|
// Copyright 2018 The Grin Developers
|
2017-09-05 08:50:25 +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.
|
|
|
|
|
2018-10-18 13:40:58 +03:00
|
|
|
extern crate chrono;
|
2018-06-18 18:18:38 +03:00
|
|
|
extern crate croaring;
|
2017-09-05 08:50:25 +03:00
|
|
|
extern crate env_logger;
|
|
|
|
extern crate grin_core as core;
|
|
|
|
extern crate grin_store as store;
|
|
|
|
|
|
|
|
use std::fs;
|
|
|
|
|
2018-10-18 13:40:58 +03:00
|
|
|
use chrono::prelude::Utc;
|
2018-06-18 18:18:38 +03:00
|
|
|
use croaring::Bitmap;
|
|
|
|
|
2018-03-04 03:19:54 +03:00
|
|
|
use core::core::pmmr::{Backend, PMMR};
|
2018-11-01 23:14:46 +03:00
|
|
|
use core::ser::{
|
|
|
|
Error, FixedLength, PMMRIndexHashable, PMMRable, Readable, Reader, Writeable, Writer,
|
|
|
|
};
|
2018-03-06 20:58:33 +03:00
|
|
|
use store::types::prune_noop;
|
2017-09-05 08:50:25 +03:00
|
|
|
|
|
|
|
#[test]
|
2018-02-22 16:45:13 +03:00
|
|
|
fn pmmr_append() {
|
2018-01-28 09:12:33 +03:00
|
|
|
let (data_dir, elems) = setup("append");
|
2018-07-05 05:31:08 +03:00
|
|
|
let mut backend = store::pmmr::PMMRBackend::new(data_dir.to_string(), true, None).unwrap();
|
2017-09-05 08:50:25 +03:00
|
|
|
|
|
|
|
// adding first set of 4 elements and sync
|
|
|
|
let mut mmr_size = load(0, &elems[0..4], &mut backend);
|
|
|
|
backend.sync().unwrap();
|
|
|
|
|
|
|
|
// adding the rest and sync again
|
|
|
|
mmr_size = load(mmr_size, &elems[4..9], &mut backend);
|
|
|
|
backend.sync().unwrap();
|
|
|
|
|
|
|
|
// check the resulting backend store and the computation of the root
|
2018-03-26 05:26:11 +03:00
|
|
|
let node_hash = elems[0].hash_with_index(0);
|
2018-03-24 02:33:59 +03:00
|
|
|
assert_eq!(backend.get_hash(1).unwrap(), node_hash);
|
2017-09-05 08:50:25 +03:00
|
|
|
|
2018-03-16 17:45:58 +03:00
|
|
|
// 0010012001001230
|
|
|
|
|
2018-03-26 05:26:11 +03:00
|
|
|
let pos_0 = elems[0].hash_with_index(0);
|
|
|
|
let pos_1 = elems[1].hash_with_index(1);
|
|
|
|
let pos_2 = (pos_0, pos_1).hash_with_index(2);
|
2018-03-16 17:45:58 +03:00
|
|
|
|
2018-03-26 05:26:11 +03:00
|
|
|
let pos_3 = elems[2].hash_with_index(3);
|
|
|
|
let pos_4 = elems[3].hash_with_index(4);
|
|
|
|
let pos_5 = (pos_3, pos_4).hash_with_index(5);
|
|
|
|
let pos_6 = (pos_2, pos_5).hash_with_index(6);
|
2018-03-16 17:45:58 +03:00
|
|
|
|
2018-03-26 05:26:11 +03:00
|
|
|
let pos_7 = elems[4].hash_with_index(7);
|
|
|
|
let pos_8 = elems[5].hash_with_index(8);
|
|
|
|
let pos_9 = (pos_7, pos_8).hash_with_index(9);
|
2018-03-16 17:45:58 +03:00
|
|
|
|
2018-03-26 05:26:11 +03:00
|
|
|
let pos_10 = elems[6].hash_with_index(10);
|
|
|
|
let pos_11 = elems[7].hash_with_index(11);
|
|
|
|
let pos_12 = (pos_10, pos_11).hash_with_index(12);
|
|
|
|
let pos_13 = (pos_9, pos_12).hash_with_index(13);
|
|
|
|
let pos_14 = (pos_6, pos_13).hash_with_index(14);
|
2018-03-16 17:45:58 +03:00
|
|
|
|
2018-03-26 05:26:11 +03:00
|
|
|
let pos_15 = elems[8].hash_with_index(15);
|
2017-09-05 08:50:25 +03:00
|
|
|
|
|
|
|
{
|
2018-03-04 03:19:54 +03:00
|
|
|
let pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
2018-03-26 05:26:11 +03:00
|
|
|
assert_eq!(pmmr.root(), (pos_14, pos_15).hash_with_index(16));
|
2017-09-05 08:50:25 +03:00
|
|
|
}
|
2018-01-28 09:12:33 +03:00
|
|
|
|
|
|
|
teardown(data_dir);
|
2017-09-05 08:50:25 +03:00
|
|
|
}
|
|
|
|
|
2018-03-13 21:22:34 +03:00
|
|
|
#[test]
|
|
|
|
fn pmmr_compact_leaf_sibling() {
|
|
|
|
let (data_dir, elems) = setup("compact_leaf_sibling");
|
|
|
|
|
|
|
|
// setup the mmr store with all elements
|
2018-07-05 05:31:08 +03:00
|
|
|
let mut backend = store::pmmr::PMMRBackend::new(data_dir.to_string(), true, None).unwrap();
|
2018-03-13 21:22:34 +03:00
|
|
|
let mmr_size = load(0, &elems[..], &mut backend);
|
|
|
|
backend.sync().unwrap();
|
|
|
|
|
|
|
|
// On far left of the MMR -
|
|
|
|
// pos 1 and 2 are leaves (and siblings)
|
|
|
|
// the parent is pos 3
|
|
|
|
|
|
|
|
let (pos_1_hash, pos_2_hash, pos_3_hash) = {
|
|
|
|
let mut pmmr = PMMR::at(&mut backend, mmr_size);
|
|
|
|
(
|
2018-03-24 02:33:59 +03:00
|
|
|
pmmr.get_hash(1).unwrap(),
|
|
|
|
pmmr.get_hash(2).unwrap(),
|
|
|
|
pmmr.get_hash(3).unwrap(),
|
2018-03-13 21:22:34 +03:00
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
// prune pos 1
|
|
|
|
{
|
|
|
|
let mut pmmr = PMMR::at(&mut backend, mmr_size);
|
2018-06-18 18:18:38 +03:00
|
|
|
pmmr.prune(1).unwrap();
|
2018-03-13 21:22:34 +03:00
|
|
|
|
|
|
|
// prune pos 8 as well to push the remove list past the cutoff
|
2018-06-18 18:18:38 +03:00
|
|
|
pmmr.prune(8).unwrap();
|
2018-03-13 21:22:34 +03:00
|
|
|
}
|
|
|
|
backend.sync().unwrap();
|
|
|
|
|
|
|
|
// // check pos 1, 2, 3 are in the state we expect after pruning
|
|
|
|
{
|
|
|
|
let pmmr = PMMR::at(&mut backend, mmr_size);
|
|
|
|
|
|
|
|
// check that pos 1 is "removed"
|
2018-03-24 02:33:59 +03:00
|
|
|
assert_eq!(pmmr.get_hash(1), None);
|
2018-03-13 21:22:34 +03:00
|
|
|
|
|
|
|
// check that pos 2 and 3 are unchanged
|
2018-03-24 02:33:59 +03:00
|
|
|
assert_eq!(pmmr.get_hash(2).unwrap(), pos_2_hash);
|
|
|
|
assert_eq!(pmmr.get_hash(3).unwrap(), pos_3_hash);
|
2018-03-13 21:22:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// check we can still retrieve the "removed" element at pos 1
|
|
|
|
// from the backend hash file.
|
|
|
|
assert_eq!(backend.get_from_file(1).unwrap(), pos_1_hash);
|
|
|
|
|
|
|
|
// aggressively compact the PMMR files
|
2018-06-18 18:18:38 +03:00
|
|
|
backend
|
2018-07-08 19:37:09 +03:00
|
|
|
.check_compact(1, &Bitmap::create(), &prune_noop)
|
2018-06-18 18:18:38 +03:00
|
|
|
.unwrap();
|
2018-03-13 21:22:34 +03:00
|
|
|
|
|
|
|
// check pos 1, 2, 3 are in the state we expect after compacting
|
|
|
|
{
|
|
|
|
let pmmr = PMMR::at(&mut backend, mmr_size);
|
|
|
|
|
|
|
|
// check that pos 1 is "removed"
|
2018-03-24 02:33:59 +03:00
|
|
|
assert_eq!(pmmr.get_hash(1), None);
|
2018-03-13 21:22:34 +03:00
|
|
|
|
|
|
|
// check that pos 2 and 3 are unchanged
|
2018-03-24 02:33:59 +03:00
|
|
|
assert_eq!(pmmr.get_hash(2).unwrap(), pos_2_hash);
|
|
|
|
assert_eq!(pmmr.get_hash(3).unwrap(), pos_3_hash);
|
2018-03-13 21:22:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check we can still retrieve the "removed" hash at pos 1 from the hash file.
|
|
|
|
// It should still be available even after pruning and compacting.
|
|
|
|
assert_eq!(backend.get_from_file(1).unwrap(), pos_1_hash);
|
2018-03-15 20:12:30 +03:00
|
|
|
|
|
|
|
teardown(data_dir);
|
2018-03-13 21:22:34 +03:00
|
|
|
}
|
|
|
|
|
2017-09-05 08:50:25 +03:00
|
|
|
#[test]
|
2018-02-22 16:45:13 +03:00
|
|
|
fn pmmr_prune_compact() {
|
2018-01-28 09:12:33 +03:00
|
|
|
let (data_dir, elems) = setup("prune_compact");
|
2017-09-05 08:50:25 +03:00
|
|
|
|
|
|
|
// setup the mmr store with all elements
|
2018-07-05 05:31:08 +03:00
|
|
|
let mut backend = store::pmmr::PMMRBackend::new(data_dir.to_string(), true, None).unwrap();
|
2017-09-05 08:50:25 +03:00
|
|
|
let mmr_size = load(0, &elems[..], &mut backend);
|
|
|
|
backend.sync().unwrap();
|
2017-09-29 21:44:25 +03:00
|
|
|
|
2017-09-05 08:50:25 +03:00
|
|
|
// save the root
|
2018-03-13 21:22:34 +03:00
|
|
|
let root = {
|
2018-03-04 03:19:54 +03:00
|
|
|
let pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
2018-03-13 21:22:34 +03:00
|
|
|
pmmr.root()
|
|
|
|
};
|
2017-09-05 08:50:25 +03:00
|
|
|
|
|
|
|
// pruning some choice nodes
|
|
|
|
{
|
2018-03-04 03:19:54 +03:00
|
|
|
let mut pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
2018-06-18 18:18:38 +03:00
|
|
|
pmmr.prune(1).unwrap();
|
|
|
|
pmmr.prune(4).unwrap();
|
|
|
|
pmmr.prune(5).unwrap();
|
2017-09-05 08:50:25 +03:00
|
|
|
}
|
|
|
|
backend.sync().unwrap();
|
|
|
|
|
2018-02-22 16:45:13 +03:00
|
|
|
// check the root and stored data
|
2017-09-05 08:50:25 +03:00
|
|
|
{
|
2018-03-04 03:19:54 +03:00
|
|
|
let pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
2017-09-05 08:50:25 +03:00
|
|
|
assert_eq!(root, pmmr.root());
|
2018-02-22 16:45:13 +03:00
|
|
|
// check we can still retrieve same element from leaf index 2
|
2018-03-24 02:33:59 +03:00
|
|
|
assert_eq!(pmmr.get_data(2).unwrap(), TestElem(2));
|
2018-03-13 21:22:34 +03:00
|
|
|
// and the same for leaf index 7
|
2018-03-24 02:33:59 +03:00
|
|
|
assert_eq!(pmmr.get_data(11).unwrap(), TestElem(7));
|
2017-09-05 08:50:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// compact
|
2018-06-18 18:18:38 +03:00
|
|
|
backend
|
2018-07-08 19:37:09 +03:00
|
|
|
.check_compact(2, &Bitmap::create(), &prune_noop)
|
2018-06-18 18:18:38 +03:00
|
|
|
.unwrap();
|
2017-09-05 08:50:25 +03:00
|
|
|
|
2018-02-22 16:45:13 +03:00
|
|
|
// recheck the root and stored data
|
2017-09-05 08:50:25 +03:00
|
|
|
{
|
2018-03-04 03:19:54 +03:00
|
|
|
let pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
2017-09-05 08:50:25 +03:00
|
|
|
assert_eq!(root, pmmr.root());
|
2018-03-24 02:33:59 +03:00
|
|
|
assert_eq!(pmmr.get_data(2).unwrap(), TestElem(2));
|
|
|
|
assert_eq!(pmmr.get_data(11).unwrap(), TestElem(7));
|
2017-09-05 08:50:25 +03:00
|
|
|
}
|
2018-01-28 09:12:33 +03:00
|
|
|
|
|
|
|
teardown(data_dir);
|
2017-09-05 08:50:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-02-22 16:45:13 +03:00
|
|
|
fn pmmr_reload() {
|
2018-01-28 09:12:33 +03:00
|
|
|
let (data_dir, elems) = setup("reload");
|
2017-09-05 08:50:25 +03:00
|
|
|
|
2018-03-13 21:22:34 +03:00
|
|
|
// set everything up with an initial backend
|
2018-07-05 05:31:08 +03:00
|
|
|
let mut backend = store::pmmr::PMMRBackend::new(data_dir.to_string(), true, None).unwrap();
|
2018-03-13 21:22:34 +03:00
|
|
|
|
|
|
|
let mmr_size = load(0, &elems[..], &mut backend);
|
|
|
|
|
|
|
|
// retrieve entries from the hash file for comparison later
|
2018-03-24 02:33:59 +03:00
|
|
|
let pos_3_hash = backend.get_hash(3).unwrap();
|
|
|
|
let pos_4_hash = backend.get_hash(4).unwrap();
|
|
|
|
let pos_5_hash = backend.get_hash(5).unwrap();
|
2018-03-13 21:22:34 +03:00
|
|
|
|
|
|
|
// save the root
|
|
|
|
let root = {
|
|
|
|
let pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
|
|
|
pmmr.root()
|
|
|
|
};
|
|
|
|
|
2017-09-05 08:50:25 +03:00
|
|
|
{
|
|
|
|
backend.sync().unwrap();
|
2017-09-29 21:44:25 +03:00
|
|
|
|
2018-03-13 21:22:34 +03:00
|
|
|
// prune a node so we have prune data
|
2017-09-05 08:50:25 +03:00
|
|
|
{
|
2018-03-04 03:19:54 +03:00
|
|
|
let mut pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
2018-06-18 18:18:38 +03:00
|
|
|
pmmr.prune(1).unwrap();
|
2018-03-13 21:22:34 +03:00
|
|
|
}
|
|
|
|
backend.sync().unwrap();
|
|
|
|
|
|
|
|
// now check and compact the backend
|
2018-06-18 18:18:38 +03:00
|
|
|
backend
|
2018-07-08 19:37:09 +03:00
|
|
|
.check_compact(1, &Bitmap::create(), &prune_noop)
|
2018-06-18 18:18:38 +03:00
|
|
|
.unwrap();
|
2018-03-13 21:22:34 +03:00
|
|
|
backend.sync().unwrap();
|
|
|
|
|
|
|
|
// prune another node to force compact to actually do something
|
|
|
|
{
|
|
|
|
let mut pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
2018-06-18 18:18:38 +03:00
|
|
|
pmmr.prune(4).unwrap();
|
|
|
|
pmmr.prune(2).unwrap();
|
2017-09-05 08:50:25 +03:00
|
|
|
}
|
|
|
|
backend.sync().unwrap();
|
2018-02-22 16:45:13 +03:00
|
|
|
|
2018-06-18 18:18:38 +03:00
|
|
|
backend
|
2018-07-08 19:37:09 +03:00
|
|
|
.check_compact(4, &Bitmap::create(), &prune_noop)
|
2018-06-18 18:18:38 +03:00
|
|
|
.unwrap();
|
2017-09-05 08:50:25 +03:00
|
|
|
backend.sync().unwrap();
|
2018-03-13 21:22:34 +03:00
|
|
|
|
2018-11-05 15:01:24 +03:00
|
|
|
assert_eq!(backend.unpruned_size(), mmr_size);
|
2017-09-05 08:50:25 +03:00
|
|
|
|
|
|
|
// prune some more to get rm log data
|
|
|
|
{
|
2018-03-04 03:19:54 +03:00
|
|
|
let mut pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
2018-06-18 18:18:38 +03:00
|
|
|
pmmr.prune(5).unwrap();
|
2017-09-05 08:50:25 +03:00
|
|
|
}
|
|
|
|
backend.sync().unwrap();
|
2018-11-05 15:01:24 +03:00
|
|
|
assert_eq!(backend.unpruned_size(), mmr_size);
|
2017-09-05 08:50:25 +03:00
|
|
|
}
|
|
|
|
|
2018-03-13 21:22:34 +03:00
|
|
|
// create a new backend referencing the data files
|
|
|
|
// and check everything still works as expected
|
2017-09-05 08:50:25 +03:00
|
|
|
{
|
2018-07-05 05:31:08 +03:00
|
|
|
let mut backend = store::pmmr::PMMRBackend::new(data_dir.to_string(), true, None).unwrap();
|
2018-11-05 15:01:24 +03:00
|
|
|
assert_eq!(backend.unpruned_size(), mmr_size);
|
2017-09-05 08:50:25 +03:00
|
|
|
{
|
2018-03-04 03:19:54 +03:00
|
|
|
let pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
2017-09-05 08:50:25 +03:00
|
|
|
assert_eq!(root, pmmr.root());
|
|
|
|
}
|
2018-03-13 21:22:34 +03:00
|
|
|
|
|
|
|
// pos 1 and pos 2 are both removed (via parent pos 3 in prune list)
|
2018-03-24 02:33:59 +03:00
|
|
|
assert_eq!(backend.get_hash(1), None);
|
|
|
|
assert_eq!(backend.get_hash(2), None);
|
2018-03-13 21:22:34 +03:00
|
|
|
|
2018-06-29 03:53:00 +03:00
|
|
|
// pos 3 is "removed" but we keep the hash around for root of pruned subtree
|
2018-06-18 18:18:38 +03:00
|
|
|
assert_eq!(backend.get_hash(3), Some(pos_3_hash));
|
2018-03-13 21:22:34 +03:00
|
|
|
|
|
|
|
// pos 4 is removed (via prune list)
|
2018-03-24 02:33:59 +03:00
|
|
|
assert_eq!(backend.get_hash(4), None);
|
2018-10-31 14:16:27 +03:00
|
|
|
// pos 5 is removed (via leaf_set)
|
2018-03-24 02:33:59 +03:00
|
|
|
assert_eq!(backend.get_hash(5), None);
|
2018-03-13 21:22:34 +03:00
|
|
|
|
|
|
|
// now check contents of the hash file
|
|
|
|
// pos 1 and pos 2 are no longer in the hash file
|
|
|
|
assert_eq!(backend.get_from_file(1), None);
|
|
|
|
assert_eq!(backend.get_from_file(2), None);
|
|
|
|
|
|
|
|
// pos 3 is still in there
|
|
|
|
assert_eq!(backend.get_from_file(3), Some(pos_3_hash));
|
|
|
|
|
|
|
|
// pos 4 and pos 5 are also still in there
|
|
|
|
assert_eq!(backend.get_from_file(4), Some(pos_4_hash));
|
|
|
|
assert_eq!(backend.get_from_file(5), Some(pos_5_hash));
|
2017-09-05 08:50:25 +03:00
|
|
|
}
|
2018-01-28 09:12:33 +03:00
|
|
|
|
|
|
|
teardown(data_dir);
|
2017-09-05 08:50:25 +03:00
|
|
|
}
|
|
|
|
|
2017-10-03 01:32:23 +03:00
|
|
|
#[test]
|
2018-02-22 16:45:13 +03:00
|
|
|
fn pmmr_rewind() {
|
2018-01-28 09:12:33 +03:00
|
|
|
let (data_dir, elems) = setup("rewind");
|
2018-07-05 05:31:08 +03:00
|
|
|
let mut backend = store::pmmr::PMMRBackend::new(data_dir.clone(), true, None).unwrap();
|
2017-10-03 01:32:23 +03:00
|
|
|
|
|
|
|
// adding elements and keeping the corresponding root
|
|
|
|
let mut mmr_size = load(0, &elems[0..4], &mut backend);
|
|
|
|
backend.sync().unwrap();
|
2018-04-19 21:52:46 +03:00
|
|
|
let root1 = {
|
2018-03-04 03:19:54 +03:00
|
|
|
let pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
2018-04-19 21:52:46 +03:00
|
|
|
pmmr.root()
|
|
|
|
};
|
2017-10-03 01:32:23 +03:00
|
|
|
|
|
|
|
mmr_size = load(mmr_size, &elems[4..6], &mut backend);
|
|
|
|
backend.sync().unwrap();
|
2018-04-19 21:52:46 +03:00
|
|
|
let root2 = {
|
2018-03-04 03:19:54 +03:00
|
|
|
let pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
2018-06-29 03:53:00 +03:00
|
|
|
assert_eq!(pmmr.unpruned_size(), 10);
|
2018-04-19 21:52:46 +03:00
|
|
|
pmmr.root()
|
|
|
|
};
|
2017-10-03 01:32:23 +03:00
|
|
|
|
|
|
|
mmr_size = load(mmr_size, &elems[6..9], &mut backend);
|
|
|
|
backend.sync().unwrap();
|
2018-06-29 03:53:00 +03:00
|
|
|
let root3 = {
|
|
|
|
let pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
|
|
|
assert_eq!(pmmr.unpruned_size(), 16);
|
|
|
|
pmmr.root()
|
|
|
|
};
|
2017-10-03 01:32:23 +03:00
|
|
|
|
2018-04-19 21:52:46 +03:00
|
|
|
// prune the first 4 elements (leaves at pos 1, 2, 4, 5)
|
2017-10-03 01:32:23 +03:00
|
|
|
{
|
2018-03-04 03:19:54 +03:00
|
|
|
let mut pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
2018-06-18 18:18:38 +03:00
|
|
|
pmmr.prune(1).unwrap();
|
|
|
|
pmmr.prune(2).unwrap();
|
|
|
|
pmmr.prune(4).unwrap();
|
|
|
|
pmmr.prune(5).unwrap();
|
2017-10-03 01:32:23 +03:00
|
|
|
}
|
2018-04-19 21:52:46 +03:00
|
|
|
backend.sync().unwrap();
|
|
|
|
|
2018-06-29 03:53:00 +03:00
|
|
|
println!("before compacting - ");
|
|
|
|
for x in 1..17 {
|
|
|
|
println!("pos {}, {:?}", x, backend.get_from_file(x));
|
|
|
|
}
|
|
|
|
|
2018-06-18 18:18:38 +03:00
|
|
|
// and compact the MMR to remove the pruned elements
|
|
|
|
backend
|
2018-07-08 19:37:09 +03:00
|
|
|
.check_compact(6, &Bitmap::create(), &prune_noop)
|
2018-06-18 18:18:38 +03:00
|
|
|
.unwrap();
|
2017-10-03 01:32:23 +03:00
|
|
|
backend.sync().unwrap();
|
2017-11-01 02:32:33 +03:00
|
|
|
|
2018-06-29 03:53:00 +03:00
|
|
|
println!("after compacting - ");
|
|
|
|
for x in 1..17 {
|
|
|
|
println!("pos {}, {:?}", x, backend.get_from_file(x));
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("root1 {:?}, root2 {:?}, root3 {:?}", root1, root2, root3);
|
|
|
|
|
2017-10-03 01:32:23 +03:00
|
|
|
// rewind and check the roots still match
|
|
|
|
{
|
2018-03-04 03:19:54 +03:00
|
|
|
let mut pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
2018-10-18 13:40:58 +03:00
|
|
|
pmmr.rewind(9, &Bitmap::of(&vec![11, 12, 16])).unwrap();
|
2018-06-29 03:53:00 +03:00
|
|
|
assert_eq!(pmmr.unpruned_size(), 10);
|
|
|
|
|
|
|
|
// assert_eq!(pmmr.root(), root2);
|
|
|
|
}
|
|
|
|
println!("after rewinding - ");
|
|
|
|
for x in 1..17 {
|
|
|
|
println!("pos {}, {:?}", x, backend.get_from_file(x));
|
2017-10-03 01:32:23 +03:00
|
|
|
}
|
2018-06-18 18:18:38 +03:00
|
|
|
|
|
|
|
println!("doing a sync after rewinding");
|
2017-10-03 01:32:23 +03:00
|
|
|
backend.sync().unwrap();
|
2018-06-18 18:18:38 +03:00
|
|
|
|
2017-10-03 01:32:23 +03:00
|
|
|
{
|
2018-03-04 03:19:54 +03:00
|
|
|
let pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, 10);
|
2017-10-03 01:32:23 +03:00
|
|
|
assert_eq!(pmmr.root(), root2);
|
|
|
|
}
|
|
|
|
|
2018-06-18 18:18:38 +03:00
|
|
|
// Also check the data file looks correct.
|
|
|
|
// pos 1, 2, 4, 5 are all leaves but these have been pruned.
|
|
|
|
for pos in vec![1, 2, 4, 5] {
|
2018-04-19 21:52:46 +03:00
|
|
|
assert_eq!(backend.get_data(pos), None);
|
|
|
|
}
|
2018-06-18 18:18:38 +03:00
|
|
|
// pos 3, 6, 7 are non-leaves so we have no data for these
|
|
|
|
for pos in vec![3, 6, 7] {
|
|
|
|
assert_eq!(backend.get_data(pos), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
// pos 8 and 9 are both leaves and should be unaffected by prior pruning
|
|
|
|
|
|
|
|
for x in 1..16 {
|
|
|
|
println!("data at {}, {:?}", x, backend.get_data(x));
|
|
|
|
}
|
|
|
|
|
2018-04-19 21:52:46 +03:00
|
|
|
assert_eq!(backend.get_data(8), Some(elems[4]));
|
2018-06-18 18:18:38 +03:00
|
|
|
assert_eq!(backend.get_hash(8), Some(elems[4].hash_with_index(7)));
|
|
|
|
|
2018-04-19 21:52:46 +03:00
|
|
|
assert_eq!(backend.get_data(9), Some(elems[5]));
|
2018-06-18 18:18:38 +03:00
|
|
|
assert_eq!(backend.get_hash(9), Some(elems[5].hash_with_index(8)));
|
2018-04-19 21:52:46 +03:00
|
|
|
|
2018-11-05 15:01:24 +03:00
|
|
|
assert_eq!(backend.data_size(), 2);
|
2018-04-19 21:52:46 +03:00
|
|
|
|
2017-10-03 01:32:23 +03:00
|
|
|
{
|
2018-03-04 03:19:54 +03:00
|
|
|
let mut pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, 10);
|
2018-10-18 13:40:58 +03:00
|
|
|
pmmr.rewind(5, &Bitmap::create()).unwrap();
|
2017-10-03 01:32:23 +03:00
|
|
|
assert_eq!(pmmr.root(), root1);
|
|
|
|
}
|
|
|
|
backend.sync().unwrap();
|
|
|
|
{
|
2018-03-04 03:19:54 +03:00
|
|
|
let pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, 7);
|
2017-10-03 01:32:23 +03:00
|
|
|
assert_eq!(pmmr.root(), root1);
|
|
|
|
}
|
2018-01-28 09:12:33 +03:00
|
|
|
|
2018-04-19 21:52:46 +03:00
|
|
|
// also check the data file looks correct
|
|
|
|
// everything up to and including pos 7 should be pruned from the data file
|
|
|
|
// but we have rewound to pos 5 so everything after that should be None
|
|
|
|
for pos in 1..10 {
|
|
|
|
assert_eq!(backend.get_data(pos), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check we have no data in the backend after
|
|
|
|
// pruning, compacting and rewinding
|
2018-11-05 15:01:24 +03:00
|
|
|
assert_eq!(backend.data_size(), 0);
|
2018-04-19 21:52:46 +03:00
|
|
|
|
2018-01-28 09:12:33 +03:00
|
|
|
teardown(data_dir);
|
2017-10-03 01:32:23 +03:00
|
|
|
}
|
|
|
|
|
2018-03-13 21:22:34 +03:00
|
|
|
#[test]
|
|
|
|
fn pmmr_compact_single_leaves() {
|
|
|
|
let (data_dir, elems) = setup("compact_single_leaves");
|
2018-07-05 05:31:08 +03:00
|
|
|
let mut backend = store::pmmr::PMMRBackend::new(data_dir.clone(), true, None).unwrap();
|
2018-03-13 21:22:34 +03:00
|
|
|
let mmr_size = load(0, &elems[0..5], &mut backend);
|
|
|
|
backend.sync().unwrap();
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
2018-06-18 18:18:38 +03:00
|
|
|
pmmr.prune(1).unwrap();
|
|
|
|
pmmr.prune(4).unwrap();
|
2018-03-13 21:22:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
backend.sync().unwrap();
|
|
|
|
|
|
|
|
// compact
|
2018-06-18 18:18:38 +03:00
|
|
|
backend
|
2018-07-08 19:37:09 +03:00
|
|
|
.check_compact(2, &Bitmap::create(), &prune_noop)
|
2018-06-18 18:18:38 +03:00
|
|
|
.unwrap();
|
2018-03-13 21:22:34 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
let mut pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
2018-06-18 18:18:38 +03:00
|
|
|
pmmr.prune(2).unwrap();
|
|
|
|
pmmr.prune(5).unwrap();
|
2018-03-13 21:22:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
backend.sync().unwrap();
|
|
|
|
|
|
|
|
// compact
|
2018-06-18 18:18:38 +03:00
|
|
|
backend
|
2018-07-08 19:37:09 +03:00
|
|
|
.check_compact(2, &Bitmap::create(), &prune_noop)
|
2018-06-18 18:18:38 +03:00
|
|
|
.unwrap();
|
2018-03-13 21:22:34 +03:00
|
|
|
|
|
|
|
teardown(data_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn pmmr_compact_entire_peak() {
|
|
|
|
let (data_dir, elems) = setup("compact_entire_peak");
|
2018-07-05 05:31:08 +03:00
|
|
|
let mut backend = store::pmmr::PMMRBackend::new(data_dir.clone(), true, None).unwrap();
|
2018-03-13 21:22:34 +03:00
|
|
|
let mmr_size = load(0, &elems[0..5], &mut backend);
|
|
|
|
backend.sync().unwrap();
|
|
|
|
|
2018-03-24 02:33:59 +03:00
|
|
|
let pos_7_hash = backend.get_hash(7).unwrap();
|
2018-03-13 21:22:34 +03:00
|
|
|
|
2018-03-24 02:33:59 +03:00
|
|
|
let pos_8 = backend.get_data(8).unwrap();
|
|
|
|
let pos_8_hash = backend.get_hash(8).unwrap();
|
2018-03-13 21:22:34 +03:00
|
|
|
|
|
|
|
// prune all leaves under the peak at pos 7
|
|
|
|
{
|
|
|
|
let mut pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
2018-06-18 18:18:38 +03:00
|
|
|
pmmr.prune(1).unwrap();
|
|
|
|
pmmr.prune(2).unwrap();
|
|
|
|
pmmr.prune(4).unwrap();
|
|
|
|
pmmr.prune(5).unwrap();
|
2018-03-13 21:22:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
backend.sync().unwrap();
|
|
|
|
|
|
|
|
// compact
|
2018-06-18 18:18:38 +03:00
|
|
|
backend
|
2018-07-08 19:37:09 +03:00
|
|
|
.check_compact(2, &Bitmap::create(), &prune_noop)
|
2018-06-18 18:18:38 +03:00
|
|
|
.unwrap();
|
2018-03-13 21:22:34 +03:00
|
|
|
|
|
|
|
// now check we have pruned up to and including the peak at pos 7
|
|
|
|
// hash still available in underlying hash file
|
2018-06-18 18:18:38 +03:00
|
|
|
assert_eq!(backend.get_hash(7), Some(pos_7_hash));
|
2018-03-13 21:22:34 +03:00
|
|
|
assert_eq!(backend.get_from_file(7), Some(pos_7_hash));
|
|
|
|
|
|
|
|
// now check we still have subsequent hash and data where we expect
|
2018-03-24 02:33:59 +03:00
|
|
|
assert_eq!(backend.get_data(8), Some(pos_8));
|
2018-06-18 18:18:38 +03:00
|
|
|
assert_eq!(backend.get_hash(8), Some(pos_8_hash));
|
2018-03-13 21:22:34 +03:00
|
|
|
assert_eq!(backend.get_from_file(8), Some(pos_8_hash));
|
|
|
|
|
|
|
|
teardown(data_dir);
|
|
|
|
}
|
|
|
|
|
2018-03-01 00:15:29 +03:00
|
|
|
#[test]
|
|
|
|
fn pmmr_compact_horizon() {
|
|
|
|
let (data_dir, elems) = setup("compact_horizon");
|
2018-07-05 05:31:08 +03:00
|
|
|
let mut backend = store::pmmr::PMMRBackend::new(data_dir.clone(), true, None).unwrap();
|
2018-03-13 21:22:34 +03:00
|
|
|
let mmr_size = load(0, &elems[..], &mut backend);
|
|
|
|
backend.sync().unwrap();
|
2018-03-01 00:15:29 +03:00
|
|
|
|
2018-03-13 21:22:34 +03:00
|
|
|
// 0010012001001230
|
|
|
|
// 9 leaves
|
2018-11-05 15:01:24 +03:00
|
|
|
assert_eq!(backend.data_size(), 19);
|
|
|
|
assert_eq!(backend.hash_size(), 35);
|
2018-03-01 00:15:29 +03:00
|
|
|
|
2018-06-18 18:18:38 +03:00
|
|
|
let pos_1_hash = backend.get_hash(1).unwrap();
|
|
|
|
let pos_2_hash = backend.get_hash(2).unwrap();
|
2018-03-24 02:33:59 +03:00
|
|
|
let pos_3_hash = backend.get_hash(3).unwrap();
|
|
|
|
let pos_6_hash = backend.get_hash(6).unwrap();
|
|
|
|
let pos_7_hash = backend.get_hash(7).unwrap();
|
2018-03-13 21:22:34 +03:00
|
|
|
|
2018-03-24 02:33:59 +03:00
|
|
|
let pos_8 = backend.get_data(8).unwrap();
|
|
|
|
let pos_8_hash = backend.get_hash(8).unwrap();
|
2018-03-13 21:22:34 +03:00
|
|
|
|
2018-03-24 02:33:59 +03:00
|
|
|
let pos_11 = backend.get_data(11).unwrap();
|
|
|
|
let pos_11_hash = backend.get_hash(11).unwrap();
|
2018-03-13 21:22:34 +03:00
|
|
|
|
|
|
|
{
|
2018-06-18 18:18:38 +03:00
|
|
|
// pruning some choice nodes
|
2018-03-01 00:15:29 +03:00
|
|
|
{
|
2018-03-04 03:19:54 +03:00
|
|
|
let mut pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
2018-06-18 18:18:38 +03:00
|
|
|
pmmr.prune(4).unwrap();
|
|
|
|
pmmr.prune(5).unwrap();
|
|
|
|
pmmr.prune(1).unwrap();
|
|
|
|
pmmr.prune(2).unwrap();
|
2018-03-01 00:15:29 +03:00
|
|
|
}
|
|
|
|
backend.sync().unwrap();
|
2018-03-13 21:22:34 +03:00
|
|
|
|
|
|
|
// check we can read hashes and data correctly after pruning
|
|
|
|
{
|
2018-06-18 18:18:38 +03:00
|
|
|
// assert_eq!(backend.get_hash(3), None);
|
2018-03-13 21:22:34 +03:00
|
|
|
assert_eq!(backend.get_from_file(3), Some(pos_3_hash));
|
|
|
|
|
2018-06-18 18:18:38 +03:00
|
|
|
// assert_eq!(backend.get_hash(6), None);
|
2018-03-13 21:22:34 +03:00
|
|
|
assert_eq!(backend.get_from_file(6), Some(pos_6_hash));
|
|
|
|
|
2018-06-18 18:18:38 +03:00
|
|
|
// assert_eq!(backend.get_hash(7), None);
|
2018-03-13 21:22:34 +03:00
|
|
|
assert_eq!(backend.get_from_file(7), Some(pos_7_hash));
|
|
|
|
|
2018-03-24 02:33:59 +03:00
|
|
|
assert_eq!(backend.get_hash(8), Some(pos_8_hash));
|
|
|
|
assert_eq!(backend.get_data(8), Some(pos_8));
|
2018-03-13 21:22:34 +03:00
|
|
|
assert_eq!(backend.get_from_file(8), Some(pos_8_hash));
|
|
|
|
|
2018-03-24 02:33:59 +03:00
|
|
|
assert_eq!(backend.get_hash(11), Some(pos_11_hash));
|
|
|
|
assert_eq!(backend.get_data(11), Some(pos_11));
|
2018-03-13 21:22:34 +03:00
|
|
|
assert_eq!(backend.get_from_file(11), Some(pos_11_hash));
|
|
|
|
}
|
|
|
|
|
2018-03-01 00:15:29 +03:00
|
|
|
// compact
|
2018-06-18 18:18:38 +03:00
|
|
|
backend
|
2018-07-08 19:37:09 +03:00
|
|
|
.check_compact(4, &Bitmap::of(&vec![1, 2]), &prune_noop)
|
2018-06-18 18:18:38 +03:00
|
|
|
.unwrap();
|
2018-03-13 21:22:34 +03:00
|
|
|
backend.sync().unwrap();
|
|
|
|
|
|
|
|
// check we can read a hash by pos correctly after compaction
|
|
|
|
{
|
2018-06-18 18:18:38 +03:00
|
|
|
assert_eq!(backend.get_hash(1), None);
|
|
|
|
assert_eq!(backend.get_from_file(1), Some(pos_1_hash));
|
2018-03-13 21:22:34 +03:00
|
|
|
|
2018-06-18 18:18:38 +03:00
|
|
|
assert_eq!(backend.get_hash(2), None);
|
|
|
|
assert_eq!(backend.get_from_file(2), Some(pos_2_hash));
|
|
|
|
|
|
|
|
assert_eq!(backend.get_hash(3), Some(pos_3_hash));
|
|
|
|
|
|
|
|
assert_eq!(backend.get_hash(4), None);
|
|
|
|
assert_eq!(backend.get_hash(5), None);
|
|
|
|
assert_eq!(backend.get_hash(6), Some(pos_6_hash));
|
2018-03-13 21:22:34 +03:00
|
|
|
|
|
|
|
assert_eq!(backend.get_from_file(7), Some(pos_7_hash));
|
|
|
|
|
2018-03-24 02:33:59 +03:00
|
|
|
assert_eq!(backend.get_hash(8), Some(pos_8_hash));
|
2018-03-13 21:22:34 +03:00
|
|
|
assert_eq!(backend.get_from_file(8), Some(pos_8_hash));
|
|
|
|
}
|
2018-03-01 00:15:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// recheck stored data
|
|
|
|
{
|
|
|
|
// recreate backend
|
2018-06-18 18:18:38 +03:00
|
|
|
let backend =
|
2018-07-05 05:31:08 +03:00
|
|
|
store::pmmr::PMMRBackend::<TestElem>::new(data_dir.to_string(), true, None).unwrap();
|
2018-03-13 21:22:34 +03:00
|
|
|
|
2018-11-05 15:01:24 +03:00
|
|
|
assert_eq!(backend.data_size(), 19);
|
|
|
|
assert_eq!(backend.hash_size(), 35);
|
2018-03-13 21:22:34 +03:00
|
|
|
|
|
|
|
// check we can read a hash by pos correctly from recreated backend
|
2018-06-18 18:18:38 +03:00
|
|
|
assert_eq!(backend.get_hash(7), Some(pos_7_hash));
|
2018-03-13 21:22:34 +03:00
|
|
|
assert_eq!(backend.get_from_file(7), Some(pos_7_hash));
|
|
|
|
|
2018-03-24 02:33:59 +03:00
|
|
|
assert_eq!(backend.get_hash(8), Some(pos_8_hash));
|
2018-03-13 21:22:34 +03:00
|
|
|
assert_eq!(backend.get_from_file(8), Some(pos_8_hash));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-06-18 18:18:38 +03:00
|
|
|
let mut backend =
|
2018-07-05 05:31:08 +03:00
|
|
|
store::pmmr::PMMRBackend::<TestElem>::new(data_dir.to_string(), true, None).unwrap();
|
2018-03-13 21:22:34 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
let mut pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
|
|
|
|
2018-06-18 18:18:38 +03:00
|
|
|
pmmr.prune(8).unwrap();
|
|
|
|
pmmr.prune(9).unwrap();
|
2018-03-13 21:22:34 +03:00
|
|
|
}
|
2018-03-04 03:19:54 +03:00
|
|
|
|
2018-03-01 00:15:29 +03:00
|
|
|
// compact some more
|
2018-06-18 18:18:38 +03:00
|
|
|
backend
|
2018-07-08 19:37:09 +03:00
|
|
|
.check_compact(9, &Bitmap::create(), &prune_noop)
|
2018-06-18 18:18:38 +03:00
|
|
|
.unwrap();
|
2018-03-01 00:15:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// recheck stored data
|
|
|
|
{
|
|
|
|
// recreate backend
|
2018-06-18 18:18:38 +03:00
|
|
|
let backend =
|
2018-07-05 05:31:08 +03:00
|
|
|
store::pmmr::PMMRBackend::<TestElem>::new(data_dir.to_string(), true, None).unwrap();
|
2018-03-13 21:22:34 +03:00
|
|
|
|
|
|
|
// 0010012001001230
|
|
|
|
|
2018-11-05 15:01:24 +03:00
|
|
|
assert_eq!(backend.data_size(), 13);
|
|
|
|
assert_eq!(backend.hash_size(), 27);
|
2018-03-13 21:22:34 +03:00
|
|
|
|
|
|
|
// check we can read a hash by pos correctly from recreated backend
|
2018-06-18 18:18:38 +03:00
|
|
|
// get_hash() and get_from_file() should return the same value
|
2018-10-31 14:16:27 +03:00
|
|
|
// and we only store leaves in the leaf_set so pos 7 still has a hash in there
|
2018-06-18 18:18:38 +03:00
|
|
|
assert_eq!(backend.get_hash(7), Some(pos_7_hash));
|
2018-03-13 21:22:34 +03:00
|
|
|
assert_eq!(backend.get_from_file(7), Some(pos_7_hash));
|
|
|
|
|
2018-03-24 02:33:59 +03:00
|
|
|
assert_eq!(backend.get_hash(11), Some(pos_11_hash));
|
|
|
|
assert_eq!(backend.get_data(11), Some(pos_11));
|
2018-03-13 21:22:34 +03:00
|
|
|
assert_eq!(backend.get_from_file(11), Some(pos_11_hash));
|
2018-03-01 00:15:29 +03:00
|
|
|
}
|
|
|
|
|
2018-06-07 00:46:21 +03:00
|
|
|
teardown(data_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn compact_twice() {
|
|
|
|
let (data_dir, elems) = setup("compact_twice");
|
|
|
|
|
|
|
|
// setup the mmr store with all elements
|
2018-07-05 05:31:08 +03:00
|
|
|
let mut backend = store::pmmr::PMMRBackend::new(data_dir.to_string(), true, None).unwrap();
|
2018-06-07 00:46:21 +03:00
|
|
|
let mmr_size = load(0, &elems[..], &mut backend);
|
|
|
|
backend.sync().unwrap();
|
|
|
|
|
|
|
|
// save the root
|
|
|
|
let root = {
|
|
|
|
let pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
|
|
|
pmmr.root()
|
|
|
|
};
|
|
|
|
|
|
|
|
// pruning some choice nodes
|
|
|
|
{
|
|
|
|
let mut pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
2018-06-18 18:18:38 +03:00
|
|
|
pmmr.prune(1).unwrap();
|
|
|
|
pmmr.prune(2).unwrap();
|
|
|
|
pmmr.prune(4).unwrap();
|
2018-06-07 00:46:21 +03:00
|
|
|
}
|
|
|
|
backend.sync().unwrap();
|
|
|
|
|
|
|
|
// check the root and stored data
|
|
|
|
{
|
|
|
|
let pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
|
|
|
assert_eq!(root, pmmr.root());
|
|
|
|
assert_eq!(pmmr.get_data(5).unwrap(), TestElem(4));
|
|
|
|
assert_eq!(pmmr.get_data(11).unwrap(), TestElem(7));
|
|
|
|
}
|
|
|
|
|
|
|
|
// compact
|
2018-06-18 18:18:38 +03:00
|
|
|
backend
|
2018-07-08 19:37:09 +03:00
|
|
|
.check_compact(2, &Bitmap::create(), &prune_noop)
|
2018-06-18 18:18:38 +03:00
|
|
|
.unwrap();
|
2018-06-07 00:46:21 +03:00
|
|
|
|
|
|
|
// recheck the root and stored data
|
|
|
|
{
|
|
|
|
let pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
|
|
|
assert_eq!(root, pmmr.root());
|
|
|
|
assert_eq!(pmmr.get_data(5).unwrap(), TestElem(4));
|
|
|
|
assert_eq!(pmmr.get_data(11).unwrap(), TestElem(7));
|
|
|
|
}
|
|
|
|
|
|
|
|
// now prune some more nodes
|
|
|
|
{
|
|
|
|
let mut pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
2018-06-18 18:18:38 +03:00
|
|
|
pmmr.prune(5).unwrap();
|
|
|
|
pmmr.prune(8).unwrap();
|
|
|
|
pmmr.prune(9).unwrap();
|
2018-06-07 00:46:21 +03:00
|
|
|
}
|
|
|
|
backend.sync().unwrap();
|
|
|
|
|
|
|
|
// recheck the root and stored data
|
|
|
|
{
|
|
|
|
let pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
|
|
|
assert_eq!(root, pmmr.root());
|
|
|
|
assert_eq!(pmmr.get_data(11).unwrap(), TestElem(7));
|
|
|
|
}
|
|
|
|
|
|
|
|
// compact
|
2018-06-18 18:18:38 +03:00
|
|
|
backend
|
2018-07-08 19:37:09 +03:00
|
|
|
.check_compact(2, &Bitmap::create(), &prune_noop)
|
2018-06-18 18:18:38 +03:00
|
|
|
.unwrap();
|
2018-06-07 00:46:21 +03:00
|
|
|
|
|
|
|
// recheck the root and stored data
|
|
|
|
{
|
|
|
|
let pmmr: PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
|
|
|
assert_eq!(root, pmmr.root());
|
|
|
|
assert_eq!(pmmr.get_data(11).unwrap(), TestElem(7));
|
|
|
|
}
|
|
|
|
|
2018-03-01 00:15:29 +03:00
|
|
|
teardown(data_dir);
|
|
|
|
}
|
|
|
|
|
2018-01-28 09:12:33 +03:00
|
|
|
fn setup(tag: &str) -> (String, Vec<TestElem>) {
|
2018-06-15 01:59:53 +03:00
|
|
|
match env_logger::try_init() {
|
|
|
|
Ok(_) => println!("Initializing env logger"),
|
|
|
|
Err(e) => println!("env logger already initialized: {:?}", e),
|
|
|
|
};
|
2018-07-30 11:33:28 +03:00
|
|
|
let t = Utc::now();
|
2018-10-18 13:40:58 +03:00
|
|
|
let data_dir = format!(
|
|
|
|
"./target/tmp/{}.{}-{}",
|
|
|
|
t.timestamp(),
|
|
|
|
t.timestamp_subsec_nanos(),
|
|
|
|
tag
|
|
|
|
);
|
2017-09-05 08:50:25 +03:00
|
|
|
fs::create_dir_all(data_dir.clone()).unwrap();
|
|
|
|
|
2018-03-13 21:22:34 +03:00
|
|
|
let mut elems = vec![];
|
|
|
|
for x in 1..20 {
|
|
|
|
elems.push(TestElem(x));
|
|
|
|
}
|
2017-09-05 08:50:25 +03:00
|
|
|
(data_dir, elems)
|
|
|
|
}
|
|
|
|
|
2018-01-28 09:12:33 +03:00
|
|
|
fn teardown(data_dir: String) {
|
|
|
|
fs::remove_dir_all(data_dir).unwrap();
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:45:13 +03:00
|
|
|
fn load(pos: u64, elems: &[TestElem], backend: &mut store::pmmr::PMMRBackend<TestElem>) -> u64 {
|
2017-09-05 08:50:25 +03:00
|
|
|
let mut pmmr = PMMR::at(backend, pos);
|
|
|
|
for elem in elems {
|
2018-12-02 22:59:24 +03:00
|
|
|
pmmr.push(elem).unwrap();
|
2017-09-05 08:50:25 +03:00
|
|
|
}
|
|
|
|
pmmr.unpruned_size()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
2018-03-13 21:22:34 +03:00
|
|
|
struct TestElem(u32);
|
2018-02-22 16:45:13 +03:00
|
|
|
|
2018-11-01 23:14:46 +03:00
|
|
|
impl FixedLength for TestElem {
|
|
|
|
const LEN: usize = 4;
|
2017-09-05 08:50:25 +03:00
|
|
|
}
|
|
|
|
|
2018-11-27 15:32:39 +03:00
|
|
|
impl PMMRable for TestElem {
|
|
|
|
type E = Self;
|
|
|
|
|
2018-12-02 22:59:24 +03:00
|
|
|
fn as_elmt(&self) -> Self::E {
|
|
|
|
self.clone()
|
2018-11-27 15:32:39 +03:00
|
|
|
}
|
|
|
|
}
|
2018-11-01 23:14:46 +03:00
|
|
|
|
2017-09-05 08:50:25 +03:00
|
|
|
impl Writeable for TestElem {
|
|
|
|
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {
|
2018-03-13 21:22:34 +03:00
|
|
|
writer.write_u32(self.0)
|
2017-09-05 08:50:25 +03:00
|
|
|
}
|
|
|
|
}
|
2018-02-22 16:45:13 +03:00
|
|
|
impl Readable for TestElem {
|
|
|
|
fn read(reader: &mut Reader) -> Result<TestElem, Error> {
|
2018-03-13 21:22:34 +03:00
|
|
|
Ok(TestElem(reader.read_u32()?))
|
2018-02-22 16:45:13 +03:00
|
|
|
}
|
|
|
|
}
|