Skip to content
This repository was archived by the owner on Jul 4, 2022. It is now read-only.

Commit db4530b

Browse files
committed
Bring in SendTransactionTypes (paritytech/substrate#5182) [WIP]
1 parent e9ed13e commit db4530b

File tree

21 files changed

+1906
-599
lines changed

21 files changed

+1906
-599
lines changed

bin/node/executor/tests/common.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,21 @@
1515
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
1616

1717
use codec::{Encode, Decode};
18+
use frame_system::offchain::AppCrypto;
1819
use frame_support::Hashable;
1920
use sp_state_machine::TestExternalities as CoreTestExternalities;
20-
use sp_core::{NeverNativeValue, NativeOrEncoded, traits::{CodeExecutor, RuntimeCode}};
21-
use sp_runtime::{ApplyExtrinsicResult, traits::{Header as HeaderT, BlakeTwo256}};
21+
use sp_core::{
22+
NeverNativeValue, NativeOrEncoded,
23+
crypto::KeyTypeId,
24+
sr25519::Signature,
25+
traits::{CodeExecutor, RuntimeCode},
26+
};
27+
use sp_runtime::{
28+
ApplyExtrinsicResult,
29+
MultiSigner,
30+
MultiSignature,
31+
traits::{Header as HeaderT, BlakeTwo256},
32+
};
2233
use sc_executor::{NativeExecutor, WasmExecutionMethod};
2334
use sc_executor::error::Result;
2435

@@ -31,6 +42,25 @@ use node_primitives::{Hash, BlockNumber};
3142
use node_testing::keyring::*;
3243
use sp_externalities::Externalities;
3344

45+
pub const TEST_KEY_TYPE_ID: KeyTypeId = KeyTypeId(*b"test");
46+
47+
pub mod sr25519 {
48+
mod app_sr25519 {
49+
use sp_application_crypto::{app_crypto, sr25519};
50+
use super::super::TEST_KEY_TYPE_ID;
51+
app_crypto!(sr25519, TEST_KEY_TYPE_ID);
52+
}
53+
54+
pub type AuthorityId = app_sr25519::Public;
55+
}
56+
57+
pub struct TestAuthorityId;
58+
impl AppCrypto<MultiSigner, MultiSignature> for TestAuthorityId {
59+
type RuntimeAppPublic = sr25519::AuthorityId;
60+
type GenericSignature = Signature;
61+
type GenericPublic = sp_core::sr25519::Public;
62+
}
63+
3464
/// The wasm runtime code.
3565
///
3666
/// `compact` since it is after post-processing with wasm-gc which performs tree-shaking thus

bin/node/executor/tests/submit_transaction.rs

Lines changed: 93 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,24 @@
1515
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
1616

1717
use node_runtime::{
18-
Call, Executive, Runtime, SubmitTransaction, UncheckedExtrinsic,
18+
Executive, Runtime, UncheckedExtrinsic,
1919
};
2020
use sp_application_crypto::AppKey;
2121
use 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;
2936
use codec::Decode;
3037

3138
pub 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]
138190
fn 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

Comments
 (0)