Skip to content

Commit

Permalink
Auto sort modules by dependency order and review native function erro…
Browse files Browse the repository at this point in the history
…rs (#971)

* auto sort modules by dependency order

* auto sort modules

* unify native function errors

* fix verification error and update error code
  • Loading branch information
pause125 authored Oct 16, 2023
1 parent 8766e33 commit f7573bb
Show file tree
Hide file tree
Showing 36 changed files with 372 additions and 134 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ task 4 'run'. lines 109-124:
status EXECUTED

task 5 'run'. lines 125-137:
status ABORTED with code 3 in 0000000000000000000000000000000000000000000000000000000000000002::raw_table
status ABORTED with code 4 in 0000000000000000000000000000000000000000000000000000000000000002::raw_table

task 6 'run'. lines 138-151:
status EXECUTED
16 changes: 16 additions & 0 deletions crates/rooch-framework/doc/bcs.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function because this can violate implicit struct invariants, therefore only pri
a general conversion back-and-force is needed, consider the <code>moveos_std::Any</code> type which preserves invariants.


- [Constants](#@Constants_0)
- [Function `to_bytes`](#0x3_bcs_to_bytes)
- [Function `to_bool`](#0x3_bcs_to_bool)
- [Function `to_u8`](#0x3_bcs_to_u8)
Expand All @@ -24,6 +25,21 @@ a general conversion back-and-force is needed, consider the <code>moveos_std::An



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

## Constants


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

The request Move type is not match with input Move type.


<pre><code><b>const</b> <a href="bcs.md#0x3_bcs_ErrorTypeNotMatch">ErrorTypeNotMatch</a>: u64 = 1;
</code></pre>



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

## Function `to_bytes`
Expand Down
18 changes: 9 additions & 9 deletions crates/rooch-framework/doc/bitcoin_address.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@
## Constants


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



<pre><code><b>const</b> <a href="bitcoin_address.md#0x3_bitcoin_address_ErrorInvalidScriptVersion">ErrorInvalidScriptVersion</a>: u64 = 2;
</code></pre>



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

Bech32 addresses including P2WPKH and P2WSH are 42 characters
Expand Down Expand Up @@ -102,15 +111,6 @@ Bech32 addresses including P2WPKH and P2WSH are 42 characters



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



<pre><code><b>const</b> <a href="bitcoin_address.md#0x3_bitcoin_address_ErrorInvalidScriptVersion">ErrorInvalidScriptVersion</a>: u64 = 2;
</code></pre>



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


Expand Down
16 changes: 16 additions & 0 deletions crates/rooch-framework/doc/decoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Module which defines decoding functions.


- [Constants](#@Constants_0)
- [Function `base58`](#0x3_decoding_base58)
- [Function `base58check`](#0x3_decoding_base58check)

Expand All @@ -14,6 +15,21 @@ Module which defines decoding functions.



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

## Constants


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

Failed to decode an address


<pre><code><b>const</b> <a href="decoding.md#0x3_decoding_ErrorDecodeFailed">ErrorDecodeFailed</a>: u64 = 1;
</code></pre>



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

## Function `base58`
Expand Down
10 changes: 10 additions & 0 deletions crates/rooch-framework/doc/ecdsa_k1_recoverable.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ Error if the public key cannot be recovered from the signature.



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

Invalid hash function


<pre><code><b>const</b> <a href="ecdsa_k1_recoverable.md#0x3_ecdsa_k1_recoverable_ErrorInvalidHashType">ErrorInvalidHashType</a>: u64 = 4;
</code></pre>



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

Hash function name that are valid for ecrecover and verify.
Expand Down
46 changes: 46 additions & 0 deletions crates/rooch-framework/doc/encoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Module which defines encoding functions.


- [Constants](#@Constants_0)
- [Function `base58`](#0x3_encoding_base58)
- [Function `base58check`](#0x3_encoding_base58check)
- [Function `bech32`](#0x3_encoding_bech32)
Expand All @@ -17,6 +18,51 @@ Module which defines encoding functions.



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

## Constants


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

Excessive script size


<pre><code><b>const</b> <a href="encoding.md#0x3_encoding_ErrorExcessiveScriptSize">ErrorExcessiveScriptSize</a>: u64 = 2;
</code></pre>



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

Invalid data


<pre><code><b>const</b> <a href="encoding.md#0x3_encoding_ErrorInvalidData">ErrorInvalidData</a>: u64 = 3;
</code></pre>



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

Invalid publich key


<pre><code><b>const</b> <a href="encoding.md#0x3_encoding_ErrorInvalidPubkey">ErrorInvalidPubkey</a>: u64 = 1;
</code></pre>



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

Invalid script version


<pre><code><b>const</b> <a href="encoding.md#0x3_encoding_ErrorInvalidScriptVersion">ErrorInvalidScriptVersion</a>: u64 = 4;
</code></pre>



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

## Function `base58`
Expand Down
3 changes: 3 additions & 0 deletions crates/rooch-framework/sources/bcs.move
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
/// a general conversion back-and-force is needed, consider the `moveos_std::Any` type which preserves invariants.
module rooch_framework::bcs {

/// The request Move type is not match with input Move type.
const ErrorTypeNotMatch: u64 = 1;

public fun to_bytes<MoveValue>(v: &MoveValue): vector<u8> {
std::bcs::to_bytes(v)
}
Expand Down
4 changes: 4 additions & 0 deletions crates/rooch-framework/sources/crypto/decoding.move
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

/// Module which defines decoding functions.
module rooch_framework::decoding {

/// Failed to decode an address
const ErrorDecodeFailed: u64 = 1;

/// @param encoded_address_bytes: encoded Bitcoin address bytes on the Bitcoin network
/// Decode the Bitcoin address bytes with Base58 algorithm and returns a raw address bytes
native public fun base58(encoded_address_bytes: &vector<u8>): vector<u8>;
Expand Down
5 changes: 3 additions & 2 deletions crates/rooch-framework/sources/crypto/ecdsa_k1.move
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

module rooch_framework::ecdsa_k1 {

/// constant codes
const ECDSA_K1_TO_BITCOIN_VALIDATOR_ID_LENGTH: u64 = 1;
const ECDSA_K1_COMPRESSED_PUBKEY_LENGTH: u64 = 33;
Expand Down Expand Up @@ -62,7 +63,7 @@ module rooch_framework::ecdsa_k1 {
}

#[test]
#[expected_failure(abort_code = ErrorInvalidSignature)]
#[expected_failure(location=Self, abort_code = 65537)] // std::error::invalid_argument(ErrorInvalidSignature)
fun test_verify_fails_invalid_sig() {
let msg = x"00010203";
let pubkey = x"033e99a541db69bd32040dfe5037fbf5210dafa8151a71e21c5204b05d95ce0a62";
Expand All @@ -71,7 +72,7 @@ module rooch_framework::ecdsa_k1 {
}

#[test]
#[expected_failure(abort_code = ErrorInvalidPubKey)]
#[expected_failure(location=Self, abort_code = 65538)] // std::error::invalid_argument(ErrorInvalidPubKey)
fun test_verify_fails_invalid_pubkey() {
let msg = x"00010203";
let pubkey = x"";
Expand Down
11 changes: 7 additions & 4 deletions crates/rooch-framework/sources/crypto/ecdsa_k1_recoverable.move
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ module rooch_framework::ecdsa_k1_recoverable {
/// Error if the public key is invalid.
const ErrorInvalidPubKey: u64 = 3;

/// Invalid hash function
const ErrorInvalidHashType: u64 = 4;

/// built-in functions
public fun public_key_length(): u64 {
ECDSA_K1_RECOVERABLE_COMPRESSED_PUBKEY_LENGTH
Expand Down Expand Up @@ -77,15 +80,15 @@ module rooch_framework::ecdsa_k1_recoverable {
}

#[test]
#[expected_failure(abort_code = ErrorFailToRecoverPubKey)]
#[expected_failure(location=Self, abort_code = 720897)] // std::error::invalid_argument(ErrorFailToRecoverPubKey)
fun test_ecrecover_pubkey_fail_to_recover() {
let msg = x"00";
let sig = x"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
ecrecover(&sig, &msg, KECCAK256);
}

#[test]
#[expected_failure(abort_code = ErrorInvalidSignature)]
#[expected_failure(location=Self, abort_code = 65538)] // std::error::invalid_argument(ErrorInvalidSignature)
fun test_ecrecover_pubkey_invalid_sig() {
let msg = b"Hello, world!";
// incorrect length sig
Expand All @@ -94,7 +97,7 @@ module rooch_framework::ecdsa_k1_recoverable {
}

#[test]
#[expected_failure(abort_code = ErrorInvalidSignature)]
#[expected_failure(location=Self, abort_code = 65538)] // std::error::invalid_argument(ErrorInvalidSignature)
fun test_verify_fails_invalid_sig() {
let msg = b"Hello, world!";
let sig = x"";
Expand All @@ -118,7 +121,7 @@ module rooch_framework::ecdsa_k1_recoverable {
}

#[test]
#[expected_failure(abort_code = ErrorInvalidPubKey)]
#[expected_failure(location=Self, abort_code = 65539)] // std::error::invalid_argument(ErrorInvalidSignature)
fun test_decompress_pubkey_invalid_pubkey() {
let pubkey = x"013e99a541db69bd32040dfe5037fbf5210dafa8151a71e21c5204b05d95ce0a62";
decompress_pubkey(&pubkey);
Expand Down
13 changes: 13 additions & 0 deletions crates/rooch-framework/sources/crypto/encoding.move
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@

/// Module which defines encoding functions.
module rooch_framework::encoding {

/// Invalid publich key
const ErrorInvalidPubkey: u64 = 1;

/// Excessive script size
const ErrorExcessiveScriptSize: u64 = 2;

/// Invalid data
const ErrorInvalidData: u64 = 3;

/// Invalid script version
const ErrorInvalidScriptVersion: u64 = 4;

/// @param address_bytes: address bytes on the Bitcoin network
/// Encode the address bytes with Base58 algorithm and returns an encoded Bitcoin address
native public fun base58(address_bytes: &vector<u8>): vector<u8>;
Expand Down
4 changes: 2 additions & 2 deletions crates/rooch-framework/sources/crypto/schnorr.move
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module rooch_framework::schnorr {
): bool;

#[test]
#[expected_failure(abort_code = ErrorInvalidSignature)]
#[expected_failure(location=Self, abort_code = 65537)] // std::error::invalid_argument(ErrorInvalidSignature)
public fun test_schnorr_invalid_sig() {
let msg = x"00010203";
let pk = x"3e99a541db69bd32040dfe5037fbf5210dafa8151a71e21c5204b05d95ce0a62";
Expand All @@ -62,7 +62,7 @@ module rooch_framework::schnorr {
}

#[test]
#[expected_failure(abort_code = ErrorInvalidPubKey)]
#[expected_failure(location=Self, abort_code = 65538)] // std::error::invalid_argument(ErrorInvalidPubKey)
public fun test_schnorr_invalid_pubkey() {
let msg = x"00010203";
let invalid_pk = x"";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use moveos_stdlib::natives::moveos_stdlib::move_module::GasParameters;
crate::natives::gas_parameter::native::define_gas_parameters_for_natives!(GasParameters, "move_module", [
[.module_name_inner.base, "module_name_inner.base", (5 + 1) * MUL],
[.module_name_inner.per_byte_in_str, "module_name_inner.per_byte_in_str", (5 + 1) * MUL],
[.verify_modules_inner.base, "verify_modules_inner.base", (5 + 1) * MUL],
[.verify_modules_inner.per_byte, "verify_modules_inner.per_byte", (5 + 1) * MUL],
[.sort_and_verify_modules_inner.base, "sort_and_verify_modules_inner.base", (5 + 1) * MUL],
[.sort_and_verify_modules_inner.per_byte, "sort_and_verify_modules_inner.per_byte", (5 + 1) * MUL],
[.request_init_functions.base, "request_init_functions.base", (5 + 1) * MUL],
[.request_init_functions.per_byte, "request_init_functions.per_byte", (5 + 1) * MUL],
[.check_compatibililty_inner.base, "check_compatibililty_inner.base", (5 + 1) * MUL],
Expand Down
12 changes: 8 additions & 4 deletions crates/rooch-framework/src/natives/rooch_framework/bcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use move_vm_types::{
use smallvec::smallvec;
use std::collections::VecDeque;

const E_TYPE_NOT_MATCH: u64 = 1;

#[derive(Debug, Clone)]
pub struct FromBytesGasParameters {
pub base: InternalGas,
Expand All @@ -27,15 +29,15 @@ impl FromBytesGasParameters {
/// Bytes are in BCS (Binary Canonical Serialization) format.
#[inline]
fn native_from_bytes(
_gas_params: &FromBytesGasParameters,
gas_params: &FromBytesGasParameters,
context: &mut NativeContext,
ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
debug_assert_eq!(ty_args.len(), 1);
debug_assert_eq!(args.len(), 1);

let cost = 0.into();
let cost = gas_params.base;

// TODO(Gas): charge for getting the layout
let layout = context.type_to_type_layout(&ty_args[0])?.ok_or_else(|| {
Expand All @@ -49,8 +51,10 @@ fn native_from_bytes(
let val = match Value::simple_deserialize(&bytes, &layout) {
Some(val) => val,
None => {
// TODO(gas): charge the gas for the failure.
return Err(PartialVMError::new(StatusCode::VALUE_DESERIALIZATION_ERROR));
return Ok(NativeResult::err(
cost,
moveos_types::move_std::error::invalid_argument(E_TYPE_NOT_MATCH),
));
}
};
// TODO(gas): charge gas for deserialization
Expand Down
Loading

0 comments on commit f7573bb

Please sign in to comment.