Skip to content

Commit

Permalink
Optimise deny_payment. Use eerywhere semantic of deny. (#1267)
Browse files Browse the repository at this point in the history
  • Loading branch information
xgreenx committed May 23, 2022
1 parent 1ebfc9a commit 1bfccc7
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 23 deletions.
19 changes: 12 additions & 7 deletions crates/lang/codegen/src/generator/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,8 @@ impl Dispatch<'_> {
}
}
};
let any_constructor_accept_payment =
self.any_constructor_accepts_payment_expr(constructor_spans);

let constructor_execute = (0..count_constructors).map(|index| {
let constructor_span = constructor_spans[index];
Expand All @@ -543,9 +545,8 @@ impl Dispatch<'_> {
}>>::IDS[#index]
}>>::CALLABLE
);
let accepts_payment = quote_spanned!(constructor_span=>
false ||
<#storage_ident as ::ink_lang::reflect::DispatchableConstructorInfo<{
let deny_payment = quote_spanned!(constructor_span=>
!<#storage_ident as ::ink_lang::reflect::DispatchableConstructorInfo<{
<#storage_ident as ::ink_lang::reflect::ContractDispatchableConstructors<{
<#storage_ident as ::ink_lang::reflect::ContractAmountDispatchables>::CONSTRUCTORS
}>>::IDS[#index]
Expand All @@ -554,10 +555,12 @@ impl Dispatch<'_> {

quote_spanned!(constructor_span=>
Self::#constructor_ident(input) => {
if #any_constructor_accept_payment && #deny_payment {
::ink_lang::codegen::deny_payment::<
<#storage_ident as ::ink_lang::reflect::ContractEnv>::Env>()?;
}

::ink_lang::codegen::execute_constructor::<#storage_ident, _, _>(
::ink_lang::codegen::ExecuteConstructorConfig {
payable: #accepts_payment,
},
move || { #constructor_callable(input) }
)
}
Expand Down Expand Up @@ -692,6 +695,8 @@ impl Dispatch<'_> {
}
}
};
let any_message_accept_payment =
self.any_message_accepts_payment_expr(message_spans);

let message_execute = (0..count_messages).map(|index| {
let message_span = message_spans[index];
Expand Down Expand Up @@ -729,7 +734,7 @@ impl Dispatch<'_> {
Self::#message_ident(input) => {
use ::core::default::Default;

if #deny_payment {
if #any_message_accept_payment && #deny_payment {
::ink_lang::codegen::deny_payment::<
<#storage_ident as ::ink_lang::reflect::ContractEnv>::Env>()?;
}
Expand Down
15 changes: 1 addition & 14 deletions crates/lang/src/codegen/dispatch/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,33 +64,20 @@ where
Ok(())
}

/// Configuration for execution of ink! constructor.
#[derive(Debug, Copy, Clone)]
pub struct ExecuteConstructorConfig {
/// Yields `true` if the ink! constructor accepts payment.
pub payable: bool,
}

/// Executes the given ink! constructor.
///
/// # Note
///
/// The closure is supposed to already contain all the arguments that the real
/// constructor message requires and forwards them.
#[inline]
pub fn execute_constructor<Contract, F, R>(
config: ExecuteConstructorConfig,
f: F,
) -> Result<(), DispatchError>
pub fn execute_constructor<Contract, F, R>(f: F) -> Result<(), DispatchError>
where
Contract: SpreadLayout + ContractRootKey + ContractEnv,
F: FnOnce() -> R,
<private::Seal<R> as ConstructorReturnType<Contract>>::ReturnValue: scale::Encode,
private::Seal<R>: ConstructorReturnType<Contract>,
{
if !config.payable {
deny_payment::<<Contract as ContractEnv>::Env>()?;
}
let result = ManuallyDrop::new(private::Seal(f()));
match result.as_result() {
Ok(contract) => {
Expand Down
1 change: 0 additions & 1 deletion crates/lang/src/codegen/dispatch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ pub use self::{
execute_constructor,
initialize_contract,
ContractRootKey,
ExecuteConstructorConfig,
},
info::ContractCallBuilder,
type_check::{
Expand Down
1 change: 0 additions & 1 deletion crates/lang/src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ pub use self::{
ContractRootKey,
DispatchInput,
DispatchOutput,
ExecuteConstructorConfig,
},
env::{
Env,
Expand Down

0 comments on commit 1bfccc7

Please sign in to comment.