1818
1919use super :: * ;
2020
21- use assert_matches :: assert_matches ;
21+ use crate :: testing :: { deser_call , deser_error } ;
2222use codec:: Encode ;
2323use jsonrpsee:: {
2424 types:: v2:: { Response , RpcError , SubscriptionResponse } ,
@@ -37,7 +37,7 @@ use sp_core::{
3737 H256 ,
3838} ;
3939use sp_keystore:: testing:: KeyStore ;
40- use std:: { mem , sync:: Arc } ;
40+ use std:: sync:: Arc ;
4141use substrate_test_runtime_client:: {
4242 self ,
4343 runtime:: { Block , Extrinsic , SessionKeys , Transfer } ,
@@ -89,21 +89,19 @@ impl TestSetup {
8989
9090#[ tokio:: test]
9191async fn author_submit_transaction_should_not_cause_error ( ) {
92- env_logger:: init ( ) ;
92+ let _ = env_logger:: try_init ( ) ;
9393 let author = TestSetup :: default ( ) . author ( ) ;
9494 let api = author. into_rpc ( ) ;
9595 let xt: Bytes = uxt ( AccountKeyring :: Alice , 1 ) . encode ( ) . into ( ) ;
9696 let extrinsic_hash: H256 = blake2_256 ( & xt) . into ( ) ;
97- let params = to_raw_value ( & [ xt. clone ( ) ] ) . unwrap ( ) ;
98- let json = api. call ( "author_submitExtrinsic" , Some ( params) ) . await . unwrap ( ) ;
99- let response: Response < H256 > = serde_json:: from_str ( & json) . unwrap ( ) ;
97+ let response: H256 =
98+ deser_call ( api. call_with ( "author_submitExtrinsic" , [ xt. clone ( ) ] ) . await . unwrap ( ) ) ;
10099
101- assert_eq ! ( response. result , extrinsic_hash, ) ;
100+ assert_eq ! ( response, extrinsic_hash) ;
102101
102+ let response = api. call_with ( "author_submitExtrinsic" , [ xt] ) . await . unwrap ( ) ;
103103 // Can't submit the same extrinsic twice
104- let params_again = to_raw_value ( & [ xt] ) . unwrap ( ) ;
105- let json = api. call ( "author_submitExtrinsic" , Some ( params_again) ) . await . unwrap ( ) ;
106- let response: RpcError = serde_json:: from_str ( & json) . unwrap ( ) ;
104+ let response = deser_error ( & response) ;
107105
108106 assert ! ( response. error. message. contains( "Already imported" ) ) ;
109107}
@@ -203,38 +201,21 @@ async fn author_should_remove_extrinsics() {
203201
204202 // Submit three extrinsics, then remove two of them (will cause the third to be removed as well,
205203 // having a higher nonce)
206- let ( xt1, xt1_bytes) = {
207- let xt_bytes = uxt ( AccountKeyring :: Alice , 0 ) . encode ( ) ;
208- let xt_hex = to_hex ( & xt_bytes, true ) ;
209- ( to_raw_value ( & [ xt_hex] ) . unwrap ( ) , xt_bytes)
210- } ;
211- let xt1_out = api. call ( "author_submitExtrinsic" , Some ( xt1) ) . await . unwrap ( ) ;
212- let xt1_hash: Response < H256 > = serde_json:: from_str ( & xt1_out) . unwrap ( ) ;
213- let xt1_hash = xt1_hash. result ;
204+ let xt1_bytes = uxt ( AccountKeyring :: Alice , 0 ) . encode ( ) ;
205+ let xt1 = to_hex ( & xt1_bytes, true ) ;
206+ let xt1_hash: H256 = deser_call ( api. call_with ( "author_submitExtrinsic" , [ xt1] ) . await . unwrap ( ) ) ;
214207
215- let ( xt2, xt2_bytes) = {
216- let xt_bytes = uxt ( AccountKeyring :: Alice , 1 ) . encode ( ) ;
217- let xt_hex = to_hex ( & xt_bytes, true ) ;
218- ( to_raw_value ( & [ xt_hex] ) . unwrap ( ) , xt_bytes)
219- } ;
220- let xt2_out = api. call ( "author_submitExtrinsic" , Some ( xt2) ) . await . unwrap ( ) ;
221- let xt2_hash: Response < H256 > = serde_json:: from_str ( & xt2_out) . unwrap ( ) ;
222- let xt2_hash = xt2_hash. result ;
208+ let xt2 = to_hex ( & uxt ( AccountKeyring :: Alice , 1 ) . encode ( ) , true ) ;
209+ let xt2_hash: H256 = deser_call ( api. call_with ( "author_submitExtrinsic" , [ xt2] ) . await . unwrap ( ) ) ;
223210
224- let ( xt3, xt3_bytes) = {
225- let xt_bytes = uxt ( AccountKeyring :: Bob , 0 ) . encode ( ) ;
226- let xt_hex = to_hex ( & xt_bytes, true ) ;
227- ( to_raw_value ( & [ xt_hex] ) . unwrap ( ) , xt_bytes)
228- } ;
229- let xt3_out = api. call ( "author_submitExtrinsic" , Some ( xt3) ) . await . unwrap ( ) ;
230- let xt3_hash: Response < H256 > = serde_json:: from_str ( & xt3_out) . unwrap ( ) ;
231- let xt3_hash = xt3_hash. result ;
211+ let xt3 = to_hex ( & uxt ( AccountKeyring :: Bob , 0 ) . encode ( ) , true ) ;
212+ let xt3_hash: H256 = deser_call ( api. call_with ( "author_submitExtrinsic" , [ xt3] ) . await . unwrap ( ) ) ;
232213 assert_eq ! ( setup. pool. status( ) . ready, 3 ) ;
233214
234215 // Now remove all three.
235216 // Notice how we need an extra `Vec` wrapping the `Vec` we want to submit as params.
236- let removed = api
237- . call_with (
217+ let removed: Vec < H256 > = deser_call (
218+ api . call_with (
238219 METH ,
239220 vec ! [ vec![
240221 hash:: ExtrinsicOrHash :: Hash ( xt3_hash) ,
@@ -243,10 +224,10 @@ async fn author_should_remove_extrinsics() {
243224 ] ] ,
244225 )
245226 . await
246- . unwrap ( ) ;
227+ . unwrap ( ) ,
228+ ) ;
247229
248- let removed: Response < Vec < H256 > > = serde_json:: from_str ( & removed) . unwrap ( ) ;
249- assert_eq ! ( removed. result, vec![ xt1_hash, xt2_hash, xt3_hash] ) ;
230+ assert_eq ! ( removed, vec![ xt1_hash, xt2_hash, xt3_hash] ) ;
250231}
251232
252233#[ tokio:: test]
@@ -273,12 +254,7 @@ async fn author_should_rotate_keys() {
273254 let setup = TestSetup :: default ( ) ;
274255 let api = setup. author ( ) . into_rpc ( ) ;
275256
276- let new_pubkeys = {
277- let json = api. call ( "author_rotateKeys" , None ) . await . unwrap ( ) ;
278- let response: Response < Bytes > = serde_json:: from_str ( & json) . unwrap ( ) ;
279- response. result
280- } ;
281-
257+ let new_pubkeys: Bytes = deser_call ( api. call ( "author_rotateKeys" , None ) . await . unwrap ( ) ) ;
282258 let session_keys =
283259 SessionKeys :: decode ( & mut & new_pubkeys[ ..] ) . expect ( "SessionKeys decode successfully" ) ;
284260 let ed25519_pubkeys = SyncCryptoStore :: keys ( & * setup. keystore , ED25519 ) . unwrap ( ) ;
@@ -295,44 +271,33 @@ async fn author_has_session_keys() {
295271 let api = TestSetup :: into_rpc ( ) ;
296272
297273 // Add a valid session key
298- let pubkeys = {
299- let json = api. call ( "author_rotateKeys" , None ) . await . expect ( "Rotates the keys" ) ;
300- let response: Response < Bytes > = serde_json:: from_str ( & json) . unwrap ( ) ;
301- response. result
302- } ;
274+ let pubkeys: Bytes =
275+ deser_call ( api. call ( "author_rotateKeys" , None ) . await . expect ( "Rotates the keys" ) ) ;
303276
304277 // Add a session key in a different keystore
305- let non_existent_pubkeys = {
278+ let non_existent_pubkeys: Bytes = {
306279 let api2 = TestSetup :: default ( ) . author ( ) . into_rpc ( ) ;
307- let json = api2. call ( "author_rotateKeys" , None ) . await . expect ( "Rotates the keys" ) ;
308- let response: Response < Bytes > = serde_json:: from_str ( & json) . unwrap ( ) ;
309- response. result
280+ deser_call ( api2. call ( "author_rotateKeys" , None ) . await . expect ( "Rotates the keys" ) )
310281 } ;
311282
312283 // Then…
313- let existing = {
314- let json = api. call_with ( "author_hasSessionKeys" , vec ! [ pubkeys] ) . await . unwrap ( ) ;
315- let response: Response < bool > = serde_json:: from_str ( & json) . unwrap ( ) ;
316- response. result
317- } ;
284+ let existing: bool =
285+ deser_call ( api. call_with ( "author_hasSessionKeys" , vec ! [ pubkeys] ) . await . unwrap ( ) ) ;
318286 assert ! ( existing, "Existing key is in the session keys" ) ;
319287
320- let inexistent = {
321- let json = api
322- . call_with ( "author_hasSessionKeys" , vec ! [ non_existent_pubkeys] )
288+ let inexistent: bool = deser_call (
289+ api. call_with ( "author_hasSessionKeys" , vec ! [ non_existent_pubkeys] )
323290 . await
324- . unwrap ( ) ;
325- let response: Response < bool > = serde_json:: from_str ( & json) . unwrap ( ) ;
326- response. result
327- } ;
291+ . unwrap ( ) ,
292+ ) ;
328293 assert_eq ! ( inexistent, false , "Inexistent key is not in the session keys" ) ;
329294
330295 let invalid = {
331296 let json = api
332297 . call_with ( "author_hasSessionKeys" , vec ! [ Bytes :: from( vec![ 1 , 2 , 3 ] ) ] )
333298 . await
334299 . unwrap ( ) ;
335- let response: RpcError = serde_json :: from_str ( & json) . unwrap ( ) ;
300+ let response: RpcError = deser_error ( & json) ;
336301 response. error . message . to_string ( )
337302 } ;
338303 assert_eq ! ( invalid, "Session keys are not encoded correctly" ) ;
0 commit comments