Skip to content

Commit

Permalink
Use ethereum-types library for uints and hashes (#76)
Browse files Browse the repository at this point in the history
* Use bigint library for types.

* In progress.

* Use ethereum-types

* Update dependencies.
  • Loading branch information
tomusdrw authored and debris committed Feb 7, 2018
1 parent 548397a commit 48dfad7
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 302 deletions.
17 changes: 9 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ keywords = ["jsonrpc", "web3", "ethereum", "rpc", "client"]
authors = ["Tomasz Drwięga <tomasz@ethcore.io>"]

[dependencies]
arrayvec = "0.3"
ethabi = "4.0"
arrayvec = "0.4"
error-chain = "0.11"
ethabi = "5.1"
ethereum-types = "0.2"
futures = "0.1"
jsonrpc-core = "7.0"
log = "0.3"
parking_lot = "0.4"
rustc-serialize = "0.3"
jsonrpc-core = "8.0.1"
log = "0.4"
parking_lot = "0.5"
rustc-hex = "1.0"
serde = "1.0"
serde_json = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
tokio-timer = "0.1"
error-chain = "0.11.0-rc.2"
# Optional deps
hyper = { version = "0.11", optional = true }
tokio-core = { version = "0.1", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion examples/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() {
// Deploying a contract
let contract = Contract::deploy(web3.eth(), include_bytes!("../src/contract/res/token.json")).unwrap()
.confirmations(4)
.options(Options::with(|mut opt| opt.gas = Some(5_000_000.into())))
.options(Options::with(|opt| opt.value = Some(5.into())))
.execute(bytecode, (
U256::from(1_000_000),
"My Token".to_owned(),
Expand Down
2 changes: 1 addition & 1 deletion src/contract/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ mod tests {

// when
builder
.options(Options::with(|mut opt| opt.value = Some(5.into())))
.options(Options::with(|opt| opt.value = Some(5.into())))
.confirmations(1)
.execute(vec![1, 2, 3, 4], (U256::from(1_000_000), "My Token".to_owned(), 3u64, "MT".to_owned()), 5.into())
.unwrap()
Expand Down
6 changes: 3 additions & 3 deletions src/contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ mod tests {
let token = contract(&transport);

// when
token.query("name", (), Address::from(5), Options::with(|mut options| {
token.query("name", (), Address::from(5), Options::with(|options| {
options.gas_price = Some(10_000_000.into());
}), BlockNumber::Latest).wait().unwrap()
};
Expand All @@ -214,7 +214,7 @@ mod tests {
fn should_call_a_contract_function() {
// given
let mut transport = TestTransport::default();
transport.set_response(rpc::Value::String(format!("{:?}", H256::from(5))));
transport.set_response(rpc::Value::String(format!("0x{:?}", H256::from(5))));

let result = {
let token = contract(&transport);
Expand All @@ -235,7 +235,7 @@ mod tests {
fn should_estimate_gas_usage() {
// given
let mut transport = TestTransport::default();
transport.set_response(rpc::Value::String(format!("{:?}", U256::from(5))));
transport.set_response(rpc::Value::String(format!("0x{:?}", U256::from(5))));

let result = {
let token = contract(&transport);
Expand Down
20 changes: 9 additions & 11 deletions src/contract/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use arrayvec::ArrayVec;
use ethabi::Token;
use contract::error::{Error, ErrorKind};
use types::{self, Address, H256, U256, U64};
use types::{Address, H256, U256, U128};

/// Output type possible to deserialize from Contract ABI
pub trait Detokenize {
Expand Down Expand Up @@ -143,7 +143,7 @@ impl Tokenizable for H256 {
for (idx, val) in s.drain(..).enumerate() {
data[idx] = val;
}
Ok(H256(data))
Ok(data.into())
},
other => Err(ErrorKind::InvalidOutputType(format!("Expected `H256`, got {:?}", other)).into()),
}
Expand All @@ -158,13 +158,13 @@ impl Tokenizable for H256 {
impl Tokenizable for Address {
fn from_token(token: Token) -> Result<Self, Error> {
match token {
Token::Address(data) => Ok(types::H160(data)),
Token::Address(data) => Ok(data),
other => Err(ErrorKind::InvalidOutputType(format!("Expected `Address`, got {:?}", other)).into()),
}
}

fn into_token(self) -> Token {
Token::Address(self.0)
Token::Address(self)
}
}

Expand All @@ -173,33 +173,31 @@ macro_rules! uint_tokenizable {
impl Tokenizable for $uint {
fn from_token(token: Token) -> Result<Self, Error> {
match token {
Token::Int(data) | Token::Uint(data) => Ok($uint::from(data.as_ref())),
Token::Int(data) | Token::Uint(data) => Ok(data.into()),
other => Err(ErrorKind::InvalidOutputType(format!("Expected `{}`, got {:?}", $name, other)).into()),
}
}

fn into_token(self) -> Token {
let u = U256::from(self.0.as_ref());
Token::Uint(u.0)
Token::Uint(self.into())
}
}
}
}

uint_tokenizable!(U256, "U256");
uint_tokenizable!(U64, "U64");
uint_tokenizable!(U128, "U128");

impl Tokenizable for u64 {
fn from_token(token: Token) -> Result<Self, Error> {
match token {
Token::Int(data) | Token::Uint(data) => Ok(U256::from(data.as_ref()).low_u64()),
Token::Int(data) | Token::Uint(data) => Ok(data.low_u64()),
other => Err(ErrorKind::InvalidOutputType(format!("Expected `u64`, got {:?}", other)).into()),
}
}

fn into_token(self) -> Token {
let u = U256::from(self);
Token::Uint(u.0)
Token::Uint(self.into())
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

extern crate arrayvec;
extern crate ethabi;
extern crate ethereum_types;
extern crate jsonrpc_core as rpc;
extern crate parking_lot;
extern crate rustc_serialize;
extern crate rustc_hex;
extern crate serde;
extern crate tokio_timer;

Expand Down
1 change: 0 additions & 1 deletion src/transports/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ impl Ipc {
{
let request = helpers::to_string(&request);
debug!("[{}] Calling: {}", id, request);

let (tx, rx) = futures::oneshot();
self.pending.lock().insert(id, tx);

Expand Down
4 changes: 2 additions & 2 deletions src/types/block.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use serde::{Serialize, Serializer};
use types::{Bytes, U64, U256, H256, H160, H2048};
use types::{Bytes, U128, U256, H256, H160, H2048};

/// The block type returned from RPC calls.
/// This is generic over a `TX` type.
Expand All @@ -26,7 +26,7 @@ pub struct Block<TX> {
#[serde(rename="receiptsRoot")]
pub receipts_root: H256,
/// Block number. None if pending.
pub number: Option<U64>,
pub number: Option<U128>,
/// Gas Used
#[serde(rename="gasUsed")]
pub gas_used: U256,
Expand Down
2 changes: 1 addition & 1 deletion src/types/bytes.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt;
use serde::{Serialize, Serializer, Deserialize, Deserializer};
use serde::de::{Error, Visitor};
use rustc_serialize::hex::{FromHex, ToHex};
use rustc_hex::{FromHex, ToHex};

/// Raw bytes wrapper
#[derive(Clone, Debug, Default, PartialEq)]
Expand Down
4 changes: 2 additions & 2 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ pub use self::sync_state::{SyncState,SyncInfo};
pub use self::transaction::{Transaction, Receipt as TransactionReceipt};
pub use self::transaction_id::TransactionId;
pub use self::transaction_request::{TransactionRequest, CallRequest, TransactionCondition};
pub use self::uint::{H64, H128, H160, H256, H512, H520, H2048, U64, U256};
pub use self::uint::{H64, H128, H160, H256, H512, H520, H2048, U128, U256};
pub use self::work::Work;

/// Address
pub type Address = H160;
/// Index in block
pub type Index = U64;
pub type Index = U128;

0 comments on commit 48dfad7

Please sign in to comment.