1515// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
1616
1717use node_runtime:: {
18- Call , Executive , Runtime , SubmitTransaction , UncheckedExtrinsic ,
18+ Executive , Runtime , UncheckedExtrinsic ,
1919} ;
2020use sp_application_crypto:: AppKey ;
2121use sp_core:: testing:: KeyStore ;
22- use sp_core:: traits:: KeystoreExt ;
23- use sp_core:: offchain:: {
24- TransactionPoolExt ,
25- testing:: TestTransactionPoolExt ,
22+ use sp_core:: {
23+ offchain:: {
24+ TransactionPoolExt ,
25+ testing:: TestTransactionPoolExt ,
26+ } ,
27+ traits:: KeystoreExt ,
28+ } ;
29+ use frame_system:: {
30+ offchain:: {
31+ Signer ,
32+ SubmitTransaction ,
33+ SendSignedTransaction ,
34+ }
2635} ;
27- use frame_system:: offchain:: { SubmitSignedTransaction , SubmitUnsignedTransaction } ;
28- use pallet_im_online:: sr25519:: AuthorityPair as Key ;
2936use codec:: Decode ;
3037
3138pub mod common;
@@ -47,8 +54,7 @@ fn should_submit_unsigned_transaction() {
4754 } ;
4855
4956 let call = pallet_im_online:: Call :: heartbeat ( heartbeat_data, signature) ;
50- <SubmitTransaction as SubmitUnsignedTransaction < Runtime , Call > >
51- :: submit_unsigned ( call)
57+ SubmitTransaction :: < Runtime , pallet_im_online:: Call < Runtime > > :: submit_unsigned_transaction ( call. into ( ) )
5258 . unwrap ( ) ;
5359
5460 assert_eq ! ( state. read( ) . transactions. len( ) , 1 )
@@ -64,23 +70,16 @@ fn should_submit_signed_transaction() {
6470 t. register_extension ( TransactionPoolExt :: new ( pool) ) ;
6571
6672 let keystore = KeyStore :: new ( ) ;
67- keystore. write ( ) . sr25519_generate_new ( Key :: ID , Some ( & format ! ( "{}/hunter1" , PHRASE ) ) ) . unwrap ( ) ;
68- keystore. write ( ) . sr25519_generate_new ( Key :: ID , Some ( & format ! ( "{}/hunter2" , PHRASE ) ) ) . unwrap ( ) ;
69- keystore. write ( ) . sr25519_generate_new ( Key :: ID , Some ( & format ! ( "{}/hunter3" , PHRASE ) ) ) . unwrap ( ) ;
73+ keystore. write ( ) . sr25519_generate_new ( sr25519 :: AuthorityId :: ID , Some ( & format ! ( "{}/hunter1" , PHRASE ) ) ) . unwrap ( ) ;
74+ keystore. write ( ) . sr25519_generate_new ( sr25519 :: AuthorityId :: ID , Some ( & format ! ( "{}/hunter2" , PHRASE ) ) ) . unwrap ( ) ;
75+ keystore. write ( ) . sr25519_generate_new ( sr25519 :: AuthorityId :: ID , Some ( & format ! ( "{}/hunter3" , PHRASE ) ) ) . unwrap ( ) ;
7076 t. register_extension ( KeystoreExt ( keystore) ) ;
7177
7278 t. execute_with ( || {
73- let keys = <SubmitTransaction as SubmitSignedTransaction < Runtime , Call > >
74- :: find_all_local_keys ( ) ;
75- assert_eq ! ( keys. len( ) , 3 , "Missing keys: {:?}" , keys) ;
76-
77- let can_sign = <SubmitTransaction as SubmitSignedTransaction < Runtime , Call > >
78- :: can_sign ( ) ;
79- assert ! ( can_sign, "Since there are keys, `can_sign` should return true" ) ;
80-
81- let call = pallet_balances:: Call :: transfer ( Default :: default ( ) , Default :: default ( ) ) ;
82- let results =
83- <SubmitTransaction as SubmitSignedTransaction < Runtime , Call > >:: submit_signed ( call) ;
79+ let results = Signer :: < Runtime , TestAuthorityId > :: all_accounts ( )
80+ . send_signed_transaction ( |_| {
81+ pallet_balances:: Call :: transfer ( Default :: default ( ) , Default :: default ( ) )
82+ } ) ;
8483
8584 let len = results. len ( ) ;
8685 assert_eq ! ( len, 3 ) ;
@@ -96,27 +95,26 @@ fn should_submit_signed_twice_from_the_same_account() {
9695 t. register_extension ( TransactionPoolExt :: new ( pool) ) ;
9796
9897 let keystore = KeyStore :: new ( ) ;
99- keystore. write ( ) . sr25519_generate_new ( Key :: ID , Some ( & format ! ( "{}/hunter1" , PHRASE ) ) ) . unwrap ( ) ;
98+ keystore. write ( ) . sr25519_generate_new ( sr25519:: AuthorityId :: ID , Some ( & format ! ( "{}/hunter1" , PHRASE ) ) ) . unwrap ( ) ;
99+ keystore. write ( ) . sr25519_generate_new ( sr25519:: AuthorityId :: ID , Some ( & format ! ( "{}/hunter2" , PHRASE ) ) ) . unwrap ( ) ;
100100 t. register_extension ( KeystoreExt ( keystore) ) ;
101101
102102 t. execute_with ( || {
103- let call = pallet_balances:: Call :: transfer ( Default :: default ( ) , Default :: default ( ) ) ;
104- let results =
105- <SubmitTransaction as SubmitSignedTransaction < Runtime , Call > >:: submit_signed ( call) ;
103+ let result = Signer :: < Runtime , TestAuthorityId > :: any_account ( )
104+ . send_signed_transaction ( |_| {
105+ pallet_balances:: Call :: transfer ( Default :: default ( ) , Default :: default ( ) )
106+ } ) ;
106107
107- let len = results. len ( ) ;
108- assert_eq ! ( len, 1 ) ;
109- assert_eq ! ( results. into_iter( ) . filter_map( |x| x. 1 . ok( ) ) . count( ) , len) ;
108+ assert ! ( result. is_some( ) ) ;
110109 assert_eq ! ( state. read( ) . transactions. len( ) , 1 ) ;
111110
112111 // submit another one from the same account. The nonce should be incremented.
113- let call = pallet_balances:: Call :: transfer ( Default :: default ( ) , Default :: default ( ) ) ;
114- let results =
115- <SubmitTransaction as SubmitSignedTransaction < Runtime , Call > >:: submit_signed ( call) ;
112+ let result = Signer :: < Runtime , TestAuthorityId > :: any_account ( )
113+ . send_signed_transaction ( |_| {
114+ pallet_balances:: Call :: transfer ( Default :: default ( ) , Default :: default ( ) )
115+ } ) ;
116116
117- let len = results. len ( ) ;
118- assert_eq ! ( len, 1 ) ;
119- assert_eq ! ( results. into_iter( ) . filter_map( |x| x. 1 . ok( ) ) . count( ) , len) ;
117+ assert ! ( result. is_some( ) ) ;
120118 assert_eq ! ( state. read( ) . transactions. len( ) , 2 ) ;
121119
122120 // now check that the transaction nonces are not equal
@@ -134,6 +132,60 @@ fn should_submit_signed_twice_from_the_same_account() {
134132 } ) ;
135133}
136134
135+ #[ test]
136+ fn should_submit_signed_twice_from_all_accounts ( ) {
137+ let mut t = new_test_ext ( COMPACT_CODE , false ) ;
138+ let ( pool, state) = TestTransactionPoolExt :: new ( ) ;
139+ t. register_extension ( TransactionPoolExt :: new ( pool) ) ;
140+
141+ let keystore = KeyStore :: new ( ) ;
142+ keystore. write ( ) . sr25519_generate_new ( sr25519:: AuthorityId :: ID , Some ( & format ! ( "{}/hunter1" , PHRASE ) ) ) . unwrap ( ) ;
143+ keystore. write ( ) . sr25519_generate_new ( sr25519:: AuthorityId :: ID , Some ( & format ! ( "{}/hunter2" , PHRASE ) ) ) . unwrap ( ) ;
144+ t. register_extension ( KeystoreExt ( keystore) ) ;
145+
146+ t. execute_with ( || {
147+ let results = Signer :: < Runtime , TestAuthorityId > :: all_accounts ( )
148+ . send_signed_transaction ( |_| {
149+ pallet_balances:: Call :: transfer ( Default :: default ( ) , Default :: default ( ) )
150+ } ) ;
151+
152+ let len = results. len ( ) ;
153+ assert_eq ! ( len, 2 ) ;
154+ assert_eq ! ( results. into_iter( ) . filter_map( |x| x. 1 . ok( ) ) . count( ) , len) ;
155+ assert_eq ! ( state. read( ) . transactions. len( ) , 2 ) ;
156+
157+ // submit another one from the same account. The nonce should be incremented.
158+ let results = Signer :: < Runtime , TestAuthorityId > :: all_accounts ( )
159+ . send_signed_transaction ( |_| {
160+ pallet_balances:: Call :: transfer ( Default :: default ( ) , Default :: default ( ) )
161+ } ) ;
162+
163+ let len = results. len ( ) ;
164+ assert_eq ! ( len, 2 ) ;
165+ assert_eq ! ( results. into_iter( ) . filter_map( |x| x. 1 . ok( ) ) . count( ) , len) ;
166+ assert_eq ! ( state. read( ) . transactions. len( ) , 4 ) ;
167+
168+ // now check that the transaction nonces are not equal
169+ let s = state. read ( ) ;
170+ fn nonce ( tx : UncheckedExtrinsic ) -> frame_system:: CheckNonce < Runtime > {
171+ let extra = tx. signature . unwrap ( ) . 2 ;
172+ extra. 4
173+ }
174+ let nonce1 = nonce ( UncheckedExtrinsic :: decode ( & mut & * s. transactions [ 0 ] ) . unwrap ( ) ) ;
175+ let nonce2 = nonce ( UncheckedExtrinsic :: decode ( & mut & * s. transactions [ 1 ] ) . unwrap ( ) ) ;
176+ let nonce3 = nonce ( UncheckedExtrinsic :: decode ( & mut & * s. transactions [ 2 ] ) . unwrap ( ) ) ;
177+ let nonce4 = nonce ( UncheckedExtrinsic :: decode ( & mut & * s. transactions [ 3 ] ) . unwrap ( ) ) ;
178+ assert ! (
179+ nonce1 != nonce3,
180+ "Transactions should have different nonces. Got: 1st tx nonce: {:?}, 2nd nonce: {:?}" , nonce1, nonce3
181+ ) ;
182+ assert ! (
183+ nonce2 != nonce4,
184+ "Transactions should have different nonces. Got: 1st tx nonce: {:?}, 2nd tx nonce: {:?}" , nonce2, nonce4
185+ ) ;
186+ } ) ;
187+ }
188+
137189#[ test]
138190fn submitted_transaction_should_be_valid ( ) {
139191 use codec:: Encode ;
@@ -145,13 +197,14 @@ fn submitted_transaction_should_be_valid() {
145197 t. register_extension ( TransactionPoolExt :: new ( pool) ) ;
146198
147199 let keystore = KeyStore :: new ( ) ;
148- keystore. write ( ) . sr25519_generate_new ( Key :: ID , Some ( & format ! ( "{}/hunter1" , PHRASE ) ) ) . unwrap ( ) ;
200+ keystore. write ( ) . sr25519_generate_new ( sr25519 :: AuthorityId :: ID , Some ( & format ! ( "{}/hunter1" , PHRASE ) ) ) . unwrap ( ) ;
149201 t. register_extension ( KeystoreExt ( keystore) ) ;
150202
151203 t. execute_with ( || {
152- let call = pallet_balances:: Call :: transfer ( Default :: default ( ) , Default :: default ( ) ) ;
153- let results =
154- <SubmitTransaction as SubmitSignedTransaction < Runtime , Call > >:: submit_signed ( call) ;
204+ let results = Signer :: < Runtime , TestAuthorityId > :: all_accounts ( )
205+ . send_signed_transaction ( |_| {
206+ pallet_balances:: Call :: transfer ( Default :: default ( ) , Default :: default ( ) )
207+ } ) ;
155208 let len = results. len ( ) ;
156209 assert_eq ! ( len, 1 ) ;
157210 assert_eq ! ( results. into_iter( ) . filter_map( |x| x. 1 . ok( ) ) . count( ) , len) ;
0 commit comments