Skip to content

Commit

Permalink
feat: test mint bits
Browse files Browse the repository at this point in the history
  • Loading branch information
yubing744 committed May 27, 2024
1 parent 610c83c commit c26ef75
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 16 deletions.
36 changes: 36 additions & 0 deletions crates/testsuite/features/cmd.feature
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,42 @@ Feature: Rooch CLI integration tests
Then assert: "{{$.move[-1].vm_status}} == Executed"
Then assert: "{{$.move[-1].return_values[0].decoded_value.value.vec[0].value.is_valid}} == true"

# deploy
Then cmd bitseed: "deploy --fee-rate 1 --generator {{$.generator[-1].inscriptions[0].Id}} --tick bits --amount 210000000000 --deploy-args {"height":{"type":"range","data":{"min":1,"max":1000}}}"
Then assert: "'{{$.deploy[-1]}}' not_contains error"

# mine a block
Then cmd ord: "wallet receive"
Then cmd bitcoin-cli: "generatetoaddress 1 {{$.wallet[-1].address}}"
Then sleep: "10"

# Sync bitseed
Then cmd: "move run --function default::bitseed_runner::run"
Then assert: "{{$.move[-1].execution_info.status.type}} == executed"

# Check mint deploy validity
Then cmd: "move view --function 0xa::bitseed::view_validity --args string:{{$.deploy[-1].inscriptions[0].Id}} "
Then assert: "{{$.move[-1].vm_status}} == Executed"
Then assert: "{{$.move[-1].return_values[0].decoded_value.value.vec[0].value.is_valid}} == true"

# mint
#Then cmd bitseed: "mint --fee-rate 1 --deploy-inscription-id {{$.deploy[-1].inscriptions[0].Id}} --user-input hello_bitseed"
#Then assert: "'{{$.mint[-1]}}' not_contains error"

# mine a block
#Then cmd ord: "wallet receive"
#Then cmd bitcoin-cli: "generatetoaddress 1 {{$.wallet[-1].address}}"
#Then sleep: "10"

# Sync bitseed
#Then cmd: "move run --function default::bitseed_runner::run"
#Then assert: "{{$.move[-1].execution_info.status.type}} == executed"

# Check mint bits validity
#Then cmd: "move view --function 0xa::bitseed::view_validity --args string:{{$.deploy[-1].inscriptions[0].Id}} "
#Then assert: "{{$.move[-1].vm_status}} == Executed"
#Then assert: "{{$.move[-1].return_values[0].decoded_value.value.vec[0].value.is_valid}} == true"

# release servers
Then stop the server
Then stop the ord server
Expand Down
12 changes: 12 additions & 0 deletions frameworks/moveos-stdlib/doc/wasm.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- [Function `add_length_with_data`](#0x2_wasm_add_length_with_data)
- [Function `create_memory_wasm_args`](#0x2_wasm_create_memory_wasm_args)
- [Function `execute_wasm_function`](#0x2_wasm_execute_wasm_function)
- [Function `execute_wasm_function_option`](#0x2_wasm_execute_wasm_function_option)
- [Function `read_data_length`](#0x2_wasm_read_data_length)
- [Function `read_data_from_heap`](#0x2_wasm_read_data_from_heap)
- [Function `release_wasm_instance`](#0x2_wasm_release_wasm_instance)
Expand Down Expand Up @@ -112,6 +113,17 @@



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

## Function `execute_wasm_function_option`



<pre><code><b>public</b> <b>fun</b> <a href="wasm.md#0x2_wasm_execute_wasm_function_option">execute_wasm_function_option</a>(instance: &<b>mut</b> <a href="wasm.md#0x2_wasm_WASMInstance">wasm::WASMInstance</a>, func_name: <a href="">vector</a>&lt;u8&gt;, args: <a href="">vector</a>&lt;u64&gt;): <a href="_Option">option::Option</a>&lt;u64&gt;
</code></pre>



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

## Function `read_data_length`
Expand Down
18 changes: 16 additions & 2 deletions frameworks/moveos-stdlib/sources/wasm.move
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,21 @@ module moveos_std::wasm {

public fun execute_wasm_function(instance: &mut WASMInstance, func_name: vector<u8>, args: vector<u64>): u64 {
features::ensure_wasm_enabled();
native_execute_wasm_function(instance.id, func_name, args)

let (ret_val, error_code) = native_execute_wasm_function(instance.id, func_name, args);
assert!(error_code > 0, error_code);
ret_val
}

public fun execute_wasm_function_option(instance: &mut WASMInstance, func_name: vector<u8>, args: vector<u64>): Option<u64> {
features::ensure_wasm_enabled();

let (ret_val, error_code) = native_execute_wasm_function(instance.id, func_name, args);
if (error_code > 0) {
return option::none()
};

option::some(ret_val)
}

public fun read_data_length(instance: &WASMInstance, data_ptr: u64): u32 {
Expand All @@ -70,7 +84,7 @@ module moveos_std::wasm {

native fun native_create_wasm_args_in_memory(instance_id: u64, func_name: vector<u8>, args_bytes: vector<vector<u8>>): vector<u64>;

native fun native_execute_wasm_function(instance_id: u64, func_name: vector<u8>, args: vector<u64>): u64;
native fun native_execute_wasm_function(instance_id: u64, func_name: vector<u8>, args: vector<u64>): (u64, u64);

native fun native_read_data_length(instance_id: u64, data_ptr: u64): u32;

Expand Down
39 changes: 38 additions & 1 deletion frameworks/moveos-stdlib/src/natives/moveos_stdlib/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use log::debug;
use std::collections::VecDeque;
use std::ffi::CString;
use std::ops::Deref;
Expand All @@ -15,7 +16,7 @@ use move_vm_types::natives::function::NativeResult;
use move_vm_types::pop_arg;
use move_vm_types::values::{Struct, Value};
use serde_json::Value as JSONValue;
use smallvec::smallvec;
use smallvec::{smallvec, SmallVec};

use moveos_wasm::wasm::{
create_wasm_instance, get_instance_pool, insert_wasm_instance, put_data_on_stack,
Expand All @@ -41,6 +42,7 @@ pub const E_GET_INSTANCE_POOL_FAILED: u64 = 15;
pub const E_UNPACK_STRUCT_FAILED: u64 = 16;
pub const E_WASM_INSTANCE_CREATION_FAILED: u64 = 17;
pub const E_WASM_REMOVE_INSTANCE_FAILED: u64 = 18;
pub const E_VM_ERROR: u64 = 99;

#[derive(Debug, Clone)]
pub struct WASMCreateInstanceGasParameters {
Expand Down Expand Up @@ -347,6 +349,41 @@ fn build_err(cost: InternalGas, abort_code: u64) -> PartialVMResult<NativeResult
// native_execute_wasm_function
#[inline]
fn native_execute_wasm_function(
gas_params: &WASMExecuteGasParameters,
_context: &mut NativeContext,
_ty_args: Vec<Type>,
args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
let vm_result = execute_wasm_function_inner(gas_params, _context, _ty_args, args);
match vm_result {
PartialVMResult::Ok(native_result) => match native_result {
NativeResult::Success { cost, ret_vals } => {
let mut new_ret_vals: SmallVec<[Value; 1]> = ret_vals;
new_ret_vals.push(Value::u64(0));

Ok(NativeResult::Success {
cost,
ret_vals: new_ret_vals,
})
}
NativeResult::Abort { cost, abort_code } => Ok(NativeResult::Success {
cost,
ret_vals: smallvec![Value::u64(0), Value::u64(abort_code)],
}),
NativeResult::OutOfGas { partial_cost } => Ok(NativeResult::OutOfGas { partial_cost }),
},
PartialVMResult::Err(err) => {
debug!("execute_wasm_function_inner vm_error: {:?}", err);

Ok(NativeResult::Success {
cost: gas_params.base_create_execution,
ret_vals: smallvec![Value::u64(0), Value::u64(E_VM_ERROR)],
})
}
}
}

fn execute_wasm_function_inner(
gas_params: &WASMExecuteGasParameters,
_context: &mut NativeContext,
_ty_args: Vec<Type>,
Expand Down
15 changes: 15 additions & 0 deletions frameworks/rooch-nursery/doc/bitseed.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- [Resource `Bitseed`](#0xa_bitseed_Bitseed)
- [Struct `BitseedCoinInfo`](#0xa_bitseed_BitseedCoinInfo)
- [Resource `BitseedStore`](#0xa_bitseed_BitseedStore)
- [Struct `InscribeGenerateArgs`](#0xa_bitseed_InscribeGenerateArgs)
- [Constants](#@Constants_0)
- [Function `genesis_init`](#0xa_bitseed_genesis_init)
- [Function `bitseed_deploy_key`](#0xa_bitseed_bitseed_deploy_key)
Expand All @@ -27,12 +28,14 @@


<pre><code><b>use</b> <a href="">0x1::bcs</a>;
<b>use</b> <a href="">0x1::debug</a>;
<b>use</b> <a href="">0x1::option</a>;
<b>use</b> <a href="">0x1::string</a>;
<b>use</b> <a href="">0x1::vector</a>;
<b>use</b> <a href="">0x2::address</a>;
<b>use</b> <a href="">0x2::cbor</a>;
<b>use</b> <a href="">0x2::hash</a>;
<b>use</b> <a href="">0x2::hex</a>;
<b>use</b> <a href="">0x2::object</a>;
<b>use</b> <a href="">0x2::simple_map</a>;
<b>use</b> <a href="">0x2::string_utils</a>;
Expand Down Expand Up @@ -78,6 +81,18 @@



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

## Struct `InscribeGenerateArgs`



<pre><code>#[data_struct]
<b>struct</b> <a href="bitseed.md#0xa_bitseed_InscribeGenerateArgs">InscribeGenerateArgs</a> <b>has</b> <b>copy</b>, drop, store
</code></pre>



<a name="@Constants_0"></a>

## Constants
Expand Down
48 changes: 35 additions & 13 deletions frameworks/rooch-nursery/sources/bitseed.move
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ module rooch_nursery::bitseed {
use std::option::{Self, Option};
use std::string::{Self, String};
use std::bcs;

use moveos_std::address;
use moveos_std::hash;
use moveos_std::hex;
use moveos_std::object::{Self, Object};
use moveos_std::string_utils;
use moveos_std::simple_map::{Self, SimpleMap};
Expand Down Expand Up @@ -451,26 +452,39 @@ module rooch_nursery::bitseed {
vector::push_back(&mut arg_list, attributes_output);
let memory_args_list = wasm::create_memory_wasm_args(&mut wasm_instance, function_name, arg_list);

let ret_val = wasm::execute_wasm_function(&mut wasm_instance, function_name, memory_args_list);
let ret_val_option = wasm::execute_wasm_function_option(&mut wasm_instance, function_name, memory_args_list);

wasm::release_wasm_instance(wasm_instance);

if (ret_val != 0 ) {
if (option::is_none(&ret_val_option)) {
option::destroy_none(ret_val_option);
return (false, option::some(std::string::utf8(b"inscribe verify execute_wasm_function fail")))
};

let ret_val = option::destroy_some(ret_val_option);
if (ret_val != 1 ) {
return (false, option::some(std::string::utf8(b"inscribe verify fail")))
};

(true, option::none<String>())
}

fun pack_inscribe_generate_args(deploy_args: vector<u8>, seed: vector<u8>, user_input: vector<u8>): vector<u8>{
native_pack_inscribe_generate_args(deploy_args, b"attrs", seed, b"seed",
user_input, b"user_input")
#[data_struct]
struct InscribeGenerateArgs has copy, drop, store {
attrs: vector<u8>,
seed: std::string::String,
user_input: std::string::String,
}

native fun native_pack_inscribe_generate_args(
deploy_args: vector<u8>, deploy_args_key: vector<u8>,
seed: vector<u8>, seed_key: vector<u8>,
user_input: vector<u8>, user_input_key: vector<u8>,
): vector<u8>;
fun pack_inscribe_generate_args(deploy_args: vector<u8>, seed: vector<u8>, user_input: vector<u8>): vector<u8>{
let args = InscribeGenerateArgs{
attrs: deploy_args,
seed: string::utf8(seed),
user_input: string::utf8(user_input)
};

cbor::to_cbor(&args)
}

fun generate_seed_from_inscription(inscription: &Inscription): vector<u8> {
let inscription_txid = ord::txid(inscription);
Expand Down Expand Up @@ -512,7 +526,7 @@ module rooch_nursery::bitseed {
vector::append(&mut buf, address::to_bytes(block_hash));
vector::append(&mut buf, address::to_bytes(txid));
vector::append(&mut buf, bcs::to_bytes(&vout)); //TODO vout to le_bytes
hash::sha3_256(buf)
hex::encode(hash::sha3_256(buf))
}

// ==== Process Bitseed Entry ==== //
Expand Down Expand Up @@ -894,4 +908,12 @@ module rooch_nursery::bitseed {
assert!(option::borrow(&reason) == &std::string::utf8(b"create wasm instance fail"), 1);
}

}
#[test]
fun test_pack_inscribe_generate_args() {
let deploy_args = x"8178377b22686569676874223a7b2274797065223a2272616e6765222c2264617461223a7b226d696e223a312c226d6178223a313030307d7d7d";
let seed = b"0xe4b6de2407ad9455a364ba0227a8591631d1253508bc41f7d1992d218dd29b47";
let user_input = b"";

pack_inscribe_generate_args(deploy_args, seed, user_input);
}
}

0 comments on commit c26ef75

Please sign in to comment.