From f110b0be139b01ccad6ea7bee2d6ef1752299441 Mon Sep 17 00:00:00 2001 From: Svyatoslav Nikolsky Date: Mon, 14 Jan 2019 11:32:32 +0300 Subject: [PATCH] update getdifficulty RPC --- Cargo.lock | 1 + db/src/block_chain_db.rs | 6 ------ pzec/commands/start.rs | 4 ++-- pzec/rpc.rs | 4 ++-- pzec/rpc_apis.rs | 2 +- rpc/Cargo.toml | 1 + rpc/src/lib.rs | 1 + rpc/src/v1/impls/blockchain.rs | 28 +++++++++++++++++++--------- rpc/src/v1/traits/blockchain.rs | 2 +- storage/src/store.rs | 4 ---- 10 files changed, 28 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9033ebce..206ca606 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1301,6 +1301,7 @@ dependencies = [ "storage 0.1.0", "sync 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)", "verification 0.1.0", ] diff --git a/db/src/block_chain_db.rs b/db/src/block_chain_db.rs index 854f4262..9ff2eeda 100644 --- a/db/src/block_chain_db.rs +++ b/db/src/block_chain_db.rs @@ -4,7 +4,6 @@ use std::path::Path; use parking_lot::RwLock; use hash::H256; use bytes::Bytes; -use primitives::compact::Compact; use chain::{ IndexedBlock, IndexedBlockHeader, IndexedTransaction, BlockHeader, Block, Transaction, OutPoint, TransactionOutput, @@ -630,11 +629,6 @@ impl Store for BlockChainDatabase where T: KeyValueDatabase { fn best_header(&self) -> BlockHeader { 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 ConfigStore for BlockChainDatabase where T: KeyValueDatabase { diff --git a/pzec/commands/start.rs b/pzec/commands/start.rs index 23ea77a5..293845e4 100644 --- a/pzec/commands/start.rs +++ b/pzec/commands/start.rs @@ -110,7 +110,7 @@ pub fn start(cfg: config::Config) -> Result<(), String> { }; 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()); 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 rpc_deps = rpc::Dependencies { - network: cfg.network, + consensus: cfg.consensus, storage: cfg.db, local_sync_node: local_sync_node, p2p_context: p2p.context().clone(), diff --git a/pzec/rpc.rs b/pzec/rpc.rs index b3986871..aa9c607f 100644 --- a/pzec/rpc.rs +++ b/pzec/rpc.rs @@ -2,14 +2,14 @@ use std::net::SocketAddr; use std::sync::Arc; use rpc_apis::{self, ApiSet}; use ethcore_rpc::{Server, start_http, MetaIoHandler, Compatibility}; -use network::Network; +use network::ConsensusParams; use std::io; use sync; use storage; use p2p; pub struct Dependencies { - pub network: Network, + pub consensus: ConsensusParams, pub local_sync_node: sync::LocalNodeRef, pub storage: storage::SharedStore, pub p2p_context: Arc, diff --git a/pzec/rpc_apis.rs b/pzec/rpc_apis.rs index 2ebb6361..13404f03 100644 --- a/pzec/rpc_apis.rs +++ b/pzec/rpc_apis.rs @@ -55,7 +55,7 @@ pub fn setup_rpc(mut handler: MetaIoHandler<()>, apis: ApiSet, deps: Dependencie match api { 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::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()), } } diff --git a/rpc/Cargo.toml b/rpc/Cargo.toml index 29c93fb7..43f27680 100644 --- a/rpc/Cargo.toml +++ b/rpc/Cargo.toml @@ -11,6 +11,7 @@ serde = "1.0" serde_json = "1.0" serde_derive = "1.0" rustc-hex = "2" +time = "0.1" tokio-core = "0.1.1" 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" } diff --git a/rpc/src/lib.rs b/rpc/src/lib.rs index b7f4fc3b..e003662c 100644 --- a/rpc/src/lib.rs +++ b/rpc/src/lib.rs @@ -8,6 +8,7 @@ extern crate jsonrpc_core; #[macro_use] extern crate jsonrpc_derive; extern crate jsonrpc_http_server; +extern crate time; extern crate tokio_core; extern crate sync; extern crate chain; diff --git a/rpc/src/v1/impls/blockchain.rs b/rpc/src/v1/impls/blockchain.rs index 39cb6e01..a409c3fb 100644 --- a/rpc/src/v1/impls/blockchain.rs +++ b/rpc/src/v1/impls/blockchain.rs @@ -13,7 +13,7 @@ use global_script::Script; use chain::OutPoint; use verification; use ser::serialize; -use network::{Network}; +use network::{Network, ConsensusParams}; use primitives::hash::H256 as GlobalH256; pub struct BlockChainClient { @@ -31,14 +31,14 @@ pub trait BlockChainClientCoreApi: Send + Sync + 'static { } pub struct BlockChainClientCore { - network: Network, + consensus: ConsensusParams, storage: storage::SharedStore, } impl BlockChainClientCore { - pub fn new(network: Network, storage: storage::SharedStore) -> Self { + pub fn new(consensus: ConsensusParams, storage: storage::SharedStore) -> Self { BlockChainClientCore { - network: network, + consensus: consensus, storage: storage, } } @@ -58,7 +58,17 @@ impl BlockChainClientCoreApi for BlockChainClientCore { } 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 { @@ -88,7 +98,7 @@ impl BlockChainClientCoreApi for BlockChainClientCore { size: block_size as u32, height: height, 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 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())), @@ -148,7 +158,7 @@ impl BlockChainClientCoreApi for BlockChainClientCore { req_sigs: script.num_signatures_required() as u32, script_type: script.script_type().into(), addresses: script_addresses.into_iter().map(|a| Address { - network: match self.network { + network: match self.consensus.network { Network::Mainnet => keys::Network::Mainnet, // there's no correct choices for Regtests && Other networks // => 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: // https://blockexplorer.com/block/00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048 @@ -576,7 +586,7 @@ pub mod tests { #[test] fn verbose_transaction_out_contents() { 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: // https://zcash.blockexplorer.com/tx/851bf6fbf7a976327817c738c489d7fa657752445430922d94c983c0b9ed4609 diff --git a/rpc/src/v1/traits/blockchain.rs b/rpc/src/v1/traits/blockchain.rs index b0f062ba..811bb189 100644 --- a/rpc/src/v1/traits/blockchain.rs +++ b/rpc/src/v1/traits/blockchain.rs @@ -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/ #[rpc(name = "getblockhash")] fn block_hash(&self, u32) -> Result; - /// 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/ #[rpc(name = "getdifficulty")] fn difficulty(&self) -> Result; diff --git a/storage/src/store.rs b/storage/src/store.rs index 7bd224e4..49e09193 100644 --- a/storage/src/store.rs +++ b/storage/src/store.rs @@ -1,6 +1,5 @@ use std::sync::Arc; use chain::BlockHeader; -use primitives::compact::Compact; use { BestBlock, BlockProvider, BlockHeaderProvider, TransactionProvider, TransactionMetaProvider, TransactionOutputProvider, BlockChain, IndexedBlockProvider, Forkable, Error, NullifierTracker, @@ -26,9 +25,6 @@ pub trait Store: AsSubstore { /// get best header fn best_header(&self) -> BlockHeader; - - /// get blockchain difficulty - fn difficulty(&self, max_bits: Compact) -> f64; } /// Allows casting Arc to reference to any substore type