Skip to content

Commit

Permalink
Loader V2 refactoring:
Browse files Browse the repository at this point in the history
  - Unify imports
  - Move environment from storages into ModuleBytesStorages
  - Move unsync code storage implementation tests
  - Remove V1 loader test flows
  • Loading branch information
georgemitenkov committed Nov 19, 2024
1 parent f865dff commit f4e1b3f
Show file tree
Hide file tree
Showing 38 changed files with 840 additions and 1,529 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions aptos-move/aptos-vm-profiling/src/bins/run_move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ fn main() -> Result<()> {

let runtime_environment = RuntimeEnvironment::new(natives);
let vm = MoveVM::new_with_runtime_environment(&runtime_environment);
let mut storage = InMemoryStorage::new();
let mut storage = InMemoryStorage::new(runtime_environment);

let test_modules = compile_test_modules();
for module in &test_modules {
Expand All @@ -188,7 +188,7 @@ fn main() -> Result<()> {
let mut sess = vm.new_session_with_extensions(&storage, extensions);

let traversal_storage = TraversalStorage::new();
let code_storage = storage.as_unsync_code_storage(runtime_environment);
let code_storage = storage.as_unsync_code_storage();

let args: Vec<Vec<u8>> = vec![];
match entrypoint {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ use move_vm_types::{
use std::{ops::Deref, sync::Arc};

/// Avoids orphan rule to implement [ModuleBytesStorage] for [StateView].
struct StateViewAdapter<'s, S> {
struct StateViewAdapter<'s, S, E> {
environment: E,
state_view: BorrowedOrOwned<'s, S>,
}

impl<'s, S: StateView> ModuleBytesStorage for StateViewAdapter<'s, S> {
impl<'s, S: StateView, E: WithRuntimeEnvironment> ModuleBytesStorage
for StateViewAdapter<'s, S, E>
{
fn fetch_module_bytes(
&self,
address: &AccountAddress,
Expand All @@ -48,7 +51,15 @@ impl<'s, S: StateView> ModuleBytesStorage for StateViewAdapter<'s, S> {
}
}

impl<'s, S: StateView> Deref for StateViewAdapter<'s, S> {
impl<'s, S: StateView, E: WithRuntimeEnvironment> WithRuntimeEnvironment
for StateViewAdapter<'s, S, E>
{
fn runtime_environment(&self) -> &RuntimeEnvironment {
self.environment.runtime_environment()
}
}

impl<'s, S: StateView, E: WithRuntimeEnvironment> Deref for StateViewAdapter<'s, S, E> {
type Target = S;

fn deref(&self) -> &Self::Target {
Expand All @@ -67,27 +78,29 @@ impl<'s, S: StateView> Deref for StateViewAdapter<'s, S> {
#[delegate(ModuleStorage, where = "S: StateView, E: WithRuntimeEnvironment")]
#[delegate(CodeStorage, where = "S: StateView, E: WithRuntimeEnvironment")]
pub struct AptosCodeStorageAdapter<'s, S, E> {
storage: UnsyncCodeStorage<UnsyncModuleStorage<'s, StateViewAdapter<'s, S>, E>>,
storage: UnsyncCodeStorage<UnsyncModuleStorage<'s, StateViewAdapter<'s, S, E>>>,
}

impl<'s, S: StateView, E: WithRuntimeEnvironment> AptosCodeStorageAdapter<'s, S, E> {
/// Creates new instance of [AptosCodeStorageAdapter] built on top of the passed state view and
/// the provided runtime environment.
fn from_borrowed(state_view: &'s S, runtime_environment: E) -> Self {
fn from_borrowed(state_view: &'s S, environment: E) -> Self {
let adapter = StateViewAdapter {
environment,
state_view: BorrowedOrOwned::Borrowed(state_view),
};
let storage = adapter.into_unsync_code_storage(runtime_environment);
let storage = adapter.into_unsync_code_storage();
Self { storage }
}

/// Creates new instance of [AptosCodeStorageAdapter] capturing the passed state view and the
/// provided environment.
fn from_owned(state_view: S, runtime_environment: E) -> Self {
fn from_owned(state_view: S, environment: E) -> Self {
let adapter = StateViewAdapter {
environment,
state_view: BorrowedOrOwned::Owned(state_view),
};
let storage = adapter.into_unsync_code_storage(runtime_environment);
let storage = adapter.into_unsync_code_storage();
Self { storage }
}

Expand Down
12 changes: 10 additions & 2 deletions aptos-move/vm-genesis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ use aptos_vm_types::{
};
use bytes::Bytes;
use claims::assert_ok;
use move_binary_format::errors::{Location, VMResult};
use move_binary_format::{
compatibility::Compatibility,
errors::{Location, VMResult},
};
use move_core_types::{
account_address::AccountAddress,
identifier::Identifier,
Expand Down Expand Up @@ -1017,7 +1020,12 @@ fn publish_framework_with_loader_v1(

#[allow(deprecated)]
session
.publish_module_bundle(code, addr, &mut UnmeteredGasMeter)
.publish_module_bundle_with_compat_config(
code,
addr,
&mut UnmeteredGasMeter,
Compatibility::full_check(),
)
.unwrap_or_else(|e| {
panic!(
"Failure publishing package `{}`: {:?}",
Expand Down
1 change: 1 addition & 0 deletions third_party/move/move-vm/integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ edition = "2021"
[dependencies]
anyhow = { workspace = true }
bytes = { workspace = true }
claims = { workspace = true }
memory-stats = { workspace = true }
move-binary-format = { path = "../../move-binary-format", features = ["testing"] }
move-bytecode-verifier = { path = "../../move-bytecode-verifier" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ use move_core_types::{
};
use move_vm_runtime::{
module_traversal::*, move_vm::MoveVM, AsUnsyncModuleStorage, RuntimeEnvironment,
WithRuntimeEnvironment,
};
use move_vm_test_utils::{BlankStorage, InMemoryStorage};
use move_vm_test_utils::InMemoryStorage;
use move_vm_types::gas::UnmeteredGasMeter;

const TEST_ADDR: AccountAddress = AccountAddress::new([42; AccountAddress::LENGTH]);
Expand All @@ -22,13 +23,13 @@ const TEST_ADDR: AccountAddress = AccountAddress::new([42; AccountAddress::LENGT
fn call_non_existent_module() {
let runtime_environment = RuntimeEnvironment::new(vec![]);
let vm = MoveVM::new_with_runtime_environment(&runtime_environment);
let storage = BlankStorage;
let storage = InMemoryStorage::new(runtime_environment);

let mut sess = vm.new_session(&storage);
let module_id = ModuleId::new(TEST_ADDR, Identifier::new("M").unwrap());
let fun_name = Identifier::new("foo").unwrap();
let traversal_storage = TraversalStorage::new();
let module_storage = storage.as_unsync_module_storage(runtime_environment);
let module_storage = storage.as_unsync_module_storage();

let err = sess
.execute_function_bypass_visibility(
Expand Down Expand Up @@ -57,18 +58,19 @@ fn call_non_existent_function() {
let mut blob = vec![];
m.serialize(&mut blob).unwrap();

let mut storage = InMemoryStorage::new();
let runtime_environment = RuntimeEnvironment::new(vec![]);
let mut storage = InMemoryStorage::new(runtime_environment);

let module_id = ModuleId::new(TEST_ADDR, Identifier::new("M").unwrap());
storage.add_module_bytes(module_id.address(), module_id.name(), blob.into());

let runtime_environment = RuntimeEnvironment::new(vec![]);
let vm = MoveVM::new_with_runtime_environment(&runtime_environment);
let vm = MoveVM::new_with_runtime_environment(storage.runtime_environment());
let mut sess = vm.new_session(&storage);

let fun_name = Identifier::new("foo").unwrap();

let traversal_storage = TraversalStorage::new();
let module_storage = storage.as_unsync_module_storage(runtime_environment);
let module_storage = storage.as_unsync_module_storage();

let err = sess
.execute_function_bypass_visibility(
Expand Down
Loading

0 comments on commit f4e1b3f

Please sign in to comment.