Merge pull request #426 from nicolasochem/getblockcount
implement getblockcount api command
This commit is contained in:
commit
454122a0c8
|
@ -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())
|
||||||
|
@ -253,6 +262,10 @@ pub mod tests {
|
||||||
test_data::genesis().hash()
|
test_data::genesis().hash()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn block_count(&self) -> u32 {
|
||||||
|
1
|
||||||
|
}
|
||||||
|
|
||||||
fn block_hash(&self, _height: u32) -> Option<GlobalH256> {
|
fn block_hash(&self, _height: u32) -> Option<GlobalH256> {
|
||||||
Some(test_data::genesis().hash())
|
Some(test_data::genesis().hash())
|
||||||
}
|
}
|
||||||
|
@ -315,6 +328,10 @@ pub mod tests {
|
||||||
test_data::genesis().hash()
|
test_data::genesis().hash()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn block_count(&self) -> u32 {
|
||||||
|
1
|
||||||
|
}
|
||||||
|
|
||||||
fn block_hash(&self, _height: u32) -> Option<GlobalH256> {
|
fn block_hash(&self, _height: u32) -> Option<GlobalH256> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -355,6 +372,23 @@ pub mod tests {
|
||||||
assert_eq!(&sample, r#"{"jsonrpc":"2.0","result":"000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f","id":1}"#);
|
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]
|
#[test]
|
||||||
fn block_hash_success() {
|
fn block_hash_success() {
|
||||||
let client = BlockChainClient::new(SuccessBlockChainClientCore::default());
|
let client = BlockChainClient::new(SuccessBlockChainClientCore::default());
|
||||||
|
|
|
@ -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")]
|
||||||
|
|
Loading…
Reference in New Issue