Skip to content

Commit

Permalink
added optional contract deploy restrict
Browse files Browse the repository at this point in the history
  • Loading branch information
supremeSOURCE committed Oct 26, 2024
1 parent fb6e909 commit 58bdb72
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
3 changes: 3 additions & 0 deletions pallets/governance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ pub mod pallet {
#[pallet::storage]
pub type Curator<T: Config> = StorageValue<_, T::AccountId, ValueQuery, DefaultKey<T>>;

/// Determines whether smart contract can be deployed by everyone or only by the curator
#[pallet::storage]
pub type RestrictContractDeploy<T: Config> = StorageValue<_, bool, ValueQuery>;
// ---------------------------------
// Extrinsics
// ---------------------------------
Expand Down
40 changes: 37 additions & 3 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ pub use pallet_subspace;
mod precompiles;
use precompiles::FrontierPrecompiles;

use frame_system::RawOrigin;
use pallet_evm::EnsureAddressOrigin;
use sp_runtime::AccountId32;

/// An index to a block.
pub type BlockNumber = u64;

Expand Down Expand Up @@ -184,7 +188,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// `spec_version`, and `authoring_version` are the same between Wasm and native.
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
// the compatible custom types.
spec_version: 424,
spec_version: 429,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down Expand Up @@ -524,8 +528,8 @@ impl pallet_evm::Config for Runtime {
type BalanceConverter = EvmBalanceConverter;
type WeightPerGas = WeightPerGas;
type BlockHashMapping = pallet_ethereum::EthereumBlockHashMapping<Self>;
type CallOrigin = pallet_evm::EnsureAddressTruncated;
type WithdrawOrigin = pallet_evm::EnsureAddressTruncated;
type CallOrigin = EnsureCuratorAddressTruncated;
type WithdrawOrigin = EnsureCuratorAddressTruncated;
type AddressMapping = HashedAddressMapping<BlakeTwo256>;
type Currency = Balances;
type RuntimeEvent = RuntimeEvent;
Expand Down Expand Up @@ -656,6 +660,36 @@ impl BalanceConverter for EvmBalanceConverter {
}
}

pub struct EnsureCuratorAddressTruncated;

impl EnsureAddressOrigin<RuntimeOrigin> for EnsureCuratorAddressTruncated {
type Success = AccountId32;

fn try_address_origin(
address: &H160,
origin: RuntimeOrigin,
) -> Result<AccountId32, RuntimeOrigin> {
<RuntimeOrigin as Into<Result<RawOrigin<AccountId32>, RuntimeOrigin>>>::into(origin)
.and_then(|o| match o {
RawOrigin::Signed(who) => {
let address_matches = AsRef::<[u8; 32]>::as_ref(&who)[0..20] == address[0..20];

if !address_matches {
return Err(RuntimeOrigin::from(RawOrigin::Signed(who)));
}

if pallet_governance::RestrictContractDeploy::<Runtime>::get()
&& Some(who.clone()) != Some(pallet_governance::Curator::<Runtime>::get()) {
return Err(RuntimeOrigin::from(RawOrigin::Signed(who)));
}

Ok(who)
}
r => Err(RuntimeOrigin::from(r)),
})
}
}

// The address format for describing accounts.
pub type Address = sp_runtime::MultiAddress<AccountId, ()>;
// Block header type as expected by this runtime.
Expand Down

0 comments on commit 58bdb72

Please sign in to comment.