Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 4f2b2cd

Browse files
committed
Subscription test
1 parent 91c14b4 commit 4f2b2cd

File tree

1 file changed

+47
-64
lines changed

1 file changed

+47
-64
lines changed

client/rpc/src/author/tests.rs

Lines changed: 47 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use sc_transaction_pool::{BasicPool, FullChainApi};
2626
use serde_json::value::to_raw_value;
2727
use sp_core::{
2828
blake2_256,
29+
bytes::to_hex,
2930
crypto::{CryptoTypePublicPair, Pair, Public},
3031
ed25519,
3132
hexdisplay::HexDisplay,
@@ -101,71 +102,53 @@ async fn author_submit_transaction_should_not_cause_error() {
101102
assert!(response.error.message.contains("Already imported"));
102103
}
103104

104-
// #[test]
105-
// fn submit_rich_transaction_should_not_cause_error() {
106-
// let p = TestSetup::default().author();
107-
// let xt = uxt(AccountKeyring::Alice, 0).encode();
108-
// let h: H256 = blake2_256(&xt).into();
109-
110-
// assert_matches!(
111-
// executor::block_on(AuthorApi::submit_extrinsic(&p, xt.clone().into())),
112-
// Ok(h2) if h == h2
113-
// );
114-
// assert!(executor::block_on(AuthorApi::submit_extrinsic(&p, xt.into())).is_err());
115-
// }
116-
117-
// #[test]
118-
// fn should_watch_extrinsic() {
119-
// // given
120-
// let setup = TestSetup::default();
121-
// let p = setup.author();
122-
123-
// let (subscriber, id_rx, data) = jsonrpc_pubsub::typed::Subscriber::new_test("test");
124-
125-
// // when
126-
// p.watch_extrinsic(
127-
// Default::default(),
128-
// subscriber,
129-
// uxt(AccountKeyring::Alice, 0).encode().into(),
130-
// );
105+
#[tokio::test]
106+
async fn author_should_watch_extrinsic() {
107+
let api = TestSetup::default().author().into_rpc();
131108

132-
// let id = executor::block_on(id_rx).unwrap().unwrap();
133-
// assert_matches!(id, SubscriptionId::String(_));
134-
135-
// let id = match id {
136-
// SubscriptionId::String(id) => id,
137-
// _ => unreachable!(),
138-
// };
139-
140-
// // check notifications
141-
// let replacement = {
142-
// let tx = Transfer {
143-
// amount: 5,
144-
// nonce: 0,
145-
// from: AccountKeyring::Alice.into(),
146-
// to: Default::default(),
147-
// };
148-
// tx.into_signed_tx()
149-
// };
150-
// executor::block_on(AuthorApi::submit_extrinsic(&p, replacement.encode().into())).unwrap();
151-
// let (res, data) = executor::block_on(data.into_future());
152-
153-
// let expected = Some(format!(
154-
// r#"{{"jsonrpc":"2.0","method":"test","params":{{"result":"ready","subscription":"{}"}}}}"#,
155-
// id,
156-
// ));
157-
// assert_eq!(res, expected);
158-
159-
// let h = blake2_256(&replacement.encode());
160-
// let expected = Some(format!(
161-
// r#"{{"jsonrpc":"2.0","method":"test","params":{{"result":{{"usurped":"0x{}"}},"subscription":"{}"
162-
// }}}}"#, HexDisplay::from(&h),
163-
// id,
164-
// ));
165-
166-
// let res = executor::block_on(data.into_future()).0;
167-
// assert_eq!(res, expected);
168-
// }
109+
let xt = {
110+
let xt_bytes = uxt(AccountKeyring::Alice, 0).encode();
111+
to_raw_value(&[to_hex(&xt_bytes, true)])
112+
}
113+
.unwrap();
114+
115+
let (subscription_id, mut rx) =
116+
api.test_subscription("author_submitAndWatchExtrinsic", Some(xt)).await;
117+
let subscription_data = rx.next().await;
118+
119+
let expected = Some(format!(
120+
// TODO: (dp) The `jsonrpc` version of this wraps the subscription ID in `"` – is this a problem? I think not.
121+
r#"{{"jsonrpc":"2.0","method":"author_submitAndWatchExtrinsic","params":{{"subscription":{},"result":"ready"}}}}"#,
122+
subscription_id,
123+
));
124+
assert_eq!(subscription_data, expected);
125+
126+
// Replace the extrinsic and observe the subscription is notified.
127+
let (xt_replacement, xt_hash) = {
128+
let tx = Transfer {
129+
amount: 5,
130+
nonce: 0,
131+
from: AccountKeyring::Alice.into(),
132+
to: Default::default(),
133+
};
134+
let tx = tx.into_signed_tx().encode();
135+
let hash = blake2_256(&tx);
136+
137+
(to_raw_value(&[to_hex(&tx, true)]).unwrap(), hash)
138+
};
139+
140+
let json = api.call("author_submitExtrinsic", Some(xt_replacement)).await.unwrap();
141+
142+
let expected = Some(format!(
143+
// TODO: (dp) The `jsonrpc` version of this wraps the subscription ID in `"` – is this a
144+
// problem? I think not.
145+
r#"{{"jsonrpc":"2.0","method":"author_submitAndWatchExtrinsic","params":{{"subscription":{},"result":{{"usurped":"0x{}"}}}}}}"#,
146+
subscription_id,
147+
HexDisplay::from(&xt_hash),
148+
));
149+
let subscription_data = rx.next().await;
150+
assert_eq!(subscription_data, expected);
151+
}
169152

170153
// #[test]
171154
// fn should_return_watch_validation_error() {

0 commit comments

Comments
 (0)