2016-10-22 21:35:48 +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.
|
|
|
|
|
|
2016-10-21 03:06:12 +03:00
|
|
|
|
//! Implementation of the chain block acceptance (or refusal) pipeline.
|
|
|
|
|
|
2017-09-28 02:46:32 +03:00
|
|
|
|
use std::sync::{Arc, RwLock};
|
2016-12-19 02:51:54 +03:00
|
|
|
|
|
2016-11-27 23:31:15 +03:00
|
|
|
|
use time;
|
2016-10-21 03:06:12 +03:00
|
|
|
|
|
2016-11-16 04:29:42 +03:00
|
|
|
|
use core::consensus;
|
2016-12-27 02:39:31 +03:00
|
|
|
|
use core::core::hash::{Hash, Hashed};
|
2017-11-01 02:32:33 +03:00
|
|
|
|
use core::core::{Block, BlockHeader};
|
2017-11-14 03:45:10 +03:00
|
|
|
|
use core::core::target::Difficulty;
|
2017-09-12 20:24:24 +03:00
|
|
|
|
use core::core::transaction;
|
2017-12-04 22:16:57 +03:00
|
|
|
|
use grin_store;
|
2017-07-04 02:46:25 +03:00
|
|
|
|
use types::*;
|
2016-10-21 03:06:12 +03:00
|
|
|
|
use store;
|
2017-09-28 02:46:32 +03:00
|
|
|
|
use sumtree;
|
2017-08-09 19:40:23 +03:00
|
|
|
|
use core::global;
|
2017-10-12 19:56:44 +03:00
|
|
|
|
use util::LOGGER;
|
2016-10-21 03:06:12 +03:00
|
|
|
|
|
|
|
|
|
/// Contextual information required to process a new block and either reject or
|
|
|
|
|
/// accept it.
|
2016-12-19 02:51:54 +03:00
|
|
|
|
pub struct BlockContext {
|
2017-08-10 03:54:10 +03:00
|
|
|
|
/// The options
|
2017-07-04 02:46:25 +03:00
|
|
|
|
pub opts: Options,
|
2017-08-10 03:54:10 +03:00
|
|
|
|
/// The store
|
2017-07-04 02:46:25 +03:00
|
|
|
|
pub store: Arc<ChainStore>,
|
2017-08-10 03:54:10 +03:00
|
|
|
|
/// The head
|
2017-07-04 02:46:25 +03:00
|
|
|
|
pub head: Tip,
|
2017-08-22 21:23:54 +03:00
|
|
|
|
/// The POW verification function
|
|
|
|
|
pub pow_verifier: fn(&BlockHeader, u32) -> bool,
|
2017-09-28 02:46:32 +03:00
|
|
|
|
/// MMR sum tree states
|
|
|
|
|
pub sumtrees: Arc<RwLock<sumtree::SumTrees>>,
|
2016-10-21 03:06:12 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-21 04:35:04 +03:00
|
|
|
|
/// Runs the block processing pipeline, including validation and finding a
|
|
|
|
|
/// place for the new block in the chain. Returns the new
|
|
|
|
|
/// chain head if updated.
|
2017-07-04 02:46:25 +03:00
|
|
|
|
pub fn process_block(b: &Block, mut ctx: BlockContext) -> Result<Option<Tip>, Error> {
|
2016-10-21 03:06:12 +03:00
|
|
|
|
// TODO should just take a promise for a block with a full header so we don't
|
2017-11-01 02:32:33 +03:00
|
|
|
|
// spend resources reading the full block when its header is invalid
|
2016-10-21 03:06:12 +03:00
|
|
|
|
|
2017-11-18 23:34:05 +03:00
|
|
|
|
debug!(
|
2017-10-12 19:56:44 +03:00
|
|
|
|
LOGGER,
|
2017-11-30 18:27:50 +03:00
|
|
|
|
"pipe: process_block {} at {} with {} inputs and {} outputs.",
|
2017-07-28 00:13:34 +03:00
|
|
|
|
b.hash(),
|
|
|
|
|
b.header.height,
|
|
|
|
|
b.inputs.len(),
|
|
|
|
|
b.outputs.len()
|
|
|
|
|
);
|
2017-07-04 02:46:25 +03:00
|
|
|
|
check_known(b.hash(), &mut ctx)?;
|
2017-02-08 00:50:01 +03:00
|
|
|
|
|
2017-10-18 10:19:44 +03:00
|
|
|
|
validate_header(&b.header, &mut ctx)?;
|
2017-09-12 20:24:24 +03:00
|
|
|
|
|
2017-12-04 22:16:57 +03:00
|
|
|
|
// valid header, now check we actually have the previous block in the store
|
|
|
|
|
// not just the header but the block itself
|
|
|
|
|
// we cannot assume we can use the chain head for this as we may be dealing with a fork
|
|
|
|
|
// we cannot use heights here as the fork may have jumped in height
|
|
|
|
|
match ctx.store.get_block(&b.header.previous) {
|
|
|
|
|
Ok(_) => {},
|
|
|
|
|
Err(grin_store::Error::NotFoundErr) => {
|
|
|
|
|
return Err(Error::Orphan);
|
|
|
|
|
},
|
|
|
|
|
Err(e) => {
|
|
|
|
|
return Err(Error::StoreErr(e, "pipe get previous".to_owned()));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// valid header and we have a previous block, time to take the lock on the sum trees
|
2017-09-28 02:46:32 +03:00
|
|
|
|
let local_sumtrees = ctx.sumtrees.clone();
|
|
|
|
|
let mut sumtrees = local_sumtrees.write().unwrap();
|
2017-10-22 10:11:45 +03:00
|
|
|
|
|
|
|
|
|
// update head now that we're in the lock
|
2017-11-01 02:32:33 +03:00
|
|
|
|
ctx.head = ctx.store
|
|
|
|
|
.head()
|
|
|
|
|
.map_err(|e| Error::StoreErr(e, "pipe reload head".to_owned()))?;
|
2017-10-22 10:11:45 +03:00
|
|
|
|
|
|
|
|
|
// start a chain extension unit of work dependent on the success of the
|
2017-11-01 02:32:33 +03:00
|
|
|
|
// internal validation and saving operations
|
2017-09-28 02:46:32 +03:00
|
|
|
|
sumtree::extending(&mut sumtrees, |mut extension| {
|
|
|
|
|
validate_block(b, &mut ctx, &mut extension)?;
|
|
|
|
|
debug!(
|
2017-10-12 19:56:44 +03:00
|
|
|
|
LOGGER,
|
2017-11-30 18:27:50 +03:00
|
|
|
|
"pipe: proces_block {} at {} is valid, save and append.",
|
|
|
|
|
b.hash(),
|
2017-09-28 02:46:32 +03:00
|
|
|
|
b.header.height,
|
|
|
|
|
);
|
2017-07-04 02:46:25 +03:00
|
|
|
|
|
2017-09-28 02:46:32 +03:00
|
|
|
|
add_block(b, &mut ctx)?;
|
|
|
|
|
let h = update_head(b, &mut ctx)?;
|
|
|
|
|
if h.is_none() {
|
|
|
|
|
extension.force_rollback();
|
|
|
|
|
}
|
|
|
|
|
Ok(h)
|
|
|
|
|
})
|
2016-10-21 03:06:12 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 22:16:57 +03:00
|
|
|
|
/// Process the block header.
|
|
|
|
|
/// This is only ever used during sync and uses a context based on sync_head.
|
|
|
|
|
pub fn sync_block_header(
|
|
|
|
|
bh: &BlockHeader,
|
|
|
|
|
mut sync_ctx: BlockContext,
|
|
|
|
|
mut header_ctx: BlockContext,
|
|
|
|
|
) -> Result<Option<Tip>, Error> {
|
2017-11-18 23:34:05 +03:00
|
|
|
|
debug!(
|
2017-10-12 19:56:44 +03:00
|
|
|
|
LOGGER,
|
2017-12-04 22:16:57 +03:00
|
|
|
|
"pipe: sync_block_header {} at {}",
|
2017-07-28 00:13:34 +03:00
|
|
|
|
bh.hash(),
|
|
|
|
|
bh.height
|
|
|
|
|
);
|
2017-07-04 02:46:25 +03:00
|
|
|
|
|
2017-12-04 22:16:57 +03:00
|
|
|
|
validate_header(&bh, &mut sync_ctx)?;
|
|
|
|
|
add_block_header(bh, &mut sync_ctx)?;
|
|
|
|
|
|
|
|
|
|
// TODO - confirm this is needed during sync process (I don't see how it is)
|
|
|
|
|
// we do not touch the sumtrees when syncing headers
|
2017-09-28 02:46:32 +03:00
|
|
|
|
// just taking the shared lock
|
2017-12-04 22:16:57 +03:00
|
|
|
|
let _ = header_ctx.sumtrees.write().unwrap();
|
2017-09-28 02:46:32 +03:00
|
|
|
|
|
2017-12-04 22:16:57 +03:00
|
|
|
|
// now update the header_head (if new header with most work) and the sync_head (always)
|
2017-12-11 19:03:21 +03:00
|
|
|
|
update_header_head(bh, &mut header_ctx)?;
|
2017-12-04 22:16:57 +03:00
|
|
|
|
update_sync_head(bh, &mut sync_ctx)
|
2017-02-08 00:50:01 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-21 04:35:04 +03:00
|
|
|
|
/// Quick in-memory check to fast-reject any block we've already handled
|
|
|
|
|
/// recently. Keeps duplicates from the network in check.
|
|
|
|
|
fn check_known(bh: Hash, ctx: &mut BlockContext) -> Result<(), Error> {
|
2017-01-10 02:16:44 +03:00
|
|
|
|
// TODO ring buffer of the last few blocks that came through here
|
2016-12-21 04:35:04 +03:00
|
|
|
|
if bh == ctx.head.last_block_h || bh == ctx.head.prev_block_h {
|
|
|
|
|
return Err(Error::Unfit("already known".to_string()));
|
|
|
|
|
}
|
2017-04-28 07:59:53 +03:00
|
|
|
|
if let Ok(b) = ctx.store.get_block(&bh) {
|
|
|
|
|
// there is a window where a block can be saved but the chain head not
|
2017-11-30 18:27:50 +03:00
|
|
|
|
// updated yet, we plug that window here by re-accepting the block
|
2017-04-28 07:59:53 +03:00
|
|
|
|
if b.header.total_difficulty <= ctx.head.total_difficulty {
|
|
|
|
|
return Err(Error::Unfit("already in store".to_string()));
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-21 04:35:04 +03:00
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2016-10-21 03:06:12 +03:00
|
|
|
|
|
2017-12-16 06:19:04 +03:00
|
|
|
|
/// First level of block validation that only needs to act on the block header
|
2016-10-21 03:06:12 +03:00
|
|
|
|
/// to make it as cheap as possible. The different validations are also
|
|
|
|
|
/// arranged by order of cost to have as little DoS surface as possible.
|
2016-11-27 23:31:15 +03:00
|
|
|
|
/// TODO require only the block header (with length information)
|
2017-02-08 00:50:01 +03:00
|
|
|
|
fn validate_header(header: &BlockHeader, ctx: &mut BlockContext) -> Result<(), Error> {
|
2016-10-21 03:06:12 +03:00
|
|
|
|
|
2017-10-10 03:08:17 +03:00
|
|
|
|
// check version, enforces scheduled hard fork
|
|
|
|
|
if !consensus::valid_header_version(header.height, header.version) {
|
2017-10-11 21:12:01 +03:00
|
|
|
|
error!(
|
2017-10-12 19:56:44 +03:00
|
|
|
|
LOGGER,
|
2017-10-11 21:12:01 +03:00
|
|
|
|
"Invalid block header version received ({}), maybe update Grin?",
|
|
|
|
|
header.version
|
|
|
|
|
);
|
2017-10-10 03:08:17 +03:00
|
|
|
|
return Err(Error::InvalidBlockVersion(header.version));
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-01 02:32:33 +03:00
|
|
|
|
if header.timestamp
|
|
|
|
|
> time::now_utc() + time::Duration::seconds(12 * (consensus::BLOCK_TIME_SEC as i64))
|
2017-10-17 00:23:10 +03:00
|
|
|
|
{
|
2017-10-10 03:08:17 +03:00
|
|
|
|
// refuse blocks more than 12 blocks intervals in future (as in bitcoin)
|
2017-12-16 06:19:04 +03:00
|
|
|
|
// TODO add warning in p2p code if local time is too different from peers
|
2017-10-10 03:08:17 +03:00
|
|
|
|
return Err(Error::InvalidBlockTime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !ctx.opts.intersects(SKIP_POW) {
|
2017-11-17 02:17:56 +03:00
|
|
|
|
let cycle_size = global::sizeshift();
|
|
|
|
|
|
2017-11-30 18:27:50 +03:00
|
|
|
|
debug!(LOGGER, "pipe: validate_header cuckoo size {}", cycle_size);
|
2017-10-10 03:08:17 +03:00
|
|
|
|
if !(ctx.pow_verifier)(header, cycle_size as u32) {
|
|
|
|
|
return Err(Error::InvalidPow);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// first I/O cost, better as late as possible
|
2017-12-14 00:52:21 +03:00
|
|
|
|
let prev = match ctx.store.get_block_header(&header.previous) {
|
|
|
|
|
Ok(prev) => Ok(prev),
|
|
|
|
|
Err(grin_store::Error::NotFoundErr) => Err(Error::Orphan),
|
|
|
|
|
Err(e) =>{
|
|
|
|
|
Err(Error::StoreErr(e, format!("previous header {}", header.previous)))
|
|
|
|
|
}
|
|
|
|
|
}?;
|
2016-11-17 04:03:23 +03:00
|
|
|
|
|
2017-01-10 02:16:44 +03:00
|
|
|
|
if header.height != prev.height + 1 {
|
|
|
|
|
return Err(Error::InvalidBlockHeight);
|
|
|
|
|
}
|
2017-12-16 06:19:04 +03:00
|
|
|
|
|
|
|
|
|
// TODO - get rid of the automated testing mode check here somehow
|
2017-09-29 21:44:25 +03:00
|
|
|
|
if header.timestamp <= prev.timestamp && !global::is_automated_testing_mode() {
|
2016-11-30 05:45:39 +03:00
|
|
|
|
// prevent time warp attacks and some timestamp manipulations by forcing strict
|
2017-12-14 00:52:21 +03:00
|
|
|
|
// time progression (but not in CI mode)
|
2016-11-17 04:03:23 +03:00
|
|
|
|
return Err(Error::InvalidBlockTime);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-10 07:30:02 +03:00
|
|
|
|
if !ctx.opts.intersects(SKIP_POW) {
|
|
|
|
|
// verify the proof of work and related parameters
|
2016-12-27 02:39:31 +03:00
|
|
|
|
|
2017-11-14 03:45:10 +03:00
|
|
|
|
// explicit check to ensure we are not below the minimum difficulty
|
|
|
|
|
// we will also check difficulty based on next_difficulty later on
|
|
|
|
|
if header.difficulty < Difficulty::minimum() {
|
|
|
|
|
return Err(Error::DifficultyTooLow);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-16 06:19:04 +03:00
|
|
|
|
// explicit check to ensure total_difficulty has increased by exactly
|
|
|
|
|
// the difficulty of the previous block
|
2017-01-10 07:30:02 +03:00
|
|
|
|
if header.total_difficulty != prev.total_difficulty.clone() + prev.pow.to_difficulty() {
|
|
|
|
|
return Err(Error::WrongTotalDifficulty);
|
|
|
|
|
}
|
2016-10-21 03:06:12 +03:00
|
|
|
|
|
2017-12-16 06:19:04 +03:00
|
|
|
|
// now check that the difficulty is not less than that calculated by the
|
|
|
|
|
// difficulty iterator based on the previous block
|
2017-06-19 18:59:56 +03:00
|
|
|
|
let diff_iter = store::DifficultyIter::from(header.previous, ctx.store.clone());
|
2017-11-01 02:32:33 +03:00
|
|
|
|
let difficulty =
|
|
|
|
|
consensus::next_difficulty(diff_iter).map_err(|e| Error::Other(e.to_string()))?;
|
2017-01-10 07:30:02 +03:00
|
|
|
|
if header.difficulty < difficulty {
|
|
|
|
|
return Err(Error::DifficultyTooLow);
|
|
|
|
|
}
|
2016-10-21 03:06:12 +03:00
|
|
|
|
}
|
2016-11-17 04:03:23 +03:00
|
|
|
|
|
2016-11-16 04:29:42 +03:00
|
|
|
|
Ok(())
|
2016-10-21 03:06:12 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-10 02:16:44 +03:00
|
|
|
|
/// Fully validate the block content.
|
2017-10-17 00:23:10 +03:00
|
|
|
|
fn validate_block(
|
|
|
|
|
b: &Block,
|
|
|
|
|
ctx: &mut BlockContext,
|
|
|
|
|
ext: &mut sumtree::Extension,
|
|
|
|
|
) -> Result<(), Error> {
|
2017-09-28 02:46:32 +03:00
|
|
|
|
// main isolated block validation, checks all commitment sums and sigs
|
2017-11-09 22:26:45 +03:00
|
|
|
|
try!(b.validate().map_err(&Error::InvalidBlockProof));
|
2017-09-12 20:24:24 +03:00
|
|
|
|
|
2017-09-28 02:46:32 +03:00
|
|
|
|
// apply the new block to the MMR trees and check the new root hashes
|
|
|
|
|
if b.header.previous == ctx.head.last_block_h {
|
|
|
|
|
// standard head extension
|
|
|
|
|
ext.apply_block(b)?;
|
|
|
|
|
} else {
|
|
|
|
|
// extending a fork, first identify the block where forking occurred
|
2017-11-30 18:27:50 +03:00
|
|
|
|
// keeping the hashes of blocks along the fork
|
2017-09-28 02:46:32 +03:00
|
|
|
|
let mut current = b.header.previous;
|
|
|
|
|
let mut hashes = vec![];
|
|
|
|
|
loop {
|
|
|
|
|
let curr_header = ctx.store.get_block_header(¤t)?;
|
2017-12-05 21:32:57 +03:00
|
|
|
|
|
|
|
|
|
if let Ok(_) = ctx.store.is_on_current_chain(&curr_header) {
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
hashes.insert(0, curr_header.hash());
|
|
|
|
|
current = curr_header.previous;
|
2017-09-28 02:46:32 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let forked_block = ctx.store.get_block(¤t)?;
|
2017-11-15 23:37:40 +03:00
|
|
|
|
|
|
|
|
|
debug!(
|
|
|
|
|
LOGGER,
|
|
|
|
|
"validate_block: forked_block: {} at {}",
|
|
|
|
|
forked_block.header.hash(),
|
|
|
|
|
forked_block.header.height,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// rewind the sum trees up to the forking block
|
|
|
|
|
ext.rewind(&forked_block)?;
|
2017-09-28 02:46:32 +03:00
|
|
|
|
|
|
|
|
|
// apply all forked blocks, including this new one
|
|
|
|
|
for h in hashes {
|
2017-12-10 00:43:42 +03:00
|
|
|
|
let fb = ctx.store.get_block(&h).map_err(|e| {
|
|
|
|
|
Error::StoreErr(e, format!("getting forked blocks"))
|
|
|
|
|
})?;
|
2017-09-28 02:46:32 +03:00
|
|
|
|
ext.apply_block(&fb)?;
|
|
|
|
|
}
|
|
|
|
|
ext.apply_block(&b)?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let (utxo_root, rproof_root, kernel_root) = ext.roots();
|
2017-11-01 02:32:33 +03:00
|
|
|
|
if utxo_root.hash != b.header.utxo_root || rproof_root.hash != b.header.range_proof_root
|
|
|
|
|
|| kernel_root.hash != b.header.kernel_root
|
2017-10-17 00:23:10 +03:00
|
|
|
|
{
|
2017-10-22 10:11:45 +03:00
|
|
|
|
ext.dump(false);
|
2017-11-15 23:37:40 +03:00
|
|
|
|
|
|
|
|
|
debug!(
|
|
|
|
|
LOGGER,
|
|
|
|
|
"validate_block: utxo roots - {:?}, {:?}",
|
|
|
|
|
utxo_root.hash,
|
|
|
|
|
b.header.utxo_root,
|
|
|
|
|
);
|
|
|
|
|
debug!(
|
|
|
|
|
LOGGER,
|
|
|
|
|
"validate_block: rproof roots - {:?}, {:?}",
|
|
|
|
|
rproof_root.hash,
|
|
|
|
|
b.header.range_proof_root,
|
|
|
|
|
);
|
|
|
|
|
debug!(
|
|
|
|
|
LOGGER,
|
|
|
|
|
"validate_block: kernel roots - {:?}, {:?}",
|
|
|
|
|
kernel_root.hash,
|
|
|
|
|
b.header.kernel_root,
|
|
|
|
|
);
|
|
|
|
|
|
2017-09-28 02:46:32 +03:00
|
|
|
|
return Err(Error::InvalidRoot);
|
|
|
|
|
}
|
2017-04-28 07:59:53 +03:00
|
|
|
|
|
2017-10-11 21:12:01 +03:00
|
|
|
|
// check for any outputs with lock_heights greater than current block height
|
2017-09-28 02:46:32 +03:00
|
|
|
|
for input in &b.inputs {
|
2017-09-12 20:24:24 +03:00
|
|
|
|
if let Ok(output) = ctx.store.get_output_by_commit(&input.commitment()) {
|
|
|
|
|
if output.features.contains(transaction::COINBASE_OUTPUT) {
|
2017-11-01 02:32:33 +03:00
|
|
|
|
if let Ok(output_header) = ctx.store
|
|
|
|
|
.get_block_header_by_output_commit(&input.commitment())
|
2017-10-17 00:23:10 +03:00
|
|
|
|
{
|
2017-10-04 20:44:22 +03:00
|
|
|
|
if b.header.height <= output_header.height + global::coinbase_maturity() {
|
2017-09-12 20:24:24 +03:00
|
|
|
|
return Err(Error::ImmatureCoinbase);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
2017-09-29 21:44:25 +03:00
|
|
|
|
}
|
2017-09-12 20:24:24 +03:00
|
|
|
|
|
2016-11-16 04:29:42 +03:00
|
|
|
|
Ok(())
|
2016-10-21 03:06:12 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-10 02:16:44 +03:00
|
|
|
|
/// Officially adds the block to our chain.
|
2016-11-16 04:29:42 +03:00
|
|
|
|
fn add_block(b: &Block, ctx: &mut BlockContext) -> Result<(), Error> {
|
2017-11-01 02:32:33 +03:00
|
|
|
|
ctx.store
|
|
|
|
|
.save_block(b)
|
|
|
|
|
.map_err(|e| Error::StoreErr(e, "pipe save block".to_owned()))
|
2016-10-21 03:06:12 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-08 00:50:01 +03:00
|
|
|
|
/// Officially adds the block header to our header chain.
|
|
|
|
|
fn add_block_header(bh: &BlockHeader, ctx: &mut BlockContext) -> Result<(), Error> {
|
2017-11-01 02:32:33 +03:00
|
|
|
|
ctx.store
|
|
|
|
|
.save_block_header(bh)
|
|
|
|
|
.map_err(|e| Error::StoreErr(e, "pipe save header".to_owned()))
|
2017-02-08 00:50:01 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-10 02:16:44 +03:00
|
|
|
|
/// Directly updates the head if we've just appended a new block to it or handle
|
|
|
|
|
/// the situation where we've just added enough work to have a fork with more
|
|
|
|
|
/// work than the head.
|
|
|
|
|
fn update_head(b: &Block, ctx: &mut BlockContext) -> Result<Option<Tip>, Error> {
|
|
|
|
|
// if we made a fork with more work than the head (which should also be true
|
2017-11-30 18:27:50 +03:00
|
|
|
|
// when extending the head), update it
|
2017-02-08 00:50:01 +03:00
|
|
|
|
let tip = Tip::from_block(&b.header);
|
2017-01-10 02:16:44 +03:00
|
|
|
|
if tip.total_difficulty > ctx.head.total_difficulty {
|
2017-04-28 07:59:53 +03:00
|
|
|
|
// update the block height index
|
2017-11-01 02:32:33 +03:00
|
|
|
|
ctx.store
|
2017-12-16 03:27:37 +03:00
|
|
|
|
.setup_height(&b.header, &ctx.head)
|
2017-11-01 02:32:33 +03:00
|
|
|
|
.map_err(|e| Error::StoreErr(e, "pipe setup height".to_owned()))?;
|
|
|
|
|
|
|
|
|
|
// in sync mode, only update the "body chain", otherwise update both the
|
2017-11-30 18:27:50 +03:00
|
|
|
|
// "header chain" and "body chain", updating the header chain in sync resets
|
|
|
|
|
// all additional "future" headers we've received
|
2017-11-01 02:32:33 +03:00
|
|
|
|
if ctx.opts.intersects(SYNC) {
|
|
|
|
|
ctx.store
|
|
|
|
|
.save_body_head(&tip)
|
|
|
|
|
.map_err(|e| Error::StoreErr(e, "pipe save body".to_owned()))?;
|
|
|
|
|
} else {
|
|
|
|
|
ctx.store
|
|
|
|
|
.save_head(&tip)
|
|
|
|
|
.map_err(|e| Error::StoreErr(e, "pipe save head".to_owned()))?;
|
|
|
|
|
}
|
2017-01-10 02:16:44 +03:00
|
|
|
|
ctx.head = tip.clone();
|
2017-12-18 16:17:11 +03:00
|
|
|
|
debug!(
|
2017-11-01 02:32:33 +03:00
|
|
|
|
LOGGER,
|
|
|
|
|
"Updated head to {} at {}.",
|
|
|
|
|
b.hash(),
|
|
|
|
|
b.header.height
|
|
|
|
|
);
|
2017-12-18 16:17:11 +03:00
|
|
|
|
if b.header.height % 500 == 0 {
|
|
|
|
|
info!(LOGGER, "pipe: chain head reached {} @ {} [{}]",
|
|
|
|
|
b.header.height, b.header.difficulty, b.hash());
|
|
|
|
|
}
|
2017-01-10 02:16:44 +03:00
|
|
|
|
Ok(Some(tip))
|
|
|
|
|
} else {
|
|
|
|
|
Ok(None)
|
|
|
|
|
}
|
2016-10-21 03:06:12 +03:00
|
|
|
|
}
|
2017-02-08 00:50:01 +03:00
|
|
|
|
|
2017-12-04 22:16:57 +03:00
|
|
|
|
/// Update the sync head so we can keep syncing from where we left off.
|
|
|
|
|
fn update_sync_head(bh: &BlockHeader, ctx: &mut BlockContext) -> Result<Option<Tip>, Error> {
|
|
|
|
|
let tip = Tip::from_block(bh);
|
|
|
|
|
ctx.store
|
|
|
|
|
.save_sync_head(&tip)
|
|
|
|
|
.map_err(|e| Error::StoreErr(e, "pipe save sync head".to_owned()))?;
|
|
|
|
|
ctx.head = tip.clone();
|
2017-12-18 16:17:11 +03:00
|
|
|
|
debug!(
|
2017-12-04 22:16:57 +03:00
|
|
|
|
LOGGER,
|
|
|
|
|
"pipe: updated sync head to {} at {}.",
|
|
|
|
|
bh.hash(),
|
|
|
|
|
bh.height,
|
|
|
|
|
);
|
2017-12-18 16:17:11 +03:00
|
|
|
|
if bh.height % 1000 == 0 {
|
|
|
|
|
info!(LOGGER, "pipe: sync head reached {} [{}]", bh.height, bh.hash());
|
|
|
|
|
}
|
2017-12-04 22:16:57 +03:00
|
|
|
|
Ok(Some(tip))
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-08 00:50:01 +03:00
|
|
|
|
fn update_header_head(bh: &BlockHeader, ctx: &mut BlockContext) -> Result<Option<Tip>, Error> {
|
|
|
|
|
let tip = Tip::from_block(bh);
|
2017-12-04 22:16:57 +03:00
|
|
|
|
debug!(LOGGER, "pipe: update_header_head {},{}", tip.total_difficulty, ctx.head.total_difficulty);
|
2017-02-08 00:50:01 +03:00
|
|
|
|
if tip.total_difficulty > ctx.head.total_difficulty {
|
2017-11-01 02:32:33 +03:00
|
|
|
|
ctx.store
|
|
|
|
|
.save_header_head(&tip)
|
|
|
|
|
.map_err(|e| Error::StoreErr(e, "pipe save header head".to_owned()))?;
|
2017-02-08 00:50:01 +03:00
|
|
|
|
ctx.head = tip.clone();
|
2017-12-18 16:17:11 +03:00
|
|
|
|
debug!(
|
2017-10-12 19:56:44 +03:00
|
|
|
|
LOGGER,
|
2017-12-04 22:16:57 +03:00
|
|
|
|
"pipe: updated header head to {} at {}.",
|
2017-07-28 00:13:34 +03:00
|
|
|
|
bh.hash(),
|
2017-12-04 22:16:57 +03:00
|
|
|
|
bh.height,
|
|
|
|
|
);
|
2017-02-08 00:50:01 +03:00
|
|
|
|
Ok(Some(tip))
|
|
|
|
|
} else {
|
|
|
|
|
Ok(None)
|
|
|
|
|
}
|
|
|
|
|
}
|