Skip to content

Commit

Permalink
comments: rename code storage APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
georgemitenkov committed Aug 24, 2024
1 parent b67a883 commit 90b0e37
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 20 deletions.
17 changes: 10 additions & 7 deletions third_party/move/move-vm/runtime/src/storage/code_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ pub fn script_hash(serialized_script: &[u8]) -> [u8; 32] {
#[delegatable_trait]
pub trait CodeStorage: ModuleStorage {
/// Returns a deserialized script, either by directly deserializing it from the
/// provided bytes, or fetching it from the storage (if it has been cached). Note
/// that there are no guarantees that the returned script is verified. An error
/// is returned if the deserialization fails.
fn fetch_deserialized_script(&self, serialized_script: &[u8]) -> VMResult<Arc<CompiledScript>>;
/// provided bytes (and caching it), or fetching it from the cache. Note that
/// there are no guarantees that the returned script is verified. An error is
/// returned if the deserialization fails.
fn deserialize_and_cache_script(
&self,
serialized_script: &[u8],
) -> VMResult<Arc<CompiledScript>>;

/// Returns a verified script. The script can either be cached, or deserialized and/or
/// verified from scratch. An error is returned if script fails to deserialize or verify.
fn fetch_verified_script(&self, serialized_script: &[u8]) -> VMResult<Arc<Script>>;
/// Returns a verified script. If not yet cached, verified from scratch and cached.
/// An error is returned if script fails to deserialize or verify.
fn verify_and_cache_script(&self, serialized_script: &[u8]) -> VMResult<Arc<Script>>;
}
4 changes: 2 additions & 2 deletions third_party/move/move-vm/runtime/src/storage/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ impl ModuleStorage for DummyCodeStorage {
}

impl CodeStorage for DummyCodeStorage {
fn fetch_deserialized_script(
fn deserialize_and_cache_script(

Check warning on line 93 in third_party/move/move-vm/runtime/src/storage/dummy.rs

View check run for this annotation

Codecov / codecov/patch

third_party/move/move-vm/runtime/src/storage/dummy.rs#L93

Added line #L93 was not covered by tests
&self,
_serialized_script: &[u8],
) -> VMResult<Arc<CompiledScript>> {
unexpected_unimplemented_error!()
}

fn fetch_verified_script(&self, _serialized_script: &[u8]) -> VMResult<Arc<Script>> {
fn verify_and_cache_script(&self, _serialized_script: &[u8]) -> VMResult<Arc<Script>> {

Check warning on line 100 in third_party/move/move-vm/runtime/src/storage/dummy.rs

View check run for this annotation

Codecov / codecov/patch

third_party/move/move-vm/runtime/src/storage/dummy.rs#L100

Added line #L100 was not covered by tests
unexpected_unimplemented_error!()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ impl ModuleStorage for UnreachableCodeStorage {
}

impl CodeStorage for UnreachableCodeStorage {
fn fetch_deserialized_script(
fn deserialize_and_cache_script(

Check warning on line 89 in third_party/move/move-vm/runtime/src/storage/implementations/unreachable_code_storage.rs

View check run for this annotation

Codecov / codecov/patch

third_party/move/move-vm/runtime/src/storage/implementations/unreachable_code_storage.rs#L89

Added line #L89 was not covered by tests
&self,
_serialized_script: &[u8],
) -> VMResult<Arc<CompiledScript>> {
unreachable_error!()
}

fn fetch_verified_script(&self, _serialized_script: &[u8]) -> VMResult<Arc<Script>> {
fn verify_and_cache_script(&self, _serialized_script: &[u8]) -> VMResult<Arc<Script>> {

Check warning on line 96 in third_party/move/move-vm/runtime/src/storage/implementations/unreachable_code_storage.rs

View check run for this annotation

Codecov / codecov/patch

third_party/move/move-vm/runtime/src/storage/implementations/unreachable_code_storage.rs#L96

Added line #L96 was not covered by tests
unreachable_error!()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ impl<M: ModuleStorage + WithEnvironment> UnsyncCodeStorage<M> {
}

impl<M: ModuleStorage + WithEnvironment> CodeStorage for UnsyncCodeStorage<M> {
fn fetch_deserialized_script(&self, serialized_script: &[u8]) -> VMResult<Arc<CompiledScript>> {
fn deserialize_and_cache_script(
&self,
serialized_script: &[u8],
) -> VMResult<Arc<CompiledScript>> {
use hash_map::Entry::*;
use ScriptStorageEntry::*;

Expand All @@ -144,7 +147,7 @@ impl<M: ModuleStorage + WithEnvironment> CodeStorage for UnsyncCodeStorage<M> {
})
}

fn fetch_verified_script(&self, serialized_script: &[u8]) -> VMResult<Arc<Script>> {
fn verify_and_cache_script(&self, serialized_script: &[u8]) -> VMResult<Arc<Script>> {
use hash_map::Entry::*;
use ScriptStorageEntry::*;

Expand Down Expand Up @@ -226,14 +229,14 @@ mod test {
let serialized_script = script(vec!["a"]);
let hash_1 = script_hash(&serialized_script);

assert_ok!(code_storage.fetch_deserialized_script(&serialized_script));
assert_ok!(code_storage.deserialize_and_cache_script(&serialized_script));
assert!(code_storage.matches(vec![hash_1], |e| matches!(e, Deserialized(..))));
assert!(code_storage.matches(vec![], |e| matches!(e, Verified(..))));

let serialized_script = script(vec!["b"]);
let hash_2 = script_hash(&serialized_script);

assert_ok!(code_storage.fetch_deserialized_script(&serialized_script));
assert_ok!(code_storage.deserialize_and_cache_script(&serialized_script));
assert!(code_storage.module_storage().does_not_have_cached_modules());
assert!(code_storage.matches(vec![hash_1, hash_2], |e| matches!(e, Deserialized(..))));
assert!(code_storage.matches(vec![], |e| matches!(e, Verified(..))));
Expand All @@ -254,12 +257,12 @@ mod test {

let serialized_script = script(vec!["a"]);
let hash = script_hash(&serialized_script);
assert_ok!(code_storage.fetch_deserialized_script(&serialized_script));
assert_ok!(code_storage.deserialize_and_cache_script(&serialized_script));
assert!(code_storage.module_storage().does_not_have_cached_modules());
assert!(code_storage.matches(vec![hash], |e| matches!(e, S::Deserialized(..))));
assert!(code_storage.matches(vec![], |e| matches!(e, S::Verified(..))));

assert_ok!(code_storage.fetch_verified_script(&serialized_script));
assert_ok!(code_storage.verify_and_cache_script(&serialized_script));

assert!(code_storage.matches(vec![], |e| matches!(e, S::Deserialized(..))));
assert!(code_storage.matches(vec![hash], |e| matches!(e, S::Verified(..))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,9 @@ impl<'e, B: ModuleBytesStorage> UnsyncModuleStorage<'e, B> {

// Step 1: verify compiled module locally.
let compiled_module = self.fetch_deserialized_module(address, module_name)?;
let size = self.fetch_module_size_in_bytes(address, module_name)?;
let size = self
.fetch_module_size_in_bytes(address, module_name)?
.ok_or_else(|| module_linker_error!(address, module_name))?;
let partially_verified_module = self
.runtime_environment
.build_partially_verified_module(compiled_module, size)?;
Expand Down
4 changes: 2 additions & 2 deletions third_party/move/move-vm/runtime/src/storage/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl LoaderV2 {
traversal_context: &mut TraversalContext,
serialized_script: &[u8],
) -> VMResult<()> {
let compiled_script = code_storage.fetch_deserialized_script(serialized_script)?;
let compiled_script = code_storage.deserialize_and_cache_script(serialized_script)?;

Check warning on line 85 in third_party/move/move-vm/runtime/src/storage/loader.rs

View check run for this annotation

Codecov / codecov/patch

third_party/move/move-vm/runtime/src/storage/loader.rs#L85

Added line #L85 was not covered by tests
let compiled_script = traversal_context.referenced_scripts.alloc(compiled_script);

// TODO(Gas): Should we charge dependency gas for the script itself?
Expand Down Expand Up @@ -151,7 +151,7 @@ impl LoaderV2 {
) -> VMResult<LoadedFunction> {
// Step 1: Load script. During the loading process, if script has not been previously
// cached, it will be verified.
let script = code_storage.fetch_verified_script(serialized_script)?;
let script = code_storage.verify_and_cache_script(serialized_script)?;

Check warning on line 154 in third_party/move/move-vm/runtime/src/storage/loader.rs

View check run for this annotation

Codecov / codecov/patch

third_party/move/move-vm/runtime/src/storage/loader.rs#L154

Added line #L154 was not covered by tests

// Step 2: Load & verify types used as type arguments passed to this script. Note that
// arguments for scripts are verified on the client side.
Expand Down

0 comments on commit 90b0e37

Please sign in to comment.