Skip to content

Commit

Permalink
[asset-mapping] fixed some code format
Browse files Browse the repository at this point in the history
  • Loading branch information
welbon committed Jan 24, 2025
1 parent a8f2710 commit 1ff229f
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 46 deletions.
43 changes: 26 additions & 17 deletions asset-mapping/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use starcoin_config::{ChainNetwork, G_TEST_CONFIG};
use starcoin_consensus::Consensus;
use starcoin_state_api::ChainStateReader;
use starcoin_types::{
account_address::AccountAddress,
account_config::CORE_CODE_ADDRESS, identifier::Identifier, language_storage::StructTag,
account_address::AccountAddress, account_config::CORE_CODE_ADDRESS, identifier::Identifier,
language_storage::StructTag,
};
use starcoin_vm_types::{
access_path::AccessPath,
Expand All @@ -25,6 +25,7 @@ use starcoin_vm_types::{
state_store::state_key::StateKey,
state_view::StateReaderExt,
};
use starcoin_vm_types::account_config::{CoinStoreResource, stc_struct_tag};
use test_helper::executor::{
association_execute_should_success, prepare_customized_genesis, prepare_genesis,
};
Expand Down Expand Up @@ -88,7 +89,7 @@ fn test_sha3_256_diffrent_with_crypto_macro() -> Result<()> {
HashValue::sha3_256_of(STARCOIN_HASH_PREFIX).as_slice(),
ser.as_slice(),
]
.concat();
.concat();

let move_hash = HashValue::sha3_256_of(&hash_vec[..]);
println!(
Expand Down Expand Up @@ -188,24 +189,19 @@ fn test_asset_mapping_whole_process() -> Result<()> {
&chain_state_1,
transfer_scripts_peer_to_peer_v2(stc_type_tag(), alice, initial_balance),
)?;

// Check balance is initial_balance
let balance = chain_state_1.get_balance(alice)?;
assert_eq!(balance, initial_balance);

let proof = chain_state_1.get_with_proof(&StateKey::resource(
&CORE_CODE_ADDRESS,
&StructTag {
address: CORE_CODE_ADDRESS,
module: Identifier::new("coin").unwrap(),
name: Identifier::new("CoinStore").unwrap(),
type_args: vec![stc_type_tag()],
},
)?)?;
let state_proof = chain_state_1.get_with_proof(
&StateKey::resource(&alice, &CoinStoreResource::struct_tag_for_token(stc_struct_tag()))?,
)?;
(
chain_state_1.state_root(),
proof.proof.account_state_proof.leaf.unwrap().0,
proof.proof.account_state_proof.leaf.unwrap().1,
proof.proof.account_state_proof.siblings,
state_proof.proof.account_state_proof.leaf().unwrap().0,
state_proof.proof.account_state_proof.leaf().unwrap().1,
state_proof.proof.account_state_proof.siblings,
)
};

Expand Down Expand Up @@ -241,7 +237,7 @@ fn test_asset_mapping_whole_process() -> Result<()> {
starcoin_account_create_account(alice),
)?;

assert_eq!(chain_state_2.get_balance(alice)?, initial_balance + 1);
assert_eq!(chain_state_2.get_balance(alice)?, 0);

// Asset mapping for alice
association_execute_should_success(
Expand All @@ -257,8 +253,21 @@ fn test_asset_mapping_whole_process() -> Result<()> {
),
)?;

assert_eq!(chain_state_2.get_balance(alice)?, initial_balance + 1);
assert_eq!(chain_state_2.get_balance(alice)?, initial_balance);
}

Ok(())
}

#[test]
fn test_hash_hello() -> Result<()> {
// 0x3338be694f50c5f338814986cdf0686453a888b84f424d792af4b9202398f392
let hello_hash = HashValue::sha3_256_of("hello".as_bytes());
println!("test_hash_hello | {:?}", hello_hash);
assert_eq!(
hello_hash.to_hex_literal(),
"0x3338be694f50c5f338814986cdf0686453a888b84f424d792af4b9202398f392",
"not expect hash"
);
Ok(())
}
2 changes: 1 addition & 1 deletion cmd/starcoin/src/account/show_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl CommandAction for ShowCommand {
let balance = resource
.decode::<CoinStoreResource>()
.ok()
.map(|balance| balance.token());
.map(|balance| balance.coin() as u128);
Some((token_code, balance.unwrap_or(0)))
} else {
None
Expand Down
2 changes: 1 addition & 1 deletion state/statedb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl AccountStateObject {
}

pub fn set(&self, data_path: DataPath, value: Vec<u8>) {
let human_str = format!("{}", data_path);
let _human_str = format!("{}", data_path);
match data_path {
DataPath::Code(module_name) => {
if self.code_tree.lock().is_none() {
Expand Down
4 changes: 2 additions & 2 deletions vm/framework/starcoin-framework/doc/asset_mapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ Verifies the framework signer and creates a new AssetMappingPool
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="asset_mapping.md#0x1_asset_mapping_initialize">initialize</a>(framework: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>, proof_root: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;){
<pre><code><b>public</b> <b>fun</b> <a href="asset_mapping.md#0x1_asset_mapping_initialize">initialize</a>(framework: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>, proof_root: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;) {
<b>assert</b>!(
<a href="../../move-stdlib/doc/signer.md#0x1_signer_address_of">signer::address_of</a>(framework) == <a href="system_addresses.md#0x1_system_addresses_get_starcoin_framework">system_addresses::get_starcoin_framework</a>(),
<a href="../../move-stdlib/doc/error.md#0x1_error_unauthenticated">error::unauthenticated</a>(<a href="asset_mapping.md#0x1_asset_mapping_EINVALID_SIGNER">EINVALID_SIGNER</a>)
Expand Down Expand Up @@ -455,7 +455,7 @@ Retrieves the balance for a specific token type
receiver: <b>address</b>,
old_token_str: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;,
amount: u64
) <b>acquires</b> <a href="asset_mapping.md#0x1_asset_mapping_AssetMappingPool">AssetMappingPool</a>, <a href="asset_mapping.md#0x1_asset_mapping_AssetMappingStore">AssetMappingStore</a> {
) <b>acquires</b> <a href="asset_mapping.md#0x1_asset_mapping_AssetMappingPool">AssetMappingPool</a>, <a href="asset_mapping.md#0x1_asset_mapping_AssetMappingStore">AssetMappingStore</a> {
<a href="asset_mapping.md#0x1_asset_mapping_assign_to_account">Self::assign_to_account</a>(system_account, receiver, old_token_str, amount);
}
</code></pre>
Expand Down
58 changes: 51 additions & 7 deletions vm/framework/starcoin-framework/sources/asset_mapping.move
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ module starcoin_framework::asset_mapping {
use starcoin_framework::system_addresses;
use starcoin_std::debug;
use starcoin_std::simple_map::{Self, SimpleMap};
#[test_only]
use std::hash;

#[test_only]
use std::vector;
Expand Down Expand Up @@ -67,7 +69,7 @@ module starcoin_framework::asset_mapping {
/// @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<u8>){
public fun initialize(framework: &signer, proof_root: vector<u8>) {
assert!(
signer::address_of(framework) == system_addresses::get_starcoin_framework(),
error::unauthenticated(EINVALID_SIGNER)
Expand Down Expand Up @@ -192,7 +194,7 @@ module starcoin_framework::asset_mapping {
receiver: address,
old_token_str: vector<u8>,
amount: u64
) acquires AssetMappingPool, AssetMappingStore {
) acquires AssetMappingPool, AssetMappingStore {
Self::assign_to_account(system_account, receiver, old_token_str, amount);
}

Expand Down Expand Up @@ -296,21 +298,39 @@ module starcoin_framework::asset_mapping {
let amount = 10000000000;
Self::initialize(framework, vector::empty<u8>());

debug::print(&std::string::utf8(b"asset_mapping::test_asset_mapping_create_store_from_coin | before create_account_for_test"));
debug::print(
&std::string::utf8(
b"asset_mapping::test_asset_mapping_create_store_from_coin | before create_account_for_test"
)
);
// create genesis account
account::create_account_for_test(signer::address_of(framework));

debug::print(&std::string::utf8(b"asset_mapping::test_asset_mapping_create_store_from_coin | starcoin_coin::initialize_for_test"));
debug::print(
&std::string::utf8(
b"asset_mapping::test_asset_mapping_create_store_from_coin | starcoin_coin::initialize_for_test"
)
);

let (burn_cap, mint_cap) = starcoin_coin::initialize_for_test(framework);

debug::print(&std::string::utf8(b"asset_mapping::test_asset_mapping_create_store_from_coin | coin::register<STC>(framework)"));
debug::print(
&std::string::utf8(
b"asset_mapping::test_asset_mapping_create_store_from_coin | coin::register<STC>(framework)"
)
);
coin::register<STC>(framework);

debug::print(&std::string::utf8(b"asset_mapping::test_asset_mapping_create_store_from_coin | starcoin_coin::mint"));
debug::print(
&std::string::utf8(b"asset_mapping::test_asset_mapping_create_store_from_coin | starcoin_coin::mint")
);
starcoin_coin::mint(framework, signer::address_of(framework), amount);

debug::print(&std::string::utf8(b"asset_mapping::test_asset_mapping_create_store_from_coin | after coin::register<STC>(framework) and mint"));
debug::print(
&std::string::utf8(
b"asset_mapping::test_asset_mapping_create_store_from_coin | after coin::register<STC>(framework) and mint"
)
);

// Construct Old token string
let old_token_str = b"0x00000000000000000000000000000001::starcoin_coin::STC";
Expand Down Expand Up @@ -402,4 +422,28 @@ module starcoin_framework::asset_mapping {
);
debug::print(&std::string::utf8(b"asset_mapping::test_asset_mapping_coin_type_verify | exited"));
}

#[test]
fun test_calculation_proof_1() {
let proof_sibling_data = vector::empty<vector<u8>>();
vector::push_back(&mut proof_sibling_data, x"6b67362f680d4d15f996aed2a5c83e3dce37cb37bed4bc498aaeef77ea8a28a2");
vector::push_back(&mut proof_sibling_data, x"5350415253455f4d45524b4c455f504c414345484f4c4445525f484153480000");
vector::push_back(&mut proof_sibling_data, x"5ca9febe74c7fde3fdcf2bd464de6d8899a0a13d464893aada2714c6fa774f9d");
vector::push_back(&mut proof_sibling_data, x"06fa88f7fae77461044d10cc504c5e6666910d1ee4d1b1d99f8dbea047d0c9ff");
vector::push_back(&mut proof_sibling_data, x"5f3620db0071243d18285e1a2c4d74b734421e65581bbb41e70498369c863cdb");
vector::push_back(&mut proof_sibling_data, x"4949e6d0a2be6d8a79fd3fee859e10e564815e88a16dec26760be15c8ae017e7");
vector::push_back(&mut proof_sibling_data, x"8cd8632ea21b3a4623bb825d2451f6c76055cda7433e1da3d76773dba7c06878");
vector::push_back(&mut proof_sibling_data, x"379f1d32988ebd8d01627d0326523e28aa5fa1dbf2e87d076d7dca72884a4c46");

assert!(starcoin_proof_verifier::computer_root_hash(
x"9afe1e0e6013eb63b6004a4eb6b1bf76bdb04b725619648163d9dbc3194f224c",
x"b9d3ba6fe71eff0b1e9c9d70401fd6767a15a82a28f2542dbc27fda50730b6e9",
proof_sibling_data,
) == x"a307d98b0b6da330fb0ac31283d6913d18627412a515b0c88e59346dfe04e0d5", 10011);
}

#[test]
fun test_asset_mapping_hello_hash() {
debug::print(&hash::sha3_256(b"hello"));
}
}
29 changes: 13 additions & 16 deletions vm/types/src/state_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use crate::{
use anyhow::{format_err, Result};
use bytes::Bytes;
use log::warn;
use move_core_types::identifier::Identifier;
use move_core_types::move_resource::MoveStructType;
use move_core_types::{
account_address::AccountAddress,
Expand Down Expand Up @@ -127,21 +126,19 @@ pub trait StateReaderExt: StateView {
/// Get balance by address and coin type
fn get_balance_by_type(&self, address: AccountAddress, type_tag: StructTag) -> Result<u128> {
// Get from coin store
let coin_balance = bcs_ext::from_bytes::<CoinStoreResource>(
&self
.get_state_value_bytes(&StateKey::resource(
&address,
&CoinStoreResource::struct_tag_for_token(type_tag.clone()),
)?)?
.ok_or_else(|| {
format_err!(
"CoinStoreResource not exists at address:{} for type tag:{}",
address,
type_tag
)
})?,
)?
.coin() as u128;
let coin_balance = match self.get_state_value_bytes(&StateKey::resource(
&address,
&CoinStoreResource::struct_tag_for_token(type_tag.clone()),
)?)? {
Some(bytes) => bcs_ext::from_bytes::<CoinStoreResource>(&bytes)?.coin() as u128,
None => {
warn!(
"CoinStoreResource not exists at address:{} for type tag:{}",
address, type_tag
);
0
}
};

let primary_store_address = fungible_store::primary_store(&address, &type_tag.address);
// Get from coin store
Expand Down
8 changes: 6 additions & 2 deletions vm/types/src/token/stc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ pub static G_STC_TOKEN_CODE: Lazy<TokenCode> = Lazy::new(|| {
static G_STC_IDENTIFIER: Lazy<Identifier> = Lazy::new(|| Identifier::new(STC_NAME).unwrap());

pub fn stc_type_tag() -> TypeTag {
TypeTag::Struct(Box::new(StructTag {
TypeTag::Struct(Box::new(stc_struct_tag()))
}

pub fn stc_struct_tag() -> StructTag {
StructTag {
address: CORE_CODE_ADDRESS,
module: Identifier::new("starcoin_coin").unwrap(),
name: G_STC_IDENTIFIER.clone(),
type_args: vec![],
}))
}
}

pub const SYMBOL_NANOSTC: &str = "nanoSTC";
Expand Down

0 comments on commit 1ff229f

Please sign in to comment.