2017-09-05 08:50:25 +03:00
|
|
|
// Copyright 2017 The Grin Developers
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
extern crate env_logger;
|
|
|
|
extern crate grin_core as core;
|
|
|
|
extern crate grin_store as store;
|
|
|
|
extern crate time;
|
|
|
|
|
|
|
|
use std::fs;
|
|
|
|
|
|
|
|
use core::ser::*;
|
2018-02-22 16:45:13 +03:00
|
|
|
use core::core::pmmr::{PMMR, Backend};
|
|
|
|
use core::core::hash::{Hash, Hashed};
|
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-03-03 12:08:36 +03:00
|
|
|
let mut backend = store::pmmr::PMMRBackend::new(data_dir.to_string(), 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-02-22 16:45:13 +03:00
|
|
|
let node_hash = elems[0].hash();
|
2017-09-05 08:50:25 +03:00
|
|
|
assert_eq!(
|
2018-02-22 16:45:13 +03:00
|
|
|
backend.get(1, false).expect("").0,
|
|
|
|
node_hash
|
2017-09-05 08:50:25 +03:00
|
|
|
);
|
|
|
|
|
2018-02-22 16:45:13 +03:00
|
|
|
let sum2 = elems[0].hash() + elems[1].hash();
|
2017-11-01 02:32:33 +03:00
|
|
|
let sum4 = sum2
|
2018-02-22 16:45:13 +03:00
|
|
|
+ (elems[2].hash() + elems[3].hash());
|
2017-11-01 02:32:33 +03:00
|
|
|
let sum8 = sum4
|
2018-02-22 16:45:13 +03:00
|
|
|
+ ((elems[4].hash() + elems[5].hash())
|
|
|
|
+ (elems[6].hash() + elems[7].hash()));
|
|
|
|
let sum9 = sum8 + elems[8].hash();
|
2017-09-05 08:50:25 +03:00
|
|
|
|
|
|
|
{
|
2018-02-22 16:45:13 +03:00
|
|
|
let pmmr:PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
2017-09-05 08:50:25 +03:00
|
|
|
assert_eq!(pmmr.root(), sum9);
|
|
|
|
}
|
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_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-03-03 12:08:36 +03:00
|
|
|
let mut backend = store::pmmr::PMMRBackend::new(data_dir.to_string(), 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-02-22 16:45:13 +03:00
|
|
|
let root: Hash;
|
2017-09-05 08:50:25 +03:00
|
|
|
{
|
2018-02-22 16:45:13 +03:00
|
|
|
let pmmr:PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
2017-09-05 08:50:25 +03:00
|
|
|
root = pmmr.root();
|
|
|
|
}
|
|
|
|
|
|
|
|
// pruning some choice nodes
|
|
|
|
{
|
2018-02-22 16:45:13 +03:00
|
|
|
let mut pmmr:PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
2017-09-29 21:09:44 +03:00
|
|
|
pmmr.prune(1, 1).unwrap();
|
|
|
|
pmmr.prune(4, 1).unwrap();
|
|
|
|
pmmr.prune(5, 1).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-02-22 16:45:13 +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
|
|
|
|
assert_eq!(pmmr.get(2, true).unwrap().1.unwrap(), TestElem([0, 0, 0, 2]));
|
2017-09-05 08:50:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// compact
|
2018-03-01 00:15:29 +03:00
|
|
|
backend.check_compact(2, 2).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-02-22 16:45:13 +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
|
|
|
assert_eq!(pmmr.get(2, true).unwrap().1.unwrap(), TestElem([0, 0, 0, 2]));
|
|
|
|
assert_eq!(pmmr.get(11, true).unwrap().1.unwrap(), TestElem([0, 0, 0, 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
|
|
|
|
|
|
|
// set everything up with a first backend
|
|
|
|
let mmr_size: u64;
|
2018-02-22 16:45:13 +03:00
|
|
|
let root: Hash;
|
2017-09-05 08:50:25 +03:00
|
|
|
{
|
2018-03-03 12:08:36 +03:00
|
|
|
let mut backend = store::pmmr::PMMRBackend::new(data_dir.to_string(), None).unwrap();
|
2017-09-05 08:50:25 +03:00
|
|
|
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 and prune some nodes so we have prune data
|
|
|
|
{
|
2018-02-22 16:45:13 +03:00
|
|
|
let mut pmmr:PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
|
|
|
pmmr.dump(false);
|
2017-09-05 08:50:25 +03:00
|
|
|
root = pmmr.root();
|
2017-09-29 21:09:44 +03:00
|
|
|
pmmr.prune(1, 1).unwrap();
|
|
|
|
pmmr.prune(4, 1).unwrap();
|
2017-09-05 08:50:25 +03:00
|
|
|
}
|
|
|
|
backend.sync().unwrap();
|
2018-02-22 16:45:13 +03:00
|
|
|
|
2018-03-01 00:15:29 +03:00
|
|
|
backend.check_compact(1, 2).unwrap();
|
2017-09-05 08:50:25 +03:00
|
|
|
backend.sync().unwrap();
|
2018-01-28 09:12:33 +03:00
|
|
|
assert_eq!(backend.unpruned_size().unwrap(), mmr_size);
|
2017-09-05 08:50:25 +03:00
|
|
|
|
|
|
|
// prune some more to get rm log data
|
|
|
|
{
|
2018-02-22 16:45:13 +03:00
|
|
|
let mut pmmr:PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
2017-09-29 21:09:44 +03:00
|
|
|
pmmr.prune(5, 1).unwrap();
|
2017-09-05 08:50:25 +03:00
|
|
|
}
|
|
|
|
backend.sync().unwrap();
|
2018-01-28 09:12:33 +03:00
|
|
|
assert_eq!(backend.unpruned_size().unwrap(), mmr_size);
|
2017-09-05 08:50:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// create a new backend and check everything is kosher
|
|
|
|
{
|
2018-02-22 16:45:13 +03:00
|
|
|
let mut backend:store::pmmr::PMMRBackend<TestElem> =
|
2018-03-03 12:08:36 +03:00
|
|
|
store::pmmr::PMMRBackend::new(data_dir.to_string(), None).unwrap();
|
2018-01-28 09:12:33 +03:00
|
|
|
assert_eq!(backend.unpruned_size().unwrap(), mmr_size);
|
2017-09-05 08:50:25 +03:00
|
|
|
{
|
2018-02-22 16:45:13 +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
|
|
|
assert_eq!(backend.get(5, false), None);
|
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-03-03 12:08:36 +03:00
|
|
|
let mut backend = store::pmmr::PMMRBackend::new(data_dir.clone(), 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-02-22 16:45:13 +03:00
|
|
|
let root1: Hash;
|
2017-10-03 01:32:23 +03:00
|
|
|
{
|
2018-02-22 16:45:13 +03:00
|
|
|
let pmmr:PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
2017-10-03 01:32:23 +03:00
|
|
|
root1 = pmmr.root();
|
|
|
|
}
|
|
|
|
|
|
|
|
mmr_size = load(mmr_size, &elems[4..6], &mut backend);
|
|
|
|
backend.sync().unwrap();
|
2018-02-22 16:45:13 +03:00
|
|
|
let root2: Hash;
|
2017-10-03 01:32:23 +03:00
|
|
|
{
|
2018-02-22 16:45:13 +03:00
|
|
|
let pmmr:PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
2017-10-03 01:32:23 +03:00
|
|
|
root2 = pmmr.root();
|
|
|
|
}
|
|
|
|
|
|
|
|
mmr_size = load(mmr_size, &elems[6..9], &mut backend);
|
|
|
|
backend.sync().unwrap();
|
|
|
|
|
|
|
|
// prune and compact the 2 first elements to spice things up
|
|
|
|
{
|
2018-02-22 16:45:13 +03:00
|
|
|
let mut pmmr:PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
2017-10-03 01:32:23 +03:00
|
|
|
pmmr.prune(1, 1).unwrap();
|
|
|
|
pmmr.prune(2, 1).unwrap();
|
|
|
|
}
|
2018-03-01 00:15:29 +03:00
|
|
|
backend.check_compact(1, 2).unwrap();
|
2017-10-03 01:32:23 +03:00
|
|
|
backend.sync().unwrap();
|
2017-11-01 02:32:33 +03:00
|
|
|
|
2017-10-03 01:32:23 +03:00
|
|
|
// rewind and check the roots still match
|
|
|
|
{
|
2018-02-22 16:45:13 +03:00
|
|
|
let mut pmmr:PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
2017-10-03 01:32:23 +03:00
|
|
|
pmmr.rewind(9, 3).unwrap();
|
|
|
|
assert_eq!(pmmr.root(), root2);
|
|
|
|
}
|
|
|
|
backend.sync().unwrap();
|
|
|
|
{
|
2018-02-22 16:45:13 +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-02-22 16:45:13 +03:00
|
|
|
let mut pmmr:PMMR<TestElem, _> = PMMR::at(&mut backend, 10);
|
2017-10-03 01:32:23 +03:00
|
|
|
pmmr.rewind(5, 3).unwrap();
|
|
|
|
assert_eq!(pmmr.root(), root1);
|
|
|
|
}
|
|
|
|
backend.sync().unwrap();
|
|
|
|
{
|
2018-02-22 16:45:13 +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
|
|
|
|
|
|
|
teardown(data_dir);
|
2017-10-03 01:32:23 +03:00
|
|
|
}
|
|
|
|
|
2018-03-01 00:15:29 +03:00
|
|
|
#[test]
|
|
|
|
fn pmmr_compact_horizon() {
|
|
|
|
let (data_dir, elems) = setup("compact_horizon");
|
|
|
|
|
|
|
|
let root: Hash;
|
|
|
|
{
|
|
|
|
// setup the mmr store with all elements
|
2018-03-03 12:08:36 +03:00
|
|
|
let mut backend = store::pmmr::PMMRBackend::new(data_dir.to_string(), None).unwrap();
|
2018-03-01 00:15:29 +03:00
|
|
|
let mmr_size = load(0, &elems[..], &mut backend);
|
|
|
|
backend.sync().unwrap();
|
|
|
|
|
|
|
|
// save the root
|
|
|
|
{
|
|
|
|
let pmmr:PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
|
|
|
root = pmmr.root();
|
|
|
|
}
|
|
|
|
|
|
|
|
// pruning some choice nodes with an increasing block height
|
|
|
|
{
|
|
|
|
let mut pmmr:PMMR<TestElem, _> = PMMR::at(&mut backend, mmr_size);
|
|
|
|
pmmr.prune(1, 1).unwrap();
|
|
|
|
pmmr.prune(2, 2).unwrap();
|
|
|
|
pmmr.prune(4, 3).unwrap();
|
|
|
|
pmmr.prune(5, 4).unwrap();
|
|
|
|
}
|
|
|
|
backend.sync().unwrap();
|
|
|
|
// compact
|
|
|
|
backend.check_compact(2, 3).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
// recheck stored data
|
|
|
|
{
|
|
|
|
// recreate backend
|
2018-03-03 12:08:36 +03:00
|
|
|
let mut backend = store::pmmr::PMMRBackend::<TestElem>::new(data_dir.to_string(), None).unwrap();
|
2018-03-01 00:15:29 +03:00
|
|
|
// 9 elements total, minus 2 compacted
|
|
|
|
assert_eq!(backend.data_size().unwrap(), 7);
|
|
|
|
// 15 nodes total, 2 pruned and compacted
|
|
|
|
assert_eq!(backend.hash_size().unwrap(), 13);
|
|
|
|
|
|
|
|
// compact some more
|
|
|
|
backend.check_compact(1, 5).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
// recheck stored data
|
|
|
|
{
|
|
|
|
// recreate backend
|
2018-03-03 12:08:36 +03:00
|
|
|
let backend = store::pmmr::PMMRBackend::<TestElem>::new(data_dir.to_string(), None).unwrap();
|
2018-03-01 00:15:29 +03:00
|
|
|
// 9 elements total, minus 4 compacted
|
|
|
|
assert_eq!(backend.data_size().unwrap(), 5);
|
|
|
|
// 15 nodes total, 6 pruned and compacted
|
|
|
|
assert_eq!(backend.hash_size().unwrap(), 9);
|
|
|
|
}
|
|
|
|
|
|
|
|
teardown(data_dir);
|
|
|
|
}
|
|
|
|
|
2018-01-28 09:12:33 +03:00
|
|
|
fn setup(tag: &str) -> (String, Vec<TestElem>) {
|
2017-09-05 08:50:25 +03:00
|
|
|
let _ = env_logger::init();
|
|
|
|
let t = time::get_time();
|
2018-01-28 09:12:33 +03:00
|
|
|
let data_dir = format!("./target/{}.{}-{}", t.sec, t.nsec, tag);
|
2017-09-05 08:50:25 +03:00
|
|
|
fs::create_dir_all(data_dir.clone()).unwrap();
|
|
|
|
|
|
|
|
let elems = vec![
|
|
|
|
TestElem([0, 0, 0, 1]),
|
|
|
|
TestElem([0, 0, 0, 2]),
|
|
|
|
TestElem([0, 0, 0, 3]),
|
|
|
|
TestElem([0, 0, 0, 4]),
|
|
|
|
TestElem([0, 0, 0, 5]),
|
|
|
|
TestElem([0, 0, 0, 6]),
|
|
|
|
TestElem([0, 0, 0, 7]),
|
|
|
|
TestElem([0, 0, 0, 8]),
|
|
|
|
TestElem([1, 0, 0, 0]),
|
|
|
|
];
|
|
|
|
(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-01-04 21:38:46 +03:00
|
|
|
pmmr.push(elem.clone()).unwrap();
|
2017-09-05 08:50:25 +03:00
|
|
|
}
|
|
|
|
pmmr.unpruned_size()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
struct TestElem([u32; 4]);
|
2018-02-22 16:45:13 +03:00
|
|
|
|
|
|
|
impl PMMRable for TestElem {
|
|
|
|
fn len() -> usize {
|
|
|
|
16
|
2017-09-05 08:50:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Writeable for TestElem {
|
|
|
|
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {
|
|
|
|
try!(writer.write_u32(self.0[0]));
|
|
|
|
try!(writer.write_u32(self.0[1]));
|
|
|
|
try!(writer.write_u32(self.0[2]));
|
|
|
|
writer.write_u32(self.0[3])
|
|
|
|
}
|
|
|
|
}
|
2018-02-22 16:45:13 +03:00
|
|
|
impl Readable for TestElem {
|
|
|
|
fn read(reader: &mut Reader) -> Result<TestElem, Error> {
|
|
|
|
Ok(TestElem (
|
|
|
|
[
|
|
|
|
reader.read_u32()?,
|
|
|
|
reader.read_u32()?,
|
|
|
|
reader.read_u32()?,
|
|
|
|
reader.read_u32()?,
|
|
|
|
]
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|