openethereum/src/block.rs

300 lines
10 KiB
Rust
Raw Normal View History

use common::*;
2016-01-08 19:12:19 +01:00
use engine::*;
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>,
2016-01-10 14:05:39 +01:00
uncles: Vec<Header>,
2016-01-08 19:12:19 +01:00
}
2016-01-10 17:11:46 +01:00
/// A set of references to `Block` fields that are publicly accessible.
pub struct BlockRefMut<'a> {
pub header: &'a Header,
pub state: &'a mut State,
pub archive: &'a Vec<Entry>,
pub uncles: &'a Vec<Header>,
}
2016-01-08 19:12:19 +01:00
impl Block {
2016-01-10 17:11:46 +01:00
/// Create a new block from the given `state`.
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-10 14:05:39 +01:00
uncles: Vec::new(),
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.
pub fn fields(&mut self) -> BlockRefMut {
BlockRefMut {
header: &self.header,
state: &mut self.state,
archive: &self.archive,
uncles: &self.uncles,
}
}
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-10 14:05:39 +01:00
/// Get all uncles in this block.
fn uncles(&self) -> &Vec<Header> { &self.block().uncles }
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.
pub struct OpenBlock<'x, 'y> {
2016-01-08 19:12:19 +01:00
block: Block,
engine: &'x Engine,
last_hashes: &'y 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.
///
/// There is no function available to push a transaction. If you want that you'll need to `reopen()` it.
pub struct ClosedBlock<'x, 'y> {
open_block: OpenBlock<'x, 'y>,
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 {
block: Block,
2016-01-10 14:05:39 +01:00
uncle_bytes: Bytes,
2016-01-08 19:12:19 +01:00
}
impl<'x, 'y> OpenBlock<'x, 'y> {
2016-01-08 22:04:21 +01:00
/// Create a new OpenBlock ready for transaction pushing.
pub fn new<'a, 'b>(engine: &'a Engine, db: OverlayDB, parent: &Header, last_hashes: &'b LastHashes, author: Address, extra_data: Bytes) -> OpenBlock<'a, 'b> {
2016-01-08 19:12:19 +01:00
let mut r = OpenBlock {
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
r.block.header.set_number(parent.number() + 1);
2016-01-10 14:05:39 +01:00
r.block.header.set_author(author);
r.block.header.set_extra_data(extra_data);
r.block.header.set_timestamp_now();
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-10 14:05:39 +01:00
/// Alter the author for the block.
pub fn set_author(&mut self, author: Address) { self.block.header.set_author(author); }
/// Alter the timestamp of the block.
pub fn set_timestamp(&mut self, timestamp: u64) { self.block.header.set_timestamp(timestamp); }
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() {
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 {
self.block.header.set_extra_data(extra_data);
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> {
if self.block.uncles.len() >= self.engine.maximum_uncle_count() {
return Err(BlockError::TooManyUncles(OutOfBounds{min: None, max: Some(self.engine.maximum_uncle_count()), found: self.block.uncles.len()}));
2016-01-10 14:05:39 +01:00
}
// TODO: check number
// TODO: check not a direct ancestor (use last_hashes for that)
self.block.uncles.push(valid_uncle_header);
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 {
number: self.block.header.number,
2016-01-08 22:04:21 +01:00
author: self.block.header.author.clone(),
timestamp: self.block.header.timestamp,
2016-01-08 22:04:21 +01:00
difficulty: self.block.header.difficulty.clone(),
2016-01-08 19:12:19 +01:00
last_hashes: self.last_hashes.clone(),
gas_used: self.block.archive.last().map(|t| t.receipt.gas_used).unwrap_or(U256::from(0)),
2016-01-08 22:04:21 +01:00
gas_limit: self.block.header.gas_limit.clone(),
}
}
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.
pub fn push_transaction(&mut self, t: Transaction, h: Option<H256>) -> Result<&Receipt, Error> {
2016-01-08 22:04:21 +01:00
let env_info = self.env_info();
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) => {
self.block.archive_set.insert(h.unwrap_or_else(||t.hash()));
2016-01-11 17:37:22 +01:00
self.block.archive.push(Entry { transaction: t, receipt: receipt });
2016-01-08 22:04:21 +01:00
Ok(&self.block.archive.last().unwrap().receipt)
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.
pub fn close(self) -> ClosedBlock<'x, 'y> {
2016-01-09 22:45:27 +01:00
let mut s = self;
s.engine.on_close_block(&mut s.block);
s.block.header.transactions_root = ordered_trie_root(s.block.archive.iter().map(|ref e| e.transaction.rlp_bytes()).collect());
2016-01-10 14:05:39 +01:00
let uncle_bytes = s.block.uncles.iter().fold(RlpStream::new_list(s.block.uncles.len()), |mut s, u| {s.append(&u.rlp(Seal::With)); s} ).out();
2016-01-09 22:45:27 +01:00
s.block.header.uncles_hash = uncle_bytes.sha3();
s.block.header.state_root = s.block.state.root().clone();
s.block.header.receipts_root = ordered_trie_root(s.block.archive.iter().map(|ref e| e.receipt.rlp_bytes()).collect());
2016-01-09 22:45:27 +01:00
s.block.header.log_bloom = s.block.archive.iter().fold(LogBloom::zero(), |mut b, e| {b |= &e.receipt.log_bloom; b});
s.block.header.gas_used = s.block.archive.last().map(|t| t.receipt.gas_used).unwrap_or(U256::from(0));
s.block.header.note_dirty();
ClosedBlock::new(s, uncle_bytes)
}
2016-01-08 19:12:19 +01:00
}
impl<'x, 'y> IsBlock for OpenBlock<'x, 'y> {
2016-01-08 22:04:21 +01:00
fn block(&self) -> &Block { &self.block }
2016-01-08 19:12:19 +01:00
}
impl<'x, 'y> IsBlock for ClosedBlock<'x, 'y> {
2016-01-10 14:05:39 +01:00
fn block(&self) -> &Block { &self.open_block.block }
}
impl<'x, 'y> ClosedBlock<'x, 'y> {
fn new<'a, 'b>(open_block: OpenBlock<'a, 'b>, uncle_bytes: Bytes) -> ClosedBlock<'a, 'b> {
ClosedBlock {
open_block: open_block,
2016-01-10 14:05:39 +01:00
uncle_bytes: uncle_bytes,
}
}
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.
pub fn seal(self, seal: Vec<Bytes>) -> Result<SealedBlock, BlockError> {
let mut s = self;
if seal.len() != s.open_block.engine.seal_fields() {
return Err(BlockError::InvalidSealArity(Mismatch{expected: s.open_block.engine.seal_fields(), found: seal.len()}));
}
s.open_block.block.header.set_seal(seal);
Ok(SealedBlock { block: s.open_block.block, uncle_bytes: s.uncle_bytes })
}
2016-01-08 19:12:19 +01:00
/// Turn this back into an `OpenBlock`.
pub fn reopen(self) -> OpenBlock<'x, 'y> { self.open_block }
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);
self.block.header.stream_rlp(&mut block_rlp, Seal::With);
block_rlp.append_list(self.block.archive.len());
for e in self.block.archive.iter() { e.transaction.rlp_append(&mut block_rlp); }
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.
pub fn drain(self) -> OverlayDB { self.block.state.drop().1 }
2016-01-08 19:12:19 +01:00
}
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
}
/// Enact the block given by `block_bytes` using `engine` on the database `db` with given `parent` block header
///
pub fn enact(block_bytes: &[u8], engine: &Engine, db: OverlayDB, parent: &Header, last_hashes: &LastHashes) -> Result<SealedBlock, Error> {
let block = BlockView::new(block_bytes);
let header = block.header_view();
let mut b = OpenBlock::new(engine, db, parent, last_hashes, header.author(), header.extra_data());
2016-01-10 22:57:31 +01:00
b.set_timestamp(header.timestamp());
for t in block.transactions().into_iter() { try!(b.push_transaction(t, None)); }
for u in block.uncles().into_iter() { try!(b.push_uncle(u)); }
Ok(try!(b.close().seal(header.seal())))
}
#[test]
fn open_block() {
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 last_hashes = vec![genesis_header.hash()];
let b = OpenBlock::new(engine.deref(), db, &genesis_header, &last_hashes, Address::zero(), vec![]);
2016-01-10 14:05:39 +01:00
let b = b.close();
let _ = b.seal(vec![]);
}
2016-01-10 23:10:06 +01:00
#[test]
fn enact_block() {
use spec::*;
let engine = Spec::new_test().to_engine().unwrap();
let genesis_header = engine.spec().genesis_header();
2016-01-10 23:10:06 +01:00
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()], Address::zero(), vec![]).close().seal(vec![]).unwrap();
let orig_bytes = b.rlp_bytes();
let orig_db = b.drain();
let mut db = OverlayDB::new_temp();
engine.spec().ensure_db_good(&mut db);
2016-01-10 23:10:06 +01:00
let e = enact(&orig_bytes, engine.deref(), db, &genesis_header, &vec![genesis_header.hash()]).unwrap();
2016-01-10 23:10:06 +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);
}