Skip to content

Commit 0c535a8

Browse files
authored
Merge pull request #149 from AurevoirXavier/develop
Patch for #145
2 parents 7eff857 + 141daf5 commit 0c535a8

File tree

14 files changed

+773
-199
lines changed

14 files changed

+773
-199
lines changed

Cargo.lock

Lines changed: 157 additions & 158 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ panic = 'unwind'
44
[workspace]
55
members = [
66
"core/cli",
7+
"core/ethash",
78
"core/merkle-mountain-range",
89
"core/fly-client",
910
"core/sr-eth-primitives",

core/ethash/Cargo.toml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[package]
2+
name = "ethash"
3+
description = "An Apache-licensed Ethash implementation."
4+
version = "0.4.0"
5+
authors = ["Wei Tang <[email protected]>"]
6+
license = "Apache-2.0"
7+
edition = "2018"
8+
9+
[dependencies]
10+
byteorder = { version = "1", default-features = false }
11+
rlp = { version = "0.4", default-features = false }
12+
sha3 = { version = "0.8", default-features = false }
13+
14+
ethereum-types = { git = "https://github.com/darwinia-network/parity-common.git", default-features = false }
15+
primitive-types = { git = "https://github.com/darwinia-network/parity-common.git", default-features = false, features = ["rlp"] }
16+
17+
[dev-dependencies]
18+
hex-literal = "0.2.1"
19+
20+
[features]
21+
default = ["std"]
22+
std = [
23+
"byteorder/std",
24+
"rlp/std",
25+
"sha3/std",
26+
27+
"ethereum-types/std",
28+
"primitive-types/std",
29+
]

core/ethash/src/dag.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use alloc::vec::Vec;
2+
use core::marker::PhantomData;
3+
use ethereum_types::{H256, H64, U256};
4+
5+
pub trait Patch {
6+
fn epoch_length() -> U256;
7+
}
8+
9+
pub struct EthereumPatch;
10+
impl Patch for EthereumPatch {
11+
fn epoch_length() -> U256 {
12+
U256::from(30000)
13+
}
14+
}
15+
16+
pub struct LightDAG<P: Patch> {
17+
epoch: usize,
18+
cache: Vec<u8>,
19+
#[allow(dead_code)]
20+
cache_size: usize,
21+
full_size: usize,
22+
_marker: PhantomData<P>,
23+
}
24+
25+
impl<P: Patch> LightDAG<P> {
26+
pub fn new(number: U256) -> Self {
27+
let epoch = (number / P::epoch_length()).as_usize();
28+
let cache_size = crate::get_cache_size(epoch);
29+
let full_size = crate::get_full_size(epoch);
30+
let seed = crate::get_seedhash(epoch);
31+
32+
let mut cache: Vec<u8> = Vec::with_capacity(cache_size);
33+
cache.resize(cache_size, 0);
34+
crate::make_cache(&mut cache, seed);
35+
36+
Self {
37+
cache,
38+
cache_size,
39+
full_size,
40+
epoch,
41+
_marker: PhantomData,
42+
}
43+
}
44+
45+
pub fn hashimoto(&self, hash: H256, nonce: H64) -> (H256, H256) {
46+
crate::hashimoto_light(hash, nonce, self.full_size, &self.cache)
47+
}
48+
49+
pub fn is_valid_for(&self, number: U256) -> bool {
50+
(number / P::epoch_length()).as_usize() == self.epoch
51+
}
52+
}

0 commit comments

Comments
 (0)