use super::*;
use subsoil::core::H256;
use subsoil::runtime::{
generic::{DigestItem, Era},
testing::{Block, Digest, Header},
traits::{Block as BlockT, Header as HeaderT, TransactionExtension},
transaction_validity::{
InvalidTransaction, TransactionValidityError, UnknownTransaction, ValidTransaction,
},
BuildStorage, DispatchError,
};
use plant_balances::Call as BalancesCall;
use topsoil_core::{
assert_err, assert_ok, derive_impl,
migrations::MultiStepMigrator,
pallet_prelude::*,
parameter_types,
traits::{fungible, ConstU8, Currency, IsInherent, VariantCount, VariantCountOf},
weights::{ConstantMultiplier, IdentityFee, RuntimeDbWeight, Weight, WeightMeter, WeightToFee},
};
use topsoil_core::system::{pallet_prelude::*, ChainContext, LastRuntimeUpgrade, LastRuntimeUpgradeInfo};
use plant_transaction_payment::FungibleAdapter;
const TEST_KEY: &[u8] = b":test:key:";
const TEST_KEY_2: &[u8] = b":test:key_2:";
#[topsoil_core::pallet(dev_mode)]
mod custom {
use super::*;
#[pallet::pallet]
pub struct Pallet<T>(_);
#[pallet::config]
pub trait Config: topsoil_core::system::Config {}
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(_: BlockNumberFor<T>) -> Weight {
Weight::from_parts(175, 0)
}
fn on_idle(_: BlockNumberFor<T>, _: Weight) -> Weight {
Weight::from_parts(175, 0)
}
fn on_poll(_: BlockNumberFor<T>, _: &mut WeightMeter) {}
fn on_finalize(_: BlockNumberFor<T>) {}
fn on_runtime_upgrade() -> Weight {
subsoil::io::storage::set(super::TEST_KEY, "module".as_bytes());
Weight::from_parts(200, 0)
}
fn offchain_worker(n: BlockNumberFor<T>) {
assert_eq!(BlockNumberFor::<T>::from(1u32), n);
}
}
#[pallet::call]
impl<T: Config> Pallet<T> {
pub fn some_function(origin: OriginFor<T>) -> DispatchResult {
topsoil_core::system::ensure_signed(origin)?;
Ok(())
}
#[pallet::weight((200, DispatchClass::Operational))]
pub fn some_root_operation(origin: OriginFor<T>) -> DispatchResult {
topsoil_core::system::ensure_root(origin)?;
Ok(())
}
pub fn some_unsigned_message(origin: OriginFor<T>) -> DispatchResult {
topsoil_core::system::ensure_none(origin)?;
Ok(())
}
pub fn allowed_unsigned(origin: OriginFor<T>) -> DispatchResult {
topsoil_core::system::ensure_root(origin)?;
Ok(())
}
pub fn unallowed_unsigned(origin: OriginFor<T>) -> DispatchResult {
topsoil_core::system::ensure_root(origin)?;
Ok(())
}
#[pallet::weight((0, DispatchClass::Mandatory))]
pub fn inherent(origin: OriginFor<T>) -> DispatchResult {
topsoil_core::system::ensure_none(origin)?;
Ok(())
}
pub fn calculate_storage_root(_origin: OriginFor<T>) -> DispatchResult {
let root = subsoil::io::storage::root(subsoil::runtime::StateVersion::V1);
subsoil::io::storage::set("storage_root".as_bytes(), &root);
Ok(())
}
}
#[pallet::inherent]
impl<T: Config> ProvideInherent for Pallet<T> {
type Call = Call<T>;
type Error = subsoil::inherents::MakeFatalError<()>;
const INHERENT_IDENTIFIER: [u8; 8] = *b"test1234";
fn create_inherent(_data: &InherentData) -> Option<Self::Call> {
None
}
fn is_inherent(call: &Self::Call) -> bool {
*call == Call::<T>::inherent {}
}
}
#[pallet::validate_unsigned]
impl<T: Config> ValidateUnsigned for Pallet<T> {
type Call = Call<T>;
fn pre_dispatch(call: &Self::Call) -> Result<(), TransactionValidityError> {
match call {
Call::allowed_unsigned { .. } => Ok(()),
Call::inherent { .. } => Ok(()),
_ => Err(UnknownTransaction::NoUnsignedValidator.into()),
}
}
fn validate_unsigned(_source: TransactionSource, call: &Self::Call) -> TransactionValidity {
match call {
Call::allowed_unsigned { .. } => Ok(Default::default()),
_ => UnknownTransaction::NoUnsignedValidator.into(),
}
}
}
}
#[topsoil_core::pallet(dev_mode)]
mod custom2 {
use super::*;
#[pallet::pallet]
pub struct Pallet<T>(_);
#[pallet::config]
pub trait Config: topsoil_core::system::Config {}
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(_: BlockNumberFor<T>) -> Weight {
assert!(
!MockedSystemCallbacks::pre_inherent_called(),
"Pre inherent hook goes after on_initialize"
);
Weight::from_parts(0, 0)
}
fn on_idle(_: BlockNumberFor<T>, _: Weight) -> Weight {
assert!(
MockedSystemCallbacks::post_transactions_called(),
"Post transactions hook goes before after on_idle"
);
Weight::from_parts(0, 0)
}
fn on_finalize(_: BlockNumberFor<T>) {
assert!(
MockedSystemCallbacks::post_transactions_called(),
"Post transactions hook goes before after on_finalize"
);
}
fn on_runtime_upgrade() -> Weight {
subsoil::io::storage::set(super::TEST_KEY, "module".as_bytes());
Weight::from_parts(0, 0)
}
}
#[pallet::call]
impl<T: Config> Pallet<T> {
pub fn allowed_unsigned(origin: OriginFor<T>) -> DispatchResult {
topsoil_core::system::ensure_root(origin)?;
Ok(())
}
pub fn some_call(_: OriginFor<T>) -> DispatchResult {
assert!(MockedSystemCallbacks::post_inherent_called());
assert!(!MockedSystemCallbacks::post_transactions_called());
assert!(System::inherents_applied());
Ok(())
}
#[pallet::weight({0})]
pub fn optional_inherent(origin: OriginFor<T>) -> DispatchResult {
topsoil_core::system::ensure_none(origin)?;
assert!(MockedSystemCallbacks::pre_inherent_called());
assert!(!MockedSystemCallbacks::post_inherent_called(), "Should not already be called");
assert!(!System::inherents_applied());
Ok(())
}
#[pallet::weight((0, DispatchClass::Mandatory))]
pub fn inherent(origin: OriginFor<T>) -> DispatchResult {
topsoil_core::system::ensure_none(origin)?;
assert!(MockedSystemCallbacks::pre_inherent_called());
assert!(!MockedSystemCallbacks::post_inherent_called(), "Should not already be called");
assert!(!System::inherents_applied());
Ok(())
}
}
#[pallet::inherent]
impl<T: Config> ProvideInherent for Pallet<T> {
type Call = Call<T>;
type Error = subsoil::inherents::MakeFatalError<()>;
const INHERENT_IDENTIFIER: [u8; 8] = *b"test1235";
fn create_inherent(_data: &InherentData) -> Option<Self::Call> {
None
}
fn is_inherent(call: &Self::Call) -> bool {
matches!(call, Call::<T>::inherent {} | Call::<T>::optional_inherent {})
}
}
#[pallet::validate_unsigned]
impl<T: Config> ValidateUnsigned for Pallet<T> {
type Call = Call<T>;
fn pre_dispatch(call: &Self::Call) -> Result<(), TransactionValidityError> {
match call {
Call::allowed_unsigned { .. }
| Call::optional_inherent { .. }
| Call::inherent { .. } => Ok(()),
_ => Err(UnknownTransaction::NoUnsignedValidator.into()),
}
}
fn validate_unsigned(_source: TransactionSource, call: &Self::Call) -> TransactionValidity {
match call {
Call::allowed_unsigned { .. } => Ok(Default::default()),
_ => UnknownTransaction::NoUnsignedValidator.into(),
}
}
}
}
topsoil_core::construct_runtime!(
pub struct Runtime
{
System: topsoil_core::system::{Pallet, Call, Config<T>, Storage, Event<T>},
Balances: plant_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
TransactionPayment: plant_transaction_payment::{Pallet, Storage, Event<T>},
Custom: custom::{Pallet, Call, ValidateUnsigned, Inherent},
Custom2: custom2::{Pallet, Call, ValidateUnsigned, Inherent},
}
);
parameter_types! {
pub BlockWeights: topsoil_core::system::limits::BlockWeights =
topsoil_core::system::limits::BlockWeights::builder()
.base_block(Weight::from_parts(10, 0))
.for_class(DispatchClass::all(), |weights| weights.base_extrinsic = Weight::from_parts(5, 0))
.for_class(DispatchClass::non_mandatory(), |weights| weights.max_total = Weight::from_parts(1024, u64::MAX).into())
.build_or_panic();
pub const DbWeight: RuntimeDbWeight = RuntimeDbWeight {
read: 10,
write: 100,
};
}
pub struct MockExtensionsWeights;
impl topsoil_core::system::ExtensionsWeightInfo for MockExtensionsWeights {
fn check_genesis() -> Weight {
Weight::zero()
}
fn check_mortality_mortal_transaction() -> Weight {
Weight::from_parts(10, 0)
}
fn check_mortality_immortal_transaction() -> Weight {
Weight::from_parts(10, 0)
}
fn check_non_zero_sender() -> Weight {
Weight::zero()
}
fn check_nonce() -> Weight {
Weight::from_parts(10, 0)
}
fn check_spec_version() -> Weight {
Weight::zero()
}
fn check_tx_version() -> Weight {
Weight::zero()
}
fn check_weight() -> Weight {
Weight::from_parts(10, 0)
}
fn weight_reclaim() -> Weight {
Weight::zero()
}
}
#[derive_impl(topsoil_core::system::config_preludes::TestDefaultConfig)]
impl topsoil_core::system::Config for Runtime {
type BlockWeights = BlockWeights;
type RuntimeOrigin = RuntimeOrigin;
type Nonce = u64;
type RuntimeCall = RuntimeCall;
type Block = TestBlock;
type RuntimeEvent = RuntimeEvent;
type Version = RuntimeVersion;
type AccountData = plant_balances::AccountData<Balance>;
type PreInherents = MockedSystemCallbacks;
type PostInherents = MockedSystemCallbacks;
type PostTransactions = MockedSystemCallbacks;
type MultiBlockMigrator = MockedModeGetter;
type ExtensionsWeightInfo = MockExtensionsWeights;
type SingleBlockMigrations = CustomOnRuntimeUpgrade;
}
#[derive(
Encode,
Decode,
DecodeWithMemTracking,
Copy,
Clone,
Eq,
PartialEq,
MaxEncodedLen,
TypeInfo,
Debug,
)]
pub enum FreezeReasonId {
Foo,
}
impl VariantCount for FreezeReasonId {
const VARIANT_COUNT: u32 = 1;
}
type Balance = u64;
pub struct BalancesWeights;
impl plant_balances::WeightInfo for BalancesWeights {
fn transfer_allow_death() -> Weight {
Weight::from_parts(25, 0)
}
fn transfer_keep_alive() -> Weight {
Weight::zero()
}
fn force_set_balance_creating() -> Weight {
Weight::zero()
}
fn force_set_balance_killing() -> Weight {
Weight::zero()
}
fn force_transfer() -> Weight {
Weight::zero()
}
fn transfer_all() -> Weight {
Weight::zero()
}
fn force_unreserve() -> Weight {
Weight::zero()
}
fn upgrade_accounts(_u: u32) -> Weight {
Weight::zero()
}
fn force_adjust_total_issuance() -> Weight {
Weight::zero()
}
fn burn_allow_death() -> Weight {
Weight::zero()
}
fn burn_keep_alive() -> Weight {
Weight::zero()
}
}
#[derive_impl(plant_balances::config_preludes::TestDefaultConfig)]
impl plant_balances::Config for Runtime {
type Balance = Balance;
type AccountStore = System;
type WeightInfo = BalancesWeights;
type RuntimeFreezeReason = FreezeReasonId;
type FreezeIdentifier = FreezeReasonId;
type MaxFreezes = VariantCountOf<FreezeReasonId>;
}
pub struct MockTxPaymentWeights;
impl plant_transaction_payment::WeightInfo for MockTxPaymentWeights {
fn charge_transaction_payment() -> Weight {
Weight::from_parts(10, 0)
}
}
parameter_types! {
pub const TransactionByteFee: Balance = 0;
}
impl plant_transaction_payment::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type OnChargeTransaction = FungibleAdapter<Balances, ()>;
type OperationalFeeMultiplier = ConstU8<5>;
type WeightToFee = IdentityFee<Balance>;
type LengthToFee = ConstantMultiplier<Balance, TransactionByteFee>;
type FeeMultiplierUpdate = ();
type WeightInfo = MockTxPaymentWeights;
}
impl custom::Config for Runtime {}
impl custom2::Config for Runtime {}
pub struct RuntimeVersion;
impl topsoil_core::traits::Get<subsoil::version::RuntimeVersion> for RuntimeVersion {
fn get() -> subsoil::version::RuntimeVersion {
RuntimeVersionTestValues::get().clone()
}
}
parameter_types! {
pub static RuntimeVersionTestValues: subsoil::version::RuntimeVersion =
Default::default();
}
type TxExtension = (
topsoil_core::system::AuthorizeCall<Runtime>,
topsoil_core::system::CheckEra<Runtime>,
topsoil_core::system::CheckNonce<Runtime>,
topsoil_core::system::CheckWeight<Runtime>,
plant_transaction_payment::ChargeTransactionPayment<Runtime>,
topsoil_core::system::WeightReclaim<Runtime>,
);
type UncheckedXt = subsoil::runtime::generic::UncheckedExtrinsic<
u64,
RuntimeCall,
subsoil::runtime::testing::UintAuthorityId,
TxExtension,
>;
type TestBlock = Block<UncheckedXt>;
const CUSTOM_ON_RUNTIME_KEY: &[u8] = b":custom:on_runtime";
pub struct CustomOnRuntimeUpgrade;
impl OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
fn on_runtime_upgrade() -> Weight {
subsoil::io::storage::set(TEST_KEY, "custom_upgrade".as_bytes());
subsoil::io::storage::set(TEST_KEY_2, "try_runtime_upgrade_works".as_bytes());
subsoil::io::storage::set(CUSTOM_ON_RUNTIME_KEY, &true.encode());
System::deposit_event(topsoil_core::system::Event::CodeUpdated);
assert_eq!(0, System::last_runtime_upgrade_spec_version());
Weight::from_parts(100, 0)
}
#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), TryRuntimeError> {
assert_eq!(
&subsoil::io::storage::get(TEST_KEY_2).unwrap()[..],
*b"try_runtime_upgrade_works"
);
Ok(())
}
}
#[allow(deprecated)]
type Executive = super::Executive<
Runtime,
Block<UncheckedXt>,
ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
CustomOnRuntimeUpgrade,
>;
parameter_types! {
pub static SystemCallbacksCalled: u32 = 0;
}
pub struct MockedSystemCallbacks;
impl PreInherents for MockedSystemCallbacks {
fn pre_inherents() {
assert_eq!(SystemCallbacksCalled::get(), 0);
SystemCallbacksCalled::set(1);
topsoil_core::storage::unhashed::put(b":pre_inherent", b"0");
}
}
impl PostInherents for MockedSystemCallbacks {
fn post_inherents() {
assert_eq!(SystemCallbacksCalled::get(), 1);
SystemCallbacksCalled::set(2);
topsoil_core::storage::unhashed::put(b":post_inherent", b"0");
}
}
impl PostTransactions for MockedSystemCallbacks {
fn post_transactions() {
assert_eq!(SystemCallbacksCalled::get(), 2);
SystemCallbacksCalled::set(3);
topsoil_core::storage::unhashed::put(b":post_transaction", b"0");
}
}
impl MockedSystemCallbacks {
fn pre_inherent_called() -> bool {
SystemCallbacksCalled::get() >= 1
}
fn post_inherent_called() -> bool {
SystemCallbacksCalled::get() >= 2
}
fn post_transactions_called() -> bool {
SystemCallbacksCalled::get() >= 3
}
fn reset() {
SystemCallbacksCalled::set(0);
topsoil_core::storage::unhashed::kill(b":pre_inherent");
topsoil_core::storage::unhashed::kill(b":post_inherent");
topsoil_core::storage::unhashed::kill(b":post_transaction");
}
}
parameter_types! {
pub static MbmActive: bool = false;
}
pub struct MockedModeGetter;
impl MultiStepMigrator for MockedModeGetter {
fn ongoing() -> bool {
MbmActive::get()
}
fn step() -> Weight {
Weight::zero()
}
}
fn tx_ext(nonce: u64, fee: Balance) -> TxExtension {
(
topsoil_core::system::AuthorizeCall::<Runtime>::new(),
topsoil_core::system::CheckEra::from(Era::Immortal),
topsoil_core::system::CheckNonce::from(nonce),
topsoil_core::system::CheckWeight::new(),
plant_transaction_payment::ChargeTransactionPayment::from(fee),
topsoil_core::system::WeightReclaim::new(),
)
.into()
}
fn call_transfer(dest: u64, value: u64) -> RuntimeCall {
RuntimeCall::Balances(BalancesCall::transfer_allow_death { dest, value })
}
#[test]
fn balance_transfer_dispatch_works() {
let mut t = topsoil_core::system::GenesisConfig::<Runtime>::default().build_storage().unwrap();
plant_balances::GenesisConfig::<Runtime> { balances: vec![(1, 211)], ..Default::default() }
.assimilate_storage(&mut t)
.unwrap();
let xt = UncheckedXt::new_signed(call_transfer(2, 69), 1, 1.into(), tx_ext(0, 0));
let weight = xt.get_dispatch_info().total_weight()
+ <Runtime as topsoil_core::system::Config>::BlockWeights::get()
.get(DispatchClass::Normal)
.base_extrinsic;
let fee: Balance =
<Runtime as plant_transaction_payment::Config>::WeightToFee::weight_to_fee(&weight);
let mut t = subsoil::io::TestExternalities::new(t);
t.execute_with(|| {
Executive::initialize_block(&Header::new_from_number(1));
let r = Executive::apply_extrinsic(xt);
assert!(r.is_ok());
assert_eq!(<plant_balances::Pallet<Runtime>>::total_balance(&1), 142 - fee);
assert_eq!(<plant_balances::Pallet<Runtime>>::total_balance(&2), 69);
});
}
fn new_test_ext(balance_factor: Balance) -> subsoil::io::TestExternalities {
let mut t = topsoil_core::system::GenesisConfig::<Runtime>::default().build_storage().unwrap();
plant_balances::GenesisConfig::<Runtime> {
balances: vec![(1, 111 * balance_factor)],
..Default::default()
}
.assimilate_storage(&mut t)
.unwrap();
let mut ext: subsoil::io::TestExternalities = t.into();
ext.execute_with(|| {
SystemCallbacksCalled::set(0);
});
ext
}
fn new_test_ext_v0(balance_factor: Balance) -> subsoil::io::TestExternalities {
let mut t = topsoil_core::system::GenesisConfig::<Runtime>::default().build_storage().unwrap();
plant_balances::GenesisConfig::<Runtime> {
balances: vec![(1, 111 * balance_factor)],
..Default::default()
}
.assimilate_storage(&mut t)
.unwrap();
(t, subsoil::runtime::StateVersion::V0).into()
}
#[test]
fn block_import_works() {
block_import_works_inner(
new_test_ext_v0(1),
array_bytes::hex_n_into_unchecked(
"4826d3bdf87dbbc883d2ab274cbe272f58ed94a904619b59953e48294d1142d2",
),
);
block_import_works_inner(
new_test_ext(1),
array_bytes::hex_n_into_unchecked(
"d6b465f5a50c9f8d5a6edc0f01d285a6b19030f097d3aaf1649b7be81649f118",
),
);
}
fn block_import_works_inner(mut ext: subsoil::io::TestExternalities, state_root: H256) {
ext.execute_with(|| {
Executive::execute_block(
Block {
header: Header {
parent_hash: [69u8; 32].into(),
number: 1,
state_root,
extrinsics_root: array_bytes::hex_n_into_unchecked(
"03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314",
),
digest: Digest { logs: vec![] },
},
extrinsics: vec![],
}
.into(),
);
});
}
#[test]
#[should_panic]
fn block_import_of_bad_state_root_fails() {
new_test_ext(1).execute_with(|| {
Executive::execute_block(
Block {
header: Header {
parent_hash: [69u8; 32].into(),
number: 1,
state_root: [0u8; 32].into(),
extrinsics_root: array_bytes::hex_n_into_unchecked(
"03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314",
),
digest: Digest { logs: vec![] },
},
extrinsics: vec![],
}
.into(),
);
});
}
#[test]
#[should_panic]
fn block_import_of_bad_extrinsic_root_fails() {
new_test_ext(1).execute_with(|| {
Executive::execute_block(
Block {
header: Header {
parent_hash: [69u8; 32].into(),
number: 1,
state_root: array_bytes::hex_n_into_unchecked(
"75e7d8f360d375bbe91bcf8019c01ab6362448b4a89e3b329717eb9d910340e5",
),
extrinsics_root: [0u8; 32].into(),
digest: Digest { logs: vec![] },
},
extrinsics: vec![],
}
.into(),
);
});
}
#[test]
fn bad_extrinsic_not_inserted() {
let mut t = new_test_ext(1);
let xt = UncheckedXt::new_signed(call_transfer(33, 69), 1, 1.into(), tx_ext(30, 0));
t.execute_with(|| {
Executive::initialize_block(&Header::new_from_number(1));
assert_err!(
Executive::apply_extrinsic(xt),
TransactionValidityError::Invalid(InvalidTransaction::Future)
);
assert_eq!(<topsoil_core::system::Pallet<Runtime>>::extrinsic_index(), Some(0));
});
}
#[test]
fn block_weight_limit_enforced() {
let mut t = new_test_ext(10000);
let transfer_weight =
<<Runtime as plant_balances::Config>::WeightInfo as plant_balances::WeightInfo>::transfer_allow_death();
let extension_weight = tx_ext(0u32.into(), 0)
.weight(&RuntimeCall::Balances(BalancesCall::transfer_allow_death { dest: 33, value: 0 }));
let block_weights = <Runtime as topsoil_core::system::Config>::BlockWeights::get();
let base_block_weight = Weight::from_parts(175, 0) + block_weights.base_block;
let limit = block_weights.get(DispatchClass::Normal).max_total.unwrap() - base_block_weight;
let num_to_exhaust_block =
limit.ref_time() / (transfer_weight.ref_time() + extension_weight.ref_time() + 5);
t.execute_with(|| {
Executive::initialize_block(&Header::new_from_number(1));
assert_eq!(<topsoil_core::system::Pallet<Runtime>>::block_weight().total(), base_block_weight);
for nonce in 0..=num_to_exhaust_block {
let xt = UncheckedXt::new_signed(
RuntimeCall::Balances(BalancesCall::transfer_allow_death { dest: 33, value: 0 }),
1,
1.into(),
tx_ext(nonce.into(), 0),
);
let encoded = xt.encode();
let encoded_len = encoded.len() as u64;
let res = Executive::apply_extrinsic(xt);
if nonce != num_to_exhaust_block {
assert!(res.is_ok());
assert_eq!(
<topsoil_core::system::Pallet<Runtime>>::block_weight().total(),
Weight::from_parts(
(transfer_weight.ref_time() + extension_weight.ref_time() + 5)
* (nonce + 1),
(nonce + 1) * encoded_len
) + base_block_weight,
);
assert_eq!(
<topsoil_core::system::Pallet<Runtime>>::extrinsic_index(),
Some(nonce as u32 + 1)
);
} else {
assert_eq!(res, Err(InvalidTransaction::ExhaustsResources.into()));
}
}
});
}
#[test]
fn block_weight_and_size_is_stored_per_tx() {
let xt = UncheckedXt::new_signed(
RuntimeCall::Balances(BalancesCall::transfer_allow_death { dest: 33, value: 0 }),
1,
1.into(),
tx_ext(0, 0),
);
let x1 = UncheckedXt::new_signed(
RuntimeCall::Balances(BalancesCall::transfer_allow_death { dest: 33, value: 0 }),
1,
1.into(),
tx_ext(1, 0),
);
let x2 = UncheckedXt::new_signed(
RuntimeCall::Balances(BalancesCall::transfer_allow_death { dest: 33, value: 0 }),
1,
1.into(),
tx_ext(2, 0),
);
let len = xt.clone().encode().len() as u32;
let extension_weight = xt.extension_weight();
let transfer_weight = <<Runtime as plant_balances::Config>::WeightInfo as plant_balances::WeightInfo>::transfer_allow_death();
let mut t = new_test_ext(2);
t.execute_with(|| {
let base_block_weight = Weight::from_parts(175, 0)
+ <Runtime as topsoil_core::system::Config>::BlockWeights::get().base_block;
Executive::initialize_block(&Header::new_from_number(1));
assert_eq!(<topsoil_core::system::Pallet<Runtime>>::block_weight().total(), base_block_weight);
let header_overhead = <topsoil_core::system::Pallet<Runtime>>::block_size();
assert!(Executive::apply_extrinsic(xt.clone()).unwrap().is_ok());
assert!(Executive::apply_extrinsic(x1.clone()).unwrap().is_ok());
assert!(Executive::apply_extrinsic(x2.clone()).unwrap().is_ok());
let extrinsic_weight = transfer_weight
+ extension_weight
+ <Runtime as topsoil_core::system::Config>::BlockWeights::get()
.get(DispatchClass::Normal)
.base_extrinsic;
assert_eq!(
<topsoil_core::system::Pallet<Runtime>>::block_weight().total(),
base_block_weight + 3u64 * extrinsic_weight + 3u64 * Weight::from_parts(0, len as u64),
);
assert_eq!(<topsoil_core::system::Pallet<Runtime>>::block_size(), 3 * len + header_overhead);
let _ = <topsoil_core::system::Pallet<Runtime>>::finalize();
assert_eq!(<topsoil_core::system::Pallet<Runtime>>::block_size(), 0);
SystemCallbacksCalled::take();
Executive::initialize_block(&Header::new_from_number(2));
assert_eq!(<topsoil_core::system::Pallet<Runtime>>::block_weight().total(), base_block_weight);
});
}
#[test]
fn validate_unsigned() {
let valid = UncheckedXt::new_bare(RuntimeCall::Custom(custom::Call::allowed_unsigned {}));
let invalid = UncheckedXt::new_bare(RuntimeCall::Custom(custom::Call::unallowed_unsigned {}));
let mut t = new_test_ext(1);
t.execute_with(|| {
Executive::initialize_block(&Header::new_from_number(1));
assert_eq!(
Executive::validate_transaction(
TransactionSource::InBlock,
valid.clone(),
Default::default(),
),
Ok(ValidTransaction::default()),
);
assert_eq!(
Executive::validate_transaction(
TransactionSource::InBlock,
invalid.clone(),
Default::default(),
),
Err(TransactionValidityError::Unknown(UnknownTransaction::NoUnsignedValidator)),
);
assert_eq!(Executive::apply_extrinsic(valid), Ok(Err(DispatchError::BadOrigin)));
assert_eq!(
Executive::apply_extrinsic(invalid),
Err(TransactionValidityError::Unknown(UnknownTransaction::NoUnsignedValidator))
);
});
}
#[test]
fn can_not_pay_for_tx_fee_on_full_lock() {
let mut t = new_test_ext(1);
t.execute_with(|| {
<plant_balances::Pallet<Runtime> as fungible::MutateFreeze<u64>>::set_freeze(
&FreezeReasonId::Foo,
&1,
110,
)
.unwrap();
let xt = UncheckedXt::new_signed(
RuntimeCall::System(topsoil_core::system::Call::remark { remark: vec![1u8] }),
1,
1.into(),
tx_ext(0, 0),
);
Executive::initialize_block(&Header::new_from_number(1));
assert_eq!(Executive::apply_extrinsic(xt), Err(InvalidTransaction::Payment.into()),);
assert_eq!(<plant_balances::Pallet<Runtime>>::total_balance(&1), 111);
});
}
#[test]
fn block_hooks_weight_is_stored() {
new_test_ext(1).execute_with(|| {
Executive::initialize_block(&Header::new_from_number(1));
Executive::finalize_block();
assert_eq!(
<topsoil_core::system::Pallet<Runtime>>::block_weight().total(),
Weight::from_parts(175 + 175 + 10, 0)
);
})
}
#[test]
fn runtime_upgraded_should_work() {
new_test_ext(1).execute_with(|| {
RuntimeVersionTestValues::mutate(|v| *v = Default::default());
assert!(LastRuntimeUpgrade::<Runtime>::exists());
assert!(!Executive::runtime_upgraded());
RuntimeVersionTestValues::mutate(|v| {
*v = subsoil::version::RuntimeVersion { spec_version: 1, ..Default::default() }
});
assert!(Executive::runtime_upgraded());
RuntimeVersionTestValues::mutate(|v| {
*v = subsoil::version::RuntimeVersion {
spec_version: 1,
spec_name: "test".into(),
..Default::default()
}
});
assert!(Executive::runtime_upgraded());
RuntimeVersionTestValues::mutate(|v| {
*v = subsoil::version::RuntimeVersion {
spec_version: 0,
impl_version: 2,
..Default::default()
}
});
assert!(!Executive::runtime_upgraded());
LastRuntimeUpgrade::<Runtime>::take();
assert!(Executive::runtime_upgraded());
})
}
#[test]
fn last_runtime_upgrade_was_upgraded_works() {
let test_data = vec![
(0, "", 1, "", true),
(1, "", 1, "", false),
(1, "", 1, "test", true),
(1, "", 0, "", false),
(1, "", 0, "test", true),
];
for (spec_version, spec_name, c_spec_version, c_spec_name, result) in test_data {
let current = subsoil::version::RuntimeVersion {
spec_version: c_spec_version,
spec_name: c_spec_name.into(),
..Default::default()
};
let last = LastRuntimeUpgradeInfo {
spec_version: spec_version.into(),
spec_name: spec_name.into(),
};
assert_eq!(result, last.was_upgraded(¤t));
}
}
#[test]
fn custom_runtime_upgrade_is_called_before_modules() {
new_test_ext(1).execute_with(|| {
RuntimeVersionTestValues::mutate(|v| {
*v = subsoil::version::RuntimeVersion { spec_version: 1, ..Default::default() }
});
Executive::initialize_block(&Header::new_from_number(1));
assert_eq!(&subsoil::io::storage::get(TEST_KEY).unwrap()[..], *b"module");
assert_eq!(subsoil::io::storage::get(CUSTOM_ON_RUNTIME_KEY).unwrap(), true.encode());
assert_eq!(
Some(RuntimeVersionTestValues::get().into()),
LastRuntimeUpgrade::<Runtime>::get(),
)
});
}
#[test]
fn event_from_runtime_upgrade_is_included() {
new_test_ext(1).execute_with(|| {
RuntimeVersionTestValues::mutate(|v| {
*v = subsoil::version::RuntimeVersion { spec_version: 1, ..Default::default() }
});
System::set_block_number(1);
Executive::initialize_block(&Header::new_from_number(2));
System::assert_last_event(topsoil_core::system::Event::<Runtime>::CodeUpdated.into());
});
}
#[test]
fn custom_runtime_upgrade_is_called_when_using_execute_block_trait() {
let xt = UncheckedXt::new_signed(
RuntimeCall::Balances(BalancesCall::transfer_allow_death { dest: 33, value: 0 }),
1,
1.into(),
tx_ext(0, 0),
);
let header = new_test_ext(1).execute_with(|| {
RuntimeVersionTestValues::mutate(|v| {
*v = subsoil::version::RuntimeVersion { spec_version: 1, ..Default::default() }
});
Executive::initialize_block(&Header::new_from_number(1));
Executive::apply_extrinsic(xt.clone()).unwrap().unwrap();
Executive::finalize_block()
});
RuntimeVersionTestValues::mutate(|v| {
*v = subsoil::version::RuntimeVersion { spec_version: 0, ..Default::default() }
});
new_test_ext(1).execute_with(|| {
RuntimeVersionTestValues::mutate(|v| {
*v = subsoil::version::RuntimeVersion { spec_version: 1, ..Default::default() }
});
<Executive as ExecuteBlock<Block<UncheckedXt>>>::execute_block(
Block::new(header, vec![xt]).into(),
);
assert_eq!(&subsoil::io::storage::get(TEST_KEY).unwrap()[..], *b"module");
assert_eq!(subsoil::io::storage::get(CUSTOM_ON_RUNTIME_KEY).unwrap(), true.encode());
});
}
#[test]
fn all_weights_are_recorded_correctly() {
RuntimeVersionTestValues::take();
new_test_ext(1).execute_with(|| {
RuntimeVersionTestValues::mutate(|v| {
*v = subsoil::version::RuntimeVersion { spec_version: 1, ..Default::default() }
});
let block_number = 1;
Executive::initialize_block(&Header::new_from_number(block_number));
LastRuntimeUpgrade::<Runtime>::take();
MockedSystemCallbacks::reset();
let runtime_upgrade_weight = Executive::execute_on_runtime_upgrade();
let on_initialize_weight =
<AllPalletsWithSystem as OnInitialize<u64>>::on_initialize(block_number);
let base_block_weight = <Runtime as topsoil_core::system::Config>::BlockWeights::get().base_block;
assert_eq!(
topsoil_core::system::Pallet::<Runtime>::block_weight().total(),
runtime_upgrade_weight + on_initialize_weight + base_block_weight,
);
});
}
#[test]
fn offchain_worker_works_as_expected() {
new_test_ext(1).execute_with(|| {
let parent_hash = subsoil::core::H256::from([69u8; 32]);
System::initialize(&1, &parent_hash, &Digest::default());
System::finalize();
let mut digest = Digest::default();
digest.push(DigestItem::Seal([1, 2, 3, 4], vec![5, 6, 7, 8]));
let header = Header::new(1, H256::default(), H256::default(), parent_hash, digest.clone());
Executive::offchain_worker(&header);
assert_eq!(digest, System::digest());
assert_eq!(parent_hash, System::block_hash(0));
assert_eq!(header.hash(), System::block_hash(1));
});
}
#[test]
fn calculating_storage_root_twice_works() {
let call = RuntimeCall::Custom(custom::Call::calculate_storage_root {});
let xt = UncheckedXt::new_signed(call, 1, 1.into(), tx_ext(0, 0));
let header = new_test_ext(1).execute_with(|| {
Executive::initialize_block(&Header::new_from_number(1));
Executive::apply_extrinsic(xt.clone()).unwrap().unwrap();
Executive::finalize_block()
});
new_test_ext(1).execute_with(|| {
Executive::execute_block(Block::new(header, vec![xt]).into());
});
}
#[test]
#[should_panic(expected = "Invalid inherent position for extrinsic at index 1")]
fn invalid_inherent_position_fail() {
let xt1 = UncheckedXt::new_signed(
RuntimeCall::Balances(BalancesCall::transfer_allow_death { dest: 33, value: 0 }),
1,
1.into(),
tx_ext(0, 0),
);
let xt2 = UncheckedXt::new_bare(RuntimeCall::Custom(custom::Call::inherent {}));
let header = new_test_ext(1).execute_with(|| {
Executive::initialize_block(&Header::new_from_number(1));
Executive::apply_extrinsic(xt1.clone()).unwrap().unwrap();
Executive::apply_extrinsic(xt2.clone()).unwrap().unwrap();
Executive::finalize_block()
});
new_test_ext(1).execute_with(|| {
Executive::execute_block(Block::new(header, vec![xt1, xt2]).into());
});
}
#[test]
fn valid_inherents_position_works() {
let xt1 = UncheckedXt::new_bare(RuntimeCall::Custom(custom::Call::inherent {}));
let xt2 = UncheckedXt::new_signed(call_transfer(33, 0), 1, 1.into(), tx_ext(0, 0));
let header = new_test_ext(1).execute_with(|| {
Executive::initialize_block(&Header::new_from_number(1));
Executive::apply_extrinsic(xt1.clone()).unwrap().unwrap();
Executive::apply_extrinsic(xt2.clone()).unwrap().unwrap();
Executive::finalize_block()
});
new_test_ext(1).execute_with(|| {
Executive::execute_block(Block::new(header, vec![xt1, xt2]).into());
});
}
#[test]
#[should_panic(expected = "A call was labelled as mandatory, but resulted in an Error.")]
fn invalid_inherents_fail_block_execution() {
let xt1 = UncheckedXt::new_signed(
RuntimeCall::Custom(custom::Call::inherent {}),
1,
1.into(),
tx_ext(0, 0),
);
new_test_ext(1).execute_with(|| {
Executive::execute_block(
Block::new(
Header::new(
1,
H256::default(),
H256::default(),
[69u8; 32].into(),
Digest::default(),
),
vec![xt1],
)
.into(),
);
});
}
#[test]
fn inherents_fail_validate_block() {
let xt1 = UncheckedXt::new_bare(RuntimeCall::Custom(custom::Call::inherent {}));
new_test_ext(1).execute_with(|| {
assert_eq!(
Executive::validate_transaction(TransactionSource::External, xt1, H256::random())
.unwrap_err(),
InvalidTransaction::MandatoryValidation.into()
);
})
}
#[test]
fn inherents_ok_while_exts_forbidden_works() {
let xt1 = UncheckedXt::new_bare(RuntimeCall::Custom(custom::Call::inherent {}));
let header = new_test_ext(1).execute_with(|| {
Executive::initialize_block(&Header::new_from_number(1));
Executive::apply_extrinsic(xt1.clone()).unwrap().unwrap();
Executive::finalize_block()
});
new_test_ext(1).execute_with(|| {
Executive::execute_block(Block::new(header, vec![xt1]).into());
});
}
#[test]
#[should_panic = "Only inherents are allowed in this block"]
fn transactions_in_only_inherents_block_errors() {
let xt1 = UncheckedXt::new_bare(RuntimeCall::Custom(custom::Call::inherent {}));
let xt2 = UncheckedXt::new_signed(call_transfer(33, 0), 1, 1.into(), tx_ext(0, 0));
let header = new_test_ext(1).execute_with(|| {
Executive::initialize_block(&Header::new_from_number(1));
Executive::apply_extrinsic(xt1.clone()).unwrap().unwrap();
Executive::apply_extrinsic(xt2.clone()).unwrap().unwrap();
Executive::finalize_block()
});
new_test_ext(1).execute_with(|| {
MbmActive::set(true);
Executive::execute_block(Block::new(header, vec![xt1, xt2]).into());
});
}
#[test]
fn transactions_in_normal_block_works() {
let xt1 = UncheckedXt::new_bare(RuntimeCall::Custom(custom::Call::inherent {}));
let xt2 = UncheckedXt::new_signed(call_transfer(33, 0), 1, 1.into(), tx_ext(0, 0));
let header = new_test_ext(1).execute_with(|| {
Executive::initialize_block(&Header::new_from_number(1));
Executive::apply_extrinsic(xt1.clone()).unwrap().unwrap();
Executive::apply_extrinsic(xt2.clone()).unwrap().unwrap();
Executive::finalize_block()
});
new_test_ext(1).execute_with(|| {
Executive::execute_block(Block::new(header, vec![xt1, xt2]).into());
});
}
#[test]
#[cfg(feature = "try-runtime")]
fn try_execute_block_works() {
let xt1 = UncheckedXt::new_bare(RuntimeCall::Custom(custom::Call::inherent {}));
let xt2 = UncheckedXt::new_signed(call_transfer(33, 0), 1, 1.into(), tx_ext(0, 0));
let header = new_test_ext(1).execute_with(|| {
Executive::initialize_block(&Header::new_from_number(1));
Executive::apply_extrinsic(xt1.clone()).unwrap().unwrap();
Executive::apply_extrinsic(xt2.clone()).unwrap().unwrap();
Executive::finalize_block()
});
new_test_ext(1).execute_with(|| {
Executive::try_execute_block(
Block::new(header, vec![xt1, xt2]).into(),
true,
true,
topsoil_try_runtime::TryStateSelect::All,
)
.unwrap();
});
}
#[test]
#[cfg(feature = "try-runtime")]
fn try_runtime_upgrade_works() {
use topsoil_core::traits::OnGenesis;
subsoil::tracing::init_for_tests();
type ExecutiveWithoutMigrations = super::Executive<
Runtime,
Block<UncheckedXt>,
ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
>;
new_test_ext(1).execute_with(|| {
AllPalletsWithSystem::on_genesis();
assert!(&subsoil::io::storage::get(TEST_KEY_2).is_none());
ExecutiveWithoutMigrations::try_runtime_upgrade(UpgradeCheckSelect::All).unwrap();
assert_eq!(
&subsoil::io::storage::get(TEST_KEY_2).unwrap()[..],
*b"try_runtime_upgrade_works"
);
});
}
#[test]
#[cfg(feature = "try-runtime")]
#[should_panic = "Only inherents are allowed in this block"]
fn try_execute_tx_forbidden_errors() {
let xt1 = UncheckedXt::new_bare(RuntimeCall::Custom(custom::Call::inherent {}));
let xt2 = UncheckedXt::new_signed(call_transfer(33, 0), 1, 1.into(), tx_ext(0, 0));
let header = new_test_ext(1).execute_with(|| {
Executive::initialize_block(&Header::new_from_number(1));
Executive::apply_extrinsic(xt1.clone()).unwrap().unwrap();
Executive::apply_extrinsic(xt2.clone()).unwrap().unwrap();
Executive::finalize_block()
});
new_test_ext(1).execute_with(|| {
MbmActive::set(true);
Executive::try_execute_block(
Block::new(header, vec![xt1, xt2]).into(),
true,
true,
topsoil_try_runtime::TryStateSelect::All,
)
.unwrap();
});
}
#[test]
fn apply_extrinsics_checks_inherents_are_first() {
let in1 = UncheckedXt::new_bare(RuntimeCall::Custom(custom::Call::inherent {}));
let in2 = UncheckedXt::new_bare(RuntimeCall::Custom2(custom2::Call::inherent {}));
let xt2 = UncheckedXt::new_signed(call_transfer(33, 0), 1, 1.into(), tx_ext(0, 0));
new_test_ext(1).execute_with(|| {
assert_ok!(
Executive::apply_extrinsics(
ExtrinsicInclusionMode::AllExtrinsics,
[].into_iter(),
|_, _| Ok(Ok(()))
),
()
);
assert_ok!(
Executive::apply_extrinsics(
ExtrinsicInclusionMode::AllExtrinsics,
[Ok(xt2.clone())].into_iter(),
|_, _| Ok(Ok(()))
),
()
);
assert_ok!(
Executive::apply_extrinsics(
ExtrinsicInclusionMode::AllExtrinsics,
[Ok(in1.clone())].into_iter(),
|_, _| Ok(Ok(()))
),
()
);
assert_ok!(
Executive::apply_extrinsics(
ExtrinsicInclusionMode::AllExtrinsics,
[Ok(in1.clone()), Ok(xt2.clone())].into_iter(),
|_, _| Ok(Ok(()))
),
()
);
assert_ok!(
Executive::apply_extrinsics(
ExtrinsicInclusionMode::AllExtrinsics,
[Ok(in2.clone()), Ok(in1.clone()), Ok(xt2.clone())].into_iter(),
|_, _| Ok(Ok(()))
),
()
);
assert_err!(
Executive::apply_extrinsics(
ExtrinsicInclusionMode::AllExtrinsics,
[Ok(xt2.clone()), Ok(in1.clone())].into_iter(),
|_, _| Ok(Ok(()))
),
ExecutiveError::InvalidInherentPosition(1)
);
assert_err!(
Executive::apply_extrinsics(
ExtrinsicInclusionMode::AllExtrinsics,
[Ok(xt2.clone()), Ok(xt2.clone()), Ok(in1.clone())].into_iter(),
|_, _| Ok(Ok(()))
),
ExecutiveError::InvalidInherentPosition(2)
);
assert_err!(
Executive::apply_extrinsics(
ExtrinsicInclusionMode::AllExtrinsics,
[Ok(xt2.clone()), Ok(xt2.clone()), Ok(xt2.clone()), Ok(in2.clone())].into_iter(),
|_, _| Ok(Ok(()))
),
ExecutiveError::InvalidInherentPosition(3)
);
assert_err!(
Executive::apply_extrinsics(
ExtrinsicInclusionMode::AllExtrinsics,
[
Ok(in2.clone()),
Ok(in1.clone()),
Err(codec::Error::from("Test")),
Ok(xt2.clone())
]
.into_iter(),
|_, _| Ok(Ok(()))
),
ExecutiveError::UnableToDecodeExtrinsic
);
});
}
#[test]
fn callbacks_in_block_execution_works() {
callbacks_in_block_execution_works_inner(false);
callbacks_in_block_execution_works_inner(true);
}
fn callbacks_in_block_execution_works_inner(mbms_active: bool) {
MbmActive::set(mbms_active);
for (n_in, n_tx) in (0..10usize).zip(0..10usize) {
let mut extrinsics = Vec::new();
let header = new_test_ext(10).execute_with(|| {
MockedSystemCallbacks::reset();
Executive::initialize_block(&Header::new_from_number(1));
assert_eq!(SystemCallbacksCalled::get(), 1);
for i in 0..n_in {
let xt = if i % 2 == 0 {
UncheckedXt::new_bare(RuntimeCall::Custom(custom::Call::inherent {}))
} else {
UncheckedXt::new_bare(RuntimeCall::Custom2(custom2::Call::optional_inherent {}))
};
Executive::apply_extrinsic(xt.clone()).unwrap().unwrap();
extrinsics.push(xt);
}
for t in 0..n_tx {
let xt = UncheckedXt::new_signed(
RuntimeCall::Custom2(custom2::Call::some_call {}),
1,
1.into(),
tx_ext(t as u64, 0),
);
Executive::apply_extrinsic(xt.clone()).unwrap().unwrap();
extrinsics.push(xt);
}
Executive::finalize_block()
});
assert_eq!(SystemCallbacksCalled::get(), 3);
new_test_ext(10).execute_with(|| {
let header = std::panic::catch_unwind(|| {
Executive::execute_block(Block::new(header, extrinsics).into());
});
match header {
Err(e) => {
let err = e.downcast::<String>().unwrap();
assert_eq!(*err, "Only inherents are allowed in this block");
assert!(
MbmActive::get() && n_tx > 0,
"Transactions should be rejected when MBMs are active"
);
},
Ok(_) => {
assert_eq!(SystemCallbacksCalled::get(), 3);
assert!(
!MbmActive::get() || n_tx == 0,
"MBMs should be deactivated after finalization"
);
},
}
});
}
}
#[test]
fn post_inherent_called_after_all_inherents() {
let in1 = UncheckedXt::new_bare(RuntimeCall::Custom2(custom2::Call::inherent {}));
let xt1 = UncheckedXt::new_signed(
RuntimeCall::Custom2(custom2::Call::some_call {}),
1,
1.into(),
tx_ext(0, 0),
);
let header = new_test_ext(1).execute_with(|| {
Executive::initialize_block(&Header::new_from_number(1));
Executive::apply_extrinsic(in1.clone()).unwrap().unwrap();
Executive::apply_extrinsic(xt1.clone()).unwrap().unwrap();
Executive::finalize_block()
});
#[cfg(feature = "try-runtime")]
new_test_ext(1).execute_with(|| {
Executive::try_execute_block(
Block::new(header.clone(), vec![in1.clone(), xt1.clone()]).into(),
true,
true,
topsoil_try_runtime::TryStateSelect::All,
)
.unwrap();
assert!(MockedSystemCallbacks::post_transactions_called());
});
new_test_ext(1).execute_with(|| {
MockedSystemCallbacks::reset();
Executive::execute_block(Block::new(header, vec![in1, xt1]).into());
assert!(MockedSystemCallbacks::post_transactions_called());
});
}
#[test]
fn post_inherent_called_after_all_optional_inherents() {
let in1 = UncheckedXt::new_bare(RuntimeCall::Custom2(custom2::Call::optional_inherent {}));
let xt1 = UncheckedXt::new_signed(
RuntimeCall::Custom2(custom2::Call::some_call {}),
1,
1.into(),
tx_ext(0, 0),
);
let header = new_test_ext(1).execute_with(|| {
Executive::initialize_block(&Header::new_from_number(1));
Executive::apply_extrinsic(in1.clone()).unwrap().unwrap();
Executive::apply_extrinsic(xt1.clone()).unwrap().unwrap();
Executive::finalize_block()
});
#[cfg(feature = "try-runtime")]
new_test_ext(1).execute_with(|| {
Executive::try_execute_block(
Block::new(header.clone(), vec![in1.clone(), xt1.clone()]).into(),
true,
true,
topsoil_try_runtime::TryStateSelect::All,
)
.unwrap();
assert!(MockedSystemCallbacks::post_transactions_called());
});
new_test_ext(1).execute_with(|| {
MockedSystemCallbacks::reset();
Executive::execute_block(Block::new(header, vec![in1, xt1]).into());
assert!(MockedSystemCallbacks::post_transactions_called());
});
}
#[test]
fn is_inherent_works() {
let ext = UncheckedXt::new_bare(RuntimeCall::Custom2(custom2::Call::inherent {}));
assert!(Runtime::is_inherent(&ext));
let ext = UncheckedXt::new_bare(RuntimeCall::Custom2(custom2::Call::optional_inherent {}));
assert!(Runtime::is_inherent(&ext));
let ext = UncheckedXt::new_signed(call_transfer(33, 0), 1, 1.into(), tx_ext(0, 0));
assert!(!Runtime::is_inherent(&ext));
let ext = UncheckedXt::new_bare(RuntimeCall::Custom2(custom2::Call::allowed_unsigned {}));
assert!(!Runtime::is_inherent(&ext), "Unsigned ext are not automatically inherents");
}
#[test]
fn max_transaction_depth_is_respected() {
use soil_test_node_runtime_client::{
prelude::*,
runtime::{ExtrinsicBuilder, RuntimeCall, UtilityCall},
BlockOrigin,
};
let client = TestClientBuilder::default().build();
let mut call = RuntimeCall::System(topsoil_core::system::Call::remark { remark: vec![1, 2, 3] });
for _ in 0..MAX_EXTRINSIC_DEPTH {
call = RuntimeCall::Utility(UtilityCall::batch { calls: vec![call] });
}
let call_one_more = RuntimeCall::Utility(UtilityCall::batch { calls: vec![call.clone()] });
let ext = ExtrinsicBuilder::new(call).build();
let mut block_builder = BlockBuilderBuilder::new(&client)
.on_parent_block(client.chain_info().best_hash)
.with_parent_block_number(0)
.build()
.unwrap();
block_builder.push(ext).unwrap();
block_builder.push(ExtrinsicBuilder::new(call_one_more).build()).unwrap_err();
let block = block_builder.build().unwrap().block;
block_on(client.import(BlockOrigin::Own, block)).unwrap();
}