From 5db84c32338bc6708dce3d299553531f27b68f8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Wed, 9 Mar 2016 12:54:07 +0100 Subject: [PATCH] Adding transactions to block --- ethcore/src/block.rs | 2 +- ethcore/src/client.rs | 35 +++++++++++++++++++++++++++++------ ethcore/src/service.rs | 2 ++ ethcore/src/tests/client.rs | 2 +- miner/src/miner.rs | 11 +++++++++-- sync/src/chain.rs | 4 ++++ sync/src/lib.rs | 4 ++++ sync/src/tests/helpers.rs | 4 ++-- 8 files changed, 52 insertions(+), 12 deletions(-) diff --git a/ethcore/src/block.rs b/ethcore/src/block.rs index 68f647e37..9ecd58e0a 100644 --- a/ethcore/src/block.rs +++ b/ethcore/src/block.rs @@ -274,7 +274,7 @@ impl<'x> OpenBlock<'x> { s.block.base.header.note_dirty(); ClosedBlock { - block: s.block, + block: s.block, uncle_bytes: uncle_bytes, } } diff --git a/ethcore/src/client.rs b/ethcore/src/client.rs index af1745ca8..fb69df757 100644 --- a/ethcore/src/client.rs +++ b/ethcore/src/client.rs @@ -32,7 +32,7 @@ use service::{NetSyncMessage, SyncMessage}; use env_info::LastHashes; use verification::*; use block::*; -use transaction::LocalizedTransaction; +use transaction::{LocalizedTransaction, SignedTransaction}; use extras::TransactionAddress; use filter::Filter; use log_entry::LocalizedLogEntry; @@ -185,7 +185,10 @@ pub trait BlockChainClient : Sync + Send { /// Returns logs matching given filter. fn logs(&self, filter: Filter) -> Vec; - fn prepare_sealing(&self, author: Address, extra_data: Bytes) -> Option; + /// Returns ClosedBlock prepared for sealing. + fn prepare_sealing(&self, author: Address, extra_data: Bytes, transactions: Vec) -> Option; + + /// 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) -> Result; } @@ -417,6 +420,12 @@ impl Client where V: Verifier { } } + { + if self.chain_info().best_block_hash != original_best { + io.send(NetworkIoMessage::User(SyncMessage::NewChainHead)).unwrap(); + } + } + imported } @@ -477,7 +486,7 @@ impl BlockChainClient for Client where V: Verifier { block.try_seal(self.engine.deref().deref(), seal) } - fn prepare_sealing(&self, author: Address, extra_data: Bytes) -> Option { + fn prepare_sealing(&self, author: Address, extra_data: Bytes, transactions: Vec) -> Option { let engine = self.engine.deref().deref(); let h = self.chain.read().unwrap().best_block_hash(); @@ -490,7 +499,9 @@ impl BlockChainClient for Client where V: Verifier { 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() .into_iter() .take(engine.maximum_uncle_count()) @@ -498,10 +509,22 @@ impl BlockChainClient for Client where V: Verifier { 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(); - 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) } diff --git a/ethcore/src/service.rs b/ethcore/src/service.rs index 443d09e3b..11380d276 100644 --- a/ethcore/src/service.rs +++ b/ethcore/src/service.rs @@ -34,6 +34,8 @@ pub enum SyncMessage { /// Hashes of blocks that were removed from canonical chain retracted: Vec, }, + /// Best Block Hash in chain has been changed + NewChainHead, /// A block is ready BlockVerified, } diff --git a/ethcore/src/tests/client.rs b/ethcore/src/tests/client.rs index d31a780e6..ed0b02788 100644 --- a/ethcore/src/tests/client.rs +++ b/ethcore/src/tests/client.rs @@ -133,7 +133,7 @@ fn can_mine() { let client_result = get_test_client_with_blocks(vec![dummy_blocks[0].clone()]); 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!(client.try_seal(b, vec![]).is_ok()); diff --git a/miner/src/miner.rs b/miner/src/miner.rs index 76130b261..501f8c35c 100644 --- a/miner/src/miner.rs +++ b/miner/src/miner.rs @@ -87,8 +87,15 @@ impl Miner { } /// New chain head event. Restart mining operation. - fn prepare_sealing(&self, chain: &BlockChainClient) { - let b = chain.prepare_sealing(*self.author.read().unwrap(), self.extra_data.read().unwrap().clone()); + pub fn prepare_sealing(&self, chain: &BlockChainClient) { + 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; } diff --git a/sync/src/chain.rs b/sync/src/chain.rs index c607f53b1..2669b71e2 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -1270,6 +1270,10 @@ impl ChainSync { // TODO [todr] propagate transactions? } + pub fn chain_new_head(&mut self, io: &mut SyncIo) { + self.miner.prepare_sealing(io.chain()); + } + } #[cfg(test)] diff --git a/sync/src/lib.rs b/sync/src/lib.rs index 0d6044135..be01d2b7b 100644 --- a/sync/src/lib.rs +++ b/sync/src/lib.rs @@ -158,6 +158,10 @@ impl NetworkProtocolHandler for EthSync { let mut sync_io = NetSyncIo::new(io, self.chain.deref()); 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 */}, } } diff --git a/sync/src/tests/helpers.rs b/sync/src/tests/helpers.rs index 37ee862b5..8c8f669a2 100644 --- a/sync/src/tests/helpers.rs +++ b/sync/src/tests/helpers.rs @@ -24,7 +24,7 @@ use io::SyncIo; use chain::ChainSync; use ::SyncConfig; use ethcore::receipt::Receipt; -use ethcore::transaction::{LocalizedTransaction, Transaction, Action}; +use ethcore::transaction::{LocalizedTransaction, SignedTransaction, Transaction, Action}; use ethcore::filter::Filter; use ethcore::log_entry::LocalizedLogEntry; @@ -311,7 +311,7 @@ impl BlockChainClient for TestBlockChainClient { } } - fn prepare_sealing(&self, author: Address, extra_data: Bytes) -> Option { + fn prepare_sealing(&self, author: Address, extra_data: Bytes, transactions: Vec) -> Option { unimplemented!() }