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

Commit

Permalink
Add ethereum-types (#5)
Browse files Browse the repository at this point in the history
* Add ethereum-types; Modify features config of primitive-types

Signed-off-by: koushiro <[email protected]>

* Modify doc of primitive-types

Signed-off-by: koushiro <[email protected]>

* Update ci config

Signed-off-by: koushiro <[email protected]>
  • Loading branch information
koushiro authored Mar 28, 2019
1 parent 760247f commit 98e7397
Show file tree
Hide file tree
Showing 11 changed files with 668 additions and 10 deletions.
8 changes: 5 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 2.1
jobs:
build:
docker:
- image: ubuntu:16.04
- image: ubuntu:18.04

working_directory: ~/chainx-common-ci

Expand All @@ -12,8 +12,8 @@ jobs:
- run:
name: Setup build environment
command: |
apt-get update
apt-get install -y curl wget build-essential zlib1g-dev python libcurl4-openssl-dev libelf-dev libdw-dev cmake binutils-dev libiberty-dev
apt update
apt install -y curl wget build-essential zlib1g-dev python libcurl4-openssl-dev libelf-dev libdw-dev cmake binutils-dev libiberty-dev
curl https://sh.rustup.rs -sSf | sh -s -- --no-modify-path --default-toolchain none -y;
source $HOME/.cargo/env
no_output_timeout: 1800s
Expand Down Expand Up @@ -41,6 +41,8 @@ jobs:
cd base58 && cargo test && cargo test --no-default-features && cd ..
cd rlp && cargo test && cargo test --no-default-features && cd ..
cd primitive-types && cargo test --features 'serde,codec,rlp' && cargo test --no-default-features --features 'codec,rlp' && cd ..
cd ethereum-types && cargo test --features 'serde,codec,rlp' && cargo test --no-default-features --features 'codec,rlp' && cd ..
- run:
name: Coverage
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ members = [
"primitive-types/impls/serde",
"primitive-types/impls/codec",
"primitive-types/impls/rlp",
"ethereum-types",
]
33 changes: 33 additions & 0 deletions ethereum-types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[package]
name = "ethereum-types"
version = "0.5.0"
authors = ["Parity Technologies <[email protected]> and koushiro <[email protected]>"]
license = "MIT"
homepage = "https://github.com/paritytech/parity-common"
description = "Ethereum types"
edition = "2018"

[dependencies]
ustd = { path = "../ustd", default-features = false }
primitive-types = { path = "../primitive-types", default-features = false }
impl-serde = { path = "../primitive-types/impls/serde", default-features = false, optional = true }
impl-codec = { path = "../primitive-types/impls/codec", default-features = false, optional = true }
impl-rlp = { path = "../primitive-types/impls/rlp", default-features = false, optional = true }
fixed-hash = { version = "0.3", default-features = false }
tiny-keccak = "1.4" # support `no_std`
crunchy = { version = "0.2.1", default-features = false, features = ["limit_256"] }

[dev-dependencies]
rustc-hex = { version = "2.0", default-features = false }
rand = { version = "0.4" }

[features]
default = ["std"]
std = [
"ustd/std",
"fixed-hash/std",
"primitive-types/std",
]
serde = ["std", "impl-serde", "primitive-types/serde"]
codec = ["impl-codec", "primitive-types/codec"]
rlp = ["impl-rlp", "primitive-types/rlp"]
151 changes: 151 additions & 0 deletions ethereum-types/benches/bigint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// Copyright 2015-2017 Parity Technologies
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! benchmarking for bigint
//! should be started with:
//! ```bash
//! rustup run nightly cargo bench
//! ```
#![feature(test)]
#![feature(asm)]

extern crate test;

use test::{black_box, Bencher};

use ethereum_types::{U128, U256, U512};

#[bench]
fn u128_mul(b: &mut Bencher) {
b.iter(|| {
let n = black_box(10000);
(1..n).fold(U128([12345u64, 0u64]), |old, new| {
old.overflowing_mul(U128::from(new | 1)).0
})
});
}

#[bench]
fn u256_add(b: &mut Bencher) {
b.iter(|| {
let n = black_box(10000);
let zero = black_box(U256::zero());
(0..n).fold(zero, |old, new| {
old.overflowing_add(U256::from(black_box(new))).0
})
});
}

#[bench]
fn u256_sub(b: &mut Bencher) {
b.iter(|| {
let n = black_box(10000);
let max = black_box(U256::max_value());
(0..n).fold(max, |old, new| {
old.overflowing_sub(U256::from(black_box(new))).0
})
});
}

#[bench]
fn u256_mul(b: &mut Bencher) {
b.iter(|| {
(1..10000).fold(black_box(U256::one()), |old, new| {
old.overflowing_mul(U256::from(black_box(new | 1))).0
})
});
}

#[bench]
fn u256_mul_small(b: &mut Bencher) {
b.iter(|| {
(1..77).fold(black_box(U256::one()), |old, _| {
old.overflowing_mul(U256::from(black_box(10))).0
})
});
}

#[bench]
fn u256_full_mul(b: &mut Bencher) {
b.iter(|| {
let n = black_box(10000);
let one = black_box(U256::one());
(1..n).map(|n| n | 1).fold(one, |old, new| {
let new = black_box(new);
let U512(ref u512words) = old.full_mul(U256([new, new, new, new]));
U256([u512words[0], u512words[2], u512words[2], u512words[3]])
})
});
}

#[bench]
fn u256_from_le(b: &mut Bencher) {
b.iter(|| {
let raw = black_box([
1u8, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73,
79, 83, 89, 97, 101, 103, 107, 109, 113, 127,
]);
let _ = U256::from_little_endian(&raw[..]);
});
}

#[bench]
fn u256_from_be(b: &mut Bencher) {
b.iter(|| {
let raw = black_box([
1u8, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73,
79, 83, 89, 97, 101, 103, 107, 109, 113, 127,
]);
let _ = U256::from_big_endian(&raw[..]);
});
}

#[bench]
fn u512_add(b: &mut Bencher) {
b.iter(|| {
let n = black_box(10000);
let zero = black_box(U512::zero());
(0..n).fold(zero, |old, new| {
let new = black_box(new);
old.overflowing_add(U512([new, new, new, new, new, new, new, new]))
.0
})
});
}

#[bench]
fn u512_sub(b: &mut Bencher) {
b.iter(|| {
let n = black_box(10000);
let max = black_box(U512::max_value());
(0..n).fold(max, |old, new| {
let new = black_box(new);
let p = new % 2;
old.overflowing_sub(U512([p, p, p, p, p, p, p, new])).0
})
});
}

#[bench]
fn u512_mul(b: &mut Bencher) {
b.iter(|| {
(1..10000).fold(black_box(U512::one()), |old, new| {
old.overflowing_mul(U512::from(black_box(new | 1))).0
})
});
}

#[bench]
fn u512_mul_small(b: &mut Bencher) {
b.iter(|| {
(1..153).fold(black_box(U512::one()), |old, _| {
old.overflowing_mul(U512::from(black_box(10))).0
})
});
}
127 changes: 127 additions & 0 deletions ethereum-types/benches/bloom.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#![feature(test)]

extern crate test;

use test::Bencher;

use ethereum_types::{Bloom, BloomInput};
use tiny_keccak::keccak256;

fn from_hex_str(s: &str) -> Vec<u8> {
rustc_hex::FromHex::from_hex(s).unwrap()
}

fn test_bloom() -> Bloom {
let hex = from_hex_str(
"00000000000000000000000000000000\
00000000100000000000000000000000\
00000000000000000000000000000000\
00000000000000000000000000000000\
00000000000000000000000000000000\
00000000000000000000000000000000\
00000002020000000000000000000000\
00000000000000000000000800000000\
10000000000000000000000000000000\
00000000000000000000001000000000\
00000000000000000000000000000000\
00000000000000000000000000000000\
00000000000000000000000000000000\
00000000000000000000000000000000\
00000000000000000000000000000000\
00000000000000000000000000000000",
);
Bloom::from_slice(&hex)
}

fn test_topic() -> Vec<u8> {
from_hex_str("02c69be41d0b7e40352fc85be1cd65eb03d40ef8427a0ca4596b1ead9a00e9fc")
}

fn test_address() -> Vec<u8> {
from_hex_str("ef2d6d194084c2de36e0dabfce45d046b37d1106")
}

fn test_dummy() -> Vec<u8> {
b"123456".to_vec()
}

fn test_dummy2() -> Vec<u8> {
b"654321".to_vec()
}

#[bench]
fn accrue_raw(b: &mut Bencher) {
let mut bloom = Bloom::default();
let topic = test_topic();
let address = test_address();
b.iter(|| {
bloom.accrue(BloomInput::Raw(&topic));
bloom.accrue(BloomInput::Raw(&address));
});
}

#[bench]
fn accrue_hash(b: &mut Bencher) {
let mut bloom = Bloom::default();
let topic = keccak256(&test_topic());
let address = keccak256(&test_address());
b.iter(|| {
bloom.accrue(BloomInput::Hash(&topic));
bloom.accrue(BloomInput::Hash(&address));
});
}

#[bench]
fn contains_input_raw(b: &mut Bencher) {
let bloom = test_bloom();
let topic = test_topic();
let address = test_address();
b.iter(|| {
assert!(bloom.contains_input(BloomInput::Raw(&topic)));
assert!(bloom.contains_input(BloomInput::Raw(&address)));
});
}

#[bench]
fn does_not_contain_raw(b: &mut Bencher) {
let bloom = test_bloom();
let dummy = test_dummy();
let dummy2 = test_dummy2();
b.iter(|| {
assert!(!bloom.contains_input(BloomInput::Raw(&dummy)));
assert!(!bloom.contains_input(BloomInput::Raw(&dummy2)));
});
}

#[bench]
fn contains_input_hash(b: &mut Bencher) {
let bloom = test_bloom();
let topic = keccak256(&test_topic());
let address = keccak256(&test_address());
b.iter(|| {
assert!(bloom.contains_input(BloomInput::Hash(&topic)));
assert!(bloom.contains_input(BloomInput::Hash(&address)));
});
}

#[bench]
fn does_not_contain_hash(b: &mut Bencher) {
let bloom = test_bloom();
let dummy = keccak256(&test_dummy());
let dummy2 = keccak256(&test_dummy2());
b.iter(|| {
assert!(!bloom.contains_input(BloomInput::Hash(&dummy)));
assert!(!bloom.contains_input(BloomInput::Hash(&dummy2)));
});
}

#[bench]
fn does_not_contain_random_hash(b: &mut Bencher) {
let bloom = test_bloom();
let dummy: Vec<_> = (0..255u8).into_iter().map(|i| keccak256(&[i])).collect();
b.iter(|| {
for d in &dummy {
assert!(!bloom.contains_input(BloomInput::Hash(d)));
}
});
}
Loading

0 comments on commit 98e7397

Please sign in to comment.