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

Commit 7ba5652

Browse files
YihaoPengsorpaas
authored andcommitted
Add a new RPC parity_submitWorkDetail similar eth_submitWork but return block hash (#9404)
* add a new RPC `eth_submitWorkDetail`similar `eth_submitWork`. It has more details (block hash, error message, and more in future) in its response and not only the `true` or `false`. * move RPC submitWorkDetail from namespace eth_ to parity_ * remove SubmitDetailResult type; submitWorkDetail return a error when failed * change error message of RPC error `cannot_submit_work`. * remove double imported H256. * put submit_work_detail into a helper to avoid the duplicate codes.
1 parent 1e9aebb commit 7ba5652

File tree

7 files changed

+80
-22
lines changed

7 files changed

+80
-22
lines changed

rpc/src/v1/helpers/errors.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ mod codes {
3535
pub const NO_AUTHOR: i64 = -32002;
3636
pub const NO_NEW_WORK: i64 = -32003;
3737
pub const NO_WORK_REQUIRED: i64 = -32004;
38+
pub const CANNOT_SUBMIT_WORK: i64 = -32005;
3839
pub const UNKNOWN_ERROR: i64 = -32009;
3940
pub const TRANSACTION_ERROR: i64 = -32010;
4041
pub const EXECUTION_ERROR: i64 = -32015;
@@ -197,6 +198,14 @@ pub fn no_work_required() -> Error {
197198
}
198199
}
199200

201+
pub fn cannot_submit_work(err: EthcoreError) -> Error {
202+
Error {
203+
code: ErrorCode::ServerError(codes::CANNOT_SUBMIT_WORK),
204+
message: "Cannot submit work.".into(),
205+
data: Some(Value::String(err.to_string())),
206+
}
207+
}
208+
200209
pub fn not_enough_data() -> Error {
201210
Error {
202211
code: ErrorCode::ServerError(codes::UNSUPPORTED_REQUEST),

rpc/src/v1/helpers/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ mod signer;
3434
mod signing_queue;
3535
mod subscribers;
3636
mod subscription_manager;
37+
mod work;
3738

3839
pub use self::dispatch::{Dispatcher, FullDispatcher};
3940
pub use self::network_settings::NetworkSettings;
@@ -50,6 +51,7 @@ pub use self::signing_queue::{
5051
pub use self::signer::SignerService;
5152
pub use self::subscribers::Subscribers;
5253
pub use self::subscription_manager::GenericPollManager;
54+
pub use self::work::submit_work_detail;
5355

5456
pub fn to_url(address: &Option<::Host>) -> Option<String> {
5557
address.as_ref().map(|host| (**host).to_owned())

rpc/src/v1/helpers/work.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
2+
// This file is part of Parity.
3+
4+
// Parity is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// Parity is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
16+
17+
//! Helpers for submit a POW work.
18+
19+
use std::sync::Arc;
20+
21+
use rlp;
22+
use ethcore::miner::{BlockChainClient, MinerService};
23+
use ethereum_types::{H64 as EthcoreH64, H256 as EthcoreH256};
24+
use jsonrpc_core::Error;
25+
use v1::types::{H64, H256};
26+
use v1::helpers::errors;
27+
28+
// Submit a POW work and return the block's hash
29+
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> {
30+
// TODO [ToDr] Should disallow submissions in case of PoA?
31+
let nonce: EthcoreH64 = nonce.into();
32+
let pow_hash: EthcoreH256 = pow_hash.into();
33+
let mix_hash: EthcoreH256 = mix_hash.into();
34+
trace!(target: "miner", "submit_work_detail: Decoded: nonce={}, pow_hash={}, mix_hash={}", nonce, pow_hash, mix_hash);
35+
let seal = vec![rlp::encode(&mix_hash).into_vec(), rlp::encode(&nonce).into_vec()];
36+
let import = miner.submit_seal(pow_hash, seal)
37+
.and_then(|block| client.import_sealed_block(block));
38+
match import {
39+
Ok(hash) => {
40+
Ok(hash.into())
41+
},
42+
Err(err) => {
43+
warn!(target: "miner", "Cannot submit work - {:?}.", err);
44+
Err(errors::cannot_submit_work(err))
45+
},
46+
}
47+
}

rpc/src/v1/impls/eth.rs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use std::thread;
2020
use std::time::{Instant, Duration, SystemTime, UNIX_EPOCH};
2121
use std::sync::Arc;
2222

23-
use rlp::{self, Rlp};
24-
use ethereum_types::{U256, H64, H256, Address};
23+
use rlp::Rlp;
24+
use ethereum_types::{U256, H256, Address};
2525
use parking_lot::Mutex;
2626

2727
use ethash::{self, SeedHashCompute};
@@ -40,7 +40,7 @@ use jsonrpc_core::{BoxFuture, Result};
4040
use jsonrpc_core::futures::future;
4141
use jsonrpc_macros::Trailing;
4242

43-
use v1::helpers::{errors, limit_logs, fake_sign};
43+
use v1::helpers::{self, errors, limit_logs, fake_sign};
4444
use v1::helpers::dispatch::{FullDispatcher, default_gas_price};
4545
use v1::helpers::block_import::is_major_importing;
4646
use v1::traits::Eth;
@@ -789,22 +789,9 @@ impl<C, SN: ?Sized, S: ?Sized, M, EM, T: StateInfo + 'static> Eth for EthClient<
789789
}
790790

791791
fn submit_work(&self, nonce: RpcH64, pow_hash: RpcH256, mix_hash: RpcH256) -> Result<bool> {
792-
// TODO [ToDr] Should disallow submissions in case of PoA?
793-
let nonce: H64 = nonce.into();
794-
let pow_hash: H256 = pow_hash.into();
795-
let mix_hash: H256 = mix_hash.into();
796-
trace!(target: "miner", "submit_work: Decoded: nonce={}, pow_hash={}, mix_hash={}", nonce, pow_hash, mix_hash);
797-
798-
let seal = vec![rlp::encode(&mix_hash).into_vec(), rlp::encode(&nonce).into_vec()];
799-
let import = self.miner.submit_seal(pow_hash, seal)
800-
.and_then(|block| self.client.import_sealed_block(block));
801-
802-
match import {
803-
Ok(_) => Ok(true),
804-
Err(err) => {
805-
warn!(target: "miner", "Cannot submit work - {:?}.", err);
806-
Ok(false)
807-
},
792+
match helpers::submit_work_detail(&self.client, &self.miner, nonce, pow_hash, mix_hash) {
793+
Ok(_) => Ok(true),
794+
Err(_) => Ok(false),
808795
}
809796
}
810797

rpc/src/v1/impls/light/parity.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use v1::helpers::light_fetch::LightFetch;
3838
use v1::metadata::Metadata;
3939
use v1::traits::Parity;
4040
use v1::types::{
41-
Bytes, U256, U64, H160, H256, H512, CallRequest,
41+
Bytes, U256, U64, H64, H160, H256, H512, CallRequest,
4242
Peers, Transaction, RpcSettings, Histogram,
4343
TransactionStats, LocalTransactionStatus,
4444
BlockNumber, LightBlockNumber, ConsensusCapability, VersionInfo,
@@ -418,4 +418,8 @@ impl Parity for ParityClient {
418418
fn call(&self, _requests: Vec<CallRequest>, _block: Trailing<BlockNumber>) -> Result<Vec<Bytes>> {
419419
Err(errors::light_unimplemented(None))
420420
}
421+
422+
fn submit_work_detail(&self, _nonce: H64, _pow_hash: H256, _mix_hash: H256) -> Result<H256> {
423+
Err(errors::light_unimplemented(None))
424+
}
421425
}

rpc/src/v1/impls/parity.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use v1::helpers::{self, errors, fake_sign, ipfs, SigningQueue, SignerService, Ne
4040
use v1::metadata::Metadata;
4141
use v1::traits::Parity;
4242
use v1::types::{
43-
Bytes, U256, U64, H160, H256, H512, CallRequest,
43+
Bytes, U256, U64, H64, H160, H256, H512, CallRequest,
4444
Peers, Transaction, RpcSettings, Histogram,
4545
TransactionStats, LocalTransactionStatus,
4646
BlockNumber, ConsensusCapability, VersionInfo,
@@ -471,4 +471,8 @@ impl<C, M, U, S> Parity for ParityClient<C, M, U> where
471471
.map(|res| res.into_iter().map(|res| res.output.into()).collect())
472472
.map_err(errors::call)
473473
}
474+
475+
fn submit_work_detail(&self, nonce: H64, pow_hash: H256, mix_hash: H256) -> Result<H256> {
476+
helpers::submit_work_detail(&self.client, &self.miner, nonce, pow_hash, mix_hash)
477+
}
474478
}

rpc/src/v1/traits/parity.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use jsonrpc_core::{BoxFuture, Result};
2222
use jsonrpc_macros::Trailing;
2323

2424
use v1::types::{
25-
H160, H256, H512, U256, U64, Bytes, CallRequest,
25+
H64, H160, H256, H512, U256, U64, Bytes, CallRequest,
2626
Peers, Transaction, RpcSettings, Histogram,
2727
TransactionStats, LocalTransactionStatus,
2828
BlockNumber, ConsensusCapability, VersionInfo,
@@ -224,5 +224,10 @@ build_rpc_trait! {
224224
/// Call contract, returning the output data.
225225
#[rpc(name = "parity_call")]
226226
fn call(&self, Vec<CallRequest>, Trailing<BlockNumber>) -> Result<Vec<Bytes>>;
227+
228+
/// Used for submitting a proof-of-work solution (similar to `eth_submitWork`,
229+
/// but returns block hash on success, and returns an explicit error message on failure).
230+
#[rpc(name = "parity_submitWorkDetail")]
231+
fn submit_work_detail(&self, H64, H256, H256) -> Result<H256>;
227232
}
228233
}

0 commit comments

Comments
 (0)