Skip to content

Commit 3079856

Browse files
committed
Implement miner_getWork RPC API
Adjust fields of Work as follows: * Change block_hash field to pow_hash * Change score field to target * Remove block_number field
1 parent 3e3012e commit 3079856

File tree

6 files changed

+62
-5
lines changed

6 files changed

+62
-5
lines changed

core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ extern crate util_error;
5959
extern crate log;
6060

6161
mod account_provider;
62-
mod block;
62+
pub mod block;
6363
mod blockchain;
6464
mod blockchain_info;
6565
mod client;

core/src/miner/miner.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,19 @@ impl MinerService for Miner {
622622
}
623623
}
624624

625+
fn map_sealing_work<C, F, T>(&self, client: &C, f: F) -> Option<T>
626+
where
627+
C: AccountData + BlockChain + BlockProducer,
628+
F: FnOnce(&ClosedBlock) -> T, {
629+
ctrace!(MINER, "map_sealing_work: entering");
630+
self.prepare_work_sealing(client);
631+
ctrace!(MINER, "map_sealing_work: sealing prepared");
632+
let mut sealing_work = self.sealing_work.lock();
633+
let ret = sealing_work.queue.use_last_ref();
634+
ctrace!(MINER, "map_sealing_work: leaving use_last_ref={:?}", ret.as_ref().map(|b| b.block().header().hash()));
635+
ret.map(f)
636+
}
637+
625638
fn submit_seal<C: ImportSealedBlock>(&self, chain: &C, block_hash: H256, seal: Vec<Bytes>) -> Result<(), Error> {
626639
let result = if let Some(b) = self.sealing_work.lock().queue.take_used_if(|b| &b.hash() == &block_hash) {
627640
ctrace!(

core/src/miner/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use ctypes::{Address, Bytes, H256, U256};
2323

2424
pub use self::miner::{Miner, MinerOptions};
2525
use super::account_provider::SignError;
26+
use super::block::ClosedBlock;
2627
use super::client::{AccountData, BlockChain, BlockProducer, ImportSealedBlock, MiningBlockChainClient};
2728
use super::error::Error;
2829
use super::parcel::{SignedParcel, UnverifiedParcel};
@@ -80,6 +81,13 @@ pub trait MinerService: Send + Sync {
8081
/// Will check the seal, but not actually insert the block into the chain.
8182
fn submit_seal<C: ImportSealedBlock>(&self, chain: &C, pow_hash: H256, seal: Vec<Bytes>) -> Result<(), Error>;
8283

84+
/// Get the sealing work package and if `Some`, apply some transform.
85+
fn map_sealing_work<C, F, T>(&self, client: &C, f: F) -> Option<T>
86+
where
87+
C: AccountData + BlockChain + BlockProducer,
88+
F: FnOnce(&ClosedBlock) -> T,
89+
Self: Sized;
90+
8391
/// Imports parcels to mem pool.
8492
fn import_external_parcels<C: MiningBlockChainClient>(
8593
&self,

rpc/src/v1/errors.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17+
use std::fmt;
18+
1719
use ccore::Error as CoreError;
1820
use kvdb::Error as KVDBError;
1921
use rlp::DecoderError;
2022

2123
use jsonrpc_core::{Error, ErrorCode, Value};
2224

2325
mod codes {
26+
pub const NO_AUTHOR: i64 = -32002;
2427
pub const NO_WORK_REQUIRED: i64 = -32004;
2528
pub const UNKNOWN_ERROR: i64 = -32009;
2629
pub const PARCEL_ERROR: i64 = -32010;
@@ -61,6 +64,14 @@ pub fn rlp(error: DecoderError) -> Error {
6164
}
6265
}
6366

67+
pub fn no_author() -> Error {
68+
Error {
69+
code: ErrorCode::ServerError(codes::NO_AUTHOR),
70+
message: "Author not configured. Run Parity with --author to configure.".into(),
71+
data: None,
72+
}
73+
}
74+
6475
pub fn no_work_required() -> Error {
6576
Error {
6677
code: ErrorCode::ServerError(codes::NO_WORK_REQUIRED),
@@ -76,3 +87,14 @@ pub fn network_disabled() -> Error {
7687
data: None,
7788
}
7889
}
90+
91+
/// Internal error signifying a logic error in code.
92+
/// Should not be used when function can just fail
93+
/// because of invalid parameters or incomplete node state.
94+
pub fn internal<T: fmt::Debug>(error: &str, data: T) -> Error {
95+
Error {
96+
code: ErrorCode::InternalError,
97+
message: format!("Internal error occurred: {}", error),
98+
data: Some(Value::String(format!("{:?}", data))),
99+
}
100+
}

rpc/src/v1/impls/miner.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
use std::sync::Arc;
1818

19+
use ccore::block::IsBlock;
1920
use ccore::{self, Client, MinerService};
2021
use ctypes::H256;
2122
use jsonrpc_core::Result;
@@ -44,7 +45,21 @@ impl Miner for MinerClient {
4445
cwarn!(MINER, "Cannot give work package - engine seals internally.");
4546
return Err(errors::no_work_required())
4647
}
47-
unimplemented!();
48+
if self.miner.author().is_zero() {
49+
cwarn!(MINER, "Cannot give work package - no author is configured. Use --author to configure!");
50+
return Err(errors::no_author())
51+
}
52+
self.miner
53+
.map_sealing_work(&*self.client, |b| {
54+
let pow_hash = b.hash();
55+
let target = self.client.engine().score_to_target(b.block().header().score());
56+
57+
Ok(Work {
58+
pow_hash,
59+
target,
60+
})
61+
})
62+
.unwrap_or(Err(errors::internal("No work found.", "")))
4863
}
4964

5065
fn submit_work(&self, pow_hash: H256, seal: Vec<Bytes>) -> Result<bool> {

rpc/src/v1/types/work.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use ctypes::{H256, U256};
1919
#[derive(Debug, Serialize)]
2020
#[serde(rename_all = "camelCase")]
2121
pub struct Work {
22-
pub block_hash: H256,
23-
pub score: U256,
24-
pub block_number: u64,
22+
pub pow_hash: H256,
23+
pub target: U256,
2524
}

0 commit comments

Comments
 (0)