Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

contracts: Add storage deposits #10082

Merged
merged 45 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
27599f9
Frame no longer needs to be mutable (refactoring artifact)
athei Sep 16, 2021
d3abb6f
Remove Contract/Tombstone deposit
athei Sep 17, 2021
8b92865
Add StorageMeter
athei Sep 20, 2021
b91e08a
cargo fmt
athei Nov 7, 2021
bc06dbd
Fix weight annotation
athei Nov 7, 2021
a60e70a
Merge branch 'master' into at-storage-deposit
athei Nov 19, 2021
7d76812
Merge branch 'master' of https://github.com/paritytech/substrate into…
Nov 19, 2021
77015a2
cargo run --quiet --release --features=runtime-benchmarks --manifest-…
Nov 19, 2021
a509d78
Merge branch 'master' into at-storage-deposit
athei Nov 21, 2021
69a1793
Simplify keep check for contract accounts
athei Nov 21, 2021
8f5d92b
Remove unused imports and functions
athei Nov 21, 2021
075a013
Merge branch 'master' into at-storage-deposit
athei Nov 25, 2021
0929c0b
Rename storage_limit to storage_deposit_limit
athei Nov 25, 2021
6489f7f
cargo fmt
athei Nov 25, 2021
4d0bb09
Fix typo
athei Nov 26, 2021
2b1723b
Finish up rename of storage_limit
athei Nov 26, 2021
a8f24e8
Fix rpc tests
athei Nov 26, 2021
23474cf
Make use of `StorageDepositLimitTooHigh`
athei Nov 26, 2021
38bb5c2
Add tests and fix bugs discovered by tests
athei Nov 26, 2021
a997978
Merge branch 'master' into at-storage-deposit
athei Nov 28, 2021
1ea021b
Add storage migration
athei Nov 28, 2021
0420871
Don't use u128 in RPC
athei Nov 29, 2021
23674c9
Merge branch 'master' into at-storage-deposit
athei Nov 29, 2021
7887161
Fix weight of migration
athei Nov 29, 2021
8a10478
Rename `endowment` to `value`
athei Nov 29, 2021
434f487
Fix bug where contract couldn't get funded by a storage deposit
athei Nov 30, 2021
bc6c8bf
Merge branch 'master' into at-storage-deposit
athei Dec 1, 2021
bddf165
Apply suggestions from code review
athei Dec 2, 2021
e23c494
Remove unused `fn storage_meter`
athei Dec 2, 2021
5ccd491
Fix copy pasta doc error
athei Dec 2, 2021
f878644
Import `MaxEncodeLen` from codec
athei Dec 2, 2021
c428ea9
Beautify RPC trait bounds
athei Dec 2, 2021
05d275b
Add re-instrument behaviour to dispatchable doc
athei Dec 2, 2021
450cd68
Merge branch 'master' into at-storage-deposit
athei Dec 2, 2021
3a1391a
Make sure a account won't be destroyed a refund after a slash
athei Dec 3, 2021
cee32d3
Merge branch 'master' into at-storage-deposit
athei Dec 3, 2021
c020184
Merge branch 'master' into at-storage-deposit
athei Dec 6, 2021
d6e3295
Apply suggestions from code review
athei Dec 7, 2021
9435231
Update `Storage::write` docs
athei Dec 7, 2021
18de747
Improve doc
athei Dec 7, 2021
9d92ccb
Remove superflous conditional
athei Dec 7, 2021
a3e32a5
Merge branch 'master' into at-storage-deposit
athei Dec 7, 2021
0fdf0cd
Typos
athei Dec 7, 2021
e09c89e
Remove superflous clone (refactoring artifact)
athei Dec 7, 2021
a92d39d
Apply suggestions from code review
athei Dec 7, 2021
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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions bin/node/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ sc-executor = { version = "0.10.0-dev", path = "../../../client/executor" }
sp-core = { version = "4.0.0-dev", path = "../../../primitives/core" }
sp-keystore = { version = "0.10.0-dev", path = "../../../primitives/keystore" }
sp-state-machine = { version = "0.10.0-dev", path = "../../../primitives/state-machine" }
sp-tracing = { version = "4.0.0-dev", path = "../../../primitives/tracing" }
sp-trie = { version = "4.0.0-dev", path = "../../../primitives/trie" }
frame-benchmarking = { version = "4.0.0-dev", path = "../../../frame/benchmarking" }

Expand Down
6 changes: 4 additions & 2 deletions bin/node/executor/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ fn deploying_wasm_contract_should_work() {

let addr = pallet_contracts::Pallet::<Runtime>::contract_address(&charlie(), &transfer_ch, &[]);

let subsistence = pallet_contracts::Pallet::<Runtime>::subsistence_threshold();
let min_balance = <Runtime as pallet_contracts::Config>::Currency::minimum_balance();

let time = 42 * 1000;
let b = construct_block(
Expand All @@ -700,8 +700,9 @@ fn deploying_wasm_contract_should_work() {
signed: Some((charlie(), signed_extra(0, 0))),
function: Call::Contracts(
pallet_contracts::Call::instantiate_with_code::<Runtime> {
endowment: 1000 * DOLLARS + subsistence,
endowment: 1000 * DOLLARS + min_balance,
gas_limit: 500_000_000,
storage_limit: None,
code: transfer_code,
data: Vec::new(),
salt: Vec::new(),
Expand All @@ -714,6 +715,7 @@ fn deploying_wasm_contract_should_work() {
dest: sp_runtime::MultiAddress::Id(addr.clone()),
value: 10,
gas_limit: 500_000_000,
storage_limit: None,
data: vec![0x00, 0x01, 0x02, 0x03],
}),
},
Expand Down
2 changes: 1 addition & 1 deletion bin/node/executor/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub fn executor_call<
hash: sp_core::blake2_256(&code).to_vec(),
heap_pages: heap_pages.and_then(|hp| Decode::decode(&mut &hp[..]).ok()),
};

sp_tracing::try_init_simple();
executor().call::<R, NC>(&mut t, &runtime_code, method, data, use_native, native_call)
}

Expand Down
28 changes: 19 additions & 9 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,10 +895,8 @@ impl pallet_tips::Config for Runtime {
}

parameter_types! {
pub ContractDeposit: Balance = deposit(
1,
<pallet_contracts::Pallet<Runtime>>::contract_info_size(),
);
pub const DepositPerItem: Balance = deposit(1, 0);
pub const DepositPerByte: Balance = deposit(0, 1);
pub const MaxValueSize: u32 = 16 * 1024;
// The lazy deletion runs inside on_initialize.
pub DeletionWeightLimit: Weight = AVERAGE_ON_INITIALIZE_RATIO *
Expand All @@ -925,7 +923,8 @@ impl pallet_contracts::Config for Runtime {
/// 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 ContractDeposit = ContractDeposit;
type DepositPerItem = DepositPerItem;
type DepositPerByte = DepositPerByte;
type CallStack = [pallet_contracts::Frame<Self>; 31];
type WeightPrice = pallet_transaction_payment::Pallet<Self>;
type WeightInfo = pallet_contracts::weights::SubstrateWeight<Self>;
Expand Down Expand Up @@ -1518,21 +1517,32 @@ impl_runtime_apis! {
dest: AccountId,
value: Balance,
gas_limit: u64,
storage_limit: Option<Balance>,
input_data: Vec<u8>,
) -> pallet_contracts_primitives::ContractExecResult {
Contracts::bare_call(origin, dest, value, gas_limit, input_data, true)
) -> pallet_contracts_primitives::ContractExecResult<Balance> {
Contracts::bare_call(origin, dest, value, gas_limit, storage_limit, input_data, true)
}

fn instantiate(
origin: AccountId,
endowment: Balance,
gas_limit: u64,
storage_limit: Option<Balance>,
code: pallet_contracts_primitives::Code<Hash>,
data: Vec<u8>,
salt: Vec<u8>,
) -> pallet_contracts_primitives::ContractInstantiateResult<AccountId>
) -> pallet_contracts_primitives::ContractInstantiateResult<AccountId, Balance>
{
Contracts::bare_instantiate(origin, endowment, gas_limit, storage_limit, code, data, salt, true)
}

fn upload_code(
origin: AccountId,
code: Vec<u8>,
storage_limit: Option<Balance>,
) -> pallet_contracts_primitives::CodeUploadResult<Hash, Balance>
{
Contracts::bare_instantiate(origin, endowment, gas_limit, code, data, salt, true)
Contracts::bare_upload_code(origin, code, storage_limit)
}

fn get_storage(
Expand Down
132 changes: 126 additions & 6 deletions frame/contracts/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
use bitflags::bitflags;
use codec::{Decode, Encode};
use sp_core::Bytes;
use sp_runtime::{DispatchError, RuntimeDebug};
use sp_runtime::{
traits::{Saturating, Zero},
DispatchError, RuntimeDebug,
};
use sp_std::prelude::*;

#[cfg(feature = "std")]
Expand All @@ -34,7 +37,7 @@ use serde::{Deserialize, Serialize};
#[derive(Eq, PartialEq, Encode, Decode, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
pub struct ContractResult<T> {
pub struct ContractResult<R, Balance> {
/// How much gas was consumed during execution.
pub gas_consumed: u64,
/// How much gas is required as gas limit in order to execute this call.
Expand All @@ -45,7 +48,14 @@ pub struct ContractResult<T> {
///
/// This can only different from [`Self::gas_consumed`] when weight pre charging
/// is used. Currently, only `seal_call_runtime` makes use of pre charging.
/// Additionally, any `seal_call` or `seal_instantiate` makes use of pre-charging
/// when a non-zero `gas_limit` argument is supplied.
pub gas_required: u64,
/// How much balance was deposited and reserved during execution in order to pay for storage.
///
/// The storage deposit is never actually charged from the caller in case of [`Self::result`]
/// is `Err`. This is because on error all storage changes are rolled back.
pub storage_deposit: StorageDeposit<Balance>,
/// An optional debug message. This message is only filled when explicitly requested
/// by the code that calls into the contract. Otherwise it is empty.
///
Expand All @@ -63,15 +73,20 @@ pub struct ContractResult<T> {
#[cfg_attr(feature = "std", serde(with = "as_string"))]
pub debug_message: Vec<u8>,
/// The execution result of the wasm code.
pub result: T,
pub result: R,
}

/// Result type of a `bare_call` call.
pub type ContractExecResult = ContractResult<Result<ExecReturnValue, DispatchError>>;
pub type ContractExecResult<Balance> =
ContractResult<Result<ExecReturnValue, DispatchError>, Balance>;

/// Result type of a `bare_instantiate` call.
pub type ContractInstantiateResult<AccountId> =
ContractResult<Result<InstantiateReturnValue<AccountId>, DispatchError>>;
pub type ContractInstantiateResult<AccountId, Balance> =
ContractResult<Result<InstantiateReturnValue<AccountId>, DispatchError>, Balance>;

/// Result type of a `bare_code_upload` call.
pub type CodeUploadResult<CodeHash, Balance> =
Result<CodeUploadReturnValue<CodeHash, Balance>, DispatchError>;

/// Result type of a `get_storage` call.
pub type GetStorageResult = Result<Option<Vec<u8>>, ContractAccessError>;
Expand Down Expand Up @@ -123,6 +138,17 @@ pub struct InstantiateReturnValue<AccountId> {
pub account_id: AccountId,
}

/// The result of succesfully uploading a contract.
#[derive(PartialEq, Eq, Encode, Decode, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
pub struct CodeUploadReturnValue<CodeHash, Balance> {
/// The key under which the new code is stored.
pub code_hash: CodeHash,
/// The deposit that was reserved at the caller. Is zero when the code already existed.
pub deposit: Balance,
}

/// Reference to an existing code hash or a new wasm module.
#[derive(Eq, PartialEq, Encode, Decode, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
Expand All @@ -134,6 +160,100 @@ pub enum Code<Hash> {
Existing(Hash),
}

/// The amount of balance that was either charged or refunded in order to pay for storage.
#[derive(Eq, PartialEq, Ord, PartialOrd, Encode, Decode, RuntimeDebug, Clone)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
pub enum StorageDeposit<Balance> {
/// The transaction reduced storage consumption.
///
/// This means that the specified amount of balance was transferred from the involved
/// contracts to the call origin.
Refund(Balance),
/// The transaction increased overall storage usage.
///
/// This means that the specified amount of balance was transferred from the call origin
/// to the contracts involved.
Charge(Balance),
}

impl<Balance: Zero> Default for StorageDeposit<Balance> {
fn default() -> Self {
Self::Charge(Zero::zero())
}
}

impl<Balance: Zero> StorageDeposit<Balance> {
/// Returns how much balance is charged or `0` in case of a refund.
pub fn charge_or_zero(self) -> Balance {
match self {
Self::Charge(amount) => amount,
Self::Refund(_) => Zero::zero(),
}
}
}

impl<Balance> StorageDeposit<Balance>
where
Balance: Saturating + Ord + Copy,
{
/// This is essentially a saturating signed add.
pub fn saturating_add(&self, rhs: &Self) -> Self {
use StorageDeposit::*;
match (self, rhs) {
(Charge(lhs), Charge(rhs)) => Charge(lhs.saturating_add(*rhs)),
(Refund(lhs), Refund(rhs)) => Refund(lhs.saturating_add(*rhs)),
(Charge(lhs), Refund(rhs)) =>
if lhs >= rhs {
Charge(lhs.saturating_sub(*rhs))
} else {
Refund(rhs.saturating_sub(*lhs))
},
(Refund(lhs), Charge(rhs)) =>
if lhs > rhs {
Refund(lhs.saturating_sub(*rhs))
} else {
Charge(rhs.saturating_sub(*lhs))
},
}
}

/// This is essentially a saturating signed sub.
pub fn saturating_sub(&self, rhs: &Self) -> Self {
use StorageDeposit::*;
match (self, rhs) {
(Charge(lhs), Refund(rhs)) => Charge(lhs.saturating_add(*rhs)),
(Refund(lhs), Charge(rhs)) => Refund(lhs.saturating_add(*rhs)),
(Charge(lhs), Charge(rhs)) =>
if lhs >= rhs {
Charge(lhs.saturating_sub(*rhs))
} else {
Refund(rhs.saturating_sub(*lhs))
},
(Refund(lhs), Refund(rhs)) =>
if lhs > rhs {
Refund(lhs.saturating_sub(*rhs))
} else {
Charge(rhs.saturating_sub(*lhs))
},
}
}

/// If the amount of deposit (this type) is constrained by a `limit` this calcuates how
/// much balance (if any) is still available from this limit.
///
/// # Note
///
/// In case of a refund the return value can be larger than `limit`.
pub fn available(&self, limit: &Balance) -> Balance {
use StorageDeposit::*;
match self {
Charge(amount) => limit.saturating_sub(*amount),
Refund(amount) => limit.saturating_add(*amount),
}
}
}

#[cfg(feature = "std")]
mod as_string {
use super::*;
Expand Down
6 changes: 4 additions & 2 deletions frame/contracts/fixtures/drain.wat
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
)
)

;; Self-destruct by sending full balance to the 0 address.
;; Try to self-destruct by sending full balance to the 0 address.
;; All the free balance will be send away but the contract's which is a valid
;; thing to do because the storage deposits will keep the account alive.
athei marked this conversation as resolved.
Show resolved Hide resolved
(call $assert
(i32.eq
(call $seal_transfer
Expand All @@ -42,7 +44,7 @@
(i32.const 0) ;; Pointer to the buffer with value to transfer
(i32.const 8) ;; Length of the buffer with value to transfer
)
(i32.const 4) ;; ReturnCode::BelowSubsistenceThreshold
(i32.const 0) ;; ReturnCode::Success
)
)
)
Expand Down
18 changes: 15 additions & 3 deletions frame/contracts/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

use codec::Codec;
use pallet_contracts_primitives::{
Code, ContractExecResult, ContractInstantiateResult, GetStorageResult,
Code, CodeUploadResult, ContractExecResult, ContractInstantiateResult, GetStorageResult,
};
use sp_std::vec::Vec;

Expand All @@ -45,8 +45,9 @@ sp_api::decl_runtime_apis! {
dest: AccountId,
value: Balance,
gas_limit: u64,
storage_limit: Option<Balance>,
input_data: Vec<u8>,
) -> ContractExecResult;
) -> ContractExecResult<Balance>;

/// Instantiate a new contract.
///
Expand All @@ -55,10 +56,21 @@ sp_api::decl_runtime_apis! {
origin: AccountId,
endowment: Balance,
gas_limit: u64,
storage_limit: Option<Balance>,
code: Code<Hash>,
data: Vec<u8>,
salt: Vec<u8>,
) -> ContractInstantiateResult<AccountId>;
) -> ContractInstantiateResult<AccountId, Balance>;


/// Upload new code without instantiating a contract from it.
///
/// See `pallet_contracts::Pallet::upload_code`.
fn upload_code(
origin: AccountId,
code: Vec<u8>,
storage_limit: Option<Balance>,
) -> CodeUploadResult<Hash, Balance>;

/// Query a given storage key in a given contract.
///
Expand Down
Loading