From e628885a00630c9f56f2fb7f714cd07470f7c3b9 Mon Sep 17 00:00:00 2001 From: Nicolas Ochem Date: Sun, 2 Jul 2017 12:50:34 -0700 Subject: [PATCH 1/3] implement getblockcount api command https://chainquery.com/bitcoin-api/getblockcount --- rpc/src/v1/impls/blockchain.rs | 9 +++++++++ rpc/src/v1/traits/blockchain.rs | 4 ++++ sync/src/utils/best_headers_chain.rs | 5 +++++ 3 files changed, 18 insertions(+) diff --git a/rpc/src/v1/impls/blockchain.rs b/rpc/src/v1/impls/blockchain.rs index d7562424..37555ff1 100644 --- a/rpc/src/v1/impls/blockchain.rs +++ b/rpc/src/v1/impls/blockchain.rs @@ -23,6 +23,7 @@ pub struct BlockChainClient { pub trait BlockChainClientCoreApi: Send + Sync + 'static { fn best_block_hash(&self) -> GlobalH256; + fn block_count(&self) -> u32; fn block_hash(&self, height: u32) -> Option; fn difficulty(&self) -> f64; fn raw_block(&self, hash: GlobalH256) -> Option; @@ -50,6 +51,10 @@ impl BlockChainClientCoreApi for BlockChainClientCore { self.storage.best_block().hash } + fn block_count(&self) -> u32 { + self.storage.best_block().number + } + fn block_hash(&self, height: u32) -> Option { self.storage.block_hash(height) } @@ -176,6 +181,10 @@ impl BlockChain for BlockChainClient where T: BlockChainClientCoreApi { Ok(self.core.best_block_hash().reversed().into()) } + fn block_count(&self) -> Result { + Ok(self.core.block_count()) + } + fn block_hash(&self, height: u32) -> Result { self.core.block_hash(height) .map(|h| h.reversed().into()) diff --git a/rpc/src/v1/traits/blockchain.rs b/rpc/src/v1/traits/blockchain.rs index 85330fd3..2c2c1451 100644 --- a/rpc/src/v1/traits/blockchain.rs +++ b/rpc/src/v1/traits/blockchain.rs @@ -14,6 +14,10 @@ build_rpc_trait! { /// @curl-example: curl --data-binary '{"jsonrpc": "2.0", "method": "getbestblockhash", "params": [], "id":1 }' -H 'content-type: application/json;' http://127.0.0.1:8332/ #[rpc(name = "getbestblockhash")] fn best_block_hash(&self) -> Result; + /// Get height of best block. + /// @curl-example: curl --data-binary '{"jsonrpc": "2.0", "method": "getblockcount", "params": [], "id":1 }' -H 'content-type: application/json;' http://127.0.0.1:8332/ + #[rpc(name = "getblockcount")] + fn block_count(&self) -> Result; /// Get hash of block at given height. /// @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")] diff --git a/sync/src/utils/best_headers_chain.rs b/sync/src/utils/best_headers_chain.rs index f6e2d4d5..5da67ce4 100644 --- a/sync/src/utils/best_headers_chain.rs +++ b/sync/src/utils/best_headers_chain.rs @@ -73,6 +73,11 @@ impl BestHeadersChain { .expect("storage_best_hash is always known") } + /// Get height of best block + pub fn block_count(&self) -> u32 { + self.best.len() + } + /// Insert new block header pub fn insert(&mut self, header: IndexedBlockHeader) { // append to the best chain From c3172a71a1d175ddc638c78a671b6ef9dad48c78 Mon Sep 17 00:00:00 2001 From: Nicolas Ochem Date: Sun, 2 Jul 2017 14:13:27 -0700 Subject: [PATCH 2/3] add tests --- rpc/src/v1/impls/blockchain.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/rpc/src/v1/impls/blockchain.rs b/rpc/src/v1/impls/blockchain.rs index 37555ff1..787f8332 100644 --- a/rpc/src/v1/impls/blockchain.rs +++ b/rpc/src/v1/impls/blockchain.rs @@ -262,6 +262,10 @@ pub mod tests { test_data::genesis().hash() } + fn block_count(&self) -> u32 { + 1 + } + fn block_hash(&self, _height: u32) -> Option { Some(test_data::genesis().hash()) } @@ -324,6 +328,10 @@ pub mod tests { test_data::genesis().hash() } + fn block_count(&self) -> u32 { + 1 + } + fn block_hash(&self, _height: u32) -> Option { None } @@ -364,6 +372,23 @@ pub mod tests { assert_eq!(&sample, r#"{"jsonrpc":"2.0","result":"000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f","id":1}"#); } + #[test] + fn block_count_success() { + let client = BlockChainClient::new(SuccessBlockChainClientCore::default()); + let mut handler = IoHandler::new(); + handler.extend_with(client.to_delegate()); + + let sample = handler.handle_request_sync(&(r#" + { + "jsonrpc": "2.0", + "method": "getblockcount", + "params": [], + "id": 1 + }"#)).unwrap(); + + assert_eq!(&sample, r#"{"jsonrpc":"2.0","result":1,"id":1}"#); + } + #[test] fn block_hash_success() { let client = BlockChainClient::new(SuccessBlockChainClientCore::default()); From 45f65084d6c2ad4fe3c0ed437411a5ba2ad55913 Mon Sep 17 00:00:00 2001 From: Nicolas Ochem Date: Sun, 2 Jul 2017 14:52:29 -0700 Subject: [PATCH 3/3] remove useless function --- sync/src/utils/best_headers_chain.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/sync/src/utils/best_headers_chain.rs b/sync/src/utils/best_headers_chain.rs index 5da67ce4..f6e2d4d5 100644 --- a/sync/src/utils/best_headers_chain.rs +++ b/sync/src/utils/best_headers_chain.rs @@ -73,11 +73,6 @@ impl BestHeadersChain { .expect("storage_best_hash is always known") } - /// Get height of best block - pub fn block_count(&self) -> u32 { - self.best.len() - } - /// Insert new block header pub fn insert(&mut self, header: IndexedBlockHeader) { // append to the best chain