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() {
+12 -1
View File
@@ -51,7 +51,8 @@ use substrate_rpc_api::{
state::StateClient,
};
type ChainBlock<T> = SignedBlock<Block<<T as System>::Header, OpaqueExtrinsic>>;
pub type ChainBlock<T> = SignedBlock<Block<<T as System>::Header, OpaqueExtrinsic>>;
pub type BlockNumber<T> = NumberOrHex<<T as System>::BlockNumber>;
/// Client for substrate rpc interfaces
pub struct Rpc<T: System> {
@@ -107,6 +108,16 @@ impl<T: System> Rpc<T> {
})
}
/// Get a block hash, returns hash of latest block by default
pub fn block_hash(&self, hash: Option<BlockNumber<T>>) -> impl Future<Item = Option<T::Hash>, Error = Error> {
self.chain.block_hash(hash).map_err(Into::into)
}
/// Get a Block
pub fn block(&self, hash: Option<T::Hash>) -> impl Future<Item = Option<ChainBlock<T>>, Error = Error> {
self.chain.block(hash).map_err(Into::into)
}
/// Fetch the runtime version
pub fn runtime_version(&self, at: Option<T::Hash>) -> impl Future<Item = RuntimeVersion, Error = Error> {
self.state.runtime_version(at).map_err(Into::into)