Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
79c54bc
substrate-test-runtime migrated to pure-frame based
michalkucharczyk Mar 28, 2023
bde190c
test block builder: helpers added
michalkucharczyk Mar 28, 2023
bca82c1
simple renaming
michalkucharczyk Mar 28, 2023
b02b857
basic_authorship test adjusted
michalkucharczyk Mar 28, 2023
6ad4794
block_building storage_proof test adjusted
michalkucharczyk Mar 28, 2023
fb9ccd9
babe: tests: should_panic expected added
michalkucharczyk Mar 28, 2023
46b0c0d
babe: tests adjusted
michalkucharczyk Mar 28, 2023
1f092be
beefy: tests adjusted
michalkucharczyk Mar 28, 2023
8779211
grandpa: tests adjusted
michalkucharczyk Mar 28, 2023
4ea7a7d
network:bitswap: test adjusted
michalkucharczyk Mar 28, 2023
b64afdb
runtime apis versions adjusted
michalkucharczyk Mar 28, 2023
8772c8a
storage keys used in runtime adjusted
michalkucharczyk Mar 28, 2023
95eac77
wasm vs native tests removed
michalkucharczyk Mar 28, 2023
7d0c0eb
rpc tests: adjusted
michalkucharczyk Mar 28, 2023
cacd226
tests: sizes adjusted
michalkucharczyk Mar 28, 2023
bd43792
cargo.lock update
michalkucharczyk Mar 28, 2023
6fa4ac2
warnings fixed
michalkucharczyk Mar 28, 2023
2125300
builders cleanup: includes / std
michalkucharczyk Mar 28, 2023
3603ab9
extrinsic validation cleanup
michalkucharczyk Mar 29, 2023
76e4792
Merge remote-tracking branch 'origin/master' into mku-refactor-substr…
Mar 29, 2023
89000e3
txpool: benches performance fixed
michalkucharczyk Mar 29, 2023
4513a70
fmt
michalkucharczyk Mar 29, 2023
53d37b0
spelling
michalkucharczyk Mar 30, 2023
c43a9dc
Apply suggestions from code review
michalkucharczyk Mar 30, 2023
2b536fc
Apply code review suggestions
michalkucharczyk Mar 30, 2023
14ac282
Apply code review suggestions
michalkucharczyk Mar 30, 2023
4990127
get rid of 1063 const
michalkucharczyk Mar 30, 2023
906f646
renaming: UncheckedExtrinsic -> Extrinsic
michalkucharczyk Mar 31, 2023
63cdbb2
test-utils-runtime: further step to pure-frame
michalkucharczyk Apr 4, 2023
b94002b
basic-authorship: tests OK
michalkucharczyk Apr 4, 2023
c17fac1
CheckSubstrateCall added + tests fixes
michalkucharczyk Apr 5, 2023
e8e73c3
test::Transfer call removed
michalkucharczyk Apr 12, 2023
18abfaf
priority / propagate / no sudo+root-testing
michalkucharczyk Apr 12, 2023
b7f9423
fixing warnings + format
michalkucharczyk Apr 12, 2023
f5fd0d9
cleanup: build2/nonce + format
michalkucharczyk Apr 13, 2023
05701e4
final tests fixes
michalkucharczyk Apr 13, 2023
4a05623
logs/comments removal
michalkucharczyk Apr 13, 2023
44c073d
should_not_accept_old_signatures test removed
michalkucharczyk Apr 13, 2023
cf1a8b5
make txpool benches work again
michalkucharczyk Apr 13, 2023
b25d035
Cargo.lock reset
michalkucharczyk Apr 13, 2023
7061723
format
michalkucharczyk Apr 13, 2023
d9b8cd7
sudo hack removed
michalkucharczyk Apr 13, 2023
2200471
txpool benches fix+cleanup
michalkucharczyk Apr 14, 2023
1ec59d2
.gitignore reverted
michalkucharczyk Apr 14, 2023
25fb0cf
Merge remote-tracking branch 'origin/master' into mku-refactor-substr…
michalkucharczyk Apr 14, 2023
e1549f7
rebase fixing + unsigned cleanup
michalkucharczyk Apr 14, 2023
1042f26
Cargo.toml/Cargo.lock cleanup
michalkucharczyk Apr 14, 2023
18c0d16
force-debug feature removed
michalkucharczyk Apr 14, 2023
abf87b5
mmr tests fixed
michalkucharczyk Apr 14, 2023
04784ad
make cargo-clippy happy
michalkucharczyk Apr 14, 2023
98a736e
network sync test uses unsigned extrinsic
michalkucharczyk Apr 14, 2023
ecd4f9c
cleanup
michalkucharczyk Apr 14, 2023
6efc711
".git/.scripts/commands/fmt/fmt.sh"
Apr 14, 2023
c5a8bd8
push_storage_change signed call remove
michalkucharczyk Apr 17, 2023
2b47289
GenesisConfig cleanup
michalkucharczyk Apr 17, 2023
63866f0
fix
michalkucharczyk Apr 17, 2023
31f3d80
fix
michalkucharczyk Apr 17, 2023
88683c8
GenesisConfig simplified
michalkucharczyk Apr 18, 2023
c956f06
storage_keys_works: reworked
michalkucharczyk Apr 18, 2023
f6aefcd
Merge remote-tracking branch 'origin/master' into mku-refactor-substr…
michalkucharczyk Apr 18, 2023
bc2b6be
storage_keys_works: expected keys in vec
michalkucharczyk Apr 19, 2023
a115071
storage keys list moved to substrate-test-runtime
michalkucharczyk Apr 19, 2023
b71a5b3
substrate-test: some sanity tests + GenesisConfigBuilder rework
michalkucharczyk Apr 19, 2023
8004fe9
Merge remote-tracking branch 'origin/master' into mku-refactor-substr…
Apr 25, 2023
ae1997a
Apply suggestions from code review
michalkucharczyk Apr 26, 2023
259f871
Apply suggestions from code review
michalkucharczyk Apr 26, 2023
22069a0
Review suggestions
michalkucharczyk Apr 26, 2023
042f2e4
fix
michalkucharczyk Apr 26, 2023
6b64277
fix
michalkucharczyk Apr 26, 2023
6443a9d
beefy: generate_blocks_and_sync block_num sync with actaul value
michalkucharczyk Apr 26, 2023
734ddfd
Merge remote-tracking branch 'origin/master' into mku-refactor-substr…
May 2, 2023
65e03c8
Apply suggestions from code review
michalkucharczyk May 3, 2023
123b023
Update test-utils/runtime/src/genesismap.rs
michalkucharczyk May 3, 2023
5c58b38
cargo update -p sc-rpc -p sc-transaction-pool
michalkucharczyk May 3, 2023
f31082b
Review suggestions
michalkucharczyk May 3, 2023
ab177b0
fix
michalkucharczyk May 3, 2023
c5970e8
doc added
michalkucharczyk May 3, 2023
1a7325e
slot_duration adjusted for Babe::slot_duration
michalkucharczyk May 3, 2023
ff0a322
Merge remote-tracking branch 'origin/master' into mku-refactor-substr…
May 3, 2023
9656ae9
small doc fixes
michalkucharczyk May 3, 2023
c83d009
Merge remote-tracking branch 'origin/master' into mku-refactor-substr…
May 3, 2023
3c4c413
array_bytes::hex used instead of hex
michalkucharczyk May 3, 2023
2bff606
tiny -> medium name fix
michalkucharczyk May 4, 2023
194b1df
Apply suggestions from code review
michalkucharczyk May 4, 2023
9597261
TransferData::try_from_unchecked_extrinsic -> try_from
michalkucharczyk May 4, 2023
ddf3ce1
Merge remote-tracking branch 'origin/master' into mku-refactor-substr…
michalkucharczyk May 4, 2023
7a01bb6
Update Cargo.lock
michalkucharczyk May 4, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
substrate-test-runtime migrated to pure-frame based
  • Loading branch information
michalkucharczyk committed Mar 28, 2023
commit 79c54bcc79c5df4eb81ccd108738c2a280e7a2c7
7 changes: 6 additions & 1 deletion test-utils/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ sp-session = { version = "4.0.0-dev", default-features = false, path = "../../pr
sp-api = { version = "4.0.0-dev", default-features = false, path = "../../primitives/api" }
sp-runtime = { version = "7.0.0", default-features = false, path = "../../primitives/runtime" }
pallet-babe = { version = "4.0.0-dev", default-features = false, path = "../../frame/babe" }
frame-executive = { version = "4.0.0-dev", default-features = false, path = "../../frame/executive" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../../frame/system" }
frame-system-rpc-runtime-api = { version = "4.0.0-dev", default-features = false, path = "../../frame/system/rpc/runtime-api" }
pallet-timestamp = { version = "4.0.0-dev", default-features = false, path = "../../frame/timestamp" }
Expand All @@ -45,6 +46,7 @@ trie-db = { version = "0.27.0", default-features = false }
sc-service = { version = "0.10.0-dev", default-features = false, optional = true, features = ["test-helpers"], path = "../../client/service" }
sp-state-machine = { version = "0.13.0", default-features = false, path = "../../primitives/state-machine" }
sp-externalities = { version = "0.13.0", default-features = false, path = "../../primitives/externalities" }
sp-debug-derive = { path = "../../primitives/debug-derive" }

# 3rd party
cfg-if = "1.0"
Expand All @@ -63,7 +65,7 @@ substrate-wasm-builder = { version = "5.0.0-dev", path = "../../utils/wasm-build

[features]
default = [
"std",
"std", "force-debug",
]
std = [
"pallet-beefy-mmr/std",
Expand Down Expand Up @@ -105,3 +107,6 @@ std = [
]
# Special feature to disable logging
disable-logging = [ "sp-api/disable-logging" ]
force-debug = [
"sp-debug-derive/force-debug"
]
228 changes: 228 additions & 0 deletions test-utils/runtime/src/extrinsic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
// 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.

#[cfg(feature = "std")]
use crate::sr25519::Pair;
use crate::{
substrate_test_pallet, substrate_test_pallet::pallet::Call as PalletCall, AccountId,
AuthorityId, RuntimeCall, Signature, SignedExtra, SignedPayload, UncheckedExtrinsic,
};
use codec::{Decode, Encode};
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use sp_core::crypto::Pair as TraitPair;
use sp_core::RuntimeDebug;
use sp_runtime::transaction_validity::{InvalidTransaction, TransactionValidityError};
use sp_std::prelude::*;

/// Transfer used in test substrate pallet
#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct Transfer {
pub from: AccountId,
pub to: AccountId,
pub amount: u64,
pub nonce: u64,
}

impl Transfer {
/// Convert into a signed unchecked extrinsic.
#[cfg(feature = "std")]
pub fn into_unchecked_extrinsic(self) -> UncheckedExtrinsic {
UncheckedExtrinsicBuilder::new_transfer(self).build()
}

/// Convert into a signed extrinsic, which will only end up included in the block
/// if it's the first transaction. Otherwise it will cause `ResourceExhaustion` error
/// which should be considered as block being full.
#[cfg(feature = "std")]
pub fn into_resources_exhausting_unchecked_extrinsic(self) -> UncheckedExtrinsic {
UncheckedExtrinsicBuilder::new(TransferCallBuilder::new(self).exhaust_resources().build())
.build()
}

/// If feasible extract `Transfer` from given `UncheckedExtrinsic`
pub fn try_from_unchecked_extrinsic(uxt: &UncheckedExtrinsic) -> Option<Self> {
if let RuntimeCall::SubstrateTest(ref test_pallet_call) = uxt.function {
if let PalletCall::transfer { transfer, .. } = test_pallet_call {
return Some(transfer.clone())
}
return None
}
None
}

/// Verify signature and extracts `Transfer` from given `UncheckedExtrinsic`, otherwise returns
/// error
pub fn try_from_unchecked_extrinsic_and_verify(
uxt: &UncheckedExtrinsic,
) -> Result<Self, TransactionValidityError> {
if let RuntimeCall::SubstrateTest(PalletCall::transfer {
ref transfer,
ref signature,
..
}) = uxt.function
{
if sp_runtime::verify_encoded_lazy(signature, transfer, &transfer.from) {
Ok(transfer.clone())
} else {
Err(InvalidTransaction::BadProof.into())
}
} else {
Err(InvalidTransaction::Call.into())
}
}
}

/// Generate `PalletCall::transfer_call`
pub struct TransferCallBuilder {
transfer: Transfer,
signature: Option<Signature>,
exhaust_resources: bool,
}

impl TransferCallBuilder {
/// Create `Self` with given `transfer` value
pub fn new(transfer: Transfer) -> Self {
TransferCallBuilder { transfer, signature: None, exhaust_resources: false }
}

/// Sign `transfer` with `signer` and embeds signature into `PalletCall::transfer_call`
#[cfg(feature = "std")]
pub fn signer(mut self, signer: Pair) -> Self {
self.signature = Some(signer.sign(&self.transfer.encode()));
self
}

/// Embed given signature into `PalletCall::transfer_call`
pub fn with_signature(mut self, signature: Signature) -> Self {
self.signature = Some(signature);
self
}

/// Set `exhaust_resources` flag of `PalletCall::transfer_call` to true
pub fn exhaust_resources(mut self) -> Self {
self.exhaust_resources = true;
self
}

#[cfg(feature = "std")]
/// Generate instance of `PalletCall::transfer_call`
pub fn build<T: substrate_test_pallet::Config>(self) -> PalletCall<T> {
let signature = match self.signature {
Some(signature) => signature,
None => sp_keyring::AccountKeyring::from_public(&self.transfer.from)
.expect("Creates keyring from public key.")
.sign(&self.transfer.encode()),
};
PalletCall::transfer {
transfer: self.transfer,
signature,
exhaust_resources_when_not_first: self.exhaust_resources,
}
}

#[cfg(not(feature = "std"))]
/// Dummy implementation for `no_std`.
pub fn build<T: substrate_test_pallet::Config>(self) -> PalletCall<T> {
unimplemented!()
}
}

/// Generates `UncheckedExtrinsic`
pub struct UncheckedExtrinsicBuilder {
function: RuntimeCall,
is_unsigned: bool,
}

impl UncheckedExtrinsicBuilder {
/// Create builder for given `RuntimeCall`
pub fn new(function: impl Into<RuntimeCall>) -> Self {
Self { function: function.into(), is_unsigned: false }
}

/// Create builder for given `Transfer`
pub fn new_transfer(transfer: Transfer) -> Self {
Self::new(TransferCallBuilder::new(transfer).build())
}

/// Create builder for `PalletCall::authorities_change` call using given parameters
pub fn new_authorities_change(new_authorities: Vec<AuthorityId>) -> Self {
Self::new(PalletCall::authorities_change { new_authorities })
}

/// Create builder for `PalletCall::include_data` call using given parameters
pub fn new_include_data(data: Vec<u8>) -> Self {
Self::new(PalletCall::include_data { data })
}

/// Create builder for `PalletCall::storage_change` call using given parameters
pub fn new_storage_change(key: Vec<u8>, value: Option<Vec<u8>>) -> Self {
Self::new(PalletCall::storage_change { key, value })
}

/// Create builder for `PalletCall::storage_change_unsigned` call using given parameters. Will
/// create unsigned UncheckedExtrinsic.
pub fn new_storage_change_unsigned(key: Vec<u8>, value: Option<Vec<u8>>) -> Self {
Self::new(PalletCall::storage_change_unsigned { key, value }).unsigned()
}

/// Create builder for `PalletCall::offchain_index_set` call using given parameters
pub fn new_offchain_index_set(key: Vec<u8>, value: Vec<u8>) -> Self {
Self::new(PalletCall::offchain_index_set { key, value })
}

/// Create builder for `PalletCall::offchain_index_clear` call using given parameters
pub fn new_offchain_index_clear(key: Vec<u8>) -> Self {
Self::new(PalletCall::offchain_index_clear { key })
}

/// Create builder for `PalletCall::new_store` call using given parameters
pub fn new_store(data: Vec<u8>) -> Self {
Self::new(PalletCall::store { data })
}

/// Create builder for `PalletCall::new_deposit_log_digest_item` call using given `log`
pub fn new_deposit_log_digest_item(log: sp_runtime::generic::DigestItem) -> Self {
Self::new(PalletCall::deposit_log_digest_item { log })
}

/// Unsigned `UncheckedExtrinsic` will be created
pub fn unsigned(mut self) -> Self {
self.is_unsigned = true;
self
}

#[cfg(not(feature = "std"))]
pub fn build(self) -> UncheckedExtrinsic {
unimplemented!()
}

/// Build `UncheckedExtrinsic` using embedded parameters
#[cfg(feature = "std")]
pub fn build(self) -> UncheckedExtrinsic {
if self.is_unsigned {
UncheckedExtrinsic::new_unsigned(self.function)
} else {
let sender = sp_keyring::AccountKeyring::Alice;
let extra = SignedExtra {};
let raw_payload = SignedPayload::from_raw(self.function.clone(), extra, ());
let signature = raw_payload.using_encoded(|e| sender.sign(e));

UncheckedExtrinsic::new_signed(self.function, sender.public(), signature, extra)
}
}
}
16 changes: 13 additions & 3 deletions test-utils/runtime/src/genesismap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

//! Tool for creating the genesis block.

use super::{system, wasm_binary_unwrap, AccountId, AuthorityId, Runtime};
use super::{substrate_test_pallet, wasm_binary_unwrap, AccountId, AuthorityId, Runtime};
use codec::{Encode, Joiner, KeyedVec};
use frame_support::traits::GenesisBuild;
use sc_service::construct_genesis_block;
Expand Down Expand Up @@ -81,12 +81,22 @@ impl GenesisConfig {
// Assimilate the system genesis config.
let mut storage =
Storage { top: map, children_default: self.extra_storage.children_default.clone() };
<system::GenesisConfig as GenesisBuild<Runtime>>::assimilate_storage(
&system::GenesisConfig { authorities: self.authorities.clone() },

<substrate_test_pallet::GenesisConfig as GenesisBuild<Runtime>>::assimilate_storage(
&substrate_test_pallet::GenesisConfig { authorities: self.authorities.clone() },
&mut storage,
)
.expect("Adding `system::GensisConfig` to the genesis");

<pallet_babe::GenesisConfig as GenesisBuild<Runtime>>::assimilate_storage(
&pallet_babe::GenesisConfig {
authorities: self.authorities.clone().into_iter().map(|x| (x, 1)).collect(),
epoch_config: Some(crate::TEST_RUNTIME_BABE_EPOCH_CONFIGURATION),
},
&mut storage,
)
.expect("Adding `pallet_babe::GensisConfig` to the genesis");

storage
}
}
Expand Down
Loading