Skip to content

Commit

Permalink
unify native function errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pause125 committed Oct 15, 2023
1 parent cce86ef commit 3ec86bf
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 72 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use move_vm_types::{
use smallvec::smallvec;
use std::collections::VecDeque;

pub const DECODE_FAILED: u64 = 1;
pub const E_DECODE_FAILED: u64 = 1;

/***************************************************************************************************
* native fun base58
Expand All @@ -24,7 +24,7 @@ pub const DECODE_FAILED: u64 = 1;
* + decoding_base58_data_cost_per_block * num_blocks | cost depends on number of blocks in message
**************************************************************************************************/
pub fn native_base58(
_gas_params: &FromBytesGasParameters,
gas_params: &FromBytesGasParameters,
_context: &mut NativeContext,
ty_args: Vec<Type>,
mut args: VecDeque<Value>,
Expand All @@ -34,14 +34,14 @@ pub fn native_base58(

// TODO(Gas): Charge the arg size dependent costs

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

let encoded_address_bytes = pop_arg!(args, VectorRef);

let Ok(bs58_raw_bytes) = bs58::decode(encoded_address_bytes.as_bytes_ref().to_vec())
.into_vec()
else {
return Ok(NativeResult::err(cost, DECODE_FAILED));
return Ok(NativeResult::err(cost, moveos_types::move_std::error::invalid_argument(E_DECODE_FAILED)));
};

Ok(NativeResult::ok(
Expand All @@ -58,7 +58,7 @@ pub fn native_base58(
* + decoding_base58check_data_cost_per_block * num_blocks | cost depends on number of blocks in message
**************************************************************************************************/
pub fn native_base58check(
_gas_params: &FromBytesGasParameters,
gas_params: &FromBytesGasParameters,
_context: &mut NativeContext,
ty_args: Vec<Type>,
mut args: VecDeque<Value>,
Expand All @@ -68,7 +68,7 @@ pub fn native_base58check(

// TODO(Gas): Charge the arg size dependent costs

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

let version_byte = pop_arg!(args, u8);
let encoded_address_bytes = pop_arg!(args, VectorRef);
Expand All @@ -77,7 +77,7 @@ pub fn native_base58check(
.with_check(Some(version_byte))
.into_vec()
else {
return Ok(NativeResult::err(cost, DECODE_FAILED));
return Ok(NativeResult::err(cost, moveos_types::move_std::error::invalid_argument(E_DECODE_FAILED)));
};

Ok(NativeResult::ok(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ use move_vm_types::{
use smallvec::smallvec;
use std::collections::VecDeque;

pub const INVALID_SIGNATURE: u64 = 1;
pub const INVALID_PUBKEY: u64 = 2;
pub const E_INVALID_SIGNATURE: u64 = 1;
pub const E_INVALID_PUBKEY: u64 = 2;

pub const KECCAK256: u8 = 0;
pub const SHA256: u8 = 1;

pub fn native_verify(
_gas_params: &FromBytesGasParameters,
gas_params: &FromBytesGasParameters,
_context: &mut NativeContext,
ty_args: Vec<Type>,
mut args: VecDeque<Value>,
Expand All @@ -46,14 +46,14 @@ pub fn native_verify(

// TODO(Gas): Charge the arg size dependent costs

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

let Ok(sig) = <Secp256k1Signature as ToFromBytes>::from_bytes(&signature_bytes_ref) else {
return Ok(NativeResult::err(cost, INVALID_SIGNATURE));
return Ok(NativeResult::err(cost, moveos_types::move_std::error::invalid_argument(E_INVALID_SIGNATURE)));
};

let Ok(public_key) = <Secp256k1PublicKey as ToFromBytes>::from_bytes(&public_key_bytes_ref) else {
return Ok(NativeResult::err(cost, INVALID_PUBKEY));
return Ok(NativeResult::err(cost, moveos_types::move_std::error::invalid_argument(E_INVALID_PUBKEY)));
};

let result = match hash {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ use move_vm_types::{
use smallvec::smallvec;
use std::collections::VecDeque;

pub const FAIL_TO_RECOVER_PUBKEY: u64 = 1;
pub const INVALID_SIGNATURE: u64 = 2;
pub const INVALID_PUBKEY: u64 = 3;
pub const E_FAIL_TO_RECOVER_PUBKEY: u64 = 1;
pub const E_INVALID_SIGNATURE: u64 = 2;
pub const E_INVALID_PUBKEY: u64 = 3;
pub const E_INVALID_HASH_TYPE: u64 = 4;

pub const KECCAK256: u8 = 0;
pub const SHA256: u8 = 1;

pub fn native_ecrecover(
_gas_params: &FromBytesGasParameters,
gas_params: &FromBytesGasParameters,
_context: &mut NativeContext,
ty_args: Vec<Type>,
mut args: VecDeque<Value>,
Expand All @@ -45,29 +46,37 @@ pub fn native_ecrecover(

// TODO(Gas): Charge the arg size dependent costs

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

let Ok(sig) = <Secp256k1RecoverableSignature as ToFromBytes>::from_bytes(&signature_ref) else {
return Ok(NativeResult::err(cost, INVALID_SIGNATURE));
return Ok(NativeResult::err(cost, moveos_types::move_std::error::invalid_argument(E_INVALID_SIGNATURE)));
};

let pk = match hash {
KECCAK256 => sig.recover_with_hash::<Keccak256>(&msg_ref),
SHA256 => sig.recover_with_hash::<Sha256>(&msg_ref),
_ => Err(FastCryptoError::InvalidInput), // We should never reach here
_ => {
return Ok(NativeResult::err(
cost,
moveos_types::move_std::error::invalid_argument(E_INVALID_HASH_TYPE),
))
} // We should never reach here
};

match pk {
Ok(pk) => Ok(NativeResult::ok(
cost,
smallvec![Value::vector_u8(pk.as_bytes().to_vec())],
)),
Err(_) => Ok(NativeResult::err(cost, FAIL_TO_RECOVER_PUBKEY)),
Err(_) => Ok(NativeResult::err(
cost,
moveos_types::move_std::error::internal(E_FAIL_TO_RECOVER_PUBKEY),
)),
}
}

pub fn native_decompress_pubkey(
_gas_params: &FromBytesGasParameters,
gas_params: &FromBytesGasParameters,
_context: &mut NativeContext,
ty_args: Vec<Type>,
mut args: VecDeque<Value>,
Expand All @@ -79,7 +88,7 @@ pub fn native_decompress_pubkey(
let pubkey_ref = pubkey.as_bytes_ref();

// TODO(Gas): Charge the arg size dependent costs
let cost = 0.into();
let cost = gas_params.base;

match Secp256k1PublicKey::from_bytes(&pubkey_ref) {
Ok(pubkey) => {
Expand All @@ -89,12 +98,15 @@ pub fn native_decompress_pubkey(
smallvec![Value::vector_u8(uncompressed.to_vec())],
))
}
Err(_) => Ok(NativeResult::err(cost, INVALID_PUBKEY)),
Err(_) => Ok(NativeResult::err(
cost,
moveos_types::move_std::error::invalid_argument(E_INVALID_PUBKEY),
)),
}
}

pub fn native_verify(
_gas_params: &FromBytesGasParameters,
gas_params: &FromBytesGasParameters,
_context: &mut NativeContext,
ty_args: Vec<Type>,
mut args: VecDeque<Value>,
Expand All @@ -112,10 +124,10 @@ pub fn native_verify(

// TODO(Gas): Charge the arg size dependent costs

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

let Ok(sig) = <Secp256k1RecoverableSignature as ToFromBytes>::from_bytes(&signature_bytes_ref) else {
return Ok(NativeResult::err(cost, INVALID_SIGNATURE));
return Ok(NativeResult::err(cost, moveos_types::move_std::error::invalid_argument(E_INVALID_SIGNATURE)));
};

let pk = match hash {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use std::collections::VecDeque;
* + ed25519_ed25519_verify_msg_cost_per_block * block_size | cost depends on number of blocks in message
**************************************************************************************************/
pub fn native_verify(
_gas_params: &FromBytesGasParameters,
gas_params: &FromBytesGasParameters,
_context: &mut NativeContext,
ty_args: Vec<Type>,
mut args: VecDeque<Value>,
Expand All @@ -38,7 +38,7 @@ pub fn native_verify(

// TODO(Gas): Charge the arg size dependent costs

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

let msg = pop_arg!(args, VectorRef);
let msg_ref = msg.as_bytes_ref();
Expand Down
Loading

0 comments on commit 3ec86bf

Please sign in to comment.