2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-02-05 13:40:41 +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/>.
|
|
|
|
|
2016-02-02 15:29:53 +01:00
|
|
|
//! Blockchain block.
|
|
|
|
|
2017-01-05 13:59:16 +01:00
|
|
|
use std::cmp;
|
2016-10-03 23:29:46 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::collections::HashSet;
|
2017-08-31 11:53:26 +02:00
|
|
|
use hash::{keccak, KECCAK_NULL_RLP, KECCAK_EMPTY_LIST_RLP};
|
2017-09-03 09:11:14 +02:00
|
|
|
use triehash::ordered_trie_root;
|
2016-10-03 23:29:46 +02:00
|
|
|
|
2017-03-22 14:41:46 +01:00
|
|
|
use rlp::{UntrustedRlp, RlpStream, Encodable, Decodable, DecoderError};
|
2017-09-04 16:36:49 +02:00
|
|
|
use bigint::prelude::U256;
|
|
|
|
use bigint::hash::H256;
|
2017-09-06 20:47:45 +02:00
|
|
|
use util::Address;
|
|
|
|
use bytes::Bytes;
|
2017-09-05 12:14:03 +02:00
|
|
|
use unexpected::{Mismatch, OutOfBounds};
|
2016-10-03 23:29:46 +02:00
|
|
|
|
|
|
|
use basic_types::{LogBloom, Seal};
|
2017-08-01 12:37:57 +02:00
|
|
|
use vm::{EnvInfo, LastHashes};
|
2017-09-26 14:19:08 +02:00
|
|
|
use engines::EthEngine;
|
2016-10-03 23:29:46 +02:00
|
|
|
use error::{Error, BlockError, TransactionError};
|
|
|
|
use factory::Factories;
|
|
|
|
use header::Header;
|
2017-09-21 10:11:53 +02:00
|
|
|
use receipt::{Receipt, TransactionOutcome};
|
2016-10-03 23:29:46 +02:00
|
|
|
use state::State;
|
2016-09-27 18:02:11 +02:00
|
|
|
use state_db::StateDB;
|
2016-07-28 20:31:29 +02:00
|
|
|
use trace::FlatTrace;
|
2017-01-13 09:51:36 +01:00
|
|
|
use transaction::{UnverifiedTransaction, SignedTransaction};
|
2016-10-03 23:29:46 +02:00
|
|
|
use verification::PreverifiedBlock;
|
|
|
|
use views::BlockView;
|
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.
|
2016-07-11 18:31:18 +02:00
|
|
|
#[derive(Default, Debug, Clone, PartialEq)]
|
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.
|
2017-01-13 09:51:36 +01:00
|
|
|
pub transactions: Vec<UnverifiedTransaction>,
|
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
|
|
|
pub fn is_good(b: &[u8]) -> bool {
|
|
|
|
UntrustedRlp::new(b).as_val::<Block>().is_ok()
|
|
|
|
}
|
2016-07-14 15:24:12 +02:00
|
|
|
|
2016-08-05 17:00:46 +02:00
|
|
|
/// Get the RLP-encoding of the block with or without the seal.
|
2016-07-14 15:24:12 +02:00
|
|
|
pub fn rlp_bytes(&self, seal: Seal) -> Bytes {
|
|
|
|
let mut block_rlp = RlpStream::new_list(3);
|
|
|
|
self.header.stream_rlp(&mut block_rlp, seal);
|
2017-03-20 19:14:29 +01:00
|
|
|
block_rlp.append_list(&self.transactions);
|
|
|
|
block_rlp.append_list(&self.uncles);
|
2016-07-14 15:24:12 +02:00
|
|
|
block_rlp.out()
|
|
|
|
}
|
2016-01-26 19:18:22 +01:00
|
|
|
}
|
2016-01-08 19:12:19 +01:00
|
|
|
|
2016-07-14 15:24:12 +02:00
|
|
|
|
2016-01-26 19:18:22 +01:00
|
|
|
impl Decodable for Block {
|
2017-03-22 14:41:46 +01:00
|
|
|
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
|
|
|
|
if rlp.as_raw().len() != rlp.payload_info()?.total() {
|
2016-02-23 11:40:23 +01:00
|
|
|
return Err(DecoderError::RlpIsTooBig);
|
2016-01-26 19:18:22 +01:00
|
|
|
}
|
2017-03-22 14:41:46 +01:00
|
|
|
if rlp.item_count()? != 3 {
|
2016-01-26 19:18:22 +01:00
|
|
|
return Err(DecoderError::RlpIncorrectListLen);
|
|
|
|
}
|
|
|
|
Ok(Block {
|
2017-03-22 14:41:46 +01:00
|
|
|
header: rlp.val_at(0)?,
|
|
|
|
transactions: rlp.list_at(1)?,
|
|
|
|
uncles: rlp.list_at(2)?,
|
2016-01-26 19:18:22 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2016-01-10 14:05:39 +01:00
|
|
|
|
2016-09-28 23:31:59 +02:00
|
|
|
/// An internal type for a block's common elements.
|
2016-03-22 13:05:18 +01:00
|
|
|
#[derive(Clone)]
|
2016-01-26 19:18:22 +01:00
|
|
|
pub struct ExecutedBlock {
|
2017-01-13 09:51:36 +01:00
|
|
|
header: Header,
|
|
|
|
transactions: Vec<SignedTransaction>,
|
|
|
|
uncles: Vec<Header>,
|
2016-01-26 19:18:22 +01:00
|
|
|
receipts: Vec<Receipt>,
|
|
|
|
transactions_set: HashSet<H256>,
|
2017-02-21 12:35:21 +01:00
|
|
|
state: State<StateDB>,
|
2016-07-28 20:31:29 +02:00
|
|
|
traces: Option<Vec<Vec<FlatTrace>>>,
|
2017-09-26 14:19:08 +02:00
|
|
|
last_hashes: Arc<LastHashes>,
|
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-05-03 17:23:53 +02:00
|
|
|
pub header: &'a mut Header,
|
2016-02-02 16:24:37 +01:00
|
|
|
/// Block transactions.
|
2016-07-12 10:28:35 +02:00
|
|
|
pub transactions: &'a [SignedTransaction],
|
2016-02-02 16:24:37 +01:00
|
|
|
/// Block uncles.
|
2016-07-12 10:28:35 +02:00
|
|
|
pub uncles: &'a [Header],
|
2016-02-02 16:24:37 +01:00
|
|
|
/// Transaction receipts.
|
2016-07-12 10:28:35 +02:00
|
|
|
pub receipts: &'a [Receipt],
|
2016-02-02 16:24:37 +01:00
|
|
|
/// State.
|
2017-02-21 12:35:21 +01:00
|
|
|
pub state: &'a mut State<StateDB>,
|
2016-03-19 12:54:34 +01:00
|
|
|
/// Traces.
|
2017-08-03 15:55:58 +02:00
|
|
|
pub traces: &'a mut Option<Vec<Vec<FlatTrace>>>,
|
2016-01-10 17:11:46 +01:00
|
|
|
}
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
impl<'a> BlockRefMut<'a> {
|
|
|
|
/// Add traces if tracing is enabled.
|
|
|
|
pub fn push_traces(&mut self, tracer: ::trace::ExecutiveTracer) {
|
|
|
|
use trace::Tracer;
|
|
|
|
|
|
|
|
if let Some(ref mut traces) = self.traces.as_mut() {
|
|
|
|
traces.push(tracer.drain())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 23:03:22 +01:00
|
|
|
/// A set of immutable references to `ExecutedBlock` fields that are publicly accessible.
|
|
|
|
pub struct BlockRef<'a> {
|
|
|
|
/// Block header.
|
|
|
|
pub header: &'a Header,
|
|
|
|
/// Block transactions.
|
2016-07-12 10:28:35 +02:00
|
|
|
pub transactions: &'a [SignedTransaction],
|
2016-03-24 23:03:22 +01:00
|
|
|
/// Block uncles.
|
2016-07-12 10:28:35 +02:00
|
|
|
pub uncles: &'a [Header],
|
2016-03-24 23:03:22 +01:00
|
|
|
/// Transaction receipts.
|
2016-07-12 10:28:35 +02:00
|
|
|
pub receipts: &'a [Receipt],
|
2016-03-24 23:03:22 +01:00
|
|
|
/// State.
|
2017-02-21 12:35:21 +01:00
|
|
|
pub state: &'a State<StateDB>,
|
2016-03-24 23:03:22 +01:00
|
|
|
/// Traces.
|
2016-07-28 20:31:29 +02:00
|
|
|
pub traces: &'a Option<Vec<Vec<FlatTrace>>>,
|
2016-03-24 23:03:22 +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`.
|
2017-09-26 14:19:08 +02:00
|
|
|
fn new(state: State<StateDB>, last_hashes: Arc<LastHashes>, tracing: bool) -> ExecutedBlock {
|
2016-03-19 12:54:34 +01:00
|
|
|
ExecutedBlock {
|
2017-01-13 09:51:36 +01:00
|
|
|
header: Default::default(),
|
|
|
|
transactions: Default::default(),
|
|
|
|
uncles: Default::default(),
|
2016-03-19 12:54:34 +01:00
|
|
|
receipts: Default::default(),
|
|
|
|
transactions_set: Default::default(),
|
|
|
|
state: state,
|
|
|
|
traces: if tracing {Some(Vec::new())} else {None},
|
2017-09-26 14:19:08 +02:00
|
|
|
last_hashes: last_hashes,
|
2016-03-19 12:54:34 +01:00
|
|
|
}
|
|
|
|
}
|
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-03-24 23:03:22 +01:00
|
|
|
pub fn fields_mut(&mut self) -> BlockRefMut {
|
2016-01-10 17:09:02 +01:00
|
|
|
BlockRefMut {
|
2017-01-13 09:51:36 +01:00
|
|
|
header: &mut self.header,
|
|
|
|
transactions: &self.transactions,
|
|
|
|
uncles: &self.uncles,
|
2016-01-10 17:09:02 +01:00
|
|
|
state: &mut self.state,
|
2016-01-26 19:18:22 +01:00
|
|
|
receipts: &self.receipts,
|
2017-08-03 15:55:58 +02:00
|
|
|
traces: &mut self.traces,
|
2016-01-10 17:09:02 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-24 23:03:22 +01:00
|
|
|
|
|
|
|
/// Get a structure containing individual references to all public fields.
|
|
|
|
pub fn fields(&self) -> BlockRef {
|
|
|
|
BlockRef {
|
2017-01-13 09:51:36 +01:00
|
|
|
header: &self.header,
|
|
|
|
transactions: &self.transactions,
|
|
|
|
uncles: &self.uncles,
|
2016-03-24 23:03:22 +01:00
|
|
|
state: &self.state,
|
|
|
|
receipts: &self.receipts,
|
|
|
|
traces: &self.traces,
|
|
|
|
}
|
|
|
|
}
|
2017-09-26 14:19:08 +02:00
|
|
|
|
|
|
|
/// Get the environment info concerning this block.
|
|
|
|
pub fn env_info(&self) -> EnvInfo {
|
|
|
|
// TODO: memoise.
|
|
|
|
EnvInfo {
|
|
|
|
number: self.header.number(),
|
|
|
|
author: self.header.author().clone(),
|
|
|
|
timestamp: self.header.timestamp(),
|
|
|
|
difficulty: self.header.difficulty().clone(),
|
|
|
|
last_hashes: self.last_hashes.clone(),
|
|
|
|
gas_used: self.receipts.last().map_or(U256::zero(), |r| r.gas_used),
|
|
|
|
gas_limit: self.header.gas_limit().clone(),
|
|
|
|
}
|
|
|
|
}
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
|
|
|
|
2016-04-06 10:07:24 +02:00
|
|
|
/// Trait for a object that is a `ExecutedBlock`.
|
2016-01-08 21:33:41 +01:00
|
|
|
pub trait IsBlock {
|
2016-07-14 15:24:12 +02:00
|
|
|
/// Get the `ExecutedBlock` associated with this object.
|
2016-01-26 19:18:22 +01:00
|
|
|
fn block(&self) -> &ExecutedBlock;
|
2016-01-08 19:12:19 +01:00
|
|
|
|
2016-07-14 15:24:12 +02:00
|
|
|
/// Get the base `Block` object associated with this.
|
2017-01-13 09:51:36 +01:00
|
|
|
fn to_base(&self) -> Block {
|
|
|
|
Block {
|
|
|
|
header: self.header().clone(),
|
|
|
|
transactions: self.transactions().iter().cloned().map(Into::into).collect(),
|
|
|
|
uncles: self.uncles().to_vec(),
|
|
|
|
}
|
|
|
|
}
|
2016-07-14 15:24:12 +02:00
|
|
|
|
2016-01-08 19:12:19 +01:00
|
|
|
/// Get the header associated with this object's block.
|
2017-01-13 09:51:36 +01:00
|
|
|
fn header(&self) -> &Header { &self.block().header }
|
2016-01-08 19:12:19 +01:00
|
|
|
|
|
|
|
/// Get the final state associated with this object's block.
|
2017-02-21 12:35:21 +01:00
|
|
|
fn state(&self) -> &State<StateDB> { &self.block().state }
|
2016-01-08 19:12:19 +01:00
|
|
|
|
|
|
|
/// Get all information on transactions in this block.
|
2017-01-13 09:51:36 +01:00
|
|
|
fn transactions(&self) -> &[SignedTransaction] { &self.block().transactions }
|
2016-01-26 19:18:22 +01:00
|
|
|
|
|
|
|
/// Get all information on receipts in this block.
|
2016-07-12 10:28:35 +02:00
|
|
|
fn receipts(&self) -> &[Receipt] { &self.block().receipts }
|
2016-01-10 14:05:39 +01:00
|
|
|
|
2016-03-19 12:54:34 +01:00
|
|
|
/// Get all information concerning transaction tracing in this block.
|
2016-07-28 20:31:29 +02:00
|
|
|
fn traces(&self) -> &Option<Vec<Vec<FlatTrace>>> { &self.block().traces }
|
2016-03-19 12:54:34 +01:00
|
|
|
|
2016-01-10 14:05:39 +01:00
|
|
|
/// Get all uncles in this block.
|
2017-01-13 09:51:36 +01:00
|
|
|
fn uncles(&self) -> &[Header] { &self.block().uncles }
|
2017-07-18 12:14:06 +02:00
|
|
|
|
|
|
|
/// Get tracing enabled flag for this block.
|
2017-08-02 17:10:06 +02:00
|
|
|
fn tracing_enabled(&self) -> bool { self.block().traces.is_some() }
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
|
|
|
|
2016-06-29 21:49:12 +02:00
|
|
|
/// Trait for a object that has a state database.
|
|
|
|
pub trait Drain {
|
2017-07-13 09:48:00 +02:00
|
|
|
/// Drop this object and return the underlying database.
|
2016-09-27 18:02:11 +02:00
|
|
|
fn drain(self) -> StateDB;
|
2016-06-29 21:49:12 +02: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
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
impl ::parity_machine::LiveBlock for ExecutedBlock {
|
|
|
|
type Header = Header;
|
|
|
|
|
|
|
|
fn header(&self) -> &Header {
|
|
|
|
&self.header
|
|
|
|
}
|
|
|
|
|
|
|
|
fn uncles(&self) -> &[Header] {
|
|
|
|
&self.uncles
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::parity_machine::Transactions for ExecutedBlock {
|
|
|
|
type Transaction = SignedTransaction;
|
|
|
|
|
|
|
|
fn transactions(&self) -> &[SignedTransaction] {
|
|
|
|
&self.transactions
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2017-09-26 14:19:08 +02:00
|
|
|
engine: &'x EthEngine,
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
|
|
|
|
2016-04-06 10:07:24 +02:00
|
|
|
/// Just like `OpenBlock`, except that we've applied `Engine::on_close_block`, finished up the non-seal header fields,
|
2016-01-08 19:12:19 +01:00
|
|
|
/// and collected the uncles.
|
|
|
|
///
|
2016-02-29 14:57:41 +01:00
|
|
|
/// There is no function available to push a transaction.
|
2016-03-22 13:05:18 +01:00
|
|
|
#[derive(Clone)]
|
2016-02-29 14:57:41 +01:00
|
|
|
pub struct ClosedBlock {
|
|
|
|
block: ExecutedBlock,
|
2016-01-10 14:05:39 +01:00
|
|
|
uncle_bytes: Bytes,
|
2017-02-21 12:35:21 +01:00
|
|
|
unclosed_state: State<StateDB>,
|
2016-03-27 20:33:23 +02:00
|
|
|
}
|
|
|
|
|
2016-04-06 10:07:24 +02:00
|
|
|
/// Just like `ClosedBlock` except that we can't reopen it and it's faster.
|
2016-03-27 20:33:23 +02:00
|
|
|
///
|
|
|
|
/// We actually store the post-`Engine::on_close_block` state, unlike in `ClosedBlock` where it's the pre.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct LockedBlock {
|
|
|
|
block: ExecutedBlock,
|
|
|
|
uncle_bytes: Bytes,
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A block that has a valid seal.
|
|
|
|
///
|
2016-04-06 10:07:24 +02:00
|
|
|
/// The block's header has valid seal arguments. The block cannot be reversed into a `ClosedBlock` or `OpenBlock`.
|
2016-01-08 19:12:19 +01:00
|
|
|
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-04-06 10:07:24 +02:00
|
|
|
/// Create a new `OpenBlock` ready for transaction pushing.
|
2016-06-07 20:44:09 +02:00
|
|
|
pub fn new(
|
2017-09-26 14:19:08 +02:00
|
|
|
engine: &'x EthEngine,
|
2016-08-24 16:53:36 +02:00
|
|
|
factories: Factories,
|
2016-06-07 20:44:09 +02:00
|
|
|
tracing: bool,
|
2016-09-27 18:02:11 +02:00
|
|
|
db: StateDB,
|
2016-06-07 20:44:09 +02:00
|
|
|
parent: &Header,
|
2016-08-03 22:03:40 +02:00
|
|
|
last_hashes: Arc<LastHashes>,
|
2016-06-07 20:44:09 +02:00
|
|
|
author: Address,
|
2016-06-23 14:29:16 +02:00
|
|
|
gas_range_target: (U256, U256),
|
2016-06-18 20:26:44 +02:00
|
|
|
extra_data: Bytes,
|
2017-06-28 13:17:36 +02:00
|
|
|
is_epoch_begin: bool,
|
2016-06-07 20:44:09 +02:00
|
|
|
) -> Result<Self, Error> {
|
2017-06-28 09:10:57 +02:00
|
|
|
let number = parent.number() + 1;
|
|
|
|
let state = State::from_existing(db, parent.state_root().clone(), engine.account_start_nonce(number), factories)?;
|
2016-01-08 19:12:19 +01:00
|
|
|
let mut r = OpenBlock {
|
2017-09-26 14:19:08 +02:00
|
|
|
block: ExecutedBlock::new(state, last_hashes, tracing),
|
2016-01-08 19:12:19 +01:00
|
|
|
engine: engine,
|
2016-01-08 22:04:21 +01:00
|
|
|
};
|
2016-01-08 19:12:19 +01:00
|
|
|
|
2017-01-13 09:51:36 +01:00
|
|
|
r.block.header.set_parent_hash(parent.hash());
|
2017-06-28 09:10:57 +02:00
|
|
|
r.block.header.set_number(number);
|
2017-01-13 09:51:36 +01:00
|
|
|
r.block.header.set_author(author);
|
|
|
|
r.block.header.set_timestamp_now(parent.timestamp());
|
|
|
|
r.block.header.set_extra_data(extra_data);
|
|
|
|
r.block.header.note_dirty();
|
2016-01-10 22:55:07 +01:00
|
|
|
|
2017-01-05 13:59:16 +01:00
|
|
|
let gas_floor_target = cmp::max(gas_range_target.0, engine.params().min_gas_limit);
|
|
|
|
let gas_ceil_target = cmp::max(gas_range_target.1, gas_floor_target);
|
2017-09-26 14:19:08 +02:00
|
|
|
|
|
|
|
engine.machine().populate_from_parent(&mut r.block.header, parent, gas_floor_target, gas_ceil_target);
|
|
|
|
engine.populate_from_parent(&mut r.block.header, parent);
|
|
|
|
|
|
|
|
engine.machine().on_new_block(&mut r.block)?;
|
|
|
|
engine.on_new_block(&mut r.block, is_epoch_begin)?;
|
2017-06-28 13:17:36 +02:00
|
|
|
|
2016-06-07 20:44:09 +02:00
|
|
|
Ok(r)
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
|
|
|
|
2016-01-10 14:05:39 +01:00
|
|
|
/// Alter the author for the block.
|
2017-01-13 09:51:36 +01:00
|
|
|
pub fn set_author(&mut self, author: Address) { self.block.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.
|
2017-01-13 09:51:36 +01:00
|
|
|
pub fn set_timestamp(&mut self, timestamp: u64) { self.block.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.
|
2017-01-13 09:51:36 +01:00
|
|
|
pub fn set_difficulty(&mut self, a: U256) { self.block.header.set_difficulty(a); }
|
2016-01-15 23:32:17 +01:00
|
|
|
|
|
|
|
/// Alter the gas limit for the block.
|
2017-01-13 09:51:36 +01:00
|
|
|
pub fn set_gas_limit(&mut self, a: U256) { self.block.header.set_gas_limit(a); }
|
2016-01-15 23:32:17 +01:00
|
|
|
|
|
|
|
/// Alter the gas limit for the block.
|
2017-01-13 09:51:36 +01:00
|
|
|
pub fn set_gas_used(&mut self, a: U256) { self.block.header.set_gas_used(a); }
|
2016-01-15 23:32:17 +01:00
|
|
|
|
2016-07-19 09:23:53 +02:00
|
|
|
/// Alter the uncles hash the block.
|
2017-01-13 09:51:36 +01:00
|
|
|
pub fn set_uncles_hash(&mut self, h: H256) { self.block.header.set_uncles_hash(h); }
|
2016-07-19 09:23:53 +02:00
|
|
|
|
|
|
|
/// Alter transactions root for the block.
|
2017-01-13 09:51:36 +01:00
|
|
|
pub fn set_transactions_root(&mut self, h: H256) { self.block.header.set_transactions_root(h); }
|
2016-07-19 09:23:53 +02:00
|
|
|
|
|
|
|
/// Alter the receipts root for the block.
|
2017-01-13 09:51:36 +01:00
|
|
|
pub fn set_receipts_root(&mut self, h: H256) { self.block.header.set_receipts_root(h); }
|
2016-07-19 09:23:53 +02: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 {
|
2017-01-13 09:51:36 +01:00
|
|
|
self.block.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> {
|
2017-12-05 15:57:45 +01:00
|
|
|
let max_uncles = self.engine.maximum_uncle_count(self.block.header().number());
|
|
|
|
if self.block.uncles.len() + 1 > max_uncles {
|
|
|
|
return Err(BlockError::TooManyUncles(OutOfBounds{
|
|
|
|
min: None,
|
|
|
|
max: Some(max_uncles),
|
|
|
|
found: self.block.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)
|
2017-01-13 09:51:36 +01:00
|
|
|
self.block.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 {
|
2017-09-26 14:19:08 +02:00
|
|
|
self.block.env_info()
|
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-03-24 23:03:22 +01:00
|
|
|
if self.block.transactions_set.contains(&t.hash()) {
|
|
|
|
return Err(From::from(TransactionError::AlreadyImported));
|
2016-03-23 17:28:02 +01:00
|
|
|
}
|
|
|
|
|
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);
|
2017-09-26 14:19:08 +02:00
|
|
|
match self.block.state.apply(&env_info, self.engine.machine(), &t, self.block.traces.is_some()) {
|
2016-03-19 12:54:34 +01:00
|
|
|
Ok(outcome) => {
|
2016-01-26 19:18:22 +01:00
|
|
|
self.block.transactions_set.insert(h.unwrap_or_else(||t.hash()));
|
2017-01-13 09:51:36 +01:00
|
|
|
self.block.transactions.push(t.into());
|
2016-03-19 12:54:34 +01:00
|
|
|
let t = outcome.trace;
|
2016-07-28 20:31:29 +02:00
|
|
|
self.block.traces.as_mut().map(|traces| traces.push(t));
|
2016-03-19 12:54:34 +01:00
|
|
|
self.block.receipts.push(outcome.receipt);
|
2016-10-20 23:41:15 +02:00
|
|
|
Ok(self.block.receipts.last().expect("receipt just pushed; qed"))
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-13 09:48:00 +02:00
|
|
|
/// Push transactions onto the block.
|
|
|
|
pub fn push_transactions(&mut self, transactions: &[SignedTransaction]) -> Result<(), Error> {
|
|
|
|
push_transactions(self, transactions)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Populate self from a header.
|
|
|
|
pub fn populate_from(&mut self, header: &Header) {
|
|
|
|
self.set_difficulty(*header.difficulty());
|
|
|
|
self.set_gas_limit(*header.gas_limit());
|
|
|
|
self.set_timestamp(header.timestamp());
|
|
|
|
self.set_author(header.author().clone());
|
|
|
|
self.set_extra_data(header.extra_data().clone()).unwrap_or_else(|e| warn!("Couldn't set extradata: {}. Ignoring.", e));
|
|
|
|
self.set_uncles_hash(header.uncles_hash().clone());
|
|
|
|
self.set_transactions_root(header.transactions_root().clone());
|
|
|
|
}
|
|
|
|
|
2016-09-21 12:49:11 +02:00
|
|
|
/// Turn this into a `ClosedBlock`.
|
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;
|
2016-03-27 20:33:23 +02:00
|
|
|
|
2016-09-28 23:31:59 +02:00
|
|
|
let unclosed_state = s.block.state.clone();
|
2016-03-27 20:33:23 +02:00
|
|
|
|
2017-05-30 11:52:33 +02:00
|
|
|
if let Err(e) = s.engine.on_close_block(&mut s.block) {
|
|
|
|
warn!("Encountered error on closing the block: {}", e);
|
|
|
|
}
|
2017-08-31 11:53:26 +02:00
|
|
|
|
2017-01-25 20:22:48 +01:00
|
|
|
if let Err(e) = s.block.state.commit() {
|
|
|
|
warn!("Encountered error on state commit: {}", e);
|
|
|
|
}
|
2017-06-28 14:16:53 +02:00
|
|
|
s.block.header.set_transactions_root(ordered_trie_root(s.block.transactions.iter().map(|e| e.rlp_bytes().into_vec())));
|
2017-01-13 09:51:36 +01:00
|
|
|
let uncle_bytes = s.block.uncles.iter().fold(RlpStream::new_list(s.block.uncles.len()), |mut s, u| {s.append_raw(&u.rlp(Seal::With), 1); s} ).out();
|
2017-08-30 19:18:28 +02:00
|
|
|
s.block.header.set_uncles_hash(keccak(&uncle_bytes));
|
2017-01-13 09:51:36 +01:00
|
|
|
s.block.header.set_state_root(s.block.state.root().clone());
|
2017-06-28 14:16:53 +02:00
|
|
|
s.block.header.set_receipts_root(ordered_trie_root(s.block.receipts.iter().map(|r| r.rlp_bytes().into_vec())));
|
2017-01-13 09:51:36 +01:00
|
|
|
s.block.header.set_log_bloom(s.block.receipts.iter().fold(LogBloom::zero(), |mut b, r| {b = &b | &r.log_bloom; b})); //TODO: use |= operator
|
|
|
|
s.block.header.set_gas_used(s.block.receipts.last().map_or(U256::zero(), |r| r.gas_used));
|
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-09-28 23:31:59 +02:00
|
|
|
unclosed_state: unclosed_state,
|
2016-03-27 20:33:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 12:49:11 +02:00
|
|
|
/// Turn this into a `LockedBlock`.
|
2016-03-27 20:33:23 +02:00
|
|
|
pub fn close_and_lock(self) -> LockedBlock {
|
|
|
|
let mut s = self;
|
|
|
|
|
2017-05-30 11:52:33 +02:00
|
|
|
if let Err(e) = s.engine.on_close_block(&mut s.block) {
|
|
|
|
warn!("Encountered error on closing the block: {}", e);
|
|
|
|
}
|
2017-01-25 20:22:48 +01:00
|
|
|
|
|
|
|
if let Err(e) = s.block.state.commit() {
|
|
|
|
warn!("Encountered error on state commit: {}", e);
|
|
|
|
}
|
2017-08-30 19:18:28 +02:00
|
|
|
if s.block.header.transactions_root().is_zero() || s.block.header.transactions_root() == &KECCAK_NULL_RLP {
|
2017-06-28 14:16:53 +02:00
|
|
|
s.block.header.set_transactions_root(ordered_trie_root(s.block.transactions.iter().map(|e| e.rlp_bytes().into_vec())));
|
2016-07-19 09:23:53 +02:00
|
|
|
}
|
2017-01-13 09:51:36 +01:00
|
|
|
let uncle_bytes = s.block.uncles.iter().fold(RlpStream::new_list(s.block.uncles.len()), |mut s, u| {s.append_raw(&u.rlp(Seal::With), 1); s} ).out();
|
2017-08-31 11:53:26 +02:00
|
|
|
if s.block.header.uncles_hash().is_zero() || s.block.header.uncles_hash() == &KECCAK_EMPTY_LIST_RLP {
|
2017-08-30 19:18:28 +02:00
|
|
|
s.block.header.set_uncles_hash(keccak(&uncle_bytes));
|
2016-07-19 09:23:53 +02:00
|
|
|
}
|
2017-08-30 19:18:28 +02:00
|
|
|
if s.block.header.receipts_root().is_zero() || s.block.header.receipts_root() == &KECCAK_NULL_RLP {
|
2017-06-28 14:16:53 +02:00
|
|
|
s.block.header.set_receipts_root(ordered_trie_root(s.block.receipts.iter().map(|r| r.rlp_bytes().into_vec())));
|
2016-07-19 09:23:53 +02:00
|
|
|
}
|
2016-09-21 12:49:11 +02:00
|
|
|
|
2017-01-13 09:51:36 +01:00
|
|
|
s.block.header.set_state_root(s.block.state.root().clone());
|
|
|
|
s.block.header.set_log_bloom(s.block.receipts.iter().fold(LogBloom::zero(), |mut b, r| {b = &b | &r.log_bloom; b})); //TODO: use |= operator
|
|
|
|
s.block.header.set_gas_used(s.block.receipts.last().map_or(U256::zero(), |r| r.gas_used));
|
2016-03-27 20:33:23 +02:00
|
|
|
|
2016-09-28 23:31:59 +02:00
|
|
|
LockedBlock {
|
2016-03-27 20:33:23 +02:00
|
|
|
block: s.block,
|
|
|
|
uncle_bytes: uncle_bytes,
|
2016-09-28 23:31:59 +02:00
|
|
|
}
|
2016-01-09 18:58:04 +01:00
|
|
|
}
|
2016-10-14 14:44:56 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
/// Return mutable block reference. To be used in tests only.
|
2017-06-28 13:17:36 +02:00
|
|
|
pub fn block_mut(&mut self) -> &mut ExecutedBlock { &mut self.block }
|
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-03-27 20:33:23 +02:00
|
|
|
impl<'x> IsBlock for LockedBlock {
|
|
|
|
fn block(&self) -> &ExecutedBlock { &self.block }
|
|
|
|
}
|
|
|
|
|
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.
|
2017-08-30 19:18:28 +02:00
|
|
|
pub fn hash(&self) -> H256 { self.header().rlp_keccak(Seal::Without) }
|
2016-01-08 19:12:19 +01:00
|
|
|
|
2016-03-27 20:33:23 +02:00
|
|
|
/// Turn this into a `LockedBlock`, unable to be reopened again.
|
2016-09-29 12:46:04 +02:00
|
|
|
pub fn lock(self) -> LockedBlock {
|
2016-03-27 20:33:23 +02:00
|
|
|
LockedBlock {
|
|
|
|
block: self.block,
|
|
|
|
uncle_bytes: self.uncle_bytes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Given an engine reference, reopen the `ClosedBlock` into an `OpenBlock`.
|
2017-09-26 14:19:08 +02:00
|
|
|
pub fn reopen(self, engine: &EthEngine) -> OpenBlock {
|
2016-03-27 20:33:23 +02:00
|
|
|
// revert rewards (i.e. set state back at last transaction's state).
|
2016-09-28 23:31:59 +02:00
|
|
|
let mut block = self.block;
|
|
|
|
block.state = self.unclosed_state;
|
2016-03-27 20:33:23 +02:00
|
|
|
OpenBlock {
|
2016-09-28 23:31:59 +02:00
|
|
|
block: block,
|
2016-03-27 20:33:23 +02:00
|
|
|
engine: engine,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LockedBlock {
|
|
|
|
/// Get the hash of the header without seal arguments.
|
2017-08-30 19:18:28 +02:00
|
|
|
pub fn hash(&self) -> H256 { self.header().rlp_keccak(Seal::Without) }
|
2016-03-27 20:33:23 +02: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.
|
2017-09-26 14:19:08 +02:00
|
|
|
pub fn seal(self, engine: &EthEngine, 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
|
|
|
}
|
2017-01-13 09:51:36 +01:00
|
|
|
s.block.header.set_seal(seal);
|
2016-02-29 14:57:41 +01:00
|
|
|
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.
|
2017-04-11 17:07:04 +02:00
|
|
|
pub fn try_seal(
|
|
|
|
self,
|
2017-09-26 14:19:08 +02:00
|
|
|
engine: &EthEngine,
|
2017-04-11 17:07:04 +02:00
|
|
|
seal: Vec<Bytes>,
|
|
|
|
) -> Result<SealedBlock, (Error, LockedBlock)> {
|
2016-03-01 00:02:48 +01:00
|
|
|
let mut s = self;
|
2017-01-13 09:51:36 +01:00
|
|
|
s.block.header.set_seal(seal);
|
2017-09-26 14:19:08 +02:00
|
|
|
|
|
|
|
// TODO: passing state context to avoid engines owning it?
|
|
|
|
match engine.verify_local_seal(&s.block.header) {
|
2016-09-28 23:31:59 +02:00
|
|
|
Err(e) => Err((e, s)),
|
2016-03-01 17:23:44 +01:00
|
|
|
_ => Ok(SealedBlock { block: s.block, uncle_bytes: s.uncle_bytes }),
|
2016-03-01 00:02:48 +01:00
|
|
|
}
|
|
|
|
}
|
2017-03-29 19:59:20 +02:00
|
|
|
|
|
|
|
/// Remove state root from transaction receipts to make them EIP-98 compatible.
|
|
|
|
pub fn strip_receipts(self) -> LockedBlock {
|
|
|
|
let mut block = self;
|
|
|
|
for receipt in &mut block.block.receipts {
|
2017-09-21 10:11:53 +02:00
|
|
|
receipt.outcome = TransactionOutcome::Unknown;
|
2017-03-29 19:59:20 +02:00
|
|
|
}
|
2017-06-28 14:16:53 +02:00
|
|
|
block.block.header.set_receipts_root(ordered_trie_root(block.block.receipts.iter().map(|r| r.rlp_bytes().into_vec())));
|
2017-03-29 19:59:20 +02:00
|
|
|
block
|
|
|
|
}
|
2016-06-29 21:49:12 +02:00
|
|
|
}
|
2016-03-01 00:02:48 +01:00
|
|
|
|
2016-06-29 21:49:12 +02:00
|
|
|
impl Drain for LockedBlock {
|
2016-01-14 19:03:48 +01:00
|
|
|
/// Drop this object and return the underlieing database.
|
2016-09-27 18:02:11 +02:00
|
|
|
fn drain(self) -> StateDB {
|
|
|
|
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);
|
2017-01-13 09:51:36 +01:00
|
|
|
self.block.header.stream_rlp(&mut block_rlp, Seal::With);
|
2017-03-20 19:14:29 +01:00
|
|
|
block_rlp.append_list(&self.block.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
|
|
|
}
|
2016-06-29 21:49:12 +02:00
|
|
|
}
|
2016-01-10 23:10:06 +01:00
|
|
|
|
2016-06-29 21:49:12 +02:00
|
|
|
impl Drain for SealedBlock {
|
2016-01-10 23:10:06 +01:00
|
|
|
/// Drop this object and return the underlieing database.
|
2016-09-27 18:02:11 +02:00
|
|
|
fn drain(self) -> StateDB {
|
|
|
|
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-06-18 20:26:44 +02:00
|
|
|
pub fn enact(
|
|
|
|
header: &Header,
|
|
|
|
transactions: &[SignedTransaction],
|
|
|
|
uncles: &[Header],
|
2017-09-26 14:19:08 +02:00
|
|
|
engine: &EthEngine,
|
2016-06-18 20:26:44 +02:00
|
|
|
tracing: bool,
|
2016-09-27 18:02:11 +02:00
|
|
|
db: StateDB,
|
2016-06-18 20:26:44 +02:00
|
|
|
parent: &Header,
|
2016-08-03 22:03:40 +02:00
|
|
|
last_hashes: Arc<LastHashes>,
|
2016-08-24 16:53:36 +02:00
|
|
|
factories: Factories,
|
2017-06-28 13:17:36 +02:00
|
|
|
is_epoch_begin: bool,
|
2016-06-18 20:26:44 +02:00
|
|
|
) -> Result<LockedBlock, 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 {
|
2017-06-28 09:10:57 +02:00
|
|
|
let s = State::from_existing(db.boxed_clone(), parent.state_root().clone(), engine.account_start_nonce(parent.number() + 1), factories.clone())?;
|
2017-02-26 13:10:50 +01:00
|
|
|
trace!(target: "enact", "num={}, root={}, author={}, author_balance={}\n",
|
|
|
|
header.number(), s.root(), header.author(), s.balance(&header.author())?);
|
2016-02-05 01:49:06 +01:00
|
|
|
}
|
2016-01-15 22:55:04 +01:00
|
|
|
}
|
2016-01-15 23:32:17 +01:00
|
|
|
|
2017-06-28 13:17:36 +02:00
|
|
|
let mut b = OpenBlock::new(
|
|
|
|
engine,
|
|
|
|
factories,
|
|
|
|
tracing,
|
|
|
|
db,
|
|
|
|
parent,
|
|
|
|
last_hashes,
|
|
|
|
Address::new(),
|
|
|
|
(3141562.into(), 31415620.into()),
|
|
|
|
vec![],
|
|
|
|
is_epoch_begin,
|
|
|
|
)?;
|
|
|
|
|
2017-07-13 09:48:00 +02:00
|
|
|
b.populate_from(header);
|
|
|
|
b.push_transactions(transactions)?;
|
2016-10-03 23:29:46 +02:00
|
|
|
|
|
|
|
for u in uncles {
|
2016-12-27 12:53:56 +01:00
|
|
|
b.push_uncle(u.clone())?;
|
2016-10-03 23:29:46 +02:00
|
|
|
}
|
2017-07-13 09:48:00 +02:00
|
|
|
|
2016-03-27 20:33:23 +02:00
|
|
|
Ok(b.close_and_lock())
|
2016-01-14 19:03:48 +01:00
|
|
|
}
|
|
|
|
|
2016-10-27 08:28:12 +02:00
|
|
|
#[inline]
|
2016-10-03 23:29:46 +02:00
|
|
|
#[cfg(not(feature = "slow-blocks"))]
|
|
|
|
fn push_transactions(block: &mut OpenBlock, transactions: &[SignedTransaction]) -> Result<(), Error> {
|
|
|
|
for t in transactions {
|
2016-12-27 12:53:56 +01:00
|
|
|
block.push_transaction(t.clone(), None)?;
|
2016-10-03 23:29:46 +02:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "slow-blocks")]
|
|
|
|
fn push_transactions(block: &mut OpenBlock, transactions: &[SignedTransaction]) -> Result<(), Error> {
|
|
|
|
use std::time;
|
|
|
|
|
|
|
|
let slow_tx = option_env!("SLOW_TX_DURATION").and_then(|v| v.parse().ok()).unwrap_or(100);
|
|
|
|
for t in transactions {
|
|
|
|
let hash = t.hash();
|
|
|
|
let start = time::Instant::now();
|
2016-12-27 12:53:56 +01:00
|
|
|
block.push_transaction(t.clone(), None)?;
|
2016-10-03 23:29:46 +02:00
|
|
|
let took = start.elapsed();
|
2017-01-25 11:02:03 +01:00
|
|
|
let took_ms = took.as_secs() * 1000 + took.subsec_nanos() as u64 / 1000000;
|
2016-10-03 23:29:46 +02:00
|
|
|
if took > time::Duration::from_millis(slow_tx) {
|
2017-01-25 11:02:03 +01:00
|
|
|
warn!("Heavy ({} ms) transaction in block {:?}: {:?}", took_ms, block.header().number(), hash);
|
2016-10-03 23:29:46 +02:00
|
|
|
}
|
2017-01-25 11:02:03 +01:00
|
|
|
debug!(target: "tx", "Transaction {:?} took: {} ms", hash, took_ms);
|
2016-10-03 23:29:46 +02:00
|
|
|
}
|
|
|
|
Ok(())
|
2016-01-17 23:07:58 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 09:51:36 +01:00
|
|
|
// TODO [ToDr] Pass `PreverifiedBlock` by move, this will avoid unecessary allocation
|
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-06-18 20:26:44 +02:00
|
|
|
pub fn enact_verified(
|
|
|
|
block: &PreverifiedBlock,
|
2017-09-26 14:19:08 +02:00
|
|
|
engine: &EthEngine,
|
2016-06-18 20:26:44 +02:00
|
|
|
tracing: bool,
|
2016-09-27 18:02:11 +02:00
|
|
|
db: StateDB,
|
2016-06-18 20:26:44 +02:00
|
|
|
parent: &Header,
|
2016-08-03 22:03:40 +02:00
|
|
|
last_hashes: Arc<LastHashes>,
|
2016-08-24 16:53:36 +02:00
|
|
|
factories: Factories,
|
2017-06-28 13:17:36 +02:00
|
|
|
is_epoch_begin: bool,
|
2016-06-18 20:26:44 +02:00
|
|
|
) -> Result<LockedBlock, Error> {
|
2016-01-17 23:07:58 +01:00
|
|
|
let view = BlockView::new(&block.bytes);
|
2017-06-28 13:17:36 +02:00
|
|
|
|
|
|
|
enact(
|
|
|
|
&block.header,
|
|
|
|
&block.transactions,
|
|
|
|
&view.uncles(),
|
|
|
|
engine,
|
|
|
|
tracing,
|
|
|
|
db,
|
|
|
|
parent,
|
|
|
|
last_hashes,
|
|
|
|
factories,
|
|
|
|
is_epoch_begin,
|
|
|
|
)
|
2016-01-17 23:07:58 +01:00
|
|
|
}
|
|
|
|
|
2016-01-31 10:52:07 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use tests::helpers::*;
|
|
|
|
use super::*;
|
2017-09-26 14:19:08 +02:00
|
|
|
use engines::EthEngine;
|
2017-08-01 12:37:57 +02:00
|
|
|
use vm::LastHashes;
|
2016-10-24 18:35:25 +02:00
|
|
|
use error::Error;
|
|
|
|
use header::Header;
|
2016-10-03 23:29:46 +02:00
|
|
|
use factory::Factories;
|
|
|
|
use state_db::StateDB;
|
2016-10-24 18:35:25 +02:00
|
|
|
use views::BlockView;
|
2016-12-23 18:44:39 +01:00
|
|
|
use util::Address;
|
2016-10-24 18:35:25 +02:00
|
|
|
use std::sync::Arc;
|
2017-01-13 09:51:36 +01:00
|
|
|
use transaction::SignedTransaction;
|
2016-10-03 23:29:46 +02:00
|
|
|
|
|
|
|
/// Enact the block given by `block_bytes` using `engine` on the database `db` with given `parent` block header
|
|
|
|
fn enact_bytes(
|
|
|
|
block_bytes: &[u8],
|
2017-09-26 14:19:08 +02:00
|
|
|
engine: &EthEngine,
|
2016-10-03 23:29:46 +02:00
|
|
|
tracing: bool,
|
|
|
|
db: StateDB,
|
|
|
|
parent: &Header,
|
|
|
|
last_hashes: Arc<LastHashes>,
|
|
|
|
factories: Factories,
|
|
|
|
) -> Result<LockedBlock, Error> {
|
|
|
|
let block = BlockView::new(block_bytes);
|
|
|
|
let header = block.header();
|
2017-01-13 09:51:36 +01:00
|
|
|
let transactions: Result<Vec<_>, Error> = block.transactions().into_iter().map(SignedTransaction::new).collect();
|
|
|
|
let transactions = transactions?;
|
2017-07-13 09:48:00 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
if ::log::max_log_level() >= ::log::LogLevel::Trace {
|
|
|
|
let s = State::from_existing(db.boxed_clone(), parent.state_root().clone(), engine.account_start_nonce(parent.number() + 1), factories.clone())?;
|
|
|
|
trace!(target: "enact", "num={}, root={}, author={}, author_balance={}\n",
|
|
|
|
header.number(), s.root(), header.author(), s.balance(&header.author())?);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut b = OpenBlock::new(
|
|
|
|
engine,
|
|
|
|
factories,
|
|
|
|
tracing,
|
|
|
|
db,
|
|
|
|
parent,
|
|
|
|
last_hashes,
|
|
|
|
Address::new(),
|
|
|
|
(3141562.into(), 31415620.into()),
|
|
|
|
vec![],
|
|
|
|
false,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
b.populate_from(&header);
|
|
|
|
b.push_transactions(&transactions)?;
|
|
|
|
|
|
|
|
for u in &block.uncles() {
|
|
|
|
b.push_uncle(u.clone())?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(b.close_and_lock())
|
2016-10-03 23:29:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Enact the block given by `block_bytes` using `engine` on the database `db` with given `parent` block header. Seal the block aferwards
|
|
|
|
fn enact_and_seal(
|
|
|
|
block_bytes: &[u8],
|
2017-09-26 14:19:08 +02:00
|
|
|
engine: &EthEngine,
|
2016-10-03 23:29:46 +02:00
|
|
|
tracing: bool,
|
|
|
|
db: StateDB,
|
|
|
|
parent: &Header,
|
|
|
|
last_hashes: Arc<LastHashes>,
|
|
|
|
factories: Factories,
|
|
|
|
) -> Result<SealedBlock, Error> {
|
|
|
|
let header = BlockView::new(block_bytes).header_view();
|
2016-12-27 12:53:56 +01:00
|
|
|
Ok(enact_bytes(block_bytes, engine, tracing, db, parent, last_hashes, factories)?.seal(engine, header.seal())?)
|
2016-10-03 23:29:46 +02:00
|
|
|
}
|
2016-01-31 10:52:07 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn open_block() {
|
|
|
|
use spec::*;
|
2016-04-09 19:20:35 +02:00
|
|
|
let spec = Spec::new_test();
|
|
|
|
let genesis_header = spec.genesis_header();
|
2017-04-06 19:26:17 +02:00
|
|
|
let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
|
2016-08-03 22:03:40 +02:00
|
|
|
let last_hashes = Arc::new(vec![genesis_header.hash()]);
|
2017-06-28 13:17:36 +02:00
|
|
|
let b = OpenBlock::new(&*spec.engine, Default::default(), false, db, &genesis_header, last_hashes, Address::zero(), (3141562.into(), 31415620.into()), vec![], false).unwrap();
|
2016-03-27 20:33:23 +02:00
|
|
|
let b = b.close_and_lock();
|
2016-08-10 16:29:40 +02:00
|
|
|
let _ = b.seal(&*spec.engine, 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::*;
|
2016-04-09 19:20:35 +02:00
|
|
|
let spec = Spec::new_test();
|
2016-08-10 16:29:40 +02:00
|
|
|
let engine = &*spec.engine;
|
2016-04-09 19:20:35 +02:00
|
|
|
let genesis_header = spec.genesis_header();
|
2016-01-31 10:52:07 +01:00
|
|
|
|
2017-04-06 19:26:17 +02:00
|
|
|
let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
|
2016-08-03 22:03:40 +02:00
|
|
|
let last_hashes = Arc::new(vec![genesis_header.hash()]);
|
2017-06-28 13:17:36 +02:00
|
|
|
let b = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes.clone(), Address::zero(), (3141562.into(), 31415620.into()), vec![], false).unwrap()
|
2016-08-10 16:29:40 +02:00
|
|
|
.close_and_lock().seal(engine, vec![]).unwrap();
|
2016-01-31 10:52:07 +01:00
|
|
|
let orig_bytes = b.rlp_bytes();
|
|
|
|
let orig_db = b.drain();
|
|
|
|
|
2017-04-06 19:26:17 +02:00
|
|
|
let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
|
2016-08-24 16:53:36 +02:00
|
|
|
let e = enact_and_seal(&orig_bytes, engine, false, db, &genesis_header, last_hashes, Default::default()).unwrap();
|
2016-01-31 10:52:07 +01:00
|
|
|
|
|
|
|
assert_eq!(e.rlp_bytes(), orig_bytes);
|
|
|
|
|
|
|
|
let db = e.drain();
|
2016-09-27 18:02:11 +02:00
|
|
|
assert_eq!(orig_db.journal_db().keys(), db.journal_db().keys());
|
|
|
|
assert!(orig_db.journal_db().keys().iter().filter(|k| orig_db.journal_db().get(k.0) != db.journal_db().get(k.0)).next() == None);
|
2016-01-31 10:52:07 +01:00
|
|
|
}
|
2016-03-14 18:20:24 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn enact_block_with_uncle() {
|
2016-03-15 14:35:45 +01:00
|
|
|
use spec::*;
|
2016-04-09 19:20:35 +02:00
|
|
|
let spec = Spec::new_test();
|
2016-08-10 16:29:40 +02:00
|
|
|
let engine = &*spec.engine;
|
2016-04-09 19:20:35 +02:00
|
|
|
let genesis_header = spec.genesis_header();
|
2016-03-15 14:35:45 +01:00
|
|
|
|
2017-04-06 19:26:17 +02:00
|
|
|
let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
|
2016-08-03 22:03:40 +02:00
|
|
|
let last_hashes = Arc::new(vec![genesis_header.hash()]);
|
2017-06-28 13:17:36 +02:00
|
|
|
let mut open_block = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes.clone(), Address::zero(), (3141562.into(), 31415620.into()), vec![], false).unwrap();
|
2016-03-15 14:35:45 +01:00
|
|
|
let mut uncle1_header = Header::new();
|
2017-06-28 16:41:08 +02:00
|
|
|
uncle1_header.set_extra_data(b"uncle1".to_vec());
|
2016-03-15 14:35:45 +01:00
|
|
|
let mut uncle2_header = Header::new();
|
2017-06-28 16:41:08 +02:00
|
|
|
uncle2_header.set_extra_data(b"uncle2".to_vec());
|
2016-03-15 14:35:45 +01:00
|
|
|
open_block.push_uncle(uncle1_header).unwrap();
|
|
|
|
open_block.push_uncle(uncle2_header).unwrap();
|
2016-08-10 16:29:40 +02:00
|
|
|
let b = open_block.close_and_lock().seal(engine, vec![]).unwrap();
|
2016-03-19 18:13:14 +01:00
|
|
|
|
2016-03-15 14:35:45 +01:00
|
|
|
let orig_bytes = b.rlp_bytes();
|
|
|
|
let orig_db = b.drain();
|
|
|
|
|
2017-04-06 19:26:17 +02:00
|
|
|
let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
|
2016-08-24 16:53:36 +02:00
|
|
|
let e = enact_and_seal(&orig_bytes, engine, false, db, &genesis_header, last_hashes, Default::default()).unwrap();
|
2016-03-15 14:35:45 +01:00
|
|
|
|
|
|
|
let bytes = e.rlp_bytes();
|
|
|
|
assert_eq!(bytes, orig_bytes);
|
|
|
|
let uncles = BlockView::new(&bytes).uncles();
|
2016-08-29 11:35:24 +02:00
|
|
|
assert_eq!(uncles[1].extra_data(), b"uncle2");
|
2016-03-15 14:35:45 +01:00
|
|
|
|
|
|
|
let db = e.drain();
|
2016-09-27 18:02:11 +02:00
|
|
|
assert_eq!(orig_db.journal_db().keys(), db.journal_db().keys());
|
|
|
|
assert!(orig_db.journal_db().keys().iter().filter(|k| orig_db.journal_db().get(k.0) != db.journal_db().get(k.0)).next() == None);
|
2016-03-14 18:20:24 +01:00
|
|
|
}
|
2016-02-02 15:29:53 +01:00
|
|
|
}
|