Skip to content

Commit

Permalink
[block-stm] Integrate script caches
Browse files Browse the repository at this point in the history
  • Loading branch information
georgemitenkov committed Sep 1, 2024
1 parent 54658bc commit d90a9ee
Show file tree
Hide file tree
Showing 15 changed files with 339 additions and 68 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion aptos-move/aptos-vm/src/aptos_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3122,7 +3122,7 @@ pub(crate) fn is_account_init_for_sponsored_transaction(
None,
)
.map_err(|e| e.finish(Location::Undefined))?;
return Ok(maybe_bytes.is_some());
return Ok(maybe_bytes.is_none());

Check warning on line 3125 in aptos-move/aptos-vm/src/aptos_vm.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/aptos-vm/src/aptos_vm.rs#L3125

Added line #L3125 was not covered by tests
}
Ok(false)
}
Expand Down
25 changes: 15 additions & 10 deletions aptos-move/block-executor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ where
// TODO(loader_v2): We do not need to clone out all module writes in V2 design
// since we do not store them anymore.
versioned_cache
.code_storage()

Check warning on line 198 in aptos-move/block-executor/src/executor.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/block-executor/src/executor.rs#L198

Added line #L198 was not covered by tests
.module_storage()
.write_pending(k, idx_to_execute)
} else {
Expand Down Expand Up @@ -300,7 +301,10 @@ where
Resource => versioned_cache.data().remove(&k, idx_to_execute),
Module => {
if runtime_environment.vm_config().use_loader_v2 {
versioned_cache.module_storage().remove(&k, idx_to_execute);
versioned_cache
.code_storage()
.module_storage()
.remove(&k, idx_to_execute);

Check warning on line 307 in aptos-move/block-executor/src/executor.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/block-executor/src/executor.rs#L304-L307

Added lines #L304 - L307 were not covered by tests
} else {
versioned_cache.modules().remove(&k, idx_to_execute);
}
Expand Down Expand Up @@ -371,7 +375,10 @@ where

let is_valid = read_set.validate_data_reads(versioned_cache.data(), idx_to_validate)
&& read_set.validate_group_reads(versioned_cache.group_data(), idx_to_validate)
&& read_set.validate_module_reads(versioned_cache.module_storage(), idx_to_validate);
&& read_set.validate_module_reads(
versioned_cache.code_storage().module_storage(),
idx_to_validate,
);

Check warning on line 381 in aptos-move/block-executor/src/executor.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/block-executor/src/executor.rs#L378-L381

Added lines #L378 - L381 were not covered by tests
Ok(is_valid)
}

Expand Down Expand Up @@ -561,13 +568,11 @@ where

if runtime_environment.vm_config().use_loader_v2 {
if let Some(module_write_set) = last_input_output.module_write_set(txn_idx) {
for (key, v) in module_write_set.into_iter() {
let entry =
ModuleStorageEntry::from_transaction_write(runtime_environment, v)?;
versioned_cache
.module_storage()
.write_published(&key, txn_idx, entry);
}
versioned_cache.code_storage().write_published_modules(
txn_idx,
runtime_environment,
module_write_set.into_iter(),
)?;

Check warning on line 575 in aptos-move/block-executor/src/executor.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/block-executor/src/executor.rs#L571-L575

Added lines #L571 - L575 were not covered by tests
scheduler.finish_module_publishing_after_commit(txn_idx, executed_at_commit);
}
}
Expand Down Expand Up @@ -1065,7 +1070,7 @@ where
if runtime_environment.vm_config().use_loader_v2 {
let entry =
ModuleStorageEntry::from_transaction_write(runtime_environment, write_op)?;
unsync_map.write_module_storage_entry(key, entry);
unsync_map.publish_module_storage_entry(key, entry);

Check warning on line 1073 in aptos-move/block-executor/src/executor.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/block-executor/src/executor.rs#L1073

Added line #L1073 was not covered by tests
} else {
unsync_map.write_module(key, write_op);
}
Expand Down
108 changes: 91 additions & 17 deletions aptos-move/block-executor/src/modules_and_scripts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ use move_binary_format::{
};
use move_core_types::{account_address::AccountAddress, identifier::IdentStr, metadata::Metadata};
use move_vm_runtime::{
module_cyclic_dependency_error, module_linker_error, CodeStorage, Module, ModuleStorage, Script,
deserialize_script, module_cyclic_dependency_error, module_linker_error, script_hash,
CodeStorage, Module, ModuleStorage, Script,
};
use std::{collections::HashSet, sync::Arc};

Expand Down Expand Up @@ -63,15 +64,79 @@ impl<'a, T: Transaction, S: TStateView<Key = T::Key>, X: Executable> CodeStorage
{
fn deserialize_and_cache_script(
&self,
_serialized_script: &[u8],
serialized_script: &[u8],

Check warning on line 67 in aptos-move/block-executor/src/modules_and_scripts.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/block-executor/src/modules_and_scripts.rs#L67

Added line #L67 was not covered by tests
) -> VMResult<Arc<CompiledScript>> {
// TODO(loader_v2): Support scripts.
todo!()
let hash = script_hash(serialized_script);

Check warning on line 69 in aptos-move/block-executor/src/modules_and_scripts.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/block-executor/src/modules_and_scripts.rs#L69

Added line #L69 was not covered by tests

let maybe_compiled_script = match &self.latest_view {
ViewState::Sync(state) => state
.versioned_map
.code_storage()
.get_deserialized_script(&hash),
ViewState::Unsync(state) => state.unsync_map.get_deserialized_script(&hash),

Check warning on line 76 in aptos-move/block-executor/src/modules_and_scripts.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/block-executor/src/modules_and_scripts.rs#L71-L76

Added lines #L71 - L76 were not covered by tests
};

Ok(match maybe_compiled_script {
Some(compiled_script) => compiled_script,

Check warning on line 80 in aptos-move/block-executor/src/modules_and_scripts.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/block-executor/src/modules_and_scripts.rs#L79-L80

Added lines #L79 - L80 were not covered by tests
None => {
let compiled_script = self.deserialize_script(serialized_script)?;

Check warning on line 82 in aptos-move/block-executor/src/modules_and_scripts.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/block-executor/src/modules_and_scripts.rs#L82

Added line #L82 was not covered by tests

match &self.latest_view {
ViewState::Sync(state) => state
.versioned_map
.code_storage()
.cache_deserialized_script(hash, compiled_script.clone()),
ViewState::Unsync(state) => state
.unsync_map
.cache_deserialized_script(hash, compiled_script.clone()),

Check warning on line 91 in aptos-move/block-executor/src/modules_and_scripts.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/block-executor/src/modules_and_scripts.rs#L84-L91

Added lines #L84 - L91 were not covered by tests
}
compiled_script

Check warning on line 93 in aptos-move/block-executor/src/modules_and_scripts.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/block-executor/src/modules_and_scripts.rs#L93

Added line #L93 was not covered by tests
},
})
}

fn verify_and_cache_script(&self, _serialized_script: &[u8]) -> VMResult<Arc<Script>> {
// TODO(loader_v2): Support scripts.
todo!()
fn verify_and_cache_script(&self, serialized_script: &[u8]) -> VMResult<Arc<Script>> {
let hash = script_hash(serialized_script);

Check warning on line 99 in aptos-move/block-executor/src/modules_and_scripts.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/block-executor/src/modules_and_scripts.rs#L98-L99

Added lines #L98 - L99 were not covered by tests

let maybe_verified_script = match &self.latest_view {
ViewState::Sync(state) => state
.versioned_map
.code_storage()
.get_verified_script(&hash),
ViewState::Unsync(state) => state.unsync_map.get_verified_script(&hash),

Check warning on line 106 in aptos-move/block-executor/src/modules_and_scripts.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/block-executor/src/modules_and_scripts.rs#L101-L106

Added lines #L101 - L106 were not covered by tests
};

let compiled_script = match maybe_verified_script {
Some(Ok(script)) => return Ok(script),
Some(Err(compiled_script)) => compiled_script,
None => self.deserialize_and_cache_script(serialized_script)?,

Check warning on line 112 in aptos-move/block-executor/src/modules_and_scripts.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/block-executor/src/modules_and_scripts.rs#L109-L112

Added lines #L109 - L112 were not covered by tests
};

// Locally verify the script.
let partially_verified_script = self
.runtime_environment
.build_partially_verified_script(compiled_script)?;

Check warning on line 118 in aptos-move/block-executor/src/modules_and_scripts.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/block-executor/src/modules_and_scripts.rs#L116-L118

Added lines #L116 - L118 were not covered by tests

// Verify the script by also looking at its dependencies.
let immediate_dependencies = partially_verified_script
.immediate_dependencies_iter()
.map(|(addr, name)| self.fetch_verified_module(addr, name))
.collect::<VMResult<Vec<_>>>()?;
let script = self
.runtime_environment
.build_verified_script(partially_verified_script, &immediate_dependencies)?;
let script = Arc::new(script);

match &self.latest_view {
ViewState::Sync(state) => state
.versioned_map
.code_storage()
.cache_verified_script(hash, script.clone()),
ViewState::Unsync(state) => {
state.unsync_map.cache_verified_script(hash, script.clone())

Check warning on line 136 in aptos-move/block-executor/src/modules_and_scripts.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/block-executor/src/modules_and_scripts.rs#L121-L136

Added lines #L121 - L136 were not covered by tests
},
}
Ok(script)

Check warning on line 139 in aptos-move/block-executor/src/modules_and_scripts.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/block-executor/src/modules_and_scripts.rs#L139

Added line #L139 was not covered by tests
}
}

Expand Down Expand Up @@ -166,11 +231,13 @@ impl<'a, T: Transaction, S: TStateView<Key = T::Key>, X: Executable> LatestView<

match &self.latest_view {
ViewState::Sync(state) => {
let result = state.versioned_map.module_storage().get_or_else(
key,
ShiftedTxnIndex::new(self.txn_idx),
|| self.get_base_module_storage_entry(key),
)?;
let result = state
.versioned_map
.code_storage()
.module_storage()
.get_or_else(key, ShiftedTxnIndex::new(self.txn_idx), || {
self.get_base_module_storage_entry(key)
})?;

Check warning on line 240 in aptos-move/block-executor/src/modules_and_scripts.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/block-executor/src/modules_and_scripts.rs#L234-L240

Added lines #L234 - L240 were not covered by tests
state
.captured_reads
.borrow_mut()
Expand Down Expand Up @@ -283,11 +350,11 @@ impl<'a, T: Transaction, S: TStateView<Key = T::Key>, X: Executable> LatestView<
let key = T::Key::from_address_and_module_name(address, module_name);
match &self.latest_view {
ViewState::Sync(state) => {
state.versioned_map.module_storage().write_if_not_verified(
&key,
txn_idx,
verified_entry,
);
state
.versioned_map
.code_storage()
.module_storage()
.write_if_not_verified(&key, txn_idx, verified_entry);

Check warning on line 357 in aptos-move/block-executor/src/modules_and_scripts.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/block-executor/src/modules_and_scripts.rs#L353-L357

Added lines #L353 - L357 were not covered by tests
},
ViewState::Unsync(state) => {
state
Expand All @@ -297,4 +364,11 @@ impl<'a, T: Transaction, S: TStateView<Key = T::Key>, X: Executable> LatestView<
}
Ok(module)
}

/// Returns the deserialized script based on the current runtime environment.
fn deserialize_script(&self, serialized_script: &[u8]) -> VMResult<Arc<CompiledScript>> {
let deserializer_config = &self.runtime_environment.vm_config().deserializer_config;
let compiled_script = deserialize_script(serialized_script, deserializer_config)?;
Ok(Arc::new(compiled_script))
}

Check warning on line 373 in aptos-move/block-executor/src/modules_and_scripts.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/block-executor/src/modules_and_scripts.rs#L369-L373

Added lines #L369 - L373 were not covered by tests
}
1 change: 1 addition & 0 deletions aptos-move/mvhashmap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ derivative = { workspace = true }
move-binary-format = { workspace = true }
move-core-types = { workspace = true }
move-vm-types = { workspace = true }
move-vm-runtime = { workspace = true }
serde = { workspace = true }

[dev-dependencies]
Expand Down
14 changes: 8 additions & 6 deletions aptos-move/mvhashmap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// SPDX-License-Identifier: Apache-2.0

use crate::{
versioned_data::VersionedData, versioned_delayed_fields::VersionedDelayedFields,
versioned_group_data::VersionedGroupData, versioned_module_storage::VersionedModuleStorage,
versioned_code_storage::VersionedCodeStorage, versioned_data::VersionedData,
versioned_delayed_fields::VersionedDelayedFields, versioned_group_data::VersionedGroupData,
versioned_modules::VersionedModules,
};
use aptos_types::{
Expand All @@ -16,12 +16,14 @@ use std::{fmt::Debug, hash::Hash};

pub mod types;
pub mod unsync_map;
pub mod versioned_code_storage;
pub mod versioned_data;
pub mod versioned_delayed_fields;
pub mod versioned_group_data;
pub mod versioned_module_storage;
pub mod versioned_modules;

mod scripts;
#[cfg(test)]
mod unit_tests;

Expand All @@ -39,7 +41,7 @@ pub struct MVHashMap<K, T, V: TransactionWrite, X: Executable, I: Clone> {
group_data: VersionedGroupData<K, T, V>,
delayed_fields: VersionedDelayedFields<I>,
modules: VersionedModules<K, V, X>,
module_storage: VersionedModuleStorage<K>,
code_storage: VersionedCodeStorage<K>,
}

impl<
Expand All @@ -60,7 +62,7 @@ impl<
delayed_fields: VersionedDelayedFields::new(),
modules: VersionedModules::new(),

module_storage: VersionedModuleStorage::empty(),
code_storage: VersionedCodeStorage::empty(),
}
}

Expand Down Expand Up @@ -94,8 +96,8 @@ impl<
&self.modules
}

pub fn module_storage(&self) -> &VersionedModuleStorage<K> {
&self.module_storage
pub fn code_storage(&self) -> &VersionedCodeStorage<K> {
&self.code_storage
}
}

Expand Down
32 changes: 32 additions & 0 deletions aptos-move/mvhashmap/src/scripts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

use move_binary_format::file_format::CompiledScript;
use move_vm_runtime::Script;
use std::sync::Arc;

/// An entry for the script cache, used by the Aptos code cache. Entries
/// can live in the cache in different states (deserialized / verified).
#[derive(Debug)]
pub(crate) enum ScriptCacheEntry {
Deserialized(Arc<CompiledScript>),
Verified(Arc<Script>),
}

impl ScriptCacheEntry {
/// Returns the deserialized (compiled) representation of the script.
pub(crate) fn as_compiled_script(&self) -> Arc<CompiledScript> {
match self {
Self::Deserialized(compiled_script) => compiled_script.clone(),
Self::Verified(script) => script.as_compiled_script(),
}
}

/// Returns true if the script entry has already been verified.
pub(crate) fn is_verified(&self) -> bool {
match self {
Self::Verified(_) => true,
Self::Deserialized(_) => false,
}
}
}
Loading

0 comments on commit d90a9ee

Please sign in to comment.