Skip to content

Commit

Permalink
Merge pull request #1777 from multiversx/vm-core
Browse files Browse the repository at this point in the history
chain-core crate
  • Loading branch information
andrei-marinica authored Sep 30, 2024
2 parents c5db177 + 5b6fee0 commit 647a571
Show file tree
Hide file tree
Showing 61 changed files with 1,123 additions and 1,051 deletions.
219 changes: 86 additions & 133 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ members = [
# "tools/plotter",
"tools/interactor-system-func-calls/",

"vm-core",
"vm",

"contracts/modules",
Expand Down
4 changes: 4 additions & 0 deletions framework/base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ path = "../derive"
version = "=0.21.0"
path = "../../data/codec"
features = ["derive"]

[dependencies.multiversx-chain-core]
version = "=0.10.0"
path = "../../vm-core"
1 change: 1 addition & 0 deletions framework/base/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod type_abi;
mod type_abi_from;
mod type_abi_impl_basic;
mod type_abi_impl_codec_multi;
mod type_abi_impl_vm_core;
mod type_description;
mod type_description_container;

Expand Down
204 changes: 204 additions & 0 deletions framework/base/src/abi/type_abi_impl_vm_core.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
use super::*;

use alloc::vec::Vec;
use multiversx_chain_core::types::{
Address, BoxedBytes, CodeMetadata, EsdtLocalRole, EsdtTokenType, H256,
};

impl TypeAbiFrom<Self> for H256 {}

impl TypeAbi for H256 {
type Unmanaged = Self;

fn type_name() -> TypeName {
"H256".into()
}

fn type_name_rust() -> TypeName {
"H256".into()
}
}

impl TypeAbiFrom<Self> for Address {}

impl TypeAbi for Address {
type Unmanaged = Self;

fn type_name() -> TypeName {
"Address".into()
}

fn type_name_rust() -> TypeName {
"Address".into()
}
}

impl TypeAbiFrom<Self> for BoxedBytes {}

impl TypeAbi for BoxedBytes {
type Unmanaged = Self;

fn type_name() -> TypeName {
"bytes".into()
}

fn type_name_rust() -> TypeName {
"BoxedBytes".into()
}
}

impl TypeAbiFrom<Self> for CodeMetadata {}
impl TypeAbi for CodeMetadata {
type Unmanaged = Self;

fn type_name() -> TypeName {
"CodeMetadata".into()
}

fn type_name_rust() -> TypeName {
"CodeMetadata".into()
}
}

impl TypeAbiFrom<Self> for EsdtTokenType {}
impl TypeAbiFrom<&Self> for EsdtTokenType {}

// implementation originally geneated via #[type_abi] attribute
impl TypeAbi for EsdtTokenType {
type Unmanaged = Self;
fn type_name() -> TypeName {
"EsdtTokenType".into()
}

#[allow(clippy::vec_init_then_push)]
fn provide_type_descriptions<TDC: TypeDescriptionContainer>(accumulator: &mut TDC) {
let type_names = Self::type_names();
if !accumulator.contains_type(&type_names.abi) {
accumulator.reserve_type_name(type_names.clone());
let mut variant_descriptions = Vec::new();
variant_descriptions.push(EnumVariantDescription::new(
&[],
"Fungible",
0usize,
Vec::new(),
));
variant_descriptions.push(EnumVariantDescription::new(
&[],
"NonFungible",
1usize,
Vec::new(),
));
variant_descriptions.push(EnumVariantDescription::new(
&[],
"SemiFungible",
2usize,
Vec::new(),
));
variant_descriptions.push(EnumVariantDescription::new(&[], "Meta", 3usize, Vec::new()));
variant_descriptions.push(EnumVariantDescription::new(
&[],
"Invalid",
4usize,
Vec::new(),
));
accumulator.insert(
type_names.clone(),
TypeDescription::new(
&[],
type_names,
TypeContents::Enum(variant_descriptions),
&[
"TopDecode",
"TopEncode",
"NestedDecode",
"NestedEncode",
"Clone",
"PartialEq",
"Eq",
"Debug",
"ManagedVecItem",
],
),
);
}
}
}

impl TypeAbiFrom<Self> for EsdtLocalRole {}
impl TypeAbiFrom<&Self> for EsdtLocalRole {}

// implementation originally geneated via #[type_abi] attribute
impl TypeAbi for EsdtLocalRole {
type Unmanaged = Self;

fn type_name() -> TypeName {
"EsdtLocalRole".into()
}

#[allow(clippy::vec_init_then_push)]
fn provide_type_descriptions<TDC: TypeDescriptionContainer>(accumulator: &mut TDC) {
let type_names = Self::type_names();
if !accumulator.contains_type(&type_names.abi) {
accumulator.reserve_type_name(type_names.clone());
let mut variant_descriptions = Vec::new();
variant_descriptions.push(EnumVariantDescription::new(&[], "None", 0usize, Vec::new()));
variant_descriptions.push(EnumVariantDescription::new(&[], "Mint", 1usize, Vec::new()));
variant_descriptions.push(EnumVariantDescription::new(&[], "Burn", 2usize, Vec::new()));
variant_descriptions.push(EnumVariantDescription::new(
&[],
"NftCreate",
3usize,
Vec::new(),
));
variant_descriptions.push(EnumVariantDescription::new(
&[],
"NftAddQuantity",
4usize,
Vec::new(),
));
variant_descriptions.push(EnumVariantDescription::new(
&[],
"NftBurn",
5usize,
Vec::new(),
));
variant_descriptions.push(EnumVariantDescription::new(
&[],
"NftAddUri",
6usize,
Vec::new(),
));
variant_descriptions.push(EnumVariantDescription::new(
&[],
"NftUpdateAttributes",
7usize,
Vec::new(),
));
variant_descriptions.push(EnumVariantDescription::new(
&[],
"Transfer",
8usize,
Vec::new(),
));
accumulator.insert(
type_names.clone(),
TypeDescription::new(
&[],
type_names,
TypeContents::Enum(variant_descriptions),
&[
"TopDecode",
"TopEncode",
"NestedDecode",
"NestedEncode",
"Clone",
"PartialEq",
"Eq",
"Debug",
"Copy",
],
),
);
}
}
}
2 changes: 1 addition & 1 deletion framework/base/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ pub use storage_api::*;
pub use vm_api::VMApi;

// Backwards compatibility.
pub use crate::types::system_proxy::builtin_func_names::*;
pub use crate::chain_core::builtin_func_names::*;
1 change: 1 addition & 0 deletions framework/base/src/formatter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod formatter_impl_bool;
mod formatter_impl_bytes;
mod formatter_impl_num;
mod formatter_impl_vm_core;
mod formatter_traits;
pub mod hex_util;

Expand Down
25 changes: 25 additions & 0 deletions framework/base/src/formatter/formatter_impl_vm_core.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use multiversx_chain_core::types::CodeMetadata;

use super::{hex_util, FormatByteReceiver, SCBinary, SCDisplay, SCLowerHex};

impl SCDisplay for CodeMetadata {
fn fmt<F: FormatByteReceiver>(&self, f: &mut F) {
self.for_each_string_token(|token| f.append_bytes(token.as_bytes()))
}
}

impl SCLowerHex for CodeMetadata {
fn fmt<F: FormatByteReceiver>(&self, f: &mut F) {
let num = self.bits().to_be_bytes();
f.append_bytes(&hex_util::byte_to_hex_digits(num[0])[..]);
f.append_bytes(&hex_util::byte_to_hex_digits(num[1])[..]);
}
}

impl SCBinary for CodeMetadata {
fn fmt<F: FormatByteReceiver>(&self, f: &mut F) {
let num = self.bits().to_be_bytes();
f.append_bytes(&hex_util::byte_to_binary_digits(num[0])[..]);
f.append_bytes(&hex_util::byte_to_binary_digits(num[1])[..]);
}
}
3 changes: 3 additions & 0 deletions framework/base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ extern crate alloc;
/// The current version of `multiversx_sc_codec`, re-exported.
pub use multiversx_sc_codec as codec;

// Re-exporting the VM-core, for convenience.
pub use multiversx_chain_core as chain_core;

/// Reexported for convenience.
pub use crate::codec::arrayvec;

Expand Down
7 changes: 5 additions & 2 deletions framework/base/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
mod crypto;
mod flags;
pub mod heap;
mod interaction;
mod io;
Expand All @@ -8,7 +7,6 @@ pub(crate) mod math_util;
mod static_buffer;

pub use crypto::*;
pub use flags::*;
pub use interaction::*;
pub use io::*;
pub use managed::*;
Expand All @@ -17,3 +15,8 @@ pub use static_buffer::*;
/// Only import the heap types in contracts when the "alloc" feature is on.
#[cfg(feature = "alloc")]
pub use heap::*;

pub use crate::chain_core::types::CodeMetadata;
pub use crate::chain_core::types::EsdtLocalRole;
pub use crate::chain_core::types::EsdtLocalRoleFlags;
pub use crate::chain_core::types::EsdtTokenType;
Loading

0 comments on commit 647a571

Please sign in to comment.