2016-01-09 12:30:41 +01:00
|
|
|
use util::*;
|
2016-01-08 19:12:19 +01:00
|
|
|
use transaction::*;
|
2016-01-08 22:04:21 +01:00
|
|
|
use receipt::*;
|
2016-01-08 19:12:19 +01:00
|
|
|
use engine::*;
|
2016-01-08 21:33:41 +01:00
|
|
|
use header::*;
|
2016-01-08 19:12:19 +01:00
|
|
|
use env_info::*;
|
2016-01-08 21:33:41 +01:00
|
|
|
use state::*;
|
2016-01-08 19:12:19 +01:00
|
|
|
|
|
|
|
/// A transaction/receipt execution entry.
|
|
|
|
pub struct Entry {
|
|
|
|
transaction: Transaction,
|
|
|
|
receipt: Receipt,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Internal type for a block's common elements.
|
|
|
|
pub struct Block {
|
|
|
|
header: Header,
|
|
|
|
|
|
|
|
/// State is the most final state in the block.
|
|
|
|
state: State,
|
|
|
|
|
|
|
|
archive: Vec<Entry>,
|
|
|
|
archive_set: HashSet<H256>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Block {
|
2016-01-08 22:04:21 +01:00
|
|
|
fn new(state: State) -> Block {
|
2016-01-08 19:12:19 +01:00
|
|
|
Block {
|
2016-01-08 21:33:41 +01:00
|
|
|
header: Header::new(),
|
2016-01-08 19:12:19 +01:00
|
|
|
state: state,
|
|
|
|
archive: Vec::new(),
|
|
|
|
archive_set: HashSet::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-08 21:33:41 +01:00
|
|
|
pub fn state_mut(&mut self) -> &mut State { &mut self.state }
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Trait for a object that is_a `Block`.
|
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.
|
|
|
|
fn block(&self) -> &Block;
|
|
|
|
|
|
|
|
/// Get the header associated with this object's block.
|
2016-01-08 21:33:41 +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.
|
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-01-08 21:33:41 +01:00
|
|
|
fn archive(&self) -> &Vec<Entry> { &self.block().archive }
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl IsBlock for Block {
|
|
|
|
fn block(&self) -> &Block { self }
|
|
|
|
}
|
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.
|
|
|
|
///
|
|
|
|
/// It's a bit like a Vec<Transaction>, eccept that whenever a transaction is pushed, we execute it and
|
|
|
|
/// maintain the system `state()`. We also archive execution receipts in preparation for later block creation.
|
2016-01-08 22:04:21 +01:00
|
|
|
pub struct OpenBlock<'engine> {
|
2016-01-08 19:12:19 +01:00
|
|
|
block: Block,
|
2016-01-08 22:04:21 +01:00
|
|
|
engine: &'engine Engine,
|
2016-01-08 19:12:19 +01:00
|
|
|
last_hashes: LastHashes,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Just like OpenBlock, except that we've applied `Engine::on_close_block`, finished up the non-seal header fields,
|
|
|
|
/// and collected the uncles.
|
|
|
|
///
|
|
|
|
/// There is no function available to push a transaction. If you want that you'll need to `reopen()` it.
|
2016-01-08 22:04:21 +01:00
|
|
|
pub struct ClosedBlock<'engine> {
|
|
|
|
open_block: OpenBlock<'engine>,
|
|
|
|
_uncles: Vec<Header>,
|
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 {
|
|
|
|
block: Block,
|
2016-01-08 22:04:21 +01:00
|
|
|
_bytes: Bytes,
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
|
|
|
|
2016-01-08 22:04:21 +01:00
|
|
|
impl<'engine> OpenBlock<'engine> {
|
|
|
|
/// Create a new OpenBlock ready for transaction pushing.
|
|
|
|
pub fn new<'a>(engine: &'a Engine, db: OverlayDB, parent: &Header, last_hashes: LastHashes) -> OpenBlock<'a> {
|
2016-01-08 19:12:19 +01:00
|
|
|
let mut r = OpenBlock {
|
2016-01-08 22:04:21 +01:00
|
|
|
block: Block::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-01-08 22:04:21 +01:00
|
|
|
engine.populate_from_parent(&mut r.block.header, parent);
|
|
|
|
engine.on_new_block(&mut r.block);
|
2016-01-08 19:12:19 +01:00
|
|
|
r
|
|
|
|
}
|
|
|
|
|
2016-01-08 22:04:21 +01:00
|
|
|
/// Get the environment info concerning this block.
|
|
|
|
pub fn env_info(&self) -> EnvInfo {
|
|
|
|
// TODO: memoise.
|
|
|
|
EnvInfo {
|
|
|
|
number: self.block.header.number.clone(),
|
|
|
|
author: self.block.header.author.clone(),
|
|
|
|
timestamp: self.block.header.timestamp.clone(),
|
|
|
|
difficulty: self.block.header.difficulty.clone(),
|
2016-01-08 19:12:19 +01:00
|
|
|
last_hashes: self.last_hashes.clone(),
|
2016-01-08 22:04:21 +01:00
|
|
|
gas_used: if let Some(ref t) = self.block.archive.last() {t.receipt.gas_used} else {U256::from(0)},
|
|
|
|
gas_limit: self.block.header.gas_limit.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Push a transaction into the block. It will be executed, and archived together with the receipt.
|
|
|
|
pub fn push_transaction(&mut self, t: Transaction, h: Option<H256>) -> Result<&Receipt, EthcoreError> {
|
|
|
|
let env_info = self.env_info();
|
|
|
|
match self.block.state.apply(&env_info, self.engine, &t, true) {
|
2016-01-08 19:12:19 +01:00
|
|
|
Ok(x) => {
|
2016-01-08 22:04:21 +01:00
|
|
|
self.block.archive_set.insert(h.unwrap_or_else(||t.sha3()));
|
|
|
|
self.block.archive.push(Entry { transaction: t, receipt: x.receipt });
|
|
|
|
Ok(&self.block.archive.last().unwrap().receipt)
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
|
|
|
Err(x) => Err(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Turn this into a `ClosedBlock`. A BlockChain must be provided in order to figure ou the uncles.
|
2016-01-09 18:20:31 +01:00
|
|
|
pub fn close(self, _uncles: Vec<Header>) -> ClosedBlock<'engine> { unimplemented!(); }
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
|
|
|
|
2016-01-08 22:04:21 +01:00
|
|
|
impl<'engine> IsBlock for OpenBlock<'engine> {
|
|
|
|
fn block(&self) -> &Block { &self.block }
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
|
|
|
|
2016-01-08 22:04:21 +01:00
|
|
|
impl<'engine> ClosedBlock<'engine> {
|
2016-01-08 19:12:19 +01:00
|
|
|
/// Get the hash of the header without seal arguments.
|
|
|
|
pub fn preseal_hash(&self) -> H256 { unimplemented!(); }
|
|
|
|
|
|
|
|
/// Turn this into a `ClosedBlock`. A BlockChain must be provided in order to figure ou the uncles.
|
2016-01-08 22:04:21 +01:00
|
|
|
pub fn seal(self, _seal_fields: Vec<Bytes>) -> SealedBlock { unimplemented!(); }
|
2016-01-08 19:12:19 +01:00
|
|
|
|
|
|
|
/// Turn this back into an `OpenBlock`.
|
2016-01-08 22:04:21 +01:00
|
|
|
pub fn reopen(self) -> OpenBlock<'engine> { unimplemented!(); }
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
|
|
|
|
2016-01-08 22:04:21 +01:00
|
|
|
impl<'engine> IsBlock for ClosedBlock<'engine> {
|
|
|
|
fn block(&self) -> &Block { &self.open_block.block }
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SealedBlock {
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IsBlock for SealedBlock {
|
2016-01-08 22:04:21 +01:00
|
|
|
fn block(&self) -> &Block { &self.block }
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
2016-01-09 18:20:31 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn open_block() {
|
|
|
|
use super::*;
|
|
|
|
use spec::*;
|
|
|
|
let engine = Spec::new_test().to_engine().unwrap();
|
|
|
|
let genesis_header = engine.spec().genesis_header();
|
|
|
|
let mut db = OverlayDB::new_temp();
|
|
|
|
engine.spec().ensure_db_good(&mut db);
|
|
|
|
let b = OpenBlock::new(engine.deref(), db, &genesis_header, vec![genesis_header.hash()]);
|
2016-01-09 19:10:05 +01:00
|
|
|
}
|