-
Notifications
You must be signed in to change notification settings - Fork 721
/
lib.rs
1532 lines (1339 loc) · 50.2 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! The Substrate runtime. This can be compiled with `#[no_std]`, ready for Wasm.
#![cfg_attr(not(feature = "std"), no_std)]
extern crate alloc;
#[cfg(feature = "std")]
pub mod extrinsic;
#[cfg(feature = "std")]
pub mod genesismap;
pub mod substrate_test_pallet;
#[cfg(not(feature = "std"))]
use alloc::{vec, vec::Vec};
use codec::{Decode, Encode};
use frame_support::{
construct_runtime, derive_impl,
dispatch::DispatchClass,
genesis_builder_helper::{build_state, get_preset},
parameter_types,
traits::{ConstU32, ConstU64},
weights::{
constants::{BlockExecutionWeight, ExtrinsicBaseWeight, WEIGHT_REF_TIME_PER_SECOND},
Weight,
},
};
use frame_system::{
limits::{BlockLength, BlockWeights},
CheckNonce, CheckWeight,
};
use scale_info::TypeInfo;
use sp_application_crypto::Ss58Codec;
use sp_keyring::AccountKeyring;
use sp_application_crypto::{ecdsa, ed25519, sr25519, RuntimeAppPublic};
use sp_core::{OpaqueMetadata, RuntimeDebug};
use sp_trie::{
trie_types::{TrieDBBuilder, TrieDBMutBuilderV1},
PrefixedMemoryDB, StorageProof,
};
use trie_db::{Trie, TrieMut};
use serde_json::json;
use sp_api::{decl_runtime_apis, impl_runtime_apis};
pub use sp_core::hash::H256;
use sp_genesis_builder::PresetId;
use sp_inherents::{CheckInherentsResult, InherentData};
use sp_runtime::{
create_runtime_str, impl_opaque_keys,
traits::{BlakeTwo256, Block as BlockT, DispatchInfoOf, NumberFor, Verify},
transaction_validity::{TransactionSource, TransactionValidity, TransactionValidityError},
ApplyExtrinsicResult, ExtrinsicInclusionMode, Perbill,
};
#[cfg(any(feature = "std", test))]
use sp_version::NativeVersion;
use sp_version::RuntimeVersion;
pub use sp_consensus_babe::{AllowedSlots, BabeEpochConfiguration, Slot};
pub use pallet_balances::Call as BalancesCall;
pub type AuraId = sp_consensus_aura::sr25519::AuthorityId;
#[cfg(feature = "std")]
pub use extrinsic::{ExtrinsicBuilder, Transfer};
const LOG_TARGET: &str = "substrate-test-runtime";
// Include the WASM binary
#[cfg(feature = "std")]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
#[cfg(feature = "std")]
pub mod wasm_binary_logging_disabled {
include!(concat!(env!("OUT_DIR"), "/wasm_binary_logging_disabled.rs"));
}
/// Wasm binary unwrapped. If built with `SKIP_WASM_BUILD`, the function panics.
#[cfg(feature = "std")]
pub fn wasm_binary_unwrap() -> &'static [u8] {
WASM_BINARY.expect(
"Development wasm binary is not available. Testing is only supported with the flag
disabled.",
)
}
/// Wasm binary unwrapped. If built with `SKIP_WASM_BUILD`, the function panics.
#[cfg(feature = "std")]
pub fn wasm_binary_logging_disabled_unwrap() -> &'static [u8] {
wasm_binary_logging_disabled::WASM_BINARY.expect(
"Development wasm binary is not available. Testing is only supported with the flag
disabled.",
)
}
/// Test runtime version.
#[sp_version::runtime_version]
pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("test"),
impl_name: create_runtime_str!("parity-test"),
authoring_version: 1,
spec_version: 2,
impl_version: 2,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
system_version: 1,
};
fn version() -> RuntimeVersion {
VERSION
}
/// Native version.
#[cfg(any(feature = "std", test))]
pub fn native_version() -> NativeVersion {
NativeVersion { runtime_version: VERSION, can_author_with: Default::default() }
}
/// Transfer data extracted from Extrinsic containing `Balances::transfer_allow_death`.
#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct TransferData {
pub from: AccountId,
pub to: AccountId,
pub amount: Balance,
pub nonce: Nonce,
}
/// The address format for describing accounts.
pub type Address = sp_core::sr25519::Public;
pub type Signature = sr25519::Signature;
#[cfg(feature = "std")]
pub type Pair = sp_core::sr25519::Pair;
/// The SignedExtension to the basic transaction logic.
pub type SignedExtra = (
CheckNonce<Runtime>,
CheckWeight<Runtime>,
CheckSubstrateCall,
frame_metadata_hash_extension::CheckMetadataHash<Runtime>,
);
/// The payload being signed in transactions.
pub type SignedPayload = sp_runtime::generic::SignedPayload<RuntimeCall, SignedExtra>;
/// Unchecked extrinsic type as expected by this runtime.
pub type Extrinsic =
sp_runtime::generic::UncheckedExtrinsic<Address, RuntimeCall, Signature, SignedExtra>;
/// An identifier for an account on this system.
pub type AccountId = <Signature as Verify>::Signer;
/// A simple hash type for all our hashing.
pub type Hash = H256;
/// The hashing algorithm used.
pub type Hashing = BlakeTwo256;
/// The block number type used in this runtime.
pub type BlockNumber = u64;
/// Index of a transaction.
pub type Nonce = u64;
/// The item of a block digest.
pub type DigestItem = sp_runtime::generic::DigestItem;
/// The digest of a block.
pub type Digest = sp_runtime::generic::Digest;
/// A test block.
pub type Block = sp_runtime::generic::Block<Header, Extrinsic>;
/// A test block's header.
pub type Header = sp_runtime::generic::Header<BlockNumber, Hashing>;
/// Balance of an account.
pub type Balance = u64;
decl_runtime_apis! {
#[api_version(2)]
pub trait TestAPI {
/// Return the balance of the given account id.
fn balance_of(id: AccountId) -> u64;
/// A benchmark function that adds one to the given value and returns the result.
fn benchmark_add_one(val: &u64) -> u64;
/// A benchmark function that adds one to each value in the given vector and returns the
/// result.
fn benchmark_vector_add_one(vec: &Vec<u64>) -> Vec<u64>;
/// A function for that the signature changed in version `2`.
#[changed_in(2)]
fn function_signature_changed() -> Vec<u64>;
/// The new signature.
fn function_signature_changed() -> u64;
/// trie no_std testing
fn use_trie() -> u64;
/// Calls function in the loop using never-inlined function pointer
fn benchmark_indirect_call() -> u64;
/// Calls function in the loop
fn benchmark_direct_call() -> u64;
/// Allocates vector with given capacity.
fn vec_with_capacity(size: u32) -> Vec<u8>;
/// Returns the initialized block number.
fn get_block_number() -> u64;
/// Test that `ed25519` crypto works in the runtime.
///
/// Returns the signature generated for the message `ed25519` and the public key.
fn test_ed25519_crypto() -> (ed25519::AppSignature, ed25519::AppPublic);
/// Test that `sr25519` crypto works in the runtime.
///
/// Returns the signature generated for the message `sr25519`.
fn test_sr25519_crypto() -> (sr25519::AppSignature, sr25519::AppPublic);
/// Test that `ecdsa` crypto works in the runtime.
///
/// Returns the signature generated for the message `ecdsa`.
fn test_ecdsa_crypto() -> (ecdsa::AppSignature, ecdsa::AppPublic);
/// Run various tests against storage.
fn test_storage();
/// Check a witness.
fn test_witness(proof: StorageProof, root: crate::Hash);
/// Test that ensures that we can call a function that takes multiple
/// arguments.
fn test_multiple_arguments(data: Vec<u8>, other: Vec<u8>, num: u32);
/// Traces log "Hey I'm runtime."
fn do_trace_log();
/// Verify the given signature, public & message bundle.
fn verify_ed25519(sig: ed25519::Signature, public: ed25519::Public, message: Vec<u8>) -> bool;
/// Write the given `value` under the given `key` into the storage and then optional panic.
fn write_key_value(key: Vec<u8>, value: Vec<u8>, panic: bool);
}
}
pub type Executive = frame_executive::Executive<
Runtime,
Block,
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
>;
#[derive(Copy, Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct CheckSubstrateCall;
impl sp_runtime::traits::Printable for CheckSubstrateCall {
fn print(&self) {
"CheckSubstrateCall".print()
}
}
impl sp_runtime::traits::Dispatchable for CheckSubstrateCall {
type RuntimeOrigin = CheckSubstrateCall;
type Config = CheckSubstrateCall;
type Info = CheckSubstrateCall;
type PostInfo = CheckSubstrateCall;
fn dispatch(
self,
_origin: Self::RuntimeOrigin,
) -> sp_runtime::DispatchResultWithInfo<Self::PostInfo> {
panic!("This implementation should not be used for actual dispatch.");
}
}
impl sp_runtime::traits::SignedExtension for CheckSubstrateCall {
type AccountId = AccountId;
type Call = RuntimeCall;
type AdditionalSigned = ();
type Pre = ();
const IDENTIFIER: &'static str = "CheckSubstrateCall";
fn additional_signed(
&self,
) -> core::result::Result<Self::AdditionalSigned, TransactionValidityError> {
Ok(())
}
fn validate(
&self,
_who: &Self::AccountId,
call: &Self::Call,
_info: &DispatchInfoOf<Self::Call>,
_len: usize,
) -> TransactionValidity {
log::trace!(target: LOG_TARGET, "validate");
match call {
RuntimeCall::SubstrateTest(ref substrate_test_call) =>
substrate_test_pallet::validate_runtime_call(substrate_test_call),
_ => Ok(Default::default()),
}
}
fn pre_dispatch(
self,
who: &Self::AccountId,
call: &Self::Call,
info: &sp_runtime::traits::DispatchInfoOf<Self::Call>,
len: usize,
) -> Result<Self::Pre, TransactionValidityError> {
self.validate(who, call, info, len).map(drop)
}
}
construct_runtime!(
pub enum Runtime
{
System: frame_system,
Babe: pallet_babe,
SubstrateTest: substrate_test_pallet::pallet,
Balances: pallet_balances,
}
);
/// We assume that ~10% of the block weight is consumed by `on_initialize` handlers.
/// This is used to limit the maximal weight of a single extrinsic.
const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(10);
/// We allow `Normal` extrinsics to fill up the block up to 75%, the rest can be used
/// by Operational extrinsics.
const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
/// Max weight, actual value does not matter for test runtime.
const MAXIMUM_BLOCK_WEIGHT: Weight =
Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND.saturating_mul(2), u64::MAX);
parameter_types! {
pub const BlockHashCount: BlockNumber = 2400;
pub const Version: RuntimeVersion = VERSION;
pub RuntimeBlockLength: BlockLength =
BlockLength::max_with_normal_ratio(5 * 1024 * 1024, NORMAL_DISPATCH_RATIO);
pub RuntimeBlockWeights: BlockWeights = BlockWeights::builder()
.base_block(BlockExecutionWeight::get())
.for_class(DispatchClass::all(), |weights| {
weights.base_extrinsic = ExtrinsicBaseWeight::get();
})
.for_class(DispatchClass::Normal, |weights| {
weights.max_total = Some(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT);
})
.for_class(DispatchClass::Operational, |weights| {
weights.max_total = Some(MAXIMUM_BLOCK_WEIGHT);
// Operational transactions have some extra reserved space, so that they
// are included even if block reached `MAXIMUM_BLOCK_WEIGHT`.
weights.reserved = Some(
MAXIMUM_BLOCK_WEIGHT - NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT
);
})
.avg_block_initialization(AVERAGE_ON_INITIALIZE_RATIO)
.build_or_panic();
}
#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl frame_system::pallet::Config for Runtime {
type BlockWeights = RuntimeBlockWeights;
type Nonce = Nonce;
type AccountId = AccountId;
type Lookup = sp_runtime::traits::IdentityLookup<Self::AccountId>;
type Block = Block;
type AccountData = pallet_balances::AccountData<Balance>;
}
pub mod currency {
use crate::Balance;
const MILLICENTS: Balance = 1_000_000_000;
const CENTS: Balance = 1_000 * MILLICENTS; // assume this is worth about a cent.
pub const DOLLARS: Balance = 100 * CENTS;
}
parameter_types! {
pub const ExistentialDeposit: Balance = 1 * currency::DOLLARS;
// For weight estimation, we assume that the most locks on an individual account will be 50.
// This number may need to be adjusted in the future if this assumption no longer holds true.
pub const MaxLocks: u32 = 50;
pub const MaxReserves: u32 = 50;
}
impl pallet_balances::Config for Runtime {
type MaxLocks = MaxLocks;
type MaxReserves = MaxReserves;
type ReserveIdentifier = [u8; 8];
type Balance = Balance;
type DustRemoval = ();
type RuntimeEvent = RuntimeEvent;
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
type WeightInfo = pallet_balances::weights::SubstrateWeight<Runtime>;
type FreezeIdentifier = ();
type MaxFreezes = ();
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
}
impl substrate_test_pallet::Config for Runtime {}
// Required for `pallet_babe::Config`.
impl pallet_timestamp::Config for Runtime {
type Moment = u64;
type OnTimestampSet = Babe;
type MinimumPeriod = ConstU64<500>;
type WeightInfo = pallet_timestamp::weights::SubstrateWeight<Runtime>;
}
parameter_types! {
pub const EpochDuration: u64 = 6;
}
impl pallet_babe::Config for Runtime {
type EpochDuration = EpochDuration;
type ExpectedBlockTime = ConstU64<10_000>;
type EpochChangeTrigger = pallet_babe::SameAuthoritiesForever;
type DisabledValidators = ();
type KeyOwnerProof = sp_core::Void;
type EquivocationReportSystem = ();
type WeightInfo = ();
type MaxAuthorities = ConstU32<10>;
type MaxNominators = ConstU32<100>;
}
/// Adds one to the given input and returns the final result.
#[inline(never)]
fn benchmark_add_one(i: u64) -> u64 {
i + 1
}
fn code_using_trie() -> u64 {
let pairs = [
(b"0103000000000000000464".to_vec(), b"0400000000".to_vec()),
(b"0103000000000000000469".to_vec(), b"0401000000".to_vec()),
]
.to_vec();
let mut mdb = PrefixedMemoryDB::default();
let mut root = core::default::Default::default();
{
let mut t = TrieDBMutBuilderV1::<Hashing>::new(&mut mdb, &mut root).build();
for (key, value) in &pairs {
if t.insert(key, value).is_err() {
return 101
}
}
}
let trie = TrieDBBuilder::<Hashing>::new(&mdb, &root).build();
let res = if let Ok(iter) = trie.iter() { iter.flatten().count() as u64 } else { 102 };
res
}
impl_opaque_keys! {
pub struct SessionKeys {
pub ed25519: ed25519::AppPublic,
pub sr25519: sr25519::AppPublic,
pub ecdsa: ecdsa::AppPublic,
}
}
pub const TEST_RUNTIME_BABE_EPOCH_CONFIGURATION: BabeEpochConfiguration = BabeEpochConfiguration {
c: (3, 10),
allowed_slots: AllowedSlots::PrimaryAndSecondaryPlainSlots,
};
impl_runtime_apis! {
impl sp_api::Core<Block> for Runtime {
fn version() -> RuntimeVersion {
version()
}
fn execute_block(block: Block) {
log::trace!(target: LOG_TARGET, "execute_block: {block:#?}");
Executive::execute_block(block);
}
fn initialize_block(header: &<Block as BlockT>::Header) -> ExtrinsicInclusionMode {
log::trace!(target: LOG_TARGET, "initialize_block: {header:#?}");
Executive::initialize_block(header)
}
}
impl sp_api::Metadata<Block> for Runtime {
fn metadata() -> OpaqueMetadata {
OpaqueMetadata::new(Runtime::metadata().into())
}
fn metadata_at_version(version: u32) -> Option<OpaqueMetadata> {
Runtime::metadata_at_version(version)
}
fn metadata_versions() -> alloc::vec::Vec<u32> {
Runtime::metadata_versions()
}
}
impl sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block> for Runtime {
fn validate_transaction(
source: TransactionSource,
utx: <Block as BlockT>::Extrinsic,
block_hash: <Block as BlockT>::Hash,
) -> TransactionValidity {
let validity = Executive::validate_transaction(source, utx.clone(), block_hash);
log::trace!(target: LOG_TARGET, "validate_transaction {:?} {:?}", utx, validity);
validity
}
}
impl sp_block_builder::BlockBuilder<Block> for Runtime {
fn apply_extrinsic(extrinsic: <Block as BlockT>::Extrinsic) -> ApplyExtrinsicResult {
Executive::apply_extrinsic(extrinsic)
}
fn finalize_block() -> <Block as BlockT>::Header {
log::trace!(target: LOG_TARGET, "finalize_block");
Executive::finalize_block()
}
fn inherent_extrinsics(_data: InherentData) -> Vec<<Block as BlockT>::Extrinsic> {
vec![]
}
fn check_inherents(_block: Block, _data: InherentData) -> CheckInherentsResult {
CheckInherentsResult::new()
}
}
impl frame_system_rpc_runtime_api::AccountNonceApi<Block, AccountId, Nonce> for Runtime {
fn account_nonce(account: AccountId) -> Nonce {
System::account_nonce(account)
}
}
impl self::TestAPI<Block> for Runtime {
fn balance_of(id: AccountId) -> u64 {
Balances::free_balance(id)
}
fn benchmark_add_one(val: &u64) -> u64 {
val + 1
}
fn benchmark_vector_add_one(vec: &Vec<u64>) -> Vec<u64> {
let mut vec = vec.clone();
vec.iter_mut().for_each(|v| *v += 1);
vec
}
fn function_signature_changed() -> u64 {
1
}
fn use_trie() -> u64 {
code_using_trie()
}
fn benchmark_indirect_call() -> u64 {
let function = benchmark_add_one;
(0..1000).fold(0, |p, i| p + function(i))
}
fn benchmark_direct_call() -> u64 {
(0..1000).fold(0, |p, i| p + benchmark_add_one(i))
}
fn vec_with_capacity(size: u32) -> Vec<u8> {
Vec::with_capacity(size as usize)
}
fn get_block_number() -> u64 {
System::block_number()
}
fn test_ed25519_crypto() -> (ed25519::AppSignature, ed25519::AppPublic) {
test_ed25519_crypto()
}
fn test_sr25519_crypto() -> (sr25519::AppSignature, sr25519::AppPublic) {
test_sr25519_crypto()
}
fn test_ecdsa_crypto() -> (ecdsa::AppSignature, ecdsa::AppPublic) {
test_ecdsa_crypto()
}
fn test_storage() {
test_read_storage();
test_read_child_storage();
}
fn test_witness(proof: StorageProof, root: crate::Hash) {
test_witness(proof, root);
}
fn test_multiple_arguments(data: Vec<u8>, other: Vec<u8>, num: u32) {
assert_eq!(&data[..], &other[..]);
assert_eq!(data.len(), num as usize);
}
fn do_trace_log() {
log::trace!(target: "test", "Hey I'm runtime");
let data = "THIS IS TRACING";
tracing::trace!(target: "test", %data, "Hey, I'm tracing");
}
fn verify_ed25519(sig: ed25519::Signature, public: ed25519::Public, message: Vec<u8>) -> bool {
sp_io::crypto::ed25519_verify(&sig, &message, &public)
}
fn write_key_value(key: Vec<u8>, value: Vec<u8>, panic: bool) {
sp_io::storage::set(&key, &value);
if panic {
panic!("I'm just following my master");
}
}
}
impl sp_consensus_aura::AuraApi<Block, AuraId> for Runtime {
fn slot_duration() -> sp_consensus_aura::SlotDuration {
sp_consensus_aura::SlotDuration::from_millis(1000)
}
fn authorities() -> Vec<AuraId> {
SubstrateTest::authorities().into_iter().map(|auth| AuraId::from(auth)).collect()
}
}
impl sp_consensus_babe::BabeApi<Block> for Runtime {
fn configuration() -> sp_consensus_babe::BabeConfiguration {
let epoch_config = Babe::epoch_config().unwrap_or(TEST_RUNTIME_BABE_EPOCH_CONFIGURATION);
sp_consensus_babe::BabeConfiguration {
slot_duration: Babe::slot_duration(),
epoch_length: EpochDuration::get(),
c: epoch_config.c,
authorities: Babe::authorities().to_vec(),
randomness: Babe::randomness(),
allowed_slots: epoch_config.allowed_slots,
}
}
fn current_epoch_start() -> Slot {
Babe::current_epoch_start()
}
fn current_epoch() -> sp_consensus_babe::Epoch {
Babe::current_epoch()
}
fn next_epoch() -> sp_consensus_babe::Epoch {
Babe::next_epoch()
}
fn submit_report_equivocation_unsigned_extrinsic(
_equivocation_proof: sp_consensus_babe::EquivocationProof<
<Block as BlockT>::Header,
>,
_key_owner_proof: sp_consensus_babe::OpaqueKeyOwnershipProof,
) -> Option<()> {
None
}
fn generate_key_ownership_proof(
_slot: sp_consensus_babe::Slot,
_authority_id: sp_consensus_babe::AuthorityId,
) -> Option<sp_consensus_babe::OpaqueKeyOwnershipProof> {
None
}
}
impl sp_offchain::OffchainWorkerApi<Block> for Runtime {
fn offchain_worker(header: &<Block as BlockT>::Header) {
let ext = Extrinsic::new_unsigned(
substrate_test_pallet::pallet::Call::storage_change{
key:b"some_key".encode(),
value:Some(header.number.encode())
}.into(),
);
sp_io::offchain::submit_transaction(ext.encode()).unwrap();
}
}
impl sp_session::SessionKeys<Block> for Runtime {
fn generate_session_keys(_: Option<Vec<u8>>) -> Vec<u8> {
SessionKeys::generate(None)
}
fn decode_session_keys(
encoded: Vec<u8>,
) -> Option<Vec<(Vec<u8>, sp_core::crypto::KeyTypeId)>> {
SessionKeys::decode_into_raw_public_keys(&encoded)
}
}
impl sp_consensus_grandpa::GrandpaApi<Block> for Runtime {
fn grandpa_authorities() -> sp_consensus_grandpa::AuthorityList {
Vec::new()
}
fn current_set_id() -> sp_consensus_grandpa::SetId {
0
}
fn submit_report_equivocation_unsigned_extrinsic(
_equivocation_proof: sp_consensus_grandpa::EquivocationProof<
<Block as BlockT>::Hash,
NumberFor<Block>,
>,
_key_owner_proof: sp_consensus_grandpa::OpaqueKeyOwnershipProof,
) -> Option<()> {
None
}
fn generate_key_ownership_proof(
_set_id: sp_consensus_grandpa::SetId,
_authority_id: sp_consensus_grandpa::AuthorityId,
) -> Option<sp_consensus_grandpa::OpaqueKeyOwnershipProof> {
None
}
}
impl sp_genesis_builder::GenesisBuilder<Block> for Runtime {
fn build_state(config: Vec<u8>) -> sp_genesis_builder::Result {
build_state::<RuntimeGenesisConfig>(config)
}
fn get_preset(name: &Option<PresetId>) -> Option<Vec<u8>> {
get_preset::<RuntimeGenesisConfig>(name, |name| {
let patch = match name.try_into() {
Ok("staging") => {
let endowed_accounts: Vec<AccountId> = vec![
AccountKeyring::Bob.public().into(),
AccountKeyring::Charlie.public().into(),
];
json!({
"balances": {
"balances": endowed_accounts.into_iter().map(|k| (k, 10 * currency::DOLLARS)).collect::<Vec<_>>(),
},
"substrateTest": {
"authorities": [
AccountKeyring::Alice.public().to_ss58check(),
AccountKeyring::Ferdie.public().to_ss58check()
],
}
})
},
Ok("foobar") => json!({"foo":"bar"}),
_ => return None,
};
Some(serde_json::to_string(&patch)
.expect("serialization to json is expected to work. qed.")
.into_bytes())
})
}
fn preset_names() -> Vec<PresetId> {
vec![PresetId::from("foobar"), PresetId::from("staging")]
}
}
}
fn test_ed25519_crypto() -> (ed25519::AppSignature, ed25519::AppPublic) {
let public0 = ed25519::AppPublic::generate_pair(None);
let public1 = ed25519::AppPublic::generate_pair(None);
let public2 = ed25519::AppPublic::generate_pair(None);
let all = ed25519::AppPublic::all();
assert!(all.contains(&public0));
assert!(all.contains(&public1));
assert!(all.contains(&public2));
let signature = public0.sign(&"ed25519").expect("Generates a valid `ed25519` signature.");
assert!(public0.verify(&"ed25519", &signature));
(signature, public0)
}
fn test_sr25519_crypto() -> (sr25519::AppSignature, sr25519::AppPublic) {
let public0 = sr25519::AppPublic::generate_pair(None);
let public1 = sr25519::AppPublic::generate_pair(None);
let public2 = sr25519::AppPublic::generate_pair(None);
let all = sr25519::AppPublic::all();
assert!(all.contains(&public0));
assert!(all.contains(&public1));
assert!(all.contains(&public2));
let signature = public0.sign(&"sr25519").expect("Generates a valid `sr25519` signature.");
assert!(public0.verify(&"sr25519", &signature));
(signature, public0)
}
fn test_ecdsa_crypto() -> (ecdsa::AppSignature, ecdsa::AppPublic) {
let public0 = ecdsa::AppPublic::generate_pair(None);
let public1 = ecdsa::AppPublic::generate_pair(None);
let public2 = ecdsa::AppPublic::generate_pair(None);
let all = ecdsa::AppPublic::all();
assert!(all.contains(&public0));
assert!(all.contains(&public1));
assert!(all.contains(&public2));
let signature = public0.sign(&"ecdsa").expect("Generates a valid `ecdsa` signature.");
assert!(public0.verify(&"ecdsa", &signature));
(signature, public0)
}
fn test_read_storage() {
const KEY: &[u8] = b":read_storage";
sp_io::storage::set(KEY, b"test");
let mut v = [0u8; 4];
let r = sp_io::storage::read(KEY, &mut v, 0);
assert_eq!(r, Some(4));
assert_eq!(&v, b"test");
let mut v = [0u8; 4];
let r = sp_io::storage::read(KEY, &mut v, 4);
assert_eq!(r, Some(0));
assert_eq!(&v, &[0, 0, 0, 0]);
}
fn test_read_child_storage() {
const STORAGE_KEY: &[u8] = b"unique_id_1";
const KEY: &[u8] = b":read_child_storage";
sp_io::default_child_storage::set(STORAGE_KEY, KEY, b"test");
let mut v = [0u8; 4];
let r = sp_io::default_child_storage::read(STORAGE_KEY, KEY, &mut v, 0);
assert_eq!(r, Some(4));
assert_eq!(&v, b"test");
let mut v = [0u8; 4];
let r = sp_io::default_child_storage::read(STORAGE_KEY, KEY, &mut v, 8);
assert_eq!(r, Some(0));
assert_eq!(&v, &[0, 0, 0, 0]);
}
fn test_witness(proof: StorageProof, root: crate::Hash) {
use sp_externalities::Externalities;
let db: sp_trie::MemoryDB<crate::Hashing> = proof.into_memory_db();
let backend = sp_state_machine::TrieBackendBuilder::<_, crate::Hashing>::new(db, root).build();
let mut overlay = sp_state_machine::OverlayedChanges::default();
let mut ext = sp_state_machine::Ext::new(
&mut overlay,
&backend,
#[cfg(feature = "std")]
None,
);
assert!(ext.storage(b"value3").is_some());
assert!(ext.storage_root(Default::default()).as_slice() == &root[..]);
ext.place_storage(vec![0], Some(vec![1]));
assert!(ext.storage_root(Default::default()).as_slice() != &root[..]);
}
/// Some tests require the hashed keys of the storage. As the values of hashed keys are not trivial
/// to guess, this small module provides the values of the keys, and the code which is required to
/// generate the keys.
#[cfg(feature = "std")]
pub mod storage_key_generator {
use super::*;
use sp_core::Pair;
/// Generate hex string without prefix
pub(super) fn hex<T>(x: T) -> String
where
T: array_bytes::Hex,
{
x.hex(Default::default())
}
fn concat_hashes(input: &Vec<&[u8]>) -> String {
input.iter().map(|s| sp_crypto_hashing::twox_128(s)).map(hex).collect()
}
fn twox_64_concat(x: &[u8]) -> Vec<u8> {
sp_crypto_hashing::twox_64(x).iter().chain(x.iter()).cloned().collect()
}
/// Generate the hashed storage keys from the raw literals. These keys are expected to be in
/// storage with given substrate-test runtime.
pub fn generate_expected_storage_hashed_keys(custom_heap_pages: bool) -> Vec<String> {
let mut literals: Vec<&[u8]> = vec![b":code", b":extrinsic_index"];
if custom_heap_pages {
literals.push(b":heappages");
}
let keys: Vec<Vec<&[u8]>> = vec![
vec![b"Babe", b":__STORAGE_VERSION__:"],
vec![b"Babe", b"Authorities"],
vec![b"Babe", b"EpochConfig"],
vec![b"Babe", b"NextAuthorities"],
vec![b"Babe", b"SegmentIndex"],
vec![b"Balances", b":__STORAGE_VERSION__:"],
vec![b"Balances", b"TotalIssuance"],
vec![b"SubstrateTest", b":__STORAGE_VERSION__:"],
vec![b"SubstrateTest", b"Authorities"],
vec![b"System", b":__STORAGE_VERSION__:"],
vec![b"System", b"LastRuntimeUpgrade"],
vec![b"System", b"ParentHash"],
vec![b"System", b"UpgradedToTripleRefCount"],
vec![b"System", b"UpgradedToU32RefCount"],
];
let mut expected_keys = keys.iter().map(concat_hashes).collect::<Vec<String>>();
expected_keys.extend(literals.into_iter().map(hex));
let balances_map_keys = (0..16_usize)
.into_iter()
.map(|i| AccountKeyring::numeric(i).public().to_vec())
.chain(vec![
AccountKeyring::Alice.public().to_vec(),
AccountKeyring::Bob.public().to_vec(),
AccountKeyring::Charlie.public().to_vec(),
])
.map(|pubkey| {
sp_crypto_hashing::blake2_128(&pubkey)
.iter()
.chain(pubkey.iter())
.cloned()
.collect::<Vec<u8>>()
})
.map(|hash_pubkey| {
[concat_hashes(&vec![b"System", b"Account"]), hex(hash_pubkey)].concat()
});
expected_keys.extend(balances_map_keys);
expected_keys.push(
[
concat_hashes(&vec![b"System", b"BlockHash"]),
hex(0u64.using_encoded(twox_64_concat)),
]
.concat(),
);
expected_keys.sort();
expected_keys
}
/// Provides the commented list of hashed keys. This contains a hard-coded list of hashed keys
/// that would be generated by `generate_expected_storage_hashed_keys`. This list is provided
/// for the debugging convenience only. Value of each hex-string is documented with the literal
/// origin.
///
/// `custom_heap_pages`: Should be set to `true` when the state contains the `:heap_pages` key
/// aka when overriding the heap pages to be used by the executor.
pub fn get_expected_storage_hashed_keys(custom_heap_pages: bool) -> Vec<&'static str> {
let mut res = vec![
//SubstrateTest|:__STORAGE_VERSION__:
"00771836bebdd29870ff246d305c578c4e7b9012096b41c4eb3aaf947f6ea429",
//SubstrateTest|Authorities
"00771836bebdd29870ff246d305c578c5e0621c4869aa60c02be9adcc98a0d1d",
//Babe|:__STORAGE_VERSION__:
"1cb6f36e027abb2091cfb5110ab5087f4e7b9012096b41c4eb3aaf947f6ea429",
//Babe|Authorities
"1cb6f36e027abb2091cfb5110ab5087f5e0621c4869aa60c02be9adcc98a0d1d",
//Babe|SegmentIndex
"1cb6f36e027abb2091cfb5110ab5087f66e8f035c8adbe7f1547b43c51e6f8a4",
//Babe|NextAuthorities
"1cb6f36e027abb2091cfb5110ab5087faacf00b9b41fda7a9268821c2a2b3e4c",
//Babe|EpochConfig
"1cb6f36e027abb2091cfb5110ab5087fdc6b171b77304263c292cc3ea5ed31ef",
//System|:__STORAGE_VERSION__:
"26aa394eea5630e07c48ae0c9558cef74e7b9012096b41c4eb3aaf947f6ea429",
//System|UpgradedToU32RefCount
"26aa394eea5630e07c48ae0c9558cef75684a022a34dd8bfa2baaf44f172b710",
//System|ParentHash
"26aa394eea5630e07c48ae0c9558cef78a42f33323cb5ced3b44dd825fda9fcc",
//System::BlockHash|0
"26aa394eea5630e07c48ae0c9558cef7a44704b568d21667356a5a050c118746bb1bdbcacd6ac9340000000000000000",
//System|UpgradedToTripleRefCount
"26aa394eea5630e07c48ae0c9558cef7a7fd6c28836b9a28522dc924110cf439",
// System|Account|blake2_128Concat("//11")
"26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da901cae4e3edfbb32c91ed3f01ab964f4eeeab50338d8e5176d3141802d7b010a55dadcd5f23cf8aaafa724627e967e90e",
// System|Account|blake2_128Concat("//4")
"26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da91b614bd4a126f2d5d294e9a8af9da25248d7e931307afb4b68d8d565d4c66e00d856c6d65f5fed6bb82dcfb60e936c67",
// System|Account|blake2_128Concat("//7")
"26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da94b21aff9fe1e8b2fc4b0775b8cbeff28ba8e2c7594dd74730f3ca835e95455d199261897edc9735d602ea29615e2b10b",
// System|Account|blake2_128Concat("//Bob")
"26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da94f9aea1afa791265fae359272badc1cf8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48",
// System|Account|blake2_128Concat("//3")
"26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da95786a2916fcb81e1bd5dcd81e0d2452884617f575372edb5a36d85c04cdf2e4699f96fe33eb5f94a28c041b88e398d0c",
// System|Account|blake2_128Concat("//14")
"26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da95b8542d9672c7b7e779cc7c1e6b605691c2115d06120ea2bee32dd601d02f36367564e7ddf84ae2717ca3f097459652e",
// System|Account|blake2_128Concat("//6")
"26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da996c30bdbfab640838e6b6d3c33ab4adb4211b79e34ee8072eab506edd4b93a7b85a14c9a05e5cdd056d98e7dbca87730",
// System|Account|blake2_128Concat("//9")
"26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da99dc65b1339ec388fbf2ca0cdef51253512c6cfd663203ea16968594f24690338befd906856c4d2f4ef32dad578dba20c",
// System|Account|blake2_128Concat("//8")
"26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da99e6eb5abd62f5fd54793da91a47e6af6125d57171ff9241f07acaa1bb6a6103517965cf2cd00e643b27e7599ebccba70",
// System|Account|blake2_128Concat("//Charlie")
"26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9b0edae20838083f2cde1c4080db8cf8090b5ab205c6974c9ea841be688864633dc9ca8a357843eeacf2314649965fe22",
// System|Account|blake2_128Concat("//10")
"26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9d0052993b6f3bd0544fd1f5e4125b9fbde3e789ecd53431fe5c06c12b72137153496dace35c695b5f4d7b41f7ed5763b",
// System|Account|blake2_128Concat("//1")
"26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9d6b7e9a5f12bc571053265dade10d3b4b606fc73f57f03cdb4c932d475ab426043e429cecc2ffff0d2672b0df8398c48",
// System|Account|blake2_128Concat("//Alice")