Start of UPnP.

This commit is contained in:
Gav Wood 2016-02-10 16:45:54 +01:00
parent 015aaad2c4
commit 35374ac09c
3 changed files with 63 additions and 6 deletions

View File

@ -63,6 +63,7 @@ Options:
--listen-address URL Specify the IP/port on which to listen for peers [default: 0.0.0.0:30304]. --listen-address URL Specify the IP/port on which to listen for peers [default: 0.0.0.0:30304].
--public-address URL Specify the IP/port on which peers may connect [default: 0.0.0.0:30304]. --public-address URL Specify the IP/port on which peers may connect [default: 0.0.0.0:30304].
--address URL Equivalent to --listen-address URL --public-address URL. --address URL Equivalent to --listen-address URL --public-address URL.
--upnp Use UPnP to try to figure out the correct network settings.
--cache-pref-size BYTES Specify the prefered size of the blockchain cache in bytes [default: 16384]. --cache-pref-size BYTES Specify the prefered size of the blockchain cache in bytes [default: 16384].
--cache-max-size BYTES Specify the maximum size of the blockchain cache in bytes [default: 262144]. --cache-max-size BYTES Specify the maximum size of the blockchain cache in bytes [default: 262144].
@ -191,7 +192,7 @@ impl Informant {
let sync_info = sync.status(); let sync_info = sync.status();
if let (_, &Some(ref last_cache_info), &Some(ref last_report)) = (self.chain_info.read().unwrap().deref(), self.cache_info.read().unwrap().deref(), self.report.read().unwrap().deref()) { if let (_, &Some(ref last_cache_info), &Some(ref last_report)) = (self.chain_info.read().unwrap().deref(), self.cache_info.read().unwrap().deref(), self.report.read().unwrap().deref()) {
println!("[ {} {} ]---[ {} blk/s | {} tx/s | {} gas/s //··· {}/{} peers, {} downloaded, {}+{} queued ···// {} ({}) bl {} ({}) ex ]", println!("[ {} {} ]---[ {} blk/s | {} tx/s | {} gas/s ··· {}/{} peers, {} downloaded, {}+{} queued ··· {} ({}) bl {} ({}) ex | {} i/c ]",
chain_info.best_block_number, chain_info.best_block_number,
chain_info.best_block_hash, chain_info.best_block_hash,
(report.blocks_imported - last_report.blocks_imported) / dur, (report.blocks_imported - last_report.blocks_imported) / dur,
@ -207,7 +208,9 @@ impl Informant {
cache_info.blocks, cache_info.blocks,
cache_info.blocks as isize - last_cache_info.blocks as isize, cache_info.blocks as isize - last_cache_info.blocks as isize,
cache_info.block_details, cache_info.block_details,
cache_info.block_details as isize - last_cache_info.block_details as isize cache_info.block_details as isize - last_cache_info.block_details as isize,
updates_per_commit()
); );
} }

View File

@ -86,6 +86,26 @@ impl NetworkConfiguration {
config.public_address = SocketAddr::from_str(&format!("0.0.0.0:{}", port)).unwrap(); config.public_address = SocketAddr::from_str(&format!("0.0.0.0:{}", port)).unwrap();
config config
} }
/// Conduct NAT if needed.
pub fn prepared(self) -> Self {
let listen = self.listen_address;
let public = self.public_address;
if self.nat_enabled {
info!("Enabling NAT");
}
NetworkConfiguration {
listen_address: listen,
public_address: public,
nat_enabled: false,
discovery_enabled: self.discovery_enabled,
pin: self.pin,
boot_nodes: self.boot_nodes,
use_secret: self.use_secret,
}
}
} }
// Tokens // Tokens
@ -296,6 +316,8 @@ pub struct Host<Message> where Message: Send + Sync + Clone {
impl<Message> Host<Message> where Message: Send + Sync + Clone { impl<Message> Host<Message> where Message: Send + Sync + Clone {
/// Create a new instance /// Create a new instance
pub fn new(config: NetworkConfiguration) -> Host<Message> { pub fn new(config: NetworkConfiguration) -> Host<Message> {
let config = config.prepared();
let addr = config.listen_address; let addr = config.listen_address;
// Setup the server socket // Setup the server socket
let tcp_listener = TcpListener::bind(&addr).unwrap(); let tcp_listener = TcpListener::bind(&addr).unwrap();

View File

@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
use std::sync::RwLock;
use std::cell::RefCell;
use hash::*; use hash::*;
use sha3::*; use sha3::*;
use hashdb::*; use hashdb::*;
@ -21,11 +23,26 @@ use rlp::*;
use super::triedbmut::*; use super::triedbmut::*;
use super::trietraits::*; use super::trietraits::*;
lazy_static! {
static ref COMMIT_COUNT: RwLock<usize> = RwLock::new(0);
static ref UPDATE_COUNT: RwLock<usize> = RwLock::new(0);
}
/// Get mean number of updates per commit so far.
pub fn updates_per_commit() -> f64 {
let cc = *COMMIT_COUNT.read().unwrap();
if cc > 0 {
(*UPDATE_COUNT.read().unwrap() as f64) / (cc as f64)
} else { 0.0 }
}
/// A mutable `Trie` implementation which hashes keys and uses a generic `HashDB` backing database. /// A mutable `Trie` implementation which hashes keys and uses a generic `HashDB` backing database.
/// ///
/// Use it as a `Trie` or `TrieMut` trait object. You can use `raw()` to get the backing TrieDBMut object. /// Use it as a `Trie` or `TrieMut` trait object. You can use `raw()` to get the backing TrieDBMut object.
pub struct SecTrieDBMut<'db> { pub struct SecTrieDBMut<'db> {
raw: TrieDBMut<'db> raw: TrieDBMut<'db>,
/// Get number of updates done on this trie so far.
pub update_count: RefCell<usize>,
} }
impl<'db> SecTrieDBMut<'db> { impl<'db> SecTrieDBMut<'db> {
@ -33,13 +50,13 @@ impl<'db> SecTrieDBMut<'db> {
/// Initialise to the state entailed by the genesis block. /// Initialise to the state entailed by the genesis block.
/// This guarantees the trie is built correctly. /// This guarantees the trie is built correctly.
pub fn new(db: &'db mut HashDB, root: &'db mut H256) -> Self { pub fn new(db: &'db mut HashDB, root: &'db mut H256) -> Self {
SecTrieDBMut { raw: TrieDBMut::new(db, root) } SecTrieDBMut { raw: TrieDBMut::new(db, root), update_count: RefCell::new(0) }
} }
/// Create a new trie with the backing database `db` and `root` /// Create a new trie with the backing database `db` and `root`
/// Panics, if `root` does not exist /// Panics, if `root` does not exist
pub fn from_existing(db: &'db mut HashDB, root: &'db mut H256) -> Self { pub fn from_existing(db: &'db mut HashDB, root: &'db mut H256) -> Self {
SecTrieDBMut { raw: TrieDBMut::from_existing(db, root) } SecTrieDBMut { raw: TrieDBMut::from_existing(db, root), update_count: RefCell::new(0) }
} }
/// Get the backing database. /// Get the backing database.
@ -50,7 +67,9 @@ impl<'db> SecTrieDBMut<'db> {
} }
impl<'db> Trie for SecTrieDBMut<'db> { impl<'db> Trie for SecTrieDBMut<'db> {
fn root(&self) -> &H256 { self.raw.root() } fn root(&self) -> &H256 {
self.raw.root()
}
fn contains(&self, key: &[u8]) -> bool { fn contains(&self, key: &[u8]) -> bool {
self.raw.contains(&key.sha3()) self.raw.contains(&key.sha3())
@ -63,14 +82,27 @@ impl<'db> Trie for SecTrieDBMut<'db> {
impl<'db> TrieMut for SecTrieDBMut<'db> { impl<'db> TrieMut for SecTrieDBMut<'db> {
fn insert(&mut self, key: &[u8], value: &[u8]) { fn insert(&mut self, key: &[u8], value: &[u8]) {
*self.update_count.borrow_mut() += 1;
self.raw.insert(&key.sha3(), value); self.raw.insert(&key.sha3(), value);
} }
fn remove(&mut self, key: &[u8]) { fn remove(&mut self, key: &[u8]) {
*self.update_count.borrow_mut() += 1;
self.raw.remove(&key.sha3()); self.raw.remove(&key.sha3());
} }
} }
impl<'db> Drop for SecTrieDBMut<'db> {
fn drop(&mut self) {
let uc = *self.update_count.borrow();
if uc > 0 {
*COMMIT_COUNT.write().unwrap() += 1;
*UPDATE_COUNT.write().unwrap() += uc;
*self.update_count.borrow_mut() = 0;
}
}
}
#[test] #[test]
fn sectrie_to_trie() { fn sectrie_to_trie() {
use memorydb::*; use memorydb::*;