Merge branch 'master' into tx_queue_live

Conflicts:
	miner/src/miner.rs
This commit is contained in:
Tomasz Drwięga 2016-03-18 09:48:35 +01:00
commit cee45e1a8e
9 changed files with 77 additions and 23 deletions

View File

@ -97,9 +97,7 @@ impl Miner {
/// Prepares new block for sealing including top transactions from queue. /// Prepares new block for sealing including top transactions from queue.
pub fn prepare_sealing(&self, chain: &BlockChainClient) { pub fn prepare_sealing(&self, chain: &BlockChainClient) {
let no_of_transactions = 128; let transactions = self.transaction_queue.lock().unwrap().top_transactions();
// TODO: should select transactions orm queue according to gas limit of block.
let transactions = self.transaction_queue.lock().unwrap().top_transactions(no_of_transactions);
let b = chain.prepare_sealing( let b = chain.prepare_sealing(
self.author(), self.author(),
@ -107,6 +105,7 @@ impl Miner {
self.extra_data(), self.extra_data(),
transactions, transactions,
); );
*self.sealing_block.lock().unwrap() = b; *self.sealing_block.lock().unwrap() = b;
} }
} }

View File

@ -59,7 +59,7 @@
//! // Check status //! // Check status
//! assert_eq!(txq.status().pending, 2); //! assert_eq!(txq.status().pending, 2);
//! // Check top transactions //! // Check top transactions
//! let top = txq.top_transactions(3); //! let top = txq.top_transactions();
//! assert_eq!(top.len(), 2); //! assert_eq!(top.len(), 2);
//! assert_eq!(top[0], st1); //! assert_eq!(top[0], st1);
//! assert_eq!(top[1], st2); //! assert_eq!(top[1], st2);
@ -69,7 +69,7 @@
//! txq.remove(&st1.hash(), &default_nonce); //! txq.remove(&st1.hash(), &default_nonce);
//! assert_eq!(txq.status().pending, 0); //! assert_eq!(txq.status().pending, 0);
//! assert_eq!(txq.status().future, 1); //! assert_eq!(txq.status().future, 1);
//! assert_eq!(txq.top_transactions(3).len(), 0); //! assert_eq!(txq.top_transactions().len(), 0);
//! } //! }
//! ``` //! ```
//! //!
@ -458,10 +458,9 @@ impl TransactionQueue {
// Will be used when mining merged // Will be used when mining merged
#[allow(dead_code)] #[allow(dead_code)]
/// Returns top transactions from the queue ordered by priority. /// Returns top transactions from the queue ordered by priority.
pub fn top_transactions(&self, size: usize) -> Vec<SignedTransaction> { pub fn top_transactions(&self) -> Vec<SignedTransaction> {
self.current.by_priority self.current.by_priority
.iter() .iter()
.take(size)
.map(|t| self.by_hash.get(&t.hash).expect("Transaction Queue Inconsistency")) .map(|t| self.by_hash.get(&t.hash).expect("Transaction Queue Inconsistency"))
.map(|t| t.transaction.clone()) .map(|t| t.transaction.clone())
.collect() .collect()
@ -756,7 +755,7 @@ mod test {
txq.add(tx2.clone(), &default_nonce).unwrap(); txq.add(tx2.clone(), &default_nonce).unwrap();
// then // then
let top = txq.top_transactions(5); let top = txq.top_transactions();
assert_eq!(top[0], tx); assert_eq!(top[0], tx);
assert_eq!(top[1], tx2); assert_eq!(top[1], tx2);
assert_eq!(top.len(), 2); assert_eq!(top.len(), 2);
@ -795,7 +794,7 @@ mod test {
let stats = txq.status(); let stats = txq.status();
assert_eq!(stats.pending, 1); assert_eq!(stats.pending, 1);
assert_eq!(stats.future, 1); assert_eq!(stats.future, 1);
let top = txq.top_transactions(5); let top = txq.top_transactions();
assert_eq!(top.len(), 1); assert_eq!(top.len(), 1);
assert_eq!(top[0], tx); assert_eq!(top[0], tx);
} }
@ -922,7 +921,7 @@ mod test {
txq.add(tx2.clone(), &default_nonce).unwrap(); txq.add(tx2.clone(), &default_nonce).unwrap();
// then // then
let t = txq.top_transactions(2); let t = txq.top_transactions();
assert_eq!(txq.status().pending, 1); assert_eq!(txq.status().pending, 1);
assert_eq!(t.len(), 1); assert_eq!(t.len(), 1);
assert_eq!(t[0], tx); assert_eq!(t[0], tx);
@ -1046,7 +1045,7 @@ mod test {
let stats = txq.status(); let stats = txq.status();
assert_eq!(stats.pending, 1); assert_eq!(stats.pending, 1);
assert_eq!(stats.future, 0); assert_eq!(stats.future, 0);
assert_eq!(txq.top_transactions(1)[0].gas_price, U256::from(200)); assert_eq!(txq.top_transactions()[0].gas_price, U256::from(200));
} }
#[test] #[test]
@ -1076,7 +1075,7 @@ mod test {
let stats = txq.status(); let stats = txq.status();
assert_eq!(stats.future, 0); assert_eq!(stats.future, 0);
assert_eq!(stats.pending, 2); assert_eq!(stats.pending, 2);
assert_eq!(txq.top_transactions(2)[1].gas_price, U256::from(200)); assert_eq!(txq.top_transactions()[1].gas_price, U256::from(200));
} }
#[test] #[test]

View File

@ -937,6 +937,11 @@ impl ChainSync {
} }
/// Called when peer sends us new transactions /// Called when peer sends us new transactions
fn on_peer_transactions(&mut self, io: &mut SyncIo, peer_id: PeerId, r: &UntrustedRlp) -> Result<(), PacketDecodeError> { fn on_peer_transactions(&mut self, io: &mut SyncIo, peer_id: PeerId, r: &UntrustedRlp) -> Result<(), PacketDecodeError> {
// accepting transactions once only fully synced
if !io.is_chain_queue_empty() {
return Ok(());
}
let item_count = r.item_count(); let item_count = r.item_count();
trace!(target: "sync", "{} -> Transactions ({} entries)", peer_id, item_count); trace!(target: "sync", "{} -> Transactions ({} entries)", peer_id, item_count);
@ -1282,10 +1287,12 @@ impl ChainSync {
/// called when block is imported to chain, updates transactions queue and propagates the blocks /// called when block is imported to chain, updates transactions queue and propagates the blocks
pub fn chain_new_blocks(&mut self, io: &mut SyncIo, imported: &[H256], invalid: &[H256], enacted: &[H256], retracted: &[H256]) { pub fn chain_new_blocks(&mut self, io: &mut SyncIo, imported: &[H256], invalid: &[H256], enacted: &[H256], retracted: &[H256]) {
// Notify miner if io.is_chain_queue_empty() {
self.miner.chain_new_blocks(io.chain(), imported, invalid, enacted, retracted); // Notify miner
// Propagate latests blocks self.miner.chain_new_blocks(io.chain(), imported, invalid, enacted, retracted);
self.propagate_latest_blocks(io); // Propagate latests blocks
self.propagate_latest_blocks(io);
}
// TODO [todr] propagate transactions? // TODO [todr] propagate transactions?
} }
@ -1654,6 +1661,33 @@ mod tests {
assert_eq!(status.transactions_in_future_queue, 0); assert_eq!(status.transactions_in_future_queue, 0);
} }
#[test]
fn should_not_add_transactions_to_queue_if_not_synced() {
// given
let mut client = TestBlockChainClient::new();
client.add_blocks(98, EachBlockWith::Uncle);
client.add_blocks(1, EachBlockWith::UncleAndTransaction);
client.add_blocks(1, EachBlockWith::Transaction);
let mut sync = dummy_sync_with_peer(client.block_hash_delta_minus(5));
let good_blocks = vec![client.block_hash_delta_minus(2)];
let retracted_blocks = vec![client.block_hash_delta_minus(1)];
let mut queue = VecDeque::new();
let mut io = TestIo::new(&mut client, &mut queue, None);
// when
sync.chain_new_blocks(&mut io, &[], &[], &[], &good_blocks);
assert_eq!(sync.miner.status().transactions_in_future_queue, 0);
assert_eq!(sync.miner.status().transactions_in_pending_queue, 0);
sync.chain_new_blocks(&mut io, &good_blocks, &[], &[], &retracted_blocks);
// then
let status = sync.miner.status();
assert_eq!(status.transactions_in_pending_queue, 0);
assert_eq!(status.transactions_in_future_queue, 0);
}
#[test] #[test]
fn returns_requested_block_headers() { fn returns_requested_block_headers() {
let mut client = TestBlockChainClient::new(); let mut client = TestBlockChainClient::new();

View File

@ -37,6 +37,10 @@ pub trait SyncIo {
fn peer_info(&self, peer_id: PeerId) -> String { fn peer_info(&self, peer_id: PeerId) -> String {
peer_id.to_string() peer_id.to_string()
} }
/// Returns if the chain block queue empty
fn is_chain_queue_empty(&self) -> bool {
self.chain().queue_info().is_empty()
}
} }
/// Wraps `NetworkContext` and the blockchain client /// Wraps `NetworkContext` and the blockchain client

View File

@ -173,7 +173,7 @@ impl NetworkProtocolHandler<SyncMessage> for EthSync {
SyncMessage::NewChainHead => { SyncMessage::NewChainHead => {
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_head(&mut sync_io); self.sync.write().unwrap().chain_new_head(&mut sync_io);
} },
_ => {/* Ignore other messages */}, _ => {/* Ignore other messages */},
} }
} }

View File

@ -29,7 +29,6 @@ sha3 = { path = "sha3" }
serde = "0.7.0" serde = "0.7.0"
clippy = { version = "0.0.50", optional = true } clippy = { version = "0.0.50", optional = true }
json-tests = { path = "json-tests" } json-tests = { path = "json-tests" }
rustc_version = "0.1.0"
igd = "0.4.2" igd = "0.4.2"
ethcore-devtools = { path = "../devtools" } ethcore-devtools = { path = "../devtools" }
libc = "0.2.7" libc = "0.2.7"
@ -44,3 +43,4 @@ dev = ["clippy"]
[build-dependencies] [build-dependencies]
vergen = "*" vergen = "*"
rustc_version = "0.1.0"

View File

@ -15,9 +15,23 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
extern crate vergen; extern crate vergen;
extern crate rustc_version;
use vergen::*; use vergen::*;
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::Path;
fn main() { fn main() {
vergen(OutputFns::all()).unwrap(); vergen(OutputFns::all()).unwrap();
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("rustc_version.rs");
let mut f = File::create(&dest_path).unwrap();
f.write_all(format!("
/// Returns compiler version.
pub fn rustc_version() -> &'static str {{
\"{}\"
}}
", rustc_version::version()).as_bytes()).unwrap();
} }

View File

@ -109,9 +109,7 @@ extern crate log as rlog;
extern crate igd; extern crate igd;
extern crate ethcore_devtools as devtools; extern crate ethcore_devtools as devtools;
extern crate libc; extern crate libc;
extern crate rustc_version;
extern crate target_info; extern crate target_info;
extern crate vergen;
extern crate bigint; extern crate bigint;
extern crate chrono; extern crate chrono;

View File

@ -20,9 +20,9 @@ use std::fs::File;
use common::*; use common::*;
use rlp::{Stream, RlpStream}; use rlp::{Stream, RlpStream};
use target_info::Target; use target_info::Target;
use rustc_version;
include!(concat!(env!("OUT_DIR"), "/version.rs")); include!(concat!(env!("OUT_DIR"), "/version.rs"));
include!(concat!(env!("OUT_DIR"), "/rustc_version.rs"));
#[derive(Debug,Clone,PartialEq,Eq)] #[derive(Debug,Clone,PartialEq,Eq)]
/// Diff type for specifying a change (or not). /// Diff type for specifying a change (or not).
@ -70,7 +70,13 @@ pub fn contents(name: &str) -> Result<Bytes, UtilError> {
/// Get the standard version string for this software. /// Get the standard version string for this software.
pub fn version() -> String { pub fn version() -> String {
format!("Parity/v{}-unstable-{}-{}/{}-{}-{}/rustc{}", env!("CARGO_PKG_VERSION"), short_sha(), commit_date().replace("-", ""), Target::arch(), Target::os(), Target::env(), rustc_version::version()) let sha3 = short_sha();
let sha3_dash = if sha3.is_empty() { "" } else { "-" };
let commit_date = commit_date().replace("-", "");
let date_dash = if commit_date.is_empty() { "" } else { "-" };
let env = Target::env();
let env_dash = if env.is_empty() { "" } else { "-" };
format!("Parity/v{}-unstable{}{}{}{}/{}-{}{}{}/rustc{}", env!("CARGO_PKG_VERSION"), sha3_dash, sha3, date_dash, commit_date, Target::arch(), Target::os(), env_dash, env, rustc_version())
} }
/// Get the standard version data for this software. /// Get the standard version data for this software.
@ -82,7 +88,7 @@ pub fn version_data() -> Bytes {
u32::from_str(env!("CARGO_PKG_VERSION_PATCH")).unwrap(); u32::from_str(env!("CARGO_PKG_VERSION_PATCH")).unwrap();
s.append(&v); s.append(&v);
s.append(&"Parity"); s.append(&"Parity");
s.append(&format!("{}", rustc_version::version())); s.append(&format!("{}", rustc_version()));
s.append(&&Target::os()[0..2]); s.append(&&Target::os()[0..2]);
s.out() s.out()
} }