Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alistair-singh committed Jul 19, 2023
1 parent 2a9f2b0 commit ad9d35c
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 138 deletions.
4 changes: 2 additions & 2 deletions core/packages/test/scripts/set-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ export RANDAO_COMMIT_DELAY=3
export RANDAO_COMMIT_EXP=3

export BRIDGE_HUB_PARAID=$bridgehub_para_id
export BRIDGE_HUB_AGENT_ID=""
export BRIDGE_HUB_AGENT_ID="0x0000000000000000000000000000000000000000000000000000000000000000"

export ASSET_HUB_PARAID=$statemine_para_id
export ASSET_HUB_AGENT_ID=""
export ASSET_HUB_AGENT_ID="0x0000000000000000000000000000000000000000000000000000000000000000"

export DEFAULT_FEE=1
export DEFAULT_REWARD=1
Expand Down
1 change: 1 addition & 0 deletions parachain/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions parachain/pallets/control/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,8 @@ xcm = { git = "https://github.com/paritytech/polkadot.git", branch = "master", d

ethabi = { git = "https://github.com/Snowfork/ethabi-decode.git", package = "ethabi-decode", branch = "master", default-features = false }


[dev-dependencies]
xcm-builder = { git = "https://github.com/paritytech/polkadot.git", branch = "master" }


xcm-builder = { git = "https://github.com/paritytech/polkadot.git", branch = "master", default-features = false }

[features]
default = ["std"]
Expand All @@ -50,7 +47,11 @@ std = [
"sp-io/std",
"sp-runtime/std",
"xcm/std",
"xcm-builder/std",
"ethabi/std"
]
runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks"]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"xcm-builder/runtime-benchmarks"
]
try-runtime = ["frame-support/try-runtime"]
5 changes: 2 additions & 3 deletions parachain/pallets/control/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ mod benchmarks {
use super::*;

#[benchmark]
fn upgrade(x: Linear<0, { T::MaxUpgradeDataSize::get() }>) -> Result<(), BenchmarkError> {
let caller: T::AccountId = whitelisted_caller();
fn upgrade(x: Linear<0, { T::MaxUpgradeDataSize::get() - 1 }>) -> Result<(), BenchmarkError> {
let logic = H160::repeat_byte(1);
let data: Vec<u8> = (0..x).map(|_| 1u8).collect();

#[extrinsic_call]
_(RawOrigin::Signed(caller), logic, Some(data));
_(RawOrigin::Root, logic, Some(data));

Ok(())
}
Expand Down
1 change: 0 additions & 1 deletion parachain/pallets/control/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ pub mod pallet {
use super::*;
use frame_support::{pallet_prelude::*, traits::EnsureOrigin};
use frame_system::pallet_prelude::*;
use xcm::v3::MultiLocation;

#[pallet::pallet]
pub struct Pallet<T>(_);
Expand Down
16 changes: 10 additions & 6 deletions parachain/pallets/control/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ use frame_support::{
use snowbridge_core::{ParaId};
use sp_core::H256;
use sp_runtime::{
BuildStorage,
AccountId32,
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
};


use xcm_builder::{EnsureXcmOrigin, SignedToAccountId32};
use xcm::prelude::*;

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
type AccountId = AccountId32;

// Configure a mock runtime to test the pallet.
frame_support::construct_runtime!(
Expand All @@ -42,7 +42,7 @@ impl frame_system::Config for Test {
type BlockNumber = u64;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = u64;
type AccountId = AccountId;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type RuntimeEvent = RuntimeEvent;
Expand All @@ -58,14 +58,18 @@ impl frame_system::Config for Test {
type MaxConsumers = frame_support::traits::ConstU32<16>;
}

parameter_types! {
pub const AnyNetwork: Option<NetworkId> = None;
}

pub type LocalOriginToLocation = SignedToAccountId32<RuntimeOrigin, AccountId, AnyNetwork>;

parameter_types! {
pub const OwnParaId: ParaId = ParaId::new(1013);
pub const MaxUpgradeDataSize: u32 = 1024;
pub const SS58Prefix: u8 = 42;
}

pub type LocalOriginToLocation = SignedToAccountId32<RuntimeOrigin, AccountId, RelayNetwork>;

impl snowbridge_control::Config for Test {
type RuntimeEvent = RuntimeEvent;
type OwnParaId = OwnParaId;
Expand Down
4 changes: 2 additions & 2 deletions parachain/pallets/control/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ pub trait WeightInfo {
/// Weights for pallet_template using the Substrate node and recommended hardware.
pub struct SubstrateWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
fn upgrade(data_size: u32) -> Weight {
fn upgrade(_data_size: u32) -> Weight {
Weight::from_parts(9_000_000, 0)
.saturating_add(T::DbWeight::get().writes(1_u64))
}
}

// For backwards compatibility and tests
impl WeightInfo for () {
fn upgrade(data_size: u32) -> Weight {
fn upgrade(_data_size: u32) -> Weight {
Weight::from_parts(9_000_000, 0)
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
Expand Down
1 change: 0 additions & 1 deletion parachain/pallets/ethereum-beacon-client/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use pallet_timestamp;
use primitives::{Fork, ForkVersions};
use sp_core::H256;
use sp_runtime::{
BuildStorage,
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
};
Expand Down
127 changes: 20 additions & 107 deletions parachain/pallets/inbound-queue/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;

frame_support::construct_runtime!(
pub struct Test
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
Expand Down Expand Up @@ -167,7 +170,7 @@ pub fn new_tester(gateway: H160) -> sp_io::TestExternalities {
}

pub fn new_tester_with_config(
config: inbound_queue::GenesisConfig<Test>,
config: inbound_queue::GenesisConfig,
) -> sp_io::TestExternalities {
let mut storage = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();

Expand Down Expand Up @@ -195,7 +198,7 @@ fn parse_dest(message: Message) -> ParaId {
}

// The originating channel address for the messages below
const OUTBOUND_QUEUE_ADDRESS: [u8; 20] = hex!["87d1f7fdfEe7f651FaBc8bFCB6E086C278b77A7d"];
const GATEWAY_ADDRESS: [u8; 20] = hex!["87d1f7fdfEe7f651FaBc8bFCB6E086C278b77A7d"];

const OUTBOUND_QUEUE_EVENT_LOG: [u8; 254] = hex!(
"
Expand All @@ -207,7 +210,7 @@ use snowbridge_core::ParaId;

#[test]
fn test_submit() {
new_tester(OUTBOUND_QUEUE_ADDRESS.into()).execute_with(|| {
new_tester(GATEWAY_ADDRESS.into()).execute_with(|| {
let relayer: AccountId = Keyring::Bob.into();
let origin = RuntimeOrigin::signed(relayer);

Expand Down Expand Up @@ -238,7 +241,7 @@ fn test_submit() {
}

#[test]
fn test_submit_with_invalid_outbound_queue() {
fn test_submit_with_invalid_gateway() {
new_tester(H160::zero()).execute_with(|| {
let relayer: AccountId = Keyring::Bob.into();
let origin = RuntimeOrigin::signed(relayer);
Expand All @@ -259,14 +262,14 @@ fn test_submit_with_invalid_outbound_queue() {
};
assert_noop!(
InboundQueue::submit(origin.clone(), message.clone()),
Error::<Test>::InvalidOutboundQueue
Error::<Test>::InvalidGateway
);
});
}

#[test]
fn test_submit_with_invalid_nonce() {
new_tester(OUTBOUND_QUEUE_ADDRESS.into()).execute_with(|| {
new_tester(GATEWAY_ADDRESS.into()).execute_with(|| {
let relayer: AccountId = Keyring::Bob.into();
let origin = RuntimeOrigin::signed(relayer);

Expand Down Expand Up @@ -300,7 +303,7 @@ fn test_submit_with_invalid_nonce() {

#[test]
fn test_submit_no_funds_to_reward_relayers() {
new_tester(OUTBOUND_QUEUE_ADDRESS.into()).execute_with(|| {
new_tester(GATEWAY_ADDRESS.into()).execute_with(|| {
let relayer: AccountId = Keyring::Bob.into();
let origin = RuntimeOrigin::signed(relayer);

Expand Down Expand Up @@ -328,119 +331,29 @@ fn test_submit_no_funds_to_reward_relayers() {
}

#[test]
fn test_add_allow_list_without_root_yields_bad_origin() {
new_tester_with_config(Default::default()).execute_with(|| {
let contract_address = hex!("0000000000000000000000000000000000000000").into();
let relayer: AccountId = Keyring::Bob.into();
let origin = RuntimeOrigin::signed(relayer);
assert_noop!(
InboundQueue::add_allow_list(origin, contract_address),
sp_runtime::DispatchError::BadOrigin,
);
});
}

#[test]
fn test_add_allow_list_with_root_succeeds() {
fn test_set_gateway_with_root_succeeds() {
new_tester_with_config(Default::default()).execute_with(|| {
let origin = RuntimeOrigin::root();
let contract_address = hex!("0000000000000000000000000000000000000000").into();
let default_gateway_address = hex!("0000000000000000000000000000000000000000").into();
let gateway_address = hex!("1000000000000000000000000000000000000000").into();

assert_eq!(<AllowList<Test>>::get().len(), 0);
assert_ok!(InboundQueue::add_allow_list(origin, contract_address));
assert_eq!(<Gateway<Test>>::get(), default_gateway_address);

System::assert_last_event(RuntimeEvent::InboundQueue(crate::Event::AllowListAdded {
address: contract_address,
}));
assert_ok!(InboundQueue::set_gateway(origin, gateway_address));

assert_eq!(<AllowList<Test>>::get().len(), 1);
assert!(<AllowList<Test>>::get().contains(&contract_address));
assert_eq!(<Gateway<Test>>::get(), gateway_address);
});
}

#[test]
fn test_add_allow_list_ignores_duplicates() {
fn test_set_gateway_without_root_yields_bad_origin() {
new_tester_with_config(Default::default()).execute_with(|| {
let origin = RuntimeOrigin::root();
let contract_address = hex!("0000000000000000000000000000000000000000").into();

assert_eq!(<AllowList<Test>>::get().len(), 0);
assert_ok!(InboundQueue::add_allow_list(origin.clone(), contract_address));
assert_eq!(<AllowList<Test>>::get().len(), 1);
assert!(<AllowList<Test>>::get().contains(&contract_address));
assert_ok!(InboundQueue::add_allow_list(origin, contract_address));
assert_eq!(<AllowList<Test>>::get().len(), 1);
assert!(<AllowList<Test>>::get().contains(&contract_address));
});
}

#[test]
fn test_add_allow_list_fails_when_exceeding_bounds() {
new_tester_with_config(Default::default()).execute_with(|| {
let origin = RuntimeOrigin::root();
let contract_address1 = hex!("0000000000000000000000000000000000000000").into();
let contract_address2 = hex!("1000000000000000000000000000000000000000").into();
let contract_address3 = hex!("3000000000000000000000000000000000000000").into();

assert_eq!(<AllowList<Test>>::get().len(), 0);

assert_ok!(InboundQueue::add_allow_list(origin.clone(), contract_address1));
assert_eq!(<AllowList<Test>>::get().len(), 1);

assert_ok!(InboundQueue::add_allow_list(origin.clone(), contract_address2));
assert_eq!(<AllowList<Test>>::get().len(), 2);

assert_noop!(
InboundQueue::add_allow_list(origin, contract_address3),
Error::<Test>::AllowListFull,
);
assert_eq!(<AllowList<Test>>::get().len(), 2);
});
}

#[test]
fn test_remove_allow_list_without_root_yields_bad_origin() {
new_tester_with_config(Default::default()).execute_with(|| {
let contract_address = hex!("0000000000000000000000000000000000000000").into();
let gateway_address = hex!("0000000000000000000000000000000000000000").into();
let relayer: AccountId = Keyring::Bob.into();
let origin = RuntimeOrigin::signed(relayer);
assert_noop!(
InboundQueue::remove_allow_list(origin, contract_address),
InboundQueue::set_gateway(origin, gateway_address),
sp_runtime::DispatchError::BadOrigin,
);
});
}

#[test]
fn test_remove_allow_list_with_root_succeeds() {
new_tester_with_config(Default::default()).execute_with(|| {
let origin = RuntimeOrigin::root();
let contract_address = hex!("0000000000000000000000000000000000000000").into();

assert_eq!(<AllowList<Test>>::get().len(), 0);
assert_ok!(InboundQueue::add_allow_list(origin.clone(), contract_address));
assert_eq!(<AllowList<Test>>::get().len(), 1);

assert_ok!(InboundQueue::remove_allow_list(origin, contract_address));
System::assert_last_event(RuntimeEvent::InboundQueue(crate::Event::AllowListRemoved {
address: contract_address,
}));

assert_eq!(<AllowList<Test>>::get().len(), 0);
assert!(!<AllowList<Test>>::get().contains(&contract_address));
});
}

#[test]
fn test_remove_allow_list_event_not_emitted_for_none_existent_item() {
new_tester_with_config(Default::default()).execute_with(|| {
let origin = RuntimeOrigin::root();
let contract_address = hex!("0000000000000000000000000000000000000000").into();

let start = System::event_count();
assert_ok!(InboundQueue::remove_allow_list(origin, contract_address));
let end = System::event_count();

assert_eq!(start, end); // No new events
});
}
4 changes: 2 additions & 2 deletions parachain/pallets/outbound-queue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ use frame_support::{
use scale_info::TypeInfo;
use snowbridge_core::ParaId;
use sp_core::{RuntimeDebug, H256};
use sp_runtime::traits::{Hash, BlockNumberProvider};
use sp_runtime::traits::Hash;
use sp_std::prelude::*;

use snowbridge_core::{
ContractId, OutboundMessage, OutboundQueue as OutboundQueueTrait, SubmitError,
OutboundMessage, OutboundQueue as OutboundQueueTrait, SubmitError,
};
use snowbridge_outbound_queue_merkle_tree::merkle_root;

Expand Down
1 change: 0 additions & 1 deletion parachain/pallets/outbound-queue/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use frame_support::{

use sp_core::H256;
use sp_runtime::{
BuildStorage,
testing::Header,
traits::{BlakeTwo256, IdentifyAccount, IdentityLookup, Keccak256, Verify},
BoundedVec, MultiSignature,
Expand Down
Loading

0 comments on commit ad9d35c

Please sign in to comment.