Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pallet contracts on Astar #891

Merged
merged 4 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion bin/collator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "astar-collator"
version = "5.1.0"
version = "5.3.0"
description = "Astar collator implementation in Rust."
build = "build.rs"
default-run = "astar-collator"
Expand Down
7 changes: 6 additions & 1 deletion runtime/astar/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "astar-runtime"
version = "5.2.0"
version = "5.3.0"
build = "build.rs"
authors.workspace = true
edition.workspace = true
Expand Down Expand Up @@ -41,6 +41,8 @@ pallet-aura = { workspace = true }
pallet-authorship = { workspace = true }
pallet-balances = { workspace = true }
pallet-base-fee = { workspace = true }
pallet-contracts = { workspace = true }
pallet-contracts-primitives = { workspace = true }
pallet-ethereum = { workspace = true }
pallet-evm = { workspace = true }
pallet-evm-precompile-blake2 = { workspace = true }
Expand Down Expand Up @@ -176,6 +178,8 @@ std = [
"cumulus-pallet-aura-ext/std",
"cumulus-pallet-dmp-queue/std",
"cumulus-pallet-xcmp-queue/std",
"pallet-contracts/std",
"pallet-contracts-primitives/std",
"cumulus-pallet-xcm/std",
"pallet-collator-selection/std",
"frame-benchmarking/std",
Expand Down Expand Up @@ -240,6 +244,7 @@ try-runtime = [
"cumulus-pallet-xcmp-queue/try-runtime",
"parachain-info/try-runtime",
"pallet-base-fee/try-runtime",
"pallet-contracts/try-runtime",
"pallet-evm/try-runtime",
]
evm-tracing = [
Expand Down
111 changes: 105 additions & 6 deletions runtime/astar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use frame_support::{
dispatch::DispatchClass,
parameter_types,
traits::{
AsEnsureOriginWithArg, ConstU32, Contains, Currency, FindAuthor, Get, Imbalance,
OnUnbalanced, WithdrawReasons,
AsEnsureOriginWithArg, ConstBool, ConstU32, Contains, Currency, FindAuthor, Get, Imbalance,
Nothing, OnUnbalanced, Randomness, WithdrawReasons,
},
weights::{
constants::{
Expand Down Expand Up @@ -57,6 +57,7 @@ use sp_runtime::{
traits::{
AccountIdConversion, AccountIdLookup, BlakeTwo256, Block as BlockT, Bounded, ConvertInto,
DispatchInfoOf, Dispatchable, OpaqueKeys, PostDispatchInfoOf, UniqueSaturatedInto, Verify,
Zero,
},
transaction_validity::{
TransactionPriority, TransactionSource, TransactionValidity, TransactionValidityError,
Expand Down Expand Up @@ -107,9 +108,7 @@ pub const fn deposit(items: u32, bytes: u32) -> Balance {
///
/// The slight difference to general `deposit` function is because there is fixed bound on how large the DB
/// key can grow so it doesn't make sense to have as high deposit per item as in the general approach.
///
/// TODO: use this when `pallet-contracts` is added to **Astar**
pub const fn _contracts_deposit(items: u32, bytes: u32) -> Balance {
pub const fn contracts_deposit(items: u32, bytes: u32) -> Balance {
items as Balance * 4 * MILLIASTR * INIT_SUPPLY_FACTOR + (bytes as Balance) * STORAGE_BYTE_FEE
}

Expand Down Expand Up @@ -140,7 +139,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("astar"),
impl_name: create_runtime_str!("astar"),
authoring_version: 1,
spec_version: 54,
spec_version: 55,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 2,
Expand Down Expand Up @@ -614,6 +613,55 @@ impl pallet_vesting::Config for Runtime {
const MAX_VESTING_SCHEDULES: u32 = 28;
}

parameter_types! {
pub const DepositPerItem: Balance = contracts_deposit(1, 0);
pub const DepositPerByte: Balance = contracts_deposit(0, 1);
// The lazy deletion runs inside on_initialize.
pub DeletionWeightLimit: Weight = AVERAGE_ON_INITIALIZE_RATIO *
RuntimeBlockWeights::get().max_block;
pub Schedule: pallet_contracts::Schedule<Runtime> = Default::default();
}

/// Codes using the randomness functionality cannot be uploaded. Neither can contracts
/// be instantiated from existing codes that use this deprecated functionality.
///
/// But since some `Randomness` config type is still required for `pallet-contracts`, we provide this dummy type.
pub struct DummyDeprecatedRandomness;
impl Randomness<Hash, BlockNumber> for DummyDeprecatedRandomness {
fn random(_: &[u8]) -> (Hash, BlockNumber) {
(Default::default(), Zero::zero())
}
}

impl pallet_contracts::Config for Runtime {
type Time = Timestamp;
type Randomness = DummyDeprecatedRandomness;
type Currency = Balances;
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
/// 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 `Call` structure itself
/// is not allowed to change the indices of existing pallets, too.
type CallFilter = Nothing;
type DepositPerItem = DepositPerItem;
type DepositPerByte = DepositPerByte;
type CallStack = [pallet_contracts::Frame<Self>; 5];
type WeightPrice = pallet_transaction_payment::Pallet<Self>;
type WeightInfo = pallet_contracts::weights::SubstrateWeight<Self>;
type ChainExtension = ();
type DeletionQueueDepth = ConstU32<128>;
type DeletionWeightLimit = DeletionWeightLimit;
type Schedule = Schedule;
type AddressGenerator = pallet_contracts::DefaultAddressGenerator;
type MaxCodeLen = ConstU32<{ 123 * 1024 }>;
type MaxStorageKeyLen = ConstU32<128>;
type UnsafeUnstableInterface = ConstBool<false>;
type MaxDebugBufferLen = ConstU32<{ 2 * 1024 * 1024 }>;
}

parameter_types! {
pub const TransactionByteFee: Balance = MILLIASTR / 100;
pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(25);
Expand Down Expand Up @@ -838,6 +886,8 @@ construct_runtime!(
EthCall: pallet_custom_signatures = 62,
BaseFee: pallet_base_fee = 63,

Contracts: pallet_contracts = 70,

Sudo: pallet_sudo = 99,
}
);
Expand Down Expand Up @@ -1246,6 +1296,55 @@ impl_runtime_apis! {
}
}

impl pallet_contracts::ContractsApi<
Block, AccountId, Balance, BlockNumber, Hash,
>
for Runtime
{
fn call(
origin: AccountId,
dest: AccountId,
value: Balance,
gas_limit: Option<Weight>,
storage_deposit_limit: Option<Balance>,
input_data: Vec<u8>,
) -> pallet_contracts_primitives::ContractExecResult<Balance> {
let gas_limit = gas_limit.unwrap_or(RuntimeBlockWeights::get().max_block);
Contracts::bare_call(origin, dest, value, gas_limit, storage_deposit_limit, input_data, true, pallet_contracts::Determinism::Deterministic)
}

fn instantiate(
origin: AccountId,
value: Balance,
gas_limit: Option<Weight>,
storage_deposit_limit: Option<Balance>,
code: pallet_contracts_primitives::Code<Hash>,
data: Vec<u8>,
salt: Vec<u8>,
) -> pallet_contracts_primitives::ContractInstantiateResult<AccountId, Balance>
{
let gas_limit = gas_limit.unwrap_or(RuntimeBlockWeights::get().max_block);
Contracts::bare_instantiate(origin, value, gas_limit, storage_deposit_limit, code, data, salt, true)
}

fn upload_code(
origin: AccountId,
code: Vec<u8>,
storage_deposit_limit: Option<Balance>,
determinism: pallet_contracts::Determinism,
) -> pallet_contracts_primitives::CodeUploadResult<Hash, Balance>
{
Contracts::bare_upload_code(origin, code, storage_deposit_limit, determinism)
}

fn get_storage(
address: AccountId,
key: Vec<u8>,
) -> pallet_contracts_primitives::GetStorageResult {
Contracts::get_storage(address, key)
}
}

#[cfg(feature = "runtime-benchmarks")]
impl frame_benchmarking::Benchmark<Block> for Runtime {
fn benchmark_metadata(extra: bool) -> (
Expand Down
2 changes: 1 addition & 1 deletion runtime/local/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "local-runtime"
version = "5.2.0"
version = "5.3.0"
build = "build.rs"
authors.workspace = true
edition.workspace = true
Expand Down
1 change: 0 additions & 1 deletion runtime/local/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,6 @@ impl pallet_vesting::Config for Runtime {
parameter_types! {
pub const DepositPerItem: Balance = deposit(1, 0);
pub const DepositPerByte: Balance = deposit(0, 1);
pub const MaxValueSize: u32 = 16 * 1024;
PierreOssun marked this conversation as resolved.
Show resolved Hide resolved
// The lazy deletion runs inside on_initialize.
pub DeletionWeightLimit: Weight = AVERAGE_ON_INITIALIZE_RATIO *
RuntimeBlockWeights::get().max_block;
Expand Down
2 changes: 1 addition & 1 deletion runtime/shibuya/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shibuya-runtime"
version = "5.2.0"
version = "5.3.0"
build = "build.rs"
authors.workspace = true
edition.workspace = true
Expand Down
11 changes: 2 additions & 9 deletions runtime/shibuya/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ use frame_support::{
parameter_types,
traits::{
AsEnsureOriginWithArg, ConstU32, Contains, Currency, EitherOfDiverse, EqualPrivilegeOnly,
FindAuthor, Get, GetStorageVersion, Imbalance, InstanceFilter, Nothing, OnUnbalanced,
WithdrawReasons,
FindAuthor, Get, Imbalance, InstanceFilter, Nothing, OnUnbalanced, WithdrawReasons,
},
weights::{
constants::{
Expand Down Expand Up @@ -170,7 +169,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("shibuya"),
impl_name: create_runtime_str!("shibuya"),
authoring_version: 1,
spec_version: 94,
spec_version: 95,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 2,
Expand Down Expand Up @@ -246,11 +245,6 @@ impl Contains<RuntimeCall> for BaseFilter {
// registering the asset location should be good enough for users, any change can be handled via issue ticket or help request
_ => false,
},
RuntimeCall::Contracts(_) => {
// We block the calls until storage migration has been finished.
// The DB read weight is already accounted for in the migration pallet's `on_initialize` function.
<pallet_contracts::Pallet<Runtime>>::on_chain_storage_version() == 9
}
// These modules are not allowed to be called by transactions:
// Other modules should works:
_ => true,
Expand Down Expand Up @@ -668,7 +662,6 @@ impl pallet_vesting::Config for Runtime {
parameter_types! {
pub const DepositPerItem: Balance = MILLISBY / 1_000_000;
pub const DepositPerByte: Balance = MILLISBY / 1_000_000;
pub const MaxValueSize: u32 = 16 * 1024;
// The lazy deletion runs inside on_initialize.
pub DeletionWeightLimit: Weight = AVERAGE_ON_INITIALIZE_RATIO *
RuntimeBlockWeights::get().max_block;
Expand Down
2 changes: 1 addition & 1 deletion runtime/shiden/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shiden-runtime"
version = "5.2.0"
version = "5.3.0"
build = "build.rs"
authors.workspace = true
edition.workspace = true
Expand Down
12 changes: 3 additions & 9 deletions runtime/shiden/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use frame_support::{
dispatch::DispatchClass,
parameter_types,
traits::{
AsEnsureOriginWithArg, ConstU32, Contains, Currency, FindAuthor, Get, GetStorageVersion,
InstanceFilter, Nothing, OnUnbalanced, WithdrawReasons,
AsEnsureOriginWithArg, ConstU32, Contains, Currency, FindAuthor, Get, InstanceFilter,
Nothing, OnUnbalanced, WithdrawReasons,
},
weights::{
constants::{
Expand Down Expand Up @@ -139,7 +139,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("shiden"),
impl_name: create_runtime_str!("shiden"),
authoring_version: 1,
spec_version: 92,
spec_version: 93,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 2,
Expand Down Expand Up @@ -209,11 +209,6 @@ impl Contains<RuntimeCall> for BaseFilter {

_ => true,
},
RuntimeCall::Contracts(_) => {
// We block the calls until storage migration has been finished.
// The DB read weight is already accounted for in the migration pallet's `on_initialize` function.
<pallet_contracts::Pallet<Runtime>>::on_chain_storage_version() == 9
}
// These modules are not allowed to be called by transactions:
// To leave collator just shutdown it, next session funds will be released
// Other modules should works:
Expand Down Expand Up @@ -623,7 +618,6 @@ impl pallet_vesting::Config for Runtime {
parameter_types! {
pub const DepositPerItem: Balance = MILLISDN / 1_000_000;
pub const DepositPerByte: Balance = MILLISDN / 1_000_000;
pub const MaxValueSize: u32 = 16 * 1024;
PierreOssun marked this conversation as resolved.
Show resolved Hide resolved
// The lazy deletion runs inside on_initialize.
pub DeletionWeightLimit: Weight = AVERAGE_ON_INITIALIZE_RATIO *
RuntimeBlockWeights::get().max_block;
Expand Down