implemented getblocktemplate RPC method

This commit is contained in:
Svyatoslav Nikolsky 2016-12-09 17:34:04 +03:00
parent b564770037
commit d706a8300d
6 changed files with 50 additions and 7 deletions

1
Cargo.lock generated
View File

@ -791,6 +791,7 @@ dependencies = [
"jsonrpc-core 4.0.0 (git+https://github.com/ethcore/jsonrpc.git)",
"jsonrpc-http-server 6.1.1 (git+https://github.com/ethcore/jsonrpc.git)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"miner 0.1.0",
"network 0.1.0",
"p2p 0.1.0",
"primitives 0.1.0",

View File

@ -48,7 +48,7 @@ pub fn setup_rpc<T: Extendable>(server: T, apis: ApiSet, deps: Dependencies) ->
for api in apis.list_apis() {
match api {
Api::Raw => server.add_delegate(RawClient::new(RawClientCore::new(deps.local_sync_node.clone())).to_delegate()),
Api::Miner => server.add_delegate(MinerClient::new(MinerClientCore::new()).to_delegate()),
Api::Miner => server.add_delegate(MinerClient::new(MinerClientCore::new(deps.local_sync_node.clone())).to_delegate()),
}
}
server

View File

@ -23,6 +23,7 @@ p2p = { path = "../p2p" }
network = { path = "../network" }
db = { path = "../db" }
test-data = { path = "../test-data" }
miner = { path = "../miner" }
[build-dependencies]
serde_codegen = { version = "0.8.0", optional = true }

View File

@ -15,7 +15,9 @@ extern crate primitives;
extern crate p2p;
extern crate network;
extern crate db;
#[cfg(test)]
extern crate test_data;
extern crate miner;
pub mod v1;
pub mod rpc_server;

View File

@ -1,38 +1,46 @@
use v1::traits::Miner;
use v1::types::{BlockTemplate, BlockTemplateRequest};
use jsonrpc_core::Error;
use sync;
use miner;
pub struct MinerClient<T: MinerClientCoreApi> {
_core: T,
core: T,
}
pub trait MinerClientCoreApi: Send + Sync + 'static {
fn get_block_template(&self) -> miner::BlockTemplate;
}
pub struct MinerClientCore {
local_sync_node: sync::LocalNodeRef,
}
impl MinerClientCore {
pub fn new() -> Self {
pub fn new(local_sync_node: sync::LocalNodeRef) -> Self {
MinerClientCore {
local_sync_node: local_sync_node,
}
}
}
impl MinerClientCoreApi for MinerClientCore {
fn get_block_template(&self) -> miner::BlockTemplate {
self.local_sync_node.get_block_template()
}
}
impl<T> MinerClient<T> where T: MinerClientCoreApi {
pub fn new(core: T) -> Self {
MinerClient {
_core: core,
core: core,
}
}
}
impl<T> Miner for MinerClient<T> where T: MinerClientCoreApi {
fn get_block_template(&self, _request: BlockTemplateRequest) -> Result<BlockTemplate, Error> {
rpc_unimplemented!()
Ok(self.core.get_block_template().into())
}
}

View File

@ -1,6 +1,8 @@
use std::collections::HashMap;
use super::hash::H256;
use super::raw_transaction::RawTransaction;
use db;
use miner;
/// Block template as described in:
/// https://github.com/bitcoin/bips/blob/master/bip-0022.mediawiki
@ -40,13 +42,13 @@ pub struct BlockTemplate {
/// A range of valid nonces (constant 00000000ffffffff)
pub noncerange: Option<String>,
/// Limit of sigops in blocks
pub sigoplimit: Option<i64>,
pub sigoplimit: Option<u32>,
/// Limit of block size
pub sizelimit: Option<u32>,
/// Limit of block weight
pub weightlimit: Option<u32>,
/// Current timestamp in seconds since epoch (Jan 1 1970 GMT)
pub curtime: i64,
pub curtime: u32,
/// Compressed target of next block
pub bits: u32,
/// The height of the next block
@ -77,6 +79,35 @@ pub struct BlockTemplateTransaction {
pub required: bool,
}
impl From<miner::BlockTemplate> for BlockTemplate {
fn from(block: miner::BlockTemplate) -> Self {
BlockTemplate {
version: block.version,
previousblockhash: block.previous_header_hash.into(),
curtime: block.time,
bits: block.nbits,
height: block.height,
transactions: block.transactions.into_iter().map(Into::into).collect(),
coinbasevalue: Some(block.coinbase_value),
sizelimit: Some(block.size_limit),
sigoplimit: Some(block.sigop_limit),
..Default::default()
}
}
}
impl From<db::IndexedTransaction> for BlockTemplateTransaction {
fn from(transaction: db::IndexedTransaction) -> Self {
use ser::serialize;
let serialize = serialize(&transaction.transaction);
BlockTemplateTransaction {
data: RawTransaction::new(Vec::from((*serialize).clone())),
..Default::default()
}
}
}
#[cfg(test)]
mod tests {
use serde_json;