2016-12-09 05:37:12 -08:00
|
|
|
use jsonrpc_core::Error;
|
2019-01-15 01:16:27 -08:00
|
|
|
use keys::Address;
|
2016-12-09 06:34:04 -08:00
|
|
|
use miner;
|
2019-06-28 14:27:46 -07:00
|
|
|
use sync;
|
|
|
|
use v1::helpers::errors::execution;
|
|
|
|
use v1::traits::Miner;
|
|
|
|
use v1::types::{BlockTemplate, BlockTemplateRequest};
|
2016-12-09 05:37:12 -08:00
|
|
|
|
|
|
|
pub struct MinerClient<T: MinerClientCoreApi> {
|
2019-06-28 14:27:46 -07:00
|
|
|
core: T,
|
2016-12-09 05:37:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait MinerClientCoreApi: Send + Sync + 'static {
|
2019-06-28 14:27:46 -07:00
|
|
|
fn get_block_template(&self) -> Result<miner::BlockTemplate, String>;
|
2016-12-09 05:37:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct MinerClientCore {
|
2019-06-28 14:27:46 -07:00
|
|
|
local_sync_node: sync::LocalNodeRef,
|
|
|
|
miner_address: Option<Address>,
|
2016-12-09 05:37:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MinerClientCore {
|
2019-06-28 14:27:46 -07:00
|
|
|
pub fn new(local_sync_node: sync::LocalNodeRef, miner_address: Option<Address>) -> Self {
|
|
|
|
MinerClientCore {
|
|
|
|
local_sync_node: local_sync_node,
|
|
|
|
miner_address: miner_address,
|
|
|
|
}
|
|
|
|
}
|
2016-12-09 05:37:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MinerClientCoreApi for MinerClientCore {
|
2019-06-28 14:27:46 -07:00
|
|
|
fn get_block_template(&self) -> Result<miner::BlockTemplate, String> {
|
|
|
|
self.miner_address
|
|
|
|
.as_ref()
|
|
|
|
.ok_or_else(|| "miner address not set".into())
|
|
|
|
.and_then(|miner_address| self.local_sync_node.get_block_template(miner_address))
|
|
|
|
}
|
2016-12-09 05:37:12 -08:00
|
|
|
}
|
|
|
|
|
2019-06-28 14:27:46 -07:00
|
|
|
impl<T> MinerClient<T>
|
|
|
|
where
|
|
|
|
T: MinerClientCoreApi,
|
|
|
|
{
|
|
|
|
pub fn new(core: T) -> Self {
|
|
|
|
MinerClient { core: core }
|
|
|
|
}
|
2016-12-09 05:37:12 -08:00
|
|
|
}
|
|
|
|
|
2019-06-28 14:27:46 -07:00
|
|
|
impl<T> Miner for MinerClient<T>
|
|
|
|
where
|
|
|
|
T: MinerClientCoreApi,
|
|
|
|
{
|
|
|
|
fn get_block_template(&self, _request: BlockTemplateRequest) -> Result<BlockTemplate, Error> {
|
|
|
|
self.core
|
|
|
|
.get_block_template()
|
|
|
|
.map(Into::into)
|
|
|
|
.map_err(|err| execution(&err))
|
|
|
|
}
|
2016-12-09 05:37:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub mod tests {
|
2019-06-28 14:27:46 -07:00
|
|
|
use super::*;
|
|
|
|
use chain;
|
|
|
|
use jsonrpc_core::IoHandler;
|
|
|
|
use miner;
|
|
|
|
use primitives::hash::H256;
|
|
|
|
use v1::traits::Miner;
|
2016-12-09 06:59:02 -08:00
|
|
|
|
2019-06-28 14:27:46 -07:00
|
|
|
#[derive(Default)]
|
|
|
|
struct SuccessMinerClientCore;
|
2016-12-09 06:59:02 -08:00
|
|
|
|
2019-06-28 14:27:46 -07:00
|
|
|
impl MinerClientCoreApi for SuccessMinerClientCore {
|
|
|
|
fn get_block_template(&self) -> Result<miner::BlockTemplate, String> {
|
|
|
|
let tx: chain::Transaction = "00000000013ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a0000000000000000000101000000000000000000000000".into();
|
|
|
|
Ok(miner::BlockTemplate {
|
|
|
|
version: 777,
|
|
|
|
previous_header_hash: H256::from(1),
|
|
|
|
final_sapling_root_hash: H256::from(2),
|
|
|
|
time: 33,
|
|
|
|
bits: 44.into(),
|
|
|
|
height: 55,
|
|
|
|
transactions: vec![tx.into()],
|
|
|
|
coinbase_tx: Default::default(),
|
|
|
|
size_limit: 77,
|
|
|
|
sigop_limit: 88,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2016-12-09 06:59:02 -08:00
|
|
|
|
2019-06-28 14:27:46 -07:00
|
|
|
#[test]
|
|
|
|
fn getblocktemplate_accepted() {
|
|
|
|
let client = MinerClient::new(SuccessMinerClientCore::default());
|
|
|
|
let mut handler = IoHandler::new();
|
|
|
|
handler.extend_with(client.to_delegate());
|
2016-12-09 06:59:02 -08:00
|
|
|
|
2019-06-28 14:27:46 -07:00
|
|
|
let sample = handler
|
|
|
|
.handle_request_sync(
|
|
|
|
&(r#"
|
2016-12-09 06:59:02 -08:00
|
|
|
{
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"method": "getblocktemplate",
|
|
|
|
"params": [{}],
|
|
|
|
"id": 1
|
2019-06-28 14:27:46 -07:00
|
|
|
}"#),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2016-12-09 06:59:02 -08:00
|
|
|
|
2019-06-28 14:27:46 -07:00
|
|
|
// direct hash is 0100000000000000000000000000000000000000000000000000000000000000
|
|
|
|
// but client expects reverse hash
|
|
|
|
assert_eq!(&sample, r#"{"jsonrpc":"2.0","result":{"bits":44,"coinbasetxn":{"data":"00000000000000000000","depends":null,"fee":null,"hash":null,"required":false,"sigops":null},"curtime":33,"finalsaplingroothash":"0000000000000000000000000000000000000000000000000000000000000000","height":55,"mintime":null,"mutable":null,"noncerange":null,"previousblockhash":"0000000000000000000000000000000000000000000000000000000000000001","sigoplimit":88,"sizelimit":77,"target":"0000000000000000000000000000000000000000000000000000000000000000","transactions":[{"data":"00000000013ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a0000000000000000000101000000000000000000000000","depends":null,"fee":null,"hash":null,"required":false,"sigops":null}],"version":777},"id":1}"#);
|
|
|
|
}
|
2016-12-09 05:37:12 -08:00
|
|
|
}
|