From abe973a26b7e72aad1537628cb84621b1d961d56 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Thu, 18 Jul 2024 21:08:19 +0300 Subject: [PATCH 01/66] integration of executor v1.7.next1 --- Cargo.lock | 75 ++++++++++++++++---------------- framework/scenario/Cargo.toml | 4 +- vm/Cargo.toml | 4 +- vm/src/vm_hooks/vh_dispatcher.rs | 49 +++++++++++++++++++++ 4 files changed, 92 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 815470d026..5a348bfd06 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -378,15 +378,15 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "bytes" -version = "1.6.0" +version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" +checksum = "a12916984aab3fa6e39d655a33e09c0071eb36d6ab3aea5c2d78551f1df6d952" [[package]] name = "cc" -version = "1.0.104" +version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74b6a57f98764a267ff415d50a25e6e166f3831a5071af4995296ea97d210490" +checksum = "324c74f2155653c90b04f25b2a47a8a631360cb908f92a772695f430c7e31052" [[package]] name = "cfg-if" @@ -431,9 +431,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.8" +version = "4.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84b3edb18336f4df585bc9aa31dd99c036dfa5dc5e9a2939a722a188f3a8970d" +checksum = "64acc1846d54c1fe936a78dc189c34e28d3f5afc348403f28ecf53660b9b8462" dependencies = [ "clap_builder", "clap_derive", @@ -441,9 +441,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.8" +version = "4.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1c09dd5ada6c6c78075d6fd0da3f90d8080651e2d6cc8eb2f1aaa4034ced708" +checksum = "6fb8393d67ba2e7bfaf28a23458e4e2b543cc73a99595511eb207fdb8aede942" dependencies = [ "anstream", "anstyle", @@ -1378,9 +1378,9 @@ dependencies = [ [[package]] name = "http-body" -version = "1.0.0" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1cac85db508abc24a2e48553ba12a996e87244a0395ce011e62b37158745d643" +checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184" dependencies = [ "bytes", "http", @@ -1413,9 +1413,9 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" [[package]] name = "hyper" -version = "1.4.0" +version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4fe55fb7a772d59a5ff1dfbff4fe0258d19b89fec4b233e75d35d5d2316badc" +checksum = "50dfd22e0e76d0f662d429a5f80fcaf3855009297eab6a0a9f8543834744ba05" dependencies = [ "bytes", "futures-channel", @@ -1916,8 +1916,7 @@ dependencies = [ [[package]] name = "multiversx-chain-vm-executor" version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b59072fa0624b55ae5ae3fa6bfa91515bbeb4ac440214bc4a509e2c8806d6e9f" +source = "git+https://github.com/multiversx/mx-vm-executor-rs?branch=rc/v1.7.next1#95acf80fb611fb51a8b7e20f5e7086e4aae68539" [[package]] name = "multiversx-price-aggregator-sc" @@ -2718,9 +2717,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.2" +version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c82cf8cff14456045f55ec4241383baeff27af886adb72ffb2162f99911de0fd" +checksum = "2a908a6e00f1fdd0dfd9c0eb08ce85126f6d8bbda50017e74bc4a4b7d4a926a4" dependencies = [ "bitflags 2.6.0", ] @@ -2913,9 +2912,9 @@ dependencies = [ [[package]] name = "rustls" -version = "0.23.10" +version = "0.23.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05cff451f60db80f490f3c182b77c35260baace73209e9cdbbe526bfe3a4d402" +checksum = "4828ea528154ae444e5a642dbb7d5623354030dc9822b83fd9bb79683c7399d0" dependencies = [ "once_cell", "rustls-pki-types", @@ -3016,9 +3015,9 @@ dependencies = [ [[package]] name = "security-framework" -version = "2.11.0" +version = "2.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c627723fd09706bacdb5cf41499e95098555af3c3c29d014dc3c458ef6be11c0" +checksum = "897b2245f0b511c87893af39b033e5ca9cce68824c4d7e7630b5a1d339658d02" dependencies = [ "bitflags 2.6.0", "core-foundation", @@ -3029,9 +3028,9 @@ dependencies = [ [[package]] name = "security-framework-sys" -version = "2.11.0" +version = "2.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "317936bbbd05227752583946b9e66d7ce3b489f84e11a94a510b4437fef407d7" +checksum = "75da29fe9b9b08fe9d6b22b5b4bcbc75d8db3aa31e639aa56bb62e9d46bfceaf" dependencies = [ "core-foundation-sys", "libc", @@ -3078,18 +3077,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.203" +version = "1.0.204" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094" +checksum = "bc76f558e0cbb2a839d37354c575f1dc3fdc6546b5be373ba43d95f231bf7c12" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.203" +version = "1.0.204" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba" +checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222" dependencies = [ "proc-macro2", "quote", @@ -3349,18 +3348,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.61" +version = "1.0.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709" +checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.61" +version = "1.0.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533" +checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" dependencies = [ "proc-macro2", "quote", @@ -3369,9 +3368,9 @@ dependencies = [ [[package]] name = "tinyvec" -version = "1.7.0" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce6b6a2fb3a985e99cebfaefa9faa3024743da73304ca1c683a36429613d3d22" +checksum = "445e881f4f6d382d5f27c034e25eb92edd7c784ceab92a0937db7f2e9471b938" dependencies = [ "tinyvec_macros", ] @@ -3400,9 +3399,9 @@ dependencies = [ [[package]] name = "tokio" -version = "1.38.0" +version = "1.38.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba4f4a02a7a80d6f274636f0aa95c7e383b912d41fe721a31f29e29698585a4a" +checksum = "eb2caba9f80616f438e09748d5acda951967e1ea58508ef53d9c6402485a46df" dependencies = [ "backtrace", "bytes", @@ -3464,9 +3463,9 @@ dependencies = [ [[package]] name = "toml" -version = "0.8.14" +version = "0.8.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f49eb2ab21d2f26bd6db7bf383edc527a7ebaee412d17af4d40fdccd442f335" +checksum = "ac2caab0bf757388c6c0ae23b3293fdb463fee59434529014f85e3263b995c28" dependencies = [ "indexmap", "serde", @@ -3486,9 +3485,9 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.22.14" +version = "0.22.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f21c7aaf97f1bd9ca9d4f9e73b0a6c74bd5afef56f2bc931943a6e1c37e04e38" +checksum = "278f3d518e152219c994ce877758516bca5e118eaed6996192a774fb9fbf0788" dependencies = [ "indexmap", "serde", diff --git a/framework/scenario/Cargo.toml b/framework/scenario/Cargo.toml index e58e6d9255..25e43afdef 100644 --- a/framework/scenario/Cargo.toml +++ b/framework/scenario/Cargo.toml @@ -50,7 +50,9 @@ version = "0.22.3" path = "../../sdk/scenario-format" [dependencies.multiversx-chain-vm-executor] -version = "0.2.0" +# version = "0.2.0" +git = "https://github.com/multiversx/mx-vm-executor-rs" +branch = "rc/v1.7.next1" [dependencies.multiversx-chain-vm] version = "=0.9.0" diff --git a/vm/Cargo.toml b/vm/Cargo.toml index baf8ec3894..2da432d240 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -35,4 +35,6 @@ rand_seeder = { version= "0.2.2", optional = true } ed25519-dalek = { version = "1.0.1" , optional = true } [dependencies.multiversx-chain-vm-executor] -version = "0.2.0" +# version = "0.2.0" +git = "https://github.com/multiversx/mx-vm-executor-rs" +branch = "rc/v1.7.next1" diff --git a/vm/src/vm_hooks/vh_dispatcher.rs b/vm/src/vm_hooks/vh_dispatcher.rs index 0dbfc54051..46c5153819 100644 --- a/vm/src/vm_hooks/vh_dispatcher.rs +++ b/vm/src/vm_hooks/vh_dispatcher.rs @@ -1859,4 +1859,53 @@ impl VMHooks for VMHooksDispatcher { ) -> i32 { panic!("Unavailable: elliptic_curve_get_values") } + + fn is_reserved_function_name(&self, name_handle: i32) -> i32 { + panic!("Unavailable: is_reserved_function_name") + } + + fn managed_get_original_caller_addr(&self, destination_handle: i32) { + panic!("Unavailable: managed_get_original_caller_addr") + } + + fn managed_get_relayer_addr(&self, destination_handle: i32) { + panic!("Unavailable: managed_get_relayer_addr") + } + + fn managed_multi_transfer_esdt_nft_execute_by_user( + &self, + user_handle: i32, + dst_handle: i32, + token_transfers_handle: i32, + gas_limit: i64, + function_handle: i32, + arguments_handle: i32, + ) -> i32 { + panic!("Unavailable: managed_multi_transfer_esdt_nft_execute_by_user") + } + + fn managed_verify_secp256r1( + &self, + key_handle: i32, + message_handle: i32, + sig_handle: i32, + ) -> i32 { + panic!("Unavailable: managed_verify_secp256r1") + } + fn managed_verify_blssignature_share( + &self, + key_handle: i32, + message_handle: i32, + sig_handle: i32, + ) -> i32 { + panic!("Unavailable: managed_verify_blssignature_share") + } + fn managed_verify_blsaggregated_signature( + &self, + key_handle: i32, + message_handle: i32, + sig_handle: i32, + ) -> i32 { + panic!("Unavailable: managed_verify_blsaggregated_signature") + } } From 36aedd479985f5e94d11fad350ad995415b9ed3c Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Fri, 8 Nov 2024 20:54:42 +0200 Subject: [PATCH 02/66] ManagedRefMut implementation --- framework/base/src/storage/storage_key.rs | 4 + .../types/interaction/managed_arg_buffer.rs | 4 + .../base/src/types/managed/basic/big_float.rs | 4 + .../base/src/types/managed/basic/big_int.rs | 5 + .../src/types/managed/basic/elliptic_curve.rs | 4 + .../src/types/managed/basic/managed_buffer.rs | 8 +- .../src/types/managed/basic/managed_map.rs | 4 + .../src/types/managed/managed_type_trait.rs | 2 + .../multi_value/multi_value_managed_vec.rs | 4 + .../src/types/managed/wrapped/big_uint.rs | 4 + .../types/managed/wrapped/managed_address.rs | 4 + .../managed/wrapped/managed_byte_array.rs | 4 + .../types/managed/wrapped/managed_ref_mut.rs | 174 ++++++++++++++++++ .../src/types/managed/wrapped/managed_vec.rs | 7 +- .../types/managed/wrapped/managed_vec_ref.rs | 18 +- .../base/src/types/managed/wrapped/mod.rs | 2 + .../types/managed/wrapped/token_identifier.rs | 4 + 17 files changed, 249 insertions(+), 7 deletions(-) create mode 100644 framework/base/src/types/managed/wrapped/managed_ref_mut.rs diff --git a/framework/base/src/storage/storage_key.rs b/framework/base/src/storage/storage_key.rs index b56358218b..f692dd5266 100644 --- a/framework/base/src/storage/storage_key.rs +++ b/framework/base/src/storage/storage_key.rs @@ -35,6 +35,10 @@ where fn transmute_from_handle_ref(handle_ref: &A::ManagedBufferHandle) -> &Self { unsafe { core::mem::transmute(handle_ref) } } + + fn transmute_from_handle_ref_mut(handle_ref: &mut A::ManagedBufferHandle) -> &mut Self { + unsafe { core::mem::transmute(handle_ref) } + } } impl StorageKey diff --git a/framework/base/src/types/interaction/managed_arg_buffer.rs b/framework/base/src/types/interaction/managed_arg_buffer.rs index c6dae76afd..bd98c40931 100644 --- a/framework/base/src/types/interaction/managed_arg_buffer.rs +++ b/framework/base/src/types/interaction/managed_arg_buffer.rs @@ -46,6 +46,10 @@ where fn transmute_from_handle_ref(handle_ref: &M::ManagedBufferHandle) -> &Self { unsafe { core::mem::transmute(handle_ref) } } + + fn transmute_from_handle_ref_mut(handle_ref: &mut M::ManagedBufferHandle) -> &mut Self { + unsafe { core::mem::transmute(handle_ref) } + } } impl ManagedArgBuffer diff --git a/framework/base/src/types/managed/basic/big_float.rs b/framework/base/src/types/managed/basic/big_float.rs index 54850a1672..802d582e94 100644 --- a/framework/base/src/types/managed/basic/big_float.rs +++ b/framework/base/src/types/managed/basic/big_float.rs @@ -38,6 +38,10 @@ impl ManagedType for BigFloat { fn transmute_from_handle_ref(handle_ref: &M::BigFloatHandle) -> &Self { unsafe { core::mem::transmute(handle_ref) } } + + fn transmute_from_handle_ref_mut(handle_ref: &mut M::BigFloatHandle) -> &mut Self { + unsafe { core::mem::transmute(handle_ref) } + } } impl From<&ManagedBuffer> for BigFloat { diff --git a/framework/base/src/types/managed/basic/big_int.rs b/framework/base/src/types/managed/basic/big_int.rs index c705511f56..cb52117c32 100644 --- a/framework/base/src/types/managed/basic/big_int.rs +++ b/framework/base/src/types/managed/basic/big_int.rs @@ -39,6 +39,11 @@ impl ManagedType for BigInt { fn transmute_from_handle_ref(handle_ref: &M::BigIntHandle) -> &Self { unsafe { core::mem::transmute(handle_ref) } } + + fn transmute_from_handle_ref_mut(handle_ref: &mut M::BigIntHandle) -> &mut Self { + unsafe { core::mem::transmute(handle_ref) } + } + } impl Default for BigInt { diff --git a/framework/base/src/types/managed/basic/elliptic_curve.rs b/framework/base/src/types/managed/basic/elliptic_curve.rs index 99189d06c5..8c110f6459 100644 --- a/framework/base/src/types/managed/basic/elliptic_curve.rs +++ b/framework/base/src/types/managed/basic/elliptic_curve.rs @@ -46,6 +46,10 @@ impl ManagedType for EllipticCurve { fn transmute_from_handle_ref(handle_ref: &M::EllipticCurveHandle) -> &Self { unsafe { core::mem::transmute(handle_ref) } } + + fn transmute_from_handle_ref_mut(handle_ref: &mut M::EllipticCurveHandle) -> &mut Self { + unsafe { core::mem::transmute(handle_ref) } + } } impl EllipticCurve { diff --git a/framework/base/src/types/managed/basic/managed_buffer.rs b/framework/base/src/types/managed/basic/managed_buffer.rs index de2dc63bb8..34ba82da14 100644 --- a/framework/base/src/types/managed/basic/managed_buffer.rs +++ b/framework/base/src/types/managed/basic/managed_buffer.rs @@ -13,7 +13,9 @@ use crate::{ hex_util::encode_bytes_as_hex, FormatBuffer, FormatByteReceiver, SCBinary, SCDisplay, SCLowerHex, }, - types::{heap::BoxedBytes, ManagedBufferCachedBuilder, ManagedType, StaticBufferRef}, + types::{ + heap::BoxedBytes, ManagedBufferCachedBuilder, ManagedType, StaticBufferRef, + }, }; /// A byte buffer managed by an external API. @@ -37,6 +39,10 @@ impl ManagedType for ManagedBuffer { fn transmute_from_handle_ref(handle_ref: &M::ManagedBufferHandle) -> &Self { unsafe { core::mem::transmute(handle_ref) } } + + fn transmute_from_handle_ref_mut(handle_ref: &mut M::ManagedBufferHandle) -> &mut Self { + unsafe { core::mem::transmute(handle_ref) } + } } impl ManagedBuffer { diff --git a/framework/base/src/types/managed/basic/managed_map.rs b/framework/base/src/types/managed/basic/managed_map.rs index 1f0c0be867..ed39011f8d 100644 --- a/framework/base/src/types/managed/basic/managed_map.rs +++ b/framework/base/src/types/managed/basic/managed_map.rs @@ -26,6 +26,10 @@ impl ManagedType for ManagedMap { fn transmute_from_handle_ref(handle_ref: &M::ManagedMapHandle) -> &Self { unsafe { core::mem::transmute(handle_ref) } } + + fn transmute_from_handle_ref_mut(handle_ref: &mut M::ManagedMapHandle) -> &mut Self { + unsafe { core::mem::transmute(handle_ref) } + } } impl ManagedMap { diff --git a/framework/base/src/types/managed/managed_type_trait.rs b/framework/base/src/types/managed/managed_type_trait.rs index ae9b48e4d0..e7b97e2ab6 100644 --- a/framework/base/src/types/managed/managed_type_trait.rs +++ b/framework/base/src/types/managed/managed_type_trait.rs @@ -26,6 +26,8 @@ pub trait ManagedType: Sized { /// For types that just wrap another managed type it is easier, call for the wrapped object. fn transmute_from_handle_ref(handle_ref: &Self::OwnHandle) -> &Self; + fn transmute_from_handle_ref_mut(handle_ref: &mut Self::OwnHandle) -> &mut Self; + fn as_ref(&self) -> ManagedRef<'_, M, Self> { ManagedRef::new(self) } diff --git a/framework/base/src/types/managed/multi_value/multi_value_managed_vec.rs b/framework/base/src/types/managed/multi_value/multi_value_managed_vec.rs index a168711af1..e628ba8421 100644 --- a/framework/base/src/types/managed/multi_value/multi_value_managed_vec.rs +++ b/framework/base/src/types/managed/multi_value/multi_value_managed_vec.rs @@ -57,6 +57,10 @@ where fn transmute_from_handle_ref(handle_ref: &M::ManagedBufferHandle) -> &Self { unsafe { core::mem::transmute(handle_ref) } } + + fn transmute_from_handle_ref_mut(handle_ref: &mut M::ManagedBufferHandle) -> &mut Self { + unsafe { core::mem::transmute(handle_ref) } + } } impl MultiValueManagedVec diff --git a/framework/base/src/types/managed/wrapped/big_uint.rs b/framework/base/src/types/managed/wrapped/big_uint.rs index 615d790480..dfba440073 100644 --- a/framework/base/src/types/managed/wrapped/big_uint.rs +++ b/framework/base/src/types/managed/wrapped/big_uint.rs @@ -39,6 +39,10 @@ impl ManagedType for BigUint { fn transmute_from_handle_ref(handle_ref: &M::BigIntHandle) -> &Self { unsafe { core::mem::transmute(handle_ref) } } + + fn transmute_from_handle_ref_mut(handle_ref: &mut M::BigIntHandle) -> &mut Self { + unsafe { core::mem::transmute(handle_ref) } + } } impl From for BigUint { diff --git a/framework/base/src/types/managed/wrapped/managed_address.rs b/framework/base/src/types/managed/wrapped/managed_address.rs index a90482fb04..18473419e4 100644 --- a/framework/base/src/types/managed/wrapped/managed_address.rs +++ b/framework/base/src/types/managed/wrapped/managed_address.rs @@ -146,6 +146,10 @@ where fn transmute_from_handle_ref(handle_ref: &M::ManagedBufferHandle) -> &Self { unsafe { core::mem::transmute(handle_ref) } } + + fn transmute_from_handle_ref_mut(handle_ref: &mut M::ManagedBufferHandle) -> &mut Self { + unsafe { core::mem::transmute(handle_ref) } + } } impl Default for ManagedAddress diff --git a/framework/base/src/types/managed/wrapped/managed_byte_array.rs b/framework/base/src/types/managed/wrapped/managed_byte_array.rs index 83e95d332c..9d7b15d2b7 100644 --- a/framework/base/src/types/managed/wrapped/managed_byte_array.rs +++ b/framework/base/src/types/managed/wrapped/managed_byte_array.rs @@ -48,6 +48,10 @@ where fn transmute_from_handle_ref(handle_ref: &M::ManagedBufferHandle) -> &Self { unsafe { core::mem::transmute(handle_ref) } } + + fn transmute_from_handle_ref_mut(handle_ref: &mut M::ManagedBufferHandle) -> &mut Self { + unsafe { core::mem::transmute(handle_ref) } + } } impl Default for ManagedByteArray diff --git a/framework/base/src/types/managed/wrapped/managed_ref_mut.rs b/framework/base/src/types/managed/wrapped/managed_ref_mut.rs new file mode 100644 index 0000000000..81315b89b2 --- /dev/null +++ b/framework/base/src/types/managed/wrapped/managed_ref_mut.rs @@ -0,0 +1,174 @@ +use core::ops::DerefMut; +use core::{borrow::Borrow, marker::PhantomData, ops::Deref}; + +use crate::codec::{ + EncodeErrorHandler, NestedEncode, NestedEncodeOutput, TopEncode, TopEncodeOutput, +}; + +use crate::{api::ManagedTypeApi, types::ManagedType}; + +/// A very efficient mutable reference to a managed type. +/// +/// It can be dereferenced mutably (DerefMut). +pub struct ManagedRefMut<'a, M, T> +where + M: ManagedTypeApi, + T: ManagedType, +{ + pub(super) _phantom_m: PhantomData, + pub(super) _phantom_t: PhantomData<&'a mut T>, + pub(super) handle: T::OwnHandle, +} + +impl<'a, M, T> ManagedRefMut<'a, M, T> +where + M: ManagedTypeApi, + T: ManagedType, +{ + pub fn new(value: &'a mut T) -> Self { + Self { + _phantom_m: PhantomData, + _phantom_t: PhantomData, + handle: value.get_handle(), + } + } + + /// Will completely disregard lifetimes, use with care. + #[doc(hidden)] + pub(crate) unsafe fn wrap_handle(handle: T::OwnHandle) -> Self { + Self { + _phantom_m: PhantomData, + _phantom_t: PhantomData, + handle, + } + } +} + +impl<'a, M, T> ManagedRefMut<'a, M, T> +where + M: ManagedTypeApi, + T: ManagedType + Clone, +{ + /// Syntactic sugar for dereferencing and cloning the object. + pub fn clone_value(&self) -> T { + self.deref().clone() + } +} + +impl<'a, M, T> Clone for ManagedRefMut<'a, M, T> +where + M: ManagedTypeApi, + T: ManagedType, +{ + #[inline] + fn clone(&self) -> Self { + Self { + _phantom_m: PhantomData, + _phantom_t: PhantomData, + handle: self.handle.clone(), + } + } +} + +impl<'a, M, T> Deref for ManagedRefMut<'a, M, T> +where + M: ManagedTypeApi, + T: ManagedType, +{ + type Target = T; + + #[inline] + fn deref(&self) -> &Self::Target { + Self::Target::transmute_from_handle_ref(&self.handle) + } +} + +impl<'a, M, T> DerefMut for ManagedRefMut<'a, M, T> +where + M: ManagedTypeApi, + T: ManagedType, +{ + fn deref_mut(&mut self) -> &mut Self::Target { + Self::Target::transmute_from_handle_ref_mut(&mut self.handle) + } +} + +impl<'a, M, T> Borrow for ManagedRefMut<'a, M, T> +where + M: ManagedTypeApi, + T: ManagedType, +{ + #[inline] + fn borrow(&self) -> &T { + self.deref() + } +} + +impl<'a, M, T> From<&'a mut T> for ManagedRefMut<'a, M, T> +where + M: ManagedTypeApi, + T: ManagedType, +{ + #[inline] + fn from(value_ref: &'a mut T) -> Self { + Self::new(value_ref) + } +} + +impl<'a, M, T> PartialEq for ManagedRefMut<'a, M, T> +where + M: ManagedTypeApi, + T: ManagedType + PartialEq, +{ + #[inline] + fn eq(&self, other: &Self) -> bool { + self.deref() == other.deref() + } +} + +impl<'a, M, T> Eq for ManagedRefMut<'a, M, T> +where + M: ManagedTypeApi, + T: ManagedType + PartialEq, +{ +} + +impl<'a, M, T> TopEncode for ManagedRefMut<'a, M, T> +where + M: ManagedTypeApi, + T: ManagedType + TopEncode, +{ + #[inline] + fn top_encode_or_handle_err(&self, output: O, h: H) -> Result<(), H::HandledErr> + where + O: TopEncodeOutput, + H: EncodeErrorHandler, + { + self.deref().top_encode_or_handle_err(output, h) + } +} + +impl<'a, M, T> NestedEncode for ManagedRefMut<'a, M, T> +where + M: ManagedTypeApi, + T: ManagedType + NestedEncode, +{ + #[inline] + fn dep_encode_or_handle_err(&self, dest: &mut O, h: H) -> Result<(), H::HandledErr> + where + O: NestedEncodeOutput, + H: EncodeErrorHandler, + { + self.deref().dep_encode_or_handle_err(dest, h) + } +} + +impl<'a, M, T> core::fmt::Debug for ManagedRefMut<'a, M, T> +where + M: ManagedTypeApi, + T: ManagedType + core::fmt::Debug, +{ + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + f.debug_tuple("ManagedRefMut").field(self.deref()).finish() + } +} diff --git a/framework/base/src/types/managed/wrapped/managed_vec.rs b/framework/base/src/types/managed/wrapped/managed_vec.rs index 3e25360ea4..eb79ffebcd 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec.rs @@ -59,6 +59,10 @@ where fn transmute_from_handle_ref(handle_ref: &M::ManagedBufferHandle) -> &Self { unsafe { core::mem::transmute(handle_ref) } } + + fn transmute_from_handle_ref_mut(handle_ref: &mut M::ManagedBufferHandle) -> &mut Self { + unsafe { core::mem::transmute(handle_ref) } + } } impl ManagedVec @@ -233,7 +237,7 @@ where ManagedVec::new() }; - self.buffer = part_before.buffer; + *self = part_before; self.buffer.append(&part_after.buffer); } @@ -764,3 +768,4 @@ where } } } + diff --git a/framework/base/src/types/managed/wrapped/managed_vec_ref.rs b/framework/base/src/types/managed/wrapped/managed_vec_ref.rs index b69a124668..b339d1cdaf 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_ref.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_ref.rs @@ -1,6 +1,6 @@ use crate::{ api::ManagedTypeApi, - types::{ManagedType, ManagedVec, ManagedVecItem}, + types::{ManagedVec, ManagedVecItem}, }; use core::{ marker::PhantomData, @@ -8,6 +8,8 @@ use core::{ ops::{Deref, DerefMut}, }; +use super::{ManagedRef, ManagedRefMut}; + pub struct ManagedVecRef<'a, M, T> where M: ManagedTypeApi, @@ -26,8 +28,10 @@ where T: ManagedVecItem, { #[inline] - fn wrap_as_managed_vec(managed_vec_handle: M::ManagedBufferHandle) -> ManagedVec { - ManagedVec::from_handle(managed_vec_handle) + unsafe fn wrap_as_managed_vec( + managed_vec_handle: M::ManagedBufferHandle, + ) -> ManagedRef<'static, M, ManagedVec> { + ManagedRef::wrap_handle(managed_vec_handle) } pub(super) fn new(managed_vec_handle: M::ManagedBufferHandle, item_index: usize) -> Self { @@ -50,8 +54,12 @@ where { fn drop(&mut self) { let item = unsafe { ManuallyDrop::take(&mut self.item) }; - let _ = - Self::wrap_as_managed_vec(self.managed_vec_handle.clone()).set(self.item_index, item); + unsafe { + let _ = + ManagedRefMut::>::wrap_handle(self.managed_vec_handle.clone()) + .set(self.item_index, item); + } + // core::mem::forget(item); } } diff --git a/framework/base/src/types/managed/wrapped/mod.rs b/framework/base/src/types/managed/wrapped/mod.rs index 1874dc48b0..37849e0b6c 100644 --- a/framework/base/src/types/managed/wrapped/mod.rs +++ b/framework/base/src/types/managed/wrapped/mod.rs @@ -14,6 +14,7 @@ mod managed_byte_array; mod managed_decimal; mod managed_option; mod managed_ref; +mod managed_ref_mut; mod managed_vec; mod managed_vec_item; mod managed_vec_item_nested_tuple; @@ -43,6 +44,7 @@ pub use managed_decimal::{ }; pub use managed_option::ManagedOption; pub use managed_ref::ManagedRef; +pub use managed_ref_mut::ManagedRefMut; pub use managed_vec::ManagedVec; pub use managed_vec_item::ManagedVecItem; pub use managed_vec_item_nested_tuple::ManagedVecItemNestedTuple; diff --git a/framework/base/src/types/managed/wrapped/token_identifier.rs b/framework/base/src/types/managed/wrapped/token_identifier.rs index 1e66615199..69481772e0 100644 --- a/framework/base/src/types/managed/wrapped/token_identifier.rs +++ b/framework/base/src/types/managed/wrapped/token_identifier.rs @@ -39,6 +39,10 @@ impl ManagedType for TokenIdentifier { fn transmute_from_handle_ref(handle_ref: &M::ManagedBufferHandle) -> &Self { unsafe { core::mem::transmute(handle_ref) } } + + fn transmute_from_handle_ref_mut(handle_ref: &mut M::ManagedBufferHandle) -> &mut Self { + unsafe { core::mem::transmute(handle_ref) } + } } impl TokenIdentifier { From 59047ab10fc6be386f6e2b77cb451e4ddcad3ac6 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Sat, 9 Nov 2024 03:26:03 +0200 Subject: [PATCH 03/66] ManagedType from_handle marked unsafe, refactoring --- .../basic-features/src/storage_direct_load.rs | 23 ++- contracts/modules/src/esdt.rs | 4 +- .../wrappers/blockchain_wrapper.rs | 139 +++++++++------- .../contract_base/wrappers/crypto_wrapper.rs | 49 +++--- .../wrappers/send_raw_wrapper.rs | 29 ++-- .../contract_base/wrappers/send_wrapper.rs | 5 +- .../wrappers/storage_raw_wrapper.rs | 3 +- framework/base/src/io/arg_de_input.rs | 37 +++-- framework/base/src/io/arg_nested_tuple.rs | 20 +-- framework/base/src/storage/storage_get.rs | 13 +- .../src/storage/storage_get_from_address.rs | 20 +-- framework/base/src/storage/storage_key.rs | 2 +- .../async_call_promises.rs | 10 +- .../contract_call_exec.rs | 10 +- .../types/interaction/managed_arg_buffer.rs | 2 +- .../types/interaction/markers/to_caller.rs | 10 +- .../src/types/interaction/markers/to_self.rs | 11 +- .../tx_exec/tx_exec_async_promises.rs | 6 +- .../base/src/types/managed/basic/big_float.rs | 38 ++--- .../managed/basic/big_float_operators.rs | 6 +- .../base/src/types/managed/basic/big_int.rs | 44 +++-- .../types/managed/basic/big_int_operators.rs | 25 +-- .../src/types/managed/basic/elliptic_curve.rs | 152 ++++++++++-------- .../src/types/managed/basic/managed_buffer.rs | 81 +++++++--- .../src/types/managed/basic/managed_map.rs | 19 ++- .../src/types/managed/managed_type_trait.rs | 4 +- .../multi_value/multi_value_encoded.rs | 2 +- .../multi_value/multi_value_managed_vec.rs | 2 +- .../src/types/managed/wrapped/big_uint.rs | 53 +++--- .../managed/wrapped/big_uint_operators.rs | 16 +- .../wrapped/egld_or_esdt_token_identifier.rs | 20 ++- .../types/managed/wrapped/managed_address.rs | 28 +++- .../managed/wrapped/managed_byte_array.rs | 13 +- .../managed_decimal_logarithm.rs | 7 +- .../managed_decimal/managed_decimal_signed.rs | 18 +-- .../types/managed/wrapped/managed_option.rs | 11 +- .../src/types/managed/wrapped/managed_ref.rs | 2 +- .../src/types/managed/wrapped/managed_vec.rs | 3 +- .../types/managed/wrapped/managed_vec_item.rs | 6 +- .../types/managed/wrapped/token_identifier.rs | 16 +- 40 files changed, 572 insertions(+), 387 deletions(-) diff --git a/contracts/feature-tests/basic-features/src/storage_direct_load.rs b/contracts/feature-tests/basic-features/src/storage_direct_load.rs index efdd51a20f..7d19f83eb5 100644 --- a/contracts/feature-tests/basic-features/src/storage_direct_load.rs +++ b/contracts/feature-tests/basic-features/src/storage_direct_load.rs @@ -1,7 +1,5 @@ multiversx_sc::imports!(); -use multiversx_sc::api::{use_raw_handle, HandleTypeInfo}; - use crate::types::*; /// Storage tests: direct load. @@ -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: <::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 + } } } diff --git a/contracts/modules/src/esdt.rs b/contracts/modules/src/esdt.rs index ea977f53be..42314e9608 100644 --- a/contracts/modules/src/esdt.rs +++ b/contracts/modules/src/esdt.rs @@ -84,7 +84,9 @@ pub trait EsdtModule { fn nft_create(&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, diff --git a/framework/base/src/contract_base/wrappers/blockchain_wrapper.rs b/framework/base/src/contract_base/wrappers/blockchain_wrapper.rs index c499c9f663..e769bb69c7 100644 --- a/framework/base/src/contract_base/wrappers/blockchain_wrapper.rs +++ b/framework/base/src/contract_base/wrappers/blockchain_wrapper.rs @@ -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, }, }; @@ -49,9 +49,11 @@ where #[inline] pub fn get_caller(&self) -> ManagedAddress { - 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.")] @@ -63,16 +65,20 @@ where #[inline] pub fn get_sc_address(&self) -> ManagedAddress { - 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 { - 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) { @@ -123,16 +129,20 @@ where #[cfg(feature = "alloc")] #[inline] pub fn get_balance_legacy(&self, address: &crate::types::Address) -> BigUint { - 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) -> BigUint { - 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] @@ -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::::from_handle(mbuf_temp_1).load_to_byte_array(&mut buffer); + unsafe { + ManagedRefMut::<'static, A, ManagedBuffer>::wrap_handle(mbuf_temp_1) + .load_to_byte_array(&mut buffer); + } CodeMetadata::from(buffer) } @@ -173,9 +186,11 @@ where #[inline] pub fn get_state_root_hash(&self) -> ManagedByteArray { - 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.")] @@ -187,9 +202,11 @@ where #[inline] pub fn get_tx_hash(&self) -> ManagedByteArray { - 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] @@ -229,9 +246,11 @@ where #[inline] pub fn get_block_random_seed(&self) -> ManagedByteArray { - 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] @@ -266,9 +285,11 @@ where #[inline] pub fn get_prev_block_random_seed(&self) -> ManagedByteArray { - 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] @@ -288,14 +309,16 @@ where token_id: &TokenIdentifier, nonce: u64, ) -> BigUint { - 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( @@ -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()), + } } } @@ -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(), + ), + } } } @@ -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 } } diff --git a/framework/base/src/contract_base/wrappers/crypto_wrapper.rs b/framework/base/src/contract_base/wrappers/crypto_wrapper.rs index 369cca1a63..3731d4a964 100644 --- a/framework/base/src/contract_base/wrappers/crypto_wrapper.rs +++ b/framework/base/src/contract_base/wrappers/crypto_wrapper.rs @@ -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}, }; @@ -30,30 +27,33 @@ where &self, data: B, ) -> ManagedByteArray { - 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>>( &self, data: B, ) -> ManagedByteArray { - 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>>( &self, data: B, ) -> ManagedByteArray { - 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( @@ -122,14 +122,15 @@ where r: &ManagedBuffer, s: &ManagedBuffer, ) -> ManagedBuffer { - 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. diff --git a/framework/base/src/contract_base/wrappers/send_raw_wrapper.rs b/framework/base/src/contract_base/wrappers/send_raw_wrapper.rs index 5d290369df..07466ac234 100644 --- a/framework/base/src/contract_base/wrappers/send_raw_wrapper.rs +++ b/framework/base/src/contract_base/wrappers/send_raw_wrapper.rs @@ -192,10 +192,12 @@ where new_address_handle, result_handle, ); - ( - ManagedAddress::from_raw_handle(new_address_handle), - ManagedVec::from_raw_handle(result_handle), - ) + unsafe { + ( + ManagedAddress::from_raw_handle(new_address_handle), + ManagedVec::from_raw_handle(result_handle), + ) + } } /// Deploys a new contract in the same shard by re-using the code of an already deployed source contract. @@ -222,10 +224,12 @@ where new_address_handle, result_handle, ); - ( - ManagedAddress::from_raw_handle(new_address_handle), - ManagedVec::from_raw_handle(result_handle), - ) + unsafe { + ( + ManagedAddress::from_raw_handle(new_address_handle), + ManagedVec::from_raw_handle(result_handle), + ) + } } pub fn upgrade_from_source_contract( @@ -291,7 +295,7 @@ where arg_buffer.get_handle().get_raw_handle(), result_handle, ); - ManagedVec::from_raw_handle(result_handle) + unsafe { ManagedVec::from_raw_handle(result_handle) } } pub fn execute_on_same_context_raw( @@ -311,7 +315,7 @@ where arg_buffer.get_handle().get_raw_handle(), result_handle, ); - ManagedVec::from_raw_handle(result_handle) + unsafe { ManagedVec::from_raw_handle(result_handle) } } /// Same shard, in-line execution of another contract. @@ -330,7 +334,7 @@ where arg_buffer.get_handle().get_raw_handle(), result_handle, ); - ManagedVec::from_raw_handle(result_handle) + unsafe { ManagedVec::from_raw_handle(result_handle) } } /// Allows synchronously calling a local function by name. Execution is resumed afterwards. @@ -357,8 +361,7 @@ where ); self.clean_return_data(); - - ManagedVec::from_raw_handle(result_handle) + unsafe { ManagedVec::from_raw_handle(result_handle) } } pub fn clean_return_data(&self) { diff --git a/framework/base/src/contract_base/wrappers/send_wrapper.rs b/framework/base/src/contract_base/wrappers/send_wrapper.rs index 3a11bde247..0c23b67cd4 100644 --- a/framework/base/src/contract_base/wrappers/send_wrapper.rs +++ b/framework/base/src/contract_base/wrappers/send_wrapper.rs @@ -2,6 +2,7 @@ use core::marker::PhantomData; use crate::codec::Empty; +use crate::types::ManagedRef; use crate::{ api::{BlockchainApi, CallTypeApi, StorageReadApi}, codec, @@ -589,7 +590,9 @@ where ) -> u64 { let big_zero = BigUint::zero(); 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.esdt_nft_create( token, diff --git a/framework/base/src/contract_base/wrappers/storage_raw_wrapper.rs b/framework/base/src/contract_base/wrappers/storage_raw_wrapper.rs index 7417d6ca52..9fd49e115b 100644 --- a/framework/base/src/contract_base/wrappers/storage_raw_wrapper.rs +++ b/framework/base/src/contract_base/wrappers/storage_raw_wrapper.rs @@ -59,7 +59,8 @@ where V: TopDecode, { let key: StorageKey = storage_key.into(); - let result_buffer = ManagedBuffer::::from_handle(use_raw_handle(MBUF_TEMPORARY_1)); + let result_buffer = + unsafe { ManagedBuffer::::from_handle(use_raw_handle(MBUF_TEMPORARY_1)) }; A::storage_read_api_impl().storage_load_from_address( address.get_handle(), key.get_handle(), diff --git a/framework/base/src/io/arg_de_input.rs b/framework/base/src/io/arg_de_input.rs index 98faef2899..7c84d9cbb9 100644 --- a/framework/base/src/io/arg_de_input.rs +++ b/framework/base/src/io/arg_de_input.rs @@ -1,10 +1,7 @@ use core::marker::PhantomData; use crate::{ - api::{ - use_raw_handle, EndpointArgumentApi, EndpointArgumentApiImpl, ManagedTypeApi, - StaticVarApiImpl, - }, + api::{EndpointArgumentApi, EndpointArgumentApiImpl, ManagedTypeApi}, codec::{ try_execute_then_cast, DecodeError, DecodeErrorHandler, TopDecodeInput, TryStaticCast, }, @@ -43,22 +40,34 @@ where } fn to_managed_buffer(&self) -> ManagedBuffer { - let mbuf_handle: AA::ManagedBufferHandle = - use_raw_handle(AA::static_var_api_impl().next_handle()); - AA::argument_api_impl().load_argument_managed_buffer(self.arg_index, mbuf_handle.clone()); - ManagedBuffer::from_handle(mbuf_handle) + unsafe { + let result = ManagedBuffer::new_uninit(); + AA::argument_api_impl() + .load_argument_managed_buffer(self.arg_index, result.get_handle()); + result + } } fn to_big_int(&self) -> BigInt { - let bi_handle: AA::BigIntHandle = use_raw_handle(AA::static_var_api_impl().next_handle()); - AA::argument_api_impl().load_argument_big_int_signed(self.arg_index, bi_handle.clone()); - BigInt::from_handle(bi_handle) + unsafe { + let result = BigInt::new_uninit(); + AA::argument_api_impl() + .load_argument_big_int_signed(self.arg_index, result.get_handle()); + result + } } fn to_big_uint(&self) -> BigUint { - let bi_handle: AA::BigIntHandle = use_raw_handle(AA::static_var_api_impl().next_handle()); - AA::argument_api_impl().load_argument_big_int_unsigned(self.arg_index, bi_handle.clone()); - BigUint::from_handle(bi_handle) + unsafe { + let result = BigUint::new_uninit(); + AA::argument_api_impl() + .load_argument_big_int_unsigned(self.arg_index, result.get_handle()); + result + } + + // let bi_handle: AA::BigIntHandle = use_raw_handle(AA::static_var_api_impl().next_handle()); + // AA::argument_api_impl().load_argument_big_int_unsigned(self.arg_index, bi_handle.clone()); + // BigUint::from_handle(bi_handle) } } diff --git a/framework/base/src/io/arg_nested_tuple.rs b/framework/base/src/io/arg_nested_tuple.rs index fca34c9886..1a0eaace7a 100644 --- a/framework/base/src/io/arg_nested_tuple.rs +++ b/framework/base/src/io/arg_nested_tuple.rs @@ -198,13 +198,15 @@ fn callback_closure_args_loader() -> ManagedResultArgLoader where AA: VMApi, { - AA::argument_api_impl() - .load_callback_closure_buffer(use_raw_handle(const_handles::MBUF_TEMPORARY_1)); - let cb_closure_args_serialized = - ManagedBuffer::::from_raw_handle(const_handles::MBUF_TEMPORARY_1); - let mut cb_closure_args_buffer = - ManagedArgBuffer::::from_raw_handle(const_handles::CALLBACK_CLOSURE_ARGS_BUFFER); - cb_closure_args_buffer.deserialize_overwrite(cb_closure_args_serialized); - - ManagedResultArgLoader::new(cb_closure_args_buffer.into_vec_of_buffers()) + unsafe { + AA::argument_api_impl() + .load_callback_closure_buffer(use_raw_handle(const_handles::MBUF_TEMPORARY_1)); + let cb_closure_args_serialized = + ManagedBuffer::::from_raw_handle(const_handles::MBUF_TEMPORARY_1); + let mut cb_closure_args_buffer = + ManagedArgBuffer::::from_raw_handle(const_handles::CALLBACK_CLOSURE_ARGS_BUFFER); + cb_closure_args_buffer.deserialize_overwrite(cb_closure_args_serialized); + + ManagedResultArgLoader::new(cb_closure_args_buffer.into_vec_of_buffers()) + } } diff --git a/framework/base/src/storage/storage_get.rs b/framework/base/src/storage/storage_get.rs index 85107896dd..b33b5e1dcb 100644 --- a/framework/base/src/storage/storage_get.rs +++ b/framework/base/src/storage/storage_get.rs @@ -3,7 +3,7 @@ use core::{convert::Infallible, marker::PhantomData}; use crate::{ api::{ const_handles, use_raw_handle, ErrorApi, ErrorApiImpl, HandleConstraints, - ManagedBufferApiImpl, ManagedTypeApi, StaticVarApiImpl, StorageReadApi, StorageReadApiImpl, + ManagedBufferApiImpl, ManagedTypeApi, StorageReadApi, StorageReadApiImpl, }, codec::*, err_msg, @@ -33,11 +33,12 @@ where } fn to_managed_buffer(&self) -> ManagedBuffer { - let mbuf_handle: A::ManagedBufferHandle = - use_raw_handle(A::static_var_api_impl().next_handle()); - A::storage_read_api_impl() - .storage_load_managed_buffer_raw(self.key.buffer.get_handle(), mbuf_handle.clone()); - ManagedBuffer::from_handle(mbuf_handle) + unsafe { + let result = ManagedBuffer::new_uninit(); + A::storage_read_api_impl() + .storage_load_managed_buffer_raw(self.key.buffer.get_handle(), result.get_handle()); + result + } } fn to_big_uint(&self) -> BigUint { diff --git a/framework/base/src/storage/storage_get_from_address.rs b/framework/base/src/storage/storage_get_from_address.rs index 2b40987bd3..13a467d2f0 100644 --- a/framework/base/src/storage/storage_get_from_address.rs +++ b/framework/base/src/storage/storage_get_from_address.rs @@ -1,7 +1,7 @@ use crate::{ api::{ const_handles, use_raw_handle, ErrorApi, HandleConstraints, ManagedBufferApiImpl, - ManagedTypeApi, StaticVarApiImpl, StorageReadApi, StorageReadApiImpl, + ManagedTypeApi, StorageReadApi, StorageReadApiImpl, }, codec::*, types::{ @@ -35,15 +35,15 @@ where } fn to_managed_buffer(&self) -> ManagedBuffer { - let mbuf_handle: A::ManagedBufferHandle = - use_raw_handle(A::static_var_api_impl().next_handle()); - A::storage_read_api_impl().storage_load_from_address( - self.addr.get_handle(), - self.key.buffer.get_handle(), - mbuf_handle.clone(), - ); - - ManagedBuffer::from_handle(mbuf_handle) + unsafe { + let result = ManagedBuffer::new_uninit(); + A::storage_read_api_impl().storage_load_from_address( + self.addr.get_handle(), + self.key.buffer.get_handle(), + result.get_handle(), + ); + result + } } fn to_big_uint(&self) -> BigUint { diff --git a/framework/base/src/storage/storage_key.rs b/framework/base/src/storage/storage_key.rs index f692dd5266..b3a35643e1 100644 --- a/framework/base/src/storage/storage_key.rs +++ b/framework/base/src/storage/storage_key.rs @@ -22,7 +22,7 @@ where type OwnHandle = A::ManagedBufferHandle; #[inline] - fn from_handle(handle: A::ManagedBufferHandle) -> Self { + unsafe fn from_handle(handle: A::ManagedBufferHandle) -> Self { StorageKey { buffer: ManagedBuffer::from_handle(handle), } diff --git a/framework/base/src/types/interaction/contract_call_legacy/async_call_promises.rs b/framework/base/src/types/interaction/contract_call_legacy/async_call_promises.rs index ee3726c0eb..f2d091e2b3 100644 --- a/framework/base/src/types/interaction/contract_call_legacy/async_call_promises.rs +++ b/framework/base/src/types/interaction/contract_call_legacy/async_call_promises.rs @@ -1,5 +1,7 @@ +use core::ops::DerefMut; + use crate::{ - api::CallTypeApi, + api::{const_handles, CallTypeApi}, contract_base::SendRawWrapper, types::{BigUint, CallbackClosure, FunctionCall, ManagedAddress, ManagedBuffer}, }; @@ -41,16 +43,14 @@ where } pub fn register_promise(self) { - use crate::{api::const_handles, types::ManagedType}; - let mut cb_closure_args_serialized = - ManagedBuffer::::from_raw_handle(const_handles::MBUF_TEMPORARY_1); + unsafe { ManagedBuffer::temp_const_ref_mut(const_handles::MBUF_TEMPORARY_1) }; let callback_name; if let Some(callback_call) = self.callback_call { callback_name = callback_call.callback_name; callback_call .closure_args - .serialize_overwrite(&mut cb_closure_args_serialized); + .serialize_overwrite(cb_closure_args_serialized.deref_mut()); } else { callback_name = ""; cb_closure_args_serialized.overwrite(&[]); diff --git a/framework/base/src/types/interaction/contract_call_legacy/contract_call_exec.rs b/framework/base/src/types/interaction/contract_call_legacy/contract_call_exec.rs index 427218f1c8..f0f1722f24 100644 --- a/framework/base/src/types/interaction/contract_call_legacy/contract_call_exec.rs +++ b/framework/base/src/types/interaction/contract_call_legacy/contract_call_exec.rs @@ -36,10 +36,12 @@ where call_value_handle.get_raw_handle(), ); - ( - BigUint::from_raw_handle(call_value_handle.get_raw_handle()), - ManagedVec::from_raw_handle(esdt_transfer_value_handle.get_raw_handle()), - ) + unsafe { + ( + BigUint::from_raw_handle(call_value_handle.get_raw_handle()), + ManagedVec::from_raw_handle(esdt_transfer_value_handle.get_raw_handle()), + ) + } } pub fn to_call_data_string(&self) -> ManagedBuffer { diff --git a/framework/base/src/types/interaction/managed_arg_buffer.rs b/framework/base/src/types/interaction/managed_arg_buffer.rs index bd98c40931..009ad74e3a 100644 --- a/framework/base/src/types/interaction/managed_arg_buffer.rs +++ b/framework/base/src/types/interaction/managed_arg_buffer.rs @@ -33,7 +33,7 @@ where type OwnHandle = M::ManagedBufferHandle; #[inline] - fn from_handle(handle: M::ManagedBufferHandle) -> Self { + unsafe fn from_handle(handle: M::ManagedBufferHandle) -> Self { ManagedArgBuffer { data: ManagedVec::from_handle(handle), } diff --git a/framework/base/src/types/interaction/markers/to_caller.rs b/framework/base/src/types/interaction/markers/to_caller.rs index f24191ce9f..9066a16239 100644 --- a/framework/base/src/types/interaction/markers/to_caller.rs +++ b/framework/base/src/types/interaction/markers/to_caller.rs @@ -1,5 +1,5 @@ use crate::{ - api::{const_handles, use_raw_handle, BlockchainApi, BlockchainApiImpl, CallTypeApi}, + api::{const_handles, BlockchainApi, BlockchainApiImpl, CallTypeApi}, contract_base::BlockchainWrapper, types::{ AnnotatedValue, ManagedAddress, ManagedBuffer, ManagedType, TxScEnv, TxTo, TxToSpecified, @@ -25,9 +25,11 @@ where where F: FnOnce(&ManagedAddress) -> R, { - let caller_handle: Api::ManagedBufferHandle = use_raw_handle(const_handles::ADDRESS_CALLER); - Api::blockchain_api_impl().load_caller_managed(caller_handle.clone()); - f(&ManagedAddress::from_handle(caller_handle)) + unsafe { + let temp = ManagedAddress::temp_const_ref(const_handles::ADDRESS_CALLER); + Api::blockchain_api_impl().load_caller_managed(temp.get_handle()); + f(&temp) + } } } diff --git a/framework/base/src/types/interaction/markers/to_self.rs b/framework/base/src/types/interaction/markers/to_self.rs index f339a448e2..8a78e13d6d 100644 --- a/framework/base/src/types/interaction/markers/to_self.rs +++ b/framework/base/src/types/interaction/markers/to_self.rs @@ -1,5 +1,5 @@ use crate::{ - api::{const_handles, use_raw_handle, BlockchainApi, BlockchainApiImpl, CallTypeApi}, + api::{const_handles, BlockchainApi, BlockchainApiImpl, CallTypeApi}, contract_base::BlockchainWrapper, types::{ AnnotatedValue, ManagedAddress, ManagedBuffer, ManagedType, TxScEnv, TxTo, TxToSpecified, @@ -26,10 +26,11 @@ where where F: FnOnce(&ManagedAddress) -> R, { - let sc_address_handle: Api::ManagedBufferHandle = - use_raw_handle(const_handles::ADDRESS_CALLER); - Api::blockchain_api_impl().load_sc_address_managed(sc_address_handle.clone()); - f(&ManagedAddress::from_handle(sc_address_handle)) + unsafe { + let temp = ManagedAddress::temp_const_ref(const_handles::ADDRESS_CALLER); + Api::blockchain_api_impl().load_sc_address_managed(temp.get_handle()); + f(&temp) + } } } diff --git a/framework/base/src/types/interaction/tx_exec/tx_exec_async_promises.rs b/framework/base/src/types/interaction/tx_exec/tx_exec_async_promises.rs index 38e38fcbcd..333bbf9379 100644 --- a/framework/base/src/types/interaction/tx_exec/tx_exec_async_promises.rs +++ b/framework/base/src/types/interaction/tx_exec/tx_exec_async_promises.rs @@ -3,8 +3,8 @@ use crate::{ contract_base::{ErrorHelper, SendRawWrapper}, types::{ interaction::callback_closure::CallbackClosureWithGas, CallbackClosure, ExplicitGas, - FunctionCall, ManagedBuffer, ManagedType, OriginalResultMarker, Tx, TxGas, TxGasValue, - TxPayment, TxResultHandler, TxScEnv, TxToSpecified, + FunctionCall, ManagedBuffer, OriginalResultMarker, Tx, TxGas, TxGasValue, TxPayment, + TxResultHandler, TxScEnv, TxToSpecified, }, }; @@ -160,7 +160,7 @@ where pub fn register_promise(self) { let callback_name = self.result_handler.callback_name(); let mut cb_closure_args_serialized = - ManagedBuffer::::from_raw_handle(const_handles::MBUF_TEMPORARY_1); + unsafe { ManagedBuffer::temp_const_ref_mut(const_handles::MBUF_TEMPORARY_1) }; self.result_handler .overwrite_with_serialized_args(&mut cb_closure_args_serialized); let extra_gas_for_callback = self.result_handler.gas_for_callback(); diff --git a/framework/base/src/types/managed/basic/big_float.rs b/framework/base/src/types/managed/basic/big_float.rs index 802d582e94..8361cb4b79 100644 --- a/framework/base/src/types/managed/basic/big_float.rs +++ b/framework/base/src/types/managed/basic/big_float.rs @@ -27,7 +27,7 @@ pub struct BigFloat { impl ManagedType for BigFloat { type OwnHandle = M::BigFloatHandle; - fn from_handle(handle: M::BigFloatHandle) -> Self { + unsafe fn from_handle(handle: M::BigFloatHandle) -> Self { BigFloat { handle } } @@ -88,7 +88,7 @@ macro_rules! big_float_conv_num { let new_bf_handle: M::BigFloatHandle = use_raw_handle(M::static_var_api_impl().next_handle()); M::managed_type_impl().bf_set_i64(new_bf_handle.clone(), value as i64); - BigFloat::from_handle(new_bf_handle) + unsafe { BigFloat::from_handle(new_bf_handle) } } } }; @@ -105,28 +105,28 @@ impl BigFloat { let new_bf_handle: M::BigFloatHandle = use_raw_handle(M::static_var_api_impl().next_handle()); M::managed_type_impl().bf_neg(new_bf_handle.clone(), self.handle.clone()); - BigFloat::from_handle(new_bf_handle) + unsafe { BigFloat::from_handle(new_bf_handle) } } pub fn abs(&self) -> Self { let new_bf_handle: M::BigFloatHandle = use_raw_handle(M::static_var_api_impl().next_handle()); M::managed_type_impl().bf_abs(new_bf_handle.clone(), self.handle.clone()); - BigFloat::from_handle(new_bf_handle) + unsafe { BigFloat::from_handle(new_bf_handle) } } pub fn from_big_uint(big_uint: &BigUint) -> Self { let new_bf_handle: M::BigFloatHandle = use_raw_handle(M::static_var_api_impl().next_handle()); M::managed_type_impl().bf_set_bi(new_bf_handle.clone(), big_uint.value.handle.clone()); - BigFloat::from_handle(new_bf_handle) + unsafe { BigFloat::from_handle(new_bf_handle) } } pub fn from_big_int(big_int: &BigInt) -> Self { let new_bf_handle: M::BigFloatHandle = use_raw_handle(M::static_var_api_impl().next_handle()); M::managed_type_impl().bf_set_bi(new_bf_handle.clone(), big_int.handle.clone()); - BigFloat::from_handle(new_bf_handle) + unsafe { BigFloat::from_handle(new_bf_handle) } } #[inline] @@ -138,42 +138,42 @@ impl BigFloat { let api = M::managed_type_impl(); let new_bf_handle = api.bf_from_parts(integral_part_value, fractional_part_value, exponent_value); - BigFloat::from_handle(new_bf_handle) + unsafe { BigFloat::from_handle(new_bf_handle) } } #[inline] pub fn from_frac(numerator_value: i64, denominator_value: i64) -> Self { let api = M::managed_type_impl(); let new_bf_handle = api.bf_from_frac(numerator_value, denominator_value); - BigFloat::from_handle(new_bf_handle) + unsafe { BigFloat::from_handle(new_bf_handle) } } #[inline] pub fn from_sci(significand_value: i64, exponent_value: i32) -> Self { let api = M::managed_type_impl(); let new_bf_handle = api.bf_from_sci(significand_value, exponent_value as i64); - BigFloat::from_handle(new_bf_handle) + unsafe { BigFloat::from_handle(new_bf_handle) } } pub fn trunc(&self) -> BigInt { let result: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); let api = M::managed_type_impl(); api.bf_trunc(result.clone(), self.handle.clone()); - BigInt::from_handle(result) + unsafe { BigInt::from_handle(result) } } pub fn floor(&self) -> BigInt { let result: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); let api = M::managed_type_impl(); api.bf_floor(result.clone(), self.handle.clone()); - BigInt::from_handle(result) + unsafe { BigInt::from_handle(result) } } pub fn ceil(&self) -> BigInt { let result: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); let api = M::managed_type_impl(); api.bf_ceil(result.clone(), self.handle.clone()); - BigInt::from_handle(result) + unsafe { BigInt::from_handle(result) } } pub fn to_fixed_point(&self, denominator: &BigFloat) -> BigInt { @@ -254,7 +254,7 @@ impl BigFloat { #[inline] pub fn zero() -> Self { - BigFloat::from_handle(M::managed_type_impl().bf_new_zero()) + unsafe { BigFloat::from_handle(M::managed_type_impl().bf_new_zero()) } } pub fn from_buffer(managed_buffer: &ManagedBuffer) -> Self { @@ -262,14 +262,14 @@ impl BigFloat { use_raw_handle(M::static_var_api_impl().next_handle()); M::managed_type_impl() .mb_to_big_float(managed_buffer.handle.clone(), new_bf_handle.clone()); - BigFloat::from_handle(new_bf_handle) + unsafe { BigFloat::from_handle(new_bf_handle) } } pub fn to_buffer(&self) -> ManagedBuffer { let new_man_buf_handle: M::ManagedBufferHandle = use_raw_handle(M::static_var_api_impl().next_handle()); M::managed_type_impl().mb_from_big_float(self.handle.clone(), new_man_buf_handle.clone()); - ManagedBuffer::from_handle(new_man_buf_handle) + unsafe { ManagedBuffer::from_handle(new_man_buf_handle) } } } @@ -278,14 +278,14 @@ impl BigFloat { let api = M::managed_type_impl(); let new_handle: M::BigFloatHandle = use_raw_handle(M::static_var_api_impl().next_handle()); api.bf_sqrt(new_handle.clone(), self.handle.clone()); - BigFloat::from_handle(new_handle) + unsafe { BigFloat::from_handle(new_handle) } } pub fn pow(&self, exp: i32) -> Self { let api = M::managed_type_impl(); let new_handle: M::BigFloatHandle = use_raw_handle(M::static_var_api_impl().next_handle()); api.bf_pow(new_handle.clone(), self.handle.clone(), exp); - BigFloat::from_handle(new_handle) + unsafe { BigFloat::from_handle(new_handle) } } /// Returns the sign of the `BigFloat` as a `Sign`. @@ -301,7 +301,7 @@ impl BigFloat { pub fn magnitude(&self) -> BigFloat { let result: M::BigFloatHandle = use_raw_handle(M::static_var_api_impl().next_handle()); M::managed_type_impl().bf_abs(result.clone(), self.handle.clone()); - BigFloat::from_handle(result) + unsafe { BigFloat::from_handle(result) } } /// Convert this `BigFloat` into its `Sign` and its magnitude, @@ -343,7 +343,7 @@ impl Clone for BigFloat { fn clone(&self) -> Self { let new_handle: M::BigFloatHandle = use_raw_handle(M::static_var_api_impl().next_handle()); M::managed_type_impl().bf_clone(new_handle.clone(), self.handle.clone()); - BigFloat::from_handle(new_handle) + unsafe { BigFloat::from_handle(new_handle) } } } diff --git a/framework/base/src/types/managed/basic/big_float_operators.rs b/framework/base/src/types/managed/basic/big_float_operators.rs index 7874c1ab86..7333a69ebe 100644 --- a/framework/base/src/types/managed/basic/big_float_operators.rs +++ b/framework/base/src/types/managed/basic/big_float_operators.rs @@ -16,7 +16,7 @@ macro_rules! binary_operator { self.handle.clone(), other.handle.clone(), ); - BigFloat::from_handle(self.handle.clone()) + unsafe { BigFloat::from_handle(self.handle.clone()) } } } @@ -31,7 +31,7 @@ macro_rules! binary_operator { self.handle.clone(), other.handle.clone(), ); - BigFloat::from_handle(result_handle) + unsafe { BigFloat::from_handle(result_handle) } } } }; @@ -82,6 +82,6 @@ impl Neg for BigFloat { let result_handle: M::BigFloatHandle = use_raw_handle(M::static_var_api_impl().next_handle()); M::managed_type_impl().bf_neg(result_handle.clone(), self.handle); - BigFloat::from_handle(result_handle) + unsafe { BigFloat::from_handle(result_handle) } } } diff --git a/framework/base/src/types/managed/basic/big_int.rs b/framework/base/src/types/managed/basic/big_int.rs index cb52117c32..9727d81dd5 100644 --- a/framework/base/src/types/managed/basic/big_int.rs +++ b/framework/base/src/types/managed/basic/big_int.rs @@ -11,7 +11,9 @@ use crate::{ NestedEncodeOutput, TopDecode, TopDecodeInput, TopEncode, TopEncodeOutput, TryStaticCast, }, formatter::{hex_util::encode_bytes_as_hex, FormatByteReceiver, SCDisplay}, - types::{heap::BoxedBytes, BigUint, ManagedBuffer, ManagedOption, ManagedType, Sign}, + types::{ + heap::BoxedBytes, BigUint, ManagedBuffer, ManagedOption, ManagedRef, ManagedType, Sign, + }, }; use super::cast_to_i64::cast_to_i64; @@ -25,7 +27,7 @@ pub struct BigInt { impl ManagedType for BigInt { type OwnHandle = M::BigIntHandle; - fn from_handle(handle: M::BigIntHandle) -> Self { + unsafe fn from_handle(handle: M::BigIntHandle) -> Self { BigInt { handle, _phantom: PhantomData, @@ -43,7 +45,6 @@ impl ManagedType for BigInt { fn transmute_from_handle_ref_mut(handle_ref: &mut M::BigIntHandle) -> &mut Self { unsafe { core::mem::transmute(handle_ref) } } - } impl Default for BigInt { @@ -68,6 +69,16 @@ impl From> for BigInt { } impl BigInt { + /// Creates a new object, without initializing it. + /// + /// ## Safety + /// + /// The value needs to be initialized after creation, otherwise the VM will halt the first time the value is attempted to be read. + pub unsafe fn new_uninit() -> Self { + let new_handle: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); + BigInt::from_handle(new_handle) + } + pub(crate) fn set_value(handle: M::BigIntHandle, value: T) where T: TryInto, @@ -88,7 +99,7 @@ impl BigInt { impl From> for BigInt { #[inline] fn from(item: BigUint) -> Self { - BigInt::from_handle(item.get_handle()) + item.into_big_int() } } @@ -100,7 +111,7 @@ macro_rules! big_int_conv_num { let handle: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); Self::set_value(handle.clone(), value); - BigInt::from_handle(handle) + unsafe { BigInt::from_handle(handle) } } } @@ -157,9 +168,8 @@ impl BigInt { #[inline] pub fn zero() -> Self { let handle: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); - // TODO: seting 0 will no longer be needed once we fix VM handle error M::managed_type_impl().bi_set_int64(handle.clone(), 0); - BigInt::from_handle(handle) + unsafe { BigInt::from_handle(handle) } } #[inline] @@ -178,7 +188,7 @@ impl BigInt { M::managed_type_impl().mb_overwrite(mb_handle.clone(), bytes); let handle: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); M::managed_type_impl().mb_to_big_int_signed(mb_handle, handle.clone()); - BigInt::from_handle(handle) + unsafe { BigInt::from_handle(handle) } } #[inline] @@ -192,7 +202,7 @@ impl BigInt { pub fn from_signed_bytes_be_buffer(managed_buffer: &ManagedBuffer) -> Self { let handle: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); M::managed_type_impl().mb_to_big_int_signed(managed_buffer.handle.clone(), handle.clone()); - BigInt::from_handle(handle) + unsafe { BigInt::from_handle(handle) } } #[inline] @@ -200,7 +210,7 @@ impl BigInt { let mb_handle: M::ManagedBufferHandle = use_raw_handle(M::static_var_api_impl().next_handle()); M::managed_type_impl().mb_from_big_int_signed(self.handle.clone(), mb_handle.clone()); - ManagedBuffer::from_handle(mb_handle) + unsafe { ManagedBuffer::from_handle(mb_handle) } } } @@ -214,7 +224,7 @@ impl Clone for BigInt { clone_handle.clone(), self.handle.clone(), ); - BigInt::from_handle(clone_handle) + unsafe { BigInt::from_handle(clone_handle) } } } @@ -224,7 +234,7 @@ impl BigInt { if sign.is_minus() { api.bi_neg(unsigned.value.handle.clone(), unsigned.value.handle.clone()); } - BigInt::from_handle(unsigned.value.handle) + unsafe { BigInt::from_handle(unsigned.value.handle) } } /// Returns the sign of the `BigInt` as a `Sign`. @@ -242,7 +252,7 @@ impl BigInt { let api = M::managed_type_impl(); let result_handle: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); api.bi_abs(result_handle.clone(), self.handle.clone()); - BigUint::from_handle(result_handle) + unsafe { BigUint::from_handle(result_handle) } } /// Convert this `BigInt` into its `Sign` and `BigUint` magnitude, @@ -335,7 +345,7 @@ impl BigInt { let result_handle: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); let exp_handle = BigUint::::make_temp(const_handles::BIG_INT_TEMPORARY_1, exp); M::managed_type_impl().bi_pow(result_handle.clone(), self.handle.clone(), exp_handle); - BigInt::from_handle(result_handle) + unsafe { BigInt::from_handle(result_handle) } } } @@ -343,9 +353,9 @@ impl SCDisplay for BigInt { fn fmt(&self, f: &mut F) { let str_handle: M::ManagedBufferHandle = use_raw_handle(const_handles::MBUF_TEMPORARY_1); M::managed_type_impl().bi_to_string(self.handle.clone(), str_handle.clone()); - f.append_managed_buffer(&ManagedBuffer::from_handle( - str_handle.cast_or_signal_error::(), - )); + let cast_handle = str_handle.cast_or_signal_error::(); + let wrap_cast = unsafe { ManagedRef::wrap_handle(cast_handle) }; + f.append_managed_buffer(&wrap_cast); } } diff --git a/framework/base/src/types/managed/basic/big_int_operators.rs b/framework/base/src/types/managed/basic/big_int_operators.rs index fed47d5a26..0cf00c66e6 100644 --- a/framework/base/src/types/managed/basic/big_int_operators.rs +++ b/framework/base/src/types/managed/basic/big_int_operators.rs @@ -19,7 +19,7 @@ macro_rules! binary_operator { self.handle.clone(), other.handle.clone(), ); - BigInt::from_handle(self.handle.clone()) + self } } @@ -44,14 +44,15 @@ macro_rules! binary_operator { fn $method(self, other: &BigInt) -> BigInt { let api = M::managed_type_impl(); - let result_handle: M::BigIntHandle = - use_raw_handle(M::static_var_api_impl().next_handle()); - api.$api_func( - result_handle.clone(), - self.handle.clone(), - other.handle.clone(), - ); - BigInt::from_handle(result_handle) + unsafe { + let result = BigInt::new_uninit(); + api.$api_func( + result.get_handle(), + self.handle.clone(), + other.handle.clone(), + ); + result + } } } @@ -59,7 +60,7 @@ macro_rules! binary_operator { type Output = BigInt; fn $method(self, other: &BigUint) -> BigInt { - self.$method(&BigInt::from_handle(other.get_handle())) + self.$method(other.as_big_int()) } } @@ -67,7 +68,7 @@ macro_rules! binary_operator { type Output = BigInt; fn $method(self, other: &BigInt) -> BigInt { - (&BigInt::from_handle(self.get_handle())).$method(other) + self.as_big_int().$method(other) } } }; @@ -120,6 +121,6 @@ impl Neg for BigInt { let api = M::managed_type_impl(); let result_handle: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); api.bi_neg(result_handle.clone(), self.handle); - BigInt::from_handle(result_handle) + unsafe { BigInt::from_handle(result_handle) } } } diff --git a/framework/base/src/types/managed/basic/elliptic_curve.rs b/framework/base/src/types/managed/basic/elliptic_curve.rs index 8c110f6459..2cc7097a18 100644 --- a/framework/base/src/types/managed/basic/elliptic_curve.rs +++ b/framework/base/src/types/managed/basic/elliptic_curve.rs @@ -35,7 +35,7 @@ pub struct EllipticCurve { impl ManagedType for EllipticCurve { type OwnHandle = M::EllipticCurveHandle; - fn from_handle(handle: M::EllipticCurveHandle) -> Self { + unsafe fn from_handle(handle: M::EllipticCurveHandle) -> Self { EllipticCurve { handle } } @@ -55,12 +55,12 @@ impl ManagedType for EllipticCurve { impl EllipticCurve { pub fn from_name(name: &ManagedBuffer) -> Self { let handle = M::managed_type_impl().ec_create_from_name_mb(name.get_handle()); - EllipticCurve::from_handle(handle) + unsafe { EllipticCurve::from_handle(handle) } } pub fn from_name_str(name: &str) -> Self { let handle = M::managed_type_impl().ec_create_from_name_bytes(name.as_bytes()); - EllipticCurve::from_handle(handle) + unsafe { EllipticCurve::from_handle(handle) } } pub fn from_bitsize(bitsize: u32) -> Option { @@ -88,14 +88,16 @@ impl EllipticCurve { x_base_point_handle.clone(), y_base_point_handle.clone(), ); - ( - BigUint::from_handle(field_order_handle), - BigUint::from_handle(base_point_order_handle), - BigUint::from_handle(eq_constant_handle), - BigUint::from_handle(x_base_point_handle), - BigUint::from_handle(y_base_point_handle), - api.ec_curve_length(self.handle.clone()), - ) + unsafe { + ( + BigUint::from_handle(field_order_handle), + BigUint::from_handle(base_point_order_handle), + BigUint::from_handle(eq_constant_handle), + BigUint::from_handle(x_base_point_handle), + BigUint::from_handle(y_base_point_handle), + api.ec_curve_length(self.handle.clone()), + ) + } } pub fn get_curve_length(&self) -> u32 { @@ -127,10 +129,12 @@ impl EllipticCurve { x_second_point.value.handle, y_second_point.value.handle, ); - ( - BigUint::from_handle(x_result_handle), - BigUint::from_handle(y_result_handle), - ) + unsafe { + ( + BigUint::from_handle(x_result_handle), + BigUint::from_handle(y_result_handle), + ) + } } pub fn double(&self, x_point: BigUint, y_point: BigUint) -> (BigUint, BigUint) { @@ -144,10 +148,12 @@ impl EllipticCurve { x_point.value.handle, y_point.value.handle, ); - ( - BigUint::from_handle(x_result_handle), - BigUint::from_handle(y_result_handle), - ) + unsafe { + ( + BigUint::from_handle(x_result_handle), + BigUint::from_handle(y_result_handle), + ) + } } pub fn is_on_curve(&self, x_point: BigUint, y_point: BigUint) -> bool { @@ -177,10 +183,12 @@ impl EllipticCurve { y_point.value.handle, data, ); - ( - BigUint::from_handle(x_result_handle), - BigUint::from_handle(y_result_handle), - ) + unsafe { + ( + BigUint::from_handle(x_result_handle), + BigUint::from_handle(y_result_handle), + ) + } } pub fn scalar_mult( @@ -200,10 +208,12 @@ impl EllipticCurve { y_point.value.handle, data.get_handle(), ); - ( - BigUint::from_handle(x_result_handle), - BigUint::from_handle(y_result_handle), - ) + unsafe { + ( + BigUint::from_handle(x_result_handle), + BigUint::from_handle(y_result_handle), + ) + } } #[deprecated( @@ -220,10 +230,12 @@ impl EllipticCurve { self.handle.clone(), data, ); - ( - BigUint::from_handle(x_result_handle), - BigUint::from_handle(y_result_handle), - ) + unsafe { + ( + BigUint::from_handle(x_result_handle), + BigUint::from_handle(y_result_handle), + ) + } } pub fn scalar_base_mult(&self, data: &ManagedBuffer) -> (BigUint, BigUint) { @@ -236,10 +248,12 @@ impl EllipticCurve { self.handle.clone(), data.get_handle(), ); - ( - BigUint::from_handle(x_result_handle), - BigUint::from_handle(y_result_handle), - ) + unsafe { + ( + BigUint::from_handle(x_result_handle), + BigUint::from_handle(y_result_handle), + ) + } } #[deprecated(since = "0.41.0", note = "Please use method `marshal` instead.")] @@ -266,7 +280,7 @@ impl EllipticCurve { y_pair.value.handle, result_handle.clone(), ); - ManagedBuffer::from_handle(result_handle) + unsafe { ManagedBuffer::from_handle(result_handle) } } #[deprecated( @@ -296,7 +310,7 @@ impl EllipticCurve { y_pair.value.handle, result_handle.clone(), ); - ManagedBuffer::from_handle(result_handle) + unsafe { ManagedBuffer::from_handle(result_handle) } } #[deprecated(since = "0.41.0", note = "Please use method `unmarshal` instead.")] @@ -310,10 +324,12 @@ impl EllipticCurve { self.handle.clone(), data, ); - ( - BigUint::from_handle(x_pair_handle), - BigUint::from_handle(y_pair_handle), - ) + unsafe { + ( + BigUint::from_handle(x_pair_handle), + BigUint::from_handle(y_pair_handle), + ) + } } pub fn unmarshal(&self, data: &ManagedBuffer) -> (BigUint, BigUint) { @@ -326,10 +342,12 @@ impl EllipticCurve { self.handle.clone(), data.get_handle(), ); - ( - BigUint::from_handle(x_pair_handle), - BigUint::from_handle(y_pair_handle), - ) + unsafe { + ( + BigUint::from_handle(x_pair_handle), + BigUint::from_handle(y_pair_handle), + ) + } } #[deprecated( @@ -346,10 +364,12 @@ impl EllipticCurve { self.handle.clone(), data, ); - ( - BigUint::from_handle(x_pair_handle), - BigUint::from_handle(y_pair_handle), - ) + unsafe { + ( + BigUint::from_handle(x_pair_handle), + BigUint::from_handle(y_pair_handle), + ) + } } pub fn unmarshal_compressed(&self, data: &ManagedBuffer) -> (BigUint, BigUint) { @@ -362,10 +382,12 @@ impl EllipticCurve { self.handle.clone(), data.get_handle(), ); - ( - BigUint::from_handle(x_pair_handle), - BigUint::from_handle(y_pair_handle), - ) + unsafe { + ( + BigUint::from_handle(x_pair_handle), + BigUint::from_handle(y_pair_handle), + ) + } } #[deprecated(since = "0.41.0", note = "Please use method `generate_key` instead.")] @@ -379,11 +401,13 @@ impl EllipticCurve { y_pub_key_handle.clone(), self.handle.clone(), ); - ( - BigUint::from_handle(x_pub_key_handle), - BigUint::from_handle(y_pub_key_handle), - private_key, - ) + unsafe { + ( + BigUint::from_handle(x_pub_key_handle), + BigUint::from_handle(y_pub_key_handle), + private_key, + ) + } } pub fn generate_key(&self) -> (BigUint, BigUint, ManagedBuffer) { @@ -398,11 +422,13 @@ impl EllipticCurve { self.handle.clone(), private_key_handle.clone(), ); - ( - BigUint::from_handle(x_pub_key_handle), - BigUint::from_handle(y_pub_key_handle), - ManagedBuffer::from_handle(private_key_handle), - ) + unsafe { + ( + BigUint::from_handle(x_pub_key_handle), + BigUint::from_handle(y_pub_key_handle), + ManagedBuffer::from_handle(private_key_handle), + ) + } } } diff --git a/framework/base/src/types/managed/basic/managed_buffer.rs b/framework/base/src/types/managed/basic/managed_buffer.rs index 34ba82da14..d1e0b52f9e 100644 --- a/framework/base/src/types/managed/basic/managed_buffer.rs +++ b/framework/base/src/types/managed/basic/managed_buffer.rs @@ -2,7 +2,7 @@ use crate::{ abi::{TypeAbi, TypeAbiFrom, TypeName}, api::{ use_raw_handle, ErrorApiImpl, HandleConstraints, InvalidSliceError, ManagedBufferApiImpl, - ManagedTypeApi, StaticVarApiImpl, + ManagedTypeApi, RawHandle, StaticVarApiImpl, }, codec::{ DecodeErrorHandler, Empty, EncodeErrorHandler, NestedDecode, NestedDecodeInput, @@ -14,7 +14,8 @@ use crate::{ SCLowerHex, }, types::{ - heap::BoxedBytes, ManagedBufferCachedBuilder, ManagedType, StaticBufferRef, + heap::BoxedBytes, ManagedBufferCachedBuilder, ManagedRef, ManagedRefMut, ManagedType, + StaticBufferRef, }, }; @@ -28,7 +29,7 @@ impl ManagedType for ManagedBuffer { type OwnHandle = M::ManagedBufferHandle; #[inline] - fn from_handle(handle: M::ManagedBufferHandle) -> Self { + unsafe fn from_handle(handle: M::ManagedBufferHandle) -> Self { ManagedBuffer { handle } } @@ -48,29 +49,61 @@ impl ManagedType for ManagedBuffer { impl ManagedBuffer { #[inline] pub fn new() -> Self { - let new_handle: M::ManagedBufferHandle = - use_raw_handle(M::static_var_api_impl().next_handle()); - // TODO: remove after VM no longer crashes with "unknown handle": - M::managed_type_impl().mb_overwrite(new_handle.clone(), &[]); - ManagedBuffer::from_handle(new_handle) + Self::new_from_bytes(&[]) } #[inline] pub fn new_from_bytes(bytes: &[u8]) -> Self { - let new_handle: M::ManagedBufferHandle = - use_raw_handle(M::static_var_api_impl().next_handle()); - M::managed_type_impl().mb_overwrite(new_handle.clone(), bytes); - ManagedBuffer::from_handle(new_handle) + unsafe { + let result = Self::new_uninit(); + M::managed_type_impl().mb_overwrite(result.get_handle(), bytes); + result + } } #[inline] pub fn new_random(nr_bytes: usize) -> Self { + unsafe { + let result = Self::new_uninit(); + M::managed_type_impl().mb_set_random(result.get_handle(), nr_bytes); + result + } + } + + /// Creates a new object, without initializing it. + /// + /// ## Safety + /// + /// The value needs to be initialized after creation, otherwise the VM will halt the first time the value is attempted to be read. + pub unsafe fn new_uninit() -> Self { let new_handle: M::ManagedBufferHandle = use_raw_handle(M::static_var_api_impl().next_handle()); - M::managed_type_impl().mb_set_random(new_handle.clone(), nr_bytes); ManagedBuffer::from_handle(new_handle) } + /// Creates a shared managed reference to a given raw handle. + /// + /// ## Safety + /// + /// The reference points to a shared value. Make sure the handle is not leaked. + + pub unsafe fn temp_const_ref( + raw_handle: RawHandle, + ) -> ManagedRef<'static, M, ManagedBuffer> { + ManagedRef::wrap_handle(use_raw_handle(raw_handle)) + } + + /// Creates a shared managed reference to a given raw handle. + /// + /// ## Safety + /// + /// The reference points to a shared value. Make sure the handle is not leaked. + pub unsafe fn temp_const_ref_mut( + raw_handle: RawHandle, + ) -> ManagedRefMut<'static, M, ManagedBuffer> { + ManagedRefMut::wrap_handle(use_raw_handle(raw_handle)) + } + fn load_static_cache(&self) -> StaticBufferRef where M: ManagedTypeApi, @@ -239,7 +272,7 @@ impl ManagedBuffer { result_handle.clone(), ); if err_result.is_ok() { - Some(ManagedBuffer::from_handle(result_handle)) + Some(unsafe { ManagedBuffer::from_handle(result_handle) }) } else { None } @@ -352,7 +385,7 @@ impl Clone for ManagedBuffer { let api = M::managed_type_impl(); let clone_handle = api.mb_new_empty(); api.mb_append(clone_handle.clone(), self.handle.clone()); - ManagedBuffer::from_handle(clone_handle) + unsafe { ManagedBuffer::from_handle(clone_handle) } } } @@ -482,9 +515,9 @@ impl TypeAbi for ManagedBuffer { impl SCDisplay for ManagedBuffer { fn fmt(&self, f: &mut F) { - f.append_managed_buffer(&ManagedBuffer::from_handle( - self.get_handle().cast_or_signal_error::(), - )); + let cast_handle = self.get_handle().cast_or_signal_error::(); + let wrap_cast = unsafe { ManagedRef::wrap_handle(cast_handle) }; + f.append_managed_buffer(&wrap_cast); } } @@ -493,18 +526,18 @@ impl SCLowerHex for ManagedBuffer { let hex_handle: M::ManagedBufferHandle = use_raw_handle(crate::api::const_handles::MBUF_TEMPORARY_1); M::managed_type_impl().mb_to_hex(self.handle.clone(), hex_handle.clone()); - f.append_managed_buffer(&ManagedBuffer::from_handle( - hex_handle.cast_or_signal_error::(), - )); + let cast_handle = hex_handle.cast_or_signal_error::(); + let wrap_cast = unsafe { ManagedRef::wrap_handle(cast_handle) }; + f.append_managed_buffer(&wrap_cast); } } impl SCBinary for ManagedBuffer { fn fmt(&self, f: &mut F) { // TODO: in Rust thr `0b` prefix appears only when writing "{:#x}", not "{:x}" - f.append_managed_buffer_binary(&ManagedBuffer::from_handle( - self.get_handle().cast_or_signal_error::(), - )); + let cast_handle = self.get_handle().cast_or_signal_error::(); + let wrap_cast = unsafe { ManagedRef::wrap_handle(cast_handle) }; + f.append_managed_buffer_binary(&wrap_cast); } } diff --git a/framework/base/src/types/managed/basic/managed_map.rs b/framework/base/src/types/managed/basic/managed_map.rs index ed39011f8d..0d7cc2f1b4 100644 --- a/framework/base/src/types/managed/basic/managed_map.rs +++ b/framework/base/src/types/managed/basic/managed_map.rs @@ -15,7 +15,7 @@ impl ManagedType for ManagedMap { type OwnHandle = M::ManagedMapHandle; #[inline] - fn from_handle(handle: M::ManagedMapHandle) -> Self { + unsafe fn from_handle(handle: M::ManagedMapHandle) -> Self { ManagedMap { handle } } @@ -35,7 +35,7 @@ impl ManagedType for ManagedMap { impl ManagedMap { pub fn new() -> Self { let new_handle = M::managed_type_impl().mm_new(); - ManagedMap::from_handle(new_handle) + unsafe { ManagedMap::from_handle(new_handle) } } } @@ -48,10 +48,15 @@ impl Default for ManagedMap { impl ManagedMap { pub fn get(&self, key: &ManagedBuffer) -> ManagedBuffer { - let new_handle: M::ManagedBufferHandle = - use_raw_handle(M::static_var_api_impl().next_handle()); - M::managed_type_impl().mm_get(self.handle.clone(), key.handle.clone(), new_handle.clone()); - ManagedBuffer::from_handle(new_handle) + unsafe { + let result = ManagedBuffer::new_uninit(); + M::managed_type_impl().mm_get( + self.handle.clone(), + key.handle.clone(), + result.get_handle(), + ); + result + } } pub fn put(&mut self, key: &ManagedBuffer, value: &ManagedBuffer) { @@ -70,7 +75,7 @@ impl ManagedMap { key.handle.clone(), new_handle.clone(), ); - ManagedBuffer::from_handle(new_handle) + unsafe { ManagedBuffer::from_handle(new_handle) } } pub fn contains(&self, key: &ManagedBuffer) -> bool { diff --git a/framework/base/src/types/managed/managed_type_trait.rs b/framework/base/src/types/managed/managed_type_trait.rs index e7b97e2ab6..ca9210d594 100644 --- a/framework/base/src/types/managed/managed_type_trait.rs +++ b/framework/base/src/types/managed/managed_type_trait.rs @@ -7,12 +7,12 @@ pub trait ManagedType: Sized { type OwnHandle: HandleConstraints; #[doc(hidden)] - fn from_handle(handle: Self::OwnHandle) -> Self; + unsafe fn from_handle(handle: Self::OwnHandle) -> Self; fn get_handle(&self) -> Self::OwnHandle; #[doc(hidden)] - fn from_raw_handle(handle: RawHandle) -> Self { + unsafe fn from_raw_handle(handle: RawHandle) -> Self { Self::from_handle(Self::OwnHandle::new(handle)) } diff --git a/framework/base/src/types/managed/multi_value/multi_value_encoded.rs b/framework/base/src/types/managed/multi_value/multi_value_encoded.rs index 04dbde9357..31d9a25e64 100644 --- a/framework/base/src/types/managed/multi_value/multi_value_encoded.rs +++ b/framework/base/src/types/managed/multi_value/multi_value_encoded.rs @@ -115,7 +115,7 @@ where M: ManagedTypeApi, { pub fn to_arg_buffer(&self) -> ManagedArgBuffer { - ManagedArgBuffer::from_handle(self.raw_buffers.get_handle()) + unsafe { ManagedArgBuffer::from_handle(self.raw_buffers.get_handle()) } } } diff --git a/framework/base/src/types/managed/multi_value/multi_value_managed_vec.rs b/framework/base/src/types/managed/multi_value/multi_value_managed_vec.rs index e628ba8421..6ad36a25e8 100644 --- a/framework/base/src/types/managed/multi_value/multi_value_managed_vec.rs +++ b/framework/base/src/types/managed/multi_value/multi_value_managed_vec.rs @@ -46,7 +46,7 @@ where type OwnHandle = M::ManagedBufferHandle; #[inline] - fn from_handle(handle: M::ManagedBufferHandle) -> Self { + unsafe fn from_handle(handle: M::ManagedBufferHandle) -> Self { Self(ManagedVec::from_handle(handle)) } diff --git a/framework/base/src/types/managed/wrapped/big_uint.rs b/framework/base/src/types/managed/wrapped/big_uint.rs index dfba440073..fc717e70c6 100644 --- a/framework/base/src/types/managed/wrapped/big_uint.rs +++ b/framework/base/src/types/managed/wrapped/big_uint.rs @@ -26,7 +26,7 @@ pub struct BigUint { impl ManagedType for BigUint { type OwnHandle = M::BigIntHandle; - fn from_handle(handle: M::BigIntHandle) -> Self { + unsafe fn from_handle(handle: M::BigIntHandle) -> Self { BigUint { value: BigInt::from_handle(handle), } @@ -66,6 +66,17 @@ impl From<&ManagedBuffer> for BigUint { } impl BigUint { + /// Creates a new object, without initializing it. + /// + /// ## Safety + /// + /// The value needs to be initialized after creation, otherwise the VM will halt the first time the value is attempted to be read. + pub unsafe fn new_uninit() -> Self { + BigUint { + value: BigInt::new_uninit(), + } + } + pub(crate) fn set_value(handle: M::BigIntHandle, value: T) where T: TryInto + num_traits::Unsigned, @@ -77,9 +88,11 @@ impl BigUint { where T: TryInto + num_traits::Unsigned, { - let handle: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); - Self::set_value(handle.clone(), value); - BigUint::from_handle(handle) + unsafe { + let result = Self::new_uninit(); + Self::set_value(result.get_handle(), value); + result + } } pub(crate) fn make_temp(handle: RawHandle, value: T) -> M::BigIntHandle @@ -173,9 +186,11 @@ impl Default for BigUint { impl BigUint { #[inline] pub fn zero() -> Self { - let handle: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); - M::managed_type_impl().bi_set_int64(handle.clone(), 0); - BigUint::from_handle(handle) + unsafe { + let result = Self::new_uninit(); + M::managed_type_impl().bi_set_int64(result.get_handle(), 0); + result + } } pub fn zero_ref() -> ManagedRef<'static, M, BigUint> { @@ -199,9 +214,11 @@ impl BigUint { pub fn from_bytes_be(bytes: &[u8]) -> Self { let mb_handle: M::ManagedBufferHandle = use_raw_handle(const_handles::MBUF_TEMPORARY_1); M::managed_type_impl().mb_overwrite(mb_handle.clone(), bytes); - let handle: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); - M::managed_type_impl().mb_to_big_int_unsigned(mb_handle, handle.clone()); - BigUint::from_handle(handle) + unsafe { + let result = Self::new_uninit(); + M::managed_type_impl().mb_to_big_int_unsigned(mb_handle, result.get_handle()); + result + } } #[inline] @@ -217,7 +234,7 @@ impl BigUint { let handle: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); M::managed_type_impl() .mb_to_big_int_unsigned(managed_buffer.handle.clone(), handle.clone()); - BigUint::from_handle(handle) + unsafe { BigUint::from_handle(handle) } } #[inline] @@ -226,7 +243,7 @@ impl BigUint { use_raw_handle(M::static_var_api_impl().next_handle()); M::managed_type_impl() .mb_from_big_int_unsigned(self.value.handle.clone(), mb_handle.clone()); - ManagedBuffer::from_handle(mb_handle) + unsafe { ManagedBuffer::from_handle(mb_handle) } } } @@ -237,7 +254,7 @@ impl BigUint { let api = M::managed_type_impl(); let result_handle: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); api.bi_sqrt(result_handle.clone(), self.value.handle.clone()); - BigUint::from_handle(result_handle) + unsafe { BigUint::from_handle(result_handle) } } #[must_use] @@ -249,7 +266,7 @@ impl BigUint { self.value.handle.clone(), big_int_temp_1, ); - BigUint::from_handle(result_handle) + unsafe { BigUint::from_handle(result_handle) } } /// The whole part of the base-2 logarithm. @@ -310,7 +327,7 @@ impl Clone for BigUint { clone_handle.clone(), self.value.handle.clone(), ); - BigUint::from_handle(clone_handle) + unsafe { BigUint::from_handle(clone_handle) } } } @@ -376,9 +393,9 @@ impl SCDisplay for BigUint { fn fmt(&self, f: &mut F) { let str_handle: M::ManagedBufferHandle = use_raw_handle(const_handles::MBUF_TEMPORARY_1); M::managed_type_impl().bi_to_string(self.value.handle.clone(), str_handle.clone()); - f.append_managed_buffer(&ManagedBuffer::from_handle( - str_handle.cast_or_signal_error::(), - )); + let cast_handle = str_handle.cast_or_signal_error::(); + let wrap_cast = unsafe { ManagedRef::wrap_handle(cast_handle) }; + f.append_managed_buffer(&wrap_cast); } } diff --git a/framework/base/src/types/managed/wrapped/big_uint_operators.rs b/framework/base/src/types/managed/wrapped/big_uint_operators.rs index 934079eef7..a4becdcf10 100644 --- a/framework/base/src/types/managed/wrapped/big_uint_operators.rs +++ b/framework/base/src/types/managed/wrapped/big_uint_operators.rs @@ -18,7 +18,7 @@ macro_rules! binary_operator { self.value.handle.clone(), other.value.handle.clone(), ); - BigUint::from_handle(self.value.handle.clone()) + self } } @@ -33,7 +33,7 @@ macro_rules! binary_operator { self.value.handle.clone(), other.value.handle.clone(), ); - BigUint::from_handle(result_handle) + unsafe { BigUint::from_handle(result_handle) } } } @@ -46,7 +46,7 @@ macro_rules! binary_operator { self.value.handle.clone(), other.value.handle.clone(), ); - BigUint::from_handle(self.value.handle.clone()) + self } } @@ -60,7 +60,7 @@ macro_rules! binary_operator { self.value.handle.clone(), big_int_temp_1, ); - BigUint::from_handle(self.value.handle.clone()) + self } } @@ -77,7 +77,7 @@ macro_rules! binary_operator { self.value.handle.clone(), big_int_temp_1, ); - BigUint::from_handle(result_handle) + unsafe { BigUint::from_handle(result_handle) } } } @@ -91,7 +91,7 @@ macro_rules! binary_operator { self.value.handle.clone(), big_int_temp_1, ); - BigUint::from_handle(self.value.handle.clone()) + self } } @@ -108,7 +108,7 @@ macro_rules! binary_operator { self.value.handle.clone(), big_int_temp_1, ); - BigUint::from_handle(result_handle) + unsafe { BigUint::from_handle(result_handle) } } } }; @@ -207,7 +207,7 @@ macro_rules! shift_traits { self.value.handle.clone(), rhs, ); - BigUint::from_handle(result_handle) + unsafe { BigUint::from_handle(result_handle) } } } }; diff --git a/framework/base/src/types/managed/wrapped/egld_or_esdt_token_identifier.rs b/framework/base/src/types/managed/wrapped/egld_or_esdt_token_identifier.rs index 19506645a2..97bf533e69 100644 --- a/framework/base/src/types/managed/wrapped/egld_or_esdt_token_identifier.rs +++ b/framework/base/src/types/managed/wrapped/egld_or_esdt_token_identifier.rs @@ -58,7 +58,7 @@ impl EgldOrEsdtTokenIdentifier { pub fn from_opt_raw_handle(opt_handle: Option) -> Self { match opt_handle { - Some(handle) => Self::esdt(TokenIdentifier::from_handle(handle)), + Some(handle) => Self::esdt(unsafe { TokenIdentifier::from_handle(handle) }), None => Self::egld(), } } @@ -240,9 +240,12 @@ impl TypeAbi for EgldOrEsdtTokenIdentifier { impl SCDisplay for EgldOrEsdtTokenIdentifier { fn fmt(&self, f: &mut F) { if let Some(token_identifier) = self.data.as_option() { - f.append_managed_buffer(&ManagedBuffer::from_handle( - token_identifier.get_handle().cast_or_signal_error::(), - )); + let cast_handle = token_identifier.get_handle().cast_or_signal_error::(); + let wrap_cast = unsafe { ManagedRef::wrap_handle(cast_handle) }; + f.append_managed_buffer(&wrap_cast); + // f.append_managed_buffer(&ManagedBuffer::from_handle( + // token_identifier.get_handle().cast_or_signal_error::(), + // )); } else { f.append_bytes(Self::EGLD_REPRESENTATION); } @@ -254,9 +257,12 @@ const EGLD_REPRESENTATION_HEX: &[u8] = b"45474C44"; impl SCLowerHex for EgldOrEsdtTokenIdentifier { fn fmt(&self, f: &mut F) { if let Some(token_identifier) = self.data.as_option() { - f.append_managed_buffer_lower_hex(&ManagedBuffer::from_handle( - token_identifier.get_handle().cast_or_signal_error::(), - )); + let cast_handle = token_identifier.get_handle().cast_or_signal_error::(); + let wrap_cast = unsafe { ManagedRef::wrap_handle(cast_handle) }; + f.append_managed_buffer_lower_hex(&wrap_cast); + // f.append_managed_buffer_lower_hex(&ManagedBuffer::from_handle( + // token_identifier.get_handle().cast_or_signal_error::(), + // )); } else { f.append_bytes(EGLD_REPRESENTATION_HEX); } diff --git a/framework/base/src/types/managed/wrapped/managed_address.rs b/framework/base/src/types/managed/wrapped/managed_address.rs index 18473419e4..a12200a039 100644 --- a/framework/base/src/types/managed/wrapped/managed_address.rs +++ b/framework/base/src/types/managed/wrapped/managed_address.rs @@ -2,7 +2,7 @@ use core::convert::{TryFrom, TryInto}; use crate::{ abi::{TypeAbi, TypeAbiFrom, TypeName}, - api::ManagedTypeApi, + api::{use_raw_handle, ManagedTypeApi, RawHandle}, codec::{ DecodeError, DecodeErrorHandler, EncodeErrorHandler, NestedDecode, NestedDecodeInput, NestedEncode, NestedEncodeOutput, TopDecode, TopDecodeInput, TopEncode, TopEncodeOutput, @@ -11,6 +11,8 @@ use crate::{ types::{heap::Address, ManagedBuffer, ManagedByteArray, ManagedType}, }; +use super::ManagedRef; + #[repr(transparent)] #[derive(Clone)] pub struct ManagedAddress { @@ -49,6 +51,28 @@ where } } + /// Creates a new object, without initializing it. + /// + /// ## Safety + /// + /// The value needs to be initialized after creation, otherwise the VM will halt the first time the value is attempted to be read. + pub unsafe fn new_uninit() -> Self { + ManagedAddress { + bytes: ManagedByteArray::new_uninit(), + } + } + + /// Creates a shared managed reference to a given raw handle. + /// + /// ## Safety + /// + /// The reference points to a shared value. Make sure the handle is not leaked. + pub unsafe fn temp_const_ref( + raw_handle: RawHandle, + ) -> ManagedRef<'static, M, ManagedAddress> { + ManagedRef::wrap_handle(use_raw_handle(raw_handle)) + } + #[inline] pub fn as_managed_buffer(&self) -> &ManagedBuffer { self.bytes.as_managed_buffer() @@ -133,7 +157,7 @@ where type OwnHandle = M::ManagedBufferHandle; #[inline] - fn from_handle(handle: M::ManagedBufferHandle) -> Self { + unsafe fn from_handle(handle: M::ManagedBufferHandle) -> Self { ManagedAddress { bytes: ManagedByteArray::from_handle(handle), } diff --git a/framework/base/src/types/managed/wrapped/managed_byte_array.rs b/framework/base/src/types/managed/wrapped/managed_byte_array.rs index 9d7b15d2b7..5ef3e661b7 100644 --- a/framework/base/src/types/managed/wrapped/managed_byte_array.rs +++ b/framework/base/src/types/managed/wrapped/managed_byte_array.rs @@ -35,7 +35,7 @@ where type OwnHandle = M::ManagedBufferHandle; #[inline] - fn from_handle(handle: M::ManagedBufferHandle) -> Self { + unsafe fn from_handle(handle: M::ManagedBufferHandle) -> Self { ManagedByteArray { buffer: ManagedBuffer::from_handle(handle), } @@ -85,6 +85,17 @@ where } } + /// Creates a new object, without initializing it. + /// + /// ## Safety + /// + /// The value needs to be initialized after creation, otherwise the VM will halt the first time the value is attempted to be read. + pub unsafe fn new_uninit() -> Self { + ManagedByteArray { + buffer: ManagedBuffer::new_uninit(), + } + } + /// Number of items. #[inline] pub fn len(&self) -> usize { diff --git a/framework/base/src/types/managed/wrapped/managed_decimal/managed_decimal_logarithm.rs b/framework/base/src/types/managed/wrapped/managed_decimal/managed_decimal_logarithm.rs index 41f173a50a..698b2fbbee 100644 --- a/framework/base/src/types/managed/wrapped/managed_decimal/managed_decimal_logarithm.rs +++ b/framework/base/src/types/managed/wrapped/managed_decimal/managed_decimal_logarithm.rs @@ -2,7 +2,7 @@ use super::decimals::{ConstDecimals, Decimals}; use super::ManagedDecimalSigned; use super::{ManagedDecimal, NumDecimals}; -use crate::proxy_imports::ManagedType; +use crate::types::ManagedRef; use crate::{ api::ManagedTypeApi, contract_base::ErrorHelper, @@ -104,7 +104,7 @@ impl ManagedDecimalSigned { return None; } - let bu = BigUint::from_handle(self.data.handle.clone()); + let bu = unsafe { ManagedRef::wrap_handle(self.data.handle.clone()) }; compute_ln(&bu, self.decimals.num_decimals()) } @@ -118,7 +118,8 @@ impl ManagedDecimalSigned { return None; } - let bu = BigUint::from_handle(self.data.handle.clone()); + let bu = unsafe { ManagedRef::wrap_handle(self.data.handle.clone()) }; + // let bu = BigUint::from_handle(self.data.handle.clone()); compute_log2(&bu, self.decimals.num_decimals()) } } diff --git a/framework/base/src/types/managed/wrapped/managed_decimal/managed_decimal_signed.rs b/framework/base/src/types/managed/wrapped/managed_decimal/managed_decimal_signed.rs index c8ee403f37..0338417f82 100644 --- a/framework/base/src/types/managed/wrapped/managed_decimal/managed_decimal_signed.rs +++ b/framework/base/src/types/managed/wrapped/managed_decimal/managed_decimal_signed.rs @@ -6,7 +6,7 @@ use crate::{ }, err_msg, formatter::{FormatBuffer, FormatByteReceiver, SCDisplay}, - types::{BigFloat, BigInt, BigUint, ManagedBuffer, ManagedType, Sign}, + types::{BigFloat, BigInt, BigUint, Sign}, }; use alloc::string::ToString; @@ -354,15 +354,15 @@ pub(super) fn managed_decimal_fmt( if len > num_dec { let temp_str_handle: M::ManagedBufferHandle = use_raw_handle(const_handles::MBUF_TEMPORARY_2); + let cast_handle = temp_str_handle.clone().cast_or_signal_error::(); + let temp_str_ref = unsafe { ManagedRef::wrap_handle(cast_handle) }; let _ = M::managed_type_impl().mb_copy_slice( full_str_handle.clone(), 0, len - num_dec, temp_str_handle.clone(), ); - f.append_managed_buffer(&ManagedBuffer::from_raw_handle( - temp_str_handle.get_raw_handle(), - )); + f.append_managed_buffer(&temp_str_ref); f.append_bytes(b"."); let _ = M::managed_type_impl().mb_copy_slice( full_str_handle.clone(), @@ -370,17 +370,15 @@ pub(super) fn managed_decimal_fmt( num_dec, temp_str_handle.clone(), ); - f.append_managed_buffer(&ManagedBuffer::from_raw_handle( - temp_str_handle.get_raw_handle(), - )); + f.append_managed_buffer(&temp_str_ref); } else { f.append_bytes(b"0."); for _ in len..num_dec { f.append_bytes(b"0"); } - f.append_managed_buffer(&ManagedBuffer::from_raw_handle( - full_str_handle.get_raw_handle(), - )); + let cast_handle = full_str_handle.clone().cast_or_signal_error::(); + let full_str_ref = unsafe { ManagedRef::wrap_handle(cast_handle) }; + f.append_managed_buffer(&full_str_ref); } } diff --git a/framework/base/src/types/managed/wrapped/managed_option.rs b/framework/base/src/types/managed/wrapped/managed_option.rs index e448ae8e88..e433913835 100644 --- a/framework/base/src/types/managed/wrapped/managed_option.rs +++ b/framework/base/src/types/managed/wrapped/managed_option.rs @@ -143,7 +143,7 @@ where F: FnOnce(Context, &T) -> R, { if self.is_some() { - f(context, &T::from_handle(self.handle.clone())) + f(context, unsafe { &T::from_handle(self.handle.clone()) }) } else { default(context) } @@ -158,7 +158,7 @@ where #[allow(clippy::redundant_clone)] // the clone is not redundant fn clone(&self) -> Self { if self.is_some() { - Self::some(T::from_handle(self.handle.clone()).clone()) + Self::some(unsafe { T::from_handle(self.handle.clone()) }.clone()) } else { Self::none() } @@ -177,7 +177,10 @@ where return true; } if self.is_some() && other.is_some() { - return T::from_handle(self.handle.clone()) == T::from_handle(other.handle.clone()); + unsafe { + return ManagedRef::<'_, _, T>::wrap_handle(self.handle.clone()) + == ManagedRef::<'_, _, T>::wrap_handle(other.handle.clone()); + } } false } @@ -327,7 +330,7 @@ where fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { if self.is_some() { f.debug_tuple("ManagedOption::Some") - .field(&T::from_handle(self.handle.clone())) + .field(unsafe { &T::from_handle(self.handle.clone()) }) .finish() } else { f.write_str("ManagedOption::None") diff --git a/framework/base/src/types/managed/wrapped/managed_ref.rs b/framework/base/src/types/managed/wrapped/managed_ref.rs index 143d79e1d4..ef0e20fb9f 100644 --- a/framework/base/src/types/managed/wrapped/managed_ref.rs +++ b/framework/base/src/types/managed/wrapped/managed_ref.rs @@ -34,7 +34,7 @@ where /// Will completely disregard lifetimes, use with care. #[doc(hidden)] - pub(crate) unsafe fn wrap_handle(handle: T::OwnHandle) -> Self { + pub unsafe fn wrap_handle(handle: T::OwnHandle) -> Self { Self { _phantom_m: PhantomData, _phantom_t: PhantomData, diff --git a/framework/base/src/types/managed/wrapped/managed_vec.rs b/framework/base/src/types/managed/wrapped/managed_vec.rs index eb79ffebcd..582054e6ec 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec.rs @@ -45,7 +45,7 @@ where type OwnHandle = M::ManagedBufferHandle; #[inline] - fn from_handle(handle: M::ManagedBufferHandle) -> Self { + unsafe fn from_handle(handle: M::ManagedBufferHandle) -> Self { ManagedVec { buffer: ManagedBuffer::from_handle(handle), _phantom: PhantomData, @@ -768,4 +768,3 @@ where } } } - diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item.rs b/framework/base/src/types/managed/wrapped/managed_vec_item.rs index 8e7dc5512a..8e88624d75 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item.rs @@ -192,7 +192,7 @@ macro_rules! impl_managed_type { fn from_byte_reader(reader: Reader) -> Self { let handle = <$ty as ManagedType>::OwnHandle::from_byte_reader(reader); - $ty::from_handle(handle) + unsafe { $ty::from_handle(handle) } } unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( @@ -226,7 +226,7 @@ where fn from_byte_reader(reader: Reader) -> Self { let handle = >::OwnHandle::from_byte_reader(reader); - Self::from_handle(handle) + unsafe { Self::from_handle(handle) } } unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( @@ -255,7 +255,7 @@ where fn from_byte_reader(reader: Reader) -> Self { let handle = M::ManagedBufferHandle::from_byte_reader(reader); - Self::from_handle(handle) + unsafe { Self::from_handle(handle) } } unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( diff --git a/framework/base/src/types/managed/wrapped/token_identifier.rs b/framework/base/src/types/managed/wrapped/token_identifier.rs index 69481772e0..10c92475a3 100644 --- a/framework/base/src/types/managed/wrapped/token_identifier.rs +++ b/framework/base/src/types/managed/wrapped/token_identifier.rs @@ -9,7 +9,7 @@ use crate::{ types::{ManagedBuffer, ManagedType}, }; -use super::EgldOrEsdtTokenIdentifier; +use super::{EgldOrEsdtTokenIdentifier, ManagedRef}; /// Specialized type for handling token identifiers. /// It wraps a BoxedBytes with the full ASCII name of the token. @@ -26,7 +26,7 @@ impl ManagedType for TokenIdentifier { type OwnHandle = M::ManagedBufferHandle; #[inline] - fn from_handle(handle: M::ManagedBufferHandle) -> Self { + unsafe fn from_handle(handle: M::ManagedBufferHandle) -> Self { TokenIdentifier { buffer: ManagedBuffer::from_handle(handle), } @@ -194,17 +194,17 @@ impl TypeAbi for TokenIdentifier { impl SCDisplay for TokenIdentifier { fn fmt(&self, f: &mut F) { - f.append_managed_buffer(&ManagedBuffer::from_handle( - self.buffer.get_handle().cast_or_signal_error::(), - )); + let cast_handle = self.buffer.get_handle().cast_or_signal_error::(); + let wrap_cast = unsafe { ManagedRef::wrap_handle(cast_handle) }; + f.append_managed_buffer(&wrap_cast); } } impl SCLowerHex for TokenIdentifier { fn fmt(&self, f: &mut F) { - f.append_managed_buffer_lower_hex(&ManagedBuffer::from_handle( - self.buffer.get_handle().cast_or_signal_error::(), - )); + let cast_handle = self.buffer.get_handle().cast_or_signal_error::(); + let wrap_cast = unsafe { ManagedRef::wrap_handle(cast_handle) }; + f.append_managed_buffer_lower_hex(&wrap_cast); } } From 9669110b9a32bb5454f950fed3af804ad2f4a9f6 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Sun, 10 Nov 2024 12:57:18 +0200 Subject: [PATCH 04/66] test fix --- tools/rust-debugger/format-tests/src/format_tests.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tools/rust-debugger/format-tests/src/format_tests.rs b/tools/rust-debugger/format-tests/src/format_tests.rs index 2c72e4acda..755bbbda17 100644 --- a/tools/rust-debugger/format-tests/src/format_tests.rs +++ b/tools/rust-debugger/format-tests/src/format_tests.rs @@ -181,7 +181,7 @@ fn main() { let invalid_handle = DebugHandle::from(-1000); let biguint_with_invalid_handle: BigUint = - BigUint::from_handle(invalid_handle.clone()); + unsafe { BigUint::from_handle(invalid_handle.clone()) }; push!( to_check, biguint_with_invalid_handle, @@ -189,7 +189,7 @@ fn main() { ); let big_float_with_invalid_handle: BigFloat = - BigFloat::from_handle(invalid_handle.clone()); + unsafe { BigFloat::from_handle(invalid_handle.clone()) }; push!( to_check, big_float_with_invalid_handle, @@ -197,7 +197,7 @@ fn main() { ); let managed_buffer_with_invalid_handle: ManagedBuffer = - ManagedBuffer::from_handle(invalid_handle.clone()); + unsafe { ManagedBuffer::from_handle(invalid_handle.clone()) }; push!( to_check, managed_buffer_with_invalid_handle, @@ -205,7 +205,7 @@ fn main() { ); let token_identifier_with_invalid_handle: TokenIdentifier = - TokenIdentifier::from_handle(invalid_handle.clone()); + unsafe { TokenIdentifier::from_handle(invalid_handle.clone()) }; push!( to_check, token_identifier_with_invalid_handle, @@ -213,7 +213,7 @@ fn main() { ); let optional_value_some_with_invalid_handle: OptionalValue> = - OptionalValue::Some(BigUint::from_handle(invalid_handle.clone())); + OptionalValue::Some(unsafe { BigUint::from_handle(invalid_handle.clone()) }); push!( to_check, optional_value_some_with_invalid_handle, From 6e5b66a6630d11851a2f769aa6c4281210677a62 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Sun, 10 Nov 2024 22:24:58 +0200 Subject: [PATCH 05/66] ManagedType from_handle refactor - BigFloat --- framework/base/src/io/arg_de_input.rs | 4 - .../base/src/types/managed/basic/big_float.rs | 127 ++++++++++-------- .../managed/basic/big_float_operators.rs | 30 +++-- 3 files changed, 90 insertions(+), 71 deletions(-) diff --git a/framework/base/src/io/arg_de_input.rs b/framework/base/src/io/arg_de_input.rs index 7c84d9cbb9..1b5502b6fa 100644 --- a/framework/base/src/io/arg_de_input.rs +++ b/framework/base/src/io/arg_de_input.rs @@ -64,10 +64,6 @@ where .load_argument_big_int_unsigned(self.arg_index, result.get_handle()); result } - - // let bi_handle: AA::BigIntHandle = use_raw_handle(AA::static_var_api_impl().next_handle()); - // AA::argument_api_impl().load_argument_big_int_unsigned(self.arg_index, bi_handle.clone()); - // BigUint::from_handle(bi_handle) } } diff --git a/framework/base/src/types/managed/basic/big_float.rs b/framework/base/src/types/managed/basic/big_float.rs index 8361cb4b79..f5a78b307a 100644 --- a/framework/base/src/types/managed/basic/big_float.rs +++ b/framework/base/src/types/managed/basic/big_float.rs @@ -102,31 +102,31 @@ big_float_conv_num! {i8} impl BigFloat { pub fn neg(&self) -> Self { - let new_bf_handle: M::BigFloatHandle = - use_raw_handle(M::static_var_api_impl().next_handle()); - M::managed_type_impl().bf_neg(new_bf_handle.clone(), self.handle.clone()); - unsafe { BigFloat::from_handle(new_bf_handle) } + unsafe { + let result = BigFloat::new_uninit(); + M::managed_type_impl().bf_neg(result.get_handle(), self.handle.clone()); + result + } } pub fn abs(&self) -> Self { - let new_bf_handle: M::BigFloatHandle = - use_raw_handle(M::static_var_api_impl().next_handle()); - M::managed_type_impl().bf_abs(new_bf_handle.clone(), self.handle.clone()); - unsafe { BigFloat::from_handle(new_bf_handle) } + unsafe { + let result = BigFloat::new_uninit(); + M::managed_type_impl().bf_abs(result.get_handle(), self.handle.clone()); + result + } } - pub fn from_big_uint(big_uint: &BigUint) -> Self { - let new_bf_handle: M::BigFloatHandle = - use_raw_handle(M::static_var_api_impl().next_handle()); - M::managed_type_impl().bf_set_bi(new_bf_handle.clone(), big_uint.value.handle.clone()); - unsafe { BigFloat::from_handle(new_bf_handle) } + pub fn from_big_int(big_int: &BigInt) -> Self { + unsafe { + let result = BigFloat::new_uninit(); + M::managed_type_impl().bf_set_bi(result.get_handle(), big_int.handle.clone()); + result + } } - pub fn from_big_int(big_int: &BigInt) -> Self { - let new_bf_handle: M::BigFloatHandle = - use_raw_handle(M::static_var_api_impl().next_handle()); - M::managed_type_impl().bf_set_bi(new_bf_handle.clone(), big_int.handle.clone()); - unsafe { BigFloat::from_handle(new_bf_handle) } + pub fn from_big_uint(big_uint: &BigUint) -> Self { + Self::from_big_int(big_uint.as_big_int()) } #[inline] @@ -156,24 +156,27 @@ impl BigFloat { } pub fn trunc(&self) -> BigInt { - let result: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); - let api = M::managed_type_impl(); - api.bf_trunc(result.clone(), self.handle.clone()); - unsafe { BigInt::from_handle(result) } + unsafe { + let result = BigInt::new_uninit(); + M::managed_type_impl().bf_trunc(result.get_handle(), self.handle.clone()); + result + } } pub fn floor(&self) -> BigInt { - let result: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); - let api = M::managed_type_impl(); - api.bf_floor(result.clone(), self.handle.clone()); - unsafe { BigInt::from_handle(result) } + unsafe { + let result = BigInt::new_uninit(); + M::managed_type_impl().bf_floor(result.get_handle(), self.handle.clone()); + result + } } pub fn ceil(&self) -> BigInt { - let result: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); - let api = M::managed_type_impl(); - api.bf_ceil(result.clone(), self.handle.clone()); - unsafe { BigInt::from_handle(result) } + unsafe { + let result = BigInt::new_uninit(); + M::managed_type_impl().bf_ceil(result.get_handle(), self.handle.clone()); + result + } } pub fn to_fixed_point(&self, denominator: &BigFloat) -> BigInt { @@ -258,34 +261,48 @@ impl BigFloat { } pub fn from_buffer(managed_buffer: &ManagedBuffer) -> Self { - let new_bf_handle: M::BigFloatHandle = - use_raw_handle(M::static_var_api_impl().next_handle()); - M::managed_type_impl() - .mb_to_big_float(managed_buffer.handle.clone(), new_bf_handle.clone()); - unsafe { BigFloat::from_handle(new_bf_handle) } + unsafe { + let result = BigFloat::new_uninit(); + M::managed_type_impl() + .mb_to_big_float(managed_buffer.handle.clone(), result.get_handle()); + result + } } pub fn to_buffer(&self) -> ManagedBuffer { - let new_man_buf_handle: M::ManagedBufferHandle = - use_raw_handle(M::static_var_api_impl().next_handle()); - M::managed_type_impl().mb_from_big_float(self.handle.clone(), new_man_buf_handle.clone()); - unsafe { ManagedBuffer::from_handle(new_man_buf_handle) } + unsafe { + let result = ManagedBuffer::new_uninit(); + M::managed_type_impl().mb_from_big_float(self.get_handle(), result.get_handle()); + result + } + } + + /// Creates a new object, without initializing it. + /// + /// ## Safety + /// + /// The value needs to be initialized after creation, otherwise the VM will halt the first time the value is attempted to be read. + pub unsafe fn new_uninit() -> Self { + let new_handle: M::BigFloatHandle = use_raw_handle(M::static_var_api_impl().next_handle()); + BigFloat::from_handle(new_handle) } } impl BigFloat { pub fn sqrt(&self) -> Self { - let api = M::managed_type_impl(); - let new_handle: M::BigFloatHandle = use_raw_handle(M::static_var_api_impl().next_handle()); - api.bf_sqrt(new_handle.clone(), self.handle.clone()); - unsafe { BigFloat::from_handle(new_handle) } + unsafe { + let result = BigFloat::new_uninit(); + M::managed_type_impl().bf_sqrt(result.get_handle(), self.handle.clone()); + result + } } pub fn pow(&self, exp: i32) -> Self { - let api = M::managed_type_impl(); - let new_handle: M::BigFloatHandle = use_raw_handle(M::static_var_api_impl().next_handle()); - api.bf_pow(new_handle.clone(), self.handle.clone(), exp); - unsafe { BigFloat::from_handle(new_handle) } + unsafe { + let result = BigFloat::new_uninit(); + M::managed_type_impl().bf_pow(result.get_handle(), self.handle.clone(), exp); + result + } } /// Returns the sign of the `BigFloat` as a `Sign`. @@ -299,9 +316,11 @@ impl BigFloat { /// Returns the magnitude of the `BigFloat` pub fn magnitude(&self) -> BigFloat { - let result: M::BigFloatHandle = use_raw_handle(M::static_var_api_impl().next_handle()); - M::managed_type_impl().bf_abs(result.clone(), self.handle.clone()); - unsafe { BigFloat::from_handle(result) } + unsafe { + let result = BigFloat::new_uninit(); + M::managed_type_impl().bf_abs(result.get_handle(), self.handle.clone()); + result + } } /// Convert this `BigFloat` into its `Sign` and its magnitude, @@ -341,9 +360,11 @@ impl BigFloat { impl Clone for BigFloat { fn clone(&self) -> Self { - let new_handle: M::BigFloatHandle = use_raw_handle(M::static_var_api_impl().next_handle()); - M::managed_type_impl().bf_clone(new_handle.clone(), self.handle.clone()); - unsafe { BigFloat::from_handle(new_handle) } + unsafe { + let result = BigFloat::new_uninit(); + M::managed_type_impl().bf_clone(result.get_handle(), self.handle.clone()); + result + } } } diff --git a/framework/base/src/types/managed/basic/big_float_operators.rs b/framework/base/src/types/managed/basic/big_float_operators.rs index 7333a69ebe..e8d27016a8 100644 --- a/framework/base/src/types/managed/basic/big_float_operators.rs +++ b/framework/base/src/types/managed/basic/big_float_operators.rs @@ -1,6 +1,6 @@ use super::BigFloat; use crate::{ - api::{use_raw_handle, BigFloatApiImpl, ManagedTypeApi, StaticVarApiImpl}, + api::{BigFloatApiImpl, ManagedTypeApi}, types::managed::managed_type_trait::ManagedType, }; use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; @@ -16,7 +16,7 @@ macro_rules! binary_operator { self.handle.clone(), other.handle.clone(), ); - unsafe { BigFloat::from_handle(self.handle.clone()) } + self } } @@ -24,14 +24,15 @@ macro_rules! binary_operator { type Output = BigFloat; fn $method(self, other: &BigFloat) -> BigFloat { - let result_handle: M::BigFloatHandle = - use_raw_handle(M::static_var_api_impl().next_handle()); - M::managed_type_impl().$api_func( - result_handle.clone(), - self.handle.clone(), - other.handle.clone(), - ); - unsafe { BigFloat::from_handle(result_handle) } + unsafe { + let result = BigFloat::new_uninit(); + M::managed_type_impl().$api_func( + result.get_handle(), + self.handle.clone(), + other.handle.clone(), + ); + result + } } } }; @@ -79,9 +80,10 @@ impl Neg for BigFloat { type Output = BigFloat; fn neg(self) -> Self::Output { - let result_handle: M::BigFloatHandle = - use_raw_handle(M::static_var_api_impl().next_handle()); - M::managed_type_impl().bf_neg(result_handle.clone(), self.handle); - unsafe { BigFloat::from_handle(result_handle) } + unsafe { + let result = BigFloat::new_uninit(); + M::managed_type_impl().bf_neg(result.get_handle(), self.handle.clone()); + result + } } } From 11898eaf0c4869cacaf254d261bd1de75826e9cd Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Mon, 11 Nov 2024 13:17:28 +0200 Subject: [PATCH 06/66] ManagedType from_handle refactor - BigInt/BigUint --- .../base/src/types/managed/basic/big_float.rs | 9 +- .../base/src/types/managed/basic/big_int.rs | 86 +++++++++++-------- .../types/managed/basic/big_int_operators.rs | 11 +-- .../src/types/managed/basic/managed_map.rs | 19 ++-- .../src/types/managed/wrapped/big_uint.rs | 61 +++++-------- .../managed/wrapped/big_uint_operators.rs | 82 +++++++++--------- 6 files changed, 135 insertions(+), 133 deletions(-) diff --git a/framework/base/src/types/managed/basic/big_float.rs b/framework/base/src/types/managed/basic/big_float.rs index f5a78b307a..634d76f67d 100644 --- a/framework/base/src/types/managed/basic/big_float.rs +++ b/framework/base/src/types/managed/basic/big_float.rs @@ -85,10 +85,11 @@ macro_rules! big_float_conv_num { impl From<$num_ty> for BigFloat { #[inline] fn from(value: $num_ty) -> Self { - let new_bf_handle: M::BigFloatHandle = - use_raw_handle(M::static_var_api_impl().next_handle()); - M::managed_type_impl().bf_set_i64(new_bf_handle.clone(), value as i64); - unsafe { BigFloat::from_handle(new_bf_handle) } + unsafe { + let result = BigFloat::new_uninit(); + M::managed_type_impl().bf_set_i64(result.get_handle(), value as i64); + result + } } } }; diff --git a/framework/base/src/types/managed/basic/big_int.rs b/framework/base/src/types/managed/basic/big_int.rs index 9727d81dd5..13c3b965af 100644 --- a/framework/base/src/types/managed/basic/big_int.rs +++ b/framework/base/src/types/managed/basic/big_int.rs @@ -108,10 +108,11 @@ macro_rules! big_int_conv_num { impl From<$num_ty> for BigInt { #[inline] fn from(value: $num_ty) -> Self { - let handle: M::BigIntHandle = - use_raw_handle(M::static_var_api_impl().next_handle()); - Self::set_value(handle.clone(), value); - unsafe { BigInt::from_handle(handle) } + unsafe { + let result = BigInt::new_uninit(); + Self::set_value(result.get_handle(), value); + result + } } } @@ -167,9 +168,11 @@ impl From for BigInt { impl BigInt { #[inline] pub fn zero() -> Self { - let handle: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); - M::managed_type_impl().bi_set_int64(handle.clone(), 0); - unsafe { BigInt::from_handle(handle) } + unsafe { + let result = BigInt::new_uninit(); + M::managed_type_impl().bi_set_int64(result.get_handle(), 0); + result + } } #[inline] @@ -186,9 +189,11 @@ impl BigInt { pub fn from_signed_bytes_be(bytes: &[u8]) -> Self { let mb_handle: M::ManagedBufferHandle = use_raw_handle(const_handles::MBUF_TEMPORARY_1); M::managed_type_impl().mb_overwrite(mb_handle.clone(), bytes); - let handle: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); - M::managed_type_impl().mb_to_big_int_signed(mb_handle, handle.clone()); - unsafe { BigInt::from_handle(handle) } + unsafe { + let result = BigInt::new_uninit(); + M::managed_type_impl().mb_to_big_int_signed(mb_handle, result.get_handle()); + result + } } #[inline] @@ -200,41 +205,47 @@ impl BigInt { #[inline] pub fn from_signed_bytes_be_buffer(managed_buffer: &ManagedBuffer) -> Self { - let handle: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); - M::managed_type_impl().mb_to_big_int_signed(managed_buffer.handle.clone(), handle.clone()); - unsafe { BigInt::from_handle(handle) } + unsafe { + let result = BigInt::new_uninit(); + M::managed_type_impl() + .mb_to_big_int_signed(managed_buffer.handle.clone(), result.get_handle()); + result + } } #[inline] pub fn to_signed_bytes_be_buffer(&self) -> ManagedBuffer { - let mb_handle: M::ManagedBufferHandle = - use_raw_handle(M::static_var_api_impl().next_handle()); - M::managed_type_impl().mb_from_big_int_signed(self.handle.clone(), mb_handle.clone()); - unsafe { ManagedBuffer::from_handle(mb_handle) } + unsafe { + let result = ManagedBuffer::new_uninit(); + M::managed_type_impl().mb_from_big_int_signed(self.handle.clone(), result.get_handle()); + result + } } } impl Clone for BigInt { fn clone(&self) -> Self { let api = M::managed_type_impl(); - let clone_handle: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); - api.bi_set_int64(clone_handle.clone(), 0); - api.bi_add( - clone_handle.clone(), - clone_handle.clone(), - self.handle.clone(), - ); - unsafe { BigInt::from_handle(clone_handle) } + unsafe { + let result = BigInt::new_uninit(); + api.bi_set_int64(result.get_handle(), 0); + api.bi_add( + result.get_handle(), + result.get_handle(), + self.handle.clone(), + ); + result + } } } impl BigInt { pub fn from_biguint(sign: Sign, unsigned: BigUint) -> Self { - let api = M::managed_type_impl(); + let result = unsigned.into_big_int(); if sign.is_minus() { - api.bi_neg(unsigned.value.handle.clone(), unsigned.value.handle.clone()); + M::managed_type_impl().bi_neg(result.handle.clone(), result.handle.clone()); } - unsafe { BigInt::from_handle(unsigned.value.handle) } + result } /// Returns the sign of the `BigInt` as a `Sign`. @@ -249,10 +260,11 @@ impl BigInt { /// Returns the magnitude of the `BigInt` as a `BigUint`. pub fn magnitude(&self) -> BigUint { - let api = M::managed_type_impl(); - let result_handle: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); - api.bi_abs(result_handle.clone(), self.handle.clone()); - unsafe { BigUint::from_handle(result_handle) } + unsafe { + let result = BigUint::new_uninit(); + M::managed_type_impl().bi_abs(result.get_handle(), self.get_handle()); + result + } } /// Convert this `BigInt` into its `Sign` and `BigUint` magnitude, @@ -267,7 +279,7 @@ impl BigInt { /// /// If the number is negative, undefined behavior might occur further down the execution. pub unsafe fn into_big_uint_unchecked(self) -> BigUint { - BigUint::from_handle(self.handle) + BigUint { value: self } } /// Converts this `BigInt` into a `BigUint`, if it's not negative. @@ -342,10 +354,12 @@ impl TopDecode for BigInt { impl BigInt { #[must_use] pub fn pow(&self, exp: u32) -> Self { - let result_handle: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); let exp_handle = BigUint::::make_temp(const_handles::BIG_INT_TEMPORARY_1, exp); - M::managed_type_impl().bi_pow(result_handle.clone(), self.handle.clone(), exp_handle); - unsafe { BigInt::from_handle(result_handle) } + unsafe { + let result = BigInt::new_uninit(); + M::managed_type_impl().bi_pow(result.get_handle(), self.get_handle(), exp_handle); + result + } } } diff --git a/framework/base/src/types/managed/basic/big_int_operators.rs b/framework/base/src/types/managed/basic/big_int_operators.rs index 0cf00c66e6..9edf670cb9 100644 --- a/framework/base/src/types/managed/basic/big_int_operators.rs +++ b/framework/base/src/types/managed/basic/big_int_operators.rs @@ -3,7 +3,7 @@ use core::ops::{ }; use crate::{ - api::{use_raw_handle, BigIntApiImpl, ManagedTypeApi, StaticVarApiImpl}, + api::{BigIntApiImpl, ManagedTypeApi}, types::{BigInt, BigUint, ManagedType, Sign}, }; @@ -118,9 +118,10 @@ impl Neg for BigInt { type Output = BigInt; fn neg(self) -> Self::Output { - let api = M::managed_type_impl(); - let result_handle: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); - api.bi_neg(result_handle.clone(), self.handle); - unsafe { BigInt::from_handle(result_handle) } + unsafe { + let result = BigInt::new_uninit(); + M::managed_type_impl().bi_neg(result.get_handle(), self.handle); + result + } } } diff --git a/framework/base/src/types/managed/basic/managed_map.rs b/framework/base/src/types/managed/basic/managed_map.rs index 0d7cc2f1b4..139f93cf9c 100644 --- a/framework/base/src/types/managed/basic/managed_map.rs +++ b/framework/base/src/types/managed/basic/managed_map.rs @@ -1,5 +1,5 @@ use crate::{ - api::{use_raw_handle, ManagedMapApiImpl, ManagedTypeApi, StaticVarApiImpl}, + api::{ManagedMapApiImpl, ManagedTypeApi}, types::ManagedType, }; @@ -68,14 +68,15 @@ impl ManagedMap { } pub fn remove(&mut self, key: &ManagedBuffer) -> ManagedBuffer { - let new_handle: M::ManagedBufferHandle = - use_raw_handle(M::static_var_api_impl().next_handle()); - M::managed_type_impl().mm_remove( - self.handle.clone(), - key.handle.clone(), - new_handle.clone(), - ); - unsafe { ManagedBuffer::from_handle(new_handle) } + unsafe { + let result = ManagedBuffer::new_uninit(); + M::managed_type_impl().mm_remove( + self.handle.clone(), + key.handle.clone(), + result.get_handle(), + ); + result + } } pub fn contains(&self, key: &ManagedBuffer) -> bool { diff --git a/framework/base/src/types/managed/wrapped/big_uint.rs b/framework/base/src/types/managed/wrapped/big_uint.rs index fc717e70c6..344842c2a3 100644 --- a/framework/base/src/types/managed/wrapped/big_uint.rs +++ b/framework/base/src/types/managed/wrapped/big_uint.rs @@ -4,7 +4,7 @@ use crate::{ abi::{TypeAbi, TypeAbiFrom, TypeName}, api::{ const_handles, use_raw_handle, BigIntApiImpl, HandleConstraints, ManagedBufferApiImpl, - ManagedTypeApi, ManagedTypeApiImpl, RawHandle, StaticVarApiImpl, + ManagedTypeApi, ManagedTypeApiImpl, RawHandle, }, codec::{ DecodeErrorHandler, EncodeErrorHandler, NestedDecode, NestedDecodeInput, NestedEncode, @@ -210,7 +210,6 @@ impl BigUint { Self::set_value(self.value.handle.clone(), value); } - #[inline] pub fn from_bytes_be(bytes: &[u8]) -> Self { let mb_handle: M::ManagedBufferHandle = use_raw_handle(const_handles::MBUF_TEMPORARY_1); M::managed_type_impl().mb_overwrite(mb_handle.clone(), bytes); @@ -221,7 +220,6 @@ impl BigUint { } } - #[inline] pub fn to_bytes_be(&self) -> BoxedBytes { let mb_handle: M::ManagedBufferHandle = use_raw_handle(const_handles::MBUF_TEMPORARY_1); M::managed_type_impl() @@ -229,44 +227,40 @@ impl BigUint { M::managed_type_impl().mb_to_boxed_bytes(mb_handle) } - #[inline] pub fn from_bytes_be_buffer(managed_buffer: &ManagedBuffer) -> Self { - let handle: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); - M::managed_type_impl() - .mb_to_big_int_unsigned(managed_buffer.handle.clone(), handle.clone()); - unsafe { BigUint::from_handle(handle) } + unsafe { + let result = BigUint::new_uninit(); + M::managed_type_impl() + .mb_to_big_int_unsigned(managed_buffer.handle.clone(), result.get_handle()); + result + } } - #[inline] pub fn to_bytes_be_buffer(&self) -> ManagedBuffer { - let mb_handle: M::ManagedBufferHandle = - use_raw_handle(M::static_var_api_impl().next_handle()); - M::managed_type_impl() - .mb_from_big_int_unsigned(self.value.handle.clone(), mb_handle.clone()); - unsafe { ManagedBuffer::from_handle(mb_handle) } + unsafe { + let result = ManagedBuffer::new_uninit(); + M::managed_type_impl().mb_from_big_int_unsigned(self.get_handle(), result.get_handle()); + result + } } } impl BigUint { - #[inline] - #[must_use] pub fn sqrt(&self) -> Self { - let api = M::managed_type_impl(); - let result_handle: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); - api.bi_sqrt(result_handle.clone(), self.value.handle.clone()); - unsafe { BigUint::from_handle(result_handle) } + unsafe { + let result = BigUint::new_uninit(); + M::managed_type_impl().bi_sqrt(result.get_handle(), self.get_handle()); + result + } } - #[must_use] pub fn pow(&self, exp: u32) -> Self { - let result_handle: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); let big_int_temp_1 = BigUint::::make_temp(const_handles::BIG_INT_TEMPORARY_1, exp); - M::managed_type_impl().bi_pow( - result_handle.clone(), - self.value.handle.clone(), - big_int_temp_1, - ); - unsafe { BigUint::from_handle(result_handle) } + unsafe { + let result = BigUint::new_uninit(); + M::managed_type_impl().bi_pow(result.get_handle(), self.get_handle(), big_int_temp_1); + result + } } /// The whole part of the base-2 logarithm. @@ -275,7 +269,6 @@ impl BigUint { /// More specifically, the log2 floor is the position of the most significant bit minus one. /// /// Will return `None` for the number zero (the logarithm in this case would approach -inf). - #[inline] pub fn log2_floor(&self) -> Option { let api = M::managed_type_impl(); let result = api.bi_log2(self.value.handle.clone()); @@ -319,15 +312,7 @@ impl BigUint { impl Clone for BigUint { fn clone(&self) -> Self { - let api = M::managed_type_impl(); - let clone_handle: M::BigIntHandle = use_raw_handle(M::static_var_api_impl().next_handle()); - api.bi_set_int64(clone_handle.clone(), 0); - api.bi_add( - clone_handle.clone(), - clone_handle.clone(), - self.value.handle.clone(), - ); - unsafe { BigUint::from_handle(clone_handle) } + unsafe { self.as_big_int().clone().into_big_uint_unchecked() } } } diff --git a/framework/base/src/types/managed/wrapped/big_uint_operators.rs b/framework/base/src/types/managed/wrapped/big_uint_operators.rs index a4becdcf10..5ca27f783e 100644 --- a/framework/base/src/types/managed/wrapped/big_uint_operators.rs +++ b/framework/base/src/types/managed/wrapped/big_uint_operators.rs @@ -1,5 +1,5 @@ use crate::{ - api::{const_handles, use_raw_handle, BigIntApiImpl, ManagedTypeApi, StaticVarApiImpl}, + api::{const_handles, BigIntApiImpl, ManagedTypeApi}, types::{BigUint, ManagedType}, }; use core::ops::{ @@ -14,9 +14,9 @@ macro_rules! binary_operator { fn $method(self, other: BigUint) -> BigUint { M::managed_type_impl().$api_func( - self.value.handle.clone(), - self.value.handle.clone(), - other.value.handle.clone(), + self.get_handle(), + self.get_handle(), + other.get_handle(), ); self } @@ -26,14 +26,15 @@ macro_rules! binary_operator { type Output = BigUint; fn $method(self, other: &BigUint) -> BigUint { - let result_handle: M::BigIntHandle = - use_raw_handle(M::static_var_api_impl().next_handle()); - M::managed_type_impl().$api_func( - result_handle.clone(), - self.value.handle.clone(), - other.value.handle.clone(), - ); - unsafe { BigUint::from_handle(result_handle) } + unsafe { + let result = BigUint::new_uninit(); + M::managed_type_impl().$api_func( + result.get_handle(), + self.get_handle(), + other.get_handle(), + ); + result + } } } @@ -42,9 +43,9 @@ macro_rules! binary_operator { fn $method(self, other: &BigUint) -> BigUint { M::managed_type_impl().$api_func( - self.value.handle.clone(), - self.value.handle.clone(), - other.value.handle.clone(), + self.get_handle(), + self.get_handle(), + other.get_handle(), ); self } @@ -56,8 +57,8 @@ macro_rules! binary_operator { fn $method(self, other: u32) -> BigUint { let big_int_temp_1 = Self::make_temp(const_handles::BIG_INT_TEMPORARY_1, other); M::managed_type_impl().$api_func( - self.value.handle.clone(), - self.value.handle.clone(), + self.get_handle(), + self.get_handle(), big_int_temp_1, ); self @@ -70,14 +71,15 @@ macro_rules! binary_operator { fn $method(self, other: u32) -> BigUint { let big_int_temp_1 = BigUint::::make_temp(const_handles::BIG_INT_TEMPORARY_1, other); - let result_handle: M::BigIntHandle = - use_raw_handle(M::static_var_api_impl().next_handle()); - M::managed_type_impl().$api_func( - result_handle.clone(), - self.value.handle.clone(), - big_int_temp_1, - ); - unsafe { BigUint::from_handle(result_handle) } + unsafe { + let result = BigUint::new_uninit(); + M::managed_type_impl().$api_func( + result.get_handle(), + self.get_handle(), + big_int_temp_1, + ); + result + } } } @@ -101,14 +103,15 @@ macro_rules! binary_operator { fn $method(self, other: u64) -> BigUint { let big_int_temp_1 = BigUint::::make_temp(const_handles::BIG_INT_TEMPORARY_1, other); - let result_handle: M::BigIntHandle = - use_raw_handle(M::static_var_api_impl().next_handle()); - M::managed_type_impl().$api_func( - result_handle.clone(), - self.value.handle.clone(), - big_int_temp_1, - ); - unsafe { BigUint::from_handle(result_handle) } + unsafe { + let result = BigUint::new_uninit(); + M::managed_type_impl().$api_func( + result.get_handle(), + self.get_handle(), + big_int_temp_1, + ); + result + } } } }; @@ -200,14 +203,11 @@ macro_rules! shift_traits { type Output = BigUint; fn $method(self, rhs: usize) -> BigUint { - let result_handle: M::BigIntHandle = - use_raw_handle(M::static_var_api_impl().next_handle()); - M::managed_type_impl().$api_func( - result_handle.clone(), - self.value.handle.clone(), - rhs, - ); - unsafe { BigUint::from_handle(result_handle) } + unsafe { + let result = BigUint::new_uninit(); + M::managed_type_impl().$api_func(result.get_handle(), self.get_handle(), rhs); + result + } } } }; From 812e9e55c835ee2f20ac75f7b44326729dbce829 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Mon, 11 Nov 2024 13:17:34 +0200 Subject: [PATCH 07/66] cleanup --- .../types/managed/wrapped/egld_or_esdt_token_identifier.rs | 6 ------ .../wrapped/managed_decimal/managed_decimal_logarithm.rs | 1 - 2 files changed, 7 deletions(-) diff --git a/framework/base/src/types/managed/wrapped/egld_or_esdt_token_identifier.rs b/framework/base/src/types/managed/wrapped/egld_or_esdt_token_identifier.rs index 97bf533e69..6221188618 100644 --- a/framework/base/src/types/managed/wrapped/egld_or_esdt_token_identifier.rs +++ b/framework/base/src/types/managed/wrapped/egld_or_esdt_token_identifier.rs @@ -243,9 +243,6 @@ impl SCDisplay for EgldOrEsdtTokenIdentifier { let cast_handle = token_identifier.get_handle().cast_or_signal_error::(); let wrap_cast = unsafe { ManagedRef::wrap_handle(cast_handle) }; f.append_managed_buffer(&wrap_cast); - // f.append_managed_buffer(&ManagedBuffer::from_handle( - // token_identifier.get_handle().cast_or_signal_error::(), - // )); } else { f.append_bytes(Self::EGLD_REPRESENTATION); } @@ -260,9 +257,6 @@ impl SCLowerHex for EgldOrEsdtTokenIdentifier { let cast_handle = token_identifier.get_handle().cast_or_signal_error::(); let wrap_cast = unsafe { ManagedRef::wrap_handle(cast_handle) }; f.append_managed_buffer_lower_hex(&wrap_cast); - // f.append_managed_buffer_lower_hex(&ManagedBuffer::from_handle( - // token_identifier.get_handle().cast_or_signal_error::(), - // )); } else { f.append_bytes(EGLD_REPRESENTATION_HEX); } diff --git a/framework/base/src/types/managed/wrapped/managed_decimal/managed_decimal_logarithm.rs b/framework/base/src/types/managed/wrapped/managed_decimal/managed_decimal_logarithm.rs index 698b2fbbee..43c4debbac 100644 --- a/framework/base/src/types/managed/wrapped/managed_decimal/managed_decimal_logarithm.rs +++ b/framework/base/src/types/managed/wrapped/managed_decimal/managed_decimal_logarithm.rs @@ -119,7 +119,6 @@ impl ManagedDecimalSigned { } let bu = unsafe { ManagedRef::wrap_handle(self.data.handle.clone()) }; - // let bu = BigUint::from_handle(self.data.handle.clone()); compute_log2(&bu, self.decimals.num_decimals()) } } From da9bea3451ffd5fa9434ab844c10b16d83776f5f Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Tue, 12 Nov 2024 09:40:11 +0200 Subject: [PATCH 08/66] ManagedOption refactor --- .../wrapped/egld_or_esdt_token_identifier.rs | 33 ++++++++++--------- .../types/managed/wrapped/managed_option.rs | 27 ++++++++------- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/framework/base/src/types/managed/wrapped/egld_or_esdt_token_identifier.rs b/framework/base/src/types/managed/wrapped/egld_or_esdt_token_identifier.rs index 6221188618..7a4611011d 100644 --- a/framework/base/src/types/managed/wrapped/egld_or_esdt_token_identifier.rs +++ b/framework/base/src/types/managed/wrapped/egld_or_esdt_token_identifier.rs @@ -25,7 +25,8 @@ use crate as multiversx_sc; // required by the ManagedVecItem derive /// /// It is, however more optimized than that. Its implementation is based on `ManagedOption`. /// -/// EGLD a special, invalid token identifier handle. This way we can fit it inside a single i32 in memory. +/// EGLD is indicated by a special, invalid token identifier handle. +/// This way we can fit it inside a single i32 in memory. #[repr(transparent)] #[derive(ManagedVecItem, Clone)] pub struct EgldOrEsdtTokenIdentifier { @@ -56,12 +57,12 @@ impl EgldOrEsdtTokenIdentifier { } } - pub fn from_opt_raw_handle(opt_handle: Option) -> Self { - match opt_handle { - Some(handle) => Self::esdt(unsafe { TokenIdentifier::from_handle(handle) }), - None => Self::egld(), - } - } + // pub fn from_opt_raw_handle(opt_handle: Option) -> Self { + // match opt_handle { + // Some(handle) => Self::esdt(unsafe { TokenIdentifier::from_handle(handle) }), + // None => Self::egld(), + // } + // } pub fn parse(data: ManagedBuffer) -> Self { if data == Self::EGLD_REPRESENTATION { @@ -268,13 +269,15 @@ where M: ManagedTypeApi, { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - if let Some(token_identifier) = self.data.as_option() { - let token_id_str = token_identifier.to_string(); - f.debug_tuple("EgldOrEsdtTokenIdentifier::Esdt") - .field(&token_id_str) - .finish() - } else { - f.write_str("EgldOrEsdtTokenIdentifier::Egld") - } + self.map_ref_or_else( + f, + |f| f.write_str("EgldOrEsdtTokenIdentifier::Egld"), + |f, token_identifier| { + let token_id_str = token_identifier.to_string(); + f.debug_tuple("EgldOrEsdtTokenIdentifier::Esdt") + .field(&token_id_str) + .finish() + }, + ) } } diff --git a/framework/base/src/types/managed/wrapped/managed_option.rs b/framework/base/src/types/managed/wrapped/managed_option.rs index e433913835..a432cde82f 100644 --- a/framework/base/src/types/managed/wrapped/managed_option.rs +++ b/framework/base/src/types/managed/wrapped/managed_option.rs @@ -41,7 +41,9 @@ where } pub fn some(value: T) -> Self { - Self::new_with_handle(value.get_handle()) + let handle = value.get_handle(); + core::mem::forget(value); + Self::new_with_handle(handle) } pub fn none() -> Self { @@ -143,7 +145,10 @@ where F: FnOnce(Context, &T) -> R, { if self.is_some() { - f(context, unsafe { &T::from_handle(self.handle.clone()) }) + let obj = unsafe { T::from_handle(self.handle.clone()) }; + let result = f(context, &obj); + core::mem::forget(obj); + result } else { default(context) } @@ -157,11 +162,7 @@ where { #[allow(clippy::redundant_clone)] // the clone is not redundant fn clone(&self) -> Self { - if self.is_some() { - Self::some(unsafe { T::from_handle(self.handle.clone()) }.clone()) - } else { - Self::none() - } + self.map_ref_or_else((), |()| Self::none(), |(), obj| Self::some(obj.clone())) } } @@ -328,12 +329,10 @@ where T: ManagedType + core::fmt::Debug, { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - if self.is_some() { - f.debug_tuple("ManagedOption::Some") - .field(unsafe { &T::from_handle(self.handle.clone()) }) - .finish() - } else { - f.write_str("ManagedOption::None") - } + self.map_ref_or_else( + f, + |f| f.write_str("ManagedOption::None"), + |f, obj| f.debug_tuple("ManagedOption::Some").field(obj).finish(), + ) } } From a97945c4680c6e504519afdf86807fe5eb990665 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Tue, 12 Nov 2024 09:41:38 +0200 Subject: [PATCH 09/66] cleanup --- .../types/managed/wrapped/egld_or_esdt_token_identifier.rs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/framework/base/src/types/managed/wrapped/egld_or_esdt_token_identifier.rs b/framework/base/src/types/managed/wrapped/egld_or_esdt_token_identifier.rs index 7a4611011d..66dd7c9ec9 100644 --- a/framework/base/src/types/managed/wrapped/egld_or_esdt_token_identifier.rs +++ b/framework/base/src/types/managed/wrapped/egld_or_esdt_token_identifier.rs @@ -57,13 +57,6 @@ impl EgldOrEsdtTokenIdentifier { } } - // pub fn from_opt_raw_handle(opt_handle: Option) -> Self { - // match opt_handle { - // Some(handle) => Self::esdt(unsafe { TokenIdentifier::from_handle(handle) }), - // None => Self::egld(), - // } - // } - pub fn parse(data: ManagedBuffer) -> Self { if data == Self::EGLD_REPRESENTATION { Self::egld() From 9eac9fd1f9e300c25d39eb99684ece8678956552 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Fri, 15 Nov 2024 09:54:08 +0200 Subject: [PATCH 10/66] cargo fmt --- chain/vm/src/vm_hooks/vh_dispatcher.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chain/vm/src/vm_hooks/vh_dispatcher.rs b/chain/vm/src/vm_hooks/vh_dispatcher.rs index 46c5153819..1af9bfbb6a 100644 --- a/chain/vm/src/vm_hooks/vh_dispatcher.rs +++ b/chain/vm/src/vm_hooks/vh_dispatcher.rs @@ -1883,7 +1883,7 @@ impl VMHooks for VMHooksDispatcher { ) -> i32 { panic!("Unavailable: managed_multi_transfer_esdt_nft_execute_by_user") } - + fn managed_verify_secp256r1( &self, key_handle: i32, From f6d62d14df7a9fe2c9fb40c94aa8bd91027c2491 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Fri, 15 Nov 2024 09:56:07 +0200 Subject: [PATCH 11/66] using multiversx-chain-vm-executor v0.3.0 --- Cargo.lock | 5 +++-- chain/vm/Cargo.toml | 4 +--- framework/scenario/Cargo.toml | 4 +--- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 95a73a2974..25a922d7a1 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -2428,8 +2428,9 @@ dependencies = [ [[package]] name = "multiversx-chain-vm-executor" -version = "0.2.0" -source = "git+https://github.com/multiversx/mx-vm-executor-rs?branch=rc/v1.7.next1#009e15459c023ec3b94fcebd826b381f7706229c" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51cce7ae386960fbf5e85afe40ca16d63f926f0620ed1a36b019212b28e17219" [[package]] name = "multiversx-price-aggregator-sc" diff --git a/chain/vm/Cargo.toml b/chain/vm/Cargo.toml index e412a709af..96d8b25926 100644 --- a/chain/vm/Cargo.toml +++ b/chain/vm/Cargo.toml @@ -39,6 +39,4 @@ version = "=0.11.0" path = "../core" [dependencies.multiversx-chain-vm-executor] -# version = "0.2.0" -git = "https://github.com/multiversx/mx-vm-executor-rs" -branch = "rc/v1.7.next1" +version = "0.3.0" diff --git a/framework/scenario/Cargo.toml b/framework/scenario/Cargo.toml index c019233953..a0982451ca 100644 --- a/framework/scenario/Cargo.toml +++ b/framework/scenario/Cargo.toml @@ -50,9 +50,7 @@ version = "0.23.0" path = "../../sdk/scenario-format" [dependencies.multiversx-chain-vm-executor] -# version = "0.2.0" -git = "https://github.com/multiversx/mx-vm-executor-rs" -branch = "rc/v1.7.next1" +version = "0.3.0" [dependencies.multiversx-chain-vm] version = "=0.11.0" From 57a580b741c5e09f4e2c415c198882f61c1419e8 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Fri, 15 Nov 2024 10:06:45 +0200 Subject: [PATCH 12/66] EI version comment --- framework/meta-lib/src/ei/ei_version.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/meta-lib/src/ei/ei_version.rs b/framework/meta-lib/src/ei/ei_version.rs index 6ba2444a6e..33fe86e254 100644 --- a/framework/meta-lib/src/ei/ei_version.rs +++ b/framework/meta-lib/src/ei/ei_version.rs @@ -29,7 +29,7 @@ pub enum EIVersion { #[default] V1_3, - /// Version to be released to mainnet in August 2024. + /// Hooks made available in the Spica release, November 12, 2024: https://multiversx.com/release/release-spica-v1-8-4-0 V1_4, /// Version planned for Q3 2024. From 28d5b919273c1669e203e5273a998094948a9b21 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Mon, 18 Nov 2024 22:28:14 +0200 Subject: [PATCH 13/66] ManagedType - forget_into_handle; ManagedOption fix --- framework/base/src/storage/storage_key.rs | 4 ++++ .../src/types/interaction/managed_arg_buffer.rs | 4 ++++ .../base/src/types/managed/basic/big_float.rs | 11 +++++++++++ framework/base/src/types/managed/basic/big_int.rs | 11 +++++++++++ .../base/src/types/managed/basic/elliptic_curve.rs | 11 +++++++++++ .../base/src/types/managed/basic/managed_buffer.rs | 11 +++++++++++ .../base/src/types/managed/basic/managed_map.rs | 11 +++++++++++ .../base/src/types/managed/managed_type_trait.rs | 5 +++++ .../managed/multi_value/multi_value_managed_vec.rs | 4 ++++ .../base/src/types/managed/wrapped/big_uint.rs | 4 ++++ .../src/types/managed/wrapped/managed_address.rs | 4 ++++ .../types/managed/wrapped/managed_byte_array.rs | 4 ++++ .../src/types/managed/wrapped/managed_option.rs | 14 +++++++------- .../base/src/types/managed/wrapped/managed_vec.rs | 4 ++++ .../src/types/managed/wrapped/token_identifier.rs | 4 ++++ 15 files changed, 99 insertions(+), 7 deletions(-) diff --git a/framework/base/src/storage/storage_key.rs b/framework/base/src/storage/storage_key.rs index b3a35643e1..74b4fa5171 100644 --- a/framework/base/src/storage/storage_key.rs +++ b/framework/base/src/storage/storage_key.rs @@ -32,6 +32,10 @@ where self.buffer.get_handle() } + unsafe fn forget_into_handle(self) -> Self::OwnHandle { + self.buffer.forget_into_handle() + } + fn transmute_from_handle_ref(handle_ref: &A::ManagedBufferHandle) -> &Self { unsafe { core::mem::transmute(handle_ref) } } diff --git a/framework/base/src/types/interaction/managed_arg_buffer.rs b/framework/base/src/types/interaction/managed_arg_buffer.rs index 009ad74e3a..e1b09441fc 100644 --- a/framework/base/src/types/interaction/managed_arg_buffer.rs +++ b/framework/base/src/types/interaction/managed_arg_buffer.rs @@ -43,6 +43,10 @@ where self.data.get_handle() } + unsafe fn forget_into_handle(self) -> Self::OwnHandle { + self.data.forget_into_handle() + } + fn transmute_from_handle_ref(handle_ref: &M::ManagedBufferHandle) -> &Self { unsafe { core::mem::transmute(handle_ref) } } diff --git a/framework/base/src/types/managed/basic/big_float.rs b/framework/base/src/types/managed/basic/big_float.rs index 634d76f67d..81c5c25c53 100644 --- a/framework/base/src/types/managed/basic/big_float.rs +++ b/framework/base/src/types/managed/basic/big_float.rs @@ -35,6 +35,17 @@ impl ManagedType for BigFloat { self.handle.clone() } + unsafe fn forget_into_handle(mut self) -> Self::OwnHandle { + unsafe { + let handle = core::mem::replace( + &mut self.handle, + core::mem::MaybeUninit::uninit().assume_init(), + ); + core::mem::forget(self); + handle + } + } + fn transmute_from_handle_ref(handle_ref: &M::BigFloatHandle) -> &Self { unsafe { core::mem::transmute(handle_ref) } } diff --git a/framework/base/src/types/managed/basic/big_int.rs b/framework/base/src/types/managed/basic/big_int.rs index 13c3b965af..e981f6a9ec 100644 --- a/framework/base/src/types/managed/basic/big_int.rs +++ b/framework/base/src/types/managed/basic/big_int.rs @@ -38,6 +38,17 @@ impl ManagedType for BigInt { self.handle.clone() } + unsafe fn forget_into_handle(mut self) -> Self::OwnHandle { + unsafe { + let handle = core::mem::replace( + &mut self.handle, + core::mem::MaybeUninit::uninit().assume_init(), + ); + core::mem::forget(self); + handle + } + } + fn transmute_from_handle_ref(handle_ref: &M::BigIntHandle) -> &Self { unsafe { core::mem::transmute(handle_ref) } } diff --git a/framework/base/src/types/managed/basic/elliptic_curve.rs b/framework/base/src/types/managed/basic/elliptic_curve.rs index 2cc7097a18..7252bd8b90 100644 --- a/framework/base/src/types/managed/basic/elliptic_curve.rs +++ b/framework/base/src/types/managed/basic/elliptic_curve.rs @@ -43,6 +43,17 @@ impl ManagedType for EllipticCurve { self.handle.clone() } + unsafe fn forget_into_handle(mut self) -> Self::OwnHandle { + unsafe { + let handle = core::mem::replace( + &mut self.handle, + core::mem::MaybeUninit::uninit().assume_init(), + ); + core::mem::forget(self); + handle + } + } + fn transmute_from_handle_ref(handle_ref: &M::EllipticCurveHandle) -> &Self { unsafe { core::mem::transmute(handle_ref) } } diff --git a/framework/base/src/types/managed/basic/managed_buffer.rs b/framework/base/src/types/managed/basic/managed_buffer.rs index d1e0b52f9e..17ee49a639 100644 --- a/framework/base/src/types/managed/basic/managed_buffer.rs +++ b/framework/base/src/types/managed/basic/managed_buffer.rs @@ -37,6 +37,17 @@ impl ManagedType for ManagedBuffer { self.handle.clone() } + unsafe fn forget_into_handle(mut self) -> Self::OwnHandle { + unsafe { + let handle = core::mem::replace( + &mut self.handle, + core::mem::MaybeUninit::uninit().assume_init(), + ); + core::mem::forget(self); + handle + } + } + fn transmute_from_handle_ref(handle_ref: &M::ManagedBufferHandle) -> &Self { unsafe { core::mem::transmute(handle_ref) } } diff --git a/framework/base/src/types/managed/basic/managed_map.rs b/framework/base/src/types/managed/basic/managed_map.rs index 139f93cf9c..e0d593a883 100644 --- a/framework/base/src/types/managed/basic/managed_map.rs +++ b/framework/base/src/types/managed/basic/managed_map.rs @@ -23,6 +23,17 @@ impl ManagedType for ManagedMap { self.handle.clone() } + unsafe fn forget_into_handle(mut self) -> Self::OwnHandle { + unsafe { + let handle = core::mem::replace( + &mut self.handle, + core::mem::MaybeUninit::uninit().assume_init(), + ); + core::mem::forget(self); + handle + } + } + fn transmute_from_handle_ref(handle_ref: &M::ManagedMapHandle) -> &Self { unsafe { core::mem::transmute(handle_ref) } } diff --git a/framework/base/src/types/managed/managed_type_trait.rs b/framework/base/src/types/managed/managed_type_trait.rs index ca9210d594..f7df30288f 100644 --- a/framework/base/src/types/managed/managed_type_trait.rs +++ b/framework/base/src/types/managed/managed_type_trait.rs @@ -11,6 +11,11 @@ pub trait ManagedType: Sized { fn get_handle(&self) -> Self::OwnHandle; + /// Forgets current object (does not run destructor), but extracts the handle. + /// + /// The handle remains an owned object, so the handle's destructor will run later, when dropped. + unsafe fn forget_into_handle(self) -> Self::OwnHandle; + #[doc(hidden)] unsafe fn from_raw_handle(handle: RawHandle) -> Self { Self::from_handle(Self::OwnHandle::new(handle)) diff --git a/framework/base/src/types/managed/multi_value/multi_value_managed_vec.rs b/framework/base/src/types/managed/multi_value/multi_value_managed_vec.rs index 6ad36a25e8..feb99b561d 100644 --- a/framework/base/src/types/managed/multi_value/multi_value_managed_vec.rs +++ b/framework/base/src/types/managed/multi_value/multi_value_managed_vec.rs @@ -54,6 +54,10 @@ where self.0.get_handle() } + unsafe fn forget_into_handle(self) -> Self::OwnHandle { + self.0.forget_into_handle() + } + fn transmute_from_handle_ref(handle_ref: &M::ManagedBufferHandle) -> &Self { unsafe { core::mem::transmute(handle_ref) } } diff --git a/framework/base/src/types/managed/wrapped/big_uint.rs b/framework/base/src/types/managed/wrapped/big_uint.rs index 344842c2a3..8151110315 100644 --- a/framework/base/src/types/managed/wrapped/big_uint.rs +++ b/framework/base/src/types/managed/wrapped/big_uint.rs @@ -36,6 +36,10 @@ impl ManagedType for BigUint { self.value.handle.clone() } + unsafe fn forget_into_handle(self) -> Self::OwnHandle { + self.value.forget_into_handle() + } + fn transmute_from_handle_ref(handle_ref: &M::BigIntHandle) -> &Self { unsafe { core::mem::transmute(handle_ref) } } diff --git a/framework/base/src/types/managed/wrapped/managed_address.rs b/framework/base/src/types/managed/wrapped/managed_address.rs index a12200a039..ec3b338961 100644 --- a/framework/base/src/types/managed/wrapped/managed_address.rs +++ b/framework/base/src/types/managed/wrapped/managed_address.rs @@ -167,6 +167,10 @@ where self.bytes.get_handle() } + unsafe fn forget_into_handle(self) -> Self::OwnHandle { + self.bytes.forget_into_handle() + } + fn transmute_from_handle_ref(handle_ref: &M::ManagedBufferHandle) -> &Self { unsafe { core::mem::transmute(handle_ref) } } diff --git a/framework/base/src/types/managed/wrapped/managed_byte_array.rs b/framework/base/src/types/managed/wrapped/managed_byte_array.rs index 5ef3e661b7..76f292e7e8 100644 --- a/framework/base/src/types/managed/wrapped/managed_byte_array.rs +++ b/framework/base/src/types/managed/wrapped/managed_byte_array.rs @@ -45,6 +45,10 @@ where self.buffer.get_handle() } + unsafe fn forget_into_handle(self) -> Self::OwnHandle { + self.buffer.forget_into_handle() + } + fn transmute_from_handle_ref(handle_ref: &M::ManagedBufferHandle) -> &Self { unsafe { core::mem::transmute(handle_ref) } } diff --git a/framework/base/src/types/managed/wrapped/managed_option.rs b/framework/base/src/types/managed/wrapped/managed_option.rs index a432cde82f..767592363d 100644 --- a/framework/base/src/types/managed/wrapped/managed_option.rs +++ b/framework/base/src/types/managed/wrapped/managed_option.rs @@ -41,9 +41,7 @@ where } pub fn some(value: T) -> Self { - let handle = value.get_handle(); - core::mem::forget(value); - Self::new_with_handle(handle) + unsafe { Self::new_with_handle(value.forget_into_handle()) } } pub fn none() -> Self { @@ -145,10 +143,12 @@ where F: FnOnce(Context, &T) -> R, { if self.is_some() { - let obj = unsafe { T::from_handle(self.handle.clone()) }; - let result = f(context, &obj); - core::mem::forget(obj); - result + unsafe { + let obj = T::from_handle(self.handle.clone()); + let result = f(context, &obj); + let _ = obj.forget_into_handle(); + result + } } else { default(context) } diff --git a/framework/base/src/types/managed/wrapped/managed_vec.rs b/framework/base/src/types/managed/wrapped/managed_vec.rs index 582054e6ec..d3212e4d03 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec.rs @@ -56,6 +56,10 @@ where self.buffer.get_handle() } + unsafe fn forget_into_handle(self) -> Self::OwnHandle { + self.buffer.forget_into_handle() + } + fn transmute_from_handle_ref(handle_ref: &M::ManagedBufferHandle) -> &Self { unsafe { core::mem::transmute(handle_ref) } } diff --git a/framework/base/src/types/managed/wrapped/token_identifier.rs b/framework/base/src/types/managed/wrapped/token_identifier.rs index 10c92475a3..39fd17f5e1 100644 --- a/framework/base/src/types/managed/wrapped/token_identifier.rs +++ b/framework/base/src/types/managed/wrapped/token_identifier.rs @@ -36,6 +36,10 @@ impl ManagedType for TokenIdentifier { self.buffer.get_handle() } + unsafe fn forget_into_handle(self) -> Self::OwnHandle { + self.buffer.forget_into_handle() + } + fn transmute_from_handle_ref(handle_ref: &M::ManagedBufferHandle) -> &Self { unsafe { core::mem::transmute(handle_ref) } } From cc250e1c81b968f4f3ceb15b1b422a1f5f80e72f Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Mon, 18 Nov 2024 22:36:13 +0200 Subject: [PATCH 14/66] ManagedType - forget_into_handle in to ManagedVecItem impl for managed types --- .../base/src/types/managed/wrapped/managed_vec_item.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item.rs b/framework/base/src/types/managed/wrapped/managed_vec_item.rs index 8e88624d75..a883d991e3 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item.rs @@ -203,7 +203,8 @@ macro_rules! impl_managed_type { } fn into_byte_writer R>(self, writer: Writer) -> R { - <$ty as ManagedType>::OwnHandle::into_byte_writer(self.get_handle(), writer) + let handle = unsafe { self.forget_into_handle() }; + <$ty as ManagedType>::OwnHandle::into_byte_writer(handle, writer) } } }; @@ -266,7 +267,8 @@ where } fn into_byte_writer R>(self, writer: Writer) -> R { - ::into_byte_writer(self.get_handle(), writer) + let handle = unsafe { self.forget_into_handle() }; + ::into_byte_writer(handle, writer) } } From 835d656cf1669f5359cf63fc9ec6afc6d91d32ad Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Mon, 18 Nov 2024 22:45:45 +0200 Subject: [PATCH 15/66] cargo clippy & fmt --- framework/base/src/types/managed/basic/big_float.rs | 7 ++----- framework/base/src/types/managed/basic/big_int.rs | 7 ++----- .../base/src/types/managed/basic/elliptic_curve.rs | 7 ++----- .../base/src/types/managed/basic/managed_buffer.rs | 7 ++----- .../base/src/types/managed/basic/managed_map.rs | 7 ++----- .../base/src/types/managed/managed_type_trait.rs | 12 +++++++++++- 6 files changed, 21 insertions(+), 26 deletions(-) diff --git a/framework/base/src/types/managed/basic/big_float.rs b/framework/base/src/types/managed/basic/big_float.rs index 81c5c25c53..9306769130 100644 --- a/framework/base/src/types/managed/basic/big_float.rs +++ b/framework/base/src/types/managed/basic/big_float.rs @@ -35,12 +35,9 @@ impl ManagedType for BigFloat { self.handle.clone() } - unsafe fn forget_into_handle(mut self) -> Self::OwnHandle { + unsafe fn forget_into_handle(self) -> Self::OwnHandle { unsafe { - let handle = core::mem::replace( - &mut self.handle, - core::mem::MaybeUninit::uninit().assume_init(), - ); + let handle = core::ptr::read(&self.handle); core::mem::forget(self); handle } diff --git a/framework/base/src/types/managed/basic/big_int.rs b/framework/base/src/types/managed/basic/big_int.rs index e981f6a9ec..464bc4150c 100644 --- a/framework/base/src/types/managed/basic/big_int.rs +++ b/framework/base/src/types/managed/basic/big_int.rs @@ -38,12 +38,9 @@ impl ManagedType for BigInt { self.handle.clone() } - unsafe fn forget_into_handle(mut self) -> Self::OwnHandle { + unsafe fn forget_into_handle(self) -> Self::OwnHandle { unsafe { - let handle = core::mem::replace( - &mut self.handle, - core::mem::MaybeUninit::uninit().assume_init(), - ); + let handle = core::ptr::read(&self.handle); core::mem::forget(self); handle } diff --git a/framework/base/src/types/managed/basic/elliptic_curve.rs b/framework/base/src/types/managed/basic/elliptic_curve.rs index 7252bd8b90..9b86dba5d1 100644 --- a/framework/base/src/types/managed/basic/elliptic_curve.rs +++ b/framework/base/src/types/managed/basic/elliptic_curve.rs @@ -43,12 +43,9 @@ impl ManagedType for EllipticCurve { self.handle.clone() } - unsafe fn forget_into_handle(mut self) -> Self::OwnHandle { + unsafe fn forget_into_handle(self) -> Self::OwnHandle { unsafe { - let handle = core::mem::replace( - &mut self.handle, - core::mem::MaybeUninit::uninit().assume_init(), - ); + let handle = core::ptr::read(&self.handle); core::mem::forget(self); handle } diff --git a/framework/base/src/types/managed/basic/managed_buffer.rs b/framework/base/src/types/managed/basic/managed_buffer.rs index 17ee49a639..cafa32a4d2 100644 --- a/framework/base/src/types/managed/basic/managed_buffer.rs +++ b/framework/base/src/types/managed/basic/managed_buffer.rs @@ -37,12 +37,9 @@ impl ManagedType for ManagedBuffer { self.handle.clone() } - unsafe fn forget_into_handle(mut self) -> Self::OwnHandle { + unsafe fn forget_into_handle(self) -> Self::OwnHandle { unsafe { - let handle = core::mem::replace( - &mut self.handle, - core::mem::MaybeUninit::uninit().assume_init(), - ); + let handle = core::ptr::read(&self.handle); core::mem::forget(self); handle } diff --git a/framework/base/src/types/managed/basic/managed_map.rs b/framework/base/src/types/managed/basic/managed_map.rs index e0d593a883..2fce9deb76 100644 --- a/framework/base/src/types/managed/basic/managed_map.rs +++ b/framework/base/src/types/managed/basic/managed_map.rs @@ -23,12 +23,9 @@ impl ManagedType for ManagedMap { self.handle.clone() } - unsafe fn forget_into_handle(mut self) -> Self::OwnHandle { + unsafe fn forget_into_handle(self) -> Self::OwnHandle { unsafe { - let handle = core::mem::replace( - &mut self.handle, - core::mem::MaybeUninit::uninit().assume_init(), - ); + let handle = core::ptr::read(&self.handle); core::mem::forget(self); handle } diff --git a/framework/base/src/types/managed/managed_type_trait.rs b/framework/base/src/types/managed/managed_type_trait.rs index f7df30288f..8b30fb8400 100644 --- a/framework/base/src/types/managed/managed_type_trait.rs +++ b/framework/base/src/types/managed/managed_type_trait.rs @@ -12,8 +12,18 @@ pub trait ManagedType: Sized { fn get_handle(&self) -> Self::OwnHandle; /// Forgets current object (does not run destructor), but extracts the handle. - /// + /// /// The handle remains an owned object, so the handle's destructor will run later, when dropped. + /// + /// ## Safety + /// + /// Destructures the object, without running a constructor. + /// + /// To avoid a memory leak, it is necessary for the object to be later + /// reconstructed from handle and its destructor run. + /// + /// It is designed to be used ManagedVec and ManagedOption, + /// where items are dropped later, together with their container. unsafe fn forget_into_handle(self) -> Self::OwnHandle; #[doc(hidden)] From 04e59ef11fd4ab1991bab0dfb0484e094b1ebce8 Mon Sep 17 00:00:00 2001 From: Mihai Calin Luca Date: Wed, 20 Nov 2024 17:10:15 +0100 Subject: [PATCH 16/66] set state initial implementation for chain simulator, test in adder --- .../adder/interactor/src/basic_interactor.rs | 2 +- .../tests/basic_interactor_cs_test.rs | 28 ++++++ .../interactor/interactor_chain_simulator.rs | 13 ++- .../src/interactor/interactor_sender.rs | 19 +++- sdk/core/src/gateway.rs | 3 + .../gateway_chain_simulator_set_state.rs | 94 +++++++++++++++++++ 6 files changed, 156 insertions(+), 3 deletions(-) create mode 100644 sdk/core/src/gateway/gateway_chain_simulator_set_state.rs diff --git a/contracts/examples/adder/interactor/src/basic_interactor.rs b/contracts/examples/adder/interactor/src/basic_interactor.rs index 63d71de11a..64dff9673e 100644 --- a/contracts/examples/adder/interactor/src/basic_interactor.rs +++ b/contracts/examples/adder/interactor/src/basic_interactor.rs @@ -57,7 +57,7 @@ impl AdderInteract { let adder_owner_address = interactor.register_wallet(test_wallets::heidi()).await; let wallet_address = interactor.register_wallet(test_wallets::ivan()).await; - interactor.generate_blocks_until_epoch(1).await.unwrap(); + let _ = interactor.generate_blocks(30u64).await; AdderInteract { interactor, diff --git a/contracts/examples/adder/interactor/tests/basic_interactor_cs_test.rs b/contracts/examples/adder/interactor/tests/basic_interactor_cs_test.rs index 1f07ee4c87..d6b98b73e5 100644 --- a/contracts/examples/adder/interactor/tests/basic_interactor_cs_test.rs +++ b/contracts/examples/adder/interactor/tests/basic_interactor_cs_test.rs @@ -1,4 +1,5 @@ use basic_interactor::{AdderInteract, Config}; +use multiversx_sc_snippets::{sdk::gateway::SetStateAccount, test_wallets}; #[tokio::test] #[cfg_attr(not(feature = "chain-simulator-tests"), ignore)] @@ -33,3 +34,30 @@ async fn simulator_upgrade_test() { let sum = basic_interact.get_sum().await; assert_eq!(sum, 7u32.into()); } + +#[tokio::test] +#[cfg_attr(not(feature = "chain-simulator-tests"), ignore)] +async fn set_state_cs_test() { + let account_address = test_wallets::mike(); + + let real_chain_interact = AdderInteract::new(Config::load_config()).await; + let simulator_interact = AdderInteract::new(Config::chain_simulator_config()).await; + + let account = real_chain_interact + .interactor + .get_account(&account_address.to_address()) + .await; + let keys = real_chain_interact + .interactor + .get_account_storage(&account_address.to_address()) + .await; + + let set_state_account = SetStateAccount::from(account).with_keys(keys); + let vec_state = vec![set_state_account]; + + let set_state_response = simulator_interact.interactor.set_state(vec_state).await; + + let _ = simulator_interact.interactor.generate_blocks(2u64).await; + + assert!(set_state_response.is_ok()); +} diff --git a/framework/snippets/src/interactor/interactor_chain_simulator.rs b/framework/snippets/src/interactor/interactor_chain_simulator.rs index 44943fe243..24dfa5390e 100644 --- a/framework/snippets/src/interactor/interactor_chain_simulator.rs +++ b/framework/snippets/src/interactor/interactor_chain_simulator.rs @@ -1,7 +1,8 @@ use anyhow::Error; use multiversx_sc_scenario::imports::Address; use multiversx_sdk::gateway::{ - ChainSimulatorGenerateBlocksRequest, ChainSimulatorSendFundsRequest, GatewayAsyncService, + ChainSimulatorGenerateBlocksRequest, ChainSimulatorSendFundsRequest, + ChainSimulatorSetStateRequest, GatewayAsyncService, SetStateAccount, }; use crate::InteractorBase; @@ -53,4 +54,14 @@ where )) .await } + + pub async fn set_state(&self, account: Vec) -> Result { + if !self.use_chain_simulator { + return Ok(String::from("no-simulator")); + } + + self.proxy + .request(ChainSimulatorSetStateRequest::for_account(account)) + .await + } } diff --git a/framework/snippets/src/interactor/interactor_sender.rs b/framework/snippets/src/interactor/interactor_sender.rs index e3877dec0a..4ba0efa162 100644 --- a/framework/snippets/src/interactor/interactor_sender.rs +++ b/framework/snippets/src/interactor/interactor_sender.rs @@ -1,7 +1,10 @@ +use std::collections::HashMap; + use crate::sdk::{data::transaction::Transaction, wallet::Wallet}; use log::debug; use multiversx_sc_scenario::multiversx_sc::types::Address; -use multiversx_sdk::gateway::{GatewayAsyncService, GetAccountRequest}; +use multiversx_sdk::data::account::Account; +use multiversx_sdk::gateway::{GatewayAsyncService, GetAccountRequest, GetAccountStorageRequest}; use crate::InteractorBase; @@ -25,6 +28,20 @@ where account.nonce } + pub async fn get_account(&self, address: &Address) -> Account { + self.proxy + .request(GetAccountRequest::new(address)) + .await + .expect("failed to retrieve account") + } + + pub async fn get_account_storage(&self, address: &Address) -> HashMap { + self.proxy + .request(GetAccountStorageRequest::new(address)) + .await + .expect("failed to retrieve account") + } + pub(crate) async fn set_nonce_and_sign_tx( &mut self, sender_address: &Address, diff --git a/sdk/core/src/gateway.rs b/sdk/core/src/gateway.rs index 1512717a85..f3e08a4af2 100644 --- a/sdk/core/src/gateway.rs +++ b/sdk/core/src/gateway.rs @@ -5,6 +5,7 @@ mod gateway_account_storage; mod gateway_block; mod gateway_chain_simulator_blocks; mod gateway_chain_simulator_send_funds; +mod gateway_chain_simulator_set_state; mod gateway_network_config; mod gateway_network_economics; mod gateway_network_status; @@ -23,6 +24,7 @@ pub use gateway_account_storage::GetAccountStorageRequest; pub use gateway_block::GetHyperBlockRequest; pub use gateway_chain_simulator_blocks::ChainSimulatorGenerateBlocksRequest; pub use gateway_chain_simulator_send_funds::ChainSimulatorSendFundsRequest; +pub use gateway_chain_simulator_set_state::{ChainSimulatorSetStateRequest, SetStateAccount}; pub use gateway_network_config::NetworkConfigRequest; pub use gateway_network_economics::NetworkEconimicsRequest; pub use gateway_network_status::NetworkStatusRequest; @@ -61,6 +63,7 @@ const GENERATE_BLOCKS_UNTIL_TX_PROCESSED_ENDPOINT: &str = "simulator/generate-blocks-until-transaction-processed"; const GENERATE_BLOCKS_UNTIL_EPOCH_REACHED_ENDPOINT: &str = "simulator/generate-blocks-until-epoch-reached"; +const SET_STATE_ENDPOINT: &str = "simulator/set-state"; pub enum GatewayRequestType { Get, diff --git a/sdk/core/src/gateway/gateway_chain_simulator_set_state.rs b/sdk/core/src/gateway/gateway_chain_simulator_set_state.rs new file mode 100644 index 0000000000..33644e72a3 --- /dev/null +++ b/sdk/core/src/gateway/gateway_chain_simulator_set_state.rs @@ -0,0 +1,94 @@ +use anyhow::anyhow; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; + +use crate::data::account::Account; + +use super::{GatewayRequest, GatewayRequestType, SET_STATE_ENDPOINT}; + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct SetStateResponse { + pub data: serde_json::Value, + pub error: String, + pub code: String, +} + +#[derive(Debug, Clone, Serialize, Deserialize, Default)] +pub struct SetStateAccount { + pub address: String, + pub nonce: u64, + pub balance: String, + pub keys: HashMap, + pub code: String, + #[serde(default)] + pub code_hash: String, + #[serde(default)] + pub root_hash: String, + #[serde(default)] + pub code_metadata: String, + #[serde(default)] + pub owner_address: String, + #[serde(default)] + pub developer_reward: String, +} + +impl From for SetStateAccount { + fn from(value: Account) -> Self { + Self { + address: value.address.to_bech32_string().unwrap_or_default(), + nonce: value.nonce, + balance: value.balance.to_string(), + keys: HashMap::new(), + code: value.code, + code_hash: value.code_hash.unwrap_or_default(), + root_hash: value.root_hash.unwrap_or_default(), + code_metadata: value.code_metadata.unwrap_or_default(), + owner_address: value.owner_address.unwrap_or_default(), + developer_reward: value.developer_reward.unwrap_or_default(), + } + } +} + +impl SetStateAccount { + pub fn with_keys(mut self, keys: HashMap) -> Self { + self.keys = keys; + + self + } +} + +/// Sets state for a list of accounts using the chain simulator API. +pub struct ChainSimulatorSetStateRequest { + pub account: Vec, +} + +impl ChainSimulatorSetStateRequest { + pub fn for_account(account: Vec) -> Self { + Self { account } + } +} + +impl GatewayRequest for ChainSimulatorSetStateRequest { + type Payload = Vec; + type DecodedJson = SetStateResponse; + type Result = String; + + fn get_payload(&self) -> Option<&Self::Payload> { + Some(&self.account) + } + + fn request_type(&self) -> GatewayRequestType { + GatewayRequestType::Post + } + + fn get_endpoint(&self) -> String { + SET_STATE_ENDPOINT.to_owned() + } + + fn process_json(&self, decoded: Self::DecodedJson) -> anyhow::Result { + match decoded.code.as_str() { + "successful" => Ok(decoded.code), + _ => Err(anyhow!("{}", decoded.error)), + } + } +} From 445d7bcd06f06f6869687b4b52488d335bd5cd31 Mon Sep 17 00:00:00 2001 From: Mihai Calin Luca Date: Thu, 21 Nov 2024 13:42:04 +0100 Subject: [PATCH 17/66] typos --- .../snippets/src/interactor/interactor_chain_simulator.rs | 4 ++-- sdk/core/src/gateway/gateway_chain_simulator_set_state.rs | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/framework/snippets/src/interactor/interactor_chain_simulator.rs b/framework/snippets/src/interactor/interactor_chain_simulator.rs index 24dfa5390e..17ca51f3a7 100644 --- a/framework/snippets/src/interactor/interactor_chain_simulator.rs +++ b/framework/snippets/src/interactor/interactor_chain_simulator.rs @@ -55,13 +55,13 @@ where .await } - pub async fn set_state(&self, account: Vec) -> Result { + pub async fn set_state(&self, accounts: Vec) -> Result { if !self.use_chain_simulator { return Ok(String::from("no-simulator")); } self.proxy - .request(ChainSimulatorSetStateRequest::for_account(account)) + .request(ChainSimulatorSetStateRequest::for_accounts(accounts)) .await } } diff --git a/sdk/core/src/gateway/gateway_chain_simulator_set_state.rs b/sdk/core/src/gateway/gateway_chain_simulator_set_state.rs index 33644e72a3..77b46d4422 100644 --- a/sdk/core/src/gateway/gateway_chain_simulator_set_state.rs +++ b/sdk/core/src/gateway/gateway_chain_simulator_set_state.rs @@ -59,12 +59,12 @@ impl SetStateAccount { /// Sets state for a list of accounts using the chain simulator API. pub struct ChainSimulatorSetStateRequest { - pub account: Vec, + pub accounts: Vec, } impl ChainSimulatorSetStateRequest { - pub fn for_account(account: Vec) -> Self { - Self { account } + pub fn for_accounts(accounts: Vec) -> Self { + Self { accounts } } } @@ -74,7 +74,7 @@ impl GatewayRequest for ChainSimulatorSetStateRequest { type Result = String; fn get_payload(&self) -> Option<&Self::Payload> { - Some(&self.account) + Some(&self.accounts) } fn request_type(&self) -> GatewayRequestType { From d829a3d8be070c64a43c3ebf70ec50c3ca2fd733 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Mon, 25 Nov 2024 15:15:55 +0200 Subject: [PATCH 18/66] ManagedVecItem payload refactor - several attempts --- .../esdt_token_payment_multi_value.rs | 5 +- .../managed/wrapped/esdt_token_payment.rs | 4 + .../types/managed/wrapped/managed_option.rs | 4 + .../types/managed/wrapped/managed_vec_item.rs | 56 ++++++- .../wrapped/managed_vec_item_nested_tuple.rs | 63 +++++++ .../wrapped/managed_vec_item_payload.rs | 155 +++++++++++++++++- .../base/src/types/managed/wrapped/mod.rs | 2 +- .../derive/src/managed_vec_item_derive.rs | 8 + .../src/api/impl_vh/debug_handle_vh.rs | 4 + 9 files changed, 295 insertions(+), 6 deletions(-) diff --git a/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs b/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs index 1ebc5d5fe7..4eb6fc60d8 100644 --- a/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs +++ b/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs @@ -44,11 +44,14 @@ impl ManagedVecItem for EsdtTokenPaymentMultiValue { const SKIPS_RESERIALIZATION: bool = EsdtTokenPayment::::SKIPS_RESERIALIZATION; type Ref<'a> = Self; - #[inline] fn from_byte_reader(reader: Reader) -> Self { EsdtTokenPayment::from_byte_reader(reader).into() } + fn read_from_payload(payload: &Self::PAYLOAD) -> Self { + EsdtTokenPayment::read_from_payload(payload).into() + } + #[inline] unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( reader: Reader, diff --git a/framework/base/src/types/managed/wrapped/esdt_token_payment.rs b/framework/base/src/types/managed/wrapped/esdt_token_payment.rs index cf2ccf0ceb..a92a9cbe16 100644 --- a/framework/base/src/types/managed/wrapped/esdt_token_payment.rs +++ b/framework/base/src/types/managed/wrapped/esdt_token_payment.rs @@ -205,6 +205,10 @@ impl ManagedVecItem for EsdtTokenPayment { } } + fn read_from_payload(payload: &Self::PAYLOAD) -> Self { + todo!() + } + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( reader: Reader, ) -> Self::Ref<'a> { diff --git a/framework/base/src/types/managed/wrapped/managed_option.rs b/framework/base/src/types/managed/wrapped/managed_option.rs index 767592363d..ae77c6bc92 100644 --- a/framework/base/src/types/managed/wrapped/managed_option.rs +++ b/framework/base/src/types/managed/wrapped/managed_option.rs @@ -208,6 +208,10 @@ where Self::new_with_handle(handle) } + fn read_from_payload(payload: &Self::PAYLOAD) -> Self { + Self::new_with_handle(use_raw_handle(i32::read_from_payload(payload))) + } + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( reader: Reader, ) -> Self::Ref<'a> { diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item.rs b/framework/base/src/types/managed/wrapped/managed_vec_item.rs index a883d991e3..8c06da18c3 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item.rs @@ -3,14 +3,14 @@ use core::borrow::Borrow; use multiversx_chain_core::types::{EsdtLocalRole, EsdtTokenType}; use crate::{ - api::ManagedTypeApi, + api::{use_raw_handle, ManagedTypeApi}, types::{ BigInt, BigUint, EllipticCurve, ManagedAddress, ManagedBuffer, ManagedByteArray, ManagedRef, ManagedType, ManagedVec, TokenIdentifier, }, }; -use super::{ManagedVecItemNestedTuple, ManagedVecItemPayload, ManagedVecItemPayloadBuffer}; +use super::{ManagedVecItemNestedTuple, ManagedVecItemNestedTupleSplit, ManagedVecItemPayload, ManagedVecItemPayloadBuffer}; /// Types that implement this trait can be items inside a `ManagedVec`. /// All these types need a payload, i.e a representation that gets stored @@ -44,6 +44,8 @@ pub trait ManagedVecItem: 'static { /// Parses given bytes as a an owned object. fn from_byte_reader(reader: Reader) -> Self; + fn read_from_payload(payload: &Self::PAYLOAD) -> Self; + /// Parses given bytes as a representation of the object, either owned, or a reference. /// /// # Safety @@ -71,11 +73,17 @@ macro_rules! impl_int { type PAYLOAD = ManagedVecItemPayloadBuffer<$payload_size>; const SKIPS_RESERIALIZATION: bool = true; type Ref<'a> = Self; + fn from_byte_reader(mut reader: Reader) -> Self { let mut arr: [u8; $payload_size] = [0u8; $payload_size]; reader(&mut arr[..]); $ty::from_be_bytes(arr) } + + fn read_from_payload(payload: &Self::PAYLOAD) -> Self { + $ty::from_be_bytes(payload.buffer) + } + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( reader: Reader, ) -> Self::Ref<'a> { @@ -107,6 +115,10 @@ impl ManagedVecItem for usize { u32::from_be_bytes(arr) as usize } + fn read_from_payload(payload: &Self::PAYLOAD) -> Self { + u32::read_from_payload(payload) as usize + } + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( reader: Reader, ) -> Self::Ref<'a> { @@ -128,6 +140,10 @@ impl ManagedVecItem for bool { u8::from_byte_reader(reader) > 0 } + fn read_from_payload(payload: &Self::PAYLOAD) -> Self { + u8::read_from_payload(payload) > 0 + } + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( reader: Reader, ) -> Self::Ref<'a> { @@ -164,6 +180,17 @@ where } } + fn read_from_payload(payload: &Self::PAYLOAD) -> Self { + // let (p1, (p2, ())) = <(u8, (T, ()))>::split_all(payload); + + // let disc = u8::read_from_payload(p1); + // let x = split_payload(payload); + // Self::PAYLOAD::spli + todo!() + // let (disc_payload, t_payload) = payload.split::<1>(); + // let x = payload; + } + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( reader: Reader, ) -> Self::Ref<'a> { @@ -195,6 +222,11 @@ macro_rules! impl_managed_type { unsafe { $ty::from_handle(handle) } } + fn read_from_payload(payload: &Self::PAYLOAD) -> Self { + let handle = use_raw_handle(i32::read_from_payload(payload)); + unsafe { Self::from_handle(handle) } + } + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( reader: Reader, ) -> Self::Ref<'a> { @@ -230,6 +262,11 @@ where unsafe { Self::from_handle(handle) } } + fn read_from_payload(payload: &Self::PAYLOAD) -> Self { + let handle = use_raw_handle(i32::read_from_payload(payload)); + unsafe { Self::from_handle(handle) } + } + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( reader: Reader, ) -> Self::Ref<'a> { @@ -259,6 +296,11 @@ where unsafe { Self::from_handle(handle) } } + fn read_from_payload(payload: &Self::PAYLOAD) -> Self { + let handle = use_raw_handle(i32::read_from_payload(payload)); + unsafe { Self::from_handle(handle) } + } + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( reader: Reader, ) -> Self::Ref<'a> { @@ -283,6 +325,10 @@ impl ManagedVecItem for EsdtTokenType { arr[0].into() } + fn read_from_payload(payload: &Self::PAYLOAD) -> Self { + u8::read_from_payload(payload).into() + } + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( reader: Reader, ) -> Self::Ref<'a> { @@ -295,7 +341,7 @@ impl ManagedVecItem for EsdtTokenType { } impl ManagedVecItem for EsdtLocalRole { - type PAYLOAD = ManagedVecItemPayloadBuffer<1>; + type PAYLOAD = ManagedVecItemPayloadBuffer<2>; const SKIPS_RESERIALIZATION: bool = false; // TODO: might be ok to be true, but needs testing type Ref<'a> = Self; @@ -303,6 +349,10 @@ impl ManagedVecItem for EsdtLocalRole { u16::from_byte_reader(reader).into() } + fn read_from_payload(payload: &Self::PAYLOAD) -> Self { + u16::read_from_payload(payload).into() + } + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( reader: Reader, ) -> Self::Ref<'a> { diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item_nested_tuple.rs b/framework/base/src/types/managed/wrapped/managed_vec_item_nested_tuple.rs index 3a25bf4993..45e6a8d820 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item_nested_tuple.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item_nested_tuple.rs @@ -5,11 +5,35 @@ use super::{ /// Syntactic sugar, that allows us to more easily represent composite payloads as nested tuples. pub trait ManagedVecItemNestedTuple { type PAYLOAD: ManagedVecItemPayload; + type Split1: ManagedVecItemPayload; + type Split2: ManagedVecItemPayload; + + fn split_payload(payload: &Self::PAYLOAD) -> (&Self::Split1, &Self::Split2); +} + +pub trait ManagedVecItemNestedTupleSplit<'a>: ManagedVecItemNestedTuple { + type S; + + fn split_all(payload: &'a Self::PAYLOAD) -> Self::S; } /// End of the list. impl ManagedVecItemNestedTuple for () { type PAYLOAD = ManagedVecItemEmptyPayload; + type Split1 = ManagedVecItemEmptyPayload; + type Split2 = ManagedVecItemEmptyPayload; + + fn split_payload(_payload: &Self::PAYLOAD) -> (&Self::Split1, &Self::Split2) { + (&ManagedVecItemEmptyPayload, &ManagedVecItemEmptyPayload) + } +} + +impl<'a> ManagedVecItemNestedTupleSplit<'a> for () { + type S = (); + + fn split_all(_payload: &'a Self::PAYLOAD) -> Self::S { + () + } } impl ManagedVecItemNestedTuple for (Head, Tail) @@ -19,8 +43,47 @@ where Head::PAYLOAD: ManagedVecItemPayloadAdd, { type PAYLOAD = >::Output; + type Split1 = ::PAYLOAD; + type Split2 = ::PAYLOAD; + + fn split_payload(payload: &Self::PAYLOAD) -> (&Self::Split1, &Self::Split2) { + Head::PAYLOAD::split_from_add(payload) + } +} + +impl<'a, Head, Tail> ManagedVecItemNestedTupleSplit<'a> for (Head, Tail) +where + Head: ManagedVecItem, + Tail: ManagedVecItemNestedTupleSplit<'a>, + Head::PAYLOAD: ManagedVecItemPayloadAdd, + Tail::PAYLOAD: 'a, +{ + type S = (&'a Head::PAYLOAD, Tail::S); + + fn split_all(payload: &'a Self::PAYLOAD) -> Self::S { + let (hp, tp) = Head::PAYLOAD::split_from_add(payload); + (hp, Tail::split_all(tp)) + } } +// pub fn split_payload( +// payload: &<(Head, Tail) as ManagedVecItemNestedTuple>::PAYLOAD, +// ) -> (&Head::PAYLOAD, &Tail::PAYLOAD) +// where +// Head: ManagedVecItem, +// Tail: ManagedVecItemNestedTuple, +// Head::PAYLOAD: ManagedVecItemPayloadAdd, +// // (Head, Tail): ManagedVecItemNestedTuple, +// { +// >::split_from_add(payload) +// // <(Head, Tail) as ManagedVecItemNestedTuple>::PAYLOAD as +// // unsafe { +// // let ptr1 = payload.buffer.as_ptr(); +// // let ptr2 = ptr1.offset($dec1 as isize); +// // (core::mem::transmute(ptr1), core::mem::transmute(ptr2)) +// // } +// } + #[cfg(test)] pub mod tests { use super::*; diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs b/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs index 07adc31a08..4fcc20d9d3 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs @@ -1,3 +1,5 @@ +use core::marker::PhantomData; + /// Describes the binary represetnation of a ManagedVecItem. /// /// It is always an array that can be allocated directly on stack. @@ -35,8 +37,9 @@ impl ManagedVecItemPayload for ManagedVecItemEmptyPayload { } /// The main ManagedVecItemPayload implementation. Uses an array in its implementation. +#[repr(transparent)] pub struct ManagedVecItemPayloadBuffer { - buffer: [u8; N], + pub buffer: [u8; N], } impl ManagedVecItemPayload for ManagedVecItemPayloadBuffer { @@ -57,6 +60,83 @@ impl ManagedVecItemPayload for ManagedVecItemPayloadBuffer { } } +#[repr(transparent)] +pub struct ManagedVecItemPayloadConcat +where + P1: ManagedVecItemPayload, + P2: ManagedVecItemPayload, + P1: ManagedVecItemPayloadAdd, +{ + _phantom_1: PhantomData, + _phantom_2: PhantomData, + buffer: >::Output, +} + +impl ManagedVecItemPayload for ManagedVecItemPayloadConcat +where + P1: ManagedVecItemPayload, + P2: ManagedVecItemPayload, + P1: ManagedVecItemPayloadAdd, +{ + fn new_buffer() -> Self { + ManagedVecItemPayloadConcat { + _phantom_1: PhantomData, + _phantom_2: PhantomData, + buffer: ManagedVecItemPayload::new_buffer(), + } + } + + fn payload_size() -> usize { + >::Output::payload_size() + } + + fn payload_slice(&self) -> &[u8] { + self.buffer.payload_slice() + } + + fn payload_slice_mut(&mut self) -> &mut [u8] { + self.buffer.payload_slice_mut() + } +} + +impl ManagedVecItemPayloadConcat +where + P1: ManagedVecItemPayload, + P2: ManagedVecItemPayload, + P1: ManagedVecItemPayloadAdd, +{ +} + +impl ManagedVecItemPayloadAdd> for P0 +where + P0: ManagedVecItemPayload, + P1: ManagedVecItemPayload, + P2: ManagedVecItemPayload, + P1: ManagedVecItemPayloadAdd, + P0: ManagedVecItemPayloadAdd<>::Output>, +{ + type Output = ManagedVecItemPayloadConcat>; + + fn split_from_add(payload: &Self::Output) -> (&Self, &ManagedVecItemPayloadConcat) { + todo!() + } + + + + // fn split_from_add( + // payload: &ManagedVecItemPayloadBuffer<$result_add>, + // ) -> ( + // &ManagedVecItemPayloadBuffer<$dec1>, + // &ManagedVecItemPayloadBuffer<$dec2>, + // ) { + // unsafe { + // let ptr1 = payload.buffer.as_ptr(); + // let ptr2 = ptr1.offset($dec1 as isize); + // (core::mem::transmute(ptr1), core::mem::transmute(ptr2)) + // } + // } +} + /// Describes concatantion of smaller payloads into a larger one. /// /// There is no runtime implementation, just a type-level addition. @@ -67,12 +147,39 @@ where Rhs: ManagedVecItemPayload, { type Output: ManagedVecItemPayload; + + fn split_from_add(payload: &Self::Output) -> (&Self, &Rhs); } impl ManagedVecItemPayloadAdd for ManagedVecItemPayloadBuffer { type Output = Self; + + fn split_from_add(payload: &Self::Output) -> (&Self, &ManagedVecItemEmptyPayload) { + (payload, &ManagedVecItemEmptyPayload) + } +} + +pub trait ManagedVecItemPayloadSplit: ManagedVecItemPayload { + type P1: ManagedVecItemPayload; + type P2: ManagedVecItemPayload; + + fn split_impl(&self) -> (&Self::P1, &Self::P2); +} + +impl ManagedVecItemPayloadBuffer { + pub fn split_me( + &self, + ) -> ( + &>::P1, + &>::P2, + ) + where + Self: ManagedVecItemPayloadSplit, + { + ManagedVecItemPayloadSplit::::split_impl(self) + } } /// Replaces a const generic expression. @@ -84,10 +191,56 @@ macro_rules! payload_add { for ManagedVecItemPayloadBuffer<$dec1> { type Output = ManagedVecItemPayloadBuffer<$result_add>; + + fn split_from_add( + payload: &ManagedVecItemPayloadBuffer<$result_add>, + ) -> ( + &ManagedVecItemPayloadBuffer<$dec1>, + &ManagedVecItemPayloadBuffer<$dec2>, + ) { + unsafe { + let ptr1 = payload.buffer.as_ptr(); + let ptr2 = ptr1.offset($dec1 as isize); + (core::mem::transmute(ptr1), core::mem::transmute(ptr2)) + } + } + } + + impl ManagedVecItemPayloadSplit<$dec1> for ManagedVecItemPayloadBuffer<$result_add> { + type P1 = ManagedVecItemPayloadBuffer<$dec1>; + type P2 = ManagedVecItemPayloadBuffer<$dec2>; + + fn split_impl( + &self, + ) -> ( + &ManagedVecItemPayloadBuffer<$dec1>, + &ManagedVecItemPayloadBuffer<$dec2>, + ) { + unsafe { + let ptr1 = self.buffer.as_ptr(); + let ptr2 = ptr1.offset($dec1 as isize); + (core::mem::transmute(ptr1), core::mem::transmute(ptr2)) + } + } } }; } +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn payload_split() { + let payload = ManagedVecItemPayloadBuffer { + buffer: [1, 2, 3, 4, 5], + }; + let (p1, p2) = payload.split_me::<3>(); + assert_eq!(p1.buffer, [1, 2, 3]); + assert_eq!(p2.buffer, [4, 5]); + } +} + payload_add!(1usize, 1usize, 2usize); payload_add!(1usize, 2usize, 3usize); payload_add!(1usize, 3usize, 4usize); diff --git a/framework/base/src/types/managed/wrapped/mod.rs b/framework/base/src/types/managed/wrapped/mod.rs index 37849e0b6c..f4db4e5212 100644 --- a/framework/base/src/types/managed/wrapped/mod.rs +++ b/framework/base/src/types/managed/wrapped/mod.rs @@ -47,7 +47,7 @@ pub use managed_ref::ManagedRef; pub use managed_ref_mut::ManagedRefMut; pub use managed_vec::ManagedVec; pub use managed_vec_item::ManagedVecItem; -pub use managed_vec_item_nested_tuple::ManagedVecItemNestedTuple; +pub use managed_vec_item_nested_tuple::{ManagedVecItemNestedTuple, ManagedVecItemNestedTupleSplit}; pub use managed_vec_item_payload::*; pub use managed_vec_owned_iter::ManagedVecOwnedIterator; pub use managed_vec_ref::ManagedVecRef; diff --git a/framework/derive/src/managed_vec_item_derive.rs b/framework/derive/src/managed_vec_item_derive.rs index 17c07f21a4..c0525ee0d5 100644 --- a/framework/derive/src/managed_vec_item_derive.rs +++ b/framework/derive/src/managed_vec_item_derive.rs @@ -133,6 +133,10 @@ fn enum_derive(data_enum: &syn::DataEnum, ast: &syn::DeriveInput) -> TokenStream } } + fn read_from_payload(payload: &Self::PAYLOAD) -> Self { + todo!() + } + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>(reader: Reader) -> Self::Ref<'a> { Self::from_byte_reader(reader) } @@ -176,6 +180,10 @@ fn struct_derive(data_struct: &syn::DataStruct, ast: &syn::DeriveInput) -> Token } } + fn read_from_payload(payload: &Self::PAYLOAD) -> Self { + todo!() + } + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>(reader: Reader) -> Self::Ref<'a> { Self::from_byte_reader(reader) } diff --git a/framework/scenario/src/api/impl_vh/debug_handle_vh.rs b/framework/scenario/src/api/impl_vh/debug_handle_vh.rs index 99dc7c6e87..c60df36991 100644 --- a/framework/scenario/src/api/impl_vh/debug_handle_vh.rs +++ b/framework/scenario/src/api/impl_vh/debug_handle_vh.rs @@ -87,6 +87,10 @@ impl ManagedVecItem for DebugHandle { use_raw_handle(RawHandle::from_byte_reader(reader)) } + fn read_from_payload(payload: &Self::PAYLOAD) -> Self { + use_raw_handle(RawHandle::read_from_payload(payload)) + } + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( reader: Reader, ) -> Self::Ref<'a> { From 6a6ff5c8ed77b853db951e2fa1afe8ac3aea3fa6 Mon Sep 17 00:00:00 2001 From: BiancaIalangi Date: Tue, 26 Nov 2024 13:49:38 +0200 Subject: [PATCH 19/66] sync mandos with v0.54.4 --- .../alloc-features/scenarios/alloc_mem_fail.scen.json | 2 +- .../alloc-features/scenarios/alloc_mem_leaking.scen.json | 2 +- .../basic-features/scenarios/crypto_verify_bls.scen.json | 2 +- .../scenarios/crypto_verify_bls_aggregated_signature.scen.json | 2 +- .../basic-features/scenarios/crypto_verify_bls_share.scen.json | 2 +- .../basic-features/scenarios/crypto_verify_secp256r1.scen.json | 2 +- .../scenarios/forwarder_call_async_retrieve_egld.scen.json | 2 +- .../scenarios/promises_call_async_retrieve_egld.scen.json | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/contracts/feature-tests/alloc-features/scenarios/alloc_mem_fail.scen.json b/contracts/feature-tests/alloc-features/scenarios/alloc_mem_fail.scen.json index 5185e53638..c8f86f7292 100644 --- a/contracts/feature-tests/alloc-features/scenarios/alloc_mem_fail.scen.json +++ b/contracts/feature-tests/alloc-features/scenarios/alloc_mem_fail.scen.json @@ -7,7 +7,7 @@ "sc:alloc-mem-fail": { "nonce": "0", "balance": "0", - "code": "file:../output/alloc-mem-fail.wasm" + "code": "mxsc:../output/alloc-mem-fail.mxsc.json" }, "address:an_account": { "nonce": "0", diff --git a/contracts/feature-tests/alloc-features/scenarios/alloc_mem_leaking.scen.json b/contracts/feature-tests/alloc-features/scenarios/alloc_mem_leaking.scen.json index 0cae3b7303..95f33ac733 100644 --- a/contracts/feature-tests/alloc-features/scenarios/alloc_mem_leaking.scen.json +++ b/contracts/feature-tests/alloc-features/scenarios/alloc_mem_leaking.scen.json @@ -7,7 +7,7 @@ "sc:alloc-mem-leaking": { "nonce": "0", "balance": "0", - "code": "file:../output/alloc-mem-leaking.wasm" + "code": "mxsc:../output/alloc-mem-leaking.mxsc.json" }, "address:an_account": { "nonce": "0", diff --git a/contracts/feature-tests/basic-features/scenarios/crypto_verify_bls.scen.json b/contracts/feature-tests/basic-features/scenarios/crypto_verify_bls.scen.json index fc67a3745b..99d683d28d 100644 --- a/contracts/feature-tests/basic-features/scenarios/crypto_verify_bls.scen.json +++ b/contracts/feature-tests/basic-features/scenarios/crypto_verify_bls.scen.json @@ -58,7 +58,7 @@ "expect": { "out": [], "status": "10", - "message": "str:err blsSignatureDeserialize 0032a2ddf341c08d1eb7232f05dc34e4454155e676b58c40fddf9a036562ac2c01533d2d557cb49d73aa9d7a89744696", + "message": "str:signature is invalid", "logs": "*", "gas": "*", "refund": "*" diff --git a/contracts/feature-tests/basic-features/scenarios/crypto_verify_bls_aggregated_signature.scen.json b/contracts/feature-tests/basic-features/scenarios/crypto_verify_bls_aggregated_signature.scen.json index 546c0d4c9f..490fb1de8b 100644 --- a/contracts/feature-tests/basic-features/scenarios/crypto_verify_bls_aggregated_signature.scen.json +++ b/contracts/feature-tests/basic-features/scenarios/crypto_verify_bls_aggregated_signature.scen.json @@ -70,7 +70,7 @@ "expect": { "out": [], "status": "10", - "message": "str:err blsSignatureDeserialize 0012858363e8caa5b398d3febdd7bc01bc2fae1fef8f486ff4d84a5f3342f2d38085904eb10b73c0879a45d23585ce8f", + "message": "str:signature is invalid", "logs": "*", "gas": "*", "refund": "*" diff --git a/contracts/feature-tests/basic-features/scenarios/crypto_verify_bls_share.scen.json b/contracts/feature-tests/basic-features/scenarios/crypto_verify_bls_share.scen.json index fa7c21611b..2fe8473f52 100644 --- a/contracts/feature-tests/basic-features/scenarios/crypto_verify_bls_share.scen.json +++ b/contracts/feature-tests/basic-features/scenarios/crypto_verify_bls_share.scen.json @@ -58,7 +58,7 @@ "expect": { "out": [], "status": "10", - "message": "str:err blsSignatureDeserialize ff725db195e37aa237cdbbda76270d4a229b6e7a3651104dc58c4349c0388e8546976fe54a04240530b99064e434c90f", + "message": "str:signature is invalid", "logs": "*", "gas": "*", "refund": "*" diff --git a/contracts/feature-tests/basic-features/scenarios/crypto_verify_secp256r1.scen.json b/contracts/feature-tests/basic-features/scenarios/crypto_verify_secp256r1.scen.json index 1c35b46271..5d22d63d9c 100644 --- a/contracts/feature-tests/basic-features/scenarios/crypto_verify_secp256r1.scen.json +++ b/contracts/feature-tests/basic-features/scenarios/crypto_verify_secp256r1.scen.json @@ -57,7 +57,7 @@ "expect": { "out": [], "status": "10", - "message": "str:signature verification failed", + "message": "str:signature is invalid", "logs": "*", "gas": "*", "refund": "*" diff --git a/contracts/feature-tests/composability/scenarios/forwarder_call_async_retrieve_egld.scen.json b/contracts/feature-tests/composability/scenarios/forwarder_call_async_retrieve_egld.scen.json index f9091ab5a9..ecfae9b375 100644 --- a/contracts/feature-tests/composability/scenarios/forwarder_call_async_retrieve_egld.scen.json +++ b/contracts/feature-tests/composability/scenarios/forwarder_call_async_retrieve_egld.scen.json @@ -84,7 +84,7 @@ "address": "sc:vault", "endpoint": "str:transferValueOnly", "topics": [ - "1000", + "", "sc:forwarder" ], "data": [ diff --git a/contracts/feature-tests/composability/scenarios/promises_call_async_retrieve_egld.scen.json b/contracts/feature-tests/composability/scenarios/promises_call_async_retrieve_egld.scen.json index bb8e199e91..e17b0fd7d5 100644 --- a/contracts/feature-tests/composability/scenarios/promises_call_async_retrieve_egld.scen.json +++ b/contracts/feature-tests/composability/scenarios/promises_call_async_retrieve_egld.scen.json @@ -83,7 +83,7 @@ "address": "sc:vault", "endpoint": "str:transferValueOnly", "topics": [ - "1000", + "", "sc:forwarder" ], "data": [ From 3f400e2c1b3098f8b38e65ef44802c9749dff2ab Mon Sep 17 00:00:00 2001 From: Mihai Calin Luca Date: Tue, 26 Nov 2024 17:12:52 +0100 Subject: [PATCH 20/66] impl write to/ read from state file in interactor and new set state function --- .../examples/adder/interactor/set_state.json | 82 +++++++++++++++++++ .../adder/interactor/src/basic_interactor.rs | 2 +- .../tests/basic_interactor_cs_test.rs | 36 +++++++- framework/snippets/Cargo.toml | 6 +- framework/snippets/src/account_tool.rs | 16 ++-- .../src/interactor/interactor_base.rs | 35 +++++++- .../interactor/interactor_chain_simulator.rs | 11 +++ .../gateway_chain_simulator_set_state.rs | 41 +++++++++- 8 files changed, 212 insertions(+), 17 deletions(-) create mode 100644 contracts/examples/adder/interactor/set_state.json diff --git a/contracts/examples/adder/interactor/set_state.json b/contracts/examples/adder/interactor/set_state.json new file mode 100644 index 0000000000..a5364326ab --- /dev/null +++ b/contracts/examples/adder/interactor/set_state.json @@ -0,0 +1,82 @@ +[ + { + "address": "erd1uv40ahysflse896x4ktnh6ecx43u7cmy9wnxnvcyp7deg299a4sq6vaywa", + "nonce": 5947, + "balance": "491982310359999986", + "keys": { + "454c524f4e446573647450544d2d35333666616201": "08021202000122ef0108011212546573742d5061696e742d486172766573741a20e32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed6020c4132a2e516d57564239575362674b52655a64615a434344766b454b70705a6b4d696d397563736e7857565041414c6a4374324368747470733a2f2f697066732e696f2f697066732f516d57564239575362674b52655a64615a434344766b454b70705a6b4d696d397563736e7857565041414c6a43743a3d746167733a3b6d657461646174613a516d52635039346b5872357a5a6a52477669376d4a36756e374c7078556859565234523452706963787a67596b74", + "454c524f4e44657364745453542d643964336136": "1209004563918244f40000", + "454c524f4e44657364745745474c442d613238633539": "120900389351ce08f09e12", + "454c524f4e44657364745453542d633636666535": "1209004563918244f40000", + "454c524f4e44657364745453542d623130616461": "1209004563918244f40000", + "454c524f4e4465736474475245454e2d306531363163": "120b00152d02c7e14af67fffdc", + "454c524f4e44657364745453542d656338383735": "12020064", + "454c524f4e44657364744c5453542d376266336431": "1209000de0b6b3a763fc19", + "454c524f4e44657364745453542d343265356138": "1209004563918244f40000", + "454c524f4e44726f6c656573647450544d2d353336666162": "0a1145534454526f6c654e46544372656174650a0f45534454526f6c654e46544275726e", + "454c524f4e44657364745453542d393836646663": "12020064", + "454c524f4e44657364745453542d323833633361": "12020064", + "454c524f4e4465736474424358535542542d33393264366172": "080112020001", + "454c524f4e44657364744c5453542d346638343965": "1209000de0b6b3a763fc19", + "454c524f4e44657364745453542d386564363538": "1209004563918244f40000", + "454c524f4e44657364745453542d343562383235": "12020064", + "454c524f4e446e6f6e636550544d2d353336666162": "01", + "454c524f4e44657364745453542d363835303064": "1209004563918244f40000", + "454c524f4e44657364745453542d336339363762": "12020064", + "454c524f4e446573647455544b2d313464353764": "120b0001e6ce88d5ebbfd00000", + "454c524f4e44657364745453542d363434633935": "12020064", + "454c524f4e44657364745453542d306632306637": "12020064", + "454c524f4e44657364745453542d333331386638": "1209004563918244f40000", + "454c524f4e44657364745453542d353538616434": "12020064", + "454c524f4e44657364745453542d363437383930": "1209004563918244f40000", + "454c524f4e44657364745453542d633933336139": "1209004563918244f40000", + "454c524f4e44657364745453542d393864633566": "1209004563918244f40000", + "454c524f4e44657364745453542d303637373232": "1209004563918244f40000", + "454c524f4e44657364745453542d346634303238": "12020064", + "454c524f4e44657364745453542d643862306438": "12020064", + "454c524f4e44657364745453542d346230653865": "1209004563918244f40000", + "454c524f4e44657364745453542d343138613232": "1209004563918244f40000", + "454c524f4e44657364745453542d396230323030": "1209004563918244f40000", + "454c524f4e44657364745453542d373639313337": "1209004563918244f40000", + "454c524f4e44657364745453542d303362373664": "12020064", + "454c524f4e44657364745453542d613562663131": "12020064", + "454c524f4e44657364745453542d353966316165": "1209004563918244f40000", + "454c524f4e44657364745453542d623136363735": "1209004563918244f40000", + "454c524f4e44657364745453542d333639646531": "1209004563918244f40000", + "454c524f4e44657364745453542d623830663863": "1209004563918244f40000", + "454c524f4e44657364745453542d633565303835": "1209004563918244f40000" + }, + "code": "", + "code_hash": "", + "root_hash": "2zSeJjLqgozEQmmgDU8L0/GidcKzJOlJgwoaTUqvDFg=", + "code_metadata": "", + "owner_address": "", + "developer_reward": "0" + }, + { + "address": "erd13x29rvmp4qlgn4emgztd8jgvyzdj0p6vn37tqxas3v9mfhq4dy7shalqrx", + "nonce": 1417, + "balance": "1753855617144056", + "keys": { + "454c524f4e446573647445564e544e4f544946592d393634383835": "120b00152d02c7e14af6800000", + "454c524f4e4465736474494e5445524e532d63393332356601": "0801120b0013097d1fb962e12fff47", + "454c524f4e44657364744e455453432d623635306261": "120b00d137965aa7a731800000", + "454c524f4e446e6f6e6365494e5445524e532d633933323566": "01", + "454c524f4e44657364745745474c442d613238633539": "120800010593b233281b", + "454c524f4e446e6f6e63654d4554414e46542d643062623339": "01", + "454c524f4e44726f6c6565736474494e5445524e532d633933323566": "0a1145534454526f6c654e46544372656174650a1645534454526f6c654e46544164645175616e74697479", + "454c524f4e44726f6c65657364744d4554414e46542d643062623339": "0a1145534454526f6c654e4654437265617465", + "454c524f4e446573647442534b2d343736343730": "120b00021e19e0c9bab23fff7b", + "454c524f4e44657364744e45543253432d306438663962": "120f0004ee2d6d3f3d6bcc25c64dc00000", + "454c524f4e4465736474424358535542542d3339326436616c": "080112020001", + "454c524f4e44657364744e4943552d393730323932": "120b00d3c21bcecceda1000000", + "454c524f4e4465736474424358535542542d3339326436616e": "080112020001" + }, + "code": "", + "code_hash": "", + "root_hash": "AJ2jyOcPXgZAl0kHAlbWZIlG3F1VDtcoLAHR6eqehBA=", + "code_metadata": "", + "owner_address": "", + "developer_reward": "0" + } +] \ No newline at end of file diff --git a/contracts/examples/adder/interactor/src/basic_interactor.rs b/contracts/examples/adder/interactor/src/basic_interactor.rs index 64dff9673e..fc08f50357 100644 --- a/contracts/examples/adder/interactor/src/basic_interactor.rs +++ b/contracts/examples/adder/interactor/src/basic_interactor.rs @@ -57,7 +57,7 @@ impl AdderInteract { let adder_owner_address = interactor.register_wallet(test_wallets::heidi()).await; let wallet_address = interactor.register_wallet(test_wallets::ivan()).await; - let _ = interactor.generate_blocks(30u64).await; + interactor.generate_blocks(30u64).await.unwrap(); AdderInteract { interactor, diff --git a/contracts/examples/adder/interactor/tests/basic_interactor_cs_test.rs b/contracts/examples/adder/interactor/tests/basic_interactor_cs_test.rs index d6b98b73e5..6d217f66f3 100644 --- a/contracts/examples/adder/interactor/tests/basic_interactor_cs_test.rs +++ b/contracts/examples/adder/interactor/tests/basic_interactor_cs_test.rs @@ -1,5 +1,5 @@ use basic_interactor::{AdderInteract, Config}; -use multiversx_sc_snippets::{sdk::gateway::SetStateAccount, test_wallets}; +use multiversx_sc_snippets::{imports::Bech32Address, sdk::gateway::SetStateAccount, test_wallets}; #[tokio::test] #[cfg_attr(not(feature = "chain-simulator-tests"), ignore)] @@ -61,3 +61,37 @@ async fn set_state_cs_test() { assert!(set_state_response.is_ok()); } + +#[tokio::test] +#[cfg_attr(not(feature = "chain-simulator-tests"), ignore)] +async fn set_state_from_file_cs_test() { + let account_address = test_wallets::mike(); + let account_address_2 = test_wallets::ivan(); + + let mut real_chain_interact = AdderInteract::new(Config::load_config()).await; + let simulator_interact = AdderInteract::new(Config::chain_simulator_config()).await; + + // now we should have current mike account in the set state file + real_chain_interact + .interactor + .retrieve_account(&Bech32Address::from(&account_address.to_address())) + .await; + + real_chain_interact + .interactor + .retrieve_account(&Bech32Address::from(&account_address_2.to_address())) + .await; + + let set_state_response = simulator_interact + .interactor + .set_state_for_saved_accounts() + .await; + + simulator_interact + .interactor + .generate_blocks(2u64) + .await + .unwrap(); + + assert!(set_state_response.is_ok()); +} diff --git a/framework/snippets/Cargo.toml b/framework/snippets/Cargo.toml index 54fe80f23f..a22b7fd721 100644 --- a/framework/snippets/Cargo.toml +++ b/framework/snippets/Cargo.toml @@ -27,8 +27,9 @@ log = "0.4.17" env_logger = "0.11" futures = "0.3" anyhow = "1.0.44" +serde_json = "1.0" -tokio = { version = "1.24", features = ["full"], optional = true} +tokio = { version = "1.24", features = ["full"], optional = true } [dependencies.multiversx-sc-scenario] version = "=0.54.1" @@ -52,6 +53,3 @@ default-features = false version = "=0.7.0" path = "../../sdk/dapp" optional = true - -[dev-dependencies] -serde_json = "1.0" diff --git a/framework/snippets/src/account_tool.rs b/framework/snippets/src/account_tool.rs index b0a5ccc220..eefc51642f 100644 --- a/framework/snippets/src/account_tool.rs +++ b/framework/snippets/src/account_tool.rs @@ -4,7 +4,7 @@ use multiversx_sc_scenario::{ imports::Bech32Address, scenario_model::{Account, BytesKey, BytesValue, Scenario, SetStateStep, Step}, }; -use multiversx_sdk::gateway::GatewayAsyncService; +use multiversx_sdk::gateway::{GatewayAsyncService, SetStateAccount}; use multiversx_sdk::gateway::{ GetAccountEsdtRolesRequest, GetAccountEsdtTokensRequest, GetAccountRequest, GetAccountStorageRequest, @@ -20,7 +20,7 @@ pub async fn print_account_as_scenario_set_state Scenario { pub async fn retrieve_account_as_scenario_set_state( api: &GatewayProxy, bech32_address: &Bech32Address, -) -> SetStateStep { +) -> (SetStateAccount, SetStateStep) { let address = bech32_address.as_address(); let sdk_account = api.request(GetAccountRequest::new(address)).await.unwrap(); @@ -63,14 +63,18 @@ pub async fn retrieve_account_as_scenario_set_state where @@ -85,9 +88,33 @@ where } pub async fn retrieve_account(&mut self, wallet_address: &Bech32Address) { - let set_state = retrieve_account_as_scenario_set_state(&self.proxy, wallet_address).await; - self.pre_runners.run_set_state_step(&set_state); - self.post_runners.run_set_state_step(&set_state); + let (set_state_account, set_state_step) = + retrieve_account_as_scenario_set_state(&self.proxy, wallet_address).await; + self.pre_runners.run_set_state_step(&set_state_step); + self.post_runners.run_set_state_step(&set_state_step); + + let path = self.get_state_file_path(); + set_state_account.add_to_state_file(path.as_path()); + } + + pub fn get_state_file_path(&self) -> PathBuf { + self.current_dir.join(INTERACTOR_SET_STATE_PATH) + } + + pub fn get_accounts_from_file(&self) -> Vec { + let file_path = self.get_state_file_path(); + + if !file_path.exists() { + return Vec::new(); + } + + let file = File::open(file_path).expect("Failed to open state file"); + let reader = BufReader::new(file); + + serde_json::from_reader(reader).unwrap_or_else(|_| { + println!("Failed to parse state file; returning an empty list of accounts"); + Vec::new() + }) } /// Tells the interactor where the crate lies relative to the workspace. diff --git a/framework/snippets/src/interactor/interactor_chain_simulator.rs b/framework/snippets/src/interactor/interactor_chain_simulator.rs index 17ca51f3a7..88fa701fe0 100644 --- a/framework/snippets/src/interactor/interactor_chain_simulator.rs +++ b/framework/snippets/src/interactor/interactor_chain_simulator.rs @@ -64,4 +64,15 @@ where .request(ChainSimulatorSetStateRequest::for_accounts(accounts)) .await } + + pub async fn set_state_for_saved_accounts(&self) -> Result { + if !self.use_chain_simulator { + return Ok(String::from("no-simulator")); + } + + let accounts = self.get_accounts_from_file(); + self.proxy + .request(ChainSimulatorSetStateRequest::for_accounts(accounts)) + .await + } } diff --git a/sdk/core/src/gateway/gateway_chain_simulator_set_state.rs b/sdk/core/src/gateway/gateway_chain_simulator_set_state.rs index 77b46d4422..284f00a709 100644 --- a/sdk/core/src/gateway/gateway_chain_simulator_set_state.rs +++ b/sdk/core/src/gateway/gateway_chain_simulator_set_state.rs @@ -1,6 +1,11 @@ use anyhow::anyhow; use serde::{Deserialize, Serialize}; -use std::collections::HashMap; +use std::{ + collections::HashMap, + fs::{File, OpenOptions}, + io::{BufReader, BufWriter}, + path::Path, +}; use crate::data::account::Account; @@ -55,6 +60,40 @@ impl SetStateAccount { self } + + pub fn add_to_state_file(self, path: &Path) { + let mut accounts = if path.exists() { + let file = File::open(path) + .unwrap_or_else(|_| panic!("Failed to open state file at path {path:#?}")); + + let reader = BufReader::new(file); + + serde_json::from_reader::<_, Vec>(reader).unwrap_or_default() + } else { + Vec::new() + }; + + if let Some(existing_account) = accounts + .iter_mut() + .find(|account| account.address == self.address) + { + *existing_account = self; + } else { + accounts.push(self); + } + + let file = OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open(path) + .unwrap_or_else(|_| panic!("Failed to open or create state file at path {path:#?}")); + + let writer = BufWriter::new(file); + serde_json::to_writer_pretty(writer, &accounts).unwrap_or_else(|_| { + panic!("Failed to write updated state accounts to file at path {path:#?}") + }); + } } /// Sets state for a list of accounts using the chain simulator API. From 4acebb0c86283aa309602b6b2b6e1dc219ddbeb8 Mon Sep 17 00:00:00 2001 From: BiancaIalangi Date: Tue, 26 Nov 2024 23:31:32 +0200 Subject: [PATCH 21/66] VM Rust - fix transferValueOnly in async call --- chain/vm/src/tx_execution/exec_general_tx.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/chain/vm/src/tx_execution/exec_general_tx.rs b/chain/vm/src/tx_execution/exec_general_tx.rs index 2100e3fb20..d5e47d7b0f 100644 --- a/chain/vm/src/tx_execution/exec_general_tx.rs +++ b/chain/vm/src/tx_execution/exec_general_tx.rs @@ -1,3 +1,4 @@ +use num_bigint::BigUint; use num_traits::Zero; use crate::{ @@ -52,6 +53,19 @@ pub(crate) fn create_transfer_value_log(tx_input: &TxInput, call_type: CallType) let mut data = vec![call_type.to_log_bytes(), tx_input.func_name.to_bytes()]; data.append(&mut tx_input.args.clone()); + if tx_input.esdt_values.is_empty() { + if !tx_input.callback_payments.egld_value.is_zero() + && tx_input.call_type == CallType::AsyncCallback + { + return TxLog { + address: tx_input.from.clone(), + endpoint: "transferValueOnly".into(), + topics: vec![b"".to_vec(), tx_input.to.to_vec()], + data, + }; + } + } + let egld_value = if tx_input.call_type == CallType::AsyncCallback { &tx_input.callback_payments.egld_value } else { From 8c6ae73f34dab9eab91d13687ad29bf91e76f628 Mon Sep 17 00:00:00 2001 From: BiancaIalangi Date: Tue, 26 Nov 2024 23:35:04 +0200 Subject: [PATCH 22/66] clippy fix --- chain/vm/src/tx_execution/exec_general_tx.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/chain/vm/src/tx_execution/exec_general_tx.rs b/chain/vm/src/tx_execution/exec_general_tx.rs index d5e47d7b0f..abf4d4344e 100644 --- a/chain/vm/src/tx_execution/exec_general_tx.rs +++ b/chain/vm/src/tx_execution/exec_general_tx.rs @@ -1,4 +1,3 @@ -use num_bigint::BigUint; use num_traits::Zero; use crate::{ From bb59ac16455067aaf3c8d177080583f997fbfc07 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 27 Nov 2024 01:15:13 +0200 Subject: [PATCH 23/66] ManagedVecItem payload refactor - more attempts, read_from_payload impl --- .../src/types/managed/wrapped/managed_vec.rs | 18 ++-- .../types/managed/wrapped/managed_vec_item.rs | 101 ++++++++++++++++-- .../wrapped/managed_vec_item_nested_tuple.rs | 18 ++++ .../wrapped/managed_vec_item_payload.rs | 69 +++++++++--- .../managed/wrapped/managed_vec_owned_iter.rs | 16 +-- .../base/src/types/managed/wrapped/mod.rs | 6 +- .../derive/src/managed_vec_item_derive.rs | 32 +++++- 7 files changed, 215 insertions(+), 45 deletions(-) diff --git a/framework/base/src/types/managed/wrapped/managed_vec.rs b/framework/base/src/types/managed/wrapped/managed_vec.rs index d3212e4d03..475bc300d3 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec.rs @@ -1,4 +1,4 @@ -use super::EncodedManagedVecItem; +use super::{EncodedManagedVecItem, ManagedVecItemPayload}; use crate::{ abi::{TypeAbi, TypeAbiFrom, TypeDescriptionContainer, TypeName}, api::{ErrorApiImpl, InvalidSliceError, ManagedTypeApi}, @@ -187,15 +187,15 @@ where pub(super) unsafe fn get_unsafe(&self, index: usize) -> T { let byte_index = index * T::payload_size(); - let mut load_result = Ok(()); - let result = T::from_byte_reader(|dest_slice| { - load_result = self.buffer.load_slice(byte_index, dest_slice); - }); - - match load_result { - Ok(_) => result, - Err(_) => M::error_api_impl().signal_error(INDEX_OUT_OF_RANGE_MSG), + let mut payload = T::PAYLOAD::new_buffer(); + if self + .buffer + .load_slice(byte_index, payload.payload_slice_mut()) + .is_err() + { + M::error_api_impl().signal_error(INDEX_OUT_OF_RANGE_MSG); } + T::read_from_payload(&payload) } pub fn set(&mut self, index: usize, item: T) -> Result<(), InvalidSliceError> { diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item.rs b/framework/base/src/types/managed/wrapped/managed_vec_item.rs index 8c06da18c3..484a106d41 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item.rs @@ -10,7 +10,10 @@ use crate::{ }, }; -use super::{ManagedVecItemNestedTuple, ManagedVecItemNestedTupleSplit, ManagedVecItemPayload, ManagedVecItemPayloadBuffer}; +use super::{ + ManagedVecItemEmptyPayload, ManagedVecItemNestedTuple, ManagedVecItemNestedTupleSplit, + ManagedVecItemPayload, ManagedVecItemPayloadAdd, ManagedVecItemPayloadBuffer, +}; /// Types that implement this trait can be items inside a `ManagedVec`. /// All these types need a payload, i.e a representation that gets stored @@ -67,6 +70,16 @@ pub trait ManagedVecItem: 'static { fn into_byte_writer R>(self, writer: Writer) -> R; } +pub unsafe fn managed_vec_item_read_from_payload_index(payload: &P, index: &mut usize) -> T +where + T: ManagedVecItem, + P: ManagedVecItemPayload, +{ + let value = T::read_from_payload(payload.slice_unchecked(*index)); + *index += T::PAYLOAD::payload_size(); + value +} + macro_rules! impl_int { ($ty:ident, $payload_size:expr) => { impl ManagedVecItem for $ty { @@ -161,9 +174,12 @@ impl ManagedVecItem for bool { impl ManagedVecItem for Option where (u8, (T, ())): ManagedVecItemNestedTuple, + ManagedVecItemPayloadBuffer<1>: ManagedVecItemPayloadAdd, T: ManagedVecItem, { - type PAYLOAD = <(u8, (T, ())) as ManagedVecItemNestedTuple>::PAYLOAD; + // type PAYLOAD = <(u8, (T, ())) as ManagedVecItemNestedTuple>::PAYLOAD; + type PAYLOAD = as ManagedVecItemPayloadAdd>::Output; + // type PAYLOAD = EquivPayload<(u8, T)>; const SKIPS_RESERIALIZATION: bool = false; type Ref<'a> = Self; @@ -182,13 +198,26 @@ where fn read_from_payload(payload: &Self::PAYLOAD) -> Self { // let (p1, (p2, ())) = <(u8, (T, ()))>::split_all(payload); - + + // let (p1, p2) = as ManagedVecItemPayloadAdd< + // T::PAYLOAD, + // >>::split_from_add(payload); + // let disc = u8::read_from_payload(p1); - // let x = split_payload(payload); - // Self::PAYLOAD::spli - todo!() - // let (disc_payload, t_payload) = payload.split::<1>(); - // let x = payload; + // if disc == 0 { + // None + // } else { + // Some(T::read_from_payload(p2)) + // } + + unsafe { + let disc = u8::read_from_payload(payload.slice_unchecked(0)); + if disc == 0 { + None + } else { + Some(T::read_from_payload(payload.slice_unchecked(1))) + } + } } unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( @@ -363,3 +392,59 @@ impl ManagedVecItem for EsdtLocalRole { ::into_byte_writer(self.as_u16(), writer) } } + +impl ManagedVecItem for () { + type PAYLOAD = ManagedVecItemEmptyPayload; + + const SKIPS_RESERIALIZATION: bool = true; + + type Ref<'a> = Self; + + fn from_byte_reader(reader: Reader) -> Self { + () + } + + fn read_from_payload(payload: &Self::PAYLOAD) -> Self { + () + } + + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( + _reader: Reader, + ) -> Self::Ref<'a> { + () + } + + fn into_byte_writer R>(self, writer: Writer) -> R { + todo!() + } +} + +impl ManagedVecItem for (Head, Tail) +where + Head: ManagedVecItem + 'static, + Tail: ManagedVecItem + 'static, +{ + type PAYLOAD = ManagedVecItemEmptyPayload; + + const SKIPS_RESERIALIZATION: bool = true; + + type Ref<'a> = Self; + + fn from_byte_reader(reader: Reader) -> Self { + todo!() + } + + fn read_from_payload(payload: &Self::PAYLOAD) -> Self { + todo!() + } + + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( + reader: Reader, + ) -> Self::Ref<'a> { + todo!() + } + + fn into_byte_writer R>(self, writer: Writer) -> R { + todo!() + } +} diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item_nested_tuple.rs b/framework/base/src/types/managed/wrapped/managed_vec_item_nested_tuple.rs index 45e6a8d820..7c64920cf3 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item_nested_tuple.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item_nested_tuple.rs @@ -86,6 +86,8 @@ where #[cfg(test)] pub mod tests { + use crate::types::ManagedVecItemPayloadBuffer; + use super::*; #[test] @@ -100,4 +102,20 @@ pub mod tests { fn assert_payload_size(expected_size: usize) { assert_eq!(N::PAYLOAD::payload_size(), expected_size); } + + #[test] + fn split_all_test() { + let p = ManagedVecItemPayloadBuffer::new_buffer(); + let (p1, (p2, ())) = <(u16, (u32, ()))>::split_all(&p); + } + + // fn split_all_t() + // where + // T: ManagedVecItem, + // ManagedVecItemPayloadBuffer<1>: ManagedVecItemPayloadAdd, + // (u16, (T, ())): ManagedVecItemNestedTuple, + // { + // let p = ManagedVecItemPayloadBuffer::new_buffer(); + // let (p1, (p2, ())) = <(u16, (T, ()))>::split_all(&p); + // } } diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs b/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs index 4fcc20d9d3..ba2d7e1296 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs @@ -1,5 +1,7 @@ use core::marker::PhantomData; +use super::ManagedVecItem; + /// Describes the binary represetnation of a ManagedVecItem. /// /// It is always an array that can be allocated directly on stack. @@ -11,6 +13,8 @@ pub trait ManagedVecItemPayload { fn payload_slice(&self) -> &[u8]; fn payload_slice_mut(&mut self) -> &mut [u8]; + + unsafe fn slice_unchecked(&self, index: usize) -> &S; } /// Empty ManagedVecItem. @@ -34,6 +38,10 @@ impl ManagedVecItemPayload for ManagedVecItemEmptyPayload { fn payload_slice_mut(&mut self) -> &mut [u8] { &mut [] } + + unsafe fn slice_unchecked(&self, index: usize) -> &S { + unimplemented!() + } } /// The main ManagedVecItemPayload implementation. Uses an array in its implementation. @@ -58,6 +66,11 @@ impl ManagedVecItemPayload for ManagedVecItemPayloadBuffer { fn payload_slice_mut(&mut self) -> &mut [u8] { &mut self.buffer[..] } + + unsafe fn slice_unchecked(&self, index: usize) -> &S { + let ptr = self.buffer.as_ptr().offset(index as isize); + core::mem::transmute(ptr) + } } #[repr(transparent)] @@ -97,6 +110,10 @@ where fn payload_slice_mut(&mut self) -> &mut [u8] { self.buffer.payload_slice_mut() } + + unsafe fn slice_unchecked(&self, index: usize) -> &S { + self.buffer.slice_unchecked(index) + } } impl ManagedVecItemPayloadConcat @@ -116,25 +133,44 @@ where P0: ManagedVecItemPayloadAdd<>::Output>, { type Output = ManagedVecItemPayloadConcat>; - + fn split_from_add(payload: &Self::Output) -> (&Self, &ManagedVecItemPayloadConcat) { todo!() } +} + +pub struct ManagedVecItemPayloadEquiv +where + T: ManagedVecItem, +{ + equiv: T::PAYLOAD, +} + +impl ManagedVecItemPayload for ManagedVecItemPayloadEquiv +where + T: ManagedVecItem, +{ + fn new_buffer() -> Self { + ManagedVecItemPayloadEquiv { + equiv: ManagedVecItemPayload::new_buffer(), + } + } + fn payload_size() -> usize { + T::PAYLOAD::payload_size() + } - - // fn split_from_add( - // payload: &ManagedVecItemPayloadBuffer<$result_add>, - // ) -> ( - // &ManagedVecItemPayloadBuffer<$dec1>, - // &ManagedVecItemPayloadBuffer<$dec2>, - // ) { - // unsafe { - // let ptr1 = payload.buffer.as_ptr(); - // let ptr2 = ptr1.offset($dec1 as isize); - // (core::mem::transmute(ptr1), core::mem::transmute(ptr2)) - // } - // } + fn payload_slice(&self) -> &[u8] { + self.equiv.payload_slice() + } + + fn payload_slice_mut(&mut self) -> &mut [u8] { + self.equiv.payload_slice_mut() + } + + unsafe fn slice_unchecked(&self, index: usize) -> &S { + self.equiv.slice_unchecked(index) + } } /// Describes concatantion of smaller payloads into a larger one. @@ -151,8 +187,9 @@ where fn split_from_add(payload: &Self::Output) -> (&Self, &Rhs); } -impl ManagedVecItemPayloadAdd - for ManagedVecItemPayloadBuffer +impl

ManagedVecItemPayloadAdd for P +where + P: ManagedVecItemPayload, { type Output = Self; diff --git a/framework/base/src/types/managed/wrapped/managed_vec_owned_iter.rs b/framework/base/src/types/managed/wrapped/managed_vec_owned_iter.rs index 20942e70d4..470bd16053 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_owned_iter.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_owned_iter.rs @@ -1,6 +1,6 @@ use crate::api::ManagedTypeApi; -use super::{ManagedVec, ManagedVecItem}; +use super::{ManagedVec, ManagedVecItem, ManagedVecItemPayload}; impl<'a, M, T> IntoIterator for &'a ManagedVec where @@ -52,15 +52,15 @@ where if next_byte_start > self.byte_end { return None; } - let result = T::from_byte_reader(|dest_slice| { - let _ = self - .managed_vec - .buffer - .load_slice(self.byte_start, dest_slice); - }); + + let mut payload = T::PAYLOAD::new_buffer(); + let _ = self + .managed_vec + .buffer + .load_slice(self.byte_start, payload.payload_slice_mut()); self.byte_start = next_byte_start; - Some(result) + Some(T::read_from_payload(&payload)) } fn size_hint(&self) -> (usize, Option) { diff --git a/framework/base/src/types/managed/wrapped/mod.rs b/framework/base/src/types/managed/wrapped/mod.rs index f4db4e5212..38d721812e 100644 --- a/framework/base/src/types/managed/wrapped/mod.rs +++ b/framework/base/src/types/managed/wrapped/mod.rs @@ -46,8 +46,10 @@ pub use managed_option::ManagedOption; pub use managed_ref::ManagedRef; pub use managed_ref_mut::ManagedRefMut; pub use managed_vec::ManagedVec; -pub use managed_vec_item::ManagedVecItem; -pub use managed_vec_item_nested_tuple::{ManagedVecItemNestedTuple, ManagedVecItemNestedTupleSplit}; +pub use managed_vec_item::{managed_vec_item_read_from_payload_index, ManagedVecItem}; +pub use managed_vec_item_nested_tuple::{ + ManagedVecItemNestedTuple, ManagedVecItemNestedTupleSplit, +}; pub use managed_vec_item_payload::*; pub use managed_vec_owned_iter::ManagedVecOwnedIterator; pub use managed_vec_ref::ManagedVecRef; diff --git a/framework/derive/src/managed_vec_item_derive.rs b/framework/derive/src/managed_vec_item_derive.rs index c0525ee0d5..d5b91d2fbf 100644 --- a/framework/derive/src/managed_vec_item_derive.rs +++ b/framework/derive/src/managed_vec_item_derive.rs @@ -66,6 +66,24 @@ fn generate_from_byte_reader_snippets(fields: &syn::Fields) -> Vec Vec { + match fields { + syn::Fields::Named(fields_named) => fields_named + .named + .iter() + .map(|field| { + let field_ident = &field.ident; + quote! { + #field_ident: multiversx_sc::types::managed_vec_item_read_from_payload_index(payload, &mut index), + } + }) + .collect(), + _ => { + panic!("ManagedVecItem only supports named fields") + } + } +} + fn generate_to_byte_writer_snippets(fields: &syn::Fields) -> Vec { match fields { syn::Fields::Named(fields_named) => fields_named @@ -134,7 +152,10 @@ fn enum_derive(data_enum: &syn::DataEnum, ast: &syn::DeriveInput) -> TokenStream } fn read_from_payload(payload: &Self::PAYLOAD) -> Self { - todo!() + let discriminant = ::read_from_payload(payload); + match discriminant { + #(#reader_match_arms)* + } } unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>(reader: Reader) -> Self::Ref<'a> { @@ -160,6 +181,7 @@ fn struct_derive(data_struct: &syn::DataStruct, ast: &syn::DeriveInput) -> Token let skips_reserialization_snippets = generate_skips_reserialization_snippets(&data_struct.fields); let from_byte_reader_snippets = generate_from_byte_reader_snippets(&data_struct.fields); + let read_from_payload_snippets = generate_read_from_payload_snippets(&data_struct.fields); let to_byte_writer_snippets = generate_to_byte_writer_snippets(&data_struct.fields); let payload_buffer_snippet = generate_payload_buffer_snippet(); @@ -181,7 +203,13 @@ fn struct_derive(data_struct: &syn::DataStruct, ast: &syn::DeriveInput) -> Token } fn read_from_payload(payload: &Self::PAYLOAD) -> Self { - todo!() + let mut index = 0; + + unsafe { + #name { + #(#read_from_payload_snippets)* + } + } } unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>(reader: Reader) -> Self::Ref<'a> { From 08b47efbdea818e49277463aa89e57082b0e648d Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 27 Nov 2024 01:32:31 +0200 Subject: [PATCH 24/66] ManagedVecItem payload refactor - cleanup --- .../managed/wrapped/esdt_token_payment.rs | 11 ++- .../types/managed/wrapped/managed_vec_item.rs | 93 ++----------------- .../wrapped/managed_vec_item_nested_tuple.rs | 2 +- .../wrapped/managed_vec_item_payload.rs | 58 +----------- 4 files changed, 22 insertions(+), 142 deletions(-) diff --git a/framework/base/src/types/managed/wrapped/esdt_token_payment.rs b/framework/base/src/types/managed/wrapped/esdt_token_payment.rs index a92a9cbe16..83369820c2 100644 --- a/framework/base/src/types/managed/wrapped/esdt_token_payment.rs +++ b/framework/base/src/types/managed/wrapped/esdt_token_payment.rs @@ -13,7 +13,7 @@ use crate::{ derive::type_abi, }; -use super::{ManagedVec, ManagedVecItemPayloadBuffer}; +use super::{managed_vec_item_read_from_payload_index, ManagedVec, ManagedVecItemPayloadBuffer}; #[type_abi] #[derive(TopEncode, NestedEncode, Clone, PartialEq, Eq, Debug)] @@ -206,7 +206,14 @@ impl ManagedVecItem for EsdtTokenPayment { } fn read_from_payload(payload: &Self::PAYLOAD) -> Self { - todo!() + let mut index = 0; + unsafe { + EsdtTokenPayment { + token_identifier: managed_vec_item_read_from_payload_index(payload, &mut index), + token_nonce: managed_vec_item_read_from_payload_index(payload, &mut index), + amount: managed_vec_item_read_from_payload_index(payload, &mut index), + } + } } unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item.rs b/framework/base/src/types/managed/wrapped/managed_vec_item.rs index 484a106d41..19d714d7b9 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item.rs @@ -10,10 +10,7 @@ use crate::{ }, }; -use super::{ - ManagedVecItemEmptyPayload, ManagedVecItemNestedTuple, ManagedVecItemNestedTupleSplit, - ManagedVecItemPayload, ManagedVecItemPayloadAdd, ManagedVecItemPayloadBuffer, -}; +use super::{ManagedVecItemPayload, ManagedVecItemPayloadAdd, ManagedVecItemPayloadBuffer}; /// Types that implement this trait can be items inside a `ManagedVec`. /// All these types need a payload, i.e a representation that gets stored @@ -173,13 +170,10 @@ impl ManagedVecItem for bool { impl ManagedVecItem for Option where - (u8, (T, ())): ManagedVecItemNestedTuple, ManagedVecItemPayloadBuffer<1>: ManagedVecItemPayloadAdd, T: ManagedVecItem, { - // type PAYLOAD = <(u8, (T, ())) as ManagedVecItemNestedTuple>::PAYLOAD; type PAYLOAD = as ManagedVecItemPayloadAdd>::Output; - // type PAYLOAD = EquivPayload<(u8, T)>; const SKIPS_RESERIALIZATION: bool = false; type Ref<'a> = Self; @@ -197,26 +191,15 @@ where } fn read_from_payload(payload: &Self::PAYLOAD) -> Self { - // let (p1, (p2, ())) = <(u8, (T, ()))>::split_all(payload); - - // let (p1, p2) = as ManagedVecItemPayloadAdd< - // T::PAYLOAD, - // >>::split_from_add(payload); - - // let disc = u8::read_from_payload(p1); - // if disc == 0 { - // None - // } else { - // Some(T::read_from_payload(p2)) - // } - - unsafe { - let disc = u8::read_from_payload(payload.slice_unchecked(0)); - if disc == 0 { - None - } else { - Some(T::read_from_payload(payload.slice_unchecked(1))) - } + let (p1, p2) = as ManagedVecItemPayloadAdd< + T::PAYLOAD, + >>::split_from_add(payload); + + let disc = u8::read_from_payload(p1); + if disc == 0 { + None + } else { + Some(T::read_from_payload(p2)) } } @@ -392,59 +375,3 @@ impl ManagedVecItem for EsdtLocalRole { ::into_byte_writer(self.as_u16(), writer) } } - -impl ManagedVecItem for () { - type PAYLOAD = ManagedVecItemEmptyPayload; - - const SKIPS_RESERIALIZATION: bool = true; - - type Ref<'a> = Self; - - fn from_byte_reader(reader: Reader) -> Self { - () - } - - fn read_from_payload(payload: &Self::PAYLOAD) -> Self { - () - } - - unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( - _reader: Reader, - ) -> Self::Ref<'a> { - () - } - - fn into_byte_writer R>(self, writer: Writer) -> R { - todo!() - } -} - -impl ManagedVecItem for (Head, Tail) -where - Head: ManagedVecItem + 'static, - Tail: ManagedVecItem + 'static, -{ - type PAYLOAD = ManagedVecItemEmptyPayload; - - const SKIPS_RESERIALIZATION: bool = true; - - type Ref<'a> = Self; - - fn from_byte_reader(reader: Reader) -> Self { - todo!() - } - - fn read_from_payload(payload: &Self::PAYLOAD) -> Self { - todo!() - } - - unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( - reader: Reader, - ) -> Self::Ref<'a> { - todo!() - } - - fn into_byte_writer R>(self, writer: Writer) -> R { - todo!() - } -} diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item_nested_tuple.rs b/framework/base/src/types/managed/wrapped/managed_vec_item_nested_tuple.rs index 7c64920cf3..1e8b5511a4 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item_nested_tuple.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item_nested_tuple.rs @@ -106,7 +106,7 @@ pub mod tests { #[test] fn split_all_test() { let p = ManagedVecItemPayloadBuffer::new_buffer(); - let (p1, (p2, ())) = <(u16, (u32, ()))>::split_all(&p); + let (_p1, (_p2, ())) = <(u16, (u32, ()))>::split_all(&p); } // fn split_all_t() diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs b/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs index ba2d7e1296..31210dd714 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs @@ -39,7 +39,7 @@ impl ManagedVecItemPayload for ManagedVecItemEmptyPayload { &mut [] } - unsafe fn slice_unchecked(&self, index: usize) -> &S { + unsafe fn slice_unchecked(&self, _index: usize) -> &S { unimplemented!() } } @@ -134,7 +134,7 @@ where { type Output = ManagedVecItemPayloadConcat>; - fn split_from_add(payload: &Self::Output) -> (&Self, &ManagedVecItemPayloadConcat) { + fn split_from_add(_payload: &Self::Output) -> (&Self, &ManagedVecItemPayloadConcat) { todo!() } } @@ -198,27 +198,6 @@ where } } -pub trait ManagedVecItemPayloadSplit: ManagedVecItemPayload { - type P1: ManagedVecItemPayload; - type P2: ManagedVecItemPayload; - - fn split_impl(&self) -> (&Self::P1, &Self::P2); -} - -impl ManagedVecItemPayloadBuffer { - pub fn split_me( - &self, - ) -> ( - &>::P1, - &>::P2, - ) - where - Self: ManagedVecItemPayloadSplit, - { - ManagedVecItemPayloadSplit::::split_impl(self) - } -} - /// Replaces a const generic expression. /// /// Remove once const generic expressions are stabilized in Rust. @@ -242,42 +221,9 @@ macro_rules! payload_add { } } } - - impl ManagedVecItemPayloadSplit<$dec1> for ManagedVecItemPayloadBuffer<$result_add> { - type P1 = ManagedVecItemPayloadBuffer<$dec1>; - type P2 = ManagedVecItemPayloadBuffer<$dec2>; - - fn split_impl( - &self, - ) -> ( - &ManagedVecItemPayloadBuffer<$dec1>, - &ManagedVecItemPayloadBuffer<$dec2>, - ) { - unsafe { - let ptr1 = self.buffer.as_ptr(); - let ptr2 = ptr1.offset($dec1 as isize); - (core::mem::transmute(ptr1), core::mem::transmute(ptr2)) - } - } - } }; } -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn payload_split() { - let payload = ManagedVecItemPayloadBuffer { - buffer: [1, 2, 3, 4, 5], - }; - let (p1, p2) = payload.split_me::<3>(); - assert_eq!(p1.buffer, [1, 2, 3]); - assert_eq!(p2.buffer, [4, 5]); - } -} - payload_add!(1usize, 1usize, 2usize); payload_add!(1usize, 2usize, 3usize); payload_add!(1usize, 3usize, 4usize); From 58090a1aa0a3d252408ab209ff9dd3451db20c2d Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 27 Nov 2024 02:12:29 +0200 Subject: [PATCH 25/66] ManagedVecItem payload refactor - borrow_from_payload impl --- .../esdt_token_payment_multi_value.rs | 5 +++ .../managed/wrapped/esdt_token_payment.rs | 5 +++ .../types/managed/wrapped/managed_option.rs | 8 +++- .../src/types/managed/wrapped/managed_vec.rs | 19 +++++---- .../types/managed/wrapped/managed_vec_item.rs | 41 +++++++++++++++++++ .../wrapped/managed_vec_item_nested_tuple.rs | 4 +- .../managed/wrapped/managed_vec_owned_iter.rs | 13 +++--- .../managed/wrapped/managed_vec_ref_iter.rs | 34 +++++++-------- .../derive/src/managed_vec_item_derive.rs | 9 ++++ .../src/api/impl_vh/debug_handle_vh.rs | 4 ++ 10 files changed, 103 insertions(+), 39 deletions(-) diff --git a/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs b/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs index 4eb6fc60d8..07cf680de0 100644 --- a/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs +++ b/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs @@ -52,6 +52,11 @@ impl ManagedVecItem for EsdtTokenPaymentMultiValue { EsdtTokenPayment::read_from_payload(payload).into() } + unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> { + // TODO: managed ref + Self::read_from_payload(payload) + } + #[inline] unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( reader: Reader, diff --git a/framework/base/src/types/managed/wrapped/esdt_token_payment.rs b/framework/base/src/types/managed/wrapped/esdt_token_payment.rs index 83369820c2..b0a62679b8 100644 --- a/framework/base/src/types/managed/wrapped/esdt_token_payment.rs +++ b/framework/base/src/types/managed/wrapped/esdt_token_payment.rs @@ -216,6 +216,11 @@ impl ManagedVecItem for EsdtTokenPayment { } } + unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> { + // TODO: managed ref + Self::read_from_payload(payload) + } + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( reader: Reader, ) -> Self::Ref<'a> { diff --git a/framework/base/src/types/managed/wrapped/managed_option.rs b/framework/base/src/types/managed/wrapped/managed_option.rs index ae77c6bc92..add7ae2669 100644 --- a/framework/base/src/types/managed/wrapped/managed_option.rs +++ b/framework/base/src/types/managed/wrapped/managed_option.rs @@ -209,7 +209,13 @@ where } fn read_from_payload(payload: &Self::PAYLOAD) -> Self { - Self::new_with_handle(use_raw_handle(i32::read_from_payload(payload))) + let handle = use_raw_handle(i32::read_from_payload(payload)); + Self::new_with_handle(handle) + } + + unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> { + // TODO: managed ref + Self::read_from_payload(payload) } unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( diff --git a/framework/base/src/types/managed/wrapped/managed_vec.rs b/framework/base/src/types/managed/wrapped/managed_vec.rs index 475bc300d3..de84d66904 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec.rs @@ -141,15 +141,16 @@ where pub fn try_get(&self, index: usize) -> Option> { let byte_index = index * T::payload_size(); - let mut load_result = Ok(()); - let result = unsafe { - T::from_byte_reader_as_borrow(|dest_slice| { - load_result = self.buffer.load_slice(byte_index, dest_slice); - }) - }; - match load_result { - Ok(_) => Some(result), - Err(_) => None, + + let mut payload = T::PAYLOAD::new_buffer(); + if self + .buffer + .load_slice(byte_index, payload.payload_slice_mut()) + .is_ok() + { + unsafe { Some(T::borrow_from_payload(&payload)) } + } else { + None } } diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item.rs b/framework/base/src/types/managed/wrapped/managed_vec_item.rs index 19d714d7b9..87e8bc3094 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item.rs @@ -46,6 +46,8 @@ pub trait ManagedVecItem: 'static { fn read_from_payload(payload: &Self::PAYLOAD) -> Self; + unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a>; + /// Parses given bytes as a representation of the object, either owned, or a reference. /// /// # Safety @@ -94,6 +96,10 @@ macro_rules! impl_int { $ty::from_be_bytes(payload.buffer) } + unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> { + $ty::from_be_bytes(payload.buffer) + } + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( reader: Reader, ) -> Self::Ref<'a> { @@ -129,6 +135,10 @@ impl ManagedVecItem for usize { u32::read_from_payload(payload) as usize } + unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> { + Self::read_from_payload(payload) + } + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( reader: Reader, ) -> Self::Ref<'a> { @@ -154,6 +164,10 @@ impl ManagedVecItem for bool { u8::read_from_payload(payload) > 0 } + unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> { + Self::read_from_payload(payload) + } + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( reader: Reader, ) -> Self::Ref<'a> { @@ -203,6 +217,10 @@ where } } + unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> { + Self::read_from_payload(payload) + } + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( reader: Reader, ) -> Self::Ref<'a> { @@ -239,6 +257,11 @@ macro_rules! impl_managed_type { unsafe { Self::from_handle(handle) } } + unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> { + let handle = use_raw_handle(i32::read_from_payload(payload)); + ManagedRef::wrap_handle(handle) + } + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( reader: Reader, ) -> Self::Ref<'a> { @@ -279,6 +302,11 @@ where unsafe { Self::from_handle(handle) } } + unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> { + let handle = use_raw_handle(i32::read_from_payload(payload)); + ManagedRef::wrap_handle(handle) + } + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( reader: Reader, ) -> Self::Ref<'a> { @@ -313,6 +341,11 @@ where unsafe { Self::from_handle(handle) } } + unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> { + let handle = use_raw_handle(i32::read_from_payload(payload)); + ManagedRef::wrap_handle(handle) + } + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( reader: Reader, ) -> Self::Ref<'a> { @@ -341,6 +374,10 @@ impl ManagedVecItem for EsdtTokenType { u8::read_from_payload(payload).into() } + unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> { + Self::read_from_payload(payload) + } + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( reader: Reader, ) -> Self::Ref<'a> { @@ -365,6 +402,10 @@ impl ManagedVecItem for EsdtLocalRole { u16::read_from_payload(payload).into() } + unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> { + Self::read_from_payload(payload) + } + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( reader: Reader, ) -> Self::Ref<'a> { diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item_nested_tuple.rs b/framework/base/src/types/managed/wrapped/managed_vec_item_nested_tuple.rs index 1e8b5511a4..11a7055a40 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item_nested_tuple.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item_nested_tuple.rs @@ -31,9 +31,7 @@ impl ManagedVecItemNestedTuple for () { impl<'a> ManagedVecItemNestedTupleSplit<'a> for () { type S = (); - fn split_all(_payload: &'a Self::PAYLOAD) -> Self::S { - () - } + fn split_all(_payload: &'a Self::PAYLOAD) -> Self::S {} } impl ManagedVecItemNestedTuple for (Head, Tail) diff --git a/framework/base/src/types/managed/wrapped/managed_vec_owned_iter.rs b/framework/base/src/types/managed/wrapped/managed_vec_owned_iter.rs index 470bd16053..8e0da8727c 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_owned_iter.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_owned_iter.rs @@ -88,14 +88,13 @@ where } self.byte_end -= T::payload_size(); - let result = T::from_byte_reader(|dest_slice| { - let _ = self - .managed_vec - .buffer - .load_slice(self.byte_end, dest_slice); - }); + let mut payload = T::PAYLOAD::new_buffer(); + let _ = self + .managed_vec + .buffer + .load_slice(self.byte_end, payload.payload_slice_mut()); - Some(result) + Some(T::read_from_payload(&payload)) } } diff --git a/framework/base/src/types/managed/wrapped/managed_vec_ref_iter.rs b/framework/base/src/types/managed/wrapped/managed_vec_ref_iter.rs index ad04b5eaf4..4703a1f61b 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_ref_iter.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_ref_iter.rs @@ -1,6 +1,6 @@ use crate::api::ManagedTypeApi; -use super::{ManagedVec, ManagedVecItem}; +use super::{ManagedVec, ManagedVecItem, ManagedVecItemPayload}; pub struct ManagedVecRefIterator<'a, M, T> where @@ -38,17 +38,16 @@ where if next_byte_start > self.byte_end { return None; } - let result = unsafe { - T::from_byte_reader_as_borrow(|dest_slice| { - let _ = self - .managed_vec - .buffer - .load_slice(self.byte_start, dest_slice); - }) - }; + + let mut payload = T::PAYLOAD::new_buffer(); + let _ = self + .managed_vec + .buffer + .load_slice(self.byte_start, payload.payload_slice_mut()); self.byte_start = next_byte_start; - Some(result) + + unsafe { Some(T::borrow_from_payload(&payload)) } } fn size_hint(&self) -> (usize, Option) { @@ -75,16 +74,13 @@ where } self.byte_end -= T::payload_size(); - let result = unsafe { - T::from_byte_reader_as_borrow(|dest_slice| { - let _ = self - .managed_vec - .buffer - .load_slice(self.byte_end, dest_slice); - }) - }; + let mut payload = T::PAYLOAD::new_buffer(); + let _ = self + .managed_vec + .buffer + .load_slice(self.byte_end, payload.payload_slice_mut()); - Some(result) + unsafe { Some(T::borrow_from_payload(&payload)) } } } diff --git a/framework/derive/src/managed_vec_item_derive.rs b/framework/derive/src/managed_vec_item_derive.rs index d5b91d2fbf..1b1d41fe69 100644 --- a/framework/derive/src/managed_vec_item_derive.rs +++ b/framework/derive/src/managed_vec_item_derive.rs @@ -158,6 +158,10 @@ fn enum_derive(data_enum: &syn::DataEnum, ast: &syn::DeriveInput) -> TokenStream } } + unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> { + Self::read_from_payload(payload) + } + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>(reader: Reader) -> Self::Ref<'a> { Self::from_byte_reader(reader) } @@ -212,6 +216,11 @@ fn struct_derive(data_struct: &syn::DataStruct, ast: &syn::DeriveInput) -> Token } } + unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> { + // TODO: managed ref + Self::read_from_payload(payload) + } + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>(reader: Reader) -> Self::Ref<'a> { Self::from_byte_reader(reader) } diff --git a/framework/scenario/src/api/impl_vh/debug_handle_vh.rs b/framework/scenario/src/api/impl_vh/debug_handle_vh.rs index c60df36991..4a6d38b212 100644 --- a/framework/scenario/src/api/impl_vh/debug_handle_vh.rs +++ b/framework/scenario/src/api/impl_vh/debug_handle_vh.rs @@ -91,6 +91,10 @@ impl ManagedVecItem for DebugHandle { use_raw_handle(RawHandle::read_from_payload(payload)) } + unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> { + Self::read_from_payload(payload) + } + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( reader: Reader, ) -> Self::Ref<'a> { From b6c31f1a49400cb9475dcd67879f0f8480052f92 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 27 Nov 2024 02:12:55 +0200 Subject: [PATCH 26/66] clippy fix --- .../src/types/managed/wrapped/managed_vec_item.rs | 5 +++++ .../types/managed/wrapped/managed_vec_item_payload.rs | 11 +++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item.rs b/framework/base/src/types/managed/wrapped/managed_vec_item.rs index 87e8bc3094..512e2d847d 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item.rs @@ -46,6 +46,11 @@ pub trait ManagedVecItem: 'static { fn read_from_payload(payload: &Self::PAYLOAD) -> Self; + /// Parses given bytes as a representation of the object, either owned, or a reference. + /// + /// # Safety + /// + /// In certain cases this involves practically disregarding the lifetimes, hence it is unsafe. unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a>; /// Parses given bytes as a representation of the object, either owned, or a reference. diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs b/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs index 31210dd714..4d336632cf 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs @@ -68,8 +68,8 @@ impl ManagedVecItemPayload for ManagedVecItemPayloadBuffer { } unsafe fn slice_unchecked(&self, index: usize) -> &S { - let ptr = self.buffer.as_ptr().offset(index as isize); - core::mem::transmute(ptr) + let ptr = self.buffer.as_ptr().add(index); + &*ptr.cast::() } } @@ -216,8 +216,11 @@ macro_rules! payload_add { ) { unsafe { let ptr1 = payload.buffer.as_ptr(); - let ptr2 = ptr1.offset($dec1 as isize); - (core::mem::transmute(ptr1), core::mem::transmute(ptr2)) + let ptr2 = ptr1.add($dec1); + ( + &*ptr1.cast::>(), + &*ptr2.cast::>(), + ) } } } From 50c674ce67eaf8cc739c083fdb3d0f467bec2f83 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 27 Nov 2024 02:20:07 +0200 Subject: [PATCH 27/66] ManagedVecItem payload refactor - remove from_byte_reader_as_borrow --- .../esdt_token_payment_multi_value.rs | 7 -- .../managed/wrapped/esdt_token_payment.rs | 6 -- .../types/managed/wrapped/managed_option.rs | 6 +- .../types/managed/wrapped/managed_vec_item.rs | 66 ------------------- .../derive/src/managed_vec_item_derive.rs | 8 --- .../src/api/impl_vh/debug_handle_vh.rs | 6 -- 6 files changed, 1 insertion(+), 98 deletions(-) diff --git a/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs b/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs index 07cf680de0..299bc8bfc2 100644 --- a/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs +++ b/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs @@ -57,13 +57,6 @@ impl ManagedVecItem for EsdtTokenPaymentMultiValue { Self::read_from_payload(payload) } - #[inline] - unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( - reader: Reader, - ) -> Self::Ref<'a> { - Self::from_byte_reader(reader) - } - #[inline] fn into_byte_writer R>(self, writer: Writer) -> R { self.obj.into_byte_writer(writer) diff --git a/framework/base/src/types/managed/wrapped/esdt_token_payment.rs b/framework/base/src/types/managed/wrapped/esdt_token_payment.rs index b0a62679b8..80d4cc0858 100644 --- a/framework/base/src/types/managed/wrapped/esdt_token_payment.rs +++ b/framework/base/src/types/managed/wrapped/esdt_token_payment.rs @@ -221,12 +221,6 @@ impl ManagedVecItem for EsdtTokenPayment { Self::read_from_payload(payload) } - unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( - reader: Reader, - ) -> Self::Ref<'a> { - Self::from_byte_reader(reader) - } - fn into_byte_writer R>(self, mut writer: Writer) -> R { let mut arr: [u8; 16] = [0u8; 16]; let mut index = 0; diff --git a/framework/base/src/types/managed/wrapped/managed_option.rs b/framework/base/src/types/managed/wrapped/managed_option.rs index add7ae2669..03af276bc9 100644 --- a/framework/base/src/types/managed/wrapped/managed_option.rs +++ b/framework/base/src/types/managed/wrapped/managed_option.rs @@ -218,11 +218,7 @@ where Self::read_from_payload(payload) } - unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( - reader: Reader, - ) -> Self::Ref<'a> { - Self::from_byte_reader(reader) - } + fn into_byte_writer R>(self, writer: Writer) -> R { ::into_byte_writer(self.handle, writer) diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item.rs b/framework/base/src/types/managed/wrapped/managed_vec_item.rs index 512e2d847d..c9b59721f2 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item.rs @@ -53,15 +53,6 @@ pub trait ManagedVecItem: 'static { /// In certain cases this involves practically disregarding the lifetimes, hence it is unsafe. unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a>; - /// Parses given bytes as a representation of the object, either owned, or a reference. - /// - /// # Safety - /// - /// In certain cases this involves practically disregarding the lifetimes, hence it is unsafe. - unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( - reader: Reader, - ) -> Self::Ref<'a>; - /// Converts the object into bytes. /// /// The output is processed by the `writer` lambda. @@ -105,12 +96,6 @@ macro_rules! impl_int { $ty::from_be_bytes(payload.buffer) } - unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( - reader: Reader, - ) -> Self::Ref<'a> { - Self::from_byte_reader(reader) - } - fn into_byte_writer R>(self, mut writer: Writer) -> R { let bytes = self.to_be_bytes(); writer(&bytes) @@ -144,12 +129,6 @@ impl ManagedVecItem for usize { Self::read_from_payload(payload) } - unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( - reader: Reader, - ) -> Self::Ref<'a> { - Self::from_byte_reader(reader) - } - fn into_byte_writer R>(self, mut writer: Writer) -> R { let bytes = (self as u32).to_be_bytes(); writer(&bytes) @@ -173,12 +152,6 @@ impl ManagedVecItem for bool { Self::read_from_payload(payload) } - unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( - reader: Reader, - ) -> Self::Ref<'a> { - Self::from_byte_reader(reader) - } - fn into_byte_writer R>(self, writer: Writer) -> R { // true -> 1u8 // false -> 0u8 @@ -226,12 +199,6 @@ where Self::read_from_payload(payload) } - unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( - reader: Reader, - ) -> Self::Ref<'a> { - Self::from_byte_reader(reader) - } - fn into_byte_writer R>(self, mut writer: Writer) -> R { let mut payload = Self::PAYLOAD::new_buffer(); let slice = payload.payload_slice_mut(); @@ -267,13 +234,6 @@ macro_rules! impl_managed_type { ManagedRef::wrap_handle(handle) } - unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( - reader: Reader, - ) -> Self::Ref<'a> { - let handle = <$ty as ManagedType>::OwnHandle::from_byte_reader(reader); - ManagedRef::wrap_handle(handle) - } - fn into_byte_writer R>(self, writer: Writer) -> R { let handle = unsafe { self.forget_into_handle() }; <$ty as ManagedType>::OwnHandle::into_byte_writer(handle, writer) @@ -312,13 +272,6 @@ where ManagedRef::wrap_handle(handle) } - unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( - reader: Reader, - ) -> Self::Ref<'a> { - let handle = >::OwnHandle::from_byte_reader(reader); - ManagedRef::wrap_handle(handle) - } - fn into_byte_writer R>(self, writer: Writer) -> R { <>::OwnHandle as ManagedVecItem>::into_byte_writer( self.get_handle(), @@ -351,13 +304,6 @@ where ManagedRef::wrap_handle(handle) } - unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( - reader: Reader, - ) -> Self::Ref<'a> { - let handle = M::ManagedBufferHandle::from_byte_reader(reader); - ManagedRef::wrap_handle(handle) - } - fn into_byte_writer R>(self, writer: Writer) -> R { let handle = unsafe { self.forget_into_handle() }; ::into_byte_writer(handle, writer) @@ -383,12 +329,6 @@ impl ManagedVecItem for EsdtTokenType { Self::read_from_payload(payload) } - unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( - reader: Reader, - ) -> Self::Ref<'a> { - Self::from_byte_reader(reader) - } - fn into_byte_writer R>(self, mut writer: Writer) -> R { writer(&[self.as_u8()]) } @@ -411,12 +351,6 @@ impl ManagedVecItem for EsdtLocalRole { Self::read_from_payload(payload) } - unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( - reader: Reader, - ) -> Self::Ref<'a> { - Self::from_byte_reader(reader) - } - fn into_byte_writer R>(self, writer: Writer) -> R { ::into_byte_writer(self.as_u16(), writer) } diff --git a/framework/derive/src/managed_vec_item_derive.rs b/framework/derive/src/managed_vec_item_derive.rs index 1b1d41fe69..c45d5550c2 100644 --- a/framework/derive/src/managed_vec_item_derive.rs +++ b/framework/derive/src/managed_vec_item_derive.rs @@ -162,10 +162,6 @@ fn enum_derive(data_enum: &syn::DataEnum, ast: &syn::DeriveInput) -> TokenStream Self::read_from_payload(payload) } - unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>(reader: Reader) -> Self::Ref<'a> { - Self::from_byte_reader(reader) - } - fn into_byte_writer R>(self, mut writer: Writer) -> R { let mut arr: [u8; 1] = [0u8; 1]; arr[0] = match self { @@ -221,10 +217,6 @@ fn struct_derive(data_struct: &syn::DataStruct, ast: &syn::DeriveInput) -> Token Self::read_from_payload(payload) } - unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>(reader: Reader) -> Self::Ref<'a> { - Self::from_byte_reader(reader) - } - fn into_byte_writer R>(self, mut writer: Writer) -> R { #payload_buffer_snippet let mut index = 0; diff --git a/framework/scenario/src/api/impl_vh/debug_handle_vh.rs b/framework/scenario/src/api/impl_vh/debug_handle_vh.rs index 4a6d38b212..8193c36331 100644 --- a/framework/scenario/src/api/impl_vh/debug_handle_vh.rs +++ b/framework/scenario/src/api/impl_vh/debug_handle_vh.rs @@ -95,12 +95,6 @@ impl ManagedVecItem for DebugHandle { Self::read_from_payload(payload) } - unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( - reader: Reader, - ) -> Self::Ref<'a> { - use_raw_handle(RawHandle::from_byte_reader(reader)) - } - fn into_byte_writer R>(self, writer: Writer) -> R { RawHandle::into_byte_writer(self.get_raw_handle(), writer) } From 348f046823ecf035390cf488249d9ace87f150a4 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 27 Nov 2024 02:39:41 +0200 Subject: [PATCH 28/66] ManagedVecItem payload refactor - remove read_from_payload --- .../esdt_token_payment_multi_value.rs | 4 -- .../wrapped/encoded_managed_vec_item.rs | 6 +- .../managed/wrapped/esdt_token_payment.rs | 27 --------- .../types/managed/wrapped/managed_option.rs | 7 --- .../src/types/managed/wrapped/managed_vec.rs | 16 ++++-- .../types/managed/wrapped/managed_vec_item.rs | 56 ------------------- .../wrapped/managed_vec_item_payload.rs | 6 ++ .../derive/src/managed_vec_item_derive.rs | 42 -------------- .../src/api/impl_vh/debug_handle_vh.rs | 4 -- .../derive_managed_vec_item_biguint_test.rs | 10 +--- ...anaged_vec_item_esdt_token_payment_test.rs | 10 +--- .../derive_managed_vec_item_simple_enum.rs | 5 +- .../derive_managed_vec_item_struct_1_test.rs | 6 +- .../derive_managed_vec_item_struct_2_test.rs | 6 +- 14 files changed, 26 insertions(+), 179 deletions(-) diff --git a/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs b/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs index 299bc8bfc2..7479df9381 100644 --- a/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs +++ b/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs @@ -44,10 +44,6 @@ impl ManagedVecItem for EsdtTokenPaymentMultiValue { const SKIPS_RESERIALIZATION: bool = EsdtTokenPayment::::SKIPS_RESERIALIZATION; type Ref<'a> = Self; - fn from_byte_reader(reader: Reader) -> Self { - EsdtTokenPayment::from_byte_reader(reader).into() - } - fn read_from_payload(payload: &Self::PAYLOAD) -> Self { EsdtTokenPayment::read_from_payload(payload).into() } diff --git a/framework/base/src/types/managed/wrapped/encoded_managed_vec_item.rs b/framework/base/src/types/managed/wrapped/encoded_managed_vec_item.rs index fd7af7c51f..205a219a2c 100644 --- a/framework/base/src/types/managed/wrapped/encoded_managed_vec_item.rs +++ b/framework/base/src/types/managed/wrapped/encoded_managed_vec_item.rs @@ -1,4 +1,4 @@ -use super::{ManagedVecItem, ManagedVecItemPayload}; +use super::ManagedVecItem; use core::{cmp::Ordering, marker::PhantomData}; pub struct EncodedManagedVecItem @@ -14,9 +14,7 @@ where T: ManagedVecItem, { pub(crate) fn decode(&self) -> T { - T::from_byte_reader(|item_bytes| { - item_bytes.copy_from_slice(self.encoded.payload_slice()); - }) + T::read_from_payload(&self.encoded) } } diff --git a/framework/base/src/types/managed/wrapped/esdt_token_payment.rs b/framework/base/src/types/managed/wrapped/esdt_token_payment.rs index 80d4cc0858..836d6d642c 100644 --- a/framework/base/src/types/managed/wrapped/esdt_token_payment.rs +++ b/framework/base/src/types/managed/wrapped/esdt_token_payment.rs @@ -153,17 +153,6 @@ impl EsdtTokenPayment { } } -fn managed_vec_item_from_slice(arr: &[u8], index: &mut usize) -> T -where - T: ManagedVecItem, -{ - ManagedVecItem::from_byte_reader(|bytes| { - let size = T::payload_size(); - bytes.copy_from_slice(&arr[*index..*index + size]); - *index += size; - }) -} - fn managed_vec_item_to_slice(arr: &mut [u8], index: &mut usize, item: T) where T: ManagedVecItem, @@ -189,22 +178,6 @@ impl ManagedVecItem for EsdtTokenPayment { const SKIPS_RESERIALIZATION: bool = false; type Ref<'a> = Self; - fn from_byte_reader(mut reader: Reader) -> Self { - let mut arr: [u8; 16] = [0u8; 16]; - reader(&mut arr[..]); - let mut index = 0; - - let token_identifier = managed_vec_item_from_slice(&arr, &mut index); - let token_nonce = managed_vec_item_from_slice(&arr, &mut index); - let amount = managed_vec_item_from_slice(&arr, &mut index); - - EsdtTokenPayment { - token_identifier, - token_nonce, - amount, - } - } - fn read_from_payload(payload: &Self::PAYLOAD) -> Self { let mut index = 0; unsafe { diff --git a/framework/base/src/types/managed/wrapped/managed_option.rs b/framework/base/src/types/managed/wrapped/managed_option.rs index 03af276bc9..b29ab033d1 100644 --- a/framework/base/src/types/managed/wrapped/managed_option.rs +++ b/framework/base/src/types/managed/wrapped/managed_option.rs @@ -203,11 +203,6 @@ where const SKIPS_RESERIALIZATION: bool = false; type Ref<'a> = Self; - fn from_byte_reader(reader: Reader) -> Self { - let handle = T::OwnHandle::from_byte_reader(reader); - Self::new_with_handle(handle) - } - fn read_from_payload(payload: &Self::PAYLOAD) -> Self { let handle = use_raw_handle(i32::read_from_payload(payload)); Self::new_with_handle(handle) @@ -218,8 +213,6 @@ where Self::read_from_payload(payload) } - - fn into_byte_writer R>(self, writer: Writer) -> R { ::into_byte_writer(self.handle, writer) } diff --git a/framework/base/src/types/managed/wrapped/managed_vec.rs b/framework/base/src/types/managed/wrapped/managed_vec.rs index de84d66904..48cbaffc05 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec.rs @@ -527,13 +527,17 @@ where return false; } let mut byte_index = 0; + let mut self_payload = T::PAYLOAD::new_buffer(); + let mut other_payload = T::PAYLOAD::new_buffer(); while byte_index < self_len { - let self_item = T::from_byte_reader(|dest_slice| { - let _ = self.buffer.load_slice(byte_index, dest_slice); - }); - let other_item = T::from_byte_reader(|dest_slice| { - let _ = other.buffer.load_slice(byte_index, dest_slice); - }); + let _ = self + .buffer + .load_slice(byte_index, self_payload.payload_slice_mut()); + let _ = self + .buffer + .load_slice(byte_index, other_payload.payload_slice_mut()); + let self_item = T::read_from_payload(&self_payload); + let other_item = T::read_from_payload(&other_payload); if self_item != other_item { return false; } diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item.rs b/framework/base/src/types/managed/wrapped/managed_vec_item.rs index c9b59721f2..60ffc83f5c 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item.rs @@ -42,8 +42,6 @@ pub trait ManagedVecItem: 'static { } /// Parses given bytes as a an owned object. - fn from_byte_reader(reader: Reader) -> Self; - fn read_from_payload(payload: &Self::PAYLOAD) -> Self; /// Parses given bytes as a representation of the object, either owned, or a reference. @@ -82,12 +80,6 @@ macro_rules! impl_int { const SKIPS_RESERIALIZATION: bool = true; type Ref<'a> = Self; - fn from_byte_reader(mut reader: Reader) -> Self { - let mut arr: [u8; $payload_size] = [0u8; $payload_size]; - reader(&mut arr[..]); - $ty::from_be_bytes(arr) - } - fn read_from_payload(payload: &Self::PAYLOAD) -> Self { $ty::from_be_bytes(payload.buffer) } @@ -115,12 +107,6 @@ impl ManagedVecItem for usize { const SKIPS_RESERIALIZATION: bool = true; type Ref<'a> = Self; - fn from_byte_reader(mut reader: Reader) -> Self { - let mut arr: [u8; 4] = [0u8; 4]; - reader(&mut arr[..]); - u32::from_be_bytes(arr) as usize - } - fn read_from_payload(payload: &Self::PAYLOAD) -> Self { u32::read_from_payload(payload) as usize } @@ -140,10 +126,6 @@ impl ManagedVecItem for bool { const SKIPS_RESERIALIZATION: bool = true; type Ref<'a> = Self; - fn from_byte_reader(reader: Reader) -> Self { - u8::from_byte_reader(reader) > 0 - } - fn read_from_payload(payload: &Self::PAYLOAD) -> Self { u8::read_from_payload(payload) > 0 } @@ -169,19 +151,6 @@ where const SKIPS_RESERIALIZATION: bool = false; type Ref<'a> = Self; - fn from_byte_reader(mut reader: Reader) -> Self { - let mut payload = Self::PAYLOAD::new_buffer(); - let payload_slice = payload.payload_slice_mut(); - reader(payload_slice); - if payload_slice[0] == 0 { - None - } else { - Some(T::from_byte_reader(|bytes| { - bytes.copy_from_slice(&payload_slice[1..]); - })) - } - } - fn read_from_payload(payload: &Self::PAYLOAD) -> Self { let (p1, p2) = as ManagedVecItemPayloadAdd< T::PAYLOAD, @@ -219,11 +188,6 @@ macro_rules! impl_managed_type { const SKIPS_RESERIALIZATION: bool = false; type Ref<'a> = ManagedRef<'a, M, Self>; - fn from_byte_reader(reader: Reader) -> Self { - let handle = <$ty as ManagedType>::OwnHandle::from_byte_reader(reader); - unsafe { $ty::from_handle(handle) } - } - fn read_from_payload(payload: &Self::PAYLOAD) -> Self { let handle = use_raw_handle(i32::read_from_payload(payload)); unsafe { Self::from_handle(handle) } @@ -257,11 +221,6 @@ where const SKIPS_RESERIALIZATION: bool = false; type Ref<'a> = ManagedRef<'a, M, Self>; - fn from_byte_reader(reader: Reader) -> Self { - let handle = >::OwnHandle::from_byte_reader(reader); - unsafe { Self::from_handle(handle) } - } - fn read_from_payload(payload: &Self::PAYLOAD) -> Self { let handle = use_raw_handle(i32::read_from_payload(payload)); unsafe { Self::from_handle(handle) } @@ -289,11 +248,6 @@ where const SKIPS_RESERIALIZATION: bool = false; type Ref<'a> = ManagedRef<'a, M, Self>; - fn from_byte_reader(reader: Reader) -> Self { - let handle = M::ManagedBufferHandle::from_byte_reader(reader); - unsafe { Self::from_handle(handle) } - } - fn read_from_payload(payload: &Self::PAYLOAD) -> Self { let handle = use_raw_handle(i32::read_from_payload(payload)); unsafe { Self::from_handle(handle) } @@ -315,12 +269,6 @@ impl ManagedVecItem for EsdtTokenType { const SKIPS_RESERIALIZATION: bool = true; type Ref<'a> = Self; - fn from_byte_reader(mut reader: Reader) -> Self { - let mut arr: [u8; 1] = [0u8; 1]; - reader(&mut arr[..]); - arr[0].into() - } - fn read_from_payload(payload: &Self::PAYLOAD) -> Self { u8::read_from_payload(payload).into() } @@ -339,10 +287,6 @@ impl ManagedVecItem for EsdtLocalRole { const SKIPS_RESERIALIZATION: bool = false; // TODO: might be ok to be true, but needs testing type Ref<'a> = Self; - fn from_byte_reader(reader: Reader) -> Self { - u16::from_byte_reader(reader).into() - } - fn read_from_payload(payload: &Self::PAYLOAD) -> Self { u16::read_from_payload(payload).into() } diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs b/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs index 4d336632cf..9b22da64f6 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs @@ -50,6 +50,12 @@ pub struct ManagedVecItemPayloadBuffer { pub buffer: [u8; N], } +impl From<[u8; N]> for ManagedVecItemPayloadBuffer { + fn from(value: [u8; N]) -> Self { + ManagedVecItemPayloadBuffer { buffer: value } + } +} + impl ManagedVecItemPayload for ManagedVecItemPayloadBuffer { fn new_buffer() -> Self { ManagedVecItemPayloadBuffer { buffer: [0u8; N] } diff --git a/framework/derive/src/managed_vec_item_derive.rs b/framework/derive/src/managed_vec_item_derive.rs index c45d5550c2..028c833168 100644 --- a/framework/derive/src/managed_vec_item_derive.rs +++ b/framework/derive/src/managed_vec_item_derive.rs @@ -43,29 +43,6 @@ fn generate_skips_reserialization_snippets(fields: &syn::Fields) -> Vec Vec { - match fields { - syn::Fields::Named(fields_named) => fields_named - .named - .iter() - .map(|field| { - let field_ident = &field.ident; - let type_name = &field.ty; - quote! { - #field_ident: multiversx_sc::types::ManagedVecItem::from_byte_reader(|bytes| { - let next_index = index + <#type_name as multiversx_sc::types::ManagedVecItem>::payload_size(); - bytes.copy_from_slice(&payload_slice[index .. next_index]); - index = next_index; - }), - } - }) - .collect(), - _ => { - panic!("ManagedVecItem only supports named fields") - } - } -} - fn generate_read_from_payload_snippets(fields: &syn::Fields) -> Vec { match fields { syn::Fields::Named(fields_named) => fields_named @@ -143,14 +120,6 @@ fn enum_derive(data_enum: &syn::DataEnum, ast: &syn::DeriveInput) -> TokenStream const SKIPS_RESERIALIZATION: bool = true; type Ref<'a> = Self; - fn from_byte_reader(mut reader: Reader) -> Self { - let mut arr: [u8; 1] = [0u8; 1]; - reader(&mut arr[..]); - match arr[0] { - #(#reader_match_arms)* - } - } - fn read_from_payload(payload: &Self::PAYLOAD) -> Self { let discriminant = ::read_from_payload(payload); match discriminant { @@ -180,7 +149,6 @@ fn struct_derive(data_struct: &syn::DataStruct, ast: &syn::DeriveInput) -> Token let payload_nested_tuple = generate_payload_nested_tuple(&data_struct.fields); let skips_reserialization_snippets = generate_skips_reserialization_snippets(&data_struct.fields); - let from_byte_reader_snippets = generate_from_byte_reader_snippets(&data_struct.fields); let read_from_payload_snippets = generate_read_from_payload_snippets(&data_struct.fields); let to_byte_writer_snippets = generate_to_byte_writer_snippets(&data_struct.fields); @@ -192,16 +160,6 @@ fn struct_derive(data_struct: &syn::DataStruct, ast: &syn::DeriveInput) -> Token const SKIPS_RESERIALIZATION: bool = #(#skips_reserialization_snippets)&&*; type Ref<'a> = Self; - fn from_byte_reader(mut reader: Reader) -> Self { - #payload_buffer_snippet - reader(payload_slice); - let mut index = 0; - - #name { - #(#from_byte_reader_snippets)* - } - } - fn read_from_payload(payload: &Self::PAYLOAD) -> Self { let mut index = 0; diff --git a/framework/scenario/src/api/impl_vh/debug_handle_vh.rs b/framework/scenario/src/api/impl_vh/debug_handle_vh.rs index 8193c36331..3a21758e6a 100644 --- a/framework/scenario/src/api/impl_vh/debug_handle_vh.rs +++ b/framework/scenario/src/api/impl_vh/debug_handle_vh.rs @@ -83,10 +83,6 @@ impl ManagedVecItem for DebugHandle { type Ref<'a> = Self; - fn from_byte_reader(reader: Reader) -> Self { - use_raw_handle(RawHandle::from_byte_reader(reader)) - } - fn read_from_payload(payload: &Self::PAYLOAD) -> Self { use_raw_handle(RawHandle::read_from_payload(payload)) } diff --git a/framework/scenario/tests/derive_managed_vec_item_biguint_test.rs b/framework/scenario/tests/derive_managed_vec_item_biguint_test.rs index 6efd920acc..9cbf9ec1b9 100644 --- a/framework/scenario/tests/derive_managed_vec_item_biguint_test.rs +++ b/framework/scenario/tests/derive_managed_vec_item_biguint_test.rs @@ -65,14 +65,8 @@ fn managed_struct_from_bytes_reader() { let arr: [u8; 8] = [0xff, 0xff, 0xff, handle_bytes[3], 0x00, 0x01, 0x23, 0x45]; let struct_from_bytes = - as multiversx_sc::types::ManagedVecItem>::from_byte_reader( - |bytes| { - bytes.copy_from_slice( - &arr - [0 - .. as multiversx_sc::types::ManagedVecItem>::payload_size()], - ); - }, + as multiversx_sc::types::ManagedVecItem>::read_from_payload( + &arr.into() ); assert_eq!(s, struct_from_bytes); } diff --git a/framework/scenario/tests/derive_managed_vec_item_esdt_token_payment_test.rs b/framework/scenario/tests/derive_managed_vec_item_esdt_token_payment_test.rs index 1018747da3..84dfd54e32 100644 --- a/framework/scenario/tests/derive_managed_vec_item_esdt_token_payment_test.rs +++ b/framework/scenario/tests/derive_managed_vec_item_esdt_token_payment_test.rs @@ -93,14 +93,8 @@ fn struct_from_bytes_reader() { ]; let struct_from_bytes = - as multiversx_sc::types::ManagedVecItem>::from_byte_reader( - |bytes| { - bytes.copy_from_slice( - &arr - [0 - .. as multiversx_sc::types::ManagedVecItem>::payload_size()], - ); - }, + as multiversx_sc::types::ManagedVecItem>::read_from_payload( + &arr.into() ); assert_eq!(s, struct_from_bytes); diff --git a/framework/scenario/tests/derive_managed_vec_item_simple_enum.rs b/framework/scenario/tests/derive_managed_vec_item_simple_enum.rs index 46dcceed33..11719b6872 100644 --- a/framework/scenario/tests/derive_managed_vec_item_simple_enum.rs +++ b/framework/scenario/tests/derive_managed_vec_item_simple_enum.rs @@ -39,9 +39,6 @@ fn enum_to_bytes_writer() { #[test] fn enum_from_bytes_reader() { let enum_from_bytes = - ::from_byte_reader(|bytes| { - assert_eq!(bytes.len(), 1); - bytes[0] = 1; - }); + ::read_from_payload(&[1u8].into()); assert_eq!(enum_from_bytes, SimpleEnum::Variant2); } diff --git a/framework/scenario/tests/derive_managed_vec_item_struct_1_test.rs b/framework/scenario/tests/derive_managed_vec_item_struct_1_test.rs index e55e6d9140..49b43b8711 100644 --- a/framework/scenario/tests/derive_managed_vec_item_struct_1_test.rs +++ b/framework/scenario/tests/derive_managed_vec_item_struct_1_test.rs @@ -94,10 +94,6 @@ fn struct_1_from_bytes_reader() { ]; let struct_from_bytes = - ::from_byte_reader(|bytes| { - bytes.copy_from_slice( - &arr[0..::payload_size()], - ); - }); + ::read_from_payload(&arr.into()); assert_eq!(s, struct_from_bytes); } diff --git a/framework/scenario/tests/derive_managed_vec_item_struct_2_test.rs b/framework/scenario/tests/derive_managed_vec_item_struct_2_test.rs index 50bea73aac..f188f1f64d 100644 --- a/framework/scenario/tests/derive_managed_vec_item_struct_2_test.rs +++ b/framework/scenario/tests/derive_managed_vec_item_struct_2_test.rs @@ -67,7 +67,7 @@ fn struct_2_from_bytes_reader() { }; #[rustfmt::skip] - let payload = &[ + let payload = [ /* u_8 */ 0x01, /* u_16 */ 0x00, 0x02, /* u_32 */ 0x00, 0x00, 0x00, 0x03, @@ -78,8 +78,6 @@ fn struct_2_from_bytes_reader() { ]; let struct_from_bytes = - ::from_byte_reader(|bytes| { - bytes.copy_from_slice(&payload[..]); - }); + ::read_from_payload(&payload.into()); assert_eq!(expected_struct, struct_from_bytes); } From 1db9f8e15963ea864e3fb35eb02d32222d00a947 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 27 Nov 2024 02:42:39 +0200 Subject: [PATCH 29/66] clippy fix --- framework/base/src/types/managed/wrapped/managed_vec_item.rs | 5 +++++ .../src/types/managed/wrapped/managed_vec_item_payload.rs | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item.rs b/framework/base/src/types/managed/wrapped/managed_vec_item.rs index 60ffc83f5c..a920cafadb 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item.rs @@ -63,6 +63,11 @@ pub trait ManagedVecItem: 'static { fn into_byte_writer R>(self, writer: Writer) -> R; } +/// Used by the ManagedVecItem derive. +/// +/// ## Safety +/// +/// Only works correctly if the given index is correct, otherwise undefined behavior is possible. pub unsafe fn managed_vec_item_read_from_payload_index(payload: &P, index: &mut usize) -> T where T: ManagedVecItem, diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs b/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs index 9b22da64f6..f8ba1b17cc 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs @@ -14,6 +14,11 @@ pub trait ManagedVecItemPayload { fn payload_slice_mut(&mut self) -> &mut [u8]; + /// Takes a sub-payload item. + /// + /// ## Safety + /// + /// Only works correctly if the given index is correct, otherwise undefined behavior is possible. unsafe fn slice_unchecked(&self, index: usize) -> &S; } From 91011d075534db269ffedfd72bfa3fe43507f27f Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 27 Nov 2024 03:42:01 +0200 Subject: [PATCH 30/66] ManagedVecItem payload refactor - eq fix --- framework/base/src/types/managed/wrapped/managed_vec.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/base/src/types/managed/wrapped/managed_vec.rs b/framework/base/src/types/managed/wrapped/managed_vec.rs index 48cbaffc05..36d965f55b 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec.rs @@ -533,7 +533,7 @@ where let _ = self .buffer .load_slice(byte_index, self_payload.payload_slice_mut()); - let _ = self + let _ = other .buffer .load_slice(byte_index, other_payload.payload_slice_mut()); let self_item = T::read_from_payload(&self_payload); From a886acceb3271302483d1036ad69b083eee81fe9 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 27 Nov 2024 03:56:50 +0200 Subject: [PATCH 31/66] ManagedVecItem payload refactor - save_to_payload impl --- .../esdt_token_payment_multi_value.rs | 4 ++ .../managed/wrapped/esdt_token_payment.rs | 15 ++++- .../types/managed/wrapped/managed_option.rs | 5 ++ .../src/types/managed/wrapped/managed_vec.rs | 4 +- .../types/managed/wrapped/managed_vec_item.rs | 67 ++++++++++++++++++- .../wrapped/managed_vec_item_payload.rs | 54 +++++++++++++++ .../base/src/types/managed/wrapped/mod.rs | 5 +- .../derive/src/managed_vec_item_derive.rs | 33 +++++++++ .../src/api/impl_vh/debug_handle_vh.rs | 4 ++ .../derive_managed_vec_item_biguint_test.rs | 19 +++--- ...anaged_vec_item_esdt_token_payment_test.rs | 19 +++--- .../derive_managed_vec_item_simple_enum.rs | 17 +++-- .../derive_managed_vec_item_struct_1_test.rs | 20 +++--- .../derive_managed_vec_item_struct_2_test.rs | 8 ++- 14 files changed, 225 insertions(+), 49 deletions(-) diff --git a/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs b/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs index 7479df9381..1fd9c5ef2b 100644 --- a/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs +++ b/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs @@ -57,6 +57,10 @@ impl ManagedVecItem for EsdtTokenPaymentMultiValue { fn into_byte_writer R>(self, writer: Writer) -> R { self.obj.into_byte_writer(writer) } + + fn save_to_payload(self, payload: &mut Self::PAYLOAD) { + self.obj.save_to_payload(payload); + } } impl TopEncodeMulti for EsdtTokenPaymentMultiValue diff --git a/framework/base/src/types/managed/wrapped/esdt_token_payment.rs b/framework/base/src/types/managed/wrapped/esdt_token_payment.rs index 836d6d642c..435a6e223e 100644 --- a/framework/base/src/types/managed/wrapped/esdt_token_payment.rs +++ b/framework/base/src/types/managed/wrapped/esdt_token_payment.rs @@ -13,7 +13,10 @@ use crate::{ derive::type_abi, }; -use super::{managed_vec_item_read_from_payload_index, ManagedVec, ManagedVecItemPayloadBuffer}; +use super::{ + managed_vec_item_read_from_payload_index, managed_vec_item_save_to_payload_index, ManagedVec, + ManagedVecItemPayloadBuffer, +}; #[type_abi] #[derive(TopEncode, NestedEncode, Clone, PartialEq, Eq, Debug)] @@ -204,6 +207,16 @@ impl ManagedVecItem for EsdtTokenPayment { writer(&arr[..]) } + + fn save_to_payload(self, payload: &mut Self::PAYLOAD) { + let mut index = 0; + + unsafe { + managed_vec_item_save_to_payload_index(self.token_identifier, payload, &mut index); + managed_vec_item_save_to_payload_index(self.token_nonce, payload, &mut index); + managed_vec_item_save_to_payload_index(self.amount, payload, &mut index); + } + } } /// The version of `EsdtTokenPayment` that contains referrences instead of owned fields. diff --git a/framework/base/src/types/managed/wrapped/managed_option.rs b/framework/base/src/types/managed/wrapped/managed_option.rs index b29ab033d1..9616696469 100644 --- a/framework/base/src/types/managed/wrapped/managed_option.rs +++ b/framework/base/src/types/managed/wrapped/managed_option.rs @@ -2,6 +2,7 @@ use core::marker::PhantomData; use crate::{ abi::TypeAbiFrom, + api::HandleConstraints, codec::{ DecodeErrorHandler, EncodeErrorHandler, NestedDecode, NestedDecodeInput, NestedEncode, NestedEncodeOutput, TopDecode, TopDecodeInput, TopEncode, TopEncodeOutput, @@ -216,6 +217,10 @@ where fn into_byte_writer R>(self, writer: Writer) -> R { ::into_byte_writer(self.handle, writer) } + + fn save_to_payload(self, payload: &mut Self::PAYLOAD) { + self.handle.get_raw_handle().save_to_payload(payload); + } } impl TopEncode for ManagedOption diff --git a/framework/base/src/types/managed/wrapped/managed_vec.rs b/framework/base/src/types/managed/wrapped/managed_vec.rs index 36d965f55b..762f2757f6 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec.rs @@ -527,12 +527,12 @@ where return false; } let mut byte_index = 0; - let mut self_payload = T::PAYLOAD::new_buffer(); - let mut other_payload = T::PAYLOAD::new_buffer(); while byte_index < self_len { + let mut self_payload = T::PAYLOAD::new_buffer(); let _ = self .buffer .load_slice(byte_index, self_payload.payload_slice_mut()); + let mut other_payload = T::PAYLOAD::new_buffer(); let _ = other .buffer .load_slice(byte_index, other_payload.payload_slice_mut()); diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item.rs b/framework/base/src/types/managed/wrapped/managed_vec_item.rs index a920cafadb..bd4b311ca2 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item.rs @@ -3,7 +3,7 @@ use core::borrow::Borrow; use multiversx_chain_core::types::{EsdtLocalRole, EsdtTokenType}; use crate::{ - api::{use_raw_handle, ManagedTypeApi}, + api::{use_raw_handle, HandleConstraints, ManagedTypeApi}, types::{ BigInt, BigUint, EllipticCurve, ManagedAddress, ManagedBuffer, ManagedByteArray, ManagedRef, ManagedType, ManagedVec, TokenIdentifier, @@ -61,6 +61,8 @@ pub trait ManagedVecItem: 'static { /// /// Note that a destructor should not be called at this moment, since the ManagedVec will take ownership of the item. fn into_byte_writer R>(self, writer: Writer) -> R; + + fn save_to_payload(self, payload: &mut Self::PAYLOAD); } /// Used by the ManagedVecItem derive. @@ -78,6 +80,23 @@ where value } +/// Used by the ManagedVecItem derive. +/// +/// ## Safety +/// +/// Only works correctly if the given index is correct, otherwise undefined behavior is possible. +pub unsafe fn managed_vec_item_save_to_payload_index( + item: T, + payload: &mut P, + index: &mut usize, +) where + T: ManagedVecItem, + P: ManagedVecItemPayload, +{ + item.save_to_payload(payload.slice_unchecked_mut(*index)); + *index += T::PAYLOAD::payload_size(); +} + macro_rules! impl_int { ($ty:ident, $payload_size:expr) => { impl ManagedVecItem for $ty { @@ -97,6 +116,10 @@ macro_rules! impl_int { let bytes = self.to_be_bytes(); writer(&bytes) } + + fn save_to_payload(self, payload: &mut Self::PAYLOAD) { + payload.buffer = self.to_be_bytes(); + } } }; } @@ -124,6 +147,10 @@ impl ManagedVecItem for usize { let bytes = (self as u32).to_be_bytes(); writer(&bytes) } + + fn save_to_payload(self, payload: &mut Self::PAYLOAD) { + (self as u32).save_to_payload(payload); + } } impl ManagedVecItem for bool { @@ -145,6 +172,10 @@ impl ManagedVecItem for bool { let u8_value = u8::from(self); ::into_byte_writer(u8_value, writer) } + + fn save_to_payload(self, payload: &mut Self::PAYLOAD) { + u8::from(self).save_to_payload(payload); + } } impl ManagedVecItem for Option @@ -184,6 +215,17 @@ where } writer(slice) } + + fn save_to_payload(self, payload: &mut Self::PAYLOAD) { + let (p1, p2) = as ManagedVecItemPayloadAdd< + T::PAYLOAD, + >>::split_mut_from_add(payload); + + if let Some(t) = self { + 1u8.save_to_payload(p1); + t.save_to_payload(p2); + } + } } macro_rules! impl_managed_type { @@ -207,6 +249,11 @@ macro_rules! impl_managed_type { let handle = unsafe { self.forget_into_handle() }; <$ty as ManagedType>::OwnHandle::into_byte_writer(handle, writer) } + + fn save_to_payload(self, payload: &mut Self::PAYLOAD) { + let handle = unsafe { self.forget_into_handle() }; + handle.get_raw_handle().save_to_payload(payload); + } } }; } @@ -242,6 +289,11 @@ where writer, ) } + + fn save_to_payload(self, payload: &mut Self::PAYLOAD) { + let handle = unsafe { self.forget_into_handle() }; + handle.get_raw_handle().save_to_payload(payload); + } } impl ManagedVecItem for ManagedVec @@ -267,6 +319,11 @@ where let handle = unsafe { self.forget_into_handle() }; ::into_byte_writer(handle, writer) } + + fn save_to_payload(self, payload: &mut Self::PAYLOAD) { + let handle = unsafe { self.forget_into_handle() }; + handle.get_raw_handle().save_to_payload(payload); + } } impl ManagedVecItem for EsdtTokenType { @@ -285,6 +342,10 @@ impl ManagedVecItem for EsdtTokenType { fn into_byte_writer R>(self, mut writer: Writer) -> R { writer(&[self.as_u8()]) } + + fn save_to_payload(self, payload: &mut Self::PAYLOAD) { + self.as_u8().save_to_payload(payload); + } } impl ManagedVecItem for EsdtLocalRole { @@ -303,4 +364,8 @@ impl ManagedVecItem for EsdtLocalRole { fn into_byte_writer R>(self, writer: Writer) -> R { ::into_byte_writer(self.as_u16(), writer) } + + fn save_to_payload(self, payload: &mut Self::PAYLOAD) { + self.as_u16().save_to_payload(payload); + } } diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs b/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs index f8ba1b17cc..e13d878a94 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs @@ -20,6 +20,13 @@ pub trait ManagedVecItemPayload { /// /// Only works correctly if the given index is correct, otherwise undefined behavior is possible. unsafe fn slice_unchecked(&self, index: usize) -> &S; + + /// Takes a sub-payload item. + /// + /// ## Safety + /// + /// Only works correctly if the given index is correct, otherwise undefined behavior is possible. + unsafe fn slice_unchecked_mut(&mut self, index: usize) -> &mut S; } /// Empty ManagedVecItem. @@ -47,6 +54,10 @@ impl ManagedVecItemPayload for ManagedVecItemEmptyPayload { unsafe fn slice_unchecked(&self, _index: usize) -> &S { unimplemented!() } + + unsafe fn slice_unchecked_mut(&mut self, _index: usize) -> &mut S { + unimplemented!() + } } /// The main ManagedVecItemPayload implementation. Uses an array in its implementation. @@ -82,6 +93,11 @@ impl ManagedVecItemPayload for ManagedVecItemPayloadBuffer { let ptr = self.buffer.as_ptr().add(index); &*ptr.cast::() } + + unsafe fn slice_unchecked_mut(&mut self, index: usize) -> &mut S { + let ptr = self.buffer.as_mut_ptr().add(index); + &mut *ptr.cast::() + } } #[repr(transparent)] @@ -125,6 +141,10 @@ where unsafe fn slice_unchecked(&self, index: usize) -> &S { self.buffer.slice_unchecked(index) } + + unsafe fn slice_unchecked_mut(&mut self, index: usize) -> &mut S { + self.buffer.slice_unchecked_mut(index) + } } impl ManagedVecItemPayloadConcat @@ -148,6 +168,12 @@ where fn split_from_add(_payload: &Self::Output) -> (&Self, &ManagedVecItemPayloadConcat) { todo!() } + + fn split_mut_from_add( + _payload: &mut Self::Output, + ) -> (&mut Self, &mut ManagedVecItemPayloadConcat) { + todo!() + } } pub struct ManagedVecItemPayloadEquiv @@ -182,6 +208,10 @@ where unsafe fn slice_unchecked(&self, index: usize) -> &S { self.equiv.slice_unchecked(index) } + + unsafe fn slice_unchecked_mut(&mut self, index: usize) -> &mut S { + self.equiv.slice_unchecked_mut(index) + } } /// Describes concatantion of smaller payloads into a larger one. @@ -196,6 +226,8 @@ where type Output: ManagedVecItemPayload; fn split_from_add(payload: &Self::Output) -> (&Self, &Rhs); + + fn split_mut_from_add(payload: &mut Self::Output) -> (&mut Self, &mut Rhs); } impl

ManagedVecItemPayloadAdd for P @@ -207,6 +239,12 @@ where fn split_from_add(payload: &Self::Output) -> (&Self, &ManagedVecItemEmptyPayload) { (payload, &ManagedVecItemEmptyPayload) } + + fn split_mut_from_add( + _payload: &mut Self::Output, + ) -> (&mut Self, &mut ManagedVecItemEmptyPayload) { + unimplemented!() + } } /// Replaces a const generic expression. @@ -234,6 +272,22 @@ macro_rules! payload_add { ) } } + + fn split_mut_from_add( + payload: &mut Self::Output, + ) -> ( + &mut ManagedVecItemPayloadBuffer<$dec1>, + &mut ManagedVecItemPayloadBuffer<$dec2>, + ) { + unsafe { + let ptr1 = payload.buffer.as_mut_ptr(); + let ptr2 = ptr1.add($dec1); + ( + &mut *ptr1.cast::>(), + &mut *ptr2.cast::>(), + ) + } + } } }; } diff --git a/framework/base/src/types/managed/wrapped/mod.rs b/framework/base/src/types/managed/wrapped/mod.rs index 38d721812e..4ca4ad6d97 100644 --- a/framework/base/src/types/managed/wrapped/mod.rs +++ b/framework/base/src/types/managed/wrapped/mod.rs @@ -46,7 +46,10 @@ pub use managed_option::ManagedOption; pub use managed_ref::ManagedRef; pub use managed_ref_mut::ManagedRefMut; pub use managed_vec::ManagedVec; -pub use managed_vec_item::{managed_vec_item_read_from_payload_index, ManagedVecItem}; +pub use managed_vec_item::{ + managed_vec_item_read_from_payload_index, managed_vec_item_save_to_payload_index, + ManagedVecItem, +}; pub use managed_vec_item_nested_tuple::{ ManagedVecItemNestedTuple, ManagedVecItemNestedTupleSplit, }; diff --git a/framework/derive/src/managed_vec_item_derive.rs b/framework/derive/src/managed_vec_item_derive.rs index 028c833168..397d83f55a 100644 --- a/framework/derive/src/managed_vec_item_derive.rs +++ b/framework/derive/src/managed_vec_item_derive.rs @@ -84,6 +84,24 @@ fn generate_to_byte_writer_snippets(fields: &syn::Fields) -> Vec Vec { + match fields { + syn::Fields::Named(fields_named) => fields_named + .named + .iter() + .map(|field| { + let field_ident = &field.ident; + quote! { + multiversx_sc::types::managed_vec_item_save_to_payload_index(self.#field_ident, payload, &mut index); + } + }) + .collect(), + _ => { + panic!("ManagedVecItem only supports named fields") + } + } +} + fn generate_payload_buffer_snippet() -> proc_macro2::TokenStream { quote! { let mut payload = ::new_buffer(); @@ -138,6 +156,13 @@ fn enum_derive(data_enum: &syn::DataEnum, ast: &syn::DeriveInput) -> TokenStream }; writer(&arr[..]) } + + fn save_to_payload(self, payload: &mut Self::PAYLOAD) { + let discriminant = match self { + #(#writer_match_arms)* + }; + ::save_to_payload(discriminant, payload); + } } }; gen.into() @@ -151,6 +176,7 @@ fn struct_derive(data_struct: &syn::DataStruct, ast: &syn::DeriveInput) -> Token generate_skips_reserialization_snippets(&data_struct.fields); let read_from_payload_snippets = generate_read_from_payload_snippets(&data_struct.fields); let to_byte_writer_snippets = generate_to_byte_writer_snippets(&data_struct.fields); + let save_to_payload_snippets = generate_save_to_payload_snippets(&data_struct.fields); let payload_buffer_snippet = generate_payload_buffer_snippet(); @@ -183,6 +209,13 @@ fn struct_derive(data_struct: &syn::DataStruct, ast: &syn::DeriveInput) -> Token writer(&payload_slice[..]) } + + fn save_to_payload(self, payload: &mut Self::PAYLOAD) { + let mut index = 0; + unsafe { + #(#save_to_payload_snippets)* + } + } } }; gen.into() diff --git a/framework/scenario/src/api/impl_vh/debug_handle_vh.rs b/framework/scenario/src/api/impl_vh/debug_handle_vh.rs index 3a21758e6a..2157ab2877 100644 --- a/framework/scenario/src/api/impl_vh/debug_handle_vh.rs +++ b/framework/scenario/src/api/impl_vh/debug_handle_vh.rs @@ -94,6 +94,10 @@ impl ManagedVecItem for DebugHandle { fn into_byte_writer R>(self, writer: Writer) -> R { RawHandle::into_byte_writer(self.get_raw_handle(), writer) } + + fn save_to_payload(self, payload: &mut Self::PAYLOAD) { + self.get_raw_handle().save_to_payload(payload); + } } impl TryStaticCast for DebugHandle {} diff --git a/framework/scenario/tests/derive_managed_vec_item_biguint_test.rs b/framework/scenario/tests/derive_managed_vec_item_biguint_test.rs index 9cbf9ec1b9..7f0d06be8f 100644 --- a/framework/scenario/tests/derive_managed_vec_item_biguint_test.rs +++ b/framework/scenario/tests/derive_managed_vec_item_biguint_test.rs @@ -1,7 +1,9 @@ use multiversx_sc::{ api::ManagedTypeApi, - codec, - codec::derive::{NestedDecode, NestedEncode, TopDecode, TopEncode}, + codec::{ + self, + derive::{NestedDecode, NestedEncode, TopDecode, TopEncode}, + }, derive::ManagedVecItem, types::{BigUint, ManagedType, ManagedVecItemPayload}, }; @@ -39,20 +41,15 @@ fn managed_struct_to_bytes_writer() { num: 0x12345, }; - let mut payload = as multiversx_sc::types::ManagedVecItem>::PAYLOAD::new_buffer(); - let payload_slice = payload.payload_slice_mut(); - let handle_bytes = s.big_uint.get_handle().to_be_bytes(); let expected = [0xff, 0xff, 0xff, handle_bytes[3], 0x00, 0x01, 0x23, 0x45]; - as multiversx_sc::types::ManagedVecItem>::into_byte_writer( + let mut payload = as multiversx_sc::types::ManagedVecItem>::PAYLOAD::new_buffer(); + as multiversx_sc::types::ManagedVecItem>::save_to_payload( s, - |bytes| { - payload_slice.copy_from_slice(bytes); - - assert_eq!(payload_slice, expected); - }, + &mut payload, ); + assert_eq!(payload.buffer, expected); } #[test] diff --git a/framework/scenario/tests/derive_managed_vec_item_esdt_token_payment_test.rs b/framework/scenario/tests/derive_managed_vec_item_esdt_token_payment_test.rs index 84dfd54e32..c6dea79b94 100644 --- a/framework/scenario/tests/derive_managed_vec_item_esdt_token_payment_test.rs +++ b/framework/scenario/tests/derive_managed_vec_item_esdt_token_payment_test.rs @@ -1,7 +1,9 @@ use multiversx_sc::{ api::ManagedTypeApi, - codec, - codec::derive::{NestedDecode, NestedEncode, TopDecode, TopEncode}, + codec::{ + self, + derive::{NestedDecode, NestedEncode, TopDecode, TopEncode}, + }, derive::ManagedVecItem, types::{ BigUint, EsdtTokenPayment, ManagedByteArray, ManagedType, ManagedVecItemPayload, @@ -50,9 +52,6 @@ fn struct_to_bytes_writer() { eth_address_2: ManagedByteArray::new_from_bytes(&[2u8; 20]), }; - let mut payload = as multiversx_sc::types::ManagedVecItem>::PAYLOAD::new_buffer(); - let payload_slice = payload.payload_slice_mut(); - let handle1 = s.token.token_identifier.get_handle().to_be_bytes(); let handle2 = s.token.amount.get_handle().to_be_bytes(); let handle3 = s.eth_address_1.get_handle().to_be_bytes(); @@ -63,14 +62,12 @@ fn struct_to_bytes_writer() { handle3[1], handle3[2], handle3[3], handle4[0], handle4[1], handle4[2], handle4[3], ]; - as multiversx_sc::types::ManagedVecItem>::into_byte_writer( + let mut payload = as multiversx_sc::types::ManagedVecItem>::PAYLOAD::new_buffer(); + as multiversx_sc::types::ManagedVecItem>::save_to_payload( s, - |bytes| { - payload_slice.copy_from_slice(bytes); - - assert_eq!(payload_slice, expected); - }, + &mut payload, ); + assert_eq!(payload.buffer, expected); } #[test] diff --git a/framework/scenario/tests/derive_managed_vec_item_simple_enum.rs b/framework/scenario/tests/derive_managed_vec_item_simple_enum.rs index 11719b6872..5a41283613 100644 --- a/framework/scenario/tests/derive_managed_vec_item_simple_enum.rs +++ b/framework/scenario/tests/derive_managed_vec_item_simple_enum.rs @@ -1,7 +1,10 @@ use multiversx_sc::{ - codec, - codec::derive::{NestedDecode, NestedEncode, TopDecode, TopEncode}, + codec::{ + self, + derive::{NestedDecode, NestedEncode, TopDecode, TopEncode}, + }, derive::ManagedVecItem, + types::{ManagedVecItemPayload, ManagedVecItemPayloadBuffer}, }; // to test, run the following command in the crate folder: @@ -27,13 +30,13 @@ fn enum_static() { #[test] fn enum_to_bytes_writer() { - ::into_byte_writer( + let mut payload = ManagedVecItemPayloadBuffer::new_buffer(); + ::save_to_payload( SimpleEnum::Variant1, - |bytes| { - assert_eq!(bytes.len(), 1); - assert_eq!(bytes[0], 0); - }, + &mut payload, ); + + assert_eq!(payload.buffer, [0]); } #[test] diff --git a/framework/scenario/tests/derive_managed_vec_item_struct_1_test.rs b/framework/scenario/tests/derive_managed_vec_item_struct_1_test.rs index 49b43b8711..20746bcb80 100644 --- a/framework/scenario/tests/derive_managed_vec_item_struct_1_test.rs +++ b/framework/scenario/tests/derive_managed_vec_item_struct_1_test.rs @@ -65,18 +65,14 @@ fn struct_1_to_bytes_writer() { }; let mut payload = ::PAYLOAD::new_buffer(); - let payload_slice = payload.payload_slice_mut(); - - ::into_byte_writer(s, |bytes| { - payload_slice.copy_from_slice(bytes); - assert_eq!( - payload_slice, - [ - 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x04, 0x01, - ] - ); - }); + ::save_to_payload(s, &mut payload); + assert_eq!( + payload.buffer, + [ + 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x04, 0x01, + ] + ); } #[test] diff --git a/framework/scenario/tests/derive_managed_vec_item_struct_2_test.rs b/framework/scenario/tests/derive_managed_vec_item_struct_2_test.rs index f188f1f64d..641bb04a0b 100644 --- a/framework/scenario/tests/derive_managed_vec_item_struct_2_test.rs +++ b/framework/scenario/tests/derive_managed_vec_item_struct_2_test.rs @@ -1,3 +1,5 @@ +use multiversx_sc::types::{ManagedVecItemPayload, ManagedVecItemPayloadBuffer}; + multiversx_sc::derive_imports!(); // to test, run the following command in the crate folder: @@ -49,9 +51,9 @@ fn struct_to_bytes_writer() { /* arr */ 0x61, 0x11, 0x62, 0x22, ]; - ::into_byte_writer(s, |bytes| { - assert_eq!(bytes, &expected_payload[..]); - }); + let mut payload = ManagedVecItemPayloadBuffer::new_buffer(); + ::save_to_payload(s, &mut payload); + assert_eq!(payload.buffer, &expected_payload[..]); } #[test] From 01a849e9a31c8b56f88b5a7d286f8cacb748dd0c Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 27 Nov 2024 10:03:20 +0200 Subject: [PATCH 32/66] ManagedVecItem payload refactor - remove into_byte_writer --- .../esdt_token_payment_multi_value.rs | 5 -- .../managed/wrapped/esdt_token_payment.rs | 22 ------- .../types/managed/wrapped/managed_option.rs | 4 -- .../src/types/managed/wrapped/managed_vec.rs | 16 ++--- .../types/managed/wrapped/managed_vec_item.rs | 60 +------------------ .../derive/src/managed_vec_item_derive.rs | 50 ---------------- .../src/api/impl_vh/debug_handle_vh.rs | 4 -- 7 files changed, 10 insertions(+), 151 deletions(-) diff --git a/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs b/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs index 1fd9c5ef2b..d881b6a995 100644 --- a/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs +++ b/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs @@ -53,11 +53,6 @@ impl ManagedVecItem for EsdtTokenPaymentMultiValue { Self::read_from_payload(payload) } - #[inline] - fn into_byte_writer R>(self, writer: Writer) -> R { - self.obj.into_byte_writer(writer) - } - fn save_to_payload(self, payload: &mut Self::PAYLOAD) { self.obj.save_to_payload(payload); } diff --git a/framework/base/src/types/managed/wrapped/esdt_token_payment.rs b/framework/base/src/types/managed/wrapped/esdt_token_payment.rs index 435a6e223e..b676fa42cc 100644 --- a/framework/base/src/types/managed/wrapped/esdt_token_payment.rs +++ b/framework/base/src/types/managed/wrapped/esdt_token_payment.rs @@ -156,17 +156,6 @@ impl EsdtTokenPayment { } } -fn managed_vec_item_to_slice(arr: &mut [u8], index: &mut usize, item: T) -where - T: ManagedVecItem, -{ - ManagedVecItem::into_byte_writer(item, |bytes| { - let size = T::payload_size(); - arr[*index..*index + size].copy_from_slice(bytes); - *index += size; - }); -} - impl IntoMultiValue for EsdtTokenPayment { type MultiValue = EsdtTokenPaymentMultiValue; @@ -197,17 +186,6 @@ impl ManagedVecItem for EsdtTokenPayment { Self::read_from_payload(payload) } - fn into_byte_writer R>(self, mut writer: Writer) -> R { - let mut arr: [u8; 16] = [0u8; 16]; - let mut index = 0; - - managed_vec_item_to_slice(&mut arr, &mut index, self.token_identifier); - managed_vec_item_to_slice(&mut arr, &mut index, self.token_nonce); - managed_vec_item_to_slice(&mut arr, &mut index, self.amount); - - writer(&arr[..]) - } - fn save_to_payload(self, payload: &mut Self::PAYLOAD) { let mut index = 0; diff --git a/framework/base/src/types/managed/wrapped/managed_option.rs b/framework/base/src/types/managed/wrapped/managed_option.rs index 9616696469..c7ee9371b9 100644 --- a/framework/base/src/types/managed/wrapped/managed_option.rs +++ b/framework/base/src/types/managed/wrapped/managed_option.rs @@ -214,10 +214,6 @@ where Self::read_from_payload(payload) } - fn into_byte_writer R>(self, writer: Writer) -> R { - ::into_byte_writer(self.handle, writer) - } - fn save_to_payload(self, payload: &mut Self::PAYLOAD) { self.handle.get_raw_handle().save_to_payload(payload); } diff --git a/framework/base/src/types/managed/wrapped/managed_vec.rs b/framework/base/src/types/managed/wrapped/managed_vec.rs index 762f2757f6..4bbf073088 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec.rs @@ -201,7 +201,9 @@ where pub fn set(&mut self, index: usize, item: T) -> Result<(), InvalidSliceError> { let byte_index = index * T::payload_size(); - item.into_byte_writer(|slice| self.buffer.set_slice(byte_index, slice)) + let mut payload = T::PAYLOAD::new_buffer(); + item.save_to_payload(&mut payload); + self.buffer.set_slice(byte_index, payload.payload_slice()) } /// Returns a new `ManagedVec`, containing the [start_index, end_index) range of elements. @@ -214,9 +216,9 @@ where } pub fn push(&mut self, item: T) { - item.into_byte_writer(|bytes| { - self.buffer.append_bytes(bytes); - }); + let mut payload = T::PAYLOAD::new_buffer(); + item.save_to_payload(&mut payload); + self.buffer.append_bytes(payload.payload_slice()); } pub fn remove(&mut self, index: usize) { @@ -260,9 +262,9 @@ where } pub fn overwrite_with_single_item(&mut self, item: T) { - item.into_byte_writer(|bytes| { - self.buffer.overwrite(bytes); - }); + let mut payload = T::PAYLOAD::new_buffer(); + item.save_to_payload(&mut payload); + self.buffer.overwrite(payload.payload_slice()); } /// Appends all the contents of another managed vec at the end of the current one. diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item.rs b/framework/base/src/types/managed/wrapped/managed_vec_item.rs index bd4b311ca2..0752796145 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item.rs @@ -53,15 +53,9 @@ pub trait ManagedVecItem: 'static { /// Converts the object into bytes. /// - /// The output is processed by the `writer` lambda. - /// The writer is provided by the caller. - /// The callee will use it to pass on the bytes. - /// /// The method is used when instering (push, overwrite) into a ManagedVec. /// /// Note that a destructor should not be called at this moment, since the ManagedVec will take ownership of the item. - fn into_byte_writer R>(self, writer: Writer) -> R; - fn save_to_payload(self, payload: &mut Self::PAYLOAD); } @@ -112,11 +106,6 @@ macro_rules! impl_int { $ty::from_be_bytes(payload.buffer) } - fn into_byte_writer R>(self, mut writer: Writer) -> R { - let bytes = self.to_be_bytes(); - writer(&bytes) - } - fn save_to_payload(self, payload: &mut Self::PAYLOAD) { payload.buffer = self.to_be_bytes(); } @@ -143,11 +132,6 @@ impl ManagedVecItem for usize { Self::read_from_payload(payload) } - fn into_byte_writer R>(self, mut writer: Writer) -> R { - let bytes = (self as u32).to_be_bytes(); - writer(&bytes) - } - fn save_to_payload(self, payload: &mut Self::PAYLOAD) { (self as u32).save_to_payload(payload); } @@ -166,14 +150,9 @@ impl ManagedVecItem for bool { Self::read_from_payload(payload) } - fn into_byte_writer R>(self, writer: Writer) -> R { + fn save_to_payload(self, payload: &mut Self::PAYLOAD) { // true -> 1u8 // false -> 0u8 - let u8_value = u8::from(self); - ::into_byte_writer(u8_value, writer) - } - - fn save_to_payload(self, payload: &mut Self::PAYLOAD) { u8::from(self).save_to_payload(payload); } } @@ -204,18 +183,6 @@ where Self::read_from_payload(payload) } - fn into_byte_writer R>(self, mut writer: Writer) -> R { - let mut payload = Self::PAYLOAD::new_buffer(); - let slice = payload.payload_slice_mut(); - if let Some(t) = self { - slice[0] = 1; - T::into_byte_writer(t, |bytes| { - slice[1..].copy_from_slice(bytes); - }); - } - writer(slice) - } - fn save_to_payload(self, payload: &mut Self::PAYLOAD) { let (p1, p2) = as ManagedVecItemPayloadAdd< T::PAYLOAD, @@ -245,11 +212,6 @@ macro_rules! impl_managed_type { ManagedRef::wrap_handle(handle) } - fn into_byte_writer R>(self, writer: Writer) -> R { - let handle = unsafe { self.forget_into_handle() }; - <$ty as ManagedType>::OwnHandle::into_byte_writer(handle, writer) - } - fn save_to_payload(self, payload: &mut Self::PAYLOAD) { let handle = unsafe { self.forget_into_handle() }; handle.get_raw_handle().save_to_payload(payload); @@ -283,13 +245,6 @@ where ManagedRef::wrap_handle(handle) } - fn into_byte_writer R>(self, writer: Writer) -> R { - <>::OwnHandle as ManagedVecItem>::into_byte_writer( - self.get_handle(), - writer, - ) - } - fn save_to_payload(self, payload: &mut Self::PAYLOAD) { let handle = unsafe { self.forget_into_handle() }; handle.get_raw_handle().save_to_payload(payload); @@ -315,11 +270,6 @@ where ManagedRef::wrap_handle(handle) } - fn into_byte_writer R>(self, writer: Writer) -> R { - let handle = unsafe { self.forget_into_handle() }; - ::into_byte_writer(handle, writer) - } - fn save_to_payload(self, payload: &mut Self::PAYLOAD) { let handle = unsafe { self.forget_into_handle() }; handle.get_raw_handle().save_to_payload(payload); @@ -339,10 +289,6 @@ impl ManagedVecItem for EsdtTokenType { Self::read_from_payload(payload) } - fn into_byte_writer R>(self, mut writer: Writer) -> R { - writer(&[self.as_u8()]) - } - fn save_to_payload(self, payload: &mut Self::PAYLOAD) { self.as_u8().save_to_payload(payload); } @@ -361,10 +307,6 @@ impl ManagedVecItem for EsdtLocalRole { Self::read_from_payload(payload) } - fn into_byte_writer R>(self, writer: Writer) -> R { - ::into_byte_writer(self.as_u16(), writer) - } - fn save_to_payload(self, payload: &mut Self::PAYLOAD) { self.as_u16().save_to_payload(payload); } diff --git a/framework/derive/src/managed_vec_item_derive.rs b/framework/derive/src/managed_vec_item_derive.rs index 397d83f55a..1531b00610 100644 --- a/framework/derive/src/managed_vec_item_derive.rs +++ b/framework/derive/src/managed_vec_item_derive.rs @@ -61,29 +61,6 @@ fn generate_read_from_payload_snippets(fields: &syn::Fields) -> Vec Vec { - match fields { - syn::Fields::Named(fields_named) => fields_named - .named - .iter() - .map(|field| { - let field_ident = &field.ident; - let type_name = &field.ty; - quote! { - multiversx_sc::types::ManagedVecItem::into_byte_writer(self.#field_ident, |bytes| { - let next_index = index + <#type_name as multiversx_sc::types::ManagedVecItem>::payload_size(); - payload_slice[index .. next_index].copy_from_slice(bytes); - index = next_index; - }); - } - }) - .collect(), - _ => { - panic!("ManagedVecItem only supports named fields") - } - } -} - fn generate_save_to_payload_snippets(fields: &syn::Fields) -> Vec { match fields { syn::Fields::Named(fields_named) => fields_named @@ -102,13 +79,6 @@ fn generate_save_to_payload_snippets(fields: &syn::Fields) -> Vec proc_macro2::TokenStream { - quote! { - let mut payload = ::new_buffer(); - let payload_slice = multiversx_sc::types::ManagedVecItemPayload::payload_slice_mut(&mut payload); - } -} - fn enum_derive(data_enum: &syn::DataEnum, ast: &syn::DeriveInput) -> TokenStream { let name = &ast.ident; let (impl_generics, ty_generics, where_clause) = &ast.generics.split_for_impl(); @@ -149,14 +119,6 @@ fn enum_derive(data_enum: &syn::DataEnum, ast: &syn::DeriveInput) -> TokenStream Self::read_from_payload(payload) } - fn into_byte_writer R>(self, mut writer: Writer) -> R { - let mut arr: [u8; 1] = [0u8; 1]; - arr[0] = match self { - #(#writer_match_arms)* - }; - writer(&arr[..]) - } - fn save_to_payload(self, payload: &mut Self::PAYLOAD) { let discriminant = match self { #(#writer_match_arms)* @@ -175,11 +137,8 @@ fn struct_derive(data_struct: &syn::DataStruct, ast: &syn::DeriveInput) -> Token let skips_reserialization_snippets = generate_skips_reserialization_snippets(&data_struct.fields); let read_from_payload_snippets = generate_read_from_payload_snippets(&data_struct.fields); - let to_byte_writer_snippets = generate_to_byte_writer_snippets(&data_struct.fields); let save_to_payload_snippets = generate_save_to_payload_snippets(&data_struct.fields); - let payload_buffer_snippet = generate_payload_buffer_snippet(); - let gen = quote! { impl #impl_generics multiversx_sc::types::ManagedVecItem for #name #ty_generics #where_clause { type PAYLOAD = <#payload_nested_tuple as multiversx_sc::types::ManagedVecItemNestedTuple>::PAYLOAD; @@ -201,15 +160,6 @@ fn struct_derive(data_struct: &syn::DataStruct, ast: &syn::DeriveInput) -> Token Self::read_from_payload(payload) } - fn into_byte_writer R>(self, mut writer: Writer) -> R { - #payload_buffer_snippet - let mut index = 0; - - #(#to_byte_writer_snippets)* - - writer(&payload_slice[..]) - } - fn save_to_payload(self, payload: &mut Self::PAYLOAD) { let mut index = 0; unsafe { diff --git a/framework/scenario/src/api/impl_vh/debug_handle_vh.rs b/framework/scenario/src/api/impl_vh/debug_handle_vh.rs index 2157ab2877..64fcd15fa9 100644 --- a/framework/scenario/src/api/impl_vh/debug_handle_vh.rs +++ b/framework/scenario/src/api/impl_vh/debug_handle_vh.rs @@ -91,10 +91,6 @@ impl ManagedVecItem for DebugHandle { Self::read_from_payload(payload) } - fn into_byte_writer R>(self, writer: Writer) -> R { - RawHandle::into_byte_writer(self.get_raw_handle(), writer) - } - fn save_to_payload(self, payload: &mut Self::PAYLOAD) { self.get_raw_handle().save_to_payload(payload); } From 2afe44488b6ac3338c8c050d0fdf61b4d7d20a73 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 27 Nov 2024 10:26:22 +0200 Subject: [PATCH 33/66] ManagedVecItem payload refactor - cleanup --- .../wrapped/managed_vec_item_nested_tuple.rs | 79 ------------ .../wrapped/managed_vec_item_payload.rs | 118 ------------------ .../base/src/types/managed/wrapped/mod.rs | 4 +- 3 files changed, 1 insertion(+), 200 deletions(-) diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item_nested_tuple.rs b/framework/base/src/types/managed/wrapped/managed_vec_item_nested_tuple.rs index 11a7055a40..3a25bf4993 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item_nested_tuple.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item_nested_tuple.rs @@ -5,33 +5,11 @@ use super::{ /// Syntactic sugar, that allows us to more easily represent composite payloads as nested tuples. pub trait ManagedVecItemNestedTuple { type PAYLOAD: ManagedVecItemPayload; - type Split1: ManagedVecItemPayload; - type Split2: ManagedVecItemPayload; - - fn split_payload(payload: &Self::PAYLOAD) -> (&Self::Split1, &Self::Split2); -} - -pub trait ManagedVecItemNestedTupleSplit<'a>: ManagedVecItemNestedTuple { - type S; - - fn split_all(payload: &'a Self::PAYLOAD) -> Self::S; } /// End of the list. impl ManagedVecItemNestedTuple for () { type PAYLOAD = ManagedVecItemEmptyPayload; - type Split1 = ManagedVecItemEmptyPayload; - type Split2 = ManagedVecItemEmptyPayload; - - fn split_payload(_payload: &Self::PAYLOAD) -> (&Self::Split1, &Self::Split2) { - (&ManagedVecItemEmptyPayload, &ManagedVecItemEmptyPayload) - } -} - -impl<'a> ManagedVecItemNestedTupleSplit<'a> for () { - type S = (); - - fn split_all(_payload: &'a Self::PAYLOAD) -> Self::S {} } impl ManagedVecItemNestedTuple for (Head, Tail) @@ -41,51 +19,10 @@ where Head::PAYLOAD: ManagedVecItemPayloadAdd, { type PAYLOAD = >::Output; - type Split1 = ::PAYLOAD; - type Split2 = ::PAYLOAD; - - fn split_payload(payload: &Self::PAYLOAD) -> (&Self::Split1, &Self::Split2) { - Head::PAYLOAD::split_from_add(payload) - } } -impl<'a, Head, Tail> ManagedVecItemNestedTupleSplit<'a> for (Head, Tail) -where - Head: ManagedVecItem, - Tail: ManagedVecItemNestedTupleSplit<'a>, - Head::PAYLOAD: ManagedVecItemPayloadAdd, - Tail::PAYLOAD: 'a, -{ - type S = (&'a Head::PAYLOAD, Tail::S); - - fn split_all(payload: &'a Self::PAYLOAD) -> Self::S { - let (hp, tp) = Head::PAYLOAD::split_from_add(payload); - (hp, Tail::split_all(tp)) - } -} - -// pub fn split_payload( -// payload: &<(Head, Tail) as ManagedVecItemNestedTuple>::PAYLOAD, -// ) -> (&Head::PAYLOAD, &Tail::PAYLOAD) -// where -// Head: ManagedVecItem, -// Tail: ManagedVecItemNestedTuple, -// Head::PAYLOAD: ManagedVecItemPayloadAdd, -// // (Head, Tail): ManagedVecItemNestedTuple, -// { -// >::split_from_add(payload) -// // <(Head, Tail) as ManagedVecItemNestedTuple>::PAYLOAD as -// // unsafe { -// // let ptr1 = payload.buffer.as_ptr(); -// // let ptr2 = ptr1.offset($dec1 as isize); -// // (core::mem::transmute(ptr1), core::mem::transmute(ptr2)) -// // } -// } - #[cfg(test)] pub mod tests { - use crate::types::ManagedVecItemPayloadBuffer; - use super::*; #[test] @@ -100,20 +37,4 @@ pub mod tests { fn assert_payload_size(expected_size: usize) { assert_eq!(N::PAYLOAD::payload_size(), expected_size); } - - #[test] - fn split_all_test() { - let p = ManagedVecItemPayloadBuffer::new_buffer(); - let (_p1, (_p2, ())) = <(u16, (u32, ()))>::split_all(&p); - } - - // fn split_all_t() - // where - // T: ManagedVecItem, - // ManagedVecItemPayloadBuffer<1>: ManagedVecItemPayloadAdd, - // (u16, (T, ())): ManagedVecItemNestedTuple, - // { - // let p = ManagedVecItemPayloadBuffer::new_buffer(); - // let (p1, (p2, ())) = <(u16, (T, ()))>::split_all(&p); - // } } diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs b/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs index e13d878a94..a3a9c58475 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item_payload.rs @@ -1,7 +1,3 @@ -use core::marker::PhantomData; - -use super::ManagedVecItem; - /// Describes the binary represetnation of a ManagedVecItem. /// /// It is always an array that can be allocated directly on stack. @@ -100,120 +96,6 @@ impl ManagedVecItemPayload for ManagedVecItemPayloadBuffer { } } -#[repr(transparent)] -pub struct ManagedVecItemPayloadConcat -where - P1: ManagedVecItemPayload, - P2: ManagedVecItemPayload, - P1: ManagedVecItemPayloadAdd, -{ - _phantom_1: PhantomData, - _phantom_2: PhantomData, - buffer: >::Output, -} - -impl ManagedVecItemPayload for ManagedVecItemPayloadConcat -where - P1: ManagedVecItemPayload, - P2: ManagedVecItemPayload, - P1: ManagedVecItemPayloadAdd, -{ - fn new_buffer() -> Self { - ManagedVecItemPayloadConcat { - _phantom_1: PhantomData, - _phantom_2: PhantomData, - buffer: ManagedVecItemPayload::new_buffer(), - } - } - - fn payload_size() -> usize { - >::Output::payload_size() - } - - fn payload_slice(&self) -> &[u8] { - self.buffer.payload_slice() - } - - fn payload_slice_mut(&mut self) -> &mut [u8] { - self.buffer.payload_slice_mut() - } - - unsafe fn slice_unchecked(&self, index: usize) -> &S { - self.buffer.slice_unchecked(index) - } - - unsafe fn slice_unchecked_mut(&mut self, index: usize) -> &mut S { - self.buffer.slice_unchecked_mut(index) - } -} - -impl ManagedVecItemPayloadConcat -where - P1: ManagedVecItemPayload, - P2: ManagedVecItemPayload, - P1: ManagedVecItemPayloadAdd, -{ -} - -impl ManagedVecItemPayloadAdd> for P0 -where - P0: ManagedVecItemPayload, - P1: ManagedVecItemPayload, - P2: ManagedVecItemPayload, - P1: ManagedVecItemPayloadAdd, - P0: ManagedVecItemPayloadAdd<>::Output>, -{ - type Output = ManagedVecItemPayloadConcat>; - - fn split_from_add(_payload: &Self::Output) -> (&Self, &ManagedVecItemPayloadConcat) { - todo!() - } - - fn split_mut_from_add( - _payload: &mut Self::Output, - ) -> (&mut Self, &mut ManagedVecItemPayloadConcat) { - todo!() - } -} - -pub struct ManagedVecItemPayloadEquiv -where - T: ManagedVecItem, -{ - equiv: T::PAYLOAD, -} - -impl ManagedVecItemPayload for ManagedVecItemPayloadEquiv -where - T: ManagedVecItem, -{ - fn new_buffer() -> Self { - ManagedVecItemPayloadEquiv { - equiv: ManagedVecItemPayload::new_buffer(), - } - } - - fn payload_size() -> usize { - T::PAYLOAD::payload_size() - } - - fn payload_slice(&self) -> &[u8] { - self.equiv.payload_slice() - } - - fn payload_slice_mut(&mut self) -> &mut [u8] { - self.equiv.payload_slice_mut() - } - - unsafe fn slice_unchecked(&self, index: usize) -> &S { - self.equiv.slice_unchecked(index) - } - - unsafe fn slice_unchecked_mut(&mut self, index: usize) -> &mut S { - self.equiv.slice_unchecked_mut(index) - } -} - /// Describes concatantion of smaller payloads into a larger one. /// /// There is no runtime implementation, just a type-level addition. diff --git a/framework/base/src/types/managed/wrapped/mod.rs b/framework/base/src/types/managed/wrapped/mod.rs index 4ca4ad6d97..5086c3bc26 100644 --- a/framework/base/src/types/managed/wrapped/mod.rs +++ b/framework/base/src/types/managed/wrapped/mod.rs @@ -50,9 +50,7 @@ pub use managed_vec_item::{ managed_vec_item_read_from_payload_index, managed_vec_item_save_to_payload_index, ManagedVecItem, }; -pub use managed_vec_item_nested_tuple::{ - ManagedVecItemNestedTuple, ManagedVecItemNestedTupleSplit, -}; +pub use managed_vec_item_nested_tuple::ManagedVecItemNestedTuple; pub use managed_vec_item_payload::*; pub use managed_vec_owned_iter::ManagedVecOwnedIterator; pub use managed_vec_ref::ManagedVecRef; From cb60d95f77f66830b9220be7a6d07e3285e23f84 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 27 Nov 2024 14:45:41 +0200 Subject: [PATCH 34/66] ManagedVecItem payload refactor - fix after merge --- .../multi_value_managed_vec_counted.rs | 14 +- .../types/managed/wrapped/managed_vec_item.rs | 127 ++++++------------ 2 files changed, 48 insertions(+), 93 deletions(-) diff --git a/framework/base/src/types/managed/multi_value/multi_value_managed_vec_counted.rs b/framework/base/src/types/managed/multi_value/multi_value_managed_vec_counted.rs index a46016e69a..0cb695e0d5 100644 --- a/framework/base/src/types/managed/multi_value/multi_value_managed_vec_counted.rs +++ b/framework/base/src/types/managed/multi_value/multi_value_managed_vec_counted.rs @@ -98,18 +98,16 @@ where const SKIPS_RESERIALIZATION: bool = false; type Ref<'a> = Self; - fn from_byte_reader(reader: Reader) -> Self { - Self::from(ManagedVec::::from_byte_reader(reader)) + fn read_from_payload(payload: &Self::PAYLOAD) -> Self { + Self::from(ManagedVec::::read_from_payload(payload)) } - unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( - reader: Reader, - ) -> Self::Ref<'a> { - Self::from_byte_reader(reader) + unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> { + Self::read_from_payload(payload) } - fn into_byte_writer R>(self, writer: Writer) -> R { - self.contents.into_byte_writer(writer) + fn save_to_payload(self, payload: &mut Self::PAYLOAD) { + self.contents.save_to_payload(payload); } } diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item.rs b/framework/base/src/types/managed/wrapped/managed_vec_item.rs index 94a4c6cd1f..65ed262a41 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item.rs @@ -11,7 +11,10 @@ use crate::{ }, }; -use super::{ManagedVecItemPayload, ManagedVecItemPayloadAdd, ManagedVecItemPayloadBuffer}; +use super::{ + ManagedVecItemNestedTuple, ManagedVecItemPayload, ManagedVecItemPayloadAdd, + ManagedVecItemPayloadBuffer, +}; /// Types that implement this trait can be items inside a `ManagedVec`. /// All these types need a payload, i.e a representation that gets stored @@ -323,49 +326,30 @@ where const SKIPS_RESERIALIZATION: bool = T1::SKIPS_RESERIALIZATION && T2::SKIPS_RESERIALIZATION; type Ref<'a> = Self; - fn from_byte_reader(mut reader: Reader) -> Self { - let mut payload = ::new_buffer(); - let payload_slice = ManagedVecItemPayload::payload_slice_mut(&mut payload); - reader(payload_slice); + fn read_from_payload(payload: &Self::PAYLOAD) -> Self { let mut index = 0; - - ( - T1::from_byte_reader(|bytes| { - let next_index = index + T1::payload_size(); - bytes.copy_from_slice(&payload_slice[index..next_index]); - index = next_index; - }), - T2::from_byte_reader(|bytes| { - let next_index = index + T2::payload_size(); - bytes.copy_from_slice(&payload_slice[index..next_index]); - index = next_index; - }), - ) - .into() + unsafe { + ( + managed_vec_item_read_from_payload_index(payload, &mut index), + managed_vec_item_read_from_payload_index(payload, &mut index), + ) + .into() + } } - unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( - reader: Reader, - ) -> Self::Ref<'a> { - Self::from_byte_reader(reader) + unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> { + // TODO: tuple of refs + Self::read_from_payload(payload) } - fn into_byte_writer R>(self, mut writer: Writer) -> R { - let mut payload = Self::PAYLOAD::new_buffer(); - let payload_slice = ManagedVecItemPayload::payload_slice_mut(&mut payload); + fn save_to_payload(self, payload: &mut Self::PAYLOAD) { + let tuple = self.into_tuple(); let mut index = 0; - let (t1, t2) = self.into_tuple(); - T1::into_byte_writer(t1, |bytes| { - let next_index = index + T1::payload_size(); - payload_slice[index..next_index].copy_from_slice(bytes); - index = next_index; - }); - T2::into_byte_writer(t2, |bytes| { - let next_index = index + T2::payload_size(); - payload_slice[index..next_index].copy_from_slice(bytes); - index = next_index; - }); - writer(payload_slice) + + unsafe { + managed_vec_item_save_to_payload_index(tuple.0, payload, &mut index); + managed_vec_item_save_to_payload_index(tuple.1, payload, &mut index); + } } } @@ -380,58 +364,31 @@ where const SKIPS_RESERIALIZATION: bool = T1::SKIPS_RESERIALIZATION && T2::SKIPS_RESERIALIZATION; type Ref<'a> = Self; - fn from_byte_reader(mut reader: Reader) -> Self { - let mut payload = ::new_buffer(); - let payload_slice = ManagedVecItemPayload::payload_slice_mut(&mut payload); - reader(payload_slice); + fn read_from_payload(payload: &Self::PAYLOAD) -> Self { let mut index = 0; - - ( - T1::from_byte_reader(|bytes| { - let next_index = index + T1::payload_size(); - bytes.copy_from_slice(&payload_slice[index..next_index]); - index = next_index; - }), - T2::from_byte_reader(|bytes| { - let next_index = index + T2::payload_size(); - bytes.copy_from_slice(&payload_slice[index..next_index]); - index = next_index; - }), - T3::from_byte_reader(|bytes| { - let next_index = index + T3::payload_size(); - bytes.copy_from_slice(&payload_slice[index..next_index]); - index = next_index; - }), - ) - .into() + unsafe { + ( + managed_vec_item_read_from_payload_index(payload, &mut index), + managed_vec_item_read_from_payload_index(payload, &mut index), + managed_vec_item_read_from_payload_index(payload, &mut index), + ) + .into() + } } - unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( - reader: Reader, - ) -> Self::Ref<'a> { - Self::from_byte_reader(reader) + unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> { + // TODO: tuple of refs + Self::read_from_payload(payload) } - fn into_byte_writer R>(self, mut writer: Writer) -> R { - let mut payload = Self::PAYLOAD::new_buffer(); - let payload_slice = ManagedVecItemPayload::payload_slice_mut(&mut payload); + fn save_to_payload(self, payload: &mut Self::PAYLOAD) { + let tuple = self.into_tuple(); let mut index = 0; - let (t1, t2, t3) = self.into_tuple(); - T1::into_byte_writer(t1, |bytes| { - let next_index = index + T1::payload_size(); - payload_slice[index..next_index].copy_from_slice(bytes); - index = next_index; - }); - T2::into_byte_writer(t2, |bytes| { - let next_index = index + T2::payload_size(); - payload_slice[index..next_index].copy_from_slice(bytes); - index = next_index; - }); - T3::into_byte_writer(t3, |bytes| { - let next_index = index + T2::payload_size(); - payload_slice[index..next_index].copy_from_slice(bytes); - index = next_index; - }); - writer(payload_slice) + + unsafe { + managed_vec_item_save_to_payload_index(tuple.0, payload, &mut index); + managed_vec_item_save_to_payload_index(tuple.1, payload, &mut index); + managed_vec_item_save_to_payload_index(tuple.2, payload, &mut index); + } } } From 567b8f1cdc22749d85d48d1e983ce6428816e652 Mon Sep 17 00:00:00 2001 From: BiancaIalangi Date: Wed, 27 Nov 2024 14:53:52 +0200 Subject: [PATCH 35/66] clippy fix --- chain/vm/src/tx_execution/exec_general_tx.rs | 21 ++++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/chain/vm/src/tx_execution/exec_general_tx.rs b/chain/vm/src/tx_execution/exec_general_tx.rs index abf4d4344e..53ca60bc30 100644 --- a/chain/vm/src/tx_execution/exec_general_tx.rs +++ b/chain/vm/src/tx_execution/exec_general_tx.rs @@ -52,17 +52,16 @@ pub(crate) fn create_transfer_value_log(tx_input: &TxInput, call_type: CallType) let mut data = vec![call_type.to_log_bytes(), tx_input.func_name.to_bytes()]; data.append(&mut tx_input.args.clone()); - if tx_input.esdt_values.is_empty() { - if !tx_input.callback_payments.egld_value.is_zero() - && tx_input.call_type == CallType::AsyncCallback - { - return TxLog { - address: tx_input.from.clone(), - endpoint: "transferValueOnly".into(), - topics: vec![b"".to_vec(), tx_input.to.to_vec()], - data, - }; - } + if tx_input.esdt_values.is_empty() + && !tx_input.callback_payments.egld_value.is_zero() + && tx_input.call_type == CallType::AsyncCallback + { + return TxLog { + address: tx_input.from.clone(), + endpoint: "transferValueOnly".into(), + topics: vec![b"".to_vec(), tx_input.to.to_vec()], + data, + }; } let egld_value = if tx_input.call_type == CallType::AsyncCallback { From dcaad2493f2fabf401d3c7e3c60d2f5367da17eb Mon Sep 17 00:00:00 2001 From: Mihai Calin Luca Date: Wed, 27 Nov 2024 13:57:40 +0100 Subject: [PATCH 36/66] interactor tests run sequentially, test fix --- .../examples/adder/interactor/Cargo.toml | 1 + .../examples/adder/interactor/set_state.json | 70 +++++++++---------- .../tests/basic_interactor_cs_test.rs | 10 ++- 3 files changed, 45 insertions(+), 36 deletions(-) diff --git a/contracts/examples/adder/interactor/Cargo.toml b/contracts/examples/adder/interactor/Cargo.toml index 30db735223..c6b37c739a 100644 --- a/contracts/examples/adder/interactor/Cargo.toml +++ b/contracts/examples/adder/interactor/Cargo.toml @@ -24,6 +24,7 @@ clap = { version = "4.4.7", features = ["derive"] } serde = { version = "1.0", features = ["derive"] } toml = "0.8.6" tokio = { version = "1.24" } +serial_test = { version = "3.2.0" } [features] chain-simulator-tests = [] diff --git a/contracts/examples/adder/interactor/set_state.json b/contracts/examples/adder/interactor/set_state.json index a5364326ab..a5115b7781 100644 --- a/contracts/examples/adder/interactor/set_state.json +++ b/contracts/examples/adder/interactor/set_state.json @@ -4,47 +4,47 @@ "nonce": 5947, "balance": "491982310359999986", "keys": { + "454c524f4e44657364745453542d363437383930": "1209004563918244f40000", + "454c524f4e446e6f6e636550544d2d353336666162": "01", "454c524f4e446573647450544d2d35333666616201": "08021202000122ef0108011212546573742d5061696e742d486172766573741a20e32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed6020c4132a2e516d57564239575362674b52655a64615a434344766b454b70705a6b4d696d397563736e7857565041414c6a4374324368747470733a2f2f697066732e696f2f697066732f516d57564239575362674b52655a64615a434344766b454b70705a6b4d696d397563736e7857565041414c6a43743a3d746167733a3b6d657461646174613a516d52635039346b5872357a5a6a52477669376d4a36756e374c7078556859565234523452706963787a67596b74", - "454c524f4e44657364745453542d643964336136": "1209004563918244f40000", - "454c524f4e44657364745745474c442d613238633539": "120900389351ce08f09e12", - "454c524f4e44657364745453542d633636666535": "1209004563918244f40000", - "454c524f4e44657364745453542d623130616461": "1209004563918244f40000", - "454c524f4e4465736474475245454e2d306531363163": "120b00152d02c7e14af67fffdc", - "454c524f4e44657364745453542d656338383735": "12020064", - "454c524f4e44657364744c5453542d376266336431": "1209000de0b6b3a763fc19", - "454c524f4e44657364745453542d343265356138": "1209004563918244f40000", "454c524f4e44726f6c656573647450544d2d353336666162": "0a1145534454526f6c654e46544372656174650a0f45534454526f6c654e46544275726e", - "454c524f4e44657364745453542d393836646663": "12020064", - "454c524f4e44657364745453542d323833633361": "12020064", - "454c524f4e4465736474424358535542542d33393264366172": "080112020001", + "454c524f4e44657364745453542d633565303835": "1209004563918244f40000", + "454c524f4e44657364745453542d346230653865": "1209004563918244f40000", "454c524f4e44657364744c5453542d346638343965": "1209000de0b6b3a763fc19", + "454c524f4e44657364745453542d393864633566": "1209004563918244f40000", + "454c524f4e44657364745453542d396230323030": "1209004563918244f40000", "454c524f4e44657364745453542d386564363538": "1209004563918244f40000", - "454c524f4e44657364745453542d343562383235": "12020064", - "454c524f4e446e6f6e636550544d2d353336666162": "01", + "454c524f4e44657364745453542d643862306438": "12020064", "454c524f4e44657364745453542d363835303064": "1209004563918244f40000", - "454c524f4e44657364745453542d336339363762": "12020064", - "454c524f4e446573647455544b2d313464353764": "120b0001e6ce88d5ebbfd00000", - "454c524f4e44657364745453542d363434633935": "12020064", - "454c524f4e44657364745453542d306632306637": "12020064", + "454c524f4e44657364745453542d323833633361": "12020064", "454c524f4e44657364745453542d333331386638": "1209004563918244f40000", - "454c524f4e44657364745453542d353538616434": "12020064", - "454c524f4e44657364745453542d363437383930": "1209004563918244f40000", - "454c524f4e44657364745453542d633933336139": "1209004563918244f40000", - "454c524f4e44657364745453542d393864633566": "1209004563918244f40000", "454c524f4e44657364745453542d303637373232": "1209004563918244f40000", - "454c524f4e44657364745453542d346634303238": "12020064", - "454c524f4e44657364745453542d643862306438": "12020064", - "454c524f4e44657364745453542d346230653865": "1209004563918244f40000", + "454c524f4e44657364745745474c442d613238633539": "120900389351ce08f09e12", "454c524f4e44657364745453542d343138613232": "1209004563918244f40000", - "454c524f4e44657364745453542d396230323030": "1209004563918244f40000", + "454c524f4e44657364745453542d656338383735": "12020064", + "454c524f4e44657364745453542d333639646531": "1209004563918244f40000", "454c524f4e44657364745453542d373639313337": "1209004563918244f40000", - "454c524f4e44657364745453542d303362373664": "12020064", + "454c524f4e44657364745453542d363434633935": "12020064", + "454c524f4e44657364745453542d623830663863": "1209004563918244f40000", + "454c524f4e4465736474475245454e2d306531363163": "120b00152d02c7e14af67fffdc", + "454c524f4e4465736474424358535542542d33393264366172": "080112020001", + "454c524f4e44657364745453542d343562383235": "12020064", "454c524f4e44657364745453542d613562663131": "12020064", - "454c524f4e44657364745453542d353966316165": "1209004563918244f40000", + "454c524f4e44657364745453542d623130616461": "1209004563918244f40000", + "454c524f4e44657364745453542d633933336139": "1209004563918244f40000", + "454c524f4e44657364744c5453542d376266336431": "1209000de0b6b3a763fc19", + "454c524f4e44657364745453542d336339363762": "12020064", "454c524f4e44657364745453542d623136363735": "1209004563918244f40000", - "454c524f4e44657364745453542d333639646531": "1209004563918244f40000", - "454c524f4e44657364745453542d623830663863": "1209004563918244f40000", - "454c524f4e44657364745453542d633565303835": "1209004563918244f40000" + "454c524f4e44657364745453542d303362373664": "12020064", + "454c524f4e44657364745453542d353538616434": "12020064", + "454c524f4e44657364745453542d353966316165": "1209004563918244f40000", + "454c524f4e44657364745453542d306632306637": "12020064", + "454c524f4e446573647455544b2d313464353764": "120b0001e6ce88d5ebbfd00000", + "454c524f4e44657364745453542d633636666535": "1209004563918244f40000", + "454c524f4e44657364745453542d346634303238": "12020064", + "454c524f4e44657364745453542d643964336136": "1209004563918244f40000", + "454c524f4e44657364745453542d343265356138": "1209004563918244f40000", + "454c524f4e44657364745453542d393836646663": "12020064" }, "code": "", "code_hash": "", @@ -58,19 +58,19 @@ "nonce": 1417, "balance": "1753855617144056", "keys": { + "454c524f4e4465736474424358535542542d3339326436616e": "080112020001", + "454c524f4e446573647442534b2d343736343730": "120b00021e19e0c9bab23fff7b", "454c524f4e446573647445564e544e4f544946592d393634383835": "120b00152d02c7e14af6800000", "454c524f4e4465736474494e5445524e532d63393332356601": "0801120b0013097d1fb962e12fff47", "454c524f4e44657364744e455453432d623635306261": "120b00d137965aa7a731800000", - "454c524f4e446e6f6e6365494e5445524e532d633933323566": "01", "454c524f4e44657364745745474c442d613238633539": "120800010593b233281b", + "454c524f4e446e6f6e6365494e5445524e532d633933323566": "01", "454c524f4e446e6f6e63654d4554414e46542d643062623339": "01", "454c524f4e44726f6c6565736474494e5445524e532d633933323566": "0a1145534454526f6c654e46544372656174650a1645534454526f6c654e46544164645175616e74697479", + "454c524f4e44657364744e4943552d393730323932": "120b00d3c21bcecceda1000000", "454c524f4e44726f6c65657364744d4554414e46542d643062623339": "0a1145534454526f6c654e4654437265617465", - "454c524f4e446573647442534b2d343736343730": "120b00021e19e0c9bab23fff7b", - "454c524f4e44657364744e45543253432d306438663962": "120f0004ee2d6d3f3d6bcc25c64dc00000", "454c524f4e4465736474424358535542542d3339326436616c": "080112020001", - "454c524f4e44657364744e4943552d393730323932": "120b00d3c21bcecceda1000000", - "454c524f4e4465736474424358535542542d3339326436616e": "080112020001" + "454c524f4e44657364744e45543253432d306438663962": "120f0004ee2d6d3f3d6bcc25c64dc00000" }, "code": "", "code_hash": "", diff --git a/contracts/examples/adder/interactor/tests/basic_interactor_cs_test.rs b/contracts/examples/adder/interactor/tests/basic_interactor_cs_test.rs index 6d217f66f3..b4e9747794 100644 --- a/contracts/examples/adder/interactor/tests/basic_interactor_cs_test.rs +++ b/contracts/examples/adder/interactor/tests/basic_interactor_cs_test.rs @@ -1,7 +1,9 @@ use basic_interactor::{AdderInteract, Config}; use multiversx_sc_snippets::{imports::Bech32Address, sdk::gateway::SetStateAccount, test_wallets}; +use serial_test::serial; #[tokio::test] +#[serial] #[cfg_attr(not(feature = "chain-simulator-tests"), ignore)] async fn simulator_upgrade_test() { let mut basic_interact = AdderInteract::new(Config::chain_simulator_config()).await; @@ -36,6 +38,7 @@ async fn simulator_upgrade_test() { } #[tokio::test] +#[serial] #[cfg_attr(not(feature = "chain-simulator-tests"), ignore)] async fn set_state_cs_test() { let account_address = test_wallets::mike(); @@ -57,12 +60,17 @@ async fn set_state_cs_test() { let set_state_response = simulator_interact.interactor.set_state(vec_state).await; - let _ = simulator_interact.interactor.generate_blocks(2u64).await; + simulator_interact + .interactor + .generate_blocks(2u64) + .await + .unwrap(); assert!(set_state_response.is_ok()); } #[tokio::test] +#[serial] #[cfg_attr(not(feature = "chain-simulator-tests"), ignore)] async fn set_state_from_file_cs_test() { let account_address = test_wallets::mike(); From 2fac9aa2a4e0f216959582222c04f28f54f69900 Mon Sep 17 00:00:00 2001 From: BiancaIalangi Date: Thu, 28 Nov 2024 13:38:12 +0200 Subject: [PATCH 37/66] update mx-scenario-go v3.0.0 --- .github/workflows/actions-nightly.yml | 4 ++-- .github/workflows/actions.yml | 2 +- .github/workflows/template-test-current.yml | 2 +- .github/workflows/template-test-released.yml | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/actions-nightly.yml b/.github/workflows/actions-nightly.yml index 2539363ff9..06be701e53 100644 --- a/.github/workflows/actions-nightly.yml +++ b/.github/workflows/actions-nightly.yml @@ -19,7 +19,7 @@ jobs: rust-toolchain: nightly-2024-05-22 path-to-sc-meta: framework/meta enable-contracts-size-report: false - mx-scenario-go-version: v2.1.0-alpha + mx-scenario-go-version: v3.0.0 coverage-args: --ignore-filename-regex='meta/src' --ignore-filename-regex='wasm-adapter' --ignore-filename-regex='benchmarks/' --ignore-filename-regex='tests/' --output ./coverage.md secrets: - token: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file + token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/actions.yml b/.github/workflows/actions.yml index 58ea9e80c6..d7bab07611 100644 --- a/.github/workflows/actions.yml +++ b/.github/workflows/actions.yml @@ -18,7 +18,7 @@ jobs: with: rust-toolchain: stable path-to-sc-meta: framework/meta - mx-scenario-go-version: v2.1.0-alpha + mx-scenario-go-version: v3.0.0 coverage-args: --ignore-filename-regex='meta/src' --ignore-filename-regex='wasm-adapter' --ignore-filename-regex='benchmarks/' --ignore-filename-regex='tests/' --output ./coverage.md secrets: token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/template-test-current.yml b/.github/workflows/template-test-current.yml index 56382850fe..9e52703919 100644 --- a/.github/workflows/template-test-current.yml +++ b/.github/workflows/template-test-current.yml @@ -29,7 +29,7 @@ jobs: run: | cargo install wasm-opt cargo install --path framework/meta - sc-meta install mx-scenario-go --tag v2.0.0 + sc-meta install mx-scenario-go --tag v3.0.0 which wasm-opt which mx-scenario-go diff --git a/.github/workflows/template-test-released.yml b/.github/workflows/template-test-released.yml index e6227765db..2e4e8f3ec5 100644 --- a/.github/workflows/template-test-released.yml +++ b/.github/workflows/template-test-released.yml @@ -29,7 +29,7 @@ jobs: run: | cargo install wasm-opt cargo install --path framework/meta - sc-meta install mx-scenario-go --tag v2.0.0 + sc-meta install mx-scenario-go --tag v3.0.0 which wasm-opt which mx-scenario-go From 4b320bedb5550325ce1608b2c1cf4aed4d85d126 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Thu, 28 Nov 2024 17:55:40 +0200 Subject: [PATCH 38/66] ManagedVecRefMut rename --- framework/base/src/types/{io/mod.rs => io.rs} | 0 .../base/src/types/{managed/mod.rs => managed.rs} | 0 .../src/types/managed/{wrapped/mod.rs => wrapped.rs} | 4 ++-- .../base/src/types/managed/wrapped/managed_vec.rs | 8 ++++---- .../{managed_vec_ref.rs => managed_vec_ref_mut.rs} | 10 +++++----- .../types/{static_buffer/mod.rs => static_buffer.rs} | 0 6 files changed, 11 insertions(+), 11 deletions(-) rename framework/base/src/types/{io/mod.rs => io.rs} (100%) rename framework/base/src/types/{managed/mod.rs => managed.rs} (100%) rename framework/base/src/types/managed/{wrapped/mod.rs => wrapped.rs} (96%) rename framework/base/src/types/managed/wrapped/{managed_vec_ref.rs => managed_vec_ref_mut.rs} (88%) rename framework/base/src/types/{static_buffer/mod.rs => static_buffer.rs} (100%) diff --git a/framework/base/src/types/io/mod.rs b/framework/base/src/types/io.rs similarity index 100% rename from framework/base/src/types/io/mod.rs rename to framework/base/src/types/io.rs diff --git a/framework/base/src/types/managed/mod.rs b/framework/base/src/types/managed.rs similarity index 100% rename from framework/base/src/types/managed/mod.rs rename to framework/base/src/types/managed.rs diff --git a/framework/base/src/types/managed/wrapped/mod.rs b/framework/base/src/types/managed/wrapped.rs similarity index 96% rename from framework/base/src/types/managed/wrapped/mod.rs rename to framework/base/src/types/managed/wrapped.rs index 5086c3bc26..8e3efa278f 100644 --- a/framework/base/src/types/managed/wrapped/mod.rs +++ b/framework/base/src/types/managed/wrapped.rs @@ -20,8 +20,8 @@ mod managed_vec_item; mod managed_vec_item_nested_tuple; mod managed_vec_item_payload; mod managed_vec_owned_iter; -mod managed_vec_ref; mod managed_vec_ref_iter; +mod managed_vec_ref_mut; pub(crate) mod preloaded_managed_buffer; mod randomness_source; mod token_identifier; @@ -53,8 +53,8 @@ pub use managed_vec_item::{ pub use managed_vec_item_nested_tuple::ManagedVecItemNestedTuple; pub use managed_vec_item_payload::*; pub use managed_vec_owned_iter::ManagedVecOwnedIterator; -pub use managed_vec_ref::ManagedVecRef; pub use managed_vec_ref_iter::ManagedVecRefIterator; +pub use managed_vec_ref_mut::ManagedVecRefMut; pub use randomness_source::RandomnessSource; pub use token_identifier::TokenIdentifier; diff --git a/framework/base/src/types/managed/wrapped/managed_vec.rs b/framework/base/src/types/managed/wrapped/managed_vec.rs index 2fd2d5fa3a..ab93734a06 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec.rs @@ -8,8 +8,8 @@ use crate::{ TopEncodeMultiOutput, TopEncodeOutput, }, types::{ - ManagedBuffer, ManagedBufferNestedDecodeInput, ManagedType, ManagedVecItem, ManagedVecRef, - ManagedVecRefIterator, MultiValueEncoded, MultiValueManagedVec, + ManagedBuffer, ManagedBufferNestedDecodeInput, ManagedType, ManagedVecItem, + ManagedVecRefIterator, ManagedVecRefMut, MultiValueEncoded, MultiValueManagedVec, }, }; use alloc::{format, vec::Vec}; @@ -182,8 +182,8 @@ where } } - pub fn get_mut(&mut self, index: usize) -> ManagedVecRef { - ManagedVecRef::new(self.get_handle(), index) + pub fn get_mut(&mut self, index: usize) -> ManagedVecRefMut { + ManagedVecRefMut::new(self.get_handle(), index) } pub(super) unsafe fn get_unsafe(&self, index: usize) -> T { diff --git a/framework/base/src/types/managed/wrapped/managed_vec_ref.rs b/framework/base/src/types/managed/wrapped/managed_vec_ref_mut.rs similarity index 88% rename from framework/base/src/types/managed/wrapped/managed_vec_ref.rs rename to framework/base/src/types/managed/wrapped/managed_vec_ref_mut.rs index b339d1cdaf..b3ab840fd1 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_ref.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_ref_mut.rs @@ -10,7 +10,7 @@ use core::{ use super::{ManagedRef, ManagedRefMut}; -pub struct ManagedVecRef<'a, M, T> +pub struct ManagedVecRefMut<'a, M, T> where M: ManagedTypeApi, T: ManagedVecItem, @@ -22,7 +22,7 @@ where item: ManuallyDrop, } -impl<'a, M, T> ManagedVecRef<'a, M, T> +impl<'a, M, T> ManagedVecRefMut<'a, M, T> where M: ManagedTypeApi, T: ManagedVecItem, @@ -47,7 +47,7 @@ where } } -impl<'a, M, T> Drop for ManagedVecRef<'a, M, T> +impl<'a, M, T> Drop for ManagedVecRefMut<'a, M, T> where M: ManagedTypeApi, T: ManagedVecItem, @@ -63,7 +63,7 @@ where } } -impl<'a, M, T> Deref for ManagedVecRef<'a, M, T> +impl<'a, M, T> Deref for ManagedVecRefMut<'a, M, T> where M: ManagedTypeApi, T: ManagedVecItem, @@ -75,7 +75,7 @@ where } } -impl<'a, M, T> DerefMut for ManagedVecRef<'a, M, T> +impl<'a, M, T> DerefMut for ManagedVecRefMut<'a, M, T> where M: ManagedTypeApi, T: ManagedVecItem, diff --git a/framework/base/src/types/static_buffer/mod.rs b/framework/base/src/types/static_buffer.rs similarity index 100% rename from framework/base/src/types/static_buffer/mod.rs rename to framework/base/src/types/static_buffer.rs From 69c69746c8e310b9c8c382ee06788f7037caef5c Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Thu, 28 Nov 2024 18:37:23 +0200 Subject: [PATCH 39/66] clippy fix (rustc 1.83.0) --- .../system_sc/system_sc_unimplemented.rs | 2 +- .../crypto-kitties/common/random/src/lib.rs | 1 + data/codec/src/impl_for_types/impl_phantom.rs | 1 - data/codec/src/single/nested_de_input_slice.rs | 2 +- data/codec/src/single/top_en_output.rs | 1 - framework/base/src/lib.rs | 1 + framework/base/src/storage/mappers/map_mapper.rs | 2 +- .../base/src/types/managed/basic/managed_buffer.rs | 1 - .../base/src/types/managed/wrapped/managed_ref.rs | 4 ++-- .../src/types/managed/wrapped/managed_ref_mut.rs | 14 +++++++------- .../meta/src/cmd/code_report/render_code_report.rs | 10 +++++----- framework/meta/src/cmd/template/template_source.rs | 2 +- framework/scenario/src/facade/expr/file_path.rs | 6 +++--- framework/scenario/src/lib.rs | 1 + .../interactor_tx/interactor_exec_env.rs | 6 +++--- .../interactor_tx/interactor_exec_transf.rs | 2 +- .../interactor_tx/interactor_query_env.rs | 4 ++-- framework/snippets/src/multi/interactor_step.rs | 2 +- framework/wasm-adapter/src/api/unsafe_buffer.rs | 2 ++ .../src/wasm_alloc/leaking_allocator.rs | 2 +- sdk/core/src/crypto/public_key.rs | 2 +- sdk/core/src/gateway/gateway_account.rs | 2 +- sdk/core/src/gateway/gateway_account_esdt_roles.rs | 2 +- .../src/gateway/gateway_account_esdt_tokens.rs | 2 +- sdk/core/src/gateway/gateway_account_storage.rs | 2 +- sdk/core/src/gateway/gateway_tx_cost.rs | 2 +- sdk/core/src/gateway/gateway_tx_info.rs | 2 +- sdk/core/src/gateway/gateway_tx_process_status.rs | 2 +- sdk/core/src/gateway/gateway_tx_send.rs | 2 +- sdk/core/src/gateway/gateway_tx_send_multi.rs | 2 +- sdk/core/src/gateway/gateway_tx_status.rs | 2 +- sdk/core/src/gateway/gateway_tx_vmquery.rs | 2 +- sdk/core/src/retrieve_tx_on_network.rs | 2 +- .../src/value_interpreter/reconstructor.rs | 2 +- 34 files changed, 48 insertions(+), 46 deletions(-) diff --git a/chain/vm/src/tx_execution/system_sc/system_sc_unimplemented.rs b/chain/vm/src/tx_execution/system_sc/system_sc_unimplemented.rs index cf93be7727..a324f34900 100644 --- a/chain/vm/src/tx_execution/system_sc/system_sc_unimplemented.rs +++ b/chain/vm/src/tx_execution/system_sc/system_sc_unimplemented.rs @@ -3,8 +3,8 @@ use crate::tx_mock::{BlockchainUpdate, TxCache, TxInput, TxResult}; /// Every unimplemented fn will be implemented and moved to its corresponding file. +/// /// This file will be deleted. - pub fn register_meta_esdt(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdate) { unimplemented!() } diff --git a/contracts/examples/crypto-kitties/common/random/src/lib.rs b/contracts/examples/crypto-kitties/common/random/src/lib.rs index 46ddd076d6..1cfb5e3fa2 100644 --- a/contracts/examples/crypto-kitties/common/random/src/lib.rs +++ b/contracts/examples/crypto-kitties/common/random/src/lib.rs @@ -24,6 +24,7 @@ pub trait Randomizeable { impl Random { /// block random seed + salt creates a stronger randomness source + #[allow(static_mut_refs)] pub fn new( seed: ManagedByteArray, salt: ManagedByteArray, diff --git a/data/codec/src/impl_for_types/impl_phantom.rs b/data/codec/src/impl_for_types/impl_phantom.rs index 6036152f02..60087753b6 100644 --- a/data/codec/src/impl_for_types/impl_phantom.rs +++ b/data/codec/src/impl_for_types/impl_phantom.rs @@ -8,7 +8,6 @@ use crate::{ /// Empty structure with an empty bytes representation. Equivalent to `false`, `0` or `[u8; 0]`, but more explicit. /// /// Note: the unit type `()` would have naturally fit this role, but we decided to make the unit type multi-value only. - impl TopEncode for PhantomData { #[inline] fn top_encode_or_handle_err(&self, output: O, _h: H) -> Result<(), H::HandledErr> diff --git a/data/codec/src/single/nested_de_input_slice.rs b/data/codec/src/single/nested_de_input_slice.rs index ce399a8de9..b0dd0eed51 100644 --- a/data/codec/src/single/nested_de_input_slice.rs +++ b/data/codec/src/single/nested_de_input_slice.rs @@ -1,7 +1,7 @@ use crate::{DecodeError, DecodeErrorHandler, NestedDecode, NestedDecodeInput}; /// A nested decode buffer implementation on referenced data. -impl<'a> NestedDecodeInput for &'a [u8] { +impl NestedDecodeInput for &[u8] { fn remaining_len(&self) -> usize { self.len() } diff --git a/data/codec/src/single/top_en_output.rs b/data/codec/src/single/top_en_output.rs index 4f955ca2f2..b2d2618d39 100644 --- a/data/codec/src/single/top_en_output.rs +++ b/data/codec/src/single/top_en_output.rs @@ -5,7 +5,6 @@ use crate::{ use alloc::vec::Vec; /// Specifies objects that can receive the result of a TopEncode computation. - /// in principle from NestedEncode performed on nested items. /// /// All methods consume the object, so they can only be called once. diff --git a/framework/base/src/lib.rs b/framework/base/src/lib.rs index 9f6680af7c..f6c765e67b 100644 --- a/framework/base/src/lib.rs +++ b/framework/base/src/lib.rs @@ -1,5 +1,6 @@ #![no_std] #![allow(deprecated)] +#![allow(clippy::needless_lifetimes)] // TODO: fix them all! pub use multiversx_sc_derive::{self as derive, contract, module, proxy}; diff --git a/framework/base/src/storage/mappers/map_mapper.rs b/framework/base/src/storage/mappers/map_mapper.rs index 0da042334a..9e90917985 100644 --- a/framework/base/src/storage/mappers/map_mapper.rs +++ b/framework/base/src/storage/mappers/map_mapper.rs @@ -438,7 +438,7 @@ where } } -impl<'a, SA, A, K, V> VacantEntry<'a, SA, A, K, V> +impl VacantEntry<'_, SA, A, K, V> where SA: StorageMapperApi, A: StorageAddress, diff --git a/framework/base/src/types/managed/basic/managed_buffer.rs b/framework/base/src/types/managed/basic/managed_buffer.rs index cafa32a4d2..d3c0c28e38 100644 --- a/framework/base/src/types/managed/basic/managed_buffer.rs +++ b/framework/base/src/types/managed/basic/managed_buffer.rs @@ -94,7 +94,6 @@ impl ManagedBuffer { /// ## Safety /// /// The reference points to a shared value. Make sure the handle is not leaked. - pub unsafe fn temp_const_ref( raw_handle: RawHandle, ) -> ManagedRef<'static, M, ManagedBuffer> { diff --git a/framework/base/src/types/managed/wrapped/managed_ref.rs b/framework/base/src/types/managed/wrapped/managed_ref.rs index ef0e20fb9f..f618063b3c 100644 --- a/framework/base/src/types/managed/wrapped/managed_ref.rs +++ b/framework/base/src/types/managed/wrapped/managed_ref.rs @@ -143,7 +143,7 @@ where } } -impl<'a, M, T> NestedEncode for ManagedRef<'a, M, T> +impl NestedEncode for ManagedRef<'_, M, T> where M: ManagedTypeApi, T: ManagedType + NestedEncode, @@ -158,7 +158,7 @@ where } } -impl<'a, M, T> core::fmt::Debug for ManagedRef<'a, M, T> +impl core::fmt::Debug for ManagedRef<'_, M, T> where M: ManagedTypeApi, T: ManagedType + core::fmt::Debug, diff --git a/framework/base/src/types/managed/wrapped/managed_ref_mut.rs b/framework/base/src/types/managed/wrapped/managed_ref_mut.rs index 81315b89b2..868ef89e78 100644 --- a/framework/base/src/types/managed/wrapped/managed_ref_mut.rs +++ b/framework/base/src/types/managed/wrapped/managed_ref_mut.rs @@ -20,12 +20,12 @@ where pub(super) handle: T::OwnHandle, } -impl<'a, M, T> ManagedRefMut<'a, M, T> +impl ManagedRefMut<'_, M, T> where M: ManagedTypeApi, T: ManagedType, { - pub fn new(value: &'a mut T) -> Self { + pub fn new(value: &mut T) -> Self { Self { _phantom_m: PhantomData, _phantom_t: PhantomData, @@ -44,7 +44,7 @@ where } } -impl<'a, M, T> ManagedRefMut<'a, M, T> +impl ManagedRefMut<'_, M, T> where M: ManagedTypeApi, T: ManagedType + Clone, @@ -55,7 +55,7 @@ where } } -impl<'a, M, T> Clone for ManagedRefMut<'a, M, T> +impl Clone for ManagedRefMut<'_, M, T> where M: ManagedTypeApi, T: ManagedType, @@ -70,7 +70,7 @@ where } } -impl<'a, M, T> Deref for ManagedRefMut<'a, M, T> +impl Deref for ManagedRefMut<'_, M, T> where M: ManagedTypeApi, T: ManagedType, @@ -83,7 +83,7 @@ where } } -impl<'a, M, T> DerefMut for ManagedRefMut<'a, M, T> +impl DerefMut for ManagedRefMut<'_, M, T> where M: ManagedTypeApi, T: ManagedType, @@ -93,7 +93,7 @@ where } } -impl<'a, M, T> Borrow for ManagedRefMut<'a, M, T> +impl Borrow for ManagedRefMut<'_, M, T> where M: ManagedTypeApi, T: ManagedType, diff --git a/framework/meta/src/cmd/code_report/render_code_report.rs b/framework/meta/src/cmd/code_report/render_code_report.rs index e6edcfae8e..f72a05abdf 100644 --- a/framework/meta/src/cmd/code_report/render_code_report.rs +++ b/framework/meta/src/cmd/code_report/render_code_report.rs @@ -61,14 +61,14 @@ impl<'a> CodeReportRender<'a> { fn write_report_for_contract( &mut self, - path: &String, - size: &String, - has_allocator: &String, - has_panic: &String, + path: &str, + size: &str, + has_allocator: &str, + has_panic: &str, ) { self.writeln(format!( "| {} | {} | {} | {} |", - path.split('/').last().unwrap_or_else(|| path), + path.split('/').last().unwrap_or(path), size, has_allocator, has_panic diff --git a/framework/meta/src/cmd/template/template_source.rs b/framework/meta/src/cmd/template/template_source.rs index ea4140cd31..c8f83ac87e 100644 --- a/framework/meta/src/cmd/template/template_source.rs +++ b/framework/meta/src/cmd/template/template_source.rs @@ -16,7 +16,7 @@ pub struct TemplateSource<'a> { pub metadata: TemplateMetadata, } -impl<'a> TemplateSource<'a> { +impl TemplateSource<'_> { pub fn copy_template(&self, target_path: impl AsRef, args_tag: FrameworkVersion) { whitelisted_deep_copy( &self.source_path, diff --git a/framework/scenario/src/facade/expr/file_path.rs b/framework/scenario/src/facade/expr/file_path.rs index 8d779a6fd1..0c05165fb2 100644 --- a/framework/scenario/src/facade/expr/file_path.rs +++ b/framework/scenario/src/facade/expr/file_path.rs @@ -12,7 +12,7 @@ const FILE_PREFIX: &str = "file:"; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct FilePath<'a>(pub &'a str); -impl<'a> FilePath<'a> { +impl FilePath<'_> { pub fn eval_to_expr(&self) -> String { format!("{FILE_PREFIX}{}", self.0) } @@ -36,9 +36,9 @@ where } } -impl<'a, Env> TxCodeValue for FilePath<'a> where Env: ScenarioTxEnv {} +impl TxCodeValue for FilePath<'_> where Env: ScenarioTxEnv {} -impl<'a> RegisterCodeSource for FilePath<'a> { +impl RegisterCodeSource for FilePath<'_> { fn into_code(self, env_data: ScenarioTxEnvData) -> Vec { self.resolve_contents(&env_data.interpreter_context()) } diff --git a/framework/scenario/src/lib.rs b/framework/scenario/src/lib.rs index cfac7eaa50..46238e2ab9 100644 --- a/framework/scenario/src/lib.rs +++ b/framework/scenario/src/lib.rs @@ -1,4 +1,5 @@ #![allow(clippy::type_complexity)] +#![allow(clippy::needless_lifetimes)] // TODO: fix them all! pub mod api; pub mod bech32; diff --git a/framework/snippets/src/interactor/interactor_tx/interactor_exec_env.rs b/framework/snippets/src/interactor/interactor_tx/interactor_exec_env.rs index c7987d28e6..ba01813f9c 100644 --- a/framework/snippets/src/interactor/interactor_tx/interactor_exec_env.rs +++ b/framework/snippets/src/interactor/interactor_tx/interactor_exec_env.rs @@ -30,7 +30,7 @@ where pub data: ScenarioTxEnvData, } -impl<'w, GatewayProxy> TxEnv for InteractorEnvExec<'w, GatewayProxy> +impl TxEnv for InteractorEnvExec<'_, GatewayProxy> where GatewayProxy: GatewayAsyncService, { @@ -51,7 +51,7 @@ where } } -impl<'w, GatewayProxy> ScenarioTxEnv for InteractorEnvExec<'w, GatewayProxy> +impl ScenarioTxEnv for InteractorEnvExec<'_, GatewayProxy> where GatewayProxy: GatewayAsyncService, { @@ -60,7 +60,7 @@ where } } -impl<'w, GatewayProxy> TxEnvWithTxHash for InteractorEnvExec<'w, GatewayProxy> +impl TxEnvWithTxHash for InteractorEnvExec<'_, GatewayProxy> where GatewayProxy: GatewayAsyncService, { diff --git a/framework/snippets/src/interactor/interactor_tx/interactor_exec_transf.rs b/framework/snippets/src/interactor/interactor_tx/interactor_exec_transf.rs index bb7559ba93..1dade9dc6b 100644 --- a/framework/snippets/src/interactor/interactor_tx/interactor_exec_transf.rs +++ b/framework/snippets/src/interactor/interactor_tx/interactor_exec_transf.rs @@ -54,7 +54,7 @@ where } } -impl<'w, GatewayProxy> InteractorExecStep<'w, GatewayProxy, TransferStep, ()> +impl InteractorExecStep<'_, GatewayProxy, TransferStep, ()> where GatewayProxy: GatewayAsyncService, { diff --git a/framework/snippets/src/interactor/interactor_tx/interactor_query_env.rs b/framework/snippets/src/interactor/interactor_tx/interactor_query_env.rs index 25d0b5e822..dbd2066a66 100644 --- a/framework/snippets/src/interactor/interactor_tx/interactor_query_env.rs +++ b/framework/snippets/src/interactor/interactor_tx/interactor_query_env.rs @@ -27,7 +27,7 @@ where pub data: ScenarioTxEnvData, } -impl<'w, GatewayProxy> TxEnv for InteractorEnvQuery<'w, GatewayProxy> +impl TxEnv for InteractorEnvQuery<'_, GatewayProxy> where GatewayProxy: GatewayAsyncService, { @@ -48,7 +48,7 @@ where } } -impl<'w, GatewayProxy> ScenarioTxEnv for InteractorEnvQuery<'w, GatewayProxy> +impl ScenarioTxEnv for InteractorEnvQuery<'_, GatewayProxy> where GatewayProxy: GatewayAsyncService, { diff --git a/framework/snippets/src/multi/interactor_step.rs b/framework/snippets/src/multi/interactor_step.rs index 93a93d6a9e..b8df05a9a5 100644 --- a/framework/snippets/src/multi/interactor_step.rs +++ b/framework/snippets/src/multi/interactor_step.rs @@ -13,7 +13,7 @@ pub enum InteractorStepRef<'a> { ScDeploy(&'a mut ScDeployStep), } -impl<'a> InteractorStepRef<'a> { +impl InteractorStepRef<'_> { pub fn to_transaction( &self, interactor: &InteractorBase, diff --git a/framework/wasm-adapter/src/api/unsafe_buffer.rs b/framework/wasm-adapter/src/api/unsafe_buffer.rs index c18034de6b..8794203daa 100644 --- a/framework/wasm-adapter/src/api/unsafe_buffer.rs +++ b/framework/wasm-adapter/src/api/unsafe_buffer.rs @@ -11,10 +11,12 @@ static mut BUFFER_1: [u8; BUFFER_1_SIZE] = [0u8; BUFFER_1_SIZE]; /// The second buffer is for when the first one is busy with something else. static mut BUFFER_2: [u8; BUFFER_2_SIZE] = [0u8; BUFFER_2_SIZE]; +#[allow(static_mut_refs)] pub(crate) unsafe fn buffer_1_ptr() -> *mut u8 { BUFFER_1.as_mut_ptr() } +#[allow(static_mut_refs)] pub(crate) unsafe fn buffer_2_ptr() -> *mut u8 { BUFFER_2.as_mut_ptr() } diff --git a/framework/wasm-adapter/src/wasm_alloc/leaking_allocator.rs b/framework/wasm-adapter/src/wasm_alloc/leaking_allocator.rs index 8a40b0ac68..06e246f0e4 100644 --- a/framework/wasm-adapter/src/wasm_alloc/leaking_allocator.rs +++ b/framework/wasm-adapter/src/wasm_alloc/leaking_allocator.rs @@ -45,7 +45,7 @@ unsafe impl GlobalAlloc for LeakingAllocator { if new_total > *size { // Request enough new space for this allocation, even if we have some space left over from the last one incase they end up non-contiguous. // Round up to a number of pages - let requested_pages = (requested_size + PAGE_SIZE - 1) / PAGE_SIZE; + let requested_pages = requested_size.div_ceil(PAGE_SIZE); let previous_page_count = memory_grow(PageCount(requested_pages)); let previous_size = previous_page_count.size_in_bytes(); diff --git a/sdk/core/src/crypto/public_key.rs b/sdk/core/src/crypto/public_key.rs index 448bf57e9d..6ad8604ecb 100644 --- a/sdk/core/src/crypto/public_key.rs +++ b/sdk/core/src/crypto/public_key.rs @@ -34,7 +34,7 @@ impl PublicKey { } } -impl<'a> From<&'a PrivateKey> for PublicKey { +impl From<&PrivateKey> for PublicKey { fn from(private_key: &PrivateKey) -> PublicKey { let bytes = private_key.to_bytes(); diff --git a/sdk/core/src/gateway/gateway_account.rs b/sdk/core/src/gateway/gateway_account.rs index 76eee4c736..913c55d0eb 100644 --- a/sdk/core/src/gateway/gateway_account.rs +++ b/sdk/core/src/gateway/gateway_account.rs @@ -16,7 +16,7 @@ impl<'a> GetAccountRequest<'a> { } } -impl<'a> GatewayRequest for GetAccountRequest<'a> { +impl GatewayRequest for GetAccountRequest<'_> { type Payload = (); type DecodedJson = AccountResponse; type Result = Account; diff --git a/sdk/core/src/gateway/gateway_account_esdt_roles.rs b/sdk/core/src/gateway/gateway_account_esdt_roles.rs index 421edd81b2..d7b19e8e41 100644 --- a/sdk/core/src/gateway/gateway_account_esdt_roles.rs +++ b/sdk/core/src/gateway/gateway_account_esdt_roles.rs @@ -16,7 +16,7 @@ impl<'a> GetAccountEsdtRolesRequest<'a> { } } -impl<'a> GatewayRequest for GetAccountEsdtRolesRequest<'a> { +impl GatewayRequest for GetAccountEsdtRolesRequest<'_> { type Payload = (); type DecodedJson = EsdtRolesResponse; type Result = HashMap>; diff --git a/sdk/core/src/gateway/gateway_account_esdt_tokens.rs b/sdk/core/src/gateway/gateway_account_esdt_tokens.rs index 72e27e54a9..e2916fe840 100644 --- a/sdk/core/src/gateway/gateway_account_esdt_tokens.rs +++ b/sdk/core/src/gateway/gateway_account_esdt_tokens.rs @@ -16,7 +16,7 @@ impl<'a> GetAccountEsdtTokensRequest<'a> { } } -impl<'a> GatewayRequest for GetAccountEsdtTokensRequest<'a> { +impl GatewayRequest for GetAccountEsdtTokensRequest<'_> { type Payload = (); type DecodedJson = EsdtBalanceResponse; type Result = HashMap; diff --git a/sdk/core/src/gateway/gateway_account_storage.rs b/sdk/core/src/gateway/gateway_account_storage.rs index 6c1bffe55f..011d29d460 100644 --- a/sdk/core/src/gateway/gateway_account_storage.rs +++ b/sdk/core/src/gateway/gateway_account_storage.rs @@ -16,7 +16,7 @@ impl<'a> GetAccountStorageRequest<'a> { } } -impl<'a> GatewayRequest for GetAccountStorageRequest<'a> { +impl GatewayRequest for GetAccountStorageRequest<'_> { type Payload = (); type DecodedJson = AccountStorageResponse; type Result = HashMap; diff --git a/sdk/core/src/gateway/gateway_tx_cost.rs b/sdk/core/src/gateway/gateway_tx_cost.rs index 333b0324c5..3ca8eff823 100644 --- a/sdk/core/src/gateway/gateway_tx_cost.rs +++ b/sdk/core/src/gateway/gateway_tx_cost.rs @@ -8,7 +8,7 @@ use super::{GatewayRequest, GatewayRequestType, COST_TRANSACTION_ENDPOINT}; /// Note: it is a POST request. pub struct GetTxCost<'a>(pub &'a Transaction); -impl<'a> GatewayRequest for GetTxCost<'a> { +impl GatewayRequest for GetTxCost<'_> { type Payload = Transaction; type DecodedJson = ResponseTxCost; type Result = TxCostResponseData; diff --git a/sdk/core/src/gateway/gateway_tx_info.rs b/sdk/core/src/gateway/gateway_tx_info.rs index f4d26e9d44..d9cc12d02b 100644 --- a/sdk/core/src/gateway/gateway_tx_info.rs +++ b/sdk/core/src/gateway/gateway_tx_info.rs @@ -27,7 +27,7 @@ impl<'a> GetTxInfo<'a> { } } -impl<'a> GatewayRequest for GetTxInfo<'a> { +impl GatewayRequest for GetTxInfo<'_> { type Payload = (); type DecodedJson = TransactionInfo; type Result = TransactionOnNetwork; diff --git a/sdk/core/src/gateway/gateway_tx_process_status.rs b/sdk/core/src/gateway/gateway_tx_process_status.rs index d829cb60fe..103bb4e4a6 100644 --- a/sdk/core/src/gateway/gateway_tx_process_status.rs +++ b/sdk/core/src/gateway/gateway_tx_process_status.rs @@ -14,7 +14,7 @@ impl<'a> GetTxProcessStatus<'a> { } } -impl<'a> GatewayRequest for GetTxProcessStatus<'a> { +impl GatewayRequest for GetTxProcessStatus<'_> { type Payload = (); type DecodedJson = TransactionProcessStatus; type Result = (String, String); diff --git a/sdk/core/src/gateway/gateway_tx_send.rs b/sdk/core/src/gateway/gateway_tx_send.rs index a76354df8e..4c3b9d584a 100644 --- a/sdk/core/src/gateway/gateway_tx_send.rs +++ b/sdk/core/src/gateway/gateway_tx_send.rs @@ -6,7 +6,7 @@ use super::{GatewayRequest, GatewayRequestType, SEND_TRANSACTION_ENDPOINT}; /// Sends a single transaction. pub struct SendTxRequest<'a>(pub &'a Transaction); -impl<'a> GatewayRequest for SendTxRequest<'a> { +impl GatewayRequest for SendTxRequest<'_> { type Payload = Transaction; type DecodedJson = SendTransactionResponse; type Result = String; diff --git a/sdk/core/src/gateway/gateway_tx_send_multi.rs b/sdk/core/src/gateway/gateway_tx_send_multi.rs index 472beaf5ba..8be1d1d4c0 100644 --- a/sdk/core/src/gateway/gateway_tx_send_multi.rs +++ b/sdk/core/src/gateway/gateway_tx_send_multi.rs @@ -7,7 +7,7 @@ use super::{GatewayRequest, GatewayRequestType, SEND_MULTIPLE_TRANSACTIONS_ENDPO /// Sends multiple transactions at once. pub struct SendMultiTxRequest<'a>(pub &'a [Transaction]); -impl<'a> GatewayRequest for SendMultiTxRequest<'a> { +impl GatewayRequest for SendMultiTxRequest<'_> { type Payload = [Transaction]; type DecodedJson = SendTransactionsResponse; type Result = Vec; diff --git a/sdk/core/src/gateway/gateway_tx_status.rs b/sdk/core/src/gateway/gateway_tx_status.rs index ac4e37e6b4..12660a2c57 100644 --- a/sdk/core/src/gateway/gateway_tx_status.rs +++ b/sdk/core/src/gateway/gateway_tx_status.rs @@ -14,7 +14,7 @@ impl<'a> GetTxStatus<'a> { } } -impl<'a> GatewayRequest for GetTxStatus<'a> { +impl GatewayRequest for GetTxStatus<'_> { type Payload = (); type DecodedJson = TransactionStatus; type Result = String; diff --git a/sdk/core/src/gateway/gateway_tx_vmquery.rs b/sdk/core/src/gateway/gateway_tx_vmquery.rs index e1523571da..9de88f6d2f 100644 --- a/sdk/core/src/gateway/gateway_tx_vmquery.rs +++ b/sdk/core/src/gateway/gateway_tx_vmquery.rs @@ -6,7 +6,7 @@ use super::{GatewayRequest, GatewayRequestType, VM_VALUES_ENDPOINT}; /// Executes a VM query. pub struct VMQueryRequest<'a>(pub &'a VMQueryInput); -impl<'a> GatewayRequest for VMQueryRequest<'a> { +impl GatewayRequest for VMQueryRequest<'_> { type Payload = VMQueryInput; type DecodedJson = ResponseVmValue; type Result = VmValuesResponseData; diff --git a/sdk/core/src/retrieve_tx_on_network.rs b/sdk/core/src/retrieve_tx_on_network.rs index 3cfcf9fc77..3c60a92fa2 100644 --- a/sdk/core/src/retrieve_tx_on_network.rs +++ b/sdk/core/src/retrieve_tx_on_network.rs @@ -138,7 +138,7 @@ pub fn extract_message_from_string_reason(reason: &str) -> String { return message[0].to_string(); } - return contract_error.last().unwrap_or(&"").split(']').collect(); + contract_error.last().unwrap_or(&"").split(']').collect() } fn create_tx_failed(error_message: &str) -> TransactionOnNetwork { diff --git a/sdk/scenario-format/src/value_interpreter/reconstructor.rs b/sdk/scenario-format/src/value_interpreter/reconstructor.rs index 6efacefba4..e21a298905 100644 --- a/sdk/scenario-format/src/value_interpreter/reconstructor.rs +++ b/sdk/scenario-format/src/value_interpreter/reconstructor.rs @@ -147,7 +147,7 @@ fn can_interpret_as_string(bytes: &[u8]) -> bool { if bytes.is_empty() { return false; } - return !bytes.iter().any(|&b| !(32..=126).contains(&b)); + !bytes.iter().any(|&b| !(32..=126).contains(&b)) } fn code_pretty(bytes: &[u8]) -> String { From d173698762be2e4d7be5ab1dd379c0915a8625ec Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Thu, 28 Nov 2024 18:44:43 +0200 Subject: [PATCH 40/66] clippy fix --- .../base/src/types/managed/wrapped/managed_vec_ref_mut.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/framework/base/src/types/managed/wrapped/managed_vec_ref_mut.rs b/framework/base/src/types/managed/wrapped/managed_vec_ref_mut.rs index b3ab840fd1..37a642648f 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_ref_mut.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_ref_mut.rs @@ -22,7 +22,7 @@ where item: ManuallyDrop, } -impl<'a, M, T> ManagedVecRefMut<'a, M, T> +impl ManagedVecRefMut<'_, M, T> where M: ManagedTypeApi, T: ManagedVecItem, @@ -47,7 +47,7 @@ where } } -impl<'a, M, T> Drop for ManagedVecRefMut<'a, M, T> +impl Drop for ManagedVecRefMut<'_, M, T> where M: ManagedTypeApi, T: ManagedVecItem, @@ -63,7 +63,7 @@ where } } -impl<'a, M, T> Deref for ManagedVecRefMut<'a, M, T> +impl Deref for ManagedVecRefMut<'_, M, T> where M: ManagedTypeApi, T: ManagedVecItem, @@ -75,7 +75,7 @@ where } } -impl<'a, M, T> DerefMut for ManagedVecRefMut<'a, M, T> +impl DerefMut for ManagedVecRefMut<'_, M, T> where M: ManagedTypeApi, T: ManagedVecItem, From 31c3a9a3e2382352a66c3630331153e2300b1954 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Thu, 28 Nov 2024 19:13:37 +0200 Subject: [PATCH 41/66] ManagedVecItem - lo longer implemented for handles directly --- .../base/src/api/managed_types/handles.rs | 7 ++---- .../src/api/impl_vh/debug_handle_vh.rs | 23 +------------------ 2 files changed, 3 insertions(+), 27 deletions(-) diff --git a/framework/base/src/api/managed_types/handles.rs b/framework/base/src/api/managed_types/handles.rs index 5a8f17ba9d..4c4c89a719 100644 --- a/framework/base/src/api/managed_types/handles.rs +++ b/framework/base/src/api/managed_types/handles.rs @@ -10,15 +10,12 @@ pub trait HandleTypeInfo { use crate::codec::TryStaticCast; -use crate::{ - api::{ErrorApi, ErrorApiImpl}, - types::ManagedVecItem, -}; +use crate::api::{ErrorApi, ErrorApiImpl}; pub type RawHandle = i32; pub trait HandleConstraints: - ManagedVecItem + TryStaticCast + Debug + Clone + From + PartialEq + PartialEq + TryStaticCast + Debug + Clone + From + PartialEq + PartialEq { fn new(handle: RawHandle) -> Self; fn to_be_bytes(&self) -> [u8; 4]; diff --git a/framework/scenario/src/api/impl_vh/debug_handle_vh.rs b/framework/scenario/src/api/impl_vh/debug_handle_vh.rs index 64fcd15fa9..5c2a60c022 100644 --- a/framework/scenario/src/api/impl_vh/debug_handle_vh.rs +++ b/framework/scenario/src/api/impl_vh/debug_handle_vh.rs @@ -1,8 +1,7 @@ use multiversx_chain_vm::tx_mock::{TxContext, TxContextStack}; use multiversx_sc::{ - api::{use_raw_handle, HandleConstraints, RawHandle}, + api::{HandleConstraints, RawHandle}, codec::TryStaticCast, - types::ManagedVecItem, }; use std::sync::Arc; @@ -76,24 +75,4 @@ impl From for DebugHandle { } } -impl ManagedVecItem for DebugHandle { - type PAYLOAD = ::PAYLOAD; - - const SKIPS_RESERIALIZATION: bool = ::SKIPS_RESERIALIZATION; - - type Ref<'a> = Self; - - fn read_from_payload(payload: &Self::PAYLOAD) -> Self { - use_raw_handle(RawHandle::read_from_payload(payload)) - } - - unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> { - Self::read_from_payload(payload) - } - - fn save_to_payload(self, payload: &mut Self::PAYLOAD) { - self.get_raw_handle().save_to_payload(payload); - } -} - impl TryStaticCast for DebugHandle {} From 16fac80f5e22e8d0c85cb1ca588edb5e1aa57f0c Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Fri, 29 Nov 2024 10:58:10 +0200 Subject: [PATCH 42/66] ManagedVecItem - struct derive yields ManagedVecRef --- .../examples/order-book/pair/src/common.rs | 2 +- .../examples/order-book/pair/src/events.rs | 8 +-- .../examples/order-book/pair/src/orders.rs | 2 +- .../src/rewards_distribution.rs | 6 +-- .../src/distribution_module.rs | 2 +- .../src/governance/governance_proposal.rs | 2 +- contracts/modules/src/governance/mod.rs | 4 +- .../esdt_token_payment_multi_value.rs | 7 ++- framework/base/src/types/managed/wrapped.rs | 2 + .../types/managed/wrapped/managed_vec_item.rs | 6 +-- .../types/managed/wrapped/managed_vec_ref.rs | 54 +++++++++++++++++++ .../derive/src/managed_vec_item_derive.rs | 5 +- 12 files changed, 77 insertions(+), 23 deletions(-) create mode 100644 framework/base/src/types/managed/wrapped/managed_vec_ref.rs diff --git a/contracts/examples/order-book/pair/src/common.rs b/contracts/examples/order-book/pair/src/common.rs index 7728e4c364..85b1c1bca8 100644 --- a/contracts/examples/order-book/pair/src/common.rs +++ b/contracts/examples/order-book/pair/src/common.rs @@ -8,7 +8,7 @@ pub const FREE_ORDER_FROM_STORAGE_MIN_PENALTIES: u64 = 6; #[type_abi] #[derive( - ManagedVecItem, TopEncode, TopDecode, NestedEncode, NestedDecode, PartialEq, Eq, Clone, + ManagedVecItem, TopEncode, TopDecode, NestedEncode, NestedDecode, PartialEq, Eq, Clone, Copy )] pub enum OrderType { Buy, diff --git a/contracts/examples/order-book/pair/src/events.rs b/contracts/examples/order-book/pair/src/events.rs index ddbd6a3496..f20331f44f 100644 --- a/contracts/examples/order-book/pair/src/events.rs +++ b/contracts/examples/order-book/pair/src/events.rs @@ -31,7 +31,7 @@ pub trait EventsModule { for order in orders.iter() { let order_type = order.order_type; let order_id = order.id; - let order_creator = order.creator; + let order_creator = &order.creator; self.match_order_event(&caller, epoch, order_type, order_id, order_creator); } @@ -44,7 +44,7 @@ pub trait EventsModule { for order in orders.iter() { let order_type = order.order_type; let order_id = order.id; - let order_creator = order.creator; + let order_creator = &order.creator; self.free_order_event(&caller, epoch, order_type, order_id, order_creator); } @@ -75,7 +75,7 @@ pub trait EventsModule { #[indexed] epoch: u64, #[indexed] order_type: OrderType, #[indexed] order_id: u64, - #[indexed] order_creator: ManagedAddress, + #[indexed] order_creator: &ManagedAddress, ); #[event("free_order")] @@ -85,6 +85,6 @@ pub trait EventsModule { #[indexed] epoch: u64, #[indexed] order_type: OrderType, #[indexed] order_id: u64, - #[indexed] order_creator: ManagedAddress, + #[indexed] order_creator: &ManagedAddress, ); } diff --git a/contracts/examples/order-book/pair/src/orders.rs b/contracts/examples/order-book/pair/src/orders.rs index 1ec11afee4..2452cb0b16 100644 --- a/contracts/examples/order-book/pair/src/orders.rs +++ b/contracts/examples/order-book/pair/src/orders.rs @@ -282,7 +282,7 @@ pub trait OrdersModule: let mut orders_vec = MultiValueManagedVec::new(); for order in orders.iter() { if order.order_type == order_type { - orders_vec.push(order); + orders_vec.push(order.clone()); } } diff --git a/contracts/examples/rewards-distribution/src/rewards_distribution.rs b/contracts/examples/rewards-distribution/src/rewards_distribution.rs index 7300fcaf88..ecbf2b7d7c 100644 --- a/contracts/examples/rewards-distribution/src/rewards_distribution.rs +++ b/contracts/examples/rewards-distribution/src/rewards_distribution.rs @@ -21,7 +21,7 @@ pub struct Bracket { } #[type_abi] -#[derive(ManagedVecItem, NestedEncode, NestedDecode)] +#[derive(ManagedVecItem, NestedEncode, NestedDecode, Clone)] pub struct ComputedBracket { pub end_index: u64, pub nft_reward_percent: BigUint, @@ -74,7 +74,7 @@ pub trait RewardsDistribution: .unwrap_or_else(|| self.new_raffle()); let mut rng = RandomnessSource::default(); - let mut bracket = raffle.computed_brackets.get(0); + let mut bracket = raffle.computed_brackets.get(0).clone(); let run_result = self.run_while_it_has_gas(DEFAULT_MIN_GAS_TO_SAVE_PROGRESS, || { let ticket = self.shuffle_and_pick_single_ticket( @@ -138,7 +138,7 @@ pub trait RewardsDistribution: ) { while ticket > bracket.end_index { computed_brackets.remove(0); - *bracket = computed_brackets.get(0); + *bracket = computed_brackets.get(0).clone(); } } diff --git a/contracts/examples/seed-nft-minter/src/distribution_module.rs b/contracts/examples/seed-nft-minter/src/distribution_module.rs index 54f8bf2586..cebd751806 100644 --- a/contracts/examples/seed-nft-minter/src/distribution_module.rs +++ b/contracts/examples/seed-nft-minter/src/distribution_module.rs @@ -35,7 +35,7 @@ pub trait DistributionModule { } self.tx() .to(&distribution.address) - .raw_call(distribution.endpoint) + .raw_call(distribution.endpoint.clone()) .egld_or_single_esdt(token_id, token_nonce, &payment_amount) .gas(distribution.gas_limit) .transfer_execute(); diff --git a/contracts/modules/src/governance/governance_proposal.rs b/contracts/modules/src/governance/governance_proposal.rs index 60d0fa27f0..db3f28038d 100644 --- a/contracts/modules/src/governance/governance_proposal.rs +++ b/contracts/modules/src/governance/governance_proposal.rs @@ -36,7 +36,7 @@ pub struct ProposalFees { } #[type_abi] -#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, ManagedVecItem)] +#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, ManagedVecItem, Clone)] pub struct FeeEntry { pub depositor_addr: ManagedAddress, pub tokens: EsdtTokenPayment, diff --git a/contracts/modules/src/governance/mod.rs b/contracts/modules/src/governance/mod.rs index 92eeb038db..85dfa4e1b4 100644 --- a/contracts/modules/src/governance/mod.rs +++ b/contracts/modules/src/governance/mod.rs @@ -74,7 +74,7 @@ pub trait GovernanceModule: let mut i = 0; while i < proposal.fees.entries.len() { if proposal.fees.entries.get(i).depositor_addr == caller { - fees_to_send.push(proposal.fees.entries.get(i)); + fees_to_send.push(proposal.fees.entries.get(i).clone()); proposal.fees.entries.remove(i); } else { i += 1; @@ -417,7 +417,7 @@ pub trait GovernanceModule: let payments = self.proposals().get(proposal_id).fees; for fee_entry in payments.entries.iter() { - let payment = fee_entry.tokens; + let payment = &fee_entry.tokens; self.tx() .to(&fee_entry.depositor_addr) .single_esdt( diff --git a/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs b/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs index d881b6a995..1d87fb2274 100644 --- a/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs +++ b/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs @@ -3,7 +3,7 @@ use crate::{ codec::{ multi_types::MultiValue3, DecodeErrorHandler, EncodeErrorHandler, TopDecodeMulti, TopDecodeMultiInput, TopDecodeMultiLength, TopEncodeMulti, TopEncodeMultiOutput, - }, + }, types::ManagedVecRef, }; use crate::{ @@ -42,15 +42,14 @@ impl EsdtTokenPaymentMultiValue { impl ManagedVecItem for EsdtTokenPaymentMultiValue { type PAYLOAD = as ManagedVecItem>::PAYLOAD; const SKIPS_RESERIALIZATION: bool = EsdtTokenPayment::::SKIPS_RESERIALIZATION; - type Ref<'a> = Self; + type Ref<'a> = ManagedVecRef<'a, Self>; fn read_from_payload(payload: &Self::PAYLOAD) -> Self { EsdtTokenPayment::read_from_payload(payload).into() } unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> { - // TODO: managed ref - Self::read_from_payload(payload) + ManagedVecRef::new(Self::read_from_payload(payload)) } fn save_to_payload(self, payload: &mut Self::PAYLOAD) { diff --git a/framework/base/src/types/managed/wrapped.rs b/framework/base/src/types/managed/wrapped.rs index 8e3efa278f..088a250d5d 100644 --- a/framework/base/src/types/managed/wrapped.rs +++ b/framework/base/src/types/managed/wrapped.rs @@ -20,6 +20,7 @@ mod managed_vec_item; mod managed_vec_item_nested_tuple; mod managed_vec_item_payload; mod managed_vec_owned_iter; +mod managed_vec_ref; mod managed_vec_ref_iter; mod managed_vec_ref_mut; pub(crate) mod preloaded_managed_buffer; @@ -53,6 +54,7 @@ pub use managed_vec_item::{ pub use managed_vec_item_nested_tuple::ManagedVecItemNestedTuple; pub use managed_vec_item_payload::*; pub use managed_vec_owned_iter::ManagedVecOwnedIterator; +pub use managed_vec_ref::ManagedVecRef; pub use managed_vec_ref_iter::ManagedVecRefIterator; pub use managed_vec_ref_mut::ManagedVecRefMut; pub use randomness_source::RandomnessSource; diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item.rs b/framework/base/src/types/managed/wrapped/managed_vec_item.rs index 65ed262a41..d14ae0067f 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item.rs @@ -13,7 +13,7 @@ use crate::{ use super::{ ManagedVecItemNestedTuple, ManagedVecItemPayload, ManagedVecItemPayloadAdd, - ManagedVecItemPayloadBuffer, + ManagedVecItemPayloadBuffer, ManagedVecRef, }; /// Types that implement this trait can be items inside a `ManagedVec`. @@ -168,7 +168,7 @@ where { type PAYLOAD = as ManagedVecItemPayloadAdd>::Output; const SKIPS_RESERIALIZATION: bool = false; - type Ref<'a> = Self; + type Ref<'a> = ManagedVecRef<'a, Self>; fn read_from_payload(payload: &Self::PAYLOAD) -> Self { let (p1, p2) = as ManagedVecItemPayloadAdd< @@ -184,7 +184,7 @@ where } unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> { - Self::read_from_payload(payload) + ManagedVecRef::new(Self::read_from_payload(payload)) } fn save_to_payload(self, payload: &mut Self::PAYLOAD) { diff --git a/framework/base/src/types/managed/wrapped/managed_vec_ref.rs b/framework/base/src/types/managed/wrapped/managed_vec_ref.rs new file mode 100644 index 0000000000..987cb60614 --- /dev/null +++ b/framework/base/src/types/managed/wrapped/managed_vec_ref.rs @@ -0,0 +1,54 @@ +use crate::types::ManagedVecItem; +use core::{borrow::Borrow, marker::PhantomData, mem::ManuallyDrop, ops::Deref}; + +pub struct ManagedVecRef<'a, T> +where + T: ManagedVecItem, +{ + _phantom: PhantomData<&'a T>, // needed for the lifetime, even though T is present + item: ManuallyDrop, +} + +impl ManagedVecRef<'_, T> +where + T: ManagedVecItem, +{ + pub unsafe fn new(item: T) -> Self { + ManagedVecRef { + _phantom: PhantomData, + item: ManuallyDrop::new(item), + } + } +} + +impl Drop for ManagedVecRef<'_, T> +where + T: ManagedVecItem, +{ + fn drop(&mut self) { + // TODO: improve + unsafe { + ManuallyDrop::drop(&mut self.item); + } + } +} + +impl Deref for ManagedVecRef<'_, T> +where + T: ManagedVecItem, +{ + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.item + } +} + +impl Borrow for ManagedVecRef<'_, T> +where + T: ManagedVecItem, +{ + fn borrow(&self) -> &T { + self.deref() + } +} diff --git a/framework/derive/src/managed_vec_item_derive.rs b/framework/derive/src/managed_vec_item_derive.rs index 1531b00610..f9d7d52671 100644 --- a/framework/derive/src/managed_vec_item_derive.rs +++ b/framework/derive/src/managed_vec_item_derive.rs @@ -143,7 +143,7 @@ fn struct_derive(data_struct: &syn::DataStruct, ast: &syn::DeriveInput) -> Token impl #impl_generics multiversx_sc::types::ManagedVecItem for #name #ty_generics #where_clause { type PAYLOAD = <#payload_nested_tuple as multiversx_sc::types::ManagedVecItemNestedTuple>::PAYLOAD; const SKIPS_RESERIALIZATION: bool = #(#skips_reserialization_snippets)&&*; - type Ref<'a> = Self; + type Ref<'a> = multiversx_sc::types::ManagedVecRef<'a, Self>; fn read_from_payload(payload: &Self::PAYLOAD) -> Self { let mut index = 0; @@ -156,8 +156,7 @@ fn struct_derive(data_struct: &syn::DataStruct, ast: &syn::DeriveInput) -> Token } unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> { - // TODO: managed ref - Self::read_from_payload(payload) + multiversx_sc::types::ManagedVecRef::new(Self::read_from_payload(payload)) } fn save_to_payload(self, payload: &mut Self::PAYLOAD) { From baf458e027f0721d18f43dc512570602eb3f2850 Mon Sep 17 00:00:00 2001 From: Alin Cruceat Date: Fri, 29 Nov 2024 13:24:58 +0200 Subject: [PATCH 43/66] remove #![allow(clippy::needless_lifetimes)] --- framework/base/src/lib.rs | 2 -- framework/scenario/src/lib.rs | 1 - 2 files changed, 3 deletions(-) diff --git a/framework/base/src/lib.rs b/framework/base/src/lib.rs index f6c765e67b..b063a8d94a 100644 --- a/framework/base/src/lib.rs +++ b/framework/base/src/lib.rs @@ -1,7 +1,5 @@ #![no_std] #![allow(deprecated)] -#![allow(clippy::needless_lifetimes)] // TODO: fix them all! - pub use multiversx_sc_derive::{self as derive, contract, module, proxy}; // re-export basic heap types diff --git a/framework/scenario/src/lib.rs b/framework/scenario/src/lib.rs index 46238e2ab9..cfac7eaa50 100644 --- a/framework/scenario/src/lib.rs +++ b/framework/scenario/src/lib.rs @@ -1,5 +1,4 @@ #![allow(clippy::type_complexity)] -#![allow(clippy::needless_lifetimes)] // TODO: fix them all! pub mod api; pub mod bech32; From b60559726fe853f47bf2ce1dc52e3637c90bb57c Mon Sep 17 00:00:00 2001 From: Alin Cruceat Date: Fri, 29 Nov 2024 13:28:59 +0200 Subject: [PATCH 44/66] easy `cargo clippy --tests --fix` --- framework/base/src/hex_call_data/cd_de.rs | 2 +- .../base/src/storage/mappers/bi_di_mapper.rs | 2 +- .../src/storage/mappers/linked_list_mapper.rs | 2 +- .../base/src/storage/mappers/map_mapper.rs | 10 ++++---- .../src/storage/mappers/map_storage_mapper.rs | 10 ++++---- .../base/src/storage/mappers/queue_mapper.rs | 2 +- .../src/storage/mappers/unique_id_mapper.rs | 2 +- .../base/src/storage/mappers/vec_mapper.rs | 2 +- framework/base/src/storage/storage_get.rs | 2 +- .../src/storage/storage_get_from_address.rs | 2 +- framework/base/src/storage/storage_set.rs | 2 +- .../annotated/annotated_impl_big_uint.rs | 2 +- .../types/interaction/expr/test_address.rs | 20 ++++++++-------- .../types/interaction/expr/test_sc_address.rs | 24 +++++++++---------- .../interaction/expr/test_token_identifier.rs | 6 ++--- .../tx_payment/test_esdt_transfer.rs | 2 +- .../tx_payment_egld_or_esdt_refs.rs | 2 +- .../tx_payment_egld_or_multi_esdt_ref.rs | 2 +- .../tx_payment/tx_payment_egld_value.rs | 2 +- .../tx_payment/tx_payment_multi_esdt.rs | 4 ++-- .../tx_payment/tx_payment_single_esdt_ref.rs | 2 +- .../wrapped/egld_or_esdt_token_identifier.rs | 4 ++-- .../wrapped/egld_or_multi_esdt_payment.rs | 2 +- .../src/types/managed/wrapped/managed_ref.rs | 14 +++++------ .../types/managed/wrapped/managed_ref_mut.rs | 10 ++++---- .../managed/wrapped/managed_vec_owned_iter.rs | 8 +++---- .../managed/wrapped/managed_vec_ref_iter.rs | 6 ++--- .../src/types/static_buffer/sparse_array.rs | 8 +++---- .../scenario/src/facade/expr/file_path.rs | 2 +- .../scenario/src/facade/expr/mxsc_path.rs | 8 +++---- .../scenario/src/facade/expr/num_expr.rs | 8 +++---- .../facade/result_handlers/expect_error.rs | 4 ++-- .../facade/result_handlers/expect_message.rs | 4 ++-- .../facade/world_tx/scenario_check_state.rs | 2 +- .../src/facade/world_tx/scenario_exec_call.rs | 8 +++---- .../facade/world_tx/scenario_query_call.rs | 4 ++-- .../src/facade/world_tx/scenario_set_state.rs | 4 ++-- .../scenario_set_account.rs | 2 +- .../scenario_set_state/scenario_set_block.rs | 2 +- 39 files changed, 102 insertions(+), 102 deletions(-) diff --git a/framework/base/src/hex_call_data/cd_de.rs b/framework/base/src/hex_call_data/cd_de.rs index ce6d6bdb63..f1826425e6 100644 --- a/framework/base/src/hex_call_data/cd_de.rs +++ b/framework/base/src/hex_call_data/cd_de.rs @@ -103,7 +103,7 @@ impl<'a> HexCallDataDeserializer<'a> { } } -impl<'a> TopDecodeMultiInput for HexCallDataDeserializer<'a> { +impl TopDecodeMultiInput for HexCallDataDeserializer<'_> { type ValueInput = Box<[u8]>; fn has_next(&self) -> bool { diff --git a/framework/base/src/storage/mappers/bi_di_mapper.rs b/framework/base/src/storage/mappers/bi_di_mapper.rs index ce0f24c159..decb892d2f 100644 --- a/framework/base/src/storage/mappers/bi_di_mapper.rs +++ b/framework/base/src/storage/mappers/bi_di_mapper.rs @@ -264,7 +264,7 @@ where } } -impl<'a, SA, K, V, A> Iterator for Iter<'a, SA, K, V, A> +impl Iterator for Iter<'_, SA, K, V, A> where SA: StorageMapperApi, A: StorageAddress, diff --git a/framework/base/src/storage/mappers/linked_list_mapper.rs b/framework/base/src/storage/mappers/linked_list_mapper.rs index a259467c29..0eba2da505 100644 --- a/framework/base/src/storage/mappers/linked_list_mapper.rs +++ b/framework/base/src/storage/mappers/linked_list_mapper.rs @@ -578,7 +578,7 @@ where } } -impl<'a, SA, T, A> Iterator for Iter<'a, SA, T, A> +impl Iterator for Iter<'_, SA, T, A> where SA: StorageMapperApi, A: StorageAddress, diff --git a/framework/base/src/storage/mappers/map_mapper.rs b/framework/base/src/storage/mappers/map_mapper.rs index 9e90917985..16cdf729fc 100644 --- a/framework/base/src/storage/mappers/map_mapper.rs +++ b/framework/base/src/storage/mappers/map_mapper.rs @@ -234,7 +234,7 @@ where } } -impl<'a, SA, A, K, V> Iterator for Iter<'a, SA, A, K, V> +impl Iterator for Iter<'_, SA, A, K, V> where SA: StorageMapperApi, A: StorageAddress, @@ -279,7 +279,7 @@ where } } -impl<'a, SA, A, K, V> Iterator for Values<'a, SA, A, K, V> +impl Iterator for Values<'_, SA, A, K, V> where SA: StorageMapperApi, A: StorageAddress, @@ -344,7 +344,7 @@ where pub(super) _marker: PhantomData<&'a mut (K, V)>, } -impl<'a, SA, A, K, V> Entry<'a, SA, A, K, V> +impl Entry<'_, SA, A, K, V> where SA: StorageMapperApi, A: StorageAddress, @@ -470,7 +470,7 @@ where } } -impl<'a, SA, A, K, V> OccupiedEntry<'a, SA, A, K, V> +impl OccupiedEntry<'_, SA, A, K, V> where SA: StorageMapperApi, A: StorageAddress, @@ -488,7 +488,7 @@ where } } -impl<'a, SA, K, V> OccupiedEntry<'a, SA, CurrentStorage, K, V> +impl OccupiedEntry<'_, SA, CurrentStorage, K, V> where SA: StorageMapperApi, K: TopEncode + TopDecode + NestedEncode + NestedDecode + Clone, diff --git a/framework/base/src/storage/mappers/map_storage_mapper.rs b/framework/base/src/storage/mappers/map_storage_mapper.rs index 699a3bab36..ed368a1822 100644 --- a/framework/base/src/storage/mappers/map_storage_mapper.rs +++ b/framework/base/src/storage/mappers/map_storage_mapper.rs @@ -221,7 +221,7 @@ where } } -impl<'a, SA, A, K, V> Iterator for Iter<'a, SA, A, K, V> +impl Iterator for Iter<'_, SA, A, K, V> where SA: StorageMapperApi, A: StorageAddress, @@ -268,7 +268,7 @@ where } } -impl<'a, SA, A, K, V> Iterator for Values<'a, SA, A, K, V> +impl Iterator for Values<'_, SA, A, K, V> where SA: StorageMapperApi, A: StorageAddress, @@ -388,7 +388,7 @@ where } } -impl<'a, SA, A, K, V> VacantEntry<'a, SA, A, K, V> +impl VacantEntry<'_, SA, A, K, V> where SA: StorageMapperApi, A: StorageAddress, @@ -420,7 +420,7 @@ where } } -impl<'a, SA, A, K, V> OccupiedEntry<'a, SA, A, K, V> +impl OccupiedEntry<'_, SA, A, K, V> where SA: StorageMapperApi, A: StorageAddress, @@ -438,7 +438,7 @@ where } } -impl<'a, SA, K, V> OccupiedEntry<'a, SA, CurrentStorage, K, V> +impl OccupiedEntry<'_, SA, CurrentStorage, K, V> where SA: StorageMapperApi, K: TopEncode + TopDecode + NestedEncode + NestedDecode + Clone + 'static, diff --git a/framework/base/src/storage/mappers/queue_mapper.rs b/framework/base/src/storage/mappers/queue_mapper.rs index 9937736b01..80572d1b41 100644 --- a/framework/base/src/storage/mappers/queue_mapper.rs +++ b/framework/base/src/storage/mappers/queue_mapper.rs @@ -484,7 +484,7 @@ where } } -impl<'a, SA, A, T> Iterator for Iter<'a, SA, A, T> +impl Iterator for Iter<'_, SA, A, T> where SA: StorageMapperApi, A: StorageAddress, diff --git a/framework/base/src/storage/mappers/unique_id_mapper.rs b/framework/base/src/storage/mappers/unique_id_mapper.rs index a1789d695a..a8605963a1 100644 --- a/framework/base/src/storage/mappers/unique_id_mapper.rs +++ b/framework/base/src/storage/mappers/unique_id_mapper.rs @@ -180,7 +180,7 @@ where } } -impl<'a, SA, A> Iterator for Iter<'a, SA, A> +impl Iterator for Iter<'_, SA, A> where SA: StorageMapperApi, A: StorageAddress, diff --git a/framework/base/src/storage/mappers/vec_mapper.rs b/framework/base/src/storage/mappers/vec_mapper.rs index 22b8763ece..0b7eb4cb5d 100644 --- a/framework/base/src/storage/mappers/vec_mapper.rs +++ b/framework/base/src/storage/mappers/vec_mapper.rs @@ -320,7 +320,7 @@ where } } -impl<'a, SA, T, A> Iterator for Iter<'a, SA, T, A> +impl Iterator for Iter<'_, SA, T, A> where SA: StorageMapperApi, A: StorageAddress, diff --git a/framework/base/src/storage/storage_get.rs b/framework/base/src/storage/storage_get.rs index b33b5e1dcb..3cd6324aae 100644 --- a/framework/base/src/storage/storage_get.rs +++ b/framework/base/src/storage/storage_get.rs @@ -57,7 +57,7 @@ where } } -impl<'k, A> TopDecodeInput for StorageGetInput<'k, A> +impl TopDecodeInput for StorageGetInput<'_, A> where A: StorageReadApi + ManagedTypeApi + ErrorApi + 'static, { diff --git a/framework/base/src/storage/storage_get_from_address.rs b/framework/base/src/storage/storage_get_from_address.rs index 13a467d2f0..14b1bfabf0 100644 --- a/framework/base/src/storage/storage_get_from_address.rs +++ b/framework/base/src/storage/storage_get_from_address.rs @@ -66,7 +66,7 @@ where } } -impl<'k, A> TopDecodeInput for StorageGetFromAddressInput<'k, A> +impl TopDecodeInput for StorageGetFromAddressInput<'_, A> where A: StorageReadApi + ManagedTypeApi + ErrorApi + 'static, { diff --git a/framework/base/src/storage/storage_set.rs b/framework/base/src/storage/storage_set.rs index a2f3c3b722..8907b2abbf 100644 --- a/framework/base/src/storage/storage_set.rs +++ b/framework/base/src/storage/storage_set.rs @@ -37,7 +37,7 @@ where } } -impl<'k, A> TopEncodeOutput for StorageSetOutput<'k, A> +impl TopEncodeOutput for StorageSetOutput<'_, A> where A: StorageWriteApi + ManagedTypeApi + ErrorApi + 'static, { diff --git a/framework/base/src/types/interaction/annotated/annotated_impl_big_uint.rs b/framework/base/src/types/interaction/annotated/annotated_impl_big_uint.rs index f05952ab75..590e73545f 100644 --- a/framework/base/src/types/interaction/annotated/annotated_impl_big_uint.rs +++ b/framework/base/src/types/interaction/annotated/annotated_impl_big_uint.rs @@ -53,7 +53,7 @@ where } } -impl<'a, Env> AnnotatedValue> for ManagedRef<'a, Env::Api, BigUint> +impl AnnotatedValue> for ManagedRef<'_, Env::Api, BigUint> where Env: TxEnv, { diff --git a/framework/base/src/types/interaction/expr/test_address.rs b/framework/base/src/types/interaction/expr/test_address.rs index 01d1dc819d..ed348c4e9c 100644 --- a/framework/base/src/types/interaction/expr/test_address.rs +++ b/framework/base/src/types/interaction/expr/test_address.rs @@ -56,13 +56,13 @@ impl<'a> TestAddress<'a> { } } -impl<'a, 'b> PartialEq> for TestAddress<'a> { +impl PartialEq> for TestAddress<'_> { fn eq(&self, other: &TestSCAddress) -> bool { self.to_address() == other.to_address() } } -impl<'a> PartialEq

for TestAddress<'a> { +impl PartialEq
for TestAddress<'_> { fn eq(&self, other: &Address) -> bool { &self.to_address() == other } @@ -74,7 +74,7 @@ impl<'a> PartialEq> for Address { } } -impl<'a, Api: ManagedTypeApi> PartialEq> for TestAddress<'a> { +impl PartialEq> for TestAddress<'_> { fn eq(&self, other: &ManagedAddress) -> bool { self.to_address() == other.to_address() } @@ -86,7 +86,7 @@ impl<'a, Api: ManagedTypeApi> PartialEq> for ManagedAddress } } -impl<'a, Env> AnnotatedValue> for TestAddress<'a> +impl AnnotatedValue> for TestAddress<'_> where Env: TxEnv, { @@ -102,7 +102,7 @@ where } } -impl<'a, Env> TxFrom for TestAddress<'a> +impl TxFrom for TestAddress<'_> where Env: TxEnv, { @@ -111,11 +111,11 @@ where expr.into() } } -impl<'a, Env> TxFromSpecified for TestAddress<'a> where Env: TxEnv {} -impl<'a, Env> TxTo for TestAddress<'a> where Env: TxEnv {} -impl<'a, Env> TxToSpecified for TestAddress<'a> where Env: TxEnv {} +impl TxFromSpecified for TestAddress<'_> where Env: TxEnv {} +impl TxTo for TestAddress<'_> where Env: TxEnv {} +impl TxToSpecified for TestAddress<'_> where Env: TxEnv {} -impl<'a> TopEncode for TestAddress<'a> { +impl TopEncode for TestAddress<'_> { fn top_encode_or_handle_err(&self, output: O, h: H) -> Result<(), H::HandledErr> where O: TopEncodeOutput, @@ -125,7 +125,7 @@ impl<'a> TopEncode for TestAddress<'a> { } } -impl<'a, Api> TypeAbiFrom> for ManagedAddress where Api: ManagedTypeApi {} +impl TypeAbiFrom> for ManagedAddress where Api: ManagedTypeApi {} #[cfg(test)] pub mod tests { diff --git a/framework/base/src/types/interaction/expr/test_sc_address.rs b/framework/base/src/types/interaction/expr/test_sc_address.rs index 564157fb94..520d3a6fc9 100644 --- a/framework/base/src/types/interaction/expr/test_sc_address.rs +++ b/framework/base/src/types/interaction/expr/test_sc_address.rs @@ -32,7 +32,7 @@ impl<'a> TestSCAddress<'a> { } } -impl<'a, Env> AnnotatedValue> for TestSCAddress<'a> +impl AnnotatedValue> for TestSCAddress<'_> where Env: TxEnv, { @@ -48,7 +48,7 @@ where } } -impl<'a> TestSCAddress<'a> { +impl TestSCAddress<'_> { pub fn to_address(&self) -> Address { self.eval_to_array().into() } @@ -58,13 +58,13 @@ impl<'a> TestSCAddress<'a> { } } -impl<'a, 'b> PartialEq> for TestSCAddress<'a> { +impl PartialEq> for TestSCAddress<'_> { fn eq(&self, other: &TestAddress) -> bool { self.to_address() == other.to_address() } } -impl<'a> PartialEq
for TestSCAddress<'a> { +impl PartialEq
for TestSCAddress<'_> { fn eq(&self, other: &Address) -> bool { &self.to_address() == other } @@ -76,7 +76,7 @@ impl<'a> PartialEq> for Address { } } -impl<'a, Api: ManagedTypeApi> PartialEq> for TestSCAddress<'a> { +impl PartialEq> for TestSCAddress<'_> { fn eq(&self, other: &ManagedAddress) -> bool { self.to_address() == other.to_address() } @@ -88,7 +88,7 @@ impl<'a, Api: ManagedTypeApi> PartialEq> for ManagedAddress TxFrom for TestSCAddress<'a> +impl TxFrom for TestSCAddress<'_> where Env: TxEnv, { @@ -97,11 +97,11 @@ where expr.into() } } -impl<'a, Env> TxFromSpecified for TestSCAddress<'a> where Env: TxEnv {} -impl<'a, Env> TxTo for TestSCAddress<'a> where Env: TxEnv {} -impl<'a, Env> TxToSpecified for TestSCAddress<'a> where Env: TxEnv {} +impl TxFromSpecified for TestSCAddress<'_> where Env: TxEnv {} +impl TxTo for TestSCAddress<'_> where Env: TxEnv {} +impl TxToSpecified for TestSCAddress<'_> where Env: TxEnv {} -impl<'a> TestSCAddress<'a> { +impl TestSCAddress<'_> { pub fn eval_to_array(&self) -> [u8; 32] { let result = *b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00______________________"; let expr_bytes = self.name.as_bytes(); @@ -130,7 +130,7 @@ impl<'a> TestSCAddress<'a> { } } -impl<'a> TopEncode for TestSCAddress<'a> { +impl TopEncode for TestSCAddress<'_> { fn top_encode_or_handle_err(&self, output: O, h: H) -> Result<(), H::HandledErr> where O: TopEncodeOutput, @@ -140,7 +140,7 @@ impl<'a> TopEncode for TestSCAddress<'a> { } } -impl<'a, Api> TypeAbiFrom> for ManagedAddress where Api: ManagedTypeApi {} +impl TypeAbiFrom> for ManagedAddress where Api: ManagedTypeApi {} #[cfg(test)] pub mod tests { diff --git a/framework/base/src/types/interaction/expr/test_token_identifier.rs b/framework/base/src/types/interaction/expr/test_token_identifier.rs index 68865d8fbc..44a87988b2 100644 --- a/framework/base/src/types/interaction/expr/test_token_identifier.rs +++ b/framework/base/src/types/interaction/expr/test_token_identifier.rs @@ -40,7 +40,7 @@ impl<'a> TestTokenIdentifier<'a> { } } -impl<'a, Env> AnnotatedValue> for TestTokenIdentifier<'a> +impl AnnotatedValue> for TestTokenIdentifier<'_> where Env: TxEnv, { @@ -64,7 +64,7 @@ where } } -impl<'a> TopEncode for TestTokenIdentifier<'a> { +impl TopEncode for TestTokenIdentifier<'_> { fn top_encode_or_handle_err(&self, output: O, h: H) -> Result<(), H::HandledErr> where O: TopEncodeOutput, @@ -74,5 +74,5 @@ impl<'a> TopEncode for TestTokenIdentifier<'a> { } } -impl<'a, Api> TypeAbiFrom> for TokenIdentifier where Api: ManagedTypeApi +impl TypeAbiFrom> for TokenIdentifier where Api: ManagedTypeApi {} diff --git a/framework/base/src/types/interaction/tx_payment/test_esdt_transfer.rs b/framework/base/src/types/interaction/tx_payment/test_esdt_transfer.rs index 99d00a219e..307bc06133 100644 --- a/framework/base/src/types/interaction/tx_payment/test_esdt_transfer.rs +++ b/framework/base/src/types/interaction/tx_payment/test_esdt_transfer.rs @@ -26,7 +26,7 @@ where } } -impl<'a, Env> TxPayment for TestEsdtTransfer<'a> +impl TxPayment for TestEsdtTransfer<'_> where Env: TxEnv, { diff --git a/framework/base/src/types/interaction/tx_payment/tx_payment_egld_or_esdt_refs.rs b/framework/base/src/types/interaction/tx_payment/tx_payment_egld_or_esdt_refs.rs index 14f9058b55..1326974817 100644 --- a/framework/base/src/types/interaction/tx_payment/tx_payment_egld_or_esdt_refs.rs +++ b/framework/base/src/types/interaction/tx_payment/tx_payment_egld_or_esdt_refs.rs @@ -2,7 +2,7 @@ use crate::types::{BigUint, EgldOrEsdtTokenPaymentRefs, ManagedAddress, TxFrom, use super::{Egld, FullPaymentData, FunctionCall, TxEnv, TxPayment}; -impl<'a, Env> TxPayment for EgldOrEsdtTokenPaymentRefs<'a, Env::Api> +impl TxPayment for EgldOrEsdtTokenPaymentRefs<'_, Env::Api> where Env: TxEnv, { diff --git a/framework/base/src/types/interaction/tx_payment/tx_payment_egld_or_multi_esdt_ref.rs b/framework/base/src/types/interaction/tx_payment/tx_payment_egld_or_multi_esdt_ref.rs index 9d4eb6c20a..8cd88504ae 100644 --- a/framework/base/src/types/interaction/tx_payment/tx_payment_egld_or_multi_esdt_ref.rs +++ b/framework/base/src/types/interaction/tx_payment/tx_payment_egld_or_multi_esdt_ref.rs @@ -2,7 +2,7 @@ use crate::types::{BigUint, EgldOrMultiEsdtPaymentRefs, ManagedAddress, TxFrom, use super::{Egld, FullPaymentData, FunctionCall, TxEnv, TxPayment}; -impl<'a, Env> TxPayment for EgldOrMultiEsdtPaymentRefs<'a, Env::Api> +impl TxPayment for EgldOrMultiEsdtPaymentRefs<'_, Env::Api> where Env: TxEnv, { diff --git a/framework/base/src/types/interaction/tx_payment/tx_payment_egld_value.rs b/framework/base/src/types/interaction/tx_payment/tx_payment_egld_value.rs index 99d91b618f..9b32d93a4e 100644 --- a/framework/base/src/types/interaction/tx_payment/tx_payment_egld_value.rs +++ b/framework/base/src/types/interaction/tx_payment/tx_payment_egld_value.rs @@ -10,5 +10,5 @@ where impl TxEgldValue for BigUint where Env: TxEnv {} impl TxEgldValue for &BigUint where Env: TxEnv {} -impl<'a, Env> TxEgldValue for ManagedRef<'a, Env::Api, BigUint> where Env: TxEnv {} +impl TxEgldValue for ManagedRef<'_, Env::Api, BigUint> where Env: TxEnv {} impl TxEgldValue for u64 where Env: TxEnv {} diff --git a/framework/base/src/types/interaction/tx_payment/tx_payment_multi_esdt.rs b/framework/base/src/types/interaction/tx_payment/tx_payment_multi_esdt.rs index c0afc2055c..ab030362d2 100644 --- a/framework/base/src/types/interaction/tx_payment/tx_payment_multi_esdt.rs +++ b/framework/base/src/types/interaction/tx_payment/tx_payment_multi_esdt.rs @@ -16,7 +16,7 @@ where impl TxPaymentMultiEsdt for MultiEsdtPayment where Env: TxEnv {} impl TxPaymentMultiEsdt for &MultiEsdtPayment where Env: TxEnv {} -impl<'a, Env> TxPaymentMultiEsdt for ManagedRef<'a, Env::Api, MultiEsdtPayment> where +impl TxPaymentMultiEsdt for ManagedRef<'_, Env::Api, MultiEsdtPayment> where Env: TxEnv { } @@ -76,7 +76,7 @@ where } } -impl<'a, Env> TxPayment for ManagedRef<'a, Env::Api, MultiEsdtPayment> +impl TxPayment for ManagedRef<'_, Env::Api, MultiEsdtPayment> where Env: TxEnv, { diff --git a/framework/base/src/types/interaction/tx_payment/tx_payment_single_esdt_ref.rs b/framework/base/src/types/interaction/tx_payment/tx_payment_single_esdt_ref.rs index c91391d316..6aecb277a6 100644 --- a/framework/base/src/types/interaction/tx_payment/tx_payment_single_esdt_ref.rs +++ b/framework/base/src/types/interaction/tx_payment/tx_payment_single_esdt_ref.rs @@ -7,7 +7,7 @@ use crate::{ use super::{FullPaymentData, FunctionCall, TxEnv, TxPayment}; -impl<'a, Env> TxPayment for EsdtTokenPaymentRefs<'a, Env::Api> +impl TxPayment for EsdtTokenPaymentRefs<'_, Env::Api> where Env: TxEnv, { diff --git a/framework/base/src/types/managed/wrapped/egld_or_esdt_token_identifier.rs b/framework/base/src/types/managed/wrapped/egld_or_esdt_token_identifier.rs index 66dd7c9ec9..0bffac57a3 100644 --- a/framework/base/src/types/managed/wrapped/egld_or_esdt_token_identifier.rs +++ b/framework/base/src/types/managed/wrapped/egld_or_esdt_token_identifier.rs @@ -207,11 +207,11 @@ impl TypeAbiFrom<&TokenIdentifier> for EgldOrEsdtTokenIdentifier where impl TypeAbiFrom<&[u8]> for EgldOrEsdtTokenIdentifier where M: ManagedTypeApi {} impl TypeAbiFrom<&str> for EgldOrEsdtTokenIdentifier where M: ManagedTypeApi {} -impl<'a, M> TypeAbiFrom> for EgldOrEsdtTokenIdentifier where +impl TypeAbiFrom> for EgldOrEsdtTokenIdentifier where M: ManagedTypeApi { } -impl<'a, M> TypeAbiFrom<&TestTokenIdentifier<'a>> for EgldOrEsdtTokenIdentifier where +impl TypeAbiFrom<&TestTokenIdentifier<'_>> for EgldOrEsdtTokenIdentifier where M: ManagedTypeApi { } diff --git a/framework/base/src/types/managed/wrapped/egld_or_multi_esdt_payment.rs b/framework/base/src/types/managed/wrapped/egld_or_multi_esdt_payment.rs index 88534f95c1..1c06f0677c 100644 --- a/framework/base/src/types/managed/wrapped/egld_or_multi_esdt_payment.rs +++ b/framework/base/src/types/managed/wrapped/egld_or_multi_esdt_payment.rs @@ -49,7 +49,7 @@ impl EgldOrMultiEsdtPayment { } } -impl<'a, M: ManagedTypeApi> EgldOrMultiEsdtPaymentRefs<'a, M> { +impl EgldOrMultiEsdtPaymentRefs<'_, M> { pub fn to_owned_payment(&self) -> EgldOrMultiEsdtPayment { match self { EgldOrMultiEsdtPaymentRefs::Egld(egld_value) => { diff --git a/framework/base/src/types/managed/wrapped/managed_ref.rs b/framework/base/src/types/managed/wrapped/managed_ref.rs index f618063b3c..efdf4a9596 100644 --- a/framework/base/src/types/managed/wrapped/managed_ref.rs +++ b/framework/base/src/types/managed/wrapped/managed_ref.rs @@ -49,7 +49,7 @@ where } } -impl<'a, M, T> ManagedRef<'a, M, T> +impl ManagedRef<'_, M, T> where M: ManagedTypeApi, T: ManagedType + Clone, @@ -60,7 +60,7 @@ where } } -impl<'a, M, T> Clone for ManagedRef<'a, M, T> +impl Clone for ManagedRef<'_, M, T> where M: ManagedTypeApi, T: ManagedType, @@ -75,7 +75,7 @@ where } } -impl<'a, M, T> Deref for ManagedRef<'a, M, T> +impl Deref for ManagedRef<'_, M, T> where M: ManagedTypeApi, T: ManagedType, @@ -88,7 +88,7 @@ where } } -impl<'a, M, T> Borrow for ManagedRef<'a, M, T> +impl Borrow for ManagedRef<'_, M, T> where M: ManagedTypeApi, T: ManagedType, @@ -110,7 +110,7 @@ where } } -impl<'a, M, T> PartialEq for ManagedRef<'a, M, T> +impl PartialEq for ManagedRef<'_, M, T> where M: ManagedTypeApi, T: ManagedType + PartialEq, @@ -121,14 +121,14 @@ where } } -impl<'a, M, T> Eq for ManagedRef<'a, M, T> +impl Eq for ManagedRef<'_, M, T> where M: ManagedTypeApi, T: ManagedType + PartialEq, { } -impl<'a, M, T> TopEncode for ManagedRef<'a, M, T> +impl TopEncode for ManagedRef<'_, M, T> where M: ManagedTypeApi, T: ManagedType + TopEncode, diff --git a/framework/base/src/types/managed/wrapped/managed_ref_mut.rs b/framework/base/src/types/managed/wrapped/managed_ref_mut.rs index 868ef89e78..e017021d24 100644 --- a/framework/base/src/types/managed/wrapped/managed_ref_mut.rs +++ b/framework/base/src/types/managed/wrapped/managed_ref_mut.rs @@ -115,7 +115,7 @@ where } } -impl<'a, M, T> PartialEq for ManagedRefMut<'a, M, T> +impl PartialEq for ManagedRefMut<'_, M, T> where M: ManagedTypeApi, T: ManagedType + PartialEq, @@ -126,14 +126,14 @@ where } } -impl<'a, M, T> Eq for ManagedRefMut<'a, M, T> +impl Eq for ManagedRefMut<'_, M, T> where M: ManagedTypeApi, T: ManagedType + PartialEq, { } -impl<'a, M, T> TopEncode for ManagedRefMut<'a, M, T> +impl TopEncode for ManagedRefMut<'_, M, T> where M: ManagedTypeApi, T: ManagedType + TopEncode, @@ -148,7 +148,7 @@ where } } -impl<'a, M, T> NestedEncode for ManagedRefMut<'a, M, T> +impl NestedEncode for ManagedRefMut<'_, M, T> where M: ManagedTypeApi, T: ManagedType + NestedEncode, @@ -163,7 +163,7 @@ where } } -impl<'a, M, T> core::fmt::Debug for ManagedRefMut<'a, M, T> +impl core::fmt::Debug for ManagedRefMut<'_, M, T> where M: ManagedTypeApi, T: ManagedType + core::fmt::Debug, diff --git a/framework/base/src/types/managed/wrapped/managed_vec_owned_iter.rs b/framework/base/src/types/managed/wrapped/managed_vec_owned_iter.rs index 8e0da8727c..b5e342d345 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_owned_iter.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_owned_iter.rs @@ -39,7 +39,7 @@ where } } -impl<'a, M, T> Iterator for ManagedVecOwnedIterator<'a, M, T> +impl Iterator for ManagedVecOwnedIterator<'_, M, T> where M: ManagedTypeApi, T: ManagedVecItem, @@ -70,14 +70,14 @@ where } } -impl<'a, M, T> ExactSizeIterator for ManagedVecOwnedIterator<'a, M, T> +impl ExactSizeIterator for ManagedVecOwnedIterator<'_, M, T> where M: ManagedTypeApi, T: ManagedVecItem, { } -impl<'a, M, T> DoubleEndedIterator for ManagedVecOwnedIterator<'a, M, T> +impl DoubleEndedIterator for ManagedVecOwnedIterator<'_, M, T> where M: ManagedTypeApi, T: ManagedVecItem, @@ -98,7 +98,7 @@ where } } -impl<'a, M, T> Clone for ManagedVecOwnedIterator<'a, M, T> +impl Clone for ManagedVecOwnedIterator<'_, M, T> where M: ManagedTypeApi, T: ManagedVecItem, diff --git a/framework/base/src/types/managed/wrapped/managed_vec_ref_iter.rs b/framework/base/src/types/managed/wrapped/managed_vec_ref_iter.rs index 4703a1f61b..18cfcf84fe 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_ref_iter.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_ref_iter.rs @@ -56,14 +56,14 @@ where } } -impl<'a, M, T> ExactSizeIterator for ManagedVecRefIterator<'a, M, T> +impl ExactSizeIterator for ManagedVecRefIterator<'_, M, T> where M: ManagedTypeApi, T: ManagedVecItem, { } -impl<'a, M, T> DoubleEndedIterator for ManagedVecRefIterator<'a, M, T> +impl DoubleEndedIterator for ManagedVecRefIterator<'_, M, T> where M: ManagedTypeApi, T: ManagedVecItem, @@ -84,7 +84,7 @@ where } } -impl<'a, M, T> Clone for ManagedVecRefIterator<'a, M, T> +impl Clone for ManagedVecRefIterator<'_, M, T> where M: ManagedTypeApi, T: ManagedVecItem, diff --git a/framework/base/src/types/static_buffer/sparse_array.rs b/framework/base/src/types/static_buffer/sparse_array.rs index bb3a083243..911069a73d 100644 --- a/framework/base/src/types/static_buffer/sparse_array.rs +++ b/framework/base/src/types/static_buffer/sparse_array.rs @@ -156,7 +156,7 @@ where } } -impl<'a, E, const CAPACITY: usize> Iterator for SparseArrayIterator<'a, E, CAPACITY> +impl Iterator for SparseArrayIterator<'_, E, CAPACITY> where E: ErrorApi, { @@ -179,12 +179,12 @@ where } } -impl<'a, E, const CAPACITY: usize> ExactSizeIterator for SparseArrayIterator<'a, E, CAPACITY> where +impl ExactSizeIterator for SparseArrayIterator<'_, E, CAPACITY> where E: ErrorApi { } -impl<'a, E, const CAPACITY: usize> DoubleEndedIterator for SparseArrayIterator<'a, E, CAPACITY> +impl DoubleEndedIterator for SparseArrayIterator<'_, E, CAPACITY> where E: ErrorApi, { @@ -200,7 +200,7 @@ where } } -impl<'a, E, const CAPACITY: usize> Clone for SparseArrayIterator<'a, E, CAPACITY> +impl Clone for SparseArrayIterator<'_, E, CAPACITY> where E: ErrorApi, { diff --git a/framework/scenario/src/facade/expr/file_path.rs b/framework/scenario/src/facade/expr/file_path.rs index 0c05165fb2..593610ecd6 100644 --- a/framework/scenario/src/facade/expr/file_path.rs +++ b/framework/scenario/src/facade/expr/file_path.rs @@ -22,7 +22,7 @@ impl FilePath<'_> { } } -impl<'a, Env> AnnotatedValue> for FilePath<'a> +impl AnnotatedValue> for FilePath<'_> where Env: ScenarioTxEnv, { diff --git a/framework/scenario/src/facade/expr/mxsc_path.rs b/framework/scenario/src/facade/expr/mxsc_path.rs index 465eb5f097..fac10d6a46 100644 --- a/framework/scenario/src/facade/expr/mxsc_path.rs +++ b/framework/scenario/src/facade/expr/mxsc_path.rs @@ -20,7 +20,7 @@ impl<'a> MxscPath<'a> { } } -impl<'a> MxscPath<'a> { +impl MxscPath<'_> { pub fn eval_to_expr(&self) -> String { format!("{MXSC_PREFIX}{}", self.path) } @@ -30,7 +30,7 @@ impl<'a> MxscPath<'a> { } } -impl<'a, Env> AnnotatedValue> for MxscPath<'a> +impl AnnotatedValue> for MxscPath<'_> where Env: ScenarioTxEnv, { @@ -44,9 +44,9 @@ where } } -impl<'a, Env> TxCodeValue for MxscPath<'a> where Env: ScenarioTxEnv {} +impl TxCodeValue for MxscPath<'_> where Env: ScenarioTxEnv {} -impl<'a> RegisterCodeSource for MxscPath<'a> { +impl RegisterCodeSource for MxscPath<'_> { fn into_code(self, env_data: ScenarioTxEnvData) -> Vec { self.resolve_contents(&env_data.interpreter_context()) } diff --git a/framework/scenario/src/facade/expr/num_expr.rs b/framework/scenario/src/facade/expr/num_expr.rs index 704f52496b..55a849c1d3 100644 --- a/framework/scenario/src/facade/expr/num_expr.rs +++ b/framework/scenario/src/facade/expr/num_expr.rs @@ -19,7 +19,7 @@ where BigUint::from_bytes_be(&bytes) } -impl<'a, Env> AnnotatedValue> for NumExpr<'a> +impl AnnotatedValue> for NumExpr<'_> where Env: ScenarioTxEnv, { @@ -32,7 +32,7 @@ where } } -impl<'a, Env> AnnotatedValue for NumExpr<'a> +impl AnnotatedValue for NumExpr<'_> where Env: ScenarioTxEnv, { @@ -45,5 +45,5 @@ where } } -impl<'a, Env> TxEgldValue for NumExpr<'a> where Env: ScenarioTxEnv {} -impl<'a, Env> TxGasValue for NumExpr<'a> where Env: ScenarioTxEnv {} +impl TxEgldValue for NumExpr<'_> where Env: ScenarioTxEnv {} +impl TxGasValue for NumExpr<'_> where Env: ScenarioTxEnv {} diff --git a/framework/scenario/src/facade/result_handlers/expect_error.rs b/framework/scenario/src/facade/result_handlers/expect_error.rs index a06a227424..55e2bde473 100644 --- a/framework/scenario/src/facade/result_handlers/expect_error.rs +++ b/framework/scenario/src/facade/result_handlers/expect_error.rs @@ -8,14 +8,14 @@ use crate::scenario_model::{BytesValue, CheckValue, TxExpect, TxResponse}; /// Can only be used in tests and interactors, not available in contracts. pub struct ExpectError<'a>(pub u64, pub &'a str); -impl<'a, Env, Original> RHListItem for ExpectError<'a> +impl RHListItem for ExpectError<'_> where Env: TxEnv, { type Returns = (); } -impl<'a, Env, Original> RHListItemExec for ExpectError<'a> +impl RHListItemExec for ExpectError<'_> where Env: TxEnv, { diff --git a/framework/scenario/src/facade/result_handlers/expect_message.rs b/framework/scenario/src/facade/result_handlers/expect_message.rs index af5fa2a429..65f412fadd 100644 --- a/framework/scenario/src/facade/result_handlers/expect_message.rs +++ b/framework/scenario/src/facade/result_handlers/expect_message.rs @@ -8,14 +8,14 @@ use crate::scenario_model::{BytesValue, CheckValue, TxExpect, TxResponse, U64Val /// Can only be used in tests and interactors, not available in contracts. pub struct ExpectMessage<'a>(pub &'a str); -impl<'a, Env, Original> RHListItem for ExpectMessage<'a> +impl RHListItem for ExpectMessage<'_> where Env: TxEnv, { type Returns = (); } -impl<'a, Env, Original> RHListItemExec for ExpectMessage<'a> +impl RHListItemExec for ExpectMessage<'_> where Env: TxEnv, { diff --git a/framework/scenario/src/facade/world_tx/scenario_check_state.rs b/framework/scenario/src/facade/world_tx/scenario_check_state.rs index 3f5423419a..182edee26a 100644 --- a/framework/scenario/src/facade/world_tx/scenario_check_state.rs +++ b/framework/scenario/src/facade/world_tx/scenario_check_state.rs @@ -230,7 +230,7 @@ impl<'w> CheckStateBuilder<'w> { } } -impl<'w> Drop for CheckStateBuilder<'w> { +impl Drop for CheckStateBuilder<'_> { fn drop(&mut self) { self.commit_accounts(); } diff --git a/framework/scenario/src/facade/world_tx/scenario_exec_call.rs b/framework/scenario/src/facade/world_tx/scenario_exec_call.rs index 9c1551111b..a856d16d91 100644 --- a/framework/scenario/src/facade/world_tx/scenario_exec_call.rs +++ b/framework/scenario/src/facade/world_tx/scenario_exec_call.rs @@ -23,7 +23,7 @@ pub struct ScenarioEnvExec<'w> { pub data: ScenarioTxEnvData, } -impl<'w> TxEnv for ScenarioEnvExec<'w> { +impl TxEnv for ScenarioEnvExec<'_> { type Api = StaticApi; type RHExpect = TxExpect; @@ -41,7 +41,7 @@ impl<'w> TxEnv for ScenarioEnvExec<'w> { } } -impl<'w> TxEnvMockDeployAddress for ScenarioEnvExec<'w> { +impl TxEnvMockDeployAddress for ScenarioEnvExec<'_> { fn mock_deploy_new_address(&mut self, from: &From, new_address: NA) where From: TxFromSpecified, @@ -65,7 +65,7 @@ impl<'w> TxEnvMockDeployAddress for ScenarioEnvExec<'w> { } } -impl<'w> ScenarioTxEnv for ScenarioEnvExec<'w> { +impl ScenarioTxEnv for ScenarioEnvExec<'_> { fn env_data(&self) -> &ScenarioTxEnvData { &self.data } @@ -115,7 +115,7 @@ where } } -impl<'w> TxEnvWithTxHash for ScenarioEnvExec<'w> { +impl TxEnvWithTxHash for ScenarioEnvExec<'_> { fn set_tx_hash(&mut self, tx_hash: H256) { self.data.set_tx_hash(tx_hash); } diff --git a/framework/scenario/src/facade/world_tx/scenario_query_call.rs b/framework/scenario/src/facade/world_tx/scenario_query_call.rs index dc416439b4..afae3d80e9 100644 --- a/framework/scenario/src/facade/world_tx/scenario_query_call.rs +++ b/framework/scenario/src/facade/world_tx/scenario_query_call.rs @@ -18,7 +18,7 @@ pub struct ScenarioEnvQuery<'w> { pub data: ScenarioTxEnvData, } -impl<'w> TxEnv for ScenarioEnvQuery<'w> { +impl TxEnv for ScenarioEnvQuery<'_> { type Api = StaticApi; type RHExpect = TxExpect; @@ -36,7 +36,7 @@ impl<'w> TxEnv for ScenarioEnvQuery<'w> { } } -impl<'w> ScenarioTxEnv for ScenarioEnvQuery<'w> { +impl ScenarioTxEnv for ScenarioEnvQuery<'_> { fn env_data(&self) -> &ScenarioTxEnvData { &self.data } diff --git a/framework/scenario/src/facade/world_tx/scenario_set_state.rs b/framework/scenario/src/facade/world_tx/scenario_set_state.rs index 3fd44d70b3..ef256453f7 100644 --- a/framework/scenario/src/facade/world_tx/scenario_set_state.rs +++ b/framework/scenario/src/facade/world_tx/scenario_set_state.rs @@ -239,7 +239,7 @@ impl<'w> SetStateBuilderBase<'w> { } } -impl<'w> SetStateBuilder<'w, ()> {} +impl SetStateBuilder<'_, ()> {} impl<'w, Item> SetStateBuilder<'w, Item> where @@ -313,7 +313,7 @@ where pub fn commit(self) {} } -impl<'w, Current> Drop for SetStateBuilder<'w, Current> +impl Drop for SetStateBuilder<'_, Current> where Current: SetStateBuilderItem, { diff --git a/framework/scenario/src/facade/world_tx/scenario_set_state/scenario_set_account.rs b/framework/scenario/src/facade/world_tx/scenario_set_state/scenario_set_account.rs index f5590894e1..a9821659a5 100644 --- a/framework/scenario/src/facade/world_tx/scenario_set_state/scenario_set_account.rs +++ b/framework/scenario/src/facade/world_tx/scenario_set_state/scenario_set_account.rs @@ -38,7 +38,7 @@ impl SetStateBuilderItem for AccountItem { } } -impl<'w> SetStateBuilder<'w, AccountItem> { +impl SetStateBuilder<'_, AccountItem> { pub fn nonce(mut self, nonce: N) -> Self where N: AnnotatedValue, diff --git a/framework/scenario/src/facade/world_tx/scenario_set_state/scenario_set_block.rs b/framework/scenario/src/facade/world_tx/scenario_set_state/scenario_set_block.rs index 3ef0d06cca..e2244207db 100644 --- a/framework/scenario/src/facade/world_tx/scenario_set_state/scenario_set_block.rs +++ b/framework/scenario/src/facade/world_tx/scenario_set_state/scenario_set_block.rs @@ -49,7 +49,7 @@ impl SetStateBuilderItem for BlockItem { } } -impl<'w> SetStateBuilder<'w, BlockItem> { +impl SetStateBuilder<'_, BlockItem> { pub fn block_epoch(mut self, block_epoch: N) -> Self where N: AnnotatedValue, From 205e4347c82917632a3faef372cf1f7e1371d1f5 Mon Sep 17 00:00:00 2001 From: Alin Cruceat Date: Fri, 29 Nov 2024 13:30:52 +0200 Subject: [PATCH 45/66] cargo fmt --- .../src/types/interaction/expr/test_token_identifier.rs | 3 +-- .../types/managed/wrapped/egld_or_esdt_token_identifier.rs | 6 ++---- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/framework/base/src/types/interaction/expr/test_token_identifier.rs b/framework/base/src/types/interaction/expr/test_token_identifier.rs index 44a87988b2..36ee135239 100644 --- a/framework/base/src/types/interaction/expr/test_token_identifier.rs +++ b/framework/base/src/types/interaction/expr/test_token_identifier.rs @@ -74,5 +74,4 @@ impl TopEncode for TestTokenIdentifier<'_> { } } -impl TypeAbiFrom> for TokenIdentifier where Api: ManagedTypeApi -{} +impl TypeAbiFrom> for TokenIdentifier where Api: ManagedTypeApi {} diff --git a/framework/base/src/types/managed/wrapped/egld_or_esdt_token_identifier.rs b/framework/base/src/types/managed/wrapped/egld_or_esdt_token_identifier.rs index 0bffac57a3..7b46cb2e50 100644 --- a/framework/base/src/types/managed/wrapped/egld_or_esdt_token_identifier.rs +++ b/framework/base/src/types/managed/wrapped/egld_or_esdt_token_identifier.rs @@ -207,10 +207,8 @@ impl TypeAbiFrom<&TokenIdentifier> for EgldOrEsdtTokenIdentifier where impl TypeAbiFrom<&[u8]> for EgldOrEsdtTokenIdentifier where M: ManagedTypeApi {} impl TypeAbiFrom<&str> for EgldOrEsdtTokenIdentifier where M: ManagedTypeApi {} -impl TypeAbiFrom> for EgldOrEsdtTokenIdentifier where - M: ManagedTypeApi -{ -} +impl TypeAbiFrom> for EgldOrEsdtTokenIdentifier where M: ManagedTypeApi +{} impl TypeAbiFrom<&TestTokenIdentifier<'_>> for EgldOrEsdtTokenIdentifier where M: ManagedTypeApi { From 05ddbbc97df09ae25bc6a0b9a204f71b36d21862 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Fri, 29 Nov 2024 15:52:27 +0200 Subject: [PATCH 46/66] ManagedVec - iterator cleanup (ref vs. owned) --- .../examples/lottery-esdt/src/lottery.rs | 2 +- .../examples/multisig/src/multisig_state.rs | 3 ++- .../examples/order-book/pair/src/common.rs | 2 +- .../examples/order-book/pair/src/events.rs | 2 +- .../seed-nft-minter/src/seed_nft_minter.rs | 2 +- .../first-contract/src/lib.rs | 4 ++-- .../forwarder-raw/src/forwarder_raw.rs | 2 +- .../composability/vault/src/vault.rs | 2 +- contracts/modules/src/staking.rs | 2 +- framework/base/src/types/heap/arg_buffer.rs | 2 +- .../types/interaction/managed_arg_buffer.rs | 2 +- .../esdt_token_payment_multi_value.rs | 3 ++- .../multi_value/multi_value_encoded.rs | 21 +++------------- .../multi_value_encoded_counted.rs | 4 ++-- .../multi_value/multi_value_managed_vec.rs | 6 +++-- .../multi_value_managed_vec_counted.rs | 6 +++-- .../src/types/managed/wrapped/managed_ref.rs | 6 ++--- .../src/types/managed/wrapped/managed_vec.rs | 9 +++---- .../managed/wrapped/managed_vec_owned_iter.rs | 24 +++++++++---------- .../types/managed/wrapped/managed_vec_ref.rs | 5 ++++ .../managed/wrapped/managed_vec_ref_iter.rs | 12 ++++++++++ .../types/managed/wrapped/traits/mergeable.rs | 2 +- framework/scenario/tests/managed_ref_test.rs | 4 ++-- 23 files changed, 68 insertions(+), 59 deletions(-) diff --git a/contracts/examples/lottery-esdt/src/lottery.rs b/contracts/examples/lottery-esdt/src/lottery.rs index 3c5d9040b1..5c573ef651 100644 --- a/contracts/examples/lottery-esdt/src/lottery.rs +++ b/contracts/examples/lottery-esdt/src/lottery.rs @@ -148,7 +148,7 @@ pub trait Lottery { if let Some(whitelist) = opt_whitelist.as_option() { let mut mapper = self.lottery_whitelist(&lottery_name); for addr in &*whitelist { - mapper.insert(addr); + mapper.insert(addr.clone()); } } diff --git a/contracts/examples/multisig/src/multisig_state.rs b/contracts/examples/multisig/src/multisig_state.rs index bfadd8efd6..bd9002f770 100644 --- a/contracts/examples/multisig/src/multisig_state.rs +++ b/contracts/examples/multisig/src/multisig_state.rs @@ -37,6 +37,7 @@ pub trait MultisigStateModule { fn add_multiple_board_members(&self, new_board_members: ManagedVec) -> usize { let mut duplicates = false; + let new_board_members_len = new_board_members.len(); self.user_mapper().get_or_create_users( new_board_members.into_iter(), |user_id, new_user| { @@ -49,7 +50,7 @@ pub trait MultisigStateModule { require!(!duplicates, "duplicate board member"); let num_board_members_mapper = self.num_board_members(); - let new_num_board_members = num_board_members_mapper.get() + new_board_members.len(); + let new_num_board_members = num_board_members_mapper.get() + new_board_members_len; num_board_members_mapper.set(new_num_board_members); new_num_board_members diff --git a/contracts/examples/order-book/pair/src/common.rs b/contracts/examples/order-book/pair/src/common.rs index 85b1c1bca8..ce28101992 100644 --- a/contracts/examples/order-book/pair/src/common.rs +++ b/contracts/examples/order-book/pair/src/common.rs @@ -8,7 +8,7 @@ pub const FREE_ORDER_FROM_STORAGE_MIN_PENALTIES: u64 = 6; #[type_abi] #[derive( - ManagedVecItem, TopEncode, TopDecode, NestedEncode, NestedDecode, PartialEq, Eq, Clone, Copy + ManagedVecItem, TopEncode, TopDecode, NestedEncode, NestedDecode, PartialEq, Eq, Clone, Copy, )] pub enum OrderType { Buy, diff --git a/contracts/examples/order-book/pair/src/events.rs b/contracts/examples/order-book/pair/src/events.rs index f20331f44f..6b66feb37d 100644 --- a/contracts/examples/order-book/pair/src/events.rs +++ b/contracts/examples/order-book/pair/src/events.rs @@ -7,7 +7,7 @@ pub trait EventsModule { fn emit_order_event(&self, order: Order) { let caller = self.blockchain().get_caller(); let epoch = self.blockchain().get_block_epoch(); - let order_type = order.order_type.clone(); + let order_type = order.order_type; self.order_event(caller, epoch, order_type, order); } diff --git a/contracts/examples/seed-nft-minter/src/seed_nft_minter.rs b/contracts/examples/seed-nft-minter/src/seed_nft_minter.rs index f0cff643c6..510212891b 100644 --- a/contracts/examples/seed-nft-minter/src/seed_nft_minter.rs +++ b/contracts/examples/seed-nft-minter/src/seed_nft_minter.rs @@ -27,7 +27,7 @@ pub trait SeedNftMinter: marketplaces: ManagedVec, distribution: ManagedVec>, ) { - self.marketplaces().extend(&marketplaces); + self.marketplaces().extend(marketplaces); self.init_distribution(distribution); } diff --git a/contracts/feature-tests/composability/esdt-contract-pair/first-contract/src/lib.rs b/contracts/feature-tests/composability/esdt-contract-pair/first-contract/src/lib.rs index 2d35ac5ea8..bf77e7ab4e 100644 --- a/contracts/feature-tests/composability/esdt-contract-pair/first-contract/src/lib.rs +++ b/contracts/feature-tests/composability/esdt-contract-pair/first-contract/src/lib.rs @@ -132,8 +132,8 @@ pub trait FirstContract { arg_buffer.push_arg(esdt_token_identifier); arg_buffer.push_arg(amount); arg_buffer.push_arg(func_name); - for arg in args.into_iter() { - arg_buffer.push_arg_raw(arg); + for arg in args { + arg_buffer.push_arg_raw(arg.clone()); } self.tx() diff --git a/contracts/feature-tests/composability/forwarder-raw/src/forwarder_raw.rs b/contracts/feature-tests/composability/forwarder-raw/src/forwarder_raw.rs index d481fd6746..a436e97b7a 100644 --- a/contracts/feature-tests/composability/forwarder-raw/src/forwarder_raw.rs +++ b/contracts/feature-tests/composability/forwarder-raw/src/forwarder_raw.rs @@ -35,7 +35,7 @@ pub trait ForwarderRaw: )); } } else { - for payment in payments.into_iter() { + for payment in payments.iter() { let _ = self.callback_payments().push(&( EgldOrEsdtTokenIdentifier::esdt(payment.token_identifier), payment.token_nonce, diff --git a/contracts/feature-tests/composability/vault/src/vault.rs b/contracts/feature-tests/composability/vault/src/vault.rs index 86ddbe31bb..37b0d65b9e 100644 --- a/contracts/feature-tests/composability/vault/src/vault.rs +++ b/contracts/feature-tests/composability/vault/src/vault.rs @@ -220,7 +220,7 @@ pub trait Vault { let mut new_tokens = ManagedVec::new(); - for payment in payments.into_iter() { + for payment in payments.iter() { // burn old tokens self.send().esdt_local_burn( &payment.token_identifier, diff --git a/contracts/modules/src/staking.rs b/contracts/modules/src/staking.rs index 7e3ce968d5..b9a76ecb01 100644 --- a/contracts/modules/src/staking.rs +++ b/contracts/modules/src/staking.rs @@ -22,7 +22,7 @@ pub trait StakingModule { user_whitelist: &ManagedVec, ) { for user in user_whitelist { - let _ = self.user_whitelist().insert(user); + let _ = self.user_whitelist().insert(user.clone()); } let nr_board_members = self.user_whitelist().len(); diff --git a/framework/base/src/types/heap/arg_buffer.rs b/framework/base/src/types/heap/arg_buffer.rs index c27be12f2c..dde5f89a06 100644 --- a/framework/base/src/types/heap/arg_buffer.rs +++ b/framework/base/src/types/heap/arg_buffer.rs @@ -91,7 +91,7 @@ where { fn from(managed_arg_buffer: &ManagedArgBuffer) -> Self { let mut result = Self::new(); - for m_arg in managed_arg_buffer.data.into_iter() { + for m_arg in &managed_arg_buffer.data { result.push_argument_bytes(m_arg.to_boxed_bytes().as_slice()); } result diff --git a/framework/base/src/types/interaction/managed_arg_buffer.rs b/framework/base/src/types/interaction/managed_arg_buffer.rs index e1b09441fc..fabe3f2c6d 100644 --- a/framework/base/src/types/interaction/managed_arg_buffer.rs +++ b/framework/base/src/types/interaction/managed_arg_buffer.rs @@ -168,7 +168,7 @@ where #[cfg(feature = "alloc")] pub fn to_raw_args_vec(&self) -> Vec> { let mut v = Vec::new(); - for item in self.data.into_iter() { + for item in &self.data { v.push(item.to_boxed_bytes().into_vec()); } v diff --git a/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs b/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs index 1d87fb2274..e97cc2707c 100644 --- a/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs +++ b/framework/base/src/types/managed/multi_value/esdt_token_payment_multi_value.rs @@ -3,7 +3,8 @@ use crate::{ codec::{ multi_types::MultiValue3, DecodeErrorHandler, EncodeErrorHandler, TopDecodeMulti, TopDecodeMultiInput, TopDecodeMultiLength, TopEncodeMulti, TopEncodeMultiOutput, - }, types::ManagedVecRef, + }, + types::ManagedVecRef, }; use crate::{ diff --git a/framework/base/src/types/managed/multi_value/multi_value_encoded.rs b/framework/base/src/types/managed/multi_value/multi_value_encoded.rs index a598ba9795..ca10b9ec6e 100644 --- a/framework/base/src/types/managed/multi_value/multi_value_encoded.rs +++ b/framework/base/src/types/managed/multi_value/multi_value_encoded.rs @@ -92,26 +92,11 @@ where try_cast_execute_or_else( v, MultiValueEncoded::from_raw_vec, - |v| MultiValueEncoded::from(&v), + MultiValueEncoded::from, ) } } -impl From<&ManagedVec> for MultiValueEncoded -where - M: ManagedTypeApi, - T: ManagedVecItem + TopEncode, -{ - #[inline] - fn from(v: &ManagedVec) -> Self { - let mut result = MultiValueEncoded::new(); - for item in v.into_iter() { - result.push(item); - } - result - } -} - impl MultiValueEncoded where M: ManagedTypeApi, @@ -181,7 +166,7 @@ where pub fn to_vec(&self) -> ManagedVec { let mut result = ManagedVec::new(); let serializer = ManagedSerializer::::new(); - for item in self.raw_buffers.into_iter() { + for item in &self.raw_buffers { result.push(serializer.top_decode_from_managed_buffer(&item)); } result @@ -198,7 +183,7 @@ where O: TopEncodeMultiOutput, H: EncodeErrorHandler, { - for elem in self.raw_buffers.into_iter() { + for elem in &self.raw_buffers { elem.multi_encode_or_handle_err(output, h)?; } Ok(()) diff --git a/framework/base/src/types/managed/multi_value/multi_value_encoded_counted.rs b/framework/base/src/types/managed/multi_value/multi_value_encoded_counted.rs index ce53008377..94b75f5fd2 100644 --- a/framework/base/src/types/managed/multi_value/multi_value_encoded_counted.rs +++ b/framework/base/src/types/managed/multi_value/multi_value_encoded_counted.rs @@ -136,7 +136,7 @@ where pub fn to_vec(&self) -> ManagedVec { let mut result = ManagedVec::new(); let serializer = ManagedSerializer::::new(); - for item in self.raw_buffers.into_iter() { + for item in &self.raw_buffers { result.push(serializer.top_decode_from_managed_buffer(&item)); } result @@ -156,7 +156,7 @@ where let raw_count = self.raw_buffers.len(); let count = raw_count / T::LEN; count.multi_encode_or_handle_err(output, h)?; - for elem in self.raw_buffers.into_iter() { + for elem in &self.raw_buffers { elem.multi_encode_or_handle_err(output, h)?; } Ok(()) diff --git a/framework/base/src/types/managed/multi_value/multi_value_managed_vec.rs b/framework/base/src/types/managed/multi_value/multi_value_managed_vec.rs index feb99b561d..e342d88cf1 100644 --- a/framework/base/src/types/managed/multi_value/multi_value_managed_vec.rs +++ b/framework/base/src/types/managed/multi_value/multi_value_managed_vec.rs @@ -1,3 +1,5 @@ +use core::borrow::Borrow; + use multiversx_sc_codec::multi_types::MultiValueVec; use crate::{ @@ -181,8 +183,8 @@ where O: TopEncodeMultiOutput, H: EncodeErrorHandler, { - for elem in self.0.into_iter() { - elem.multi_encode_or_handle_err(output, h)?; + for elem in &self.0 { + elem.borrow().multi_encode_or_handle_err(output, h)?; } Ok(()) } diff --git a/framework/base/src/types/managed/multi_value/multi_value_managed_vec_counted.rs b/framework/base/src/types/managed/multi_value/multi_value_managed_vec_counted.rs index 0cb695e0d5..4f51989a9b 100644 --- a/framework/base/src/types/managed/multi_value/multi_value_managed_vec_counted.rs +++ b/framework/base/src/types/managed/multi_value/multi_value_managed_vec_counted.rs @@ -1,3 +1,5 @@ +use core::borrow::Borrow; + use crate::{ abi::{TypeAbi, TypeAbiFrom, TypeDescriptionContainer, TypeName}, api::ManagedTypeApi, @@ -122,8 +124,8 @@ where H: EncodeErrorHandler, { self.len().multi_encode_or_handle_err(output, h)?; - for elem in self.contents.into_iter() { - elem.multi_encode_or_handle_err(output, h)?; + for elem in &self.contents { + elem.borrow().multi_encode_or_handle_err(output, h)?; } Ok(()) } diff --git a/framework/base/src/types/managed/wrapped/managed_ref.rs b/framework/base/src/types/managed/wrapped/managed_ref.rs index f618063b3c..ba99ab0880 100644 --- a/framework/base/src/types/managed/wrapped/managed_ref.rs +++ b/framework/base/src/types/managed/wrapped/managed_ref.rs @@ -60,13 +60,13 @@ where } } -impl<'a, M, T> Clone for ManagedRef<'a, M, T> +impl<'a, M, T> ManagedRef<'a, M, T> where M: ManagedTypeApi, T: ManagedType, { - #[inline] - fn clone(&self) -> Self { + /// Clones the reference itself, not the object contained therein. + pub fn clone_ref(&self) -> Self { Self { _phantom_m: PhantomData, _phantom_t: PhantomData, diff --git a/framework/base/src/types/managed/wrapped/managed_vec.rs b/framework/base/src/types/managed/wrapped/managed_vec.rs index ab93734a06..ebb0f0d157 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec.rs @@ -521,7 +521,7 @@ where fn clone(&self) -> Self { let mut result = ManagedVec::new(); for item in self.into_iter() { - result.push(item.clone()) + result.push(item.borrow().clone()) } result } @@ -611,7 +611,8 @@ where } else { let mut nested_buffer = output.start_nested_encode(); for item in self { - item.dep_encode_or_handle_err(&mut nested_buffer, h)?; + item.borrow() + .dep_encode_or_handle_err(&mut nested_buffer, h)?; } output.finalize_nested_encode(nested_buffer); Ok(()) @@ -631,7 +632,7 @@ where { self.len().dep_encode_or_handle_err(dest, h)?; for item in self { - item.dep_encode_or_handle_err(dest, h)?; + item.borrow().dep_encode_or_handle_err(dest, h)?; } Ok(()) } @@ -748,7 +749,7 @@ where fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { let mut dbg_list = f.debug_list(); for item in self.into_iter() { - dbg_list.entry(&item); + dbg_list.entry(item.borrow()); } dbg_list.finish() } diff --git a/framework/base/src/types/managed/wrapped/managed_vec_owned_iter.rs b/framework/base/src/types/managed/wrapped/managed_vec_owned_iter.rs index 8e0da8727c..b688bb466c 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_owned_iter.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_owned_iter.rs @@ -2,34 +2,34 @@ use crate::api::ManagedTypeApi; use super::{ManagedVec, ManagedVecItem, ManagedVecItemPayload}; -impl<'a, M, T> IntoIterator for &'a ManagedVec +impl IntoIterator for ManagedVec where M: ManagedTypeApi, T: ManagedVecItem, { type Item = T; - type IntoIter = ManagedVecOwnedIterator<'a, M, T>; + type IntoIter = ManagedVecOwnedIterator; fn into_iter(self) -> Self::IntoIter { ManagedVecOwnedIterator::new(self) } } -pub struct ManagedVecOwnedIterator<'a, M, T> +pub struct ManagedVecOwnedIterator where M: ManagedTypeApi, T: ManagedVecItem, { - managed_vec: &'a ManagedVec, + managed_vec: ManagedVec, byte_start: usize, byte_end: usize, } -impl<'a, M, T> ManagedVecOwnedIterator<'a, M, T> +impl ManagedVecOwnedIterator where M: ManagedTypeApi, T: ManagedVecItem, { - pub(crate) fn new(managed_vec: &'a ManagedVec) -> Self { + pub(crate) fn new(managed_vec: ManagedVec) -> Self { let byte_end = managed_vec.byte_len(); ManagedVecOwnedIterator { managed_vec, @@ -39,7 +39,7 @@ where } } -impl<'a, M, T> Iterator for ManagedVecOwnedIterator<'a, M, T> +impl Iterator for ManagedVecOwnedIterator where M: ManagedTypeApi, T: ManagedVecItem, @@ -70,14 +70,14 @@ where } } -impl<'a, M, T> ExactSizeIterator for ManagedVecOwnedIterator<'a, M, T> +impl ExactSizeIterator for ManagedVecOwnedIterator where M: ManagedTypeApi, T: ManagedVecItem, { } -impl<'a, M, T> DoubleEndedIterator for ManagedVecOwnedIterator<'a, M, T> +impl DoubleEndedIterator for ManagedVecOwnedIterator where M: ManagedTypeApi, T: ManagedVecItem, @@ -98,15 +98,15 @@ where } } -impl<'a, M, T> Clone for ManagedVecOwnedIterator<'a, M, T> +impl Clone for ManagedVecOwnedIterator where M: ManagedTypeApi, - T: ManagedVecItem, + T: ManagedVecItem + Clone, { fn clone(&self) -> Self { let byte_end = self.byte_end; Self { - managed_vec: self.managed_vec, + managed_vec: self.managed_vec.clone(), byte_start: self.byte_start, byte_end, } diff --git a/framework/base/src/types/managed/wrapped/managed_vec_ref.rs b/framework/base/src/types/managed/wrapped/managed_vec_ref.rs index 987cb60614..de78fd5e6f 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_ref.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_ref.rs @@ -13,6 +13,11 @@ impl ManagedVecRef<'_, T> where T: ManagedVecItem, { + /// Creates a new ManagedVecRef instance. + /// + /// ## Safety + /// + /// The ManagedVecRef object might not drop its content, effectively leading to a leak. pub unsafe fn new(item: T) -> Self { ManagedVecRef { _phantom: PhantomData, diff --git a/framework/base/src/types/managed/wrapped/managed_vec_ref_iter.rs b/framework/base/src/types/managed/wrapped/managed_vec_ref_iter.rs index 4703a1f61b..9720ba741f 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_ref_iter.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_ref_iter.rs @@ -2,6 +2,18 @@ use crate::api::ManagedTypeApi; use super::{ManagedVec, ManagedVecItem, ManagedVecItemPayload}; +impl<'a, M, T> IntoIterator for &'a ManagedVec +where + M: ManagedTypeApi, + T: ManagedVecItem, +{ + type Item = T::Ref<'a>; + type IntoIter = ManagedVecRefIterator<'a, M, T>; + fn into_iter(self) -> Self::IntoIter { + ManagedVecRefIterator::new(self) + } +} + pub struct ManagedVecRefIterator<'a, M, T> where M: ManagedTypeApi, diff --git a/framework/base/src/types/managed/wrapped/traits/mergeable.rs b/framework/base/src/types/managed/wrapped/traits/mergeable.rs index 51298ba130..295357f9b6 100644 --- a/framework/base/src/types/managed/wrapped/traits/mergeable.rs +++ b/framework/base/src/types/managed/wrapped/traits/mergeable.rs @@ -21,7 +21,7 @@ pub trait Mergeable { where Self: Sized + ManagedVecItem, { - for item in &others { + for item in others { self.merge_with(item); } } diff --git a/framework/scenario/tests/managed_ref_test.rs b/framework/scenario/tests/managed_ref_test.rs index 2edfeac515..dedba7904f 100644 --- a/framework/scenario/tests/managed_ref_test.rs +++ b/framework/scenario/tests/managed_ref_test.rs @@ -16,9 +16,9 @@ where let obj_ref = obj.as_ref(); assert_eq!( obj_ref.get_handle(), - ManagedRef::get_raw_handle_of_ref(obj_ref.clone()) + ManagedRef::get_raw_handle_of_ref(obj_ref.clone_ref()) ); - let obj_clone: T = Clone::clone(&obj_ref); + let obj_clone: T = obj_ref.clone(); assert_eq!(obj, obj_clone); assert_ne!(obj_ref.get_handle(), obj_clone.get_handle()); } From 490a31c3adf3524b64a8229361d48dbedcc7d157 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Fri, 29 Nov 2024 17:16:08 +0200 Subject: [PATCH 47/66] stack overflow fix --- .../base/src/types/managed/multi_value/multi_value_encoded.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/framework/base/src/types/managed/multi_value/multi_value_encoded.rs b/framework/base/src/types/managed/multi_value/multi_value_encoded.rs index ca10b9ec6e..e69d741f4a 100644 --- a/framework/base/src/types/managed/multi_value/multi_value_encoded.rs +++ b/framework/base/src/types/managed/multi_value/multi_value_encoded.rs @@ -87,12 +87,11 @@ where T: ManagedVecItem + TopEncode + 'static, { #[inline] - #[rustfmt::skip] fn from(v: ManagedVec) -> Self { try_cast_execute_or_else( v, MultiValueEncoded::from_raw_vec, - MultiValueEncoded::from, + MultiValueEncoded::from_iter, ) } } From 809b71a17ee9af5760f1d3ad83bfe6244e7a0f41 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Fri, 29 Nov 2024 17:49:15 +0200 Subject: [PATCH 48/66] ManagedVecItem - ref for tuples --- .../basic-features/src/echo_managed.rs | 4 ++-- .../multi_value/multi_value_managed_vec.rs | 16 +++++++++++++++- .../types/managed/wrapped/managed_vec_item.rs | 10 ++++------ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/contracts/feature-tests/basic-features/src/echo_managed.rs b/contracts/feature-tests/basic-features/src/echo_managed.rs index ffabfa5b16..98154b2d9f 100644 --- a/contracts/feature-tests/basic-features/src/echo_managed.rs +++ b/contracts/feature-tests/basic-features/src/echo_managed.rs @@ -159,11 +159,11 @@ pub trait EchoManagedTypes { MultiValue3>>, > { let mut result = MultiValueEncoded::new(); - for triple in address_number_pairs.into_iter() { + for triple in address_number_pairs { let (address, x, counted_list) = triple.into_tuple(); let mut counted_lazy = MultiValueEncodedCounted::new(); let v = counted_list.into_vec(); - for pair in &v { + for pair in v { counted_lazy.push(pair); } result.push((address, x, counted_lazy).into()); diff --git a/framework/base/src/types/managed/multi_value/multi_value_managed_vec.rs b/framework/base/src/types/managed/multi_value/multi_value_managed_vec.rs index e342d88cf1..aa34600120 100644 --- a/framework/base/src/types/managed/multi_value/multi_value_managed_vec.rs +++ b/framework/base/src/types/managed/multi_value/multi_value_managed_vec.rs @@ -9,7 +9,7 @@ use crate::{ DecodeErrorHandler, EncodeErrorHandler, TopDecodeMulti, TopDecodeMultiInput, TopEncodeMulti, TopEncodeMultiOutput, Vec, }, - types::ManagedType, + types::{ManagedType, ManagedVecOwnedIterator}, }; use crate::types::{ManagedVec, ManagedVecItem, ManagedVecRefIterator}; @@ -144,6 +144,20 @@ where } } +impl IntoIterator for MultiValueManagedVec +where + M: ManagedTypeApi, + T: ManagedVecItem, +{ + type Item = T; + + type IntoIter = ManagedVecOwnedIterator; + + fn into_iter(self) -> Self::IntoIter { + self.0.into_iter() + } +} + impl<'a, M, T> IntoIterator for &'a MultiValueManagedVec where M: ManagedTypeApi, diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item.rs b/framework/base/src/types/managed/wrapped/managed_vec_item.rs index d14ae0067f..62e62ad4dd 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item.rs @@ -324,7 +324,7 @@ where { type PAYLOAD = <(T1, (T2, ())) as ManagedVecItemNestedTuple>::PAYLOAD; const SKIPS_RESERIALIZATION: bool = T1::SKIPS_RESERIALIZATION && T2::SKIPS_RESERIALIZATION; - type Ref<'a> = Self; + type Ref<'a> = ManagedVecRef<'a, Self>; fn read_from_payload(payload: &Self::PAYLOAD) -> Self { let mut index = 0; @@ -338,8 +338,7 @@ where } unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> { - // TODO: tuple of refs - Self::read_from_payload(payload) + ManagedVecRef::new(Self::read_from_payload(payload)) } fn save_to_payload(self, payload: &mut Self::PAYLOAD) { @@ -362,7 +361,7 @@ where { type PAYLOAD = <(T1, (T2, (T3, ()))) as ManagedVecItemNestedTuple>::PAYLOAD; const SKIPS_RESERIALIZATION: bool = T1::SKIPS_RESERIALIZATION && T2::SKIPS_RESERIALIZATION; - type Ref<'a> = Self; + type Ref<'a> = ManagedVecRef<'a, Self>; fn read_from_payload(payload: &Self::PAYLOAD) -> Self { let mut index = 0; @@ -377,8 +376,7 @@ where } unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> { - // TODO: tuple of refs - Self::read_from_payload(payload) + ManagedVecRef::new(Self::read_from_payload(payload)) } fn save_to_payload(self, payload: &mut Self::PAYLOAD) { From d1095ad25973548f52a65500f39f1422a30d9555 Mon Sep 17 00:00:00 2001 From: BiancaIalangi Date: Mon, 2 Dec 2024 15:52:09 +0200 Subject: [PATCH 49/66] unified - payable blackbox --- .../payable-features/src/payable_features.rs | 4 +- .../src/payable_features_proxy.rs | 184 ++++++++++++++++++ .../tests/payable_blackbox_test.rs | 71 ++++--- 3 files changed, 230 insertions(+), 29 deletions(-) create mode 100644 contracts/feature-tests/payable-features/src/payable_features_proxy.rs diff --git a/contracts/feature-tests/payable-features/src/payable_features.rs b/contracts/feature-tests/payable-features/src/payable_features.rs index d8296f8592..37fe3f0c9d 100644 --- a/contracts/feature-tests/payable-features/src/payable_features.rs +++ b/contracts/feature-tests/payable-features/src/payable_features.rs @@ -1,7 +1,9 @@ #![no_std] #![allow(clippy::type_complexity)] -multiversx_sc::imports!(); +use multiversx_sc::imports::*; + +pub mod payable_features_proxy; /// Contract that only tests the call value features, /// i.e. the framework/Arwen functionality for accepting EGLD and ESDT payments. diff --git a/contracts/feature-tests/payable-features/src/payable_features_proxy.rs b/contracts/feature-tests/payable-features/src/payable_features_proxy.rs new file mode 100644 index 0000000000..7d6e020c89 --- /dev/null +++ b/contracts/feature-tests/payable-features/src/payable_features_proxy.rs @@ -0,0 +1,184 @@ +// Code generated by the multiversx-sc proxy generator. DO NOT EDIT. + +//////////////////////////////////////////////////// +////////////////// AUTO-GENERATED ////////////////// +//////////////////////////////////////////////////// + +#![allow(dead_code)] +#![allow(clippy::all)] + +use multiversx_sc::proxy_imports::*; + +pub struct PayableFeaturesProxy; + +impl TxProxyTrait for PayableFeaturesProxy +where + Env: TxEnv, + From: TxFrom, + To: TxTo, + Gas: TxGas, +{ + type TxProxyMethods = PayableFeaturesProxyMethods; + + fn proxy_methods(self, tx: Tx) -> Self::TxProxyMethods { + PayableFeaturesProxyMethods { wrapped_tx: tx } + } +} + +pub struct PayableFeaturesProxyMethods +where + Env: TxEnv, + From: TxFrom, + To: TxTo, + Gas: TxGas, +{ + wrapped_tx: Tx, +} + +#[rustfmt::skip] +impl PayableFeaturesProxyMethods +where + Env: TxEnv, + Env::Api: VMApi, + From: TxFrom, + Gas: TxGas, +{ + pub fn init( + self, + ) -> TxTypedDeploy { + self.wrapped_tx + .payment(NotPayable) + .raw_deploy() + .original_result() + } +} + +#[rustfmt::skip] +impl PayableFeaturesProxyMethods +where + Env: TxEnv, + Env::Api: VMApi, + From: TxFrom, + To: TxTo, + Gas: TxGas, +{ + pub fn echo_call_value( + self, + ) -> TxTypedCall, ManagedVec>>> { + self.wrapped_tx + .raw_call("echo_call_value") + .original_result() + } + + pub fn payment_multiple( + self, + ) -> TxTypedCall>> { + self.wrapped_tx + .raw_call("payment_multiple") + .original_result() + } + + pub fn payment_array_3( + self, + ) -> TxTypedCall, EsdtTokenPayment, EsdtTokenPayment>> { + self.wrapped_tx + .raw_call("payment_array_3") + .original_result() + } + + pub fn payable_any_1( + self, + ) -> TxTypedCall, EgldOrEsdtTokenIdentifier>> { + self.wrapped_tx + .raw_call("payable_any_1") + .original_result() + } + + pub fn payable_any_2( + self, + ) -> TxTypedCall, EgldOrEsdtTokenIdentifier>> { + self.wrapped_tx + .raw_call("payable_any_2") + .original_result() + } + + pub fn payable_any_3( + self, + ) -> TxTypedCall, EgldOrEsdtTokenIdentifier>> { + self.wrapped_tx + .raw_call("payable_any_3") + .original_result() + } + + pub fn payable_any_4( + self, + ) -> TxTypedCall, EgldOrEsdtTokenIdentifier>> { + self.wrapped_tx + .raw_call("payable_any_4") + .original_result() + } + + pub fn payable_egld_1( + self, + ) -> TxTypedCall, EgldOrEsdtTokenIdentifier>> { + self.wrapped_tx + .raw_call("payable_egld_1") + .original_result() + } + + pub fn payable_egld_2( + self, + ) -> TxTypedCall, EgldOrEsdtTokenIdentifier>> { + self.wrapped_tx + .raw_call("payable_egld_2") + .original_result() + } + + pub fn payable_egld_3( + self, + ) -> TxTypedCall, EgldOrEsdtTokenIdentifier>> { + self.wrapped_tx + .raw_call("payable_egld_3") + .original_result() + } + + pub fn payable_egld_4( + self, + ) -> TxTypedCall, EgldOrEsdtTokenIdentifier>> { + self.wrapped_tx + .raw_call("payable_egld_4") + .original_result() + } + + pub fn payable_token_1( + self, + ) -> TxTypedCall, EgldOrEsdtTokenIdentifier>> { + self.wrapped_tx + .raw_call("payable_token_1") + .original_result() + } + + pub fn payable_token_2( + self, + ) -> TxTypedCall, TokenIdentifier>> { + self.wrapped_tx + .raw_call("payable_token_2") + .original_result() + } + + pub fn payable_token_3( + self, + ) -> TxTypedCall, EgldOrEsdtTokenIdentifier>> { + self.wrapped_tx + .raw_call("payable_token_3") + .original_result() + } + + pub fn payable_token_4( + self, + ) -> TxTypedCall, TokenIdentifier>> { + self.wrapped_tx + .raw_call("payable_token_4") + .original_result() + } +} diff --git a/contracts/feature-tests/payable-features/tests/payable_blackbox_test.rs b/contracts/feature-tests/payable-features/tests/payable_blackbox_test.rs index 44aa98c1b5..b56dc46aaa 100644 --- a/contracts/feature-tests/payable-features/tests/payable_blackbox_test.rs +++ b/contracts/feature-tests/payable-features/tests/payable_blackbox_test.rs @@ -1,6 +1,13 @@ use multiversx_sc_scenario::imports::*; +use payable_features::payable_features_proxy; -const PF_PATH_EXPR: &str = "mxsc:output/payable-features.mxsc.json"; +const PF_PATH_EXPR: MxscPath = MxscPath::new("output/payable-features.mxsc.json"); +const PAYABLE_FEATURES_ADDRESS: TestSCAddress = TestSCAddress::new("payable-features"); +const USER: TestAddress = TestAddress::new("an-account"); +const TOKEN_1: TestTokenIdentifier = TestTokenIdentifier::new("TOK-000001"); +const TOKEN_2: TestTokenIdentifier = TestTokenIdentifier::new("TOK-000002"); +const TOKEN_3: TestTokenIdentifier = TestTokenIdentifier::new("TOK-000003"); +const SFT: TestTokenIdentifier = TestTokenIdentifier::new("SFT-123"); fn world() -> ScenarioWorld { let mut blockchain = ScenarioWorld::new(); @@ -13,33 +20,41 @@ fn world() -> ScenarioWorld { #[test] fn payable_multi() { let mut world = world(); - let pf_code = world.code_expression(PF_PATH_EXPR); world - .set_state_step( - SetStateStep::new() - .put_account("sc:payable-features", Account::new().code(pf_code)) - .put_account( - "address:an-account", - Account::new() - .balance("10000") - .esdt_balance("str:TOK-000001", "1000") - .esdt_balance("str:TOK-000002", "500") - .esdt_balance("str:TOK-000003", "500") - .esdt_nft_balance("str:SFT-123", 5u32, 10u32, Option::<()>::None) - ), - ) - .sc_call( - ScCallStep::new() - .from("address:an-account") - .to("sc:payable-features") - .function("echo_call_value") - .esdt_transfer("str:TOK-000001", 0, "100") - .esdt_transfer("str:TOK-000002", 0, "400") - .expect( - TxExpect::ok() - .result("0") - .result("nested:str:TOK-000001|u64:0|biguint:100|nested:str:TOK-000002|u64:0|biguint:400") - ), - ); + .account(USER) + .balance(10000) + .esdt_balance(TOKEN_1, 1000) + .esdt_balance(TOKEN_2, 500) + .esdt_balance(TOKEN_3, 500) + .esdt_nft_balance(SFT, 5, 10, ()); + + world + .tx() + .from(USER) + .typed(payable_features_proxy::PayableFeaturesProxy) + .init() + .new_address(PAYABLE_FEATURES_ADDRESS) + .code(PF_PATH_EXPR) + .run(); + + let result = world + .tx() + .from(USER) + .to(PAYABLE_FEATURES_ADDRESS) + .typed(payable_features_proxy::PayableFeaturesProxy) + .echo_call_value() + .esdt(TestEsdtTransfer(TOKEN_1, 0, 100)) + .esdt(TestEsdtTransfer(TOKEN_2, 0, 400)) + .returns(ReturnsResultUnmanaged) + .run(); + + assert_eq!(result.0 .0, RustBigUint::from(0u32)); + assert_eq!( + result.0 .1, + vec![ + EsdtTokenPayment::new(TOKEN_1.to_token_identifier(), 0, BigUint::from(100u32)), + EsdtTokenPayment::new(TOKEN_2.to_token_identifier(), 0, BigUint::from(400u32)) + ] + ); } From 1401103b3789a6bde8b9a16941c477057903717a Mon Sep 17 00:00:00 2001 From: BiancaIalangi Date: Mon, 2 Dec 2024 16:28:40 +0200 Subject: [PATCH 50/66] insert payable token test --- .../scenarios/payable_token_5.scen.json | 120 ++++++++++++++++++ .../tests/payable_scenario_go_test.rs | 5 + .../tests/payable_scenario_rs_test.rs | 5 + 3 files changed, 130 insertions(+) create mode 100644 contracts/feature-tests/payable-features/scenarios/payable_token_5.scen.json diff --git a/contracts/feature-tests/payable-features/scenarios/payable_token_5.scen.json b/contracts/feature-tests/payable-features/scenarios/payable_token_5.scen.json new file mode 100644 index 0000000000..6193d505e4 --- /dev/null +++ b/contracts/feature-tests/payable-features/scenarios/payable_token_5.scen.json @@ -0,0 +1,120 @@ +{ + "name": "payable", + "gasSchedule": "v3", + "steps": [ + { + "step": "setState", + "accounts": { + "sc:payable-features": { + "nonce": "0", + "balance": "0", + "code": "mxsc:../output/payable-features.mxsc.json", + "codeMetadata": "0x0000" + }, + "address:an-account": { + "nonce": "0", + "balance": "1,000,000,000,000", + "esdt": { + "str:PAYABLE-FEATURES-TOKEN": "1,000,000,000,000", + "str:OTHER-TOKEN": "1,000,000,000,000" + } + } + } + }, + { + "step": "scCall", + "id": "payable_token_1.1", + "tx": { + "from": "address:an-account", + "to": "sc:payable-features", + "function": "payable_token_1", + "arguments": [], + "gasLimit": "50,000,000", + "gasPrice": "0" + }, + "expect": { + "out": [], + "status": "4", + "message": "str:function expects single ESDT payment", + "logs": "*", + "gas": "*", + "refund": "*" + } + }, + { + "step": "scCall", + "id": "payable_token_1.2", + "tx": { + "from": "address:an-account", + "to": "sc:payable-features", + "egldValue": "5", + "function": "payable_token_1", + "arguments": [], + "gasLimit": "50,000,000", + "gasPrice": "0" + }, + "expect": { + "out": [], + "status": "4", + "message": "str:function expects single ESDT payment", + "logs": "*", + "gas": "*", + "refund": "*" + } + }, + { + "step": "scCall", + "id": "payable_token_1.3", + "tx": { + "from": "address:an-account", + "to": "sc:payable-features", + "esdtValue": [ + { + "tokenIdentifier": "str:PAYABLE-FEATURES-TOKEN", + "value": "100" + } + ], + "function": "payable_token_1", + "arguments": [], + "gasLimit": "50,000,000", + "gasPrice": "0" + }, + "expect": { + "out": [ + "0x64", + "0x50415941424c452d46454154555245532d544f4b454e" + ], + "status": "0", + "logs": "*", + "gas": "*", + "refund": "*" + } + }, + { + "step": "scCall", + "id": "payable_token_1.4", + "tx": { + "from": "address:an-account", + "to": "sc:payable-features", + "esdtValue": [ + { + "tokenIdentifier": "str:OTHER-TOKEN", + "value": "100" + } + ], + "function": "payable_token_1", + "arguments": [], + "gasLimit": "50,000,000", + "gasPrice": "0" + }, + "expect": { + "out": [], + "status": "4", + "message": "str:bad call value token provided", + "logs": "*", + "gas": "*", + "refund": "*" + } + } + ] +} \ No newline at end of file diff --git a/contracts/feature-tests/payable-features/tests/payable_scenario_go_test.rs b/contracts/feature-tests/payable-features/tests/payable_scenario_go_test.rs index 58db78cf8f..a3ababd9c7 100644 --- a/contracts/feature-tests/payable-features/tests/payable_scenario_go_test.rs +++ b/contracts/feature-tests/payable-features/tests/payable_scenario_go_test.rs @@ -78,3 +78,8 @@ fn payable_token_3_go() { fn payable_token_4_go() { world().run("scenarios/payable_token_4.scen.json"); } + +#[test] +fn payable_token_5_go() { + world().run("scenarios/payable_token_5.scen.json"); +} diff --git a/contracts/feature-tests/payable-features/tests/payable_scenario_rs_test.rs b/contracts/feature-tests/payable-features/tests/payable_scenario_rs_test.rs index 4e88c1cd75..01b0ea567b 100644 --- a/contracts/feature-tests/payable-features/tests/payable_scenario_rs_test.rs +++ b/contracts/feature-tests/payable-features/tests/payable_scenario_rs_test.rs @@ -84,3 +84,8 @@ fn payable_token_3_rs() { fn payable_token_4_rs() { world().run("scenarios/payable_token_4.scen.json"); } + +#[test] +fn payable_token_5_rs() { + world().run("scenarios/payable_token_5.scen.json"); +} From afab864db5a78553793220961d2b2c9d2dbce790 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Tue, 3 Dec 2024 03:09:54 +0200 Subject: [PATCH 51/66] ManagedVecItem - ref for EsdtTokenPayment --- chain/core/src/types/flags/esdt_token_type.rs | 2 +- contracts/core/wegld-swap/src/wegld.rs | 8 ++-- .../digital-cash/src/pay_fee_and_fund.rs | 4 +- .../src/esdt_transfer_with_fee.rs | 4 +- .../fractional-nfts/src/fractional_nfts.rs | 2 +- .../examples/nft-subscription/src/lib.rs | 38 ++++++++++++---- .../order-book/pair/src/validation.rs | 14 ++++-- .../seed-nft-minter/src/seed_nft_minter.rs | 2 +- .../src/storage_mapper_fungible_token.rs | 9 ++-- .../first-contract/src/lib.rs | 12 +++--- .../child/src/lib.rs | 2 +- .../src/fwd_call_transf_exec_legacy.rs | 2 +- .../forwarder-legacy/src/fwd_esdt_legacy.rs | 4 +- .../forwarder-raw/src/forwarder_raw.rs | 4 +- .../forwarder-raw/src/forwarder_raw_async.rs | 4 +- .../composability/forwarder/src/fwd_esdt.rs | 4 +- .../src/fwd_call_promises_bt.rs | 14 +++--- .../composability/vault/src/vault.rs | 4 +- .../payable-features/src/payable_features.rs | 10 ++--- .../rust-testing-framework-tester/src/lib.rs | 4 +- .../use-module/src/token_merge_mod_impl.rs | 2 +- .../bonding_curve/utils/owner_endpoints.rs | 19 ++++---- .../src/bonding_curve/utils/user_endpoints.rs | 17 +++++--- .../modules/src/default_issue_callbacks.rs | 2 +- contracts/modules/src/governance/mod.rs | 2 +- contracts/modules/src/token_merge/mod.rs | 2 +- .../wrappers/call_value_wrapper.rs | 43 +++++++++++++------ .../contract_call_convert.rs | 2 +- .../contract_call_exec.rs | 2 +- .../result_handlers/returns_bt_single_esdt.rs | 3 +- .../managed/wrapped/esdt_token_payment.rs | 7 ++- .../scenario/tx_to_step/tx_to_step_call.rs | 2 +- .../tx_to_step/tx_to_step_transfer.rs | 2 +- 33 files changed, 152 insertions(+), 100 deletions(-) diff --git a/chain/core/src/types/flags/esdt_token_type.rs b/chain/core/src/types/flags/esdt_token_type.rs index 74db07c9a6..3668ca6fe6 100644 --- a/chain/core/src/types/flags/esdt_token_type.rs +++ b/chain/core/src/types/flags/esdt_token_type.rs @@ -13,7 +13,7 @@ const ESDT_TYPE_INVALID: &[u8] = &[]; // Note: In the current implementation, SemiFungible is never returned -#[derive(TopDecode, TopEncode, NestedDecode, NestedEncode, Clone, PartialEq, Eq, Debug)] +#[derive(TopDecode, TopEncode, NestedDecode, NestedEncode, Clone, Copy, PartialEq, Eq, Debug)] pub enum EsdtTokenType { Fungible, NonFungible, diff --git a/contracts/core/wegld-swap/src/wegld.rs b/contracts/core/wegld-swap/src/wegld.rs index 23a4493494..1c9cf11a2a 100644 --- a/contracts/core/wegld-swap/src/wegld.rs +++ b/contracts/core/wegld-swap/src/wegld.rs @@ -39,10 +39,10 @@ pub trait EgldEsdtSwap: multiversx_sc_modules::pause::PauseModule { let (payment_token, payment_amount) = self.call_value().single_fungible_esdt(); let wrapped_egld_token_id = self.wrapped_egld_token_id().get(); - require!(payment_token == wrapped_egld_token_id, "Wrong esdt token"); - require!(payment_amount > 0u32, "Must pay more than 0 tokens!"); + require!(*payment_token == wrapped_egld_token_id, "Wrong esdt token"); + require!(*payment_amount > 0u32, "Must pay more than 0 tokens!"); require!( - payment_amount <= self.get_locked_egld_balance(), + *payment_amount <= self.get_locked_egld_balance(), "Contract does not have enough funds" ); @@ -51,7 +51,7 @@ pub trait EgldEsdtSwap: multiversx_sc_modules::pause::PauseModule { // 1 wrapped eGLD = 1 eGLD, so we pay back the same amount let caller = self.blockchain().get_caller(); - self.tx().to(&caller).egld(&payment_amount).transfer(); + self.tx().to(&caller).egld(&*payment_amount).transfer(); } #[view(getLockedEgldBalance)] diff --git a/contracts/examples/digital-cash/src/pay_fee_and_fund.rs b/contracts/examples/digital-cash/src/pay_fee_and_fund.rs index 45e913c51d..edff9df18e 100644 --- a/contracts/examples/digital-cash/src/pay_fee_and_fund.rs +++ b/contracts/examples/digital-cash/src/pay_fee_and_fund.rs @@ -7,8 +7,8 @@ pub trait PayFeeAndFund: storage::StorageModule + helpers::HelpersModule { #[endpoint(payFeeAndFundESDT)] #[payable("*")] fn pay_fee_and_fund_esdt(&self, address: ManagedAddress, valability: u64) { - let mut payments = self.call_value().all_esdt_transfers().clone_value(); - let fee = EgldOrEsdtTokenPayment::from(payments.get(0)); + let mut payments = self.call_value().all_esdt_transfers().clone(); + let fee = EgldOrEsdtTokenPayment::from(payments.get(0).clone()); let caller_address = self.blockchain().get_caller(); self.update_fees(caller_address, &address, fee); diff --git a/contracts/examples/esdt-transfer-with-fee/src/esdt_transfer_with_fee.rs b/contracts/examples/esdt-transfer-with-fee/src/esdt_transfer_with_fee.rs index de883ef83a..5967c8feaa 100644 --- a/contracts/examples/esdt-transfer-with-fee/src/esdt_transfer_with_fee.rs +++ b/contracts/examples/esdt-transfer-with-fee/src/esdt_transfer_with_fee.rs @@ -71,13 +71,13 @@ pub trait EsdtTransferWithFee { "Mismatching payment for covering fees" ); let _ = self.get_payment_after_fees(fee_type, &next_payment); - new_payments.push(payment); + new_payments.push(payment.clone()); }, Fee::Percentage(_) => { new_payments.push(self.get_payment_after_fees(fee_type, &payment)); }, Fee::Unset => { - new_payments.push(payment); + new_payments.push(payment.clone()); }, } } diff --git a/contracts/examples/fractional-nfts/src/fractional_nfts.rs b/contracts/examples/fractional-nfts/src/fractional_nfts.rs index 16f1c87aed..2b9844449a 100644 --- a/contracts/examples/fractional-nfts/src/fractional_nfts.rs +++ b/contracts/examples/fractional-nfts/src/fractional_nfts.rs @@ -74,7 +74,7 @@ pub trait FractionalNfts: default_issue_callbacks::DefaultIssueCallbacksModule { let fractional_token = fractional_token_mapper.get_token_id_ref(); let hash = ManagedBuffer::new(); let fractional_info = - FractionalUriInfo::new(original_payment, initial_fractional_amount.clone()); + FractionalUriInfo::new(original_payment.clone(), initial_fractional_amount.clone()); let uris = fractional_info.to_uris(); let fractional_nonce = self.send().esdt_nft_create( diff --git a/contracts/examples/nft-subscription/src/lib.rs b/contracts/examples/nft-subscription/src/lib.rs index 7b0b63096e..210a20020f 100644 --- a/contracts/examples/nft-subscription/src/lib.rs +++ b/contracts/examples/nft-subscription/src/lib.rs @@ -49,34 +49,54 @@ pub trait NftSubscription: #[payable("*")] #[endpoint] fn update_attributes(&self, attributes: ManagedBuffer) { - let (id, nonce, _) = self.call_value().single_esdt().into_tuple(); - self.update_subscription_attributes::(&id, nonce, attributes); + let payment = self.call_value().single_esdt(); + self.update_subscription_attributes::( + &payment.token_identifier, + payment.token_nonce, + attributes, + ); self.tx() .to(ToCaller) - .single_esdt(&id, nonce, &BigUint::from(1u8)) + .single_esdt( + &payment.token_identifier, + payment.token_nonce, + &BigUint::from(1u8), + ) .transfer(); } #[payable("*")] #[endpoint] fn renew(&self, duration: u64) { - let (id, nonce, _) = self.call_value().single_esdt().into_tuple(); - self.renew_subscription::(&id, nonce, duration); + let payment = self.call_value().single_esdt(); + self.renew_subscription::( + &payment.token_identifier, + payment.token_nonce, + duration, + ); self.tx() .to(ToCaller) - .single_esdt(&id, nonce, &BigUint::from(1u8)) + .single_esdt( + &payment.token_identifier, + payment.token_nonce, + &BigUint::from(1u8), + ) .transfer(); } #[payable("*")] #[endpoint] fn cancel(&self) { - let (id, nonce, _) = self.call_value().single_esdt().into_tuple(); - self.cancel_subscription::(&id, nonce); + let payment = self.call_value().single_esdt(); + self.cancel_subscription::(&payment.token_identifier, payment.token_nonce); self.tx() .to(ToCaller) - .single_esdt(&id, nonce, &BigUint::from(1u8)) + .single_esdt( + &payment.token_identifier, + payment.token_nonce, + &BigUint::from(1u8), + ) .transfer(); } diff --git a/contracts/examples/order-book/pair/src/validation.rs b/contracts/examples/order-book/pair/src/validation.rs index 488edbdb24..306f299014 100644 --- a/contracts/examples/order-book/pair/src/validation.rs +++ b/contracts/examples/order-book/pair/src/validation.rs @@ -72,22 +72,28 @@ pub trait ValidationModule: common::CommonModule { let (token_id, amount) = self.call_value().single_fungible_esdt(); let second_token_id = self.second_token_id().get(); require!( - token_id == second_token_id, + *token_id == second_token_id, "Token in and second token id should be the same" ); - Payment { token_id, amount } + Payment { + token_id: token_id.clone(), + amount: amount.clone(), + } } fn require_valid_sell_payment(&self) -> Payment { let (token_id, amount) = self.call_value().single_fungible_esdt(); let first_token_id = self.first_token_id().get(); require!( - token_id == first_token_id, + *token_id == first_token_id, "Token in and first token id should be the same" ); - Payment { token_id, amount } + Payment { + token_id: token_id.clone(), + amount: amount.clone(), + } } fn require_valid_match_input_order_ids(&self, order_ids: &ManagedVec) { diff --git a/contracts/examples/seed-nft-minter/src/seed_nft_minter.rs b/contracts/examples/seed-nft-minter/src/seed_nft_minter.rs index 510212891b..98734c0fb0 100644 --- a/contracts/examples/seed-nft-minter/src/seed_nft_minter.rs +++ b/contracts/examples/seed-nft-minter/src/seed_nft_minter.rs @@ -102,7 +102,7 @@ pub trait SeedNftMinter: } else { esdt_payments .try_get(0) - .map(|esdt_payment| esdt_payment.amount) + .map(|esdt_payment| esdt_payment.amount.clone()) .unwrap_or_default() }; total_amount += amount; diff --git a/contracts/feature-tests/basic-features/src/storage_mapper_fungible_token.rs b/contracts/feature-tests/basic-features/src/storage_mapper_fungible_token.rs index 6eccdd3f83..feeecb2b33 100644 --- a/contracts/feature-tests/basic-features/src/storage_mapper_fungible_token.rs +++ b/contracts/feature-tests/basic-features/src/storage_mapper_fungible_token.rs @@ -61,8 +61,9 @@ pub trait FungibleTokenMapperFeatures: fn custom_issue_non_zero_supply_cb(&self, #[call_result] result: ManagedAsyncCallResult<()>) { match result { ManagedAsyncCallResult::Ok(()) => { - let token_identifier = self.call_value().single_esdt().token_identifier; - self.fungible_token_mapper().set_token_id(token_identifier); + let token_identifier = &self.call_value().single_esdt().token_identifier; + self.fungible_token_mapper() + .set_token_id(token_identifier.clone()); }, ManagedAsyncCallResult::Err(_) => { self.fungible_token_mapper().clear(); @@ -128,9 +129,9 @@ pub trait FungibleTokenMapperFeatures: #[payable("*")] #[endpoint] fn require_same_token_fungible(&self) { - let payment_token = self.call_value().single_esdt().token_identifier; + let payment_token = &self.call_value().single_esdt().token_identifier; self.fungible_token_mapper() - .require_same_token(&payment_token); + .require_same_token(payment_token); } #[payable("*")] diff --git a/contracts/feature-tests/composability/esdt-contract-pair/first-contract/src/lib.rs b/contracts/feature-tests/composability/esdt-contract-pair/first-contract/src/lib.rs index bf77e7ab4e..f4d09b555e 100644 --- a/contracts/feature-tests/composability/esdt-contract-pair/first-contract/src/lib.rs +++ b/contracts/feature-tests/composability/esdt-contract-pair/first-contract/src/lib.rs @@ -25,7 +25,7 @@ pub trait FirstContract { let expected_token_identifier = self.get_contract_esdt_token_identifier(); require!( - actual_token_identifier == expected_token_identifier, + *actual_token_identifier == expected_token_identifier, "Wrong esdt token" ); @@ -45,13 +45,13 @@ pub trait FirstContract { let expected_token_identifier = self.get_contract_esdt_token_identifier(); require!( - actual_token_identifier == expected_token_identifier, + *actual_token_identifier == expected_token_identifier, "Wrong esdt token" ); self.call_esdt_second_contract( &expected_token_identifier, - &(esdt_value / 2u32), + &(esdt_value.clone() / 2u32), &self.get_second_contract_address(), &ManagedBuffer::from(SECOND_CONTRACT_ACCEPT_ESDT_PAYMENT), &ManagedVec::new(), @@ -65,7 +65,7 @@ pub trait FirstContract { let expected_token_identifier = self.get_contract_esdt_token_identifier(); require!( - actual_token_identifier == expected_token_identifier, + *actual_token_identifier == expected_token_identifier, "Wrong esdt token" ); @@ -86,7 +86,7 @@ pub trait FirstContract { let expected_token_identifier = self.get_contract_esdt_token_identifier(); require!( - actual_token_identifier == expected_token_identifier, + *actual_token_identifier == expected_token_identifier, "Wrong esdt token" ); @@ -107,7 +107,7 @@ pub trait FirstContract { let expected_token_identifier = self.get_contract_esdt_token_identifier(); require!( - actual_token_identifier == expected_token_identifier, + *actual_token_identifier == expected_token_identifier, "Wrong esdt token" ); diff --git a/contracts/feature-tests/composability/execute-on-dest-esdt-issue-callback/child/src/lib.rs b/contracts/feature-tests/composability/execute-on-dest-esdt-issue-callback/child/src/lib.rs index e763a222af..dcb634f054 100644 --- a/contracts/feature-tests/composability/execute-on-dest-esdt-issue-callback/child/src/lib.rs +++ b/contracts/feature-tests/composability/execute-on-dest-esdt-issue-callback/child/src/lib.rs @@ -46,7 +46,7 @@ pub trait Child { #[callback] fn esdt_issue_callback(&self, #[call_result] _result: IgnoreValue) { let (token_identifier, _amount) = self.call_value().single_fungible_esdt(); - self.wrapped_egld_token_identifier().set(&token_identifier); + self.wrapped_egld_token_identifier().set(token_identifier); } // storage diff --git a/contracts/feature-tests/composability/forwarder-legacy/src/fwd_call_transf_exec_legacy.rs b/contracts/feature-tests/composability/forwarder-legacy/src/fwd_call_transf_exec_legacy.rs index e789e4644f..645f173b28 100644 --- a/contracts/feature-tests/composability/forwarder-legacy/src/fwd_call_transf_exec_legacy.rs +++ b/contracts/feature-tests/composability/forwarder-legacy/src/fwd_call_transf_exec_legacy.rs @@ -28,7 +28,7 @@ pub trait ForwarderTransferExecuteModule { self.vault_proxy() .contract(to) .accept_funds() - .payment((payment.token_identifier, 0, payment.amount)) + .single_esdt(&payment.token_identifier, 0, &payment.amount) .transfer_execute(); } diff --git a/contracts/feature-tests/composability/forwarder-legacy/src/fwd_esdt_legacy.rs b/contracts/feature-tests/composability/forwarder-legacy/src/fwd_esdt_legacy.rs index 6fa20793d8..7ba477b448 100644 --- a/contracts/feature-tests/composability/forwarder-legacy/src/fwd_esdt_legacy.rs +++ b/contracts/feature-tests/composability/forwarder-legacy/src/fwd_esdt_legacy.rs @@ -39,8 +39,8 @@ pub trait ForwarderEsdtModule: fwd_storage_legacy::ForwarderStorageModule { #[endpoint] fn send_esdt_with_fees(&self, to: ManagedAddress, percentage_fees: BigUint) { let (token_id, payment) = self.call_value().single_fungible_esdt(); - let fees = &payment * &percentage_fees / PERCENTAGE_TOTAL; - let amount_to_send = payment - fees; + let fees = &*payment * &percentage_fees / PERCENTAGE_TOTAL; + let amount_to_send = payment.clone() - fees; self.send().direct_esdt(&to, &token_id, 0, &amount_to_send); } diff --git a/contracts/feature-tests/composability/forwarder-raw/src/forwarder_raw.rs b/contracts/feature-tests/composability/forwarder-raw/src/forwarder_raw.rs index a436e97b7a..796bea5684 100644 --- a/contracts/feature-tests/composability/forwarder-raw/src/forwarder_raw.rs +++ b/contracts/feature-tests/composability/forwarder-raw/src/forwarder_raw.rs @@ -37,9 +37,9 @@ pub trait ForwarderRaw: } else { for payment in payments.iter() { let _ = self.callback_payments().push(&( - EgldOrEsdtTokenIdentifier::esdt(payment.token_identifier), + EgldOrEsdtTokenIdentifier::esdt(payment.token_identifier.clone()), payment.token_nonce, - payment.amount, + payment.amount.clone(), )); } } diff --git a/contracts/feature-tests/composability/forwarder-raw/src/forwarder_raw_async.rs b/contracts/feature-tests/composability/forwarder-raw/src/forwarder_raw_async.rs index 47240f3c52..df66f30a30 100644 --- a/contracts/feature-tests/composability/forwarder-raw/src/forwarder_raw_async.rs +++ b/contracts/feature-tests/composability/forwarder-raw/src/forwarder_raw_async.rs @@ -114,8 +114,8 @@ pub trait ForwarderRawAsync: super::forwarder_raw_common::ForwarderRawCommon { let (token, payment) = self.call_value().single_fungible_esdt(); self.forward_contract_call( to, - EgldOrEsdtTokenIdentifier::esdt(token), - payment, + EgldOrEsdtTokenIdentifier::esdt(token.clone()), + payment.clone(), endpoint_name, args, ) diff --git a/contracts/feature-tests/composability/forwarder/src/fwd_esdt.rs b/contracts/feature-tests/composability/forwarder/src/fwd_esdt.rs index a4dbaa28b1..425ca07053 100644 --- a/contracts/feature-tests/composability/forwarder/src/fwd_esdt.rs +++ b/contracts/feature-tests/composability/forwarder/src/fwd_esdt.rs @@ -42,8 +42,8 @@ pub trait ForwarderEsdtModule: fwd_storage::ForwarderStorageModule { #[endpoint] fn send_esdt_with_fees(&self, to: ManagedAddress, percentage_fees: BigUint) { let (token_id, payment) = self.call_value().single_fungible_esdt(); - let fees = &payment * &percentage_fees / PERCENTAGE_TOTAL; - let amount_to_send = payment - fees; + let fees = percentage_fees * &*payment / PERCENTAGE_TOTAL; + let amount_to_send = payment.clone() - fees; self.tx() .to(&to) diff --git a/contracts/feature-tests/composability/promises-features/src/fwd_call_promises_bt.rs b/contracts/feature-tests/composability/promises-features/src/fwd_call_promises_bt.rs index 2634e2c5ac..ba73843958 100644 --- a/contracts/feature-tests/composability/promises-features/src/fwd_call_promises_bt.rs +++ b/contracts/feature-tests/composability/promises-features/src/fwd_call_promises_bt.rs @@ -45,15 +45,19 @@ pub trait CallPromisesBackTransfersModule: common::CommonModule { } for esdt_transfer in &back_transfers.esdt_payments { - let (token, nonce, payment) = esdt_transfer.into_tuple(); - let esdt_token_id = EgldOrEsdtTokenIdentifier::esdt(token); - self.retrieve_funds_callback_event(&esdt_token_id, nonce, &payment); + let esdt_token_id = + EgldOrEsdtTokenIdentifier::esdt(esdt_transfer.token_identifier.clone()); + self.retrieve_funds_callback_event( + &esdt_token_id, + esdt_transfer.token_nonce, + &esdt_transfer.amount, + ); let _ = self.callback_data().push(&CallbackData { callback_name: ManagedBuffer::from(b"retrieve_funds_callback"), token_identifier: esdt_token_id, - token_nonce: nonce, - token_amount: payment, + token_nonce: esdt_transfer.token_nonce, + token_amount: esdt_transfer.amount.clone(), args: ManagedVec::new(), }); } diff --git a/contracts/feature-tests/composability/vault/src/vault.rs b/contracts/feature-tests/composability/vault/src/vault.rs index 37b0d65b9e..d6f3feb81b 100644 --- a/contracts/feature-tests/composability/vault/src/vault.rs +++ b/contracts/feature-tests/composability/vault/src/vault.rs @@ -240,9 +240,9 @@ pub trait Vault { ); new_tokens.push(EsdtTokenPayment::new( - payment.token_identifier, + payment.token_identifier.clone(), new_token_nonce, - payment.amount, + payment.amount.clone(), )); } diff --git a/contracts/feature-tests/payable-features/src/payable_features.rs b/contracts/feature-tests/payable-features/src/payable_features.rs index d8296f8592..6cad32b135 100644 --- a/contracts/feature-tests/payable-features/src/payable_features.rs +++ b/contracts/feature-tests/payable-features/src/payable_features.rs @@ -35,7 +35,7 @@ pub trait PayableFeatures { #[payable("*")] fn payment_array_3(&self) -> MultiValue3 { let [payment_a, payment_b, payment_c] = self.call_value().multi_esdt(); - (payment_a, payment_b, payment_c).into() + (payment_a.clone(), payment_b.clone(), payment_c.clone()).into() } #[endpoint] @@ -129,7 +129,7 @@ pub trait PayableFeatures { &self, #[payment] payment: BigUint, ) -> MultiValue2 { - let token = self.call_value().single_esdt().token_identifier; + let token = self.call_value().single_esdt().token_identifier.clone(); (payment, token).into() } @@ -140,14 +140,14 @@ pub trait PayableFeatures { #[payment_token] token: EgldOrEsdtTokenIdentifier, ) -> MultiValue2 { let payment = self.call_value().single_esdt(); - (payment.amount, token).into() + (payment.amount.clone(), token).into() } #[endpoint] #[payable("PAYABLE-FEATURES-TOKEN")] fn payable_token_4(&self) -> MultiValue2 { - let payment = self.call_value().single_esdt().amount; - let token = self.call_value().single_esdt().token_identifier; + let payment = self.call_value().single_esdt().amount.clone(); + let token = self.call_value().single_esdt().token_identifier.clone(); (payment, token).into() } } diff --git a/contracts/feature-tests/rust-testing-framework-tester/src/lib.rs b/contracts/feature-tests/rust-testing-framework-tester/src/lib.rs index ffc411dad0..9d7be05858 100644 --- a/contracts/feature-tests/rust-testing-framework-tester/src/lib.rs +++ b/contracts/feature-tests/rust-testing-framework-tester/src/lib.rs @@ -71,7 +71,7 @@ pub trait RustTestingFrameworkTester: dummy_module::DummyModule { #[endpoint] fn receive_esdt(&self) -> (TokenIdentifier, BigUint) { let payment = self.call_value().single_esdt(); - (payment.token_identifier, payment.amount) + (payment.token_identifier.clone(), payment.amount.clone()) } #[payable("*")] @@ -84,7 +84,7 @@ pub trait RustTestingFrameworkTester: dummy_module::DummyModule { #[endpoint] fn receive_esdt_half(&self) { let payment = self.call_value().single_esdt(); - let amount = payment.amount / 2u32; + let amount = &payment.amount / 2u32; self.tx() .to(ToCaller) diff --git a/contracts/feature-tests/use-module/src/token_merge_mod_impl.rs b/contracts/feature-tests/use-module/src/token_merge_mod_impl.rs index b5d7711481..04f7b9cc7d 100644 --- a/contracts/feature-tests/use-module/src/token_merge_mod_impl.rs +++ b/contracts/feature-tests/use-module/src/token_merge_mod_impl.rs @@ -55,7 +55,7 @@ pub trait TokenMergeModImpl: ) -> ManagedVec { let payment = self.call_value().single_esdt(); let attributes_creator = DefaultMergedAttributesWrapper::new(); - self.split_token_partial(payment, tokens_to_remove, &attributes_creator) + self.split_token_partial(payment.clone(), tokens_to_remove, &attributes_creator) } } diff --git a/contracts/modules/src/bonding_curve/utils/owner_endpoints.rs b/contracts/modules/src/bonding_curve/utils/owner_endpoints.rs index 382e1fbc28..1c51d47905 100644 --- a/contracts/modules/src/bonding_curve/utils/owner_endpoints.rs +++ b/contracts/modules/src/bonding_curve/utils/owner_endpoints.rs @@ -90,11 +90,14 @@ pub trait OwnerEndpointsModule: storage::StorageModule + events::EventsModule { + PartialEq + Default, { - let (identifier, nonce, amount) = self.call_value().single_esdt().into_tuple(); + let esdt_payment = self.call_value().single_esdt(); + let identifier = &esdt_payment.token_identifier; + let nonce = esdt_payment.token_nonce; + let amount = &esdt_payment.amount; let caller = self.blockchain().get_caller(); let mut set_payment = EgldOrEsdtTokenIdentifier::egld(); - if self.bonding_curve(&identifier).is_empty() { + if self.bonding_curve(identifier).is_empty() { match payment_token { OptionalValue::Some(token) => set_payment = EgldOrEsdtTokenIdentifier::esdt(token), OptionalValue::None => { @@ -102,27 +105,27 @@ pub trait OwnerEndpointsModule: storage::StorageModule + events::EventsModule { }, }; } - if self.token_details(&identifier).is_empty() { + if self.token_details(identifier).is_empty() { let nonces = ManagedVec::from_single_item(nonce); - self.token_details(&identifier).set(&TokenOwnershipData { + self.token_details(identifier).set(&TokenOwnershipData { token_nonces: nonces, owner: caller.clone(), }); } else { - let mut details = self.token_details(&identifier).get(); + let mut details = self.token_details(identifier).get(); require!( details.owner == caller, "The token was already deposited by another address" ); if !details.token_nonces.contains(&nonce) { details.token_nonces.push(nonce); - self.token_details(&identifier).set(&details); + self.token_details(identifier).set(&details); } } - self.set_curve_storage::(&identifier, amount.clone(), set_payment); + self.set_curve_storage::(identifier, amount.clone(), set_payment); self.owned_tokens(&caller).insert(identifier.clone()); - self.nonce_amount(&identifier, nonce) + self.nonce_amount(identifier, nonce) .update(|current_amount| *current_amount += amount); } diff --git a/contracts/modules/src/bonding_curve/utils/user_endpoints.rs b/contracts/modules/src/bonding_curve/utils/user_endpoints.rs index f627d26256..a51236512a 100644 --- a/contracts/modules/src/bonding_curve/utils/user_endpoints.rs +++ b/contracts/modules/src/bonding_curve/utils/user_endpoints.rs @@ -21,11 +21,14 @@ pub trait UserEndpointsModule: storage::StorageModule + events::EventsModule { + PartialEq + Default, { - let (offered_token, nonce, sell_amount) = self.call_value().single_esdt().into_tuple(); - let _ = self.check_owned_return_payment_token::(&offered_token, &sell_amount); + let esdt_payment = self.call_value().single_esdt(); + let offered_token = &esdt_payment.token_identifier; + let nonce = esdt_payment.token_nonce; + let sell_amount = &esdt_payment.amount; + let _ = self.check_owned_return_payment_token::(offered_token, sell_amount); let (calculated_price, payment_token) = - self.bonding_curve(&offered_token).update(|buffer| { + self.bonding_curve(offered_token).update(|buffer| { let serializer = ManagedSerializer::new(); let mut bonding_curve: BondingCurve = @@ -35,9 +38,9 @@ pub trait UserEndpointsModule: storage::StorageModule + events::EventsModule { bonding_curve.sell_availability, "Selling is not available on this token" ); - let price = self.compute_sell_price::(&offered_token, &sell_amount); + let price = self.compute_sell_price::(offered_token, sell_amount); bonding_curve.payment.amount -= &price; - bonding_curve.arguments.balance += &sell_amount; + bonding_curve.arguments.balance += sell_amount; let payment_token = bonding_curve.payment_token(); *buffer = serializer.top_encode_to_managed_buffer(&bonding_curve); (price, payment_token) @@ -45,7 +48,7 @@ pub trait UserEndpointsModule: storage::StorageModule + events::EventsModule { let caller = self.blockchain().get_caller(); - self.nonce_amount(&offered_token, nonce) + self.nonce_amount(offered_token, nonce) .update(|val| *val += sell_amount); self.tx() @@ -53,7 +56,7 @@ pub trait UserEndpointsModule: storage::StorageModule + events::EventsModule { .egld_or_single_esdt(&payment_token, 0u64, &calculated_price) .transfer(); - self.token_details(&offered_token) + self.token_details(offered_token) .update(|details| details.add_nonce(nonce)); self.sell_token_event(&caller, &calculated_price); diff --git a/contracts/modules/src/default_issue_callbacks.rs b/contracts/modules/src/default_issue_callbacks.rs index 6edd7abeec..8e9e1c6e90 100644 --- a/contracts/modules/src/default_issue_callbacks.rs +++ b/contracts/modules/src/default_issue_callbacks.rs @@ -35,7 +35,7 @@ pub trait DefaultIssueCallbacksModule { let key = StorageKey::from(storage_key); match result { ManagedAsyncCallResult::Ok(()) => { - let token_id = self.call_value().single_esdt().token_identifier; + let token_id = self.call_value().single_esdt().token_identifier.clone(); storage_set(key.as_ref(), &TokenMapperState::Token(token_id)); }, ManagedAsyncCallResult::Err(_) => { diff --git a/contracts/modules/src/governance/mod.rs b/contracts/modules/src/governance/mod.rs index 85dfa4e1b4..4d407cf593 100644 --- a/contracts/modules/src/governance/mod.rs +++ b/contracts/modules/src/governance/mod.rs @@ -435,7 +435,7 @@ pub trait GovernanceModule: payment.token_identifier == self.governance_token_id().get(), "Only Governance token accepted as payment" ); - payment + payment.clone() } fn require_valid_proposal_id(&self, proposal_id: usize) { diff --git a/contracts/modules/src/token_merge/mod.rs b/contracts/modules/src/token_merge/mod.rs index b940980dfe..dad6665c0d 100644 --- a/contracts/modules/src/token_merge/mod.rs +++ b/contracts/modules/src/token_merge/mod.rs @@ -69,7 +69,7 @@ pub trait TokenMergeModule: } for single_token_instance in single_tokens { - all_merged_instances.add_or_update_instance(single_token_instance); + all_merged_instances.add_or_update_instance(single_token_instance.clone()); } let merged_token_payment = diff --git a/framework/base/src/contract_base/wrappers/call_value_wrapper.rs b/framework/base/src/contract_base/wrappers/call_value_wrapper.rs index 3006805ef0..3fb85e1658 100644 --- a/framework/base/src/contract_base/wrappers/call_value_wrapper.rs +++ b/framework/base/src/contract_base/wrappers/call_value_wrapper.rs @@ -8,8 +8,8 @@ use crate::{ err_msg, types::{ BigUint, ConstDecimals, EgldOrEsdtTokenIdentifier, EgldOrEsdtTokenPayment, - EgldOrMultiEsdtPayment, EsdtTokenPayment, ManagedDecimal, ManagedRef, ManagedVec, - TokenIdentifier, + EgldOrMultiEsdtPayment, EsdtTokenPayment, ManagedDecimal, ManagedRef, ManagedType, + ManagedVec, ManagedVecRef, TokenIdentifier, }, }; @@ -71,12 +71,12 @@ where /// Can be used to extract all payments in one line like this: /// /// `let [payment_a, payment_b, payment_c] = self.call_value().multi_esdt();`. - pub fn multi_esdt(&self) -> [EsdtTokenPayment; N] { - self.all_esdt_transfers() - .to_array_of_refs::() - .unwrap_or_else(|| { - A::error_api_impl().signal_error(err_msg::INCORRECT_NUM_ESDT_TRANSFERS.as_bytes()) - }) + pub fn multi_esdt(&self) -> [ManagedVecRef<'static, EsdtTokenPayment>; N] { + let esdt_transfers = self.all_esdt_transfers(); + let array = esdt_transfers.to_array_of_refs::().unwrap_or_else(|| { + A::error_api_impl().signal_error(err_msg::INCORRECT_NUM_ESDT_TRANSFERS.as_bytes()) + }); + unsafe { core::mem::transmute(array) } } /// Expects precisely one ESDT token transfer, fungible or not. @@ -84,9 +84,13 @@ where /// Will return the received ESDT payment. /// /// The amount cannot be 0, since that would not qualify as an ESDT transfer. - pub fn single_esdt(&self) -> EsdtTokenPayment { - let [payments] = self.multi_esdt(); - payments + pub fn single_esdt(&self) -> ManagedVecRef<'static, EsdtTokenPayment> { + let esdt_transfers = self.all_esdt_transfers(); + if esdt_transfers.len() != 1 { + A::error_api_impl().signal_error(err_msg::INCORRECT_NUM_ESDT_TRANSFERS.as_bytes()) + } + let value = esdt_transfers.get(0); + unsafe { core::mem::transmute(value) } } /// Expects precisely one fungible ESDT token transfer. @@ -94,12 +98,23 @@ where /// Returns the token ID and the amount for fungible ESDT transfers. /// /// The amount cannot be 0, since that would not qualify as an ESDT transfer. - pub fn single_fungible_esdt(&self) -> (TokenIdentifier, BigUint) { + pub fn single_fungible_esdt( + &self, + ) -> ( + ManagedRef<'static, A, TokenIdentifier>, + ManagedRef<'static, A, BigUint>, + ) { let payment = self.single_esdt(); if payment.token_nonce != 0 { A::error_api_impl().signal_error(err_msg::FUNGIBLE_TOKEN_EXPECTED_ERR_MSG.as_bytes()); } - (payment.token_identifier, payment.amount) + + unsafe { + ( + ManagedRef::wrap_handle(payment.token_identifier.get_handle()), + ManagedRef::wrap_handle(payment.amount.get_handle()), + ) + } } /// Accepts and returns either an EGLD payment, or a single ESDT token. @@ -115,7 +130,7 @@ where token_nonce: 0, amount: self.egld_value().clone_value(), }, - 1 => esdt_transfers.get(0).into(), + 1 => esdt_transfers.get(0).clone().into(), _ => A::error_api_impl().signal_error(err_msg::INCORRECT_NUM_ESDT_TRANSFERS.as_bytes()), } } diff --git a/framework/base/src/types/interaction/contract_call_legacy/contract_call_convert.rs b/framework/base/src/types/interaction/contract_call_legacy/contract_call_convert.rs index fe4225d9a4..6a66c3e0f5 100644 --- a/framework/base/src/types/interaction/contract_call_legacy/contract_call_convert.rs +++ b/framework/base/src/types/interaction/contract_call_legacy/contract_call_convert.rs @@ -20,7 +20,7 @@ where ) -> Self { match payments.len() { 0 => self, - 1 => self.convert_to_single_transfer_esdt_call(payments.get(0)), + 1 => self.convert_to_single_transfer_esdt_call(payments.get(0).clone()), _ => self.convert_to_multi_transfer_esdt_call(payments), } } diff --git a/framework/base/src/types/interaction/contract_call_legacy/contract_call_exec.rs b/framework/base/src/types/interaction/contract_call_legacy/contract_call_exec.rs index f0f1722f24..e2281a4b38 100644 --- a/framework/base/src/types/interaction/contract_call_legacy/contract_call_exec.rs +++ b/framework/base/src/types/interaction/contract_call_legacy/contract_call_exec.rs @@ -208,7 +208,7 @@ where pub(super) fn transfer_execute_esdt(self, payments: ManagedVec>) { match payments.len() { 0 => self.transfer_execute_egld(BigUint::zero()), - 1 => self.transfer_execute_single_esdt(payments.get(0)), + 1 => self.transfer_execute_single_esdt(payments.get(0).clone()), _ => self.transfer_execute_multi_esdt(payments), } } diff --git a/framework/base/src/types/interaction/result_handlers/returns_bt_single_esdt.rs b/framework/base/src/types/interaction/result_handlers/returns_bt_single_esdt.rs index 996891c1c2..10a4c59003 100644 --- a/framework/base/src/types/interaction/result_handlers/returns_bt_single_esdt.rs +++ b/framework/base/src/types/interaction/result_handlers/returns_bt_single_esdt.rs @@ -28,6 +28,7 @@ where Env::Api::error_api_impl().signal_error(b"Back transfers expected to be a single ESDT") } - esdt_payments.get(0) + let x = esdt_payments.get(0).clone(); + x } } diff --git a/framework/base/src/types/managed/wrapped/esdt_token_payment.rs b/framework/base/src/types/managed/wrapped/esdt_token_payment.rs index b676fa42cc..041ddd1f64 100644 --- a/framework/base/src/types/managed/wrapped/esdt_token_payment.rs +++ b/framework/base/src/types/managed/wrapped/esdt_token_payment.rs @@ -15,7 +15,7 @@ use crate::{ use super::{ managed_vec_item_read_from_payload_index, managed_vec_item_save_to_payload_index, ManagedVec, - ManagedVecItemPayloadBuffer, + ManagedVecItemPayloadBuffer, ManagedVecRef, }; #[type_abi] @@ -168,7 +168,7 @@ impl IntoMultiValue for EsdtTokenPayment { impl ManagedVecItem for EsdtTokenPayment { type PAYLOAD = ManagedVecItemPayloadBuffer<16>; const SKIPS_RESERIALIZATION: bool = false; - type Ref<'a> = Self; + type Ref<'a> = ManagedVecRef<'a, Self>; fn read_from_payload(payload: &Self::PAYLOAD) -> Self { let mut index = 0; @@ -182,8 +182,7 @@ impl ManagedVecItem for EsdtTokenPayment { } unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> { - // TODO: managed ref - Self::read_from_payload(payload) + ManagedVecRef::new(Self::read_from_payload(payload)) } fn save_to_payload(self, payload: &mut Self::PAYLOAD) { diff --git a/framework/scenario/src/scenario/tx_to_step/tx_to_step_call.rs b/framework/scenario/src/scenario/tx_to_step/tx_to_step_call.rs index 0661806509..752437a130 100644 --- a/framework/scenario/src/scenario/tx_to_step/tx_to_step_call.rs +++ b/framework/scenario/src/scenario/tx_to_step/tx_to_step_call.rs @@ -71,7 +71,7 @@ where step.tx.esdt_value = full_payment_data .multi_esdt .iter() - .map(TxESDT::from) + .map(|item| TxESDT::from(item.clone())) .collect(); } diff --git a/framework/scenario/src/scenario/tx_to_step/tx_to_step_transfer.rs b/framework/scenario/src/scenario/tx_to_step/tx_to_step_transfer.rs index d2baa6bc6c..782f86dc16 100644 --- a/framework/scenario/src/scenario/tx_to_step/tx_to_step_transfer.rs +++ b/framework/scenario/src/scenario/tx_to_step/tx_to_step_transfer.rs @@ -52,7 +52,7 @@ where step.tx.esdt_value = full_payment_data .multi_esdt .iter() - .map(TxESDT::from) + .map(|item| TxESDT::from(item.clone())) .collect(); } From 98ef758fc65d3989a1fc601633eba0eb65be36c9 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 4 Dec 2024 14:45:20 +0200 Subject: [PATCH 52/66] ManagedVec iter - file rename --- framework/base/src/types/managed/wrapped.rs | 8 ++++---- ...anaged_vec_owned_iter.rs => managed_vec_iter_owned.rs} | 0 .../{managed_vec_ref_iter.rs => managed_vec_iter_ref.rs} | 0 3 files changed, 4 insertions(+), 4 deletions(-) rename framework/base/src/types/managed/wrapped/{managed_vec_owned_iter.rs => managed_vec_iter_owned.rs} (100%) rename framework/base/src/types/managed/wrapped/{managed_vec_ref_iter.rs => managed_vec_iter_ref.rs} (100%) diff --git a/framework/base/src/types/managed/wrapped.rs b/framework/base/src/types/managed/wrapped.rs index 088a250d5d..3924b7b3da 100644 --- a/framework/base/src/types/managed/wrapped.rs +++ b/framework/base/src/types/managed/wrapped.rs @@ -19,9 +19,9 @@ mod managed_vec; mod managed_vec_item; mod managed_vec_item_nested_tuple; mod managed_vec_item_payload; -mod managed_vec_owned_iter; +mod managed_vec_iter_owned; +mod managed_vec_iter_ref; mod managed_vec_ref; -mod managed_vec_ref_iter; mod managed_vec_ref_mut; pub(crate) mod preloaded_managed_buffer; mod randomness_source; @@ -53,9 +53,9 @@ pub use managed_vec_item::{ }; pub use managed_vec_item_nested_tuple::ManagedVecItemNestedTuple; pub use managed_vec_item_payload::*; -pub use managed_vec_owned_iter::ManagedVecOwnedIterator; +pub use managed_vec_iter_owned::ManagedVecOwnedIterator; +pub use managed_vec_iter_ref::ManagedVecRefIterator; pub use managed_vec_ref::ManagedVecRef; -pub use managed_vec_ref_iter::ManagedVecRefIterator; pub use managed_vec_ref_mut::ManagedVecRefMut; pub use randomness_source::RandomnessSource; pub use token_identifier::TokenIdentifier; diff --git a/framework/base/src/types/managed/wrapped/managed_vec_owned_iter.rs b/framework/base/src/types/managed/wrapped/managed_vec_iter_owned.rs similarity index 100% rename from framework/base/src/types/managed/wrapped/managed_vec_owned_iter.rs rename to framework/base/src/types/managed/wrapped/managed_vec_iter_owned.rs diff --git a/framework/base/src/types/managed/wrapped/managed_vec_ref_iter.rs b/framework/base/src/types/managed/wrapped/managed_vec_iter_ref.rs similarity index 100% rename from framework/base/src/types/managed/wrapped/managed_vec_ref_iter.rs rename to framework/base/src/types/managed/wrapped/managed_vec_iter_ref.rs From 1db87bb2a3616b7c2a92fde6bde7f4ed7c35da9c Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 4 Dec 2024 15:09:14 +0200 Subject: [PATCH 53/66] Cargo.lock update --- Cargo.lock | 178 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 110 insertions(+), 68 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ee094a3520..6eb7455172 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -160,9 +160,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.93" +version = "1.0.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c95c10ba0b00a02636238b814946408b1322d5ac4760326e6fb8ec956d85775" +checksum = "c1fd03a028ef38ba2276dce7e33fcd6369c158a1bca17946c4b1b701891c1ff7" [[package]] name = "anymap2" @@ -262,6 +262,7 @@ dependencies = [ "clap", "multiversx-sc-snippets", "serde", + "serial_test", "tokio", "toml", ] @@ -413,15 +414,15 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "bytes" -version = "1.8.0" +version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ac0150caa2ae65ca5bd83f25c7de183dea78d4d366469f148435e2acfbad0da" +checksum = "325918d6fe32f23b19878fe4b34794ae41fc19ddbe53b10571a4874d44ffd39b" [[package]] name = "cc" -version = "1.2.1" +version = "1.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd9de9f2205d5ef3fd67e685b0df337994ddd4495e2a28d185500d0e1edfea47" +checksum = "f34d93e62b03caf570cccc334cbc6c2fceca82f39211051345108adcba3eebdc" dependencies = [ "shlex", ] @@ -485,9 +486,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.21" +version = "4.5.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb3b4b9e5a7c7514dfa52869339ee98b3156b0bfb4e8a77c4ff4babb64b1604f" +checksum = "69371e34337c4c984bbe322360c2547210bf632eb2814bbe78a6e87a2935bd2b" dependencies = [ "clap_builder", "clap_derive", @@ -495,9 +496,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.21" +version = "4.5.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b17a95aa67cc7b5ebd32aa5370189aa0d79069ef1c64ce893bd30fb24bff20ec" +checksum = "6e24c1b4099818523236a8ca881d2b45db98dadfb4625cf6608c12069fcbbde1" dependencies = [ "anstream", "anstyle", @@ -1014,12 +1015,12 @@ dependencies = [ [[package]] name = "errno" -version = "0.3.9" +version = "0.3.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" +checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -1515,7 +1516,7 @@ dependencies = [ "futures-core", "futures-sink", "gloo-utils 0.2.0", - "http 1.1.0", + "http 1.2.0", "js-sys", "pin-project", "serde", @@ -1617,8 +1618,8 @@ dependencies = [ "fnv", "futures-core", "futures-sink", - "http 1.1.0", - "indexmap 2.6.0", + "http 1.2.0", + "indexmap 2.7.0", "slab", "tokio", "tokio-util", @@ -1708,9 +1709,9 @@ dependencies = [ [[package]] name = "http" -version = "1.1.0" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21b9ddb458710bc376481b842f5da65cdf31522de232c1ca8146abce2a358258" +checksum = "f16ca2af56261c99fba8bac40a10251ce8188205a4c448fbb745a2e4daa76fea" dependencies = [ "bytes", "fnv", @@ -1724,7 +1725,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184" dependencies = [ "bytes", - "http 1.1.0", + "http 1.2.0", ] [[package]] @@ -1735,7 +1736,7 @@ checksum = "793429d76616a256bcb62c2a2ec2bed781c8307e797e2598c50010f2bee2544f" dependencies = [ "bytes", "futures-util", - "http 1.1.0", + "http 1.2.0", "http-body", "pin-project-lite", ] @@ -1762,7 +1763,7 @@ dependencies = [ "futures-channel", "futures-util", "h2", - "http 1.1.0", + "http 1.2.0", "http-body", "httparse", "itoa", @@ -1779,7 +1780,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08afdbb5c31130e3034af566421053ab03787c640246a446327f550d11bcb333" dependencies = [ "futures-util", - "http 1.1.0", + "http 1.2.0", "hyper", "hyper-util", "rustls", @@ -1815,7 +1816,7 @@ dependencies = [ "bytes", "futures-channel", "futures-util", - "http 1.1.0", + "http 1.2.0", "http-body", "hyper", "pin-project-lite", @@ -2001,9 +2002,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.6.0" +version = "2.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" +checksum = "62f822373a4fe84d4bb149bf54e584a7f4abec90e072ed49cda0edea5b95471f" dependencies = [ "equivalent", "hashbrown 0.15.2", @@ -2060,10 +2061,11 @@ checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" [[package]] name = "js-sys" -version = "0.3.72" +version = "0.3.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a88f1bda2bd75b0452a14784937d796722fdebfe50df998aeb3f0b7603019a9" +checksum = "a865e038f7f6ed956f788f0d7d60c541fff74c7bd74272c5d4cf15c63743e705" dependencies = [ + "once_cell", "wasm-bindgen", ] @@ -2169,9 +2171,9 @@ checksum = "884e2677b40cc8c339eaefcb701c32ef1fd2493d71118dc0ca4b6a736c93bd67" [[package]] name = "libc" -version = "0.2.166" +version = "0.2.167" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2ccc108bbc0b1331bd061864e7cd823c0cab660bbe6970e66e2c0614decde36" +checksum = "09d6582e104315a817dff97f75133544b2e094ee22447d2acf4a74e189ba06fc" [[package]] name = "linked-list-repeat" @@ -2330,11 +2332,10 @@ dependencies = [ [[package]] name = "mio" -version = "1.0.2" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec" +checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd" dependencies = [ - "hermit-abi", "libc", "wasi", "windows-sys 0.52.0", @@ -3445,7 +3446,7 @@ dependencies = [ "futures-core", "futures-util", "h2", - "http 1.1.0", + "http 1.2.0", "http-body", "http-body-util", "hyper", @@ -3580,9 +3581,9 @@ checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" [[package]] name = "rustc-hash" -version = "2.0.0" +version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "583034fd73374156e66797ed8e5b0d5690409c9226b22d87cb7f19821c05d152" +checksum = "c7fb8039b3032c191086b10f11f319a6e99e1e82889c5cc6046f515c9db1d497" [[package]] name = "rustc_version" @@ -3676,6 +3677,15 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "scc" +version = "2.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66b202022bb57c049555430e11fc22fea12909276a80a4c3d368da36ac1d88ed" +dependencies = [ + "sdd", +] + [[package]] name = "scenario-tester" version = "0.0.0" @@ -3719,6 +3729,12 @@ dependencies = [ "sha2", ] +[[package]] +name = "sdd" +version = "3.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49c1eeaf4b6a87c7479688c6d52b9f1153cedd3c489300564f932b065c6eab95" + [[package]] name = "second-contract" version = "0.0.0" @@ -3835,7 +3851,7 @@ version = "1.0.133" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c7fceb2473b9166b2294ef05efcb65a3db80803f0b03ef86a5fc88a2b85ee377" dependencies = [ - "indexmap 2.6.0", + "indexmap 2.7.0", "itoa", "memchr", "ryu", @@ -3874,6 +3890,31 @@ dependencies = [ "serde", ] +[[package]] +name = "serial_test" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b258109f244e1d6891bf1053a55d63a5cd4f8f4c30cf9a1280989f80e7a1fa9" +dependencies = [ + "futures", + "log", + "once_cell", + "parking_lot", + "scc", + "serial_test_derive", +] + +[[package]] +name = "serial_test_derive" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d69265a08751de7844521fd15003ae0a888e035773ba05695c5c759a6f89eef" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.77", +] + [[package]] name = "set-repeat" version = "0.0.0" @@ -4193,9 +4234,9 @@ dependencies = [ [[package]] name = "tokio" -version = "1.41.1" +version = "1.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22cfb5bee7a6a52939ca9224d6ac897bb669134078daa8735560897f69de4d33" +checksum = "5cec9b21b0450273377fc97bd4c33a8acffc8c996c987a7c5b319a0083707551" dependencies = [ "backtrace", "bytes", @@ -4254,9 +4295,9 @@ dependencies = [ [[package]] name = "tokio-util" -version = "0.7.12" +version = "0.7.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61e7c3654c13bcd040d4a03abee2c75b1d14a37b423cf5a813ceae1cc903ec6a" +checksum = "d7fcaa8d55a2bdd6b83ace262b016eca0d79ee02818c5c1bcdf0305114081078" dependencies = [ "bytes", "futures-core", @@ -4271,7 +4312,7 @@ version = "0.8.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1ed1f98e3fdc28d6d910e6737ae6ab1a93bf1985935a1193e68f93eeb68d24e" dependencies = [ - "indexmap 2.6.0", + "indexmap 2.7.0", "serde", "serde_spanned", "toml_datetime", @@ -4293,7 +4334,7 @@ version = "0.22.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ae48d6208a266e853d946088ed816055e556cc6028c5e8e2b84d9fa5dd7c7f5" dependencies = [ - "indexmap 2.6.0", + "indexmap 2.7.0", "serde", "serde_spanned", "toml_datetime", @@ -4534,9 +4575,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.95" +version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "128d1e363af62632b8eb57219c8fd7877144af57558fb2ef0368d0087bddeb2e" +checksum = "d15e63b4482863c109d70a7b8706c1e364eb6ea449b201a76c5b89cedcec2d5c" dependencies = [ "cfg-if", "once_cell", @@ -4545,9 +4586,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.95" +version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb6dd4d3ca0ddffd1dd1c9c04f94b868c37ff5fac97c30b97cff2d74fce3a358" +checksum = "8d36ef12e3aaca16ddd3f67922bc63e48e953f126de60bd33ccc0101ef9998cd" dependencies = [ "bumpalo", "log", @@ -4560,21 +4601,22 @@ dependencies = [ [[package]] name = "wasm-bindgen-futures" -version = "0.4.45" +version = "0.4.47" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc7ec4f8827a71586374db3e87abdb5a2bb3a15afed140221307c3ec06b1f63b" +checksum = "9dfaf8f50e5f293737ee323940c7d8b08a66a95a419223d9f41610ca08b0833d" dependencies = [ "cfg-if", "js-sys", + "once_cell", "wasm-bindgen", "web-sys", ] [[package]] name = "wasm-bindgen-macro" -version = "0.2.95" +version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e79384be7f8f5a9dd5d7167216f022090cf1f9ec128e6e6a482a2cb5c5422c56" +checksum = "705440e08b42d3e4b36de7d66c944be628d579796b8090bfa3471478a2260051" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -4582,9 +4624,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.95" +version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26c6ab57572f7a24a4985830b120de1594465e5d500f24afe89e16b4e833ef68" +checksum = "98c9ae5a76e46f4deecd0f0255cc223cfa18dc9b261213b8aa0c7b36f61b3f1d" dependencies = [ "proc-macro2", "quote", @@ -4595,18 +4637,18 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.95" +version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65fc09f10666a9f147042251e0dda9c18f166ff7de300607007e96bdebc1068d" +checksum = "6ee99da9c5ba11bd675621338ef6fa52296b76b83305e9b6e5c77d4c286d6d49" [[package]] name = "wasm-encoder" -version = "0.221.0" +version = "0.221.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de35b6c3ef1f53ac7a31b5e69bc00f1542ea337e7e7162dc34c68b537ff82690" +checksum = "c17a3bd88f2155da63a1f2fcb8a56377a24f0b6dfed12733bb5f544e86f690c5" dependencies = [ "leb128", - "wasmparser 0.221.0", + "wasmparser 0.221.2", ] [[package]] @@ -4618,19 +4660,19 @@ dependencies = [ "ahash", "bitflags", "hashbrown 0.14.5", - "indexmap 2.6.0", + "indexmap 2.7.0", "semver", "serde", ] [[package]] name = "wasmparser" -version = "0.221.0" +version = "0.221.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8659e755615170cfe20da468865c989da78c5da16d8652e69a75acda02406a92" +checksum = "9845c470a2e10b61dd42c385839cdd6496363ed63b5c9e420b5488b77bd22083" dependencies = [ "bitflags", - "indexmap 2.6.0", + "indexmap 2.7.0", "semver", ] @@ -4647,9 +4689,9 @@ dependencies = [ [[package]] name = "wast" -version = "221.0.0" +version = "221.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d8eb1933d493dd07484a255c3f52236123333f5befaa3be36182a50d393ec54" +checksum = "fcc4470b9de917ba199157d1f0ae104f2ae362be728c43e68c571c7715bd629e" dependencies = [ "bumpalo", "leb128", @@ -4660,18 +4702,18 @@ dependencies = [ [[package]] name = "wat" -version = "1.221.0" +version = "1.221.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c813fd4e5b2b97242830b56e7b7dc5479bc17aaa8730109be35e61909af83993" +checksum = "6b1f3c6d82af47286494c6caea1d332037f5cbeeac82bbf5ef59cb8c201c466e" dependencies = [ "wast", ] [[package]] name = "web-sys" -version = "0.3.72" +version = "0.3.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6488b90108c040df0fe62fa815cbdee25124641df01814dd7282749234c6112" +checksum = "a98bc3c33f0fe7e59ad7cd041b89034fa82a7c2d4365ca538dda6cdaf513863c" dependencies = [ "js-sys", "wasm-bindgen", @@ -5077,7 +5119,7 @@ dependencies = [ "crossbeam-utils", "displaydoc", "flate2", - "indexmap 2.6.0", + "indexmap 2.7.0", "memchr", "thiserror", "zopfli", From 0df3c0a42f3f22e4f2975768adaca5c5699e8456 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 4 Dec 2024 15:21:54 +0200 Subject: [PATCH 54/66] ManagedVec iterator refactor, ManagedVecPayloadIterator --- framework/base/src/types/managed/wrapped.rs | 2 + .../managed/wrapped/managed_vec_iter_owned.rs | 62 ++--------- .../wrapped/managed_vec_iter_payload.rs | 103 ++++++++++++++++++ .../managed/wrapped/managed_vec_iter_ref.rs | 58 ++++------ 4 files changed, 134 insertions(+), 91 deletions(-) create mode 100644 framework/base/src/types/managed/wrapped/managed_vec_iter_payload.rs diff --git a/framework/base/src/types/managed/wrapped.rs b/framework/base/src/types/managed/wrapped.rs index 3924b7b3da..52d827c79f 100644 --- a/framework/base/src/types/managed/wrapped.rs +++ b/framework/base/src/types/managed/wrapped.rs @@ -20,6 +20,7 @@ mod managed_vec_item; mod managed_vec_item_nested_tuple; mod managed_vec_item_payload; mod managed_vec_iter_owned; +mod managed_vec_iter_payload; mod managed_vec_iter_ref; mod managed_vec_ref; mod managed_vec_ref_mut; @@ -54,6 +55,7 @@ pub use managed_vec_item::{ pub use managed_vec_item_nested_tuple::ManagedVecItemNestedTuple; pub use managed_vec_item_payload::*; pub use managed_vec_iter_owned::ManagedVecOwnedIterator; +pub use managed_vec_iter_payload::ManagedVecPayloadIterator; pub use managed_vec_iter_ref::ManagedVecRefIterator; pub use managed_vec_ref::ManagedVecRef; pub use managed_vec_ref_mut::ManagedVecRefMut; diff --git a/framework/base/src/types/managed/wrapped/managed_vec_iter_owned.rs b/framework/base/src/types/managed/wrapped/managed_vec_iter_owned.rs index b688bb466c..055b4b4a11 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_iter_owned.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_iter_owned.rs @@ -1,6 +1,6 @@ -use crate::api::ManagedTypeApi; +use crate::{api::ManagedTypeApi, types::ManagedType}; -use super::{ManagedVec, ManagedVecItem, ManagedVecItemPayload}; +use super::{ManagedVec, ManagedVecItem, ManagedVecPayloadIterator}; impl IntoIterator for ManagedVec where @@ -19,9 +19,7 @@ where M: ManagedTypeApi, T: ManagedVecItem, { - managed_vec: ManagedVec, - byte_start: usize, - byte_end: usize, + payload_iter: ManagedVecPayloadIterator, } impl ManagedVecOwnedIterator @@ -30,11 +28,10 @@ where T: ManagedVecItem, { pub(crate) fn new(managed_vec: ManagedVec) -> Self { - let byte_end = managed_vec.byte_len(); - ManagedVecOwnedIterator { - managed_vec, - byte_start: 0, - byte_end, + unsafe { + ManagedVecOwnedIterator { + payload_iter: ManagedVecPayloadIterator::new(managed_vec.forget_into_handle()), + } } } } @@ -47,26 +44,12 @@ where type Item = T; fn next(&mut self) -> Option { - // managedrev / reference type - let next_byte_start = self.byte_start + T::payload_size(); - if next_byte_start > self.byte_end { - return None; - } - - let mut payload = T::PAYLOAD::new_buffer(); - let _ = self - .managed_vec - .buffer - .load_slice(self.byte_start, payload.payload_slice_mut()); - - self.byte_start = next_byte_start; + let payload = self.payload_iter.next()?; Some(T::read_from_payload(&payload)) } fn size_hint(&self) -> (usize, Option) { - let size = T::payload_size(); - let remaining = (self.byte_end - self.byte_start) / size; - (remaining, Some(remaining)) + self.payload_iter.size_hint() } } @@ -83,32 +66,7 @@ where T: ManagedVecItem, { fn next_back(&mut self) -> Option { - if self.byte_start + T::payload_size() > self.byte_end { - return None; - } - self.byte_end -= T::payload_size(); - - let mut payload = T::PAYLOAD::new_buffer(); - let _ = self - .managed_vec - .buffer - .load_slice(self.byte_end, payload.payload_slice_mut()); - + let payload = self.payload_iter.next_back()?; Some(T::read_from_payload(&payload)) } } - -impl Clone for ManagedVecOwnedIterator -where - M: ManagedTypeApi, - T: ManagedVecItem + Clone, -{ - fn clone(&self) -> Self { - let byte_end = self.byte_end; - Self { - managed_vec: self.managed_vec.clone(), - byte_start: self.byte_start, - byte_end, - } - } -} diff --git a/framework/base/src/types/managed/wrapped/managed_vec_iter_payload.rs b/framework/base/src/types/managed/wrapped/managed_vec_iter_payload.rs new file mode 100644 index 0000000000..44293133d2 --- /dev/null +++ b/framework/base/src/types/managed/wrapped/managed_vec_iter_payload.rs @@ -0,0 +1,103 @@ +use core::marker::PhantomData; + +use crate::api::{ManagedBufferApiImpl, ManagedTypeApi}; + +use super::ManagedVecItemPayload; + +pub struct ManagedVecPayloadIterator +where + M: ManagedTypeApi, + P: ManagedVecItemPayload, +{ + pub(super) vec_handle: M::ManagedBufferHandle, + byte_start: usize, + byte_end: usize, + _phantom: PhantomData

, +} + +impl ManagedVecPayloadIterator +where + M: ManagedTypeApi, + P: ManagedVecItemPayload, +{ + /// Unsafe because it works with the managed vec handle directly, so does not take ownership into account. + pub(crate) unsafe fn new(vec_handle: M::ManagedBufferHandle) -> Self { + let byte_end = M::managed_type_impl().mb_len(vec_handle.clone()); + ManagedVecPayloadIterator { + vec_handle, + byte_start: 0, + byte_end, + _phantom: PhantomData, + } + } + + /// Unsafe because it works with the managed vec handle directly, so does not take ownership into account. + pub(super) unsafe fn clone_iter(&self) -> Self { + ManagedVecPayloadIterator { + vec_handle: self.vec_handle.clone(), + byte_start: self.byte_start, + byte_end: self.byte_end, + _phantom: PhantomData, + } + } +} + +impl Iterator for ManagedVecPayloadIterator +where + M: ManagedTypeApi, + P: ManagedVecItemPayload, +{ + type Item = P; + + fn next(&mut self) -> Option

{ + let next_byte_start = self.byte_start + P::payload_size(); + if next_byte_start > self.byte_end { + return None; + } + + let mut payload = P::new_buffer(); + let _ = M::managed_type_impl().mb_load_slice( + self.vec_handle.clone(), + self.byte_start, + payload.payload_slice_mut(), + ); + + self.byte_start = next_byte_start; + Some(payload) + } + + fn size_hint(&self) -> (usize, Option) { + let size = P::payload_size(); + let remaining = (self.byte_end - self.byte_start) / size; + (remaining, Some(remaining)) + } +} + +impl ExactSizeIterator for ManagedVecPayloadIterator +where + M: ManagedTypeApi, + P: ManagedVecItemPayload, +{ +} + +impl DoubleEndedIterator for ManagedVecPayloadIterator +where + M: ManagedTypeApi, + P: ManagedVecItemPayload, +{ + fn next_back(&mut self) -> Option { + if self.byte_start + P::payload_size() > self.byte_end { + return None; + } + self.byte_end -= P::payload_size(); + + let mut payload = P::new_buffer(); + let _ = M::managed_type_impl().mb_load_slice( + self.vec_handle.clone(), + self.byte_end, + payload.payload_slice_mut(), + ); + + Some(payload) + } +} diff --git a/framework/base/src/types/managed/wrapped/managed_vec_iter_ref.rs b/framework/base/src/types/managed/wrapped/managed_vec_iter_ref.rs index c0c0f89485..b57a16d3d7 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_iter_ref.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_iter_ref.rs @@ -1,6 +1,8 @@ -use crate::api::ManagedTypeApi; +use core::marker::PhantomData; -use super::{ManagedVec, ManagedVecItem, ManagedVecItemPayload}; +use crate::{api::ManagedTypeApi, types::ManagedType}; + +use super::{ManagedVec, ManagedVecItem, ManagedVecPayloadIterator}; impl<'a, M, T> IntoIterator for &'a ManagedVec where @@ -19,9 +21,8 @@ where M: ManagedTypeApi, T: ManagedVecItem, { - managed_vec: &'a ManagedVec, - byte_start: usize, - byte_end: usize, + payload_iter: ManagedVecPayloadIterator, + _phantom: PhantomData<&'a ManagedVec>, } impl<'a, M, T> ManagedVecRefIterator<'a, M, T> @@ -30,10 +31,11 @@ where T: ManagedVecItem, { pub(crate) fn new(managed_vec: &'a ManagedVec) -> Self { - ManagedVecRefIterator { - managed_vec, - byte_start: 0, - byte_end: managed_vec.byte_len(), + unsafe { + ManagedVecRefIterator { + payload_iter: ManagedVecPayloadIterator::new(managed_vec.get_handle()), + _phantom: PhantomData, + } } } } @@ -46,25 +48,12 @@ where type Item = T::Ref<'a>; fn next(&mut self) -> Option { - let next_byte_start = self.byte_start + T::payload_size(); - if next_byte_start > self.byte_end { - return None; - } - - let mut payload = T::PAYLOAD::new_buffer(); - let _ = self - .managed_vec - .buffer - .load_slice(self.byte_start, payload.payload_slice_mut()); - - self.byte_start = next_byte_start; - + let payload = self.payload_iter.next()?; unsafe { Some(T::borrow_from_payload(&payload)) } } fn size_hint(&self) -> (usize, Option) { - let remaining = (self.byte_end - self.byte_start) / T::payload_size(); - (remaining, Some(remaining)) + self.payload_iter.size_hint() } } @@ -81,17 +70,7 @@ where T: ManagedVecItem, { fn next_back(&mut self) -> Option { - if self.byte_start + T::payload_size() > self.byte_end { - return None; - } - self.byte_end -= T::payload_size(); - - let mut payload = T::PAYLOAD::new_buffer(); - let _ = self - .managed_vec - .buffer - .load_slice(self.byte_end, payload.payload_slice_mut()); - + let payload = self.payload_iter.next_back()?; unsafe { Some(T::borrow_from_payload(&payload)) } } } @@ -102,10 +81,11 @@ where T: ManagedVecItem, { fn clone(&self) -> Self { - Self { - managed_vec: self.managed_vec, - byte_start: self.byte_start, - byte_end: self.byte_end, + unsafe { + ManagedVecRefIterator { + payload_iter: self.payload_iter.clone_iter(), + _phantom: PhantomData, + } } } } From 65f9681f6773af6dc2c35df3b50abdb687bd70cc Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 11 Dec 2024 17:35:25 +0200 Subject: [PATCH 55/66] EndpointWrappers call_* method refactor --- .../tests/price_aggregator_whitebox_test.rs | 15 ++- framework/derive/src/contract_impl.rs | 8 +- .../derive/src/generate/endpoints_mod_gen.rs | 4 +- .../derive/src/generate/function_selector.rs | 15 ++- .../derive/src/generate/method_call_gen.rs | 2 +- framework/derive/src/generate/snippets.rs | 3 +- .../scenario/tests/contract_without_macros.rs | 97 ++++++++++++++----- 7 files changed, 93 insertions(+), 51 deletions(-) diff --git a/contracts/core/price-aggregator/tests/price_aggregator_whitebox_test.rs b/contracts/core/price-aggregator/tests/price_aggregator_whitebox_test.rs index f44d9f76fc..a23edf113a 100644 --- a/contracts/core/price-aggregator/tests/price_aggregator_whitebox_test.rs +++ b/contracts/core/price-aggregator/tests/price_aggregator_whitebox_test.rs @@ -2,10 +2,7 @@ use multiversx_price_aggregator_sc::{ price_aggregator_data::{OracleStatus, TimestampedPrice, TokenPair}, PriceAggregator, MAX_ROUND_DURATION_SECONDS, }; -use multiversx_sc_modules::{ - pause::EndpointWrappers as PauseEndpointWrappers, - staking::{EndpointWrappers as StakingEndpointWrappers, StakingModule}, -}; +use multiversx_sc_modules::{pause::PauseModule, staking::StakingModule}; use multiversx_sc_scenario::imports::*; pub const DECIMALS: u8 = 0; @@ -73,7 +70,7 @@ fn test_price_aggregator_submit() { .from(OWNER_ADDRESS) .to(PRICE_AGGREGATOR_ADDRESS) .whitebox(multiversx_price_aggregator_sc::contract_obj, |sc| { - sc.call_unpause_endpoint(); + sc.unpause_endpoint(); }); // submit first timestamp too old @@ -197,7 +194,7 @@ fn test_price_aggregator_submit_round_ok() { .from(OWNER_ADDRESS) .to(PRICE_AGGREGATOR_ADDRESS) .whitebox(multiversx_price_aggregator_sc::contract_obj, |sc| { - sc.call_unpause_endpoint(); + sc.unpause_endpoint(); }); // submit first @@ -304,7 +301,7 @@ fn test_price_aggregator_discarded_round() { .from(OWNER_ADDRESS) .to(PRICE_AGGREGATOR_ADDRESS) .whitebox(multiversx_price_aggregator_sc::contract_obj, |sc| { - sc.call_unpause_endpoint(); + sc.unpause_endpoint(); }); // submit first @@ -380,7 +377,7 @@ fn test_price_aggregator_slashing() { .from(OWNER_ADDRESS) .to(PRICE_AGGREGATOR_ADDRESS) .whitebox(multiversx_price_aggregator_sc::contract_obj, |sc| { - sc.call_unpause_endpoint(); + sc.unpause_endpoint(); }); world @@ -489,7 +486,7 @@ fn setup() -> (ScenarioWorld, Vec

) { .to(PRICE_AGGREGATOR_ADDRESS) .egld(STAKE_AMOUNT) .whitebox(multiversx_price_aggregator_sc::contract_obj, |sc| { - sc.call_stake(); + sc.stake(); }); } diff --git a/framework/derive/src/contract_impl.rs b/framework/derive/src/contract_impl.rs index 1f6b3c9383..b10d66d3ad 100644 --- a/framework/derive/src/contract_impl.rs +++ b/framework/derive/src/contract_impl.rs @@ -52,7 +52,7 @@ pub fn contract_implementation( impl #trait_name_ident for C where - C: AutoImpl #(#supertraits_main)* + C: AutoImpl #(#supertraits_main)* { #(#auto_impls)* @@ -75,15 +75,15 @@ pub fn contract_implementation( { #(#call_methods)* - fn call(&self, fn_name: &str) -> bool { + fn call(&mut self, fn_name: &str) -> bool { #function_selector_body } - fn callback_selector(&self, mut ___cb_closure___: multiversx_sc::types::CallbackClosureForDeser) -> multiversx_sc::types::CallbackSelectorResult { + fn callback_selector(&mut self, mut ___cb_closure___: multiversx_sc::types::CallbackClosureForDeser) -> multiversx_sc::types::CallbackSelectorResult { #callback_selector_body } - fn callback(&self) { + fn callback(&mut self) { #callback_body } } diff --git a/framework/derive/src/generate/endpoints_mod_gen.rs b/framework/derive/src/generate/endpoints_mod_gen.rs index 4bba840a90..e30ac37604 100644 --- a/framework/derive/src/generate/endpoints_mod_gen.rs +++ b/framework/derive/src/generate/endpoints_mod_gen.rs @@ -37,7 +37,7 @@ pub fn generate_endpoints_mod( A: multiversx_sc::api::VMApi , { super::EndpointWrappers::callback( - &multiversx_sc::contract_base::UniversalContractObj::::new(), + &mut multiversx_sc::contract_base::UniversalContractObj::::new(), ); } } @@ -93,7 +93,7 @@ fn generate_wasm_endpoint( A: multiversx_sc::api::VMApi, { super::EndpointWrappers::#call_method_ident( - &multiversx_sc::contract_base::UniversalContractObj::::new(), + &mut multiversx_sc::contract_base::UniversalContractObj::::new(), ); } } diff --git a/framework/derive/src/generate/function_selector.rs b/framework/derive/src/generate/function_selector.rs index 6f157724f8..3aed56f622 100644 --- a/framework/derive/src/generate/function_selector.rs +++ b/framework/derive/src/generate/function_selector.rs @@ -48,21 +48,20 @@ pub fn generate_function_selector_body(contract: &ContractTrait) -> proc_macro2: let module_calls = supertrait_gen::function_selector_module_calls(contract.supertraits.as_slice()); quote! { - if match fn_name { + match fn_name { "callBack" => { self::EndpointWrappers::callback(self); - return true; + true }, "init" if ::external_view_init_override() => { multiversx_sc::external_view_contract::external_view_contract_constructor::(); - return true; + true }, #(#match_arms)* - other => false - } { - return true; + other => { + #(#module_calls)* + false + } } - #(#module_calls)* - false } } diff --git a/framework/derive/src/generate/method_call_gen.rs b/framework/derive/src/generate/method_call_gen.rs index a34db90f06..74d3f3ce4f 100644 --- a/framework/derive/src/generate/method_call_gen.rs +++ b/framework/derive/src/generate/method_call_gen.rs @@ -27,7 +27,7 @@ pub fn generate_call_method(m: &Method) -> proc_macro2::TokenStream { let call_method_body = generate_endpoint_call_method_body(m); quote! { #[inline] - fn #call_method_ident (&self) { + fn #call_method_ident (&mut self) { #call_method_body } } diff --git a/framework/derive/src/generate/snippets.rs b/framework/derive/src/generate/snippets.rs index 6e66eb9ae4..91fc8a863f 100644 --- a/framework/derive/src/generate/snippets.rs +++ b/framework/derive/src/generate/snippets.rs @@ -67,7 +67,8 @@ pub fn impl_callable_contract() -> proc_macro2::TokenStream { A: multiversx_sc::api::VMApi + Send + Sync, { fn call(&self, fn_name: &str) -> bool { - EndpointWrappers::call(self, fn_name) + let mut obj = multiversx_sc::contract_base::UniversalContractObj::::new(); + EndpointWrappers::call(&mut obj, fn_name) } } } diff --git a/framework/scenario/tests/contract_without_macros.rs b/framework/scenario/tests/contract_without_macros.rs index c597bb9e81..ccdcc493fd 100644 --- a/framework/scenario/tests/contract_without_macros.rs +++ b/framework/scenario/tests/contract_without_macros.rs @@ -9,7 +9,7 @@ #![allow(unused)] use multiversx_sc::{ - contract_base::ProxyObjNew, + contract_base::{CallableContractBuilder, ProxyObjNew}, types::{BigInt, ManagedAddress}, }; use multiversx_sc_scenario::api::{SingleTxApi, StaticApi}; @@ -57,19 +57,19 @@ mod module_1 { pub trait EndpointWrappers: VersionModule + multiversx_sc::contract_base::ContractBase { #[inline] - fn call_version(&self) { + fn call_version(&mut self) { multiversx_sc::io::call_value_init::not_payable::(); let result = self.version(); multiversx_sc::io::finish_multi::(&result) } - fn call_some_async(&self) { + fn call_some_async(&mut self) { self.some_async(); multiversx_sc::io::finish_multi::(&()) } - fn call(&self, fn_name: &str) -> bool { - if match fn_name { + fn call(&mut self, fn_name: &str) -> bool { + match fn_name { "callBack" => { self.callback(); return true; @@ -79,10 +79,13 @@ mod module_1 { true }, _other => false, - } { - return true; } - false + } + fn callback_selector( + &mut self, + mut ___cb_closure___: multiversx_sc::types::CallbackClosureForDeser, + ) -> multiversx_sc::types::CallbackSelectorResult { + multiversx_sc::types::CallbackSelectorResult::NotProcessed(___cb_closure___) } } @@ -277,7 +280,7 @@ mod sample_adder { Adder + multiversx_sc::contract_base::ContractBase + super::module_1::EndpointWrappers { #[inline] - fn call_sum(&self) { + fn call_sum(&mut self) { ::init_static(); multiversx_sc::io::call_value_init::not_payable::(); let () = multiversx_sc::io::load_endpoint_args::(()); @@ -285,7 +288,7 @@ mod sample_adder { multiversx_sc::io::finish_multi::(&result); } #[inline] - fn call_init(&self) { + fn call_init(&mut self) { ::init_static(); multiversx_sc::io::call_value_init::not_payable::(); let (initial_value, ()) = multiversx_sc::io::load_endpoint_args::< @@ -295,7 +298,7 @@ mod sample_adder { self.init(initial_value); } #[inline] - fn call_upgrade(&self) { + fn call_upgrade(&mut self) { ::init_static(); multiversx_sc::io::call_value_init::not_payable::(); let (initial_value, ()) = multiversx_sc::io::load_endpoint_args::< @@ -305,7 +308,7 @@ mod sample_adder { self.upgrade(initial_value); } #[inline] - fn call_add(&self) { + fn call_add(&mut self) { ::init_static(); multiversx_sc::io::call_value_init::not_payable::(); let (value, ()) = multiversx_sc::io::load_endpoint_args::< @@ -314,8 +317,8 @@ mod sample_adder { >(("value", ())); self.add(value); } - fn call(&self, fn_name: &str) -> bool { - if match fn_name { + fn call(&mut self, fn_name: &str) -> bool { + match fn_name { "callBack" => { self::EndpointWrappers::callback(self); return true; @@ -346,19 +349,58 @@ mod sample_adder { self.call_add(); true }, - other => false, - } { - return true; + other => { + if super::module_1::EndpointWrappers::call(self, fn_name) { + return true; + } + false + }, } - false } fn callback_selector( - &self, + &mut self, mut ___cb_closure___: multiversx_sc::types::CallbackClosureForDeser, ) -> multiversx_sc::types::CallbackSelectorResult { + let ___cb_closure_matcher___ = ___cb_closure___.matcher::<32usize>(); + if ___cb_closure_matcher___.matches_empty() { + return multiversx_sc::types::CallbackSelectorResult::Processed; + } + match super::module_1::EndpointWrappers::callback_selector(self, ___cb_closure___) { + multiversx_sc::types::CallbackSelectorResult::Processed => { + return multiversx_sc::types::CallbackSelectorResult::Processed; + }, + multiversx_sc::types::CallbackSelectorResult::NotProcessed( + recovered_cb_closure, + ) => { + ___cb_closure___ = recovered_cb_closure; + }, + } + match super::module_1::EndpointWrappers::callback_selector(self, ___cb_closure___) { + multiversx_sc::types::CallbackSelectorResult::Processed => { + return multiversx_sc::types::CallbackSelectorResult::Processed; + }, + multiversx_sc::types::CallbackSelectorResult::NotProcessed( + recovered_cb_closure, + ) => { + ___cb_closure___ = recovered_cb_closure; + }, + } multiversx_sc::types::CallbackSelectorResult::NotProcessed(___cb_closure___) } - fn callback(&self) {} + fn callback(&mut self) { + if let Some(___cb_closure___) = + multiversx_sc::types::CallbackClosureForDeser::storage_load_and_clear::() + { + if let multiversx_sc::types::CallbackSelectorResult::NotProcessed(_) = + self::EndpointWrappers::callback_selector(self, ___cb_closure___) + { + multiversx_sc::api::ErrorApiImpl::signal_error( + &::error_api_impl(), + err_msg::CALLBACK_BAD_FUNC.as_bytes(), + ); + } + } + } } impl EndpointWrappers for multiversx_sc::contract_base::UniversalContractObj where @@ -441,7 +483,7 @@ mod sample_adder { A: multiversx_sc::api::VMApi, { super::EndpointWrappers::call_sum( - &multiversx_sc::contract_base::UniversalContractObj::::new(), + &mut multiversx_sc::contract_base::UniversalContractObj::::new(), ); } pub fn init() @@ -449,7 +491,7 @@ mod sample_adder { A: multiversx_sc::api::VMApi, { super::EndpointWrappers::call_init( - &multiversx_sc::contract_base::UniversalContractObj::::new(), + &mut multiversx_sc::contract_base::UniversalContractObj::::new(), ); } pub fn upgrade() @@ -457,7 +499,7 @@ mod sample_adder { A: multiversx_sc::api::VMApi, { super::EndpointWrappers::call_upgrade( - &multiversx_sc::contract_base::UniversalContractObj::::new(), + &mut multiversx_sc::contract_base::UniversalContractObj::::new(), ); } pub fn add() @@ -465,7 +507,7 @@ mod sample_adder { A: multiversx_sc::api::VMApi, { super::EndpointWrappers::call_add( - &multiversx_sc::contract_base::UniversalContractObj::::new(), + &mut multiversx_sc::contract_base::UniversalContractObj::::new(), ); } pub fn callBack() @@ -473,7 +515,7 @@ mod sample_adder { A: multiversx_sc::api::VMApi, { super::EndpointWrappers::callback( - &multiversx_sc::contract_base::UniversalContractObj::::new(), + &mut multiversx_sc::contract_base::UniversalContractObj::::new(), ); } } @@ -597,7 +639,8 @@ mod sample_adder { A: multiversx_sc::api::VMApi, { fn call(&self, fn_name: &str) -> bool { - EndpointWrappers::call(self, fn_name) + let mut obj = multiversx_sc::contract_base::UniversalContractObj::::new(); + EndpointWrappers::call(&mut obj, fn_name) } } @@ -770,8 +813,10 @@ fn contract_without_macros_basic() { assert_eq!(BigInt::from(100), adder.version()); + let adder = sample_adder::ContractBuilder.new_contract_obj::(); assert!(!adder.call("invalid_endpoint")); + let adder = sample_adder::ContractBuilder.new_contract_obj::(); assert!(adder.call("getSum")); let mut own_proxy = From de3b6df1fa6ebabc69c8eefb34a38c390f738941 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 11 Dec 2024 18:25:56 +0200 Subject: [PATCH 56/66] cleanup, comments --- framework/scenario/tests/contract_without_macros.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/framework/scenario/tests/contract_without_macros.rs b/framework/scenario/tests/contract_without_macros.rs index ccdcc493fd..d461adea6d 100644 --- a/framework/scenario/tests/contract_without_macros.rs +++ b/framework/scenario/tests/contract_without_macros.rs @@ -254,8 +254,6 @@ mod sample_adder { ///////////////////////////////////////////////////////////////////////////////////////////////// pub trait AutoImpl: multiversx_sc::contract_base::ContractBase {} - // impl super::module_1::AutoImpl for C where C: AutoImpl {} - impl Adder for C where C: AutoImpl + super::module_1::AutoImpl, @@ -639,6 +637,8 @@ mod sample_adder { A: multiversx_sc::api::VMApi, { fn call(&self, fn_name: &str) -> bool { + // creating a new object, which we can mutate + // because of dynamic traits, we cannot move `self` let mut obj = multiversx_sc::contract_base::UniversalContractObj::::new(); EndpointWrappers::call(&mut obj, fn_name) } From 81f28d55bc1d465e0abafa33e2181fa4368543ea Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 11 Dec 2024 20:34:09 +0200 Subject: [PATCH 57/66] refactor: ManagedResultArgLoader replaced with ManagedVec iterators --- .../contract_call_legacy/contract_deploy.rs | 4 +-- .../base/src/types/interaction/tx_exec.rs | 4 +-- .../multi_value/multi_value_encoded_iter.rs | 23 +++++++------ .../managed/wrapped/managed_vec_iter_owned.rs | 33 ++++++++++++++++++- .../wrapped/managed_vec_iter_payload.rs | 21 +++++++++--- .../managed/wrapped/managed_vec_iter_ref.rs | 33 ++++++++++++++++++- 6 files changed, 95 insertions(+), 23 deletions(-) diff --git a/framework/base/src/types/interaction/contract_call_legacy/contract_deploy.rs b/framework/base/src/types/interaction/contract_call_legacy/contract_deploy.rs index 6b19010e3d..8b12548b30 100644 --- a/framework/base/src/types/interaction/contract_call_legacy/contract_deploy.rs +++ b/framework/base/src/types/interaction/contract_call_legacy/contract_deploy.rs @@ -9,7 +9,7 @@ use crate::{ api::{BlockchainApiImpl, CallTypeApi}, contract_base::{ExitCodecErrorHandler, SendRawWrapper}, err_msg, - io::{ArgErrorHandler, ArgId, ManagedResultArgLoader}, + io::{ArgErrorHandler, ArgId}, types::{ BigUint, CodeMetadata, ManagedAddress, ManagedArgBuffer, ManagedBuffer, ManagedOption, ManagedVec, @@ -112,7 +112,7 @@ where where RequestedResult: TopDecodeMulti + TypeAbiFrom, { - let mut loader = ManagedResultArgLoader::new(raw_result); + let mut loader = raw_result.into_iter(); let arg_id = ArgId::from(&b"init result"[..]); let h = ArgErrorHandler::::from(arg_id); RequestedResult::multi_decode_or_handle_err(&mut loader, h).unwrap_infallible() diff --git a/framework/base/src/types/interaction/tx_exec.rs b/framework/base/src/types/interaction/tx_exec.rs index 0c200df330..6f4ffc40d3 100644 --- a/framework/base/src/types/interaction/tx_exec.rs +++ b/framework/base/src/types/interaction/tx_exec.rs @@ -15,7 +15,7 @@ use unwrap_infallible::UnwrapInfallible; use crate::{ api::CallTypeApi, - io::{ArgErrorHandler, ArgId, ManagedResultArgLoader}, + io::{ArgErrorHandler, ArgId}, types::{ManagedBuffer, ManagedVec}, }; use multiversx_sc_codec::TopDecodeMulti; @@ -30,7 +30,7 @@ where SA: CallTypeApi + 'static, RequestedResult: TopDecodeMulti, { - let mut loader = ManagedResultArgLoader::new(raw_result); + let mut loader = raw_result.into_iter(); let arg_id = ArgId::from(&b"sync result"[..]); let h: ArgErrorHandler = ArgErrorHandler::::from(arg_id); RequestedResult::multi_decode_or_handle_err(&mut loader, h).unwrap_infallible() diff --git a/framework/base/src/types/managed/multi_value/multi_value_encoded_iter.rs b/framework/base/src/types/managed/multi_value/multi_value_encoded_iter.rs index 4b8db58ffb..670586fbf6 100644 --- a/framework/base/src/types/managed/multi_value/multi_value_encoded_iter.rs +++ b/framework/base/src/types/managed/multi_value/multi_value_encoded_iter.rs @@ -4,10 +4,10 @@ use unwrap_infallible::UnwrapInfallible; use crate::codec::{TopDecodeMulti, TopDecodeMultiInput}; -use crate::types::{ManagedBuffer, ManagedVec}; +use crate::types::{ManagedBuffer, ManagedVec, ManagedVecOwnedIterator}; use crate::{ api::{ErrorApi, ManagedTypeApi}, - io::{ArgErrorHandler, ArgId, ManagedResultArgLoader}, + io::{ArgErrorHandler, ArgId}, }; /// Iterator for `MultiValueEncoded` and `MultiValueEncodedCounted`. @@ -18,7 +18,7 @@ where M: ManagedTypeApi + ErrorApi, T: TopDecodeMulti, { - data_loader: ManagedResultArgLoader, + data_loader: ManagedVecOwnedIterator>, _phantom: PhantomData, } @@ -29,7 +29,7 @@ where { pub(crate) fn new(raw_buffers: ManagedVec>) -> Self { MultiValueEncodedIterator { - data_loader: ManagedResultArgLoader::new(raw_buffers), + data_loader: raw_buffers.into_iter(), _phantom: PhantomData, } } @@ -43,14 +43,13 @@ where type Item = T; fn next(&mut self) -> Option { - if self.data_loader.has_next() { - let arg_id = ArgId::from(&b"var args"[..]); - let h = ArgErrorHandler::::from(arg_id); - let result = - T::multi_decode_or_handle_err(&mut self.data_loader, h).unwrap_infallible(); - Some(result) - } else { - None + if !self.data_loader.has_next() { + return None; } + + let arg_id = ArgId::from(&b"var args"[..]); + let h = ArgErrorHandler::::from(arg_id); + let result = T::multi_decode_or_handle_err(&mut self.data_loader, h).unwrap_infallible(); + Some(result) } } diff --git a/framework/base/src/types/managed/wrapped/managed_vec_iter_owned.rs b/framework/base/src/types/managed/wrapped/managed_vec_iter_owned.rs index 055b4b4a11..f4712e7464 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_iter_owned.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_iter_owned.rs @@ -1,4 +1,9 @@ -use crate::{api::ManagedTypeApi, types::ManagedType}; +use multiversx_sc_codec::{DecodeError, DecodeErrorHandler, TopDecodeMultiInput}; + +use crate::{ + api::{ErrorApi, ManagedTypeApi}, + types::{ManagedBuffer, ManagedType}, +}; use super::{ManagedVec, ManagedVecItem, ManagedVecPayloadIterator}; @@ -34,6 +39,10 @@ where } } } + + pub(crate) fn iter_is_empty(&self) -> bool { + self.payload_iter.iter_is_empty() + } } impl Iterator for ManagedVecOwnedIterator @@ -70,3 +79,25 @@ where Some(T::read_from_payload(&payload)) } } + +impl TopDecodeMultiInput for ManagedVecOwnedIterator> +where + A: ManagedTypeApi + ErrorApi, +{ + type ValueInput = ManagedBuffer; + + fn has_next(&self) -> bool { + !self.iter_is_empty() + } + + fn next_value_input(&mut self, h: H) -> Result + where + H: DecodeErrorHandler, + { + if let Some(buffer) = self.next() { + Ok(buffer) + } else { + Err(h.handle_error(DecodeError::MULTI_TOO_FEW_ARGS)) + } + } +} diff --git a/framework/base/src/types/managed/wrapped/managed_vec_iter_payload.rs b/framework/base/src/types/managed/wrapped/managed_vec_iter_payload.rs index 44293133d2..28fd6e3943 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_iter_payload.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_iter_payload.rs @@ -40,6 +40,15 @@ where _phantom: PhantomData, } } + + pub(crate) fn remaining(&self) -> usize { + (self.byte_end - self.byte_start) / P::payload_size() + } + + /// TODO: can be replaced with ExactSizeIterator::is_empty once it's stabilized + pub(crate) fn iter_is_empty(&self) -> bool { + self.byte_start >= self.byte_end + } } impl Iterator for ManagedVecPayloadIterator @@ -50,10 +59,10 @@ where type Item = P; fn next(&mut self) -> Option

{ - let next_byte_start = self.byte_start + P::payload_size(); - if next_byte_start > self.byte_end { + if self.iter_is_empty() { return None; } + let next_byte_start = self.byte_start + P::payload_size(); let mut payload = P::new_buffer(); let _ = M::managed_type_impl().mb_load_slice( @@ -67,8 +76,7 @@ where } fn size_hint(&self) -> (usize, Option) { - let size = P::payload_size(); - let remaining = (self.byte_end - self.byte_start) / size; + let remaining = self.remaining(); (remaining, Some(remaining)) } } @@ -78,6 +86,9 @@ where M: ManagedTypeApi, P: ManagedVecItemPayload, { + fn len(&self) -> usize { + self.remaining() + } } impl DoubleEndedIterator for ManagedVecPayloadIterator @@ -86,7 +97,7 @@ where P: ManagedVecItemPayload, { fn next_back(&mut self) -> Option { - if self.byte_start + P::payload_size() > self.byte_end { + if self.iter_is_empty() { return None; } self.byte_end -= P::payload_size(); diff --git a/framework/base/src/types/managed/wrapped/managed_vec_iter_ref.rs b/framework/base/src/types/managed/wrapped/managed_vec_iter_ref.rs index b57a16d3d7..7305462b1c 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_iter_ref.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_iter_ref.rs @@ -1,6 +1,11 @@ use core::marker::PhantomData; -use crate::{api::ManagedTypeApi, types::ManagedType}; +use multiversx_sc_codec::{DecodeError, DecodeErrorHandler, TopDecodeMultiInput}; + +use crate::{ + api::{ErrorApi, ManagedTypeApi}, + types::{ManagedBuffer, ManagedType}, +}; use super::{ManagedVec, ManagedVecItem, ManagedVecPayloadIterator}; @@ -38,6 +43,10 @@ where } } } + + pub(crate) fn iter_is_empty(&self) -> bool { + self.payload_iter.iter_is_empty() + } } impl<'a, M, T> Iterator for ManagedVecRefIterator<'a, M, T> @@ -89,3 +98,25 @@ where } } } + +impl<'a, A> TopDecodeMultiInput for ManagedVecRefIterator<'a, A, ManagedBuffer> +where + A: ManagedTypeApi + ErrorApi, +{ + type ValueInput = ManagedBuffer; + + fn has_next(&self) -> bool { + !self.iter_is_empty() + } + + fn next_value_input(&mut self, h: H) -> Result + where + H: DecodeErrorHandler, + { + if let Some(buffer) = self.next() { + Ok(buffer.clone()) + } else { + Err(h.handle_error(DecodeError::MULTI_TOO_FEW_ARGS)) + } + } +} From 2e776b7b56f1a579fc776294d1bb2612d02103be Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 11 Dec 2024 20:41:15 +0200 Subject: [PATCH 58/66] clippy fix --- .../base/src/types/managed/wrapped/managed_vec_iter_ref.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/base/src/types/managed/wrapped/managed_vec_iter_ref.rs b/framework/base/src/types/managed/wrapped/managed_vec_iter_ref.rs index 7305462b1c..ae5212c9d5 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_iter_ref.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_iter_ref.rs @@ -99,7 +99,7 @@ where } } -impl<'a, A> TopDecodeMultiInput for ManagedVecRefIterator<'a, A, ManagedBuffer> +impl TopDecodeMultiInput for ManagedVecRefIterator<'_, A, ManagedBuffer> where A: ManagedTypeApi + ErrorApi, { From 7955fcc41e3c9e6c07b12a439907607458534a23 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 11 Dec 2024 20:41:43 +0200 Subject: [PATCH 59/66] refactor: ManagedResultArgLoader replaced in legacy callback --- framework/base/src/types/interaction/callback_closure.rs | 7 +++---- framework/derive/src/generate/method_call_gen_arg.rs | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/framework/base/src/types/interaction/callback_closure.rs b/framework/base/src/types/interaction/callback_closure.rs index 6b2b73feee..b7ea4264a8 100644 --- a/framework/base/src/types/interaction/callback_closure.rs +++ b/framework/base/src/types/interaction/callback_closure.rs @@ -9,10 +9,9 @@ use crate::{ }, contract_base::{BlockchainWrapper, ExitCodecErrorHandler, ManagedSerializer}, err_msg, - io::ManagedResultArgLoader, storage::StorageKey, storage_clear, storage_get, storage_set, - types::{ManagedBuffer, ManagedType}, + types::{ManagedBuffer, ManagedType, ManagedVecRefIterator}, }; use super::ManagedArgBuffer; @@ -125,8 +124,8 @@ impl CallbackClosureForDeser { CallbackClosureMatcher::new(&self.callback_name) } - pub fn into_arg_loader(self) -> ManagedResultArgLoader { - ManagedResultArgLoader::new(self.closure_args.data) + pub fn arg_iter(&self) -> ManagedVecRefIterator<'_, M, ManagedBuffer> { + self.closure_args.iter_buffers() } } diff --git a/framework/derive/src/generate/method_call_gen_arg.rs b/framework/derive/src/generate/method_call_gen_arg.rs index 20550ca024..8c9b7a0aee 100644 --- a/framework/derive/src/generate/method_call_gen_arg.rs +++ b/framework/derive/src/generate/method_call_gen_arg.rs @@ -88,7 +88,7 @@ pub fn load_legacy_cb_closure_args_snippet(m: &Method) -> proc_macro2::TokenStre }); quote! { let #closure_var_names = multiversx_sc::io::load_multi_args_custom_loader::( - ___cb_closure___.into_arg_loader(), + ___cb_closure___.arg_iter(), #closure_var_names_str, ); } From d74aa6add2e85e36adb924da075b2ebd9a3ac4df Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 11 Dec 2024 21:08:42 +0200 Subject: [PATCH 60/66] simplified callback_selector --- .../interaction/callback_selector_result.rs | 18 ++++----- framework/derive/src/contract_impl.rs | 4 +- framework/derive/src/generate/callback_gen.rs | 18 +++------ .../scenario/tests/contract_without_macros.rs | 39 ++++++------------- 4 files changed, 30 insertions(+), 49 deletions(-) diff --git a/framework/base/src/types/interaction/callback_selector_result.rs b/framework/base/src/types/interaction/callback_selector_result.rs index d04487b57f..e6bd200a66 100644 --- a/framework/base/src/types/interaction/callback_selector_result.rs +++ b/framework/base/src/types/interaction/callback_selector_result.rs @@ -1,13 +1,13 @@ -use crate::api::{ErrorApi, ManagedTypeApi}; - -use super::CallbackClosureForDeser; - /// Used internally between the `callback` and `callback_selector` methods. /// It is likely to be removed in the future. -pub enum CallbackSelectorResult -where - A: ManagedTypeApi + ErrorApi, -{ +#[derive(Clone, Copy, PartialEq, Eq)] +pub enum CallbackSelectorResult { Processed, - NotProcessed(CallbackClosureForDeser), + NotProcessed, +} + +impl CallbackSelectorResult { + pub fn is_processed(self) -> bool { + matches!(self, CallbackSelectorResult::Processed) + } } diff --git a/framework/derive/src/contract_impl.rs b/framework/derive/src/contract_impl.rs index b10d66d3ad..32ee2d76e0 100644 --- a/framework/derive/src/contract_impl.rs +++ b/framework/derive/src/contract_impl.rs @@ -79,7 +79,9 @@ pub fn contract_implementation( #function_selector_body } - fn callback_selector(&mut self, mut ___cb_closure___: multiversx_sc::types::CallbackClosureForDeser) -> multiversx_sc::types::CallbackSelectorResult { + fn callback_selector(&mut self, ___cb_closure___: &multiversx_sc::types::CallbackClosureForDeser) + -> multiversx_sc::types::CallbackSelectorResult + { #callback_selector_body } diff --git a/framework/derive/src/generate/callback_gen.rs b/framework/derive/src/generate/callback_gen.rs index 23a3a56713..e16e6daafa 100644 --- a/framework/derive/src/generate/callback_gen.rs +++ b/framework/derive/src/generate/callback_gen.rs @@ -29,7 +29,7 @@ pub fn generate_callback_selector_and_main( let cb_main_body = quote! { let _ = self::EndpointWrappers::callback_selector( self, - multiversx_sc::types::CallbackClosureForDeser::no_callback(), + &multiversx_sc::types::CallbackClosureForDeser::no_callback(), ); }; (cb_selector_body, cb_main_body) @@ -39,7 +39,7 @@ pub fn generate_callback_selector_and_main( module_calls(contract.supertraits.as_slice()); if match_arms.is_empty() && module_calls.is_empty() { let cb_selector_body = quote! { - multiversx_sc::types::CallbackSelectorResult::NotProcessed(___cb_closure___) + multiversx_sc::types::CallbackSelectorResult::NotProcessed }; let cb_main_body = quote! {}; (cb_selector_body, cb_main_body) @@ -47,8 +47,7 @@ pub fn generate_callback_selector_and_main( let cb_selector_body = callback_selector_body(match_arms, module_calls); let cb_main_body = quote! { if let Some(___cb_closure___) = multiversx_sc::types::CallbackClosureForDeser::storage_load_and_clear::() { - if let multiversx_sc::types::CallbackSelectorResult::NotProcessed(_) = - self::EndpointWrappers::callback_selector(self, ___cb_closure___) { + if !self::EndpointWrappers::callback_selector(self, &___cb_closure___).is_processed() { multiversx_sc::api::ErrorApiImpl::signal_error( &::error_api_impl(), err_msg::CALLBACK_BAD_FUNC.as_bytes(), @@ -79,7 +78,7 @@ fn callback_selector_body( } #(#match_arms)* #(#module_calls)* - multiversx_sc::types::CallbackSelectorResult::NotProcessed(___cb_closure___) + multiversx_sc::types::CallbackSelectorResult::NotProcessed } } @@ -123,13 +122,8 @@ pub fn module_calls(supertraits: &[Supertrait]) -> Vec .map(|supertrait| { let module_path = &supertrait.module_path; quote! { - match #module_path EndpointWrappers::callback_selector(self, ___cb_closure___) { - multiversx_sc::types::CallbackSelectorResult::Processed => { - return multiversx_sc::types::CallbackSelectorResult::Processed; - }, - multiversx_sc::types::CallbackSelectorResult::NotProcessed(recovered_cb_closure) => { - ___cb_closure___ = recovered_cb_closure; - }, + if #module_path EndpointWrappers::callback_selector(self, ___cb_closure___).is_processed() { + return multiversx_sc::types::CallbackSelectorResult::Processed; } } }) diff --git a/framework/scenario/tests/contract_without_macros.rs b/framework/scenario/tests/contract_without_macros.rs index d461adea6d..29189bf898 100644 --- a/framework/scenario/tests/contract_without_macros.rs +++ b/framework/scenario/tests/contract_without_macros.rs @@ -83,9 +83,9 @@ mod module_1 { } fn callback_selector( &mut self, - mut ___cb_closure___: multiversx_sc::types::CallbackClosureForDeser, - ) -> multiversx_sc::types::CallbackSelectorResult { - multiversx_sc::types::CallbackSelectorResult::NotProcessed(___cb_closure___) + ___cb_closure___: &multiversx_sc::types::CallbackClosureForDeser, + ) -> multiversx_sc::types::CallbackSelectorResult { + multiversx_sc::types::CallbackSelectorResult::NotProcessed } } @@ -357,40 +357,25 @@ mod sample_adder { } fn callback_selector( &mut self, - mut ___cb_closure___: multiversx_sc::types::CallbackClosureForDeser, - ) -> multiversx_sc::types::CallbackSelectorResult { + ___cb_closure___: &multiversx_sc::types::CallbackClosureForDeser, + ) -> multiversx_sc::types::CallbackSelectorResult { let ___cb_closure_matcher___ = ___cb_closure___.matcher::<32usize>(); if ___cb_closure_matcher___.matches_empty() { return multiversx_sc::types::CallbackSelectorResult::Processed; } - match super::module_1::EndpointWrappers::callback_selector(self, ___cb_closure___) { - multiversx_sc::types::CallbackSelectorResult::Processed => { - return multiversx_sc::types::CallbackSelectorResult::Processed; - }, - multiversx_sc::types::CallbackSelectorResult::NotProcessed( - recovered_cb_closure, - ) => { - ___cb_closure___ = recovered_cb_closure; - }, - } - match super::module_1::EndpointWrappers::callback_selector(self, ___cb_closure___) { - multiversx_sc::types::CallbackSelectorResult::Processed => { - return multiversx_sc::types::CallbackSelectorResult::Processed; - }, - multiversx_sc::types::CallbackSelectorResult::NotProcessed( - recovered_cb_closure, - ) => { - ___cb_closure___ = recovered_cb_closure; - }, + if super::module_1::EndpointWrappers::callback_selector(self, ___cb_closure___) + .is_processed() + { + return multiversx_sc::types::CallbackSelectorResult::Processed; } - multiversx_sc::types::CallbackSelectorResult::NotProcessed(___cb_closure___) + multiversx_sc::types::CallbackSelectorResult::NotProcessed } fn callback(&mut self) { if let Some(___cb_closure___) = multiversx_sc::types::CallbackClosureForDeser::storage_load_and_clear::() { - if let multiversx_sc::types::CallbackSelectorResult::NotProcessed(_) = - self::EndpointWrappers::callback_selector(self, ___cb_closure___) + if !self::EndpointWrappers::callback_selector(self, &___cb_closure___) + .is_processed() { multiversx_sc::api::ErrorApiImpl::signal_error( &::error_api_impl(), From eedd7f2c2a198248b276e0761442aacb98e17666 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 11 Dec 2024 21:23:42 +0200 Subject: [PATCH 61/66] refactor: ManagedResultArgLoader removed from load_callback_closure_args --- framework/base/src/io/arg_nested_tuple.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/framework/base/src/io/arg_nested_tuple.rs b/framework/base/src/io/arg_nested_tuple.rs index 1a0eaace7a..aa833633d8 100644 --- a/framework/base/src/io/arg_nested_tuple.rs +++ b/framework/base/src/io/arg_nested_tuple.rs @@ -1,15 +1,15 @@ use unwrap_infallible::UnwrapInfallible; -use super::{EndpointDynArgLoader, EndpointSingleArgLoader, ManagedResultArgLoader}; +use super::{EndpointDynArgLoader, EndpointSingleArgLoader}; use crate::{ api::{ - const_handles, use_raw_handle, EndpointArgumentApi, EndpointArgumentApiImpl, ErrorApi, - ErrorApiImpl, ManagedTypeApi, StaticVarApiImpl, VMApi, + const_handles, EndpointArgumentApi, EndpointArgumentApiImpl, ErrorApi, ErrorApiImpl, + ManagedTypeApi, StaticVarApiImpl, VMApi, }, codec::{DecodeError, TopDecodeMulti, TopDecodeMultiInput}, err_msg, io::{ArgErrorHandler, ArgId}, - types::{ManagedArgBuffer, ManagedBuffer, ManagedType}, + types::{ManagedArgBuffer, ManagedBuffer, ManagedType, ManagedVecRefIterator}, }; /// Argument count cannot change during execution, and it can get queried multiple times, @@ -194,19 +194,16 @@ where N::next_multi_arg(loader, arg_names) } -fn callback_closure_args_loader() -> ManagedResultArgLoader +fn callback_closure_args_loader() -> ManagedVecRefIterator<'static, AA, ManagedBuffer> where AA: VMApi, { + let cb_closure_args_serialized = ManagedBuffer::::new(); + AA::argument_api_impl().load_callback_closure_buffer(cb_closure_args_serialized.get_handle()); unsafe { - AA::argument_api_impl() - .load_callback_closure_buffer(use_raw_handle(const_handles::MBUF_TEMPORARY_1)); - let cb_closure_args_serialized = - ManagedBuffer::::from_raw_handle(const_handles::MBUF_TEMPORARY_1); let mut cb_closure_args_buffer = ManagedArgBuffer::::from_raw_handle(const_handles::CALLBACK_CLOSURE_ARGS_BUFFER); cb_closure_args_buffer.deserialize_overwrite(cb_closure_args_serialized); - - ManagedResultArgLoader::new(cb_closure_args_buffer.into_vec_of_buffers()) + ManagedVecRefIterator::new_from_handle(cb_closure_args_buffer.forget_into_handle()) } } From 806aa3f6b359577d31716f40fa6de7b413889282 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 11 Dec 2024 21:23:55 +0200 Subject: [PATCH 62/66] removed ManagedResultArgLoader --- framework/base/src/io.rs | 2 - .../base/src/io/managed_result_arg_loader.rs | 52 ------------------- .../managed/wrapped/managed_vec_iter_ref.rs | 8 ++- 3 files changed, 6 insertions(+), 56 deletions(-) delete mode 100644 framework/base/src/io/managed_result_arg_loader.rs diff --git a/framework/base/src/io.rs b/framework/base/src/io.rs index be7f4444f6..7d71dd2e27 100644 --- a/framework/base/src/io.rs +++ b/framework/base/src/io.rs @@ -7,7 +7,6 @@ mod arg_nested_tuple; mod bytes_arg_loader; pub mod call_value_init; mod finish; -mod managed_result_arg_loader; mod signal_error; pub use arg_de_input::*; @@ -18,5 +17,4 @@ use arg_loader_single::*; pub use arg_nested_tuple::*; pub use bytes_arg_loader::*; pub use finish::*; -pub use managed_result_arg_loader::*; pub use signal_error::*; diff --git a/framework/base/src/io/managed_result_arg_loader.rs b/framework/base/src/io/managed_result_arg_loader.rs deleted file mode 100644 index 77bc9967e1..0000000000 --- a/framework/base/src/io/managed_result_arg_loader.rs +++ /dev/null @@ -1,52 +0,0 @@ -use crate::codec::{DecodeError, DecodeErrorHandler, TopDecodeMultiInput}; - -use crate::{ - api::{ErrorApi, ManagedTypeApi}, - types::{ManagedBuffer, ManagedVec}, -}; - -pub struct ManagedResultArgLoader -where - A: ManagedTypeApi + ErrorApi, -{ - data: ManagedVec>, - data_len: usize, - next_index: usize, -} - -impl ManagedResultArgLoader -where - A: ManagedTypeApi + ErrorApi, -{ - pub fn new(data: ManagedVec>) -> Self { - let data_len = data.len(); - ManagedResultArgLoader { - data, - data_len, - next_index: 0, - } - } -} - -impl TopDecodeMultiInput for ManagedResultArgLoader -where - A: ManagedTypeApi + ErrorApi, -{ - type ValueInput = ManagedBuffer; - - fn has_next(&self) -> bool { - self.next_index < self.data_len - } - - fn next_value_input(&mut self, h: H) -> Result - where - H: DecodeErrorHandler, - { - if let Some(buffer) = self.data.try_get(self.next_index) { - self.next_index += 1; - Ok((*buffer).clone()) - } else { - Err(h.handle_error(DecodeError::MULTI_TOO_FEW_ARGS)) - } - } -} diff --git a/framework/base/src/types/managed/wrapped/managed_vec_iter_ref.rs b/framework/base/src/types/managed/wrapped/managed_vec_iter_ref.rs index ae5212c9d5..f48e3ecab1 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_iter_ref.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_iter_ref.rs @@ -35,15 +35,19 @@ where M: ManagedTypeApi, T: ManagedVecItem, { - pub(crate) fn new(managed_vec: &'a ManagedVec) -> Self { + pub(crate) unsafe fn new_from_handle(vec_handle: M::ManagedBufferHandle) -> Self { unsafe { ManagedVecRefIterator { - payload_iter: ManagedVecPayloadIterator::new(managed_vec.get_handle()), + payload_iter: ManagedVecPayloadIterator::new(vec_handle), _phantom: PhantomData, } } } + pub(crate) fn new(managed_vec: &'a ManagedVec) -> Self { + unsafe { ManagedVecRefIterator::new_from_handle(managed_vec.get_handle()) } + } + pub(crate) fn iter_is_empty(&self) -> bool { self.payload_iter.iter_is_empty() } From f84bbd7e3b105d2aa607b83864cdb5ccafbd73aa Mon Sep 17 00:00:00 2001 From: BiancaIalangi Date: Thu, 12 Dec 2024 15:21:54 +0200 Subject: [PATCH 63/66] code report - show diff size in report --- framework/meta/src/cmd/code_report/compare.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/framework/meta/src/cmd/code_report/compare.rs b/framework/meta/src/cmd/code_report/compare.rs index 28c604962d..98f64c7738 100644 --- a/framework/meta/src/cmd/code_report/compare.rs +++ b/framework/meta/src/cmd/code_report/compare.rs @@ -1,10 +1,20 @@ pub(crate) fn size_status_after_comparing(size: usize, compared_size: usize) -> String { match size.cmp(&compared_size) { std::cmp::Ordering::Greater => { - format!("{} :arrow_right: {} :red_circle:", compared_size, size) + format!( + "{} :arrow_right: {} :red_circle: (+{})", + compared_size, + size, + size - compared_size + ) }, std::cmp::Ordering::Less => { - format!("{} :arrow_right: {} :green_circle:", compared_size, size) + format!( + "{} :arrow_right: {} :green_circle: (-{})", + compared_size, + size, + compared_size - size + ) }, std::cmp::Ordering::Equal => { format!("{}", size) From 96a414af84aa2f12b1e8646432ec909e518d1928 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Thu, 12 Dec 2024 16:34:02 +0200 Subject: [PATCH 64/66] ContractObj wraps UniversalContractObj --- framework/derive/src/generate/snippets.rs | 20 +++++++------------ .../scenario/tests/contract_without_macros.rs | 17 ++++++---------- 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/framework/derive/src/generate/snippets.rs b/framework/derive/src/generate/snippets.rs index 91fc8a863f..64c590ad5f 100644 --- a/framework/derive/src/generate/snippets.rs +++ b/framework/derive/src/generate/snippets.rs @@ -1,11 +1,8 @@ pub fn contract_object_def() -> proc_macro2::TokenStream { quote! { - pub struct ContractObj - where - A: multiversx_sc::api::VMApi, - { - _phantom: core::marker::PhantomData, - } + pub struct ContractObj(multiversx_sc::contract_base::UniversalContractObj) + where + A: multiversx_sc::api::VMApi; } } @@ -26,9 +23,7 @@ pub fn new_contract_object_fn() -> proc_macro2::TokenStream { where A: multiversx_sc::api::VMApi, { - ContractObj { - _phantom: core::marker::PhantomData, - } + ContractObj::(multiversx_sc::contract_base::UniversalContractObj::::new()) } pub struct ContractBuilder; @@ -36,10 +31,9 @@ pub fn new_contract_object_fn() -> proc_macro2::TokenStream { impl multiversx_sc::contract_base::CallableContractBuilder for self::ContractBuilder { fn new_contract_obj( &self, - ) -> multiversx_sc::types::heap::Box { - multiversx_sc::types::heap::Box::new(ContractObj:: { - _phantom: core::marker::PhantomData, - }) + ) -> multiversx_sc::types::heap::Box + { + multiversx_sc::types::heap::Box::new(self::contract_obj::()) } } } diff --git a/framework/scenario/tests/contract_without_macros.rs b/framework/scenario/tests/contract_without_macros.rs index 29189bf898..d0da5fac52 100644 --- a/framework/scenario/tests/contract_without_macros.rs +++ b/framework/scenario/tests/contract_without_macros.rs @@ -592,12 +592,9 @@ mod sample_adder { ///////////////////////////////////////////////////////////////////////////////////////////////// //////// CONTRACT OBJECT //////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////// - pub struct ContractObj + pub struct ContractObj(multiversx_sc::contract_base::UniversalContractObj) where - A: multiversx_sc::api::VMApi, - { - _phantom: core::marker::PhantomData, - } + A: multiversx_sc::api::VMApi; ///////////////////////////////////////////////////////////////////////////////////////////////// //////// CONTRACT OBJECT as CONTRACT BASE /////////////////////////////////////////////////////// @@ -633,19 +630,17 @@ mod sample_adder { where A: multiversx_sc::api::VMApi, { - ContractObj { - _phantom: core::marker::PhantomData, - } + ContractObj::(multiversx_sc::contract_base::UniversalContractObj::::new()) } + pub struct ContractBuilder; + impl multiversx_sc::contract_base::CallableContractBuilder for self::ContractBuilder { fn new_contract_obj( &self, ) -> multiversx_sc::types::heap::Box { - multiversx_sc::types::heap::Box::new(ContractObj:: { - _phantom: core::marker::PhantomData, - }) + multiversx_sc::types::heap::Box::new(self::contract_obj::()) } } From 52302401391fdb07c489abde11e13f446c8e1532 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Fri, 13 Dec 2024 21:31:45 +0200 Subject: [PATCH 65/66] static api - initialization flags grouped into single bitfield --- framework/base/src/api/managed_types.rs | 2 + .../src/api/managed_types/static_var_api.rs | 18 ++++++- .../api/managed_types/static_var_api_flags.rs | 53 +++++++++++++++++++ .../uncallable/static_var_api_uncallable.rs | 10 +++- .../wrappers/call_value_wrapper.rs | 22 ++++---- .../src/api/local_api_vh/static_var_api_vh.rs | 12 ++++- .../src/debug_executor/tx_static_vars.rs | 7 ++- .../api/managed_types/static_var_api_node.rs | 13 ++++- 8 files changed, 119 insertions(+), 18 deletions(-) create mode 100644 framework/base/src/api/managed_types/static_var_api_flags.rs diff --git a/framework/base/src/api/managed_types.rs b/framework/base/src/api/managed_types.rs index 3562020b95..1322ede5ee 100644 --- a/framework/base/src/api/managed_types.rs +++ b/framework/base/src/api/managed_types.rs @@ -8,6 +8,7 @@ mod managed_map_api; mod managed_type_api; mod managed_type_api_impl; mod static_var_api; +mod static_var_api_flags; mod token_identifier_util; pub use big_float_api::*; @@ -19,3 +20,4 @@ pub use managed_map_api::*; pub use managed_type_api::*; pub use managed_type_api_impl::*; pub use static_var_api::*; +pub use static_var_api_flags::StaticVarApiFlags; diff --git a/framework/base/src/api/managed_types/static_var_api.rs b/framework/base/src/api/managed_types/static_var_api.rs index 8eaf474ef3..8fd007d648 100644 --- a/framework/base/src/api/managed_types/static_var_api.rs +++ b/framework/base/src/api/managed_types/static_var_api.rs @@ -1,6 +1,6 @@ use crate::types::LockableStaticBuffer; -use super::RawHandle; +use super::{RawHandle, StaticVarApiFlags}; pub trait StaticVarApi { type StaticVarApiImpl: StaticVarApiImpl; @@ -24,6 +24,22 @@ pub trait StaticVarApiImpl { fn get_num_arguments(&self) -> i32; + fn set_flags(&self, flags: StaticVarApiFlags); + + fn get_flags(&self) -> StaticVarApiFlags; + + /// Returns true if the flag is set, false if is default (false). + /// + /// If the flag is unset (false), will set it. + fn flag_is_set_or_update(&self, flag: StaticVarApiFlags) -> bool { + let mut current_flags = self.get_flags(); + let contains_flag = current_flags.check_and_set(flag); + if !contains_flag { + self.set_flags(current_flags); + } + contains_flag + } + fn set_call_value_egld_handle(&self, handle: RawHandle); fn get_call_value_egld_handle(&self) -> RawHandle; diff --git a/framework/base/src/api/managed_types/static_var_api_flags.rs b/framework/base/src/api/managed_types/static_var_api_flags.rs new file mode 100644 index 0000000000..1c9e153e69 --- /dev/null +++ b/framework/base/src/api/managed_types/static_var_api_flags.rs @@ -0,0 +1,53 @@ +use bitflags::bitflags; + +bitflags! { + #[derive(Clone, Copy, Debug, PartialEq, Eq)] + pub struct StaticVarApiFlags: u8 { + const NONE = 0b00000000; + const CALL_VALUE_EGLD_INITIALIZED = 0b00000001; + const CALL_VALUE_EGLD_MULTI_INITIALIZED = 0b00000010; + const CALL_VALUE_MULTI_ESDT_INITIALIZED = 0b00000100; + const CALL_VALUE_ALL_INITIALIZED = 0b00001000; + } +} + +impl StaticVarApiFlags { + pub fn check_and_set(&mut self, other: StaticVarApiFlags) -> bool { + let contains_flag = self.contains(other); + if !contains_flag { + *self |= other; + } + contains_flag + } +} + +#[cfg(test)] +pub mod tests { + use super::StaticVarApiFlags; + + #[test] + fn test_check_and_set() { + let mut current = StaticVarApiFlags::NONE; + + assert!(current.check_and_set(StaticVarApiFlags::NONE)); + assert_eq!(current, StaticVarApiFlags::NONE); + + assert!(!current.check_and_set(StaticVarApiFlags::CALL_VALUE_EGLD_INITIALIZED)); + assert_eq!(current, StaticVarApiFlags::CALL_VALUE_EGLD_INITIALIZED); + assert!(current.check_and_set(StaticVarApiFlags::CALL_VALUE_EGLD_INITIALIZED)); + assert_eq!(current, StaticVarApiFlags::CALL_VALUE_EGLD_INITIALIZED); + + assert!(!current.check_and_set(StaticVarApiFlags::CALL_VALUE_ALL_INITIALIZED)); + assert_eq!( + current, + StaticVarApiFlags::CALL_VALUE_EGLD_INITIALIZED + | StaticVarApiFlags::CALL_VALUE_ALL_INITIALIZED + ); + assert!(current.check_and_set(StaticVarApiFlags::CALL_VALUE_ALL_INITIALIZED)); + assert_eq!( + current, + StaticVarApiFlags::CALL_VALUE_EGLD_INITIALIZED + | StaticVarApiFlags::CALL_VALUE_ALL_INITIALIZED + ); + } +} diff --git a/framework/base/src/api/uncallable/static_var_api_uncallable.rs b/framework/base/src/api/uncallable/static_var_api_uncallable.rs index 1da3c772cf..db126b337d 100644 --- a/framework/base/src/api/uncallable/static_var_api_uncallable.rs +++ b/framework/base/src/api/uncallable/static_var_api_uncallable.rs @@ -1,5 +1,5 @@ use crate::{ - api::{RawHandle, StaticVarApi, StaticVarApiImpl}, + api::{RawHandle, StaticVarApi, StaticVarApiFlags, StaticVarApiImpl}, types::LockableStaticBuffer, }; @@ -41,6 +41,14 @@ impl StaticVarApiImpl for UncallableApi { unreachable!() } + fn set_flags(&self, _flags: StaticVarApiFlags) { + unreachable!() + } + + fn get_flags(&self) -> StaticVarApiFlags { + unreachable!() + } + fn set_call_value_egld_handle(&self, _handle: RawHandle) { unreachable!() } diff --git a/framework/base/src/contract_base/wrappers/call_value_wrapper.rs b/framework/base/src/contract_base/wrappers/call_value_wrapper.rs index 3fb85e1658..1edb635a9d 100644 --- a/framework/base/src/contract_base/wrappers/call_value_wrapper.rs +++ b/framework/base/src/contract_base/wrappers/call_value_wrapper.rs @@ -3,7 +3,7 @@ use core::marker::PhantomData; use crate::{ api::{ const_handles, use_raw_handle, CallValueApi, CallValueApiImpl, ErrorApi, ErrorApiImpl, - HandleConstraints, ManagedTypeApi, StaticVarApiImpl, + ManagedTypeApi, StaticVarApiFlags, StaticVarApiImpl, }, err_msg, types::{ @@ -34,11 +34,10 @@ where /// Retrieves the EGLD call value from the VM. /// Will return 0 in case of an ESDT transfer (cannot have both EGLD and ESDT transfer simultaneously). pub fn egld_value(&self) -> ManagedRef<'static, A, BigUint> { - let mut call_value_handle: A::BigIntHandle = - use_raw_handle(A::static_var_api_impl().get_call_value_egld_handle()); - if call_value_handle == const_handles::UNINITIALIZED_HANDLE { - call_value_handle = use_raw_handle(const_handles::CALL_VALUE_EGLD); - A::static_var_api_impl().set_call_value_egld_handle(call_value_handle.get_raw_handle()); + let call_value_handle: A::BigIntHandle = use_raw_handle(const_handles::CALL_VALUE_EGLD); + if !A::static_var_api_impl() + .flag_is_set_or_update(StaticVarApiFlags::CALL_VALUE_EGLD_INITIALIZED) + { A::call_value_api_impl().load_egld_value(call_value_handle.clone()); } unsafe { ManagedRef::wrap_handle(call_value_handle) } @@ -55,12 +54,11 @@ where /// Will return 0 results if nothing was transfered, or just EGLD. /// Fully managed underlying types, very efficient. pub fn all_esdt_transfers(&self) -> ManagedRef<'static, A, ManagedVec>> { - let mut call_value_handle: A::ManagedBufferHandle = - use_raw_handle(A::static_var_api_impl().get_call_value_multi_esdt_handle()); - if call_value_handle == const_handles::UNINITIALIZED_HANDLE { - call_value_handle = use_raw_handle(const_handles::CALL_VALUE_MULTI_ESDT); - A::static_var_api_impl() - .set_call_value_multi_esdt_handle(call_value_handle.get_raw_handle()); + let call_value_handle: A::ManagedBufferHandle = + use_raw_handle(const_handles::CALL_VALUE_MULTI_ESDT); + if !A::static_var_api_impl() + .flag_is_set_or_update(StaticVarApiFlags::CALL_VALUE_MULTI_ESDT_INITIALIZED) + { A::call_value_api_impl().load_all_esdt_transfers(call_value_handle.clone()); } unsafe { ManagedRef::wrap_handle(call_value_handle) } diff --git a/framework/scenario/src/api/local_api_vh/static_var_api_vh.rs b/framework/scenario/src/api/local_api_vh/static_var_api_vh.rs index e72ae45f74..07bef47bda 100644 --- a/framework/scenario/src/api/local_api_vh/static_var_api_vh.rs +++ b/framework/scenario/src/api/local_api_vh/static_var_api_vh.rs @@ -1,6 +1,6 @@ use crate::api::{VMHooksApi, VMHooksApiBackend}; use multiversx_sc::{ - api::{use_raw_handle, RawHandle, StaticVarApi, StaticVarApiImpl}, + api::{use_raw_handle, RawHandle, StaticVarApi, StaticVarApiFlags, StaticVarApiImpl}, types::LockableStaticBuffer, }; @@ -55,6 +55,16 @@ impl StaticVarApiImpl for VMHooksApi { self.with_static_data(|data| data.static_vars_cell.borrow().num_arguments) } + fn set_flags(&self, flags: StaticVarApiFlags) { + self.with_static_data(|data| { + data.static_vars_cell.borrow_mut().flags = flags; + }) + } + + fn get_flags(&self) -> StaticVarApiFlags { + self.with_static_data(|data| data.static_vars_cell.borrow().flags) + } + fn set_call_value_egld_handle(&self, handle: RawHandle) { self.with_static_data(|data| { data.static_vars_cell.borrow_mut().call_value_egld_handle = handle; diff --git a/framework/scenario/src/debug_executor/tx_static_vars.rs b/framework/scenario/src/debug_executor/tx_static_vars.rs index 88277694c4..bca543d8ba 100644 --- a/framework/scenario/src/debug_executor/tx_static_vars.rs +++ b/framework/scenario/src/debug_executor/tx_static_vars.rs @@ -1,13 +1,15 @@ -use multiversx_sc::api::{const_handles, RawHandle}; +use multiversx_sc::api::{const_handles, RawHandle, StaticVarApiFlags}; #[derive(Debug)] pub struct TxStaticVars { pub external_view_target_address_handle: RawHandle, pub next_handle: RawHandle, pub num_arguments: i32, + pub flags: StaticVarApiFlags, pub call_value_egld_handle: RawHandle, pub call_value_multi_esdt_handle: RawHandle, - //vec of true/false, true if bit from handle = scaling_start + index is not empty + + /// Vec of true/false, true if bit from handle = scaling_start + index is not empty pub scaling_factor_init: [bool; const_handles::SCALING_FACTOR_LENGTH], } @@ -20,6 +22,7 @@ impl Default for TxStaticVars { call_value_egld_handle: const_handles::UNINITIALIZED_HANDLE, call_value_multi_esdt_handle: const_handles::UNINITIALIZED_HANDLE, scaling_factor_init: [false; const_handles::SCALING_FACTOR_LENGTH], + flags: StaticVarApiFlags::NONE, } } } diff --git a/framework/wasm-adapter/src/api/managed_types/static_var_api_node.rs b/framework/wasm-adapter/src/api/managed_types/static_var_api_node.rs index 501d53d68f..b282449c94 100644 --- a/framework/wasm-adapter/src/api/managed_types/static_var_api_node.rs +++ b/framework/wasm-adapter/src/api/managed_types/static_var_api_node.rs @@ -1,5 +1,5 @@ use multiversx_sc::{ - api::{const_handles, RawHandle, StaticVarApi, StaticVarApiImpl}, + api::{const_handles, RawHandle, StaticVarApi, StaticVarApiFlags, StaticVarApiImpl}, types::LockableStaticBuffer, }; @@ -9,6 +9,7 @@ static mut STATIC_BUFFER: LockableStaticBuffer = LockableStaticBuffer::new(); static mut EXTERNAL_VIEW_TARGET_ADDRESS_HANDLE: i32 = 0; static mut NEXT_HANDLE: i32 = const_handles::NEW_HANDLE_START_FROM; static mut NUM_ARGUMENTS: i32 = 0; +static mut FLAGS: StaticVarApiFlags = StaticVarApiFlags::NONE; static mut CALL_VALUE_EGLD_HANDLE: i32 = const_handles::UNINITIALIZED_HANDLE; static mut CALL_VALUE_MULTI_ESDT_HANDLE: i32 = const_handles::UNINITIALIZED_HANDLE; static mut SCALING_FACTOR_INIT: [bool; const_handles::SCALING_FACTOR_LENGTH] = @@ -62,6 +63,16 @@ impl StaticVarApiImpl for VmApiImpl { unsafe { NUM_ARGUMENTS } } + fn set_flags(&self, flags: StaticVarApiFlags) { + unsafe { + FLAGS = flags; + } + } + + fn get_flags(&self) -> StaticVarApiFlags { + unsafe { FLAGS } + } + fn set_call_value_egld_handle(&self, handle: RawHandle) { unsafe { CALL_VALUE_EGLD_HANDLE = handle; From de2c6302d11ebe2c1cdd9dc25bf8bae863f3e964 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Sat, 14 Dec 2024 05:28:13 +0200 Subject: [PATCH 66/66] static api - removing old call value handle fields --- .../src/api/managed_types/static_var_api.rs | 8 ------ .../uncallable/static_var_api_uncallable.rs | 16 ----------- .../src/api/local_api_vh/static_var_api_vh.rs | 28 +------------------ .../src/debug_executor/tx_static_vars.rs | 4 --- .../api/managed_types/static_var_api_node.rs | 22 --------------- 5 files changed, 1 insertion(+), 77 deletions(-) diff --git a/framework/base/src/api/managed_types/static_var_api.rs b/framework/base/src/api/managed_types/static_var_api.rs index 8fd007d648..9044cd6b06 100644 --- a/framework/base/src/api/managed_types/static_var_api.rs +++ b/framework/base/src/api/managed_types/static_var_api.rs @@ -40,14 +40,6 @@ pub trait StaticVarApiImpl { contains_flag } - fn set_call_value_egld_handle(&self, handle: RawHandle); - - fn get_call_value_egld_handle(&self) -> RawHandle; - - fn set_call_value_multi_esdt_handle(&self, handle: RawHandle); - - fn get_call_value_multi_esdt_handle(&self) -> RawHandle; - fn is_scaling_factor_cached(&self, decimals: usize) -> bool; fn set_scaling_factor_cached(&self, decimals: usize); diff --git a/framework/base/src/api/uncallable/static_var_api_uncallable.rs b/framework/base/src/api/uncallable/static_var_api_uncallable.rs index db126b337d..005a302e53 100644 --- a/framework/base/src/api/uncallable/static_var_api_uncallable.rs +++ b/framework/base/src/api/uncallable/static_var_api_uncallable.rs @@ -49,22 +49,6 @@ impl StaticVarApiImpl for UncallableApi { unreachable!() } - fn set_call_value_egld_handle(&self, _handle: RawHandle) { - unreachable!() - } - - fn get_call_value_egld_handle(&self) -> RawHandle { - unreachable!() - } - - fn set_call_value_multi_esdt_handle(&self, _handle: RawHandle) { - unreachable!() - } - - fn get_call_value_multi_esdt_handle(&self) -> RawHandle { - unreachable!() - } - fn is_scaling_factor_cached(&self, _decimals: usize) -> bool { unreachable!() } diff --git a/framework/scenario/src/api/local_api_vh/static_var_api_vh.rs b/framework/scenario/src/api/local_api_vh/static_var_api_vh.rs index 07bef47bda..903094279e 100644 --- a/framework/scenario/src/api/local_api_vh/static_var_api_vh.rs +++ b/framework/scenario/src/api/local_api_vh/static_var_api_vh.rs @@ -1,6 +1,6 @@ use crate::api::{VMHooksApi, VMHooksApiBackend}; use multiversx_sc::{ - api::{use_raw_handle, RawHandle, StaticVarApi, StaticVarApiFlags, StaticVarApiImpl}, + api::{RawHandle, StaticVarApi, StaticVarApiFlags, StaticVarApiImpl}, types::LockableStaticBuffer, }; @@ -65,32 +65,6 @@ impl StaticVarApiImpl for VMHooksApi { self.with_static_data(|data| data.static_vars_cell.borrow().flags) } - fn set_call_value_egld_handle(&self, handle: RawHandle) { - self.with_static_data(|data| { - data.static_vars_cell.borrow_mut().call_value_egld_handle = handle; - }) - } - - fn get_call_value_egld_handle(&self) -> RawHandle { - self.with_static_data(|data| { - use_raw_handle(data.static_vars_cell.borrow().call_value_egld_handle) - }) - } - - fn set_call_value_multi_esdt_handle(&self, handle: RawHandle) { - self.with_static_data(|data| { - data.static_vars_cell - .borrow_mut() - .call_value_multi_esdt_handle = handle; - }) - } - - fn get_call_value_multi_esdt_handle(&self) -> RawHandle { - self.with_static_data(|data| { - use_raw_handle(data.static_vars_cell.borrow().call_value_multi_esdt_handle) - }) - } - fn is_scaling_factor_cached(&self, decimals: usize) -> bool { self.with_static_data(|data| data.static_vars_cell.borrow().scaling_factor_init[decimals]) } diff --git a/framework/scenario/src/debug_executor/tx_static_vars.rs b/framework/scenario/src/debug_executor/tx_static_vars.rs index bca543d8ba..3468d14da6 100644 --- a/framework/scenario/src/debug_executor/tx_static_vars.rs +++ b/framework/scenario/src/debug_executor/tx_static_vars.rs @@ -6,8 +6,6 @@ pub struct TxStaticVars { pub next_handle: RawHandle, pub num_arguments: i32, pub flags: StaticVarApiFlags, - pub call_value_egld_handle: RawHandle, - pub call_value_multi_esdt_handle: RawHandle, /// Vec of true/false, true if bit from handle = scaling_start + index is not empty pub scaling_factor_init: [bool; const_handles::SCALING_FACTOR_LENGTH], @@ -19,8 +17,6 @@ impl Default for TxStaticVars { external_view_target_address_handle: 0, next_handle: const_handles::NEW_HANDLE_START_FROM, num_arguments: -1, - call_value_egld_handle: const_handles::UNINITIALIZED_HANDLE, - call_value_multi_esdt_handle: const_handles::UNINITIALIZED_HANDLE, scaling_factor_init: [false; const_handles::SCALING_FACTOR_LENGTH], flags: StaticVarApiFlags::NONE, } diff --git a/framework/wasm-adapter/src/api/managed_types/static_var_api_node.rs b/framework/wasm-adapter/src/api/managed_types/static_var_api_node.rs index b282449c94..3c6b19996f 100644 --- a/framework/wasm-adapter/src/api/managed_types/static_var_api_node.rs +++ b/framework/wasm-adapter/src/api/managed_types/static_var_api_node.rs @@ -10,8 +10,6 @@ static mut EXTERNAL_VIEW_TARGET_ADDRESS_HANDLE: i32 = 0; static mut NEXT_HANDLE: i32 = const_handles::NEW_HANDLE_START_FROM; static mut NUM_ARGUMENTS: i32 = 0; static mut FLAGS: StaticVarApiFlags = StaticVarApiFlags::NONE; -static mut CALL_VALUE_EGLD_HANDLE: i32 = const_handles::UNINITIALIZED_HANDLE; -static mut CALL_VALUE_MULTI_ESDT_HANDLE: i32 = const_handles::UNINITIALIZED_HANDLE; static mut SCALING_FACTOR_INIT: [bool; const_handles::SCALING_FACTOR_LENGTH] = [false; const_handles::SCALING_FACTOR_LENGTH]; @@ -73,26 +71,6 @@ impl StaticVarApiImpl for VmApiImpl { unsafe { FLAGS } } - fn set_call_value_egld_handle(&self, handle: RawHandle) { - unsafe { - CALL_VALUE_EGLD_HANDLE = handle; - } - } - - fn get_call_value_egld_handle(&self) -> RawHandle { - unsafe { CALL_VALUE_EGLD_HANDLE } - } - - fn set_call_value_multi_esdt_handle(&self, handle: RawHandle) { - unsafe { - CALL_VALUE_MULTI_ESDT_HANDLE = handle; - } - } - - fn get_call_value_multi_esdt_handle(&self) -> RawHandle { - unsafe { CALL_VALUE_MULTI_ESDT_HANDLE } - } - fn is_scaling_factor_cached(&self, decimals: usize) -> bool { unsafe { SCALING_FACTOR_INIT[decimals] } }