Skip to content

Commit

Permalink
[Kreivo Runtime] Instantiate pallet_contracts (#415)
Browse files Browse the repository at this point in the history
* change(kreivo-runtime): instantiate `pallet_contracts`

* change(kreivo-runtime): only instantiate contracts from root or specific communities
  • Loading branch information
pandres95 authored Jun 27, 2024
1 parent 0a05c39 commit dcdb7be
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 9 deletions.
74 changes: 65 additions & 9 deletions Cargo.lock

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

32 changes: 32 additions & 0 deletions pallets/communities/src/origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,35 @@ where
Ok(frame_system::RawOrigin::Signed(Pallet::<T>::community_account(&community_id)).into())
}
}

/// Ensure the origin is any `Signed` origin.
pub struct AsSignedByStaticCommunity<T, C>(PhantomData<(T, C)>);
impl<T, C, OuterOrigin> EnsureOrigin<OuterOrigin> for AsSignedByStaticCommunity<T, C>
where
OuterOrigin: OriginTrait
+ From<frame_system::RawOrigin<T::AccountId>>
+ From<RawOrigin<T>>
+ Clone
+ Into<Result<frame_system::RawOrigin<T::AccountId>, OuterOrigin>>
+ Into<Result<RawOrigin<T>, OuterOrigin>>,
T: Config,
C: Get<CommunityIdOf<T>>,
{
type Success = T::AccountId;

fn try_origin(o: OuterOrigin) -> Result<Self::Success, OuterOrigin> {
match o.clone().into() {
Ok(RawOrigin { ref community_id, .. }) if community_id == &C::get() => {
Ok(Pallet::<T>::community_account(community_id))
}
_ => Err(o.clone()),
}
}

#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin() -> Result<OuterOrigin, ()> {
use crate::BenchmarkHelper;
let community_id = T::BenchmarkHelper::community_id();
Ok(frame_system::RawOrigin::Signed(Pallet::<T>::community_account(&community_id)).into())
}
}
4 changes: 4 additions & 0 deletions runtime/kreivo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pallet-assets = {workspace = true}
pallet-aura = {workspace = true}
pallet-authorship = {workspace = true}
pallet-balances = {workspace = true}
pallet-contracts = {workspace = true}
pallet-multisig = {workspace = true}
pallet-nfts = {workspace = true}
pallet-preimage = {workspace = true}
Expand Down Expand Up @@ -132,6 +133,7 @@ std = [
"pallet-aura/std",
"pallet-authorship/std",
"pallet-balances/std",
"pallet-contracts/std",
"pallet-communities-manager/std",
"pallet-communities/std",
"pallet-collator-selection/std",
Expand Down Expand Up @@ -195,6 +197,7 @@ runtime-benchmarks = [
"pallet-asset-tx-payment/runtime-benchmarks",
"pallet-assets/runtime-benchmarks",
"pallet-balances/runtime-benchmarks",
"pallet-contracts/runtime-benchmarks",
"pallet-communities-manager/runtime-benchmarks",
"pallet-communities/runtime-benchmarks",
"pallet-collator-selection/runtime-benchmarks",
Expand Down Expand Up @@ -236,6 +239,7 @@ try-runtime = [
"pallet-aura/try-runtime",
"pallet-authorship/try-runtime",
"pallet-balances/try-runtime",
"pallet-contracts/try-runtime",
"pallet-communities-manager/try-runtime",
"pallet-communities/try-runtime",
"pallet-collator-selection/try-runtime",
Expand Down
107 changes: 107 additions & 0 deletions runtime/kreivo/src/contracts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
use crate::{
deposit, AccountId, Balance, Balances, Perbill, Runtime, RuntimeCall, RuntimeEvent, RuntimeHoldReason, Timestamp,
TreasuryAccount,
};
use frame_support::{
parameter_types,
traits::{ConstBool, ConstU32, EitherOf, Randomness},
};
use frame_system::{pallet_prelude::BlockNumberFor, EnsureRootWithSuccess};
use pallet_balances::Call as BalancesCall;
use pallet_communities::origin::AsSignedByStaticCommunity;
use sp_core::ConstU16;

pub enum AllowBalancesCall {}

impl frame_support::traits::Contains<RuntimeCall> for AllowBalancesCall {
fn contains(call: &RuntimeCall) -> bool {
matches!(call, RuntimeCall::Balances(BalancesCall::transfer_allow_death { .. }))
}
}

fn schedule<T: pallet_contracts::Config>() -> pallet_contracts::Schedule<T> {
pallet_contracts::Schedule {
limits: pallet_contracts::Limits {
runtime_memory: 1024 * 1024 * 1024,
..Default::default()
},
..Default::default()
}
}

// randomness-collective-flip is insecure. Provide dummy randomness as
// placeholder for the deprecated trait. https://github.com/paritytech/polkadot-sdk/blob/9bf1a5e23884921498b381728bfddaae93f83744/substrate/frame/contracts/mock-network/src/parachain/contracts_config.rs#L45
pub struct DummyRandomness<T: pallet_contracts::Config>(sp_std::marker::PhantomData<T>);

impl<T: pallet_contracts::Config> Randomness<T::Hash, BlockNumberFor<T>> for DummyRandomness<T> {
fn random(_subject: &[u8]) -> (T::Hash, BlockNumberFor<T>) {
(Default::default(), Default::default())
}
}

parameter_types! {
pub const DepositPerItem: Balance = deposit(1, 0);
pub const DepositPerByte: Balance = deposit(0, 1);
pub Schedule: pallet_contracts::Schedule<Runtime> = schedule::<Runtime>();
pub const DefaultDepositLimit: Balance = deposit(1024, 1024 * 1024);
pub const CodeHashLockupDepositPercent: Perbill = Perbill::from_percent(0);
}

impl pallet_contracts::Config for Runtime {
type Time = Timestamp;
type Randomness = DummyRandomness<Self>;
type Currency = Balances;
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;

type UploadOrigin = EnsureRootWithSuccess<AccountId, TreasuryAccount>;
type InstantiateOrigin = EitherOf<
EnsureRootWithSuccess<AccountId, TreasuryAccount>,
EitherOf<
AsSignedByStaticCommunity<Runtime, ConstU16<1>>, // Virto
AsSignedByStaticCommunity<Runtime, ConstU16<2>>, // Kippu
>,
>;

/// The safest default is to allow no calls at all.
///
/// Runtimes should whitelist dispatchables that are allowed to be called
/// from contracts and make sure they are stable. Dispatchables exposed to
/// contracts are not allowed to change because that would break already
/// deployed contracts. The `RuntimeCall` structure itself is not allowed
/// to change the indices of existing pallets, too.
type CallFilter = AllowBalancesCall;
type DepositPerItem = DepositPerItem;
type DepositPerByte = DepositPerByte;
type CallStack = [pallet_contracts::Frame<Self>; 23];
type WeightPrice = pallet_transaction_payment::Pallet<Self>;
type WeightInfo = pallet_contracts::weights::SubstrateWeight<Self>;
type Schedule = Schedule;
type AddressGenerator = pallet_contracts::DefaultAddressGenerator;

type ChainExtension = ();
type ApiVersion = ();

// ## From Pop Network
// This node is geared towards development and testing of contracts.
// We decided to increase the default allowed contract size for this
// reason (the default is `128 * 1024`).
//
// Our reasoning is that the error code `CodeTooLarge` is thrown
// if a too-large contract is uploaded. We noticed that it poses
// less friction during development when the requirement here is
// just more lax.
type MaxCodeLen = ConstU32<{ 256 * 1024 }>;
type DefaultDepositLimit = DefaultDepositLimit;
type MaxStorageKeyLen = ConstU32<128>;
type MaxDebugBufferLen = ConstU32<{ 2 * 1024 * 1024 }>;
type UnsafeUnstableInterface = ConstBool<true>;
type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent;
type MaxDelegateDependencies = ConstU32<32>;
type RuntimeHoldReason = RuntimeHoldReason;

type Environment = ();
type Debug = ();
type Migrations = ();
type Xcm = pallet_xcm::Pallet<Self>;
}
5 changes: 5 additions & 0 deletions runtime/kreivo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod tests;

pub mod apis;
pub mod constants;
pub mod contracts;
pub mod governance;
pub mod impls;
mod weights;
Expand Down Expand Up @@ -217,6 +218,9 @@ construct_runtime!(
CommunityReferenda: pallet_referenda::<Instance2> = 73,
CommunityMemberships: pallet_nfts::<Instance2> = 74,
CommunitiesManager: pallet_communities_manager = 75,

// Contracts
Contracts: pallet_contracts = 80,
}
);

Expand Down Expand Up @@ -759,6 +763,7 @@ mod benches {
[pallet_referenda, CommunityReferenda]
[pallet_communities_manager, CommunitiesManager]
[pallet_nfts, CommunityMemberships]
[pallet_contracts, Contracts]
// XCM
// NOTE: Make sure you point to the individual modules below.
[pallet_xcm_benchmarks::fungible, XcmBalances]
Expand Down

0 comments on commit dcdb7be

Please sign in to comment.