Skip to content

Commit da5a124

Browse files
Michael MüllerRobbepop
Michael Müller
andauthored
Add example usage comments to EnvAccess methods (#797)
* Add example usage comments to env methods * Remove superfluous line * wip * Remove debug code * Make doc comment examples compile * Remove superfluous line * Surround with complete contract code * Experiment with macro to reduce code duplication * Revert "Experiment with macro to reduce code duplication" This reverts commit f731912. * Add complete contract code in doc tests * Remove unnecessary `[dev-dependencies]` * Improve code in doc tests * Fix doc tests * Improve code appearance * Add better doc test examples * Make `gas` be of type `u64` instead of `Balance` * Make `gas` be of type `u64` instead of `Balance` * Make `gas` be of type `u64` instead of `Balance` * Apply suggestions from code review Co-authored-by: Robin Freyler <[email protected]> * Add simple access permission contract * Fix syntax Co-authored-by: Robin Freyler <[email protected]>
1 parent 056cf4c commit da5a124

File tree

13 files changed

+706
-46
lines changed

13 files changed

+706
-46
lines changed

crates/env/src/api.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ where
9090
/// # Errors
9191
///
9292
/// If the returned value cannot be properly decoded.
93-
pub fn gas_left<T>() -> Result<T::Balance>
93+
pub fn gas_left<T>() -> Result<u64>
9494
where
9595
T: Environment,
9696
{
@@ -436,7 +436,8 @@ pub fn restore_contract<T>(
436436
})
437437
}
438438

439-
/// Terminates the existence of the currently executed smart contract.
439+
/// Terminates the existence of the currently executed smart contract
440+
/// without creating a tombstone.
440441
///
441442
/// This removes the calling account and transfers all remaining balance
442443
/// to the given beneficiary.
@@ -561,6 +562,15 @@ pub fn debug_message(message: &str) {
561562
}
562563

563564
/// Conducts the crypto hash of the given input and stores the result in `output`.
565+
///
566+
/// # Example
567+
///
568+
/// ```
569+
/// use ink_env::hash::{Sha2x256, HashOutput};
570+
/// let input: &[u8] = &[13, 14, 15];
571+
/// let mut output = <Sha2x256 as HashOutput>::Type::default(); // 256-bit buffer
572+
/// let hash = ink_env::hash_bytes::<Sha2x256>(input, &mut output);
573+
/// ```
564574
pub fn hash_bytes<H>(input: &[u8], output: &mut <H as HashOutput>::Type)
565575
where
566576
H: CryptoHash,

crates/env/src/backend.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ pub trait TypedEnvBackend: EnvBackend {
203203
/// # Note
204204
///
205205
/// For more details visit: [`gas_left`][`crate::gas_left`]
206-
fn gas_left<T: Environment>(&mut self) -> Result<T::Balance>;
206+
fn gas_left<T: Environment>(&mut self) -> Result<u64>;
207207

208208
/// Returns the timestamp of the current block.
209209
///

crates/env/src/call/call_builder.rs

+1
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ mod seal {
302302
impl Sealed for () {}
303303
impl<T> Sealed for super::ReturnType<T> {}
304304
}
305+
305306
/// Types that can be used in [`CallBuilder::returns`] to signal return type.
306307
pub trait IndicateReturnType: Default + self::seal::Sealed {}
307308
impl IndicateReturnType for () {}

crates/env/src/engine/experimental_off_chain/impls.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,8 @@ impl TypedEnvBackend for EnvInstance {
284284
self.get_property::<T::Balance>(Engine::value_transferred)
285285
}
286286

287-
fn gas_left<T: Environment>(&mut self) -> Result<T::Balance> {
288-
self.get_property::<T::Balance>(Engine::gas_left)
287+
fn gas_left<T: Environment>(&mut self) -> Result<u64> {
288+
self.get_property::<u64>(Engine::gas_left)
289289
}
290290

291291
fn block_timestamp<T: Environment>(&mut self) -> Result<T::Timestamp> {

crates/env/src/engine/off_chain/db/exec_context.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub struct ExecContext {
3737
/// The transferred value from caller to callee.
3838
pub transferred_value: OffBalance,
3939
/// The gas provided for the whole execution.
40-
pub gas: OffBalance,
40+
pub gas: u64,
4141
/// The inputs provided for the whole execution.
4242
///
4343
/// # Note
@@ -82,11 +82,11 @@ impl ExecContext {
8282
}
8383

8484
/// Returns the gas.
85-
pub fn gas<T>(&self) -> Result<T::Balance>
85+
pub fn gas<T>(&self) -> u64
8686
where
8787
T: Environment,
8888
{
89-
self.gas.decode().map_err(Into::into)
89+
self.gas
9090
}
9191

9292
/// Returns the call data.
@@ -122,7 +122,7 @@ where
122122
/// The transferred value from caller to callee.
123123
transferred_value: Option<T::Balance>,
124124
/// The gas provided for the contract execution from caller to callee.
125-
gas: Option<T::Balance>,
125+
gas: Option<u64>,
126126
/// The inputs given to the contract execution.
127127
call_data: Option<CallData>,
128128
}
@@ -173,7 +173,7 @@ where
173173
/// # Panics
174174
///
175175
/// If there has already been set provided gas.
176-
pub fn gas(mut self, gas: T::Balance) -> Self {
176+
pub fn gas(mut self, gas: u64) -> Self {
177177
if self.gas.is_some() {
178178
panic!("already has provided gas");
179179
}
@@ -223,7 +223,7 @@ where
223223
caller: TypedEncoded::new(&caller),
224224
callee: TypedEncoded::new(&callee),
225225
transferred_value: TypedEncoded::new(&transferred_value),
226-
gas: TypedEncoded::new(&gas),
226+
gas,
227227
call_data: self.call_data.unwrap(),
228228
output: None,
229229
}

crates/env/src/engine/off_chain/impls.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,11 @@ impl TypedEnvBackend for EnvInstance {
316316
.saturating_mul(gas.try_into().unwrap_or_else(|_| Bounded::max_value())))
317317
}
318318

319-
fn gas_left<T: Environment>(&mut self) -> Result<T::Balance> {
320-
self.exec_context()
319+
fn gas_left<T: Environment>(&mut self) -> Result<u64> {
320+
Ok(self
321+
.exec_context()
321322
.expect(UNINITIALIZED_EXEC_CONTEXT)
322-
.gas::<T>()
323-
.map_err(|_| scale::Error::from("could not decode gas left"))
324-
.map_err(Into::into)
323+
.gas::<T>())
325324
}
326325

327326
fn block_timestamp<T: Environment>(&mut self) -> Result<T::Timestamp> {

crates/env/src/engine/off_chain/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ impl EnvInstance {
205205
ExecContext::build::<T>()
206206
.caller(default_accounts.alice)
207207
.callee(contract_account_id)
208-
.gas(T::Balance::from(500_000u32))
208+
.gas(500_000u64)
209209
.transferred_value(T::Balance::from(500u32))
210210
.call_data(CallData::new(Selector::new(selector_bytes_for_call)))
211211
.finish(),

crates/env/src/engine/off_chain/test_api.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use std::panic::UnwindSafe;
4444
pub fn push_execution_context<T>(
4545
caller: T::AccountId,
4646
callee: T::AccountId,
47-
gas_limit: T::Balance,
47+
gas_limit: u64,
4848
endowment: T::Balance,
4949
call_data: CallData,
5050
) where

crates/env/src/engine/on_chain/impls.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@ impl TypedEnvBackend for EnvInstance {
309309
self.get_property::<T::Balance>(ext::value_transferred)
310310
}
311311

312-
fn gas_left<T: Environment>(&mut self) -> Result<T::Balance> {
313-
self.get_property::<T::Balance>(ext::gas_left)
312+
fn gas_left<T: Environment>(&mut self) -> Result<u64> {
313+
self.get_property::<u64>(ext::gas_left)
314314
}
315315

316316
fn block_timestamp<T: Environment>(&mut self) -> Result<T::Timestamp> {

crates/lang/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ scale = { package = "parity-scale-codec", version = "2.1", default-features = fa
2626
derive_more = { version = "0.99", default-features = false, features = ["from"] }
2727
static_assertions = "1.1"
2828

29+
[dev-dependencies]
30+
# required for the doctest of `env_access::EnvAccess::instantiate_contract`
31+
scale-info = { version = "0.6", default-features = false, features = ["derive"] }
32+
2933
[features]
3034
default = ["std"]
3135
std = [

crates/lang/macro/tests/ui/pass/08-static-env.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ mod static_env {
1313
}
1414

1515
#[ink(message)]
16-
pub fn gas_left(&mut self) -> Balance {
16+
pub fn gas_left(&mut self) -> u64 {
1717
Self::env().gas_left()
1818
}
1919
}

0 commit comments

Comments
 (0)