Skip to content

Commit 776df42

Browse files
authored
Unify ink_env::{eval_contract, invoke_contract} (#1165)
* Merge contract invoke and eval * Fix up call builder * Fmt * Restore off chain param getters * Use ReturnType in generated CallBuilder * No longer need to explicitly wrap return type in ReturnType * Fmt * Remove some unused ReturnType usage * Default to `returns::<()>()`, remove ReturnType usage in api * Fmt * Fix UI test * Another UI test
1 parent 3122bad commit 776df42

File tree

14 files changed

+74
-272
lines changed

14 files changed

+74
-272
lines changed

crates/env/src/api.rs

+3-30
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use crate::{
2121
TypedEnvBackend,
2222
},
2323
call::{
24-
utils::ReturnType,
2524
CallParams,
2625
CreateParams,
2726
},
@@ -216,33 +215,7 @@ pub fn clear_contract_storage(key: &Key) {
216215
})
217216
}
218217

219-
/// Invokes a contract message.
220-
///
221-
/// # Note
222-
///
223-
/// - Prefer using this over [`eval_contract`] if possible. [`invoke_contract`]
224-
/// will generally have a better performance since it won't try to fetch any results.
225-
/// - This is a low level way to invoke another smart contract.
226-
/// Prefer to use the ink! guided and type safe approach to using this.
227-
///
228-
/// # Errors
229-
///
230-
/// - If the called account does not exist.
231-
/// - If the called account is not a contract.
232-
/// - If arguments passed to the called contract message are invalid.
233-
/// - If the called contract execution has trapped.
234-
/// - If the called contract ran out of gas upon execution.
235-
pub fn invoke_contract<T, Args>(params: &CallParams<T, Args, ()>) -> Result<()>
236-
where
237-
T: Environment,
238-
Args: scale::Encode,
239-
{
240-
<EnvInstance as OnInstance>::on_instance(|instance| {
241-
TypedEnvBackend::invoke_contract::<T, Args>(instance, params)
242-
})
243-
}
244-
245-
/// Evaluates a contract message and returns its result.
218+
/// Invokes a contract message and returns its result.
246219
///
247220
/// # Note
248221
///
@@ -257,14 +230,14 @@ where
257230
/// - If the called contract execution has trapped.
258231
/// - If the called contract ran out of gas upon execution.
259232
/// - If the returned value failed to decode properly.
260-
pub fn eval_contract<T, Args, R>(params: &CallParams<T, Args, ReturnType<R>>) -> Result<R>
233+
pub fn invoke_contract<T, Args, R>(params: &CallParams<T, Args, R>) -> Result<R>
261234
where
262235
T: Environment,
263236
Args: scale::Encode,
264237
R: scale::Decode,
265238
{
266239
<EnvInstance as OnInstance>::on_instance(|instance| {
267-
TypedEnvBackend::eval_contract::<T, Args, R>(instance, params)
240+
TypedEnvBackend::invoke_contract::<T, Args, R>(instance, params)
268241
})
269242
}
270243

crates/env/src/backend.rs

+3-17
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
use crate::{
1616
call::{
17-
utils::ReturnType,
1817
CallParams,
1918
CreateParams,
2019
},
@@ -359,27 +358,14 @@ pub trait TypedEnvBackend: EnvBackend {
359358
T: Environment,
360359
Event: Topics + scale::Encode;
361360

362-
/// Invokes a contract message.
361+
/// Invokes a contract message and returns its result.
363362
///
364363
/// # Note
365364
///
366365
/// For more details visit: [`invoke_contract`][`crate::invoke_contract`]
367-
fn invoke_contract<T, Args>(
366+
fn invoke_contract<T, Args, R>(
368367
&mut self,
369-
call_data: &CallParams<T, Args, ()>,
370-
) -> Result<()>
371-
where
372-
T: Environment,
373-
Args: scale::Encode;
374-
375-
/// Evaluates a contract message and returns its result.
376-
///
377-
/// # Note
378-
///
379-
/// For more details visit: [`eval_contract`][`crate::eval_contract`]
380-
fn eval_contract<T, Args, R>(
381-
&mut self,
382-
call_data: &CallParams<T, Args, ReturnType<R>>,
368+
call_data: &CallParams<T, Args, R>,
383369
) -> Result<R>
384370
where
385371
T: Environment,

crates/env/src/call/call_builder.rs

+10-67
Original file line numberDiff line numberDiff line change
@@ -84,38 +84,17 @@ where
8484
}
8585
}
8686

87-
impl<E, Args> CallParams<E, Args, ()>
88-
where
89-
E: Environment,
90-
Args: scale::Encode,
91-
{
92-
/// Invokes the contract with the given built-up call parameters.
93-
///
94-
/// # Note
95-
///
96-
/// Prefer [`invoke`](`Self::invoke`) over [`eval`](`Self::eval`) if the
97-
/// called contract message does not return anything because it is more efficient.
98-
pub fn invoke(&self) -> Result<(), crate::Error> {
99-
crate::invoke_contract(self)
100-
}
101-
}
102-
103-
impl<E, Args, R> CallParams<E, Args, ReturnType<R>>
87+
impl<E, Args, R> CallParams<E, Args, R>
10488
where
10589
E: Environment,
10690
Args: scale::Encode,
10791
R: scale::Decode,
10892
{
109-
/// Evaluates the contract with the given built-up call parameters.
93+
/// Invokes the contract with the given built-up call parameters.
11094
///
11195
/// Returns the result of the contract execution.
112-
///
113-
/// # Note
114-
///
115-
/// Prefer [`invoke`](`Self::invoke`) over [`eval`](`Self::eval`) if the
116-
/// called contract message does not return anything because it is more efficient.
117-
pub fn eval(&self) -> Result<R, crate::Error> {
118-
crate::eval_contract(self)
96+
pub fn invoke(&self) -> Result<R, crate::Error> {
97+
crate::invoke_contract(self)
11998
}
12099
}
121100

@@ -179,7 +158,7 @@ where
179158
/// # use ::ink_env::{
180159
/// # Environment,
181160
/// # DefaultEnvironment,
182-
/// # call::{build_call, Selector, ExecutionInput, utils::ReturnType},
161+
/// # call::{build_call, Selector, ExecutionInput},
183162
/// # };
184163
/// # type AccountId = <DefaultEnvironment as Environment>::AccountId;
185164
/// let my_return_value: i32 = build_call::<DefaultEnvironment>()
@@ -192,7 +171,7 @@ where
192171
/// .push_arg(true)
193172
/// .push_arg(&[0x10; 32])
194173
/// )
195-
/// .returns::<ReturnType<i32>>()
174+
/// .returns::<i32>()
196175
/// .fire()
197176
/// .unwrap();
198177
/// ```
@@ -328,18 +307,6 @@ where
328307
}
329308
}
330309

331-
mod seal {
332-
/// Used to prevent users from implementing `IndicateReturnType` for their own types.
333-
pub trait Sealed {}
334-
impl Sealed for () {}
335-
impl<T> Sealed for super::ReturnType<T> {}
336-
}
337-
338-
/// Types that can be used in [`CallBuilder::returns`] to signal return type.
339-
pub trait IndicateReturnType: Default + self::seal::Sealed {}
340-
impl IndicateReturnType for () {}
341-
impl<T> IndicateReturnType for ReturnType<T> {}
342-
343310
impl<E, Callee, GasLimit, TransferredValue, Args>
344311
CallBuilder<E, Callee, GasLimit, TransferredValue, Args, Unset<ReturnType<()>>>
345312
where
@@ -350,14 +317,11 @@ where
350317
/// # Note
351318
///
352319
/// Either use `.returns::<()>` to signal that the call does not return a value
353-
/// or use `.returns::<ReturnType<T>>` to signal that the call returns a value of
354-
/// type `T`.
320+
/// or use `.returns::<T>` to signal that the call returns a value of type `T`.
355321
#[inline]
356322
pub fn returns<R>(
357323
self,
358-
) -> CallBuilder<E, Callee, GasLimit, TransferredValue, Args, Set<R>>
359-
where
360-
R: IndicateReturnType,
324+
) -> CallBuilder<E, Callee, GasLimit, TransferredValue, Args, Set<ReturnType<R>>>
361325
{
362326
CallBuilder {
363327
env: Default::default(),
@@ -414,7 +378,7 @@ impl<E, GasLimit, TransferredValue, Args, RetType>
414378
GasLimit,
415379
TransferredValue,
416380
Set<ExecutionInput<Args>>,
417-
Set<RetType>,
381+
Set<ReturnType<RetType>>,
418382
>
419383
where
420384
E: Environment,
@@ -465,27 +429,6 @@ where
465429
}
466430
}
467431

468-
impl<E, GasLimit, TransferredValue, Args>
469-
CallBuilder<
470-
E,
471-
Set<E::AccountId>,
472-
GasLimit,
473-
TransferredValue,
474-
Set<ExecutionInput<Args>>,
475-
Set<()>,
476-
>
477-
where
478-
E: Environment,
479-
GasLimit: Unwrap<Output = u64>,
480-
Args: scale::Encode,
481-
TransferredValue: Unwrap<Output = E::Balance>,
482-
{
483-
/// Invokes the cross-chain function call.
484-
pub fn fire(self) -> Result<(), Error> {
485-
self.params().invoke()
486-
}
487-
}
488-
489432
impl<E, GasLimit, TransferredValue>
490433
CallBuilder<
491434
E,
@@ -524,6 +467,6 @@ where
524467
{
525468
/// Invokes the cross-chain function call and returns the result.
526469
pub fn fire(self) -> Result<R, Error> {
527-
self.params().eval()
470+
self.params().invoke()
528471
}
529472
}

crates/env/src/call/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use core::marker::PhantomData;
1818

1919
/// Represents a return type.
2020
///
21-
/// Used as a marker type to differentiate at compile-time between invoke and evaluate.
21+
/// Used as a marker type to define the return type of an ink! message in call builders.
2222
#[derive(Debug)]
2323
pub struct ReturnType<T>(PhantomData<fn() -> T>);
2424

crates/env/src/call/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ mod selector;
2323
/// Utility types for the cross-contract calling API.
2424
pub mod utils {
2525
pub use super::{
26-
call_builder::IndicateReturnType,
2726
common::{
2827
ReturnType,
2928
Set,

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

+5-14
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use super::EnvInstance;
1616
use crate::{
1717
call::{
18-
utils::ReturnType,
1918
CallParams,
2019
CreateParams,
2120
},
@@ -387,10 +386,14 @@ impl TypedEnvBackend for EnvInstance {
387386
self.engine.deposit_event(&enc_topics[..], enc_data);
388387
}
389388

390-
fn invoke_contract<T, Args>(&mut self, params: &CallParams<T, Args, ()>) -> Result<()>
389+
fn invoke_contract<T, Args, R>(
390+
&mut self,
391+
params: &CallParams<T, Args, R>,
392+
) -> Result<R>
391393
where
392394
T: Environment,
393395
Args: scale::Encode,
396+
R: scale::Decode,
394397
{
395398
let _gas_limit = params.gas_limit();
396399
let _callee = params.callee();
@@ -400,18 +403,6 @@ impl TypedEnvBackend for EnvInstance {
400403
unimplemented!("off-chain environment does not support contract invocation")
401404
}
402405

403-
fn eval_contract<T, Args, R>(
404-
&mut self,
405-
_call_params: &CallParams<T, Args, ReturnType<R>>,
406-
) -> Result<R>
407-
where
408-
T: Environment,
409-
Args: scale::Encode,
410-
R: scale::Decode,
411-
{
412-
unimplemented!("off-chain environment does not support contract evaluation")
413-
}
414-
415406
fn instantiate_contract<T, Args, Salt, C>(
416407
&mut self,
417408
params: &CreateParams<T, Args, Salt, C>,

0 commit comments

Comments
 (0)