From 6b023e74e6a1f02b581cda8a0431691207113f64 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 4 Mar 2022 14:25:53 +0000 Subject: [PATCH 01/12] Merge contract invoke and eval --- crates/env/src/api.rs | 32 +-------- crates/env/src/backend.rs | 17 +---- crates/env/src/call/call_builder.rs | 29 ++------- crates/env/src/engine/off_chain/impls.rs | 17 +---- crates/env/src/engine/on_chain/impls.rs | 83 +++++++++--------------- crates/lang/src/env_access.rs | 69 ++------------------ examples/erc1155/lib.rs | 4 +- 7 files changed, 50 insertions(+), 201 deletions(-) diff --git a/crates/env/src/api.rs b/crates/env/src/api.rs index 065d4112978..646c7dbc595 100644 --- a/crates/env/src/api.rs +++ b/crates/env/src/api.rs @@ -216,33 +216,7 @@ pub fn clear_contract_storage(key: &Key) { }) } -/// Invokes a contract message. -/// -/// # Note -/// -/// - Prefer using this over [`eval_contract`] if possible. [`invoke_contract`] -/// will generally have a better performance since it won't try to fetch any results. -/// - This is a low level way to invoke another smart contract. -/// Prefer to use the ink! guided and type safe approach to using this. -/// -/// # Errors -/// -/// - If the called account does not exist. -/// - If the called account is not a contract. -/// - 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. -pub fn invoke_contract(params: &CallParams) -> Result<()> -where - T: Environment, - Args: scale::Encode, -{ - ::on_instance(|instance| { - TypedEnvBackend::invoke_contract::(instance, params) - }) -} - -/// Evaluates a contract message and returns its result. +/// Invokes a contract message and returns its result. /// /// # Note /// @@ -257,14 +231,14 @@ where /// - If the called contract execution has trapped. /// - If the called contract ran out of gas upon execution. /// - If the returned value failed to decode properly. -pub fn eval_contract(params: &CallParams>) -> Result +pub fn invoke_contract(params: &CallParams>) -> Result where T: Environment, Args: scale::Encode, R: scale::Decode, { ::on_instance(|instance| { - TypedEnvBackend::eval_contract::(instance, params) + TypedEnvBackend::invoke_contract::(instance, params) }) } diff --git a/crates/env/src/backend.rs b/crates/env/src/backend.rs index 7182d6ad9a8..967a476cbd1 100644 --- a/crates/env/src/backend.rs +++ b/crates/env/src/backend.rs @@ -359,25 +359,12 @@ pub trait TypedEnvBackend: EnvBackend { T: Environment, Event: Topics + scale::Encode; - /// Invokes a contract message. + /// Invokes a contract message and returns its result. /// /// # Note /// /// For more details visit: [`invoke_contract`][`crate::invoke_contract`] - fn invoke_contract( - &mut self, - call_data: &CallParams, - ) -> Result<()> - where - T: Environment, - Args: scale::Encode; - - /// Evaluates a contract message and returns its result. - /// - /// # Note - /// - /// For more details visit: [`eval_contract`][`crate::eval_contract`] - fn eval_contract( + fn invoke_contract( &mut self, call_data: &CallParams>, ) -> Result diff --git a/crates/env/src/call/call_builder.rs b/crates/env/src/call/call_builder.rs index b7a37a1177e..13e18003c8b 100644 --- a/crates/env/src/call/call_builder.rs +++ b/crates/env/src/call/call_builder.rs @@ -84,38 +84,17 @@ where } } -impl CallParams -where - E: Environment, - Args: scale::Encode, -{ - /// Invokes the contract with the given built-up call parameters. - /// - /// # Note - /// - /// Prefer [`invoke`](`Self::invoke`) over [`eval`](`Self::eval`) if the - /// called contract message does not return anything because it is more efficient. - pub fn invoke(&self) -> Result<(), crate::Error> { - crate::invoke_contract(self) - } -} - impl CallParams> where E: Environment, Args: scale::Encode, R: scale::Decode, { - /// Evaluates the contract with the given built-up call parameters. + /// Invokes the contract with the given built-up call parameters. /// /// Returns the result of the contract execution. - /// - /// # Note - /// - /// Prefer [`invoke`](`Self::invoke`) over [`eval`](`Self::eval`) if the - /// called contract message does not return anything because it is more efficient. - pub fn eval(&self) -> Result { - crate::eval_contract(self) + pub fn invoke(&self) -> Result { + crate::invoke_contract(self) } } @@ -524,6 +503,6 @@ where { /// Invokes the cross-chain function call and returns the result. pub fn fire(self) -> Result { - self.params().eval() + self.params().invoke() } } diff --git a/crates/env/src/engine/off_chain/impls.rs b/crates/env/src/engine/off_chain/impls.rs index 2ecf10a0acd..942327cdffe 100644 --- a/crates/env/src/engine/off_chain/impls.rs +++ b/crates/env/src/engine/off_chain/impls.rs @@ -387,20 +387,7 @@ impl TypedEnvBackend for EnvInstance { self.engine.deposit_event(&enc_topics[..], enc_data); } - fn invoke_contract(&mut self, params: &CallParams) -> Result<()> - where - T: Environment, - Args: scale::Encode, - { - let _gas_limit = params.gas_limit(); - let _callee = params.callee(); - let _call_flags = params.call_flags().into_u32(); - let _transferred_value = params.transferred_value(); - let _input = params.exec_input(); - unimplemented!("off-chain environment does not support contract invocation") - } - - fn eval_contract( + fn invoke_contract( &mut self, _call_params: &CallParams>, ) -> Result @@ -409,7 +396,7 @@ impl TypedEnvBackend for EnvInstance { Args: scale::Encode, R: scale::Decode, { - unimplemented!("off-chain environment does not support contract evaluation") + unimplemented!("off-chain environment does not support contract invocation") } fn instantiate_contract( diff --git a/crates/env/src/engine/on_chain/impls.rs b/crates/env/src/engine/on_chain/impls.rs index 4ee4b5e0864..b3133f65303 100644 --- a/crates/env/src/engine/on_chain/impls.rs +++ b/crates/env/src/engine/on_chain/impls.rs @@ -215,45 +215,6 @@ impl EnvInstance { ext_fn(full_scope); scale::Decode::decode(&mut &full_scope[..]).map_err(Into::into) } - - /// Reusable implementation for invoking another contract message. - fn invoke_contract_impl( - &mut self, - params: &CallParams, - ) -> Result - where - T: Environment, - Args: scale::Encode, - R: scale::Decode, - { - let mut scope = self.scoped_buffer(); - let gas_limit = params.gas_limit(); - let enc_callee = scope.take_encoded(params.callee()); - let enc_transferred_value = scope.take_encoded(params.transferred_value()); - let call_flags = params.call_flags(); - let enc_input = if !call_flags.forward_input() && !call_flags.clone_input() { - scope.take_encoded(params.exec_input()) - } else { - &mut [] - }; - let output = &mut scope.take_rest(); - let flags = params.call_flags().into_u32(); - let call_result = ext::call( - flags, - enc_callee, - gas_limit, - enc_transferred_value, - enc_input, - output, - ); - match call_result { - Ok(()) | Err(ext::Error::CalleeReverted) => { - let decoded = scale::Decode::decode(&mut &output[..])?; - Ok(decoded) - } - Err(actual_error) => Err(actual_error.into()), - } - } } impl EnvBackend for EnvInstance { @@ -396,27 +357,43 @@ impl TypedEnvBackend for EnvInstance { ext::deposit_event(enc_topics, enc_data); } - fn invoke_contract( + fn invoke_contract( &mut self, - call_params: &CallParams, - ) -> Result<()> - where - T: Environment, - Args: scale::Encode, - { - self.invoke_contract_impl(call_params) - } - - fn eval_contract( - &mut self, - call_params: &CallParams>, + params: &CallParams>, ) -> Result where T: Environment, Args: scale::Encode, R: scale::Decode, { - self.invoke_contract_impl(call_params) + let mut scope = self.scoped_buffer(); + let gas_limit = params.gas_limit(); + let enc_callee = scope.take_encoded(params.callee()); + let enc_transferred_value = scope.take_encoded(params.transferred_value()); + let call_flags = params.call_flags(); + let enc_input = if !call_flags.forward_input() && !call_flags.clone_input() { + scope.take_encoded(params.exec_input()) + } else { + &mut [] + }; + let output = &mut scope.take_rest(); + let flags = params.call_flags().into_u32(); + let call_result = ext::call( + flags, + enc_callee, + gas_limit, + enc_transferred_value, + enc_input, + output, + ); + match call_result { + Ok(()) | Err(ext::Error::CalleeReverted) => { + let decoded = scale::Decode::decode(&mut &output[..])?; + Ok(decoded) + } + Err(actual_error) => Err(actual_error.into()), + } + } fn instantiate_contract( diff --git a/crates/lang/src/env_access.rs b/crates/lang/src/env_access.rs index 11ca46d40ef..206be339b16 100644 --- a/crates/lang/src/env_access.rs +++ b/crates/lang/src/env_access.rs @@ -504,62 +504,7 @@ where ink_env::instantiate_contract::(params) } - /// Invokes a contract message without fetching its result. - /// - /// # Example - /// - /// ``` - /// # use ink_lang as ink; - /// # #[ink::contract] - /// # pub mod my_contract { - /// use ink_env::{ - /// DefaultEnvironment, - /// call::{build_call, Selector, ExecutionInput} - /// }; - /// - /// # - /// # #[ink(storage)] - /// # pub struct MyContract { } - /// # - /// # impl MyContract { - /// # #[ink(constructor)] - /// # pub fn new() -> Self { - /// # Self {} - /// # } - /// # - /// /// Invokes another contract message without fetching the result. - /// #[ink(message)] - /// pub fn invoke_contract(&self) { - /// let call_params = build_call::() - /// .callee(AccountId::from([0x42; 32])) - /// .gas_limit(5000) - /// .transferred_value(10) - /// .exec_input( - /// ExecutionInput::new(Selector::new([0xCA, 0xFE, 0xBA, 0xBE])) - /// .push_arg(42) - /// .push_arg(true) - /// .push_arg(&[0x10u8; 32]) - /// ) - /// .returns::<()>() - /// .params(); - /// self.env().invoke_contract(&call_params).expect("call invocation must succeed"); - /// } - /// # - /// # } - /// # } - /// ``` - /// - /// # Note - /// - /// For more details visit: [`ink_env::invoke_contract`] - pub fn invoke_contract(self, params: &CallParams) -> Result<()> - where - Args: scale::Encode, - { - ink_env::invoke_contract::(params) - } - - /// Evaluates a contract message and returns its result. + /// Invokes a contract message and returns its result. /// /// # Example /// @@ -582,9 +527,9 @@ where /// # Self {} /// # } /// # - /// /// Evaluates a contract message and fetches the result. + /// /// Invokes a contract message and fetches the result. /// #[ink(message)] - /// pub fn evaluate_contract(&self) -> i32 { + /// pub fn invoke_contract(&self) -> i32 { /// let call_params = build_call::() /// .callee(AccountId::from([0x42; 32])) /// .gas_limit(5000) @@ -597,7 +542,7 @@ where /// ) /// .returns::>() /// .params(); - /// self.env().eval_contract(&call_params).expect("call invocation must succeed") + /// self.env().invoke_contract(&call_params).expect("call invocation must succeed") /// } /// # /// # } @@ -606,8 +551,8 @@ where /// /// # Note /// - /// For more details visit: [`ink_env::eval_contract`] - pub fn eval_contract( + /// For more details visit: [`ink_env::invoke_contract`] + pub fn invoke_contract( self, params: &CallParams>, ) -> Result @@ -615,7 +560,7 @@ where Args: scale::Encode, R: scale::Decode, { - ink_env::eval_contract::(params) + ink_env::invoke_contract::(params) } /// Terminates the existence of a contract. diff --git a/examples/erc1155/lib.rs b/examples/erc1155/lib.rs index 60a643eb32f..a96c728cbd9 100644 --- a/examples/erc1155/lib.rs +++ b/examples/erc1155/lib.rs @@ -381,7 +381,7 @@ mod erc1155 { value: Balance, data: Vec, ) { - // This is disabled during tests due to the use of `eval_contract()` not being + // This is disabled during tests due to the use of `invoke_contract()` not being // supported (tests end up panicking). #[cfg(not(test))] { @@ -408,7 +408,7 @@ mod erc1155 { .returns::>>() .params(); - match ink_env::eval_contract(¶ms) { + match ink_env::invoke_contract(¶ms) { Ok(v) => { ink_env::debug_println!( "Received return value \"{:?}\" from contract {:?}", From 750a1ade39acdbe0e7515075212985096d13b557 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 4 Mar 2022 14:49:23 +0000 Subject: [PATCH 02/12] Fix up call builder --- crates/env/src/call/call_builder.rs | 41 ----------------------------- 1 file changed, 41 deletions(-) diff --git a/crates/env/src/call/call_builder.rs b/crates/env/src/call/call_builder.rs index 13e18003c8b..fe83fc311ee 100644 --- a/crates/env/src/call/call_builder.rs +++ b/crates/env/src/call/call_builder.rs @@ -444,47 +444,6 @@ where } } -impl - CallBuilder< - E, - Set, - GasLimit, - TransferredValue, - Set>, - Set<()>, - > -where - E: Environment, - GasLimit: Unwrap, - Args: scale::Encode, - TransferredValue: Unwrap, -{ - /// Invokes the cross-chain function call. - pub fn fire(self) -> Result<(), Error> { - self.params().invoke() - } -} - -impl - CallBuilder< - E, - Set, - GasLimit, - TransferredValue, - Unset>, - Unset>, - > -where - E: Environment, - GasLimit: Unwrap, - TransferredValue: Unwrap, -{ - /// Invokes the cross-chain function call. - pub fn fire(self) -> Result<(), Error> { - self.params().invoke() - } -} - impl CallBuilder< E, From 27d822e9c30f862696ee35b646696a8623e7388e Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 4 Mar 2022 14:57:33 +0000 Subject: [PATCH 03/12] Fmt --- crates/env/src/api.rs | 4 +++- crates/env/src/engine/on_chain/impls.rs | 1 - 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/env/src/api.rs b/crates/env/src/api.rs index 646c7dbc595..1fbd6a10cce 100644 --- a/crates/env/src/api.rs +++ b/crates/env/src/api.rs @@ -231,7 +231,9 @@ pub fn clear_contract_storage(key: &Key) { /// - If the called contract execution has trapped. /// - If the called contract ran out of gas upon execution. /// - If the returned value failed to decode properly. -pub fn invoke_contract(params: &CallParams>) -> Result +pub fn invoke_contract( + params: &CallParams>, +) -> Result where T: Environment, Args: scale::Encode, diff --git a/crates/env/src/engine/on_chain/impls.rs b/crates/env/src/engine/on_chain/impls.rs index b3133f65303..34c9ed7e775 100644 --- a/crates/env/src/engine/on_chain/impls.rs +++ b/crates/env/src/engine/on_chain/impls.rs @@ -393,7 +393,6 @@ impl TypedEnvBackend for EnvInstance { } Err(actual_error) => Err(actual_error.into()), } - } fn instantiate_contract( From 669dd3e65a27a04469e5edfdeef8256797961f32 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 7 Mar 2022 10:18:04 +0000 Subject: [PATCH 04/12] Restore off chain param getters --- crates/env/src/engine/off_chain/impls.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/env/src/engine/off_chain/impls.rs b/crates/env/src/engine/off_chain/impls.rs index 942327cdffe..f3f65d1a7fc 100644 --- a/crates/env/src/engine/off_chain/impls.rs +++ b/crates/env/src/engine/off_chain/impls.rs @@ -389,13 +389,18 @@ impl TypedEnvBackend for EnvInstance { fn invoke_contract( &mut self, - _call_params: &CallParams>, + params: &CallParams>, ) -> Result where T: Environment, Args: scale::Encode, R: scale::Decode, { + let _gas_limit = params.gas_limit(); + let _callee = params.callee(); + let _call_flags = params.call_flags().into_u32(); + let _transferred_value = params.transferred_value(); + let _input = params.exec_input(); unimplemented!("off-chain environment does not support contract invocation") } From 059c81c712d9c32f0a821bf038b29b93975a6e17 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 7 Mar 2022 11:57:33 +0000 Subject: [PATCH 05/12] Use ReturnType in generated CallBuilder --- crates/lang/codegen/src/generator/as_dependency/call_builder.rs | 2 +- crates/lang/codegen/src/generator/trait_def/call_builder.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/lang/codegen/src/generator/as_dependency/call_builder.rs b/crates/lang/codegen/src/generator/as_dependency/call_builder.rs index 57750e47a84..a34f5c4c277 100644 --- a/crates/lang/codegen/src/generator/as_dependency/call_builder.rs +++ b/crates/lang/codegen/src/generator/as_dependency/call_builder.rs @@ -373,7 +373,7 @@ impl CallBuilder<'_> { let mut_tok = callable.receiver().is_ref_mut().then(|| quote! { mut }); let output = message.output(); let output_sig = output.map_or_else( - || quote! { () }, + || quote! { ::ink_env::call::utils::ReturnType<()> }, |output| quote! { ::ink_env::call::utils::ReturnType<#output> }, ); let output_span = output.span(); diff --git a/crates/lang/codegen/src/generator/trait_def/call_builder.rs b/crates/lang/codegen/src/generator/trait_def/call_builder.rs index 3f820c98095..9374d66a38c 100644 --- a/crates/lang/codegen/src/generator/trait_def/call_builder.rs +++ b/crates/lang/codegen/src/generator/trait_def/call_builder.rs @@ -366,7 +366,7 @@ impl CallBuilder<'_> { let output_ident = generator::output_ident(message_ident); let output = message.output(); let output_sig = output.map_or_else( - || quote! { () }, + || quote! { ::ink_env::call::utils::ReturnType<()> }, |output| quote! { ::ink_env::call::utils::ReturnType<#output> }, ); let selector_bytes = selector.hex_lits(); From a8354894c12f99d4b6b3f9e7b68f8d414c111c3c Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 7 Mar 2022 12:55:40 +0000 Subject: [PATCH 06/12] No longer need to explicitly wrap return type in ReturnType --- crates/env/src/call/call_builder.rs | 24 ++++--------------- crates/env/src/call/common.rs | 2 +- crates/env/src/call/mod.rs | 1 - .../generator/as_dependency/call_builder.rs | 9 +++---- .../src/generator/trait_def/call_builder.rs | 9 +++---- crates/lang/src/env_access.rs | 2 +- examples/erc1155/lib.rs | 2 +- examples/multisig/lib.rs | 2 +- 8 files changed, 14 insertions(+), 37 deletions(-) diff --git a/crates/env/src/call/call_builder.rs b/crates/env/src/call/call_builder.rs index fe83fc311ee..908327e8828 100644 --- a/crates/env/src/call/call_builder.rs +++ b/crates/env/src/call/call_builder.rs @@ -158,7 +158,7 @@ where /// # use ::ink_env::{ /// # Environment, /// # DefaultEnvironment, -/// # call::{build_call, Selector, ExecutionInput, utils::ReturnType}, +/// # call::{build_call, Selector, ExecutionInput}, /// # }; /// # type AccountId = ::AccountId; /// let my_return_value: i32 = build_call::() @@ -171,7 +171,7 @@ where /// .push_arg(true) /// .push_arg(&[0x10; 32]) /// ) -/// .returns::>() +/// .returns::() /// .fire() /// .unwrap(); /// ``` @@ -307,18 +307,6 @@ where } } -mod seal { - /// Used to prevent users from implementing `IndicateReturnType` for their own types. - pub trait Sealed {} - impl Sealed for () {} - impl Sealed for super::ReturnType {} -} - -/// Types that can be used in [`CallBuilder::returns`] to signal return type. -pub trait IndicateReturnType: Default + self::seal::Sealed {} -impl IndicateReturnType for () {} -impl IndicateReturnType for ReturnType {} - impl CallBuilder>> where @@ -329,15 +317,11 @@ where /// # Note /// /// Either use `.returns::<()>` to signal that the call does not return a value - /// or use `.returns::>` to signal that the call returns a value of - /// type `T`. + /// or use `.returns::` to signal that the call returns a value of type `T`. #[inline] pub fn returns( self, - ) -> CallBuilder> - where - R: IndicateReturnType, - { + ) -> CallBuilder>> { CallBuilder { env: Default::default(), callee: self.callee, diff --git a/crates/env/src/call/common.rs b/crates/env/src/call/common.rs index 94cf6c7bfcb..e1cc4171a2d 100644 --- a/crates/env/src/call/common.rs +++ b/crates/env/src/call/common.rs @@ -18,7 +18,7 @@ use core::marker::PhantomData; /// Represents a return type. /// -/// Used as a marker type to differentiate at compile-time between invoke and evaluate. +/// Used as a marker type to define the return type of an ink! message in call builders. #[derive(Debug)] pub struct ReturnType(PhantomData T>); diff --git a/crates/env/src/call/mod.rs b/crates/env/src/call/mod.rs index 7cb8c4fa97c..a4c6fa653f8 100644 --- a/crates/env/src/call/mod.rs +++ b/crates/env/src/call/mod.rs @@ -23,7 +23,6 @@ mod selector; /// Utility types for the cross-contract calling API. pub mod utils { pub use super::{ - call_builder::IndicateReturnType, common::{ ReturnType, Set, diff --git a/crates/lang/codegen/src/generator/as_dependency/call_builder.rs b/crates/lang/codegen/src/generator/as_dependency/call_builder.rs index a34f5c4c277..5f40dd63337 100644 --- a/crates/lang/codegen/src/generator/as_dependency/call_builder.rs +++ b/crates/lang/codegen/src/generator/as_dependency/call_builder.rs @@ -372,10 +372,7 @@ impl CallBuilder<'_> { let arg_list = generator::generate_argument_list(input_types.iter().cloned()); let mut_tok = callable.receiver().is_ref_mut().then(|| quote! { mut }); let output = message.output(); - let output_sig = output.map_or_else( - || quote! { ::ink_env::call::utils::ReturnType<()> }, - |output| quote! { ::ink_env::call::utils::ReturnType<#output> }, - ); + let return_type = output.map_or_else(|| quote! { () }, |output| quote! { #output }); let output_span = output.span(); let output_type = quote_spanned!(output_span=> ::ink_env::call::CallBuilder< @@ -384,7 +381,7 @@ impl CallBuilder<'_> { ::ink_env::call::utils::Unset< ::core::primitive::u64 >, ::ink_env::call::utils::Unset< ::Balance >, ::ink_env::call::utils::Set< ::ink_env::call::ExecutionInput<#arg_list> >, - ::ink_env::call::utils::Set<#output_sig>, + ::ink_env::call::utils::Set< ::ink_env::call::utils::ReturnType<#return_type> >, > ); quote_spanned!(span=> @@ -405,7 +402,7 @@ impl CallBuilder<'_> { .push_arg(#input_bindings) )* ) - .returns::<#output_sig>() + .returns::<#return_type>() } ) } diff --git a/crates/lang/codegen/src/generator/trait_def/call_builder.rs b/crates/lang/codegen/src/generator/trait_def/call_builder.rs index 9374d66a38c..7ef5f88bf7f 100644 --- a/crates/lang/codegen/src/generator/trait_def/call_builder.rs +++ b/crates/lang/codegen/src/generator/trait_def/call_builder.rs @@ -365,10 +365,7 @@ impl CallBuilder<'_> { .filter_attr(message.attrs()); let output_ident = generator::output_ident(message_ident); let output = message.output(); - let output_sig = output.map_or_else( - || quote! { ::ink_env::call::utils::ReturnType<()> }, - |output| quote! { ::ink_env::call::utils::ReturnType<#output> }, - ); + let output_type = output.map_or_else(|| quote! { () }, |output| quote! { #output }); let selector_bytes = selector.hex_lits(); let input_bindings = generator::input_bindings(message.inputs()); let input_types = generator::input_types(message.inputs()); @@ -382,7 +379,7 @@ impl CallBuilder<'_> { ::ink_env::call::utils::Unset< ::core::primitive::u64 >, ::ink_env::call::utils::Unset< ::Balance >, ::ink_env::call::utils::Set< ::ink_env::call::ExecutionInput<#arg_list> >, - ::ink_env::call::utils::Set<#output_sig>, + ::ink_env::call::utils::Set< ::ink_env::call::utils::ReturnType<#output_type> >, >; #( #attrs )* @@ -401,7 +398,7 @@ impl CallBuilder<'_> { .push_arg(#input_bindings) )* ) - .returns::<#output_sig>() + .returns::<#output_type>() } ) } diff --git a/crates/lang/src/env_access.rs b/crates/lang/src/env_access.rs index 206be339b16..a8f214b74db 100644 --- a/crates/lang/src/env_access.rs +++ b/crates/lang/src/env_access.rs @@ -540,7 +540,7 @@ where /// .push_arg(true) /// .push_arg(&[0x10u8; 32]) /// ) - /// .returns::>() + /// .returns::() /// .params(); /// self.env().invoke_contract(&call_params).expect("call invocation must succeed") /// } diff --git a/examples/erc1155/lib.rs b/examples/erc1155/lib.rs index a96c728cbd9..77c03d6d298 100644 --- a/examples/erc1155/lib.rs +++ b/examples/erc1155/lib.rs @@ -405,7 +405,7 @@ mod erc1155 { .push_arg(value) .push_arg(data), ) - .returns::>>() + .returns::>() .params(); match ink_env::invoke_contract(¶ms) { diff --git a/examples/multisig/lib.rs b/examples/multisig/lib.rs index 365834cc30c..d44a9620ff0 100755 --- a/examples/multisig/lib.rs +++ b/examples/multisig/lib.rs @@ -551,7 +551,7 @@ mod multisig { .exec_input( ExecutionInput::new(t.selector.into()).push_arg(CallInput(&t.input)), ) - .returns::>>() + .returns::>() .fire() .map_err(|_| Error::TransactionFailed); self.env().emit_event(Execution { From 638701d1d60a8d86c1e9185fee0f0161b2bd52ec Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 7 Mar 2022 12:56:00 +0000 Subject: [PATCH 07/12] Fmt --- crates/env/src/call/call_builder.rs | 3 ++- .../lang/codegen/src/generator/as_dependency/call_builder.rs | 3 ++- crates/lang/codegen/src/generator/trait_def/call_builder.rs | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/env/src/call/call_builder.rs b/crates/env/src/call/call_builder.rs index 908327e8828..13921f0cbaa 100644 --- a/crates/env/src/call/call_builder.rs +++ b/crates/env/src/call/call_builder.rs @@ -321,7 +321,8 @@ where #[inline] pub fn returns( self, - ) -> CallBuilder>> { + ) -> CallBuilder>> + { CallBuilder { env: Default::default(), callee: self.callee, diff --git a/crates/lang/codegen/src/generator/as_dependency/call_builder.rs b/crates/lang/codegen/src/generator/as_dependency/call_builder.rs index 5f40dd63337..757d15ae2f5 100644 --- a/crates/lang/codegen/src/generator/as_dependency/call_builder.rs +++ b/crates/lang/codegen/src/generator/as_dependency/call_builder.rs @@ -372,7 +372,8 @@ impl CallBuilder<'_> { let arg_list = generator::generate_argument_list(input_types.iter().cloned()); let mut_tok = callable.receiver().is_ref_mut().then(|| quote! { mut }); let output = message.output(); - let return_type = output.map_or_else(|| quote! { () }, |output| quote! { #output }); + let return_type = + output.map_or_else(|| quote! { () }, |output| quote! { #output }); let output_span = output.span(); let output_type = quote_spanned!(output_span=> ::ink_env::call::CallBuilder< diff --git a/crates/lang/codegen/src/generator/trait_def/call_builder.rs b/crates/lang/codegen/src/generator/trait_def/call_builder.rs index 7ef5f88bf7f..21414335e8b 100644 --- a/crates/lang/codegen/src/generator/trait_def/call_builder.rs +++ b/crates/lang/codegen/src/generator/trait_def/call_builder.rs @@ -365,7 +365,8 @@ impl CallBuilder<'_> { .filter_attr(message.attrs()); let output_ident = generator::output_ident(message_ident); let output = message.output(); - let output_type = output.map_or_else(|| quote! { () }, |output| quote! { #output }); + let output_type = + output.map_or_else(|| quote! { () }, |output| quote! { #output }); let selector_bytes = selector.hex_lits(); let input_bindings = generator::input_bindings(message.inputs()); let input_types = generator::input_types(message.inputs()); From 90fb48cc16a7e5516aff351794603f4c897839f8 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 7 Mar 2022 13:07:04 +0000 Subject: [PATCH 08/12] Remove some unused ReturnType usage --- crates/lang/src/env_access.rs | 2 +- examples/erc1155/lib.rs | 1 - examples/multisig/lib.rs | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/lang/src/env_access.rs b/crates/lang/src/env_access.rs index a8f214b74db..fc5a3e6c9ae 100644 --- a/crates/lang/src/env_access.rs +++ b/crates/lang/src/env_access.rs @@ -514,7 +514,7 @@ where /// # pub mod my_contract { /// use ink_env::{ /// DefaultEnvironment, - /// call::{build_call, Selector, ExecutionInput, utils::ReturnType} + /// call::{build_call, Selector, ExecutionInput} /// }; /// /// # diff --git a/examples/erc1155/lib.rs b/examples/erc1155/lib.rs index 77c03d6d298..7060f57e9e5 100644 --- a/examples/erc1155/lib.rs +++ b/examples/erc1155/lib.rs @@ -387,7 +387,6 @@ mod erc1155 { { use ink_env::call::{ build_call, - utils::ReturnType, ExecutionInput, Selector, }; diff --git a/examples/multisig/lib.rs b/examples/multisig/lib.rs index d44a9620ff0..e6e643e8d9b 100755 --- a/examples/multisig/lib.rs +++ b/examples/multisig/lib.rs @@ -66,7 +66,6 @@ use ink_lang as ink; mod multisig { use ink_env::call::{ build_call, - utils::ReturnType, ExecutionInput, }; use ink_prelude::vec::Vec; From c0cccb74270f4ef9b92ea3dbde7a06b25f1bf091 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 7 Mar 2022 17:28:07 +0000 Subject: [PATCH 09/12] Default to `returns::<()>()`, remove ReturnType usage in api --- crates/env/src/api.rs | 3 +-- crates/env/src/backend.rs | 3 +-- crates/env/src/call/call_builder.rs | 24 ++++++++++++++++++++++-- crates/env/src/engine/off_chain/impls.rs | 3 +-- crates/env/src/engine/on_chain/impls.rs | 3 +-- crates/lang/src/env_access.rs | 3 +-- 6 files changed, 27 insertions(+), 12 deletions(-) diff --git a/crates/env/src/api.rs b/crates/env/src/api.rs index 1fbd6a10cce..fbe20b13169 100644 --- a/crates/env/src/api.rs +++ b/crates/env/src/api.rs @@ -21,7 +21,6 @@ use crate::{ TypedEnvBackend, }, call::{ - utils::ReturnType, CallParams, CreateParams, }, @@ -232,7 +231,7 @@ pub fn clear_contract_storage(key: &Key) { /// - If the called contract ran out of gas upon execution. /// - If the returned value failed to decode properly. pub fn invoke_contract( - params: &CallParams>, + params: &CallParams, ) -> Result where T: Environment, diff --git a/crates/env/src/backend.rs b/crates/env/src/backend.rs index 967a476cbd1..5fed490a2c9 100644 --- a/crates/env/src/backend.rs +++ b/crates/env/src/backend.rs @@ -14,7 +14,6 @@ use crate::{ call::{ - utils::ReturnType, CallParams, CreateParams, }, @@ -366,7 +365,7 @@ pub trait TypedEnvBackend: EnvBackend { /// For more details visit: [`invoke_contract`][`crate::invoke_contract`] fn invoke_contract( &mut self, - call_data: &CallParams>, + call_data: &CallParams, ) -> Result where T: Environment, diff --git a/crates/env/src/call/call_builder.rs b/crates/env/src/call/call_builder.rs index 13921f0cbaa..3bef7651301 100644 --- a/crates/env/src/call/call_builder.rs +++ b/crates/env/src/call/call_builder.rs @@ -84,7 +84,7 @@ where } } -impl CallParams> +impl CallParams where E: Environment, Args: scale::Encode, @@ -378,7 +378,7 @@ impl GasLimit, TransferredValue, Set>, - Set, + Set>, > where E: Environment, @@ -429,6 +429,26 @@ where } } +impl + CallBuilder< + E, + Set, + GasLimit, + TransferredValue, + Unset>, + Unset>, + > +where + E: Environment, + GasLimit: Unwrap, + TransferredValue: Unwrap, +{ + /// Invokes the cross-chain function call. + pub fn fire(self) -> Result<(), Error> { + self.params().invoke() + } +} + impl CallBuilder< E, diff --git a/crates/env/src/engine/off_chain/impls.rs b/crates/env/src/engine/off_chain/impls.rs index f3f65d1a7fc..d8a98edb6a5 100644 --- a/crates/env/src/engine/off_chain/impls.rs +++ b/crates/env/src/engine/off_chain/impls.rs @@ -15,7 +15,6 @@ use super::EnvInstance; use crate::{ call::{ - utils::ReturnType, CallParams, CreateParams, }, @@ -389,7 +388,7 @@ impl TypedEnvBackend for EnvInstance { fn invoke_contract( &mut self, - params: &CallParams>, + params: &CallParams, ) -> Result where T: Environment, diff --git a/crates/env/src/engine/on_chain/impls.rs b/crates/env/src/engine/on_chain/impls.rs index 34c9ed7e775..548bf0dab7e 100644 --- a/crates/env/src/engine/on_chain/impls.rs +++ b/crates/env/src/engine/on_chain/impls.rs @@ -20,7 +20,6 @@ use super::{ }; use crate::{ call::{ - utils::ReturnType, CallParams, CreateParams, }, @@ -359,7 +358,7 @@ impl TypedEnvBackend for EnvInstance { fn invoke_contract( &mut self, - params: &CallParams>, + params: &CallParams, ) -> Result where T: Environment, diff --git a/crates/lang/src/env_access.rs b/crates/lang/src/env_access.rs index fc5a3e6c9ae..92a79814db2 100644 --- a/crates/lang/src/env_access.rs +++ b/crates/lang/src/env_access.rs @@ -16,7 +16,6 @@ use crate::ChainExtensionInstance; use core::marker::PhantomData; use ink_env::{ call::{ - utils::ReturnType, CallParams, CreateParams, }, @@ -554,7 +553,7 @@ where /// For more details visit: [`ink_env::invoke_contract`] pub fn invoke_contract( self, - params: &CallParams>, + params: &CallParams, ) -> Result where Args: scale::Encode, From 0c7f1c9722c7b80217553658e8a4937c483692a0 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 7 Mar 2022 17:28:46 +0000 Subject: [PATCH 10/12] Fmt --- crates/env/src/api.rs | 4 +--- crates/lang/src/env_access.rs | 5 +---- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/crates/env/src/api.rs b/crates/env/src/api.rs index fbe20b13169..b5d351448de 100644 --- a/crates/env/src/api.rs +++ b/crates/env/src/api.rs @@ -230,9 +230,7 @@ pub fn clear_contract_storage(key: &Key) { /// - If the called contract execution has trapped. /// - If the called contract ran out of gas upon execution. /// - If the returned value failed to decode properly. -pub fn invoke_contract( - params: &CallParams, -) -> Result +pub fn invoke_contract(params: &CallParams) -> Result where T: Environment, Args: scale::Encode, diff --git a/crates/lang/src/env_access.rs b/crates/lang/src/env_access.rs index 92a79814db2..572a529795a 100644 --- a/crates/lang/src/env_access.rs +++ b/crates/lang/src/env_access.rs @@ -551,10 +551,7 @@ where /// # Note /// /// For more details visit: [`ink_env::invoke_contract`] - pub fn invoke_contract( - self, - params: &CallParams, - ) -> Result + pub fn invoke_contract(self, params: &CallParams) -> Result where Args: scale::Encode, R: scale::Decode, From 0fb2507e8e8c5fe93421cc89a99155bcce2db3cb Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 7 Mar 2022 17:48:26 +0000 Subject: [PATCH 11/12] Fix UI test --- .../tests/ui/contract/fail/message-input-non-codec.stderr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/lang/tests/ui/contract/fail/message-input-non-codec.stderr b/crates/lang/tests/ui/contract/fail/message-input-non-codec.stderr index 9299baed8f3..853f789bf6d 100644 --- a/crates/lang/tests/ui/contract/fail/message-input-non-codec.stderr +++ b/crates/lang/tests/ui/contract/fail/message-input-non-codec.stderr @@ -32,11 +32,11 @@ note: required by a bound in `ExecutionInput::>::push_arg` -error[E0599]: the method `fire` exists for struct `ink_env::call::CallBuilder, Unset, Unset, Set, ArgumentList>>>, Set<()>>`, but its trait bounds were not satisfied +error[E0599]: the method `fire` exists for struct `ink_env::call::CallBuilder, Unset, Unset, Set, ArgumentList>>>, Set>>`, but its trait bounds were not satisfied --> tests/ui/contract/fail/message-input-non-codec.rs:18:9 | 18 | pub fn message(&self, _input: NonCodecType) {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ method cannot be called on `ink_env::call::CallBuilder, Unset, Unset, Set, ArgumentList>>>, Set<()>>` due to unsatisfied trait bounds + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ method cannot be called on `ink_env::call::CallBuilder, Unset, Unset, Set, ArgumentList>>>, Set>>` due to unsatisfied trait bounds | ::: $WORKSPACE/crates/env/src/call/execution_input.rs | From 0148b1323fe6ec32ad39ef9f7bdc284f7685d3b7 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 7 Mar 2022 17:49:04 +0000 Subject: [PATCH 12/12] Another UI test --- .../tests/ui/trait_def/fail/message_input_non_codec.stderr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/lang/tests/ui/trait_def/fail/message_input_non_codec.stderr b/crates/lang/tests/ui/trait_def/fail/message_input_non_codec.stderr index 2b0fe21ef79..75e38274408 100644 --- a/crates/lang/tests/ui/trait_def/fail/message_input_non_codec.stderr +++ b/crates/lang/tests/ui/trait_def/fail/message_input_non_codec.stderr @@ -25,12 +25,12 @@ note: required by a bound in `ExecutionInput::>::push_arg` -error[E0599]: the method `fire` exists for struct `CallBuilder::AccountId>, Unset, Unset<::Balance>, Set, ArgumentList>>>, Set<()>>`, but its trait bounds were not satisfied +error[E0599]: the method `fire` exists for struct `CallBuilder::AccountId>, Unset, Unset<::Balance>, Set, ArgumentList>>>, Set>>`, but its trait bounds were not satisfied --> tests/ui/trait_def/fail/message_input_non_codec.rs:7:5 | 7 | / #[ink(message)] 8 | | fn message(&self, input: NonCodec); - | |_______________________________________^ method cannot be called on `CallBuilder::AccountId>, Unset, Unset<::Balance>, Set, ArgumentList>>>, Set<()>>` due to unsatisfied trait bounds + | |_______________________________________^ method cannot be called on `CallBuilder::AccountId>, Unset, Unset<::Balance>, Set, ArgumentList>>>, Set>>` due to unsatisfied trait bounds | ::: $WORKSPACE/crates/env/src/call/execution_input.rs |