From eded498c183c9eafa769a62354383029c2cf24f5 Mon Sep 17 00:00:00 2001 From: Igor Date: Tue, 5 Nov 2024 13:08:12 -0800 Subject: [PATCH] reorganizing files a bit fixing unit test failure --- aptos-move/e2e-tests/src/executor.rs | 8 +- .../executor-benchmark/src/native_executor.rs | 2 +- types/src/account_config/events/coin.rs | 113 ++++++++++++++++++ types/src/account_config/events/deposit.rs | 28 ----- .../account_config/events/fungible_asset.rs | 36 ++++++ types/src/account_config/events/mod.rs | 8 +- types/src/account_config/events/withdraw.rs | 48 -------- .../src/account_config/resources/coin_info.rs | 14 ++- .../account_config/resources/coin_store.rs | 8 ++ .../resources/fungible_asset_metadata.rs | 6 +- .../resources/fungible_store.rs | 32 +---- types/src/account_config/resources/mod.rs | 2 + .../src/account_config/resources/type_info.rs | 52 ++++++++ types/src/event.rs | 1 - types/src/move_utils/mod.rs | 1 + types/src/move_utils/move_event_v1.rs | 19 +++ types/src/utility_coin.rs | 9 ++ 17 files changed, 261 insertions(+), 126 deletions(-) create mode 100644 types/src/account_config/events/coin.rs delete mode 100644 types/src/account_config/events/deposit.rs create mode 100644 types/src/account_config/events/fungible_asset.rs delete mode 100644 types/src/account_config/events/withdraw.rs create mode 100644 types/src/account_config/resources/type_info.rs create mode 100644 types/src/move_utils/move_event_v1.rs diff --git a/aptos-move/e2e-tests/src/executor.rs b/aptos-move/e2e-tests/src/executor.rs index 4c44a7e585c634..b97961dad4efc8 100644 --- a/aptos-move/e2e-tests/src/executor.rs +++ b/aptos-move/e2e-tests/src/executor.rs @@ -24,7 +24,7 @@ use aptos_keygen::KeyGen; use aptos_types::{ account_config::{ new_block_event_key, AccountResource, CoinInfoResource, CoinStoreResource, - ConcurrentSupply, NewBlockEvent, ObjectGroupResource, CORE_CODE_ADDRESS, + ConcurrentSupplyResource, NewBlockEvent, ObjectGroupResource, CORE_CODE_ADDRESS, }, block_executor::config::{ BlockExecutorConfig, BlockExecutorConfigFromOnchain, BlockExecutorLocalConfig, @@ -438,10 +438,10 @@ impl FakeExecutor { let mut fa_resource_group = self .read_resource_group::(&AccountAddress::TEN) .expect("resource group must exist in data store"); - let mut supply = bcs::from_bytes::( + let mut supply = bcs::from_bytes::( fa_resource_group .group - .get(&ConcurrentSupply::struct_tag()) + .get(&ConcurrentSupplyResource::struct_tag()) .unwrap(), ) .unwrap(); @@ -451,7 +451,7 @@ impl FakeExecutor { fa_resource_group .group .insert( - ConcurrentSupply::struct_tag(), + ConcurrentSupplyResource::struct_tag(), bcs::to_bytes(&supply).unwrap(), ) .unwrap(); diff --git a/execution/executor-benchmark/src/native_executor.rs b/execution/executor-benchmark/src/native_executor.rs index 00b725936e1be7..9fc4827b207510 100644 --- a/execution/executor-benchmark/src/native_executor.rs +++ b/execution/executor-benchmark/src/native_executor.rs @@ -15,7 +15,7 @@ use aptos_executor_types::execution_output::ExecutionOutput; use aptos_storage_interface::cached_state_view::CachedStateView; use aptos_types::{ account_address::AccountAddress, - account_config::{deposit::DepositEvent, withdraw::WithdrawEvent}, + account_config::{DepositEvent, WithdrawEvent}, block_executor::{ config::BlockExecutorConfigFromOnchain, partitioner::{ExecutableTransactions, PartitionedTransactions}, diff --git a/types/src/account_config/events/coin.rs b/types/src/account_config/events/coin.rs new file mode 100644 index 00000000000000..1a0cf5844e7e53 --- /dev/null +++ b/types/src/account_config/events/coin.rs @@ -0,0 +1,113 @@ +// Copyright © Aptos Foundation +// SPDX-License-Identifier: Apache-2.0 + +use crate::{ + account_config::TypeInfoResource, + move_utils::{move_event_v1::MoveEventV1Type, move_event_v2::MoveEventV2Type}, +}; +use anyhow::Result; +use move_core_types::{ + account_address::AccountAddress, ident_str, identifier::IdentStr, move_resource::MoveStructType, +}; +use serde::{Deserialize, Serialize}; + +/// Struct that represents a SentPaymentEvent. +#[derive(Debug, Serialize, Deserialize)] +pub struct WithdrawEvent { + pub amount: u64, +} + +impl WithdrawEvent { + pub fn try_from_bytes(bytes: &[u8]) -> Result { + bcs::from_bytes(bytes).map_err(Into::into) + } + + /// Get the amount sent or received + pub fn amount(&self) -> u64 { + self.amount + } +} + +impl MoveStructType for WithdrawEvent { + const MODULE_NAME: &'static IdentStr = ident_str!("coin"); + const STRUCT_NAME: &'static IdentStr = ident_str!("WithdrawEvent"); +} + +impl MoveEventV1Type for WithdrawEvent {} + +#[derive(Debug, Serialize, Deserialize)] +pub struct CoinWithdraw { + pub coin_type: String, + pub account: AccountAddress, + pub amount: u64, +} + +impl CoinWithdraw { + pub fn try_from_bytes(bytes: &[u8]) -> Result { + bcs::from_bytes(bytes).map_err(Into::into) + } +} + +impl MoveStructType for CoinWithdraw { + const MODULE_NAME: &'static IdentStr = ident_str!("coin"); + const STRUCT_NAME: &'static IdentStr = ident_str!("CoinWithdraw"); +} + +impl MoveEventV2Type for CoinWithdraw {} + +/// Struct that represents a DepositPaymentEvent. +#[derive(Debug, Serialize, Deserialize)] +pub struct DepositEvent { + pub amount: u64, +} + +impl DepositEvent { + pub fn try_from_bytes(bytes: &[u8]) -> Result { + bcs::from_bytes(bytes).map_err(Into::into) + } + + /// Get the amount sent or received + pub fn amount(&self) -> u64 { + self.amount + } +} + +impl MoveStructType for DepositEvent { + const MODULE_NAME: &'static IdentStr = ident_str!("coin"); + const STRUCT_NAME: &'static IdentStr = ident_str!("DepositEvent"); +} + +impl MoveEventV1Type for DepositEvent {} + +#[derive(Debug, Serialize, Deserialize)] +pub struct CoinDeposit { + pub coin_type: String, + pub account: AccountAddress, + pub amount: u64, +} + +impl CoinDeposit { + pub fn try_from_bytes(bytes: &[u8]) -> Result { + bcs::from_bytes(bytes).map_err(Into::into) + } +} + +impl MoveStructType for CoinDeposit { + const MODULE_NAME: &'static IdentStr = ident_str!("coin"); + const STRUCT_NAME: &'static IdentStr = ident_str!("CoinDeposit"); +} + +impl MoveEventV2Type for CoinDeposit {} + +#[derive(Debug, Serialize, Deserialize)] +pub struct CoinRegister { + pub account: AccountAddress, + pub type_info: TypeInfoResource, +} + +impl MoveStructType for CoinRegister { + const MODULE_NAME: &'static IdentStr = ident_str!("account"); + const STRUCT_NAME: &'static IdentStr = ident_str!("CoinRegister"); +} + +impl MoveEventV2Type for CoinRegister {} diff --git a/types/src/account_config/events/deposit.rs b/types/src/account_config/events/deposit.rs deleted file mode 100644 index 2c59bc4fc336f1..00000000000000 --- a/types/src/account_config/events/deposit.rs +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright © Aptos Foundation -// SPDX-License-Identifier: Apache-2.0 - -use anyhow::Result; -use move_core_types::{ident_str, identifier::IdentStr, move_resource::MoveStructType}; -use serde::{Deserialize, Serialize}; - -/// Struct that represents a DepositPaymentEvent. -#[derive(Debug, Serialize, Deserialize)] -pub struct DepositEvent { - amount: u64, -} - -impl DepositEvent { - pub fn try_from_bytes(bytes: &[u8]) -> Result { - bcs::from_bytes(bytes).map_err(Into::into) - } - - /// Get the amount sent or received - pub fn amount(&self) -> u64 { - self.amount - } -} - -impl MoveStructType for DepositEvent { - const MODULE_NAME: &'static IdentStr = ident_str!("coin"); - const STRUCT_NAME: &'static IdentStr = ident_str!("DepositEvent"); -} diff --git a/types/src/account_config/events/fungible_asset.rs b/types/src/account_config/events/fungible_asset.rs new file mode 100644 index 00000000000000..ed003be6d64829 --- /dev/null +++ b/types/src/account_config/events/fungible_asset.rs @@ -0,0 +1,36 @@ +// Copyright © Aptos Foundation +// SPDX-License-Identifier: Apache-2.0 + +use crate::move_utils::move_event_v2::MoveEventV2Type; +use move_core_types::{ + account_address::AccountAddress, ident_str, identifier::IdentStr, move_resource::MoveStructType, +}; +use serde::{Deserialize, Serialize}; + +/// Struct that represents a Withdraw event. +#[derive(Debug, Serialize, Deserialize)] +pub struct WithdrawFAEvent { + pub store: AccountAddress, + pub amount: u64, +} + +impl MoveEventV2Type for WithdrawFAEvent {} + +impl MoveStructType for WithdrawFAEvent { + const MODULE_NAME: &'static IdentStr = ident_str!("fungible_asset"); + const STRUCT_NAME: &'static IdentStr = ident_str!("Withdraw"); +} + +/// Struct that represents a Deposit event. +#[derive(Debug, Serialize, Deserialize)] +pub struct DepositFAEvent { + pub store: AccountAddress, + pub amount: u64, +} + +impl MoveEventV2Type for DepositFAEvent {} + +impl MoveStructType for DepositFAEvent { + const MODULE_NAME: &'static IdentStr = ident_str!("fungible_asset"); + const STRUCT_NAME: &'static IdentStr = ident_str!("Deposit"); +} diff --git a/types/src/account_config/events/mod.rs b/types/src/account_config/events/mod.rs index 6a224aaae5904f..f2207ec1bbad2f 100644 --- a/types/src/account_config/events/mod.rs +++ b/types/src/account_config/events/mod.rs @@ -2,12 +2,12 @@ // Parts of the project are originally copyright © Meta Platforms, Inc. // SPDX-License-Identifier: Apache-2.0 -pub mod deposit; +pub mod coin; +pub mod fungible_asset; pub mod new_block; pub mod new_epoch; -pub mod withdraw; -pub use deposit::*; +pub use coin::*; +pub use fungible_asset::*; pub use new_block::*; pub use new_epoch::*; -pub use withdraw::*; diff --git a/types/src/account_config/events/withdraw.rs b/types/src/account_config/events/withdraw.rs deleted file mode 100644 index da27bb19e7c028..00000000000000 --- a/types/src/account_config/events/withdraw.rs +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright © Aptos Foundation -// SPDX-License-Identifier: Apache-2.0 - -use anyhow::Result; -use move_core_types::{ - account_address::AccountAddress, ident_str, identifier::IdentStr, move_resource::MoveStructType, -}; -use serde::{Deserialize, Serialize}; - -/// Struct that represents a SentPaymentEvent. -#[derive(Debug, Serialize, Deserialize)] -pub struct WithdrawEvent { - amount: u64, -} - -impl WithdrawEvent { - pub fn try_from_bytes(bytes: &[u8]) -> Result { - bcs::from_bytes(bytes).map_err(Into::into) - } - - /// Get the amount sent or received - pub fn amount(&self) -> u64 { - self.amount - } -} - -impl MoveStructType for WithdrawEvent { - const MODULE_NAME: &'static IdentStr = ident_str!("coin"); - const STRUCT_NAME: &'static IdentStr = ident_str!("WithdrawEvent"); -} - -#[derive(Debug, Serialize, Deserialize)] -pub struct CoinWithdraw { - pub coin_type: String, - pub account: AccountAddress, - pub amount: u64, -} - -impl CoinWithdraw { - pub fn try_from_bytes(bytes: &[u8]) -> Result { - bcs::from_bytes(bytes).map_err(Into::into) - } -} - -impl MoveStructType for CoinWithdraw { - const MODULE_NAME: &'static IdentStr = ident_str!("coin"); - const STRUCT_NAME: &'static IdentStr = ident_str!("CoinWithdraw"); -} diff --git a/types/src/account_config/resources/coin_info.rs b/types/src/account_config/resources/coin_info.rs index 4ab8f09f48694b..394ef0eaf3996f 100644 --- a/types/src/account_config/resources/coin_info.rs +++ b/types/src/account_config/resources/coin_info.rs @@ -76,18 +76,20 @@ impl CoinInfoResource { } } - /// Returns a writeset corresponding to the creation of CoinInfo in Move. - /// This can be passed to data store for testing total supply. - pub fn to_writeset(&self, supply: u128) -> anyhow::Result { - let value_state_key = self - .supply + pub fn supply_aggregator_state_key(&self) -> StateKey { + self.supply .as_ref() .unwrap() .aggregator .as_ref() .unwrap() - .state_key(); + .state_key() + } + /// Returns a writeset corresponding to the creation of CoinInfo in Move. + /// This can be passed to data store for testing total supply. + pub fn to_writeset(&self, supply: u128) -> anyhow::Result { + let value_state_key = self.supply_aggregator_state_key(); // We store CoinInfo and aggregatable value separately. let write_set = vec![ ( diff --git a/types/src/account_config/resources/coin_store.rs b/types/src/account_config/resources/coin_store.rs index 33938dc082208d..472d9234c7112b 100644 --- a/types/src/account_config/resources/coin_store.rs +++ b/types/src/account_config/resources/coin_store.rs @@ -81,9 +81,17 @@ impl CoinStoreResource { &self.deposit_events } + pub fn deposit_events_mut(&mut self) -> &mut EventHandle { + &mut self.deposit_events + } + pub fn withdraw_events(&self) -> &EventHandle { &self.withdraw_events } + + pub fn withdraw_events_mut(&mut self) -> &mut EventHandle { + &mut self.withdraw_events + } } impl MoveStructType for CoinStoreResource { diff --git a/types/src/account_config/resources/fungible_asset_metadata.rs b/types/src/account_config/resources/fungible_asset_metadata.rs index e28a929a8ec525..8ec9c90ff2557d 100644 --- a/types/src/account_config/resources/fungible_asset_metadata.rs +++ b/types/src/account_config/resources/fungible_asset_metadata.rs @@ -10,13 +10,13 @@ use move_core_types::{ use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize)] -pub struct ConcurrentSupply { +pub struct ConcurrentSupplyResource { pub current: AggregatorResource, } -impl MoveStructType for ConcurrentSupply { +impl MoveStructType for ConcurrentSupplyResource { const MODULE_NAME: &'static IdentStr = ident_str!("fungible_asset"); const STRUCT_NAME: &'static IdentStr = ident_str!("ConcurrentSupply"); } -impl MoveResource for ConcurrentSupply {} +impl MoveResource for ConcurrentSupplyResource {} diff --git a/types/src/account_config/resources/fungible_store.rs b/types/src/account_config/resources/fungible_store.rs index c0555e8fce4848..e8066ca712c00f 100644 --- a/types/src/account_config/resources/fungible_store.rs +++ b/types/src/account_config/resources/fungible_store.rs @@ -2,9 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 use super::aggregator::AggregatorResource; -use crate::{ - account_address::create_derived_object_address, move_utils::move_event_v2::MoveEventV2Type, -}; +use crate::account_address::create_derived_object_address; use move_core_types::{ account_address::AccountAddress, ident_str, @@ -96,31 +94,3 @@ impl MoveStructType for MigrationFlag { } impl MoveResource for MigrationFlag {} - -/// Struct that represents a Withdraw event. -#[derive(Debug, Serialize, Deserialize)] -pub struct WithdrawFAEvent { - pub store: AccountAddress, - pub amount: u64, -} - -impl MoveEventV2Type for WithdrawFAEvent {} - -impl MoveStructType for WithdrawFAEvent { - const MODULE_NAME: &'static IdentStr = ident_str!("fungble_asset"); - const STRUCT_NAME: &'static IdentStr = ident_str!("Withdraw"); -} - -/// Struct that represents a Deposit event. -#[derive(Debug, Serialize, Deserialize)] -pub struct DepositFAEvent { - pub store: AccountAddress, - pub amount: u64, -} - -impl MoveEventV2Type for DepositFAEvent {} - -impl MoveStructType for DepositFAEvent { - const MODULE_NAME: &'static IdentStr = ident_str!("fungble_asset"); - const STRUCT_NAME: &'static IdentStr = ident_str!("Deposit"); -} diff --git a/types/src/account_config/resources/mod.rs b/types/src/account_config/resources/mod.rs index 82bee857de31a8..cbe90b75996390 100644 --- a/types/src/account_config/resources/mod.rs +++ b/types/src/account_config/resources/mod.rs @@ -11,6 +11,7 @@ pub mod core_account; pub mod fungible_asset_metadata; pub mod fungible_store; pub mod object; +pub mod type_info; pub use chain_id::*; pub use challenge::*; @@ -20,3 +21,4 @@ pub use core_account::*; pub use fungible_asset_metadata::*; pub use fungible_store::*; pub use object::*; +pub use type_info::*; diff --git a/types/src/account_config/resources/type_info.rs b/types/src/account_config/resources/type_info.rs new file mode 100644 index 00000000000000..a026f0f4227743 --- /dev/null +++ b/types/src/account_config/resources/type_info.rs @@ -0,0 +1,52 @@ +// Copyright © Aptos Foundation +// SPDX-License-Identifier: Apache-2.0 + +use itertools::Itertools; +use move_core_types::{ + account_address::AccountAddress, + ident_str, + identifier::IdentStr, + move_resource::{MoveResource, MoveStructType}, +}; +use serde::{Deserialize, Serialize}; + +/// A Rust representation of TypeInfo. +#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)] +pub struct TypeInfoResource { + pub account_address: AccountAddress, + pub module_name: Vec, + pub struct_name: Vec, +} + +impl TypeInfoResource { + pub fn new() -> anyhow::Result { + let struct_tag = T::struct_tag(); + Ok(Self { + account_address: struct_tag.address, + module_name: bcs::to_bytes(&struct_tag.module.to_string())?, + struct_name: bcs::to_bytes( + &if struct_tag.type_args.is_empty() { + struct_tag.name.to_string() + } else { + format!( + "{}<{}>", + struct_tag.name, + struct_tag + .type_args + .iter() + .map(|v| v.to_string()) + .join(", ") + ) + .to_string() + }, + )?, + }) + } +} + +impl MoveStructType for TypeInfoResource { + const MODULE_NAME: &'static IdentStr = ident_str!("type_info"); + const STRUCT_NAME: &'static IdentStr = ident_str!("TypeInfo"); +} + +impl MoveResource for TypeInfoResource {} diff --git a/types/src/event.rs b/types/src/event.rs index ae7956cbf4cd7b..a3a1bd137827d6 100644 --- a/types/src/event.rs +++ b/types/src/event.rs @@ -117,7 +117,6 @@ impl EventHandle { } } - #[cfg(any(test, feature = "fuzzing"))] pub fn count_mut(&mut self) -> &mut u64 { &mut self.count } diff --git a/types/src/move_utils/mod.rs b/types/src/move_utils/mod.rs index 5160513ecc8e70..762ea7d76535bb 100644 --- a/types/src/move_utils/mod.rs +++ b/types/src/move_utils/mod.rs @@ -40,4 +40,5 @@ impl FromStr for MemberId { } pub mod as_move_value; +pub mod move_event_v1; pub mod move_event_v2; diff --git a/types/src/move_utils/move_event_v1.rs b/types/src/move_utils/move_event_v1.rs new file mode 100644 index 00000000000000..e438f805d997b6 --- /dev/null +++ b/types/src/move_utils/move_event_v1.rs @@ -0,0 +1,19 @@ +// Copyright © Aptos Foundation +// SPDX-License-Identifier: Apache-2.0 + +use crate::{contract_event::ContractEvent, event::EventHandle}; +use move_core_types::{language_storage::TypeTag, move_resource::MoveStructType}; +use serde::Serialize; + +pub trait MoveEventV1Type: MoveStructType + Serialize { + fn create_event_v1(&self, handle: &mut EventHandle) -> ContractEvent { + let sequence_number = handle.count(); + *handle.count_mut() = sequence_number + 1; + ContractEvent::new_v1( + *handle.key(), + sequence_number, + TypeTag::Struct(Box::new(Self::struct_tag())), + bcs::to_bytes(self).unwrap(), + ) + } +} diff --git a/types/src/utility_coin.rs b/types/src/utility_coin.rs index 0074ac9fb74784..625f63e823109b 100644 --- a/types/src/utility_coin.rs +++ b/types/src/utility_coin.rs @@ -4,9 +4,12 @@ use crate::account_address::AccountAddress; use move_core_types::{ ident_str, + identifier::IdentStr, language_storage::{StructTag, TypeTag}, + move_resource::MoveStructType, }; use once_cell::sync::Lazy; +use serde::{Deserialize, Serialize}; pub trait CoinType { fn type_tag() -> TypeTag; @@ -23,6 +26,7 @@ static APTOS_COIN_TYPE: Lazy = Lazy::new(|| { })) }); +#[derive(Debug, Serialize, Deserialize)] pub struct AptosCoinType; impl CoinType for AptosCoinType { @@ -34,3 +38,8 @@ impl CoinType for AptosCoinType { AccountAddress::ONE } } + +impl MoveStructType for AptosCoinType { + const MODULE_NAME: &'static IdentStr = ident_str!("aptos_coin"); + const STRUCT_NAME: &'static IdentStr = ident_str!("AptosCoin"); +}