Skip to content

Commit

Permalink
Merge pull request #149 from AurevoirXavier/develop
Browse files Browse the repository at this point in the history
Patch for #145
  • Loading branch information
hackfisher authored Dec 7, 2019
2 parents 7eff857 + 141daf5 commit 0c535a8
Show file tree
Hide file tree
Showing 14 changed files with 773 additions and 199 deletions.
315 changes: 157 additions & 158 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ panic = 'unwind'
[workspace]
members = [
"core/cli",
"core/ethash",
"core/merkle-mountain-range",
"core/fly-client",
"core/sr-eth-primitives",
Expand Down
29 changes: 29 additions & 0 deletions core/ethash/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "ethash"
description = "An Apache-licensed Ethash implementation."
version = "0.4.0"
authors = ["Wei Tang <[email protected]>"]
license = "Apache-2.0"
edition = "2018"

[dependencies]
byteorder = { version = "1", default-features = false }
rlp = { version = "0.4", default-features = false }
sha3 = { version = "0.8", default-features = false }

ethereum-types = { git = "https://github.com/darwinia-network/parity-common.git", default-features = false }
primitive-types = { git = "https://github.com/darwinia-network/parity-common.git", default-features = false, features = ["rlp"] }

[dev-dependencies]
hex-literal = "0.2.1"

[features]
default = ["std"]
std = [
"byteorder/std",
"rlp/std",
"sha3/std",

"ethereum-types/std",
"primitive-types/std",
]
52 changes: 52 additions & 0 deletions core/ethash/src/dag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use alloc::vec::Vec;
use core::marker::PhantomData;
use ethereum_types::{H256, H64, U256};

pub trait Patch {
fn epoch_length() -> U256;
}

pub struct EthereumPatch;
impl Patch for EthereumPatch {
fn epoch_length() -> U256 {
U256::from(30000)
}
}

pub struct LightDAG<P: Patch> {
epoch: usize,
cache: Vec<u8>,
#[allow(dead_code)]
cache_size: usize,
full_size: usize,
_marker: PhantomData<P>,
}

impl<P: Patch> LightDAG<P> {
pub fn new(number: U256) -> Self {
let epoch = (number / P::epoch_length()).as_usize();
let cache_size = crate::get_cache_size(epoch);
let full_size = crate::get_full_size(epoch);
let seed = crate::get_seedhash(epoch);

let mut cache: Vec<u8> = Vec::with_capacity(cache_size);
cache.resize(cache_size, 0);
crate::make_cache(&mut cache, seed);

Self {
cache,
cache_size,
full_size,
epoch,
_marker: PhantomData,
}
}

pub fn hashimoto(&self, hash: H256, nonce: H64) -> (H256, H256) {
crate::hashimoto_light(hash, nonce, self.full_size, &self.cache)
}

pub fn is_valid_for(&self, number: U256) -> bool {
(number / P::epoch_length()).as_usize() == self.epoch
}
}
Loading

0 comments on commit 0c535a8

Please sign in to comment.