Skip to content

Commit

Permalink
Set the empty contract create value to Anyone
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Feb 4, 2025
1 parent be46277 commit eb4e295
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 22 deletions.
19 changes: 10 additions & 9 deletions domains/client/domain-operator/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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"
);

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
24 changes: 14 additions & 10 deletions domains/pallets/evm-tracker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -55,7 +56,16 @@ mod pallet {
// be updated.
#[pallet::storage]
pub(super) type ContractCreationAllowedBy<T: Config> =
StorageValue<_, PermissionedActionAllowedBy<T::AccountId>, OptionQuery>;
StorageValue<_, PermissionedActionAllowedBy<T::AccountId>, ValueQuery, DefaultToAnyone>;

/// Default value for ContractCreationAllowedBy if it is not set.
pub struct DefaultToAnyone;

impl<AccountId: Codec + Clone> Get<PermissionedActionAllowedBy<AccountId>> for DefaultToAnyone {
fn get() -> PermissionedActionAllowedBy<AccountId> {
PermissionedActionAllowedBy::Anyone
}
}

/// Pallet EVM account nonce tracker.
#[pallet::pallet]
Expand Down Expand Up @@ -100,23 +110,17 @@ impl<T: Config> Pallet<T> {

/// 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::<T>::get()
.map(|allowed_by| allowed_by.is_allowed(signer))
.unwrap_or(true)
ContractCreationAllowedBy::<T>::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::<T>::get()
.map(|allowed_by| allowed_by.is_anyone_allowed())
.unwrap_or(true)
ContractCreationAllowedBy::<T>::get().is_anyone_allowed()
}

/// Returns the current contract creation allow list.
/// Mainly used in tests.
pub fn contract_creation_allowed_by() -> Option<PermissionedActionAllowedBy<T::AccountId>> {
pub fn contract_creation_allowed_by() -> PermissionedActionAllowedBy<T::AccountId> {
ContractCreationAllowedBy::<T>::get()
}
}
2 changes: 1 addition & 1 deletion domains/test/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PermissionedActionAllowedBy<EthereumAccountId>>;
}
}
2 changes: 1 addition & 1 deletion domains/test/runtime/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1651,7 +1651,7 @@ impl_runtime_apis! {

impl domain_test_primitives::EvmOnchainStateApi<Block> for Runtime {
fn evm_contract_creation_allowed_by() -> Option<PermissionedActionAllowedBy<EthereumAccountId>> {
EVMNoncetracker::contract_creation_allowed_by()
Some(EVMNoncetracker::contract_creation_allowed_by())
}
}

Expand Down
3 changes: 2 additions & 1 deletion domains/test/service/src/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<EthereumAccountId>> {
) -> PermissionedActionAllowedBy<EthereumAccountId> {
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")
}
}

Expand Down

0 comments on commit eb4e295

Please sign in to comment.