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

Commit

Permalink
Remove old cache data (#2081)
Browse files Browse the repository at this point in the history
  • Loading branch information
krl authored and arkpar committed Sep 14, 2016
1 parent 9ed9857 commit 2ba4968
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions ethash/src/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub struct Light {
seed_compute: Mutex<SeedHashCompute>,
}

/// Light cache structur
/// Light cache structure
impl Light {
/// Create a new light cache for a given block number
pub fn new(block_number: u64) -> Light {
Expand Down Expand Up @@ -134,16 +134,24 @@ impl Light {
})
}

pub fn to_file(&self) -> io::Result<()> {
pub fn to_file(&self) -> io::Result<PathBuf> {
let seed_compute = self.seed_compute.lock();
let path = Light::file_path(seed_compute.get_seedhash(self.block_number));

if self.block_number >= ETHASH_EPOCH_LENGTH * 2 {
let deprecated = Light::file_path(
seed_compute.get_seedhash(self.block_number - ETHASH_EPOCH_LENGTH * 2));
debug!(target: "ethash", "removing: {:?}", &deprecated);
try!(fs::remove_file(deprecated));
}

try!(fs::create_dir_all(path.parent().unwrap()));
let mut file = try!(File::create(path));
let mut file = try!(File::create(&path));

let cache_size = self.cache.len() * NODE_BYTES;
let buf = unsafe { slice::from_raw_parts(self.cache.as_ptr() as *const u8, cache_size) };
try!(file.write(buf));
Ok(())
Ok(path)
}
}

Expand Down Expand Up @@ -455,3 +463,18 @@ fn test_seed_compute_after_newer() {
let hash = [241, 175, 44, 134, 39, 121, 245, 239, 228, 236, 43, 160, 195, 152, 46, 7, 199, 5, 253, 147, 241, 206, 98, 43, 3, 104, 17, 40, 192, 79, 106, 162];
assert_eq!(seed_compute.get_seedhash(486382), hash);
}

#[test]
fn test_drop_old_data() {
let first = Light::new(0).to_file().unwrap();

let second = Light::new(ETHASH_EPOCH_LENGTH).to_file().unwrap();
assert!(fs::metadata(&first).is_ok());

let _ = Light::new(ETHASH_EPOCH_LENGTH * 2).to_file();
assert!(fs::metadata(&first).is_err());
assert!(fs::metadata(&second).is_ok());

let _ = Light::new(ETHASH_EPOCH_LENGTH * 3).to_file();
assert!(fs::metadata(&second).is_err());
}

0 comments on commit 2ba4968

Please sign in to comment.