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 std::collections::hash_set::*;
|
||||||
use util::hash::*;
|
use util::hash::*;
|
||||||
|
use util::bytes::*;
|
||||||
|
use util::uint::*;
|
||||||
use util::error::*;
|
use util::error::*;
|
||||||
|
use util::overlaydb::*;
|
||||||
use transaction::*;
|
use transaction::*;
|
||||||
|
use receipt::*;
|
||||||
|
use blockchain::*;
|
||||||
use engine::*;
|
use engine::*;
|
||||||
use header::*;
|
use header::*;
|
||||||
use env_info::*;
|
use env_info::*;
|
||||||
@ -25,7 +30,7 @@ pub struct Block {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Block {
|
impl Block {
|
||||||
fn new(header: Header, state: State) -> Block {
|
fn new(state: State) -> Block {
|
||||||
Block {
|
Block {
|
||||||
header: Header::new(),
|
header: Header::new(),
|
||||||
state: state,
|
state: state,
|
||||||
@ -55,14 +60,14 @@ pub trait IsBlock {
|
|||||||
impl IsBlock for Block {
|
impl IsBlock for Block {
|
||||||
fn block(&self) -> &Block { self }
|
fn block(&self) -> &Block { self }
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
/// Block that is ready for transactions to be added.
|
/// 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
|
/// 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.
|
/// maintain the system `state()`. We also archive execution receipts in preparation for later block creation.
|
||||||
pub struct OpenBlock {
|
pub struct OpenBlock<'engine> {
|
||||||
block: Block,
|
block: Block,
|
||||||
engine: &Engine,
|
engine: &'engine Engine,
|
||||||
last_hashes: LastHashes,
|
last_hashes: LastHashes,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,9 +75,9 @@ pub struct OpenBlock {
|
|||||||
/// and collected the uncles.
|
/// and collected the uncles.
|
||||||
///
|
///
|
||||||
/// There is no function available to push a transaction. If you want that you'll need to `reopen()` it.
|
/// There is no function available to push a transaction. If you want that you'll need to `reopen()` it.
|
||||||
pub struct ClosedBlock {
|
pub struct ClosedBlock<'engine> {
|
||||||
open_block: OpenBlock,
|
open_block: OpenBlock<'engine>,
|
||||||
uncles: Vec<Header>,
|
_uncles: Vec<Header>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A block that has a valid seal.
|
/// 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.
|
/// The block's header has valid seal arguments. The block cannot be reversed into a ClosedBlock or OpenBlock.
|
||||||
pub struct SealedBlock {
|
pub struct SealedBlock {
|
||||||
block: Block,
|
block: Block,
|
||||||
bytes: Bytes,
|
_bytes: Bytes,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl OpenBlock {
|
impl<'engine> OpenBlock<'engine> {
|
||||||
pub fn new(engine: &Engine, mut db: OverlayDB, parent: &Header, last_hashes: LastHashes) -> OpenBlock {
|
/// 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 {
|
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,
|
engine: engine,
|
||||||
last_hashes: last_hashes,
|
last_hashes: last_hashes,
|
||||||
}
|
};
|
||||||
|
|
||||||
engine.populate_from_parent(r.block.header, parent);
|
engine.populate_from_parent(&mut r.block.header, parent);
|
||||||
engine.on_init_block(&mut r);
|
engine.on_new_block(&mut r.block);
|
||||||
r
|
r
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Turn this into a `ClosedBlock`. A BlockChain must be provided in order to figure ou the uncles.
|
/// Get the environment info concerning this block.
|
||||||
pub fn push_transaction(&mut self, t: Transaction, mut h: Option<H256>) -> Result<&Receipt, EthcoreError> {
|
pub fn env_info(&self) -> EnvInfo {
|
||||||
let env_info = EnvInfo{
|
// TODO: memoise.
|
||||||
number: self.header.number,
|
EnvInfo {
|
||||||
author: self.header.author,
|
number: self.block.header.number.clone(),
|
||||||
timestamp: self.header.timestamp,
|
author: self.block.header.author.clone(),
|
||||||
difficulty: self.header.difficulty,
|
timestamp: self.block.header.timestamp.clone(),
|
||||||
|
difficulty: self.block.header.difficulty.clone(),
|
||||||
last_hashes: self.last_hashes.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)},
|
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(),
|
||||||
match self.state.apply(env_info, self.engine, t, true) {
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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) => {
|
Ok(x) => {
|
||||||
self.transactionHashes.insert(h.unwrap_or_else(||t.sha3()));
|
self.block.archive_set.insert(h.unwrap_or_else(||t.sha3()));
|
||||||
self.transactions.push(BlockTransaction{t, x.receipt});
|
self.block.archive.push(Entry { transaction: t, receipt: x.receipt });
|
||||||
Ok(&self.transactions.last().unwrap().receipt)
|
Ok(&self.block.archive.last().unwrap().receipt)
|
||||||
}
|
}
|
||||||
Err(x) => Err(x)
|
Err(x) => Err(x)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Turn this into a `ClosedBlock`. A BlockChain must be provided in order to figure ou the uncles.
|
/// 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 {
|
impl<'engine> IsBlock for OpenBlock<'engine> {
|
||||||
fn block(&self) -> &Block { self.block }
|
fn block(&self) -> &Block { &self.block }
|
||||||
fn block_mut(&self) -> &mut Block { self.block }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ClosedBlock {
|
impl<'engine> ClosedBlock<'engine> {
|
||||||
/// Get the hash of the header without seal arguments.
|
/// Get the hash of the header without seal arguments.
|
||||||
pub fn preseal_hash(&self) -> H256 { unimplemented!(); }
|
pub fn preseal_hash(&self) -> H256 { unimplemented!(); }
|
||||||
|
|
||||||
/// Turn this into a `ClosedBlock`. A BlockChain must be provided in order to figure ou the uncles.
|
/// 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`.
|
/// Turn this back into an `OpenBlock`.
|
||||||
pub fn reopen(self) -> OpenBlock { unimplemented!(); }
|
pub fn reopen(self) -> OpenBlock<'engine> { unimplemented!(); }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IsBlock for ClosedBlock {
|
impl<'engine> IsBlock for ClosedBlock<'engine> {
|
||||||
fn block(&self) -> &Block { self.open_block.block }
|
fn block(&self) -> &Block { &self.open_block.block }
|
||||||
fn block_mut(&self) -> &mut Block { self.open_block.block }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SealedBlock {
|
impl SealedBlock {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IsBlock for SealedBlock {
|
impl IsBlock for SealedBlock {
|
||||||
fn block(&self) -> &Block { self.block }
|
fn block(&self) -> &Block { &self.block }
|
||||||
fn block_mut(&self) -> &mut Block { self.block.block }
|
|
||||||
}
|
}
|
||||||
*/
|
|
@ -36,11 +36,11 @@ pub trait Engine {
|
|||||||
|
|
||||||
/// Some intrinsic operation parameters; by default they take their value from the `spec()`'s `engine_params`.
|
/// 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 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.
|
/// Block transformation functions, before and after the transactions.
|
||||||
fn on_new_block(&self, _block: &mut Block) -> Result<(), EthcoreError> { Ok(()) }
|
fn on_new_block(&self, _block: &mut Block) {}
|
||||||
fn on_close_block(&self, _block: &mut Block) -> Result<(), EthcoreError> { Ok(()) }
|
fn on_close_block(&self, _block: &mut Block) {}
|
||||||
|
|
||||||
/// Verify that `header` is valid.
|
/// Verify that `header` is valid.
|
||||||
/// `parent` (the parent header) and `block` (the header's full block) may be provided for additional
|
/// `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.
|
/// Don't forget to call Super::populateFromParent when subclassing & overriding.
|
||||||
// TODO: consider including State in the params.
|
// 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
|
// 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.
|
// 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 util::rlp::decode;
|
||||||
use engine::Engine;
|
use engine::Engine;
|
||||||
use spec::Spec;
|
use spec::Spec;
|
||||||
@ -24,9 +24,10 @@ impl Engine for Ethash {
|
|||||||
fn evm_schedule(&self, _env_info: &EnvInfo) -> EvmSchedule { EvmSchedule::new_frontier() }
|
fn evm_schedule(&self, _env_info: &EnvInfo) -> EvmSchedule { EvmSchedule::new_frontier() }
|
||||||
|
|
||||||
/// Apply the block reward on finalisation of the block.
|
/// 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();
|
let a = block.header().author.clone();
|
||||||
block.state_mut().add_balance(&a, &decode(&self.spec().engine_params.get("block_reward").unwrap()));
|
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::hash::*;
|
||||||
|
use util::uint::*;
|
||||||
|
|
||||||
/// Information describing execution of a transaction.
|
/// Information describing execution of a transaction.
|
||||||
pub struct Receipt {
|
pub struct Receipt {
|
||||||
// TODO
|
// TODO
|
||||||
pub state_root: H256,
|
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.
|
/// Information concerning the result of the `State::apply` operation.
|
||||||
pub struct ApplyInfo {
|
pub struct ApplyInfo {
|
||||||
r: Receipt,
|
pub receipt: Receipt,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type ApplyResult = Result<ApplyInfo, EthcoreError>;
|
pub type ApplyResult = Result<ApplyInfo, EthcoreError>;
|
||||||
@ -49,7 +49,7 @@ impl State {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Creates new state with existing state root
|
/// 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
|
// trie should panic! if root does not exist
|
||||||
let _ = TrieDB::new(&mut db, &mut root);
|
let _ = TrieDB::new(&mut db, &mut root);
|
||||||
@ -136,7 +136,7 @@ impl State {
|
|||||||
|
|
||||||
/// Execute a given transaction.
|
/// Execute a given transaction.
|
||||||
/// This will change the state accordingly.
|
/// 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!();
|
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() {
|
for (address, ref a) in accounts.iter() {
|
||||||
match a {
|
match a {
|
||||||
&&Some(ref account) => trie.insert(address, &account.rlp()),
|
&&Some(ref account) => trie.insert(address, &account.rlp()),
|
||||||
@ -245,7 +245,7 @@ fn code_from_database() {
|
|||||||
s.drop()
|
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()));
|
assert_eq!(s.code(&a), Some([1u8, 2, 3].to_vec()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -259,7 +259,7 @@ fn storage_at_from_database() {
|
|||||||
s.drop()
|
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)));
|
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()
|
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.balance(&a), U256::from(69u64));
|
||||||
assert_eq!(s.nonce(&a), U256::from(1u64));
|
assert_eq!(s.nonce(&a), U256::from(1u64));
|
||||||
}
|
}
|
||||||
|
@ -3,10 +3,6 @@ use util::bytes::*;
|
|||||||
use util::uint::*;
|
use util::uint::*;
|
||||||
use util::rlp::*;
|
use util::rlp::*;
|
||||||
|
|
||||||
pub struct Receipt {
|
|
||||||
gas_used: U256,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A set of information describing an externally-originating message call
|
/// A set of information describing an externally-originating message call
|
||||||
/// or contract creation operation.
|
/// or contract creation operation.
|
||||||
pub struct Transaction {
|
pub struct Transaction {
|
||||||
@ -23,10 +19,16 @@ impl Transaction {
|
|||||||
pub fn is_contract_creation(&self) -> bool {
|
pub fn is_contract_creation(&self) -> bool {
|
||||||
self.to.is_none()
|
self.to.is_none()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Is this transaction meant to send a message?
|
/// Is this transaction meant to send a message?
|
||||||
pub fn is_message_call(&self) -> bool {
|
pub fn is_message_call(&self) -> bool {
|
||||||
!self.is_contract_creation()
|
!self.is_contract_creation()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get the hash of this transaction.
|
||||||
|
pub fn sha3(&self) -> H256 {
|
||||||
|
unimplemented!();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Encodable for Transaction {
|
impl Encodable for Transaction {
|
||||||
|
Loading…
Reference in New Issue
Block a user