dissolve util (#7460)

* ethereum-types refactor in progress

* ethereum-types refactor in progress

* ethereum-types refactor in progress

* ethereum-types refactor in progress

* ethereum-types refactor finished

* removed obsolete util/src/lib.rs

* removed commented out code
This commit is contained in:
Marek Kotewicz
2018-01-10 13:35:18 +01:00
committed by Svyatoslav Nikolsky
parent 4a4d64be56
commit e95b093483
395 changed files with 744 additions and 1913 deletions

View File

@@ -15,7 +15,7 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
//! Rust code contract generator.
//! The code generated will require a dependence on the `ethcore-bigint::prelude`,
//! The code generated will require a dependence on the `ethcore-ethereum_types`,
//! `ethabi`, `byteorder`, and `futures` crates.
//! This currently isn't hygienic, so compilation of generated code may fail
//! due to missing crates or name collisions. This will change when
@@ -47,7 +47,7 @@ pub fn generate_module(struct_name: &str, abi: &str) -> Result<String, Error> {
use byteorder::{{BigEndian, ByteOrder}};
use futures::{{future, Future, IntoFuture}};
use ethabi::{{Bytes, Contract, Token, Event}};
use bigint;
use ethereum_types;
type BoxFuture<A, B> = Box<Future<Item = A, Error = B> + Send>;
@@ -56,7 +56,7 @@ type BoxFuture<A, B> = Box<Future<Item = A, Error = B> + Send>;
pub struct {name} {{
contract: Contract,
/// Address to make calls to.
pub address: bigint::prelude::H160,
pub address: ethereum_types::H160,
}}
const ABI: &'static str = r#"{abi_str}"#;
@@ -64,7 +64,7 @@ const ABI: &'static str = r#"{abi_str}"#;
impl {name} {{
/// Create a new instance of `{name}` with an address.
/// Calls can be made, given a callback for dispatching calls asynchronously.
pub fn new(address: bigint::prelude::H160) -> Self {{
pub fn new(address: ethereum_types::H160) -> Self {{
let contract = Contract::load(ABI.as_bytes())
.expect("ABI checked at generation-time; qed");
{name} {{
@@ -109,7 +109,7 @@ fn generate_functions(contract: &Contract) -> Result<String, Error> {
/// Outputs: {abi_outputs:?}
pub fn {snake_name}<F, U>(&self, call: F, {params}) -> BoxFuture<{output_type}, String>
where
F: FnOnce(bigint::prelude::H160, Vec<u8>) -> U,
F: FnOnce(ethereum_types::H160, Vec<u8>) -> U,
U: IntoFuture<Item=Vec<u8>, Error=String>,
U::Future: Send + 'static
{{
@@ -231,8 +231,8 @@ fn output_params_codegen(outputs: &[ParamType]) -> Result<(String, String), Para
// create code for an argument type from param type.
fn rust_type(input: ParamType) -> Result<String, ParamType> {
Ok(match input {
ParamType::Address => "bigint::prelude::H160".into(),
ParamType::FixedBytes(len) if len <= 32 => format!("bigint::prelude::H{}", len * 8),
ParamType::Address => "ethereum_types::H160".into(),
ParamType::FixedBytes(len) if len <= 32 => format!("ethereum_types::H{}", len * 8),
ParamType::Bytes | ParamType::FixedBytes(_) => "Vec<u8>".into(),
ParamType::Int(width) => match width {
8 | 16 | 32 | 64 => format!("i{}", width),
@@ -240,7 +240,7 @@ fn rust_type(input: ParamType) -> Result<String, ParamType> {
},
ParamType::Uint(width) => match width {
8 | 16 | 32 | 64 => format!("u{}", width),
128 | 160 | 256 => format!("bigint::prelude::U{}", width),
128 | 160 | 256 => format!("ethereum_types::U{}", width),
_ => return Err(ParamType::Uint(width)),
},
ParamType::Bool => "bool".into(),
@@ -273,8 +273,8 @@ fn tokenize(name: &str, input: ParamType) -> (bool, String) {
},
ParamType::Uint(width) => format!(
"let mut r = [0; 32]; {}.to_big_endian(&mut r); Token::Uint(r)",
if width <= 64 { format!("bigint::prelude::U256::from({} as u64)", name) }
else { format!("bigint::prelude::U256::from({})", name) }
if width <= 64 { format!("ethereum_types::U256::from({} as u64)", name) }
else { format!("ethereum_types::U256::from({})", name) }
),
ParamType::Bool => format!("Token::Bool({})", name),
ParamType::String => format!("Token::String({})", name),
@@ -295,11 +295,11 @@ fn tokenize(name: &str, input: ParamType) -> (bool, String) {
// panics on unsupported types.
fn detokenize(name: &str, output_type: ParamType) -> String {
match output_type {
ParamType::Address => format!("{}.to_address().map(bigint::prelude::H160)", name),
ParamType::Address => format!("{}.to_address().map(ethereum_types::H160)", name),
ParamType::Bytes => format!("{}.to_bytes()", name),
ParamType::FixedBytes(len) if len <= 32 => {
// ensure no panic on slice too small.
let read_hash = format!("b.resize({}, 0); bigint::prelude::H{}::from_slice(&b[..{}])",
let read_hash = format!("b.resize({}, 0); ethereum_types::H{}::from_slice(&b[..{}])",
len, len * 8, len);
format!("{}.to_fixed_bytes().map(|mut b| {{ {} }})",
@@ -318,7 +318,7 @@ fn detokenize(name: &str, output_type: ParamType) -> String {
let read_uint = match width {
8 => "u[31] as u8".into(),
16 | 32 | 64 => format!("BigEndian::read_u{}(&u[{}..])", width, 32 - (width / 8)),
_ => format!("bigint::prelude::U{}::from(&u[..])", width),
_ => format!("ethereum_types::U{}::from(&u[..])", width),
};
format!("{}.to_uint().map(|u| {})", name, read_uint)
@@ -343,30 +343,30 @@ mod tests {
#[test]
fn input_types() {
assert_eq!(::input_params_codegen(&[]).unwrap().0, "");
assert_eq!(::input_params_codegen(&[ParamType::Address]).unwrap().0, "param_0: bigint::prelude::H160, ");
assert_eq!(::input_params_codegen(&[ParamType::Address]).unwrap().0, "param_0: ethereum_types::H160, ");
assert_eq!(::input_params_codegen(&[ParamType::Address, ParamType::Bytes]).unwrap().0,
"param_0: bigint::prelude::H160, param_1: Vec<u8>, ");
"param_0: ethereum_types::H160, param_1: Vec<u8>, ");
}
#[test]
fn output_types() {
assert_eq!(::output_params_codegen(&[]).unwrap().0, "()");
assert_eq!(::output_params_codegen(&[ParamType::Address]).unwrap().0, "(bigint::prelude::H160)");
assert_eq!(::output_params_codegen(&[ParamType::Address]).unwrap().0, "(ethereum_types::H160)");
assert_eq!(::output_params_codegen(&[ParamType::Address, ParamType::Array(Box::new(ParamType::Bytes))]).unwrap().0,
"(bigint::prelude::H160, Vec<Vec<u8>>)");
"(ethereum_types::H160, Vec<Vec<u8>>)");
}
#[test]
fn rust_type() {
assert_eq!(::rust_type(ParamType::FixedBytes(32)).unwrap(), "bigint::prelude::H256");
assert_eq!(::rust_type(ParamType::FixedBytes(32)).unwrap(), "ethereum_types::H256");
assert_eq!(::rust_type(ParamType::Array(Box::new(ParamType::FixedBytes(32)))).unwrap(),
"Vec<bigint::prelude::H256>");
"Vec<ethereum_types::H256>");
assert_eq!(::rust_type(ParamType::Uint(64)).unwrap(), "u64");
assert!(::rust_type(ParamType::Uint(63)).is_err());
assert_eq!(::rust_type(ParamType::Int(32)).unwrap(), "i32");
assert_eq!(::rust_type(ParamType::Uint(256)).unwrap(), "bigint::prelude::U256");
assert_eq!(::rust_type(ParamType::Uint(256)).unwrap(), "ethereum_types::U256");
}
// codegen tests will need bootstrapping of some kind.