2017-11-22 23:14:42 +03:00
|
|
|
|
2017-09-28 02:46:32 +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.
|
|
|
|
|
|
|
|
//! Utility structs to handle the 3 sumtrees (utxo, range proof, kernel) more
|
|
|
|
//! conveniently and transactionally.
|
|
|
|
|
|
|
|
use std::fs;
|
|
|
|
use std::collections::HashMap;
|
2018-02-10 01:32:16 +03:00
|
|
|
use std::fs::File;
|
|
|
|
use std::ops::Deref;
|
|
|
|
use std::path::{Path, PathBuf};
|
2017-09-28 02:46:32 +03:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2018-02-17 20:56:22 +03:00
|
|
|
use util::static_secp_instance;
|
2018-02-10 01:32:16 +03:00
|
|
|
use util::secp::pedersen::{RangeProof, Commitment};
|
|
|
|
|
|
|
|
use core::consensus::reward;
|
|
|
|
use core::core::{Block, BlockHeader, SumCommit, Input, Output, OutputIdentifier, OutputFeatures, TxKernel};
|
|
|
|
use core::core::pmmr::{self, HashSum, NoSum, Summable, PMMR};
|
2017-11-15 23:37:40 +03:00
|
|
|
use core::core::hash::Hashed;
|
2018-02-17 20:56:22 +03:00
|
|
|
use core::ser;
|
2017-09-28 02:46:32 +03:00
|
|
|
use grin_store;
|
2018-02-10 01:32:16 +03:00
|
|
|
use grin_store::sumtree::{PMMRBackend, AppendOnlyFile};
|
2017-09-28 02:46:32 +03:00
|
|
|
use types::ChainStore;
|
|
|
|
use types::Error;
|
2018-02-10 01:32:16 +03:00
|
|
|
use util::{LOGGER, zip};
|
2017-09-28 02:46:32 +03:00
|
|
|
|
|
|
|
const SUMTREES_SUBDIR: &'static str = "sumtrees";
|
|
|
|
const UTXO_SUBDIR: &'static str = "utxo";
|
|
|
|
const RANGE_PROOF_SUBDIR: &'static str = "rangeproof";
|
|
|
|
const KERNEL_SUBDIR: &'static str = "kernel";
|
2018-02-10 01:32:16 +03:00
|
|
|
const KERNEL_FILE: &'static str = "kernel_full_data.bin";
|
|
|
|
const SUMTREES_ZIP: &'static str = "sumtrees_snapshot.zip";
|
2017-09-28 02:46:32 +03:00
|
|
|
|
2017-09-29 21:44:25 +03:00
|
|
|
struct PMMRHandle<T>
|
2017-10-17 00:23:10 +03:00
|
|
|
where
|
|
|
|
T: Summable + Clone,
|
2017-09-29 21:44:25 +03:00
|
|
|
{
|
2017-09-28 02:46:32 +03:00
|
|
|
backend: PMMRBackend<T>,
|
|
|
|
last_pos: u64,
|
|
|
|
}
|
|
|
|
|
2017-09-29 21:44:25 +03:00
|
|
|
impl<T> PMMRHandle<T>
|
2017-10-17 00:23:10 +03:00
|
|
|
where
|
|
|
|
T: Summable + Clone,
|
2017-09-29 21:44:25 +03:00
|
|
|
{
|
2017-09-28 02:46:32 +03:00
|
|
|
fn new(root_dir: String, file_name: &str) -> Result<PMMRHandle<T>, Error> {
|
|
|
|
let path = Path::new(&root_dir).join(SUMTREES_SUBDIR).join(file_name);
|
|
|
|
fs::create_dir_all(path.clone())?;
|
|
|
|
let be = PMMRBackend::new(path.to_str().unwrap().to_string())?;
|
|
|
|
let sz = be.unpruned_size()?;
|
|
|
|
Ok(PMMRHandle {
|
|
|
|
backend: be,
|
|
|
|
last_pos: sz,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An easy to manipulate structure holding the 3 sum trees necessary to
|
|
|
|
/// validate blocks and capturing the UTXO set, the range proofs and the
|
|
|
|
/// kernels. Also handles the index of Commitments to positions in the
|
|
|
|
/// output and range proof sum trees.
|
|
|
|
///
|
|
|
|
/// Note that the index is never authoritative, only the trees are
|
|
|
|
/// guaranteed to indicate whether an output is spent or not. The index
|
|
|
|
/// may have commitments that have already been spent, even with
|
|
|
|
/// pruning enabled.
|
2018-02-10 01:32:16 +03:00
|
|
|
///
|
|
|
|
/// In addition of the sumtrees, this maintains the full list of kernel
|
|
|
|
/// data so it can be easily packaged for sync or validation.
|
2017-09-28 02:46:32 +03:00
|
|
|
pub struct SumTrees {
|
|
|
|
output_pmmr_h: PMMRHandle<SumCommit>,
|
|
|
|
rproof_pmmr_h: PMMRHandle<NoSum<RangeProof>>,
|
|
|
|
kernel_pmmr_h: PMMRHandle<NoSum<TxKernel>>,
|
2018-02-10 01:32:16 +03:00
|
|
|
kernel_file: AppendOnlyFile,
|
2017-09-28 02:46:32 +03:00
|
|
|
|
|
|
|
// chain store used as index of commitments to MMR positions
|
|
|
|
commit_index: Arc<ChainStore>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SumTrees {
|
|
|
|
/// Open an existing or new set of backends for the SumTrees
|
|
|
|
pub fn open(root_dir: String, commit_index: Arc<ChainStore>) -> Result<SumTrees, Error> {
|
2018-02-10 01:32:16 +03:00
|
|
|
let mut kernel_file_path: PathBuf = [&root_dir, SUMTREES_SUBDIR, KERNEL_SUBDIR].iter().collect();
|
|
|
|
fs::create_dir_all(kernel_file_path.clone())?;
|
|
|
|
kernel_file_path.push(KERNEL_FILE);
|
|
|
|
let kernel_file = AppendOnlyFile::open(kernel_file_path.to_str().unwrap().to_owned())?;
|
|
|
|
|
2017-09-28 02:46:32 +03:00
|
|
|
Ok(SumTrees {
|
|
|
|
output_pmmr_h: PMMRHandle::new(root_dir.clone(), UTXO_SUBDIR)?,
|
|
|
|
rproof_pmmr_h: PMMRHandle::new(root_dir.clone(), RANGE_PROOF_SUBDIR)?,
|
|
|
|
kernel_pmmr_h: PMMRHandle::new(root_dir.clone(), KERNEL_SUBDIR)?,
|
2018-02-10 01:32:16 +03:00
|
|
|
kernel_file: kernel_file,
|
2017-09-28 02:46:32 +03:00
|
|
|
commit_index: commit_index,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-01-17 06:03:40 +03:00
|
|
|
/// Check is an output is unspent.
|
|
|
|
/// We look in the index to find the output MMR pos.
|
|
|
|
/// Then we check the entry in the output MMR and confirm the hash matches.
|
|
|
|
pub fn is_unspent(&mut self, output: &OutputIdentifier) -> Result<(), Error> {
|
|
|
|
match self.commit_index.get_output_pos(&output.commit) {
|
2017-11-22 23:14:42 +03:00
|
|
|
Ok(pos) => {
|
2017-12-25 03:07:50 +03:00
|
|
|
let output_pmmr = PMMR::at(
|
|
|
|
&mut self.output_pmmr_h.backend,
|
2018-01-17 06:03:40 +03:00
|
|
|
self.output_pmmr_h.last_pos,
|
2017-12-25 03:07:50 +03:00
|
|
|
);
|
2018-01-17 06:03:40 +03:00
|
|
|
if let Some(HashSum { hash, sum: _ }) = output_pmmr.get(pos) {
|
|
|
|
let sum_commit = output.as_sum_commit();
|
|
|
|
let hash_sum = HashSum::from_summable(pos, &sum_commit);
|
|
|
|
if hash == hash_sum.hash {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(Error::SumTreeErr(format!("sumtree hash mismatch")))
|
|
|
|
}
|
2017-11-22 23:14:42 +03:00
|
|
|
} else {
|
2018-01-17 06:03:40 +03:00
|
|
|
Err(Error::OutputNotFound)
|
2017-11-22 23:14:42 +03:00
|
|
|
}
|
|
|
|
}
|
2018-01-17 06:03:40 +03:00
|
|
|
Err(grin_store::Error::NotFoundErr) => Err(Error::OutputNotFound),
|
|
|
|
Err(e) => Err(Error::StoreErr(e, format!("sumtree unspent check"))),
|
2017-09-28 02:46:32 +03:00
|
|
|
}
|
|
|
|
}
|
2017-10-24 21:11:58 +03:00
|
|
|
|
2018-01-17 06:03:40 +03:00
|
|
|
/// Check the output being spent by the input has sufficiently matured.
|
|
|
|
/// This only applies for coinbase outputs being spent (1,000 blocks).
|
|
|
|
/// Non-coinbase outputs will always pass this check.
|
|
|
|
/// For a coinbase output we find the block by the block hash provided in the input
|
|
|
|
/// and check coinbase maturty based on the height of this block.
|
|
|
|
pub fn is_matured(
|
|
|
|
&mut self,
|
|
|
|
input: &Input,
|
|
|
|
height: u64,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
// We should never be in a situation where we are checking maturity rules
|
|
|
|
// if the output is already spent (this should have already been checked).
|
|
|
|
let output = OutputIdentifier::from_input(&input);
|
|
|
|
assert!(self.is_unspent(&output).is_ok());
|
|
|
|
|
|
|
|
// At this point we can be sure the input is spending the output
|
|
|
|
// it claims to be spending, and that it is coinbase or non-coinbase.
|
|
|
|
// If we are spending a coinbase output then go find the block
|
|
|
|
// and check the coinbase maturity rule is being met.
|
2018-02-05 22:43:54 +03:00
|
|
|
if input.features.contains(OutputFeatures::COINBASE_OUTPUT) {
|
2018-01-17 06:03:40 +03:00
|
|
|
let block_hash = &input.out_block
|
|
|
|
.expect("input spending coinbase output must have a block hash");
|
|
|
|
let block = self.commit_index.get_block(&block_hash)?;
|
|
|
|
block.verify_coinbase_maturity(&input, height)
|
|
|
|
.map_err(|_| Error::ImmatureCoinbase)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-10-28 00:57:04 +03:00
|
|
|
/// returns the last N nodes inserted into the tree (i.e. the 'bottom'
|
|
|
|
/// nodes at level 0
|
|
|
|
pub fn last_n_utxo(&mut self, distance: u64) -> Vec<HashSum<SumCommit>> {
|
|
|
|
let output_pmmr = PMMR::at(&mut self.output_pmmr_h.backend, self.output_pmmr_h.last_pos);
|
|
|
|
output_pmmr.get_last_n_insertions(distance)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// as above, for range proofs
|
|
|
|
pub fn last_n_rangeproof(&mut self, distance: u64) -> Vec<HashSum<NoSum<RangeProof>>> {
|
|
|
|
let rproof_pmmr = PMMR::at(&mut self.rproof_pmmr_h.backend, self.rproof_pmmr_h.last_pos);
|
|
|
|
rproof_pmmr.get_last_n_insertions(distance)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// as above, for kernels
|
|
|
|
pub fn last_n_kernel(&mut self, distance: u64) -> Vec<HashSum<NoSum<TxKernel>>> {
|
|
|
|
let kernel_pmmr = PMMR::at(&mut self.kernel_pmmr_h.backend, self.kernel_pmmr_h.last_pos);
|
|
|
|
kernel_pmmr.get_last_n_insertions(distance)
|
|
|
|
}
|
|
|
|
|
2018-02-10 01:32:16 +03:00
|
|
|
/// Output and kernel MMR indexes at the end of the provided block
|
|
|
|
pub fn indexes_at(&self, block: &Block) -> Result<(u64, u64), Error> {
|
|
|
|
indexes_at(block, self.commit_index.deref())
|
|
|
|
}
|
|
|
|
|
2017-10-24 21:11:58 +03:00
|
|
|
/// Get sum tree roots
|
|
|
|
pub fn roots(
|
|
|
|
&mut self,
|
2017-11-01 02:32:33 +03:00
|
|
|
) -> (
|
|
|
|
HashSum<SumCommit>,
|
|
|
|
HashSum<NoSum<RangeProof>>,
|
|
|
|
HashSum<NoSum<TxKernel>>,
|
|
|
|
) {
|
2017-10-28 00:57:04 +03:00
|
|
|
let output_pmmr = PMMR::at(&mut self.output_pmmr_h.backend, self.output_pmmr_h.last_pos);
|
|
|
|
let rproof_pmmr = PMMR::at(&mut self.rproof_pmmr_h.backend, self.rproof_pmmr_h.last_pos);
|
|
|
|
let kernel_pmmr = PMMR::at(&mut self.kernel_pmmr_h.backend, self.kernel_pmmr_h.last_pos);
|
|
|
|
(output_pmmr.root(), rproof_pmmr.root(), kernel_pmmr.root())
|
2017-10-24 21:11:58 +03:00
|
|
|
}
|
2017-09-28 02:46:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Starts a new unit of work to extend the chain with additional blocks,
|
|
|
|
/// accepting a closure that will work within that unit of work. The closure
|
|
|
|
/// has access to an Extension object that allows the addition of blocks to
|
|
|
|
/// the sumtrees and the checking of the current tree roots.
|
|
|
|
///
|
|
|
|
/// If the closure returns an error, modifications are canceled and the unit
|
|
|
|
/// of work is abandoned. Otherwise, the unit of work is permanently applied.
|
|
|
|
pub fn extending<'a, F, T>(trees: &'a mut SumTrees, inner: F) -> Result<T, Error>
|
2017-10-17 00:23:10 +03:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Extension) -> Result<T, Error>,
|
2017-09-29 21:44:25 +03:00
|
|
|
{
|
2017-09-28 02:46:32 +03:00
|
|
|
let sizes: (u64, u64, u64);
|
|
|
|
let res: Result<T, Error>;
|
|
|
|
let rollback: bool;
|
|
|
|
{
|
|
|
|
let commit_index = trees.commit_index.clone();
|
2018-02-10 01:32:16 +03:00
|
|
|
|
|
|
|
debug!(LOGGER, "Starting new sumtree extension.");
|
2017-09-28 02:46:32 +03:00
|
|
|
let mut extension = Extension::new(trees, commit_index);
|
|
|
|
res = inner(&mut extension);
|
2018-02-10 01:32:16 +03:00
|
|
|
|
2017-09-28 02:46:32 +03:00
|
|
|
rollback = extension.rollback;
|
|
|
|
if res.is_ok() && !rollback {
|
|
|
|
extension.save_pos_index()?;
|
|
|
|
}
|
|
|
|
sizes = extension.sizes();
|
|
|
|
}
|
|
|
|
match res {
|
|
|
|
Err(e) => {
|
2017-10-28 00:57:04 +03:00
|
|
|
debug!(LOGGER, "Error returned, discarding sumtree extension.");
|
2017-09-28 02:46:32 +03:00
|
|
|
trees.output_pmmr_h.backend.discard();
|
|
|
|
trees.rproof_pmmr_h.backend.discard();
|
|
|
|
trees.kernel_pmmr_h.backend.discard();
|
2018-02-10 01:32:16 +03:00
|
|
|
trees.kernel_file.discard();
|
2017-09-28 02:46:32 +03:00
|
|
|
Err(e)
|
|
|
|
}
|
|
|
|
Ok(r) => {
|
|
|
|
if rollback {
|
2017-10-28 00:57:04 +03:00
|
|
|
debug!(LOGGER, "Rollbacking sumtree extension.");
|
2017-09-28 02:46:32 +03:00
|
|
|
trees.output_pmmr_h.backend.discard();
|
|
|
|
trees.rproof_pmmr_h.backend.discard();
|
|
|
|
trees.kernel_pmmr_h.backend.discard();
|
2018-02-10 01:32:16 +03:00
|
|
|
trees.kernel_file.discard();
|
2017-09-28 02:46:32 +03:00
|
|
|
} else {
|
2017-10-28 00:57:04 +03:00
|
|
|
debug!(LOGGER, "Committing sumtree extension.");
|
2017-09-28 02:46:32 +03:00
|
|
|
trees.output_pmmr_h.backend.sync()?;
|
|
|
|
trees.rproof_pmmr_h.backend.sync()?;
|
|
|
|
trees.kernel_pmmr_h.backend.sync()?;
|
2018-02-10 01:32:16 +03:00
|
|
|
trees.kernel_file.flush()?;
|
2017-09-28 02:46:32 +03:00
|
|
|
trees.output_pmmr_h.last_pos = sizes.0;
|
|
|
|
trees.rproof_pmmr_h.last_pos = sizes.1;
|
|
|
|
trees.kernel_pmmr_h.last_pos = sizes.2;
|
|
|
|
}
|
|
|
|
|
2017-10-28 00:57:04 +03:00
|
|
|
debug!(LOGGER, "Sumtree extension done.");
|
2017-09-28 02:46:32 +03:00
|
|
|
Ok(r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Allows the application of new blocks on top of the sum trees in a
|
|
|
|
/// reversible manner within a unit of work provided by the `extending`
|
|
|
|
/// function.
|
|
|
|
pub struct Extension<'a> {
|
|
|
|
output_pmmr: PMMR<'a, SumCommit, PMMRBackend<SumCommit>>,
|
|
|
|
rproof_pmmr: PMMR<'a, NoSum<RangeProof>, PMMRBackend<NoSum<RangeProof>>>,
|
|
|
|
kernel_pmmr: PMMR<'a, NoSum<TxKernel>, PMMRBackend<NoSum<TxKernel>>>,
|
|
|
|
|
2018-02-10 01:32:16 +03:00
|
|
|
kernel_file: &'a mut AppendOnlyFile,
|
2017-09-28 02:46:32 +03:00
|
|
|
commit_index: Arc<ChainStore>,
|
|
|
|
new_output_commits: HashMap<Commitment, u64>,
|
|
|
|
new_kernel_excesses: HashMap<Commitment, u64>,
|
2017-09-29 21:44:25 +03:00
|
|
|
rollback: bool,
|
2017-09-28 02:46:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Extension<'a> {
|
|
|
|
// constructor
|
2018-02-10 01:32:16 +03:00
|
|
|
fn new(
|
|
|
|
trees: &'a mut SumTrees,
|
|
|
|
commit_index: Arc<ChainStore>,
|
|
|
|
) -> Extension<'a> {
|
|
|
|
|
2017-09-28 02:46:32 +03:00
|
|
|
Extension {
|
2017-09-29 21:44:25 +03:00
|
|
|
output_pmmr: PMMR::at(
|
|
|
|
&mut trees.output_pmmr_h.backend,
|
|
|
|
trees.output_pmmr_h.last_pos,
|
|
|
|
),
|
|
|
|
rproof_pmmr: PMMR::at(
|
|
|
|
&mut trees.rproof_pmmr_h.backend,
|
|
|
|
trees.rproof_pmmr_h.last_pos,
|
|
|
|
),
|
|
|
|
kernel_pmmr: PMMR::at(
|
|
|
|
&mut trees.kernel_pmmr_h.backend,
|
|
|
|
trees.kernel_pmmr_h.last_pos,
|
|
|
|
),
|
2018-02-10 01:32:16 +03:00
|
|
|
kernel_file: &mut trees.kernel_file,
|
2017-09-28 02:46:32 +03:00
|
|
|
commit_index: commit_index,
|
|
|
|
new_output_commits: HashMap::new(),
|
|
|
|
new_kernel_excesses: HashMap::new(),
|
|
|
|
rollback: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Apply a new set of blocks on top the existing sum trees. Blocks are
|
|
|
|
/// applied in order of the provided Vec. If pruning is enabled, inputs also
|
|
|
|
/// prune MMR data.
|
|
|
|
pub fn apply_block(&mut self, b: &Block) -> Result<(), Error> {
|
|
|
|
|
2018-01-08 04:23:23 +03:00
|
|
|
// first applying coinbase outputs. due to the construction of PMMRs the
|
|
|
|
// last element, when its a leaf, can never be pruned as it has no parent
|
|
|
|
// yet and it will be needed to calculate that hash. to work around this,
|
|
|
|
// we insert coinbase outputs first to add at least one output of padding
|
|
|
|
for out in &b.outputs {
|
2018-02-05 22:43:54 +03:00
|
|
|
if out.features.contains(OutputFeatures::COINBASE_OUTPUT) {
|
2018-01-08 04:23:23 +03:00
|
|
|
self.apply_output(out)?;
|
|
|
|
}
|
|
|
|
}
|
2018-01-17 06:03:40 +03:00
|
|
|
|
|
|
|
// then doing inputs guarantees an input can't spend an output in the
|
2017-11-29 02:43:02 +03:00
|
|
|
// same block, enforcing block cut-through
|
2017-09-28 02:46:32 +03:00
|
|
|
for input in &b.inputs {
|
2018-01-08 04:23:23 +03:00
|
|
|
self.apply_input(input, b.header.height)?;
|
2017-09-28 02:46:32 +03:00
|
|
|
}
|
|
|
|
|
2018-01-08 04:23:23 +03:00
|
|
|
// now all regular, non coinbase outputs
|
2017-09-28 02:46:32 +03:00
|
|
|
for out in &b.outputs {
|
2018-02-05 22:43:54 +03:00
|
|
|
if !out.features.contains(OutputFeatures::COINBASE_OUTPUT) {
|
2018-01-08 04:23:23 +03:00
|
|
|
self.apply_output(out)?;
|
2017-09-28 02:46:32 +03:00
|
|
|
}
|
|
|
|
}
|
2018-01-17 06:03:40 +03:00
|
|
|
|
2018-01-08 04:23:23 +03:00
|
|
|
// finally, applying all kernels
|
2017-09-28 02:46:32 +03:00
|
|
|
for kernel in &b.kernels {
|
2018-01-08 04:23:23 +03:00
|
|
|
self.apply_kernel(kernel)?;
|
2017-09-28 02:46:32 +03:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn save_pos_index(&self) -> Result<(), Error> {
|
|
|
|
for (commit, pos) in &self.new_output_commits {
|
|
|
|
self.commit_index.save_output_pos(commit, *pos)?;
|
|
|
|
}
|
2017-12-22 20:15:44 +03:00
|
|
|
|
2017-09-28 02:46:32 +03:00
|
|
|
for (excess, pos) in &self.new_kernel_excesses {
|
|
|
|
self.commit_index.save_kernel_pos(excess, *pos)?;
|
|
|
|
}
|
2017-12-22 20:15:44 +03:00
|
|
|
|
2017-09-28 02:46:32 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-01-08 04:23:23 +03:00
|
|
|
fn apply_input(&mut self, input: &Input, height: u64) -> Result<(), Error> {
|
|
|
|
let commit = input.commitment();
|
|
|
|
let pos_res = self.get_output_pos(&commit);
|
|
|
|
if let Ok(pos) = pos_res {
|
2018-01-17 06:03:40 +03:00
|
|
|
if let Some(HashSum { hash, sum: _ }) = self.output_pmmr.get(pos) {
|
|
|
|
let sum_commit = SumCommit::from_input(&input);
|
|
|
|
|
|
|
|
// check hash from pmmr matches hash from input
|
|
|
|
// if not then the input is not being honest about
|
|
|
|
// what it is attempting to spend...
|
|
|
|
let hash_sum = HashSum::from_summable(pos, &sum_commit);
|
|
|
|
if hash != hash_sum.hash {
|
|
|
|
return Err(Error::SumTreeErr(format!("output pmmr hash mismatch")));
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point we can be sure the input is spending the output
|
|
|
|
// it claims to be spending, and it is coinbase or non-coinbase.
|
|
|
|
// If we are spending a coinbase output then go find the block
|
|
|
|
// and check the coinbase maturity rule is being met.
|
2018-02-05 22:43:54 +03:00
|
|
|
if input.features.contains(OutputFeatures::COINBASE_OUTPUT) {
|
2018-01-17 06:03:40 +03:00
|
|
|
let block_hash = &input.out_block
|
|
|
|
.expect("input spending coinbase output must have a block hash");
|
|
|
|
let block = self.commit_index.get_block(&block_hash)?;
|
|
|
|
block.verify_coinbase_maturity(&input, height)
|
|
|
|
.map_err(|_| Error::ImmatureCoinbase)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now prune the output_pmmr and rproof_pmmr.
|
|
|
|
// Input is not valid if we cannot prune successfully (to spend an unspent output).
|
2018-01-08 04:23:23 +03:00
|
|
|
match self.output_pmmr.prune(pos, height as u32) {
|
|
|
|
Ok(true) => {
|
|
|
|
self.rproof_pmmr
|
|
|
|
.prune(pos, height as u32)
|
|
|
|
.map_err(|s| Error::SumTreeErr(s))?;
|
|
|
|
}
|
|
|
|
Ok(false) => return Err(Error::AlreadySpent(commit)),
|
|
|
|
Err(s) => return Err(Error::SumTreeErr(s)),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(Error::AlreadySpent(commit));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn apply_output(&mut self, out: &Output) -> Result<(), Error> {
|
|
|
|
let commit = out.commitment();
|
2018-01-17 06:03:40 +03:00
|
|
|
let sum_commit = SumCommit::from_output(out);
|
2018-01-08 04:23:23 +03:00
|
|
|
|
|
|
|
if let Ok(pos) = self.get_output_pos(&commit) {
|
|
|
|
// we need to check whether the commitment is in the current MMR view
|
|
|
|
// as well as the index doesn't support rewind and is non-authoritative
|
|
|
|
// (non-historical node will have a much smaller one)
|
|
|
|
// note that this doesn't show the commitment *never* existed, just
|
|
|
|
// that this is not an existing unspent commitment right now
|
|
|
|
if let Some(c) = self.output_pmmr.get(pos) {
|
2018-01-17 06:03:40 +03:00
|
|
|
let hash_sum = HashSum::from_summable(pos, &sum_commit);
|
2018-01-08 04:23:23 +03:00
|
|
|
|
|
|
|
// processing a new fork so we may get a position on the old
|
|
|
|
// fork that exists but matches a different node
|
|
|
|
// filtering that case out
|
2018-01-17 06:03:40 +03:00
|
|
|
if c.hash == hash_sum.hash {
|
2018-01-08 04:23:23 +03:00
|
|
|
return Err(Error::DuplicateCommitment(commit));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// push new outputs commitments in their MMR and save them in the index
|
|
|
|
let pos = self.output_pmmr
|
|
|
|
.push(sum_commit)
|
|
|
|
.map_err(&Error::SumTreeErr)?;
|
|
|
|
|
|
|
|
self.new_output_commits.insert(out.commitment(), pos);
|
|
|
|
|
|
|
|
// push range proofs in their MMR
|
|
|
|
self.rproof_pmmr
|
|
|
|
.push(NoSum(out.proof))
|
|
|
|
.map_err(&Error::SumTreeErr)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn apply_kernel(&mut self, kernel: &TxKernel) -> Result<(), Error> {
|
|
|
|
if let Ok(pos) = self.get_kernel_pos(&kernel.excess) {
|
|
|
|
// same as outputs
|
|
|
|
if let Some(k) = self.kernel_pmmr.get(pos) {
|
|
|
|
let hashsum = HashSum::from_summable(pos, &NoSum(kernel));
|
|
|
|
if k.hash == hashsum.hash {
|
|
|
|
return Err(Error::DuplicateKernel(kernel.excess.clone()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-10 01:32:16 +03:00
|
|
|
|
|
|
|
// push kernels in their MMR and file
|
2018-01-08 04:23:23 +03:00
|
|
|
let pos = self.kernel_pmmr
|
|
|
|
.push(NoSum(kernel.clone()))
|
|
|
|
.map_err(&Error::SumTreeErr)?;
|
|
|
|
self.new_kernel_excesses.insert(kernel.excess, pos);
|
2018-02-10 01:32:16 +03:00
|
|
|
self.kernel_file.append(&mut ser::ser_vec(&kernel).unwrap());
|
|
|
|
|
2018-01-08 04:23:23 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-02-10 01:32:16 +03:00
|
|
|
/// Rewinds the MMRs to the provided block, using the last output and
|
2017-09-28 02:46:32 +03:00
|
|
|
/// last kernel of the block we want to rewind to.
|
2017-11-15 23:37:40 +03:00
|
|
|
pub fn rewind(&mut self, block: &Block) -> Result<(), Error> {
|
|
|
|
debug!(
|
|
|
|
LOGGER,
|
|
|
|
"Rewind sumtrees to header {} at {}",
|
|
|
|
block.header.hash(),
|
|
|
|
block.header.height,
|
|
|
|
);
|
|
|
|
|
2018-02-10 01:32:16 +03:00
|
|
|
// rewind each MMR
|
|
|
|
let (out_pos_rew, kern_pos_rew) = indexes_at(block, self.commit_index.deref())?;
|
|
|
|
self.rewind_pos(block.header.height, out_pos_rew, kern_pos_rew)?;
|
2018-02-17 20:56:22 +03:00
|
|
|
|
2018-02-10 01:32:16 +03:00
|
|
|
// rewind the kernel file store, the position is the number of kernels
|
|
|
|
// multiplied by their size
|
2018-02-20 02:20:32 +03:00
|
|
|
// the number of kernels is the number of leaves in the MMR
|
|
|
|
let pos = pmmr::n_leaves(kern_pos_rew);
|
2018-02-10 01:32:16 +03:00
|
|
|
self.kernel_file.rewind(pos * (TxKernel::size() as u64));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2017-11-15 23:37:40 +03:00
|
|
|
|
2018-02-10 01:32:16 +03:00
|
|
|
/// Rewinds the MMRs to the provided positions, given the output and
|
|
|
|
/// kernel we want to rewind to.
|
|
|
|
pub fn rewind_pos(&mut self, height: u64, out_pos_rew: u64, kern_pos_rew: u64) -> Result<(), Error> {
|
2017-11-15 23:37:40 +03:00
|
|
|
debug!(
|
|
|
|
LOGGER,
|
|
|
|
"Rewind sumtrees to output pos: {}, kernel pos: {}",
|
|
|
|
out_pos_rew,
|
|
|
|
kern_pos_rew,
|
|
|
|
);
|
|
|
|
|
2017-09-29 21:44:25 +03:00
|
|
|
self.output_pmmr
|
|
|
|
.rewind(out_pos_rew, height as u32)
|
|
|
|
.map_err(&Error::SumTreeErr)?;
|
|
|
|
self.rproof_pmmr
|
|
|
|
.rewind(out_pos_rew, height as u32)
|
|
|
|
.map_err(&Error::SumTreeErr)?;
|
|
|
|
self.kernel_pmmr
|
|
|
|
.rewind(kern_pos_rew, height as u32)
|
|
|
|
.map_err(&Error::SumTreeErr)?;
|
2017-11-15 23:37:40 +03:00
|
|
|
|
2017-10-28 00:57:04 +03:00
|
|
|
self.dump(true);
|
2017-09-28 02:46:32 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-12-25 03:27:13 +03:00
|
|
|
fn get_output_pos(&self, commit: &Commitment) -> Result<u64, grin_store::Error> {
|
|
|
|
if let Some(pos) = self.new_output_commits.get(commit) {
|
|
|
|
Ok(*pos)
|
|
|
|
} else {
|
|
|
|
self.commit_index.get_output_pos(commit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_kernel_pos(&self, excess: &Commitment) -> Result<u64, grin_store::Error> {
|
|
|
|
if let Some(pos) = self.new_kernel_excesses.get(excess) {
|
|
|
|
Ok(*pos)
|
|
|
|
} else {
|
|
|
|
self.commit_index.get_kernel_pos(excess)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-28 02:46:32 +03:00
|
|
|
/// Current root hashes and sums (if applicable) for the UTXO, range proof
|
|
|
|
/// and kernel sum trees.
|
2017-10-17 00:23:10 +03:00
|
|
|
pub fn roots(
|
|
|
|
&self,
|
2017-11-01 02:32:33 +03:00
|
|
|
) -> (
|
|
|
|
HashSum<SumCommit>,
|
|
|
|
HashSum<NoSum<RangeProof>>,
|
|
|
|
HashSum<NoSum<TxKernel>>,
|
|
|
|
) {
|
2017-10-17 00:23:10 +03:00
|
|
|
(
|
|
|
|
self.output_pmmr.root(),
|
|
|
|
self.rproof_pmmr.root(),
|
|
|
|
self.kernel_pmmr.root(),
|
|
|
|
)
|
2017-09-28 02:46:32 +03:00
|
|
|
}
|
|
|
|
|
2018-02-10 01:32:16 +03:00
|
|
|
/// Validate the current sumtree state against a block header
|
|
|
|
pub fn validate(&self, header: &BlockHeader) -> Result<(), Error> {
|
|
|
|
// validate all hashes and sums within the trees
|
|
|
|
if let Err(e) = self.output_pmmr.validate() {
|
|
|
|
return Err(Error::InvalidSumtree(e));
|
|
|
|
}
|
|
|
|
if let Err(e) = self.rproof_pmmr.validate() {
|
|
|
|
return Err(Error::InvalidSumtree(e));
|
|
|
|
}
|
|
|
|
if let Err(e) = self.kernel_pmmr.validate() {
|
|
|
|
return Err(Error::InvalidSumtree(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate the tree roots against the block header
|
|
|
|
let (utxo_root, rproof_root, kernel_root) = self.roots();
|
|
|
|
if utxo_root.hash != header.utxo_root || rproof_root.hash != header.range_proof_root
|
|
|
|
|| kernel_root.hash != header.kernel_root
|
|
|
|
{
|
|
|
|
return Err(Error::InvalidRoot);
|
|
|
|
}
|
|
|
|
|
|
|
|
// the real magicking: the sum of all kernel excess should equal the sum
|
|
|
|
// of all UTXO commitments, minus the total supply
|
|
|
|
let (kernel_sum, fees) = self.sum_kernels()?;
|
|
|
|
let utxo_sum = self.sum_utxos()?;
|
|
|
|
{
|
|
|
|
let secp = static_secp_instance();
|
|
|
|
let secp = secp.lock().unwrap();
|
|
|
|
let over_commit = secp.commit_value(header.height * reward(0) - fees / 2)?;
|
|
|
|
let adjusted_sum_utxo = secp.commit_sum(vec![utxo_sum], vec![over_commit])?;
|
|
|
|
|
|
|
|
if adjusted_sum_utxo != kernel_sum {
|
|
|
|
return Err(Error::InvalidSumtree("Differing UTXO commitment and kernel excess sums.".to_owned()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Rebuild the index of MMR positions to the corresponding UTXO and kernel
|
|
|
|
/// by iterating over the whole MMR data. This is a costly operation
|
|
|
|
/// performed only when we receive a full new chain state.
|
|
|
|
pub fn rebuild_index(&self) -> Result<(), Error> {
|
|
|
|
for n in 1..self.output_pmmr.unpruned_size()+1 {
|
|
|
|
// non-pruned leaves only
|
|
|
|
if pmmr::bintree_postorder_height(n) == 0 {
|
|
|
|
if let Some(hs) = self.output_pmmr.get(n) {
|
|
|
|
self.commit_index.save_output_pos(&hs.sum.commit, n)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-09-28 02:46:32 +03:00
|
|
|
/// Force the rollback of this extension, no matter the result
|
|
|
|
pub fn force_rollback(&mut self) {
|
|
|
|
self.rollback = true;
|
|
|
|
}
|
|
|
|
|
2017-10-28 00:57:04 +03:00
|
|
|
/// Dumps the state of the 3 sum trees to stdout for debugging. Short
|
2018-02-10 01:32:16 +03:00
|
|
|
/// version only prints the UTXO tree.
|
2017-10-22 10:11:45 +03:00
|
|
|
pub fn dump(&self, short: bool) {
|
|
|
|
debug!(LOGGER, "-- outputs --");
|
|
|
|
self.output_pmmr.dump(short);
|
2017-10-28 00:57:04 +03:00
|
|
|
if !short {
|
|
|
|
debug!(LOGGER, "-- range proofs --");
|
|
|
|
self.rproof_pmmr.dump(short);
|
|
|
|
debug!(LOGGER, "-- kernels --");
|
|
|
|
self.kernel_pmmr.dump(short);
|
|
|
|
}
|
2017-10-12 22:23:58 +03:00
|
|
|
}
|
|
|
|
|
2017-09-28 02:46:32 +03:00
|
|
|
// Sizes of the sum trees, used by `extending` on rollback.
|
|
|
|
fn sizes(&self) -> (u64, u64, u64) {
|
2017-10-17 00:23:10 +03:00
|
|
|
(
|
|
|
|
self.output_pmmr.unpruned_size(),
|
|
|
|
self.rproof_pmmr.unpruned_size(),
|
|
|
|
self.kernel_pmmr.unpruned_size(),
|
|
|
|
)
|
2017-09-28 02:46:32 +03:00
|
|
|
}
|
2018-02-10 01:32:16 +03:00
|
|
|
|
|
|
|
/// Sums the excess of all our kernels, validating their signatures on the way
|
|
|
|
fn sum_kernels(&self) -> Result<(Commitment, u64), Error> {
|
|
|
|
// make sure we have the right count of kernels using the MMR, the storage
|
|
|
|
// file may have a few more
|
|
|
|
let mmr_sz = self.kernel_pmmr.unpruned_size();
|
2018-02-20 02:20:32 +03:00
|
|
|
let count = pmmr::n_leaves(mmr_sz);
|
2018-02-10 01:32:16 +03:00
|
|
|
|
|
|
|
let mut kernel_file = File::open(self.kernel_file.path())?;
|
|
|
|
let first: TxKernel = ser::deserialize(&mut kernel_file)?;
|
|
|
|
first.verify()?;
|
|
|
|
let mut sum_kernel = first.excess;
|
|
|
|
let mut fees = first.fee;
|
|
|
|
|
|
|
|
let secp = static_secp_instance();
|
|
|
|
let mut kern_count = 1;
|
|
|
|
loop {
|
|
|
|
match ser::deserialize::<TxKernel>(&mut kernel_file) {
|
|
|
|
Ok(kernel) => {
|
|
|
|
kernel.verify()?;
|
|
|
|
let secp = secp.lock().unwrap();
|
|
|
|
sum_kernel = secp.commit_sum(vec![sum_kernel, kernel.excess], vec![])?;
|
|
|
|
fees += kernel.fee;
|
|
|
|
kern_count += 1;
|
|
|
|
if kern_count == count {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_) => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug!(LOGGER, "Validated and summed {} kernels", kern_count);
|
|
|
|
Ok((sum_kernel, fees))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sums all our UTXO commitments
|
|
|
|
fn sum_utxos(&self) -> Result<Commitment, Error> {
|
|
|
|
let mut sum_utxo = None;
|
|
|
|
let mut utxo_count = 0;
|
|
|
|
let secp = static_secp_instance();
|
|
|
|
for n in 1..self.output_pmmr.unpruned_size()+1 {
|
|
|
|
if pmmr::bintree_postorder_height(n) == 0 {
|
|
|
|
if let Some(hs) = self.output_pmmr.get(n) {
|
|
|
|
if n == 1 {
|
|
|
|
sum_utxo = Some(hs.sum.commit);
|
|
|
|
} else {
|
|
|
|
let secp = secp.lock().unwrap();
|
|
|
|
sum_utxo = Some(secp.commit_sum(vec![sum_utxo.unwrap(), hs.sum.commit], vec![])?);
|
|
|
|
}
|
|
|
|
utxo_count += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug!(LOGGER, "Summed {} UTXOs", utxo_count);
|
|
|
|
Ok(sum_utxo.unwrap())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Output and kernel MMR indexes at the end of the provided block
|
|
|
|
fn indexes_at(block: &Block, commit_index: &ChainStore) -> Result<(u64, u64), Error> {
|
|
|
|
let out_idx = match block.outputs.last() {
|
|
|
|
Some(output) => commit_index.get_output_pos(&output.commitment())
|
|
|
|
.map_err(|e| {
|
|
|
|
Error::StoreErr(e, format!("missing output pos for known block"))
|
|
|
|
})?,
|
|
|
|
None => 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
let kern_idx = match block.kernels.last() {
|
|
|
|
Some(kernel) => commit_index.get_kernel_pos(&kernel.excess)
|
|
|
|
.map_err(|e| {
|
|
|
|
Error::StoreErr(e, format!("missing kernel pos for known block"))
|
|
|
|
})?,
|
|
|
|
None => 0,
|
|
|
|
};
|
|
|
|
Ok((out_idx, kern_idx))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Packages the sumtree data files into a zip and returns a Read to the
|
|
|
|
/// resulting file
|
|
|
|
pub fn zip_read(root_dir: String) -> Result<File, Error> {
|
|
|
|
let sumtrees_path = Path::new(&root_dir).join(SUMTREES_SUBDIR);
|
|
|
|
let zip_path = Path::new(&root_dir).join(SUMTREES_ZIP);
|
|
|
|
|
|
|
|
// create the zip archive
|
|
|
|
{
|
|
|
|
zip::compress(&sumtrees_path, &File::create(zip_path.clone())?)
|
|
|
|
.map_err(|ze| Error::Other(ze.to_string()))?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// open it again to read it back
|
|
|
|
let zip_file = File::open(zip_path)?;
|
|
|
|
Ok(zip_file)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extract the sumtree data from a zip file and writes the content into the
|
|
|
|
/// sumtree storage dir
|
|
|
|
pub fn zip_write(root_dir: String, sumtree_data: File) -> Result<(), Error> {
|
|
|
|
let sumtrees_path = Path::new(&root_dir).join(SUMTREES_SUBDIR);
|
|
|
|
|
|
|
|
fs::create_dir_all(sumtrees_path.clone())?;
|
|
|
|
zip::decompress(sumtree_data, &sumtrees_path)
|
|
|
|
.map_err(|ze| Error::Other(ze.to_string()))
|
2017-09-28 02:46:32 +03:00
|
|
|
}
|