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

Add a new RPC parity_submitWorkDetail similar eth_submitWork but return block hash #9404

Merged
merged 7 commits into from
Oct 2, 2018
9 changes: 9 additions & 0 deletions rpc/src/v1/helpers/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ mod codes {
pub const NO_AUTHOR: i64 = -32002;
pub const NO_NEW_WORK: i64 = -32003;
pub const NO_WORK_REQUIRED: i64 = -32004;
pub const CANNOT_SUBMIT_WORK: i64 = -32005;
pub const UNKNOWN_ERROR: i64 = -32009;
pub const TRANSACTION_ERROR: i64 = -32010;
pub const EXECUTION_ERROR: i64 = -32015;
Expand Down Expand Up @@ -187,6 +188,14 @@ pub fn no_work_required() -> Error {
}
}

pub fn cannot_submit_work(err: EthcoreError) -> Error {
Error {
code: ErrorCode::ServerError(codes::CANNOT_SUBMIT_WORK),
message: "Cannot submit work.".into(),
data: Some(Value::String(err.to_string())),
}
}

pub fn not_enough_data() -> Error {
Error {
code: ErrorCode::ServerError(codes::UNSUPPORTED_REQUEST),
Expand Down
6 changes: 5 additions & 1 deletion rpc/src/v1/impls/light/parity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use v1::helpers::light_fetch::LightFetch;
use v1::metadata::Metadata;
use v1::traits::Parity;
use v1::types::{
Bytes, U256, U64, H160, H256, H512, CallRequest,
Bytes, U256, U64, H64, H160, H256, H512, CallRequest,
Peers, Transaction, RpcSettings, Histogram,
TransactionStats, LocalTransactionStatus,
BlockNumber, ConsensusCapability, VersionInfo,
Expand Down Expand Up @@ -422,4 +422,8 @@ impl Parity for ParityClient {
fn call(&self, _requests: Vec<CallRequest>, _block: Trailing<BlockNumber>) -> Result<Vec<Bytes>> {
Err(errors::light_unimplemented(None))
}

fn submit_work_detail(&self, _nonce: H64, _pow_hash: H256, _mix_hash: H256) -> Result<H256> {
Err(errors::light_unimplemented(None))
}
}
27 changes: 25 additions & 2 deletions rpc/src/v1/impls/parity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use std::sync::Arc;
use std::str::FromStr;
use std::collections::{BTreeMap, HashSet};

use ethereum_types::Address;
use rlp::{self};
use ethereum_types::{Address, H64 as EthcoreH64, H256 as EthcoreH256};
use version::version_data;

use crypto::DEFAULT_MAC;
Expand All @@ -40,7 +41,7 @@ use v1::helpers::{self, errors, fake_sign, ipfs, SigningQueue, SignerService, Ne
use v1::metadata::Metadata;
use v1::traits::Parity;
use v1::types::{
Bytes, U256, U64, H160, H256, H512, CallRequest,
Bytes, U256, U64, H64, H160, H256, H512, CallRequest,
Peers, Transaction, RpcSettings, Histogram,
TransactionStats, LocalTransactionStatus,
BlockNumber, ConsensusCapability, VersionInfo,
Expand Down Expand Up @@ -449,4 +450,26 @@ impl<C, M, U, S> Parity for ParityClient<C, M, U> where
.map(|res| res.into_iter().map(|res| res.output.into()).collect())
.map_err(errors::call)
}

fn submit_work_detail(&self, nonce: H64, pow_hash: H256, mix_hash: H256) -> Result<H256> {

This comment was marked as resolved.

Copy link
Contributor Author

@YihaoPeng YihaoPeng Oct 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am trying to make such a change, but I am having trouble.
The question is what type of miner and client should be.
I noticed that in paraity.rs and eth.rs they are all defined as generics and two definitions are different.
I tried to use generics but I encountered an error like this:

pub fn submit_work_detail<C: BlockChainClient, M: MinerService>(client: C, miner: M, nonce: H64, pow_hash: H256, mix_hash: H256) -> Result<H256, Error> {...}
The trait bound `std::sync::Arc<C>: ethcore::miner::BlockChainClient` is not satisfied
rpc/src/v1/impls/eth.rs:785:9
the trait `ethcore::miner::BlockChainClient` is not implemented for `std::sync::Arc<C>`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I have got it. This works:

pub fn submit_work_detail<C: BlockChainClient, M: MinerService>(client: &Arc<C>, miner: &Arc<M>, nonce: H64, pow_hash: H256, mix_hash: H256) -> Result<H256, Error> {...}

// TODO [ToDr] Should disallow submissions in case of PoA?
let nonce: EthcoreH64 = nonce.into();
let pow_hash: EthcoreH256 = pow_hash.into();
let mix_hash: EthcoreH256 = mix_hash.into();
trace!(target: "miner", "submit_work_detail: Decoded: nonce={}, pow_hash={}, mix_hash={}", nonce, pow_hash, mix_hash);

let seal = vec![rlp::encode(&mix_hash).into_vec(), rlp::encode(&nonce).into_vec()];
let import = self.miner.submit_seal(pow_hash, seal)
.and_then(|block| self.client.import_sealed_block(block));

match import {
YihaoPeng marked this conversation as resolved.
Show resolved Hide resolved
Ok(hash) => {
Ok(hash.into())
},
Err(err) => {
warn!(target: "miner", "Cannot submit work - {:?}.", err);
Err(errors::cannot_submit_work(err))
},
}
}
}
7 changes: 6 additions & 1 deletion rpc/src/v1/traits/parity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use jsonrpc_core::{BoxFuture, Result};
use jsonrpc_macros::Trailing;

use v1::types::{
H160, H256, H512, U256, U64, Bytes, CallRequest,
H64, H160, H256, H512, U256, U64, Bytes, CallRequest,
Peers, Transaction, RpcSettings, Histogram,
TransactionStats, LocalTransactionStatus,
BlockNumber, ConsensusCapability, VersionInfo,
Expand Down Expand Up @@ -218,5 +218,10 @@ build_rpc_trait! {
/// Call contract, returning the output data.
#[rpc(name = "parity_call")]
fn call(&self, Vec<CallRequest>, Trailing<BlockNumber>) -> Result<Vec<Bytes>>;

/// Used for submitting a proof-of-work solution (similar to `eth_submitWork`,
/// but returns block hash on success, and returns an explicit error message on failure).
#[rpc(name = "parity_submitWorkDetail")]
fn submit_work_detail(&self, H64, H256, H256) -> Result<H256>;
}
}