Skip to content

Commit 48dfad7

Browse files
tomusdrwdebris
authored andcommitted
Use ethereum-types library for uints and hashes (#76)
* Use bigint library for types. * In progress. * Use ethereum-types * Update dependencies.
1 parent 548397a commit 48dfad7

File tree

11 files changed

+50
-302
lines changed

11 files changed

+50
-302
lines changed

Cargo.toml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@ keywords = ["jsonrpc", "web3", "ethereum", "rpc", "client"]
1010
authors = ["Tomasz Drwięga <[email protected]>"]
1111

1212
[dependencies]
13-
arrayvec = "0.3"
14-
ethabi = "4.0"
13+
arrayvec = "0.4"
14+
error-chain = "0.11"
15+
ethabi = "5.1"
16+
ethereum-types = "0.2"
1517
futures = "0.1"
16-
jsonrpc-core = "7.0"
17-
log = "0.3"
18-
parking_lot = "0.4"
19-
rustc-serialize = "0.3"
18+
jsonrpc-core = "8.0.1"
19+
log = "0.4"
20+
parking_lot = "0.5"
21+
rustc-hex = "1.0"
2022
serde = "1.0"
21-
serde_json = "1.0"
2223
serde_derive = "1.0"
24+
serde_json = "1.0"
2325
tokio-timer = "0.1"
24-
error-chain = "0.11.0-rc.2"
2526
# Optional deps
2627
hyper = { version = "0.11", optional = true }
2728
tokio-core = { version = "0.1", optional = true }

examples/contract.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn main() {
1616
// Deploying a contract
1717
let contract = Contract::deploy(web3.eth(), include_bytes!("../src/contract/res/token.json")).unwrap()
1818
.confirmations(4)
19-
.options(Options::with(|mut opt| opt.gas = Some(5_000_000.into())))
19+
.options(Options::with(|opt| opt.value = Some(5.into())))
2020
.execute(bytecode, (
2121
U256::from(1_000_000),
2222
"My Token".to_owned(),

src/contract/deploy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ mod tests {
144144

145145
// when
146146
builder
147-
.options(Options::with(|mut opt| opt.value = Some(5.into())))
147+
.options(Options::with(|opt| opt.value = Some(5.into())))
148148
.confirmations(1)
149149
.execute(vec![1, 2, 3, 4], (U256::from(1_000_000), "My Token".to_owned(), 3u64, "MT".to_owned()), 5.into())
150150
.unwrap()

src/contract/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ mod tests {
196196
let token = contract(&transport);
197197

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

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

240240
let result = {
241241
let token = contract(&transport);

src/contract/tokens.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use arrayvec::ArrayVec;
44
use ethabi::Token;
55
use contract::error::{Error, ErrorKind};
6-
use types::{self, Address, H256, U256, U64};
6+
use types::{Address, H256, U256, U128};
77

88
/// Output type possible to deserialize from Contract ABI
99
pub trait Detokenize {
@@ -143,7 +143,7 @@ impl Tokenizable for H256 {
143143
for (idx, val) in s.drain(..).enumerate() {
144144
data[idx] = val;
145145
}
146-
Ok(H256(data))
146+
Ok(data.into())
147147
},
148148
other => Err(ErrorKind::InvalidOutputType(format!("Expected `H256`, got {:?}", other)).into()),
149149
}
@@ -158,13 +158,13 @@ impl Tokenizable for H256 {
158158
impl Tokenizable for Address {
159159
fn from_token(token: Token) -> Result<Self, Error> {
160160
match token {
161-
Token::Address(data) => Ok(types::H160(data)),
161+
Token::Address(data) => Ok(data),
162162
other => Err(ErrorKind::InvalidOutputType(format!("Expected `Address`, got {:?}", other)).into()),
163163
}
164164
}
165165

166166
fn into_token(self) -> Token {
167-
Token::Address(self.0)
167+
Token::Address(self)
168168
}
169169
}
170170

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

181181
fn into_token(self) -> Token {
182-
let u = U256::from(self.0.as_ref());
183-
Token::Uint(u.0)
182+
Token::Uint(self.into())
184183
}
185184
}
186185
}
187186
}
188187

189188
uint_tokenizable!(U256, "U256");
190-
uint_tokenizable!(U64, "U64");
189+
uint_tokenizable!(U128, "U128");
191190

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

200199
fn into_token(self) -> Token {
201-
let u = U256::from(self);
202-
Token::Uint(u.0)
200+
Token::Uint(self.into())
203201
}
204202
}
205203

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
extern crate arrayvec;
66
extern crate ethabi;
7+
extern crate ethereum_types;
78
extern crate jsonrpc_core as rpc;
89
extern crate parking_lot;
9-
extern crate rustc_serialize;
10+
extern crate rustc_hex;
1011
extern crate serde;
1112
extern crate tokio_timer;
1213

src/transports/ipc.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ impl Ipc {
102102
{
103103
let request = helpers::to_string(&request);
104104
debug!("[{}] Calling: {}", id, request);
105-
106105
let (tx, rx) = futures::oneshot();
107106
self.pending.lock().insert(id, tx);
108107

src/types/block.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use serde::{Serialize, Serializer};
2-
use types::{Bytes, U64, U256, H256, H160, H2048};
2+
use types::{Bytes, U128, U256, H256, H160, H2048};
33

44
/// The block type returned from RPC calls.
55
/// This is generic over a `TX` type.
@@ -26,7 +26,7 @@ pub struct Block<TX> {
2626
#[serde(rename="receiptsRoot")]
2727
pub receipts_root: H256,
2828
/// Block number. None if pending.
29-
pub number: Option<U64>,
29+
pub number: Option<U128>,
3030
/// Gas Used
3131
#[serde(rename="gasUsed")]
3232
pub gas_used: U256,

src/types/bytes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt;
22
use serde::{Serialize, Serializer, Deserialize, Deserializer};
33
use serde::de::{Error, Visitor};
4-
use rustc_serialize::hex::{FromHex, ToHex};
4+
use rustc_hex::{FromHex, ToHex};
55

66
/// Raw bytes wrapper
77
#[derive(Clone, Debug, Default, PartialEq)]

src/types/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ pub use self::sync_state::{SyncState,SyncInfo};
1717
pub use self::transaction::{Transaction, Receipt as TransactionReceipt};
1818
pub use self::transaction_id::TransactionId;
1919
pub use self::transaction_request::{TransactionRequest, CallRequest, TransactionCondition};
20-
pub use self::uint::{H64, H128, H160, H256, H512, H520, H2048, U64, U256};
20+
pub use self::uint::{H64, H128, H160, H256, H512, H520, H2048, U128, U256};
2121
pub use self::work::Work;
2222

2323
/// Address
2424
pub type Address = H160;
2525
/// Index in block
26-
pub type Index = U64;
26+
pub type Index = U128;

0 commit comments

Comments
 (0)