2017-07-04 02:46:25 +03:00
|
|
|
// Copyright 2016 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.
|
|
|
|
|
|
|
|
//! Facade and handler for the rest of the blockchain implementation
|
|
|
|
//! and mostly the chain pipeline.
|
|
|
|
|
2017-12-22 01:29:24 +03:00
|
|
|
use std::collections::HashMap;
|
2017-09-28 02:46:32 +03:00
|
|
|
use std::sync::{Arc, Mutex, RwLock};
|
2017-07-04 02:46:25 +03:00
|
|
|
|
2017-11-01 02:20:55 +03:00
|
|
|
use util::secp::pedersen::{Commitment, RangeProof};
|
2017-07-04 02:46:25 +03:00
|
|
|
|
2017-11-01 02:32:33 +03:00
|
|
|
use core::core::SumCommit;
|
|
|
|
use core::core::pmmr::{HashSum, NoSum};
|
2017-10-28 00:57:04 +03:00
|
|
|
|
|
|
|
use core::core::{Block, BlockHeader, Output, TxKernel};
|
2017-07-04 02:46:25 +03:00
|
|
|
use core::core::target::Difficulty;
|
2017-12-05 21:32:57 +03:00
|
|
|
use core::core::hash::Hash;
|
2017-08-29 19:32:45 +03:00
|
|
|
use grin_store::Error::NotFoundErr;
|
2017-07-04 02:46:25 +03:00
|
|
|
use pipe;
|
|
|
|
use store;
|
2017-09-28 02:46:32 +03:00
|
|
|
use sumtree;
|
2017-07-04 02:46:25 +03:00
|
|
|
use types::*;
|
2017-10-12 19:56:44 +03:00
|
|
|
use util::LOGGER;
|
2017-07-04 02:46:25 +03:00
|
|
|
|
2017-12-22 01:29:24 +03:00
|
|
|
const MAX_ORPHANS: usize = 100;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct Orphan {
|
|
|
|
block: Block,
|
|
|
|
opts: Options,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct OrphanBlockPool {
|
|
|
|
// blocks indexed by their hash
|
|
|
|
orphans: RwLock<HashMap<Hash, Orphan>>,
|
|
|
|
// additional index of previous -> hash
|
|
|
|
// so we can efficiently identify a child block (ex-orphan) after processing a block
|
|
|
|
prev_idx: RwLock<HashMap<Hash, Hash>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OrphanBlockPool {
|
|
|
|
fn new() -> OrphanBlockPool {
|
|
|
|
OrphanBlockPool {
|
|
|
|
orphans: RwLock::new(HashMap::new()),
|
|
|
|
prev_idx: RwLock::new(HashMap::new()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn len(&self) -> usize {
|
|
|
|
let orphans = self.orphans.read().unwrap();
|
|
|
|
orphans.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add(&self, orphan: Orphan) {
|
|
|
|
{
|
|
|
|
let mut orphans = self.orphans.write().unwrap();
|
|
|
|
let mut prev_idx = self.prev_idx.write().unwrap();
|
|
|
|
orphans.insert(orphan.block.hash(), orphan.clone());
|
|
|
|
prev_idx.insert(orphan.block.header.previous, orphan.block.hash());
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.len() > MAX_ORPHANS {
|
|
|
|
let max = {
|
|
|
|
let orphans = self.orphans.read().unwrap();
|
|
|
|
orphans.values().max_by_key(|x| x.block.header.height).cloned()
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(x) = max {
|
|
|
|
let mut orphans = self.orphans.write().unwrap();
|
|
|
|
let mut prev_idx = self.prev_idx.write().unwrap();
|
|
|
|
orphans.remove(&x.block.hash());
|
|
|
|
prev_idx.remove(&x.block.header.previous);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn remove(&self, hash: &Hash) -> Option<Orphan> {
|
|
|
|
let mut orphans = self.orphans.write().unwrap();
|
|
|
|
let mut prev_idx = self.prev_idx.write().unwrap();
|
|
|
|
let orphan = orphans.remove(hash);
|
|
|
|
if let Some(x) = orphan.clone() {
|
|
|
|
prev_idx.remove(&x.block.header.previous);
|
|
|
|
}
|
|
|
|
orphan
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get an orphan from the pool indexed by the hash of its parent
|
|
|
|
fn get_by_previous(&self, hash: &Hash) -> Option<Orphan> {
|
|
|
|
let orphans = self.orphans.read().unwrap();
|
|
|
|
let prev_idx = self.prev_idx.read().unwrap();
|
|
|
|
if let Some(hash) = prev_idx.get(hash) {
|
|
|
|
orphans.get(hash).cloned()
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn contains(&self, hash: &Hash) -> bool {
|
|
|
|
let orphans = self.orphans.read().unwrap();
|
|
|
|
orphans.contains_key(hash)
|
|
|
|
}
|
|
|
|
}
|
2017-07-18 23:57:09 +03:00
|
|
|
|
2017-07-04 02:46:25 +03:00
|
|
|
/// Facade to the blockchain block processing pipeline and storage. Provides
|
|
|
|
/// the current view of the UTXO set according to the chain state. Also
|
|
|
|
/// maintains locking for the pipeline to avoid conflicting processing.
|
|
|
|
pub struct Chain {
|
|
|
|
store: Arc<ChainStore>,
|
|
|
|
adapter: Arc<ChainAdapter>,
|
2017-07-27 22:08:48 +03:00
|
|
|
|
2017-07-04 02:46:25 +03:00
|
|
|
head: Arc<Mutex<Tip>>,
|
2017-12-22 01:29:24 +03:00
|
|
|
orphans: Arc<OrphanBlockPool>,
|
2017-09-28 02:46:32 +03:00
|
|
|
sumtrees: Arc<RwLock<sumtree::SumTrees>>,
|
2017-08-22 21:23:54 +03:00
|
|
|
|
2017-09-29 21:44:25 +03:00
|
|
|
// POW verification function
|
2017-08-22 21:23:54 +03:00
|
|
|
pow_verifier: fn(&BlockHeader, u32) -> bool,
|
2017-07-04 02:46:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl Sync for Chain {}
|
|
|
|
unsafe impl Send for Chain {}
|
|
|
|
|
|
|
|
impl Chain {
|
2017-08-22 21:23:54 +03:00
|
|
|
/// Check whether the chain exists. If not, the call to 'init' will
|
|
|
|
/// expect an already mined genesis block. This keeps the chain free
|
|
|
|
/// from needing to know about the mining implementation
|
2017-09-29 21:44:25 +03:00
|
|
|
pub fn chain_exists(db_root: String) -> bool {
|
2017-08-22 21:23:54 +03:00
|
|
|
let chain_store = store::ChainKVStore::new(db_root).unwrap();
|
|
|
|
match chain_store.head() {
|
2017-09-29 21:44:25 +03:00
|
|
|
Ok(_) => true,
|
2017-08-29 19:32:45 +03:00
|
|
|
Err(NotFoundErr) => false,
|
2017-08-22 21:23:54 +03:00
|
|
|
Err(_) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-13 18:24:49 +03:00
|
|
|
/// Initializes the blockchain and returns a new Chain instance. Does a check
|
2017-07-04 02:46:25 +03:00
|
|
|
/// on the current chain head to make sure it exists and creates one based
|
2017-11-13 18:24:49 +03:00
|
|
|
/// on the genesis block if necessary.
|
2017-10-13 07:45:07 +03:00
|
|
|
pub fn init(
|
|
|
|
db_root: String,
|
|
|
|
adapter: Arc<ChainAdapter>,
|
2017-11-20 20:35:52 +03:00
|
|
|
genesis: Block,
|
2017-10-13 07:45:07 +03:00
|
|
|
pow_verifier: fn(&BlockHeader, u32) -> bool,
|
|
|
|
) -> Result<Chain, Error> {
|
2017-09-28 02:46:32 +03:00
|
|
|
let chain_store = store::ChainKVStore::new(db_root.clone())?;
|
2017-07-04 02:46:25 +03:00
|
|
|
|
|
|
|
// check if we have a head in store, otherwise the genesis block is it
|
|
|
|
let head = match chain_store.head() {
|
|
|
|
Ok(tip) => tip,
|
2017-08-29 19:32:45 +03:00
|
|
|
Err(NotFoundErr) => {
|
2017-12-16 03:27:37 +03:00
|
|
|
let tip = Tip::new(genesis.hash());
|
2017-11-20 20:35:52 +03:00
|
|
|
chain_store.save_block(&genesis)?;
|
2017-12-16 05:17:27 +03:00
|
|
|
chain_store.setup_height(&genesis.header, &tip)?;
|
2017-07-04 02:46:25 +03:00
|
|
|
|
|
|
|
// saving a new tip based on genesis
|
|
|
|
chain_store.save_head(&tip)?;
|
2017-11-13 18:24:49 +03:00
|
|
|
info!(
|
|
|
|
LOGGER,
|
2017-11-20 20:35:52 +03:00
|
|
|
"Saved genesis block: {:?}, nonce: {:?}, pow: {:?}",
|
|
|
|
genesis.hash(),
|
|
|
|
genesis.header.nonce,
|
|
|
|
genesis.header.pow,
|
2017-11-13 18:24:49 +03:00
|
|
|
);
|
2017-07-04 02:46:25 +03:00
|
|
|
tip
|
|
|
|
}
|
2017-10-22 10:11:45 +03:00
|
|
|
Err(e) => return Err(Error::StoreErr(e, "chain init load head".to_owned())),
|
2017-07-04 02:46:25 +03:00
|
|
|
};
|
|
|
|
|
2017-12-04 22:16:57 +03:00
|
|
|
// make sure sync_head is available for later use
|
|
|
|
let _ = match chain_store.get_sync_head() {
|
|
|
|
Ok(tip) => tip,
|
|
|
|
Err(NotFoundErr) => {
|
2017-12-05 21:32:57 +03:00
|
|
|
let tip = chain_store.head().unwrap();
|
2017-12-04 22:16:57 +03:00
|
|
|
chain_store.save_sync_head(&tip)?;
|
|
|
|
tip
|
|
|
|
},
|
|
|
|
Err(e) => return Err(Error::StoreErr(e, "chain init sync head".to_owned())),
|
|
|
|
};
|
|
|
|
|
2017-11-20 20:35:52 +03:00
|
|
|
info!(
|
|
|
|
LOGGER,
|
|
|
|
"Chain init: {:?}",
|
|
|
|
head,
|
|
|
|
);
|
|
|
|
|
2017-09-28 02:46:32 +03:00
|
|
|
let store = Arc::new(chain_store);
|
|
|
|
let sumtrees = sumtree::SumTrees::open(db_root, store.clone())?;
|
2017-08-29 19:32:45 +03:00
|
|
|
|
2017-07-04 02:46:25 +03:00
|
|
|
Ok(Chain {
|
2017-09-28 02:46:32 +03:00
|
|
|
store: store,
|
2017-07-04 02:46:25 +03:00
|
|
|
adapter: adapter,
|
|
|
|
head: Arc::new(Mutex::new(head)),
|
2017-12-22 01:29:24 +03:00
|
|
|
orphans: Arc::new(OrphanBlockPool::new()),
|
2017-09-28 02:46:32 +03:00
|
|
|
sumtrees: Arc::new(RwLock::new(sumtrees)),
|
2017-08-22 21:23:54 +03:00
|
|
|
pow_verifier: pow_verifier,
|
2017-07-04 02:46:25 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempt to add a new block to the chain. Returns the new chain tip if it
|
|
|
|
/// has been added to the longest chain, None if it's added to an (as of
|
2017-07-27 22:08:48 +03:00
|
|
|
/// now) orphan chain.
|
2017-12-22 01:29:24 +03:00
|
|
|
pub fn process_block(&self, b: Block, opts: Options)
|
|
|
|
-> Result<Option<Tip>, Error>
|
|
|
|
{
|
2017-11-01 02:32:33 +03:00
|
|
|
let head = self.store
|
|
|
|
.head()
|
|
|
|
.map_err(|e| Error::StoreErr(e, "chain load head".to_owned()))?;
|
|
|
|
let height = head.height;
|
2017-07-04 02:46:25 +03:00
|
|
|
let ctx = self.ctx_from_head(head, opts);
|
|
|
|
|
2017-07-27 22:08:48 +03:00
|
|
|
let res = pipe::process_block(&b, ctx);
|
|
|
|
|
2017-07-28 00:13:34 +03:00
|
|
|
match res {
|
|
|
|
Ok(Some(ref tip)) => {
|
|
|
|
// block got accepted and extended the head, updating our head
|
|
|
|
let chain_head = self.head.clone();
|
2017-07-28 02:47:33 +03:00
|
|
|
{
|
|
|
|
let mut head = chain_head.lock().unwrap();
|
|
|
|
*head = tip.clone();
|
|
|
|
}
|
2017-10-15 23:38:41 +03:00
|
|
|
|
|
|
|
// notifying other parts of the system of the update
|
|
|
|
if !opts.intersects(SYNC) {
|
|
|
|
// broadcast the block
|
|
|
|
let adapter = self.adapter.clone();
|
|
|
|
adapter.block_accepted(&b);
|
|
|
|
}
|
2017-12-22 01:29:24 +03:00
|
|
|
// We just accepted a block so see if we can now accept any orphan(s)
|
|
|
|
self.check_orphans(&b);
|
2017-12-19 00:18:36 +03:00
|
|
|
},
|
|
|
|
Ok(None) => {
|
|
|
|
// block got accepted but we did not extend the head
|
|
|
|
// so its on a fork (or is the start of a new fork)
|
|
|
|
// broadcast the block out so everyone knows about the fork
|
|
|
|
//
|
|
|
|
// TODO - This opens us to an amplification attack on blocks
|
|
|
|
// mined at a low difficulty. We should suppress really old blocks
|
|
|
|
// or less relevant blocks somehow.
|
|
|
|
// We should also probably consider banning nodes that send us really old blocks.
|
|
|
|
//
|
|
|
|
if !opts.intersects(SYNC) {
|
|
|
|
// broadcast the block
|
|
|
|
let adapter = self.adapter.clone();
|
|
|
|
adapter.block_accepted(&b);
|
|
|
|
}
|
2017-12-22 01:29:24 +03:00
|
|
|
// We just accepted a block so see if we can now accept any orphan(s)
|
|
|
|
self.check_orphans(&b);
|
2017-12-19 00:18:36 +03:00
|
|
|
},
|
2017-12-16 06:19:04 +03:00
|
|
|
Err(Error::Orphan) => {
|
|
|
|
// TODO - Do we want to check that orphan height is > current height?
|
|
|
|
// TODO - Just check heights here? Or should we be checking total_difficulty as well?
|
|
|
|
let block_hash = b.hash();
|
|
|
|
if b.header.height < height + (MAX_ORPHANS as u64) {
|
2017-12-22 01:29:24 +03:00
|
|
|
let orphan = Orphan {
|
|
|
|
block: b.clone(),
|
|
|
|
opts: opts,
|
|
|
|
};
|
|
|
|
|
|
|
|
// In the case of a fork - it is possible to have multiple blocks
|
|
|
|
// that are children of a given block.
|
|
|
|
// We do not handle this currently for orphans (future enhancement?).
|
|
|
|
// We just assume "last one wins" for now.
|
|
|
|
&self.orphans.add(orphan);
|
|
|
|
|
2017-12-16 06:19:04 +03:00
|
|
|
debug!(
|
|
|
|
LOGGER,
|
|
|
|
"process_block: orphan: {:?}, # orphans {}",
|
|
|
|
block_hash,
|
2017-12-22 01:29:24 +03:00
|
|
|
self.orphans.len(),
|
2017-12-16 06:19:04 +03:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
debug!(
|
|
|
|
LOGGER,
|
|
|
|
"process_block: orphan: {:?}, (dropping, height {} vs {})",
|
|
|
|
block_hash,
|
|
|
|
b.header.height,
|
|
|
|
height,
|
|
|
|
);
|
|
|
|
}
|
2017-11-01 02:32:33 +03:00
|
|
|
},
|
2017-11-17 02:17:56 +03:00
|
|
|
Err(Error::Unfit(ref msg)) => {
|
|
|
|
debug!(
|
|
|
|
LOGGER,
|
|
|
|
"Block {} at {} is unfit at this time: {}",
|
|
|
|
b.hash(),
|
|
|
|
b.header.height,
|
|
|
|
msg
|
|
|
|
);
|
|
|
|
}
|
2017-09-28 02:46:32 +03:00
|
|
|
Err(ref e) => {
|
2017-09-29 21:44:25 +03:00
|
|
|
info!(
|
2017-10-12 19:56:44 +03:00
|
|
|
LOGGER,
|
2017-10-15 23:38:41 +03:00
|
|
|
"Rejected block {} at {}: {:?}",
|
2017-09-29 21:44:25 +03:00
|
|
|
b.hash(),
|
|
|
|
b.header.height,
|
|
|
|
e
|
|
|
|
);
|
2017-09-28 02:46:32 +03:00
|
|
|
}
|
2017-07-28 00:13:34 +03:00
|
|
|
}
|
2017-07-04 02:46:25 +03:00
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2017-12-04 22:16:57 +03:00
|
|
|
/// Attempt to add a new header to the header chain.
|
|
|
|
/// This is only ever used during sync and uses sync_head.
|
|
|
|
pub fn sync_block_header(
|
2017-10-13 07:45:07 +03:00
|
|
|
&self,
|
|
|
|
bh: &BlockHeader,
|
|
|
|
opts: Options,
|
|
|
|
) -> Result<Option<Tip>, Error> {
|
2017-12-04 22:16:57 +03:00
|
|
|
let sync_head = self.get_sync_head()?;
|
|
|
|
let header_head = self.get_header_head()?;
|
|
|
|
let sync_ctx = self.ctx_from_head(sync_head, opts);
|
|
|
|
let header_ctx = self.ctx_from_head(header_head, opts);
|
|
|
|
pipe::sync_block_header(bh, sync_ctx, header_ctx)
|
2017-07-04 02:46:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn ctx_from_head(&self, head: Tip, opts: Options) -> pipe::BlockContext {
|
|
|
|
pipe::BlockContext {
|
2017-11-16 00:49:15 +03:00
|
|
|
opts: opts,
|
2017-07-04 02:46:25 +03:00
|
|
|
store: self.store.clone(),
|
|
|
|
head: head,
|
2017-08-22 21:23:54 +03:00
|
|
|
pow_verifier: self.pow_verifier,
|
2017-09-28 02:46:32 +03:00
|
|
|
sumtrees: self.sumtrees.clone(),
|
2017-07-04 02:46:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-16 06:19:04 +03:00
|
|
|
/// Check if hash is for a known orphan.
|
2017-12-14 20:23:35 +03:00
|
|
|
pub fn is_orphan(&self, hash: &Hash) -> bool {
|
2017-12-22 01:29:24 +03:00
|
|
|
self.orphans.contains(hash)
|
2017-12-14 20:23:35 +03:00
|
|
|
}
|
|
|
|
|
2017-12-22 01:29:24 +03:00
|
|
|
fn check_orphans(&self, block: &Block) {
|
|
|
|
debug!(
|
|
|
|
LOGGER,
|
|
|
|
"chain: check_orphans: # orphans {}",
|
|
|
|
self.orphans.len(),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Is there an orphan in our orphans that we can now process?
|
|
|
|
// We just processed the given block, are there any orphans that have this block
|
|
|
|
// as their "previous" block?
|
|
|
|
if let Some(orphan) = self.orphans.get_by_previous(&block.hash()) {
|
|
|
|
self.orphans.remove(&orphan.block.hash());
|
|
|
|
let _ = self.process_block(orphan.block, orphan.opts);
|
2017-07-28 00:13:34 +03:00
|
|
|
}
|
|
|
|
}
|
2017-07-27 22:08:48 +03:00
|
|
|
|
2017-09-28 02:46:32 +03:00
|
|
|
/// Gets an unspent output from its commitment. With return None if the
|
|
|
|
/// output doesn't exist or has been spent. This querying is done in a
|
|
|
|
/// way that's consistent with the current chain state and more
|
|
|
|
/// specifically the current winning fork.
|
2017-09-12 20:24:24 +03:00
|
|
|
pub fn get_unspent(&self, output_ref: &Commitment) -> Result<Output, Error> {
|
2017-09-28 02:46:32 +03:00
|
|
|
let sumtrees = self.sumtrees.read().unwrap();
|
|
|
|
let is_unspent = sumtrees.is_unspent(output_ref)?;
|
|
|
|
if is_unspent {
|
2017-11-01 02:32:33 +03:00
|
|
|
self.store
|
|
|
|
.get_output_by_commit(output_ref)
|
|
|
|
.map_err(|e| Error::StoreErr(e, "chain get unspent".to_owned()))
|
2017-09-28 02:46:32 +03:00
|
|
|
} else {
|
|
|
|
Err(Error::OutputNotFound)
|
2017-07-04 02:46:25 +03:00
|
|
|
}
|
2017-09-28 02:46:32 +03:00
|
|
|
}
|
|
|
|
|
2017-11-20 13:38:49 +03:00
|
|
|
/// Checks whether an output is unspent
|
|
|
|
pub fn is_unspent(&self, output_ref: &Commitment) -> Result<bool, Error> {
|
|
|
|
let sumtrees = self.sumtrees.read().unwrap();
|
|
|
|
sumtrees.is_unspent(output_ref)
|
|
|
|
}
|
|
|
|
|
2017-09-28 02:46:32 +03:00
|
|
|
/// Sets the sumtree roots on a brand new block by applying the block on the
|
|
|
|
/// current sumtree state.
|
|
|
|
pub fn set_sumtree_roots(&self, b: &mut Block) -> Result<(), Error> {
|
|
|
|
let mut sumtrees = self.sumtrees.write().unwrap();
|
2017-09-29 21:44:25 +03:00
|
|
|
|
2017-10-13 07:45:07 +03:00
|
|
|
let roots = sumtree::extending(&mut sumtrees, |extension| {
|
2017-09-28 02:46:32 +03:00
|
|
|
// apply the block on the sumtrees and check the resulting root
|
|
|
|
extension.apply_block(b)?;
|
|
|
|
extension.force_rollback();
|
|
|
|
Ok(extension.roots())
|
|
|
|
})?;
|
|
|
|
|
|
|
|
b.header.utxo_root = roots.0.hash;
|
|
|
|
b.header.range_proof_root = roots.1.hash;
|
|
|
|
b.header.kernel_root = roots.2.hash;
|
|
|
|
Ok(())
|
2017-07-04 02:46:25 +03:00
|
|
|
}
|
|
|
|
|
2017-10-28 00:57:04 +03:00
|
|
|
/// returs sumtree roots
|
2017-11-01 02:32:33 +03:00
|
|
|
pub fn get_sumtree_roots(
|
|
|
|
&self,
|
|
|
|
) -> (
|
|
|
|
HashSum<SumCommit>,
|
2017-10-28 00:57:04 +03:00
|
|
|
HashSum<NoSum<RangeProof>>,
|
2017-11-01 02:32:33 +03:00
|
|
|
HashSum<NoSum<TxKernel>>,
|
|
|
|
) {
|
2017-10-28 00:57:04 +03:00
|
|
|
let mut sumtrees = self.sumtrees.write().unwrap();
|
|
|
|
sumtrees.roots()
|
|
|
|
}
|
|
|
|
|
2017-11-28 07:44:33 +03:00
|
|
|
/// Reset the header head to the same as the main head. When sync is running,
|
|
|
|
/// the header head will go ahead to try to download as many as possible.
|
|
|
|
/// However if a block, when fully received, is found invalid, the header
|
|
|
|
/// head need to backtrack to the last known valid position.
|
|
|
|
pub fn reset_header_head(&self) -> Result<(), Error> {
|
|
|
|
let head = self.head.lock().unwrap();
|
|
|
|
debug!(LOGGER, "Reset header head to {} at {}",
|
|
|
|
head.last_block_h, head.height);
|
|
|
|
self.store.save_header_head(&head).map_err(From::from)
|
|
|
|
}
|
|
|
|
|
2017-10-28 00:57:04 +03:00
|
|
|
/// returns the last n nodes inserted into the utxo sum tree
|
|
|
|
/// returns sum tree hash plus output itself (as the sum is contained
|
|
|
|
/// in the output anyhow)
|
2017-11-01 02:32:33 +03:00
|
|
|
pub fn get_last_n_utxo(&self, distance: u64) -> Vec<(Hash, Output)> {
|
2017-10-28 00:57:04 +03:00
|
|
|
let mut sumtrees = self.sumtrees.write().unwrap();
|
|
|
|
let mut return_vec = Vec::new();
|
2017-11-01 02:32:33 +03:00
|
|
|
let sum_nodes = sumtrees.last_n_utxo(distance);
|
2017-10-28 00:57:04 +03:00
|
|
|
for sum_commit in sum_nodes {
|
|
|
|
let output = self.store.get_output_by_commit(&sum_commit.sum.commit);
|
|
|
|
return_vec.push((sum_commit.hash, output.unwrap()));
|
|
|
|
}
|
|
|
|
return_vec
|
|
|
|
}
|
|
|
|
|
|
|
|
/// as above, for rangeproofs
|
2017-11-01 02:32:33 +03:00
|
|
|
pub fn get_last_n_rangeproof(&self, distance: u64) -> Vec<HashSum<NoSum<RangeProof>>> {
|
2017-10-28 00:57:04 +03:00
|
|
|
let mut sumtrees = self.sumtrees.write().unwrap();
|
|
|
|
sumtrees.last_n_rangeproof(distance)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// as above, for kernels
|
2017-11-01 02:32:33 +03:00
|
|
|
pub fn get_last_n_kernel(&self, distance: u64) -> Vec<HashSum<NoSum<TxKernel>>> {
|
2017-10-28 00:57:04 +03:00
|
|
|
let mut sumtrees = self.sumtrees.write().unwrap();
|
|
|
|
sumtrees.last_n_kernel(distance)
|
|
|
|
}
|
|
|
|
|
2017-07-04 02:46:25 +03:00
|
|
|
/// Total difficulty at the head of the chain
|
|
|
|
pub fn total_difficulty(&self) -> Difficulty {
|
|
|
|
self.head.lock().unwrap().clone().total_difficulty
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the tip that's also the head of the chain
|
|
|
|
pub fn head(&self) -> Result<Tip, Error> {
|
|
|
|
Ok(self.head.lock().unwrap().clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Block header for the chain head
|
|
|
|
pub fn head_header(&self) -> Result<BlockHeader, Error> {
|
2017-11-01 02:32:33 +03:00
|
|
|
self.store
|
|
|
|
.head_header()
|
|
|
|
.map_err(|e| Error::StoreErr(e, "chain head header".to_owned()))
|
2017-07-04 02:46:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets a block header by hash
|
|
|
|
pub fn get_block(&self, h: &Hash) -> Result<Block, Error> {
|
2017-11-01 02:32:33 +03:00
|
|
|
self.store
|
|
|
|
.get_block(h)
|
|
|
|
.map_err(|e| Error::StoreErr(e, "chain get block".to_owned()))
|
2017-07-04 02:46:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets a block header by hash
|
|
|
|
pub fn get_block_header(&self, h: &Hash) -> Result<BlockHeader, Error> {
|
2017-11-01 02:32:33 +03:00
|
|
|
self.store
|
|
|
|
.get_block_header(h)
|
|
|
|
.map_err(|e| Error::StoreErr(e, "chain get header".to_owned()))
|
2017-07-04 02:46:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the block header at the provided height
|
|
|
|
pub fn get_header_by_height(&self, height: u64) -> Result<BlockHeader, Error> {
|
2017-11-01 02:32:33 +03:00
|
|
|
self.store.get_header_by_height(height).map_err(|e| {
|
|
|
|
Error::StoreErr(e, "chain get header by height".to_owned())
|
|
|
|
})
|
2017-07-04 02:46:25 +03:00
|
|
|
}
|
|
|
|
|
2017-12-05 21:32:57 +03:00
|
|
|
/// Verifies the given block header is actually on the current chain.
|
|
|
|
/// Checks the header_by_height index to verify the header is where we say it is
|
|
|
|
pub fn is_on_current_chain(&self, header: &BlockHeader) -> Result<(), Error> {
|
|
|
|
self.store.is_on_current_chain(header).map_err(|e| {
|
|
|
|
Error::StoreErr(e, "chain is_on_current_chain".to_owned())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-09-12 20:24:24 +03:00
|
|
|
/// Gets the block header by the provided output commitment
|
2017-10-13 07:45:07 +03:00
|
|
|
pub fn get_block_header_by_output_commit(
|
|
|
|
&self,
|
|
|
|
commit: &Commitment,
|
|
|
|
) -> Result<BlockHeader, Error> {
|
2017-09-29 21:44:25 +03:00
|
|
|
self.store
|
|
|
|
.get_block_header_by_output_commit(commit)
|
2017-10-22 10:11:45 +03:00
|
|
|
.map_err(|e| Error::StoreErr(e, "chain get commitment".to_owned()))
|
2017-09-12 20:24:24 +03:00
|
|
|
}
|
2017-08-29 19:32:45 +03:00
|
|
|
|
2017-12-04 22:16:57 +03:00
|
|
|
/// Get the tip of the current "sync" header chain.
|
|
|
|
/// This may be significantly different to current header chain.
|
|
|
|
pub fn get_sync_head(&self) -> Result<Tip, Error> {
|
|
|
|
self.store
|
|
|
|
.get_sync_head()
|
|
|
|
.map_err(|e| Error::StoreErr(e, "chain get sync head".to_owned()))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the tip of the header chain.
|
2017-07-04 02:46:25 +03:00
|
|
|
pub fn get_header_head(&self) -> Result<Tip, Error> {
|
2017-11-01 02:32:33 +03:00
|
|
|
self.store
|
|
|
|
.get_header_head()
|
|
|
|
.map_err(|e| Error::StoreErr(e, "chain get header head".to_owned()))
|
2017-07-04 02:46:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds an iterator on blocks starting from the current chain head and
|
|
|
|
/// running backward. Specialized to return information pertaining to block
|
|
|
|
/// difficulty calculation (timestamp and previous difficulties).
|
|
|
|
pub fn difficulty_iter(&self) -> store::DifficultyIter {
|
|
|
|
let head = self.head.lock().unwrap();
|
|
|
|
store::DifficultyIter::from(head.last_block_h, self.store.clone())
|
|
|
|
}
|
|
|
|
}
|