Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

external module #132

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions external/Move.toml
Original file line number Diff line number Diff line change
@@ -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 = "_"
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module self::u256 {
module external::u256 {
// U256.
//=================================================================================================================

Expand Down Expand Up @@ -99,7 +99,7 @@ module self::u256 {
}
}

use self::utiles::split_u128;
use external::u64::split_u128;


/// Returns a `U256` from `u128` value.
Expand Down Expand Up @@ -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;

Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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
}

Expand Down Expand Up @@ -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())) {
Expand All @@ -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())) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module self::utiles {
module external::u64 {
/// Max `u64` value.
const U64_MAX: u128 = 18446744073709551615;

Expand Down
1 change: 1 addition & 0 deletions translator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"
eth = { path = "eth" }
mv = { path = "mv" }
intrinsic = { path = "intrinsic" }
external = { path = "external" }

anyhow.workspace = true
ethabi.workspace = true
Expand Down
96 changes: 90 additions & 6 deletions translator/intrinsic/build_helper/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand All @@ -199,6 +196,15 @@ pub(crate) fn generate_table(paths: &Paths) -> Result<()> {
.collect::<Result<Vec<String>>>()?
.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: {:?}",
Expand All @@ -213,6 +219,84 @@ pub(crate) fn generate_table(paths: &Paths) -> Result<()> {
Ok(())
}

fn get_enum_code_external(template_mv: &CompiledModule, data: EnumType) -> Result<String> {
let name = data.name();
let table = data.talbe_ref();

let enum_body = table
.iter()
.map(|(name, ..)| format!("{name},"))
.collect::<Vec<String>>()
.join("\n ");

let enum_fn_name = table
.iter()
.map(|(name, move_fn)| format!("Self::{name} => \"{move_fn}\","))
.collect::<Vec<String>>()
.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::<Result<Vec<String>>>()?
.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<String> {
let name = data.name();
let table = data.talbe_ref();
Expand Down
2 changes: 2 additions & 0 deletions translator/intrinsic/mv/Move.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -13,3 +14,4 @@ self = "0x42"

test_token_admin = "0x43417434fd869edee76cca2a4d2301e528a1551b1d719b75c350c3c97d15b8b9"
test_account = "0x15"
external = "0x242eb66e8fadeb0aadb7027eb2aa78613f4cf903dcd68a798f04de4d123c135a"
2 changes: 1 addition & 1 deletion translator/intrinsic/mv/sources/info.move
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions translator/intrinsic/mv/sources/memory.move
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions translator/intrinsic/mv/sources/persist.move
Original file line number Diff line number Diff line change
Expand Up @@ -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<U256, U256>,
Expand Down