Skip to content

Commit

Permalink
Insipx/add block method (paritytech#18)
Browse files Browse the repository at this point in the history
* Fix compile errors

* Fix tests, close paritytech#13 BTW

* add block method

* add block_hash method

* add tests for block and block_hash
  • Loading branch information
insipx authored and ascjones committed Sep 12, 2019
1 parent 650c6d3 commit e9070e2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ use crate::{
rpc::{
MapStream,
Rpc,
ChainBlock,
BlockNumber
},
srml::{
system::{
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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() {
Expand Down
13 changes: 12 additions & 1 deletion src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e9070e2

Please sign in to comment.