Skip to content

Commit

Permalink
Optimize get property for most primitive types (#974)
Browse files Browse the repository at this point in the history
* implement get_propery optimization for caller, gas_left and account_id

* implement optimization for little-endian types

* make most of the property query functions infallible

* adjust ERC-20 example for changes

* apply rustfmt

* fix ink_lang_macro doc tests

* fix contract-terminate example contract

* fix contract-transfer example

* fix ERC-20 and trait-ERC-20 example contracts

* fix ERC-721 example

* fix UI test

* fix some off-chain tests

* apply rustfmt
  • Loading branch information
Robbepop authored Oct 21, 2021
1 parent ee9015d commit 764a44a
Show file tree
Hide file tree
Showing 17 changed files with 305 additions and 166 deletions.
22 changes: 11 additions & 11 deletions crates/env/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use ink_primitives::Key;
/// # Errors
///
/// If the returned caller cannot be properly decoded.
pub fn caller<T>() -> Result<T::AccountId>
pub fn caller<T>() -> T::AccountId
where
T: Environment,
{
Expand All @@ -62,7 +62,7 @@ where
/// # Errors
///
/// If the returned value cannot be properly decoded.
pub fn transferred_balance<T>() -> Result<T::Balance>
pub fn transferred_balance<T>() -> T::Balance
where
T: Environment,
{
Expand All @@ -76,7 +76,7 @@ where
/// # Errors
///
/// If the returned value cannot be properly decoded.
pub fn weight_to_fee<T>(gas: u64) -> Result<T::Balance>
pub fn weight_to_fee<T>(gas: u64) -> T::Balance
where
T: Environment,
{
Expand All @@ -90,7 +90,7 @@ where
/// # Errors
///
/// If the returned value cannot be properly decoded.
pub fn gas_left<T>() -> Result<u64>
pub fn gas_left<T>() -> u64
where
T: Environment,
{
Expand All @@ -104,7 +104,7 @@ where
/// # Errors
///
/// If the returned value cannot be properly decoded.
pub fn block_timestamp<T>() -> Result<T::Timestamp>
pub fn block_timestamp<T>() -> T::Timestamp
where
T: Environment,
{
Expand All @@ -122,7 +122,7 @@ where
/// # Errors
///
/// If the returned value cannot be properly decoded.
pub fn account_id<T>() -> Result<T::AccountId>
pub fn account_id<T>() -> T::AccountId
where
T: Environment,
{
Expand All @@ -136,7 +136,7 @@ where
/// # Errors
///
/// If the returned value cannot be properly decoded.
pub fn balance<T>() -> Result<T::Balance>
pub fn balance<T>() -> T::Balance
where
T: Environment,
{
Expand All @@ -150,7 +150,7 @@ where
/// # Errors
///
/// If the returned value cannot be properly decoded.
pub fn rent_allowance<T>() -> Result<T::Balance>
pub fn rent_allowance<T>() -> T::Balance
where
T: Environment,
{
Expand Down Expand Up @@ -200,7 +200,7 @@ where
/// # Errors
///
/// If the returned value cannot be properly decoded.
pub fn block_number<T>() -> Result<T::BlockNumber>
pub fn block_number<T>() -> T::BlockNumber
where
T: Environment,
{
Expand All @@ -214,7 +214,7 @@ where
/// # Errors
///
/// If the returned value cannot be properly decoded.
pub fn minimum_balance<T>() -> Result<T::Balance>
pub fn minimum_balance<T>() -> T::Balance
where
T: Environment,
{
Expand All @@ -228,7 +228,7 @@ where
/// # Errors
///
/// If the returned value cannot be properly decoded.
pub fn tombstone_deposit<T>() -> Result<T::Balance>
pub fn tombstone_deposit<T>() -> T::Balance
where
T: Environment,
{
Expand Down
22 changes: 11 additions & 11 deletions crates/env/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,56 +186,56 @@ pub trait TypedEnvBackend: EnvBackend {
/// # Note
///
/// For more details visit: [`caller`][`crate::caller`]
fn caller<T: Environment>(&mut self) -> Result<T::AccountId>;
fn caller<T: Environment>(&mut self) -> T::AccountId;

/// Returns the transferred balance for the contract execution.
///
/// # Note
///
/// For more details visit: [`transferred_balance`][`crate::transferred_balance`]
fn transferred_balance<T: Environment>(&mut self) -> Result<T::Balance>;
fn transferred_balance<T: Environment>(&mut self) -> T::Balance;

/// Returns the price for the specified amount of gas.
///
/// # Note
///
/// For more details visit: [`weight_to_fee`][`crate::weight_to_fee`]
fn weight_to_fee<T: Environment>(&mut self, gas: u64) -> Result<T::Balance>;
fn weight_to_fee<T: Environment>(&mut self, gas: u64) -> T::Balance;

/// Returns the amount of gas left for the contract execution.
///
/// # Note
///
/// For more details visit: [`gas_left`][`crate::gas_left`]
fn gas_left<T: Environment>(&mut self) -> Result<u64>;
fn gas_left<T: Environment>(&mut self) -> u64;

/// Returns the timestamp of the current block.
///
/// # Note
///
/// For more details visit: [`block_timestamp`][`crate::block_timestamp`]
fn block_timestamp<T: Environment>(&mut self) -> Result<T::Timestamp>;
fn block_timestamp<T: Environment>(&mut self) -> T::Timestamp;

/// Returns the address of the executed contract.
///
/// # Note
///
/// For more details visit: [`account_id`][`crate::account_id`]
fn account_id<T: Environment>(&mut self) -> Result<T::AccountId>;
fn account_id<T: Environment>(&mut self) -> T::AccountId;

/// Returns the balance of the executed contract.
///
/// # Note
///
/// For more details visit: [`balance`][`crate::balance`]
fn balance<T: Environment>(&mut self) -> Result<T::Balance>;
fn balance<T: Environment>(&mut self) -> T::Balance;

/// Returns the current rent allowance for the executed contract.
///
/// # Note
///
/// For more details visit: [`rent_allowance`][`crate::rent_allowance`]
fn rent_allowance<T: Environment>(&mut self) -> Result<T::Balance>;
fn rent_allowance<T: Environment>(&mut self) -> T::Balance;

/// Returns information needed for rent calculations.
///
Expand All @@ -259,21 +259,21 @@ pub trait TypedEnvBackend: EnvBackend {
/// # Note
///
/// For more details visit: [`block_number`][`crate::block_number`]
fn block_number<T: Environment>(&mut self) -> Result<T::BlockNumber>;
fn block_number<T: Environment>(&mut self) -> T::BlockNumber;

/// Returns the minimum balance that is required for creating an account.
///
/// # Note
///
/// For more details visit: [`minimum_balance`][`crate::minimum_balance`]
fn minimum_balance<T: Environment>(&mut self) -> Result<T::Balance>;
fn minimum_balance<T: Environment>(&mut self) -> T::Balance;

/// Returns the tombstone deposit of the contract chain.
///
/// # Note
///
/// For more details visit: [`tombstone_deposit`][`crate::tombstone_deposit`]
fn tombstone_deposit<T: Environment>(&mut self) -> Result<T::Balance>;
fn tombstone_deposit<T: Environment>(&mut self) -> T::Balance;

/// Emits an event with the given event data.
///
Expand Down
56 changes: 44 additions & 12 deletions crates/env/src/engine/experimental_off_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,32 +311,53 @@ impl EnvBackend for EnvInstance {
}

impl TypedEnvBackend for EnvInstance {
fn caller<T: Environment>(&mut self) -> Result<T::AccountId> {
fn caller<T: Environment>(&mut self) -> T::AccountId {
self.get_property::<T::AccountId>(Engine::caller)
.unwrap_or_else(|error| {
panic!("could not read `caller` property: {:?}", error)
})
}

fn transferred_balance<T: Environment>(&mut self) -> Result<T::Balance> {
fn transferred_balance<T: Environment>(&mut self) -> T::Balance {
self.get_property::<T::Balance>(Engine::value_transferred)
.unwrap_or_else(|error| {
panic!("could not read `transferred_value` property: {:?}", error)
})
}

fn gas_left<T: Environment>(&mut self) -> Result<u64> {
fn gas_left<T: Environment>(&mut self) -> u64 {
self.get_property::<u64>(Engine::gas_left)
.unwrap_or_else(|error| {
panic!("could not read `gas_left` property: {:?}", error)
})
}

fn block_timestamp<T: Environment>(&mut self) -> Result<T::Timestamp> {
fn block_timestamp<T: Environment>(&mut self) -> T::Timestamp {
self.get_property::<T::Timestamp>(Engine::block_timestamp)
.unwrap_or_else(|error| {
panic!("could not read `block_timestamp` property: {:?}", error)
})
}

fn account_id<T: Environment>(&mut self) -> Result<T::AccountId> {
fn account_id<T: Environment>(&mut self) -> T::AccountId {
self.get_property::<T::AccountId>(Engine::address)
.unwrap_or_else(|error| {
panic!("could not read `account_id` property: {:?}", error)
})
}

fn balance<T: Environment>(&mut self) -> Result<T::Balance> {
fn balance<T: Environment>(&mut self) -> T::Balance {
self.get_property::<T::Balance>(Engine::balance)
.unwrap_or_else(|error| {
panic!("could not read `balance` property: {:?}", error)
})
}

fn rent_allowance<T: Environment>(&mut self) -> Result<T::Balance> {
fn rent_allowance<T: Environment>(&mut self) -> T::Balance {
self.get_property::<T::Balance>(Engine::rent_allowance)
.unwrap_or_else(|error| {
panic!("could not read `rent_allowance` property: {:?}", error)
})
}

fn rent_params<T>(&mut self) -> Result<RentParams<T>>
Expand All @@ -356,16 +377,25 @@ impl TypedEnvBackend for EnvInstance {
unimplemented!("off-chain environment does not support rent status")
}

fn block_number<T: Environment>(&mut self) -> Result<T::BlockNumber> {
fn block_number<T: Environment>(&mut self) -> T::BlockNumber {
self.get_property::<T::BlockNumber>(Engine::block_number)
.unwrap_or_else(|error| {
panic!("could not read `block_number` property: {:?}", error)
})
}

fn minimum_balance<T: Environment>(&mut self) -> Result<T::Balance> {
fn minimum_balance<T: Environment>(&mut self) -> T::Balance {
self.get_property::<T::Balance>(Engine::minimum_balance)
.unwrap_or_else(|error| {
panic!("could not read `minimum_balance` property: {:?}", error)
})
}

fn tombstone_deposit<T: Environment>(&mut self) -> Result<T::Balance> {
fn tombstone_deposit<T: Environment>(&mut self) -> T::Balance {
self.get_property::<T::Balance>(Engine::tombstone_deposit)
.unwrap_or_else(|error| {
panic!("could not read `tombstone_deposit` property: {:?}", error)
})
}

fn emit_event<T, Event>(&mut self, event: Event)
Expand Down Expand Up @@ -470,10 +500,12 @@ impl TypedEnvBackend for EnvInstance {
.map_err(Into::into)
}

fn weight_to_fee<T: Environment>(&mut self, gas: u64) -> Result<T::Balance> {
fn weight_to_fee<T: Environment>(&mut self, gas: u64) -> T::Balance {
let mut output: [u8; BUFFER_SIZE] = [0; BUFFER_SIZE];
self.engine.weight_to_fee(gas, &mut &mut output[..]);
scale::Decode::decode(&mut &output[..]).map_err(Into::into)
scale::Decode::decode(&mut &output[..]).unwrap_or_else(|error| {
panic!("could not read `weight_to_fee` property: {:?}", error)
})
}

fn random<T>(&mut self, subject: &[u8]) -> Result<(T::Hash, T::BlockNumber)>
Expand Down
Loading

0 comments on commit 764a44a

Please sign in to comment.