Better API allowing one fewer clone.

This commit is contained in:
Gav Wood 2016-01-10 17:11:46 +01:00
parent 2cc3ee66d7
commit fea418703b
2 changed files with 11 additions and 10 deletions

View File

@ -8,14 +8,6 @@ pub struct Entry {
receipt: Receipt, receipt: Receipt,
} }
/// A set of fields that are publicly alterable.
pub struct BlockRefMut<'a> {
pub header: &'a Header,
pub state: &'a mut State,
pub archive: &'a Vec<Entry>,
pub uncles: &'a Vec<Header>,
}
/// Internal type for a block's common elements. /// Internal type for a block's common elements.
pub struct Block { pub struct Block {
header: Header, header: Header,
@ -29,7 +21,16 @@ pub struct Block {
uncles: Vec<Header>, uncles: Vec<Header>,
} }
/// 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>,
}
impl Block { impl Block {
/// Create a new block from the given `state`.
fn new(state: State) -> Block { fn new(state: State) -> Block {
Block { Block {
header: Header::new(), header: Header::new(),
@ -40,6 +41,7 @@ impl Block {
} }
} }
/// Get a structure containing individual references to all public fields.
pub fn fields(&mut self) -> BlockRefMut { pub fn fields(&mut self) -> BlockRefMut {
BlockRefMut { BlockRefMut {
header: &self.header, header: &self.header,

View File

@ -24,8 +24,7 @@ impl Engine for Ethash {
fn on_close_block(&self, block: &mut Block) { fn on_close_block(&self, block: &mut Block) {
let reward = self.spec().engine_params.get("blockReward").map(|a| decode(&a)).unwrap_or(U256::from(0u64)); let reward = self.spec().engine_params.get("blockReward").map(|a| decode(&a)).unwrap_or(U256::from(0u64));
let fields = block.fields(); let fields = block.fields();
let author = &fields.header.author; fields.state.add_balance(&fields.header.author, &reward);
fields.state.add_balance(author, &reward);
/* /*
let uncle_authors = block.uncles.iter().map(|u| u.author().clone()).collect(); let uncle_authors = block.uncles.iter().map(|u| u.author().clone()).collect();
for a in uncle_authors { for a in uncle_authors {