Adding transactions to block

This commit is contained in:
Tomasz Drwięga 2016-03-09 12:54:07 +01:00
parent 49f1834ffb
commit 5db84c3233
8 changed files with 52 additions and 12 deletions

View File

@ -274,7 +274,7 @@ impl<'x> OpenBlock<'x> {
s.block.base.header.note_dirty(); s.block.base.header.note_dirty();
ClosedBlock { ClosedBlock {
block: s.block, block: s.block,
uncle_bytes: uncle_bytes, uncle_bytes: uncle_bytes,
} }
} }

View File

@ -32,7 +32,7 @@ use service::{NetSyncMessage, SyncMessage};
use env_info::LastHashes; use env_info::LastHashes;
use verification::*; use verification::*;
use block::*; use block::*;
use transaction::LocalizedTransaction; use transaction::{LocalizedTransaction, SignedTransaction};
use extras::TransactionAddress; use extras::TransactionAddress;
use filter::Filter; use filter::Filter;
use log_entry::LocalizedLogEntry; use log_entry::LocalizedLogEntry;
@ -185,7 +185,10 @@ pub trait BlockChainClient : Sync + Send {
/// Returns logs matching given filter. /// Returns logs matching given filter.
fn logs(&self, filter: Filter) -> Vec<LocalizedLogEntry>; fn logs(&self, filter: Filter) -> Vec<LocalizedLogEntry>;
fn prepare_sealing(&self, author: Address, extra_data: Bytes) -> Option<ClosedBlock>; /// Returns ClosedBlock prepared for sealing.
fn prepare_sealing(&self, author: Address, extra_data: Bytes, transactions: Vec<SignedTransaction>) -> Option<ClosedBlock>;
/// Attempts to seal given block. Returns `SealedBlock` on success and the same block in case of error.
fn try_seal(&self, block: ClosedBlock, seal: Vec<Bytes>) -> Result<SealedBlock, ClosedBlock>; fn try_seal(&self, block: ClosedBlock, seal: Vec<Bytes>) -> Result<SealedBlock, ClosedBlock>;
} }
@ -417,6 +420,12 @@ impl<V> Client<V> where V: Verifier {
} }
} }
{
if self.chain_info().best_block_hash != original_best {
io.send(NetworkIoMessage::User(SyncMessage::NewChainHead)).unwrap();
}
}
imported imported
} }
@ -477,7 +486,7 @@ impl<V> BlockChainClient for Client<V> where V: Verifier {
block.try_seal(self.engine.deref().deref(), seal) block.try_seal(self.engine.deref().deref(), seal)
} }
fn prepare_sealing(&self, author: Address, extra_data: Bytes) -> Option<ClosedBlock> { fn prepare_sealing(&self, author: Address, extra_data: Bytes, transactions: Vec<SignedTransaction>) -> Option<ClosedBlock> {
let engine = self.engine.deref().deref(); let engine = self.engine.deref().deref();
let h = self.chain.read().unwrap().best_block_hash(); let h = self.chain.read().unwrap().best_block_hash();
@ -490,7 +499,9 @@ impl<V> BlockChainClient for Client<V> where V: Verifier {
extra_data, extra_data,
); );
self.chain.read().unwrap().find_uncle_headers(&h, engine.maximum_uncle_age()) // Add uncles
self.chain.read().unwrap()
.find_uncle_headers(&h, engine.maximum_uncle_age())
.unwrap() .unwrap()
.into_iter() .into_iter()
.take(engine.maximum_uncle_count()) .take(engine.maximum_uncle_count())
@ -498,10 +509,22 @@ impl<V> BlockChainClient for Client<V> where V: Verifier {
b.push_uncle(h).unwrap(); b.push_uncle(h).unwrap();
}); });
// TODO: push transactions. // Add transactions
let block_number = b.block().header().number();
for tx in transactions {
let import = b.push_transaction(tx, None);
if let Err(e) = import {
trace!("Error adding transaction to block: number={}. Error: {:?}", block_number, e);
}
}
// And close
let b = b.close(); let b = b.close();
trace!("Sealing: number={}, hash={}, diff={}", b.hash(), b.block().header().difficulty(), b.block().header().number()); trace!("Sealing: number={}, hash={}, diff={}",
b.block().header().number(),
b.hash(),
b.block().header().difficulty()
);
Some(b) Some(b)
} }

View File

@ -34,6 +34,8 @@ pub enum SyncMessage {
/// Hashes of blocks that were removed from canonical chain /// Hashes of blocks that were removed from canonical chain
retracted: Vec<H256>, retracted: Vec<H256>,
}, },
/// Best Block Hash in chain has been changed
NewChainHead,
/// A block is ready /// A block is ready
BlockVerified, BlockVerified,
} }

View File

@ -133,7 +133,7 @@ fn can_mine() {
let client_result = get_test_client_with_blocks(vec![dummy_blocks[0].clone()]); let client_result = get_test_client_with_blocks(vec![dummy_blocks[0].clone()]);
let client = client_result.reference(); let client = client_result.reference();
let b = client.prepare_sealing(Address::default(), vec![]).unwrap(); let b = client.prepare_sealing(Address::default(), vec![], vec![]).unwrap();
assert_eq!(*b.block().header().parent_hash(), BlockView::new(&dummy_blocks[0]).header_view().sha3()); assert_eq!(*b.block().header().parent_hash(), BlockView::new(&dummy_blocks[0]).header_view().sha3());
assert!(client.try_seal(b, vec![]).is_ok()); assert!(client.try_seal(b, vec![]).is_ok());

View File

@ -87,8 +87,15 @@ impl Miner {
} }
/// New chain head event. Restart mining operation. /// New chain head event. Restart mining operation.
fn prepare_sealing(&self, chain: &BlockChainClient) { pub fn prepare_sealing(&self, chain: &BlockChainClient) {
let b = chain.prepare_sealing(*self.author.read().unwrap(), self.extra_data.read().unwrap().clone()); let no_of_transactions = 128;
let transactions = self.transaction_queue.lock().unwrap().top_transactions(no_of_transactions);
let b = chain.prepare_sealing(
self.author(),
self.extra_data(),
transactions,
);
*self.sealing_block.lock().unwrap() = b; *self.sealing_block.lock().unwrap() = b;
} }

View File

@ -1270,6 +1270,10 @@ impl ChainSync {
// TODO [todr] propagate transactions? // TODO [todr] propagate transactions?
} }
pub fn chain_new_head(&mut self, io: &mut SyncIo) {
self.miner.prepare_sealing(io.chain());
}
} }
#[cfg(test)] #[cfg(test)]

View File

@ -158,6 +158,10 @@ impl NetworkProtocolHandler<SyncMessage> for EthSync {
let mut sync_io = NetSyncIo::new(io, self.chain.deref()); let mut sync_io = NetSyncIo::new(io, self.chain.deref());
self.sync.write().unwrap().chain_new_blocks(&mut sync_io, good, bad, retracted); self.sync.write().unwrap().chain_new_blocks(&mut sync_io, good, bad, retracted);
}, },
SyncMessage::NewChainHead => {
let mut sync_io = NetSyncIo::new(io, self.chain.deref());
self.sync.write().unwrap().chain_new_head(&mut sync_io);
}
_ => {/* Ignore other messages */}, _ => {/* Ignore other messages */},
} }
} }

View File

@ -24,7 +24,7 @@ use io::SyncIo;
use chain::ChainSync; use chain::ChainSync;
use ::SyncConfig; use ::SyncConfig;
use ethcore::receipt::Receipt; use ethcore::receipt::Receipt;
use ethcore::transaction::{LocalizedTransaction, Transaction, Action}; use ethcore::transaction::{LocalizedTransaction, SignedTransaction, Transaction, Action};
use ethcore::filter::Filter; use ethcore::filter::Filter;
use ethcore::log_entry::LocalizedLogEntry; use ethcore::log_entry::LocalizedLogEntry;
@ -311,7 +311,7 @@ impl BlockChainClient for TestBlockChainClient {
} }
} }
fn prepare_sealing(&self, author: Address, extra_data: Bytes) -> Option<ClosedBlock> { fn prepare_sealing(&self, author: Address, extra_data: Bytes, transactions: Vec<SignedTransaction>) -> Option<ClosedBlock> {
unimplemented!() unimplemented!()
} }