implement getblockcount api command
https://chainquery.com/bitcoin-api/getblockcount
This commit is contained in:
parent
eaee51a7a3
commit
e628885a00
|
@ -23,6 +23,7 @@ pub struct BlockChainClient<T: BlockChainClientCoreApi> {
|
||||||
|
|
||||||
pub trait BlockChainClientCoreApi: Send + Sync + 'static {
|
pub trait BlockChainClientCoreApi: Send + Sync + 'static {
|
||||||
fn best_block_hash(&self) -> GlobalH256;
|
fn best_block_hash(&self) -> GlobalH256;
|
||||||
|
fn block_count(&self) -> u32;
|
||||||
fn block_hash(&self, height: u32) -> Option<GlobalH256>;
|
fn block_hash(&self, height: u32) -> Option<GlobalH256>;
|
||||||
fn difficulty(&self) -> f64;
|
fn difficulty(&self) -> f64;
|
||||||
fn raw_block(&self, hash: GlobalH256) -> Option<RawBlock>;
|
fn raw_block(&self, hash: GlobalH256) -> Option<RawBlock>;
|
||||||
|
@ -50,6 +51,10 @@ impl BlockChainClientCoreApi for BlockChainClientCore {
|
||||||
self.storage.best_block().hash
|
self.storage.best_block().hash
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn block_count(&self) -> u32 {
|
||||||
|
self.storage.best_block().number
|
||||||
|
}
|
||||||
|
|
||||||
fn block_hash(&self, height: u32) -> Option<GlobalH256> {
|
fn block_hash(&self, height: u32) -> Option<GlobalH256> {
|
||||||
self.storage.block_hash(height)
|
self.storage.block_hash(height)
|
||||||
}
|
}
|
||||||
|
@ -176,6 +181,10 @@ impl<T> BlockChain for BlockChainClient<T> where T: BlockChainClientCoreApi {
|
||||||
Ok(self.core.best_block_hash().reversed().into())
|
Ok(self.core.best_block_hash().reversed().into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn block_count(&self) -> Result<u32, Error> {
|
||||||
|
Ok(self.core.block_count())
|
||||||
|
}
|
||||||
|
|
||||||
fn block_hash(&self, height: u32) -> Result<H256, Error> {
|
fn block_hash(&self, height: u32) -> Result<H256, Error> {
|
||||||
self.core.block_hash(height)
|
self.core.block_hash(height)
|
||||||
.map(|h| h.reversed().into())
|
.map(|h| h.reversed().into())
|
||||||
|
|
|
@ -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/
|
/// @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")]
|
#[rpc(name = "getbestblockhash")]
|
||||||
fn best_block_hash(&self) -> Result<H256, Error>;
|
fn best_block_hash(&self) -> Result<H256, Error>;
|
||||||
|
/// 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<u32, Error>;
|
||||||
/// Get hash of block at given height.
|
/// 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/
|
/// @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")]
|
||||||
|
|
|
@ -73,6 +73,11 @@ impl BestHeadersChain {
|
||||||
.expect("storage_best_hash is always known")
|
.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
|
/// Insert new block header
|
||||||
pub fn insert(&mut self, header: IndexedBlockHeader) {
|
pub fn insert(&mut self, header: IndexedBlockHeader) {
|
||||||
// append to the best chain
|
// append to the best chain
|
||||||
|
|
Loading…
Reference in New Issue