Merge pull request #426 from nicolasochem/getblockcount

implement getblockcount api command
This commit is contained in:
Marek Kotewicz 2017-07-03 12:13:34 +02:00 committed by GitHub
commit 454122a0c8
2 changed files with 38 additions and 0 deletions

View File

@ -23,6 +23,7 @@ pub struct BlockChainClient<T: BlockChainClientCoreApi> {
pub trait BlockChainClientCoreApi: Send + Sync + 'static {
fn best_block_hash(&self) -> GlobalH256;
fn block_count(&self) -> u32;
fn block_hash(&self, height: u32) -> Option<GlobalH256>;
fn difficulty(&self) -> f64;
fn raw_block(&self, hash: GlobalH256) -> Option<RawBlock>;
@ -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<GlobalH256> {
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())
}
fn block_count(&self) -> Result<u32, Error> {
Ok(self.core.block_count())
}
fn block_hash(&self, height: u32) -> Result<H256, Error> {
self.core.block_hash(height)
.map(|h| h.reversed().into())
@ -253,6 +262,10 @@ pub mod tests {
test_data::genesis().hash()
}
fn block_count(&self) -> u32 {
1
}
fn block_hash(&self, _height: u32) -> Option<GlobalH256> {
Some(test_data::genesis().hash())
}
@ -315,6 +328,10 @@ pub mod tests {
test_data::genesis().hash()
}
fn block_count(&self) -> u32 {
1
}
fn block_hash(&self, _height: u32) -> Option<GlobalH256> {
None
}
@ -355,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());

View File

@ -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<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.
/// @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")]