Skip to content

Commit

Permalink
Add tests for contract allow lists
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Jan 21, 2025
1 parent 6f92663 commit b29c6f3
Show file tree
Hide file tree
Showing 9 changed files with 420 additions and 14 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

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

200 changes: 199 additions & 1 deletion crates/pallet-domains/src/domain_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ mod tests {
use frame_support::{assert_err, assert_ok};
use hex_literal::hex;
use sp_domains::storage::RawGenesis;
use sp_domains::RuntimeObject;
use sp_domains::{EvmDomainRuntimeConfig, PermissionedActionAllowedBy, RuntimeObject};
use sp_runtime::traits::Convert;
use sp_std::vec;
use sp_version::RuntimeVersion;
Expand Down Expand Up @@ -726,4 +726,202 @@ mod tests {
);
});
}

#[test]
fn test_domain_instantiation_evm_contract_allow_list() {
let creator = 1u128;
let created_at = 0u64;
// Construct a valid default domain config
let mut domain_config_params = DomainConfigParams {
domain_name: "evm-domain".to_owned(),
runtime_id: 0,
maybe_bundle_limit: None,
bundle_slot_probability: (1, 1),
operator_allow_list: OperatorAllowList::Anyone,
initial_balances: vec![(
AccountId20Converter::convert(AccountId20::from(hex!(
"f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac"
))),
1_000_000 * SSC,
)],
domain_runtime_config: Default::default(),
};

let mut ext = new_test_ext();
ext.execute_with(|| {
assert_eq!(NextDomainId::<Test>::get(), 0.into());
// Register runtime id
RuntimeRegistry::<Test>::insert(
domain_config_params.runtime_id,
RuntimeObject {
runtime_name: "evm".to_owned(),
runtime_type: Default::default(),
runtime_upgrades: 0,
hash: Default::default(),
raw_genesis: RawGenesis::dummy(vec![1, 2, 3, 4]),
version: RuntimeVersion {
spec_name: "test".into(),
spec_version: 1,
impl_version: 1,
transaction_version: 1,
..Default::default()
},
created_at: Default::default(),
updated_at: Default::default(),
instance_count: 0,
},
);

// Set enough fund to creator
Balances::make_free_balance_be(
&creator,
<Test as Config>::DomainInstantiationDeposit::get()
// for domain total issuance
+ 1_000_000 * SSC
+ <Test as pallet_balances::Config>::ExistentialDeposit::get(),
);

// should be successful
let domain_id =
do_instantiate_domain::<Test>(domain_config_params.clone(), creator, created_at)
.unwrap();
let domain_obj = DomainRegistry::<Test>::get(domain_id).unwrap();

assert_eq!(domain_obj.owner_account_id, creator);
assert_eq!(domain_obj.created_at, created_at);
assert_eq!(
domain_obj.domain_config,
into_domain_config::<Test>(domain_config_params.clone()).unwrap()
);
assert_eq!(
domain_obj
.domain_config
.domain_runtime_config
.initial_contract_creation_allow_list(),
Some(&PermissionedActionAllowedBy::Anyone),
"anyone should be allowed to create contracts by default"
);

// Set empty list
let mut list = vec![];
domain_config_params.domain_runtime_config = EvmDomainRuntimeConfig {
initial_contract_creation_allow_list: PermissionedActionAllowedBy::Accounts(
list.clone(),
),
}
.into();

// Set enough fund to creator
Balances::make_free_balance_be(
&creator,
<Test as Config>::DomainInstantiationDeposit::get()
// for domain total issuance
+ 1_000_000 * SSC
+ <Test as pallet_balances::Config>::ExistentialDeposit::get(),
);

// should be successful
let domain_id =
do_instantiate_domain::<Test>(domain_config_params.clone(), creator, created_at)
.unwrap();
let domain_obj = DomainRegistry::<Test>::get(domain_id).unwrap();

assert_eq!(domain_obj.owner_account_id, creator);
assert_eq!(domain_obj.created_at, created_at);
assert_eq!(
domain_obj.domain_config,
into_domain_config::<Test>(domain_config_params.clone()).unwrap()
);
assert_eq!(
domain_obj
.domain_config
.domain_runtime_config
.initial_contract_creation_allow_list(),
Some(&PermissionedActionAllowedBy::Accounts(list)),
"empty list should work"
);

// Set 1 account in list
list = vec![hex!("0102030405060708091011121314151617181920").into()];
domain_config_params.domain_runtime_config = EvmDomainRuntimeConfig {
initial_contract_creation_allow_list: PermissionedActionAllowedBy::Accounts(
list.clone(),
),
}
.into();

// Set enough fund to creator
Balances::make_free_balance_be(
&creator,
<Test as Config>::DomainInstantiationDeposit::get()
// for domain total issuance
+ 1_000_000 * SSC
+ <Test as pallet_balances::Config>::ExistentialDeposit::get(),
);

// should be successful
let domain_id =
do_instantiate_domain::<Test>(domain_config_params.clone(), creator, created_at)
.unwrap();
let domain_obj = DomainRegistry::<Test>::get(domain_id).unwrap();

assert_eq!(domain_obj.owner_account_id, creator);
assert_eq!(domain_obj.created_at, created_at);
assert_eq!(
domain_obj.domain_config,
into_domain_config::<Test>(domain_config_params.clone()).unwrap()
);
assert_eq!(
domain_obj
.domain_config
.domain_runtime_config
.initial_contract_creation_allow_list(),
Some(&PermissionedActionAllowedBy::Accounts(list)),
"1 account list should work"
);

// Set multi account list
list = vec![
hex!("0102030405060708091011121314151617181920").into(),
hex!("1102030405060708091011121314151617181920").into(),
hex!("2102030405060708091011121314151617181920").into(),
];
domain_config_params.domain_runtime_config = EvmDomainRuntimeConfig {
initial_contract_creation_allow_list: PermissionedActionAllowedBy::Accounts(
list.clone(),
),
}
.into();

// Set enough fund to creator
Balances::make_free_balance_be(
&creator,
<Test as Config>::DomainInstantiationDeposit::get()
// for domain total issuance
+ 1_000_000 * SSC
+ <Test as pallet_balances::Config>::ExistentialDeposit::get(),
);

// should be successful
let domain_id =
do_instantiate_domain::<Test>(domain_config_params.clone(), creator, created_at)
.unwrap();
let domain_obj = DomainRegistry::<Test>::get(domain_id).unwrap();

assert_eq!(domain_obj.owner_account_id, creator);
assert_eq!(domain_obj.created_at, created_at);
assert_eq!(
domain_obj.domain_config,
into_domain_config::<Test>(domain_config_params.clone()).unwrap()
);
assert_eq!(
domain_obj
.domain_config
.domain_runtime_config
.initial_contract_creation_allow_list(),
Some(&PermissionedActionAllowedBy::Accounts(list)),
"multi account list should work"
);
});
}
}
2 changes: 2 additions & 0 deletions domains/client/domain-operator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ domain-test-service = { version = "0.1.0", path = "../../test/service" }
domain-test-primitives = { version = "0.1.0", path = "../../test/primitives" }
evm-domain-test-runtime = { version = "0.1.0", path = "../../test/runtime/evm" }
frame-system = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "94a1a8143a89bbe9f938c1939ff68abc1519a305" }
hex-literal = "0.4.1"
pallet-balances = { git = "https://github.com/subspace/polkadot-sdk", rev = "94a1a8143a89bbe9f938c1939ff68abc1519a305" }
pallet-domains = { version = "0.1.0", path = "../../../crates/pallet-domains" }
pallet-domain-sudo = { version = "0.1.0", path = "../../pallets/domain-sudo" }
pallet-evm-tracker = { version = "0.1.0", path = "../../pallets/evm-tracker" }
pallet-messenger = { version = "0.1.0", path = "../../../domains/pallets/messenger" }
pallet-sudo = { git = "https://github.com/subspace/polkadot-sdk", rev = "94a1a8143a89bbe9f938c1939ff68abc1519a305" }
pallet-timestamp = { git = "https://github.com/subspace/polkadot-sdk", rev = "94a1a8143a89bbe9f938c1939ff68abc1519a305" }
Expand Down
Loading

0 comments on commit b29c6f3

Please sign in to comment.