From e9070e278621cf2b75e2fc943bbcd9443cb9a77f Mon Sep 17 00:00:00 2001 From: Andrew Plaza Date: Thu, 12 Sep 2019 21:34:24 +0200 Subject: [PATCH] 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 --- src/lib.rs | 30 ++++++++++++++++++++++++++++++ src/rpc.rs | 13 ++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 6478e1a38c..42e7fbe6f5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -55,6 +55,8 @@ use crate::{ rpc::{ MapStream, Rpc, + ChainBlock, + BlockNumber }, srml::{ system::{ @@ -183,6 +185,18 @@ impl Client { 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>) -> impl Future, Error = Error> { + self.connect().and_then(|rpc| rpc.block_hash(hash.map(|h| h))) + } + + /// Get a block + pub fn block(&self, hash: Option) -> impl Future>, Error = Error> + where H: Into + '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() { diff --git a/src/rpc.rs b/src/rpc.rs index b316c4751d..2aa4146b8b 100644 --- a/src/rpc.rs +++ b/src/rpc.rs @@ -51,7 +51,8 @@ use substrate_rpc_api::{ state::StateClient, }; -type ChainBlock = SignedBlock::Header, OpaqueExtrinsic>>; +pub type ChainBlock = SignedBlock::Header, OpaqueExtrinsic>>; +pub type BlockNumber = NumberOrHex<::BlockNumber>; /// Client for substrate rpc interfaces pub struct Rpc { @@ -107,6 +108,16 @@ impl Rpc { }) } + /// Get a block hash, returns hash of latest block by default + pub fn block_hash(&self, hash: Option>) -> impl Future, Error = Error> { + self.chain.block_hash(hash).map_err(Into::into) + } + + /// Get a Block + pub fn block(&self, hash: Option) -> impl Future>, Error = Error> { + self.chain.block(hash).map_err(Into::into) + } + /// Fetch the runtime version pub fn runtime_version(&self, at: Option) -> impl Future { self.state.runtime_version(at).map_err(Into::into)