From e7be301d7d4c2e3c066ee45977b9b252d6c1e174 Mon Sep 17 00:00:00 2001 From: welbon <2261238+welbon@users.noreply.github.com> Date: Tue, 3 Dec 2024 21:32:26 +0800 Subject: [PATCH 1/5] [compiler-v2 framework] add some debug informations --- vm/framework/move-stdlib/doc/features.md | 6 ++- .../move-stdlib/sources/configs/features.move | 6 ++- .../starcoin-framework/doc/account.md | 21 +++------- vm/framework/starcoin-framework/doc/coin.md | 1 - .../doc/primary_fungible_store.md | 24 ++++++++++-- .../starcoin-framework/sources/account.move | 39 ++++++++----------- .../starcoin-framework/sources/coin.move | 1 - .../sources/primary_fungible_store.move | 26 ++++++++++--- .../sources/stc/stc_block.move | 4 -- 9 files changed, 70 insertions(+), 58 deletions(-) diff --git a/vm/framework/move-stdlib/doc/features.md b/vm/framework/move-stdlib/doc/features.md index 06f9dd9250..34938834d1 100644 --- a/vm/framework/move-stdlib/doc/features.md +++ b/vm/framework/move-stdlib/doc/features.md @@ -2988,8 +2988,10 @@ Lifetime: transient Implementation -
public fun new_accounts_default_to_fa_stc_store_enabled(): bool acquires Features {
-    is_enabled(NEW_ACCOUNTS_DEFAULT_TO_FA_APT_STORE)
+
public fun new_accounts_default_to_fa_stc_store_enabled(): bool {
+    // TODO(BobOng): [framework-upgrade] to confirm which feature flag should be used here
+    // is_enabled(NEW_ACCOUNTS_DEFAULT_TO_FA_APT_STORE)
+    false
 }
 
diff --git a/vm/framework/move-stdlib/sources/configs/features.move b/vm/framework/move-stdlib/sources/configs/features.move index 8894381f60..1cc63752a1 100644 --- a/vm/framework/move-stdlib/sources/configs/features.move +++ b/vm/framework/move-stdlib/sources/configs/features.move @@ -536,8 +536,10 @@ module std::features { public fun get_new_accounts_default_to_fa_apt_store_feature(): u64 { NEW_ACCOUNTS_DEFAULT_TO_FA_APT_STORE } - public fun new_accounts_default_to_fa_stc_store_enabled(): bool acquires Features { - is_enabled(NEW_ACCOUNTS_DEFAULT_TO_FA_APT_STORE) + public fun new_accounts_default_to_fa_stc_store_enabled(): bool { + // TODO(BobOng): [framework-upgrade] to confirm which feature flag should be used here + // is_enabled(NEW_ACCOUNTS_DEFAULT_TO_FA_APT_STORE) + false } /// Lifetime: transient diff --git a/vm/framework/starcoin-framework/doc/account.md b/vm/framework/starcoin-framework/doc/account.md index d532072446..b87c2a2284 100644 --- a/vm/framework/starcoin-framework/doc/account.md +++ b/vm/framework/starcoin-framework/doc/account.md @@ -96,6 +96,7 @@
use 0x1::bcs;
+use 0x1::bcs_util;
 use 0x1::chain_id;
 use 0x1::create_signer;
 use 0x1::debug;
@@ -1294,7 +1295,7 @@ to rotate his address to Alice's address in the first place.
     };
 
     // Construct a valid `RotationProofChallenge` that `cap_rotate_key` and `cap_update_table` will validate against.
-    let curr_auth_key_as_address = from_bcs::to_address(account_resource.authentication_key);
+    let curr_auth_key_as_address = from_bcs::to_address(bcs_util::truncate_16(account_resource.authentication_key));
     let challenge = RotationProofChallenge {
         sequence_number: account_resource.sequence_number,
         originator: addr,
@@ -1357,7 +1358,7 @@ to rotate his address to Alice's address in the first place.
         error::not_found(ENO_SUCH_ROTATION_CAPABILITY_OFFER)
     );
 
-    let curr_auth_key = from_bcs::to_address(offerer_account_resource.authentication_key);
+    let curr_auth_key = from_bcs::to_address(bcs_util::truncate_16(offerer_account_resource.authentication_key));
     let challenge = RotationProofChallenge {
         sequence_number: get_sequence_number(delegate_address),
         originator: rotation_cap_offerer_address,
@@ -1893,7 +1894,7 @@ in the event of key recovery.
     };
 
     // Set `OriginatingAddress[new_auth_key] = originating_address`.
-    let new_auth_key = from_bcs::to_address(new_auth_key_vector);
+    let new_auth_key = from_bcs::to_address(bcs_util::truncate_16(new_auth_key_vector));
     table::add(address_map, new_auth_key, originating_addr);
 
     if (std::features::module_event_migration_enabled()) {
@@ -1942,7 +1943,7 @@ involves the use of a cryptographic hash operation and should be use thoughtfull
     let bytes = bcs::to_bytes(source);
     vector::append(&mut bytes, seed);
     vector::push_back(&mut bytes, DERIVE_RESOURCE_ACCOUNT_SCHEME);
-    from_bcs::to_address(hash::sha3_256(bytes))
+    from_bcs::to_address(bcs_util::truncate_16(hash::sha3_256(bytes)))
 }
 
@@ -2050,17 +2051,7 @@ Convert from authentication key to address
public fun auth_key_to_address(authentication_key: vector<u8>): address {
-    assert!(vector::length(&authentication_key) == 32, error::invalid_argument(EMALFORMED_AUTHENTICATION_KEY));
-    let address_bytes = vector::empty<u8>();
-
-    let i = 16;
-    while (i < 32) {
-        let b = *vector::borrow(&authentication_key, i);
-        vector::push_back(&mut address_bytes, b);
-        i = i + 1;
-    };
-
-    from_bcs::to_address(address_bytes)
+    from_bcs::to_address(bcs_util::truncate_16(authentication_key))
 }
 
diff --git a/vm/framework/starcoin-framework/doc/coin.md b/vm/framework/starcoin-framework/doc/coin.md index 24909b1d02..100b4c56ff 100644 --- a/vm/framework/starcoin-framework/doc/coin.md +++ b/vm/framework/starcoin-framework/doc/coin.md @@ -1462,7 +1462,6 @@ Create STC pairing by passing StarcoinCoin. ) }; - debug::print(&std::string::utf8(b"coin::create_and_return_paired_metadata_if_not_exist | 5")); primary_fungible_store::create_primary_store_enabled_fungible_asset( &metadata_object_cref, option::none(), diff --git a/vm/framework/starcoin-framework/doc/primary_fungible_store.md b/vm/framework/starcoin-framework/doc/primary_fungible_store.md index 87d98d08eb..5043b16dca 100644 --- a/vm/framework/starcoin-framework/doc/primary_fungible_store.md +++ b/vm/framework/starcoin-framework/doc/primary_fungible_store.md @@ -48,7 +48,8 @@ fungible asset to it. This emits an deposit event. - [Module-level Specification](#module-level-spec) -
use 0x1::dispatchable_fungible_asset;
+
use 0x1::debug;
+use 0x1::dispatchable_fungible_asset;
 use 0x1::fungible_asset;
 use 0x1::object;
 use 0x1::option;
@@ -156,12 +157,15 @@ Ensure that the primary store object for the given address exists. If it doesn't
     owner: address,
     metadata: Object<T>,
 ): Object<FungibleStore> acquires DeriveRefPod {
+    debug::print(&string::utf8(b"primary_fungible_store::ensure_primary_store_exists | entered"));
     let store_addr = primary_store_address(owner, metadata);
-    if (fungible_asset::store_exists(store_addr)) {
+    let ret = if (fungible_asset::store_exists(store_addr)) {
         object::address_to_object(store_addr)
     } else {
         create_primary_store(owner, metadata)
-    }
+    };
+    debug::print(&string::utf8(b"primary_fungible_store::ensure_primary_store_exists | exited"));
+    ret
 }
 
@@ -189,6 +193,10 @@ Create a primary store object to hold fungible asset for the given address. owner_addr: address, metadata: Object<T>, ): Object<FungibleStore> acquires DeriveRefPod { + debug::print(&string::utf8(b"primary_fungible_store::create_primary_store | entered")); + debug::print(&owner_addr); + debug::print(&metadata); + let metadata_addr = object::object_address(&metadata); object::address_to_object<Metadata>(metadata_addr); let derive_ref = &borrow_global<DeriveRefPod>(metadata_addr).metadata_derive_ref; @@ -197,7 +205,11 @@ Create a primary store object to hold fungible asset for the given address. let transfer_ref = &object::generate_transfer_ref(constructor_ref); object::disable_ungated_transfer(transfer_ref); - fungible_asset::create_store(constructor_ref, metadata) + let ret = fungible_asset::create_store(constructor_ref, metadata); + debug::print(&string::utf8(b"primary_fungible_store::create_primary_store | exited")); + debug::print(&ret); + + ret }
@@ -499,9 +511,13 @@ Deposit fungible asset fa to the given account's primary store.
public fun deposit(owner: address, fa: FungibleAsset) acquires DeriveRefPod {
+    debug::print(&string::utf8(b"primary_fungible_store::deposit | entered"));
+
     let metadata = fungible_asset::asset_metadata(&fa);
     let store = ensure_primary_store_exists(owner, metadata);
     dispatchable_fungible_asset::deposit(store, fa);
+
+    debug::print(&string::utf8(b"primary_fungible_store::deposit | exited"));
 }
 
diff --git a/vm/framework/starcoin-framework/sources/account.move b/vm/framework/starcoin-framework/sources/account.move index fa15c74e76..553b2e717c 100644 --- a/vm/framework/starcoin-framework/sources/account.move +++ b/vm/framework/starcoin-framework/sources/account.move @@ -4,7 +4,9 @@ module starcoin_framework::account { use std::hash; use std::option::{Self, Option}; use std::signer; + use std::string; use std::vector; + use starcoin_framework::bcs_util; use starcoin_framework::chain_id; use starcoin_framework::create_signer::create_signer; @@ -366,7 +368,7 @@ module starcoin_framework::account { }; // Construct a valid `RotationProofChallenge` that `cap_rotate_key` and `cap_update_table` will validate against. - let curr_auth_key_as_address = from_bcs::to_address(account_resource.authentication_key); + let curr_auth_key_as_address = from_bcs::to_address(bcs_util::truncate_16(account_resource.authentication_key)); let challenge = RotationProofChallenge { sequence_number: account_resource.sequence_number, originator: addr, @@ -409,7 +411,7 @@ module starcoin_framework::account { error::not_found(ENO_SUCH_ROTATION_CAPABILITY_OFFER) ); - let curr_auth_key = from_bcs::to_address(offerer_account_resource.authentication_key); + let curr_auth_key = from_bcs::to_address(bcs_util::truncate_16(offerer_account_resource.authentication_key)); let challenge = RotationProofChallenge { sequence_number: get_sequence_number(delegate_address), originator: rotation_cap_offerer_address, @@ -687,7 +689,7 @@ module starcoin_framework::account { }; // Set `OriginatingAddress[new_auth_key] = originating_address`. - let new_auth_key = from_bcs::to_address(new_auth_key_vector); + let new_auth_key = from_bcs::to_address(bcs_util::truncate_16(new_auth_key_vector)); table::add(address_map, new_auth_key, originating_addr); if (std::features::module_event_migration_enabled()) { @@ -719,7 +721,7 @@ module starcoin_framework::account { let bytes = bcs::to_bytes(source); vector::append(&mut bytes, seed); vector::push_back(&mut bytes, DERIVE_RESOURCE_ACCOUNT_SCHEME); - from_bcs::to_address(hash::sha3_256(bytes)) + from_bcs::to_address(bcs_util::truncate_16(hash::sha3_256(bytes))) } /// A resource account is used to manage resources independent of an account managed by a user. @@ -767,17 +769,7 @@ module starcoin_framework::account { /// Convert from authentication key to address public fun auth_key_to_address(authentication_key: vector): address { - assert!(vector::length(&authentication_key) == 32, error::invalid_argument(EMALFORMED_AUTHENTICATION_KEY)); - let address_bytes = vector::empty(); - - let i = 16; - while (i < 32) { - let b = *vector::borrow(&authentication_key, i); - vector::push_back(&mut address_bytes, b); - i = i + 1; - }; - - from_bcs::to_address(address_bytes) + from_bcs::to_address(bcs_util::truncate_16(authentication_key)) } /// create the account for system reserved addresses @@ -1091,11 +1083,12 @@ module starcoin_framework::account { } #[test_only] - public fun create_account_from_ed25519_public_key(pk_bytes: vector): signer { + public fun create_account_from_ed25519_public_key(pk_bytes: vector): signer acquires Account { let pk = ed25519::new_unvalidated_public_key_from_bytes(pk_bytes); let curr_auth_key = ed25519::unvalidated_public_key_to_authentication_key(&pk); - let alice_address = from_bcs::to_address(curr_auth_key); + let alice_address = from_bcs::to_address(bcs_util::truncate_16(curr_auth_key)); let alice = create_account_unchecked(alice_address); + rotate_authentication_key_internal(&alice, curr_auth_key); alice } @@ -1104,7 +1097,7 @@ module starcoin_framework::account { // #[test(bob = @0x345)] - #[expected_failure(abort_code = 65544, location = Self)] + #[expected_failure(abort_code = 65544, location = starcoin_framework::account)] public entry fun test_invalid_offer_signer_capability(bob: signer) acquires Account { let (_alice_sk, alice_pk) = ed25519::generate_keys(); let alice_pk_bytes = ed25519::validated_public_key_to_bytes(&alice_pk); @@ -1432,13 +1425,13 @@ module starcoin_framework::account { let (curr_sk, curr_pk) = multi_ed25519::generate_keys(2, 3); let curr_pk_unvalidated = multi_ed25519::public_key_to_unvalidated(&curr_pk); let curr_auth_key = multi_ed25519::unvalidated_public_key_to_authentication_key(&curr_pk_unvalidated); - let alice_addr = from_bcs::to_address(curr_auth_key); + let alice_addr = from_bcs::to_address(bcs_util::truncate_16(curr_auth_key)); let alice = create_account_unchecked(alice_addr); let (new_sk, new_pk) = multi_ed25519::generate_keys(4, 5); let new_pk_unvalidated = multi_ed25519::public_key_to_unvalidated(&new_pk); let new_auth_key = multi_ed25519::unvalidated_public_key_to_authentication_key(&new_pk_unvalidated); - let new_address = from_bcs::to_address(new_auth_key); + let new_address = from_bcs::to_address(bcs_util::truncate_16(new_auth_key)); let challenge = RotationProofChallenge { sequence_number: borrow_global(alice_addr).sequence_number, @@ -1474,7 +1467,7 @@ module starcoin_framework::account { let (curr_sk, curr_pk) = multi_ed25519::generate_keys(2, 3); let curr_pk_unvalidated = multi_ed25519::public_key_to_unvalidated(&curr_pk); let curr_auth_key = multi_ed25519::unvalidated_public_key_to_authentication_key(&curr_pk_unvalidated); - let alice_addr = from_bcs::to_address(curr_auth_key); + let alice_addr = from_bcs::to_address(bcs_util::truncate_16(curr_auth_key)); let alice = create_account_unchecked(alice_addr); let account_resource = borrow_global_mut(alice_addr); @@ -1482,7 +1475,7 @@ module starcoin_framework::account { let (new_sk, new_pk) = ed25519::generate_keys(); let new_pk_unvalidated = ed25519::public_key_to_unvalidated(&new_pk); let new_auth_key = ed25519::unvalidated_public_key_to_authentication_key(&new_pk_unvalidated); - let new_addr = from_bcs::to_address(new_auth_key); + let new_addr = from_bcs::to_address(bcs_util::truncate_16(new_auth_key)); let challenge = RotationProofChallenge { sequence_number: account_resource.sequence_number, @@ -1521,7 +1514,7 @@ module starcoin_framework::account { let (_new_sk, new_pk) = ed25519::generate_keys(); let new_pk_unvalidated = ed25519::public_key_to_unvalidated(&new_pk); let new_auth_key = ed25519::unvalidated_public_key_to_authentication_key(&new_pk_unvalidated); - let _new_addr = from_bcs::to_address(new_auth_key); + let _new_addr = from_bcs::to_address(bcs_util::truncate_16(new_auth_key)); rotate_authentication_key_call(&alice, new_auth_key); assert!(borrow_global(alice_addr).authentication_key == new_auth_key, 0); diff --git a/vm/framework/starcoin-framework/sources/coin.move b/vm/framework/starcoin-framework/sources/coin.move index f42e9606ec..a50352c14d 100644 --- a/vm/framework/starcoin-framework/sources/coin.move +++ b/vm/framework/starcoin-framework/sources/coin.move @@ -332,7 +332,6 @@ module starcoin_framework::coin { ) }; - debug::print(&std::string::utf8(b"coin::create_and_return_paired_metadata_if_not_exist | 5")); primary_fungible_store::create_primary_store_enabled_fungible_asset( &metadata_object_cref, option::none(), diff --git a/vm/framework/starcoin-framework/sources/primary_fungible_store.move b/vm/framework/starcoin-framework/sources/primary_fungible_store.move index ca4965b241..a2cf51daa2 100644 --- a/vm/framework/starcoin-framework/sources/primary_fungible_store.move +++ b/vm/framework/starcoin-framework/sources/primary_fungible_store.move @@ -18,7 +18,8 @@ module starcoin_framework::primary_fungible_store { use std::option::Option; use std::signer; - use std::string::String; + use std::string::{Self, String}; + use starcoin_std::debug; #[resource_group_member(group = starcoin_framework::object::ObjectGroup)] /// A resource that holds the derive ref for the fungible asset metadata object. This is used to create primary @@ -60,12 +61,15 @@ module starcoin_framework::primary_fungible_store { owner: address, metadata: Object, ): Object acquires DeriveRefPod { + debug::print(&string::utf8(b"primary_fungible_store::ensure_primary_store_exists | entered")); let store_addr = primary_store_address(owner, metadata); - if (fungible_asset::store_exists(store_addr)) { + let ret = if (fungible_asset::store_exists(store_addr)) { object::address_to_object(store_addr) } else { create_primary_store(owner, metadata) - } + }; + debug::print(&string::utf8(b"primary_fungible_store::ensure_primary_store_exists | exited")); + ret } /// Create a primary store object to hold fungible asset for the given address. @@ -73,6 +77,10 @@ module starcoin_framework::primary_fungible_store { owner_addr: address, metadata: Object, ): Object acquires DeriveRefPod { + debug::print(&string::utf8(b"primary_fungible_store::create_primary_store | entered")); + debug::print(&owner_addr); + debug::print(&metadata); + let metadata_addr = object::object_address(&metadata); object::address_to_object(metadata_addr); let derive_ref = &borrow_global(metadata_addr).metadata_derive_ref; @@ -81,7 +89,11 @@ module starcoin_framework::primary_fungible_store { let transfer_ref = &object::generate_transfer_ref(constructor_ref); object::disable_ungated_transfer(transfer_ref); - fungible_asset::create_store(constructor_ref, metadata) + let ret = fungible_asset::create_store(constructor_ref, metadata); + debug::print(&string::utf8(b"primary_fungible_store::create_primary_store | exited")); + debug::print(&ret); + + ret } #[view] @@ -163,9 +175,13 @@ module starcoin_framework::primary_fungible_store { /// Deposit fungible asset `fa` to the given account's primary store. public fun deposit(owner: address, fa: FungibleAsset) acquires DeriveRefPod { + debug::print(&string::utf8(b"primary_fungible_store::deposit | entered")); + let metadata = fungible_asset::asset_metadata(&fa); let store = ensure_primary_store_exists(owner, metadata); dispatchable_fungible_asset::deposit(store, fa); + + debug::print(&string::utf8(b"primary_fungible_store::deposit | exited")); } /// Deposit fungible asset `fa` to the given account's primary store. @@ -270,8 +286,6 @@ module starcoin_framework::primary_fungible_store { generate_transfer_ref }; #[test_only] - use std::string; - #[test_only] use std::option; #[test_only] diff --git a/vm/framework/starcoin-framework/sources/stc/stc_block.move b/vm/framework/starcoin-framework/sources/stc/stc_block.move index fdaaeffa24..461290a5b6 100644 --- a/vm/framework/starcoin-framework/sources/stc/stc_block.move +++ b/vm/framework/starcoin-framework/sources/stc/stc_block.move @@ -21,11 +21,7 @@ module starcoin_framework::stc_block { #[test_only] use std::hash; #[test_only] - use starcoin_framework::account::{create_signer_for_test, create_account_if_does_not_exist}; - #[test_only] use starcoin_framework::bcs_util; - #[test_only] - use starcoin_framework::starcoin_account::create_account; const BLOCK_INTERVAL_NUMBER: u64 = 5; const CHECKPOINT_LENGTH: u64 = 60; From 80d698f3cd63007b7093102c71a49eb3442dbb28 Mon Sep 17 00:00:00 2001 From: welbon <2261238+welbon@users.noreply.github.com> Date: Wed, 4 Dec 2024 13:38:51 +0800 Subject: [PATCH 2/5] [compiler-v2 framework] fixed the unittest erro caused by auth key --- .../starcoin-framework/doc/account.md | 61 ++++++++++++------ .../doc/resource_account.md | 21 ++++-- .../starcoin-framework/sources/account.move | 64 ++++++++++++------- .../sources/account.spec.move | 20 +++--- .../sources/resource_account.move | 8 +-- .../sources/resource_account.spec.move | 24 ++++--- 6 files changed, 129 insertions(+), 69 deletions(-) diff --git a/vm/framework/starcoin-framework/doc/account.md b/vm/framework/starcoin-framework/doc/account.md index b87c2a2284..59308e9ff3 100644 --- a/vm/framework/starcoin-framework/doc/account.md +++ b/vm/framework/starcoin-framework/doc/account.md @@ -958,7 +958,7 @@ is returned. This way, the caller of this function can publish additional resour error::invalid_argument(ECANNOT_RESERVED_ADDRESS) ); - let signer = create_account_unchecked(new_address); + let signer = create_account_unchecked(new_address, vector::empty<u8>()); debug::print(&std::string::utf8(b"account::create_account | Exited")); @@ -976,7 +976,7 @@ is returned. This way, the caller of this function can publish additional resour -
fun create_account_unchecked(new_address: address): signer
+
fun create_account_unchecked(new_address: address, authentication_key: vector<u8>): signer
 
@@ -985,10 +985,11 @@ is returned. This way, the caller of this function can publish additional resour Implementation -
fun create_account_unchecked(new_address: address): signer {
+
fun create_account_unchecked(new_address: address, authentication_key: vector<u8>): signer {
     let new_account = create_signer(new_address);
-    // fixme: create authentication key from address.
-    let authentication_key = ZERO_AUTH_KEY;
+    if (vector::is_empty(&authentication_key)) {
+        authentication_key = ZERO_AUTH_KEY
+    };
     assert!(
         vector::length(&authentication_key) == 32,
         error::invalid_argument(EMALFORMED_AUTHENTICATION_KEY)
@@ -1271,6 +1272,8 @@ to rotate his address to Alice's address in the first place.
     cap_rotate_key: vector<u8>,
     cap_update_table: vector<u8>,
 ) acquires Account, OriginatingAddress {
+    debug::print(&string::utf8(b"account::rotate_authentication_key | entered"));
+
     let addr = signer::address_of(account);
     assert!(exists_at(addr), error::not_found(EACCOUNT_DOES_NOT_EXIST));
     let account_resource = borrow_global_mut<Account>(addr);
@@ -1294,8 +1297,12 @@ to rotate his address to Alice's address in the first place.
         abort error::invalid_argument(EINVALID_SCHEME)
     };
 
+    debug::print(&string::utf8(b"starcoin_framework::rotate_authentication_key | curr_auth_key_as_address"));
+    let truncated_authentication_key = bcs_util::truncate_16(account_resource.authentication_key);
+    debug::print(&truncated_authentication_key);
+
     // Construct a valid `RotationProofChallenge` that `cap_rotate_key` and `cap_update_table` will validate against.
-    let curr_auth_key_as_address = from_bcs::to_address(bcs_util::truncate_16(account_resource.authentication_key));
+    let curr_auth_key_as_address = from_bcs::to_address(truncated_authentication_key);
     let challenge = RotationProofChallenge {
         sequence_number: account_resource.sequence_number,
         originator: addr,
@@ -1319,6 +1326,8 @@ to rotate his address to Alice's address in the first place.
 
     // Update the `OriginatingAddress` table.
     update_auth_key_and_originating_address_table(addr, account_resource, new_auth_key);
+
+    debug::print(&string::utf8(b"account::rotate_authentication_key | exited"));
 }
 
@@ -1871,8 +1880,10 @@ in the event of key recovery. account_resource: &mut Account, new_auth_key_vector: vector<u8>, ) acquires OriginatingAddress { + debug::print(&string::utf8(b"account::update_auth_key_and_originating_address_table | entered")); + let address_map = &mut borrow_global_mut<OriginatingAddress>(@starcoin_framework).address_map; - let curr_auth_key = from_bcs::to_address(account_resource.authentication_key); + let curr_auth_key = from_bcs::to_address(bcs_util::truncate_16(account_resource.authentication_key)); // Checks `OriginatingAddress[curr_auth_key]` is either unmapped, or mapped to `originating_address`. // If it's mapped to the originating address, removes that mapping. @@ -1893,8 +1904,14 @@ in the event of key recovery. ); }; + // Set `OriginatingAddress[new_auth_key] = originating_address`. let new_auth_key = from_bcs::to_address(bcs_util::truncate_16(new_auth_key_vector)); + + debug::print(&string::utf8(b"account::update_auth_key_and_originating_address_table | new_auth_key")); + debug::print(&new_auth_key); + + table::add(address_map, new_auth_key, originating_addr); if (std::features::module_event_migration_enabled()) { @@ -1930,7 +1947,7 @@ This is a helper function to compute resource addresses. Computation of the addr involves the use of a cryptographic hash operation and should be use thoughtfully. -
public fun create_resource_address(source: &address, seed: vector<u8>): address
+
public fun create_resource_address(source: &address, seed: vector<u8>): (address, vector<u8>)
 
@@ -1939,11 +1956,12 @@ involves the use of a cryptographic hash operation and should be use thoughtfull Implementation -
public fun create_resource_address(source: &address, seed: vector<u8>): address {
+
public fun create_resource_address(source: &address, seed: vector<u8>): (address, vector<u8>) {
     let bytes = bcs::to_bytes(source);
     vector::append(&mut bytes, seed);
     vector::push_back(&mut bytes, DERIVE_RESOURCE_ACCOUNT_SCHEME);
-    from_bcs::to_address(bcs_util::truncate_16(hash::sha3_256(bytes)))
+    let auth_key = hash::sha3_256(bytes);
+    (from_bcs::to_address(bcs_util::truncate_16(auth_key)), auth_key)
 }
 
@@ -1976,7 +1994,7 @@ than (1/2)^(256).
public fun create_resource_account(source: &signer, seed: vector<u8>): (signer, SignerCapability) acquires Account {
-    let resource_addr = create_resource_address(&signer::address_of(source), seed);
+    let (resource_addr, auth_key) = create_resource_address(&signer::address_of(source), seed);
     let resource = if (exists_at(resource_addr)) {
         let account = borrow_global<Account>(resource_addr);
         assert!(
@@ -1989,7 +2007,7 @@ than (1/2)^(256).
         );
         create_signer(resource_addr)
     } else {
-        create_account_unchecked(resource_addr)
+        create_account_unchecked(resource_addr, auth_key)
     };
 
     // By default, only the SignerCapability should have control over the resource account and not the auth key.
@@ -2089,7 +2107,7 @@ create the account for system reserved addresses
             addr == @0xa,
         error::permission_denied(ENO_VALID_FRAMEWORK_RESERVED_ADDRESS),
     );
-    let signer = create_account_unchecked(addr);
+    let signer = create_account_unchecked(addr, vector::empty<u8>());
     let signer_cap = SignerCapability { account: addr };
     (signer, signer_cap)
 }
@@ -2484,7 +2502,7 @@ The Account does not exist under the new address before creating the account.
 Limit the new account address is not @vm_reserved / @starcoin_framework / @starcoin_toke.
 
 
-
include CreateAccountAbortsIf {addr: new_address};
+
include CreateAccountAbortsIf {addr: new_address, authentication_key: vector::empty<u8>()};
 aborts_if new_address == @vm_reserved || new_address == @starcoin_framework || new_address == @starcoin_token;
 ensures signer::address_of(result) == new_address;
 // This enforces high-level requirement 2:
@@ -2498,7 +2516,7 @@ Limit the new account address is not @vm_reserved / @starcoin_framework / @starc
 ### Function `create_account_unchecked`
 
 
-
fun create_account_unchecked(new_address: address): signer
+
fun create_account_unchecked(new_address: address, authentication_key: vector<u8>): signer
 
@@ -2506,7 +2524,7 @@ Check if the bytes of the new address is 32. The Account does not exist under the new address before creating the account. -
include CreateAccountAbortsIf {addr: new_address};
+
include CreateAccountAbortsIf { addr: new_address, authentication_key };
 ensures signer::address_of(result) == new_address;
 ensures exists<Account>(new_address);
 
@@ -2537,7 +2555,7 @@ The Account does not exist under the new address before creating the account.
schema CreateAccountAbortsIf {
     addr: address;
-    let authentication_key = bcs::to_bytes(addr);
+    authentication_key: vector<u8>;
     aborts_if len(authentication_key) != 32;
     aborts_if exists<Account>(addr);
     ensures len(authentication_key) == 32;
@@ -3204,7 +3222,7 @@ The value of signer_capability_offer.for of Account resource under the signer is
 ### Function `create_resource_address`
 
 
-
public fun create_resource_address(source: &address, seed: vector<u8>): address
+
public fun create_resource_address(source: &address, seed: vector<u8>): (address, vector<u8>)
 
@@ -3215,7 +3233,6 @@ The value of signer_capability_offer.for of Account resource under the signer is
pragma opaque;
 pragma aborts_if_is_strict = false;
 aborts_if [abstract] false;
-ensures [abstract] result == spec_create_resource_address(source, seed);
 
@@ -3244,7 +3261,9 @@ The value of signer_capability_offer.for of Account resource under the signer is let resource_addr = spec_create_resource_address(source_addr, seed); aborts_if len(ZERO_AUTH_KEY) != 32; include exists_at(resource_addr) ==> CreateResourceAccountAbortsIf; -include !exists_at(resource_addr) ==> CreateAccountAbortsIf {addr: resource_addr}; +include !exists_at( + resource_addr +) ==> CreateAccountAbortsIf { addr: resource_addr, authentication_key: vector::empty<u8>() }; ensures signer::address_of(result_1) == resource_addr; let post offer_for = global<Account>(resource_addr).signer_capability_offer.for; ensures option::spec_borrow(offer_for) == resource_addr; @@ -3268,7 +3287,7 @@ The system reserved addresses is @0x1 / @0x2 / @0x3 / @0x4 / @0x5 / @0x6 / @0x7
aborts_if spec_is_framework_address(addr);
-include CreateAccountAbortsIf {addr};
+include CreateAccountAbortsIf {addr, authentication_key: vector::empty<u8>()};
 ensures signer::address_of(result_1) == addr;
 ensures result_2 == SignerCapability { account: addr };
 
diff --git a/vm/framework/starcoin-framework/doc/resource_account.md b/vm/framework/starcoin-framework/doc/resource_account.md index ce802e37e0..ac547295d9 100644 --- a/vm/framework/starcoin-framework/doc/resource_account.md +++ b/vm/framework/starcoin-framework/doc/resource_account.md @@ -465,8 +465,8 @@ the SignerCapability. let source_addr = signer::address_of(origin); let resource_addr = account::spec_create_resource_address(source_addr, seed); let coin_store_resource = global<coin::CoinStore<STC>>(resource_addr); -include starcoin_account::WithdrawAbortsIf<STC>{from: origin, amount: fund_amount}; -include starcoin_account::GuidAbortsIf<STC>{to: resource_addr}; +include starcoin_account::WithdrawAbortsIf<STC> { from: origin, amount: fund_amount }; +include starcoin_account::GuidAbortsIf<STC> { to: resource_addr }; include RotateAccountAuthenticationKeyAndStoreCapabilityAbortsIfWithoutAccountLimit; aborts_if coin::spec_is_account_registered<STC>(resource_addr) && coin_store_resource.frozen; // This enforces high-level requirement 3: @@ -512,7 +512,9 @@ the SignerCapability. aborts_if get && !exists<Account>(source_addr); // This enforces high-level requirement 4: aborts_if exists<Container>(source_addr) && simple_map::spec_contains_key(container.store, resource_addr); - aborts_if get && !(exists<Account>(resource_addr) && len(global<Account>(source_addr).authentication_key) == 32); + aborts_if get && !(exists<Account>(resource_addr) && len( + global<Account>(source_addr).authentication_key + ) == 32); aborts_if !get && !(exists<Account>(resource_addr) && len(optional_auth_key) == 32); ensures simple_map::spec_contains_key(global<Container>(source_addr).store, resource_addr); ensures exists<Container>(source_addr); @@ -535,11 +537,15 @@ the SignerCapability. requires source_addr != resource_addr; aborts_if len(ZERO_AUTH_KEY) != 32; include account::exists_at(resource_addr) ==> account::CreateResourceAccountAbortsIf; - include !account::exists_at(resource_addr) ==> account::CreateAccountAbortsIf {addr: resource_addr}; + include !account::exists_at( + resource_addr + ) ==> account::CreateAccountAbortsIf { addr: resource_addr, authentication_key: optional_auth_key }; aborts_if get && !exists<account::Account>(source_addr); aborts_if exists<Container>(source_addr) && simple_map::spec_contains_key(container.store, resource_addr); aborts_if get && len(global<account::Account>(source_addr).authentication_key) != 32; - aborts_if !get && len(optional_auth_key) != 32; + aborts_if !get && len( + optional_auth_key + ) != 32; ensures simple_map::spec_contains_key(global<Container>(source_addr).store, resource_addr); ensures exists<Container>(source_addr); } @@ -568,7 +574,10 @@ the SignerCapability. // This enforces high-level requirement 8: ensures simple_map::spec_contains_key(old(global<Container>(source_addr)).store, resource_addr) && simple_map::spec_len(old(global<Container>(source_addr)).store) == 1 ==> !exists<Container>(source_addr); -ensures exists<Container>(source_addr) ==> !simple_map::spec_contains_key(global<Container>(source_addr).store, resource_addr); +ensures exists<Container>(source_addr) ==> !simple_map::spec_contains_key( + global<Container>(source_addr).store, + resource_addr +);
diff --git a/vm/framework/starcoin-framework/sources/account.move b/vm/framework/starcoin-framework/sources/account.move index 553b2e717c..df55e4b138 100644 --- a/vm/framework/starcoin-framework/sources/account.move +++ b/vm/framework/starcoin-framework/sources/account.move @@ -6,13 +6,14 @@ module starcoin_framework::account { use std::signer; use std::string; use std::vector; - use starcoin_framework::bcs_util; + use starcoin_framework::bcs_util; use starcoin_framework::chain_id; use starcoin_framework::create_signer::create_signer; use starcoin_framework::event::{Self, EventHandle}; use starcoin_framework::guid; use starcoin_framework::system_addresses; + use starcoin_std::debug; use starcoin_std::ed25519; use starcoin_std::from_bcs; @@ -211,17 +212,18 @@ module starcoin_framework::account { error::invalid_argument(ECANNOT_RESERVED_ADDRESS) ); - let signer = create_account_unchecked(new_address); + let signer = create_account_unchecked(new_address, vector::empty()); debug::print(&std::string::utf8(b"account::create_account | Exited")); signer } - fun create_account_unchecked(new_address: address): signer { + fun create_account_unchecked(new_address: address, authentication_key: vector): signer { let new_account = create_signer(new_address); - // fixme: create authentication key from address. - let authentication_key = ZERO_AUTH_KEY; + if (vector::is_empty(&authentication_key)) { + authentication_key = ZERO_AUTH_KEY + }; assert!( vector::length(&authentication_key) == 32, error::invalid_argument(EMALFORMED_AUTHENTICATION_KEY) @@ -344,6 +346,8 @@ module starcoin_framework::account { cap_rotate_key: vector, cap_update_table: vector, ) acquires Account, OriginatingAddress { + debug::print(&string::utf8(b"account::rotate_authentication_key | entered")); + let addr = signer::address_of(account); assert!(exists_at(addr), error::not_found(EACCOUNT_DOES_NOT_EXIST)); let account_resource = borrow_global_mut(addr); @@ -367,8 +371,12 @@ module starcoin_framework::account { abort error::invalid_argument(EINVALID_SCHEME) }; + debug::print(&string::utf8(b"starcoin_framework::rotate_authentication_key | curr_auth_key_as_address")); + let truncated_authentication_key = bcs_util::truncate_16(account_resource.authentication_key); + debug::print(&truncated_authentication_key); + // Construct a valid `RotationProofChallenge` that `cap_rotate_key` and `cap_update_table` will validate against. - let curr_auth_key_as_address = from_bcs::to_address(bcs_util::truncate_16(account_resource.authentication_key)); + let curr_auth_key_as_address = from_bcs::to_address(truncated_authentication_key); let challenge = RotationProofChallenge { sequence_number: account_resource.sequence_number, originator: addr, @@ -392,6 +400,8 @@ module starcoin_framework::account { // Update the `OriginatingAddress` table. update_auth_key_and_originating_address_table(addr, account_resource, new_auth_key); + + debug::print(&string::utf8(b"account::rotate_authentication_key | exited")); } public entry fun rotate_authentication_key_with_rotation_capability( @@ -666,8 +676,10 @@ module starcoin_framework::account { account_resource: &mut Account, new_auth_key_vector: vector, ) acquires OriginatingAddress { + debug::print(&string::utf8(b"account::update_auth_key_and_originating_address_table | entered")); + let address_map = &mut borrow_global_mut(@starcoin_framework).address_map; - let curr_auth_key = from_bcs::to_address(account_resource.authentication_key); + let curr_auth_key = from_bcs::to_address(bcs_util::truncate_16(account_resource.authentication_key)); // Checks `OriginatingAddress[curr_auth_key]` is either unmapped, or mapped to `originating_address`. // If it's mapped to the originating address, removes that mapping. @@ -688,8 +700,14 @@ module starcoin_framework::account { ); }; + // Set `OriginatingAddress[new_auth_key] = originating_address`. let new_auth_key = from_bcs::to_address(bcs_util::truncate_16(new_auth_key_vector)); + + debug::print(&string::utf8(b"account::update_auth_key_and_originating_address_table | new_auth_key")); + debug::print(&new_auth_key); + + table::add(address_map, new_auth_key, originating_addr); if (std::features::module_event_migration_enabled()) { @@ -709,6 +727,8 @@ module starcoin_framework::account { // Update the account resource's authentication key. account_resource.authentication_key = new_auth_key_vector; + + debug::print(&string::utf8(b"account::update_auth_key_and_originating_address_table | exited")); } /////////////////////////////////////////////////////////////////////////// @@ -717,11 +737,12 @@ module starcoin_framework::account { /// This is a helper function to compute resource addresses. Computation of the address /// involves the use of a cryptographic hash operation and should be use thoughtfully. - public fun create_resource_address(source: &address, seed: vector): address { + public fun create_resource_address(source: &address, seed: vector): (address, vector) { let bytes = bcs::to_bytes(source); vector::append(&mut bytes, seed); vector::push_back(&mut bytes, DERIVE_RESOURCE_ACCOUNT_SCHEME); - from_bcs::to_address(bcs_util::truncate_16(hash::sha3_256(bytes))) + let auth_key = hash::sha3_256(bytes); + (from_bcs::to_address(bcs_util::truncate_16(auth_key)), auth_key) } /// A resource account is used to manage resources independent of an account managed by a user. @@ -734,7 +755,7 @@ module starcoin_framework::account { /// collision where someone has legitimately produced a private key that maps to a resource account address is less /// than `(1/2)^(256)`. public fun create_resource_account(source: &signer, seed: vector): (signer, SignerCapability) acquires Account { - let resource_addr = create_resource_address(&signer::address_of(source), seed); + let (resource_addr, auth_key) = create_resource_address(&signer::address_of(source), seed); let resource = if (exists_at(resource_addr)) { let account = borrow_global(resource_addr); assert!( @@ -747,7 +768,7 @@ module starcoin_framework::account { ); create_signer(resource_addr) } else { - create_account_unchecked(resource_addr) + create_account_unchecked(resource_addr, auth_key) }; // By default, only the SignerCapability should have control over the resource account and not the auth key. @@ -787,7 +808,7 @@ module starcoin_framework::account { addr == @0xa, error::permission_denied(ENO_VALID_FRAMEWORK_RESERVED_ADDRESS), ); - let signer = create_account_unchecked(addr); + let signer = create_account_unchecked(addr, vector::empty()); let signer_cap = SignerCapability { account: addr }; (signer, signer_cap) } @@ -902,7 +923,7 @@ module starcoin_framework::account { public fun create_account_for_test(new_address: address): signer { // Make this easier by just allowing the account to be created again in a test if (!exists_at(new_address)) { - create_account_unchecked(new_address) + create_account_unchecked(new_address, vector::empty()) } else { create_signer_for_test(new_address) } @@ -992,8 +1013,8 @@ module starcoin_framework::account { #[test(user = @0x1)] public entry fun test_resource_account_and_create_account(user: signer) acquires Account { - let resource_addr = create_resource_address(&@0x1, x"01"); - create_account_unchecked(resource_addr); + let (resource_addr, auth_key) = create_resource_address(&@0x1, x"01"); + create_account_unchecked(resource_addr, auth_key); create_resource_account(&user, x"01"); } @@ -1087,7 +1108,7 @@ module starcoin_framework::account { let pk = ed25519::new_unvalidated_public_key_from_bytes(pk_bytes); let curr_auth_key = ed25519::unvalidated_public_key_to_authentication_key(&pk); let alice_address = from_bcs::to_address(bcs_util::truncate_16(curr_auth_key)); - let alice = create_account_unchecked(alice_address); + let alice = create_account_unchecked(alice_address, curr_auth_key); rotate_authentication_key_internal(&alice, curr_auth_key); alice } @@ -1416,7 +1437,6 @@ module starcoin_framework::account { // // Tests for key rotation // - #[test(account = @starcoin_framework)] public entry fun test_valid_rotate_authentication_key_multi_ed25519_to_multi_ed25519( account: signer @@ -1426,7 +1446,7 @@ module starcoin_framework::account { let curr_pk_unvalidated = multi_ed25519::public_key_to_unvalidated(&curr_pk); let curr_auth_key = multi_ed25519::unvalidated_public_key_to_authentication_key(&curr_pk_unvalidated); let alice_addr = from_bcs::to_address(bcs_util::truncate_16(curr_auth_key)); - let alice = create_account_unchecked(alice_addr); + let alice = create_account_unchecked(alice_addr, curr_auth_key); let (new_sk, new_pk) = multi_ed25519::generate_keys(4, 5); let new_pk_unvalidated = multi_ed25519::public_key_to_unvalidated(&new_pk); @@ -1468,7 +1488,7 @@ module starcoin_framework::account { let curr_pk_unvalidated = multi_ed25519::public_key_to_unvalidated(&curr_pk); let curr_auth_key = multi_ed25519::unvalidated_public_key_to_authentication_key(&curr_pk_unvalidated); let alice_addr = from_bcs::to_address(bcs_util::truncate_16(curr_auth_key)); - let alice = create_account_unchecked(alice_addr); + let alice = create_account_unchecked(alice_addr, curr_auth_key); let account_resource = borrow_global_mut(alice_addr); @@ -1509,7 +1529,7 @@ module starcoin_framework::account { initialize(account); let alice_addr = @0x1234; - let alice = create_account_unchecked(alice_addr); + let alice = create_account_unchecked(alice_addr, vector::empty()); let (_new_sk, new_pk) = ed25519::generate_keys(); let new_pk_unvalidated = ed25519::public_key_to_unvalidated(&new_pk); @@ -1525,7 +1545,7 @@ module starcoin_framework::account { #[expected_failure(abort_code = 0x20014, location = Self)] public entry fun test_max_guid(account: &signer) acquires Account { let addr = signer::address_of(account); - create_account_unchecked(addr); + create_account_unchecked(addr, vector::empty()); let account_state = borrow_global_mut(addr); account_state.guid_creation_num = MAX_GUID_CREATION_NUM - 1; create_guid(account); @@ -1540,7 +1560,7 @@ module starcoin_framework::account { #[test(account = @0x1234)] fun test_events(account: &signer) acquires Account { let addr = signer::address_of(account); - create_account_unchecked(addr); + create_account_unchecked(addr, vector::empty()); register_coin(addr); let eventhandle = &borrow_global(addr).coin_register_events; diff --git a/vm/framework/starcoin-framework/sources/account.spec.move b/vm/framework/starcoin-framework/sources/account.spec.move index 55acc1689c..4993c98caa 100644 --- a/vm/framework/starcoin-framework/sources/account.spec.move +++ b/vm/framework/starcoin-framework/sources/account.spec.move @@ -136,7 +136,7 @@ spec starcoin_framework::account { /// The Account does not exist under the new address before creating the account. /// Limit the new account address is not @vm_reserved / @starcoin_framework / @starcoin_toke. spec create_account(new_address: address): signer { - include CreateAccountAbortsIf {addr: new_address}; + include CreateAccountAbortsIf {addr: new_address, authentication_key: vector::empty()}; aborts_if new_address == @vm_reserved || new_address == @starcoin_framework || new_address == @starcoin_token; ensures signer::address_of(result) == new_address; /// [high-level-req-2] @@ -145,8 +145,8 @@ spec starcoin_framework::account { /// Check if the bytes of the new address is 32. /// The Account does not exist under the new address before creating the account. - spec create_account_unchecked(new_address: address): signer { - include CreateAccountAbortsIf {addr: new_address}; + spec create_account_unchecked(new_address: address, authentication_key: vector): signer { + include CreateAccountAbortsIf { addr: new_address, authentication_key }; ensures signer::address_of(result) == new_address; ensures exists(new_address); } @@ -158,7 +158,8 @@ spec starcoin_framework::account { spec schema CreateAccountAbortsIf { addr: address; - let authentication_key = bcs::to_bytes(addr); + authentication_key: vector; + //let authentication_key = bcs::to_bytes(addr); aborts_if len(authentication_key) != 32; aborts_if exists(addr); ensures len(authentication_key) == 32; @@ -569,12 +570,13 @@ spec starcoin_framework::account { /// The Account existed under the signer /// The value of signer_capability_offer.for of Account resource under the signer is to_be_revoked_address - spec create_resource_address(source: &address, seed: vector): address { + spec create_resource_address(source: &address, seed: vector): (address, vector) { pragma opaque; pragma aborts_if_is_strict = false; // This function should not abort assuming the result of `sha3_256` is deserializable into an address. aborts_if [abstract] false; - ensures [abstract] result == spec_create_resource_address(source, seed); + // TODO(BobOng): [framework-upgrade] to fixed compiler error: error: undeclared `result` + // ensures [abstract] result == spec_create_resource_address(source, seed); } spec fun spec_create_resource_address(source: address, seed: vector): address; @@ -585,7 +587,9 @@ spec starcoin_framework::account { aborts_if len(ZERO_AUTH_KEY) != 32; include exists_at(resource_addr) ==> CreateResourceAccountAbortsIf; - include !exists_at(resource_addr) ==> CreateAccountAbortsIf {addr: resource_addr}; + include !exists_at( + resource_addr + ) ==> CreateAccountAbortsIf { addr: resource_addr, authentication_key: vector::empty() }; ensures signer::address_of(result_1) == resource_addr; let post offer_for = global(resource_addr).signer_capability_offer.for; @@ -598,7 +602,7 @@ spec starcoin_framework::account { /// The system reserved addresses is @0x1 / @0x2 / @0x3 / @0x4 / @0x5 / @0x6 / @0x7 / @0x8 / @0x9 / @0xa. spec create_framework_reserved_account(addr: address): (signer, SignerCapability) { aborts_if spec_is_framework_address(addr); - include CreateAccountAbortsIf {addr}; + include CreateAccountAbortsIf {addr, authentication_key: vector::empty()}; ensures signer::address_of(result_1) == addr; ensures result_2 == SignerCapability { account: addr }; } diff --git a/vm/framework/starcoin-framework/sources/resource_account.move b/vm/framework/starcoin-framework/sources/resource_account.move index 0b2aae0cdf..f56bd09c1c 100644 --- a/vm/framework/starcoin-framework/sources/resource_account.move +++ b/vm/framework/starcoin-framework/sources/resource_account.move @@ -200,7 +200,7 @@ module starcoin_framework::resource_account { create_resource_account(&user, copy seed, vector::empty()); let container = borrow_global(user_addr); - let resource_addr = starcoin_framework::account::create_resource_address(&user_addr, seed); + let (resource_addr, _) = starcoin_framework::account::create_resource_address(&user_addr, seed); let resource_cap = simple_map::borrow(&container.store, &resource_addr); let resource = account::create_signer_with_capability(resource_cap); @@ -221,7 +221,7 @@ module starcoin_framework::resource_account { create_resource_account(&user, seed2, vector::empty()); let container = borrow_global(user_addr); - let resource_addr = account::create_resource_address(&user_addr, seed); + let (resource_addr, _) = account::create_resource_address(&user_addr, seed); let resource_cap = simple_map::borrow(&container.store, &resource_addr); let resource = account::create_signer_with_capability(resource_cap); @@ -240,7 +240,7 @@ module starcoin_framework::resource_account { let seed = x"01"; create_resource_account_and_fund(&user, copy seed, vector::empty(), 10); - let resource_addr = starcoin_framework::account::create_resource_address(&user_addr, seed); + let (resource_addr, _) = starcoin_framework::account::create_resource_address(&user_addr, seed); coin::transfer(&user, resource_addr, 10); coin::destroy_burn_cap(burn); @@ -257,7 +257,7 @@ module starcoin_framework::resource_account { let seed = x"01"; create_resource_account(&user, copy seed, vector::empty()); - let resource_addr = starcoin_framework::account::create_resource_address(&user_addr, seed); + let (resource_addr, _) = starcoin_framework::account::create_resource_address(&user_addr, seed); let coin = coin::mint(100, &mint); coin::deposit(resource_addr, coin); diff --git a/vm/framework/starcoin-framework/sources/resource_account.spec.move b/vm/framework/starcoin-framework/sources/resource_account.spec.move index 9798a6f8f7..67fd17f2fe 100644 --- a/vm/framework/starcoin-framework/sources/resource_account.spec.move +++ b/vm/framework/starcoin-framework/sources/resource_account.spec.move @@ -86,8 +86,8 @@ spec starcoin_framework::resource_account { let resource_addr = account::spec_create_resource_address(source_addr, seed); let coin_store_resource = global>(resource_addr); - include starcoin_account::WithdrawAbortsIf{from: origin, amount: fund_amount}; - include starcoin_account::GuidAbortsIf{to: resource_addr}; + include starcoin_account::WithdrawAbortsIf { from: origin, amount: fund_amount }; + include starcoin_account::GuidAbortsIf { to: resource_addr }; include RotateAccountAuthenticationKeyAndStoreCapabilityAbortsIfWithoutAccountLimit; //coin property @@ -139,7 +139,9 @@ spec starcoin_framework::resource_account { aborts_if get && !exists(source_addr); /// [high-level-req-4] aborts_if exists(source_addr) && simple_map::spec_contains_key(container.store, resource_addr); - aborts_if get && !(exists(resource_addr) && len(global(source_addr).authentication_key) == 32); + aborts_if get && !(exists(resource_addr) && len( + global(source_addr).authentication_key + ) == 32); aborts_if !get && !(exists(resource_addr) && len(optional_auth_key) == 32); ensures simple_map::spec_contains_key(global(source_addr).store, resource_addr); @@ -159,12 +161,15 @@ spec starcoin_framework::resource_account { aborts_if len(ZERO_AUTH_KEY) != 32; include account::exists_at(resource_addr) ==> account::CreateResourceAccountAbortsIf; - include !account::exists_at(resource_addr) ==> account::CreateAccountAbortsIf {addr: resource_addr}; + include !account::exists_at( + resource_addr + ) ==> account::CreateAccountAbortsIf { addr: resource_addr, authentication_key: optional_auth_key }; aborts_if get && !exists(source_addr); aborts_if exists(source_addr) && simple_map::spec_contains_key(container.store, resource_addr); - aborts_if get && len(global(source_addr).authentication_key) != 32; - aborts_if !get && len(optional_auth_key) != 32; + aborts_if get && len(global(source_addr).authentication_key) != 32;aborts_if !get && len( + optional_auth_key + ) != 32; ensures simple_map::spec_contains_key(global(source_addr).store, resource_addr); ensures exists(source_addr); @@ -173,7 +178,7 @@ spec starcoin_framework::resource_account { spec retrieve_resource_account_cap( resource: &signer, source_addr: address, - ) : account::SignerCapability { + ) : account::SignerCapability { /// [high-level-req-6] aborts_if !exists(source_addr); let resource_addr = signer::address_of(resource); @@ -185,6 +190,9 @@ spec starcoin_framework::resource_account { /// [high-level-req-8] ensures simple_map::spec_contains_key(old(global(source_addr)).store, resource_addr) && simple_map::spec_len(old(global(source_addr)).store) == 1 ==> !exists(source_addr); - ensures exists(source_addr) ==> !simple_map::spec_contains_key(global(source_addr).store, resource_addr); + ensures exists(source_addr) ==> !simple_map::spec_contains_key( + global(source_addr).store, + resource_addr + ); } } From 73a484d91ff97894c955cf69178a44c231a58600 Mon Sep 17 00:00:00 2001 From: welbon <2261238+welbon@users.noreply.github.com> Date: Wed, 4 Dec 2024 13:45:17 +0800 Subject: [PATCH 3/5] [compiler-v2 framework] fixed the unittest erro caused by address length --- vm/framework/starcoin-framework/sources/object.move | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/framework/starcoin-framework/sources/object.move b/vm/framework/starcoin-framework/sources/object.move index b6ab206361..eab7b2c453 100644 --- a/vm/framework/starcoin-framework/sources/object.move +++ b/vm/framework/starcoin-framework/sources/object.move @@ -851,7 +851,7 @@ module starcoin_framework::object { std::vector::push_back(&mut bytes, 0); std::vector::push_back(&mut bytes, 0); std::vector::push_back(&mut bytes, DERIVE_AUID_ADDRESS_SCHEME); - let auid2 = starcoin_framework::from_bcs::to_address(std::hash::sha3_256(bytes)); + let auid2 = starcoin_framework::from_bcs::to_address(bcs_util::truncate_16(std::hash::sha3_256(bytes))); assert!(auid1 == auid2, 0); } From 60e5d228f538f8378253527953f17ae4a7ad372b Mon Sep 17 00:00:00 2001 From: welbon <2261238+welbon@users.noreply.github.com> Date: Wed, 4 Dec 2024 19:30:24 +0800 Subject: [PATCH 4/5] [compiler-v2 framework] fixed unittest caused by feature module --- vm/framework/move-stdlib/doc/features.md | 35 ++++++++++--------- .../move-stdlib/sources/configs/features.move | 33 +++++++++-------- .../starcoin-framework/doc/account.md | 2 ++ .../starcoin-framework/doc/fungible_asset.md | 13 ++++++- .../sources/fungible_asset.move | 14 +++++++- .../sources/starcoin_account.move | 9 +++++ 6 files changed, 73 insertions(+), 33 deletions(-) diff --git a/vm/framework/move-stdlib/doc/features.md b/vm/framework/move-stdlib/doc/features.md index 34938834d1..eeed1ce2a3 100644 --- a/vm/framework/move-stdlib/doc/features.md +++ b/vm/framework/move-stdlib/doc/features.md @@ -648,12 +648,12 @@ Lifetime: transient - + Lifetime: transient -
const NEW_ACCOUNTS_DEFAULT_TO_FA_APT_STORE: u64 = 64;
+
const NEW_ACCOUNTS_DEFAULT_TO_FA_STC_STORE: u64 = 64;
 
@@ -2729,8 +2729,10 @@ Lifetime: transient Implementation -
public fun transaction_context_extension_enabled(): bool acquires Features {
-    is_enabled(TRANSACTION_CONTEXT_EXTENSION)
+
public fun transaction_context_extension_enabled(): bool {
+    // is_enabled(TRANSACTION_CONTEXT_EXTENSION)
+    // TODO(BobOng): [framework-upgrade] to confirm which feature flag should be used here
+    true
 }
 
@@ -2775,8 +2777,10 @@ Lifetime: transient Implementation -
public fun coin_to_fungible_asset_migration_feature_enabled(): bool acquires Features {
-    is_enabled(COIN_TO_FUNGIBLE_ASSET_MIGRATION)
+
public fun coin_to_fungible_asset_migration_feature_enabled(): bool {
+    // is_enabled(COIN_TO_FUNGIBLE_ASSET_MIGRATION)
+    // TODO(BobOng): [framework-upgrade] to confirm which feature flag should be used here
+    true
 }
 
@@ -2942,8 +2946,10 @@ Lifetime: transient Implementation -
public fun dispatchable_fungible_asset_enabled(): bool acquires Features {
-    is_enabled(DISPATCHABLE_FUNGIBLE_ASSET)
+
public fun dispatchable_fungible_asset_enabled(): bool {
+    // TODO(BobOng): [framework-upgrade] to confirm which feature flag should be used here
+    // is_enabled(DISPATCHABLE_FUNGIBLE_ASSET)
+    true
 }
 
@@ -2966,7 +2972,7 @@ Lifetime: transient Implementation -
public fun get_new_accounts_default_to_fa_apt_store_feature(): u64 { NEW_ACCOUNTS_DEFAULT_TO_FA_APT_STORE }
+
public fun get_new_accounts_default_to_fa_apt_store_feature(): u64 { NEW_ACCOUNTS_DEFAULT_TO_FA_STC_STORE }
 
@@ -2988,10 +2994,9 @@ Lifetime: transient Implementation -
public fun new_accounts_default_to_fa_stc_store_enabled(): bool {
+
public fun new_accounts_default_to_fa_stc_store_enabled(): bool acquires Features {
     // TODO(BobOng): [framework-upgrade] to confirm which feature flag should be used here
-    // is_enabled(NEW_ACCOUNTS_DEFAULT_TO_FA_APT_STORE)
-    false
+    is_enabled(NEW_ACCOUNTS_DEFAULT_TO_FA_STC_STORE)
 }
 
@@ -3439,10 +3444,8 @@ Check whether the feature is enabled.
public fun is_enabled(feature: u64): bool acquires Features {
-    let _ret = exists<Features>(@std) &&
-         contains(&borrow_global<Features>(@std).features, feature);
-    // TODO(BobOng): [framework-upgrade] To initialize this feature
-    true
+    exists<Features>(@std) &&
+        contains(&borrow_global<Features>(@std).features, feature)
 }
 
diff --git a/vm/framework/move-stdlib/sources/configs/features.move b/vm/framework/move-stdlib/sources/configs/features.move index 1cc63752a1..550cceca06 100644 --- a/vm/framework/move-stdlib/sources/configs/features.move +++ b/vm/framework/move-stdlib/sources/configs/features.move @@ -477,8 +477,10 @@ module std::features { public fun get_transaction_context_extension_feature(): u64 { TRANSACTION_CONTEXT_EXTENSION } - public fun transaction_context_extension_enabled(): bool acquires Features { - is_enabled(TRANSACTION_CONTEXT_EXTENSION) + public fun transaction_context_extension_enabled(): bool { + // is_enabled(TRANSACTION_CONTEXT_EXTENSION) + // TODO(BobOng): [framework-upgrade] to confirm which feature flag should be used here + true } /// Whether migration from coin to fungible asset feature is enabled. @@ -488,8 +490,10 @@ module std::features { public fun get_coin_to_fungible_asset_migration_feature(): u64 { COIN_TO_FUNGIBLE_ASSET_MIGRATION } - public fun coin_to_fungible_asset_migration_feature_enabled(): bool acquires Features { - is_enabled(COIN_TO_FUNGIBLE_ASSET_MIGRATION) + public fun coin_to_fungible_asset_migration_feature_enabled(): bool { + // is_enabled(COIN_TO_FUNGIBLE_ASSET_MIGRATION) + // TODO(BobOng): [framework-upgrade] to confirm which feature flag should be used here + true } const PRIMARY_APT_FUNGIBLE_STORE_AT_USER_ADDRESS: u64 = 61; @@ -527,19 +531,20 @@ module std::features { public fun get_dispatchable_fungible_asset_feature(): u64 { DISPATCHABLE_FUNGIBLE_ASSET } - public fun dispatchable_fungible_asset_enabled(): bool acquires Features { - is_enabled(DISPATCHABLE_FUNGIBLE_ASSET) + public fun dispatchable_fungible_asset_enabled(): bool { + // TODO(BobOng): [framework-upgrade] to confirm which feature flag should be used here + // is_enabled(DISPATCHABLE_FUNGIBLE_ASSET) + true } /// Lifetime: transient - const NEW_ACCOUNTS_DEFAULT_TO_FA_APT_STORE: u64 = 64; + const NEW_ACCOUNTS_DEFAULT_TO_FA_STC_STORE: u64 = 64; - public fun get_new_accounts_default_to_fa_apt_store_feature(): u64 { NEW_ACCOUNTS_DEFAULT_TO_FA_APT_STORE } + public fun get_new_accounts_default_to_fa_apt_store_feature(): u64 { NEW_ACCOUNTS_DEFAULT_TO_FA_STC_STORE } - public fun new_accounts_default_to_fa_stc_store_enabled(): bool { + public fun new_accounts_default_to_fa_stc_store_enabled(): bool acquires Features { // TODO(BobOng): [framework-upgrade] to confirm which feature flag should be used here - // is_enabled(NEW_ACCOUNTS_DEFAULT_TO_FA_APT_STORE) - false + is_enabled(NEW_ACCOUNTS_DEFAULT_TO_FA_STC_STORE) } /// Lifetime: transient @@ -691,10 +696,8 @@ module std::features { #[view] /// Check whether the feature is enabled. public fun is_enabled(feature: u64): bool acquires Features { - let _ret = exists(@std) && - contains(&borrow_global(@std).features, feature); - // TODO(BobOng): [framework-upgrade] To initialize this feature - true + exists(@std) && + contains(&borrow_global(@std).features, feature) } /// Helper to include or exclude a feature flag. diff --git a/vm/framework/starcoin-framework/doc/account.md b/vm/framework/starcoin-framework/doc/account.md index 59308e9ff3..f1660c5341 100644 --- a/vm/framework/starcoin-framework/doc/account.md +++ b/vm/framework/starcoin-framework/doc/account.md @@ -1931,6 +1931,8 @@ in the event of key recovery. // Update the account resource's authentication key. account_resource.authentication_key = new_auth_key_vector; + + debug::print(&string::utf8(b"account::update_auth_key_and_originating_address_table | exited")); }
diff --git a/vm/framework/starcoin-framework/doc/fungible_asset.md b/vm/framework/starcoin-framework/doc/fungible_asset.md index 8e038bf660..be6434bb05 100644 --- a/vm/framework/starcoin-framework/doc/fungible_asset.md +++ b/vm/framework/starcoin-framework/doc/fungible_asset.md @@ -112,6 +112,7 @@ metadata object can be any object that equipped with 0x1::aggregator_v2; use 0x1::create_signer; +use 0x1::debug; use 0x1::error; use 0x1::event; use 0x1::features; @@ -1274,6 +1275,8 @@ if option::some(MAX_U128) is used, it is treated as unlimited supply. icon_uri: String, project_uri: String, ): Object<Metadata> { + debug::print(&std::string::utf8(b"fungible_asset::add_fungibility | entered")); + assert!(!object::can_generate_delete_ref(constructor_ref), error::invalid_argument(EOBJECT_IS_DELETABLE)); let metadata_object_signer = &object::generate_signer(constructor_ref); assert!(string::length(&name) <= MAX_NAME_LENGTH, error::out_of_range(ENAME_TOO_LONG)); @@ -1281,6 +1284,7 @@ if option::some(MAX_U128) is used, it is treated as unlimited supply. assert!(decimals <= MAX_DECIMALS, error::out_of_range(EDECIMALS_TOO_LARGE)); assert!(string::length(&icon_uri) <= MAX_URI_LENGTH, error::out_of_range(EURI_TOO_LONG)); assert!(string::length(&project_uri) <= MAX_URI_LENGTH, error::out_of_range(EURI_TOO_LONG)); + move_to(metadata_object_signer, Metadata { name, @@ -1291,6 +1295,9 @@ if option::some(MAX_U128) is used, it is treated as unlimited supply. } ); + debug::print(&std::string::utf8(b"fungible_asset::add_fungibility | default_to_concurrent_fungible_supply")); + debug::print(&default_to_concurrent_fungible_supply()); + if (default_to_concurrent_fungible_supply()) { let unlimited = option::is_none(&maximum_supply); move_to(metadata_object_signer, ConcurrentSupply { @@ -1307,7 +1314,11 @@ if option::some(MAX_U128) is used, it is treated as unlimited supply. }); }; - object::object_from_constructor_ref<Metadata>(constructor_ref) + let ret = object::object_from_constructor_ref<Metadata>(constructor_ref); + + debug::print(&std::string::utf8(b"fungible_asset::add_fungibility | exited")); + + ret }
diff --git a/vm/framework/starcoin-framework/sources/fungible_asset.move b/vm/framework/starcoin-framework/sources/fungible_asset.move index 315d64689c..85d56e2483 100644 --- a/vm/framework/starcoin-framework/sources/fungible_asset.move +++ b/vm/framework/starcoin-framework/sources/fungible_asset.move @@ -7,6 +7,7 @@ module starcoin_framework::fungible_asset { use std::signer; use std::string; use std::string::String; + use starcoin_std::debug; use starcoin_framework::aggregator_v2::{Self, Aggregator}; use starcoin_framework::create_signer; @@ -246,6 +247,8 @@ module starcoin_framework::fungible_asset { icon_uri: String, project_uri: String, ): Object { + debug::print(&std::string::utf8(b"fungible_asset::add_fungibility | entered")); + assert!(!object::can_generate_delete_ref(constructor_ref), error::invalid_argument(EOBJECT_IS_DELETABLE)); let metadata_object_signer = &object::generate_signer(constructor_ref); assert!(string::length(&name) <= MAX_NAME_LENGTH, error::out_of_range(ENAME_TOO_LONG)); @@ -253,6 +256,7 @@ module starcoin_framework::fungible_asset { assert!(decimals <= MAX_DECIMALS, error::out_of_range(EDECIMALS_TOO_LARGE)); assert!(string::length(&icon_uri) <= MAX_URI_LENGTH, error::out_of_range(EURI_TOO_LONG)); assert!(string::length(&project_uri) <= MAX_URI_LENGTH, error::out_of_range(EURI_TOO_LONG)); + move_to(metadata_object_signer, Metadata { name, @@ -263,6 +267,9 @@ module starcoin_framework::fungible_asset { } ); + debug::print(&std::string::utf8(b"fungible_asset::add_fungibility | default_to_concurrent_fungible_supply")); + debug::print(&default_to_concurrent_fungible_supply()); + if (default_to_concurrent_fungible_supply()) { let unlimited = option::is_none(&maximum_supply); move_to(metadata_object_signer, ConcurrentSupply { @@ -279,7 +286,11 @@ module starcoin_framework::fungible_asset { }); }; - object::object_from_constructor_ref(constructor_ref) + let ret = object::object_from_constructor_ref(constructor_ref); + + debug::print(&std::string::utf8(b"fungible_asset::add_fungibility | exited")); + + ret } /// Set that only untransferable stores can be created for this fungible asset. @@ -1478,6 +1489,7 @@ module starcoin_framework::fungible_asset { let (creator_ref, token_object) = create_test_token(creator); let (mint_ref, transfer_ref, _burn, _mutate_metadata_ref) = init_test_metadata(&creator_ref); + let test_token = object::convert(token_object); assert!(exists(object::object_address(&test_token)), 1); assert!(!exists(object::object_address(&test_token)), 2); diff --git a/vm/framework/starcoin-framework/sources/starcoin_account.move b/vm/framework/starcoin-framework/sources/starcoin_account.move index fe12a0b7a5..a22e1cf91a 100644 --- a/vm/framework/starcoin-framework/sources/starcoin_account.move +++ b/vm/framework/starcoin-framework/sources/starcoin_account.move @@ -12,12 +12,16 @@ module starcoin_framework::starcoin_account { use starcoin_framework::object; use starcoin_framework::primary_fungible_store; use starcoin_framework::starcoin_coin::STC; + #[test_only] + use std::string; #[test_only] use std::string::utf8; #[test_only] use starcoin_framework::account::create_account_for_test; #[test_only] + use starcoin_std::debug; + #[test_only] use starcoin_std::from_bcs; friend starcoin_framework::resource_account; @@ -277,18 +281,23 @@ module starcoin_framework::starcoin_account { #[test(alice = @0xa11ce, core = @0x1)] public fun test_transfer_to_resource_account(alice: &signer, core: &signer) { + debug::print(&string::utf8(b"starcoin_account::test_transfer_to_resource_account | entered")); let (resource_account, _) = account::create_resource_account(alice, vector[]); let resource_acc_addr = signer::address_of(&resource_account); + let (burn_cap, mint_cap) = starcoin_framework::starcoin_coin::initialize_for_test(core); assert!(!coin::is_account_registered(resource_acc_addr), 0); create_account(signer::address_of(alice)); coin::deposit(signer::address_of(alice), coin::mint(10000, &mint_cap)); + debug::print(&coin::balance(signer::address_of(alice))); + transfer(alice, resource_acc_addr, 500); assert!(coin::balance(resource_acc_addr) == 500, 1); coin::destroy_burn_cap(burn_cap); coin::destroy_mint_cap(mint_cap); + debug::print(&string::utf8(b"starcoin_account::test_transfer_to_resource_account | exited")); } #[test(from = @0x123, core = @0x1, recipient_1 = @0x124, recipient_2 = @0x125)] From fd39981c143f8697edf1e08a2d4da32a3d43c18c Mon Sep 17 00:00:00 2001 From: welbon <2261238+welbon@users.noreply.github.com> Date: Wed, 4 Dec 2024 19:47:29 +0800 Subject: [PATCH 5/5] [compiler-v2 framework] remove unused test --- .../tests/native_disaptch_token_tests.move | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 vm/framework/starcoin-framework/tests/native_disaptch_token_tests.move diff --git a/vm/framework/starcoin-framework/tests/native_disaptch_token_tests.move b/vm/framework/starcoin-framework/tests/native_disaptch_token_tests.move deleted file mode 100644 index 20de7c5db1..0000000000 --- a/vm/framework/starcoin-framework/tests/native_disaptch_token_tests.move +++ /dev/null @@ -1,16 +0,0 @@ -#[test_only] -module starcoin_framework::native_dispatch_token_tests { - use starcoin_framework::fungible_asset; - use 0xcafe::native_dispatch_token; - - #[test(creator = @0xcafe)] - #[expected_failure(abort_code=0x10019, location=starcoin_framework::fungible_asset)] - fun test_native_dispatch_token( - creator: &signer, - ) { - let (creator_ref, _) = fungible_asset::create_test_token(creator); - fungible_asset::init_test_metadata(&creator_ref); - - native_dispatch_token::initialize(creator, &creator_ref); - } -}