Skip to content

Commit

Permalink
ManagedType from_handle marked unsafe, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Nov 9, 2024
1 parent 36aedd4 commit 59047ab
Show file tree
Hide file tree
Showing 40 changed files with 572 additions and 387 deletions.
23 changes: 10 additions & 13 deletions contracts/feature-tests/basic-features/src/storage_direct_load.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
multiversx_sc::imports!();

use multiversx_sc::api::{use_raw_handle, HandleTypeInfo};

use crate::types::*;

/// Storage tests: direct load.
Expand Down Expand Up @@ -78,16 +76,15 @@ pub trait StorageLoadFeatures {
#[endpoint]
fn load_from_address_raw(&self, address: ManagedAddress, key: ManagedBuffer) -> ManagedBuffer {
// TODO: maybe wrap this kind of functionality in a StorageRawWrapper
use multiversx_sc::api::{
StaticVarApi, StaticVarApiImpl, StorageReadApi, StorageReadApiImpl,
};
let value_handle: <<Self as ContractBase>::Api as HandleTypeInfo>::ManagedBufferHandle =
use_raw_handle(Self::Api::static_var_api_impl().next_handle());
Self::Api::storage_read_api_impl().storage_load_from_address(
address.get_handle(),
key.get_handle(),
value_handle.clone(),
);
ManagedBuffer::from_handle(value_handle)
use multiversx_sc::api::{StorageReadApi, StorageReadApiImpl};
unsafe {
let value = ManagedBuffer::new_uninit();
Self::Api::storage_read_api_impl().storage_load_from_address(
address.get_handle(),
key.get_handle(),
value.get_handle(),
);
value
}
}
}
4 changes: 3 additions & 1 deletion contracts/modules/src/esdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ pub trait EsdtModule {
fn nft_create<T: TopEncode>(&self, amount: &BigUint, attributes: &T) -> u64 {
let token_id = self.token_id().get();
let empty_buffer = ManagedBuffer::new();
let empty_vec = ManagedVec::from_handle(empty_buffer.get_handle());

// sneakily reuses the same handle
let empty_vec = unsafe { ManagedRef::wrap_handle(empty_buffer.get_handle()) };

self.send().esdt_nft_create(
&token_id,
Expand Down
139 changes: 83 additions & 56 deletions framework/base/src/contract_base/wrappers/blockchain_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::{
storage::{self},
types::{
BackTransfers, BigUint, CodeMetadata, EgldOrEsdtTokenIdentifier, EsdtLocalRoleFlags,
EsdtTokenData, EsdtTokenType, ManagedAddress, ManagedBuffer, ManagedByteArray, ManagedType,
ManagedVec, TokenIdentifier,
EsdtTokenData, EsdtTokenType, ManagedAddress, ManagedBuffer, ManagedByteArray,
ManagedRefMut, ManagedType, ManagedVec, TokenIdentifier,
},
};

Expand Down Expand Up @@ -49,9 +49,11 @@ 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)
unsafe {
let result = ManagedAddress::new_uninit();
A::blockchain_api_impl().load_caller_managed(result.get_handle());
result
}
}

#[deprecated(since = "0.41.0", note = "Please use method `get_sc_address` instead.")]
Expand All @@ -63,16 +65,20 @@ 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)
unsafe {
let result = ManagedAddress::new_uninit();
A::blockchain_api_impl().load_sc_address_managed(result.get_handle());
result
}
}

#[inline]
pub fn get_owner_address(&self) -> ManagedAddress<A> {
let handle: A::ManagedBufferHandle = use_raw_handle(A::static_var_api_impl().next_handle());
A::blockchain_api_impl().load_owner_address_managed(handle.clone());
ManagedAddress::from_handle(handle)
unsafe {
let result = ManagedAddress::new_uninit();
A::blockchain_api_impl().load_owner_address_managed(result.get_handle());
result
}
}

pub fn check_caller_is_owner(&self) {
Expand Down Expand Up @@ -123,16 +129,20 @@ where
#[cfg(feature = "alloc")]
#[inline]
pub fn get_balance_legacy(&self, address: &crate::types::Address) -> BigUint<A> {
let handle: A::BigIntHandle = use_raw_handle(A::static_var_api_impl().next_handle());
A::blockchain_api_impl().load_balance_legacy(handle.clone(), address);
BigUint::from_handle(handle)
unsafe {
let result = BigUint::new_uninit();
A::blockchain_api_impl().load_balance_legacy(result.get_handle(), address);
result
}
}

#[inline]
pub fn get_balance(&self, address: &ManagedAddress<A>) -> BigUint<A> {
let handle: A::BigIntHandle = use_raw_handle(A::static_var_api_impl().next_handle());
A::blockchain_api_impl().load_balance(handle.clone(), address.get_handle());
BigUint::from_handle(handle)
unsafe {
let result = BigUint::new_uninit();
A::blockchain_api_impl().load_balance(result.get_handle(), address.get_handle());
result
}
}

#[inline]
Expand All @@ -141,7 +151,10 @@ where
A::blockchain_api_impl()
.managed_get_code_metadata(address.get_handle(), mbuf_temp_1.clone());
let mut buffer = [0u8; 2];
ManagedBuffer::<A>::from_handle(mbuf_temp_1).load_to_byte_array(&mut buffer);
unsafe {
ManagedRefMut::<'static, A, ManagedBuffer<A>>::wrap_handle(mbuf_temp_1)
.load_to_byte_array(&mut buffer);
}
CodeMetadata::from(buffer)
}

Expand Down Expand Up @@ -173,9 +186,11 @@ where

#[inline]
pub fn get_state_root_hash(&self) -> ManagedByteArray<A, 32> {
let handle: A::ManagedBufferHandle = use_raw_handle(A::static_var_api_impl().next_handle());
A::blockchain_api_impl().load_state_root_hash_managed(handle.clone());
ManagedByteArray::from_handle(handle)
unsafe {
let result = ManagedByteArray::new_uninit();
A::blockchain_api_impl().load_state_root_hash_managed(result.get_handle());
result
}
}

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

#[inline]
pub fn get_tx_hash(&self) -> ManagedByteArray<A, 32> {
let handle: A::ManagedBufferHandle = use_raw_handle(A::static_var_api_impl().next_handle());
A::blockchain_api_impl().load_tx_hash_managed(handle.clone());
ManagedByteArray::from_handle(handle)
unsafe {
let result = ManagedByteArray::new_uninit();
A::blockchain_api_impl().load_tx_hash_managed(result.get_handle());
result
}
}

#[inline]
Expand Down Expand Up @@ -229,9 +246,11 @@ where

#[inline]
pub fn get_block_random_seed(&self) -> ManagedByteArray<A, 48> {
let handle: A::ManagedBufferHandle = use_raw_handle(A::static_var_api_impl().next_handle());
A::blockchain_api_impl().load_block_random_seed_managed(handle.clone());
ManagedByteArray::from_handle(handle)
unsafe {
let result = ManagedByteArray::new_uninit();
A::blockchain_api_impl().load_block_random_seed_managed(result.get_handle());
result
}
}

#[inline]
Expand Down Expand Up @@ -266,9 +285,11 @@ where

#[inline]
pub fn get_prev_block_random_seed(&self) -> ManagedByteArray<A, 48> {
let handle: A::ManagedBufferHandle = use_raw_handle(A::static_var_api_impl().next_handle());
A::blockchain_api_impl().load_prev_block_random_seed_managed(handle.clone());
ManagedByteArray::from_handle(handle)
unsafe {
let result = ManagedByteArray::new_uninit();
A::blockchain_api_impl().load_prev_block_random_seed_managed(result.get_handle());
result
}
}

#[inline]
Expand All @@ -288,14 +309,16 @@ where
token_id: &TokenIdentifier<A>,
nonce: u64,
) -> BigUint<A> {
let result_handle: A::BigIntHandle = use_raw_handle(A::static_var_api_impl().next_handle());
A::blockchain_api_impl().load_esdt_balance(
address.get_handle(),
token_id.get_handle(),
nonce,
result_handle.clone(),
);
BigUint::from_handle(result_handle)
unsafe {
let result = BigUint::new_uninit();
A::blockchain_api_impl().load_esdt_balance(
address.get_handle(),
token_id.get_handle(),
nonce,
result.get_handle(),
);
result
}
}

pub fn get_esdt_token_data(
Expand Down Expand Up @@ -346,16 +369,18 @@ where
let _ = managed_api_impl.mb_load_slice(properties_handle, 0, &mut properties_bytes[..]);
let frozen = esdt_is_frozen(&properties_bytes);

EsdtTokenData {
token_type,
amount: BigUint::from_raw_handle(value_handle.get_raw_handle()),
frozen,
hash: ManagedBuffer::from_raw_handle(hash_handle.get_raw_handle()),
name: ManagedBuffer::from_raw_handle(name_handle.get_raw_handle()),
attributes: ManagedBuffer::from_raw_handle(attributes_handle.get_raw_handle()),
creator: ManagedAddress::from_raw_handle(creator_handle.get_raw_handle()),
royalties: BigUint::from_raw_handle(royalties_handle.get_raw_handle()),
uris: ManagedVec::from_raw_handle(uris_handle.get_raw_handle()),
unsafe {
EsdtTokenData {
token_type,
amount: BigUint::from_raw_handle(value_handle.get_raw_handle()),
frozen,
hash: ManagedBuffer::from_raw_handle(hash_handle.get_raw_handle()),
name: ManagedBuffer::from_raw_handle(name_handle.get_raw_handle()),
attributes: ManagedBuffer::from_raw_handle(attributes_handle.get_raw_handle()),
creator: ManagedAddress::from_raw_handle(creator_handle.get_raw_handle()),
royalties: BigUint::from_raw_handle(royalties_handle.get_raw_handle()),
uris: ManagedVec::from_raw_handle(uris_handle.get_raw_handle()),
}
}
}

Expand All @@ -375,9 +400,13 @@ where
call_value_handle.get_raw_handle(),
);

BackTransfers {
total_egld_amount: BigUint::from_raw_handle(call_value_handle.get_raw_handle()),
esdt_payments: ManagedVec::from_raw_handle(esdt_transfer_value_handle.get_raw_handle()),
unsafe {
BackTransfers {
total_egld_amount: BigUint::from_raw_handle(call_value_handle.get_raw_handle()),
esdt_payments: ManagedVec::from_raw_handle(
esdt_transfer_value_handle.get_raw_handle(),
),
}
}
}

Expand Down Expand Up @@ -441,13 +470,11 @@ where
// load value
A::storage_read_api_impl()
.storage_load_managed_buffer_raw(temp_handle_1, temp_handle_2.clone());
let result_handle: A::BigIntHandle = use_raw_handle(A::static_var_api_impl().next_handle());

// convert value to BigUint
A::managed_type_impl().mb_to_big_int_unsigned(temp_handle_2, result_handle.clone());

//wrap
BigUint::from_handle(result_handle)
let result = unsafe { BigUint::new_uninit() };
A::managed_type_impl().mb_to_big_int_unsigned(temp_handle_2, result.get_handle());
result
}
}

Expand Down
49 changes: 25 additions & 24 deletions framework/base/src/contract_base/wrappers/crypto_wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use core::marker::PhantomData;

use crate::{
api::{
use_raw_handle, CryptoApi, CryptoApiImpl, StaticVarApiImpl, KECCAK256_RESULT_LEN,
SHA256_RESULT_LEN,
},
api::{CryptoApi, CryptoApiImpl, KECCAK256_RESULT_LEN, SHA256_RESULT_LEN},
types::{ManagedBuffer, ManagedByteArray, ManagedType, ManagedVec, MessageHashType},
};

Expand All @@ -30,30 +27,33 @@ where
&self,
data: B,
) -> ManagedByteArray<A, SHA256_RESULT_LEN> {
let new_handle: A::ManagedBufferHandle =
use_raw_handle(A::static_var_api_impl().next_handle());
A::crypto_api_impl().sha256_managed(new_handle.clone(), data.borrow().get_handle());
ManagedByteArray::from_handle(new_handle)
unsafe {
let result = ManagedByteArray::new_uninit();
A::crypto_api_impl().sha256_managed(result.get_handle(), data.borrow().get_handle());
result
}
}

pub fn keccak256<B: core::borrow::Borrow<ManagedBuffer<A>>>(
&self,
data: B,
) -> ManagedByteArray<A, KECCAK256_RESULT_LEN> {
let new_handle: A::ManagedBufferHandle =
use_raw_handle(A::static_var_api_impl().next_handle());
A::crypto_api_impl().keccak256_managed(new_handle.clone(), data.borrow().get_handle());
ManagedByteArray::from_handle(new_handle)
unsafe {
let result = ManagedByteArray::new_uninit();
A::crypto_api_impl().keccak256_managed(result.get_handle(), data.borrow().get_handle());
result
}
}

pub fn ripemd160<B: core::borrow::Borrow<ManagedBuffer<A>>>(
&self,
data: B,
) -> ManagedByteArray<A, { crate::api::RIPEMD_RESULT_LEN }> {
let new_handle: A::ManagedBufferHandle =
use_raw_handle(A::static_var_api_impl().next_handle());
A::crypto_api_impl().ripemd160_managed(new_handle.clone(), data.borrow().get_handle());
ManagedByteArray::from_handle(new_handle)
unsafe {
let result = ManagedByteArray::new_uninit();
A::crypto_api_impl().ripemd160_managed(result.get_handle(), data.borrow().get_handle());
result
}
}

pub fn verify_bls(
Expand Down Expand Up @@ -122,14 +122,15 @@ where
r: &ManagedBuffer<A>,
s: &ManagedBuffer<A>,
) -> ManagedBuffer<A> {
let new_handle: A::ManagedBufferHandle =
use_raw_handle(A::static_var_api_impl().next_handle());
A::crypto_api_impl().encode_secp256k1_der_signature_managed(
r.get_handle(),
s.get_handle(),
new_handle.clone(),
);
ManagedBuffer::from_handle(new_handle)
unsafe {
let result = ManagedBuffer::new_uninit();
A::crypto_api_impl().encode_secp256k1_der_signature_managed(
r.get_handle(),
s.get_handle(),
result.get_handle(),
);
result
}
}

/// Calls the Vm to verify secp256r1 signature.
Expand Down
Loading

0 comments on commit 59047ab

Please sign in to comment.