Skip to content

Commit

Permalink
Remove the state rent API (#1036)
Browse files Browse the repository at this point in the history
* Remove the state rent API

* Apply `cargo fmt`, make clippy happy

* Make clippy happy

* Remove unused `sp-arithmetic` dep

* Remove `RentFraction` and related deps

* Update test fixtures
  • Loading branch information
cmichi authored Nov 24, 2021
1 parent d2f19d9 commit 9ea95ff
Show file tree
Hide file tree
Showing 32 changed files with 36 additions and 1,081 deletions.
3 changes: 3 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

This is the 7th release candidate for ink! 3.0.

## Removed
- Removed the state rent API ‒ [#1036](https://github.com/paritytech/ink/pull/1036).

## Added
- The ink! codegen now heavily relies on static type information based on traits defined in `ink_lang`.
- Some of those traits and their carried information can be used for static reflection of ink!
Expand Down
45 changes: 2 additions & 43 deletions crates/engine/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ use crate::{
DebugInfo,
EmittedEvent,
},
types::{
AccountId,
Key,
},
types::AccountId,
};
use std::panic::panic_any;

Expand Down Expand Up @@ -89,7 +86,7 @@ define_error_codes! {
NewContractNotFunded = 6,
/// No code could be found at the supplied code hash.
CodeNotFound = 7,
/// The account that was called is either no contract (e.g. user account) or is a tombstone.
/// The account that was called is no contract, but a plain account.
NotCallable = 8,
/// The call to `seal_debug_message` had no effect because debug message
/// recording was disabled.
Expand Down Expand Up @@ -244,9 +241,6 @@ impl Engine {
self.transfer(beneficiary, value)
.expect("transfer did not work");

// What is currently missing is to set a tombstone with a code hash here
// and remove the contract storage subsequently.

// Encode the result of the termination and panic with it.
// This enables testing for the proper result and makes sure this
// method returns `Never`.
Expand Down Expand Up @@ -299,29 +293,6 @@ impl Engine {
set_output(output, callee)
}

/// Restores a tombstone to the original smart contract.
///
/// # Params
///
/// - `account_id`: Encoded bytes of the `AccountId` of the to-be-restored contract.
/// - `code_hash`: Encoded code hash of the to-be-restored contract.
/// - `rent_allowance`: The encoded rent allowance of the restored contract
/// upon successful restoration.
/// - `filtered_keys`: Storage keys that will be ignored for the tombstone hash
/// match calculation that decide whether the original contract
/// storage and the storage of the restorer contract is equal.
pub fn restore_to(
&mut self,
_account_id: &[u8],
_code_hash: &[u8],
_rent_allowance: &[u8],
filtered_keys: &[&[u8]],
) {
let _filtered_keys: Vec<crate::Key> =
filtered_keys.iter().map(|k| Key::from_bytes(k)).collect();
unimplemented!("off-chain environment does not yet support `restore_to`");
}

/// Records the given debug message and appends to stdout.
pub fn debug_message(&mut self, message: &str) {
self.debug_info.record_debug_message(String::from(message));
Expand Down Expand Up @@ -360,18 +331,10 @@ impl Engine {
unimplemented!("off-chain environment does not yet support `gas_left`");
}

pub fn rent_allowance(&self, _output: &mut &mut [u8]) {
unimplemented!("off-chain environment does not yet support `rent_allowance`");
}

pub fn minimum_balance(&self, _output: &mut &mut [u8]) {
unimplemented!("off-chain environment does not yet support `minimum_balance`");
}

pub fn tombstone_deposit(&self, _output: &mut &mut [u8]) {
unimplemented!("off-chain environment does not yet support `tombstone_deposit`");
}

#[allow(clippy::too_many_arguments)]
pub fn instantiate(
&mut self,
Expand Down Expand Up @@ -401,10 +364,6 @@ impl Engine {
unimplemented!("off-chain environment does not yet support `weight_to_fee`");
}

pub fn set_rent_allowance(&mut self, _value: &[u8]) {
unimplemented!("off-chain environment does not yet support `set_rent_allowance`");
}

pub fn random(&self, _subject: &[u8], _output: &mut &mut [u8]) {
unimplemented!("off-chain environment does not yet support `random`");
}
Expand Down
1 change: 0 additions & 1 deletion crates/engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ mod types;
mod tests;

pub use types::AccountError;
use types::Key;

use derive_more::From;

Expand Down
1 change: 1 addition & 0 deletions crates/engine/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub struct Key(Vec<u8>);

impl Key {
/// Creates a new `Key` from the given raw bytes.
#[allow(dead_code)]
pub fn from_bytes(bytes: &[u8]) -> Self {
Self(bytes.to_vec())
}
Expand Down
1 change: 0 additions & 1 deletion crates/env/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ cfg-if = "1.0"
paste = "1.0"
arrayref = "0.3"
static_assertions = "1.1"
sp-arithmetic = { version = "3.0", default-features = false }

[target.'cfg(target_arch = "wasm32")'.dependencies]
libsecp256k1 = { version = "0.7.0", default-features = false }
Expand Down
146 changes: 1 addition & 145 deletions crates/env/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ use crate::{
HashOutput,
},
topics::Topics,
types::{
RentParams,
RentStatus,
},
Environment,
Result,
};
Expand Down Expand Up @@ -145,56 +141,6 @@ where
})
}

/// Returns the current rent allowance for the executed contract.
///
/// # Errors
///
/// If the returned value cannot be properly decoded.
pub fn rent_allowance<T>() -> T::Balance
where
T: Environment,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::rent_allowance::<T>(instance)
})
}

/// Returns information needed for rent calculations.
///
/// # Errors
///
/// If the returned value cannot be properly decoded.
pub fn rent_params<T>() -> Result<RentParams<T>>
where
T: Environment,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::rent_params::<T>(instance)
})
}

/// Returns information about the required deposit and resulting rent.
///
/// # Parameters
///
/// - `at_refcount`: The `refcount` assumed for the returned `custom_refcount_*` fields.
/// If `None` is supplied the `custom_refcount_*` fields will also be `None`.
///
/// The `current_*` fields of `RentStatus` do **not** consider changes to the code's
/// `refcount` made during the currently running call.
///
/// # Errors
///
/// If the returned value cannot be properly decoded.
pub fn rent_status<T>(at_refcount: Option<core::num::NonZeroU32>) -> Result<RentStatus<T>>
where
T: Environment,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::rent_status::<T>(instance, at_refcount)
})
}

/// Returns the current block number.
///
/// # Errors
Expand Down Expand Up @@ -223,20 +169,6 @@ where
})
}

/// Returns the tombstone deposit for the contracts chain.
///
/// # Errors
///
/// If the returned value cannot be properly decoded.
pub fn tombstone_deposit<T>() -> T::Balance
where
T: Environment,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::tombstone_deposit::<T>(instance)
})
}

/// Emits an event with the given event data.
pub fn emit_event<T, Event>(event: Event)
where
Expand All @@ -248,16 +180,6 @@ where
})
}

/// Sets the rent allowance of the executed contract to the new value.
pub fn set_rent_allowance<T>(new_value: T::Balance)
where
T: Environment,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::set_rent_allowance::<T>(instance, new_value)
})
}

/// Writes the value to the contract storage under the given key.
///
/// # Panics
Expand Down Expand Up @@ -306,7 +228,6 @@ pub fn clear_contract_storage(key: &Key) {
///
/// - If the called account does not exist.
/// - If the called account is not a contract.
/// - If the called contract is a tombstone.
/// - If arguments passed to the called contract message are invalid.
/// - If the called contract execution has trapped.
/// - If the called contract ran out of gas upon execution.
Expand All @@ -331,7 +252,6 @@ where
///
/// - If the called account does not exist.
/// - If the called account is not a contract.
/// - If the called contract is a tombstone.
/// - If arguments passed to the called contract message are invalid.
/// - If the called contract execution has trapped.
/// - If the called contract ran out of gas upon execution.
Expand Down Expand Up @@ -375,75 +295,11 @@ where
})
}

/// Restores a smart contract in tombstone state.
///
/// # Params
///
/// - `account_id`: Account ID of the to-be-restored contract.
/// - `code_hash`: Code hash of the to-be-restored contract.
/// - `rent_allowance`: Rent allowance of the restored contract
/// upon successful restoration.
/// - `filtered_keys`: Storage keys to be excluded when calculating the tombstone hash,
/// which is used to decide whether the original contract and the
/// to-be-restored contract have matching storage.
///
/// # Usage
///
/// A smart contract that has too few funds to pay for its storage fees
/// can eventually be evicted. An evicted smart contract `C` leaves behind
/// a tombstone associated with a hash that has been computed partially
/// by its storage contents.
///
/// To restore contract `C` back to a fully working contract the normal
/// process is to write another contract `C2` with the only purpose to
/// eventually have the absolutely same contract storage as `C` did when
/// it was evicted.
/// For that purpose `C2` can use other storage keys that have not been in
/// use by contract `C`.
/// Once `C2` contract storage matches the storage of `C` when it was evicted
/// `C2` can invoke this method in order to initiate restoration of `C`.
/// A tombstone hash is calculated for `C2` and if it matches the tombstone
/// hash of `C` the restoration is going to be successful.
/// The `filtered_keys` argument can be used to ignore the extraneous keys
/// used by `C2` but not used by `C`.
///
/// The process of such a smart contract restoration can generally be very expensive.
///
/// # Note
///
/// - `filtered_keys` can be used to ignore certain storage regions
/// in the restorer contract to not influence the hash calculations.
/// - Does *not* perform restoration right away but defers it to the end of
/// the contract execution.
/// - Restoration is canceled if there is no tombstone in the destination
/// address or if the hashes don't match. No changes are made in this case.
pub fn restore_contract<T>(
account_id: T::AccountId,
code_hash: T::Hash,
rent_allowance: T::Balance,
filtered_keys: &[Key],
) where
T: Environment,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::restore_contract::<T>(
instance,
account_id,
code_hash,
rent_allowance,
filtered_keys,
)
})
}

/// Terminates the existence of the currently executed smart contract
/// without creating a tombstone.
/// Terminates the existence of the currently executed smart contract.
///
/// This removes the calling account and transfers all remaining balance
/// to the given beneficiary.
///
/// No tombstone will be created, this function kills a contract completely!
///
/// # Note
///
/// This function never returns. Either the termination was successful and the
Expand Down
Loading

0 comments on commit 9ea95ff

Please sign in to comment.