Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

node-filter does not use ChainNotify #8231

Merged
merged 2 commits into from
Mar 28, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion ethcore/node_filter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ authors = ["Parity Technologies <[email protected]>"]

[dependencies]
ethcore = { path = ".."}
ethcore-bytes = { path = "../../util/bytes" }
ethcore-network-devp2p = { path = "../../util/network-devp2p" }
ethereum-types = "0.2"
log = "0.3"
Expand Down
53 changes: 21 additions & 32 deletions ethcore/node_filter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

extern crate ethabi;
extern crate ethcore;
extern crate ethcore_bytes as bytes;
extern crate ethcore_network_devp2p as network;
extern crate ethereum_types;
extern crate lru_cache;
Expand All @@ -42,8 +41,7 @@ use std::sync::Weak;
use lru_cache::LruCache;
use parking_lot::Mutex;

use bytes::Bytes;
use ethcore::client::{BlockChainClient, BlockId, ChainNotify};
use ethcore::client::{BlockChainClient, BlockId};
use ethereum_types::{H256, Address};
use network::{NodeId, ConnectionFilter, ConnectionDirection};

Expand All @@ -56,7 +54,7 @@ pub struct NodeFilter {
contract: peer_set::PeerSet,
client: Weak<BlockChainClient>,
contract_address: Address,
permission_cache: Mutex<LruCache<NodeId, bool>>,
permission_cache: Mutex<LruCache<(H256, NodeId), bool>>,
}

impl NodeFilter {
Expand All @@ -69,25 +67,27 @@ impl NodeFilter {
permission_cache: Mutex::new(LruCache::new(MAX_CACHE_SIZE)),
}
}

/// Clear cached permissions.
pub fn clear_cache(&self) {
self.permission_cache.lock().clear();
}
}

impl ConnectionFilter for NodeFilter {
fn connection_allowed(&self, own_id: &NodeId, connecting_id: &NodeId, _direction: ConnectionDirection) -> bool {
let client = match self.client.upgrade() {
Some(client) => client,
None => return false,
};

let block_hash = match client.block_hash(BlockId::Latest) {
Some(block_hash) => block_hash,
None => return false,
};

let key = (block_hash, *connecting_id);

let mut cache = self.permission_cache.lock();
if let Some(res) = cache.get_mut(connecting_id) {
if let Some(res) = cache.get_mut(&key) {
return *res;
}

let client = match self.client.upgrade() {
Some(client) => client,
None => return false,
};

let address = self.contract_address;
let own_low = H256::from_slice(&own_id[0..32]);
Expand All @@ -103,28 +103,17 @@ impl ConnectionFilter for NodeFilter {
false
});

cache.insert(*connecting_id, allowed);
cache.insert(key, allowed);
allowed
}
}

impl ChainNotify for NodeFilter {
fn new_blocks(&self, imported: Vec<H256>, _invalid: Vec<H256>, _enacted: Vec<H256>, _retracted: Vec<H256>, _sealed: Vec<H256>, _proposed: Vec<Bytes>, _duration: u64) {
if !imported.is_empty() {
self.clear_cache();
}
}
}


#[cfg(test)]
mod test {
use std::sync::{Arc, Weak};
use std::str::FromStr;
use ethcore::spec::Spec;
use ethcore::client::{BlockChainClient, Client, ClientConfig};
use ethcore::miner::Miner;
use ethereum_types::Address;
use network::{ConnectionDirection, ConnectionFilter, NodeId};
use io::IoChannel;
use super::NodeFilter;
Expand All @@ -133,7 +122,7 @@ mod test {
/// Contract code: https://gist.github.com/arkpar/467dbcc73cbb85b0997a7a10ffa0695f
#[test]
fn node_filter() {
let contract_addr = Address::from_str("0000000000000000000000000000000000000005").unwrap();
let contract_addr = "0000000000000000000000000000000000000005".into();
let data = include_bytes!("../res/node_filter.json");
let tempdir = TempDir::new("").unwrap();
let spec = Spec::load(&tempdir.path(), &data[..]).unwrap();
Expand All @@ -147,11 +136,11 @@ mod test {
IoChannel::disconnected(),
).unwrap();
let filter = NodeFilter::new(Arc::downgrade(&client) as Weak<BlockChainClient>, contract_addr);
let self1 = NodeId::from_str("00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002").unwrap();
let self2 = NodeId::from_str("00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003").unwrap();
let node1 = NodeId::from_str("00000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000012").unwrap();
let node2 = NodeId::from_str("00000000000000000000000000000000000000000000000000000000000000210000000000000000000000000000000000000000000000000000000000000022").unwrap();
let nodex = NodeId::from_str("77000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap();
let self1: NodeId = "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002".into();
let self2: NodeId = "00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003".into();
let node1: NodeId = "00000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000012".into();
let node2: NodeId = "00000000000000000000000000000000000000000000000000000000000000210000000000000000000000000000000000000000000000000000000000000022".into();
let nodex: NodeId = "77000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000".into();

assert!(filter.connection_allowed(&self1, &node1, ConnectionDirection::Inbound));
assert!(filter.connection_allowed(&self1, &nodex, ConnectionDirection::Inbound));
Expand Down
3 changes: 0 additions & 3 deletions parity/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,9 +685,6 @@ pub fn execute_impl(cmd: RunCmd, can_restart: bool, logger: Arc<RotatingLogger>)
).map_err(|e| format!("Sync error: {}", e))?;

service.add_notify(chain_notify.clone());
if let Some(filter) = connection_filter {
service.add_notify(filter);
}

// start network
if network_enabled {
Expand Down