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

Commit

Permalink
Merge branch 'master' into ao-txn-propagation-peer-cache
Browse files Browse the repository at this point in the history
* master:
  idiomatic changes to PodState (#10834)
  Allow --nat extip:your.host.here.org (#10830)
  When updating the client or when called from RPC, sleep should mean sleep (#10814)
  Remove excessive warning (#10831)
  Fix typo in README.md (#10828)
  ethcore does not use byteorder (#10829)
  • Loading branch information
ordian committed Jul 3, 2019
2 parents 253fd0b + 8d24b4e commit bee05cb
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 62 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ We recommend installing Rust through [rustup](https://www.rustup.rs/). If you do

`clang` is required. It comes with Xcode command line tools or can be installed with homebrew.

- Windows
- Windows:
Make sure you have Visual Studio 2015 with C++ support installed. Next, download and run the `rustup` installer from
https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/rustup-init.exe, start "VS2015 x64 Native Tools Command Prompt", and use the following command to install and set up the `msvc` toolchain:
```bash
Expand Down
1 change: 0 additions & 1 deletion ethcore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ authors = ["Parity Technologies <admin@parity.io>"]
ansi_term = "0.11"
blooms-db = { path = "../util/blooms-db", optional = true }
bn = { git = "https://github.com/paritytech/bn", default-features = false }
byteorder = "1.0"
common-types = { path = "types" }
crossbeam = "0.4"
derive_more = "0.14.0"
Expand Down
5 changes: 3 additions & 2 deletions ethcore/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use std::cmp::{max, min};
use std::io::{self, Read};

use byteorder::{ByteOrder, BigEndian};
use parity_crypto::digest;
use num::{BigUint, Zero, One};

Expand Down Expand Up @@ -369,7 +368,9 @@ impl Impl for ModexpImpl {
// but so would running out of addressable memory!
let mut read_len = |reader: &mut io::Chain<&[u8], io::Repeat>| {
reader.read_exact(&mut buf[..]).expect("reading from zero-extended memory cannot fail; qed");
BigEndian::read_u64(&buf[24..]) as usize
let mut len_bytes = [0u8; 8];
len_bytes.copy_from_slice(&buf[24..]);
u64::from_be_bytes(len_bytes) as usize
};

let base_len = read_len(&mut reader);
Expand Down
10 changes: 5 additions & 5 deletions ethcore/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,7 @@ impl Client {
let mut ss = self.sleep_state.lock();
if let Some(t) = ss.last_activity {
if Instant::now() > t + timeout {
self.sleep();
self.sleep(false);
ss.last_activity = None;
}
}
Expand All @@ -1118,7 +1118,7 @@ impl Client {
let now = Instant::now();
if let Some(t) = ss.last_activity {
if now > t + timeout {
self.sleep();
self.sleep(false);
ss.last_activity = None;
ss.last_autosleep = Some(now);
}
Expand Down Expand Up @@ -1217,10 +1217,10 @@ impl Client {
}
}

fn sleep(&self) {
fn sleep(&self, force: bool) {
if self.liveness.load(AtomicOrdering::Relaxed) {
// only sleep if the import queue is mostly empty.
if self.queue_info().total_queue_size() <= MAX_QUEUE_SIZE_TO_SLEEP_ON {
if force || (self.queue_info().total_queue_size() <= MAX_QUEUE_SIZE_TO_SLEEP_ON) {
self.liveness.store(false, AtomicOrdering::Relaxed);
self.notify(|n| n.stop());
info!(target: "mode", "sleep: Sleeping.");
Expand Down Expand Up @@ -1733,7 +1733,7 @@ impl BlockChainClient for Client {
}
match new_mode {
Mode::Active => self.wake_up(),
Mode::Off => self.sleep(),
Mode::Off => self.sleep(true),
_ => {(*self.sleep_state.lock()).last_activity = Some(Instant::now()); }
}
}
Expand Down
1 change: 0 additions & 1 deletion ethcore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@

extern crate ansi_term;
extern crate bn;
extern crate byteorder;
extern crate common_types as types;
extern crate crossbeam;
extern crate ethabi;
Expand Down
16 changes: 2 additions & 14 deletions ethcore/src/pod_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

//! Account system expressed in Plain Old Data.

use std::fmt;
use std::collections::BTreeMap;
use itertools::Itertools;
use hash::{keccak};
Expand Down Expand Up @@ -53,7 +52,8 @@ pub struct PodAccount {
fn opt_bytes_to_hex<S>(opt_bytes: &Option<Bytes>, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
serializer.collect_str(&format_args!("0x{}",opt_bytes.as_ref().map_or("".to_string(), |b|b.to_hex())))
let readable = opt_bytes.as_ref().map(|b| b.to_hex()).unwrap_or_default();
serializer.collect_str(&format_args!("0x{}", readable))
}

impl PodAccount {
Expand Down Expand Up @@ -124,18 +124,6 @@ impl From<ethjson::spec::Account> for PodAccount {
}
}

impl fmt::Display for PodAccount {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "(bal={}; nonce={}; code={} bytes, #{}; storage={} items)",
self.balance,
self.nonce,
self.code.as_ref().map_or(0, |c| c.len()),
self.code.as_ref().map_or_else(H256::zero, |c| keccak(c)),
self.storage.len(),
)
}
}

/// Determine difference between two optionally existant `Account`s. Returns None
/// if they are the same.
pub fn diff_pod(pre: Option<&PodAccount>, post: Option<&PodAccount>) -> Option<AccountDiff> {
Expand Down
31 changes: 10 additions & 21 deletions ethcore/src/pod_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@

//! State of all accounts in the system expressed in Plain Old Data.

use std::fmt;
use std::collections::BTreeMap;
use itertools::Itertools;
use ethereum_types::{H256, Address};
use triehash::sec_trie_root;
use pod_account::{self, PodAccount};
Expand All @@ -30,12 +28,6 @@ use ethjson;
pub struct PodState(BTreeMap<Address, PodAccount>);

impl PodState {
/// Contruct a new object from the `m`.
pub fn new() -> PodState { Default::default() }

/// Contruct a new object from the `m`.
pub fn from(m: BTreeMap<Address, PodAccount>) -> PodState { PodState(m) }

/// Get the underlying map.
pub fn get(&self) -> &BTreeMap<Address, PodAccount> { &self.0 }

Expand Down Expand Up @@ -65,31 +57,28 @@ impl From<ethjson::spec::State> for PodState {
}
}

impl fmt::Display for PodState {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (add, acc) in &self.0 {
writeln!(f, "{} => {}", add, acc)?;
}
Ok(())
impl From<BTreeMap<Address, PodAccount>> for PodState {
fn from(s: BTreeMap<Address, PodAccount>) -> Self {
PodState(s)
}
}

/// Calculate and return diff between `pre` state and `post` state.
pub fn diff_pod(pre: &PodState, post: &PodState) -> StateDiff {
StateDiff {
raw: pre.get().keys()
.merge(post.get().keys())
.filter_map(|acc| pod_account::diff_pod(pre.get().get(acc), post.get().get(acc)).map(|d| (acc.clone(), d)))
raw: pre.0.keys()
.chain(post.0.keys())
.filter_map(|acc| pod_account::diff_pod(pre.0.get(acc), post.0.get(acc)).map(|d| (*acc, d)))
.collect()
}
}

#[cfg(test)]
mod test {
use std::collections::BTreeMap;
use types::state_diff::*;
use types::account_diff::*;
use pod_account::PodAccount;
use types::account_diff::{AccountDiff, Diff};
use types::state_diff::StateDiff;
use super::{PodState, Address};

#[test]
Expand All @@ -102,15 +91,15 @@ mod test {
storage: map![],
}
]);
assert_eq!(super::diff_pod(&a, &PodState::new()), StateDiff { raw: map![
assert_eq!(super::diff_pod(&a, &PodState::default()), StateDiff { raw: map![
Address::from_low_u64_be(1) => AccountDiff{
balance: Diff::Died(69.into()),
nonce: Diff::Died(0.into()),
code: Diff::Died(vec![]),
storage: map![],
}
]});
assert_eq!(super::diff_pod(&PodState::new(), &a), StateDiff{ raw: map![
assert_eq!(super::diff_pod(&PodState::default(), &a), StateDiff{ raw: map![
Address::from_low_u64_be(1) => AccountDiff{
balance: Diff::Born(69.into()),
nonce: Diff::Born(0.into()),
Expand Down
17 changes: 9 additions & 8 deletions ethcore/src/state_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use std::io;
use std::sync::Arc;

use bloom_journal::{Bloom, BloomJournal};
use byteorder::{LittleEndian, ByteOrder};
use db::COL_ACCOUNT_BLOOM;
use ethereum_types::{H256, Address};
use hash::keccak;
Expand Down Expand Up @@ -169,11 +168,15 @@ impl StateDB {
let hash_count = hash_count_bytes[0];

let mut bloom_parts = vec![0u64; ACCOUNT_BLOOM_SPACE / 8];
let mut key = [0u8; 8];
for i in 0..ACCOUNT_BLOOM_SPACE / 8 {
LittleEndian::write_u64(&mut key, i as u64);
let key: [u8; 8] = (i as u64).to_le_bytes();
bloom_parts[i] = db.get(COL_ACCOUNT_BLOOM, &key).expect("low-level database error")
.and_then(|val| Some(LittleEndian::read_u64(&val[..])))
.map(|val| {
assert!(val.len() == 8, "low-level database error");
let mut buff = [0u8; 8];
buff.copy_from_slice(&*val);
u64::from_le_bytes(buff)
})
.unwrap_or(0u64);
}

Expand All @@ -186,12 +189,10 @@ impl StateDB {
pub fn commit_bloom(batch: &mut DBTransaction, journal: BloomJournal) -> io::Result<()> {
assert!(journal.hash_functions <= 255);
batch.put(COL_ACCOUNT_BLOOM, ACCOUNT_BLOOM_HASHCOUNT_KEY, &[journal.hash_functions as u8]);
let mut key = [0u8; 8];
let mut val = [0u8; 8];

for (bloom_part_index, bloom_part_value) in journal.entries {
LittleEndian::write_u64(&mut key, bloom_part_index as u64);
LittleEndian::write_u64(&mut val, bloom_part_value);
let key: [u8; 8] = (bloom_part_index as u64).to_le_bytes();
let val: [u8; 8] = bloom_part_value.to_le_bytes();
batch.put(COL_ACCOUNT_BLOOM, &key, &val);
}
Ok(())
Expand Down
44 changes: 40 additions & 4 deletions parity/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use std::time::Duration;
use std::io::Read;
use std::net::SocketAddr;
use std::net::{SocketAddr, ToSocketAddrs};
use std::path::PathBuf;
use std::collections::{HashSet, BTreeMap};
use std::iter::FromIterator;
Expand Down Expand Up @@ -725,9 +725,18 @@ impl Configuration {
let port = self.args.arg_ports_shift + self.args.arg_port;
let listen_address = SocketAddr::new(self.interface(&self.args.arg_interface).parse().unwrap(), port);
let public_address = if self.args.arg_nat.starts_with("extip:") {
let host = &self.args.arg_nat[6..];
let host = host.parse().map_err(|_| format!("Invalid host given with `--nat extip:{}`", host))?;
Some(SocketAddr::new(host, port))
let host = self.args.arg_nat[6..].split(':').next().expect("split has at least one part; qed");
let host = format!("{}:{}", host, port);
match host.to_socket_addrs() {
Ok(mut addr_iter) => {
if let Some(addr) = addr_iter.next() {
Some(addr)
} else {
return Err(format!("Invalid host given with `--nat extip:{}`", &self.args.arg_nat[6..]))
}
},
Err(_) => return Err(format!("Invalid host given with `--nat extip:{}`", &self.args.arg_nat[6..]))
}
} else {
None
};
Expand Down Expand Up @@ -1844,6 +1853,33 @@ mod tests {
assert_eq!(conf1.ipfs_config().port, 5002);
}

#[test]
fn should_resolve_external_nat_hosts() {
// Ip works
let conf = parse(&["parity", "--nat", "extip:1.1.1.1"]);
assert_eq!(conf.net_addresses().unwrap().1.unwrap().ip().to_string(), "1.1.1.1");
assert_eq!(conf.net_addresses().unwrap().1.unwrap().port(), 30303);

// Ip with port works, port is discarded
let conf = parse(&["parity", "--nat", "extip:192.168.1.1:123"]);
assert_eq!(conf.net_addresses().unwrap().1.unwrap().ip().to_string(), "192.168.1.1");
assert_eq!(conf.net_addresses().unwrap().1.unwrap().port(), 30303);

// Hostname works
let conf = parse(&["parity", "--nat", "extip:ethereum.org"]);
assert!(conf.net_addresses().unwrap().1.is_some());
assert_eq!(conf.net_addresses().unwrap().1.unwrap().port(), 30303);

// Hostname works, garbage at the end is discarded
let conf = parse(&["parity", "--nat", "extip:ethereum.org:whatever bla bla 123"]);
assert!(conf.net_addresses().unwrap().1.is_some());
assert_eq!(conf.net_addresses().unwrap().1.unwrap().port(), 30303);

// Garbage is error
let conf = parse(&["parity", "--nat", "extip:blabla"]);
assert!(conf.net_addresses().is_err());
}

#[test]
fn should_expose_all_servers() {
// given
Expand Down
5 changes: 1 addition & 4 deletions rpc/src/v1/helpers/engine_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ impl ethcore::engines::EngineSigner for EngineSigner {
fn sign(&self, message: ethkey::Message) -> Result<ethkey::Signature, ethkey::Error> {
match self.accounts.sign(self.address, Some(self.password.clone()), message) {
Ok(ok) => Ok(ok),
Err(e) => {
warn!("Unable to sign consensus message: {:?}", e);
Err(ethkey::Error::InvalidSecret)
},
Err(e) => Err(ethkey::Error::InvalidSecret),
}
}

Expand Down

0 comments on commit bee05cb

Please sign in to comment.