Skip to content

Commit

Permalink
BlockchainWrapper - caller & sc address handle optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Mar 23, 2024
1 parent 0027e4a commit 32817dd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
3 changes: 3 additions & 0 deletions framework/base/src/api/managed_types/const_handles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub const CALLBACK_CLOSURE_ARGS_BUFFER: RawHandle = -23;
pub const MBUF_TEMPORARY_1: RawHandle = -25;
pub const MBUF_TEMPORARY_2: RawHandle = -26;

pub const ADDRESS_CALLER: RawHandle = -30;
pub const ADDRESS_SELF: RawHandle = -31;

pub const NEW_HANDLE_START_FROM: RawHandle = -100; // > -100 reserved for APIs

/// Used as a flag. Do not use as a regular handle.
Expand Down
18 changes: 9 additions & 9 deletions framework/base/src/contract_base/wrappers/blockchain_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ where

#[inline]
pub fn get_caller(&self) -> ManagedAddress<A> {
let handle: A::ManagedBufferHandle = use_raw_handle(A::static_var_api_impl().next_handle());
A::blockchain_api_impl().load_caller_managed(handle.clone());
ManagedAddress::from_handle(handle)
let caller_handle: A::ManagedBufferHandle = use_raw_handle(const_handles::ADDRESS_CALLER);
A::blockchain_api_impl().load_caller_managed(caller_handle.clone());
ManagedAddress::from_handle(caller_handle)
}

#[deprecated(since = "0.41.0", note = "Please use method `get_sc_address` instead.")]
Expand All @@ -63,9 +63,9 @@ where

#[inline]
pub fn get_sc_address(&self) -> ManagedAddress<A> {
let handle: A::ManagedBufferHandle = use_raw_handle(A::static_var_api_impl().next_handle());
A::blockchain_api_impl().load_sc_address_managed(handle.clone());
ManagedAddress::from_handle(handle)
let sc_address_handle: A::ManagedBufferHandle = use_raw_handle(const_handles::ADDRESS_SELF);
A::blockchain_api_impl().load_sc_address_managed(sc_address_handle.clone());
ManagedAddress::from_handle(sc_address_handle)
}

#[inline]
Expand All @@ -82,9 +82,9 @@ where
}

pub fn check_caller_is_user_account(&self) {
let mbuf_temp_1: A::ManagedBufferHandle = use_raw_handle(const_handles::MBUF_TEMPORARY_1);
A::blockchain_api_impl().load_caller_managed(mbuf_temp_1.clone());
if A::blockchain_api_impl().is_smart_contract(mbuf_temp_1) {
let caller_handle: A::ManagedBufferHandle = use_raw_handle(const_handles::ADDRESS_CALLER);
A::blockchain_api_impl().load_caller_managed(caller_handle.clone());
if A::blockchain_api_impl().is_smart_contract(caller_handle) {
A::error_api_impl().signal_error(ONLY_USER_ACCOUNT_CALLER);
}
}
Expand Down

0 comments on commit 32817dd

Please sign in to comment.