Merge pull request #466 from ethcore/ethash
Get rid of lru_cache dependency
This commit is contained in:
commit
704d6bd069
@ -7,5 +7,4 @@ authors = ["arkpar <arkadiy@ethcore.io"]
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
log = "0.3"
|
log = "0.3"
|
||||||
lru-cache = "0.0"
|
|
||||||
sha3 = { path = "../util/sha3" }
|
sha3 = { path = "../util/sha3" }
|
||||||
|
@ -17,28 +17,39 @@
|
|||||||
//! Ethash implementation
|
//! Ethash implementation
|
||||||
//! See https://github.com/ethereum/wiki/wiki/Ethash
|
//! See https://github.com/ethereum/wiki/wiki/Ethash
|
||||||
extern crate sha3;
|
extern crate sha3;
|
||||||
extern crate lru_cache;
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate log;
|
extern crate log;
|
||||||
mod sizes;
|
mod sizes;
|
||||||
mod compute;
|
mod compute;
|
||||||
|
|
||||||
use lru_cache::LruCache;
|
use std::mem;
|
||||||
use compute::Light;
|
use compute::Light;
|
||||||
pub use compute::{quick_get_difficulty, H256, ProofOfWork, ETHASH_EPOCH_LENGTH};
|
pub use compute::{quick_get_difficulty, H256, ProofOfWork, ETHASH_EPOCH_LENGTH};
|
||||||
|
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
|
struct LightCache {
|
||||||
|
recent_epoch: Option<u64>,
|
||||||
|
recent: Option<Arc<Light>>,
|
||||||
|
prev_epoch: Option<u64>,
|
||||||
|
prev: Option<Arc<Light>>,
|
||||||
|
}
|
||||||
|
|
||||||
/// Lighy/Full cache manager
|
/// Lighy/Full cache manager
|
||||||
pub struct EthashManager {
|
pub struct EthashManager {
|
||||||
lights: Mutex<LruCache<u64, Arc<Light>>>
|
cache: Mutex<LightCache>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EthashManager {
|
impl EthashManager {
|
||||||
/// Create a new new instance of ethash manager
|
/// Create a new new instance of ethash manager
|
||||||
pub fn new() -> EthashManager {
|
pub fn new() -> EthashManager {
|
||||||
EthashManager {
|
EthashManager {
|
||||||
lights: Mutex::new(LruCache::new(2))
|
cache: Mutex::new(LightCache {
|
||||||
|
recent_epoch: None,
|
||||||
|
recent: None,
|
||||||
|
prev_epoch: None,
|
||||||
|
prev: None,
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,8 +61,24 @@ impl EthashManager {
|
|||||||
pub fn compute_light(&self, block_number: u64, header_hash: &H256, nonce: u64) -> ProofOfWork {
|
pub fn compute_light(&self, block_number: u64, header_hash: &H256, nonce: u64) -> ProofOfWork {
|
||||||
let epoch = block_number / ETHASH_EPOCH_LENGTH;
|
let epoch = block_number / ETHASH_EPOCH_LENGTH;
|
||||||
let light = {
|
let light = {
|
||||||
let mut lights = self.lights.lock().unwrap();
|
let mut lights = self.cache.lock().unwrap();
|
||||||
match lights.get_mut(&epoch).map(|l| l.clone()) {
|
let light = match lights.recent_epoch.clone() {
|
||||||
|
Some(ref e) if *e == epoch => lights.recent.clone(),
|
||||||
|
_ => match lights.prev_epoch.clone() {
|
||||||
|
Some(e) if e == epoch => {
|
||||||
|
// swap
|
||||||
|
let t = lights.prev_epoch;
|
||||||
|
lights.prev_epoch = lights.recent_epoch;
|
||||||
|
lights.recent_epoch = t;
|
||||||
|
let t = lights.prev.clone();
|
||||||
|
lights.prev = lights.recent.clone();
|
||||||
|
lights.recent = t;
|
||||||
|
lights.recent.clone()
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
match light {
|
||||||
None => {
|
None => {
|
||||||
let light = match Light::from_file(block_number) {
|
let light = match Light::from_file(block_number) {
|
||||||
Ok(light) => Arc::new(light),
|
Ok(light) => Arc::new(light),
|
||||||
@ -64,7 +91,8 @@ impl EthashManager {
|
|||||||
Arc::new(light)
|
Arc::new(light)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
lights.insert(epoch, light.clone());
|
lights.prev_epoch = mem::replace(&mut lights.recent_epoch, Some(epoch));
|
||||||
|
lights.prev = mem::replace(&mut lights.recent, Some(light.clone()));
|
||||||
light
|
light
|
||||||
}
|
}
|
||||||
Some(light) => light
|
Some(light) => light
|
||||||
@ -73,3 +101,19 @@ impl EthashManager {
|
|||||||
light.compute(header_hash, nonce)
|
light.compute(header_hash, nonce)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_lru() {
|
||||||
|
let ethash = EthashManager::new();
|
||||||
|
let hash = [0u8; 32];
|
||||||
|
ethash.compute_light(1, &hash, 1);
|
||||||
|
ethash.compute_light(50000, &hash, 1);
|
||||||
|
assert_eq!(ethash.cache.lock().unwrap().recent_epoch.unwrap(), 1);
|
||||||
|
assert_eq!(ethash.cache.lock().unwrap().prev_epoch.unwrap(), 0);
|
||||||
|
ethash.compute_light(1, &hash, 1);
|
||||||
|
assert_eq!(ethash.cache.lock().unwrap().recent_epoch.unwrap(), 0);
|
||||||
|
assert_eq!(ethash.cache.lock().unwrap().prev_epoch.unwrap(), 1);
|
||||||
|
ethash.compute_light(70000, &hash, 1);
|
||||||
|
assert_eq!(ethash.cache.lock().unwrap().recent_epoch.unwrap(), 2);
|
||||||
|
assert_eq!(ethash.cache.lock().unwrap().prev_epoch.unwrap(), 0);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user