Skip to content

Commit

Permalink
[aptos-vm] add state value metadata support for module storage
Browse files Browse the repository at this point in the history
  • Loading branch information
georgemitenkov committed Sep 4, 2024
1 parent 6140df5 commit 290541b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

use aptos_types::state_store::state_value::StateValueMetadata;
use move_binary_format::errors::PartialVMResult;
use move_core_types::{account_address::AccountAddress, identifier::IdentStr};
use move_vm_runtime::{DummyCodeStorage, ModuleStorage};

/// Represents module storage used by the Aptos blockchain.
pub trait AptosModuleStorage: ModuleStorage {}
pub trait AptosModuleStorage: ModuleStorage {
/// Returns the state value metadata of an associated with this module. The
/// error is returned if there is a storage error. If the module does not exist,
/// `None` is returned.
fn fetch_state_value_metadata(
&self,
address: &AccountAddress,
module_name: &IdentStr,
) -> PartialVMResult<Option<StateValueMetadata>>;
}

impl AptosModuleStorage for DummyCodeStorage {}
impl AptosModuleStorage for DummyCodeStorage {
fn fetch_state_value_metadata(
&self,
_address: &AccountAddress,
_module_name: &IdentStr,
) -> PartialVMResult<Option<StateValueMetadata>> {
Ok(None)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::module_and_script_storage::{
module_storage::AptosModuleStorage, script_storage::AptosScriptStorage,
};
use aptos_types::state_store::{state_key::StateKey, StateView};
use aptos_types::state_store::{state_key::StateKey, state_value::StateValueMetadata, StateView};
use bytes::Bytes;
use move_binary_format::{errors::PartialVMResult, file_format::CompiledScript, CompiledModule};
use move_core_types::{account_address::AccountAddress, identifier::IdentStr, metadata::Metadata};
Expand Down Expand Up @@ -90,7 +90,23 @@ impl<'s, S: StateView> ModuleStorage for AptosCodeStorageAdapter<'s, S> {
}
}

impl<'s, S: StateView> AptosModuleStorage for AptosCodeStorageAdapter<'s, S> {}
impl<'s, S: StateView> AptosModuleStorage for AptosCodeStorageAdapter<'s, S> {
fn fetch_state_value_metadata(
&self,
address: &AccountAddress,
module_name: &IdentStr,
) -> PartialVMResult<Option<StateValueMetadata>> {
let state_key = StateKey::module(address, module_name);
Ok(self
.storage
.module_storage()
.byte_storage()
.state_view
.get_state_value(&state_key)
.map_err(|e| module_storage_error!(address, module_name, e))?
.map(|s| s.into_metadata()))
}
}

impl<'s, S: StateView> ScriptStorage for AptosCodeStorageAdapter<'s, S> {
fn fetch_deserialized_script(
Expand Down
12 changes: 9 additions & 3 deletions aptos-move/aptos-vm/src/move_vm_ext/write_op_converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ impl<'r> WriteOpConverter<'r> {
for (bytes, compiled_module) in code_and_modules.into_iter() {
let addr = compiled_module.self_addr();
let name = compiled_module.self_name();
let state_key = StateKey::module(addr, name);

let module_exists = module_storage.check_module_exists(addr, name)?;
let op = if module_exists {
Expand All @@ -186,8 +185,15 @@ impl<'r> WriteOpConverter<'r> {
Op::New(bytes)
};

// TODO(loader_v2): Query state value metadata from module storage.
let write_op = self.convert_module(&state_key, op, false)?;
let state_value_metadata = module_storage.fetch_state_value_metadata(addr, name)?;
let write_op = self.convert(
state_value_metadata,
op,
// For modules, creation is never a modification.
false,
)?;

let state_key = StateKey::module(addr, name);
write_ops.insert(state_key, write_op);
}
Ok(write_ops)
Expand Down

0 comments on commit 290541b

Please sign in to comment.