eth_getwork implemented.

This commit is contained in:
Gav Wood 2016-02-29 19:30:13 +01:00
parent 2c32b0fc1c
commit ffc5c2ea7b
9 changed files with 78 additions and 14 deletions

1
Cargo.lock generated
View File

@ -203,6 +203,7 @@ name = "ethcore-rpc"
version = "0.9.99" version = "0.9.99"
dependencies = [ dependencies = [
"clippy 0.0.44 (registry+https://github.com/rust-lang/crates.io-index)", "clippy 0.0.44 (registry+https://github.com/rust-lang/crates.io-index)",
"ethash 0.9.99",
"ethcore 0.9.99", "ethcore 0.9.99",
"ethcore-util 0.9.99", "ethcore-util 0.9.99",
"ethsync 0.9.99", "ethsync 0.9.99",

View File

@ -172,7 +172,8 @@ fn get_data_size(block_number: u64) -> usize {
} }
#[inline] #[inline]
fn get_seedhash(block_number: u64) -> H256 { /// Given the `block_number`, determine the seed hash for Ethash.
pub fn get_seedhash(block_number: u64) -> H256 {
let epochs = block_number / ETHASH_EPOCH_LENGTH; let epochs = block_number / ETHASH_EPOCH_LENGTH;
let mut ret: H256 = [0u8; 32]; let mut ret: H256 = [0u8; 32];
for _ in 0..epochs { for _ in 0..epochs {

View File

@ -24,7 +24,7 @@ mod compute;
use std::mem; use std::mem;
use compute::Light; use compute::Light;
pub use compute::{quick_get_difficulty, H256, ProofOfWork, ETHASH_EPOCH_LENGTH}; pub use compute::{get_seedhash, quick_get_difficulty, H256, ProofOfWork, ETHASH_EPOCH_LENGTH};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
@ -35,7 +35,7 @@ struct LightCache {
prev: Option<Arc<Light>>, prev: Option<Arc<Light>>,
} }
/// Lighy/Full cache manager /// Light/Full cache manager.
pub struct EthashManager { pub struct EthashManager {
cache: Mutex<LightCache>, cache: Mutex<LightCache>,
} }

View File

@ -195,7 +195,7 @@ pub struct Client {
panic_handler: Arc<PanicHandler>, panic_handler: Arc<PanicHandler>,
// for sealing... // for sealing...
_sealing_block: Mutex<Option<ClosedBlock>>, sealing_block: Mutex<Option<ClosedBlock>>,
} }
const HISTORY: u64 = 1000; const HISTORY: u64 = 1000;
@ -232,7 +232,7 @@ impl Client {
report: RwLock::new(Default::default()), report: RwLock::new(Default::default()),
import_lock: Mutex::new(()), import_lock: Mutex::new(()),
panic_handler: panic_handler, panic_handler: panic_handler,
_sealing_block: Mutex::new(None), sealing_block: Mutex::new(None),
})) }))
} }
@ -417,9 +417,7 @@ impl Client {
/// New chain head event. /// New chain head event.
pub fn new_chain_head(&self) { pub fn new_chain_head(&self) {
let h = self.chain.read().unwrap().best_block_hash(); let h = self.chain.read().unwrap().best_block_hash();
info!("NEW CHAIN HEAD: #{}: {}", self.chain.read().unwrap().best_block_number(), h); info!("New best block: #{}: {}", self.chain.read().unwrap().best_block_number(), h);
info!("Preparing to seal.");
let b = OpenBlock::new( let b = OpenBlock::new(
self.engine.deref().deref(), self.engine.deref().deref(),
self.state_db.lock().unwrap().clone(), self.state_db.lock().unwrap().clone(),
@ -430,8 +428,11 @@ impl Client {
); );
let b = b.close(); let b = b.close();
info!("Sealed: hash={}, diff={}, number={}", b.hash(), b.block().header().difficulty(), b.block().header().number()); info!("Sealed: hash={}, diff={}, number={}", b.hash(), b.block().header().difficulty(), b.block().header().number());
*self._sealing_block.lock().unwrap() = Some(b); *self.sealing_block.lock().unwrap() = Some(b);
} }
/// Grab the `ClosedBlock` that we want to be sealed. Comes as a mutex that you have to lock.
pub fn sealing_block(&self) -> &Mutex<Option<ClosedBlock>> { &self.sealing_block }
} }
// TODO: need MinerService MinerIoHandler // TODO: need MinerService MinerIoHandler

View File

@ -144,9 +144,9 @@ impl Engine for Ethash {
} }
let difficulty = Ethash::boundary_to_difficulty(&Ethash::from_ethash(quick_get_difficulty( let difficulty = Ethash::boundary_to_difficulty(&Ethash::from_ethash(quick_get_difficulty(
&Ethash::to_ethash(header.bare_hash()), &Ethash::to_ethash(header.bare_hash()),
header.nonce().low_u64(), header.nonce().low_u64(),
&Ethash::to_ethash(header.mix_hash())))); &Ethash::to_ethash(header.mix_hash()) )));
if difficulty < header.difficulty { if difficulty < header.difficulty {
return Err(From::from(BlockError::InvalidProofOfWork(OutOfBounds { min: Some(header.difficulty), max: None, found: difficulty }))); return Err(From::from(BlockError::InvalidProofOfWork(OutOfBounds { min: Some(header.difficulty), max: None, found: difficulty })));
} }
@ -241,10 +241,16 @@ impl Ethash {
target target
} }
fn boundary_to_difficulty(boundary: &H256) -> U256 { /// Convert an Ethash boundary to its original difficulty. Basically just `f(x) = 2^256 / x`.
pub fn boundary_to_difficulty(boundary: &H256) -> U256 {
U256::from((U512::one() << 256) / x!(U256::from(boundary.as_slice()))) U256::from((U512::one() << 256) / x!(U256::from(boundary.as_slice())))
} }
/// Convert an Ethash difficulty to the target boundary. Basically just `f(x) = 2^256 / x`.
pub fn difficulty_to_boundary(difficulty: &U256) -> H256 {
x!(U256::from((U512::one() << 256) / x!(difficulty)))
}
fn to_ethash(hash: H256) -> EH256 { fn to_ethash(hash: H256) -> EH256 {
unsafe { mem::transmute(hash) } unsafe { mem::transmute(hash) }
} }

View File

@ -15,6 +15,7 @@ jsonrpc-core = "1.2"
jsonrpc-http-server = "2.1" jsonrpc-http-server = "2.1"
ethcore-util = { path = "../util" } ethcore-util = { path = "../util" }
ethcore = { path = "../ethcore" } ethcore = { path = "../ethcore" }
ethash = { path = "../ethash" }
ethsync = { path = "../sync" } ethsync = { path = "../sync" }
clippy = { version = "0.0.44", optional = true } clippy = { version = "0.0.44", optional = true }
rustc-serialize = "0.3" rustc-serialize = "0.3"

View File

@ -22,7 +22,11 @@ use util::hash::*;
use util::uint::*; use util::uint::*;
use util::sha3::*; use util::sha3::*;
use ethcore::client::*; use ethcore::client::*;
use ethcore::block::{IsBlock};
use ethcore::views::*; use ethcore::views::*;
extern crate ethash;
use self::ethash::get_seedhash;
use ethcore::ethereum::Ethash;
use ethcore::ethereum::denominations::shannon; use ethcore::ethereum::denominations::shannon;
use v1::traits::{Eth, EthFilter}; use v1::traits::{Eth, EthFilter};
use v1::types::{Block, BlockTransactions, BlockNumber, Bytes, SyncStatus, SyncInfo, Transaction, OptionalValue, Index, Filter, Log}; use v1::types::{Block, BlockTransactions, BlockNumber, Bytes, SyncStatus, SyncInfo, Transaction, OptionalValue, Index, Filter, Log};
@ -209,6 +213,29 @@ impl Eth for EthClient {
to_value(&logs) to_value(&logs)
}) })
} }
fn work(&self, params: Params) -> Result<Value, Error> {
match params {
Params::None => {
let c = take_weak!(self.client);
let u = c.sealing_block().lock().unwrap();
match *u {
Some(ref b) => {
let current_hash = b.hash();
let target = Ethash::difficulty_to_boundary(b.block().header().difficulty());
let seed_hash = get_seedhash(b.block().header().number());
to_value(&(current_hash, seed_hash, target))
}
_ => Err(Error::invalid_params())
}
},
_ => Err(Error::invalid_params())
}
}
// fn submit_work(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
// fn submit_hashrate(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
} }
/// Eth filter rpc implementation. /// Eth filter rpc implementation.

View File

@ -69,5 +69,5 @@ pub fn contents(name: &str) -> Result<Bytes, UtilError> {
/// Get the standard version string for this software. /// Get the standard version string for this software.
pub fn version() -> String { pub fn version() -> String {
format!("Parity//{}-{}-{}/{}-{}-{}/rustc{}", env!("CARGO_PKG_VERSION"), short_sha(), commit_date().replace("-", ""), Target::arch(), Target::os(), Target::env(), rustc_version::version()) format!("Parity//v{}-{}-{}/{}-{}-{}/rustc{}", env!("CARGO_PKG_VERSION"), short_sha(), commit_date().replace("-", ""), Target::arch(), Target::os(), Target::env(), rustc_version::version())
} }

View File

@ -1124,6 +1124,33 @@ impl From<U512> for U256 {
} }
} }
impl<'a> From<&'a U256> for U512 {
fn from(value: &'a U256) -> U512 {
let U256(ref arr) = *value;
let mut ret = [0; 8];
ret[0] = arr[0];
ret[1] = arr[1];
ret[2] = arr[2];
ret[3] = arr[3];
U512(ret)
}
}
impl<'a> From<&'a U512> for U256 {
fn from(value: &'a U512) -> U256 {
let U512(ref arr) = *value;
if arr[4] | arr[5] | arr[6] | arr[7] != 0 {
panic!("Overflow");
}
let mut ret = [0; 4];
ret[0] = arr[0];
ret[1] = arr[1];
ret[2] = arr[2];
ret[3] = arr[3];
U256(ret)
}
}
impl From<U256> for U128 { impl From<U256> for U128 {
fn from(value: U256) -> U128 { fn from(value: U256) -> U128 {
let U256(ref arr) = value; let U256(ref arr) = value;