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

Remove Populatable and BytesConvertable traits #2019

Merged
merged 2 commits into from
Sep 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ethcore/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ impl Impl for EcRecover {
let s = H256::from_slice(&input[96..128]);

let bit = match v[31] {
27 | 28 if &v.as_slice()[..31] == &[0; 31] => v[31] - 27,
27 | 28 if &v.0[..31] == &[0; 31] => v[31] - 27,
_ => return,
};

let s = Signature::from_rsv(&r, &s, bit);
if s.is_valid() {
if let Ok(p) = ec_recover(&s, &hash) {
let r = p.as_slice().sha3();
let r = p.sha3();

let out_len = min(output.len(), 32);

Expand Down
6 changes: 3 additions & 3 deletions ethcore/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use ethkey::Error as EthkeyError;

pub use types::executed::{ExecutionError, CallError};

#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Copy)]
/// Errors concerning transaction processing.
pub enum TransactionError {
/// Transaction is already imported to the queue
Expand Down Expand Up @@ -87,7 +87,7 @@ impl fmt::Display for TransactionError {
}
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Clone, Copy, Eq)]
/// Errors concerning block processing.
pub enum BlockError {
/// Block has too many uncles.
Expand Down Expand Up @@ -185,7 +185,7 @@ impl fmt::Display for BlockError {
}
}

#[derive(Debug, PartialEq)]
#[derive(Debug, Clone, Copy, PartialEq)]
/// Import to the block queue result
pub enum ImportError {
/// Already in the block chain.
Expand Down
2 changes: 1 addition & 1 deletion ethcore/src/evm/interpreter/gasometer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl<Gas: CostType> Gasometer<Gas> {
instructions::SSTORE => {
let address = H256::from(stack.peek(0));
let newval = stack.peek(1);
let val = U256::from(ext.storage_at(&address).as_slice());
let val = U256::from(&*ext.storage_at(&address));

let gas = if val.is_zero() && !newval.is_zero() {
schedule.sstore_set_gas
Expand Down
10 changes: 5 additions & 5 deletions ethcore/src/evm/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,18 +403,18 @@ impl<Cost: CostType> Interpreter<Cost> {
let offset = stack.pop_back();
let size = stack.pop_back();
let sha3 = self.mem.read_slice(offset, size).sha3();
stack.push(U256::from(sha3.as_slice()));
stack.push(U256::from(&*sha3));
},
instructions::SLOAD => {
let key = H256::from(&stack.pop_back());
let word = U256::from(ext.storage_at(&key).as_slice());
let word = U256::from(&*ext.storage_at(&key));
stack.push(word);
},
instructions::SSTORE => {
let address = H256::from(&stack.pop_back());
let val = stack.pop_back();

let current_val = U256::from(ext.storage_at(&address).as_slice());
let current_val = U256::from(&*ext.storage_at(&address));
// Increase refund for clear
if !self.is_zero(&current_val) && self.is_zero(&val) {
ext.inc_sstore_clears();
Expand Down Expand Up @@ -491,7 +491,7 @@ impl<Cost: CostType> Interpreter<Cost> {
instructions::BLOCKHASH => {
let block_number = stack.pop_back();
let block_hash = ext.blockhash(&block_number);
stack.push(U256::from(block_hash.as_slice()));
stack.push(U256::from(&*block_hash));
},
instructions::COINBASE => {
stack.push(address_to_u256(ext.env_info().author.clone()));
Expand Down Expand Up @@ -807,7 +807,7 @@ fn u256_to_address(value: &U256) -> Address {

#[inline]
fn address_to_u256(value: Address) -> U256 {
U256::from(H256::from(value).as_slice())
U256::from(&*H256::from(value))
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions ethcore/src/pod_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl PodAccount {
let mut stream = RlpStream::new_list(4);
stream.append(&self.nonce);
stream.append(&self.balance);
stream.append(&sec_trie_root(self.storage.iter().map(|(k, v)| (k.to_vec(), encode(&U256::from(v.as_slice())).to_vec())).collect()));
stream.append(&sec_trie_root(self.storage.iter().map(|(k, v)| (k.to_vec(), encode(&U256::from(&**v)).to_vec())).collect()));
stream.append(&self.code.as_ref().unwrap_or(&vec![]).sha3());
stream.out()
}
Expand All @@ -71,7 +71,7 @@ impl PodAccount {
let mut r = H256::new();
let mut t = SecTrieDBMut::new(db, &mut r);
for (k, v) in &self.storage {
if let Err(e) = t.insert(k, &encode(&U256::from(v.as_slice()))) {
if let Err(e) = t.insert(k, &encode(&U256::from(&**v))) {
warn!("Encountered potential DB corruption: {}", e);
}
}
Expand Down
2 changes: 1 addition & 1 deletion ethcore/src/spec/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ impl Spec {
{
let mut t = SecTrieDBMut::new(db, &mut root);
for (address, account) in self.genesis_state.get().iter() {
try!(t.insert(address.as_slice(), &account.rlp()));
try!(t.insert(&**address, &account.rlp()));
}
}
for (address, account) in self.genesis_state.get().iter() {
Expand Down
2 changes: 1 addition & 1 deletion ethcore/src/state/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ impl Account {
// so we can call overloaded `to_bytes` method
let res = match v.is_zero() {
true => t.remove(k),
false => t.insert(k, &encode(&U256::from(v.as_slice()))),
false => t.insert(k, &encode(&U256::from(&*v))),
};

if let Err(e) = res {
Expand Down
8 changes: 3 additions & 5 deletions ethcore/src/types/account_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,10 @@ impl AccountDiff {

// TODO: refactor into something nicer.
fn interpreted_hash(u: &H256) -> String {
use util::bytes::*;

if u <= &H256::from(0xffffffff) {
format!("{} = 0x{:x}", U256::from(u.as_slice()).low_u32(), U256::from(u.as_slice()).low_u32())
format!("{} = 0x{:x}", U256::from(&**u).low_u32(), U256::from(&**u).low_u32())
} else if u <= &H256::from(u64::max_value()) {
format!("{} = 0x{:x}", U256::from(u.as_slice()).low_u64(), U256::from(u.as_slice()).low_u64())
format!("{} = 0x{:x}", U256::from(&**u).low_u64(), U256::from(&**u).low_u64())
// } else if u <= &H256::from("0xffffffffffffffffffffffffffffffffffffffff") {
// format!("@{}", Address::from(u))
} else {
Expand All @@ -113,7 +111,7 @@ fn interpreted_hash(u: &H256) -> String {

impl fmt::Display for AccountDiff {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use util::bytes::*;
use util::bytes::ToPretty;

match self.nonce {
Diff::Born(ref x) => try!(write!(f, " non {}", x)),
Expand Down
3 changes: 1 addition & 2 deletions ethcore/src/types/transaction_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@

use ipc::binary::{BinaryConvertError, BinaryConvertable};
use error::{TransactionError, Error};
use util::Populatable;

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, Copy, PartialEq)]
/// Represents the result of importing transaction.
pub enum TransactionImportResult {
/// Transaction was imported to current queue.
Expand Down
22 changes: 14 additions & 8 deletions ipc/rpc/src/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

//! Binary representation of types

use util::bytes::Populatable;
use util::{U256, U512, H256, H2048, Address};
use std::mem;
use std::collections::{VecDeque, BTreeMap};
Expand Down Expand Up @@ -706,14 +705,21 @@ pub fn serialize<T: BinaryConvertable>(t: &T) -> Result<Vec<u8>, BinaryError> {
#[macro_export]
macro_rules! binary_fixed_size {
($target_ty: ty) => {
impl BinaryConvertable for $target_ty {
impl BinaryConvertable for $target_ty where $target_ty: Copy {
fn from_bytes(bytes: &[u8], _length_stack: &mut ::std::collections::VecDeque<usize>) -> Result<Self, BinaryConvertError> {
match bytes.len().cmp(&::std::mem::size_of::<$target_ty>()) {
let size = ::std::mem::size_of::<$target_ty>();
match bytes.len().cmp(&size) {
::std::cmp::Ordering::Equal => (),
_ => return Err(BinaryConvertError::size(::std::mem::size_of::<$target_ty>(), bytes.len())),
_ => return Err(BinaryConvertError::size(size, bytes.len())),
};
let mut res: Self = unsafe { ::std::mem::uninitialized() };
res.copy_raw(bytes);
let res: Self = unsafe {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the size is checked in advance, how fill with zero is safer than just unintialized (which is no-op)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's strictly safer but in general it's better practice than using uninitialized. LLVM can optimize out the zero-ing so I doubt we'd see a performance penalty. uninitialized imo should be used only for FFI and dirty tricks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well it's most used operation in ipc serialization
Every hash, every vector element go through it, so we better be sure imo

let mut temp = ::std::mem::zeroed();
let temp_ptr = &mut temp as *mut _ as *mut u8;
::std::ptr::copy_nonoverlapping(bytes.as_ptr(), temp_ptr, size);

temp
};

Ok(res)
}

Expand All @@ -731,14 +737,14 @@ macro_rules! binary_fixed_size {
}

/// Fixed-sized version of Handshake struct
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct BinHandshake {
api_version: BinVersion,
protocol_version: BinVersion,
}

/// Shorten version of semver Version without `pre` and `build` information
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct BinVersion {
pub major: u64,
pub minor: u64,
Expand Down
4 changes: 2 additions & 2 deletions sync/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use std::sync::Arc;
use network::{NetworkProtocolHandler, NetworkService, NetworkContext, PeerId,
NetworkConfiguration as BasicNetworkConfiguration, NonReservedPeerMode, NetworkError};
use util::{U256, H256, Populatable};
use util::{U256, H256};
use io::{TimerToken};
use ethcore::client::{BlockChainClient, ChainNotify};
use ethcore::header::BlockNumber;
Expand All @@ -32,7 +32,7 @@ use parking_lot::RwLock;
pub const ETH_PROTOCOL: &'static str = "eth";

/// Sync configuration
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Copy)]
pub struct SyncConfig {
/// Max blocks to download ahead
pub max_download_ahead_blocks: usize,
Expand Down
2 changes: 1 addition & 1 deletion sync/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ pub enum SyncState {
}

/// Syncing status and statistics
#[derive(Clone)]
#[derive(Clone, Copy)]
pub struct SyncStatus {
/// State
pub state: SyncState,
Expand Down
Loading