Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
pause125 committed Oct 26, 2023
1 parent 165ecc1 commit 308cdfd
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 34 deletions.
1 change: 0 additions & 1 deletion crates/rooch/src/commands/move_cli/commands/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ impl CommandAction<ExecuteTransactionResponseView> for Publish {
let empty_modules = modules.iter_modules_owned().is_empty();
let pkg_address = if !empty_modules {
let first_module = &modules.iter_modules_owned()[0];
println!("compiled module: {:?}", first_module.clone());
first_module.self_id().address().to_owned()
} else {
return Err(RoochError::MoveCompilationError(format!(
Expand Down
55 changes: 44 additions & 11 deletions moveos/moveos-stdlib/moveos-stdlib/doc/move_module.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@
- [Function `sort_and_verify_modules`](#0x2_move_module_sort_and_verify_modules)
- [Function `check_comatibility`](#0x2_move_module_check_comatibility)
- [Function `binding_module_address`](#0x2_move_module_binding_module_address)
- [Function `binding_module_name`](#0x2_move_module_binding_module_name)
- [Function `replace_module_name`](#0x2_move_module_replace_module_name)
- [Function `replace_struct_name`](#0x2_move_module_replace_struct_name)
- [Function `request_init_functions`](#0x2_move_module_request_init_functions)
- [Function `replace_address_identifiers`](#0x2_move_module_replace_address_identifiers)
- [Function `replace_identifiers`](#0x2_move_module_replace_identifiers)
- [Function `replace_addresses_constant`](#0x2_move_module_replace_addresses_constant)
- [Function `replace_bytes_constant`](#0x2_move_module_replace_bytes_constant)


<pre><code><b>use</b> <a href="">0x1::string</a>;
<pre><code><b>use</b> <a href="">0x1::error</a>;
<b>use</b> <a href="">0x1::string</a>;
<b>use</b> <a href="">0x1::vector</a>;
</code></pre>

Expand Down Expand Up @@ -263,14 +265,14 @@ Binding given module's address to the new address

</details>

<a name="0x2_move_module_binding_module_name"></a>
<a name="0x2_move_module_replace_module_name"></a>

## Function `binding_module_name`
## Function `replace_module_name`

Binding given module's name to the new name
Replace given module's name to the new name


<pre><code><b>public</b> <b>fun</b> <a href="move_module.md#0x2_move_module_binding_module_name">binding_module_name</a>(modules: <a href="">vector</a>&lt;<a href="move_module.md#0x2_move_module_MoveModule">move_module::MoveModule</a>&gt;, old_name: <a href="_String">string::String</a>, new_name: <a href="_String">string::String</a>): <a href="">vector</a>&lt;<a href="move_module.md#0x2_move_module_MoveModule">move_module::MoveModule</a>&gt;
<pre><code><b>public</b> <b>fun</b> <a href="move_module.md#0x2_move_module_replace_module_name">replace_module_name</a>(modules: <a href="">vector</a>&lt;<a href="move_module.md#0x2_move_module_MoveModule">move_module::MoveModule</a>&gt;, old_names: <a href="">vector</a>&lt;<a href="_String">string::String</a>&gt;, new_names: <a href="">vector</a>&lt;<a href="_String">string::String</a>&gt;): <a href="">vector</a>&lt;<a href="move_module.md#0x2_move_module_MoveModule">move_module::MoveModule</a>&gt;
</code></pre>


Expand All @@ -279,20 +281,22 @@ Binding given module's name to the new name
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="move_module.md#0x2_move_module_binding_module_name">binding_module_name</a>(
<pre><code><b>public</b> <b>fun</b> <a href="move_module.md#0x2_move_module_replace_module_name">replace_module_name</a>(
modules: <a href="">vector</a>&lt;<a href="move_module.md#0x2_move_module_MoveModule">MoveModule</a>&gt;,
old_name: String,
new_name: String,
old_names: <a href="">vector</a>&lt;String&gt;,
new_names: <a href="">vector</a>&lt;String&gt;,
): <a href="">vector</a>&lt;<a href="move_module.md#0x2_move_module_MoveModule">MoveModule</a>&gt; {
<b>assert</b>!(
<a href="_length">vector::length</a>(&old_names) == <a href="_length">vector::length</a>(&new_names),
<a href="_invalid_argument">error::invalid_argument</a>(<a href="move_module.md#0x2_move_module_ErrorLengthNotMatch">ErrorLengthNotMatch</a>)
);
<b>let</b> bytes_vec = <a href="_empty">vector::empty</a>&lt;<a href="">vector</a>&lt;u8&gt;&gt;();
<b>let</b> i = 0u64;
<b>let</b> len = <a href="_length">vector::length</a>(&modules);
<b>while</b> (i &lt; len) {
<a href="_push_back">vector::push_back</a>(&<b>mut</b> bytes_vec, <a href="_pop_back">vector::pop_back</a>(&<b>mut</b> modules).byte_codes);
i = i + 1;
};
<b>let</b> old_names = <a href="_singleton">vector::singleton</a>(old_name);
<b>let</b> new_names = <a href="_singleton">vector::singleton</a>(new_name);

<b>let</b> rebinded_bytes = <a href="move_module.md#0x2_move_module_replace_identifiers">replace_identifiers</a>(bytes_vec, old_names, new_names);
<b>let</b> rebinded_modules = <a href="_empty">vector::empty</a>&lt;<a href="move_module.md#0x2_move_module_MoveModule">MoveModule</a>&gt;();
Expand All @@ -311,6 +315,35 @@ Binding given module's name to the new name



</details>

<a name="0x2_move_module_replace_struct_name"></a>

## Function `replace_struct_name`

Replace given struct's name to the new name


<pre><code><b>public</b> <b>fun</b> <a href="move_module.md#0x2_move_module_replace_struct_name">replace_struct_name</a>(modules: <a href="">vector</a>&lt;<a href="move_module.md#0x2_move_module_MoveModule">move_module::MoveModule</a>&gt;, old_names: <a href="">vector</a>&lt;<a href="_String">string::String</a>&gt;, new_names: <a href="">vector</a>&lt;<a href="_String">string::String</a>&gt;): <a href="">vector</a>&lt;<a href="move_module.md#0x2_move_module_MoveModule">move_module::MoveModule</a>&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="move_module.md#0x2_move_module_replace_struct_name">replace_struct_name</a>(
modules: <a href="">vector</a>&lt;<a href="move_module.md#0x2_move_module_MoveModule">MoveModule</a>&gt;,
old_names: <a href="">vector</a>&lt;String&gt;,
new_names: <a href="">vector</a>&lt;String&gt;,
): <a href="">vector</a>&lt;<a href="move_module.md#0x2_move_module_MoveModule">MoveModule</a>&gt; {
<a href="move_module.md#0x2_move_module_replace_module_name">replace_module_name</a>(modules, old_names, new_names)
}
</code></pre>



</details>

<a name="0x2_move_module_request_init_functions"></a>
Expand Down
24 changes: 18 additions & 6 deletions moveos/moveos-stdlib/moveos-stdlib/sources/move_module.move
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/// `move_module` provides some basic functions for handle Move module in Move.
module moveos_std::move_module {
use std::vector;
use std::error;
use std::string::String;

friend moveos_std::account_storage;
Expand Down Expand Up @@ -85,21 +86,23 @@ module moveos_std::move_module {
rebinded_modules
}

/// Binding given module's name to the new name
public fun binding_module_name(
/// Replace given module's name to the new name
public fun replace_module_name(
modules: vector<MoveModule>,
old_name: String,
new_name: String,
old_names: vector<String>,
new_names: vector<String>,
): vector<MoveModule> {
assert!(
vector::length(&old_names) == vector::length(&new_names),
error::invalid_argument(ErrorLengthNotMatch)
);
let bytes_vec = vector::empty<vector<u8>>();
let i = 0u64;
let len = vector::length(&modules);
while (i < len) {
vector::push_back(&mut bytes_vec, vector::pop_back(&mut modules).byte_codes);
i = i + 1;
};
let old_names = vector::singleton(old_name);
let new_names = vector::singleton(new_name);

let rebinded_bytes = replace_identifiers(bytes_vec, old_names, new_names);
let rebinded_modules = vector::empty<MoveModule>();
Expand All @@ -115,6 +118,15 @@ module moveos_std::move_module {
rebinded_modules
}

/// Replace given struct's name to the new name
public fun replace_struct_name(
modules: vector<MoveModule>,
old_names: vector<String>,
new_names: vector<String>,
): vector<MoveModule> {
replace_module_name(modules, old_names, new_names)
}

native fun module_name_inner(byte_codes: &vector<u8>): String;

/// Sort modules by dependency order and then verify.
Expand Down
24 changes: 8 additions & 16 deletions moveos/moveos-stdlib/src/natives/moveos_stdlib/move_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,23 +578,15 @@ fn module_replace_constant_bytes(
.with_message("cannot deserialize constant".to_string())
})?;

// match constant_value {
// MoveValue::Vector(vals) => MoveValue::vec_to_vec_u8(vals)?,
// }
if let MoveValue::Vector(vals) = constant_value {
match MoveValue::vec_to_vec_u8(vals) {
Ok(bytes) => {
if let Some(new_bytes) = bytes_mapping.get(&bytes) {
constant.data = MoveValue::vector_u8(new_bytes.clone())
.simple_serialize()
.ok_or_else(|| {
PartialVMError::new(StatusCode::VALUE_SERIALIZATION_ERROR)
.with_message("cannot serialize constant".to_string())
})?;
}
}
Err(_) => {
// Inner type is not u8, just pass
if let Ok(bytes) = MoveValue::vec_to_vec_u8(vals) {
if let Some(new_bytes) = bytes_mapping.get(&bytes) {
constant.data = MoveValue::vector_u8(new_bytes.clone())
.simple_serialize()
.ok_or_else(|| {
PartialVMError::new(StatusCode::VALUE_SERIALIZATION_ERROR)
.with_message("cannot serialize constant".to_string())
})?;
}
}
}
Expand Down

0 comments on commit 308cdfd

Please sign in to comment.