Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Merge branch 'snowbridge' into ron/more-integration-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Jan 29, 2024
2 parents cade895 + 0d4fa19 commit 9b238d6
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use bp_runtime::ChainId;
use frame_support::{
parameter_types,
traits::{ConstU32, Contains, Equals, Everything, Nothing},
StoragePrefixedMap,
};
use frame_system::EnsureRoot;
use pallet_xcm::XcmPassthrough;
Expand Down Expand Up @@ -160,9 +161,12 @@ impl Contains<RuntimeCall> for SafeCallFilter {
match call {
RuntimeCall::System(frame_system::Call::set_storage { items })
if items.iter().all(|(k, _)| {
k.eq(&DeliveryRewardInBalance::key()) |
k.eq(&RequiredStakeForStakeAndSlash::key()) |
k.eq(&EthereumGatewayAddress::key())
k.eq(&DeliveryRewardInBalance::key()) ||
k.eq(&RequiredStakeForStakeAndSlash::key()) ||
k.eq(&EthereumGatewayAddress::key()) ||
// Allow resetting of Ethereum nonces in Rococo only.
k.starts_with(&snowbridge_pallet_inbound_queue::Nonce::<Runtime>::final_prefix()) ||
k.starts_with(&snowbridge_pallet_outbound_queue::Nonce::<Runtime>::final_prefix())
}) =>
return true,
_ => (),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use parachains_common::{
rococo::{consensus::RELAY_CHAIN_SLOT_DURATION_MILLIS, fee::WeightToFee},
AccountId, AuraId, Balance, SLOT_DURATION,
};
use snowbridge_core::ChannelId;
use sp_consensus_aura::SlotDuration;
use sp_core::H160;
use sp_keyring::AccountKeyring::Alice;
Expand Down Expand Up @@ -222,6 +223,72 @@ mod bridge_hub_westend_tests {
)
}

#[test]
fn change_ethereum_nonces_by_governance_works() {
let channel_id_one: ChannelId = [1; 32].into();
let channel_id_two: ChannelId = [2; 32].into();
let nonce = 42;

// Reset a single inbound channel
bridge_hub_test_utils::test_cases::set_storage_keys_by_governance_works::<Runtime>(
collator_session_keys(),
bp_bridge_hub_rococo::BRIDGE_HUB_ROCOCO_PARACHAIN_ID,
Box::new(|call| RuntimeCall::System(call).encode()),
vec![
(snowbridge_pallet_outbound_queue::Nonce::<Runtime>::hashed_key_for::<ChannelId>(
channel_id_one,
)
.to_vec(), 0u64.encode()),
(snowbridge_pallet_inbound_queue::Nonce::<Runtime>::hashed_key_for::<ChannelId>(
channel_id_one,
)
.to_vec(), 0u64.encode()),
],
|| {
// Outbound
snowbridge_pallet_outbound_queue::Nonce::<Runtime>::insert::<ChannelId, u64>(
channel_id_one,
nonce,
);
snowbridge_pallet_outbound_queue::Nonce::<Runtime>::insert::<ChannelId, u64>(
channel_id_two,
nonce,
);

// Inbound
snowbridge_pallet_inbound_queue::Nonce::<Runtime>::insert::<ChannelId, u64>(
channel_id_one,
nonce,
);
snowbridge_pallet_inbound_queue::Nonce::<Runtime>::insert::<ChannelId, u64>(
channel_id_two,
nonce,
);
},
|| {
// Outbound
assert_eq!(
snowbridge_pallet_outbound_queue::Nonce::<Runtime>::get(channel_id_one),
0
);
assert_eq!(
snowbridge_pallet_outbound_queue::Nonce::<Runtime>::get(channel_id_two),
nonce
);

// Inbound
assert_eq!(
snowbridge_pallet_inbound_queue::Nonce::<Runtime>::get(channel_id_one),
0
);
assert_eq!(
snowbridge_pallet_inbound_queue::Nonce::<Runtime>::get(channel_id_two),
nonce
);
},
);
}

#[test]
fn change_delivery_reward_by_governance_works() {
bridge_hub_test_utils::test_cases::change_storage_constant_by_governance_works::<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ pub type RuntimeHelper<Runtime, AllPalletsWithoutSystem = ()> =
parachains_runtimes_test_utils::RuntimeHelper<Runtime, AllPalletsWithoutSystem>;

// Re-export test_case from `parachains-runtimes-test-utils`
pub use parachains_runtimes_test_utils::test_cases::change_storage_constant_by_governance_works;
pub use parachains_runtimes_test_utils::test_cases::{
change_storage_constant_by_governance_works, set_storage_keys_by_governance_works,
};

/// Prepare default runtime storage and run test within this context.
pub fn run_test<Runtime, T>(
Expand Down
52 changes: 52 additions & 0 deletions cumulus/parachains/runtimes/test-utils/src/test_cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,55 @@ pub fn change_storage_constant_by_governance_works<Runtime, StorageConstant, Sto
);
})
}

/// Test-case makes sure that `Runtime` can change storage constant via governance-like call
pub fn set_storage_keys_by_governance_works<Runtime>(
collator_session_key: CollatorSessionKeys<Runtime>,
runtime_para_id: u32,
runtime_call_encode: Box<dyn Fn(frame_system::Call<Runtime>) -> Vec<u8>>,
storage_items: Vec<(Vec<u8>, Vec<u8>)>,
initialize_storage: impl FnOnce() -> (),
assert_storage: impl FnOnce() -> (),
) where
Runtime: frame_system::Config
+ pallet_balances::Config
+ pallet_session::Config
+ pallet_xcm::Config
+ parachain_info::Config
+ pallet_collator_selection::Config
+ cumulus_pallet_parachain_system::Config,
ValidatorIdOf<Runtime>: From<AccountIdOf<Runtime>>,
{
let mut runtime = ExtBuilder::<Runtime>::default()
.with_collators(collator_session_key.collators())
.with_session_keys(collator_session_key.session_keys())
.with_para_id(runtime_para_id.into())
.with_tracing()
.build();
runtime.execute_with(|| {
initialize_storage();
});
runtime.execute_with(|| {
// encode `kill_storage` call
let kill_storage_call = runtime_call_encode(frame_system::Call::<Runtime>::set_storage {
items: storage_items.clone(),
});

// estimate - storing just 1 value
use frame_system::WeightInfo;
let require_weight_at_most =
<Runtime as frame_system::Config>::SystemWeightInfo::set_storage(
storage_items.len().try_into().unwrap(),
);

// execute XCM with Transact to `set_storage` as governance does
assert_ok!(RuntimeHelper::<Runtime>::execute_as_governance(
kill_storage_call,
require_weight_at_most
)
.ensure_complete());
});
runtime.execute_with(|| {
assert_storage();
});
}

0 comments on commit 9b238d6

Please sign in to comment.