Merge branch 'master' into ui-2
This commit is contained in:
		
						commit
						0238295654
					
				
							
								
								
									
										18
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										18
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							| @ -301,6 +301,7 @@ dependencies = [ | ||||
|  "bloomable 0.1.0", | ||||
|  "ethcore-util 1.8.0", | ||||
|  "ethjson 0.1.0", | ||||
|  "heapsize 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", | ||||
|  "rlp 0.2.0", | ||||
|  "rlp_derive 0.1.0", | ||||
|  "rustc-hex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", | ||||
| @ -516,6 +517,7 @@ dependencies = [ | ||||
|  "evm 0.1.0", | ||||
|  "futures 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", | ||||
|  "hardware-wallet 1.8.0", | ||||
|  "heapsize 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", | ||||
|  "hyper 0.10.0-a.0 (git+https://github.com/paritytech/hyper)", | ||||
|  "itertools 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)", | ||||
|  "lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", | ||||
| @ -652,6 +654,7 @@ dependencies = [ | ||||
|  "ethcore-util 1.8.0", | ||||
|  "evm 0.1.0", | ||||
|  "futures 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", | ||||
|  "heapsize 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", | ||||
|  "itertools 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)", | ||||
|  "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", | ||||
|  "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", | ||||
| @ -920,6 +923,7 @@ dependencies = [ | ||||
|  "ethcore-util 1.8.0", | ||||
|  "ethjson 0.1.0", | ||||
|  "evmjit 1.8.0", | ||||
|  "heapsize 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", | ||||
|  "lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", | ||||
|  "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", | ||||
|  "parity-wasm 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)", | ||||
| @ -1615,6 +1619,19 @@ dependencies = [ | ||||
|  "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", | ||||
| ] | ||||
| 
 | ||||
| [[package]] | ||||
| name = "node-filter" | ||||
| version = "1.8.0" | ||||
| dependencies = [ | ||||
|  "ethcore 1.8.0", | ||||
|  "ethcore-io 1.8.0", | ||||
|  "ethcore-network 1.8.0", | ||||
|  "ethcore-util 1.8.0", | ||||
|  "futures 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", | ||||
|  "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", | ||||
|  "native-contracts 0.1.0", | ||||
| ] | ||||
| 
 | ||||
| [[package]] | ||||
| name = "node-health" | ||||
| version = "0.1.0" | ||||
| @ -1838,6 +1855,7 @@ dependencies = [ | ||||
|  "isatty 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", | ||||
|  "jsonrpc-core 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)", | ||||
|  "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", | ||||
|  "node-filter 1.8.0", | ||||
|  "node-health 0.1.0", | ||||
|  "num_cpus 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", | ||||
|  "number_prefix 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", | ||||
|  | ||||
| @ -42,6 +42,7 @@ ethcore-light = { path = "ethcore/light" } | ||||
| ethcore-logger = { path = "logger" } | ||||
| ethcore-stratum = { path = "stratum" } | ||||
| ethcore-network = { path = "util/network" } | ||||
| node-filter = { path = "ethcore/node_filter" } | ||||
| ethkey = { path = "ethkey" } | ||||
| node-health = { path = "dapps/node-health" } | ||||
| rlp = { path = "util/rlp" } | ||||
|  | ||||
| @ -35,6 +35,7 @@ ethstore = { path = "../ethstore" } | ||||
| evm = { path = "evm" } | ||||
| futures = "0.1" | ||||
| hardware-wallet = { path = "../hw" } | ||||
| heapsize = "0.4" | ||||
| hyper = { git = "https://github.com/paritytech/hyper", default-features = false } | ||||
| itertools = "0.5" | ||||
| lazy_static = "0.2" | ||||
|  | ||||
| @ -10,6 +10,7 @@ common-types = { path = "../types" } | ||||
| ethcore-util = { path = "../../util" } | ||||
| evmjit = { path = "../../evmjit", optional = true } | ||||
| ethjson = { path = "../../json" } | ||||
| heapsize = "0.4" | ||||
| lazy_static = "0.2" | ||||
| log = "0.3" | ||||
| rlp = { path = "../../util/rlp" } | ||||
|  | ||||
| @ -15,7 +15,8 @@ | ||||
| // along with Parity.  If not, see <http://www.gnu.org/licenses/>.
 | ||||
| 
 | ||||
| use std::sync::Arc; | ||||
| use util::{H256, HeapSizeOf, Mutex}; | ||||
| use heapsize::HeapSizeOf; | ||||
| use util::{H256, Mutex}; | ||||
| use util::sha3::*; | ||||
| use util::cache::MemoryLruCache; | ||||
| use bit_set::BitSet; | ||||
|  | ||||
| @ -25,6 +25,7 @@ extern crate rlp; | ||||
| extern crate parity_wasm; | ||||
| extern crate wasm_utils; | ||||
| extern crate ethcore_logger; | ||||
| extern crate heapsize; | ||||
| extern crate vm; | ||||
| 
 | ||||
| #[macro_use] | ||||
|  | ||||
| @ -19,6 +19,7 @@ ethcore-io = { path = "../../util/io" } | ||||
| ethcore-ipc = { path = "../../ipc/rpc", optional = true } | ||||
| ethcore-devtools = { path = "../../devtools" } | ||||
| evm = { path = "../evm" } | ||||
| heapsize = "0.4" | ||||
| vm = { path = "../vm" } | ||||
| rlp = { path = "../../util/rlp" } | ||||
| rlp_derive = { path = "../../util/rlp_derive" } | ||||
|  | ||||
| @ -26,7 +26,8 @@ use ethcore::receipt::Receipt; | ||||
| 
 | ||||
| use stats::Corpus; | ||||
| use time::{SteadyTime, Duration}; | ||||
| use util::{U256, H256, HeapSizeOf}; | ||||
| use heapsize::HeapSizeOf; | ||||
| use util::{U256, H256}; | ||||
| use util::cache::MemoryLruCache; | ||||
| 
 | ||||
| /// Configuration for how much data to cache.
 | ||||
|  | ||||
| @ -36,7 +36,8 @@ use ethcore::header::Header; | ||||
| use ethcore::ids::BlockId; | ||||
| 
 | ||||
| use rlp::{Encodable, Decodable, DecoderError, RlpStream, Rlp, UntrustedRlp}; | ||||
| use util::{H256, U256, HeapSizeOf, RwLock}; | ||||
| use heapsize::HeapSizeOf; | ||||
| use util::{H256, U256, RwLock}; | ||||
| use util::kvdb::{DBTransaction, KeyValueDB}; | ||||
| 
 | ||||
| use cache::Cache; | ||||
|  | ||||
| @ -330,7 +330,7 @@ impl Client { | ||||
| 
 | ||||
| 	/// Get blockchain mem usage in bytes.
 | ||||
| 	pub fn chain_mem_used(&self) -> usize { | ||||
| 		use util::HeapSizeOf; | ||||
| 		use heapsize::HeapSizeOf; | ||||
| 
 | ||||
| 		self.chain.heap_size_of_children() | ||||
| 	} | ||||
|  | ||||
| @ -72,6 +72,7 @@ extern crate ethcore_network as network; | ||||
| extern crate ethcore_util as util; | ||||
| extern crate ethcore; | ||||
| extern crate evm; | ||||
| extern crate heapsize; | ||||
| extern crate futures; | ||||
| extern crate itertools; | ||||
| extern crate rand; | ||||
|  | ||||
| @ -28,6 +28,7 @@ const SERVICE_TRANSACTION_ABI: &'static str = include_str!("res/service_transact | ||||
| const SECRETSTORE_ACL_STORAGE_ABI: &'static str = include_str!("res/secretstore_acl_storage.json"); | ||||
| const VALIDATOR_SET_ABI: &'static str = include_str!("res/validator_set.json"); | ||||
| const VALIDATOR_REPORT_ABI: &'static str = include_str!("res/validator_report.json"); | ||||
| const PEER_SET_ABI: &'static str = include_str!("res/peer_set.json"); | ||||
| 
 | ||||
| const TEST_VALIDATOR_SET_ABI: &'static str = include_str!("res/test_validator_set.json"); | ||||
| 
 | ||||
| @ -53,6 +54,7 @@ fn main() { | ||||
| 	build_file("SecretStoreAclStorage", SECRETSTORE_ACL_STORAGE_ABI, "secretstore_acl_storage.rs"); | ||||
| 	build_file("ValidatorSet", VALIDATOR_SET_ABI, "validator_set.rs"); | ||||
| 	build_file("ValidatorReport", VALIDATOR_REPORT_ABI, "validator_report.rs"); | ||||
| 	build_file("PeerSet", PEER_SET_ABI, "peer_set.rs"); | ||||
| 
 | ||||
| 	build_test_contracts(); | ||||
| } | ||||
|  | ||||
							
								
								
									
										1
									
								
								ethcore/native_contracts/res/peer_set.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								ethcore/native_contracts/res/peer_set.json
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1 @@ | ||||
| [{"constant":true,"inputs":[{"name":"sl","type":"bytes32"},{"name":"sh","type":"bytes32"},{"name":"pl","type":"bytes32"},{"name":"ph","type":"bytes32"}],"name":"connectionAllowed","outputs":[{"name":"res","type":"bool"}],"payable":false,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"}] | ||||
| @ -30,6 +30,7 @@ mod service_transaction; | ||||
| mod secretstore_acl_storage; | ||||
| mod validator_set; | ||||
| mod validator_report; | ||||
| mod peer_set; | ||||
| 
 | ||||
| pub mod test_contracts; | ||||
| 
 | ||||
| @ -40,3 +41,4 @@ pub use self::service_transaction::ServiceTransactionChecker; | ||||
| pub use self::secretstore_acl_storage::SecretStoreAclStorage; | ||||
| pub use self::validator_set::ValidatorSet; | ||||
| pub use self::validator_report::ValidatorReport; | ||||
| pub use self::peer_set::PeerSet; | ||||
|  | ||||
							
								
								
									
										21
									
								
								ethcore/native_contracts/src/peer_set.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								ethcore/native_contracts/src/peer_set.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,21 @@ | ||||
| // Copyright 2015-2017 Parity Technologies (UK) Ltd.
 | ||||
| // This file is part of Parity.
 | ||||
| 
 | ||||
| // Parity is free software: you can redistribute it and/or modify
 | ||||
| // it under the terms of the GNU General Public License as published by
 | ||||
| // the Free Software Foundation, either version 3 of the License, or
 | ||||
| // (at your option) any later version.
 | ||||
| 
 | ||||
| // Parity is distributed in the hope that it will be useful,
 | ||||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | ||||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | ||||
| // GNU General Public License for more details.
 | ||||
| 
 | ||||
| // You should have received a copy of the GNU General Public License
 | ||||
| // along with Parity.  If not, see <http://www.gnu.org/licenses/>.
 | ||||
| 
 | ||||
| #![allow(unused_mut, unused_variables, unused_imports)] | ||||
| 
 | ||||
| //! Peer set contract.
 | ||||
| 
 | ||||
| include!(concat!(env!("OUT_DIR"), "/peer_set.rs")); | ||||
							
								
								
									
										16
									
								
								ethcore/node_filter/Cargo.toml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								ethcore/node_filter/Cargo.toml
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,16 @@ | ||||
| [package] | ||||
| description = "Parity smart network connections" | ||||
| homepage = "http://parity.io" | ||||
| license = "GPL-3.0" | ||||
| name = "node-filter" | ||||
| version = "1.8.0" | ||||
| authors = ["Parity Technologies <admin@parity.io>"] | ||||
| 
 | ||||
| [dependencies] | ||||
| ethcore = { path = ".."} | ||||
| ethcore-util = { path = "../../util" } | ||||
| ethcore-io = { path = "../../util/io" } | ||||
| ethcore-network = { path = "../../util/network" } | ||||
| native-contracts = { path = "../native_contracts" } | ||||
| futures = "0.1" | ||||
| log = "0.3" | ||||
							
								
								
									
										44
									
								
								ethcore/node_filter/res/node_filter.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								ethcore/node_filter/res/node_filter.json
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,44 @@ | ||||
| { | ||||
| 	"name": "TestNodeFilterContract", | ||||
| 	"engine": { | ||||
| 		"authorityRound": { | ||||
| 			"params": { | ||||
| 				"stepDuration": 1, | ||||
| 				"startStep": 2, | ||||
| 				"validators": { | ||||
| 					"contract": "0x0000000000000000000000000000000000000005" | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 	}, | ||||
| 	"params": { | ||||
| 		"accountStartNonce": "0x0", | ||||
| 		"maximumExtraDataSize": "0x20", | ||||
| 		"minGasLimit": "0x1388", | ||||
| 		"networkID" : "0x69", | ||||
| 		"gasLimitBoundDivisor": "0x0400" | ||||
| 	}, | ||||
| 	"genesis": { | ||||
| 		"seal": { | ||||
| 			"generic": "0xc180" | ||||
| 		}, | ||||
| 		"difficulty": "0x20000", | ||||
| 		"author": "0x0000000000000000000000000000000000000000", | ||||
| 		"timestamp": "0x00", | ||||
| 		"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", | ||||
| 		"extraData": "0x", | ||||
| 		"gasLimit": "0x222222" | ||||
| 	}, | ||||
| 	"accounts": { | ||||
| 		"0000000000000000000000000000000000000001": { "balance": "1", "builtin": { "name": "ecrecover", "pricing": { "linear": { "base": 3000, "word": 0 } } } }, | ||||
| 		"0000000000000000000000000000000000000002": { "balance": "1", "builtin": { "name": "sha256", "pricing": { "linear": { "base": 60, "word": 12 } } } }, | ||||
| 		"0000000000000000000000000000000000000003": { "balance": "1", "builtin": { "name": "ripemd160", "pricing": { "linear": { "base": 600, "word": 120 } } } }, | ||||
| 		"0000000000000000000000000000000000000004": { "balance": "1", "builtin": { "name": "identity", "pricing": { "linear": { "base": 15, "word": 3 } } } }, | ||||
| 		"0000000000000000000000000000000000000005": { | ||||
| 			"balance": "1", | ||||
| 			"constructor": "6060604052341561000f57600080fd5b5b6012600102600080601160010260001916815260200190815260200160002081600019169055506022600102600080602160010260001916815260200190815260200160002081600019169055506032600102600080603160010260001916815260200190815260200160002081600019169055506042600102600080604160010260001916815260200190815260200160002081600019169055505b5b610155806100bd6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063994d790a1461003e575b600080fd5b341561004957600080fd5b61008a6004808035600019169060200190919080356000191690602001909190803560001916906020019091908035600019169060200190919050506100a4565b604051808215151515815260200191505060405180910390f35b60006001800285600019161480156100c3575060026001028460001916145b156100d15760019050610121565b60006001028360001916141580156100f157506000600102826000191614155b801561011e5750816000191660008085600019166000191681526020019081526020016000205460001916145b90505b9493505050505600a165627a7a723058202082b8d8667fd397925f39785d8e804540beda0524d28af15921375145dfcc250029" | ||||
| 		}, | ||||
| 		"0x7d577a597b2742b498cb5cf0c26cdcd726d39e6e": { "balance": "1606938044258990275541962092341162602522202993782792835301376" }, | ||||
| 		"0x82a978b3f5962a5b0957d9ee9eef472ee55b42f1": { "balance": "1606938044258990275541962092341162602522202993782792835301376" } | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										154
									
								
								ethcore/node_filter/src/lib.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										154
									
								
								ethcore/node_filter/src/lib.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,154 @@ | ||||
| // Copyright 2015-2017 Parity Technologies (UK) Ltd.
 | ||||
| // This file is part of Parity.
 | ||||
| 
 | ||||
| // Parity is free software: you can redistribute it and/or modify
 | ||||
| // it under the terms of the GNU General Public License as published by
 | ||||
| // the Free Software Foundation, either version 3 of the License, or
 | ||||
| // (at your option) any later version.
 | ||||
| 
 | ||||
| // Parity is distributed in the hope that it will be useful,
 | ||||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | ||||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | ||||
| // GNU General Public License for more details.
 | ||||
| 
 | ||||
| // You should have received a copy of the GNU General Public License
 | ||||
| // along with Parity.  If not, see <http://www.gnu.org/licenses/>.
 | ||||
| 
 | ||||
| //! Smart contract based node filter.
 | ||||
| 
 | ||||
| extern crate ethcore; | ||||
| extern crate ethcore_util as util; | ||||
| extern crate ethcore_network as network; | ||||
| extern crate native_contracts; | ||||
| extern crate futures; | ||||
| #[cfg(test)] extern crate ethcore_io as io; | ||||
| #[macro_use] extern crate log; | ||||
| 
 | ||||
| use std::sync::Weak; | ||||
| use std::collections::HashMap; | ||||
| use native_contracts::PeerSet as Contract; | ||||
| use network::{NodeId, ConnectionFilter, ConnectionDirection}; | ||||
| use ethcore::client::{BlockChainClient, BlockId, ChainNotify}; | ||||
| use util::{Mutex, Address, H256, Bytes}; | ||||
| use futures::Future; | ||||
| 
 | ||||
| const MAX_CACHE_SIZE: usize = 4096; | ||||
| 
 | ||||
| /// Connection filter that uses a contract to manage permissions.
 | ||||
| pub struct NodeFilter { | ||||
| 	contract: Mutex<Option<Contract>>, | ||||
| 	client: Weak<BlockChainClient>, | ||||
| 	contract_address: Address, | ||||
| 	permission_cache: Mutex<HashMap<NodeId, bool>>, | ||||
| } | ||||
| 
 | ||||
| impl NodeFilter { | ||||
| 	/// Create a new instance. Accepts a contract address.
 | ||||
| 	pub fn new(client: Weak<BlockChainClient>, contract_address: Address) -> NodeFilter { | ||||
| 		NodeFilter { | ||||
| 			contract: Mutex::new(None), | ||||
| 			client: client, | ||||
| 			contract_address: contract_address, | ||||
| 			permission_cache: Mutex::new(HashMap::new()), | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	/// 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 mut cache = self.permission_cache.lock(); | ||||
| 		if let Some(res) = cache.get(connecting_id) { | ||||
| 			return *res; | ||||
| 		} | ||||
| 
 | ||||
| 		let mut contract = self.contract.lock(); | ||||
| 		if contract.is_none() { | ||||
| 			*contract = Some(Contract::new(self.contract_address)); | ||||
| 		} | ||||
| 
 | ||||
| 		let allowed = match (self.client.upgrade(), &*contract) { | ||||
| 			(Some(ref client), &Some(ref contract)) => { | ||||
| 				let own_low = H256::from_slice(&own_id[0..32]); | ||||
| 				let own_high = H256::from_slice(&own_id[32..64]); | ||||
| 				let id_low = H256::from_slice(&connecting_id[0..32]); | ||||
| 				let id_high = H256::from_slice(&connecting_id[32..64]); | ||||
| 				let allowed = contract.connection_allowed( | ||||
| 					|addr, data| futures::done(client.call_contract(BlockId::Latest, addr, data)), | ||||
| 					own_low, | ||||
| 					own_high, | ||||
| 					id_low, | ||||
| 					id_high, | ||||
| 				).wait().unwrap_or_else(|e| { | ||||
| 					debug!("Error callling peer set contract: {:?}", e); | ||||
| 					false | ||||
| 				}); | ||||
| 
 | ||||
| 				allowed | ||||
| 			} | ||||
| 			_ => false, | ||||
| 		}; | ||||
| 
 | ||||
| 		if cache.len() < MAX_CACHE_SIZE { | ||||
| 			cache.insert(*connecting_id, 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 util::{Address}; | ||||
| 	use network::{ConnectionDirection, ConnectionFilter, NodeId}; | ||||
| 	use io::IoChannel; | ||||
| 	use super::NodeFilter; | ||||
| 
 | ||||
| 	/// Contract code: https://gist.github.com/arkpar/467dbcc73cbb85b0997a7a10ffa0695f
 | ||||
| 	#[test] | ||||
| 	fn node_filter() { | ||||
| 		let contract_addr = Address::from_str("0000000000000000000000000000000000000005").unwrap(); | ||||
| 		let data = include_bytes!("../res/node_filter.json"); | ||||
| 		let spec = Spec::load(::std::env::temp_dir(), &data[..]).unwrap(); | ||||
| 		let client_db = Arc::new(::util::kvdb::in_memory(::ethcore::db::NUM_COLUMNS.unwrap_or(0))); | ||||
| 
 | ||||
| 		let client = Client::new( | ||||
| 			ClientConfig::default(), | ||||
| 			&spec, | ||||
| 			client_db, | ||||
| 			Arc::new(Miner::with_spec(&spec)), | ||||
| 			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(); | ||||
| 
 | ||||
| 		assert!(filter.connection_allowed(&self1, &node1, ConnectionDirection::Inbound)); | ||||
| 		assert!(filter.connection_allowed(&self1, &nodex, ConnectionDirection::Inbound)); | ||||
| 		filter.clear_cache(); | ||||
| 		assert!(filter.connection_allowed(&self2, &node1, ConnectionDirection::Inbound)); | ||||
| 		assert!(filter.connection_allowed(&self2, &node2, ConnectionDirection::Inbound)); | ||||
| 		assert!(!filter.connection_allowed(&self2, &nodex, ConnectionDirection::Inbound)); | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										35
									
								
								ethcore/res/null_morden_with_reward.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								ethcore/res/null_morden_with_reward.json
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,35 @@ | ||||
| { | ||||
| 	"name": "Morden", | ||||
| 	"engine": { | ||||
| 		"null": null | ||||
| 	}, | ||||
| 	"params": { | ||||
| 		"gasLimitBoundDivisor": "0x0400",  | ||||
| 		"accountStartNonce": "0x0", | ||||
| 		"maximumExtraDataSize": "0x20", | ||||
| 		"minGasLimit": "0x1388", | ||||
| 		"networkID" : "0x2", | ||||
| 		"blockReward": "0x4563918244F40000" | ||||
| 	}, | ||||
| 	"genesis": { | ||||
| 		"seal": { | ||||
| 			"ethereum": { | ||||
| 				"nonce": "0x00006d6f7264656e", | ||||
| 				"mixHash": "0x00000000000000000000000000000000000000647572616c65787365646c6578" | ||||
| 			} | ||||
| 		}, | ||||
| 		"difficulty": "0x20000", | ||||
| 		"author": "0x0000000000000000000000000000000000000000", | ||||
| 		"timestamp": "0x00", | ||||
| 		"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", | ||||
| 		"extraData": "0x", | ||||
| 		"gasLimit": "0x2fefd8" | ||||
| 	}, | ||||
| 	"accounts": { | ||||
| 		"0000000000000000000000000000000000000001": { "balance": "1", "nonce": "1048576", "builtin": { "name": "ecrecover", "pricing": { "linear": { "base": 3000, "word": 0 } } } }, | ||||
| 		"0000000000000000000000000000000000000002": { "balance": "1", "nonce": "1048576", "builtin": { "name": "sha256", "pricing": { "linear": { "base": 60, "word": 12 } } } }, | ||||
| 		"0000000000000000000000000000000000000003": { "balance": "1", "nonce": "1048576", "builtin": { "name": "ripemd160", "pricing": { "linear": { "base": 600, "word": 120 } } } }, | ||||
| 		"0000000000000000000000000000000000000004": { "balance": "1", "nonce": "1048576", "builtin": { "name": "identity", "pricing": { "linear": { "base": 15, "word": 3 } } } }, | ||||
| 		"102e61f5d8f9bc71d0ad4a084df4e65e05ce0e1c": { "balance": "1606938044258990275541962092341162602522202993782792835301376", "nonce": "1048576" } | ||||
| 	} | ||||
| } | ||||
| @ -21,7 +21,7 @@ use std::sync::Arc; | ||||
| use std::collections::HashSet; | ||||
| 
 | ||||
| use rlp::{UntrustedRlp, RlpStream, Encodable, Decodable, DecoderError}; | ||||
| use util::{Bytes, Address, Hashable, U256, H256, ordered_trie_root, SHA3_NULL_RLP}; | ||||
| use util::{Bytes, Address, Hashable, U256, H256, ordered_trie_root, SHA3_NULL_RLP, SHA3_EMPTY_LIST_RLP}; | ||||
| use util::error::{Mismatch, OutOfBounds}; | ||||
| 
 | ||||
| use basic_types::{LogBloom, Seal}; | ||||
| @ -107,7 +107,7 @@ pub struct BlockRefMut<'a> { | ||||
| 	/// State.
 | ||||
| 	pub state: &'a mut State<StateDB>, | ||||
| 	/// Traces.
 | ||||
| 	pub traces: &'a Option<Vec<Vec<FlatTrace>>>, | ||||
| 	pub traces: &'a mut Option<Vec<Vec<FlatTrace>>>, | ||||
| } | ||||
| 
 | ||||
| /// A set of immutable references to `ExecutedBlock` fields that are publicly accessible.
 | ||||
| @ -148,7 +148,7 @@ impl ExecutedBlock { | ||||
| 			uncles: &self.uncles, | ||||
| 			state: &mut self.state, | ||||
| 			receipts: &self.receipts, | ||||
| 			traces: &self.traces, | ||||
| 			traces: &mut self.traces, | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| @ -196,6 +196,9 @@ pub trait IsBlock { | ||||
| 
 | ||||
| 	/// Get all uncles in this block.
 | ||||
| 	fn uncles(&self) -> &[Header] { &self.block().uncles } | ||||
| 
 | ||||
| 	/// Get tracing enabled flag for this block.
 | ||||
| 	fn tracing_enabled(&self) -> bool { self.block().traces.is_some() } | ||||
| } | ||||
| 
 | ||||
| /// Trait for a object that has a state database.
 | ||||
| @ -395,6 +398,7 @@ impl<'x> OpenBlock<'x> { | ||||
| 		if let Err(e) = s.engine.on_close_block(&mut s.block) { | ||||
| 			warn!("Encountered error on closing the block: {}", e); | ||||
| 		} | ||||
| 		
 | ||||
| 		if let Err(e) = s.block.state.commit() { | ||||
| 			warn!("Encountered error on state commit: {}", e); | ||||
| 		} | ||||
| @ -429,7 +433,7 @@ impl<'x> OpenBlock<'x> { | ||||
| 			s.block.header.set_transactions_root(ordered_trie_root(s.block.transactions.iter().map(|e| e.rlp_bytes().into_vec()))); | ||||
| 		} | ||||
| 		let uncle_bytes = s.block.uncles.iter().fold(RlpStream::new_list(s.block.uncles.len()), |mut s, u| {s.append_raw(&u.rlp(Seal::With), 1); s} ).out(); | ||||
| 		if s.block.header.uncles_hash().is_zero() { | ||||
| 		if s.block.header.uncles_hash().is_zero() || s.block.header.uncles_hash() == &SHA3_EMPTY_LIST_RLP { | ||||
| 			s.block.header.set_uncles_hash(uncle_bytes.sha3()); | ||||
| 		} | ||||
| 		if s.block.header.receipts_root().is_zero() || s.block.header.receipts_root() == &SHA3_NULL_RLP { | ||||
|  | ||||
| @ -21,6 +21,7 @@ use std::sync::Arc; | ||||
| use std::mem; | ||||
| use itertools::Itertools; | ||||
| use bloomchain as bc; | ||||
| use heapsize::HeapSizeOf; | ||||
| use util::*; | ||||
| use rlp::*; | ||||
| use header::*; | ||||
|  | ||||
| @ -25,7 +25,8 @@ use engines::epoch::{Transition as EpochTransition}; | ||||
| use header::BlockNumber; | ||||
| use receipt::Receipt; | ||||
| 
 | ||||
| use util::{HeapSizeOf, H256, H264, U256}; | ||||
| use heapsize::HeapSizeOf; | ||||
| use util::{H256, H264, U256}; | ||||
| use util::kvdb::PREFIX_LEN as DB_PREFIX_LEN; | ||||
| 
 | ||||
| /// Represents index of extra data in database
 | ||||
|  | ||||
| @ -15,7 +15,7 @@ | ||||
| // along with Parity.  If not, see <http://www.gnu.org/licenses/>.
 | ||||
| 
 | ||||
| use bloomchain as bc; | ||||
| use util::HeapSizeOf; | ||||
| use heapsize::HeapSizeOf; | ||||
| use basic_types::LogBloom; | ||||
| 
 | ||||
| /// Helper structure representing bloom of the trace.
 | ||||
|  | ||||
| @ -16,7 +16,7 @@ | ||||
| 
 | ||||
| use bloomchain::group as bc; | ||||
| use rlp::*; | ||||
| use util::HeapSizeOf; | ||||
| use heapsize::HeapSizeOf; | ||||
| use super::Bloom; | ||||
| 
 | ||||
| /// Represents group of X consecutive blooms.
 | ||||
|  | ||||
| @ -15,7 +15,7 @@ | ||||
| // along with Parity.  If not, see <http://www.gnu.org/licenses/>.
 | ||||
| 
 | ||||
| use bloomchain::group as bc; | ||||
| use util::HeapSizeOf; | ||||
| use heapsize::HeapSizeOf; | ||||
| 
 | ||||
| /// Represents `BloomGroup` position in database.
 | ||||
| #[derive(PartialEq, Eq, Hash, Clone, Debug)] | ||||
|  | ||||
| @ -28,7 +28,8 @@ use header::{BlockNumber, Header as FullHeader}; | ||||
| use transaction::UnverifiedTransaction; | ||||
| use views; | ||||
| 
 | ||||
| use util::{Address, Hashable, H256, H2048, U256, HeapSizeOf}; | ||||
| use heapsize::HeapSizeOf; | ||||
| use util::{Address, Hashable, H256, H2048, U256}; | ||||
| use rlp::Rlp; | ||||
| 
 | ||||
| /// Owning header view.
 | ||||
|  | ||||
| @ -31,7 +31,6 @@ use error::{Error, TransactionError, BlockError}; | ||||
| use ethjson; | ||||
| use header::{Header, BlockNumber}; | ||||
| use spec::CommonParams; | ||||
| use state::CleanupMode; | ||||
| use transaction::UnverifiedTransaction; | ||||
| 
 | ||||
| use super::signer::EngineSigner; | ||||
| @ -522,7 +521,9 @@ impl Engine for AuthorityRound { | ||||
| 		let parent_hash = block.fields().header.parent_hash().clone(); | ||||
| 		::engines::common::push_last_hash(block, last_hashes.clone(), self, &parent_hash)?; | ||||
| 
 | ||||
| 		if !epoch_begin { return Ok(()) } | ||||
| 		// with immediate transitions, we don't use the epoch mechanism anyway.
 | ||||
| 		// the genesis is always considered an epoch, but we ignore it intentionally.
 | ||||
| 		if self.immediate_transitions || !epoch_begin { return Ok(()) } | ||||
| 
 | ||||
| 		// genesis is never a new block, but might as well check.
 | ||||
| 		let header = block.fields().header.clone(); | ||||
| @ -546,17 +547,7 @@ impl Engine for AuthorityRound { | ||||
| 
 | ||||
| 	/// Apply the block reward on finalisation of the block.
 | ||||
| 	fn on_close_block(&self, block: &mut ExecutedBlock) -> Result<(), Error> { | ||||
| 		let fields = block.fields_mut(); | ||||
| 		// Bestow block reward
 | ||||
| 		let reward = self.params().block_reward; | ||||
| 		let res = fields.state.add_balance(fields.header.author(), &reward, CleanupMode::NoEmpty) | ||||
| 			.map_err(::error::Error::from) | ||||
| 			.and_then(|_| fields.state.commit()); | ||||
| 		// Commit state so that we can actually figure out the state root.
 | ||||
| 		if let Err(ref e) = res { | ||||
| 			warn!("Encountered error on closing block: {}", e); | ||||
| 		} | ||||
| 		res | ||||
| 		::engines::common::bestow_block_reward(block, self) | ||||
| 	} | ||||
| 
 | ||||
| 	/// Check the number of seal fields.
 | ||||
|  | ||||
| @ -399,8 +399,9 @@ pub mod common { | ||||
| 	use transaction::SYSTEM_ADDRESS; | ||||
| 	use executive::Executive; | ||||
| 	use vm::{CallType, ActionParams, ActionValue, EnvInfo, LastHashes}; | ||||
| 	use trace::{NoopTracer, NoopVMTracer}; | ||||
| 	use trace::{NoopTracer, NoopVMTracer, Tracer, ExecutiveTracer, RewardType}; | ||||
| 	use state::Substate; | ||||
| 	use state::CleanupMode; | ||||
| 
 | ||||
| 	use util::*; | ||||
| 	use super::Engine; | ||||
| @ -469,4 +470,27 @@ pub mod common { | ||||
| 		} | ||||
| 		Ok(()) | ||||
| 	} | ||||
| 
 | ||||
| 	/// Trace rewards on closing block
 | ||||
| 	pub fn bestow_block_reward<E: Engine + ?Sized>(block: &mut ExecutedBlock, engine: &E) -> Result<(), Error> { | ||||
| 		let fields = block.fields_mut(); | ||||
| 		// Bestow block reward
 | ||||
| 		let reward = engine.params().block_reward; | ||||
| 		let res = fields.state.add_balance(fields.header.author(), &reward, CleanupMode::NoEmpty) | ||||
| 			.map_err(::error::Error::from) | ||||
| 			.and_then(|_| fields.state.commit()); | ||||
| 
 | ||||
| 		let block_author = fields.header.author().clone(); | ||||
| 		fields.traces.as_mut().map(|mut traces| { | ||||
|   			let mut tracer = ExecutiveTracer::default(); | ||||
|   			tracer.trace_reward(block_author, engine.params().block_reward, RewardType::Block); | ||||
|   			traces.push(tracer.drain()) | ||||
| 		}); | ||||
| 
 | ||||
| 		// Commit state so that we can actually figure out the state root.
 | ||||
| 		if let Err(ref e) = res { | ||||
| 			warn!("Encountered error on bestowing reward: {}", e); | ||||
| 		} | ||||
| 		res | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @ -17,10 +17,15 @@ | ||||
| use std::collections::BTreeMap; | ||||
| use util::Address; | ||||
| use builtin::Builtin; | ||||
| use block::{ExecutedBlock, IsBlock}; | ||||
| use util::U256; | ||||
| use engines::Engine; | ||||
| use spec::CommonParams; | ||||
| use evm::Schedule; | ||||
| use header::BlockNumber; | ||||
| use error::Error; | ||||
| use state::CleanupMode; | ||||
| use trace::{Tracer, ExecutiveTracer, RewardType}; | ||||
| 
 | ||||
| /// An engine which does not provide any consensus mechanism and does not seal blocks.
 | ||||
| pub struct NullEngine { | ||||
| @ -64,4 +69,48 @@ impl Engine for NullEngine { | ||||
| 	fn snapshot_components(&self) -> Option<Box<::snapshot::SnapshotComponents>> { | ||||
| 		Some(Box::new(::snapshot::PowSnapshot::new(10000, 10000))) | ||||
| 	} | ||||
| 
 | ||||
| 	fn on_close_block(&self, block: &mut ExecutedBlock) -> Result<(), Error> { | ||||
| 		if self.params.block_reward == U256::zero() { | ||||
| 			// we don't have to apply reward in this case
 | ||||
| 			return Ok(()) | ||||
| 		} | ||||
| 
 | ||||
| 		/// Block reward
 | ||||
| 		let tracing_enabled = block.tracing_enabled(); | ||||
| 		let fields = block.fields_mut(); | ||||
| 		let mut tracer = ExecutiveTracer::default(); | ||||
| 
 | ||||
| 		let result_block_reward = U256::from(1000000000); | ||||
| 		fields.state.add_balance( | ||||
| 			fields.header.author(), | ||||
| 			&result_block_reward, | ||||
| 			CleanupMode::NoEmpty | ||||
| 		)?; | ||||
| 
 | ||||
| 		if tracing_enabled { | ||||
| 			let block_author = fields.header.author().clone(); | ||||
| 			tracer.trace_reward(block_author, result_block_reward, RewardType::Block); | ||||
| 		} | ||||
| 
 | ||||
| 		/// Uncle rewards
 | ||||
| 		let result_uncle_reward = U256::from(10000000); | ||||
| 		for u in fields.uncles.iter() { | ||||
| 			let uncle_author = u.author().clone(); | ||||
| 			fields.state.add_balance( | ||||
| 				u.author(), | ||||
| 				&(result_uncle_reward), | ||||
| 				CleanupMode::NoEmpty | ||||
| 			)?; | ||||
| 			if tracing_enabled { | ||||
| 				tracer.trace_reward(uncle_author, result_uncle_reward, RewardType::Uncle); | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		fields.state.commit()?; | ||||
| 		if tracing_enabled { | ||||
| 			fields.traces.as_mut().map(|mut traces| traces.push(tracer.drain())); | ||||
| 		} | ||||
| 		Ok(()) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @ -40,7 +40,6 @@ use account_provider::AccountProvider; | ||||
| use block::*; | ||||
| use spec::CommonParams; | ||||
| use engines::{Engine, Seal, EngineError, ConstructedVerifier}; | ||||
| use state::CleanupMode; | ||||
| use io::IoService; | ||||
| use super::signer::EngineSigner; | ||||
| use super::validator_set::{ValidatorSet, SimpleList}; | ||||
| @ -541,17 +540,7 @@ impl Engine for Tendermint { | ||||
| 
 | ||||
| 	/// Apply the block reward on finalisation of the block.
 | ||||
| 	fn on_close_block(&self, block: &mut ExecutedBlock) -> Result<(), Error>{ | ||||
| 		let fields = block.fields_mut(); | ||||
| 		// Bestow block reward
 | ||||
| 		let reward = self.params().block_reward; | ||||
| 		let res = fields.state.add_balance(fields.header.author(), &reward, CleanupMode::NoEmpty) | ||||
| 			.map_err(::error::Error::from) | ||||
| 			.and_then(|_| fields.state.commit()); | ||||
| 		// Commit state so that we can actually figure out the state root.
 | ||||
| 		if let Err(ref e) = res { | ||||
| 			warn!("Encountered error on closing block: {}", e); | ||||
| 		} | ||||
| 		res | ||||
| 		::engines::common::bestow_block_reward(block, self) | ||||
| 	} | ||||
| 
 | ||||
| 	fn verify_block_basic(&self, header: &Header, _block: Option<&[u8]>) -> Result<(), Error> { | ||||
|  | ||||
| @ -16,7 +16,8 @@ | ||||
| 
 | ||||
| /// Preconfigured validator list.
 | ||||
| 
 | ||||
| use util::{H256, Address, HeapSizeOf}; | ||||
| use heapsize::HeapSizeOf; | ||||
| use util::{H256, Address}; | ||||
| 
 | ||||
| use engines::{Call, Engine}; | ||||
| use header::{BlockNumber, Header}; | ||||
|  | ||||
| @ -19,7 +19,8 @@ | ||||
| use std::str::FromStr; | ||||
| use std::sync::Arc; | ||||
| use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrdering}; | ||||
| use util::{Bytes, H256, Address, HeapSizeOf}; | ||||
| use heapsize::HeapSizeOf; | ||||
| use util::{Bytes, H256, Address}; | ||||
| 
 | ||||
| use engines::{Call, Engine}; | ||||
| use header::{Header, BlockNumber}; | ||||
|  | ||||
| @ -24,6 +24,7 @@ use block::*; | ||||
| use builtin::Builtin; | ||||
| use vm::EnvInfo; | ||||
| use error::{BlockError, Error, TransactionError}; | ||||
| use trace::{Tracer, ExecutiveTracer, RewardType}; | ||||
| use header::{Header, BlockNumber}; | ||||
| use state::CleanupMode; | ||||
| use spec::CommonParams; | ||||
| @ -285,38 +286,60 @@ impl Engine for Arc<Ethash> { | ||||
| 	/// Apply the block reward on finalisation of the block.
 | ||||
| 	/// This assumes that all uncles are valid uncles (i.e. of at least one generation before the current).
 | ||||
| 	fn on_close_block(&self, block: &mut ExecutedBlock) -> Result<(), Error> { | ||||
| 		use std::ops::Shr; | ||||
| 		let reward = self.params().block_reward; | ||||
| 		let tracing_enabled = block.tracing_enabled(); | ||||
| 		let fields = block.fields_mut(); | ||||
| 		let eras_rounds = self.ethash_params.ecip1017_era_rounds; | ||||
| 		let (eras, reward) = ecip1017_eras_block_reward(eras_rounds, reward, fields.header.number()); | ||||
| 		let mut tracer = ExecutiveTracer::default(); | ||||
| 
 | ||||
| 		// Bestow block reward
 | ||||
| 		let result_block_reward = reward + reward.shr(5) * U256::from(fields.uncles.len()); | ||||
| 		fields.state.add_balance( | ||||
| 			fields.header.author(), | ||||
| 			&(reward + reward / U256::from(32) * U256::from(fields.uncles.len())), | ||||
| 			&result_block_reward, | ||||
| 			CleanupMode::NoEmpty | ||||
| 		)?; | ||||
| 
 | ||||
| 		if tracing_enabled { | ||||
| 			let block_author = fields.header.author().clone(); | ||||
| 			tracer.trace_reward(block_author, result_block_reward, RewardType::Block); | ||||
| 		} | ||||
| 
 | ||||
| 		// Bestow uncle rewards
 | ||||
| 		let current_number = fields.header.number(); | ||||
| 		for u in fields.uncles.iter() { | ||||
| 			let uncle_author = u.author().clone(); | ||||
| 			let result_uncle_reward: U256; | ||||
| 
 | ||||
| 			if eras == 0 { | ||||
| 				result_uncle_reward = (reward * U256::from(8 + u.number() - current_number)).shr(3); | ||||
| 				fields.state.add_balance( | ||||
| 					u.author(), | ||||
| 					&(reward * U256::from(8 + u.number() - current_number) / U256::from(8)), | ||||
| 					&result_uncle_reward, | ||||
| 					CleanupMode::NoEmpty | ||||
| 				) | ||||
| 			} else { | ||||
| 				result_uncle_reward = reward.shr(5); | ||||
| 				fields.state.add_balance( | ||||
| 					u.author(), | ||||
| 					&(reward / U256::from(32)), | ||||
| 					&result_uncle_reward, | ||||
| 					CleanupMode::NoEmpty | ||||
| 				) | ||||
| 			}?; | ||||
| 
 | ||||
| 			// Trace uncle rewards
 | ||||
| 			if tracing_enabled { | ||||
| 				tracer.trace_reward(uncle_author, result_uncle_reward, RewardType::Uncle); | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		// Commit state so that we can actually figure out the state root.
 | ||||
| 		fields.state.commit()?; | ||||
| 		if tracing_enabled { | ||||
| 			fields.traces.as_mut().map(|mut traces| traces.push(tracer.drain())); | ||||
| 		} | ||||
| 		Ok(()) | ||||
| 	} | ||||
| 
 | ||||
|  | ||||
| @ -18,6 +18,7 @@ | ||||
| 
 | ||||
| use std::cmp; | ||||
| use std::cell::RefCell; | ||||
| use heapsize::HeapSizeOf; | ||||
| use util::*; | ||||
| use basic_types::{LogBloom, ZERO_LOGBLOOM}; | ||||
| use time::get_time; | ||||
|  | ||||
| @ -101,6 +101,7 @@ extern crate num; | ||||
| extern crate price_info; | ||||
| extern crate rand; | ||||
| extern crate rlp; | ||||
| extern crate heapsize; | ||||
| 
 | ||||
| #[macro_use] | ||||
| extern crate rlp_derive; | ||||
|  | ||||
| @ -105,7 +105,8 @@ use std::cmp::Ordering; | ||||
| use std::cmp; | ||||
| use std::collections::{HashSet, HashMap, BTreeSet, BTreeMap}; | ||||
| use linked_hash_map::LinkedHashMap; | ||||
| use util::{Address, H256, U256, HeapSizeOf}; | ||||
| use heapsize::HeapSizeOf; | ||||
| use util::{Address, H256, U256}; | ||||
| use table::Table; | ||||
| use transaction::*; | ||||
| use error::{Error, TransactionError}; | ||||
| @ -506,8 +507,6 @@ pub struct AccountDetails { | ||||
| 	pub balance: U256, | ||||
| } | ||||
| 
 | ||||
| /// Transactions with `gas > (gas_limit + gas_limit * Factor(in percents))` are not imported to the queue.
 | ||||
| const GAS_LIMIT_HYSTERESIS: usize = 200; // (100/GAS_LIMIT_HYSTERESIS) %
 | ||||
| /// Transaction with the same (sender, nonce) can be replaced only if
 | ||||
| /// `new_gas_price > old_gas_price + old_gas_price >> SHIFT`
 | ||||
| const GAS_PRICE_BUMP_SHIFT: usize = 3; // 2 = 25%, 3 = 12.5%, 4 = 6.25%
 | ||||
| @ -570,8 +569,8 @@ pub struct TransactionQueue { | ||||
| 	minimal_gas_price: U256, | ||||
| 	/// The maximum amount of gas any individual transaction may use.
 | ||||
| 	tx_gas_limit: U256, | ||||
| 	/// Current gas limit (block gas limit * factor). Transactions above the limit will not be accepted (default to !0)
 | ||||
| 	total_gas_limit: U256, | ||||
| 	/// Current gas limit (block gas limit). Transactions above the limit will not be accepted (default to !0)
 | ||||
| 	block_gas_limit: U256, | ||||
| 	/// Maximal time transaction may occupy the queue.
 | ||||
| 	/// When we reach `max_time_in_queue / 2^3` we re-validate
 | ||||
| 	/// account balance.
 | ||||
| @ -631,7 +630,7 @@ impl TransactionQueue { | ||||
| 		TransactionQueue { | ||||
| 			strategy, | ||||
| 			minimal_gas_price: U256::zero(), | ||||
| 			total_gas_limit: !U256::zero(), | ||||
| 			block_gas_limit: !U256::zero(), | ||||
| 			tx_gas_limit, | ||||
| 			max_time_in_queue: DEFAULT_QUEUING_PERIOD, | ||||
| 			current, | ||||
| @ -674,16 +673,10 @@ impl TransactionQueue { | ||||
| 		self.current.gas_price_entry_limit() | ||||
| 	} | ||||
| 
 | ||||
| 	/// Sets new gas limit. Transactions with gas slightly (`GAS_LIMIT_HYSTERESIS`) above the limit won't be imported.
 | ||||
| 	/// Sets new gas limit. Transactions with gas over the limit will not be accepted.
 | ||||
| 	/// Any transaction already imported to the queue is not affected.
 | ||||
| 	pub fn set_gas_limit(&mut self, gas_limit: U256) { | ||||
| 		let extra = gas_limit / U256::from(GAS_LIMIT_HYSTERESIS); | ||||
| 
 | ||||
| 		let total_gas_limit = match gas_limit.overflowing_add(extra) { | ||||
| 			(_, true) => !U256::zero(), | ||||
| 			(val, false) => val, | ||||
| 		}; | ||||
| 		self.total_gas_limit = total_gas_limit; | ||||
| 		self.block_gas_limit = gas_limit; | ||||
| 	} | ||||
| 
 | ||||
| 	/// Sets new total gas limit.
 | ||||
| @ -819,13 +812,13 @@ impl TransactionQueue { | ||||
| 			})); | ||||
| 		} | ||||
| 
 | ||||
| 		let gas_limit = cmp::min(self.tx_gas_limit, self.total_gas_limit); | ||||
| 		let gas_limit = cmp::min(self.tx_gas_limit, self.block_gas_limit); | ||||
| 		if tx.gas > gas_limit { | ||||
| 			trace!(target: "txqueue", | ||||
| 				"Dropping transaction above gas limit: {:?} ({} > min({}, {}))", | ||||
| 				tx.hash(), | ||||
| 				tx.gas, | ||||
| 				self.total_gas_limit, | ||||
| 				self.block_gas_limit, | ||||
| 				self.tx_gas_limit | ||||
| 			); | ||||
| 			return Err(Error::Transaction(TransactionError::GasLimitExceeded { | ||||
| @ -1922,13 +1915,13 @@ pub mod test { | ||||
| 		// given
 | ||||
| 		let mut txq = TransactionQueue::default(); | ||||
| 		txq.set_gas_limit(U256::zero()); | ||||
| 		assert_eq!(txq.total_gas_limit, U256::zero()); | ||||
| 		assert_eq!(txq.block_gas_limit, U256::zero()); | ||||
| 
 | ||||
| 		// when
 | ||||
| 		txq.set_gas_limit(!U256::zero()); | ||||
| 
 | ||||
| 		// then
 | ||||
| 		assert_eq!(txq.total_gas_limit, !U256::zero()); | ||||
| 		assert_eq!(txq.block_gas_limit, !U256::zero()); | ||||
| 	} | ||||
| 
 | ||||
| 	#[test] | ||||
| @ -1945,7 +1938,7 @@ pub mod test { | ||||
| 
 | ||||
| 		// then
 | ||||
| 		assert_eq!(unwrap_tx_err(res), TransactionError::GasLimitExceeded { | ||||
| 			limit: U256::from(50_250), // Should be 100.5% of set_gas_limit
 | ||||
| 			limit: U256::from(50_000), | ||||
| 			got: gas, | ||||
| 		}); | ||||
| 		let stats = txq.status(); | ||||
|  | ||||
| @ -100,6 +100,8 @@ pub struct CommonParams { | ||||
| 	pub block_reward: U256, | ||||
| 	/// Registrar contract address.
 | ||||
| 	pub registrar: Address, | ||||
| 	/// Node permission managing contract address.
 | ||||
| 	pub node_permission_contract: Option<Address>, | ||||
| } | ||||
| 
 | ||||
| impl CommonParams { | ||||
| @ -171,6 +173,7 @@ impl From<ethjson::spec::Params> for CommonParams { | ||||
| 			gas_limit_bound_divisor: p.gas_limit_bound_divisor.into(), | ||||
| 			block_reward: p.block_reward.map_or_else(U256::zero, Into::into), | ||||
| 			registrar: p.registrar.map_or_else(Address::new, Into::into), | ||||
| 			node_permission_contract: p.node_permission_contract.map(Into::into), | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| @ -343,7 +346,6 @@ impl Spec { | ||||
| 				}; | ||||
| 
 | ||||
| 				let mut substate = Substate::new(); | ||||
| 				state.kill_account(&address); | ||||
| 
 | ||||
| 				{ | ||||
| 					let mut exec = Executive::new(&mut state, &env_info, self.engine.as_ref()); | ||||
| @ -481,6 +483,9 @@ impl Spec { | ||||
| 	/// Create a new Spec which conforms to the Frontier-era Morden chain except that it's a NullEngine consensus.
 | ||||
| 	pub fn new_test() -> Spec { load_bundled!("null_morden") } | ||||
| 
 | ||||
| 	/// Create a new Spec which conforms to the Frontier-era Morden chain except that it's a NullEngine consensus with applying reward on block close.
 | ||||
| 	pub fn new_test_with_reward() -> Spec { load_bundled!("null_morden_with_reward") } | ||||
| 
 | ||||
| 	/// Create a new Spec which is a NullEngine consensus with a premine of address whose secret is sha3('').
 | ||||
| 	pub fn new_null() -> Spec { load_bundled!("null") } | ||||
| 
 | ||||
| @ -548,6 +553,9 @@ mod tests { | ||||
| 		let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap(); | ||||
| 		let state = State::from_existing(db.boxed_clone(), spec.state_root(), spec.engine.account_start_nonce(0), Default::default()).unwrap(); | ||||
| 		let expected = H256::from_str("0000000000000000000000000000000000000000000000000000000000000001").unwrap(); | ||||
| 		assert_eq!(state.storage_at(&Address::from_str("0000000000000000000000000000000000000005").unwrap(), &H256::zero()).unwrap(), expected); | ||||
| 		let address = Address::from_str("0000000000000000000000000000000000000005").unwrap(); | ||||
| 
 | ||||
| 		assert_eq!(state.storage_at(&address, &H256::zero()).unwrap(), expected); | ||||
| 		assert_eq!(state.balance(&address).unwrap(), 1.into()); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @ -17,6 +17,7 @@ | ||||
| pub mod helpers; | ||||
| mod client; | ||||
| mod evm; | ||||
| mod trace; | ||||
| 
 | ||||
| #[cfg(feature="ipc")] | ||||
| mod rpc; | ||||
|  | ||||
							
								
								
									
										206
									
								
								ethcore/src/tests/trace.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										206
									
								
								ethcore/src/tests/trace.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,206 @@ | ||||
| // Copyright 2015-2017 Parity Technologies (UK) Ltd.
 | ||||
| // This file is part of Parity.
 | ||||
| 
 | ||||
| // Parity is free software: you can redistribute it and/or modify
 | ||||
| // it under the terms of the GNU General Public License as published by
 | ||||
| // the Free Software Foundation, either version 3 of the License, or
 | ||||
| // (at your option) any later version.
 | ||||
| 
 | ||||
| // Parity is distributed in the hope that it will be useful,
 | ||||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | ||||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | ||||
| // GNU General Public License for more details.
 | ||||
| 
 | ||||
| // You should have received a copy of the GNU General Public License
 | ||||
| // along with Parity.  If not, see <http://www.gnu.org/licenses/>.
 | ||||
| 
 | ||||
| //! Client tests of tracing
 | ||||
| 
 | ||||
| use ethkey::KeyPair; | ||||
| use block::*; | ||||
| use util::*; | ||||
| use io::*; | ||||
| use spec::*; | ||||
| use client::*; | ||||
| use tests::helpers::*; | ||||
| use devtools::RandomTempPath; | ||||
| use client::{BlockChainClient, Client, ClientConfig}; | ||||
| use util::kvdb::{Database, DatabaseConfig}; | ||||
| use std::sync::Arc; | ||||
| use header::Header; | ||||
| use miner::Miner; | ||||
| use transaction::{Action, Transaction}; | ||||
| use views::BlockView; | ||||
| use trace::{RewardType, LocalizedTrace}; | ||||
| use trace::trace::Action::Reward; | ||||
| 
 | ||||
| #[test] | ||||
| fn can_trace_block_and_uncle_reward() { | ||||
| 	let dir = RandomTempPath::new(); | ||||
| 	let spec = Spec::new_test_with_reward(); | ||||
| 	let engine = &*spec.engine; | ||||
| 
 | ||||
| 	// Create client
 | ||||
| 	let db_config = DatabaseConfig::with_columns(::db::NUM_COLUMNS); | ||||
| 	let mut client_config = ClientConfig::default(); | ||||
| 	client_config.tracing.enabled = true; | ||||
| 	let client_db = Arc::new(Database::open(&db_config, dir.as_path().to_str().unwrap()).unwrap()); | ||||
| 	let client = Client::new( | ||||
| 		client_config, | ||||
| 		&spec, | ||||
| 		client_db, | ||||
| 		Arc::new(Miner::with_spec(&spec)), | ||||
| 		IoChannel::disconnected(), | ||||
| 	).unwrap(); | ||||
| 
 | ||||
| 	// Create test data:
 | ||||
| 	// genesis
 | ||||
| 	//    |
 | ||||
| 	// root_block
 | ||||
| 	//    |
 | ||||
| 	// parent_block
 | ||||
| 	//    |
 | ||||
| 	// block with transaction and uncle
 | ||||
| 
 | ||||
| 	let genesis_header = spec.genesis_header();    
 | ||||
| 	let mut db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap(); | ||||
| 	let mut rolling_timestamp = 40; | ||||
| 	let mut last_hashes = vec![]; | ||||
| 	let mut last_header = genesis_header.clone(); | ||||
| 	last_hashes.push(last_header.hash()); | ||||
| 
 | ||||
| 	let kp = KeyPair::from_secret_slice(&"".sha3()).unwrap(); | ||||
| 	let author = kp.address(); | ||||
| 
 | ||||
| 	// Add root block first
 | ||||
| 	let mut root_block = OpenBlock::new( | ||||
| 		engine, | ||||
| 		Default::default(), | ||||
| 		false, | ||||
| 		db, | ||||
| 		&last_header, | ||||
| 		Arc::new(last_hashes.clone()), | ||||
| 		author.clone(), | ||||
| 		(3141562.into(), 31415620.into()), | ||||
| 		vec![], | ||||
| 		false, | ||||
| 	).unwrap(); | ||||
| 	root_block.set_difficulty(U256::from(0x20000)); | ||||
| 	rolling_timestamp += 10; | ||||
| 	root_block.set_timestamp(rolling_timestamp); | ||||
| 
 | ||||
| 	let root_block = root_block.close_and_lock().seal(engine, vec![]).unwrap();	
 | ||||
| 
 | ||||
| 	if let Err(e) = client.import_block(root_block.rlp_bytes()) { | ||||
| 		panic!("error importing block which is valid by definition: {:?}", e); | ||||
| 	} | ||||
| 
 | ||||
| 	last_header = BlockView::new(&root_block.rlp_bytes()).header(); | ||||
| 	let root_header = last_header.clone(); | ||||
| 	db = root_block.drain(); | ||||
| 
 | ||||
| 	last_hashes.push(last_header.hash()); | ||||
| 
 | ||||
| 	// Add parent block
 | ||||
| 	let mut parent_block = OpenBlock::new( | ||||
| 		engine, | ||||
| 		Default::default(), | ||||
| 		false, | ||||
| 		db, | ||||
| 		&last_header, | ||||
| 		Arc::new(last_hashes.clone()), | ||||
| 		author.clone(), | ||||
| 		(3141562.into(), 31415620.into()), | ||||
| 		vec![], | ||||
| 		false, | ||||
| 	).unwrap(); | ||||
| 	parent_block.set_difficulty(U256::from(0x20000)); | ||||
| 	rolling_timestamp += 10; | ||||
| 	parent_block.set_timestamp(rolling_timestamp); | ||||
| 
 | ||||
| 	let parent_block = parent_block.close_and_lock().seal(engine, vec![]).unwrap();	
 | ||||
| 
 | ||||
| 	if let Err(e) = client.import_block(parent_block.rlp_bytes()) { | ||||
| 		panic!("error importing block which is valid by definition: {:?}", e); | ||||
| 	} | ||||
| 
 | ||||
| 	last_header = BlockView::new(&parent_block.rlp_bytes()).header(); | ||||
| 	db = parent_block.drain(); | ||||
| 
 | ||||
| 	last_hashes.push(last_header.hash()); | ||||
| 
 | ||||
| 	// Add testing block with transaction and uncle
 | ||||
| 	let mut block = OpenBlock::new( | ||||
| 		engine, 
 | ||||
| 		Default::default(), 
 | ||||
| 		true, 
 | ||||
| 		db, 
 | ||||
| 		&last_header, 
 | ||||
| 		Arc::new(last_hashes.clone()), | ||||
| 		author.clone(), | ||||
| 		(3141562.into(), 31415620.into()), 
 | ||||
| 		vec![], | ||||
| 		false | ||||
| 		).unwrap(); | ||||
| 	block.set_difficulty(U256::from(0x20000)); | ||||
| 	rolling_timestamp += 10; | ||||
| 	block.set_timestamp(rolling_timestamp); | ||||
| 
 | ||||
| 	let mut n = 0; | ||||
| 	for _ in 0..1 { | ||||
| 		block.push_transaction(Transaction { | ||||
| 			nonce: n.into(), | ||||
| 			gas_price: 10000.into(), | ||||
| 			gas: 100000.into(), | ||||
| 			action: Action::Create, | ||||
| 			data: vec![], | ||||
| 			value: U256::zero(), | ||||
| 		}.sign(kp.secret(), Some(spec.network_id())), None).unwrap(); | ||||
| 		n += 1; | ||||
| 	} | ||||
| 
 | ||||
| 	let mut uncle = Header::new(); | ||||
| 	let uncle_author: Address = "ef2d6d194084c2de36e0dabfce45d046b37d1106".into(); | ||||
| 	uncle.set_author(uncle_author); | ||||
| 	uncle.set_parent_hash(root_header.hash()); | ||||
| 	uncle.set_gas_limit(U256::from(50_000)); | ||||
| 	uncle.set_number(root_header.number() + 1); | ||||
| 	uncle.set_timestamp(rolling_timestamp); | ||||
| 	block.push_uncle(uncle).unwrap(); | ||||
| 
 | ||||
| 	let block = block.close_and_lock().seal(engine, vec![]).unwrap(); | ||||
| 
 | ||||
| 	let res = client.import_block(block.rlp_bytes()); | ||||
| 	if res.is_err() { | ||||
| 		panic!("error importing block: {:#?}", res.err().unwrap());        
 | ||||
| 	} | ||||
| 
 | ||||
| 	block.drain(); | ||||
| 	client.flush_queue(); | ||||
| 	client.import_verified_blocks(); | ||||
| 
 | ||||
| 	// Test0. Check overall filter
 | ||||
| 	let filter = TraceFilter { | ||||
| 		range: (BlockId::Number(1)..BlockId::Number(3)), | ||||
| 		from_address: vec![], | ||||
| 		to_address: vec![], | ||||
| 	}; | ||||
| 
 | ||||
| 	let traces = client.filter_traces(filter); | ||||
| 	assert!(traces.is_some(), "Filtered traces should be present"); | ||||
| 	let traces_vec = traces.unwrap(); | ||||
| 	let block_reward_traces: Vec<LocalizedTrace> = traces_vec.clone().into_iter().filter(|trace| match (trace).action { | ||||
| 		Reward(ref a) => a.reward_type == RewardType::Block, | ||||
| 		_ => false, | ||||
| 	}).collect(); | ||||
| 	assert_eq!(block_reward_traces.len(), 3); | ||||
| 	let uncle_reward_traces: Vec<LocalizedTrace> = traces_vec.clone().into_iter().filter(|trace| match (trace).action { | ||||
| 		Reward(ref a) => a.reward_type == RewardType::Uncle, | ||||
| 		_ => false, | ||||
| 	}).collect(); | ||||
| 	assert_eq!(uncle_reward_traces.len(), 1); | ||||
| 
 | ||||
| 	// Test1. Check block filter
 | ||||
| 	let traces = client.block_traces(BlockId::Number(3)); | ||||
| 	assert_eq!(traces.unwrap().len(), 3);	
 | ||||
| } | ||||
| @ -20,7 +20,8 @@ use std::collections::{HashMap, VecDeque}; | ||||
| use std::sync::Arc; | ||||
| use bloomchain::{Number, Config as BloomConfig}; | ||||
| use bloomchain::group::{BloomGroupDatabase, BloomGroupChain, GroupPosition, BloomGroup}; | ||||
| use util::{H256, H264, KeyValueDB, DBTransaction, RwLock, HeapSizeOf}; | ||||
| use heapsize::HeapSizeOf; | ||||
| use util::{H256, H264, KeyValueDB, DBTransaction, RwLock}; | ||||
| use header::BlockNumber; | ||||
| use trace::{LocalizedTrace, Config, Filter, Database as TraceDatabase, ImportRequest, DatabaseExtras}; | ||||
| use db::{self, Key, Writable, Readable, CacheUpdatePolicy}; | ||||
| @ -215,8 +216,11 @@ impl<T> TraceDB<T> where T: DatabaseExtras { | ||||
| 		block_number: BlockNumber, | ||||
| 		tx_number: usize | ||||
| 	) -> Vec<LocalizedTrace> { | ||||
| 		let tx_hash = self.extras.transaction_hash(block_number, tx_number) | ||||
| 			.expect("Expected to find transaction hash. Database is probably corrupted"); | ||||
| 		let (trace_tx_number, trace_tx_hash) = match self.extras.transaction_hash(block_number, tx_number) { | ||||
| 			Some(hash) => (Some(tx_number), Some(hash.clone())), | ||||
| 			//None means trace without transaction (reward)
 | ||||
| 			None => (None, None), | ||||
| 		}; | ||||
| 
 | ||||
| 		let flat_traces: Vec<FlatTrace> = traces.into(); | ||||
| 		flat_traces.into_iter() | ||||
| @ -227,8 +231,8 @@ impl<T> TraceDB<T> where T: DatabaseExtras { | ||||
| 						result: trace.result, | ||||
| 						subtraces: trace.subtraces, | ||||
| 						trace_address: trace.trace_address.into_iter().collect(), | ||||
| 						transaction_number: tx_number, | ||||
| 						transaction_hash: tx_hash.clone(), | ||||
| 						transaction_number: trace_tx_number, | ||||
| 						transaction_hash: trace_tx_hash, | ||||
| 						block_number: block_number, | ||||
| 						block_hash: block_hash | ||||
| 					}), | ||||
| @ -321,8 +325,8 @@ impl<T> TraceDatabase for TraceDB<T> where T: DatabaseExtras { | ||||
| 						result: trace.result, | ||||
| 						subtraces: trace.subtraces, | ||||
| 						trace_address: trace.trace_address.into_iter().collect(), | ||||
| 						transaction_number: tx_position, | ||||
| 						transaction_hash: tx_hash, | ||||
| 						transaction_number: Some(tx_position), | ||||
| 						transaction_hash: Some(tx_hash), | ||||
| 						block_number: block_number, | ||||
| 						block_hash: block_hash, | ||||
| 					} | ||||
| @ -345,8 +349,8 @@ impl<T> TraceDatabase for TraceDB<T> where T: DatabaseExtras { | ||||
| 						result: trace.result, | ||||
| 						subtraces: trace.subtraces, | ||||
| 						trace_address: trace.trace_address.into_iter().collect(), | ||||
| 						transaction_number: tx_position, | ||||
| 						transaction_hash: tx_hash.clone(), | ||||
| 						transaction_number: Some(tx_position), | ||||
| 						transaction_hash: Some(tx_hash.clone()), | ||||
| 						block_number: block_number, | ||||
| 						block_hash: block_hash | ||||
| 					}) | ||||
| @ -363,8 +367,11 @@ impl<T> TraceDatabase for TraceDB<T> where T: DatabaseExtras { | ||||
| 						.map(Into::<Vec<FlatTrace>>::into) | ||||
| 						.enumerate() | ||||
| 						.flat_map(|(tx_position, traces)| { | ||||
| 							let tx_hash = self.extras.transaction_hash(block_number, tx_position) | ||||
| 								.expect("Expected to find transaction hash. Database is probably corrupted"); | ||||
| 							let (trace_tx_number, trace_tx_hash) = match self.extras.transaction_hash(block_number, tx_position) { | ||||
| 								Some(hash) => (Some(tx_position), Some(hash.clone())), | ||||
| 								//None means trace without transaction (reward)
 | ||||
| 								None => (None, None), | ||||
| 							}; | ||||
| 
 | ||||
| 							traces.into_iter() | ||||
| 								.map(|trace| LocalizedTrace { | ||||
| @ -372,8 +379,8 @@ impl<T> TraceDatabase for TraceDB<T> where T: DatabaseExtras { | ||||
| 									result: trace.result, | ||||
| 									subtraces: trace.subtraces, | ||||
| 									trace_address: trace.trace_address.into_iter().collect(), | ||||
| 									transaction_number: tx_position, | ||||
| 									transaction_hash: tx_hash.clone(), | ||||
| 									transaction_number: trace_tx_number, | ||||
| 									transaction_hash: trace_tx_hash, | ||||
| 									block_number: block_number, | ||||
| 									block_hash: block_hash, | ||||
| 								}) | ||||
| @ -543,8 +550,8 @@ mod tests { | ||||
| 			result: Res::FailedCall(TraceError::OutOfGas), | ||||
| 			trace_address: vec![], | ||||
| 			subtraces: 0, | ||||
| 			transaction_number: 0, | ||||
| 			transaction_hash: tx_hash, | ||||
| 			transaction_number: Some(0), | ||||
| 			transaction_hash: Some(tx_hash), | ||||
| 			block_number: block_number, | ||||
| 			block_hash: block_hash, | ||||
| 		} | ||||
|  | ||||
| @ -18,7 +18,7 @@ | ||||
| 
 | ||||
| use util::{Bytes, Address, U256}; | ||||
| use vm::ActionParams; | ||||
| use trace::trace::{Call, Create, Action, Res, CreateResult, CallResult, VMTrace, VMOperation, VMExecutedOperation, MemoryDiff, StorageDiff, Suicide}; | ||||
| use trace::trace::{Call, Create, Action, Res, CreateResult, CallResult, VMTrace, VMOperation, VMExecutedOperation, MemoryDiff, StorageDiff, Suicide, Reward, RewardType}; | ||||
| use trace::{Tracer, VMTracer, FlatTrace, TraceError}; | ||||
| 
 | ||||
| /// Simple executive tracer. Traces all calls and creates. Ignores delegatecalls.
 | ||||
| @ -151,15 +151,22 @@ impl Tracer for ExecutiveTracer { | ||||
| 	fn trace_suicide(&mut self, address: Address, balance: U256, refund_address: Address) { | ||||
| 		let trace = FlatTrace { | ||||
| 			subtraces: 0, | ||||
| 			action: Action::Suicide(Suicide { | ||||
| 				address: address, | ||||
| 				refund_address: refund_address, | ||||
| 				balance: balance, | ||||
| 			}), | ||||
| 			action: Action::Suicide(Suicide { address, refund_address, balance } ), | ||||
| 			result: Res::None, | ||||
| 			trace_address: Default::default(), | ||||
| 		}; | ||||
| 		debug!(target: "trace", "Traced failed suicide {:?}", trace); | ||||
| 		debug!(target: "trace", "Traced suicide {:?}", trace); | ||||
| 		self.traces.push(trace); | ||||
| 	} | ||||
| 	
 | ||||
| 	fn trace_reward(&mut self, author: Address, value: U256, reward_type: RewardType) { | ||||
| 		let trace = FlatTrace { | ||||
| 			subtraces: 0, | ||||
| 			action: Action::Reward(Reward { author, value, reward_type } ), | ||||
| 			result: Res::None, | ||||
| 			trace_address: Default::default(), | ||||
| 		}; | ||||
| 		debug!(target: "trace", "Traced reward {:?}", trace); | ||||
| 		self.traces.push(trace); | ||||
| 	} | ||||
| 
 | ||||
|  | ||||
| @ -33,7 +33,7 @@ pub use self::localized::LocalizedTrace; | ||||
| 
 | ||||
| pub use self::types::{filter, flat, localized, trace}; | ||||
| pub use self::types::error::Error as TraceError; | ||||
| pub use self::types::trace::{VMTrace, VMOperation, VMExecutedOperation, MemoryDiff, StorageDiff}; | ||||
| pub use self::types::trace::{VMTrace, VMOperation, VMExecutedOperation, MemoryDiff, StorageDiff, RewardType}; | ||||
| pub use self::types::flat::{FlatTrace, FlatTransactionTraces, FlatBlockTraces}; | ||||
| pub use self::types::filter::{Filter, AddressesFilter}; | ||||
| 
 | ||||
| @ -81,6 +81,9 @@ pub trait Tracer: Send { | ||||
| 	/// Stores suicide info.
 | ||||
| 	fn trace_suicide(&mut self, address: Address, balance: U256, refund_address: Address); | ||||
| 
 | ||||
| 	/// Stores reward info.
 | ||||
| 	fn trace_reward(&mut self, author: Address, value: U256, reward_type: RewardType); | ||||
| 
 | ||||
| 	/// Spawn subtracer which will be used to trace deeper levels of execution.
 | ||||
| 	fn subtracer(&self) -> Self where Self: Sized; | ||||
| 
 | ||||
|  | ||||
| @ -19,7 +19,7 @@ | ||||
| use util::{Bytes, Address, U256}; | ||||
| use vm::ActionParams; | ||||
| use trace::{Tracer, VMTracer, FlatTrace, TraceError}; | ||||
| use trace::trace::{Call, Create, VMTrace}; | ||||
| use trace::trace::{Call, Create, VMTrace, RewardType}; | ||||
| 
 | ||||
| /// Nonoperative tracer. Does not trace anything.
 | ||||
| pub struct NoopTracer; | ||||
| @ -58,6 +58,9 @@ impl Tracer for NoopTracer { | ||||
| 	fn trace_suicide(&mut self, _address: Address, _balance: U256, _refund_address: Address) { | ||||
| 	} | ||||
| 
 | ||||
| 	fn trace_reward(&mut self, _: Address, _: U256, _: RewardType) { | ||||
| 	} | ||||
| 
 | ||||
| 	fn subtracer(&self) -> Self { | ||||
| 		NoopTracer | ||||
| 	} | ||||
|  | ||||
| @ -113,7 +113,7 @@ impl Filter { | ||||
| 				let from_matches = self.from_address.matches(&call.from); | ||||
| 				let to_matches = self.to_address.matches(&call.to); | ||||
| 				from_matches && to_matches | ||||
| 			} | ||||
| 			}, | ||||
| 			Action::Create(ref create) => { | ||||
| 				let from_matches = self.from_address.matches(&create.from); | ||||
| 
 | ||||
| @ -128,7 +128,10 @@ impl Filter { | ||||
| 				let from_matches = self.from_address.matches(&suicide.address); | ||||
| 				let to_matches = self.to_address.matches(&suicide.refund_address); | ||||
| 				from_matches && to_matches | ||||
| 			} | ||||
| 			}, | ||||
| 			Action::Reward(ref reward) => { | ||||
| 				self.to_address.matches(&reward.author) | ||||
| 			}, | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| @ -138,9 +141,9 @@ mod tests { | ||||
| 	use util::Address; | ||||
| 	use util::sha3::Hashable; | ||||
| 	use bloomable::Bloomable; | ||||
| 	use trace::trace::{Action, Call, Res, Create, CreateResult, Suicide}; | ||||
| 	use trace::trace::{Action, Call, Res, Create, CreateResult, Suicide, Reward}; | ||||
| 	use trace::flat::FlatTrace; | ||||
| 	use trace::{Filter, AddressesFilter, TraceError}; | ||||
| 	use trace::{Filter, AddressesFilter, TraceError, RewardType}; | ||||
| 	use evm::CallType; | ||||
| 
 | ||||
| 	#[test] | ||||
| @ -341,5 +344,24 @@ mod tests { | ||||
| 		assert!(f4.matches(&trace)); | ||||
| 		assert!(f5.matches(&trace)); | ||||
| 		assert!(!f6.matches(&trace)); | ||||
| 
 | ||||
| 		let trace = FlatTrace { | ||||
| 			action: Action::Reward(Reward { | ||||
| 				author: 2.into(), | ||||
| 				value: 100.into(), | ||||
| 				reward_type: RewardType::Block, | ||||
| 			}), | ||||
| 			result: Res::None, | ||||
| 			trace_address: vec![].into_iter().collect(), | ||||
| 			subtraces: 0 | ||||
| 		}; | ||||
| 
 | ||||
| 		assert!(f0.matches(&trace)); | ||||
| 		assert!(f1.matches(&trace)); | ||||
| 		assert!(f2.matches(&trace)); | ||||
| 		assert!(f3.matches(&trace)); | ||||
| 		assert!(f4.matches(&trace)); | ||||
| 		assert!(f5.matches(&trace)); | ||||
| 		assert!(!f6.matches(&trace)); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @ -18,7 +18,7 @@ | ||||
| 
 | ||||
| use std::collections::VecDeque; | ||||
| use rlp::*; | ||||
| use util::HeapSizeOf; | ||||
| use heapsize::HeapSizeOf; | ||||
| use basic_types::LogBloom; | ||||
| use super::trace::{Action, Res}; | ||||
| 
 | ||||
| @ -138,8 +138,9 @@ impl Into<Vec<FlatTransactionTraces>> for FlatBlockTraces { | ||||
| mod tests { | ||||
| 	use rlp::*; | ||||
| 	use super::{FlatBlockTraces, FlatTransactionTraces, FlatTrace}; | ||||
| 	use trace::trace::{Action, Res, CallResult, Call, Suicide}; | ||||
| 	use trace::trace::{Action, Res, CallResult, Call, Suicide, Reward}; | ||||
| 	use evm::CallType; | ||||
| 	use trace::RewardType; | ||||
| 
 | ||||
| 	#[test] | ||||
| 	fn encode_flat_transaction_traces() { | ||||
| @ -214,9 +215,32 @@ mod tests { | ||||
| 			subtraces: 0, | ||||
| 		}; | ||||
| 
 | ||||
| 		let flat_trace3 = FlatTrace { | ||||
| 			action: Action::Reward(Reward { | ||||
| 				author: "412fda7643b37d436cb40628f6dbbb80a07267ed".parse().unwrap(), | ||||
| 				value: 10.into(), | ||||
| 				reward_type: RewardType::Uncle, | ||||
| 			}), | ||||
| 			result: Res::None, | ||||
| 			trace_address: vec![0].into_iter().collect(), | ||||
| 			subtraces: 0, | ||||
| 		}; | ||||
| 
 | ||||
| 		let flat_trace4 = FlatTrace { | ||||
| 			action: Action::Reward(Reward { | ||||
| 				author: "412fda7643b37d436cb40628f6dbbb80a07267ed".parse().unwrap(), | ||||
| 				value: 10.into(), | ||||
| 				reward_type: RewardType::Block, | ||||
| 			}), | ||||
| 			result: Res::None, | ||||
| 			trace_address: vec![0].into_iter().collect(), | ||||
| 			subtraces: 0, | ||||
| 		}; | ||||
| 
 | ||||
| 		let block_traces = FlatBlockTraces(vec![ | ||||
| 			FlatTransactionTraces(vec![flat_trace]), | ||||
| 			FlatTransactionTraces(vec![flat_trace1, flat_trace2]) | ||||
| 			FlatTransactionTraces(vec![flat_trace1, flat_trace2]), | ||||
| 			FlatTransactionTraces(vec![flat_trace3, flat_trace4]) | ||||
| 		]); | ||||
| 
 | ||||
| 		let encoded = ::rlp::encode(&block_traces); | ||||
|  | ||||
| @ -34,9 +34,9 @@ pub struct LocalizedTrace { | ||||
| 	/// [index in root, index in first CALL, index in second CALL, ...]
 | ||||
| 	pub trace_address: Vec<usize>, | ||||
| 	/// Transaction number within the block.
 | ||||
| 	pub transaction_number: usize, | ||||
| 	pub transaction_number: Option<usize>, | ||||
| 	/// Signed transaction hash.
 | ||||
| 	pub transaction_hash: H256, | ||||
| 	pub transaction_hash: Option<H256>, | ||||
| 	/// Block number.
 | ||||
| 	pub block_number: BlockNumber, | ||||
| 	/// Block hash.
 | ||||
|  | ||||
| @ -128,6 +128,77 @@ impl Create { | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| /// Reward type.
 | ||||
| #[derive(Debug, PartialEq, Clone)] | ||||
| #[cfg_attr(feature = "ipc", binary)] | ||||
| pub enum RewardType { | ||||
| 	/// Block
 | ||||
| 	Block, | ||||
| 	/// Uncle
 | ||||
| 	Uncle, | ||||
| } | ||||
| 
 | ||||
| impl Encodable for RewardType { | ||||
| 	fn rlp_append(&self, s: &mut RlpStream) { | ||||
| 		let v = match *self { | ||||
| 			RewardType::Block => 0u32, | ||||
| 			RewardType::Uncle => 1, | ||||
| 		}; | ||||
| 		Encodable::rlp_append(&v, s); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| impl Decodable for RewardType { | ||||
| 	fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> { | ||||
| 		rlp.as_val().and_then(|v| Ok(match v { | ||||
| 			0u32 => RewardType::Block, | ||||
| 			1 => RewardType::Uncle, | ||||
| 			_ => return Err(DecoderError::Custom("Invalid value of RewardType item")), | ||||
| 		})) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| /// Reward action
 | ||||
| #[derive(Debug, Clone, PartialEq)] | ||||
| #[cfg_attr(feature = "ipc", binary)] | ||||
| pub struct Reward { | ||||
| 	/// Author's address.
 | ||||
| 	pub author: Address, | ||||
| 	/// Reward amount.
 | ||||
| 	pub value: U256, | ||||
| 	/// Reward type.
 | ||||
| 	pub reward_type: RewardType, | ||||
| } | ||||
| 
 | ||||
| impl Reward { | ||||
| 	/// Return reward action bloom.
 | ||||
| 	pub fn bloom(&self) -> LogBloom { | ||||
| 		LogBloom::from_bloomed(&self.author.sha3()) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| impl Encodable for Reward { | ||||
| 	fn rlp_append(&self, s: &mut RlpStream) { | ||||
| 		s.begin_list(3); | ||||
| 		s.append(&self.author); | ||||
| 		s.append(&self.value); | ||||
| 		s.append(&self.reward_type); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| impl Decodable for Reward { | ||||
| 	fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> { | ||||
| 		let res = Reward { | ||||
| 			author: rlp.val_at(0)?, | ||||
| 			value: rlp.val_at(1)?, | ||||
| 			reward_type: rlp.val_at(2)?, | ||||
| 		}; | ||||
| 
 | ||||
| 		Ok(res) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| /// Suicide action.
 | ||||
| #[derive(Debug, Clone, PartialEq, RlpEncodable, RlpDecodable)] | ||||
| #[cfg_attr(feature = "ipc", binary)] | ||||
| @ -158,6 +229,8 @@ pub enum Action { | ||||
| 	Create(Create), | ||||
| 	/// Suicide.
 | ||||
| 	Suicide(Suicide), | ||||
| 	/// Reward
 | ||||
| 	Reward(Reward), | ||||
| } | ||||
| 
 | ||||
| impl Encodable for Action { | ||||
| @ -175,7 +248,12 @@ impl Encodable for Action { | ||||
| 			Action::Suicide(ref suicide) => { | ||||
| 				s.append(&2u8); | ||||
| 				s.append(suicide); | ||||
| 			}, | ||||
| 			Action::Reward(ref reward) => { | ||||
| 				s.append(&3u8); | ||||
| 				s.append(reward); | ||||
| 			} | ||||
| 
 | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| @ -187,6 +265,7 @@ impl Decodable for Action { | ||||
| 			0 => rlp.val_at(1).map(Action::Call), | ||||
| 			1 => rlp.val_at(1).map(Action::Create), | ||||
| 			2 => rlp.val_at(1).map(Action::Suicide), | ||||
| 			3 => rlp.val_at(1).map(Action::Reward), | ||||
| 			_ => Err(DecoderError::Custom("Invalid action type.")), | ||||
| 		} | ||||
| 	} | ||||
| @ -199,6 +278,7 @@ impl Action { | ||||
| 			Action::Call(ref call) => call.bloom(), | ||||
| 			Action::Create(ref create) => create.bloom(), | ||||
| 			Action::Suicide(ref suicide) => suicide.bloom(), | ||||
| 			Action::Reward(ref reward) => reward.bloom(), | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @ -19,7 +19,8 @@ | ||||
| use std::ops::Deref; | ||||
| use rlp::*; | ||||
| use util::sha3::Hashable; | ||||
| use util::{H256, Address, U256, Bytes, HeapSizeOf}; | ||||
| use heapsize::HeapSizeOf; | ||||
| use util::{H256, Address, U256, Bytes}; | ||||
| use ethkey::{Signature, Secret, Public, recover, public_to_address, Error as EthkeyError}; | ||||
| use error::*; | ||||
| use evm::Schedule; | ||||
|  | ||||
| @ -19,7 +19,8 @@ | ||||
| use engines::Engine; | ||||
| use error::Error; | ||||
| 
 | ||||
| use util::{HeapSizeOf, H256, U256}; | ||||
| use heapsize::HeapSizeOf; | ||||
| use util::{H256, U256}; | ||||
| 
 | ||||
| pub use self::blocks::Blocks; | ||||
| pub use self::headers::Headers; | ||||
| @ -72,7 +73,8 @@ pub mod blocks { | ||||
| 	use header::Header; | ||||
| 	use verification::{PreverifiedBlock, verify_block_basic, verify_block_unordered}; | ||||
| 
 | ||||
| 	use util::{Bytes, HeapSizeOf, H256, U256}; | ||||
| 	use heapsize::HeapSizeOf; | ||||
| 	use util::{Bytes, H256, U256}; | ||||
| 
 | ||||
| 	/// A mode for verifying blocks.
 | ||||
| 	pub struct Blocks; | ||||
|  | ||||
| @ -22,6 +22,7 @@ use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering as AtomicOrdering}; | ||||
| use std::sync::{Condvar as SCondvar, Mutex as SMutex, Arc}; | ||||
| use std::cmp; | ||||
| use std::collections::{VecDeque, HashSet, HashMap}; | ||||
| use heapsize::HeapSizeOf; | ||||
| use util::*; | ||||
| use io::*; | ||||
| use error::*; | ||||
|  | ||||
| @ -22,6 +22,7 @@ | ||||
| //! 3. Final verification against the blockchain done before enactment.
 | ||||
| 
 | ||||
| use std::collections::HashSet; | ||||
| use heapsize::HeapSizeOf; | ||||
| use util::*; | ||||
| use engines::Engine; | ||||
| use error::{BlockError, Error}; | ||||
|  | ||||
| @ -10,6 +10,7 @@ rlp_derive = { path = "../../util/rlp_derive" } | ||||
| ethcore-util = { path = "../../util" } | ||||
| ethjson = { path = "../../json" } | ||||
| bloomable = { path = "../../util/bloomable" } | ||||
| heapsize = "0.4" | ||||
| 
 | ||||
| [dev-dependencies] | ||||
| rustc-hex= "1.0" | ||||
|  | ||||
| @ -22,6 +22,7 @@ extern crate rlp; | ||||
| #[macro_use] | ||||
| extern crate rlp_derive; | ||||
| extern crate bloomable; | ||||
| extern crate heapsize; | ||||
| 
 | ||||
| #[cfg(test)] | ||||
| extern crate rustc_hex; | ||||
|  | ||||
| @ -17,7 +17,8 @@ | ||||
| //! Log entry type definition.
 | ||||
| 
 | ||||
| use std::ops::Deref; | ||||
| use util::{H256, Address, Bytes, HeapSizeOf, Hashable}; | ||||
| use heapsize::HeapSizeOf; | ||||
| use util::{H256, Address, Bytes, Hashable}; | ||||
| use bloomable::Bloomable; | ||||
| 
 | ||||
| use {BlockNumber}; | ||||
|  | ||||
| @ -17,7 +17,7 @@ | ||||
| //! Receipt
 | ||||
| 
 | ||||
| use util::{H256, U256, Address}; | ||||
| use util::HeapSizeOf; | ||||
| use heapsize::HeapSizeOf; | ||||
| use rlp::*; | ||||
| 
 | ||||
| use {BlockNumber}; | ||||
|  | ||||
| @ -102,6 +102,12 @@ pub struct Params { | ||||
| 	pub block_reward: Option<Uint>, | ||||
| 	/// See `CommonParams` docs.
 | ||||
| 	pub registrar: Option<Address>, | ||||
| 	/// Apply reward flag
 | ||||
| 	#[serde(rename="applyReward")] | ||||
| 	pub apply_reward: Option<bool>, | ||||
| 	/// Node permission contract address.
 | ||||
| 	#[serde(rename="nodePermissionContract")] | ||||
| 	pub node_permission_contract: Option<Address>, | ||||
| } | ||||
| 
 | ||||
| #[cfg(test)] | ||||
|  | ||||
| @ -69,6 +69,7 @@ extern crate parity_updater as updater; | ||||
| extern crate parity_whisper; | ||||
| extern crate path; | ||||
| extern crate rpc_cli; | ||||
| extern crate node_filter; | ||||
| 
 | ||||
| #[macro_use] | ||||
| extern crate log as rlog; | ||||
|  | ||||
| @ -19,7 +19,7 @@ use std::path::Path; | ||||
| 
 | ||||
| use ethcore::client::BlockChainClient; | ||||
| use hypervisor::Hypervisor; | ||||
| use ethsync::{AttachedProtocol, SyncConfig, NetworkConfiguration, NetworkError, Params}; | ||||
| use ethsync::{AttachedProtocol, SyncConfig, NetworkConfiguration, NetworkError, Params, ConnectionFilter}; | ||||
| use ethcore::snapshot::SnapshotService; | ||||
| use light::Provider; | ||||
| 
 | ||||
| @ -183,6 +183,7 @@ pub fn sync( | ||||
| 	provider: Arc<Provider>, | ||||
| 	_log_settings: &LogConfig, | ||||
| 	attached_protos: Vec<AttachedProtocol>, | ||||
| 	connection_filter: Option<Arc<ConnectionFilter>>, | ||||
| ) -> Result<SyncModules, NetworkError> { | ||||
| 	let eth_sync = EthSync::new(Params { | ||||
| 		config: sync_cfg, | ||||
| @ -191,7 +192,8 @@ pub fn sync( | ||||
| 		snapshot_service: snapshot_service, | ||||
| 		network_config: net_cfg, | ||||
| 		attached_protos: attached_protos, | ||||
| 	})?; | ||||
| 	}, | ||||
| 	connection_filter)?; | ||||
| 
 | ||||
| 	Ok((eth_sync.clone() as Arc<SyncProvider>, eth_sync.clone() as Arc<ManageNetwork>, eth_sync.clone() as Arc<ChainNotify>)) | ||||
| } | ||||
|  | ||||
| @ -15,7 +15,7 @@ | ||||
| // along with Parity.  If not, see <http://www.gnu.org/licenses/>.
 | ||||
| 
 | ||||
| use std::fmt; | ||||
| use std::sync::Arc; | ||||
| use std::sync::{Arc, Weak}; | ||||
| use std::net::{TcpListener}; | ||||
| 
 | ||||
| use ctrlc::CtrlC; | ||||
| @ -38,6 +38,7 @@ use parity_reactor::EventLoop; | ||||
| use parity_rpc::{NetworkSettings, informant, is_major_importing}; | ||||
| use updater::{UpdatePolicy, Updater}; | ||||
| use util::{Colour, version, Mutex, Condvar}; | ||||
| use node_filter::NodeFilter; | ||||
| 
 | ||||
| use params::{ | ||||
| 	SpecType, Pruning, AccountsConfig, GasPricerConfig, MinerExtras, Switch, | ||||
| @ -569,11 +570,13 @@ pub fn execute(cmd: RunCmd, can_restart: bool, logger: Arc<RotatingLogger>) -> R | ||||
| 		miner.clone(), | ||||
| 	).map_err(|e| format!("Client service error: {:?}", e))?; | ||||
| 
 | ||||
| 	let connection_filter_address = spec.params().node_permission_contract; | ||||
| 	// drop the spec to free up genesis state.
 | ||||
| 	drop(spec); | ||||
| 
 | ||||
| 	// take handle to client
 | ||||
| 	let client = service.client(); | ||||
| 	let connection_filter = connection_filter_address.map(|a| Arc::new(NodeFilter::new(Arc::downgrade(&client) as Weak<BlockChainClient>, a))); | ||||
| 	let snapshot_service = service.snapshot_service(); | ||||
| 
 | ||||
| 	// initialize the local node information store.
 | ||||
| @ -645,9 +648,13 @@ pub fn execute(cmd: RunCmd, can_restart: bool, logger: Arc<RotatingLogger>) -> R | ||||
| 		client.clone(), | ||||
| 		&cmd.logger_config, | ||||
| 		attached_protos, | ||||
| 		connection_filter.clone().map(|f| f as Arc<::ethsync::ConnectionFilter + 'static>), | ||||
| 	).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 { | ||||
|  | ||||
| @ -47,8 +47,8 @@ fn io() -> Tester { | ||||
| 		result: Res::None, | ||||
| 		subtraces: 0, | ||||
| 		trace_address: vec![0], | ||||
| 		transaction_number: 0, | ||||
| 		transaction_hash: 5.into(), | ||||
| 		transaction_number: Some(0), | ||||
| 		transaction_hash: Some(5.into()), | ||||
| 		block_number: 10, | ||||
| 		block_hash: 10.into(), | ||||
| 	}]); | ||||
|  | ||||
| @ -299,6 +299,49 @@ impl From<trace::Call> for Call { | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| /// Reward type.
 | ||||
| #[derive(Debug, Serialize)] | ||||
| pub enum RewardType { | ||||
| 	/// Block
 | ||||
| 	#[serde(rename="block")] | ||||
| 	Block, | ||||
| 	/// Uncle
 | ||||
| 	#[serde(rename="uncle")] | ||||
| 	Uncle, | ||||
| } | ||||
| 
 | ||||
| impl From<trace::RewardType> for RewardType { | ||||
| 	fn from(c: trace::RewardType) -> Self { | ||||
| 		match c { | ||||
| 			trace::RewardType::Block => RewardType::Block, | ||||
| 			trace::RewardType::Uncle => RewardType::Uncle, | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| /// Reward action
 | ||||
| #[derive(Debug, Serialize)] | ||||
| pub struct Reward { | ||||
| 	/// Author's address.
 | ||||
| 	pub author: H160, | ||||
| 	/// Reward amount.
 | ||||
| 	pub value: U256, | ||||
| 	/// Reward type.
 | ||||
| 	#[serde(rename="rewardType")] | ||||
| 	pub reward_type: RewardType, | ||||
| } | ||||
| 
 | ||||
| impl From<trace::Reward> for Reward { | ||||
| 	fn from(r: trace::Reward) -> Self { | ||||
| 		Reward { | ||||
| 			author: r.author.into(), | ||||
| 			value: r.value.into(), | ||||
| 			reward_type: r.reward_type.into(), | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| /// Suicide
 | ||||
| #[derive(Debug, Serialize)] | ||||
| pub struct Suicide { | ||||
| @ -330,6 +373,8 @@ pub enum Action { | ||||
| 	Create(Create), | ||||
| 	/// Suicide
 | ||||
| 	Suicide(Suicide), | ||||
| 	/// Reward
 | ||||
| 	Reward(Reward),	
 | ||||
| } | ||||
| 
 | ||||
| impl From<trace::Action> for Action { | ||||
| @ -338,6 +383,7 @@ impl From<trace::Action> for Action { | ||||
| 			trace::Action::Call(call) => Action::Call(call.into()), | ||||
| 			trace::Action::Create(create) => Action::Create(create.into()), | ||||
| 			trace::Action::Suicide(suicide) => Action::Suicide(suicide.into()), | ||||
| 			trace::Action::Reward(reward) => Action::Reward(reward.into()), | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| @ -422,9 +468,9 @@ pub struct LocalizedTrace { | ||||
| 	/// Subtraces
 | ||||
| 	subtraces: usize, | ||||
| 	/// Transaction position
 | ||||
| 	transaction_position: usize, | ||||
| 	transaction_position: Option<usize>, | ||||
| 	/// Transaction hash
 | ||||
| 	transaction_hash: H256, | ||||
| 	transaction_hash: Option<H256>, | ||||
| 	/// Block Number
 | ||||
| 	block_number: u64, | ||||
| 	/// Block Hash
 | ||||
| @ -449,6 +495,10 @@ impl Serialize for LocalizedTrace { | ||||
| 				struc.serialize_field("type", "suicide")?; | ||||
| 				struc.serialize_field("action", suicide)?; | ||||
| 			}, | ||||
| 			Action::Reward(ref reward) => { | ||||
| 				struc.serialize_field("type", "reward")?; | ||||
| 				struc.serialize_field("action", reward)?; | ||||
| 			}, | ||||
| 		} | ||||
| 
 | ||||
| 		match self.result { | ||||
| @ -477,8 +527,8 @@ impl From<EthLocalizedTrace> for LocalizedTrace { | ||||
| 			result: t.result.into(), | ||||
| 			trace_address: t.trace_address.into_iter().map(Into::into).collect(), | ||||
| 			subtraces: t.subtraces.into(), | ||||
| 			transaction_position: t.transaction_number.into(), | ||||
| 			transaction_hash: t.transaction_hash.into(), | ||||
| 			transaction_position: t.transaction_number.map(Into::into), | ||||
| 			transaction_hash: t.transaction_hash.map(Into::into), | ||||
| 			block_number: t.block_number.into(), | ||||
| 			block_hash: t.block_hash.into(), | ||||
| 		} | ||||
| @ -516,6 +566,10 @@ impl Serialize for Trace { | ||||
| 				struc.serialize_field("type", "suicide")?; | ||||
| 				struc.serialize_field("action", suicide)?; | ||||
| 			}, | ||||
| 			Action::Reward(ref reward) => { | ||||
| 				struc.serialize_field("type", "reward")?; | ||||
| 				struc.serialize_field("action", reward)?; | ||||
| 			}, | ||||
| 		} | ||||
| 
 | ||||
| 		match self.result { | ||||
| @ -607,8 +661,8 @@ mod tests { | ||||
| 			}), | ||||
| 			trace_address: vec![10], | ||||
| 			subtraces: 1, | ||||
| 			transaction_position: 11, | ||||
| 			transaction_hash: 12.into(), | ||||
| 			transaction_position: Some(11), | ||||
| 			transaction_hash: Some(12.into()), | ||||
| 			block_number: 13, | ||||
| 			block_hash: 14.into(), | ||||
| 		}; | ||||
| @ -630,8 +684,8 @@ mod tests { | ||||
| 			result: Res::FailedCall(TraceError::OutOfGas), | ||||
| 			trace_address: vec![10], | ||||
| 			subtraces: 1, | ||||
| 			transaction_position: 11, | ||||
| 			transaction_hash: 12.into(), | ||||
| 			transaction_position: Some(11), | ||||
| 			transaction_hash: Some(12.into()), | ||||
| 			block_number: 13, | ||||
| 			block_hash: 14.into(), | ||||
| 		}; | ||||
| @ -655,8 +709,8 @@ mod tests { | ||||
| 			}), | ||||
| 			trace_address: vec![10], | ||||
| 			subtraces: 1, | ||||
| 			transaction_position: 11, | ||||
| 			transaction_hash: 12.into(), | ||||
| 			transaction_position: Some(11), | ||||
| 			transaction_hash: Some(12.into()), | ||||
| 			block_number: 13, | ||||
| 			block_hash: 14.into(), | ||||
| 		}; | ||||
| @ -676,8 +730,8 @@ mod tests { | ||||
| 			result: Res::FailedCreate(TraceError::OutOfGas), | ||||
| 			trace_address: vec![10], | ||||
| 			subtraces: 1, | ||||
| 			transaction_position: 11, | ||||
| 			transaction_hash: 12.into(), | ||||
| 			transaction_position: Some(11), | ||||
| 			transaction_hash: Some(12.into()), | ||||
| 			block_number: 13, | ||||
| 			block_hash: 14.into(), | ||||
| 		}; | ||||
| @ -696,8 +750,8 @@ mod tests { | ||||
| 			result: Res::None, | ||||
| 			trace_address: vec![10], | ||||
| 			subtraces: 1, | ||||
| 			transaction_position: 11, | ||||
| 			transaction_hash: 12.into(), | ||||
| 			transaction_position: Some(11), | ||||
| 			transaction_hash: Some(12.into()), | ||||
| 			block_number: 13, | ||||
| 			block_hash: 14.into(), | ||||
| 		}; | ||||
| @ -705,6 +759,26 @@ mod tests { | ||||
| 		assert_eq!(serialized, r#"{"type":"suicide","action":{"address":"0x0000000000000000000000000000000000000004","refundAddress":"0x0000000000000000000000000000000000000006","balance":"0x7"},"result":null,"traceAddress":[10],"subtraces":1,"transactionPosition":11,"transactionHash":"0x000000000000000000000000000000000000000000000000000000000000000c","blockNumber":13,"blockHash":"0x000000000000000000000000000000000000000000000000000000000000000e"}"#); | ||||
| 	} | ||||
| 
 | ||||
| 	#[test] | ||||
| 	fn test_trace_reward_serialize() { | ||||
| 		let t = LocalizedTrace { | ||||
| 			action: Action::Reward(Reward { | ||||
| 				author: 4.into(), | ||||
| 				value: 6.into(), | ||||
| 				reward_type: RewardType::Block, | ||||
| 			}), | ||||
| 			result: Res::None, | ||||
| 			trace_address: vec![10], | ||||
| 			subtraces: 1, | ||||
| 			transaction_position: None, | ||||
| 			transaction_hash: None, | ||||
| 			block_number: 13, | ||||
| 			block_hash: 14.into(), | ||||
| 		}; | ||||
| 		let serialized = serde_json::to_string(&t).unwrap(); | ||||
| 		assert_eq!(serialized, r#"{"type":"reward","action":{"author":"0x0000000000000000000000000000000000000004","value":"0x6","rewardType":"block"},"result":null,"traceAddress":[10],"subtraces":1,"transactionPosition":null,"transactionHash":null,"blockNumber":13,"blockHash":"0x000000000000000000000000000000000000000000000000000000000000000e"}"#); | ||||
| 	} | ||||
| 
 | ||||
| 	#[test] | ||||
| 	fn test_vmtrace_serialize() { | ||||
| 		let t = VMTrace { | ||||
|  | ||||
| @ -1,5 +1,5 @@ | ||||
| name: parity | ||||
| version: master | ||||
| version: git | ||||
| summary: Fast, light, robust Ethereum implementation | ||||
| description: | | ||||
|   Parity's goal is to be the fastest, lightest, and most secure Ethereum | ||||
|  | ||||
| @ -19,7 +19,7 @@ use std::collections::{HashMap, BTreeMap}; | ||||
| use std::io; | ||||
| use util::Bytes; | ||||
| use network::{NetworkProtocolHandler, NetworkService, NetworkContext, HostInfo, PeerId, ProtocolId, | ||||
| 	NetworkConfiguration as BasicNetworkConfiguration, NonReservedPeerMode, NetworkError}; | ||||
| 	NetworkConfiguration as BasicNetworkConfiguration, NonReservedPeerMode, NetworkError, ConnectionFilter}; | ||||
| use util::{U256, H256, H512}; | ||||
| use io::{TimerToken}; | ||||
| use ethcore::ethstore::ethkey::Secret; | ||||
| @ -236,7 +236,7 @@ pub struct EthSync { | ||||
| 
 | ||||
| impl EthSync { | ||||
| 	/// Creates and register protocol with the network service
 | ||||
| 	pub fn new(params: Params) -> Result<Arc<EthSync>, NetworkError> { | ||||
| 	pub fn new(params: Params, connection_filter: Option<Arc<ConnectionFilter>>) -> Result<Arc<EthSync>, NetworkError> { | ||||
| 		const MAX_LIGHTSERV_LOAD: f64 = 0.5; | ||||
| 
 | ||||
| 		let pruning_info = params.chain.pruning_info(); | ||||
| @ -272,7 +272,7 @@ impl EthSync { | ||||
| 		}; | ||||
| 
 | ||||
| 		let chain_sync = ChainSync::new(params.config, &*params.chain); | ||||
| 		let service = NetworkService::new(params.network_config.clone().into_basic()?)?; | ||||
| 		let service = NetworkService::new(params.network_config.clone().into_basic()?, connection_filter)?; | ||||
| 
 | ||||
| 		let sync = Arc::new(EthSync { | ||||
| 			network: service, | ||||
| @ -736,7 +736,7 @@ impl LightSync { | ||||
| 			(sync_handler, Arc::new(light_proto)) | ||||
| 		}; | ||||
| 
 | ||||
| 		let service = NetworkService::new(params.network_config)?; | ||||
| 		let service = NetworkService::new(params.network_config, None)?; | ||||
| 
 | ||||
| 		Ok(LightSync { | ||||
| 			proto: light_proto, | ||||
|  | ||||
| @ -20,6 +20,7 @@ | ||||
| 
 | ||||
| use std::collections::{HashSet, VecDeque}; | ||||
| use std::cmp; | ||||
| use heapsize::HeapSizeOf; | ||||
| use util::*; | ||||
| use rlp::*; | ||||
| use ethcore::views::{BlockView}; | ||||
|  | ||||
| @ -17,6 +17,7 @@ | ||||
| use std::collections::{HashSet, HashMap}; | ||||
| use std::collections::hash_map::Entry; | ||||
| use smallvec::SmallVec; | ||||
| use heapsize::HeapSizeOf; | ||||
| use util::*; | ||||
| use rlp::*; | ||||
| use network::NetworkError; | ||||
|  | ||||
| @ -91,6 +91,7 @@ | ||||
| 
 | ||||
| use std::collections::{HashSet, HashMap}; | ||||
| use std::cmp; | ||||
| use heapsize::HeapSizeOf; | ||||
| use util::*; | ||||
| use rlp::*; | ||||
| use network::*; | ||||
|  | ||||
| @ -76,7 +76,7 @@ mod api; | ||||
| 
 | ||||
| pub use api::*; | ||||
| pub use chain::{SyncStatus, SyncState}; | ||||
| pub use network::{is_valid_node_url, NonReservedPeerMode, NetworkError}; | ||||
| pub use network::{is_valid_node_url, NonReservedPeerMode, NetworkError, ConnectionFilter, ConnectionDirection}; | ||||
| 
 | ||||
| /// IPC interfaces
 | ||||
| #[cfg(feature="ipc")] | ||||
|  | ||||
| @ -24,9 +24,6 @@ use std::hash::{Hash, Hasher}; | ||||
| use std::collections::HashSet; | ||||
| use siphasher::sip::SipHasher; | ||||
| 
 | ||||
| // TODO [ToDr] Both hashers are exactly the same - no point to keep two.
 | ||||
| const NUMBER_OF_HASHERS: usize = 2; | ||||
| 
 | ||||
| /// BitVec structure with journalling
 | ||||
| /// Every time any of the blocks is getting set it's index is tracked
 | ||||
| /// and can be then drained by `drain` method
 | ||||
| @ -80,8 +77,6 @@ pub struct Bloom { | ||||
| 	bitmap: BitVecJournal, | ||||
| 	bitmap_bits: u64, | ||||
| 	k_num: u32, | ||||
| 	// TODO [ToDr] Both hashers are exactly the same - no point to keep two.
 | ||||
| 	sips: [SipHasher; NUMBER_OF_HASHERS], | ||||
| } | ||||
| 
 | ||||
| impl Bloom { | ||||
| @ -93,12 +88,10 @@ impl Bloom { | ||||
| 		let bitmap_bits = (bitmap_size as u64) * 8u64; | ||||
| 		let k_num = Bloom::optimal_k_num(bitmap_bits, items_count); | ||||
| 		let bitmap = BitVecJournal::new(bitmap_bits as usize); | ||||
| 		let sips = [SipHasher::new(), SipHasher::new()]; | ||||
| 		Bloom { | ||||
| 			bitmap: bitmap, | ||||
| 			bitmap_bits: bitmap_bits, | ||||
| 			k_num: k_num, | ||||
| 			sips: sips, | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| @ -107,12 +100,10 @@ impl Bloom { | ||||
| 		let bitmap_size = parts.len() * 8; | ||||
| 		let bitmap_bits = (bitmap_size as u64) * 8u64; | ||||
| 		let bitmap = BitVecJournal::from_parts(parts); | ||||
| 		let sips = [SipHasher::new(), SipHasher::new()]; | ||||
| 		Bloom { | ||||
| 			bitmap: bitmap, | ||||
| 			bitmap_bits: bitmap_bits, | ||||
| 			k_num: k_num, | ||||
| 			sips: sips, | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| @ -139,9 +130,9 @@ impl Bloom { | ||||
| 	pub fn set<T>(&mut self, item: T) | ||||
| 		where T: Hash | ||||
| 	{ | ||||
| 		let mut hashes = [0u64, 0u64]; | ||||
| 		let base_hash = Bloom::sip_hash(&item); | ||||
| 		for k_i in 0..self.k_num { | ||||
| 			let bit_offset = (self.bloom_hash(&mut hashes, &item, k_i) % self.bitmap_bits) as usize; | ||||
| 			let bit_offset = (Bloom::bloom_hash(base_hash, k_i) % self.bitmap_bits) as usize; | ||||
| 			self.bitmap.set(bit_offset); | ||||
| 		} | ||||
| 	} | ||||
| @ -151,9 +142,9 @@ impl Bloom { | ||||
| 	pub fn check<T>(&self, item: T) -> bool | ||||
| 		where T: Hash | ||||
| 	{ | ||||
| 		let mut hashes = [0u64, 0u64]; | ||||
| 		let base_hash = Bloom::sip_hash(&item); | ||||
| 		for k_i in 0..self.k_num { | ||||
| 			let bit_offset = (self.bloom_hash(&mut hashes, &item, k_i) % self.bitmap_bits) as usize; | ||||
| 			let bit_offset = (Bloom::bloom_hash(base_hash, k_i) % self.bitmap_bits) as usize; | ||||
| 			if !self.bitmap.get(bit_offset) { | ||||
| 				return false; | ||||
| 			} | ||||
| @ -178,17 +169,20 @@ impl Bloom { | ||||
| 		cmp::max(k_num, 1) | ||||
| 	} | ||||
| 
 | ||||
| 	fn bloom_hash<T>(&self, hashes: &mut [u64; NUMBER_OF_HASHERS], item: &T, k_i: u32) -> u64 | ||||
| 	fn sip_hash<T>(item: &T) -> u64 | ||||
| 		where T: Hash | ||||
| 	{ | ||||
| 		if k_i < NUMBER_OF_HASHERS as u32 { | ||||
| 			let mut sip = self.sips[k_i as usize].clone(); | ||||
| 			item.hash(&mut sip); | ||||
| 			let hash = sip.finish(); | ||||
| 			hashes[k_i as usize] = hash; | ||||
| 			hash | ||||
| 		let mut sip = SipHasher::new(); | ||||
| 		item.hash(&mut sip); | ||||
| 		let hash = sip.finish(); | ||||
| 		hash | ||||
| 	} | ||||
| 
 | ||||
| 	fn bloom_hash(base_hash: u64, k_i: u32) -> u64 { | ||||
| 		if k_i < 2 { | ||||
| 			base_hash | ||||
| 		} else { | ||||
| 			hashes[0].wrapping_add((k_i as u64).wrapping_mul(hashes[1]) % 0xffffffffffffffc5) | ||||
| 			base_hash.wrapping_add((k_i as u64).wrapping_mul(base_hash) % 0xffffffffffffffc5) | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| @ -218,6 +212,7 @@ pub struct BloomJournal { | ||||
| #[cfg(test)] | ||||
| mod tests { | ||||
| 	use super::Bloom; | ||||
| 	use std::collections::HashSet; | ||||
| 
 | ||||
| 	#[test] | ||||
| 	fn get_set() { | ||||
| @ -248,4 +243,16 @@ mod tests { | ||||
| 		// 2/8/64 = 0.00390625
 | ||||
| 		assert!(full >= 0.0039f64 && full <= 0.004f64); | ||||
| 	} | ||||
| 
 | ||||
| 	#[test] | ||||
| 	fn hash_backward_compatibility() { | ||||
| 		let ss = vec!["you", "should", "not", "break", "hash", "backward", "compatibility"]; | ||||
| 		let mut bloom = Bloom::new(16, 8); | ||||
| 		for s in ss.iter() { | ||||
| 			bloom.set(&s); | ||||
| 		} | ||||
| 		let drained_elems: HashSet<u64> = bloom.drain_journal().entries.into_iter().map(|t| t.1).collect(); | ||||
| 		let expected: HashSet<u64> = [2094615114573771027u64, 244675582389208413u64].iter().cloned().collect(); | ||||
| 		assert_eq!(drained_elems, expected); | ||||
| 	} | ||||
| } | ||||
|  | ||||
							
								
								
									
										31
									
								
								util/network/src/connection_filter.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								util/network/src/connection_filter.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,31 @@ | ||||
| // Copyright 2015-2017 Parity Technologies (UK) Ltd.
 | ||||
| // This file is part of Parity.
 | ||||
| 
 | ||||
| // Parity is free software: you can redistribute it and/or modify
 | ||||
| // it under the terms of the GNU General Public License as published by
 | ||||
| // the Free Software Foundation, either version 3 of the License, or
 | ||||
| // (at your option) any later version.
 | ||||
| 
 | ||||
| // Parity is distributed in the hope that it will be useful,
 | ||||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | ||||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | ||||
| // GNU General Public License for more details.
 | ||||
| 
 | ||||
| // You should have received a copy of the GNU General Public License
 | ||||
| // along with Parity.  If not, see <http://www.gnu.org/licenses/>.
 | ||||
| 
 | ||||
| //! Connection filter trait.
 | ||||
| 
 | ||||
| use super::NodeId; | ||||
| 
 | ||||
| /// Filtered connection direction.
 | ||||
| pub enum ConnectionDirection { | ||||
| 	Inbound, | ||||
| 	Outbound, | ||||
| } | ||||
| 
 | ||||
| /// Connection filter. Each connection is checked against `connection_allowed`.
 | ||||
| pub trait ConnectionFilter : Send + Sync { | ||||
| 	/// Filter a connection. Returns `true` if connection should be allowed. `false` if rejected.
 | ||||
| 	fn connection_allowed(&self, own_id: &NodeId, connecting_id: &NodeId, direction: ConnectionDirection) -> bool; | ||||
| } | ||||
| @ -42,6 +42,7 @@ use discovery::{Discovery, TableUpdates, NodeEntry}; | ||||
| use ip_utils::{map_external_address, select_public_address}; | ||||
| use path::restrict_permissions_owner; | ||||
| use parking_lot::{Mutex, RwLock}; | ||||
| use connection_filter::{ConnectionFilter, ConnectionDirection}; | ||||
| 
 | ||||
| type Slab<T> = ::slab::Slab<T, usize>; | ||||
| 
 | ||||
| @ -380,11 +381,12 @@ pub struct Host { | ||||
| 	reserved_nodes: RwLock<HashSet<NodeId>>, | ||||
| 	num_sessions: AtomicUsize, | ||||
| 	stopping: AtomicBool, | ||||
| 	filter: Option<Arc<ConnectionFilter>>, | ||||
| } | ||||
| 
 | ||||
| impl Host { | ||||
| 	/// Create a new instance
 | ||||
| 	pub fn new(mut config: NetworkConfiguration, stats: Arc<NetworkStats>) -> Result<Host, NetworkError> { | ||||
| 	pub fn new(mut config: NetworkConfiguration, stats: Arc<NetworkStats>, filter: Option<Arc<ConnectionFilter>>) -> Result<Host, NetworkError> { | ||||
| 		let mut listen_address = match config.listen_address { | ||||
| 			None => SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(0, 0, 0, 0), DEFAULT_PORT)), | ||||
| 			Some(addr) => addr, | ||||
| @ -437,6 +439,7 @@ impl Host { | ||||
| 			reserved_nodes: RwLock::new(HashSet::new()), | ||||
| 			num_sessions: AtomicUsize::new(0), | ||||
| 			stopping: AtomicBool::new(false), | ||||
| 			filter: filter, | ||||
| 		}; | ||||
| 
 | ||||
| 		for n in boot_nodes { | ||||
| @ -691,8 +694,12 @@ impl Host { | ||||
| 
 | ||||
| 		let max_handshakes_per_round = max_handshakes / 2; | ||||
| 		let mut started: usize = 0; | ||||
| 		for id in nodes.filter(|id| !self.have_session(id) && !self.connecting_to(id) && *id != self_id) | ||||
| 			.take(min(max_handshakes_per_round, max_handshakes - handshake_count)) { | ||||
| 		for id in nodes.filter(|id| 
 | ||||
| 				!self.have_session(id) && | ||||
| 				!self.connecting_to(id) && | ||||
| 				*id != self_id && | ||||
| 				self.filter.as_ref().map_or(true, |f| f.connection_allowed(&self_id, &id, ConnectionDirection::Outbound)) | ||||
| 			).take(min(max_handshakes_per_round, max_handshakes - handshake_count)) { | ||||
| 			self.connect_peer(&id, io); | ||||
| 			started += 1; | ||||
| 		} | ||||
| @ -827,7 +834,7 @@ impl Host { | ||||
| 						Ok(SessionData::Ready) => { | ||||
| 							self.num_sessions.fetch_add(1, AtomicOrdering::SeqCst); | ||||
| 							let session_count = self.session_count(); | ||||
| 							let (min_peers, max_peers, reserved_only) = { | ||||
| 							let (min_peers, max_peers, reserved_only, self_id) = { | ||||
| 								let info = self.info.read(); | ||||
| 								let mut max_peers = info.config.max_peers; | ||||
| 								for cap in s.info.capabilities.iter() { | ||||
| @ -836,7 +843,7 @@ impl Host { | ||||
| 										break; | ||||
| 									} | ||||
| 								} | ||||
| 								(info.config.min_peers as usize, max_peers as usize, info.config.non_reserved_mode == NonReservedPeerMode::Deny) | ||||
| 								(info.config.min_peers as usize, max_peers as usize, info.config.non_reserved_mode == NonReservedPeerMode::Deny, info.id().clone()) | ||||
| 							}; | ||||
| 
 | ||||
| 							let id = s.id().expect("Ready session always has id").clone(); | ||||
| @ -852,6 +859,14 @@ impl Host { | ||||
| 									break; | ||||
| 								} | ||||
| 							} | ||||
| 
 | ||||
| 							if !self.filter.as_ref().map_or(true, |f| f.connection_allowed(&self_id, &id, ConnectionDirection::Inbound)) { | ||||
| 								trace!(target: "network", "Inbound connection not allowed for {:?}", id); | ||||
| 								s.disconnect(io, DisconnectReason::UnexpectedIdentity); | ||||
| 								kill = true; | ||||
| 								break; | ||||
| 							} | ||||
| 
 | ||||
| 							ready_id = Some(id); | ||||
| 
 | ||||
| 							// Add it to the node table
 | ||||
| @ -1266,7 +1281,7 @@ fn host_client_url() { | ||||
| 	let mut config = NetworkConfiguration::new_local(); | ||||
| 	let key = "6f7b0d801bc7b5ce7bbd930b84fd0369b3eb25d09be58d64ba811091046f3aa2".parse().unwrap(); | ||||
| 	config.use_secret = Some(key); | ||||
| 	let host: Host = Host::new(config, Arc::new(NetworkStats::new())).unwrap(); | ||||
| 	let host: Host = Host::new(config, Arc::new(NetworkStats::new()), None).unwrap(); | ||||
| 	assert!(host.local_url().starts_with("enode://101b3ef5a4ea7a1c7928e24c4c75fd053c235d7b80c22ae5c03d145d0ac7396e2a4ffff9adee3133a7b05044a5cee08115fd65145e5165d646bde371010d803c@")); | ||||
| } | ||||
| 
 | ||||
|  | ||||
| @ -44,7 +44,7 @@ | ||||
| //! }
 | ||||
| //!
 | ||||
| //! fn main () {
 | ||||
| //! 	let mut service = NetworkService::new(NetworkConfiguration::new_local()).expect("Error creating network service");
 | ||||
| //! 	let mut service = NetworkService::new(NetworkConfiguration::new_local(), None).expect("Error creating network service");
 | ||||
| //! 	service.start().expect("Error starting service");
 | ||||
| //! 	service.register_protocol(Arc::new(MyHandler), *b"myp", 1, &[1u8]);
 | ||||
| //!
 | ||||
| @ -95,6 +95,7 @@ mod error; | ||||
| mod node_table; | ||||
| mod stats; | ||||
| mod ip_utils; | ||||
| mod connection_filter; | ||||
| 
 | ||||
| #[cfg(test)] | ||||
| mod tests; | ||||
| @ -104,6 +105,7 @@ pub use service::NetworkService; | ||||
| pub use error::NetworkError; | ||||
| pub use stats::NetworkStats; | ||||
| pub use session::SessionInfo; | ||||
| pub use connection_filter::{ConnectionFilter, ConnectionDirection}; | ||||
| 
 | ||||
| pub use io::TimerToken; | ||||
| pub use node_table::{is_valid_node_url, NodeId}; | ||||
|  | ||||
| @ -22,6 +22,7 @@ use io::*; | ||||
| use parking_lot::RwLock; | ||||
| use std::sync::Arc; | ||||
| use ansi_term::Colour; | ||||
| use connection_filter::ConnectionFilter; | ||||
| 
 | ||||
| struct HostHandler { | ||||
| 	public_url: RwLock<Option<String>> | ||||
| @ -48,11 +49,12 @@ pub struct NetworkService { | ||||
| 	stats: Arc<NetworkStats>, | ||||
| 	host_handler: Arc<HostHandler>, | ||||
| 	config: NetworkConfiguration, | ||||
| 	filter: Option<Arc<ConnectionFilter>>, | ||||
| } | ||||
| 
 | ||||
| impl NetworkService { | ||||
| 	/// Starts IO event loop
 | ||||
| 	pub fn new(config: NetworkConfiguration) -> Result<NetworkService, NetworkError> { | ||||
| 	pub fn new(config: NetworkConfiguration, filter: Option<Arc<ConnectionFilter>>) -> Result<NetworkService, NetworkError> { | ||||
| 		let host_handler = Arc::new(HostHandler { public_url: RwLock::new(None) }); | ||||
| 		let io_service = IoService::<NetworkIoMessage>::start()?; | ||||
| 
 | ||||
| @ -65,6 +67,7 @@ impl NetworkService { | ||||
| 			host: RwLock::new(None), | ||||
| 			config: config, | ||||
| 			host_handler: host_handler, | ||||
| 			filter: filter, | ||||
| 		}) | ||||
| 	} | ||||
| 
 | ||||
| @ -115,7 +118,7 @@ impl NetworkService { | ||||
| 	pub fn start(&self) -> Result<(), NetworkError> { | ||||
| 		let mut host = self.host.write(); | ||||
| 		if host.is_none() { | ||||
| 			let h = Arc::new(Host::new(self.config.clone(), self.stats.clone())?); | ||||
| 			let h = Arc::new(Host::new(self.config.clone(), self.stats.clone(), self.filter.clone())?); | ||||
| 			self.io_service.register_handler(h.clone())?; | ||||
| 			*host = Some(h); | ||||
| 		} | ||||
|  | ||||
| @ -92,7 +92,7 @@ impl NetworkProtocolHandler for TestProtocol { | ||||
| 
 | ||||
| #[test] | ||||
| fn net_service() { | ||||
| 	let service = NetworkService::new(NetworkConfiguration::new_local()).expect("Error creating network service"); | ||||
| 	let service = NetworkService::new(NetworkConfiguration::new_local(), None).expect("Error creating network service"); | ||||
| 	service.start().unwrap(); | ||||
| 	service.register_protocol(Arc::new(TestProtocol::new(false)), *b"myp", 1, &[1u8]).unwrap(); | ||||
| } | ||||
| @ -104,13 +104,13 @@ fn net_connect() { | ||||
| 	let mut config1 = NetworkConfiguration::new_local(); | ||||
| 	config1.use_secret = Some(key1.secret().clone()); | ||||
| 	config1.boot_nodes = vec![ ]; | ||||
| 	let mut service1 = NetworkService::new(config1).unwrap(); | ||||
| 	let mut service1 = NetworkService::new(config1, None).unwrap(); | ||||
| 	service1.start().unwrap(); | ||||
| 	let handler1 = TestProtocol::register(&mut service1, false); | ||||
| 	let mut config2 = NetworkConfiguration::new_local(); | ||||
| 	info!("net_connect: local URL: {}", service1.local_url().unwrap()); | ||||
| 	config2.boot_nodes = vec![ service1.local_url().unwrap() ]; | ||||
| 	let mut service2 = NetworkService::new(config2).unwrap(); | ||||
| 	let mut service2 = NetworkService::new(config2, None).unwrap(); | ||||
| 	service2.start().unwrap(); | ||||
| 	let handler2 = TestProtocol::register(&mut service2, false); | ||||
| 	while !handler1.got_packet() && !handler2.got_packet() && (service1.stats().sessions() == 0 || service2.stats().sessions() == 0) { | ||||
| @ -123,7 +123,7 @@ fn net_connect() { | ||||
| #[test] | ||||
| fn net_start_stop() { | ||||
| 	let config = NetworkConfiguration::new_local(); | ||||
| 	let service = NetworkService::new(config).unwrap(); | ||||
| 	let service = NetworkService::new(config, None).unwrap(); | ||||
| 	service.start().unwrap(); | ||||
| 	service.stop().unwrap(); | ||||
| 	service.start().unwrap(); | ||||
| @ -135,12 +135,12 @@ fn net_disconnect() { | ||||
| 	let mut config1 = NetworkConfiguration::new_local(); | ||||
| 	config1.use_secret = Some(key1.secret().clone()); | ||||
| 	config1.boot_nodes = vec![ ]; | ||||
| 	let mut service1 = NetworkService::new(config1).unwrap(); | ||||
| 	let mut service1 = NetworkService::new(config1, None).unwrap(); | ||||
| 	service1.start().unwrap(); | ||||
| 	let handler1 = TestProtocol::register(&mut service1, false); | ||||
| 	let mut config2 = NetworkConfiguration::new_local(); | ||||
| 	config2.boot_nodes = vec![ service1.local_url().unwrap() ]; | ||||
| 	let mut service2 = NetworkService::new(config2).unwrap(); | ||||
| 	let mut service2 = NetworkService::new(config2, None).unwrap(); | ||||
| 	service2.start().unwrap(); | ||||
| 	let handler2 = TestProtocol::register(&mut service2, true); | ||||
| 	while !(handler1.got_disconnect() && handler2.got_disconnect()) { | ||||
| @ -153,7 +153,7 @@ fn net_disconnect() { | ||||
| #[test] | ||||
| fn net_timeout() { | ||||
| 	let config = NetworkConfiguration::new_local(); | ||||
| 	let mut service = NetworkService::new(config).unwrap(); | ||||
| 	let mut service = NetworkService::new(config, None).unwrap(); | ||||
| 	service.start().unwrap(); | ||||
| 	let handler = TestProtocol::register(&mut service, false); | ||||
| 	while !handler.got_timeout() { | ||||
|  | ||||
| @ -151,7 +151,6 @@ pub use bigint::prelude::*; | ||||
| pub use bigint::hash; | ||||
| 
 | ||||
| pub use ansi_term::{Colour, Style}; | ||||
| pub use heapsize::HeapSizeOf; | ||||
| pub use parking_lot::{Condvar, Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard}; | ||||
| 
 | ||||
| /// 160-bit integer representing account address
 | ||||
|  | ||||
| @ -367,44 +367,50 @@ impl<'a> TrieDBMut<'a> { | ||||
| 	} | ||||
| 
 | ||||
| 	// walk the trie, attempting to find the key's node.
 | ||||
| 	fn lookup<'x, 'key>(&'x self, partial: NibbleSlice<'key>, handle: &NodeHandle) -> super::Result<Option<DBValue>> | ||||
| 	fn lookup<'x, 'key>(&'x self, mut partial: NibbleSlice<'key>, handle: &NodeHandle) -> super::Result<Option<DBValue>> | ||||
| 		where 'x: 'key | ||||
| 	{ | ||||
| 		match *handle { | ||||
| 			NodeHandle::Hash(ref hash) => Lookup { | ||||
| 				db: &*self.db, | ||||
| 				query: DBValue::from_slice, | ||||
| 				hash: hash.clone(), | ||||
| 			}.look_up(partial), | ||||
| 			NodeHandle::InMemory(ref handle) => match self.storage[handle] { | ||||
| 				Node::Empty => Ok(None), | ||||
| 				Node::Leaf(ref key, ref value) => { | ||||
| 					if NibbleSlice::from_encoded(key).0 == partial { | ||||
| 						Ok(Some(DBValue::from_slice(value))) | ||||
| 					} else { | ||||
| 						Ok(None) | ||||
| 		let mut handle = handle; | ||||
| 		loop { | ||||
| 			let (mid, child) = match *handle { | ||||
| 				NodeHandle::Hash(ref hash) => return Lookup { | ||||
| 					db: &*self.db, | ||||
| 					query: DBValue::from_slice, | ||||
| 					hash: hash.clone(), | ||||
| 				}.look_up(partial), | ||||
| 				NodeHandle::InMemory(ref handle) => match self.storage[handle] { | ||||
| 					Node::Empty => return Ok(None), | ||||
| 					Node::Leaf(ref key, ref value) => { | ||||
| 						if NibbleSlice::from_encoded(key).0 == partial { | ||||
| 							return Ok(Some(DBValue::from_slice(value))); | ||||
| 						} else { | ||||
| 							return Ok(None); | ||||
| 						} | ||||
| 					} | ||||
| 				} | ||||
| 				Node::Extension(ref slice, ref child) => { | ||||
| 					let slice = NibbleSlice::from_encoded(slice).0; | ||||
| 					if partial.starts_with(&slice) { | ||||
| 						self.lookup(partial.mid(slice.len()), child) | ||||
| 					} else { | ||||
| 						Ok(None) | ||||
| 					Node::Extension(ref slice, ref child) => { | ||||
| 						let slice = NibbleSlice::from_encoded(slice).0; | ||||
| 						if partial.starts_with(&slice) { | ||||
| 							(slice.len(), child) | ||||
| 						} else { | ||||
| 							return Ok(None); | ||||
| 						} | ||||
| 					} | ||||
| 				} | ||||
| 				Node::Branch(ref children, ref value) => { | ||||
| 					if partial.is_empty() { | ||||
| 						Ok(value.as_ref().map(|v| DBValue::from_slice(v))) | ||||
| 					} else { | ||||
| 						let idx = partial.at(0); | ||||
| 						match children[idx as usize].as_ref() { | ||||
| 							Some(child) => self.lookup(partial.mid(1), child), | ||||
| 							None => Ok(None), | ||||
| 					Node::Branch(ref children, ref value) => { | ||||
| 						if partial.is_empty() { | ||||
| 							return Ok(value.as_ref().map(|v| DBValue::from_slice(v))); | ||||
| 						} else { | ||||
| 							let idx = partial.at(0); | ||||
| 							match children[idx as usize].as_ref() { | ||||
| 								Some(child) => (1, child), | ||||
| 								None => return Ok(None), | ||||
| 							} | ||||
| 						} | ||||
| 					} | ||||
| 				} | ||||
| 			} | ||||
| 			}; | ||||
| 
 | ||||
| 			partial = partial.mid(mid); | ||||
| 			handle = child; | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user