diff --git a/Makefile b/Makefile index adc8200c..a43891bb 100644 --- a/Makefile +++ b/Makefile @@ -53,7 +53,7 @@ test-safety: # Use package list mode to include all subdirectores. The -count=1 turns off caching. GODEBUG=cgocheck=2 go test -race -v -count=1 -parallel=1 ./... -test-rust: test-compiler test-lib test-e2e test-movevm test-json +test-rust: test-compiler test-lib test-e2e test-movevm test-json test-storage test-compiler: cargo test -p initia-move-compiler @@ -67,6 +67,9 @@ test-json: test-lib: cargo test -p initia-move-vm +test-storage: + cargo test -p initia-move-storage + test-e2e: cargo test -p e2e-move-tests --features testing diff --git a/api/libmovevm.dylib b/api/libmovevm.dylib index 54a34f90..9d35732f 100755 Binary files a/api/libmovevm.dylib and b/api/libmovevm.dylib differ diff --git a/crates/storage/src/code_scale.rs b/crates/storage/src/code_scale.rs index a3c9bcdc..78bf2864 100644 --- a/crates/storage/src/code_scale.rs +++ b/crates/storage/src/code_scale.rs @@ -21,12 +21,24 @@ use crate::state_view::Checksum; pub struct CodeScale; +unsafe fn convert_to_my_code( + code: &Code, +) -> &MyCode { + &*(code as *const Code + as *const MyCode) +} + +unsafe fn convert_to_my_module_code( + code: &ModuleCode, +) -> &MyModuleCode { + &*(code as *const ModuleCode + as *const MyModuleCode) +} + impl WeightScale> for CodeScale { fn weight(&self, _key: &Checksum, value: &Code) -> usize { unsafe { - let value = &*(value as *const Code - as *const MyCode); - value.get_size() + convert_to_my_code(value).get_size() } } } @@ -42,10 +54,200 @@ impl WeightScale>, ) -> usize { unsafe { - let value = &*(value.as_ref() - as *const ModuleCode - as *const MyModuleCode); - value.get_size() + convert_to_my_module_code(value).get_size() } } } + +#[cfg(test)] +mod test { + use std::str::FromStr; + use std::sync::Arc; + + use move_binary_format::file_format::{basic_test_module, empty_script_with_dependencies}; + use move_core_types::account_address::AccountAddress; + use move_core_types::identifier::Identifier; + use move_core_types::language_storage::ModuleId; + use move_vm_runtime::{compute_code_hash, CodeStorage, ModuleStorage, RuntimeEnvironment}; + use move_vm_types::code::{Code, ModuleCode, WithBytes, WithHash}; + + use crate::code_storage::test::make_script; + use crate::code_storage::AsInitiaCodeStorage; + use crate::memory_module_storage::InMemoryStorage; + use crate::module_cache::{BytesWithHash, InitiaModuleCache, NoVersion}; + use crate::module_storage::test::{add_module_bytes, TEST_CACHE_CAPACITY}; + use crate::module_storage::AsInitiaModuleStorage; + use crate::move_core_type::code::Code as MyCode; + use crate::move_core_type::file_format::{AbilitySet, AddressIdentifierIndex, Bytecode, CodeUnit, FieldDefinition, FunctionDefinition, FunctionHandle, FunctionHandleIndex, IdentifierIndex, ModuleHandle, ModuleHandleIndex, SignatureIndex, SignatureToken, StructDefinition, StructFieldInformation, StructHandle, StructHandleIndex, TableIndex, TypeSignature, Visibility}; + use crate::move_core_type::move_core_type::{AccountAddress as MyAccountAddress, Identifier as MyIdentifier}; + use crate::script_cache::InitiaScriptCache; + + #[test] + fn test_compiled_module_convert_to_my_module_code() { + let version = NoVersion{}; + let extension = Arc::new(BytesWithHash::new(vec![1, 2, 3].into(), [1u8; 32])); + let module_code = ModuleCode::from_deserialized(basic_test_module(), extension, version); + let my_module_code = unsafe { super::convert_to_my_module_code(&module_code) }; + + match &my_module_code.code { + MyCode::Deserialized(compiled_module) => { + assert_eq!(compiled_module.function_handles.len(), 1); + assert_eq!(*compiled_module.function_handles.get(0).unwrap(), FunctionHandle { + module: ModuleHandleIndex(0), + name: IdentifierIndex(1), + parameters: SignatureIndex(0), + return_: SignatureIndex(0), + type_parameters: vec![], + access_specifiers: None, + }); + + assert_eq!(compiled_module.identifiers.len(), 4); + assert_eq!(*compiled_module.identifiers.get(1).unwrap(), MyIdentifier::from_str("foo").unwrap()); + assert_eq!(*compiled_module.identifiers.get(2).unwrap(), MyIdentifier::from_str("Bar").unwrap()); + assert_eq!(*compiled_module.identifiers.get(3).unwrap(), MyIdentifier::from_str("x").unwrap()); + + assert_eq!(compiled_module.function_defs.len(), 1); + assert_eq!(*compiled_module.function_defs.get(0).unwrap(), FunctionDefinition { + function: FunctionHandleIndex(0), + visibility: Visibility::Private, + is_entry: false, + acquires_global_resources: vec![], + code: Some(CodeUnit { + locals: SignatureIndex(0), + code: vec![Bytecode::Ret], + }), + }); + + assert_eq!(compiled_module.struct_handles.len(), 1); + assert_eq!(*compiled_module.struct_handles.get(0).unwrap(), StructHandle { + module: ModuleHandleIndex(0), + name: IdentifierIndex(2), + abilities: AbilitySet(0), + type_parameters: vec![], + }); + + assert_eq!(compiled_module.struct_defs.len(), 1); + assert_eq!(*compiled_module.struct_defs.get(0).unwrap(), StructDefinition { + struct_handle: StructHandleIndex(0), + field_information: StructFieldInformation::Declared(vec![FieldDefinition { + name: IdentifierIndex(3), + signature: TypeSignature(SignatureToken::U64), + }]), + }); + } + MyCode::Verified(_) => { + + } + } + } + + #[test] + fn test_convert_to_my_code() { + let code = Code::from_deserialized(empty_script_with_dependencies(vec!["a", "b", "c"])); + let my_code = unsafe { super::convert_to_my_code(&code) }; + + match &my_code { + MyCode::Deserialized(compiled_script) => { + assert_eq!(compiled_script.code, CodeUnit{ + locals: SignatureIndex(0), + code: vec![Bytecode::Ret], + }); + + assert_eq!(compiled_script.address_identifiers.len(), 1); + assert_eq!(*compiled_script.address_identifiers.get(0).unwrap(), MyAccountAddress([0u8; 32])); + + assert_eq!(compiled_script.identifiers.len(), 3); + assert_eq!(*compiled_script.identifiers.get(0).unwrap(), MyIdentifier::from_str("a").unwrap()); + assert_eq!(*compiled_script.identifiers.get(1).unwrap(), MyIdentifier::from_str("b").unwrap()); + assert_eq!(*compiled_script.identifiers.get(2).unwrap(), MyIdentifier::from_str("c").unwrap()); + + assert_eq!(compiled_script.module_handles.len(), 3); + assert_eq!(*compiled_script.module_handles.get(0).unwrap(), ModuleHandle { + address: AddressIdentifierIndex(0), + name: IdentifierIndex(0 as TableIndex), + }); + assert_eq!(*compiled_script.module_handles.get(1).unwrap(), ModuleHandle { + address: AddressIdentifierIndex(0), + name: IdentifierIndex(1 as TableIndex), + }); + assert_eq!(*compiled_script.module_handles.get(2).unwrap(), ModuleHandle { + address: AddressIdentifierIndex(0), + name: IdentifierIndex(2 as TableIndex), + }); + } + MyCode::Verified(_) => { + panic!("Expected deserialized code") + } + } + } + + #[test] + fn test_module_convert_to_my_module_code() { + let mut module_bytes_storage = InMemoryStorage::new(); + let module_cache = InitiaModuleCache::new(TEST_CACHE_CAPACITY); + + let a_id = ModuleId::new(AccountAddress::ZERO, Identifier::new("a").unwrap()); + ModuleId::new(AccountAddress::ZERO, Identifier::new("b").unwrap()); + ModuleId::new(AccountAddress::ZERO, Identifier::new("c").unwrap()); + + add_module_bytes(&mut module_bytes_storage, "a", vec!["b"], vec!["d"]); + add_module_bytes(&mut module_bytes_storage, "b", vec!["c"], vec![]); + add_module_bytes(&mut module_bytes_storage, "c", vec![], vec![]); + add_module_bytes(&mut module_bytes_storage, "d", vec![], vec!["c"]); + + let runtime_environment = RuntimeEnvironment::new(vec![]); + let module_storage = + module_bytes_storage.into_initia_module_storage(&runtime_environment, module_cache); + + let module = module_storage.fetch_verified_module(a_id.address(), a_id.name()).unwrap().unwrap(); + let module_code = ModuleCode::from_verified(module.as_ref().clone(), Arc::new(BytesWithHash::new(vec![1, 2, 3].into(), [1u8; 32])), NoVersion{}); + let my_module_code = unsafe { super::convert_to_my_module_code(&module_code) }; + + assert_eq!(my_module_code.extension.bytes().to_vec(), vec![1, 2, 3]); + assert_eq!(*my_module_code.extension.hash(), [1u8; 32]); + assert_eq!(my_module_code.version, NoVersion{}); + + let converted_module = match &my_module_code.code { + MyCode::Deserialized(_) => panic!("Expected verified code"), + MyCode::Verified(code) => code + }; + + assert_eq!(format!("{:?}", module.as_ref()), format!("{:?}", converted_module.as_ref())); + } + + #[test] + fn test_script_convert_to_my_code() { + let mut module_bytes_storage = InMemoryStorage::new(); + let module_cache = InitiaModuleCache::new(TEST_CACHE_CAPACITY); + let script_cache = InitiaScriptCache::new(TEST_CACHE_CAPACITY); + + ModuleId::new(AccountAddress::ZERO, Identifier::new("a").unwrap()); + ModuleId::new(AccountAddress::ZERO, Identifier::new("b").unwrap()); + ModuleId::new(AccountAddress::ZERO, Identifier::new("c").unwrap()); + + add_module_bytes(&mut module_bytes_storage, "a", vec!["b", "c"], vec![]); + add_module_bytes(&mut module_bytes_storage, "b", vec![], vec![]); + add_module_bytes(&mut module_bytes_storage, "c", vec![], vec![]); + + let runtime_environment = RuntimeEnvironment::new(vec![]); + let code_storage = module_bytes_storage.into_initia_code_storage( + &runtime_environment, + script_cache, + module_cache, + ); + + let serialized_script = make_script(vec!["a"]); + compute_code_hash(&serialized_script); + code_storage.deserialize_and_cache_script(&serialized_script).unwrap(); + let script = code_storage.verify_and_cache_script(&serialized_script).unwrap(); + + let script_code = Code::from_verified(script.as_ref().clone()); + let my_script_code = unsafe { super::convert_to_my_code(&script_code) }; + + let converted_script = match my_script_code { + MyCode::Deserialized(_) => panic!("Expected verified code"), + MyCode::Verified(code) => code + }; + assert_eq!(format!("{:?}", script.as_ref()), format!("{:?}", converted_script.as_ref())); + } +} \ No newline at end of file diff --git a/crates/storage/src/code_storage.rs b/crates/storage/src/code_storage.rs index 2fb50547..accd43cd 100644 --- a/crates/storage/src/code_storage.rs +++ b/crates/storage/src/code_storage.rs @@ -172,7 +172,7 @@ impl InitiaCodeStorage { } #[cfg(test)] -mod test { +pub(crate) mod test { use claims::assert_ok; use move_binary_format::{ file_format::empty_script_with_dependencies, file_format_common::VERSION_DEFAULT, @@ -190,7 +190,7 @@ mod test { script_cache::InitiaScriptCache, }; - fn make_script<'a>(dependencies: impl IntoIterator) -> Vec { + pub fn make_script<'a>(dependencies: impl IntoIterator) -> Vec { let mut script = empty_script_with_dependencies(dependencies); script.version = VERSION_DEFAULT; diff --git a/crates/storage/src/module_cache.rs b/crates/storage/src/module_cache.rs index 534e6808..f75f7b21 100644 --- a/crates/storage/src/module_cache.rs +++ b/crates/storage/src/module_cache.rs @@ -20,7 +20,7 @@ fn bytes_len(bytes: &Bytes) -> usize { /// Extension for modules stored in [UnsyncModuleStorage] to also capture information about bytes /// and hash. -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct BytesWithHash { /// Bytes of the module. #[get_size(size_fn = bytes_len)] @@ -49,7 +49,7 @@ impl WithHash for BytesWithHash { } /// Placeholder for module versioning since we do not allow to mutate [UnsyncModuleStorage]. -#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, GetSize)] +#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, GetSize, Debug)] pub struct NoVersion; pub struct InitiaModuleCache { diff --git a/crates/storage/src/move_core_type/code.rs b/crates/storage/src/move_core_type/code.rs index d10be1a1..6583a9f9 100644 --- a/crates/storage/src/move_core_type/code.rs +++ b/crates/storage/src/move_core_type/code.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use get_size::GetSize; #[allow(dead_code)] -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub enum Code { /// Deserialized code, not yet verified with bytecode verifier. Deserialized(Arc), @@ -11,12 +11,12 @@ pub enum Code { Verified(Arc), } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct ModuleCode { /// Module's code, either deserialized or verified. pub code: Code, /// Module's extension - any additional metadata associated with this module. pub extension: Arc, /// Version of the code (e.g., which transaction within the block published this module). - version: V, + pub version: V, } diff --git a/crates/storage/src/move_core_type/file_format.rs b/crates/storage/src/move_core_type/file_format.rs index c682b2a9..e23a1d51 100644 --- a/crates/storage/src/move_core_type/file_format.rs +++ b/crates/storage/src/move_core_type/file_format.rs @@ -3,7 +3,7 @@ use get_size::GetSize; use primitive_types::U256 as PrimitiveU256; #[allow(dead_code)] -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub enum Bytecode { Pop, Ret, @@ -94,7 +94,7 @@ pub enum Bytecode { } #[allow(dead_code)] -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] pub struct U256(PrimitiveU256); impl GetSize for U256 { @@ -112,7 +112,7 @@ macro_rules! define_index { kind: $kind: ident, doc: $comment: literal, } => { - #[derive(GetSize)] + #[derive(GetSize, PartialEq, Eq, Debug)] pub struct $name(pub TableIndex); }; } @@ -232,7 +232,7 @@ pub type TypeSignaturePool = Vec; /// locals used and their types. pub type SignaturePool = Vec; -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct ModuleHandle { /// Index into the `AddressIdentifierIndex`. Identifies module-holding account's address. pub address: AddressIdentifierIndex, @@ -240,7 +240,7 @@ pub struct ModuleHandle { pub name: IdentifierIndex, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct StructHandle { /// The module that defines the type. pub module: ModuleHandleIndex, @@ -254,7 +254,7 @@ pub struct StructHandle { pub type_parameters: Vec, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct StructTypeParameter { /// The type parameter constraints. pub constraints: AbilitySet, @@ -262,7 +262,7 @@ pub struct StructTypeParameter { pub is_phantom: bool, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct FunctionHandle { /// The module that defines the function. pub module: ModuleHandleIndex, @@ -281,13 +281,13 @@ pub struct FunctionHandle { pub access_specifiers: Option>, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct FieldHandle { pub owner: StructDefinitionIndex, pub field: MemberCount, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct VariantFieldHandle { /// The structure which defines the variant. pub struct_index: StructDefinitionIndex, @@ -298,51 +298,51 @@ pub struct VariantFieldHandle { pub field: MemberCount, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct StructVariantHandle { pub struct_index: StructDefinitionIndex, pub variant: VariantIndex, } #[allow(dead_code)] -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub enum StructFieldInformation { Native, Declared(Vec), DeclaredVariants(Vec), } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct StructDefInstantiation { pub def: StructDefinitionIndex, pub type_parameters: SignatureIndex, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct StructVariantInstantiation { pub handle: StructVariantHandleIndex, pub type_parameters: SignatureIndex, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct FunctionInstantiation { pub handle: FunctionHandleIndex, pub type_parameters: SignatureIndex, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct FieldInstantiation { pub handle: FieldHandleIndex, pub type_parameters: SignatureIndex, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct VariantFieldInstantiation { pub handle: VariantFieldHandleIndex, pub type_parameters: SignatureIndex, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct StructDefinition { /// The `StructHandle` for this `StructDefinition`. This has the name and the abilities /// for the type. @@ -353,7 +353,7 @@ pub struct StructDefinition { pub field_information: StructFieldInformation, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct FieldDefinition { /// The name of the field. pub name: IdentifierIndex, @@ -361,14 +361,14 @@ pub struct FieldDefinition { pub signature: TypeSignature, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct VariantDefinition { pub name: IdentifierIndex, pub fields: Vec, } #[allow(dead_code)] -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub enum Visibility { /// Accessible within its defining module only. Private = 0x0, @@ -381,7 +381,7 @@ pub enum Visibility { Friend = 0x3, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct FunctionDefinition { /// The prototype of the function (module, name, signature). pub function: FunctionHandleIndex, @@ -404,10 +404,10 @@ pub struct FunctionDefinition { pub code: Option, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct TypeSignature(pub SignatureToken); -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct FunctionSignature { /// The list of return types. pub return_: Vec, @@ -417,7 +417,7 @@ pub struct FunctionSignature { pub type_parameters: Vec, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct Signature(pub Vec); /// Type parameters are encoded as indices. This index can also be used to lookup the kind of a @@ -425,7 +425,7 @@ pub struct Signature(pub Vec); pub type TypeParameterIndex = u16; #[allow(dead_code)] -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub enum Ability { /// Allows values of types with this ability to be copied, via CopyLoc or ReadRef Copy = 0x1, @@ -440,16 +440,16 @@ pub enum Ability { Key = 0x8, } -#[derive(GetSize)] -pub struct AbilitySet(u8); +#[derive(GetSize, PartialEq, Eq, Debug)] +pub struct AbilitySet(pub u8); -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct AbilitySetIterator { set: AbilitySet, idx: u8, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct AccessSpecifier { /// The kind of access: read, write, or both. pub kind: AccessKind, @@ -462,7 +462,7 @@ pub struct AccessSpecifier { } #[allow(dead_code)] -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub enum AccessKind { Reads, Writes, @@ -470,7 +470,7 @@ pub enum AccessKind { } #[allow(dead_code)] -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub enum ResourceSpecifier { /// Any resource Any, @@ -485,7 +485,7 @@ pub enum ResourceSpecifier { } #[allow(dead_code)] -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub enum AddressSpecifier { /// Resource can be stored at any address. Any, @@ -504,7 +504,7 @@ pub enum AddressSpecifier { } #[allow(dead_code)] -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub enum SignatureToken { /// Boolean, `true` or `false`. Bool, @@ -538,23 +538,23 @@ pub enum SignatureToken { U256, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct SignatureTokenPreorderTraversalIter<'a> { stack: Vec<&'a SignatureToken>, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct SignatureTokenPreorderTraversalIterWithDepth<'a> { stack: Vec<(&'a SignatureToken, usize)>, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct Constant { pub type_: SignatureToken, pub data: Vec, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct CodeUnit { /// List of locals type. All locals are typed. pub locals: SignatureIndex, @@ -562,7 +562,7 @@ pub struct CodeUnit { pub code: Vec, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct CompiledScript { /// Version number found during deserialization pub version: u32, @@ -593,7 +593,7 @@ pub struct CompiledScript { pub parameters: SignatureIndex, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct CompiledModule { /// Version number found during deserialization pub version: u32, diff --git a/crates/storage/src/move_core_type/function.rs b/crates/storage/src/move_core_type/function.rs index 1b79548b..8356a6a7 100644 --- a/crates/storage/src/move_core_type/function.rs +++ b/crates/storage/src/move_core_type/function.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::{fmt::Debug, sync::Arc}; use super::{ file_format::{AbilitySet, AccessSpecifier, Bytecode, FunctionDefinitionIndex}, @@ -29,6 +29,14 @@ pub struct Function { pub(crate) access_specifier: AccessSpecifier, } +impl Debug for Function { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { + f.debug_struct("Function") + .field("name", &self.name) + .finish() + } +} + #[allow(dead_code)] #[derive(GetSize)] /// For loaded function representation, specifies the owner: a script or a module. @@ -48,7 +56,7 @@ pub struct LoadedFunction { pub(crate) function: Arc, } -#[derive(GetSize)] +#[derive(GetSize, Debug)] pub(crate) struct FunctionInstantiation { // index to `ModuleCache::functions` global table pub(crate) handle: FunctionHandle, @@ -56,7 +64,7 @@ pub(crate) struct FunctionInstantiation { } #[allow(dead_code)] -#[derive(GetSize)] +#[derive(GetSize, Debug)] pub(crate) enum FunctionHandle { Local(Arc), Remote { module: ModuleId, name: Identifier }, diff --git a/crates/storage/src/move_core_type/modules.rs b/crates/storage/src/move_core_type/modules.rs index ea491388..e4745c41 100644 --- a/crates/storage/src/move_core_type/modules.rs +++ b/crates/storage/src/move_core_type/modules.rs @@ -16,7 +16,7 @@ use super::{ runtime_types::{StructType, Type}, }; -#[derive(GetSize)] +#[derive(GetSize, Debug)] pub struct Module { id: ModuleId, @@ -68,20 +68,20 @@ pub struct Module { pub(crate) single_signature_token_map: BTreeMap, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub(crate) struct StructDef { pub(crate) field_count: u16, pub(crate) definition_struct_type: Arc, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub(crate) struct StructInstantiation { pub(crate) field_count: u16, pub(crate) definition_struct_type: Arc, pub(crate) instantiation: Vec, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub(crate) struct StructVariantInfo { pub(crate) field_count: u16, pub(crate) variant: VariantIndex, @@ -89,14 +89,14 @@ pub(crate) struct StructVariantInfo { pub(crate) instantiation: Vec, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub(crate) struct FieldHandle { pub(crate) offset: usize, pub(crate) field_ty: Type, pub(crate) definition_struct_type: Arc, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub(crate) struct FieldInstantiation { pub(crate) offset: usize, pub(crate) uninstantiated_field_ty: Type, @@ -104,7 +104,7 @@ pub(crate) struct FieldInstantiation { pub(crate) instantiation: Vec, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub(crate) struct VariantFieldInfo { pub(crate) offset: usize, pub(crate) uninstantiated_field_ty: Type, diff --git a/crates/storage/src/move_core_type/move_core_type.rs b/crates/storage/src/move_core_type/move_core_type.rs index e1f09c0f..99d45e11 100644 --- a/crates/storage/src/move_core_type/move_core_type.rs +++ b/crates/storage/src/move_core_type/move_core_type.rs @@ -1,6 +1,12 @@ use get_size::GetSize; -use std::fmt::Debug; +use std::fmt::{self, Debug}; +#[cfg(test)] +use std::str::FromStr; +#[cfg(test)] +use anyhow::Result; + +#[derive(PartialEq, Eq, Hash, Debug)] pub struct Identifier(Box); impl GetSize for Identifier { @@ -9,22 +15,52 @@ impl GetSize for Identifier { } } -impl Debug for Identifier { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { - f.write_str(&self.0) + +#[cfg(test)] +impl FromStr for Identifier { + type Err = anyhow::Error; + + fn from_str(data: &str) -> Result { + Ok(Self::new(data)) } } -#[derive(GetSize)] +#[cfg(test)] +impl Identifier { + pub fn new(s: impl Into>) -> Self { + Self(s.into()) + } +} + +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct ModuleId { pub address: AccountAddress, pub name: Identifier, } -#[derive(GetSize)] -pub struct AccountAddress([u8; 32]); +#[derive(GetSize, PartialEq, Eq)] +pub struct AccountAddress(pub [u8; 32]); +impl fmt::Debug for AccountAddress { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{:x}", self) + } +} + +impl fmt::LowerHex for AccountAddress { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if f.alternate() { + write!(f, "0x")?; + } + + for byte in &self.0 { + write!(f, "{:02x}", byte)?; + } + + Ok(()) + } +} -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct Metadata { /// The key identifying the type of metadata. pub key: Vec, diff --git a/crates/storage/src/move_core_type/runtime_access_specifier.rs b/crates/storage/src/move_core_type/runtime_access_specifier.rs index 3cb0ac45..0b8bc30a 100644 --- a/crates/storage/src/move_core_type/runtime_access_specifier.rs +++ b/crates/storage/src/move_core_type/runtime_access_specifier.rs @@ -36,7 +36,7 @@ use super::{ }; #[allow(dead_code)] -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub enum AccessSpecifier { /// Universal access granted Any, @@ -47,7 +47,7 @@ pub enum AccessSpecifier { Constraint(Vec, Vec), } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct AccessSpecifierClause { pub kind: AccessKind, pub resource: ResourceSpecifier, @@ -55,7 +55,7 @@ pub struct AccessSpecifierClause { } #[allow(dead_code)] -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub enum ResourceSpecifier { Any, DeclaredAtAddress(AccountAddress), @@ -65,7 +65,7 @@ pub enum ResourceSpecifier { } #[allow(dead_code)] -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub enum AddressSpecifier { Any, Literal(AccountAddress), @@ -76,7 +76,7 @@ pub enum AddressSpecifier { } #[allow(dead_code)] -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub enum AddressSpecifierFunction { /// Identity function -- just returns the value of the parameter. Identity, @@ -86,7 +86,7 @@ pub enum AddressSpecifierFunction { ObjectAddress, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct AccessInstance { pub kind: AccessKind, pub resource: StructIdentifier, diff --git a/crates/storage/src/move_core_type/runtime_types.rs b/crates/storage/src/move_core_type/runtime_types.rs index f7356d61..80884c51 100644 --- a/crates/storage/src/move_core_type/runtime_types.rs +++ b/crates/storage/src/move_core_type/runtime_types.rs @@ -9,14 +9,14 @@ use get_size::GetSize; use smallbitvec::SmallBitVec; use triomphe::Arc as TriompheArc; -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct DepthFormula { pub terms: Vec<(TypeParameterIndex, u64)>, // Ti + Ci pub constant: Option, // Cbase } #[allow(dead_code)] -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct StructType { pub idx: StructNameIndex, pub layout: StructLayout, @@ -29,22 +29,23 @@ pub struct StructType { } #[allow(dead_code)] -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub enum StructLayout { Single(Vec<(Identifier, Type)>), Variants(Vec<(Identifier, Vec<(Identifier, Type)>)>), } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct StructNameIndex(pub usize); -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct StructIdentifier { pub module: ModuleId, pub name: Identifier, } #[allow(dead_code)] +#[derive(PartialEq, Eq, Debug)] pub enum Type { Bool, U8, @@ -97,7 +98,7 @@ impl GetSize for Type { } #[allow(dead_code)] -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] // Cache for the ability of struct. They will be ignored when comparing equality or Ord as they are just used for caching purpose. pub struct AbilityInfo { base_ability_set: AbilitySet, @@ -105,7 +106,7 @@ pub struct AbilityInfo { phantom_ty_args_mask: SmallBitVec, } -#[derive(GetSize)] +#[derive(GetSize, PartialEq, Eq, Debug)] pub struct TypeBuilder { // Maximum number of nodes a fully-instantiated type has. max_ty_size: u64, diff --git a/crates/storage/src/move_core_type/script.rs b/crates/storage/src/move_core_type/script.rs index 066170a8..f878368a 100644 --- a/crates/storage/src/move_core_type/script.rs +++ b/crates/storage/src/move_core_type/script.rs @@ -11,7 +11,7 @@ use super::{ runtime_types::Type, }; -#[derive(GetSize)] +#[derive(GetSize, Debug)] pub struct Script { // primitive pools pub(crate) script: Arc,