diff --git a/Cargo.lock b/Cargo.lock index a017939a..181e289b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4673,6 +4673,7 @@ dependencies = [ "pallet-collator-selection", "pallet-communities", "pallet-communities-manager", + "pallet-contracts", "pallet-message-queue", "pallet-multisig", "pallet-nfts", @@ -6711,6 +6712,61 @@ dependencies = [ "virto-common", ] +[[package]] +name = "pallet-contracts" +version = "27.0.0" +source = "git+https://github.com/virto-network/polkadot-sdk?branch=release-virto-v1.12.0#a5ec6b76cbb4aad28244fa61341856cc823d57bc" +dependencies = [ + "bitflags 1.3.2", + "environmental", + "frame-benchmarking", + "frame-support", + "frame-system", + "impl-trait-for-tuples", + "log", + "pallet-balances", + "pallet-contracts-proc-macro", + "pallet-contracts-uapi", + "parity-scale-codec", + "paste", + "rand 0.8.5", + "rand_pcg", + "scale-info", + "serde", + "smallvec", + "sp-api", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std 14.0.0 (git+https://github.com/virto-network/polkadot-sdk?branch=release-virto-v1.12.0)", + "staging-xcm", + "staging-xcm-builder", + "wasm-instrument", + "wasmi", +] + +[[package]] +name = "pallet-contracts-proc-macro" +version = "18.0.0" +source = "git+https://github.com/virto-network/polkadot-sdk?branch=release-virto-v1.12.0#a5ec6b76cbb4aad28244fa61341856cc823d57bc" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", +] + +[[package]] +name = "pallet-contracts-uapi" +version = "5.0.0" +source = "git+https://github.com/virto-network/polkadot-sdk?branch=release-virto-v1.12.0#a5ec6b76cbb4aad28244fa61341856cc823d57bc" +dependencies = [ + "bitflags 1.3.2", + "parity-scale-codec", + "paste", + "polkavm-derive", + "scale-info", +] + [[package]] name = "pallet-conviction-voting" version = "28.0.0" @@ -12485,7 +12541,7 @@ dependencies = [ [[package]] name = "sp-crypto-ec-utils" version = "0.10.0" -source = "git+https://github.com/paritytech/polkadot-sdk#c4b3c1c6c6e492c4196e06fbba824a58e8119a3b" +source = "git+https://github.com/paritytech/polkadot-sdk#de41ae85ec600189c4621aaf9e58afc612f101f7" dependencies = [ "ark-bls12-377", "ark-bls12-377-ext", @@ -12547,7 +12603,7 @@ dependencies = [ [[package]] name = "sp-debug-derive" version = "14.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#c4b3c1c6c6e492c4196e06fbba824a58e8119a3b" +source = "git+https://github.com/paritytech/polkadot-sdk#de41ae85ec600189c4621aaf9e58afc612f101f7" dependencies = [ "proc-macro2", "quote", @@ -12567,7 +12623,7 @@ dependencies = [ [[package]] name = "sp-externalities" version = "0.25.0" -source = "git+https://github.com/paritytech/polkadot-sdk#c4b3c1c6c6e492c4196e06fbba824a58e8119a3b" +source = "git+https://github.com/paritytech/polkadot-sdk#de41ae85ec600189c4621aaf9e58afc612f101f7" dependencies = [ "environmental", "parity-scale-codec", @@ -12783,7 +12839,7 @@ dependencies = [ [[package]] name = "sp-runtime-interface" version = "24.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#c4b3c1c6c6e492c4196e06fbba824a58e8119a3b" +source = "git+https://github.com/paritytech/polkadot-sdk#de41ae85ec600189c4621aaf9e58afc612f101f7" dependencies = [ "bytes", "impl-trait-for-tuples", @@ -12815,7 +12871,7 @@ dependencies = [ [[package]] name = "sp-runtime-interface-proc-macro" version = "17.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#c4b3c1c6c6e492c4196e06fbba824a58e8119a3b" +source = "git+https://github.com/paritytech/polkadot-sdk#de41ae85ec600189c4621aaf9e58afc612f101f7" dependencies = [ "Inflector", "expander", @@ -12904,7 +12960,7 @@ source = "git+https://github.com/virto-network/polkadot-sdk?branch=release-virto [[package]] name = "sp-std" version = "14.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#c4b3c1c6c6e492c4196e06fbba824a58e8119a3b" +source = "git+https://github.com/paritytech/polkadot-sdk#de41ae85ec600189c4621aaf9e58afc612f101f7" [[package]] name = "sp-storage" @@ -12921,7 +12977,7 @@ dependencies = [ [[package]] name = "sp-storage" version = "19.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#c4b3c1c6c6e492c4196e06fbba824a58e8119a3b" +source = "git+https://github.com/paritytech/polkadot-sdk#de41ae85ec600189c4621aaf9e58afc612f101f7" dependencies = [ "impl-serde", "parity-scale-codec", @@ -12956,7 +13012,7 @@ dependencies = [ [[package]] name = "sp-tracing" version = "16.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#c4b3c1c6c6e492c4196e06fbba824a58e8119a3b" +source = "git+https://github.com/paritytech/polkadot-sdk#de41ae85ec600189c4621aaf9e58afc612f101f7" dependencies = [ "parity-scale-codec", "tracing", @@ -13053,7 +13109,7 @@ dependencies = [ [[package]] name = "sp-wasm-interface" version = "20.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#c4b3c1c6c6e492c4196e06fbba824a58e8119a3b" +source = "git+https://github.com/paritytech/polkadot-sdk#de41ae85ec600189c4621aaf9e58afc612f101f7" dependencies = [ "impl-trait-for-tuples", "log", diff --git a/pallets/communities/src/origin.rs b/pallets/communities/src/origin.rs index ef69997b..1746279f 100644 --- a/pallets/communities/src/origin.rs +++ b/pallets/communities/src/origin.rs @@ -224,3 +224,35 @@ where Ok(frame_system::RawOrigin::Signed(Pallet::::community_account(&community_id)).into()) } } + +/// Ensure the origin is any `Signed` origin. +pub struct AsSignedByStaticCommunity(PhantomData<(T, C)>); +impl EnsureOrigin for AsSignedByStaticCommunity +where + OuterOrigin: OriginTrait + + From> + + From> + + Clone + + Into, OuterOrigin>> + + Into, OuterOrigin>>, + T: Config, + C: Get>, +{ + type Success = T::AccountId; + + fn try_origin(o: OuterOrigin) -> Result { + match o.clone().into() { + Ok(RawOrigin { ref community_id, .. }) if community_id == &C::get() => { + Ok(Pallet::::community_account(community_id)) + } + _ => Err(o.clone()), + } + } + + #[cfg(feature = "runtime-benchmarks")] + fn try_successful_origin() -> Result { + use crate::BenchmarkHelper; + let community_id = T::BenchmarkHelper::community_id(); + Ok(frame_system::RawOrigin::Signed(Pallet::::community_account(&community_id)).into()) + } +} diff --git a/runtime/kreivo/Cargo.toml b/runtime/kreivo/Cargo.toml index b7847c5d..29b4c052 100644 --- a/runtime/kreivo/Cargo.toml +++ b/runtime/kreivo/Cargo.toml @@ -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} @@ -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", @@ -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", @@ -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", diff --git a/runtime/kreivo/src/contracts.rs b/runtime/kreivo/src/contracts.rs new file mode 100644 index 00000000..ec3ec2f8 --- /dev/null +++ b/runtime/kreivo/src/contracts.rs @@ -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 for AllowBalancesCall { + fn contains(call: &RuntimeCall) -> bool { + matches!(call, RuntimeCall::Balances(BalancesCall::transfer_allow_death { .. })) + } +} + +fn schedule() -> pallet_contracts::Schedule { + 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(sp_std::marker::PhantomData); + +impl Randomness> for DummyRandomness { + fn random(_subject: &[u8]) -> (T::Hash, BlockNumberFor) { + (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 = schedule::(); + 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; + type Currency = Balances; + type RuntimeEvent = RuntimeEvent; + type RuntimeCall = RuntimeCall; + + type UploadOrigin = EnsureRootWithSuccess; + type InstantiateOrigin = EitherOf< + EnsureRootWithSuccess, + EitherOf< + AsSignedByStaticCommunity>, // Virto + AsSignedByStaticCommunity>, // 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; 23]; + type WeightPrice = pallet_transaction_payment::Pallet; + type WeightInfo = pallet_contracts::weights::SubstrateWeight; + 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; + type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent; + type MaxDelegateDependencies = ConstU32<32>; + type RuntimeHoldReason = RuntimeHoldReason; + + type Environment = (); + type Debug = (); + type Migrations = (); + type Xcm = pallet_xcm::Pallet; +} diff --git a/runtime/kreivo/src/lib.rs b/runtime/kreivo/src/lib.rs index 22c34611..b46216e3 100644 --- a/runtime/kreivo/src/lib.rs +++ b/runtime/kreivo/src/lib.rs @@ -12,6 +12,7 @@ mod tests; pub mod apis; pub mod constants; +pub mod contracts; pub mod governance; pub mod impls; mod weights; @@ -217,6 +218,9 @@ construct_runtime!( CommunityReferenda: pallet_referenda:: = 73, CommunityMemberships: pallet_nfts:: = 74, CommunitiesManager: pallet_communities_manager = 75, + + // Contracts + Contracts: pallet_contracts = 80, } ); @@ -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]