Skip to content

Commit

Permalink
Use a custom error value for contract allow list failures
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Jan 17, 2025
1 parent c8cd63a commit eabc2ac
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions domains/runtime/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ pub type Executive = domain_pallet_executive::Executive<
AllPalletsWithSystem,
>;

/// The custom error value returned when a user tries to create a contract, but their account is
/// not on the allow list.
pub const ERR_CONTRACT_CREATION_NOT_ALLOWED_BY_USER: u8 = 0xCC;

const MAX_CONTRACT_RECURSION_DEPTH: u16 = 5;

/// Rejects contracts that can't be created under the current allow list.
Expand Down Expand Up @@ -210,6 +214,9 @@ pub fn is_create_contract(call: &RuntimeCall, mut recursion_depth_left: u16) ->
#[derive(Debug, Encode, Decode, Clone, Eq, PartialEq, Default, TypeInfo)]
pub struct CheckContractCreation;

// Unsigned calls can't create contracts. Only pallet-evm and pallet-ethereum can create contracts.
// For pallet-evm all contracts are signed extrinsics, for pallet-ethereum there is only one
// extrinsic that is self-contained.
impl SignedExtension for CheckContractCreation {
const IDENTIFIER: &'static str = "CheckContractCreation";
type AccountId = <Runtime as frame_system::Config>::AccountId;
Expand All @@ -230,7 +237,7 @@ impl SignedExtension for CheckContractCreation {
) -> TransactionValidity {
// Reject contract creation unless the account is in the allow list.
if !is_create_contract_allowed(call, who) {
InvalidTransaction::Call.into()
InvalidTransaction::Custom(ERR_CONTRACT_CREATION_NOT_ALLOWED_BY_USER).into()
} else {
Ok(ValidTransaction::default())
}
Expand All @@ -246,8 +253,6 @@ impl SignedExtension for CheckContractCreation {
self.validate(who, call, info, len)?;
Ok(())
}

// TODO: can unsigned calls create contracts?
}

impl fp_self_contained::SelfContainedCall for RuntimeCall {
Expand All @@ -274,8 +279,10 @@ impl fp_self_contained::SelfContainedCall for RuntimeCall {
len: usize,
) -> Option<TransactionValidity> {
if !is_create_contract_allowed(self, &(*info).into()) {
// TODO: should this be Custom() instead?
return Some(Err(InvalidTransaction::Call.into()));
return Some(Err(InvalidTransaction::Custom(
ERR_CONTRACT_CREATION_NOT_ALLOWED_BY_USER,
)
.into()));
}

match self {
Expand Down Expand Up @@ -303,8 +310,10 @@ impl fp_self_contained::SelfContainedCall for RuntimeCall {
len: usize,
) -> Option<Result<(), TransactionValidityError>> {
if !is_create_contract_allowed(self, &(*info).into()) {
// TODO: should this be Custom() instead?
return Some(Err(InvalidTransaction::Call.into()));
return Some(Err(InvalidTransaction::Custom(
ERR_CONTRACT_CREATION_NOT_ALLOWED_BY_USER,
)
.into()));
}

match self {
Expand Down

0 comments on commit eabc2ac

Please sign in to comment.