Skip to content

Hotfix/filter nested batches #1662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: devnet-ready
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
31 changes: 30 additions & 1 deletion pallets/subtensor/src/tests/batch_tx.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use super::mock::*;
use frame_support::{assert_ok, traits::Currency};
use frame_support::{
assert_ok,
traits::{Contains, Currency},
};
use frame_system::Config;
use sp_core::U256;

Expand Down Expand Up @@ -33,3 +36,29 @@ fn test_batch_txs() {
assert_eq!(Balances::total_balance(&charlie), 2_000_000_000);
});
}

#[test]
fn test_cant_nest_batch_txs() {
let _alice = U256::from(0);
let bob = U256::from(1);
let charlie = U256::from(2);

new_test_ext(1).execute_with(|| {
let call = RuntimeCall::Utility(pallet_utility::Call::batch {
calls: vec![
RuntimeCall::Balances(BalanceCall::transfer_allow_death {
dest: bob,
value: 1_000_000_000,
}),
RuntimeCall::Utility(pallet_utility::Call::force_batch {
calls: vec![RuntimeCall::Balances(BalanceCall::transfer_allow_death {
dest: charlie,
value: 1_000_000_000,
})],
}),
],
});

assert!(!<Test as Config>::BaseCallFilter::contains(&call));
});
}
27 changes: 25 additions & 2 deletions pallets/subtensor/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
use crate::utils::rate_limiting::TransactionType;
use frame_support::derive_impl;
use frame_support::dispatch::DispatchResultWithPostInfo;
use frame_support::traits::{Contains, Everything, InsideBoth};
use frame_support::weights::Weight;
use frame_support::weights::constants::RocksDbWeight;
use frame_support::{
assert_ok, parameter_types,
traits::{Everything, Hooks, PrivilegeCmp},
traits::{Hooks, PrivilegeCmp},
};
use frame_system as system;
use frame_system::{EnsureNever, EnsureRoot, RawOrigin, limits};
Expand Down Expand Up @@ -88,9 +89,31 @@ impl pallet_balances::Config for Test {
type MaxFreezes = ();
}

pub struct NoNestingCallFilter;

impl Contains<RuntimeCall> for NoNestingCallFilter {
fn contains(call: &RuntimeCall) -> bool {
match call {
RuntimeCall::Utility(inner) => {
let calls = match inner {
pallet_utility::Call::force_batch { calls } => calls,
pallet_utility::Call::batch { calls } => calls,
pallet_utility::Call::batch_all { calls } => calls,
_ => &Vec::new(),
};

!calls.iter().any(|call| {
matches!(call, RuntimeCall::Utility(inner) if matches!(inner, pallet_utility::Call::force_batch { .. } | pallet_utility::Call::batch_all { .. } | pallet_utility::Call::batch { .. }))
})
}
_ => true,
}
}
}

#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl system::Config for Test {
type BaseCallFilter = Everything;
type BaseCallFilter = InsideBoth<Everything, NoNestingCallFilter>;
type BlockWeights = BlockWeights;
type BlockLength = ();
type DbWeight = RocksDbWeight;
Expand Down
28 changes: 25 additions & 3 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub mod check_nonce;
mod migrations;

use codec::{Compact, Decode, Encode};
use frame_support::traits::Imbalance;
use frame_support::traits::{Imbalance, InsideBoth};
use frame_support::{
PalletId,
dispatch::DispatchResultWithPostInfo,
Expand Down Expand Up @@ -209,7 +209,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// `spec_version`, and `authoring_version` are the same between Wasm and native.
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
// the compatible custom types.
spec_version: 271,
spec_version: 272,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down Expand Up @@ -244,11 +244,33 @@ parameter_types! {
pub const SS58Prefix: u8 = 42;
}

pub struct NoNestingCallFilter;

impl Contains<RuntimeCall> for NoNestingCallFilter {
fn contains(call: &RuntimeCall) -> bool {
match call {
RuntimeCall::Utility(inner) => {
let calls = match inner {
pallet_utility::Call::force_batch { calls } => calls,
pallet_utility::Call::batch { calls } => calls,
pallet_utility::Call::batch_all { calls } => calls,
_ => &Vec::new(),
};

!calls.iter().any(|call| {
matches!(call, RuntimeCall::Utility(inner) if matches!(inner, pallet_utility::Call::force_batch { .. } | pallet_utility::Call::batch_all { .. } | pallet_utility::Call::batch { .. }))
})
}
_ => true,
}
}
}

// Configure FRAME pallets to include in runtime.

impl frame_system::Config for Runtime {
// The basic call filter to use in dispatchable.
type BaseCallFilter = SafeMode;
type BaseCallFilter = InsideBoth<SafeMode, NoNestingCallFilter>;
// Block & extrinsics weights: base values and limits.
type BlockWeights = BlockWeights;
// The maximum length of a block (in bytes).
Expand Down
Loading