update getdifficulty RPC
This commit is contained in:
parent
0d98b13e2f
commit
f110b0be13
|
@ -1301,6 +1301,7 @@ dependencies = [
|
||||||
"storage 0.1.0",
|
"storage 0.1.0",
|
||||||
"sync 0.1.0",
|
"sync 0.1.0",
|
||||||
"test-data 0.1.0",
|
"test-data 0.1.0",
|
||||||
|
"time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
|
"tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"verification 0.1.0",
|
"verification 0.1.0",
|
||||||
]
|
]
|
||||||
|
|
|
@ -4,7 +4,6 @@ use std::path::Path;
|
||||||
use parking_lot::RwLock;
|
use parking_lot::RwLock;
|
||||||
use hash::H256;
|
use hash::H256;
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use primitives::compact::Compact;
|
|
||||||
use chain::{
|
use chain::{
|
||||||
IndexedBlock, IndexedBlockHeader, IndexedTransaction, BlockHeader, Block, Transaction,
|
IndexedBlock, IndexedBlockHeader, IndexedTransaction, BlockHeader, Block, Transaction,
|
||||||
OutPoint, TransactionOutput,
|
OutPoint, TransactionOutput,
|
||||||
|
@ -630,11 +629,6 @@ impl<T> Store for BlockChainDatabase<T> where T: KeyValueDatabase {
|
||||||
fn best_header(&self) -> BlockHeader {
|
fn best_header(&self) -> BlockHeader {
|
||||||
self.block_header(self.best_block().hash.into()).expect("best block header should be in db; qed")
|
self.block_header(self.best_block().hash.into()).expect("best block header should be in db; qed")
|
||||||
}
|
}
|
||||||
|
|
||||||
/// get blockchain difficulty
|
|
||||||
fn difficulty(&self, max_bits: Compact) -> f64 {
|
|
||||||
self.best_header().bits.to_f64(max_bits)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> ConfigStore for BlockChainDatabase<T> where T: KeyValueDatabase {
|
impl<T> ConfigStore for BlockChainDatabase<T> where T: KeyValueDatabase {
|
||||||
|
|
|
@ -110,7 +110,7 @@ pub fn start(cfg: config::Config) -> Result<(), String> {
|
||||||
};
|
};
|
||||||
|
|
||||||
let sync_peers = create_sync_peers();
|
let sync_peers = create_sync_peers();
|
||||||
let local_sync_node = create_local_sync_node(cfg.consensus, cfg.db.clone(), sync_peers.clone(), cfg.verification_params);
|
let local_sync_node = create_local_sync_node(cfg.consensus.clone(), cfg.db.clone(), sync_peers.clone(), cfg.verification_params);
|
||||||
let sync_connection_factory = create_sync_connection_factory(sync_peers.clone(), local_sync_node.clone());
|
let sync_connection_factory = create_sync_connection_factory(sync_peers.clone(), local_sync_node.clone());
|
||||||
|
|
||||||
if let Some(block_notify_command) = cfg.block_notify_command {
|
if let Some(block_notify_command) = cfg.block_notify_command {
|
||||||
|
@ -119,7 +119,7 @@ pub fn start(cfg: config::Config) -> Result<(), String> {
|
||||||
|
|
||||||
let p2p = try!(p2p::P2P::new(p2p_cfg, sync_connection_factory, el.handle()).map_err(|x| x.to_string()));
|
let p2p = try!(p2p::P2P::new(p2p_cfg, sync_connection_factory, el.handle()).map_err(|x| x.to_string()));
|
||||||
let rpc_deps = rpc::Dependencies {
|
let rpc_deps = rpc::Dependencies {
|
||||||
network: cfg.network,
|
consensus: cfg.consensus,
|
||||||
storage: cfg.db,
|
storage: cfg.db,
|
||||||
local_sync_node: local_sync_node,
|
local_sync_node: local_sync_node,
|
||||||
p2p_context: p2p.context().clone(),
|
p2p_context: p2p.context().clone(),
|
||||||
|
|
|
@ -2,14 +2,14 @@ use std::net::SocketAddr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use rpc_apis::{self, ApiSet};
|
use rpc_apis::{self, ApiSet};
|
||||||
use ethcore_rpc::{Server, start_http, MetaIoHandler, Compatibility};
|
use ethcore_rpc::{Server, start_http, MetaIoHandler, Compatibility};
|
||||||
use network::Network;
|
use network::ConsensusParams;
|
||||||
use std::io;
|
use std::io;
|
||||||
use sync;
|
use sync;
|
||||||
use storage;
|
use storage;
|
||||||
use p2p;
|
use p2p;
|
||||||
|
|
||||||
pub struct Dependencies {
|
pub struct Dependencies {
|
||||||
pub network: Network,
|
pub consensus: ConsensusParams,
|
||||||
pub local_sync_node: sync::LocalNodeRef,
|
pub local_sync_node: sync::LocalNodeRef,
|
||||||
pub storage: storage::SharedStore,
|
pub storage: storage::SharedStore,
|
||||||
pub p2p_context: Arc<p2p::Context>,
|
pub p2p_context: Arc<p2p::Context>,
|
||||||
|
|
|
@ -55,7 +55,7 @@ pub fn setup_rpc(mut handler: MetaIoHandler<()>, apis: ApiSet, deps: Dependencie
|
||||||
match api {
|
match api {
|
||||||
Api::Raw => handler.extend_with(RawClient::new(RawClientCore::new(deps.local_sync_node.clone())).to_delegate()),
|
Api::Raw => handler.extend_with(RawClient::new(RawClientCore::new(deps.local_sync_node.clone())).to_delegate()),
|
||||||
Api::Miner => handler.extend_with(MinerClient::new(MinerClientCore::new(deps.local_sync_node.clone())).to_delegate()),
|
Api::Miner => handler.extend_with(MinerClient::new(MinerClientCore::new(deps.local_sync_node.clone())).to_delegate()),
|
||||||
Api::BlockChain => handler.extend_with(BlockChainClient::new(BlockChainClientCore::new(deps.network, deps.storage.clone())).to_delegate()),
|
Api::BlockChain => handler.extend_with(BlockChainClient::new(BlockChainClientCore::new(deps.consensus.clone(), deps.storage.clone())).to_delegate()),
|
||||||
Api::Network => handler.extend_with(NetworkClient::new(NetworkClientCore::new(deps.p2p_context.clone())).to_delegate()),
|
Api::Network => handler.extend_with(NetworkClient::new(NetworkClientCore::new(deps.p2p_context.clone())).to_delegate()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@ serde = "1.0"
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
serde_derive = "1.0"
|
serde_derive = "1.0"
|
||||||
rustc-hex = "2"
|
rustc-hex = "2"
|
||||||
|
time = "0.1"
|
||||||
tokio-core = "0.1.1"
|
tokio-core = "0.1.1"
|
||||||
jsonrpc-core = { git = "https://github.com/ethcore/jsonrpc.git", branch = "pzec_dependency_multiple_trailing_args" }
|
jsonrpc-core = { git = "https://github.com/ethcore/jsonrpc.git", branch = "pzec_dependency_multiple_trailing_args" }
|
||||||
jsonrpc-derive = { git = "https://github.com/ethcore/jsonrpc.git", branch = "pzec_dependency_multiple_trailing_args" }
|
jsonrpc-derive = { git = "https://github.com/ethcore/jsonrpc.git", branch = "pzec_dependency_multiple_trailing_args" }
|
||||||
|
|
|
@ -8,6 +8,7 @@ extern crate jsonrpc_core;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate jsonrpc_derive;
|
extern crate jsonrpc_derive;
|
||||||
extern crate jsonrpc_http_server;
|
extern crate jsonrpc_http_server;
|
||||||
|
extern crate time;
|
||||||
extern crate tokio_core;
|
extern crate tokio_core;
|
||||||
extern crate sync;
|
extern crate sync;
|
||||||
extern crate chain;
|
extern crate chain;
|
||||||
|
|
|
@ -13,7 +13,7 @@ use global_script::Script;
|
||||||
use chain::OutPoint;
|
use chain::OutPoint;
|
||||||
use verification;
|
use verification;
|
||||||
use ser::serialize;
|
use ser::serialize;
|
||||||
use network::{Network};
|
use network::{Network, ConsensusParams};
|
||||||
use primitives::hash::H256 as GlobalH256;
|
use primitives::hash::H256 as GlobalH256;
|
||||||
|
|
||||||
pub struct BlockChainClient<T: BlockChainClientCoreApi> {
|
pub struct BlockChainClient<T: BlockChainClientCoreApi> {
|
||||||
|
@ -31,14 +31,14 @@ pub trait BlockChainClientCoreApi: Send + Sync + 'static {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct BlockChainClientCore {
|
pub struct BlockChainClientCore {
|
||||||
network: Network,
|
consensus: ConsensusParams,
|
||||||
storage: storage::SharedStore,
|
storage: storage::SharedStore,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BlockChainClientCore {
|
impl BlockChainClientCore {
|
||||||
pub fn new(network: Network, storage: storage::SharedStore) -> Self {
|
pub fn new(consensus: ConsensusParams, storage: storage::SharedStore) -> Self {
|
||||||
BlockChainClientCore {
|
BlockChainClientCore {
|
||||||
network: network,
|
consensus: consensus,
|
||||||
storage: storage,
|
storage: storage,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,7 +58,17 @@ impl BlockChainClientCoreApi for BlockChainClientCore {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn difficulty(&self) -> f64 {
|
fn difficulty(&self) -> f64 {
|
||||||
self.storage.difficulty(self.network.max_bits().into())
|
let best_block = self.storage.best_block();
|
||||||
|
let now = ::time::get_time().sec as u32;
|
||||||
|
|
||||||
|
let next_work_required = verification::work_required(
|
||||||
|
best_block.hash,
|
||||||
|
now,
|
||||||
|
best_block.number + 1,
|
||||||
|
self.storage.as_block_header_provider(),
|
||||||
|
&self.consensus);
|
||||||
|
|
||||||
|
next_work_required.to_f64(self.consensus.network.max_bits().into())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn raw_block(&self, hash: GlobalH256) -> Option<RawBlock> {
|
fn raw_block(&self, hash: GlobalH256) -> Option<RawBlock> {
|
||||||
|
@ -88,7 +98,7 @@ impl BlockChainClientCoreApi for BlockChainClientCore {
|
||||||
size: block_size as u32,
|
size: block_size as u32,
|
||||||
height: height,
|
height: height,
|
||||||
mediantime: Some(median_time),
|
mediantime: Some(median_time),
|
||||||
difficulty: block.header.raw.bits.to_f64(self.network.max_bits().into()),
|
difficulty: block.header.raw.bits.to_f64(self.consensus.network.max_bits().into()),
|
||||||
chainwork: U256::default(), // TODO: read from storage
|
chainwork: U256::default(), // TODO: read from storage
|
||||||
previousblockhash: Some(block.header.raw.previous_header_hash.clone().into()),
|
previousblockhash: Some(block.header.raw.previous_header_hash.clone().into()),
|
||||||
nextblockhash: height.and_then(|h| self.storage.block_hash(h + 1).map(|h| h.into())),
|
nextblockhash: height.and_then(|h| self.storage.block_hash(h + 1).map(|h| h.into())),
|
||||||
|
@ -148,7 +158,7 @@ impl BlockChainClientCoreApi for BlockChainClientCore {
|
||||||
req_sigs: script.num_signatures_required() as u32,
|
req_sigs: script.num_signatures_required() as u32,
|
||||||
script_type: script.script_type().into(),
|
script_type: script.script_type().into(),
|
||||||
addresses: script_addresses.into_iter().map(|a| Address {
|
addresses: script_addresses.into_iter().map(|a| Address {
|
||||||
network: match self.network {
|
network: match self.consensus.network {
|
||||||
Network::Mainnet => keys::Network::Mainnet,
|
Network::Mainnet => keys::Network::Mainnet,
|
||||||
// there's no correct choices for Regtests && Other networks
|
// there's no correct choices for Regtests && Other networks
|
||||||
// => let's just make Testnet key
|
// => let's just make Testnet key
|
||||||
|
@ -442,7 +452,7 @@ pub mod tests {
|
||||||
]
|
]
|
||||||
));
|
));
|
||||||
|
|
||||||
let core = BlockChainClientCore::new(Network::Mainnet, storage);
|
let core = BlockChainClientCore::new(ConsensusParams::new(Network::Mainnet), storage);
|
||||||
|
|
||||||
// get info on block #1:
|
// get info on block #1:
|
||||||
// https://blockexplorer.com/block/00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048
|
// https://blockexplorer.com/block/00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048
|
||||||
|
@ -576,7 +586,7 @@ pub mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn verbose_transaction_out_contents() {
|
fn verbose_transaction_out_contents() {
|
||||||
let storage = Arc::new(BlockChainDatabase::init_test_chain(vec![test_data::genesis().into(), test_data::block_h1().into()]));
|
let storage = Arc::new(BlockChainDatabase::init_test_chain(vec![test_data::genesis().into(), test_data::block_h1().into()]));
|
||||||
let core = BlockChainClientCore::new(Network::Mainnet, storage);
|
let core = BlockChainClientCore::new(ConsensusParams::new(Network::Mainnet), storage);
|
||||||
|
|
||||||
// get info on tx from block#1:
|
// get info on tx from block#1:
|
||||||
// https://zcash.blockexplorer.com/tx/851bf6fbf7a976327817c738c489d7fa657752445430922d94c983c0b9ed4609
|
// https://zcash.blockexplorer.com/tx/851bf6fbf7a976327817c738c489d7fa657752445430922d94c983c0b9ed4609
|
||||||
|
|
|
@ -21,7 +21,7 @@ pub trait BlockChain {
|
||||||
/// @curl-example: curl --data-binary '{"jsonrpc": "2.0", "method": "getblockhash", "params": [0], "id":1 }' -H 'content-type: application/json' http://127.0.0.1:8332/
|
/// @curl-example: curl --data-binary '{"jsonrpc": "2.0", "method": "getblockhash", "params": [0], "id":1 }' -H 'content-type: application/json' http://127.0.0.1:8332/
|
||||||
#[rpc(name = "getblockhash")]
|
#[rpc(name = "getblockhash")]
|
||||||
fn block_hash(&self, u32) -> Result<H256, Error>;
|
fn block_hash(&self, u32) -> Result<H256, Error>;
|
||||||
/// Get proof-of-work difficulty as a multiple of the minimum difficulty
|
/// Get proof-of-work difficulty for the next block as a multiple of the minimum difficulty
|
||||||
/// @curl-example: curl --data-binary '{"jsonrpc": "2.0", "method": "getdifficulty", "params": [], "id":1 }' -H 'content-type: application/json' http://127.0.0.1:8332/
|
/// @curl-example: curl --data-binary '{"jsonrpc": "2.0", "method": "getdifficulty", "params": [], "id":1 }' -H 'content-type: application/json' http://127.0.0.1:8332/
|
||||||
#[rpc(name = "getdifficulty")]
|
#[rpc(name = "getdifficulty")]
|
||||||
fn difficulty(&self) -> Result<f64, Error>;
|
fn difficulty(&self) -> Result<f64, Error>;
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use chain::BlockHeader;
|
use chain::BlockHeader;
|
||||||
use primitives::compact::Compact;
|
|
||||||
use {
|
use {
|
||||||
BestBlock, BlockProvider, BlockHeaderProvider, TransactionProvider, TransactionMetaProvider,
|
BestBlock, BlockProvider, BlockHeaderProvider, TransactionProvider, TransactionMetaProvider,
|
||||||
TransactionOutputProvider, BlockChain, IndexedBlockProvider, Forkable, Error, NullifierTracker,
|
TransactionOutputProvider, BlockChain, IndexedBlockProvider, Forkable, Error, NullifierTracker,
|
||||||
|
@ -26,9 +25,6 @@ pub trait Store: AsSubstore {
|
||||||
|
|
||||||
/// get best header
|
/// get best header
|
||||||
fn best_header(&self) -> BlockHeader;
|
fn best_header(&self) -> BlockHeader;
|
||||||
|
|
||||||
/// get blockchain difficulty
|
|
||||||
fn difficulty(&self, max_bits: Compact) -> f64;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Allows casting Arc<Store> to reference to any substore type
|
/// Allows casting Arc<Store> to reference to any substore type
|
||||||
|
|
Loading…
Reference in New Issue