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();
ClosedBlock {
block: s.block,
block: s.block,
uncle_bytes: uncle_bytes,
}
}

View File

@ -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<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>;
}
@ -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
}
@ -477,7 +486,7 @@ impl<V> BlockChainClient for Client<V> where V: Verifier {
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 h = self.chain.read().unwrap().best_block_hash();
@ -490,7 +499,9 @@ impl<V> BlockChainClient for Client<V> 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<V> BlockChainClient for Client<V> 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)
}

View File

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

View File

@ -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;
}

View File

@ -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)]

View File

@ -158,6 +158,10 @@ impl NetworkProtocolHandler<SyncMessage> 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 */},
}
}

View File

@ -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<ClosedBlock> {
fn prepare_sealing(&self, author: Address, extra_data: Bytes, transactions: Vec<SignedTransaction>) -> Option<ClosedBlock> {
unimplemented!()
}