Skip to content

Commit 1bfccc7

Browse files
authored
Optimise deny_payment. Use eerywhere semantic of deny. (#1267)
1 parent 1ebfc9a commit 1bfccc7

File tree

4 files changed

+13
-23
lines changed

4 files changed

+13
-23
lines changed

crates/lang/codegen/src/generator/dispatch.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,8 @@ impl Dispatch<'_> {
532532
}
533533
}
534534
};
535+
let any_constructor_accept_payment =
536+
self.any_constructor_accepts_payment_expr(constructor_spans);
535537

536538
let constructor_execute = (0..count_constructors).map(|index| {
537539
let constructor_span = constructor_spans[index];
@@ -543,9 +545,8 @@ impl Dispatch<'_> {
543545
}>>::IDS[#index]
544546
}>>::CALLABLE
545547
);
546-
let accepts_payment = quote_spanned!(constructor_span=>
547-
false ||
548-
<#storage_ident as ::ink_lang::reflect::DispatchableConstructorInfo<{
548+
let deny_payment = quote_spanned!(constructor_span=>
549+
!<#storage_ident as ::ink_lang::reflect::DispatchableConstructorInfo<{
549550
<#storage_ident as ::ink_lang::reflect::ContractDispatchableConstructors<{
550551
<#storage_ident as ::ink_lang::reflect::ContractAmountDispatchables>::CONSTRUCTORS
551552
}>>::IDS[#index]
@@ -554,10 +555,12 @@ impl Dispatch<'_> {
554555

555556
quote_spanned!(constructor_span=>
556557
Self::#constructor_ident(input) => {
558+
if #any_constructor_accept_payment && #deny_payment {
559+
::ink_lang::codegen::deny_payment::<
560+
<#storage_ident as ::ink_lang::reflect::ContractEnv>::Env>()?;
561+
}
562+
557563
::ink_lang::codegen::execute_constructor::<#storage_ident, _, _>(
558-
::ink_lang::codegen::ExecuteConstructorConfig {
559-
payable: #accepts_payment,
560-
},
561564
move || { #constructor_callable(input) }
562565
)
563566
}
@@ -692,6 +695,8 @@ impl Dispatch<'_> {
692695
}
693696
}
694697
};
698+
let any_message_accept_payment =
699+
self.any_message_accepts_payment_expr(message_spans);
695700

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

732-
if #deny_payment {
737+
if #any_message_accept_payment && #deny_payment {
733738
::ink_lang::codegen::deny_payment::<
734739
<#storage_ident as ::ink_lang::reflect::ContractEnv>::Env>()?;
735740
}

crates/lang/src/codegen/dispatch/execution.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,33 +64,20 @@ where
6464
Ok(())
6565
}
6666

67-
/// Configuration for execution of ink! constructor.
68-
#[derive(Debug, Copy, Clone)]
69-
pub struct ExecuteConstructorConfig {
70-
/// Yields `true` if the ink! constructor accepts payment.
71-
pub payable: bool,
72-
}
73-
7467
/// Executes the given ink! constructor.
7568
///
7669
/// # Note
7770
///
7871
/// The closure is supposed to already contain all the arguments that the real
7972
/// constructor message requires and forwards them.
8073
#[inline]
81-
pub fn execute_constructor<Contract, F, R>(
82-
config: ExecuteConstructorConfig,
83-
f: F,
84-
) -> Result<(), DispatchError>
74+
pub fn execute_constructor<Contract, F, R>(f: F) -> Result<(), DispatchError>
8575
where
8676
Contract: SpreadLayout + ContractRootKey + ContractEnv,
8777
F: FnOnce() -> R,
8878
<private::Seal<R> as ConstructorReturnType<Contract>>::ReturnValue: scale::Encode,
8979
private::Seal<R>: ConstructorReturnType<Contract>,
9080
{
91-
if !config.payable {
92-
deny_payment::<<Contract as ContractEnv>::Env>()?;
93-
}
9481
let result = ManuallyDrop::new(private::Seal(f()));
9582
match result.as_result() {
9683
Ok(contract) => {

crates/lang/src/codegen/dispatch/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ pub use self::{
2222
execute_constructor,
2323
initialize_contract,
2424
ContractRootKey,
25-
ExecuteConstructorConfig,
2625
},
2726
info::ContractCallBuilder,
2827
type_check::{

crates/lang/src/codegen/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ pub use self::{
3030
ContractRootKey,
3131
DispatchInput,
3232
DispatchOutput,
33-
ExecuteConstructorConfig,
3433
},
3534
env::{
3635
Env,

0 commit comments

Comments
 (0)