Prevent broadcasting transactions to peer that send them.

This commit is contained in:
Tomasz Drwięga
2016-12-10 21:22:19 +01:00
parent aaf6da4c00
commit 19ca9ad460
12 changed files with 25 additions and 107 deletions

View File

@@ -99,8 +99,6 @@ pub struct TransactionStats {
pub first_seen: u64,
/// Peers it was propagated to.
pub propagated_to: BTreeMap<H512, usize>,
/// Peers that propagated the transaction back.
pub received_from: BTreeMap<H512, usize>,
}
/// Peer connection information
@@ -338,9 +336,9 @@ impl ChainNotify for EthSync {
self.network.stop().unwrap_or_else(|e| warn!("Error stopping network: {:?}", e));
}
fn transactions_imported(&self, hashes: Vec<H256>, peer_id: Option<H512>, block_number: u64) {
fn transactions_received(&self, hashes: Vec<H256>, peer_id: PeerId) {
let mut sync = self.sync_handler.sync.write();
sync.transactions_imported(hashes, peer_id, block_number);
sync.transactions_received(hashes, peer_id);
}
}
@@ -351,7 +349,7 @@ struct TxRelay(Arc<BlockChainClient>);
impl LightHandler for TxRelay {
fn on_transactions(&self, ctx: &EventContext, relay: &[::ethcore::transaction::SignedTransaction]) {
trace!(target: "les", "Relaying {} transactions from peer {}", relay.len(), ctx.peer());
self.0.queue_transactions(relay.iter().map(|tx| ::rlp::encode(tx).to_vec()).collect(), ctx.persistent_peer_id())
self.0.queue_transactions(relay.iter().map(|tx| ::rlp::encode(tx).to_vec()).collect(), ctx.peer())
}
}

View File

@@ -432,10 +432,10 @@ impl ChainSync {
self.transactions_stats.stats()
}
/// Updates statistics for imported transactions.
pub fn transactions_imported(&mut self, hashes: Vec<H256>, peer_id: Option<H512>, block_number: u64) {
for hash in hashes {
self.transactions_stats.received(hash, peer_id, block_number);
/// Updates transactions were received by a peer
pub fn transactions_received(&mut self, hashes: Vec<H256>, peer_id: PeerId) {
if let Some(mut peer_info) = self.peers.get_mut(&peer_id) {
peer_info.last_sent_transactions.extend(&hashes);
}
}
@@ -1416,8 +1416,7 @@ impl ChainSync {
let tx = rlp.as_raw().to_vec();
transactions.push(tx);
}
let id = io.peer_session_info(peer_id).and_then(|info| info.id);
io.chain().queue_transactions(transactions, id);
io.chain().queue_transactions(transactions, peer_id);
Ok(())
}

View File

@@ -26,7 +26,6 @@ type BlockNumber = u64;
pub struct Stats {
first_seen: BlockNumber,
propagated_to: HashMap<NodeId, usize>,
received_from: HashMap<NodeId, usize>,
}
impl Stats {
@@ -34,7 +33,6 @@ impl Stats {
Stats {
first_seen: number,
propagated_to: Default::default(),
received_from: Default::default(),
}
}
}
@@ -47,10 +45,6 @@ impl<'a> From<&'a Stats> for TransactionStats {
.iter()
.map(|(hash, size)| (*hash, *size))
.collect(),
received_from: other.received_from
.iter()
.map(|(hash, size)| (*hash, *size))
.collect(),
}
}
}
@@ -69,14 +63,6 @@ impl TransactionsStats {
*count = count.saturating_add(1);
}
/// Increase number of back-propagations from given `enodeid`.
pub fn received(&mut self, hash: H256, enode_id: Option<NodeId>, current_block_num: BlockNumber) {
let enode_id = enode_id.unwrap_or_default();
let mut stats = self.pending_transactions.entry(hash).or_insert_with(|| Stats::new(current_block_num));
let mut count = stats.received_from.entry(enode_id).or_insert(0);
*count = count.saturating_add(1);
}
/// Returns propagation stats for given hash or `None` if hash is not known.
#[cfg(test)]
pub fn get(&self, hash: &H256) -> Option<&Stats> {
@@ -127,32 +113,6 @@ mod tests {
enodeid1 => 2,
enodeid2 => 1
],
received_from: Default::default(),
}));
}
#[test]
fn should_keep_track_of_back_propagations() {
// given
let mut stats = TransactionsStats::default();
let hash = 5.into();
let enodeid1 = 2.into();
let enodeid2 = 5.into();
// when
stats.received(hash, Some(enodeid1), 5);
stats.received(hash, Some(enodeid1), 10);
stats.received(hash, Some(enodeid2), 15);
// then
let stats = stats.get(&hash);
assert_eq!(stats, Some(&Stats {
first_seen: 5,
propagated_to: Default::default(),
received_from: hash_map![
enodeid1 => 2,
enodeid2 => 1
]
}));
}