From e504d112d168121e5a724cf01a7aa0baaee6736c Mon Sep 17 00:00:00 2001 From: Aaron Date: Thu, 16 Jan 2025 11:50:48 +0800 Subject: [PATCH] [fa] add back migration_flag for exchanges (#15738) --- .../framework/aptos-framework/doc/coin.md | 19 ++++----- .../aptos-framework/sources/coin.move | 40 +++++++++---------- 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/aptos-move/framework/aptos-framework/doc/coin.md b/aptos-move/framework/aptos-framework/doc/coin.md index 4c82eabb2d92b..8371b6ef9fd0e 100644 --- a/aptos-move/framework/aptos-framework/doc/coin.md +++ b/aptos-move/framework/aptos-framework/doc/coin.md @@ -950,7 +950,6 @@ The flag the existence of which indicates the primary fungible store is created
#[resource_group_member(#[group = 0x1::object::ObjectGroup])]
-#[deprecated]
 struct MigrationFlag has key
 
@@ -2092,6 +2091,7 @@ or disallow upgradability of total supply. let metadata = ensure_paired_metadata<CoinType>(); let store = primary_fungible_store::ensure_primary_store_exists(account, metadata); + let store_address = object::object_address(&store); if (exists<CoinStore<CoinType>>(account)) { let CoinStore<CoinType> { coin, frozen, deposit_events, withdraw_events } = move_from<CoinStore<CoinType>>( account @@ -2122,6 +2122,9 @@ or disallow upgradability of total supply. fungible_asset::set_frozen_flag_internal(store, frozen); } }; + if (!exists<MigrationFlag>(store_address)) { + move_to(&create_signer::create_signer(store_address), MigrationFlag {}); + } } @@ -2707,13 +2710,11 @@ Deposit the coin balance into the recipient's account and emit an event. account_address: address, metadata: Object<Metadata> ): bool { - (features::new_accounts_default_to_fa_apt_store_enabled() && object::object_address(&metadata) == @0xa) || { - let primary_store_address = primary_fungible_store::primary_store_address<Metadata>( - account_address, - metadata - ); - fungible_asset::store_exists(primary_store_address) - } + let primary_store_address = primary_fungible_store::primary_store_address<Metadata>(account_address, metadata); + fungible_asset::store_exists(primary_store_address) && ( + // migration flag is needed, until we start defaulting new accounts to APT PFS + features::new_accounts_default_to_fa_apt_store_enabled() || exists<MigrationFlag>(primary_store_address) + ) } @@ -2753,7 +2754,7 @@ This is for internal use only and doesn't emit an DepositEvent. )) { let fa = coin_to_fungible_asset(coin); let metadata = fungible_asset::asset_metadata(&fa); - let store = primary_fungible_store::ensure_primary_store_exists(account_addr, metadata); + let store = primary_fungible_store::primary_store(account_addr, metadata); fungible_asset::unchecked_deposit_with_no_events(object::object_address(&store), fa); } else { abort error::not_found(ECOIN_STORE_NOT_PUBLISHED) diff --git a/aptos-move/framework/aptos-framework/sources/coin.move b/aptos-move/framework/aptos-framework/sources/coin.move index a396b121b82de..60f94fe0435b4 100644 --- a/aptos-move/framework/aptos-framework/sources/coin.move +++ b/aptos-move/framework/aptos-framework/sources/coin.move @@ -580,6 +580,7 @@ module aptos_framework::coin { let metadata = ensure_paired_metadata(); let store = primary_fungible_store::ensure_primary_store_exists(account, metadata); + let store_address = object::object_address(&store); if (exists>(account)) { let CoinStore { coin, frozen, deposit_events, withdraw_events } = move_from>( account @@ -610,6 +611,9 @@ module aptos_framework::coin { fungible_asset::set_frozen_flag_internal(store, frozen); } }; + if (!exists(store_address)) { + move_to(&create_signer::create_signer(store_address), MigrationFlag {}); + } } /// Voluntarily migrate to fungible store for `CoinType` if not yet. @@ -842,13 +846,11 @@ module aptos_framework::coin { account_address: address, metadata: Object ): bool { - (features::new_accounts_default_to_fa_apt_store_enabled() && object::object_address(&metadata) == @0xa) || { - let primary_store_address = primary_fungible_store::primary_store_address( - account_address, - metadata - ); - fungible_asset::store_exists(primary_store_address) - } + let primary_store_address = primary_fungible_store::primary_store_address(account_address, metadata); + fungible_asset::store_exists(primary_store_address) && ( + // migration flag is needed, until we start defaulting new accounts to APT PFS + features::new_accounts_default_to_fa_apt_store_enabled() || exists(primary_store_address) + ) } /// Deposit the coin balance into the recipient's account without checking if the account is frozen. @@ -868,7 +870,7 @@ module aptos_framework::coin { )) { let fa = coin_to_fungible_asset(coin); let metadata = fungible_asset::asset_metadata(&fa); - let store = primary_fungible_store::ensure_primary_store_exists(account_addr, metadata); + let store = primary_fungible_store::primary_store(account_addr, metadata); fungible_asset::unchecked_deposit_with_no_events(object::object_address(&store), fa); } else { abort error::not_found(ECOIN_STORE_NOT_PUBLISHED) @@ -1924,6 +1926,7 @@ module aptos_framework::coin { } #[test(account = @aptos_framework, aaron = @0xaa10, bob = @0xb0b)] + #[expected_failure(abort_code = 0x60005, location = Self)] fun test_force_deposit( account: &signer, aaron: &signer, @@ -1937,7 +1940,6 @@ module aptos_framework::coin { account::create_account_for_test(bob_addr); let (burn_cap, freeze_cap, mint_cap) = initialize_and_register_fake_money(account, 1, true); maybe_convert_to_fungible_store(aaron_addr); - maybe_convert_to_fungible_store(bob_addr); deposit(aaron_addr, mint(1, &mint_cap)); force_deposit(account_addr, mint(100, &mint_cap)); @@ -1967,8 +1969,6 @@ module aptos_framework::coin { account::create_account_for_test(account_addr); account::create_account_for_test(aaron_addr); account::create_account_for_test(bob_addr); - let feature = features::get_new_accounts_default_to_fa_apt_store_feature(); - features::change_feature_flags_for_testing(account, vector[], vector[feature]); let (burn_cap, freeze_cap, mint_cap) = initialize_and_register_fake_money(account, 1, true); assert!(coin_store_exists(account_addr), 0); @@ -1977,13 +1977,6 @@ module aptos_framework::coin { assert!(!coin_store_exists(aaron_addr), 0); assert!(!is_account_registered(aaron_addr), 0); - register(bob); - assert!(coin_store_exists(bob_addr), 0); - maybe_convert_to_fungible_store(bob_addr); - assert!(!coin_store_exists(bob_addr), 0); - register(bob); - assert!(!coin_store_exists(bob_addr), 0); - maybe_convert_to_fungible_store(aaron_addr); let coin = mint(100, &mint_cap); deposit(aaron_addr, coin); @@ -1995,8 +1988,15 @@ module aptos_framework::coin { assert!(!coin_store_exists(account_addr), 0); assert!(is_account_registered(account_addr), 0); + // Deposit FA to bob to created primary fungible store without `MigrationFlag`. primary_fungible_store::deposit(bob_addr, coin_to_fungible_asset(mint(100, &mint_cap))); assert!(!coin_store_exists(bob_addr), 0); + register(bob); + assert!(coin_store_exists(bob_addr), 0); + maybe_convert_to_fungible_store(bob_addr); + assert!(!coin_store_exists(bob_addr), 0); + register(bob); + assert!(!coin_store_exists(bob_addr), 0); move_to(account, FakeMoneyCapabilities { burn_cap, @@ -2018,8 +2018,9 @@ module aptos_framework::coin { assert!(coin_balance(account_addr) == 0, 0); assert!(balance(account_addr) == 100, 0); let coin = withdraw(account, 50); - assert!(can_receive_paired_fungible_asset(account_addr, ensure_paired_metadata()), 0); + assert!(!can_receive_paired_fungible_asset(account_addr, ensure_paired_metadata()), 0); maybe_convert_to_fungible_store(account_addr); + assert!(can_receive_paired_fungible_asset(account_addr, ensure_paired_metadata()), 0); deposit(account_addr, coin); assert!(coin_balance(account_addr) == 0, 0); assert!(balance(account_addr) == 100, 0); @@ -2031,7 +2032,6 @@ module aptos_framework::coin { }); } - #[deprecated] #[resource_group_member(group = aptos_framework::object::ObjectGroup)] /// The flag the existence of which indicates the primary fungible store is created by the migration from CoinStore. struct MigrationFlag has key {}