Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: audit #110

Merged
merged 8 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions crates/natives/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ pub fn make_all(
("query_stargate", native_query_stargate),
#[cfg(feature = "testing")]
("set_query_response", native_test_only_set_query_response),
#[cfg(feature = "testing")]
("unset_query_response", native_test_only_unset_query_response),
];

builder.make_named_natives(natives)
Expand Down Expand Up @@ -340,3 +342,31 @@ fn native_test_only_set_query_response(

Ok(smallvec![])
}

#[cfg(feature = "testing")]
fn native_test_only_unset_query_response(
context: &mut SafeNativeContext,
ty_args: Vec<Type>,
mut arguments: VecDeque<Value>,
) -> SafeNativeResult<SmallVec<[Value; 1]>> {
debug_assert!(ty_args.is_empty());
debug_assert!(arguments.len() == 2);

let data = safely_pop_arg!(arguments, Vector).to_vec_u8()?;
let path_or_name_bytes = safely_pop_arg!(arguments, Vector).to_vec_u8()?;

let path_or_name =
String::from_utf8(path_or_name_bytes).map_err(|_| SafeNativeError::Abort {
abort_code: UNABLE_TO_PARSE_STRING,
})?;

let mut hasher = Sha3_256::new();
hasher.update(path_or_name.as_bytes());
hasher.update(&data);
let hash = hex::encode(hasher.finalize());

let query_context = context.extensions_mut().get_mut::<NativeQueryContext>();
query_context.responses.remove(hash.as_str());

Ok(smallvec![])
}
Binary file modified precompile/binaries/minlib/collection.mv
Binary file not shown.
Binary file modified precompile/binaries/minlib/dex.mv
Binary file not shown.
Binary file modified precompile/binaries/minlib/nft.mv
Binary file not shown.
Binary file modified precompile/binaries/stdlib/collection.mv
Binary file not shown.
Binary file modified precompile/binaries/stdlib/dex.mv
Binary file not shown.
Binary file modified precompile/binaries/stdlib/nft.mv
Binary file not shown.
6 changes: 3 additions & 3 deletions precompile/modules/initia_stdlib/sources/coin.move
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ module initia_std::coin {
}

#[test_only]
fun initialized_coin(account: &signer, symbol: String,)
fun initialize_coin_for_testing(account: &signer, symbol: String,)
: (
BurnCapability, FreezeCapability, MintCapability
) {
Expand All @@ -397,8 +397,8 @@ module initia_std::coin {
#[test(chain = @0x1, not_chain = @0x2)]
fun test_denom_metadata_convert(chain: signer, not_chain: signer,) {
initia_std::primary_fungible_store::init_module_for_test();
initialized_coin(&chain, string::utf8(b"INIT"));
initialized_coin(&not_chain, string::utf8(b"INIT"));
initialize_coin_for_testing(&chain, string::utf8(b"INIT"));
initialize_coin_for_testing(&not_chain, string::utf8(b"INIT"));
let metadata = metadata(
std::signer::address_of(&chain),
string::utf8(b"INIT"),
Expand Down
9 changes: 6 additions & 3 deletions precompile/modules/initia_stdlib/sources/dex.move
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,12 @@ module initia_std::dex {
(coin_b_pool, coin_a_pool, coin_b_weight, coin_a_weight)
};

decimal128::from_ratio_u64(
decimal128::mul_u64(&base_weight, quote_pool),
decimal128::mul_u64(&quote_weight, base_pool),
let numerator = decimal128::div_u64(&base_weight, base_pool);
let denominator = decimal128::div_u64(&quote_weight, quote_pool);

decimal128::from_ratio(
decimal128::val(&numerator),
decimal128::val(&denominator),
)
}

Expand Down
15 changes: 15 additions & 0 deletions precompile/modules/initia_stdlib/sources/query.move
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ module initia_std::query {
path_or_name: vector<u8>, data: vector<u8>, response: vector<u8>
);

#[test_only]
native public fun unset_query_response(
path_or_name: vector<u8>, data: vector<u8>
);

#[test]
fun test_query_custom() {
set_query_response(b"path", b"data123", b"output");
Expand All @@ -94,4 +99,14 @@ module initia_std::query {
let res = query_stargate(b"path", b"data123");
assert!(res == b"output", 0);
}

#[test]
#[expected_failure(abort_code = 0x1006E, location = Self)]
fun test_query_unsset() {
set_query_response(b"path", b"data123", b"output");
unset_query_response(b"path", b"data123");

let res = query_custom(b"path", b"data123");
assert!(res == b"", 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ module initia_std::collection {
error::out_of_range(ECOLLECTION_NAME_TOO_LONG),
);
assert!(
string::index_of(name, &string::utf8(b"::")) == len,
string::index_of(name, &string::utf8(b":")) == len,
error::invalid_argument(EINVALID_COLLECTION_NAME),
);
}
Expand Down
2 changes: 1 addition & 1 deletion precompile/modules/initia_stdlib/sources/token/nft.move
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ module initia_std::nft {
error::out_of_range(ENFT_TOKEN_ID_TOO_LONG),
);
assert!(
string::index_of(token_id, &string::utf8(b"::")) == len,
string::index_of(token_id, &string::utf8(b":")) == len,
error::invalid_argument(EINVALID_TOKEN_ID),
);
}
Expand Down
6 changes: 3 additions & 3 deletions precompile/modules/minitia_stdlib/sources/coin.move
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ module minitia_std::coin {
}

#[test_only]
fun initialized_coin(account: &signer, symbol: String,)
fun initialize_coin_for_testing(account: &signer, symbol: String,)
: (
BurnCapability, FreezeCapability, MintCapability
) {
Expand All @@ -396,8 +396,8 @@ module minitia_std::coin {
#[test(chain = @0x1, not_chain = @0x2)]
fun test_denom_metadata_convert(chain: signer, not_chain: signer,) {
minitia_std::primary_fungible_store::init_module_for_test();
initialized_coin(&chain, string::utf8(b"INIT"));
initialized_coin(&not_chain, string::utf8(b"INIT"));
initialize_coin_for_testing(&chain, string::utf8(b"INIT"));
initialize_coin_for_testing(&not_chain, string::utf8(b"INIT"));
let metadata = metadata(
std::signer::address_of(&chain),
string::utf8(b"INIT"),
Expand Down
9 changes: 6 additions & 3 deletions precompile/modules/minitia_stdlib/sources/dex.move
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,12 @@ module minitia_std::dex {
(coin_b_pool, coin_a_pool, coin_b_weight, coin_a_weight)
};

decimal128::from_ratio_u64(
decimal128::mul_u64(&base_weight, quote_pool),
decimal128::mul_u64(&quote_weight, base_pool),
let numerator = decimal128::div_u64(&base_weight, base_pool);
let denominator = decimal128::div_u64(&quote_weight, quote_pool);

decimal128::from_ratio(
decimal128::val(&numerator),
decimal128::val(&denominator),
)
}

Expand Down
16 changes: 16 additions & 0 deletions precompile/modules/minitia_stdlib/sources/query.move
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ module minitia_std::query {
path_or_name: vector<u8>, data: vector<u8>, response: vector<u8>
);


#[test_only]
native public fun unset_query_response(
path_or_name: vector<u8>, data: vector<u8>
);

#[test]
fun test_query_custom() {
set_query_response(b"path", b"data123", b"output");
Expand All @@ -94,4 +100,14 @@ module minitia_std::query {
let res = query_stargate(b"path", b"data123");
assert!(res == b"output", 0);
}

#[test]
#[expected_failure(abort_code = 0x1006E, location = Self)]
fun test_query_unsset() {
set_query_response(b"path", b"data123", b"output");
unset_query_response(b"path", b"data123");

let res = query_custom(b"path", b"data123");
assert!(res == b"", 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ module minitia_std::collection {
error::out_of_range(ECOLLECTION_NAME_TOO_LONG),
);
assert!(
string::index_of(name, &string::utf8(b"::")) == len,
string::index_of(name, &string::utf8(b":")) == len,
error::invalid_argument(EINVALID_COLLECTION_NAME),
);
}
Expand Down
2 changes: 1 addition & 1 deletion precompile/modules/minitia_stdlib/sources/token/nft.move
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ module minitia_std::nft {
error::out_of_range(ENFT_TOKEN_ID_TOO_LONG),
);
assert!(
string::index_of(token_id, &string::utf8(b"::")) == len,
string::index_of(token_id, &string::utf8(b":")) == len,
error::invalid_argument(EINVALID_TOKEN_ID),
);
}
Expand Down
Loading