Last finalised block PubSub & RPC (#1080)

* Finalised blocks PubSub

* Finalised head RPC.

* Unify spelling.
This commit is contained in:
Tomasz Drwięga
2018-11-07 13:55:00 +01:00
committed by Gav Wood
parent 660c747c51
commit 3acafa9d2f
2 changed files with 136 additions and 23 deletions
+64
View File
@@ -150,7 +150,39 @@ fn should_return_block_hash() {
client.block_hash(Some(1u64).into()),
Ok(Some(ref x)) if x == &block.hash()
);
}
#[test]
fn should_return_finalised_hash() {
let core = ::tokio::runtime::Runtime::new().unwrap();
let remote = core.executor();
let client = Chain {
client: Arc::new(test_client::new()),
subscriptions: Subscriptions::new(remote),
};
assert_matches!(
client.finalised_head(),
Ok(ref x) if x == &client.client.genesis_hash()
);
// import new block
let builder = client.client.new_block().unwrap();
client.client.justify_and_import(BlockOrigin::Own, builder.bake().unwrap()).unwrap();
// no finalisation yet
assert_matches!(
client.finalised_head(),
Ok(ref x) if x == &client.client.genesis_hash()
);
// finalise
client.client.finalize_block(BlockId::number(1), true).unwrap();
assert_matches!(
client.finalised_head(),
Ok(ref x) if x == &client.client.block_hash(1).unwrap().unwrap()
);
}
#[test]
@@ -184,6 +216,38 @@ fn should_notify_about_latest_block() {
assert_eq!(core.block_on(next.into_future()).unwrap().0, None);
}
#[test]
fn should_notify_about_finalised_block() {
let mut core = ::tokio::runtime::Runtime::new().unwrap();
let remote = core.executor();
let (subscriber, id, transport) = pubsub::Subscriber::new_test("test");
{
let api = Chain {
client: Arc::new(test_client::new()),
subscriptions: Subscriptions::new(remote),
};
api.subscribe_finalised_heads(Default::default(), subscriber);
// assert id assigned
assert_eq!(core.block_on(id), Ok(Ok(SubscriptionId::Number(1))));
let builder = api.client.new_block().unwrap();
api.client.justify_and_import(BlockOrigin::Own, builder.bake().unwrap()).unwrap();
api.client.finalize_block(BlockId::number(1), true).unwrap();
}
// assert initial head sent.
let (notification, next) = core.block_on(transport.into_future()).unwrap();
assert!(notification.is_some());
// assert notification sent to transport
let (notification, next) = core.block_on(next.into_future()).unwrap();
assert!(notification.is_some());
// no more notifications on this channel
assert_eq!(core.block_on(next.into_future()).unwrap().0, None);
}
#[test]
fn should_return_runtime_version() {
let core = ::tokio::runtime::Runtime::new().unwrap();