Fix JSONRPC I/O.

This commit is contained in:
Gav Wood 2016-03-01 01:15:00 +01:00
parent 394e9c679b
commit 2266d74c2a
4 changed files with 23 additions and 4 deletions

1
Cargo.lock generated
View File

@ -209,6 +209,7 @@ dependencies = [
"ethsync 0.9.99", "ethsync 0.9.99",
"jsonrpc-core 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "jsonrpc-core 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"jsonrpc-http-server 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "jsonrpc-http-server 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_codegen 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde_codegen 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -249,6 +249,11 @@ impl Ethash {
x!(U256::from((U512::one() << 256) / x!(difficulty))) x!(U256::from((U512::one() << 256) / x!(difficulty)))
} }
/// Given the `block_number`, determine the seed hash for Ethash.
pub fn get_seedhash(number: BlockNumber) -> H256 {
Self::from_ethash(ethash::get_seedhash(number))
}
fn to_ethash(hash: H256) -> EH256 { fn to_ethash(hash: H256) -> EH256 {
unsafe { mem::transmute(hash) } unsafe { mem::transmute(hash) }
} }
@ -259,12 +264,17 @@ impl Ethash {
} }
impl Header { impl Header {
/// Get the none field of the header.
pub fn nonce(&self) -> H64 { pub fn nonce(&self) -> H64 {
decode(&self.seal()[1]) decode(&self.seal()[1])
} }
/// Get the mix hash field of the header.
pub fn mix_hash(&self) -> H256 { pub fn mix_hash(&self) -> H256 {
decode(&self.seal()[0]) decode(&self.seal()[0])
} }
/// Set the nonce and mix hash fields of the header.
pub fn set_nonce_and_mix_hash(&mut self, nonce: &H64, mix_hash: &H256) { pub fn set_nonce_and_mix_hash(&mut self, nonce: &H64, mix_hash: &H256) {
self.seal = vec![encode(mix_hash).to_vec(), encode(nonce).to_vec()]; self.seal = vec![encode(mix_hash).to_vec(), encode(nonce).to_vec()];
} }

View File

@ -9,6 +9,7 @@ build = "build.rs"
[lib] [lib]
[dependencies] [dependencies]
log = "0.3"
serde = "0.7.0" serde = "0.7.0"
serde_json = "0.7.0" serde_json = "0.7.0"
jsonrpc-core = "1.2" jsonrpc-core = "1.2"

View File

@ -25,8 +25,7 @@ use util::rlp::encode;
use ethcore::client::*; use ethcore::client::*;
use ethcore::block::{IsBlock}; use ethcore::block::{IsBlock};
use ethcore::views::*; use ethcore::views::*;
extern crate ethash; //#[macro_use] extern crate log;
use self::ethash::get_seedhash;
use ethcore::ethereum::Ethash; use ethcore::ethereum::Ethash;
use ethcore::ethereum::denominations::shannon; use ethcore::ethereum::denominations::shannon;
use v1::traits::{Eth, EthFilter}; use v1::traits::{Eth, EthFilter};
@ -216,6 +215,7 @@ impl Eth for EthClient {
} }
fn work(&self, params: Params) -> Result<Value, Error> { fn work(&self, params: Params) -> Result<Value, Error> {
println!("Work wanted: {:?}", params);
match params { match params {
Params::None => { Params::None => {
let c = take_weak!(self.client); let c = take_weak!(self.client);
@ -224,7 +224,7 @@ impl Eth for EthClient {
Some(ref b) => { Some(ref b) => {
let pow_hash = b.hash(); let pow_hash = b.hash();
let target = Ethash::difficulty_to_boundary(b.block().header().difficulty()); let target = Ethash::difficulty_to_boundary(b.block().header().difficulty());
let seed_hash = get_seedhash(b.block().header().number()); let seed_hash = Ethash::get_seedhash(b.block().header().number());
to_value(&(pow_hash, seed_hash, target)) to_value(&(pow_hash, seed_hash, target))
} }
_ => Err(Error::invalid_params()) _ => Err(Error::invalid_params())
@ -235,6 +235,7 @@ impl Eth for EthClient {
} }
fn submit_work(&self, params: Params) -> Result<Value, Error> { fn submit_work(&self, params: Params) -> Result<Value, Error> {
println!("Work submission: {:?}", params);
from_params::<(H64, H256, H256)>(params).and_then(|(nonce, pow_hash, mix_hash)| { from_params::<(H64, H256, H256)>(params).and_then(|(nonce, pow_hash, mix_hash)| {
let c = take_weak!(self.client); let c = take_weak!(self.client);
let seal = vec![encode(&mix_hash).to_vec(), encode(&nonce).to_vec()]; let seal = vec![encode(&mix_hash).to_vec(), encode(&nonce).to_vec()];
@ -242,7 +243,13 @@ impl Eth for EthClient {
}) })
} }
// fn submit_hashrate(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() } fn submit_hashrate(&self, params: Params) -> Result<Value, Error> {
println!("Hashrate submission: {:?}", params);
from_params::<(Index, H256)>(params).and_then(|(rate, id)| {
println!("Miner {} reports a hash rate of {} H/s", id, rate.value());
to_value(&true)
})
}
} }
/// Eth filter rpc implementation. /// Eth filter rpc implementation.