Insipx/add block method (#18)

* Fix compile errors

* Fix tests, close #13 BTW

* add block method

* add block_hash method

* add tests for block and block_hash
This commit is contained in:
Andrew Plaza
2019-09-12 21:34:24 +02:00
committed by Andrew Jones
parent 650c6d3bb0
commit e9070e2786
2 changed files with 42 additions and 1 deletions
+30
View File
@@ -55,6 +55,8 @@ use crate::{
rpc::{
MapStream,
Rpc,
ChainBlock,
BlockNumber
},
srml::{
system::{
@@ -183,6 +185,18 @@ impl<T: System + 'static> Client<T> {
self.fetch(key).map(|value| value.unwrap_or_default())
}
/// Get a block hash. By default returns the latest block hash
pub fn block_hash(&self, hash: Option<BlockNumber<T>>) -> impl Future<Item = Option<T::Hash>, Error = Error> {
self.connect().and_then(|rpc| rpc.block_hash(hash.map(|h| h)))
}
/// Get a block
pub fn block<H>(&self, hash: Option<H>) -> impl Future<Item = Option<ChainBlock<T>>, Error = Error>
where H: Into<T::Hash> + 'static
{
self.connect().and_then(|rpc| rpc.block(hash.map(|h| h.into())))
}
/// Subscribe to events.
pub fn subscribe_events(
&self,
@@ -504,6 +518,22 @@ mod tests {
.expect("Extrinsic should be included in a block");
}
#[test]
#[ignore] // requires locally running substrate node
fn test_getting_hash() {
let (mut rt, client) = test_setup();
rt.block_on(client.block_hash(None)).unwrap();
}
#[test]
#[ignore] // requires locally running substrate node
fn test_getting_block() {
let (mut rt, client) = test_setup();
rt.block_on(client.block_hash(None).and_then(move |h| {
client.block(h)
})).unwrap();
}
#[test]
#[ignore] // requires locally running substrate node
fn test_state_read_free_balance() {