Skip to content

Instantly share code, notes, and snippets.

@ssbb
Created June 14, 2017 17:04
Show Gist options
  • Save ssbb/d93c6cdfc978001b078a923b3793baaa to your computer and use it in GitHub Desktop.
Save ssbb/d93c6cdfc978001b078a923b3793baaa to your computer and use it in GitHub Desktop.
#[macro_use] extern crate rustler;
#[macro_use] extern crate lazy_static;
extern crate ethash;
extern crate ethcore_util;
use rustler::{NifEnv, NifTerm, NifResult, NifEncoder};
use ethash::{EthashManager};
use ethcore_util::*;
mod atoms {
rustler_atoms! {
atom ok;
}
}
rustler_export_nifs! {
"Elixir.Ethash",
[
("compute_light", 3, compute_light),
],
None
}
static mut ethash: EthashManager = EthashManager::new();
fn compute_light<'a>(env: NifEnv<'a>, args: &[NifTerm<'a>]) -> NifResult<NifTerm<'a>> {
let block_number: u64 = try!(args[0].decode());
let header_hex: String = try!(args[1].decode());
let nonce: u64 = try!(args[2].decode());
let header_hash: H256 = H256::from_str(&header_hex).unwrap();
unsafe {
let result = ethash.compute_light(block_number, &header_hash.0, nonce);
Ok((atoms::ok(), H256(result.value).hex(), H256(result.mix_hash).hex()).encode(env))
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment