2016-12-12 22:59:05 +01:00
|
|
|
// Copyright 2015, 2016 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
//! Light client header chain.
|
|
|
|
//!
|
|
|
|
//! Unlike a full node's `BlockChain` this doesn't store much in the database.
|
|
|
|
//! It stores candidates for the last 2048-4096 blocks as well as CHT roots for
|
|
|
|
//! historical blocks all the way to the genesis.
|
|
|
|
//!
|
|
|
|
//! This is separate from the `BlockChain` for two reasons:
|
|
|
|
//! - It stores only headers (and a pruned subset of them)
|
|
|
|
//! - To allow for flexibility in the database layout once that's incorporated.
|
2016-12-13 14:48:03 +01:00
|
|
|
// TODO: use DB instead of memory. DB Layout: just the contents of `candidates`/`headers`
|
|
|
|
//
|
2016-12-12 22:59:05 +01:00
|
|
|
|
|
|
|
use std::collections::{BTreeMap, HashMap};
|
|
|
|
|
2017-01-16 16:55:23 +01:00
|
|
|
use cht;
|
2016-12-13 20:13:16 +01:00
|
|
|
|
|
|
|
use ethcore::block_status::BlockStatus;
|
2016-12-12 22:59:05 +01:00
|
|
|
use ethcore::error::BlockError;
|
|
|
|
use ethcore::ids::BlockId;
|
|
|
|
use ethcore::views::HeaderView;
|
2016-12-23 14:50:42 +01:00
|
|
|
use util::{Bytes, H256, U256, HeapSizeOf, Mutex, RwLock};
|
2016-12-12 22:59:05 +01:00
|
|
|
|
2016-12-13 14:48:03 +01:00
|
|
|
use smallvec::SmallVec;
|
|
|
|
|
2016-12-13 20:13:16 +01:00
|
|
|
/// Store at least this many candidate headers at all times.
|
|
|
|
/// Also functions as the delay for computing CHTs as they aren't
|
|
|
|
/// relevant to any blocks we've got in memory.
|
|
|
|
const HISTORY: u64 = 2048;
|
2016-12-12 22:59:05 +01:00
|
|
|
|
2016-12-13 14:48:03 +01:00
|
|
|
/// Information about a block.
|
2016-12-12 22:59:05 +01:00
|
|
|
#[derive(Debug, Clone)]
|
2016-12-13 14:48:03 +01:00
|
|
|
pub struct BlockDescriptor {
|
|
|
|
/// The block's hash
|
|
|
|
pub hash: H256,
|
|
|
|
/// The block's number
|
|
|
|
pub number: u64,
|
|
|
|
/// The block's total difficulty.
|
|
|
|
pub total_difficulty: U256,
|
2016-12-12 22:59:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// candidate block description.
|
|
|
|
struct Candidate {
|
|
|
|
hash: H256,
|
|
|
|
parent_hash: H256,
|
|
|
|
total_difficulty: U256,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Entry {
|
2016-12-13 14:48:03 +01:00
|
|
|
candidates: SmallVec<[Candidate; 3]>, // 3 arbitrarily chosen
|
2016-12-12 22:59:05 +01:00
|
|
|
canonical_hash: H256,
|
|
|
|
}
|
|
|
|
|
2016-12-23 14:50:42 +01:00
|
|
|
impl HeapSizeOf for Entry {
|
|
|
|
fn heap_size_of_children(&self) -> usize {
|
|
|
|
match self.candidates.spilled() {
|
|
|
|
false => 0,
|
|
|
|
true => self.candidates.capacity() * ::std::mem::size_of::<Candidate>(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-12 22:59:05 +01:00
|
|
|
/// Header chain. See module docs for more details.
|
|
|
|
pub struct HeaderChain {
|
|
|
|
genesis_header: Bytes, // special-case the genesis.
|
|
|
|
candidates: RwLock<BTreeMap<u64, Entry>>,
|
|
|
|
headers: RwLock<HashMap<H256, Bytes>>,
|
2016-12-13 14:48:03 +01:00
|
|
|
best_block: RwLock<BlockDescriptor>,
|
2016-12-12 22:59:05 +01:00
|
|
|
cht_roots: Mutex<Vec<H256>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HeaderChain {
|
|
|
|
/// Create a new header chain given this genesis block.
|
|
|
|
pub fn new(genesis: &[u8]) -> Self {
|
|
|
|
let g_view = HeaderView::new(genesis);
|
|
|
|
|
|
|
|
HeaderChain {
|
|
|
|
genesis_header: genesis.to_owned(),
|
2016-12-13 14:48:03 +01:00
|
|
|
best_block: RwLock::new(BlockDescriptor {
|
2016-12-12 22:59:05 +01:00
|
|
|
hash: g_view.hash(),
|
|
|
|
number: 0,
|
|
|
|
total_difficulty: g_view.difficulty(),
|
|
|
|
}),
|
|
|
|
candidates: RwLock::new(BTreeMap::new()),
|
|
|
|
headers: RwLock::new(HashMap::new()),
|
|
|
|
cht_roots: Mutex::new(Vec::new()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Insert a pre-verified header.
|
2016-12-13 20:13:16 +01:00
|
|
|
///
|
|
|
|
/// This blindly trusts that the data given to it is
|
|
|
|
/// a) valid RLP encoding of a header and
|
|
|
|
/// b) has sensible data contained within it.
|
2016-12-12 22:59:05 +01:00
|
|
|
pub fn insert(&self, header: Bytes) -> Result<(), BlockError> {
|
|
|
|
let view = HeaderView::new(&header);
|
|
|
|
let hash = view.hash();
|
|
|
|
let number = view.number();
|
|
|
|
let parent_hash = view.parent_hash();
|
|
|
|
|
2016-12-16 23:53:20 +01:00
|
|
|
// hold candidates the whole time to guard import order.
|
|
|
|
let mut candidates = self.candidates.write();
|
|
|
|
|
2016-12-12 22:59:05 +01:00
|
|
|
// find parent details.
|
2016-12-27 12:24:18 +01:00
|
|
|
let parent_td =
|
2016-12-12 22:59:05 +01:00
|
|
|
if number == 1 {
|
|
|
|
let g_view = HeaderView::new(&self.genesis_header);
|
|
|
|
g_view.difficulty()
|
|
|
|
} else {
|
2016-12-27 12:24:18 +01:00
|
|
|
candidates.get(&(number - 1))
|
2016-12-12 22:59:05 +01:00
|
|
|
.and_then(|entry| entry.candidates.iter().find(|c| c.hash == parent_hash))
|
2016-12-27 12:24:18 +01:00
|
|
|
.map(|c| c.total_difficulty)
|
|
|
|
.ok_or_else(|| BlockError::UnknownParent(parent_hash))?
|
|
|
|
};
|
2016-12-12 22:59:05 +01:00
|
|
|
|
|
|
|
let total_difficulty = parent_td + view.difficulty();
|
|
|
|
|
|
|
|
// insert headers and candidates entries.
|
2016-12-13 14:48:03 +01:00
|
|
|
candidates.entry(number).or_insert_with(|| Entry { candidates: SmallVec::new(), canonical_hash: hash})
|
2016-12-12 22:59:05 +01:00
|
|
|
.candidates.push(Candidate {
|
|
|
|
hash: hash,
|
|
|
|
parent_hash: parent_hash,
|
|
|
|
total_difficulty: total_difficulty,
|
|
|
|
});
|
|
|
|
|
|
|
|
self.headers.write().insert(hash, header.clone());
|
|
|
|
|
|
|
|
// reorganize ancestors so canonical entries are first in their
|
|
|
|
// respective candidates vectors.
|
|
|
|
if self.best_block.read().total_difficulty < total_difficulty {
|
|
|
|
let mut canon_hash = hash;
|
|
|
|
for (_, entry) in candidates.iter_mut().rev().skip_while(|&(height, _)| *height > number) {
|
|
|
|
if entry.canonical_hash == canon_hash { break; }
|
|
|
|
|
|
|
|
let canon = entry.candidates.iter().find(|x| x.hash == canon_hash)
|
|
|
|
.expect("blocks are only inserted if parent is present; or this is the block we just added; qed");
|
|
|
|
|
2016-12-13 20:13:16 +01:00
|
|
|
// what about reorgs > cht::SIZE + HISTORY?
|
|
|
|
// resetting to the last block of a given CHT should be possible.
|
2016-12-12 22:59:05 +01:00
|
|
|
canon_hash = canon.parent_hash;
|
|
|
|
}
|
|
|
|
|
2016-12-13 14:48:03 +01:00
|
|
|
*self.best_block.write() = BlockDescriptor {
|
2016-12-12 22:59:05 +01:00
|
|
|
hash: hash,
|
|
|
|
number: number,
|
|
|
|
total_difficulty: total_difficulty,
|
|
|
|
};
|
|
|
|
|
|
|
|
// produce next CHT root if it's time.
|
|
|
|
let earliest_era = *candidates.keys().next().expect("at least one era just created; qed");
|
2016-12-13 20:13:16 +01:00
|
|
|
if earliest_era + HISTORY + cht::SIZE <= number {
|
|
|
|
let mut values = Vec::with_capacity(cht::SIZE as usize);
|
2016-12-13 14:48:03 +01:00
|
|
|
{
|
|
|
|
let mut headers = self.headers.write();
|
2016-12-13 20:13:16 +01:00
|
|
|
for i in (0..cht::SIZE).map(|x| x + earliest_era) {
|
2016-12-13 14:48:03 +01:00
|
|
|
let era_entry = candidates.remove(&i)
|
|
|
|
.expect("all eras are sequential with no gaps; qed");
|
|
|
|
|
|
|
|
for ancient in &era_entry.candidates {
|
|
|
|
headers.remove(&ancient.hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
values.push((
|
|
|
|
::rlp::encode(&i).to_vec(),
|
|
|
|
::rlp::encode(&era_entry.canonical_hash).to_vec(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2016-12-12 22:59:05 +01:00
|
|
|
|
|
|
|
let cht_root = ::util::triehash::trie_root(values);
|
2016-12-13 20:13:16 +01:00
|
|
|
debug!(target: "chain", "Produced CHT {} root: {:?}", (earliest_era - 1) % cht::SIZE, cht_root);
|
2016-12-12 22:59:05 +01:00
|
|
|
|
|
|
|
self.cht_roots.lock().push(cht_root);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a block header. In the case of query by number, only canonical blocks
|
|
|
|
/// will be returned.
|
2016-12-13 14:48:03 +01:00
|
|
|
pub fn get_header(&self, id: BlockId) -> Option<Bytes> {
|
2016-12-12 22:59:05 +01:00
|
|
|
match id {
|
|
|
|
BlockId::Earliest | BlockId::Number(0) => Some(self.genesis_header.clone()),
|
|
|
|
BlockId::Hash(hash) => self.headers.read().get(&hash).map(|x| x.to_vec()),
|
|
|
|
BlockId::Number(num) => {
|
|
|
|
if self.best_block.read().number < num { return None }
|
|
|
|
|
|
|
|
self.candidates.read().get(&num).map(|entry| entry.canonical_hash)
|
|
|
|
.and_then(|hash| self.headers.read().get(&hash).map(|x| x.to_vec()))
|
|
|
|
}
|
|
|
|
BlockId::Latest | BlockId::Pending => {
|
|
|
|
let hash = self.best_block.read().hash;
|
|
|
|
self.headers.read().get(&hash).map(|x| x.to_vec())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 14:48:03 +01:00
|
|
|
/// Get the nth CHT root, if it's been computed.
|
|
|
|
///
|
|
|
|
/// CHT root 0 is from block `1..2048`.
|
|
|
|
/// CHT root 1 is from block `2049..4096`
|
|
|
|
/// and so on.
|
|
|
|
///
|
|
|
|
/// This is because it's assumed that the genesis hash is known,
|
|
|
|
/// so including it within a CHT would be redundant.
|
|
|
|
pub fn cht_root(&self, n: usize) -> Option<H256> {
|
|
|
|
self.cht_roots.lock().get(n).map(|h| h.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the genesis hash.
|
|
|
|
pub fn genesis_hash(&self) -> H256 {
|
2016-12-27 12:24:18 +01:00
|
|
|
::util::Hashable::sha3(&self.genesis_header)
|
2016-12-13 14:48:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the best block's data.
|
|
|
|
pub fn best_block(&self) -> BlockDescriptor {
|
|
|
|
self.best_block.read().clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If there is a gap between the genesis and the rest
|
|
|
|
/// of the stored blocks, return the first post-gap block.
|
|
|
|
pub fn first_block(&self) -> Option<BlockDescriptor> {
|
|
|
|
let candidates = self.candidates.read();
|
|
|
|
match candidates.iter().next() {
|
|
|
|
None | Some((&1, _)) => None,
|
|
|
|
Some((&height, entry)) => Some(BlockDescriptor {
|
|
|
|
number: height,
|
|
|
|
hash: entry.canonical_hash,
|
|
|
|
total_difficulty: entry.candidates.iter().find(|x| x.hash == entry.canonical_hash)
|
|
|
|
.expect("entry always stores canonical candidate; qed").total_difficulty,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2016-12-13 20:13:16 +01:00
|
|
|
|
|
|
|
/// Get block status.
|
|
|
|
pub fn status(&self, hash: &H256) -> BlockStatus {
|
|
|
|
match self.headers.read().contains_key(hash) {
|
|
|
|
true => BlockStatus::InChain,
|
|
|
|
false => BlockStatus::Unknown,
|
|
|
|
}
|
|
|
|
}
|
2016-12-13 14:48:03 +01:00
|
|
|
}
|
|
|
|
|
2016-12-23 14:50:42 +01:00
|
|
|
impl HeapSizeOf for HeaderChain {
|
|
|
|
fn heap_size_of_children(&self) -> usize {
|
|
|
|
self.candidates.read().heap_size_of_children() +
|
|
|
|
self.headers.read().heap_size_of_children() +
|
|
|
|
self.cht_roots.lock().heap_size_of_children()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 14:48:03 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::HeaderChain;
|
|
|
|
use ethcore::ids::BlockId;
|
|
|
|
use ethcore::header::Header;
|
|
|
|
use ethcore::spec::Spec;
|
|
|
|
|
|
|
|
#[test]
|
2016-12-13 20:13:16 +01:00
|
|
|
fn basic_chain() {
|
2016-12-13 14:48:03 +01:00
|
|
|
let spec = Spec::new_test();
|
|
|
|
let genesis_header = spec.genesis_header();
|
|
|
|
|
|
|
|
let chain = HeaderChain::new(&::rlp::encode(&genesis_header));
|
|
|
|
|
|
|
|
let mut parent_hash = genesis_header.hash();
|
|
|
|
let mut rolling_timestamp = genesis_header.timestamp();
|
|
|
|
for i in 1..10000 {
|
|
|
|
let mut header = Header::new();
|
|
|
|
header.set_parent_hash(parent_hash);
|
|
|
|
header.set_number(i);
|
|
|
|
header.set_timestamp(rolling_timestamp);
|
|
|
|
header.set_difficulty(*genesis_header.difficulty() * i.into());
|
|
|
|
|
2016-12-13 21:09:57 +01:00
|
|
|
chain.insert(::rlp::encode(&header).to_vec()).unwrap();
|
2016-12-13 14:48:03 +01:00
|
|
|
|
|
|
|
parent_hash = header.hash();
|
|
|
|
rolling_timestamp += 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(chain.get_header(BlockId::Number(10)).is_none());
|
|
|
|
assert!(chain.get_header(BlockId::Number(9000)).is_some());
|
|
|
|
assert!(chain.cht_root(2).is_some());
|
|
|
|
assert!(chain.cht_root(3).is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reorganize() {
|
|
|
|
let spec = Spec::new_test();
|
|
|
|
let genesis_header = spec.genesis_header();
|
|
|
|
|
|
|
|
let chain = HeaderChain::new(&::rlp::encode(&genesis_header));
|
|
|
|
|
|
|
|
let mut parent_hash = genesis_header.hash();
|
|
|
|
let mut rolling_timestamp = genesis_header.timestamp();
|
|
|
|
for i in 1..6 {
|
|
|
|
let mut header = Header::new();
|
|
|
|
header.set_parent_hash(parent_hash);
|
|
|
|
header.set_number(i);
|
|
|
|
header.set_timestamp(rolling_timestamp);
|
|
|
|
header.set_difficulty(*genesis_header.difficulty() * i.into());
|
|
|
|
|
|
|
|
chain.insert(::rlp::encode(&header).to_vec()).unwrap();
|
|
|
|
|
|
|
|
parent_hash = header.hash();
|
|
|
|
rolling_timestamp += 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut rolling_timestamp = rolling_timestamp;
|
|
|
|
let mut parent_hash = parent_hash;
|
|
|
|
for i in 6..16 {
|
|
|
|
let mut header = Header::new();
|
|
|
|
header.set_parent_hash(parent_hash);
|
|
|
|
header.set_number(i);
|
|
|
|
header.set_timestamp(rolling_timestamp);
|
|
|
|
header.set_difficulty(*genesis_header.difficulty() * i.into());
|
|
|
|
|
|
|
|
chain.insert(::rlp::encode(&header).to_vec()).unwrap();
|
|
|
|
|
|
|
|
parent_hash = header.hash();
|
|
|
|
rolling_timestamp += 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(chain.best_block().number, 15);
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut rolling_timestamp = rolling_timestamp;
|
|
|
|
let mut parent_hash = parent_hash;
|
|
|
|
|
|
|
|
// import a shorter chain which has better TD.
|
|
|
|
for i in 6..13 {
|
|
|
|
let mut header = Header::new();
|
|
|
|
header.set_parent_hash(parent_hash);
|
|
|
|
header.set_number(i);
|
|
|
|
header.set_timestamp(rolling_timestamp);
|
|
|
|
header.set_difficulty(*genesis_header.difficulty() * (i * i).into());
|
|
|
|
|
|
|
|
chain.insert(::rlp::encode(&header).to_vec()).unwrap();
|
|
|
|
|
|
|
|
parent_hash = header.hash();
|
|
|
|
rolling_timestamp += 11;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(chain.best_block().number, 12);
|
|
|
|
}
|
2016-12-12 22:59:05 +01:00
|
|
|
}
|