Skip to content

Commit

Permalink
[framework] Fungible Asset - Mutable Metadata - Max length checks (#1…
Browse files Browse the repository at this point in the history
…3917)

* Added check for max fields, similar to what `add_fungibility` does.

* FA tests for mutable metadata had too long of symbol length.

* FA tests for mutable metadata had too long of symbol length.

* Added new expecting-failure tests for each `mutate_metadata` parameter.
  • Loading branch information
Alexzander-Stone authored Sep 11, 2024
1 parent 31ae1f0 commit 1365a99
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 16 deletions.
20 changes: 15 additions & 5 deletions aptos-move/framework/aptos-framework/doc/fungible_asset.md
Original file line number Diff line number Diff line change
Expand Up @@ -3170,19 +3170,29 @@ Mutate specified fields of the fungible asset's <code><a href="fungible_asset.md
<b>let</b> mutable_metadata = <b>borrow_global_mut</b>&lt;<a href="fungible_asset.md#0x1_fungible_asset_Metadata">Metadata</a>&gt;(metadata_address);

<b>if</b> (<a href="../../aptos-stdlib/../move-stdlib/doc/option.md#0x1_option_is_some">option::is_some</a>(&name)){
mutable_metadata.name = <a href="../../aptos-stdlib/../move-stdlib/doc/option.md#0x1_option_extract">option::extract</a>(&<b>mut</b> name);
<b>let</b> name = <a href="../../aptos-stdlib/../move-stdlib/doc/option.md#0x1_option_extract">option::extract</a>(&<b>mut</b> name);
<b>assert</b>!(<a href="../../aptos-stdlib/../move-stdlib/doc/string.md#0x1_string_length">string::length</a>(&name) &lt;= <a href="fungible_asset.md#0x1_fungible_asset_MAX_NAME_LENGTH">MAX_NAME_LENGTH</a>, <a href="../../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error_out_of_range">error::out_of_range</a>(<a href="fungible_asset.md#0x1_fungible_asset_ENAME_TOO_LONG">ENAME_TOO_LONG</a>));
mutable_metadata.name = name;
};
<b>if</b> (<a href="../../aptos-stdlib/../move-stdlib/doc/option.md#0x1_option_is_some">option::is_some</a>(&symbol)){
mutable_metadata.symbol = <a href="../../aptos-stdlib/../move-stdlib/doc/option.md#0x1_option_extract">option::extract</a>(&<b>mut</b> symbol);
<b>let</b> symbol = <a href="../../aptos-stdlib/../move-stdlib/doc/option.md#0x1_option_extract">option::extract</a>(&<b>mut</b> symbol);
<b>assert</b>!(<a href="../../aptos-stdlib/../move-stdlib/doc/string.md#0x1_string_length">string::length</a>(&symbol) &lt;= <a href="fungible_asset.md#0x1_fungible_asset_MAX_SYMBOL_LENGTH">MAX_SYMBOL_LENGTH</a>, <a href="../../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error_out_of_range">error::out_of_range</a>(<a href="fungible_asset.md#0x1_fungible_asset_ESYMBOL_TOO_LONG">ESYMBOL_TOO_LONG</a>));
mutable_metadata.symbol = symbol;
};
<b>if</b> (<a href="../../aptos-stdlib/../move-stdlib/doc/option.md#0x1_option_is_some">option::is_some</a>(&decimals)){
mutable_metadata.decimals = <a href="../../aptos-stdlib/../move-stdlib/doc/option.md#0x1_option_extract">option::extract</a>(&<b>mut</b> decimals);
<b>let</b> decimals = <a href="../../aptos-stdlib/../move-stdlib/doc/option.md#0x1_option_extract">option::extract</a>(&<b>mut</b> decimals);
<b>assert</b>!(<a href="fungible_asset.md#0x1_fungible_asset_decimals">decimals</a> &lt;= <a href="fungible_asset.md#0x1_fungible_asset_MAX_DECIMALS">MAX_DECIMALS</a>, <a href="../../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error_out_of_range">error::out_of_range</a>(<a href="fungible_asset.md#0x1_fungible_asset_EDECIMALS_TOO_LARGE">EDECIMALS_TOO_LARGE</a>));
mutable_metadata.decimals = decimals;
};
<b>if</b> (<a href="../../aptos-stdlib/../move-stdlib/doc/option.md#0x1_option_is_some">option::is_some</a>(&icon_uri)){
mutable_metadata.icon_uri = <a href="../../aptos-stdlib/../move-stdlib/doc/option.md#0x1_option_extract">option::extract</a>(&<b>mut</b> icon_uri);
<b>let</b> icon_uri = <a href="../../aptos-stdlib/../move-stdlib/doc/option.md#0x1_option_extract">option::extract</a>(&<b>mut</b> icon_uri);
<b>assert</b>!(<a href="../../aptos-stdlib/../move-stdlib/doc/string.md#0x1_string_length">string::length</a>(&icon_uri) &lt;= <a href="fungible_asset.md#0x1_fungible_asset_MAX_URI_LENGTH">MAX_URI_LENGTH</a>, <a href="../../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error_out_of_range">error::out_of_range</a>(<a href="fungible_asset.md#0x1_fungible_asset_EURI_TOO_LONG">EURI_TOO_LONG</a>));
mutable_metadata.icon_uri = icon_uri;
};
<b>if</b> (<a href="../../aptos-stdlib/../move-stdlib/doc/option.md#0x1_option_is_some">option::is_some</a>(&project_uri)){
mutable_metadata.project_uri = <a href="../../aptos-stdlib/../move-stdlib/doc/option.md#0x1_option_extract">option::extract</a>(&<b>mut</b> project_uri);
<b>let</b> project_uri = <a href="../../aptos-stdlib/../move-stdlib/doc/option.md#0x1_option_extract">option::extract</a>(&<b>mut</b> project_uri);
<b>assert</b>!(<a href="../../aptos-stdlib/../move-stdlib/doc/string.md#0x1_string_length">string::length</a>(&project_uri) &lt;= <a href="fungible_asset.md#0x1_fungible_asset_MAX_URI_LENGTH">MAX_URI_LENGTH</a>, <a href="../../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error_out_of_range">error::out_of_range</a>(<a href="fungible_asset.md#0x1_fungible_asset_EURI_TOO_LONG">EURI_TOO_LONG</a>));
mutable_metadata.project_uri = project_uri;
};
}
</code></pre>
Expand Down
129 changes: 118 additions & 11 deletions aptos-move/framework/aptos-framework/sources/fungible_asset.move
Original file line number Diff line number Diff line change
Expand Up @@ -960,19 +960,29 @@ module aptos_framework::fungible_asset {
let mutable_metadata = borrow_global_mut<Metadata>(metadata_address);

if (option::is_some(&name)){
mutable_metadata.name = option::extract(&mut name);
let name = option::extract(&mut name);
assert!(string::length(&name) <= MAX_NAME_LENGTH, error::out_of_range(ENAME_TOO_LONG));
mutable_metadata.name = name;
};
if (option::is_some(&symbol)){
mutable_metadata.symbol = option::extract(&mut symbol);
let symbol = option::extract(&mut symbol);
assert!(string::length(&symbol) <= MAX_SYMBOL_LENGTH, error::out_of_range(ESYMBOL_TOO_LONG));
mutable_metadata.symbol = symbol;
};
if (option::is_some(&decimals)){
mutable_metadata.decimals = option::extract(&mut decimals);
let decimals = option::extract(&mut decimals);
assert!(decimals <= MAX_DECIMALS, error::out_of_range(EDECIMALS_TOO_LARGE));
mutable_metadata.decimals = decimals;
};
if (option::is_some(&icon_uri)){
mutable_metadata.icon_uri = option::extract(&mut icon_uri);
let icon_uri = option::extract(&mut icon_uri);
assert!(string::length(&icon_uri) <= MAX_URI_LENGTH, error::out_of_range(EURI_TOO_LONG));
mutable_metadata.icon_uri = icon_uri;
};
if (option::is_some(&project_uri)){
mutable_metadata.project_uri = option::extract(&mut project_uri);
let project_uri = option::extract(&mut project_uri);
assert!(string::length(&project_uri) <= MAX_URI_LENGTH, error::out_of_range(EURI_TOO_LONG));
mutable_metadata.project_uri = project_uri;
};
}

Expand Down Expand Up @@ -1311,13 +1321,13 @@ module aptos_framework::fungible_asset {
mutate_metadata(
&mutate_metadata_ref,
option::some(string::utf8(b"mutated_name")),
option::some(string::utf8(b"mutated_symbol")),
option::some(string::utf8(b"m_symbol")),
option::none(),
option::none(),
option::none()
);
assert!(name(metadata) == string::utf8(b"mutated_name"), 8);
assert!(symbol(metadata) == string::utf8(b"mutated_symbol"), 9);
assert!(symbol(metadata) == string::utf8(b"m_symbol"), 9);
assert!(decimals(metadata) == 0, 10);
assert!(icon_uri(metadata) == string::utf8(b"http://www.example.com/favicon.ico"), 11);
assert!(project_uri(metadata) == string::utf8(b"http://www.example.com"), 12);
Expand Down Expand Up @@ -1392,13 +1402,13 @@ module aptos_framework::fungible_asset {
mutate_metadata(
&mutate_metadata_ref,
option::some(string::utf8(b"mutated_name")),
option::some(string::utf8(b"mutated_symbol")),
option::some(string::utf8(b"m_symbol")),
option::some(10),
option::some(string::utf8(b"http://www.mutated-example.com/favicon.ico")),
option::some(string::utf8(b"http://www.mutated-example.com"))
);
assert!(name(metadata) == string::utf8(b"mutated_name"), 1);
assert!(symbol(metadata) == string::utf8(b"mutated_symbol"), 2);
assert!(symbol(metadata) == string::utf8(b"m_symbol"), 2);
assert!(decimals(metadata) == 10, 3);
assert!(icon_uri(metadata) == string::utf8(b"http://www.mutated-example.com/favicon.ico"), 4);
assert!(project_uri(metadata) == string::utf8(b"http://www.mutated-example.com"), 5);
Expand All @@ -1414,18 +1424,115 @@ module aptos_framework::fungible_asset {
mutate_metadata(
&mutate_metadata_ref,
option::some(string::utf8(b"mutated_name")),
option::some(string::utf8(b"mutated_symbol")),
option::some(string::utf8(b"m_symbol")),
option::none(),
option::none(),
option::none()
);
assert!(name(metadata) == string::utf8(b"mutated_name"), 8);
assert!(symbol(metadata) == string::utf8(b"mutated_symbol"), 9);
assert!(symbol(metadata) == string::utf8(b"m_symbol"), 9);
assert!(decimals(metadata) == 0, 10);
assert!(icon_uri(metadata) == string::utf8(b"http://www.example.com/favicon.ico"), 11);
assert!(project_uri(metadata) == string::utf8(b"http://www.example.com"), 12);
}

#[test(creator = @0xcafe)]
#[expected_failure(abort_code = 0x2000f, location = Self)]
fun test_mutate_metadata_name_over_maximum_length(
creator: &signer
) acquires Metadata {
let (_mint_ref, _transfer_ref, _burn_ref, mutate_metadata_ref, _) = create_fungible_asset(creator);

mutate_metadata(
&mutate_metadata_ref,
option::some(string::utf8(b"mutated_name_will_be_too_long_for_the_maximum_length_check")),
option::none(),
option::none(),
option::none(),
option::none()
);
}

#[test(creator = @0xcafe)]
#[expected_failure(abort_code = 0x20010, location = Self)]
fun test_mutate_metadata_symbol_over_maximum_length(
creator: &signer
) acquires Metadata {
let (_mint_ref, _transfer_ref, _burn_ref, mutate_metadata_ref, _) = create_fungible_asset(creator);

mutate_metadata(
&mutate_metadata_ref,
option::none(),
option::some(string::utf8(b"mutated_symbol_will_be_too_long_for_the_maximum_length_check")),
option::none(),
option::none(),
option::none()
);
}

#[test(creator = @0xcafe)]
#[expected_failure(abort_code = 0x20011, location = Self)]
fun test_mutate_metadata_decimals_over_maximum_amount(
creator: &signer
) acquires Metadata {
let (_mint_ref, _transfer_ref, _burn_ref, mutate_metadata_ref, _) = create_fungible_asset(creator);

mutate_metadata(
&mutate_metadata_ref,
option::none(),
option::none(),
option::some(50),
option::none(),
option::none()
);
}

#[test_only]
fun create_exceedingly_long_uri(): vector<u8> {
use std::vector;

let too_long_of_uri = b"mutated_uri_will_be_too_long_for_the_maximum_length_check.com/";
for (i in 0..50) {
vector::append(&mut too_long_of_uri, b"too_long_of_uri");
};

too_long_of_uri
}

#[test(creator = @0xcafe)]
#[expected_failure(abort_code = 0x20013, location = Self)]
fun test_mutate_metadata_icon_uri_over_maximum_length(
creator: &signer
) acquires Metadata {
let (_mint_ref, _transfer_ref, _burn_ref, mutate_metadata_ref, _) = create_fungible_asset(creator);
let too_long_of_uri = create_exceedingly_long_uri();
mutate_metadata(
&mutate_metadata_ref,
option::none(),
option::none(),
option::none(),
option::some(string::utf8(too_long_of_uri)),
option::none()
);
}

#[test(creator = @0xcafe)]
#[expected_failure(abort_code = 0x20013, location = Self)]
fun test_mutate_metadata_project_uri_over_maximum_length(
creator: &signer
) acquires Metadata {
let (_mint_ref, _transfer_ref, _burn_ref, mutate_metadata_ref, _) = create_fungible_asset(creator);
let too_long_of_uri = create_exceedingly_long_uri();
mutate_metadata(
&mutate_metadata_ref,
option::none(),
option::none(),
option::none(),
option::none(),
option::some(string::utf8(too_long_of_uri))
);
}

#[test(creator = @0xcafe)]
fun test_merge_and_exact(creator: &signer) acquires Supply, ConcurrentSupply {
let (mint_ref, _transfer_ref, burn_ref, _mutate_metadata_ref, _) = create_fungible_asset(creator);
Expand Down

0 comments on commit 1365a99

Please sign in to comment.