From eb4e29533bc38d2121e9d379c02c5dbda07546ef Mon Sep 17 00:00:00 2001 From: teor Date: Tue, 4 Feb 2025 11:30:34 +1000 Subject: [PATCH] Set the empty contract create value to Anyone --- domains/client/domain-operator/src/tests.rs | 19 ++++++++-------- domains/pallets/evm-tracker/src/lib.rs | 24 ++++++++++++--------- domains/test/primitives/src/lib.rs | 2 +- domains/test/runtime/evm/src/lib.rs | 2 +- domains/test/service/src/domain.rs | 3 ++- 5 files changed, 28 insertions(+), 22 deletions(-) diff --git a/domains/client/domain-operator/src/tests.rs b/domains/client/domain-operator/src/tests.rs index f7f9404fb4..72b93d564b 100644 --- a/domains/client/domain-operator/src/tests.rs +++ b/domains/client/domain-operator/src/tests.rs @@ -465,7 +465,7 @@ async fn test_evm_domain_create_contracts_with_allow_list_reject_all() { // Wait until list is updated produce_blocks_until!(ferdie, alice, { - alice.evm_contract_creation_allowed_by().as_ref() == Some(&allow_list) + alice.evm_contract_creation_allowed_by() == allow_list }) .await .unwrap(); @@ -693,7 +693,7 @@ async fn test_evm_domain_create_contracts_with_allow_list_single() { // Wait until list is updated produce_blocks_until!(ferdie, alice, { - alice.evm_contract_creation_allowed_by().as_ref() == Some(&allow_list) + alice.evm_contract_creation_allowed_by() == allow_list }) .await .unwrap(); @@ -889,7 +889,7 @@ async fn test_evm_domain_create_contracts_with_allow_list_multiple() { // Wait until list is updated produce_blocks_until!(ferdie, alice, { - alice.evm_contract_creation_allowed_by().as_ref() == Some(&allow_list) + alice.evm_contract_creation_allowed_by() == allow_list }) .await .unwrap(); @@ -4769,8 +4769,9 @@ async fn test_domain_sudo_calls() { produce_blocks!(ferdie, alice, 3).await.unwrap(); // check initial contract allow list - assert!( - alice.evm_contract_creation_allowed_by() == Some(PermissionedActionAllowedBy::Anyone), + assert_eq!( + alice.evm_contract_creation_allowed_by(), + PermissionedActionAllowedBy::Anyone, "initial contract allow list should be anyone" ); @@ -4805,7 +4806,7 @@ async fn test_domain_sudo_calls() { // Wait until list is updated produce_blocks_until!(ferdie, alice, { - alice.evm_contract_creation_allowed_by().as_ref() == Some(&allow_list) + alice.evm_contract_creation_allowed_by() == allow_list }) .await .unwrap(); @@ -4835,7 +4836,7 @@ async fn test_domain_sudo_calls() { // Wait until list is updated produce_blocks_until!(ferdie, alice, { - alice.evm_contract_creation_allowed_by().as_ref() == Some(&allow_list) + alice.evm_contract_creation_allowed_by() == allow_list }) .await .unwrap(); @@ -4865,7 +4866,7 @@ async fn test_domain_sudo_calls() { // Wait until list is updated produce_blocks_until!(ferdie, alice, { - alice.evm_contract_creation_allowed_by().as_ref() == Some(&allow_list) + alice.evm_contract_creation_allowed_by() == allow_list }) .await .unwrap(); @@ -4895,7 +4896,7 @@ async fn test_domain_sudo_calls() { // Wait until list is updated produce_blocks_until!(ferdie, alice, { - alice.evm_contract_creation_allowed_by().as_ref() == Some(&allow_list) + alice.evm_contract_creation_allowed_by() == allow_list }) .await .unwrap(); diff --git a/domains/pallets/evm-tracker/src/lib.rs b/domains/pallets/evm-tracker/src/lib.rs index 06f0a05abd..ab289a41c1 100644 --- a/domains/pallets/evm-tracker/src/lib.rs +++ b/domains/pallets/evm-tracker/src/lib.rs @@ -31,6 +31,7 @@ use sp_domains::PermissionedActionAllowedBy; #[frame_support::pallet] mod pallet { + use codec::Codec; use frame_support::pallet_prelude::*; use frame_system::pallet_prelude::*; use sp_core::U256; @@ -55,7 +56,16 @@ mod pallet { // be updated. #[pallet::storage] pub(super) type ContractCreationAllowedBy = - StorageValue<_, PermissionedActionAllowedBy, OptionQuery>; + StorageValue<_, PermissionedActionAllowedBy, ValueQuery, DefaultToAnyone>; + + /// Default value for ContractCreationAllowedBy if it is not set. + pub struct DefaultToAnyone; + + impl Get> for DefaultToAnyone { + fn get() -> PermissionedActionAllowedBy { + PermissionedActionAllowedBy::Anyone + } + } /// Pallet EVM account nonce tracker. #[pallet::pallet] @@ -100,23 +110,17 @@ impl Pallet { /// Returns true if the account is allowed to create contracts. pub fn is_allowed_to_create_contracts(signer: &T::AccountId) -> bool { - // Unlike domain instantiation, no storage value means "anyone can create contracts". - ContractCreationAllowedBy::::get() - .map(|allowed_by| allowed_by.is_allowed(signer)) - .unwrap_or(true) + ContractCreationAllowedBy::::get().is_allowed(signer) } /// Returns true if the account is allowed to create contracts. pub fn is_allowed_to_create_unsigned_contracts() -> bool { - // Unlike domain instantiation, no storage value means "anyone can create contracts". - ContractCreationAllowedBy::::get() - .map(|allowed_by| allowed_by.is_anyone_allowed()) - .unwrap_or(true) + ContractCreationAllowedBy::::get().is_anyone_allowed() } /// Returns the current contract creation allow list. /// Mainly used in tests. - pub fn contract_creation_allowed_by() -> Option> { + pub fn contract_creation_allowed_by() -> PermissionedActionAllowedBy { ContractCreationAllowedBy::::get() } } diff --git a/domains/test/primitives/src/lib.rs b/domains/test/primitives/src/lib.rs index 13e957c4b8..ef3b706c19 100644 --- a/domains/test/primitives/src/lib.rs +++ b/domains/test/primitives/src/lib.rs @@ -42,7 +42,7 @@ sp_api::decl_runtime_apis! { pub trait EvmOnchainStateApi { /// Returns the current EVM contract creation allow list. - /// Returns `None` if this is not an EVM domain, or if the allow list isn't set (allow all). + /// Returns `None` if this is not an EVM domain. fn evm_contract_creation_allowed_by() -> Option>; } } diff --git a/domains/test/runtime/evm/src/lib.rs b/domains/test/runtime/evm/src/lib.rs index 11a7b4570f..4213509ba9 100644 --- a/domains/test/runtime/evm/src/lib.rs +++ b/domains/test/runtime/evm/src/lib.rs @@ -1651,7 +1651,7 @@ impl_runtime_apis! { impl domain_test_primitives::EvmOnchainStateApi for Runtime { fn evm_contract_creation_allowed_by() -> Option> { - EVMNoncetracker::contract_creation_allowed_by() + Some(EVMNoncetracker::contract_creation_allowed_by()) } } diff --git a/domains/test/service/src/domain.rs b/domains/test/service/src/domain.rs index 0379b4ec12..e04daf3497 100644 --- a/domains/test/service/src/domain.rs +++ b/domains/test/service/src/domain.rs @@ -432,11 +432,12 @@ where /// Returns `None` if this is not an EVM domain, or if the allow list isn't set (allow all). pub fn evm_contract_creation_allowed_by( &self, - ) -> Option> { + ) -> PermissionedActionAllowedBy { self.client .runtime_api() .evm_contract_creation_allowed_by(self.client.info().best_hash) .expect("Failed to get EVM contact creation allow list") + .expect("Should be an EVM domain") } }