2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-12-12 22:59:05 +01:00
|
|
|
// 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.
|
|
|
|
|
2017-03-21 20:23:58 +01:00
|
|
|
use std::collections::BTreeMap;
|
2017-03-21 19:45:52 +01:00
|
|
|
use std::sync::Arc;
|
2016-12-12 22:59:05 +01:00
|
|
|
|
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;
|
2017-02-03 15:32:22 +01:00
|
|
|
use ethcore::encoded;
|
|
|
|
use ethcore::header::Header;
|
2016-12-12 22:59:05 +01:00
|
|
|
use ethcore::ids::BlockId;
|
2017-03-21 19:45:52 +01:00
|
|
|
|
2017-03-22 22:08:39 +01:00
|
|
|
use rlp::{Encodable, Decodable, DecoderError, RlpStream, Rlp, UntrustedRlp};
|
2017-03-21 19:45:52 +01:00
|
|
|
use util::{H256, U256, HeapSizeOf, RwLock};
|
|
|
|
use util::kvdb::{DBTransaction, KeyValueDB};
|
2016-12-12 22:59:05 +01:00
|
|
|
|
2017-04-05 13:50:11 +02:00
|
|
|
use cache::Cache;
|
|
|
|
use util::Mutex;
|
|
|
|
|
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
|
|
|
|
2017-03-21 20:23:58 +01:00
|
|
|
/// The best block key. Maps to an RLP list: [best_era, last_era]
|
|
|
|
const CURRENT_KEY: &'static [u8] = &*b"best_and_latest";
|
2017-03-21 19:45:52 +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>(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-21 19:45:52 +01:00
|
|
|
impl Encodable for Entry {
|
|
|
|
fn rlp_append(&self, s: &mut RlpStream) {
|
|
|
|
s.begin_list(self.candidates.len());
|
|
|
|
|
|
|
|
for candidate in &self.candidates {
|
|
|
|
s.begin_list(3)
|
|
|
|
.append(&candidate.hash)
|
|
|
|
.append(&candidate.parent_hash)
|
|
|
|
.append(&candidate.total_difficulty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Decodable for Entry {
|
2017-03-22 22:08:39 +01:00
|
|
|
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
|
2017-03-21 19:45:52 +01:00
|
|
|
|
|
|
|
let mut candidates = SmallVec::<[Candidate; 3]>::new();
|
|
|
|
|
|
|
|
for item in rlp.iter() {
|
|
|
|
candidates.push(Candidate {
|
|
|
|
hash: item.val_at(0)?,
|
|
|
|
parent_hash: item.val_at(1)?,
|
|
|
|
total_difficulty: item.val_at(2)?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if candidates.is_empty() { return Err(DecoderError::Custom("Empty candidates vector submitted.")) }
|
|
|
|
|
|
|
|
// rely on the invariant that the canonical entry is always first.
|
|
|
|
let canon_hash = candidates[0].hash;
|
|
|
|
Ok(Entry {
|
|
|
|
candidates: candidates,
|
|
|
|
canonical_hash: canon_hash,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cht_key(number: u64) -> String {
|
2017-04-05 11:30:51 +02:00
|
|
|
format!("{:08x}_canonical", number)
|
2017-03-21 19:45:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn era_key(number: u64) -> String {
|
|
|
|
format!("candidates_{}", number)
|
|
|
|
}
|
|
|
|
|
2017-03-23 04:00:22 +01:00
|
|
|
/// Pending changes from `insert` to be applied after the database write has finished.
|
|
|
|
pub struct PendingChanges {
|
|
|
|
best_block: Option<BlockDescriptor>, // new best block.
|
|
|
|
}
|
|
|
|
|
2016-12-12 22:59:05 +01:00
|
|
|
/// Header chain. See module docs for more details.
|
|
|
|
pub struct HeaderChain {
|
2017-02-03 15:32:22 +01:00
|
|
|
genesis_header: encoded::Header, // special-case the genesis.
|
2016-12-12 22:59:05 +01:00
|
|
|
candidates: RwLock<BTreeMap<u64, Entry>>,
|
2016-12-13 14:48:03 +01:00
|
|
|
best_block: RwLock<BlockDescriptor>,
|
2017-03-21 19:45:52 +01:00
|
|
|
db: Arc<KeyValueDB>,
|
|
|
|
col: Option<u32>,
|
2017-04-05 13:50:11 +02:00
|
|
|
cache: Arc<Mutex<Cache>>,
|
2016-12-12 22:59:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl HeaderChain {
|
2017-03-21 19:45:52 +01:00
|
|
|
/// Create a new header chain given this genesis block and database to read from.
|
2017-04-05 13:50:11 +02:00
|
|
|
pub fn new(db: Arc<KeyValueDB>, col: Option<u32>, genesis: &[u8], cache: Arc<Mutex<Cache>>) -> Result<Self, String> {
|
2017-02-03 15:32:22 +01:00
|
|
|
use ethcore::views::HeaderView;
|
|
|
|
|
2017-03-21 20:23:58 +01:00
|
|
|
let chain = if let Some(current) = db.get(col, CURRENT_KEY)? {
|
|
|
|
let (best_number, highest_number) = {
|
|
|
|
let rlp = Rlp::new(¤t);
|
|
|
|
(rlp.val_at(0), rlp.val_at(1))
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut cur_number = highest_number;
|
2017-03-21 19:45:52 +01:00
|
|
|
let mut candidates = BTreeMap::new();
|
|
|
|
|
|
|
|
// load all era entries and referenced headers within them.
|
|
|
|
while let Some(entry) = db.get(col, era_key(cur_number).as_bytes())? {
|
|
|
|
let entry: Entry = ::rlp::decode(&entry);
|
2017-03-21 20:23:58 +01:00
|
|
|
trace!(target: "chain", "loaded header chain entry for era {} with {} candidates",
|
|
|
|
cur_number, entry.candidates.len());
|
|
|
|
|
2017-03-21 19:45:52 +01:00
|
|
|
candidates.insert(cur_number, entry);
|
|
|
|
|
|
|
|
cur_number -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill best block block descriptor.
|
|
|
|
let best_block = {
|
2017-03-21 20:23:58 +01:00
|
|
|
let era = match candidates.get(&best_number) {
|
|
|
|
Some(era) => era,
|
|
|
|
None => return Err(format!("Database corrupt: highest block referenced but no data.")),
|
|
|
|
};
|
|
|
|
|
2017-03-21 19:45:52 +01:00
|
|
|
let best = &era.candidates[0];
|
|
|
|
BlockDescriptor {
|
|
|
|
hash: best.hash,
|
|
|
|
number: best_number,
|
|
|
|
total_difficulty: best.total_difficulty,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
HeaderChain {
|
|
|
|
genesis_header: encoded::Header::new(genesis.to_owned()),
|
|
|
|
best_block: RwLock::new(best_block),
|
|
|
|
candidates: RwLock::new(candidates),
|
|
|
|
db: db,
|
|
|
|
col: col,
|
2017-04-05 13:50:11 +02:00
|
|
|
cache: cache,
|
2017-03-21 19:45:52 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let g_view = HeaderView::new(genesis);
|
|
|
|
HeaderChain {
|
|
|
|
genesis_header: encoded::Header::new(genesis.to_owned()),
|
|
|
|
best_block: RwLock::new(BlockDescriptor {
|
|
|
|
hash: g_view.hash(),
|
|
|
|
number: 0,
|
|
|
|
total_difficulty: g_view.difficulty(),
|
|
|
|
}),
|
|
|
|
candidates: RwLock::new(BTreeMap::new()),
|
|
|
|
db: db,
|
|
|
|
col: col,
|
2017-04-05 13:50:11 +02:00
|
|
|
cache: cache,
|
2017-03-21 19:45:52 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(chain)
|
2016-12-12 22:59:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Insert a pre-verified header.
|
2016-12-13 20:13:16 +01:00
|
|
|
///
|
2017-02-03 15:32:22 +01:00
|
|
|
/// This blindly trusts that the data given to it is sensible.
|
2017-03-23 04:00:22 +01:00
|
|
|
/// Returns a set of pending changes to be applied with `apply_pending`
|
|
|
|
/// before the next call to insert and after the transaction has been written.
|
|
|
|
pub fn insert(&self, transaction: &mut DBTransaction, header: Header) -> Result<PendingChanges, BlockError> {
|
2017-02-03 15:32:22 +01:00
|
|
|
let hash = header.hash();
|
|
|
|
let number = header.number();
|
|
|
|
let parent_hash = *header.parent_hash();
|
2017-03-23 04:00:22 +01:00
|
|
|
let mut pending = PendingChanges {
|
|
|
|
best_block: None,
|
|
|
|
};
|
2016-12-12 22:59:05 +01:00
|
|
|
|
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 {
|
2017-02-03 15:32:22 +01:00
|
|
|
self.genesis_header.difficulty()
|
2016-12-12 22:59:05 +01:00
|
|
|
} 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
|
|
|
|
2017-02-03 15:32:22 +01:00
|
|
|
let total_difficulty = parent_td + *header.difficulty();
|
2016-12-12 22:59:05 +01:00
|
|
|
|
2017-03-21 20:23:58 +01:00
|
|
|
// insert headers and candidates entries and write era to disk.
|
2017-03-21 19:45:52 +01:00
|
|
|
{
|
|
|
|
let cur_era = candidates.entry(number)
|
|
|
|
.or_insert_with(|| Entry { candidates: SmallVec::new(), canonical_hash: hash });
|
|
|
|
cur_era.candidates.push(Candidate {
|
2016-12-12 22:59:05 +01:00
|
|
|
hash: hash,
|
|
|
|
parent_hash: parent_hash,
|
|
|
|
total_difficulty: total_difficulty,
|
2017-03-21 19:45:52 +01:00
|
|
|
});
|
2017-03-21 20:23:58 +01:00
|
|
|
|
|
|
|
// fix ordering of era before writing.
|
|
|
|
if total_difficulty > cur_era.candidates[0].total_difficulty {
|
|
|
|
let cur_pos = cur_era.candidates.len() - 1;
|
|
|
|
cur_era.candidates.swap(cur_pos, 0);
|
|
|
|
cur_era.canonical_hash = hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
transaction.put(self.col, era_key(number).as_bytes(), &::rlp::encode(&*cur_era))
|
2017-03-21 19:45:52 +01:00
|
|
|
}
|
2016-12-12 22:59:05 +01:00
|
|
|
|
2017-03-21 19:45:52 +01:00
|
|
|
let raw = ::rlp::encode(&header);
|
|
|
|
transaction.put(self.col, &hash[..], &*raw);
|
2017-03-21 20:23:58 +01:00
|
|
|
|
|
|
|
let (best_num, is_new_best) = {
|
|
|
|
let cur_best = self.best_block.read();
|
|
|
|
if cur_best.total_difficulty < total_difficulty {
|
|
|
|
(number, true)
|
|
|
|
} else {
|
|
|
|
(cur_best.number, false)
|
|
|
|
}
|
|
|
|
};
|
2016-12-12 22:59:05 +01:00
|
|
|
|
|
|
|
// reorganize ancestors so canonical entries are first in their
|
|
|
|
// respective candidates vectors.
|
2017-03-21 20:23:58 +01:00
|
|
|
if is_new_best {
|
2016-12-12 22:59:05 +01:00
|
|
|
let mut canon_hash = hash;
|
2017-01-20 12:41:59 +01:00
|
|
|
for (&height, entry) in candidates.iter_mut().rev().skip_while(|&(height, _)| *height > number) {
|
|
|
|
if height != number && entry.canonical_hash == canon_hash { break; }
|
2016-12-12 22:59:05 +01:00
|
|
|
|
2017-01-20 12:41:59 +01:00
|
|
|
trace!(target: "chain", "Setting new canonical block {} for block height {}",
|
|
|
|
canon_hash, height);
|
|
|
|
|
|
|
|
let canon_pos = entry.candidates.iter().position(|x| x.hash == canon_hash)
|
2016-12-12 22:59:05 +01:00
|
|
|
.expect("blocks are only inserted if parent is present; or this is the block we just added; qed");
|
|
|
|
|
2017-01-20 12:41:59 +01:00
|
|
|
// move the new canonical entry to the front and set the
|
|
|
|
// era's canonical hash.
|
|
|
|
entry.candidates.swap(0, canon_pos);
|
|
|
|
entry.canonical_hash = canon_hash;
|
|
|
|
|
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.
|
2017-01-20 12:41:59 +01:00
|
|
|
canon_hash = entry.candidates[0].parent_hash;
|
2017-03-21 19:45:52 +01:00
|
|
|
|
2017-03-21 20:23:58 +01:00
|
|
|
// write altered era to disk
|
|
|
|
if height != number {
|
|
|
|
let rlp_era = ::rlp::encode(&*entry);
|
|
|
|
transaction.put(self.col, era_key(height).as_bytes(), &rlp_era);
|
|
|
|
}
|
2016-12-12 22:59:05 +01:00
|
|
|
}
|
|
|
|
|
2017-01-20 12:41:59 +01:00
|
|
|
trace!(target: "chain", "New best block: ({}, {}), TD {}", number, hash, total_difficulty);
|
2017-03-23 04:00:22 +01:00
|
|
|
pending.best_block = Some(BlockDescriptor {
|
2016-12-12 22:59:05 +01:00
|
|
|
hash: hash,
|
|
|
|
number: number,
|
|
|
|
total_difficulty: total_difficulty,
|
2017-03-23 04:00:22 +01:00
|
|
|
});
|
2016-12-12 22:59:05 +01:00
|
|
|
|
|
|
|
// 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 {
|
2017-02-03 19:50:23 +01:00
|
|
|
let cht_num = cht::block_to_cht_number(earliest_era)
|
|
|
|
.expect("fails only for number == 0; genesis never imported; qed");
|
|
|
|
|
|
|
|
let cht_root = {
|
|
|
|
let mut i = earliest_era;
|
|
|
|
|
|
|
|
// iterable function which removes the candidates as it goes
|
|
|
|
// along. this will only be called until the CHT is complete.
|
|
|
|
let iter = || {
|
2016-12-13 14:48:03 +01:00
|
|
|
let era_entry = candidates.remove(&i)
|
|
|
|
.expect("all eras are sequential with no gaps; qed");
|
2017-03-21 19:45:52 +01:00
|
|
|
transaction.delete(self.col, era_key(i).as_bytes());
|
|
|
|
|
2017-02-03 19:50:23 +01:00
|
|
|
i += 1;
|
2016-12-13 14:48:03 +01:00
|
|
|
|
|
|
|
for ancient in &era_entry.candidates {
|
2017-03-21 19:45:52 +01:00
|
|
|
transaction.delete(self.col, &ancient.hash);
|
2016-12-13 14:48:03 +01:00
|
|
|
}
|
|
|
|
|
2017-02-03 19:50:23 +01:00
|
|
|
let canon = &era_entry.candidates[0];
|
|
|
|
(canon.hash, canon.total_difficulty)
|
|
|
|
};
|
|
|
|
cht::compute_root(cht_num, ::itertools::repeat_call(iter))
|
|
|
|
.expect("fails only when too few items; this is checked; qed")
|
|
|
|
};
|
2016-12-12 22:59:05 +01:00
|
|
|
|
2017-03-21 19:45:52 +01:00
|
|
|
// write the CHT root to the database.
|
2017-02-03 19:50:23 +01:00
|
|
|
debug!(target: "chain", "Produced CHT {} root: {:?}", cht_num, cht_root);
|
2017-03-21 19:45:52 +01:00
|
|
|
transaction.put(self.col, cht_key(cht_num).as_bytes(), &::rlp::encode(&cht_root));
|
2016-12-12 22:59:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-21 20:23:58 +01:00
|
|
|
// write the best and latest eras to the database.
|
|
|
|
{
|
|
|
|
let latest_num = *candidates.iter().rev().next().expect("at least one era just inserted; qed").0;
|
|
|
|
let mut stream = RlpStream::new_list(2);
|
|
|
|
stream.append(&best_num).append(&latest_num);
|
|
|
|
transaction.put(self.col, CURRENT_KEY, &stream.out())
|
|
|
|
}
|
2017-03-23 04:00:22 +01:00
|
|
|
Ok(pending)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Apply pending changes from a previous `insert` operation.
|
|
|
|
/// Must be done before the next `insert` call.
|
|
|
|
pub fn apply_pending(&self, pending: PendingChanges) {
|
|
|
|
if let Some(best_block) = pending.best_block {
|
|
|
|
*self.best_block.write() = best_block;
|
|
|
|
}
|
2016-12-12 22:59:05 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 12:07:54 +02:00
|
|
|
/// Get a block's hash by ID. In the case of query by number, only canonical results
|
|
|
|
/// will be returned.
|
|
|
|
pub fn block_hash(&self, id: BlockId) -> Option<H256> {
|
|
|
|
match id {
|
|
|
|
BlockId::Earliest => Some(self.genesis_hash()),
|
|
|
|
BlockId::Hash(hash) => Some(hash),
|
|
|
|
BlockId::Number(num) => {
|
|
|
|
if self.best_block.read().number < num { return None }
|
|
|
|
self.candidates.read().get(&num).map(|entry| entry.canonical_hash)
|
|
|
|
}
|
|
|
|
BlockId::Latest | BlockId::Pending => {
|
|
|
|
Some(self.best_block.read().hash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-12 22:59:05 +01:00
|
|
|
/// Get a block header. In the case of query by number, only canonical blocks
|
|
|
|
/// will be returned.
|
2017-02-08 20:22:41 +01:00
|
|
|
pub fn block_header(&self, id: BlockId) -> Option<encoded::Header> {
|
2017-03-21 20:23:58 +01:00
|
|
|
let load_from_db = |hash: H256| {
|
2017-04-06 20:50:52 +02:00
|
|
|
let mut cache = self.cache.lock();
|
|
|
|
|
2017-04-06 20:58:57 +02:00
|
|
|
match cache.block_header(&hash) {
|
2017-04-06 20:50:52 +02:00
|
|
|
Some(header) => Some(header),
|
|
|
|
None => {
|
|
|
|
match self.db.get(self.col, &hash) {
|
2017-04-06 22:31:03 +02:00
|
|
|
Ok(db_value) => {
|
|
|
|
db_value.map(|x| x.to_vec()).map(encoded::Header::new)
|
2017-04-06 20:50:52 +02:00
|
|
|
.and_then(|header| {
|
|
|
|
cache.insert_block_header(hash.clone(), header.clone());
|
|
|
|
Some(header)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
warn!(target: "chain", "Failed to read from database: {}", e);
|
|
|
|
None
|
|
|
|
}
|
2017-04-06 15:46:59 +02:00
|
|
|
}
|
2017-03-21 20:23:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-12-12 22:59:05 +01:00
|
|
|
match id {
|
|
|
|
BlockId::Earliest | BlockId::Number(0) => Some(self.genesis_header.clone()),
|
2017-03-21 20:23:58 +01:00
|
|
|
BlockId::Hash(hash) => load_from_db(hash),
|
2016-12-12 22:59:05 +01:00
|
|
|
BlockId::Number(num) => {
|
|
|
|
if self.best_block.read().number < num { return None }
|
|
|
|
|
|
|
|
self.candidates.read().get(&num).map(|entry| entry.canonical_hash)
|
2017-03-21 20:23:58 +01:00
|
|
|
.and_then(load_from_db)
|
2016-12-12 22:59:05 +01:00
|
|
|
}
|
|
|
|
BlockId::Latest | BlockId::Pending => {
|
2017-03-23 04:00:22 +01:00
|
|
|
// hold candidates hear to prevent deletion of the header
|
|
|
|
// as we read it.
|
|
|
|
let _candidates = self.candidates.read();
|
2017-02-03 16:11:09 +01:00
|
|
|
let hash = {
|
|
|
|
let best = self.best_block.read();
|
|
|
|
if best.number == 0 {
|
|
|
|
return Some(self.genesis_header.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
best.hash
|
|
|
|
};
|
|
|
|
|
2017-03-21 20:23:58 +01:00
|
|
|
load_from_db(hash)
|
2016-12-12 22:59:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-12 12:07:54 +02:00
|
|
|
/// Get a block's chain score.
|
|
|
|
/// Returns nothing for non-canonical blocks.
|
|
|
|
pub fn score(&self, id: BlockId) -> Option<U256> {
|
|
|
|
let genesis_hash = self.genesis_hash();
|
|
|
|
match id {
|
|
|
|
BlockId::Earliest | BlockId::Number(0) => Some(self.genesis_header.difficulty()),
|
|
|
|
BlockId::Hash(hash) if hash == genesis_hash => Some(self.genesis_header.difficulty()),
|
|
|
|
BlockId::Hash(hash) => match self.block_header(BlockId::Hash(hash)) {
|
|
|
|
Some(header) => self.candidates.read().get(&header.number())
|
|
|
|
.and_then(|era| era.candidates.iter().find(|e| e.hash == hash))
|
|
|
|
.map(|c| c.total_difficulty),
|
|
|
|
None => None,
|
|
|
|
},
|
|
|
|
BlockId::Number(num) => {
|
|
|
|
let candidates = self.candidates.read();
|
|
|
|
if self.best_block.read().number < num { return None }
|
|
|
|
candidates.get(&num).map(|era| era.candidates[0].total_difficulty)
|
|
|
|
}
|
|
|
|
BlockId::Latest | BlockId::Pending => Some(self.best_block.read().total_difficulty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-04 17:19:39 +01:00
|
|
|
/// Get the best block's header.
|
|
|
|
pub fn best_header(&self) -> encoded::Header {
|
2017-02-08 20:22:41 +01:00
|
|
|
self.block_header(BlockId::Latest).expect("Header for best block always stored; qed")
|
2017-02-04 17:19:39 +01:00
|
|
|
}
|
|
|
|
|
2017-02-17 17:08:46 +01:00
|
|
|
/// Get an iterator over a block and its ancestry.
|
|
|
|
pub fn ancestry_iter(&self, start: BlockId) -> AncestryIter {
|
|
|
|
AncestryIter {
|
|
|
|
next: self.block_header(start),
|
|
|
|
chain: self,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-06 20:50:52 +02:00
|
|
|
/// Get the nth CHT root, if it's been computed.
|
2016-12-13 14:48:03 +01:00
|
|
|
///
|
|
|
|
/// 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> {
|
2017-03-21 19:45:52 +01:00
|
|
|
match self.db.get(self.col, cht_key(n as u64).as_bytes()) {
|
|
|
|
Ok(val) => val.map(|x| ::rlp::decode(&x)),
|
|
|
|
Err(e) => {
|
|
|
|
warn!(target: "chain", "Error reading from database: {}", e);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2016-12-13 14:48:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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 {
|
2017-03-21 20:23:58 +01:00
|
|
|
match self.db.get(self.col, &*hash).ok().map_or(false, |x| x.is_some()) {
|
2016-12-13 20:13:16 +01:00
|
|
|
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 {
|
2017-03-21 20:23:58 +01:00
|
|
|
self.candidates.read().heap_size_of_children()
|
2016-12-23 14:50:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-17 17:08:46 +01:00
|
|
|
/// Iterator over a block's ancestry.
|
|
|
|
pub struct AncestryIter<'a> {
|
|
|
|
next: Option<encoded::Header>,
|
|
|
|
chain: &'a HeaderChain,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator for AncestryIter<'a> {
|
|
|
|
type Item = encoded::Header;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<encoded::Header> {
|
|
|
|
let next = self.next.take();
|
|
|
|
if let Some(p_hash) = next.as_ref().map(|hdr| hdr.parent_hash()) {
|
|
|
|
self.next = self.chain.block_header(BlockId::Hash(p_hash));
|
|
|
|
}
|
|
|
|
|
|
|
|
next
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 14:48:03 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::HeaderChain;
|
2017-03-21 19:45:52 +01:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2016-12-13 14:48:03 +01:00
|
|
|
use ethcore::ids::BlockId;
|
|
|
|
use ethcore::header::Header;
|
|
|
|
use ethcore::spec::Spec;
|
2017-04-05 13:50:11 +02:00
|
|
|
use cache::Cache;
|
|
|
|
|
|
|
|
use time::Duration;
|
|
|
|
use util::Mutex;
|
|
|
|
|
2017-03-21 19:45:52 +01:00
|
|
|
fn make_db() -> Arc<::util::KeyValueDB> {
|
|
|
|
Arc::new(::util::kvdb::in_memory(0))
|
|
|
|
}
|
|
|
|
|
2016-12-13 14:48:03 +01:00
|
|
|
#[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();
|
2017-03-21 19:45:52 +01:00
|
|
|
let db = make_db();
|
2016-12-13 14:48:03 +01:00
|
|
|
|
2017-04-05 13:50:11 +02:00
|
|
|
let cache = Arc::new(Mutex::new(Cache::new(Default::default(), Duration::hours(6))));
|
|
|
|
|
|
|
|
let chain = HeaderChain::new(db.clone(), None, &::rlp::encode(&genesis_header), cache).unwrap();
|
2016-12-13 14:48:03 +01:00
|
|
|
|
|
|
|
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());
|
2017-02-03 15:32:22 +01:00
|
|
|
parent_hash = header.hash();
|
2016-12-13 14:48:03 +01:00
|
|
|
|
2017-03-21 19:45:52 +01:00
|
|
|
let mut tx = db.transaction();
|
2017-03-23 14:15:13 +01:00
|
|
|
let pending = chain.insert(&mut tx, header).unwrap();
|
2017-03-21 19:45:52 +01:00
|
|
|
db.write(tx).unwrap();
|
2017-03-23 14:15:13 +01:00
|
|
|
chain.apply_pending(pending);
|
2016-12-13 14:48:03 +01:00
|
|
|
|
|
|
|
rolling_timestamp += 10;
|
|
|
|
}
|
|
|
|
|
2017-02-08 20:22:41 +01:00
|
|
|
assert!(chain.block_header(BlockId::Number(10)).is_none());
|
|
|
|
assert!(chain.block_header(BlockId::Number(9000)).is_some());
|
2016-12-13 14:48:03 +01:00
|
|
|
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();
|
2017-03-21 19:45:52 +01:00
|
|
|
let db = make_db();
|
2017-04-05 13:50:11 +02:00
|
|
|
let cache = Arc::new(Mutex::new(Cache::new(Default::default(), Duration::hours(6))));
|
|
|
|
|
|
|
|
let chain = HeaderChain::new(db.clone(), None, &::rlp::encode(&genesis_header), cache).unwrap();
|
2016-12-13 14:48:03 +01:00
|
|
|
|
|
|
|
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());
|
2017-02-03 15:32:22 +01:00
|
|
|
parent_hash = header.hash();
|
2016-12-13 14:48:03 +01:00
|
|
|
|
2017-03-21 19:45:52 +01:00
|
|
|
let mut tx = db.transaction();
|
2017-03-23 14:15:13 +01:00
|
|
|
let pending = chain.insert(&mut tx, header).unwrap();
|
2017-03-21 19:45:52 +01:00
|
|
|
db.write(tx).unwrap();
|
2017-03-23 14:15:13 +01:00
|
|
|
chain.apply_pending(pending);
|
2016-12-13 14:48:03 +01:00
|
|
|
|
|
|
|
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());
|
2017-02-03 15:32:22 +01:00
|
|
|
parent_hash = header.hash();
|
2016-12-13 14:48:03 +01:00
|
|
|
|
2017-03-21 19:45:52 +01:00
|
|
|
let mut tx = db.transaction();
|
2017-03-23 14:15:13 +01:00
|
|
|
let pending = chain.insert(&mut tx, header).unwrap();
|
2017-03-21 19:45:52 +01:00
|
|
|
db.write(tx).unwrap();
|
2017-03-23 14:15:13 +01:00
|
|
|
chain.apply_pending(pending);
|
2016-12-13 14:48:03 +01:00
|
|
|
|
|
|
|
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());
|
2017-02-03 15:32:22 +01:00
|
|
|
parent_hash = header.hash();
|
2016-12-13 14:48:03 +01:00
|
|
|
|
2017-03-21 19:45:52 +01:00
|
|
|
let mut tx = db.transaction();
|
2017-03-23 14:15:13 +01:00
|
|
|
let pending = chain.insert(&mut tx, header).unwrap();
|
2017-03-21 19:45:52 +01:00
|
|
|
db.write(tx).unwrap();
|
2017-03-23 14:15:13 +01:00
|
|
|
chain.apply_pending(pending);
|
2016-12-13 14:48:03 +01:00
|
|
|
|
|
|
|
rolling_timestamp += 11;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-20 12:41:59 +01:00
|
|
|
let (mut num, mut canon_hash) = (chain.best_block().number, chain.best_block().hash);
|
|
|
|
assert_eq!(num, 12);
|
|
|
|
|
|
|
|
while num > 0 {
|
2017-02-08 20:22:41 +01:00
|
|
|
let header = chain.block_header(BlockId::Number(num)).unwrap();
|
2017-01-20 12:41:59 +01:00
|
|
|
assert_eq!(header.hash(), canon_hash);
|
|
|
|
|
2017-02-03 15:32:22 +01:00
|
|
|
canon_hash = header.parent_hash();
|
2017-01-20 12:41:59 +01:00
|
|
|
num -= 1;
|
|
|
|
}
|
2016-12-13 14:48:03 +01:00
|
|
|
}
|
2017-02-03 16:11:09 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn earliest_is_latest() {
|
|
|
|
let spec = Spec::new_test();
|
|
|
|
let genesis_header = spec.genesis_header();
|
2017-03-21 19:45:52 +01:00
|
|
|
let db = make_db();
|
2017-04-05 13:50:11 +02:00
|
|
|
let cache = Arc::new(Mutex::new(Cache::new(Default::default(), Duration::hours(6))));
|
|
|
|
|
|
|
|
let chain = HeaderChain::new(db.clone(), None, &::rlp::encode(&genesis_header), cache).unwrap();
|
2017-02-03 16:11:09 +01:00
|
|
|
|
|
|
|
|
2017-02-08 20:22:41 +01:00
|
|
|
assert!(chain.block_header(BlockId::Earliest).is_some());
|
|
|
|
assert!(chain.block_header(BlockId::Latest).is_some());
|
|
|
|
assert!(chain.block_header(BlockId::Pending).is_some());
|
2017-02-03 16:11:09 +01:00
|
|
|
}
|
2017-03-21 19:45:52 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn restore_from_db() {
|
|
|
|
let spec = Spec::new_test();
|
|
|
|
let genesis_header = spec.genesis_header();
|
|
|
|
let db = make_db();
|
2017-04-05 13:50:11 +02:00
|
|
|
let cache = Arc::new(Mutex::new(Cache::new(Default::default(), Duration::hours(6))));
|
2017-03-21 19:45:52 +01:00
|
|
|
|
|
|
|
{
|
2017-04-05 13:50:11 +02:00
|
|
|
let chain = HeaderChain::new(db.clone(), None, &::rlp::encode(&genesis_header), cache.clone()).unwrap();
|
2017-03-21 19:45:52 +01:00
|
|
|
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());
|
|
|
|
parent_hash = header.hash();
|
|
|
|
|
|
|
|
let mut tx = db.transaction();
|
2017-03-23 14:15:13 +01:00
|
|
|
let pending = chain.insert(&mut tx, header).unwrap();
|
2017-03-21 19:45:52 +01:00
|
|
|
db.write(tx).unwrap();
|
2017-03-23 14:15:13 +01:00
|
|
|
chain.apply_pending(pending);
|
2017-03-21 19:45:52 +01:00
|
|
|
|
|
|
|
rolling_timestamp += 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-05 13:50:11 +02:00
|
|
|
let chain = HeaderChain::new(db.clone(), None, &::rlp::encode(&genesis_header), cache.clone()).unwrap();
|
2017-03-21 19:45:52 +01:00
|
|
|
assert!(chain.block_header(BlockId::Number(10)).is_none());
|
|
|
|
assert!(chain.block_header(BlockId::Number(9000)).is_some());
|
|
|
|
assert!(chain.cht_root(2).is_some());
|
|
|
|
assert!(chain.cht_root(3).is_none());
|
|
|
|
assert_eq!(chain.block_header(BlockId::Latest).unwrap().number(), 9999);
|
|
|
|
}
|
2017-03-21 20:23:58 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn restore_higher_non_canonical() {
|
|
|
|
let spec = Spec::new_test();
|
|
|
|
let genesis_header = spec.genesis_header();
|
|
|
|
let db = make_db();
|
2017-04-05 13:50:11 +02:00
|
|
|
let cache = Arc::new(Mutex::new(Cache::new(Default::default(), Duration::hours(6))));
|
2017-03-21 20:23:58 +01:00
|
|
|
|
|
|
|
{
|
2017-04-05 13:50:11 +02:00
|
|
|
let chain = HeaderChain::new(db.clone(), None, &::rlp::encode(&genesis_header), cache.clone()).unwrap();
|
2017-03-21 20:23:58 +01:00
|
|
|
let mut parent_hash = genesis_header.hash();
|
|
|
|
let mut rolling_timestamp = genesis_header.timestamp();
|
|
|
|
|
|
|
|
// push 100 low-difficulty blocks.
|
|
|
|
for i in 1..101 {
|
|
|
|
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());
|
|
|
|
parent_hash = header.hash();
|
|
|
|
|
|
|
|
let mut tx = db.transaction();
|
2017-03-23 14:15:13 +01:00
|
|
|
let pending = chain.insert(&mut tx, header).unwrap();
|
2017-03-21 20:23:58 +01:00
|
|
|
db.write(tx).unwrap();
|
2017-03-23 14:15:13 +01:00
|
|
|
chain.apply_pending(pending);
|
2017-03-21 20:23:58 +01:00
|
|
|
|
|
|
|
rolling_timestamp += 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
// push fewer high-difficulty blocks.
|
|
|
|
for i in 1..11 {
|
|
|
|
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() * 1000.into());
|
|
|
|
parent_hash = header.hash();
|
|
|
|
|
|
|
|
let mut tx = db.transaction();
|
2017-03-23 14:15:13 +01:00
|
|
|
let pending = chain.insert(&mut tx, header).unwrap();
|
2017-03-21 20:23:58 +01:00
|
|
|
db.write(tx).unwrap();
|
2017-03-23 14:15:13 +01:00
|
|
|
chain.apply_pending(pending);
|
2017-03-21 20:23:58 +01:00
|
|
|
|
|
|
|
rolling_timestamp += 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(chain.block_header(BlockId::Latest).unwrap().number(), 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
// after restoration, non-canonical eras should still be loaded.
|
2017-04-05 13:50:11 +02:00
|
|
|
let chain = HeaderChain::new(db.clone(), None, &::rlp::encode(&genesis_header), cache.clone()).unwrap();
|
2017-03-21 20:23:58 +01:00
|
|
|
assert_eq!(chain.block_header(BlockId::Latest).unwrap().number(), 10);
|
|
|
|
assert!(chain.candidates.read().get(&100).is_some())
|
|
|
|
}
|
2016-12-12 22:59:05 +01:00
|
|
|
}
|