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 Aug 19, 2024
1 parent 882dfc8 commit d7437e8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 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
@@ -1,13 +1,16 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

use crate::module_and_script_storage::{
module_storage::AptosModuleStorage, script_storage::AptosScriptStorage,
state_view_adapter::ModuleStorageEntry::Deserialized,
use crate::{
module_and_script_storage::{
module_storage::AptosModuleStorage, script_storage::AptosScriptStorage,
state_view_adapter::ModuleStorageEntry::Deserialized,
},
resolver::TModuleView,
};
use aptos_types::{
on_chain_config::{Features, OnChainConfig},
state_store::{state_key::StateKey, StateView},
state_store::{state_key::StateKey, state_value::StateValueMetadata, StateView},
vm::configs::aptos_prod_deserializer_config,
};
use bytes::Bytes;
Expand Down Expand Up @@ -224,7 +227,16 @@ 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);
self.state_view.get_module_state_value_metadata(&state_key)
}
}

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 d7437e8

Please sign in to comment.