Skip to content

Commit

Permalink
introduce big number libraries (#121)
Browse files Browse the repository at this point in the history
* introduce big number libraries

* remove decimal128 and decimal256

* update missing bcs update
  • Loading branch information
beer-1 authored Sep 5, 2024
1 parent 6623a5c commit c4dbbc4
Show file tree
Hide file tree
Showing 70 changed files with 3,624 additions and 4,533 deletions.
4 changes: 4 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[env]

RUST_BIGDECIMAL_FMT_EXPONENTIAL_UPPER_THRESHOLD="100"
RUST_BIGDECIMAL_FMT_EXPONENTIAL_LOWER_THRESHOLD="100"
4 changes: 2 additions & 2 deletions crates/compiler/src/extended_checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ impl<'a> ExtendedChecker<'a> {
| "0x1::option::Option"
| "0x1::fixed_point32::FixedPoint32"
| "0x1::fixed_point64::FixedPoint64"
| "0x1::decimal128::Decimal128"
| "0x1::decimal256::Decimal256"
| "0x1::biguint::BigUint"
| "0x1::bigdecimal::BigDecimal"
)
}
}
Expand Down
12 changes: 12 additions & 0 deletions crates/e2e-move-tests/src/tests/args.data/pack/sources/hello.move
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module 0xCAFE::test {
use std::vector;
use std::option::{Self, Option};
use std::object::{Self, Object};
use std::bigdecimal::{Self, BigDecimal};
use std::biguint::{Self, BigUint};

struct ModuleData<T> has key, store {
state: T,
Expand Down Expand Up @@ -154,6 +156,16 @@ module 0xCAFE::test {
};
}

public entry fun biguint_test(bn: BigUint, num: u64) {
let bn2 = biguint::from_u64(num);
assert!(biguint::eq(bn, bn2), 1);
}

public entry fun bigdecimal_test(bd: BigDecimal, numerator: u64, denominator: u64) {
let bd2 = bigdecimal::from_ratio_u64(numerator, denominator);
assert!(bigdecimal::eq(bd, bd2), 1);
}

fun find_hello_in_msgs_of_msgs(msgs: &vector<vector<String>>) {
let outer_len = vector::length(msgs);
while (outer_len > 0) {
Expand Down
69 changes: 69 additions & 0 deletions crates/e2e-move-tests/src/tests/args.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::MoveHarness;
use bigdecimal::num_bigint::BigUint;
use bigdecimal::FromPrimitive;
use move_core_types::account_address::AccountAddress;
use move_core_types::identifier::Identifier;
use move_core_types::language_storage::{StructTag, TypeTag};
Expand Down Expand Up @@ -783,6 +785,73 @@ fn json_object_args() {
assert_eq!(res, data_string);
}

#[test]
fn biguint_bigdecimal() {
let acc = AccountAddress::from_hex_literal("0xcafe").expect("0xcafe account should be created");
let path = "src/tests/args.data/pack";
let mut h = MoveHarness::new();

h.initialize();

// publish package
let output = h.publish_package(&acc, path).expect("should success");
h.commit(output, true);

// execute create_object
let entry = "0xcafe::test::biguint_test";
h.run_entry_function(
vec![acc],
str::parse(entry).unwrap(),
vec![],
vec![
bcs::to_bytes(&BigUint::from_u64(100u64).unwrap().to_bytes_le()).unwrap(),
bcs::to_bytes(&100u64).unwrap(),
],
)
.unwrap();

h.run_entry_function_with_json(
vec![acc],
str::parse(entry).unwrap(),
vec![],
vec![
r#""100""#.to_string(),
r#""100""#.to_string(),
],
)
.unwrap();

let entry = "0xcafe::test::bigdecimal_test";
h.run_entry_function(
vec![acc],
str::parse(entry).unwrap(),
vec![],
vec![
bcs::to_bytes(
&BigUint::from_u128(50000000000000000u128)
.unwrap()
.to_bytes_le(),
)
.unwrap(),
bcs::to_bytes(&1u64).unwrap(),
bcs::to_bytes(&20u64).unwrap(),
],
)
.unwrap();

h.run_entry_function_with_json(
vec![acc],
str::parse(entry).unwrap(),
vec![],
vec![
r#""0.05""#.to_string(),
r#""1""#.to_string(),
r#""20""#.to_string(),
],
)
.unwrap();
}

#[derive(Deserialize)]
struct CreateEvent {
object: AccountAddress,
Expand Down
2 changes: 1 addition & 1 deletion crates/e2e-move-tests/src/tests/cosmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ fn test_cosmos_nft_transfer() {
bcs::to_bytes(&true).unwrap(),
bcs::to_bytes(&true).unwrap(),
bcs::to_bytes(&true).unwrap(),
vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
vec![0],
],
ExpectedOutput::new(VMStatus::Executed, None, None, None),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
module 0x2::test {
use std::vector;
use std::option;
use std::decimal256;
use std::decimal128;
use std::biguint;
use std::bigdecimal;
use std::string;

#[view]
Expand All @@ -22,23 +22,23 @@ module 0x2::test {
}

#[view]
public fun decimal1(): decimal256::Decimal256 {
decimal256::from_ratio(123, 100) // 1.23
public fun bigdecimal1(): bigdecimal::BigDecimal {
bigdecimal::from_ratio_u64(123, 100) // 1.23
}

#[view]
public fun decimal2(): decimal256::Decimal256 {
decimal256::from_ratio(123, 1000) // 0.123
public fun bigdecimal2(): bigdecimal::BigDecimal {
bigdecimal::from_ratio_u64(123, 1000) // 0.123
}

#[view]
public fun decimal3(): decimal128::Decimal128 {
decimal128::from_ratio(123, 1) // 123
public fun biguint1(): biguint::BigUint {
biguint::from_u64(123)
}

#[view]
public fun decimal4(): decimal128::Decimal128 {
decimal128::from_ratio(123, 1000000) // 0.000123
public fun biguint2(): biguint::BigUint {
biguint::from_u128(12312983219839218392183)
}

#[view]
Expand Down
10 changes: 5 additions & 5 deletions crates/e2e-move-tests/src/tests/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn test_output() {

let test_decimal = (
None,
"0x2::test::decimal1",
"0x2::test::bigdecimal1",
vec![],
vec![],
ExpectedOutput::new(VMStatus::Executed, Some("\"1.23\"".to_string()), None, None),
Expand All @@ -94,7 +94,7 @@ fn test_output() {

let test_decimal2 = (
None,
"0x2::test::decimal2",
"0x2::test::bigdecimal2",
vec![],
vec![],
ExpectedOutput::new(
Expand All @@ -108,7 +108,7 @@ fn test_output() {

let test_decimal3 = (
None,
"0x2::test::decimal3",
"0x2::test::biguint1",
vec![],
vec![],
ExpectedOutput::new(VMStatus::Executed, Some("\"123\"".to_string()), None, None),
Expand All @@ -117,12 +117,12 @@ fn test_output() {

let test_decimal4 = (
None,
"0x2::test::decimal4",
"0x2::test::biguint2",
vec![],
vec![],
ExpectedOutput::new(
VMStatus::Executed,
Some("\"0.000123\"".to_string()),
Some("\"12312983219839218392183\"".to_string()),
None,
None,
),
Expand Down
20 changes: 20 additions & 0 deletions crates/gas/src/initia_stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,24 @@ crate::macros::define_gas_parameters!(
[function_info_check_dispatch_type_compatibility_impl_base: InternalGas, "function_info.check_dispatch_type_compatibility_impl.base", 1002],
[function_info_load_function_base: InternalGas, "function_info.load_function.base", 551],
[dispatchable_fungible_asset_dispatch_base: InternalGas, "dispatchable_fungible_asset.dispatch.base", 551],

[biguint_add_base: InternalGas, "biguint.add.base", 588],
[biguint_add_per_byte: InternalGasPerByte, "biguint.add.per_byte", 3],
[biguint_sub_base: InternalGas, "biguint.sub.base", 588],
[biguint_sub_per_byte: InternalGasPerByte, "biguint.sub.per_byte", 3],
[biguint_mul_base: InternalGas, "biguint.mul.base", 588],
[biguint_mul_per_byte: InternalGasPerByte, "biguint.mul.per_byte", 3],
[biguint_div_base: InternalGas, "biguint.div.base", 588],
[biguint_div_per_byte: InternalGasPerByte, "biguint.div.per_byte", 3],
[biguint_new_base: InternalGas, "biguint.new.base", 441],
[biguint_cast_base: InternalGas, "biguint.cast.base", 441],
[biguint_cast_per_byte: InternalGasPerByte, "biguint.cast.per_byte", 3],
[biguint_lt_base: InternalGas, "biguint.lt.base", 588],
[biguint_lt_per_byte: InternalGasPerByte, "biguint.lt.per_byte", 3],
[biguint_gt_base: InternalGas, "biguint.gt.base", 588],
[biguint_gt_per_byte: InternalGasPerByte, "biguint.gt.per_byte", 3],
[biguint_le_base: InternalGas, "biguint.le.base", 588],
[biguint_le_per_byte: InternalGasPerByte, "biguint.le.per_byte", 3],
[biguint_ge_base: InternalGas, "biguint.ge.base", 588],
[biguint_ge_per_byte: InternalGasPerByte, "biguint.ge.per_byte", 3]
]);
Loading

0 comments on commit c4dbbc4

Please sign in to comment.