2016-02-05 13:40:41 +01:00
|
|
|
// Copyright 2015, 2016 Ethcore (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/>.
|
|
|
|
|
2016-02-02 15:29:53 +01:00
|
|
|
//! Blockchain block.
|
|
|
|
|
2016-03-11 11:16:49 +01:00
|
|
|
#![cfg_attr(feature="dev", allow(ptr_arg))] // Because of &LastHashes -> &Vec<_>
|
2016-01-19 11:10:38 +01:00
|
|
|
|
2016-01-09 22:13:13 +01:00
|
|
|
use common::*;
|
2016-01-08 19:12:19 +01:00
|
|
|
use engine::*;
|
2016-01-08 21:33:41 +01:00
|
|
|
use state::*;
|
2016-03-01 00:02:48 +01:00
|
|
|
use verification::PreverifiedBlock;
|
2016-01-08 19:12:19 +01:00
|
|
|
|
2016-01-26 19:18:22 +01:00
|
|
|
/// A block, encoded as it is on the block chain.
|
|
|
|
// TODO: rename to Block
|
|
|
|
#[derive(Default, Debug, Clone)]
|
2016-01-08 19:12:19 +01:00
|
|
|
pub struct Block {
|
2016-01-26 19:18:22 +01:00
|
|
|
/// The header of this block.
|
|
|
|
pub header: Header,
|
|
|
|
/// The transactions in this block.
|
2016-02-04 17:23:53 +01:00
|
|
|
pub transactions: Vec<SignedTransaction>,
|
2016-01-26 19:18:22 +01:00
|
|
|
/// The uncles of this block.
|
|
|
|
pub uncles: Vec<Header>,
|
|
|
|
}
|
2016-01-08 19:12:19 +01:00
|
|
|
|
2016-01-26 19:18:22 +01:00
|
|
|
impl Block {
|
2016-02-02 16:24:37 +01:00
|
|
|
/// Returns true if the given bytes form a valid encoding of a block in RLP.
|
2016-01-26 19:18:22 +01:00
|
|
|
// TODO: implement Decoder for this and have this use that.
|
|
|
|
pub fn is_good(b: &[u8]) -> bool {
|
|
|
|
/*
|
|
|
|
let urlp = UntrustedRlp::new(&b);
|
|
|
|
if !urlp.is_list() || urlp.item_count() != 3 || urlp.size() != b.len() { return false; }
|
|
|
|
if urlp.val_at::<Header>(0).is_err() { return false; }
|
|
|
|
|
|
|
|
if !urlp.at(1).unwrap().is_list() { return false; }
|
|
|
|
if urlp.at(1).unwrap().iter().find(|i| i.as_val::<Transaction>().is_err()).is_some() {
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-23 11:40:23 +01:00
|
|
|
|
2016-01-26 19:18:22 +01:00
|
|
|
if !urlp.at(2).unwrap().is_list() { return false; }
|
|
|
|
if urlp.at(2).unwrap().iter().find(|i| i.as_val::<Header>().is_err()).is_some() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
true*/
|
|
|
|
UntrustedRlp::new(b).as_val::<Block>().is_ok()
|
|
|
|
}
|
|
|
|
}
|
2016-01-08 19:12:19 +01:00
|
|
|
|
2016-01-26 19:18:22 +01:00
|
|
|
impl Decodable for Block {
|
|
|
|
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
|
|
|
|
if decoder.as_raw().len() != try!(decoder.as_rlp().payload_info()).total() {
|
2016-02-23 11:40:23 +01:00
|
|
|
return Err(DecoderError::RlpIsTooBig);
|
2016-01-26 19:18:22 +01:00
|
|
|
}
|
2016-01-29 13:59:29 +01:00
|
|
|
let d = decoder.as_rlp();
|
|
|
|
if d.item_count() != 3 {
|
2016-01-26 19:18:22 +01:00
|
|
|
return Err(DecoderError::RlpIncorrectListLen);
|
|
|
|
}
|
|
|
|
Ok(Block {
|
2016-01-29 13:59:29 +01:00
|
|
|
header: try!(d.val_at(0)),
|
|
|
|
transactions: try!(d.val_at(1)),
|
|
|
|
uncles: try!(d.val_at(2)),
|
2016-01-26 19:18:22 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2016-01-10 14:05:39 +01:00
|
|
|
|
2016-01-26 19:18:22 +01:00
|
|
|
/// Internal type for a block's common elements.
|
|
|
|
// TODO: rename to ExecutedBlock
|
|
|
|
// TODO: use BareBlock
|
2016-02-03 19:34:51 +01:00
|
|
|
#[derive(Debug)]
|
2016-01-26 19:18:22 +01:00
|
|
|
pub struct ExecutedBlock {
|
|
|
|
base: Block,
|
|
|
|
|
|
|
|
receipts: Vec<Receipt>,
|
|
|
|
transactions_set: HashSet<H256>,
|
|
|
|
state: State,
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
|
|
|
|
2016-02-23 11:40:23 +01:00
|
|
|
/// A set of references to `ExecutedBlock` fields that are publicly accessible.
|
2016-01-10 17:11:46 +01:00
|
|
|
pub struct BlockRefMut<'a> {
|
2016-02-02 16:24:37 +01:00
|
|
|
/// Block header.
|
2016-01-10 17:11:46 +01:00
|
|
|
pub header: &'a Header,
|
2016-02-02 16:24:37 +01:00
|
|
|
/// Block transactions.
|
2016-02-04 17:23:53 +01:00
|
|
|
pub transactions: &'a Vec<SignedTransaction>,
|
2016-02-02 16:24:37 +01:00
|
|
|
/// Block uncles.
|
2016-01-10 17:11:46 +01:00
|
|
|
pub uncles: &'a Vec<Header>,
|
2016-02-02 16:24:37 +01:00
|
|
|
/// Transaction receipts.
|
2016-01-26 19:18:22 +01:00
|
|
|
pub receipts: &'a Vec<Receipt>,
|
2016-02-02 16:24:37 +01:00
|
|
|
/// State.
|
2016-01-26 19:18:22 +01:00
|
|
|
pub state: &'a mut State,
|
2016-01-10 17:11:46 +01:00
|
|
|
}
|
|
|
|
|
2016-01-26 19:18:22 +01:00
|
|
|
impl ExecutedBlock {
|
2016-01-10 17:11:46 +01:00
|
|
|
/// Create a new block from the given `state`.
|
2016-01-26 19:18:22 +01:00
|
|
|
fn new(state: State) -> ExecutedBlock { ExecutedBlock { base: Default::default(), receipts: Default::default(), transactions_set: Default::default(), state: state } }
|
2016-01-08 19:12:19 +01:00
|
|
|
|
2016-01-10 17:11:46 +01:00
|
|
|
/// Get a structure containing individual references to all public fields.
|
2016-01-10 17:09:02 +01:00
|
|
|
pub fn fields(&mut self) -> BlockRefMut {
|
|
|
|
BlockRefMut {
|
2016-01-26 19:18:22 +01:00
|
|
|
header: &self.base.header,
|
|
|
|
transactions: &self.base.transactions,
|
|
|
|
uncles: &self.base.uncles,
|
2016-01-10 17:09:02 +01:00
|
|
|
state: &mut self.state,
|
2016-01-26 19:18:22 +01:00
|
|
|
receipts: &self.receipts,
|
2016-01-10 17:09:02 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
|
|
|
|
2016-01-26 19:18:22 +01:00
|
|
|
/// Trait for a object that is_a `ExecutedBlock`.
|
2016-01-08 21:33:41 +01:00
|
|
|
pub trait IsBlock {
|
2016-01-08 19:12:19 +01:00
|
|
|
/// Get the block associated with this object.
|
2016-01-26 19:18:22 +01:00
|
|
|
fn block(&self) -> &ExecutedBlock;
|
2016-01-08 19:12:19 +01:00
|
|
|
|
|
|
|
/// Get the header associated with this object's block.
|
2016-01-26 19:18:22 +01:00
|
|
|
fn header(&self) -> &Header { &self.block().base.header }
|
2016-01-08 19:12:19 +01:00
|
|
|
|
|
|
|
/// Get the final state associated with this object's block.
|
2016-01-08 21:33:41 +01:00
|
|
|
fn state(&self) -> &State { &self.block().state }
|
2016-01-08 19:12:19 +01:00
|
|
|
|
|
|
|
/// Get all information on transactions in this block.
|
2016-02-04 17:23:53 +01:00
|
|
|
fn transactions(&self) -> &Vec<SignedTransaction> { &self.block().base.transactions }
|
2016-01-26 19:18:22 +01:00
|
|
|
|
|
|
|
/// Get all information on receipts in this block.
|
|
|
|
fn receipts(&self) -> &Vec<Receipt> { &self.block().receipts }
|
2016-01-10 14:05:39 +01:00
|
|
|
|
|
|
|
/// Get all uncles in this block.
|
2016-01-26 19:18:22 +01:00
|
|
|
fn uncles(&self) -> &Vec<Header> { &self.block().base.uncles }
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
|
|
|
|
2016-01-26 19:18:22 +01:00
|
|
|
impl IsBlock for ExecutedBlock {
|
|
|
|
fn block(&self) -> &ExecutedBlock { self }
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
2016-01-08 22:04:21 +01:00
|
|
|
|
2016-01-08 19:12:19 +01:00
|
|
|
/// Block that is ready for transactions to be added.
|
|
|
|
///
|
2016-02-26 17:27:56 +01:00
|
|
|
/// It's a bit like a Vec<Transaction>, except that whenever a transaction is pushed, we execute it and
|
2016-01-08 19:12:19 +01:00
|
|
|
/// maintain the system `state()`. We also archive execution receipts in preparation for later block creation.
|
2016-02-24 11:17:25 +01:00
|
|
|
pub struct OpenBlock<'x> {
|
2016-01-26 19:18:22 +01:00
|
|
|
block: ExecutedBlock,
|
2016-01-10 22:55:07 +01:00
|
|
|
engine: &'x Engine,
|
2016-02-24 11:17:25 +01:00
|
|
|
last_hashes: LastHashes,
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Just like OpenBlock, except that we've applied `Engine::on_close_block`, finished up the non-seal header fields,
|
|
|
|
/// and collected the uncles.
|
|
|
|
///
|
2016-02-29 14:57:41 +01:00
|
|
|
/// There is no function available to push a transaction.
|
|
|
|
pub struct ClosedBlock {
|
|
|
|
block: ExecutedBlock,
|
2016-01-10 14:05:39 +01:00
|
|
|
uncle_bytes: Bytes,
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A block that has a valid seal.
|
|
|
|
///
|
|
|
|
/// The block's header has valid seal arguments. The block cannot be reversed into a ClosedBlock or OpenBlock.
|
|
|
|
pub struct SealedBlock {
|
2016-01-26 19:18:22 +01:00
|
|
|
block: ExecutedBlock,
|
2016-01-10 14:05:39 +01:00
|
|
|
uncle_bytes: Bytes,
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
|
|
|
|
2016-02-24 11:17:25 +01:00
|
|
|
impl<'x> OpenBlock<'x> {
|
2016-01-08 22:04:21 +01:00
|
|
|
/// Create a new OpenBlock ready for transaction pushing.
|
2016-02-24 11:17:25 +01:00
|
|
|
pub fn new(engine: &'x Engine, db: JournalDB, parent: &Header, last_hashes: LastHashes, author: Address, extra_data: Bytes) -> Self {
|
2016-01-08 19:12:19 +01:00
|
|
|
let mut r = OpenBlock {
|
2016-01-26 19:18:22 +01:00
|
|
|
block: ExecutedBlock::new(State::from_existing(db, parent.state_root().clone(), engine.account_start_nonce())),
|
2016-01-08 19:12:19 +01:00
|
|
|
engine: engine,
|
|
|
|
last_hashes: last_hashes,
|
2016-01-08 22:04:21 +01:00
|
|
|
};
|
2016-01-08 19:12:19 +01:00
|
|
|
|
2016-03-02 00:52:18 +01:00
|
|
|
r.block.base.header.parent_hash = parent.hash();
|
|
|
|
r.block.base.header.number = parent.number + 1;
|
|
|
|
r.block.base.header.author = author;
|
2016-02-29 14:57:41 +01:00
|
|
|
r.block.base.header.set_timestamp_now(parent.timestamp());
|
2016-03-02 00:52:18 +01:00
|
|
|
r.block.base.header.extra_data = extra_data;
|
|
|
|
r.block.base.header.note_dirty();
|
2016-01-10 22:55:07 +01:00
|
|
|
|
2016-01-26 19:18:22 +01:00
|
|
|
engine.populate_from_parent(&mut r.block.base.header, parent);
|
2016-01-08 22:04:21 +01:00
|
|
|
engine.on_new_block(&mut r.block);
|
2016-01-08 19:12:19 +01:00
|
|
|
r
|
|
|
|
}
|
|
|
|
|
2016-01-10 14:05:39 +01:00
|
|
|
/// Alter the author for the block.
|
2016-01-26 19:18:22 +01:00
|
|
|
pub fn set_author(&mut self, author: Address) { self.block.base.header.set_author(author); }
|
2016-01-10 14:05:39 +01:00
|
|
|
|
2016-01-10 22:55:07 +01:00
|
|
|
/// Alter the timestamp of the block.
|
2016-01-26 19:18:22 +01:00
|
|
|
pub fn set_timestamp(&mut self, timestamp: u64) { self.block.base.header.set_timestamp(timestamp); }
|
2016-01-10 22:55:07 +01:00
|
|
|
|
2016-01-15 23:32:17 +01:00
|
|
|
/// Alter the difficulty for the block.
|
2016-01-26 19:18:22 +01:00
|
|
|
pub fn set_difficulty(&mut self, a: U256) { self.block.base.header.set_difficulty(a); }
|
2016-01-15 23:32:17 +01:00
|
|
|
|
|
|
|
/// Alter the gas limit for the block.
|
2016-01-26 19:18:22 +01:00
|
|
|
pub fn set_gas_limit(&mut self, a: U256) { self.block.base.header.set_gas_limit(a); }
|
2016-01-15 23:32:17 +01:00
|
|
|
|
|
|
|
/// Alter the gas limit for the block.
|
2016-01-26 19:18:22 +01:00
|
|
|
pub fn set_gas_used(&mut self, a: U256) { self.block.base.header.set_gas_used(a); }
|
2016-01-15 23:32:17 +01:00
|
|
|
|
2016-01-10 14:05:39 +01:00
|
|
|
/// Alter the extra_data for the block.
|
|
|
|
pub fn set_extra_data(&mut self, extra_data: Bytes) -> Result<(), BlockError> {
|
|
|
|
if extra_data.len() > self.engine.maximum_extra_data_size() {
|
2016-01-12 11:44:16 +01:00
|
|
|
Err(BlockError::ExtraDataOutOfBounds(OutOfBounds{min: None, max: Some(self.engine.maximum_extra_data_size()), found: extra_data.len()}))
|
2016-01-10 14:05:39 +01:00
|
|
|
} else {
|
2016-01-26 19:18:22 +01:00
|
|
|
self.block.base.header.set_extra_data(extra_data);
|
2016-01-10 14:05:39 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add an uncle to the block, if possible.
|
|
|
|
///
|
|
|
|
/// NOTE Will check chain constraints and the uncle number but will NOT check
|
|
|
|
/// that the header itself is actually valid.
|
|
|
|
pub fn push_uncle(&mut self, valid_uncle_header: Header) -> Result<(), BlockError> {
|
2016-03-04 23:29:56 +01:00
|
|
|
if self.block.base.uncles.len() + 1 > self.engine.maximum_uncle_count() {
|
|
|
|
return Err(BlockError::TooManyUncles(OutOfBounds{min: None, max: Some(self.engine.maximum_uncle_count()), found: self.block.base.uncles.len() + 1}));
|
2016-01-10 14:05:39 +01:00
|
|
|
}
|
|
|
|
// TODO: check number
|
|
|
|
// TODO: check not a direct ancestor (use last_hashes for that)
|
2016-01-26 19:18:22 +01:00
|
|
|
self.block.base.uncles.push(valid_uncle_header);
|
2016-01-10 14:05:39 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-01-08 22:04:21 +01:00
|
|
|
/// Get the environment info concerning this block.
|
|
|
|
pub fn env_info(&self) -> EnvInfo {
|
|
|
|
// TODO: memoise.
|
|
|
|
EnvInfo {
|
2016-01-26 19:18:22 +01:00
|
|
|
number: self.block.base.header.number,
|
|
|
|
author: self.block.base.header.author.clone(),
|
|
|
|
timestamp: self.block.base.header.timestamp,
|
|
|
|
difficulty: self.block.base.header.difficulty.clone(),
|
2016-01-15 23:32:17 +01:00
|
|
|
last_hashes: self.last_hashes.clone(), // TODO: should be a reference.
|
2016-01-26 19:18:22 +01:00
|
|
|
gas_used: self.block.receipts.last().map_or(U256::zero(), |r| r.gas_used),
|
|
|
|
gas_limit: self.block.base.header.gas_limit.clone(),
|
2016-01-08 22:04:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-10 14:05:39 +01:00
|
|
|
/// Push a transaction into the block.
|
|
|
|
///
|
|
|
|
/// If valid, it will be executed, and archived together with the receipt.
|
2016-02-04 17:23:53 +01:00
|
|
|
pub fn push_transaction(&mut self, t: SignedTransaction, h: Option<H256>) -> Result<&Receipt, Error> {
|
2016-01-08 22:04:21 +01:00
|
|
|
let env_info = self.env_info();
|
2016-01-15 23:32:17 +01:00
|
|
|
// info!("env_info says gas_used={}", env_info.gas_used);
|
2016-01-11 13:29:15 +01:00
|
|
|
match self.block.state.apply(&env_info, self.engine, &t) {
|
2016-01-11 17:37:22 +01:00
|
|
|
Ok(receipt) => {
|
2016-01-26 19:18:22 +01:00
|
|
|
self.block.transactions_set.insert(h.unwrap_or_else(||t.hash()));
|
|
|
|
self.block.base.transactions.push(t);
|
|
|
|
self.block.receipts.push(receipt);
|
|
|
|
Ok(&self.block.receipts.last().unwrap())
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
2016-01-11 17:37:22 +01:00
|
|
|
Err(x) => Err(From::from(x))
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-09 18:26:35 +01:00
|
|
|
/// Turn this into a `ClosedBlock`. A BlockChain must be provided in order to figure out the uncles.
|
2016-02-29 14:57:41 +01:00
|
|
|
pub fn close(self) -> ClosedBlock {
|
2016-01-09 22:45:27 +01:00
|
|
|
let mut s = self;
|
|
|
|
s.engine.on_close_block(&mut s.block);
|
2016-01-27 17:31:45 +01:00
|
|
|
s.block.base.header.transactions_root = ordered_trie_root(s.block.base.transactions.iter().map(|ref e| e.rlp_bytes().to_vec()).collect());
|
2016-01-26 19:18:22 +01:00
|
|
|
let uncle_bytes = s.block.base.uncles.iter().fold(RlpStream::new_list(s.block.base.uncles.len()), |mut s, u| {s.append(&u.rlp(Seal::With)); s} ).out();
|
|
|
|
s.block.base.header.uncles_hash = uncle_bytes.sha3();
|
|
|
|
s.block.base.header.state_root = s.block.state.root().clone();
|
2016-01-27 17:31:45 +01:00
|
|
|
s.block.base.header.receipts_root = ordered_trie_root(s.block.receipts.iter().map(|ref r| r.rlp_bytes().to_vec()).collect());
|
2016-02-19 00:23:05 +01:00
|
|
|
s.block.base.header.log_bloom = s.block.receipts.iter().fold(LogBloom::zero(), |mut b, r| {b = &b | &r.log_bloom; b}); //TODO: use |= operator
|
2016-01-26 19:18:22 +01:00
|
|
|
s.block.base.header.gas_used = s.block.receipts.last().map_or(U256::zero(), |r| r.gas_used);
|
|
|
|
s.block.base.header.note_dirty();
|
2016-01-09 22:45:27 +01:00
|
|
|
|
2016-02-29 14:57:41 +01:00
|
|
|
ClosedBlock {
|
2016-03-07 14:33:00 +01:00
|
|
|
block: s.block,
|
2016-02-29 14:57:41 +01:00
|
|
|
uncle_bytes: uncle_bytes,
|
|
|
|
}
|
2016-01-09 18:58:04 +01:00
|
|
|
}
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
|
|
|
|
2016-02-24 11:17:25 +01:00
|
|
|
impl<'x> IsBlock for OpenBlock<'x> {
|
2016-01-26 19:18:22 +01:00
|
|
|
fn block(&self) -> &ExecutedBlock { &self.block }
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
|
|
|
|
2016-02-29 14:57:41 +01:00
|
|
|
impl<'x> IsBlock for ClosedBlock {
|
|
|
|
fn block(&self) -> &ExecutedBlock { &self.block }
|
2016-01-10 14:05:39 +01:00
|
|
|
}
|
|
|
|
|
2016-02-29 14:57:41 +01:00
|
|
|
impl ClosedBlock {
|
2016-01-08 19:12:19 +01:00
|
|
|
/// Get the hash of the header without seal arguments.
|
2016-01-10 14:05:39 +01:00
|
|
|
pub fn hash(&self) -> H256 { self.header().rlp_sha3(Seal::Without) }
|
2016-01-08 19:12:19 +01:00
|
|
|
|
2016-01-10 14:05:39 +01:00
|
|
|
/// Provide a valid seal in order to turn this into a `SealedBlock`.
|
|
|
|
///
|
|
|
|
/// NOTE: This does not check the validity of `seal` with the engine.
|
2016-02-29 14:57:41 +01:00
|
|
|
pub fn seal(self, engine: &Engine, seal: Vec<Bytes>) -> Result<SealedBlock, BlockError> {
|
2016-01-10 14:05:39 +01:00
|
|
|
let mut s = self;
|
2016-02-29 14:57:41 +01:00
|
|
|
if seal.len() != engine.seal_fields() {
|
|
|
|
return Err(BlockError::InvalidSealArity(Mismatch{expected: engine.seal_fields(), found: seal.len()}));
|
2016-01-10 14:05:39 +01:00
|
|
|
}
|
2016-02-29 14:57:41 +01:00
|
|
|
s.block.base.header.set_seal(seal);
|
|
|
|
Ok(SealedBlock { block: s.block, uncle_bytes: s.uncle_bytes })
|
2016-01-10 14:05:39 +01:00
|
|
|
}
|
2016-01-08 19:12:19 +01:00
|
|
|
|
2016-03-01 00:02:48 +01:00
|
|
|
/// Provide a valid seal in order to turn this into a `SealedBlock`.
|
|
|
|
/// This does check the validity of `seal` with the engine.
|
|
|
|
/// Returns the `ClosedBlock` back again if the seal is no good.
|
|
|
|
pub fn try_seal(self, engine: &Engine, seal: Vec<Bytes>) -> Result<SealedBlock, ClosedBlock> {
|
|
|
|
let mut s = self;
|
|
|
|
s.block.base.header.set_seal(seal);
|
2016-03-01 17:23:44 +01:00
|
|
|
match engine.verify_block_seal(&s.block.base.header) {
|
|
|
|
Err(_) => Err(s),
|
|
|
|
_ => Ok(SealedBlock { block: s.block, uncle_bytes: s.uncle_bytes }),
|
2016-03-01 00:02:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-14 19:03:48 +01:00
|
|
|
/// Drop this object and return the underlieing database.
|
2016-02-29 14:57:41 +01:00
|
|
|
pub fn drain(self) -> JournalDB { self.block.state.drop().1 }
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SealedBlock {
|
2016-01-10 14:05:39 +01:00
|
|
|
/// Get the RLP-encoding of the block.
|
|
|
|
pub fn rlp_bytes(&self) -> Bytes {
|
|
|
|
let mut block_rlp = RlpStream::new_list(3);
|
2016-01-26 19:18:22 +01:00
|
|
|
self.block.base.header.stream_rlp(&mut block_rlp, Seal::With);
|
2016-01-28 20:13:05 +01:00
|
|
|
block_rlp.append(&self.block.base.transactions);
|
2016-01-10 14:05:39 +01:00
|
|
|
block_rlp.append_raw(&self.uncle_bytes, 1);
|
|
|
|
block_rlp.out()
|
2016-01-10 23:10:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Drop this object and return the underlieing database.
|
2016-01-18 13:54:46 +01:00
|
|
|
pub fn drain(self) -> JournalDB { self.block.state.drop().1 }
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl IsBlock for SealedBlock {
|
2016-01-26 19:18:22 +01:00
|
|
|
fn block(&self) -> &ExecutedBlock { &self.block }
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
2016-01-09 18:20:31 +01:00
|
|
|
|
2016-01-17 23:07:58 +01:00
|
|
|
/// Enact the block given by block header, transactions and uncles
|
2016-02-29 14:57:41 +01:00
|
|
|
pub fn enact(header: &Header, transactions: &[SignedTransaction], uncles: &[Header], engine: &Engine, db: JournalDB, parent: &Header, last_hashes: LastHashes) -> Result<ClosedBlock, Error> {
|
2016-01-15 22:55:04 +01:00
|
|
|
{
|
2016-02-05 01:49:06 +01:00
|
|
|
if ::log::max_log_level() >= ::log::LogLevel::Trace {
|
|
|
|
let s = State::from_existing(db.clone(), parent.state_root().clone(), engine.account_start_nonce());
|
|
|
|
trace!("enact(): root={}, author={}, author_balance={}\n", s.root(), header.author(), s.balance(&header.author()));
|
|
|
|
}
|
2016-01-15 22:55:04 +01:00
|
|
|
}
|
2016-01-15 23:32:17 +01:00
|
|
|
|
2016-01-17 23:07:58 +01:00
|
|
|
let mut b = OpenBlock::new(engine, db, parent, last_hashes, header.author().clone(), header.extra_data().clone());
|
|
|
|
b.set_difficulty(*header.difficulty());
|
|
|
|
b.set_gas_limit(*header.gas_limit());
|
2016-01-10 22:57:31 +01:00
|
|
|
b.set_timestamp(header.timestamp());
|
2016-01-17 23:07:58 +01:00
|
|
|
for t in transactions { try!(b.push_transaction(t.clone(), None)); }
|
|
|
|
for u in uncles { try!(b.push_uncle(u.clone())); }
|
2016-01-14 19:03:48 +01:00
|
|
|
Ok(b.close())
|
|
|
|
}
|
|
|
|
|
2016-01-17 23:07:58 +01:00
|
|
|
/// Enact the block given by `block_bytes` using `engine` on the database `db` with given `parent` block header
|
2016-02-29 14:57:41 +01:00
|
|
|
pub fn enact_bytes(block_bytes: &[u8], engine: &Engine, db: JournalDB, parent: &Header, last_hashes: LastHashes) -> Result<ClosedBlock, Error> {
|
2016-01-17 23:07:58 +01:00
|
|
|
let block = BlockView::new(block_bytes);
|
|
|
|
let header = block.header();
|
|
|
|
enact(&header, &block.transactions(), &block.uncles(), engine, db, parent, last_hashes)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Enact the block given by `block_bytes` using `engine` on the database `db` with given `parent` block header
|
2016-03-01 00:02:48 +01:00
|
|
|
pub fn enact_verified(block: &PreverifiedBlock, engine: &Engine, db: JournalDB, parent: &Header, last_hashes: LastHashes) -> Result<ClosedBlock, Error> {
|
2016-01-17 23:07:58 +01:00
|
|
|
let view = BlockView::new(&block.bytes);
|
|
|
|
enact(&block.header, &block.transactions, &view.uncles(), engine, db, parent, last_hashes)
|
|
|
|
}
|
|
|
|
|
2016-01-14 19:03:48 +01:00
|
|
|
/// Enact the block given by `block_bytes` using `engine` on the database `db` with given `parent` block header. Seal the block aferwards
|
2016-02-24 11:17:25 +01:00
|
|
|
pub fn enact_and_seal(block_bytes: &[u8], engine: &Engine, db: JournalDB, parent: &Header, last_hashes: LastHashes) -> Result<SealedBlock, Error> {
|
2016-01-14 19:03:48 +01:00
|
|
|
let header = BlockView::new(block_bytes).header_view();
|
2016-02-29 14:57:41 +01:00
|
|
|
Ok(try!(try!(enact_bytes(block_bytes, engine, db, parent, last_hashes)).seal(engine, header.seal())))
|
2016-01-10 22:55:07 +01:00
|
|
|
}
|
|
|
|
|
2016-01-31 10:52:07 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use tests::helpers::*;
|
|
|
|
use super::*;
|
|
|
|
use common::*;
|
|
|
|
use engine::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn open_block() {
|
|
|
|
use spec::*;
|
|
|
|
let engine = Spec::new_test().to_engine().unwrap();
|
|
|
|
let genesis_header = engine.spec().genesis_header();
|
|
|
|
let mut db_result = get_temp_journal_db();
|
2016-02-03 19:34:51 +01:00
|
|
|
let mut db = db_result.take();
|
|
|
|
engine.spec().ensure_db_good(&mut db);
|
2016-01-31 10:52:07 +01:00
|
|
|
let last_hashes = vec![genesis_header.hash()];
|
2016-02-24 11:17:25 +01:00
|
|
|
let b = OpenBlock::new(engine.deref(), db, &genesis_header, last_hashes, Address::zero(), vec![]);
|
2016-01-31 10:52:07 +01:00
|
|
|
let b = b.close();
|
2016-02-29 14:57:41 +01:00
|
|
|
let _ = b.seal(engine.deref(), vec![]);
|
2016-01-31 10:52:07 +01:00
|
|
|
}
|
2016-01-10 23:10:06 +01:00
|
|
|
|
2016-01-31 10:52:07 +01:00
|
|
|
#[test]
|
|
|
|
fn enact_block() {
|
|
|
|
use spec::*;
|
|
|
|
let engine = Spec::new_test().to_engine().unwrap();
|
|
|
|
let genesis_header = engine.spec().genesis_header();
|
|
|
|
|
|
|
|
let mut db_result = get_temp_journal_db();
|
2016-02-03 19:34:51 +01:00
|
|
|
let mut db = db_result.take();
|
|
|
|
engine.spec().ensure_db_good(&mut db);
|
2016-03-01 20:02:59 +01:00
|
|
|
let b = OpenBlock::new(engine.deref(), db, &genesis_header, vec![genesis_header.hash()], Address::zero(), vec![]).close().seal(engine.deref(), vec![]).unwrap();
|
2016-01-31 10:52:07 +01:00
|
|
|
let orig_bytes = b.rlp_bytes();
|
|
|
|
let orig_db = b.drain();
|
|
|
|
|
|
|
|
let mut db_result = get_temp_journal_db();
|
2016-02-03 19:34:51 +01:00
|
|
|
let mut db = db_result.take();
|
|
|
|
engine.spec().ensure_db_good(&mut db);
|
2016-02-24 11:17:25 +01:00
|
|
|
let e = enact_and_seal(&orig_bytes, engine.deref(), db, &genesis_header, vec![genesis_header.hash()]).unwrap();
|
2016-01-31 10:52:07 +01:00
|
|
|
|
|
|
|
assert_eq!(e.rlp_bytes(), orig_bytes);
|
|
|
|
|
|
|
|
let db = e.drain();
|
|
|
|
assert_eq!(orig_db.keys(), db.keys());
|
|
|
|
assert!(orig_db.keys().iter().filter(|k| orig_db.get(k.0) != db.get(k.0)).next() == None);
|
|
|
|
}
|
2016-02-02 15:29:53 +01:00
|
|
|
}
|