diff --git a/vm/framework/starcoin-framework/doc/asset_mapping.md b/vm/framework/starcoin-framework/doc/asset_mapping.md index f9bb36f976..a52e1ad8ce 100644 --- a/vm/framework/starcoin-framework/doc/asset_mapping.md +++ b/vm/framework/starcoin-framework/doc/asset_mapping.md @@ -10,7 +10,6 @@ with proof verification. - [Resource `AssetMappingStore`](#0x1_asset_mapping_AssetMappingStore) -- [Resource `AssetMappingProof`](#0x1_asset_mapping_AssetMappingProof) - [Resource `AssetMappingPool`](#0x1_asset_mapping_AssetMappingPool) - [Constants](#@Constants_0) - [Function `initialize`](#0x1_asset_mapping_initialize) @@ -29,7 +28,7 @@ with proof verification. use 0x1::object; use 0x1::primary_fungible_store; use 0x1::signer; -use 0x1::smart_table; +use 0x1::simple_map; use 0x1::starcoin_proof_verifier; use 0x1::stc_util; use 0x1::string; @@ -49,7 +48,8 @@ Contains: - fungible_metadata: The type of fungible assets -
struct AssetMappingStore has store, key
+
#[resource_group_member(#[group = 0x1::object::ObjectGroup])]
+struct AssetMappingStore has store, key
 
@@ -72,7 +72,7 @@ Contains:
-fungible_metadata: object::Object<fungible_asset::Metadata> +metadata: object::Object<fungible_asset::Metadata>
@@ -82,13 +82,15 @@ Contains: - + -## Resource `AssetMappingProof` +## Resource `AssetMappingPool` +AssetMappingCoinType represents a mapping that from old version token types to now version asset stores +eg. 0x1::STC::STC -> 0x1::starcoin_coin::STC -
struct AssetMappingProof has store, key
+
struct AssetMappingPool has store, key
 
@@ -104,31 +106,8 @@ Contains:
- - - - - - - -## Resource `AssetMappingPool` - -AssetMappingCoinType represents a mapping that from old version token types to now version asset stores -eg. 0x1::STC::STC -> 0x1::starcoin_coin::STC - - -
struct AssetMappingPool has store, key
-
- - - -
-Fields - - -
-token_mapping: smart_table::SmartTable<string::String, asset_mapping::AssetMappingStore> +token_mapping: simple_map::SimpleMap<string::String, address>
@@ -143,6 +122,15 @@ eg. 0x1::STC::STC -> 0x1::starcoin_coin::STC ## Constants + + + + +
const ASSET_MAPPING_OBJECT_SEED: vector<u8> = [97, 115, 115, 101, 116, 45, 109, 97, 112, 112, 105, 110, 103];
+
+ + + @@ -199,17 +187,15 @@ Verifies the framework signer and creates a new AssetMappingPool Implementation -
public fun initialize(framework: &signer, proof_root: vector<u8>) {
+
public fun initialize(framework: &signer, proof_root: vector<u8>){
     assert!(
         signer::address_of(framework) == system_addresses::get_starcoin_framework(),
         error::unauthenticated(EINVALID_SIGNER)
     );
-    move_to(framework, AssetMappingProof {
+    move_to(framework, AssetMappingPool {
+        token_mapping: simple_map::new(),
         proof_root,
     });
-    move_to(framework, AssetMappingPool {
-        token_mapping: smart_table::new<string::String, AssetMappingStore>(),
-    })
 }
 
@@ -251,10 +237,12 @@ Requirements: error::unauthenticated(EINVALID_SIGNER) ); + debug::print(&string::utf8(b"asset_mapping::create_store_from_coin | coin_to_fungible_asset")); + let fungible_asset = coin::coin_to_fungible_asset(coin); let ( - fungible_metadata, + metadata, fungible_store, extend_ref ) = create_store_for_coin_type<T>(token_issuer); @@ -265,16 +253,23 @@ Requirements: fungible_asset::deposit(fungible_store, fungible_asset); // Add token mapping coin type - let asset_coin_type = borrow_global_mut<AssetMappingPool>(system_addresses::get_starcoin_framework()); - smart_table::add( + let asset_coin_type = + borrow_global_mut<AssetMappingPool>(system_addresses::get_starcoin_framework()); + + let store_constructor_ref = &object::create_object(system_addresses::get_core_resource_address()); + let store_signer = &object::generate_signer(store_constructor_ref); + move_to(store_signer, AssetMappingStore { + extend_ref, + fungible_store, + metadata, + }); + + simple_map::add( &mut asset_coin_type.token_mapping, string::utf8(old_token_str), - AssetMappingStore { - fungible_store, - fungible_metadata, - extend_ref, - } + object::address_from_constructor_ref(store_constructor_ref), ); + debug::print(&string::utf8(b"asset_mapping::create_store_from_coin | exited")); }
@@ -341,12 +336,11 @@ Retrieves the balance for a specific token type Implementation -
fun fungible_store_balance(old_asset_str: vector<u8>): u64 acquires AssetMappingPool {
-    // let metadata = coin::ensure_paired_metadata<T>();
+
fun fungible_store_balance(old_asset_str: vector<u8>): u64 acquires AssetMappingPool, AssetMappingStore {
     let pool = borrow_global<AssetMappingPool>(system_addresses::get_starcoin_framework());
-    let fungible_asset_store =
-        smart_table::borrow(&pool.token_mapping, string::utf8(old_asset_str)).fungible_store;
-    fungible_asset::balance(fungible_asset_store)
+    let store_object_addr = simple_map::borrow(&pool.token_mapping, &string::utf8(old_asset_str));
+    let mapping_store = borrow_global<AssetMappingStore>(*store_object_addr);
+    fungible_asset::balance(mapping_store.fungible_store)
 }
 
@@ -377,9 +371,9 @@ Retrieves the balance for a specific token type proof_value_hash: vector<u8>, proof_siblings: vector<u8>, amount: u64 -) acquires AssetMappingPool, AssetMappingProof { +) acquires AssetMappingPool, AssetMappingStore { assert!( - exists<AssetMappingProof>(system_addresses::get_starcoin_framework()), + exists<AssetMappingPool>(system_addresses::get_starcoin_framework()), error::invalid_state(EINVALID_PROOF_ROOT) ); @@ -425,7 +419,7 @@ Requirements: receiver: address, old_token_str: vector<u8>, amount: u64 -) acquires AssetMappingPool { +) acquires AssetMappingPool, AssetMappingStore { debug::print(&string::utf8(b"asset_mapping::assign_to_account | entered")); let account_addr = signer::address_of(system_account); @@ -441,15 +435,16 @@ Requirements: ); let coin_type_mapping = - borrow_global<AssetMappingPool>(system_addresses::get_starcoin_framework()); + borrow_global_mut<AssetMappingPool>(system_addresses::get_starcoin_framework()); debug::print(&string::utf8(b"asset_mapping::assign_to_account | coin_type_mapping")); + debug::print(&coin_type_mapping.token_mapping); - let mapping_store = smart_table::borrow( - &coin_type_mapping.token_mapping, - string::utf8(old_token_str) - ); - debug::print(&string::utf8(b"asset_mapping::assign_to_account | metadata")); - debug::print(&fungible_asset::is_frozen(mapping_store.fungible_store)); + let mapping_store_addr = simple_map::borrow(&coin_type_mapping.token_mapping, &string::utf8(old_token_str)); + debug::print(mapping_store_addr); + let mapping_store = borrow_global<AssetMappingStore>(*mapping_store_addr); + + // debug::print(&string::utf8(b"asset_mapping::assign_to_account | metadata")); + // debug::print(&fungible_asset::is_frozen(mapping_store.fungible_store)); debug::print(&string::utf8(b"asset_mapping::assign_to_account | fungible_asset::withdraw")); let mapping_fa = fungible_asset::withdraw( @@ -460,7 +455,7 @@ Requirements: debug::print(&string::utf8(b"asset_mapping::assign_to_account | Getting receiver fungible store: ")); let target_store = - primary_fungible_store::ensure_primary_store_exists(receiver, mapping_store.fungible_metadata); + primary_fungible_store::ensure_primary_store_exists(receiver, mapping_store.metadata); fungible_asset::deposit(target_store, mapping_fa); debug::print(&string::utf8(b"asset_mapping::assign_to_account | exited")); @@ -491,9 +486,9 @@ Computes and verifies the provided proof proof_path_hash: vector<u8>, blob_hash: vector<u8>, proof_siblings: vector<vector<u8>> -): bool acquires AssetMappingProof { +): bool acquires AssetMappingPool { let expect_proof_root = - borrow_global_mut<AssetMappingProof>(system_addresses::get_starcoin_framework()).proof_root; + borrow_global_mut<AssetMappingPool>(system_addresses::get_starcoin_framework()).proof_root; let actual_root = starcoin_proof_verifier::computer_root_hash( proof_path_hash, blob_hash, diff --git a/vm/framework/starcoin-framework/doc/coin.md b/vm/framework/starcoin-framework/doc/coin.md index b93e67c8ee..ac56213ac3 100644 --- a/vm/framework/starcoin-framework/doc/coin.md +++ b/vm/framework/starcoin-framework/doc/coin.md @@ -1591,9 +1591,15 @@ Conversion from coin to fungible asset
public fun coin_to_fungible_asset<CoinType>(
     coin: Coin<CoinType>
 ): FungibleAsset acquires CoinConversionMap, CoinInfo {
+    debug::print(&string::utf8(b"coin::coin_to_fungible_asset | entered"));
+
     let metadata = ensure_paired_metadata<CoinType>();
     let amount = burn_internal(coin);
-    fungible_asset::mint_internal(metadata, amount)
+
+    let ret = fungible_asset::mint_internal(metadata, amount);
+
+    debug::print(&string::utf8(b"coin::coin_to_fungible_asset | exited"));
+    ret
 }
 
diff --git a/vm/framework/starcoin-framework/doc/fungible_asset.md b/vm/framework/starcoin-framework/doc/fungible_asset.md index 38d8d52948..e1b42385cd 100644 --- a/vm/framework/starcoin-framework/doc/fungible_asset.md +++ b/vm/framework/starcoin-framework/doc/fungible_asset.md @@ -3576,11 +3576,11 @@ Decrease the supply of a fungible asset by burning.
inline fun borrow_store_resource<T: key>(store: &Object<T>): &FungibleStore acquires FungibleStore {
-    debug::print(&string::utf8(b"fungible_asset::borrow_store_resource | entered"));
+    // debug::print(&string::utf8(b"fungible_asset::borrow_store_resource | entered"));
     let store_addr = object::object_address(store);
     debug::print(&store_addr);
     assert!(exists<FungibleStore>(store_addr), error::not_found(EFUNGIBLE_STORE_EXISTENCE));
-    debug::print(&string::utf8(b"fungible_asset::borrow_store_resource | exited"));
+    // debug::print(&string::utf8(b"fungible_asset::borrow_store_resource | exited"));
     borrow_global<FungibleStore>(store_addr)
 }
 
diff --git a/vm/framework/starcoin-framework/doc/object.md b/vm/framework/starcoin-framework/doc/object.md index d9bc8d748e..f62819822b 100644 --- a/vm/framework/starcoin-framework/doc/object.md +++ b/vm/framework/starcoin-framework/doc/object.md @@ -1097,11 +1097,9 @@ by knowing the user generated seed used to create them. Named objects cannot be
public fun create_named_object(creator: &signer, seed: vector<u8>): ConstructorRef {
-    // debug::print(&string::utf8(b"object::create_named_object | entered"));
     let creator_address = signer::address_of(creator);
     let obj_addr = create_object_address(&creator_address, seed);
     let ret = create_object_internal(creator_address, obj_addr, false);
-    // debug::print(&string::utf8(b"object::create_named_object | exited"));
     ret
 }
 
diff --git a/vm/framework/starcoin-framework/doc/stc_genesis.md b/vm/framework/starcoin-framework/doc/stc_genesis.md index 9d59577a9d..11c06a6d81 100644 --- a/vm/framework/starcoin-framework/doc/stc_genesis.md +++ b/vm/framework/starcoin-framework/doc/stc_genesis.md @@ -394,6 +394,7 @@ Overall governance allocation strategy: b"0x1::STC::STC", asset_mapping_coin ); + // fungible_asset::put_test_store_genesis(core_resource_account); // Initialize treasury let treasury_withdraw_cap = treasury::initialize(starcoin_framework, total_supply_stc); diff --git a/vm/framework/starcoin-framework/integration-tests/asset_mapping/basic.exp b/vm/framework/starcoin-framework/integration-tests/asset_mapping/basic.exp index a101ee7d56..e0ac221789 100644 --- a/vm/framework/starcoin-framework/integration-tests/asset_mapping/basic.exp +++ b/vm/framework/starcoin-framework/integration-tests/asset_mapping/basic.exp @@ -1,13 +1,18 @@ -processed 7 tasks +processed 6 tasks -task 5 'run'. lines 57-67: +task 5 'run'. lines 108-120: { - "gas_used": 5320541, - "status": "Executed" -} - -task 6 'run'. lines 69-93: -{ - "gas_used": 4349075, - "status": "Executed" + "gas_used": 936902, + "status": { + "ExecutionFailure": { + "location": { + "Module": { + "address": "0x00000000000000000000000000000001", + "name": "asset_mapping" + } + }, + "function": 0, + "code_offset": 54 + } + } } diff --git a/vm/framework/starcoin-framework/integration-tests/asset_mapping/basic.move b/vm/framework/starcoin-framework/integration-tests/asset_mapping/basic.move index 98856c7acf..0040186842 100644 --- a/vm/framework/starcoin-framework/integration-tests/asset_mapping/basic.move +++ b/vm/framework/starcoin-framework/integration-tests/asset_mapping/basic.move @@ -8,100 +8,113 @@ //# faucet --addr core_resources +// +// //# publish +// module bob::fake_money { +// use std::signer; +// use std::string; +// +// use starcoin_framework::coin; +// +// struct FakeMoney has key {} +// +// struct FakeMoneyCapabilities has key { +// burn_cap: coin::BurnCapability, +// freeze_cap: coin::FreezeCapability, +// mint_cap: coin::MintCapability, +// } +// +// public fun init(account: &signer, decimal: u8) { +// let ( +// burn_cap, +// freeze_cap, +// mint_cap +// ) = coin::initialize( +// account, +// string::utf8(b"FakeMoney"), +// string::utf8(b"FakeMoney"), +// decimal, +// true, +// ); +// coin::register(account); +// move_to(account, FakeMoneyCapabilities { +// burn_cap, +// freeze_cap, +// mint_cap, +// }) +// } +// +// public fun mint(account: &signer, amount: u64): coin::Coin acquires FakeMoneyCapabilities { +// let cap = borrow_global(signer::address_of(account)); +// coin::mint(amount, &cap.mint_cap) +// } +// +// public fun burn(coin: coin::Coin) acquires FakeMoneyCapabilities { +// let cap = borrow_global(@bob); +// coin::burn(coin, &cap.burn_cap) +// } +// } +// // check: EXECUTED +// +// //# run --signers bob +// script { +// use bob::fake_money::{Self, FakeMoney}; +// use starcoin_framework::asset_mapping; +// +// fun test_create_fake_money_store(account: &signer) { +// fake_money::init(account, 9); +// let fake_money_coin = fake_money::mint(account, 100000000000); +// asset_mapping::create_store_from_coin(account, b"bob::fake_money::FakeMoney", fake_money_coin); +// } +// } +// +// //# run --signers Genesis +// script { +// use bob::fake_money::{FakeMoney}; +// use starcoin_framework::coin; +// use starcoin_framework::asset_mapping; +// +// fun test_create_fake_money_store(account: &signer) { +// asset_mapping::assign_to_account(account, @bob, b"bob::fake_money::FakeMoney", 50000000000); +// assert!(coin::balance(@bob) == 50000000000, 10001); +// } +// } +// // check: EXECUTED +// +// //# run --signers core_resources +// script { +// use starcoin_framework::coin; +// use starcoin_framework::asset_mapping; +// use bob::fake_money::{FakeMoney}; +// +// fun test_create_fake_money_store(account: &signer) { +// asset_mapping::assign_to_account(account, @bob, b"bob::fake_money::FakeMoney", 50000000000); +// assert!(coin::balance(@bob) == 100000000000, 10002); +// } +// } +// // check: EXECUTED +// +// //# run --signers core_resources +// script { +// use starcoin_framework::asset_mapping; +// +// fun test_create_fake_money_store(account: &signer) { +// asset_mapping::assign_to_account(account, @bob, b"bob::fake_money::FakeMoney", 50000000000); +// } +// } +// // check: ABORT: fungible_asset.move 65540 -//# publish -module bob::fake_money { - use std::signer; - use std::string; - - use starcoin_framework::coin; - - struct FakeMoney has key {} - - struct FakeMoneyCapabilities has key { - burn_cap: coin::BurnCapability, - freeze_cap: coin::FreezeCapability, - mint_cap: coin::MintCapability, - } - - public fun init(account: &signer, decimal: u8) { - let ( - burn_cap, - freeze_cap, - mint_cap - ) = coin::initialize( - account, - string::utf8(b"FakeMoney"), - string::utf8(b"FakeMoney"), - decimal, - true, - ); - coin::register(account); - move_to(account, FakeMoneyCapabilities { - burn_cap, - freeze_cap, - mint_cap, - }) - } - - public fun mint(account: &signer, amount: u64): coin::Coin acquires FakeMoneyCapabilities { - let cap = borrow_global(signer::address_of(account)); - coin::mint(amount, &cap.mint_cap) - } - - public fun burn(coin: coin::Coin) acquires FakeMoneyCapabilities { - let cap = borrow_global(@bob); - coin::burn(coin, &cap.burn_cap) - } -} -// check: EXECUTED - -//# run --signers bob -script { - use bob::fake_money::{Self, FakeMoney}; - use starcoin_framework::asset_mapping; - - fun test_create_fake_money_store(account: &signer) { - fake_money::init(account, 9); - let fake_money_coin = fake_money::mint(account, 100000000000); - asset_mapping::create_store_from_coin(account, b"bob::fake_money::FakeMoney", fake_money_coin); - } -} //# run --signers Genesis script { - use bob::fake_money::{FakeMoney}; - use starcoin_framework::coin; - use starcoin_framework::asset_mapping; - - fun test_create_fake_money_store(account: &signer) { - asset_mapping::assign_to_account(account, @bob, b"bob::fake_money::FakeMoney", 100000000000); - assert!(coin::balance(@bob) == 100000000000, 10001); - } -} - -//# run --signers core_resources -script { - use bob::fake_money::{FakeMoney}; + use starcoin_framework::starcoin_coin::STC; use starcoin_framework::coin; use starcoin_framework::asset_mapping; - fun test_create_fake_money_store(account: &signer) { - asset_mapping::assign_to_account(account, @bob, b"bob::fake_money::FakeMoney", 100000000000); - assert!(coin::balance(@bob) == 100000000000, 10001); + fun test_asset_mapping_assign_to_account_with_proof(account: &signer) { + assert!(coin::balance(@alice) == 0, 10001); + asset_mapping::assign_to_account(account, @alice, b"0x1::STC::STC", 1000000000); + assert!(coin::balance(@alice) == 1000000000, 10002); } } - -// //# run --signers Genesis -// script { -// use starcoin_framework::starcoin_coin::STC; -// use starcoin_framework::coin; -// use starcoin_framework::asset_mapping; -// -// fun test_asset_mapping_assign_to_account_with_proof(framework: signer) { -// assert!(coin::balance(@alice) == 0, 10001); -// asset_mapping::assign_to_account(&framework, @alice, b"0x1::STC::STC", 100); -// assert!(coin::balance(@alice) == 100, 10002); -// } -// } -// // check: EXECUTED \ No newline at end of file +// check: EXECUTED diff --git a/vm/framework/starcoin-framework/sources/asset_mapping.move b/vm/framework/starcoin-framework/sources/asset_mapping.move index 651f76bccf..379bef0681 100644 --- a/vm/framework/starcoin-framework/sources/asset_mapping.move +++ b/vm/framework/starcoin-framework/sources/asset_mapping.move @@ -16,7 +16,7 @@ module starcoin_framework::asset_mapping { use starcoin_framework::stc_util; use starcoin_framework::system_addresses; use starcoin_std::debug; - use starcoin_std::smart_table; + use starcoin_std::simple_map::{Self, SimpleMap}; #[test_only] use std::vector; @@ -29,6 +29,7 @@ module starcoin_framework::asset_mapping { #[test_only] use starcoin_std::type_info; + #[resource_group_member(group = starcoin_framework::object::ObjectGroup)] /// AssetMappingStore represents a store for mapped assets /// Contains: /// - extend_ref: Reference for extending object capabilities @@ -37,18 +38,20 @@ module starcoin_framework::asset_mapping { struct AssetMappingStore has key, store { extend_ref: ExtendRef, fungible_store: Object, - fungible_metadata: Object, + metadata: Object } - struct AssetMappingProof has key, store { - proof_root: vector, + struct AssetMappingStoreT has key { + coin: coin::Coin, + old_path_str: vector, } /// AssetMappingCoinType represents a mapping that from old version token types to now version asset stores /// eg. 0x1::STC::STC -> 0x1::starcoin_coin::STC /// struct AssetMappingPool has key, store { - token_mapping: smart_table::SmartTable, + proof_root: vector, + token_mapping: SimpleMap, } /// Error code for invalid signer @@ -57,23 +60,24 @@ module starcoin_framework::asset_mapping { const EINVALID_NOT_PROOF: u64 = 103; const EINVALID_ASSET_MAPPING_POOL: u64 = 104; + const ASSET_MAPPING_OBJECT_SEED: vector = b"asset-mapping"; + /// Initializes the asset mapping pool /// @param framework - The framework signer /// @param proof_root - Initial proof root for verification /// Verifies the framework signer and creates a new AssetMappingPool - public fun initialize(framework: &signer, proof_root: vector) { + public fun initialize(framework: &signer, proof_root: vector){ assert!( signer::address_of(framework) == system_addresses::get_starcoin_framework(), error::unauthenticated(EINVALID_SIGNER) ); - move_to(framework, AssetMappingProof { + move_to(framework, AssetMappingPool { + token_mapping: simple_map::new(), proof_root, }); - move_to(framework, AssetMappingPool { - token_mapping: smart_table::new(), - }) } + /// Creates a new store from a coin /// @param token_issuer - The token issuer signer /// @param coin - The coin to be stored @@ -93,10 +97,12 @@ module starcoin_framework::asset_mapping { error::unauthenticated(EINVALID_SIGNER) ); + debug::print(&string::utf8(b"asset_mapping::create_store_from_coin | coin_to_fungible_asset")); + let fungible_asset = coin::coin_to_fungible_asset(coin); let ( - fungible_metadata, + metadata, fungible_store, extend_ref ) = create_store_for_coin_type(token_issuer); @@ -107,16 +113,23 @@ module starcoin_framework::asset_mapping { fungible_asset::deposit(fungible_store, fungible_asset); // Add token mapping coin type - let asset_coin_type = borrow_global_mut(system_addresses::get_starcoin_framework()); - smart_table::add( + let asset_coin_type = + borrow_global_mut(system_addresses::get_starcoin_framework()); + + let store_constructor_ref = &object::create_object(system_addresses::get_core_resource_address()); + let store_signer = &object::generate_signer(store_constructor_ref); + move_to(store_signer, AssetMappingStore { + extend_ref, + fungible_store, + metadata, + }); + + simple_map::add( &mut asset_coin_type.token_mapping, string::utf8(old_token_str), - AssetMappingStore { - fungible_store, - fungible_metadata, - extend_ref, - } + object::address_from_constructor_ref(store_constructor_ref), ); + debug::print(&string::utf8(b"asset_mapping::create_store_from_coin | exited")); } @@ -143,12 +156,11 @@ module starcoin_framework::asset_mapping { /// Retrieves the balance for a specific token type /// @returns Current balance of the token in the mapping pool - fun fungible_store_balance(old_asset_str: vector): u64 acquires AssetMappingPool { - // let metadata = coin::ensure_paired_metadata(); + fun fungible_store_balance(old_asset_str: vector): u64 acquires AssetMappingPool, AssetMappingStore { let pool = borrow_global(system_addresses::get_starcoin_framework()); - let fungible_asset_store = - smart_table::borrow(&pool.token_mapping, string::utf8(old_asset_str)).fungible_store; - fungible_asset::balance(fungible_asset_store) + let store_object_addr = simple_map::borrow(&pool.token_mapping, &string::utf8(old_asset_str)); + let mapping_store = borrow_global(*store_object_addr); + fungible_asset::balance(mapping_store.fungible_store) } public entry fun assign_to_account_with_proof( @@ -159,9 +171,9 @@ module starcoin_framework::asset_mapping { proof_value_hash: vector, proof_siblings: vector, amount: u64 - ) acquires AssetMappingPool, AssetMappingProof { + ) acquires AssetMappingPool, AssetMappingStore { assert!( - exists(system_addresses::get_starcoin_framework()), + exists(system_addresses::get_starcoin_framework()), error::invalid_state(EINVALID_PROOF_ROOT) ); @@ -187,7 +199,7 @@ module starcoin_framework::asset_mapping { receiver: address, old_token_str: vector, amount: u64 - ) acquires AssetMappingPool { + ) acquires AssetMappingPool, AssetMappingStore { debug::print(&string::utf8(b"asset_mapping::assign_to_account | entered")); let account_addr = signer::address_of(system_account); @@ -203,15 +215,16 @@ module starcoin_framework::asset_mapping { ); let coin_type_mapping = - borrow_global(system_addresses::get_starcoin_framework()); + borrow_global_mut(system_addresses::get_starcoin_framework()); debug::print(&string::utf8(b"asset_mapping::assign_to_account | coin_type_mapping")); + debug::print(&coin_type_mapping.token_mapping); - let mapping_store = smart_table::borrow( - &coin_type_mapping.token_mapping, - string::utf8(old_token_str) - ); - debug::print(&string::utf8(b"asset_mapping::assign_to_account | metadata")); - debug::print(&fungible_asset::is_frozen(mapping_store.fungible_store)); + let mapping_store_addr = simple_map::borrow(&coin_type_mapping.token_mapping, &string::utf8(old_token_str)); + debug::print(mapping_store_addr); + let mapping_store = borrow_global(*mapping_store_addr); + + // debug::print(&string::utf8(b"asset_mapping::assign_to_account | metadata")); + // debug::print(&fungible_asset::is_frozen(mapping_store.fungible_store)); debug::print(&string::utf8(b"asset_mapping::assign_to_account | fungible_asset::withdraw")); let mapping_fa = fungible_asset::withdraw( @@ -222,7 +235,7 @@ module starcoin_framework::asset_mapping { debug::print(&string::utf8(b"asset_mapping::assign_to_account | Getting receiver fungible store: ")); let target_store = - primary_fungible_store::ensure_primary_store_exists(receiver, mapping_store.fungible_metadata); + primary_fungible_store::ensure_primary_store_exists(receiver, mapping_store.metadata); fungible_asset::deposit(target_store, mapping_fa); debug::print(&string::utf8(b"asset_mapping::assign_to_account | exited")); @@ -233,9 +246,9 @@ module starcoin_framework::asset_mapping { proof_path_hash: vector, blob_hash: vector, proof_siblings: vector> - ): bool acquires AssetMappingProof { + ): bool acquires AssetMappingPool { let expect_proof_root = - borrow_global_mut(system_addresses::get_starcoin_framework()).proof_root; + borrow_global_mut(system_addresses::get_starcoin_framework()).proof_root; let actual_root = starcoin_proof_verifier::computer_root_hash( proof_path_hash, blob_hash, @@ -244,6 +257,7 @@ module starcoin_framework::asset_mapping { expect_proof_root == actual_root } + // Test function for asset mapping store creation and assignment // Tests // Store creation from coin @@ -254,7 +268,7 @@ module starcoin_framework::asset_mapping { fun test_asset_mapping_create_store_from_coin( framework: &signer, alice: &signer - ) acquires AssetMappingPool { + ) acquires AssetMappingPool, AssetMappingStore { debug::print(&std::string::utf8(b"asset_mapping::test_asset_mapping_create_store_from_coin | entered")); let amount = 10000000000; @@ -306,7 +320,7 @@ module starcoin_framework::asset_mapping { } #[test(framework= @starcoin_framework)] - fun test_asset_mapping_calculation_proof(framework: &signer) acquires AssetMappingProof { + fun test_asset_mapping_calculation_proof(framework: &signer) acquires AssetMappingPool { let siblings_data = vector::empty(); vector::append(&mut siblings_data, x"cfb1462d4fc72f736eab2a56b2bf72ca6ad1c4e8c79557046a8b0adce047f007"); vector::push_back(&mut siblings_data, splite_symbol()); diff --git a/vm/framework/starcoin-framework/sources/coin.move b/vm/framework/starcoin-framework/sources/coin.move index 48e2fd6116..9d01ea8c90 100644 --- a/vm/framework/starcoin-framework/sources/coin.move +++ b/vm/framework/starcoin-framework/sources/coin.move @@ -402,9 +402,15 @@ module starcoin_framework::coin { public fun coin_to_fungible_asset( coin: Coin ): FungibleAsset acquires CoinConversionMap, CoinInfo { + debug::print(&string::utf8(b"coin::coin_to_fungible_asset | entered")); + let metadata = ensure_paired_metadata(); let amount = burn_internal(coin); - fungible_asset::mint_internal(metadata, amount) + + let ret = fungible_asset::mint_internal(metadata, amount); + + debug::print(&string::utf8(b"coin::coin_to_fungible_asset | exited")); + ret } /// Conversion from fungible asset to coin. Not public to push the migration to FA. diff --git a/vm/framework/starcoin-framework/sources/fungible_asset.move b/vm/framework/starcoin-framework/sources/fungible_asset.move index 54089ea199..719579a1c1 100644 --- a/vm/framework/starcoin-framework/sources/fungible_asset.move +++ b/vm/framework/starcoin-framework/sources/fungible_asset.move @@ -1149,11 +1149,11 @@ module starcoin_framework::fungible_asset { } inline fun borrow_store_resource(store: &Object): &FungibleStore acquires FungibleStore { - debug::print(&string::utf8(b"fungible_asset::borrow_store_resource | entered")); + // debug::print(&string::utf8(b"fungible_asset::borrow_store_resource | entered")); let store_addr = object::object_address(store); debug::print(&store_addr); assert!(exists(store_addr), error::not_found(EFUNGIBLE_STORE_EXISTENCE)); - debug::print(&string::utf8(b"fungible_asset::borrow_store_resource | exited")); + // debug::print(&string::utf8(b"fungible_asset::borrow_store_resource | exited")); borrow_global(store_addr) } @@ -1208,6 +1208,7 @@ module starcoin_framework::fungible_asset { move_to(&object_signer, ConcurrentFungibleBalance { balance }); } + #[test_only] #[resource_group_member(group = starcoin_framework::object::ObjectGroup)] diff --git a/vm/framework/starcoin-framework/sources/object.move b/vm/framework/starcoin-framework/sources/object.move index 23671a14ad..4bf36e69d8 100644 --- a/vm/framework/starcoin-framework/sources/object.move +++ b/vm/framework/starcoin-framework/sources/object.move @@ -258,11 +258,9 @@ module starcoin_framework::object { /// Create a new named object and return the ConstructorRef. Named objects can be queried globally /// by knowing the user generated seed used to create them. Named objects cannot be deleted. public fun create_named_object(creator: &signer, seed: vector): ConstructorRef { - // debug::print(&string::utf8(b"object::create_named_object | entered")); let creator_address = signer::address_of(creator); let obj_addr = create_object_address(&creator_address, seed); let ret = create_object_internal(creator_address, obj_addr, false); - // debug::print(&string::utf8(b"object::create_named_object | exited")); ret } diff --git a/vm/framework/starcoin-framework/sources/stc/stc_genesis.move b/vm/framework/starcoin-framework/sources/stc/stc_genesis.move index 5d19898250..b4bb068ca1 100644 --- a/vm/framework/starcoin-framework/sources/stc/stc_genesis.move +++ b/vm/framework/starcoin-framework/sources/stc/stc_genesis.move @@ -4,6 +4,7 @@ module starcoin_framework::stc_genesis { use std::features; use std::option; use std::vector; + use starcoin_framework::fungible_asset; use starcoin_framework::asset_mapping; use starcoin_framework::account; @@ -310,6 +311,7 @@ module starcoin_framework::stc_genesis { b"0x1::STC::STC", asset_mapping_coin ); + // fungible_asset::put_test_store_genesis(core_resource_account); // Initialize treasury let treasury_withdraw_cap = treasury::initialize(starcoin_framework, total_supply_stc); diff --git a/vm/vm-runtime/src/move_vm_ext/vm.rs b/vm/vm-runtime/src/move_vm_ext/vm.rs index a7454c9521..c8b792ae21 100644 --- a/vm/vm-runtime/src/move_vm_ext/vm.rs +++ b/vm/vm-runtime/src/move_vm_ext/vm.rs @@ -25,10 +25,11 @@ use starcoin_vm_types::{ use std::ops::Deref; use std::sync::Arc; +use starcoin_framework::natives::transaction_context::NativeTransactionContext; pub struct MoveVmExt { inner: MoveVM, - _chain_id: u8, + chain_id: u8, features: Arc, } @@ -37,7 +38,7 @@ impl MoveVmExt { native_gas_parameters: NativeGasParameters, misc_gas_parameters: MiscGasParameters, gas_feature_version: u64, - _chain_id: u8, + chain_id: u8, features: Features, timed_features: TimedFeatures, gas_hook: Option, @@ -65,7 +66,7 @@ impl MoveVmExt { } Ok(Self { inner: WarmVmCache::get_warm_vm(builder, vm_config, resolver)?, - _chain_id, + chain_id, features: Arc::new(features), }) } @@ -131,6 +132,14 @@ impl MoveVmExt { extensions.add(NativeAggregatorContext::new(txn_hash, resolver, resolver)); extensions.add(NativeEventContext::default()); extensions.add(NativeObjectContext::default()); + extensions.add(NativeTransactionContext::new( + txn_hash.to_vec(), + //session_id.into_script_hash(), + vec![1], // TODO(BobOng): [compiler-v2] to confirm the script hash + self.chain_id, + // TODO(BobOng): [compiler-v2] to confirm the user transaction context + None, + )); // The VM code loader has bugs around module upgrade. After a module upgrade, the internal // cache needs to be flushed to work around those bugs.