Skip to content

Commit

Permalink
If FA is enabled, pass correct coin_store_location in read/write sets…
Browse files Browse the repository at this point in the history
… of sharded txns
  • Loading branch information
manudhundi committed Nov 21, 2024
1 parent daea913 commit a4c1b24
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ impl<'a> AptosTestAdapter<'a> {
&StateKey::resource_typed::<CoinStoreResource<AptosCoinType>>(signer_addr)
.expect("failed to create StateKey"),
)
.expect("account must exist in data store");
.expect("account must exist in data store");
let coin = if let Some(data) = data_blob {
bcs::from_bytes::<CoinStoreResource<AptosCoinType>>(&data)
.ok()
Expand All @@ -459,7 +459,7 @@ impl<'a> AptosTestAdapter<'a> {
&ObjectGroupResource::struct_tag(),
),
)
.expect("account must exist in data store");
.expect("account must exist in data store");

let group: Option<BTreeMap<StructTag, Vec<u8>>> = bytes_opt
.map(|bytes| bcs::from_bytes(&bytes))
Expand Down
87 changes: 79 additions & 8 deletions aptos-move/aptos-vm/tests/sharded_block_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,21 +193,26 @@ mod test_utils {
executor::FakeExecutor,
};
use aptos_types::{
account_config::ObjectGroupResource,
block_executor::{
config::BlockExecutorConfigFromOnchain, partitioner::PartitionedTransactions,
},
on_chain_config::{FeatureFlag, Features, OnChainConfig},
state_store::state_key::{inner::StateKeyInner, StateKey},
transaction::{
analyzed_transaction::AnalyzedTransaction,
signature_verified_transaction::SignatureVerifiedTransaction, Transaction,
TransactionOutput,
analyzed_transaction::{
initialize_default_to_fa_apt_store, AnalyzedTransaction, DEFAULT_TO_FA_APT_STORE,
},
signature_verified_transaction::SignatureVerifiedTransaction,
Transaction, TransactionOutput,
},
};
use aptos_vm::{
aptos_vm::AptosVMBlockExecutor,
sharded_block_executor::{executor_client::ExecutorClient, ShardedBlockExecutor},
VMBlockExecutor,
};
use move_core_types::account_address::AccountAddress;
use move_core_types::{account_address::AccountAddress, move_resource::MoveStructType};
use rand::{rngs::OsRng, Rng};
use std::{
collections::HashMap,
Expand Down Expand Up @@ -263,6 +268,18 @@ mod test_utils {
sharded_txn_output: Vec<TransactionOutput>,
) {
assert_eq!(unsharded_txn_output.len(), sharded_txn_output.len());

// TODO: Fix total_supply with FA enabled
let key_to_filter = AccountAddress::from_hex_literal("0xa").unwrap();
let state_key_to_filter =
StateKey::resource_group(&key_to_filter, &ObjectGroupResource::struct_tag());
let path_to_filter =
if let StateKeyInner::AccessPath(access_path) = state_key_to_filter.inner() {
access_path
} else {
panic!("Expected AccessPath");
};

for i in 0..unsharded_txn_output.len() {
assert_eq!(
unsharded_txn_output[i].status(),
Expand All @@ -272,10 +289,41 @@ mod test_utils {
unsharded_txn_output[i].gas_used(),
sharded_txn_output[i].gas_used()
);
assert_eq!(
unsharded_txn_output[i].write_set(),
sharded_txn_output[i].write_set()
);
if *DEFAULT_TO_FA_APT_STORE.get().unwrap() {
let unsharded_write_set_without_table_item = unsharded_txn_output[i]
.write_set()
.into_iter()
.filter(|(k, _)| {
if let StateKeyInner::AccessPath(access_path) = k.inner() {
!(access_path.address == key_to_filter
&& access_path.path == path_to_filter.path)
} else {
true
}
})
.collect::<Vec<_>>();
let sharded_write_set_without_table_item = sharded_txn_output[i]
.write_set()
.into_iter()
.filter(|(k, _)| {
if let StateKeyInner::AccessPath(access_path) = k.inner() {
!(access_path.address == key_to_filter
&& access_path.path == path_to_filter.path)
} else {
true
}
})
.collect::<Vec<_>>();
assert_eq!(
unsharded_write_set_without_table_item,
sharded_write_set_without_table_item
);
} else {
assert_eq!(
unsharded_txn_output[i].write_set(),
sharded_txn_output[i].write_set()
);
}
assert_eq!(
unsharded_txn_output[i].events(),
sharded_txn_output[i].events()
Expand All @@ -291,6 +339,14 @@ mod test_utils {
let num_shards = 8;
let mut executor = FakeExecutor::from_head_genesis();
let mut transactions = Vec::new();

if DEFAULT_TO_FA_APT_STORE.get().is_none() {
let features = Features::fetch_config(&executor.data_store()).unwrap_or_default();
let use_fa_balance =
features.is_enabled(FeatureFlag::NEW_ACCOUNTS_DEFAULT_TO_FA_APT_STORE);
initialize_default_to_fa_apt_store(use_fa_balance);
}

for _ in 0..num_txns {
transactions.push(generate_non_conflicting_p2p(&mut executor).0)
}
Expand Down Expand Up @@ -328,6 +384,14 @@ mod test_utils {
let mut transactions = Vec::new();
let mut accounts = Vec::new();
let mut txn_hash_to_account = HashMap::new();

if DEFAULT_TO_FA_APT_STORE.get().is_none() {
let features = Features::fetch_config(&executor.data_store()).unwrap_or_default();
let use_fa_balance =
features.is_enabled(FeatureFlag::NEW_ACCOUNTS_DEFAULT_TO_FA_APT_STORE);
initialize_default_to_fa_apt_store(use_fa_balance);
}

for _ in 0..num_accounts {
let account = generate_account_at(&mut executor, AccountAddress::random());
accounts.push(Mutex::new(account));
Expand Down Expand Up @@ -379,6 +443,13 @@ mod test_utils {
let mut accounts = Vec::new();
let mut executor = FakeExecutor::from_head_genesis();

if DEFAULT_TO_FA_APT_STORE.get().is_none() {
let features = Features::fetch_config(&executor.data_store()).unwrap_or_default();
let use_fa_balance =
features.is_enabled(FeatureFlag::NEW_ACCOUNTS_DEFAULT_TO_FA_APT_STORE);
initialize_default_to_fa_apt_store(use_fa_balance);
}

for _ in 0..num_accounts {
let account = generate_account_at(&mut executor, AccountAddress::random());
accounts.push(Mutex::new(account));
Expand Down
30 changes: 25 additions & 5 deletions types/src/transaction/analyzed_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// SPDX-License-Identifier: Apache-2.0

use crate::{
account_config::{AccountResource, CoinInfoResource, CoinStoreResource},
account_config,
account_config::{AccountResource, CoinInfoResource, CoinStoreResource, ObjectGroupResource},
chain_id::ChainId,
on_chain_config::{CurrentTimeMicroseconds, Features, TransactionFeeBurnCap},
state_store::{state_key::StateKey, table::TableHandle},
Expand All @@ -16,10 +17,21 @@ use aptos_crypto::HashValue;
pub use move_core_types::abi::{
ArgumentABI, ScriptFunctionABI as EntryFunctionABI, TransactionScriptABI, TypeArgumentABI,
};
use move_core_types::{account_address::AccountAddress, language_storage::StructTag};
use move_core_types::{
account_address::AccountAddress, language_storage::StructTag, move_resource::MoveStructType,
};
use once_cell::sync::OnceCell;
use serde::{Deserialize, Serialize};
use std::hash::{Hash, Hasher};

pub static DEFAULT_TO_FA_APT_STORE: OnceCell<bool> = OnceCell::new();

pub fn initialize_default_to_fa_apt_store(value: bool) {
DEFAULT_TO_FA_APT_STORE
.set(value)
.expect("DEFAULT_TO_FA_APT_STORE can only be initialized once");
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct AnalyzedTransaction {
transaction: SignatureVerifiedTransaction,
Expand Down Expand Up @@ -162,9 +174,17 @@ pub fn account_resource_location(address: AccountAddress) -> StorageLocation {
}

pub fn coin_store_location(address: AccountAddress) -> StorageLocation {
StorageLocation::Specific(
StateKey::resource_typed::<CoinStoreResource<AptosCoinType>>(&address).unwrap(),
)
let default_to_fa_apt_store = DEFAULT_TO_FA_APT_STORE.get();
if default_to_fa_apt_store.is_some() && *default_to_fa_apt_store.unwrap() {
StorageLocation::Specific(StateKey::resource_group(
&account_config::fungible_store::primary_apt_store(address),
&ObjectGroupResource::struct_tag(),
))
} else {
StorageLocation::Specific(
StateKey::resource_typed::<CoinStoreResource<AptosCoinType>>(&address).unwrap(),
)
}
}

pub fn current_ts_location() -> StorageLocation {
Expand Down

0 comments on commit a4c1b24

Please sign in to comment.