From 19283bdfba6a4be0b0f21e0bbf1f03ccf828c93d Mon Sep 17 00:00:00 2001 From: steph-mipt Date: Mon, 5 Dec 2022 10:16:07 +0300 Subject: [PATCH] external module without deploy trought translator --- external/Move.toml | 12 +++ .../mv => external}/sources/u256.move | 38 ++++---- .../mv => external}/sources/u512.move | 8 +- .../mv => external}/sources/u64.move | 2 +- translator/Cargo.toml | 1 + translator/intrinsic/build_helper/table.rs | 96 +++++++++++++++++-- translator/intrinsic/mv/Move.toml | 2 + translator/intrinsic/mv/sources/info.move | 2 +- translator/intrinsic/mv/sources/memory.move | 4 +- translator/intrinsic/mv/sources/persist.move | 4 +- 10 files changed, 134 insertions(+), 35 deletions(-) create mode 100644 external/Move.toml rename {translator/intrinsic/mv => external}/sources/u256.move (96%) rename {translator/intrinsic/mv => external}/sources/u512.move (97%) rename {translator/intrinsic/mv => external}/sources/u64.move (98%) diff --git a/external/Move.toml b/external/Move.toml new file mode 100644 index 00000000..3cfb0eb8 --- /dev/null +++ b/external/Move.toml @@ -0,0 +1,12 @@ +[package] +name = "EthMathTypes" +version = "1.0.0" + +[dependencies] +[dependencies.AptosFramework] +git = 'https://github.com/aptos-labs/aptos-core.git' +rev = 'main' +subdir = 'aptos-move/framework/aptos-framework' + +[addresses] +external = "_" \ No newline at end of file diff --git a/translator/intrinsic/mv/sources/u256.move b/external/sources/u256.move similarity index 96% rename from translator/intrinsic/mv/sources/u256.move rename to external/sources/u256.move index 61048d7f..413dd72d 100644 --- a/translator/intrinsic/mv/sources/u256.move +++ b/external/sources/u256.move @@ -1,4 +1,4 @@ -module self::u256 { +module external::u256 { // U256. //================================================================================================================= @@ -99,7 +99,7 @@ module self::u256 { } } - use self::utiles::split_u128; + use external::u64::split_u128; /// Returns a `U256` from `u128` value. @@ -127,7 +127,7 @@ module self::u256 { } // API - fun from_address(addr: address): U256 { + public fun from_address(addr: address): U256 { let encoded = std::bcs::to_bytes(&addr); let i = 0u64; @@ -150,7 +150,7 @@ module self::u256 { } } - fun from_u64s(v0: u64, v1: u64, v2: u64, v3: u64): U256 { + public fun from_u64s(v0: u64, v1: u64, v2: u64, v3: u64): U256 { return U256 { v0, v1, @@ -179,7 +179,7 @@ module self::u256 { } // API - fun to_bool(a: U256): bool { + public fun to_bool(a: U256): bool { if (a.v0 == 0 && a.v1 == 0 && a.v2 == 0 && a.v3 == 0) { false } else { @@ -406,7 +406,7 @@ module self::u256 { ret } - use self::utiles::leading_zeros_u64; + use external::u64::leading_zeros_u64; // Private functions. /// Get bits used to store `a`. @@ -447,22 +447,22 @@ module self::u256 { } // API - fun lt(a: U256, b: U256): bool { + public fun lt(a: U256, b: U256): bool { compare(&a, &b) == LESS_THAN } // API - fun le(a: U256, b: U256): bool { + public fun le(a: U256, b: U256): bool { compare(&a, &b) != GREATER_THAN } // API - fun gt(a: U256, b: U256): bool { + public fun gt(a: U256, b: U256): bool { compare(&a, &b) == GREATER_THAN } // API - fun ge(a: U256, b: U256): bool { + public fun ge(a: U256, b: U256): bool { compare(&a, &b) != LESS_THAN } @@ -519,7 +519,7 @@ module self::u256 { EQUAL } - use self::utiles::overflowing_add_u64; + use external::u64::overflowing_add_u64; // API /// Adds two `U256` and returns sum. @@ -560,7 +560,7 @@ module self::u256 { ret } - use self::u512::{zero_d, get_d, put_d}; + use external::u512::{zero_d, get_d, put_d}; // API /// Multiples two `U256`. @@ -620,7 +620,7 @@ module self::u256 { ret } - use self::utiles::overflowing_sub_u64; + use external::u64::overflowing_sub_u64; // API /// Subtracts two `U256`, returns result. @@ -828,7 +828,7 @@ module self::u256 { } // API - fun shr(a: U256, shift: U256): U256 { + public fun shr(a: U256, shift: U256): U256 { let ret = zero(); let (shift, o) = as_u128_safe(shift); @@ -840,7 +840,7 @@ module self::u256 { } // API - fun shl(a: U256, shift: U256): U256 { + public fun shl(a: U256, shift: U256): U256 { let ret = zero(); let (shift, o) = as_u128_safe(shift); @@ -852,7 +852,7 @@ module self::u256 { } // API - fun is_zero(a: U256): bool { + public fun is_zero(a: U256): bool { a.v0 == 0 && a.v1 == 0 && a.v2 == 0 && a.v3 == 0 } @@ -897,7 +897,7 @@ module self::u256 { *std::vector::borrow_mut(vec, offset + 7) = ((a & 0xFF) as u8); } - use self::u512::overflowing_add_d; + use external::u512::overflowing_add_d; public fun add_mod(a: U256, b: U256, mod: U256): U256 { if (eq(mod, zero())) { @@ -920,7 +920,7 @@ module self::u256 { } } - use self::u512::mod_d; + use external::u512::mod_d; public fun mul_mod(a: U256, b: U256, mod: U256): U256 { if (eq(mod, zero())) { @@ -940,7 +940,7 @@ module self::u256 { } - use self::u512::{U512, new_u512}; + use external::u512::{U512, new_u512}; /// Convert `U512` to `U256`. public fun u512_to_u256(a: U512): (U256, bool) { diff --git a/translator/intrinsic/mv/sources/u512.move b/external/sources/u512.move similarity index 97% rename from translator/intrinsic/mv/sources/u512.move rename to external/sources/u512.move index cf16e420..359d2c31 100644 --- a/translator/intrinsic/mv/sources/u512.move +++ b/external/sources/u512.move @@ -1,4 +1,4 @@ -module self::u512 { +module external::u512 { // Errors. /// When trying to get or put word into U256 but it's out of index. const EWORDS_OVERFLOW: u64 = 1; @@ -126,7 +126,7 @@ module self::u512 { EQUAL } - use self::utiles::overflowing_add_u64; + use external::u64::overflowing_add_u64; /// Add two `U512`, returns result. public fun overflowing_add_d(a: U512, b: U512): U512 { @@ -166,7 +166,7 @@ module self::u512 { ret } - use self::utiles::overflowing_sub_u64; + use external::u64::overflowing_sub_u64; /// Subtracts two `U512`, returns result. public fun overflowing_sub_d(a: U512, b: U512): U512 { @@ -240,7 +240,7 @@ module self::u512 { a } - use self::utiles::leading_zeros_u64; + use external::u64::leading_zeros_u64; /// Get bits used to store `a` in U512. public fun bits_d(a: &U512): u64 { diff --git a/translator/intrinsic/mv/sources/u64.move b/external/sources/u64.move similarity index 98% rename from translator/intrinsic/mv/sources/u64.move rename to external/sources/u64.move index 292ad28f..35d8bae0 100644 --- a/translator/intrinsic/mv/sources/u64.move +++ b/external/sources/u64.move @@ -1,4 +1,4 @@ -module self::utiles { +module external::u64 { /// Max `u64` value. const U64_MAX: u128 = 18446744073709551615; diff --git a/translator/Cargo.toml b/translator/Cargo.toml index 8dbe8b4c..6dfbbd20 100644 --- a/translator/Cargo.toml +++ b/translator/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" eth = { path = "eth" } mv = { path = "mv" } intrinsic = { path = "intrinsic" } +external = { path = "external" } anyhow.workspace = true ethabi.workspace = true diff --git a/translator/intrinsic/build_helper/table.rs b/translator/intrinsic/build_helper/table.rs index ccb2e6ae..6bb6380a 100644 --- a/translator/intrinsic/build_helper/table.rs +++ b/translator/intrinsic/build_helper/table.rs @@ -2,7 +2,9 @@ use std::fs; use anyhow::{anyhow, Result}; use move_binary_format::access::ModuleAccess; -use move_binary_format::file_format::{ConstantPoolIndex, SignatureToken}; +use move_binary_format::file_format::{ + ConstantPoolIndex, SignatureToken, StructHandleIndex, TableIndex, +}; use move_binary_format::CompiledModule; use move_core_types::account_address::AccountAddress; use move_core_types::identifier::IdentStr; @@ -184,11 +186,6 @@ pub(crate) fn generate_table(paths: &Paths) -> Result<()> { mut_token: true, table: PERSIST_TABLE.to_vec(), }, - EnumType::Structure { - name: "U256", - mut_token: false, - table: U256_TABLE.to_vec(), - }, EnumType::Module { name: "Info", table: INFO_TABLE.to_vec(), @@ -199,6 +196,15 @@ pub(crate) fn generate_table(paths: &Paths) -> Result<()> { .collect::>>()? .join("\n"); + result += &get_enum_code_external( + &template, + EnumType::Structure { + name: "U256", + mut_token: false, + table: U256_TABLE.to_vec(), + }, + )?; + let index = find_address_const(&template, SELF_ADDRESS).ok_or_else(|| { anyhow!( "Can't find self address index in template module: {:?}", @@ -213,6 +219,84 @@ pub(crate) fn generate_table(paths: &Paths) -> Result<()> { Ok(()) } +fn get_enum_code_external(template_mv: &CompiledModule, data: EnumType) -> Result { + let name = data.name(); + let table = data.talbe_ref(); + + let enum_body = table + .iter() + .map(|(name, ..)| format!("{name},")) + .collect::>() + .join("\n "); + + let enum_fn_name = table + .iter() + .map(|(name, move_fn)| format!("Self::{name} => \"{move_fn}\",")) + .collect::>() + .join("\n "); + + let mut reserved_handler_index = template_mv.function_handles.len() - 1; + let enum_fn_handler = table + .iter() + .map(|(name, move_fn)| { + let fn_index = match find_function_index(template_mv, move_fn) { + Some(idx) => idx, + None => { + reserved_handler_index += 1; + reserved_handler_index + } + }; + + Ok(format!("Self::{name} => FunctionHandleIndex({fn_index}),")) + }) + .collect::>>()? + .join("\n "); + + let mut result = TEMPLATE_ENUM + .replace("###ENUM_NAME###", name) + .replace("###ENUM_BODY###", &enum_body) + .replace("###ENUM_FN_NAME###", &enum_fn_name) + .replace("###ENUM_FN_HANDLER###", &enum_fn_handler); + + // structure + if let EnumType::Structure { mut_token, .. } = data { + let structure_handle_index = match template_mv + .struct_handles + .iter() + .position(|h| template_mv.identifier_at(h.name).as_str() == name) + { + Some(pos) => StructHandleIndex(pos as TableIndex), + None => StructHandleIndex(template_mv.struct_handles.len() as TableIndex), + }; + + let enum_index_handle = structure_handle_index.to_string(); + let enum_index_definition = match template_mv + .struct_defs + .iter() + .position(|item| item.struct_handle == structure_handle_index) + { + Some(pos) => pos.to_string(), + None => template_mv.struct_defs.len().to_string(), + }; + + result += TEMPLATE_ENUM_STRUCTURE + .replace("###ENUM_NAME###", name) + .replace( + "###ENUM_INDEX_TOKEN###", + if mut_token { + TEMPLATE_ENUM_TOKEN_MUT + } else { + TEMPLATE_ENUM_TOKEN + }, + ) + .replace("###ENUM_INDEX_HANDLE###", &enum_index_handle) + .replace("###ENUM_INDEX_DEFINITION###", &enum_index_definition) + .as_str(); + } + + Ok(result) +} + fn gen_enum_code(template_mv: &CompiledModule, data: EnumType) -> Result { let name = data.name(); let table = data.talbe_ref(); diff --git a/translator/intrinsic/mv/Move.toml b/translator/intrinsic/mv/Move.toml index 541c8439..290c74fb 100644 --- a/translator/intrinsic/mv/Move.toml +++ b/translator/intrinsic/mv/Move.toml @@ -3,6 +3,7 @@ name = "intrinsic" version = "0.0.1" [dependencies] +EthMathTypes = { local = "../../external/" } [dependencies.AptosFramework] git = 'https://github.com/aptos-labs/aptos-core.git' rev = 'main' @@ -13,3 +14,4 @@ self = "0x42" test_token_admin = "0x43417434fd869edee76cca2a4d2301e528a1551b1d719b75c350c3c97d15b8b9" test_account = "0x15" +external = "0x242eb66e8fadeb0aadb7027eb2aa78613f4cf903dcd68a798f04de4d123c135a" diff --git a/translator/intrinsic/mv/sources/info.move b/translator/intrinsic/mv/sources/info.move index 6f60f6ea..b503a92a 100644 --- a/translator/intrinsic/mv/sources/info.move +++ b/translator/intrinsic/mv/sources/info.move @@ -4,7 +4,7 @@ module self::info { use aptos_framework::aptos_coin::AptosCoin; use aptos_framework::account; - use self::u256::{U256, from_u64, to_address}; + use external::u256::{from_u64, to_address}; /// AptosCoin - balance diff --git a/translator/intrinsic/mv/sources/memory.move b/translator/intrinsic/mv/sources/memory.move index 6c50abcf..3e139357 100644 --- a/translator/intrinsic/mv/sources/memory.move +++ b/translator/intrinsic/mv/sources/memory.move @@ -2,9 +2,9 @@ module self::memory { // Memory. //================================================================================================================= - use self::u256::{U256, new_u256, from_bytes, get, zero, as_u64, from_u128}; + use external::u256::{new_u256, from_bytes, get, as_u64, from_u128}; - use self::utiles::split_u128; + use external::u64::split_u128; const ELENGTH: u64 = 0x1; const OUT_OF_MEMORY: u64 = 0x2; diff --git a/translator/intrinsic/mv/sources/persist.move b/translator/intrinsic/mv/sources/persist.move index ab70821d..4cf71122 100644 --- a/translator/intrinsic/mv/sources/persist.move +++ b/translator/intrinsic/mv/sources/persist.move @@ -2,11 +2,11 @@ module self::persist { // Storage. //================================================================================================================= - use self::u256::{U256, zero}; + use external::u256::{U256, zero}; use self::memory::{Memory, mslice}; #[test_only] - use self::u256::{from_u128, as_u128}; + use external::u256::{from_u128, as_u128}; struct Persist has store, key { tbl: aptos_std::table::Table,