Compiling, fleshed-out Block.
This commit is contained in:
parent
7bad30a1bf
commit
76ba2df141
89
src/block.rs
89
src/block.rs
@ -1,7 +1,12 @@
|
||||
use std::collections::hash_set::*;
|
||||
use util::hash::*;
|
||||
use util::bytes::*;
|
||||
use util::uint::*;
|
||||
use util::error::*;
|
||||
use util::overlaydb::*;
|
||||
use transaction::*;
|
||||
use receipt::*;
|
||||
use blockchain::*;
|
||||
use engine::*;
|
||||
use header::*;
|
||||
use env_info::*;
|
||||
@ -25,7 +30,7 @@ pub struct Block {
|
||||
}
|
||||
|
||||
impl Block {
|
||||
fn new(header: Header, state: State) -> Block {
|
||||
fn new(state: State) -> Block {
|
||||
Block {
|
||||
header: Header::new(),
|
||||
state: state,
|
||||
@ -55,14 +60,14 @@ pub trait IsBlock {
|
||||
impl IsBlock for Block {
|
||||
fn block(&self) -> &Block { self }
|
||||
}
|
||||
/*
|
||||
|
||||
/// 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 {
|
||||
pub struct OpenBlock<'engine> {
|
||||
block: Block,
|
||||
engine: &Engine,
|
||||
engine: &'engine Engine,
|
||||
last_hashes: LastHashes,
|
||||
}
|
||||
|
||||
@ -70,9 +75,9 @@ pub struct OpenBlock {
|
||||
/// 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 {
|
||||
open_block: OpenBlock,
|
||||
uncles: Vec<Header>,
|
||||
pub struct ClosedBlock<'engine> {
|
||||
open_block: OpenBlock<'engine>,
|
||||
_uncles: Vec<Header>,
|
||||
}
|
||||
|
||||
/// A block that has a valid seal.
|
||||
@ -80,72 +85,76 @@ pub struct ClosedBlock {
|
||||
/// The block's header has valid seal arguments. The block cannot be reversed into a ClosedBlock or OpenBlock.
|
||||
pub struct SealedBlock {
|
||||
block: Block,
|
||||
bytes: Bytes,
|
||||
_bytes: Bytes,
|
||||
}
|
||||
|
||||
impl OpenBlock {
|
||||
pub fn new(engine: &Engine, mut db: OverlayDB, parent: &Header, last_hashes: LastHashes) -> OpenBlock {
|
||||
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> {
|
||||
let mut r = OpenBlock {
|
||||
block: Block::new(State::new_existing(db, parent.state_root.clone(), engine.account_start_nonce())),
|
||||
block: Block::new(State::from_existing(db, parent.state_root.clone(), engine.account_start_nonce())),
|
||||
engine: engine,
|
||||
last_hashes: last_hashes,
|
||||
}
|
||||
};
|
||||
|
||||
engine.populate_from_parent(r.block.header, parent);
|
||||
engine.on_init_block(&mut r);
|
||||
engine.populate_from_parent(&mut r.block.header, parent);
|
||||
engine.on_new_block(&mut r.block);
|
||||
r
|
||||
}
|
||||
|
||||
/// Turn this into a `ClosedBlock`. A BlockChain must be provided in order to figure ou the uncles.
|
||||
pub fn push_transaction(&mut self, t: Transaction, mut h: Option<H256>) -> Result<&Receipt, EthcoreError> {
|
||||
let env_info = EnvInfo{
|
||||
number: self.header.number,
|
||||
author: self.header.author,
|
||||
timestamp: self.header.timestamp,
|
||||
difficulty: self.header.difficulty,
|
||||
/// 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(),
|
||||
last_hashes: self.last_hashes.clone(),
|
||||
gas_used: if let Some(ref t) = self.archive.last() {t.receipt.gas_used} else {U256::from(0)},
|
||||
};
|
||||
match self.state.apply(env_info, self.engine, t, true) {
|
||||
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) {
|
||||
Ok(x) => {
|
||||
self.transactionHashes.insert(h.unwrap_or_else(||t.sha3()));
|
||||
self.transactions.push(BlockTransaction{t, x.receipt});
|
||||
Ok(&self.transactions.last().unwrap().receipt)
|
||||
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)
|
||||
}
|
||||
Err(x) => Err(x)
|
||||
}
|
||||
}
|
||||
|
||||
/// Turn this into a `ClosedBlock`. A BlockChain must be provided in order to figure ou the uncles.
|
||||
pub fn close(self, bc: &BlockChain) -> ClosedBlock { unimplemented!(); }
|
||||
pub fn close(self, _bc: &BlockChain) -> ClosedBlock { unimplemented!(); }
|
||||
}
|
||||
|
||||
impl IsBlock for OpenBlock {
|
||||
fn block(&self) -> &Block { self.block }
|
||||
fn block_mut(&self) -> &mut Block { self.block }
|
||||
impl<'engine> IsBlock for OpenBlock<'engine> {
|
||||
fn block(&self) -> &Block { &self.block }
|
||||
}
|
||||
|
||||
impl ClosedBlock {
|
||||
impl<'engine> ClosedBlock<'engine> {
|
||||
/// 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.
|
||||
pub fn seal(self, seal_fields: Vec<Bytes>) -> SealedBlock { unimplemented!(); }
|
||||
pub fn seal(self, _seal_fields: Vec<Bytes>) -> SealedBlock { unimplemented!(); }
|
||||
|
||||
/// Turn this back into an `OpenBlock`.
|
||||
pub fn reopen(self) -> OpenBlock { unimplemented!(); }
|
||||
pub fn reopen(self) -> OpenBlock<'engine> { unimplemented!(); }
|
||||
}
|
||||
|
||||
impl IsBlock for ClosedBlock {
|
||||
fn block(&self) -> &Block { self.open_block.block }
|
||||
fn block_mut(&self) -> &mut Block { self.open_block.block }
|
||||
impl<'engine> IsBlock for ClosedBlock<'engine> {
|
||||
fn block(&self) -> &Block { &self.open_block.block }
|
||||
}
|
||||
|
||||
impl SealedBlock {
|
||||
}
|
||||
|
||||
impl IsBlock for SealedBlock {
|
||||
fn block(&self) -> &Block { self.block }
|
||||
fn block_mut(&self) -> &mut Block { self.block.block }
|
||||
fn block(&self) -> &Block { &self.block }
|
||||
}
|
||||
*/
|
@ -36,11 +36,11 @@ pub trait Engine {
|
||||
|
||||
/// Some intrinsic operation parameters; by default they take their value from the `spec()`'s `engine_params`.
|
||||
fn maximum_extra_data_size(&self, _env_info: &EnvInfo) -> usize { decode(&self.spec().engine_params.get("maximum_extra_data_size").unwrap()) }
|
||||
fn account_start_nonce(&self, _env_info: &EnvInfo) -> U256 { decode(&self.spec().engine_params.get("account_start_nonce").unwrap()) }
|
||||
fn account_start_nonce(&self) -> U256 { decode(&self.spec().engine_params.get("account_start_nonce").unwrap()) }
|
||||
|
||||
/// Block transformation functions, before and after the transactions.
|
||||
fn on_new_block(&self, _block: &mut Block) -> Result<(), EthcoreError> { Ok(()) }
|
||||
fn on_close_block(&self, _block: &mut Block) -> Result<(), EthcoreError> { Ok(()) }
|
||||
fn on_new_block(&self, _block: &mut Block) {}
|
||||
fn on_close_block(&self, _block: &mut Block) {}
|
||||
|
||||
/// Verify that `header` is valid.
|
||||
/// `parent` (the parent header) and `block` (the header's full block) may be provided for additional
|
||||
@ -55,7 +55,7 @@ pub trait Engine {
|
||||
|
||||
/// Don't forget to call Super::populateFromParent when subclassing & overriding.
|
||||
// TODO: consider including State in the params.
|
||||
fn populate_from_parent(&self, _header: &mut Header, _parent: &Header) -> Result<(), EthcoreError> { Ok(()) }
|
||||
fn populate_from_parent(&self, _header: &mut Header, _parent: &Header) {}
|
||||
|
||||
// TODO: builtin contract routing - to do this properly, it will require removing the built-in configuration-reading logic
|
||||
// from Spec into here and removing the Spec::builtins field.
|
||||
|
@ -1,4 +1,4 @@
|
||||
use util::error::*;
|
||||
//use util::error::*;
|
||||
use util::rlp::decode;
|
||||
use engine::Engine;
|
||||
use spec::Spec;
|
||||
@ -24,9 +24,10 @@ impl Engine for Ethash {
|
||||
fn evm_schedule(&self, _env_info: &EnvInfo) -> EvmSchedule { EvmSchedule::new_frontier() }
|
||||
|
||||
/// Apply the block reward on finalisation of the block.
|
||||
fn on_close_block(&self, block: &mut Block) -> Result<(), EthcoreError> {
|
||||
fn on_close_block(&self, block: &mut Block) {
|
||||
let a = block.header().author.clone();
|
||||
block.state_mut().add_balance(&a, &decode(&self.spec().engine_params.get("block_reward").unwrap()));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: test for on_close_block.
|
||||
|
@ -1,7 +1,9 @@
|
||||
use util::hash::*;
|
||||
use util::uint::*;
|
||||
|
||||
/// Information describing execution of a transaction.
|
||||
pub struct Receipt {
|
||||
// TODO
|
||||
pub state_root: H256,
|
||||
pub gas_used: U256,
|
||||
}
|
||||
|
14
src/state.rs
14
src/state.rs
@ -17,7 +17,7 @@ use engine::Engine;
|
||||
|
||||
/// Information concerning the result of the `State::apply` operation.
|
||||
pub struct ApplyInfo {
|
||||
r: Receipt,
|
||||
pub receipt: Receipt,
|
||||
}
|
||||
|
||||
pub type ApplyResult = Result<ApplyInfo, EthcoreError>;
|
||||
@ -49,7 +49,7 @@ impl State {
|
||||
}
|
||||
|
||||
/// Creates new state with existing state root
|
||||
pub fn new_existing(mut db: OverlayDB, mut root: H256, account_start_nonce: U256) -> State {
|
||||
pub fn from_existing(mut db: OverlayDB, mut root: H256, account_start_nonce: U256) -> State {
|
||||
{
|
||||
// trie should panic! if root does not exist
|
||||
let _ = TrieDB::new(&mut db, &mut root);
|
||||
@ -136,7 +136,7 @@ impl State {
|
||||
|
||||
/// Execute a given transaction.
|
||||
/// This will change the state accordingly.
|
||||
pub fn apply(_env_info: &EnvInfo, _engine: &Engine, _t: &Transaction, _is_permanent: bool) -> ApplyResult {
|
||||
pub fn apply(&mut self, _env_info: &EnvInfo, _engine: &Engine, _t: &Transaction, _is_permanent: bool) -> ApplyResult {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
@ -161,7 +161,7 @@ impl State {
|
||||
}
|
||||
|
||||
{
|
||||
let mut trie = TrieDBMut::new_existing(db, &mut root);
|
||||
let mut trie = TrieDBMut::from_existing(db, &mut root);
|
||||
for (address, ref a) in accounts.iter() {
|
||||
match a {
|
||||
&&Some(ref account) => trie.insert(address, &account.rlp()),
|
||||
@ -245,7 +245,7 @@ fn code_from_database() {
|
||||
s.drop()
|
||||
};
|
||||
|
||||
let s = State::new_existing(db, r, U256::from(0u8));
|
||||
let s = State::from_existing(db, r, U256::from(0u8));
|
||||
assert_eq!(s.code(&a), Some([1u8, 2, 3].to_vec()));
|
||||
}
|
||||
|
||||
@ -259,7 +259,7 @@ fn storage_at_from_database() {
|
||||
s.drop()
|
||||
};
|
||||
|
||||
let s = State::new_existing(db, r, U256::from(0u8));
|
||||
let s = State::from_existing(db, r, U256::from(0u8));
|
||||
assert_eq!(s.storage_at(&a, &H256::from(&U256::from(01u64))), H256::from(&U256::from(69u64)));
|
||||
}
|
||||
|
||||
@ -275,7 +275,7 @@ fn get_from_database() {
|
||||
s.drop()
|
||||
};
|
||||
|
||||
let s = State::new_existing(db, r, U256::from(0u8));
|
||||
let s = State::from_existing(db, r, U256::from(0u8));
|
||||
assert_eq!(s.balance(&a), U256::from(69u64));
|
||||
assert_eq!(s.nonce(&a), U256::from(1u64));
|
||||
}
|
||||
|
@ -3,10 +3,6 @@ use util::bytes::*;
|
||||
use util::uint::*;
|
||||
use util::rlp::*;
|
||||
|
||||
pub struct Receipt {
|
||||
gas_used: U256,
|
||||
}
|
||||
|
||||
/// A set of information describing an externally-originating message call
|
||||
/// or contract creation operation.
|
||||
pub struct Transaction {
|
||||
@ -23,10 +19,16 @@ impl Transaction {
|
||||
pub fn is_contract_creation(&self) -> bool {
|
||||
self.to.is_none()
|
||||
}
|
||||
|
||||
/// Is this transaction meant to send a message?
|
||||
pub fn is_message_call(&self) -> bool {
|
||||
!self.is_contract_creation()
|
||||
}
|
||||
|
||||
/// Get the hash of this transaction.
|
||||
pub fn sha3(&self) -> H256 {
|
||||
unimplemented!();
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodable for Transaction {
|
||||
|
Loading…
Reference in New Issue
Block a user