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