Skip to content

Commit 65a5578

Browse files
committed
Nit
1 parent 3f9f609 commit 65a5578

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

runtimes/bulletin-westend/tests/tests.rs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ fn advance_block() {
5858
TransactionStorage::on_initialize(next);
5959
}
6060

61+
/// Constructs an unsigned extrinsic when `sender` is `None`.
6162
fn construct_extrinsic(
62-
sender: sp_core::sr25519::Pair,
63+
sender: Option<sp_core::sr25519::Pair>,
6364
call: RuntimeCall,
64-
) -> Result<UncheckedExtrinsic, sp_runtime::transaction_validity::TransactionValidityError> {
65-
let account_id = parachains_common::AccountId::from(sender.public());
65+
) -> Result<UncheckedExtrinsic, transaction_validity::TransactionValidityError> {
6666
// provide a known block hash for the immortal era check
6767
frame_system::BlockHash::<Runtime>::insert(0, PcHash::default());
6868
let inner = (
@@ -72,9 +72,12 @@ fn construct_extrinsic(
7272
frame_system::CheckTxVersion::<Runtime>::new(),
7373
frame_system::CheckGenesis::<Runtime>::new(),
7474
frame_system::CheckEra::<Runtime>::from(sp_runtime::generic::Era::immortal()),
75-
frame_system::CheckNonce::<Runtime>::from(
76-
frame_system::Pallet::<Runtime>::account(&account_id).nonce,
77-
),
75+
frame_system::CheckNonce::<Runtime>::from(if let Some(s) = sender.as_ref() {
76+
let account_id = AccountId::from(s.public());
77+
frame_system::Pallet::<Runtime>::account(&account_id).nonce
78+
} else {
79+
0
80+
}),
7881
frame_system::CheckWeight::<Runtime>::new(),
7982
pallet_skip_feeless_payment::SkipCheckIfFeeless::from(
8083
pallet_transaction_payment::ChargeTransactionPayment::<Runtime>::from(0u128),
@@ -84,18 +87,26 @@ fn construct_extrinsic(
8487
);
8588
let tx_ext: TxExtension =
8689
cumulus_pallet_weight_reclaim::StorageWeightReclaim::<Runtime, _>::from(inner);
87-
let payload = sp_runtime::generic::SignedPayload::new(call.clone(), tx_ext.clone())?;
88-
let signature = payload.using_encoded(|e| sender.sign(e));
89-
Ok(UncheckedExtrinsic::new_signed(
90-
call,
91-
account_id.into(),
92-
PcSignature::Sr25519(signature),
93-
tx_ext,
94-
))
90+
91+
if let Some(s) = sender.as_ref() {
92+
// Signed call.
93+
let account_id = AccountId::from(s.public());
94+
let payload = sp_runtime::generic::SignedPayload::new(call.clone(), tx_ext.clone())?;
95+
let signature = payload.using_encoded(|e| s.sign(e));
96+
Ok(UncheckedExtrinsic::new_signed(
97+
call,
98+
account_id.into(),
99+
PcSignature::Sr25519(signature),
100+
tx_ext,
101+
))
102+
} else {
103+
// Unsigned call.
104+
Ok(UncheckedExtrinsic::new_transaction(call, tx_ext))
105+
}
95106
}
96107

97108
fn construct_and_apply_extrinsic(
98-
account: sp_core::sr25519::Pair,
109+
account: Option<sp_core::sr25519::Pair>,
99110
call: RuntimeCall,
100111
) -> ApplyExtrinsicResult {
101112
let dispatch_info = call.get_dispatch_info();
@@ -147,7 +158,7 @@ fn transaction_storage_runtime_sizes() {
147158

148159
tracing::info!("Storing data with size: {size} and index: {index}");
149160
let res = construct_and_apply_extrinsic(
150-
account.pair(),
161+
Some(account.pair()),
151162
RuntimeCall::TransactionStorage(TxStorageCall::<Runtime>::store {
152163
data: vec![0u8; size],
153164
}),
@@ -177,7 +188,7 @@ fn transaction_storage_runtime_sizes() {
177188
AuthorizationExtent { transactions: 1_u32, bytes: oversized },
178189
);
179190
let res = construct_and_apply_extrinsic(
180-
account.pair(),
191+
Some(account.pair()),
181192
RuntimeCall::TransactionStorage(TxStorageCall::<Runtime>::store {
182193
data: vec![0u8; oversized as usize],
183194
}),
@@ -224,7 +235,7 @@ fn transaction_storage_max_throughput_per_block() {
224235
// Store all 8 transactions in the same block (no advance_block between them)
225236
for index in 0..NUM_TRANSACTIONS {
226237
let res = construct_and_apply_extrinsic(
227-
account.pair(),
238+
Some(account.pair()),
228239
RuntimeCall::TransactionStorage(TxStorageCall::<Runtime>::store {
229240
data: vec![index as u8; TRANSACTION_SIZE as _],
230241
}),
@@ -236,7 +247,7 @@ fn transaction_storage_max_throughput_per_block() {
236247
// 9th should fail.
237248
assert_err!(
238249
construct_and_apply_extrinsic(
239-
account.pair(),
250+
Some(account.pair()),
240251
RuntimeCall::TransactionStorage(TxStorageCall::<Runtime>::store {
241252
data: vec![0u8; TRANSACTION_SIZE as _],
242253
}),
@@ -258,6 +269,7 @@ fn transaction_storage_max_throughput_per_block() {
258269
fn authorized_storage_transactions_are_for_free() {
259270
sp_io::TestExternalities::new(RuntimeGenesisConfig::default().build_storage().unwrap())
260271
.execute_with(|| {
272+
// 1. user authorization flow.
261273
let account = Sr25519Keyring::Eve;
262274
let who: AccountId = account.to_account_id();
263275
let call = RuntimeCall::TransactionStorage(TxStorageCall::<Runtime>::store {
@@ -266,7 +278,7 @@ fn authorized_storage_transactions_are_for_free() {
266278

267279
// Not authorized account should fail to store.
268280
assert_err!(
269-
construct_and_apply_extrinsic(account.pair(), call.clone()),
281+
construct_and_apply_extrinsic(Some(account.pair()), call.clone()),
270282
transaction_validity::TransactionValidityError::Invalid(
271283
InvalidTransaction::Payment
272284
)
@@ -279,7 +291,7 @@ fn authorized_storage_transactions_are_for_free() {
279291
24,
280292
));
281293
// Now should work.
282-
let res = construct_and_apply_extrinsic(account.pair(), call);
294+
let res = construct_and_apply_extrinsic(Some(account.pair()), call);
283295
assert_ok!(res);
284296
assert_ok!(res.unwrap());
285297
});

0 commit comments

Comments
 (0)