Skip to content

Commit

Permalink
[move-cm][closures] Refactor: Move type conversions out of Loader i…
Browse files Browse the repository at this point in the history
…nto a trait

Type conversions from runtime types to `MoveTypeLayout` and `TypeTag` currently are associated with the `Loader` type. However, they are needed for the `FunctionValueExtension` trait which needs to be constructed in contexts where no loader but only `ModuleStorage` exists.

This PR moves the conversion functions into a new trait `TypeConverter`. The trait is then implemented two times based on `ModuleStorage` only and based on the existing `Loader`, for downwards compatibility.
  • Loading branch information
wrwg committed Jan 17, 2025
1 parent 6aa1fbf commit 23f4130
Show file tree
Hide file tree
Showing 10 changed files with 638 additions and 523 deletions.
30 changes: 28 additions & 2 deletions third_party/move/move-core/types/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

use crate::{
account_address::AccountAddress,
ident_str,
identifier::Identifier,
language_storage::{StructTag, TypeTag},
language_storage::{ModuleId, StructTag, TypeTag},
u256,
};
use anyhow::{anyhow, bail, Result as AResult};
Expand Down Expand Up @@ -197,6 +198,31 @@ pub enum IdentifierMappingKind {
DerivedString,
}

impl IdentifierMappingKind {
/// If the struct identifier has a special mapping, return it.
pub fn from_ident(
module_id: &ModuleId,
struct_id: &Identifier,
) -> Option<IdentifierMappingKind> {
if module_id.address().eq(&AccountAddress::ONE)
&& module_id.name().eq(ident_str!("aggregator_v2"))
{
let ident_str = struct_id.as_ident_str();
if ident_str.eq(ident_str!("Aggregator")) {
Some(IdentifierMappingKind::Aggregator)
} else if ident_str.eq(ident_str!("AggregatorSnapshot")) {
Some(IdentifierMappingKind::Snapshot)
} else if ident_str.eq(ident_str!("DerivedStringSnapshot")) {
Some(IdentifierMappingKind::DerivedString)
} else {
None
}
} else {
None
}
}
}

#[derive(Debug, Clone, Hash, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(
any(test, feature = "fuzzing"),
Expand Down Expand Up @@ -850,7 +876,7 @@ impl fmt::Display for MoveTypeLayout {
U256 => write!(f, "u256"),
Address => write!(f, "address"),
Vector(typ) => write!(f, "vector<{}>", typ),
Struct(s) => write!(f, "{}", s),
Struct(s) => fmt::Display::fmt(s, f),
Signer => write!(f, "signer"),
// TODO[agg_v2](cleanup): consider printing the tag as well.
Native(_, typ) => write!(f, "native<{}>", typ),
Expand Down
11 changes: 7 additions & 4 deletions third_party/move/move-vm/runtime/src/data_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use crate::{
loader::{LegacyModuleStorageAdapter, Loader},
logging::expect_no_verification_errors,
storage::module_storage::FunctionValueExtensionAdapter,
ModuleStorage,
storage::{
module_storage::FunctionValueExtensionAdapter, ty_layout_converter::LoaderLayoutConverter,
},
LayoutConverter, ModuleStorage,
};
use bytes::Bytes;
use move_binary_format::{
Expand Down Expand Up @@ -229,8 +231,9 @@ impl<'r> TransactionDataCache<'r> {
},
};
// TODO(Gas): Shall we charge for this?
let (ty_layout, has_aggregator_lifting) = loader
.type_to_type_layout_with_identifier_mappings(ty, module_store, module_storage)?;
let (ty_layout, has_aggregator_lifting) =
LoaderLayoutConverter::new(loader, module_store, module_storage)
.type_to_type_layout_with_identifier_mappings(ty)?;

let (data, bytes_loaded) = match loader {
Loader::V1(_) => {
Expand Down
1 change: 1 addition & 0 deletions third_party/move/move-vm/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ pub use storage::{
},
module_storage::{ambassador_impl_ModuleStorage, AsFunctionValueExtension, ModuleStorage},
publishing::{StagingModuleStorage, VerifiedModuleBundle},
ty_layout_converter::{LayoutConverter, StorageLayoutConverter},
};
Loading

0 comments on commit 23f4130

Please sign in to comment.