Skip to content

Commit

Permalink
Add tests for executive try-state
Browse files Browse the repository at this point in the history
  • Loading branch information
ntn-x2 committed May 29, 2024
1 parent efb3041 commit 03c5648
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 15 deletions.
85 changes: 71 additions & 14 deletions substrate/frame/executive/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,40 @@ use pallet_balances::Call as BalancesCall;

const TEST_KEY: &[u8] = b":test:key:";

#[cfg(feature = "try-runtime")]
mod try_runtime {
use frame_support::traits::{IdentifiableTryStateLogic, TryStateLogic};
use sp_runtime::traits::AtLeast32BitUnsigned;

// Will contain `true` when the custom runtime logic was called.
pub(super) static mut CANARY_FLAG: bool = false;

const CUSTOM_TRY_STATE_ID: &[u8] = b"custom_try_state";

pub(super) struct CustomTryState;

impl<BlockNumber> TryStateLogic<BlockNumber> for CustomTryState
where
BlockNumber: Clone + sp_std::fmt::Debug + AtLeast32BitUnsigned,
{
fn try_state(_: BlockNumber) -> Result<(), sp_runtime::TryRuntimeError> {
unsafe {
CANARY_FLAG = true;
}
Ok(())
}
}

impl<BlockNumber> IdentifiableTryStateLogic<BlockNumber> for CustomTryState
where
BlockNumber: Clone + sp_std::fmt::Debug + AtLeast32BitUnsigned,
{
fn matches_id(id: &[u8]) -> bool {
id == CUSTOM_TRY_STATE_ID
}
}
}

#[frame_support::pallet(dev_mode)]
mod custom {
use super::*;
Expand Down Expand Up @@ -78,6 +112,18 @@ mod custom {
fn offchain_worker(n: BlockNumberFor<T>) {
assert_eq!(BlockNumberFor::<T>::from(1u32), n);
}

// Verify that `CustomTryState` has been called before.
#[cfg(feature = "try-runtime")]
fn try_state(_n: BlockNumberFor<T>) -> Result<(), sp_runtime::TryRuntimeError> {
unsafe {
assert!(
try_runtime::CANARY_FLAG,
"Custom `try-runtime` did not run before pallet `try-runtime`."
);
}
Ok(())
}
}

#[pallet::call]
Expand Down Expand Up @@ -268,9 +314,9 @@ mod custom2 {
// Inherent call is accepted for being dispatched
fn pre_dispatch(call: &Self::Call) -> Result<(), TransactionValidityError> {
match call {
Call::allowed_unsigned { .. } |
Call::optional_inherent { .. } |
Call::inherent { .. } => Ok(()),
Call::allowed_unsigned { .. }
| Call::optional_inherent { .. }
| Call::inherent { .. } => Ok(()),
_ => Err(UnknownTransaction::NoUnsignedValidator.into()),
}
}
Expand Down Expand Up @@ -385,13 +431,19 @@ impl OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
}
}

#[cfg(not(feature = "try-runtime"))]
type CustomOnTryState = ();
#[cfg(feature = "try-runtime")]
type CustomOnTryState = try_runtime::CustomTryState;

type Executive = super::Executive<
Runtime,
Block<TestXt>,
ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
CustomOnRuntimeUpgrade,
CustomOnTryState,
>;

parameter_types! {
Expand Down Expand Up @@ -486,8 +538,8 @@ fn balance_transfer_dispatch_works() {
.assimilate_storage(&mut t)
.unwrap();
let xt = TestXt::new(call_transfer(2, 69), sign_extra(1, 0, 0));
let weight = xt.get_dispatch_info().weight +
<Runtime as frame_system::Config>::BlockWeights::get()
let weight = xt.get_dispatch_info().weight
+ <Runtime as frame_system::Config>::BlockWeights::get()
.get(DispatchClass::Normal)
.base_extrinsic;
let fee: Balance =
Expand All @@ -511,9 +563,14 @@ fn new_test_ext(balance_factor: Balance) -> sp_io::TestExternalities {
ext.execute_with(|| {
SystemCallbacksCalled::set(0);
});

#[cfg(feature = "try-runtime")]
unsafe {
try_runtime::CANARY_FLAG = false;
}

ext
}

fn new_test_ext_v0(balance_factor: Balance) -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::<Runtime>::default().build_storage().unwrap();
pallet_balances::GenesisConfig::<Runtime> { balances: vec![(1, 111 * balance_factor)] }
Expand Down Expand Up @@ -669,8 +726,8 @@ fn block_weight_and_size_is_stored_per_tx() {
let mut t = new_test_ext(1);
t.execute_with(|| {
// Block execution weight + on_initialize weight from custom module
let base_block_weight = Weight::from_parts(175, 0) +
<Runtime as frame_system::Config>::BlockWeights::get().base_block;
let base_block_weight = Weight::from_parts(175, 0)
+ <Runtime as frame_system::Config>::BlockWeights::get().base_block;

Executive::initialize_block(&Header::new_from_number(1));

Expand All @@ -682,8 +739,8 @@ fn block_weight_and_size_is_stored_per_tx() {
assert!(Executive::apply_extrinsic(x2.clone()).unwrap().is_ok());

// default weight for `TestXt` == encoded length.
let extrinsic_weight = Weight::from_parts(len as u64, 0) +
<Runtime as frame_system::Config>::BlockWeights::get()
let extrinsic_weight = Weight::from_parts(len as u64, 0)
+ <Runtime as frame_system::Config>::BlockWeights::get()
.get(DispatchClass::Normal)
.base_extrinsic;
assert_eq!(
Expand Down Expand Up @@ -940,10 +997,10 @@ fn all_weights_are_recorded_correctly() {
// Weights are recorded correctly
assert_eq!(
frame_system::Pallet::<Runtime>::block_weight().total(),
custom_runtime_upgrade_weight +
runtime_upgrade_weight +
on_initialize_weight +
base_block_weight,
custom_runtime_upgrade_weight
+ runtime_upgrade_weight
+ on_initialize_weight
+ base_block_weight,
);
});
}
Expand Down
2 changes: 1 addition & 1 deletion substrate/primitives/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ pub trait Crypto {
use ed25519_dalek::Verifier;

let Ok(public_key) = ed25519_dalek::VerifyingKey::from_bytes(&pub_key.0) else {
return false
return false;
};

let sig = ed25519_dalek::Signature::from_bytes(&sig.0);
Expand Down

0 comments on commit 03c5648

Please sign in to comment.