Skip to content

Commit

Permalink
pass the MoveOSCodeCache
Browse files Browse the repository at this point in the history
  • Loading branch information
steelgeek091 committed Jan 15, 2025
1 parent c27987f commit 925a86a
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 39 deletions.
6 changes: 3 additions & 3 deletions moveos/moveos/src/moveos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub struct MoveOS<'a> {

impl<'a> MoveOS<'a> {
pub fn new(
runtime_environment: &RuntimeEnvironment,
runtime_environment: &'a RuntimeEnvironment,
db: MoveOSStore,
system_pre_execute_functions: Vec<FunctionCall>,
system_post_execute_functions: Vec<FunctionCall>,
Expand Down Expand Up @@ -475,7 +475,7 @@ impl<'a> MoveOS<'a> {
// else return VMError and a bool which indicate if we should respawn the session.
fn execute_action(
&self,
session: &mut MoveOSSession<'_, '_, '_, RootObjectResolver<MoveOSStore>, MoveOSGasMeter>,
session: &mut MoveOSSession<'_, '_, RootObjectResolver<MoveOSStore>, MoveOSGasMeter>,
action: VerifiedMoveAction,
) -> Result<(), VMError> {
session.execute_move_action(action)
Expand All @@ -484,7 +484,7 @@ impl<'a> MoveOS<'a> {
fn execution_cleanup(
&self,
is_system_call: bool,
mut session: MoveOSSession<'_, '_, '_, RootObjectResolver<MoveOSStore>, MoveOSGasMeter>,
mut session: MoveOSSession<'_, '_, RootObjectResolver<MoveOSStore>, MoveOSGasMeter>,
status: VMStatus,
vm_error_info: Option<VMErrorInfo>,
) -> Result<(RawTransactionOutput, Option<VMErrorInfo>)> {
Expand Down
2 changes: 1 addition & 1 deletion moveos/moveos/src/moveos_test_model_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub fn build_file_to_module_env(
KnownAttribute::get_all_attribute_names(),
)
.set_pre_compiled_lib_opt(pre_compiled_deps)
.run_with_sources::<PASS_PARSER>(targets_sources, deps_sources)?;
.run_with_sources::<PASS_PARSER>(targets_sources.clone(), deps_sources)?;

let (comment_map, compiler) = match comments_and_compiler_res {
Err(diags) => {
Expand Down
25 changes: 14 additions & 11 deletions moveos/moveos/src/vm/data_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use move_core_types::{
language_storage::ModuleId, value::MoveTypeLayout, vm_status::StatusCode,
};
use move_vm_runtime::data_cache::TransactionCache;
use move_vm_runtime::loader::modules::LegacyModuleStorageAdapter;
use move_vm_runtime::loader::modules::{LegacyModuleStorage, LegacyModuleStorageAdapter};
use move_vm_runtime::logging::expect_no_verification_errors;
use move_vm_runtime::ModuleStorage;
use move_vm_types::{
Expand Down Expand Up @@ -57,7 +57,7 @@ pub struct MoveosDataCache<'r, 'l, S> {
compiled_scripts: BTreeMap<[u8; 32], Arc<CompiledScript>>,
compiled_modules: BTreeMap<ModuleId, (Arc<CompiledModule>, usize, [u8; 32])>,

code_cache: &'r MoveOSCodeCache<'r>,
code_cache: MoveOSCodeCache<'r>,
}

impl<'r, 'l, S: MoveOSResolver> MoveosDataCache<'r, 'l, S> {
Expand All @@ -67,7 +67,7 @@ impl<'r, 'l, S: MoveOSResolver> MoveosDataCache<'r, 'l, S> {
resolver: &'r S,
loader: &'l Loader,
object_runtime: Rc<RwLock<ObjectRuntime<'r>>>,
code_cache: &'r MoveOSCodeCache<'r>,
code_cache: MoveOSCodeCache<'r>,
) -> Self {
MoveosDataCache {
resolver,
Expand Down Expand Up @@ -320,25 +320,28 @@ pub fn into_change_set(

impl<'r, 'l, S: MoveOSResolver> TypeLayoutLoader for MoveosDataCache<'r, 'l, S> {
fn get_type_layout(&self, type_tag: &TypeTag) -> PartialVMResult<MoveTypeLayout> {
let legacy_module_cache =
Arc::new(self.code_cache.legacy_module_cache.clone()) as Arc<dyn LegacyModuleStorage>;
let legacy_module_storage = &LegacyModuleStorageAdapter::new(legacy_module_cache);
self.loader
.get_type_layout(
type_tag,
self.resolver,
&LegacyModuleStorageAdapter::new(Arc::new(&self.code_cache.legacy_module_cache)),
self.code_cache,
legacy_module_storage,
&self.code_cache,
)
.map_err(|e| e.to_partial())
}

fn type_to_type_layout(&self, ty: &Type) -> PartialVMResult<MoveTypeLayout> {
self.loader.type_to_type_layout(
ty,
&LegacyModuleStorageAdapter::new(Arc::new(&self.code_cache.legacy_module_cache)),
self.code_cache,
)
let legacy_module_cache =
Arc::new(self.code_cache.legacy_module_cache.clone()) as Arc<dyn LegacyModuleStorage>;
let legacy_module_storage = &LegacyModuleStorageAdapter::new(legacy_module_cache);
self.loader
.type_to_type_layout(ty, legacy_module_storage, &self.code_cache)
}

fn type_to_type_tag(&self, ty: &Type) -> PartialVMResult<TypeTag> {
self.loader.type_to_type_tag(ty, self.code_cache)
self.loader.type_to_type_tag(ty, &self.code_cache)
}
}
2 changes: 2 additions & 0 deletions moveos/moveos/src/vm/module_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ where
}
}

#[derive(Clone)]
pub struct RoochModuleExtension {
/// Serialized representation of the module.
bytes: Bytes,
Expand Down Expand Up @@ -476,6 +477,7 @@ enum PersistedStateValue {
},
}

#[derive(Clone)]
pub struct MoveOSCodeCache<'a> {
pub runtime_environment: &'a RuntimeEnvironment,
pub script_cache: UnsyncScriptCache<[u8; 32], CompiledScript, Script>,
Expand Down
65 changes: 43 additions & 22 deletions moveos/moveos/src/vm/moveos_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ impl MoveOSVM {
}

pub fn new_session<
'c,
'r,
S: MoveOSResolver,
G: SwitchableGasMeter + ClassifiedGasMeter + Clone,
Expand All @@ -84,14 +83,14 @@ impl MoveOSVM {
remote: &'r S,
ctx: TxContext,
gas_meter: G,
global_module_cache: &'c GlobalModuleCache<
global_module_cache: &'r GlobalModuleCache<
ModuleId,
CompiledModule,
Module,
RoochModuleExtension,
>,
runtime_environment: &'c RuntimeEnvironment,
) -> MoveOSSession<'c, 'r, '_, S, G> {
runtime_environment: &'r RuntimeEnvironment,
) -> MoveOSSession<'r, '_, S, G> {
let root = remote.root();
let object_runtime = Rc::new(RwLock::new(ObjectRuntime::new(ctx, root.clone(), remote)));
MoveOSSession::new(
Expand All @@ -105,19 +104,19 @@ impl MoveOSVM {
)
}

pub fn new_genesis_session<'c, 'r, S: MoveOSResolver>(
pub fn new_genesis_session<'r, S: MoveOSResolver>(
&self,
remote: &'r S,
ctx: TxContext,
genesis_objects: Vec<(ObjectState, MoveTypeLayout)>,
global_module_cache: &'c GlobalModuleCache<
global_module_cache: &'r GlobalModuleCache<
ModuleId,
CompiledModule,
Module,
RoochModuleExtension,
>,
runtime_environment: &'c RuntimeEnvironment,
) -> MoveOSSession<'c, 'r, '_, S, UnmeteredGasMeter> {
runtime_environment: &'r RuntimeEnvironment,
) -> MoveOSSession<'r, '_, S, UnmeteredGasMeter> {
let root = remote.root();
let object_runtime = Rc::new(RwLock::new(ObjectRuntime::genesis(
ctx,
Expand All @@ -139,7 +138,6 @@ impl MoveOSVM {
}

pub fn new_readonly_session<
'c,
'r,
S: MoveOSResolver,
G: SwitchableGasMeter + ClassifiedGasMeter + Clone,
Expand All @@ -148,14 +146,14 @@ impl MoveOSVM {
remote: &'r S,
ctx: TxContext,
gas_meter: G,
global_module_cache: &'c GlobalModuleCache<
global_module_cache: &'r GlobalModuleCache<
ModuleId,
CompiledModule,
Module,
RoochModuleExtension,
>,
runtime_environment: &'c RuntimeEnvironment,
) -> MoveOSSession<'c, 'r, '_, S, G> {
runtime_environment: &'r RuntimeEnvironment,
) -> MoveOSSession<'r, '_, S, G> {
let root = remote.root();
let object_runtime = Rc::new(RwLock::new(ObjectRuntime::new(ctx, root.clone(), remote)));
MoveOSSession::new(
Expand All @@ -181,18 +179,18 @@ impl MoveOSVM {
/// MoveOSSession is a wrapper of MoveVM session with MoveOS specific features.
/// It is used to execute a transaction, every transaction should be executed in a new session.
/// Every session has a TxContext, if the transaction have multiple actions, the TxContext is shared.
pub struct MoveOSSession<'c, 'r, 'l, S, G> {
pub struct MoveOSSession<'r, 'l, S, G> {
pub(crate) vm: &'l MoveVM,
pub(crate) remote: &'r S,
pub(crate) session: Session<'r, 'l, MoveosDataCache<'r, 'l, S>>,
pub(crate) object_runtime: Rc<RwLock<ObjectRuntime<'r>>>,
pub(crate) gas_meter: G,
pub(crate) code_cache: MoveOSCodeCache<'c>,
pub(crate) code_cache: MoveOSCodeCache<'r>,
pub(crate) read_only: bool,
}

#[allow(clippy::arc_with_non_send_sync)]
impl<'c, 'r, 'l, S, G> MoveOSSession<'c, 'r, 'l, S, G>
impl<'r, 'l, S, G> MoveOSSession<'r, 'l, S, G>
where
S: MoveOSResolver,
G: SwitchableGasMeter + ClassifiedGasMeter,
Expand All @@ -203,18 +201,24 @@ where
object_runtime: Rc<RwLock<ObjectRuntime<'r>>>,
gas_meter: G,
read_only: bool,
global_module_cache: &'c GlobalModuleCache<
global_module_cache: &'r GlobalModuleCache<
ModuleId,
CompiledModule,
Module,
RoochModuleExtension,
>,
runtime_environment: &'c RuntimeEnvironment,
runtime_environment: &'r RuntimeEnvironment,
) -> Self {
Self {
vm,
remote,
session: Self::new_inner_session(vm, remote, object_runtime.clone()),
session: Self::new_inner_session(
vm,
remote,
object_runtime.clone(),
global_module_cache,
runtime_environment,
),
object_runtime,
gas_meter,
code_cache: MoveOSCodeCache::new(global_module_cache, runtime_environment),
Expand All @@ -230,7 +234,13 @@ where
let root = self.remote.root().clone();
let object_runtime = Rc::new(RwLock::new(ObjectRuntime::new(new_ctx, root, self.remote)));
Self {
session: Self::new_inner_session(self.vm, self.remote, object_runtime.clone()),
session: Self::new_inner_session(
self.vm,
self.remote,
object_runtime.clone(),
self.code_cache.global_module_cache,
self.code_cache.runtime_environment
),
object_runtime,
..self
}
Expand All @@ -240,6 +250,13 @@ where
vm: &'l MoveVM,
remote: &'r S,
object_runtime: Rc<RwLock<ObjectRuntime<'r>>>,
global_module_cache: &'r GlobalModuleCache<
ModuleId,
CompiledModule,
Module,
RoochModuleExtension,
>,
runtime_environment: &'r RuntimeEnvironment,
) -> Session<'r, 'l, MoveosDataCache<'r, 'l, S>> {
let mut extensions = NativeContextExtensions::default();

Expand All @@ -256,8 +273,12 @@ where
// vm.mark_loader_cache_as_invalid();
vm.flush_loader_cache_if_invalidated();
let loader = vm.runtime.loader();
let data_store: MoveosDataCache<'r, 'l, S> =
MoveosDataCache::new(remote, loader, object_runtime);
let data_store: MoveosDataCache<'r, 'l, S> = MoveosDataCache::new(
remote,
loader,
object_runtime,
MoveOSCodeCache::new(global_module_cache, runtime_environment),
);
vm.new_session_with_extensions_legacy(data_store, extensions)
}

Expand All @@ -272,7 +293,7 @@ where
match action {
MoveAction::Script(call) => {
let loaded_function = self.session.load_script(
self.remote,
&self.code_cache,
call.code.as_slice(),
call.ty_args.as_slice(),
)?;
Expand Down
4 changes: 2 additions & 2 deletions moveos/moveos/src/vm/tx_argument_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use std::ops::Deref;
use std::sync::Arc;
use std::vec::IntoIter;

impl<'c, 'r, 'l, S, G> MoveOSSession<'c, 'r, 'l, S, G>
impl<'r, 'l, S, G> MoveOSSession<'r, 'l, S, G>
where
S: MoveOSResolver,
G: SwitchableGasMeter + ClassifiedGasMeter,
Expand Down Expand Up @@ -460,7 +460,7 @@ where
}
}

impl<'c, 'r, 'l, S, G> TypeLayoutLoader for MoveOSSession<'c, 'r, 'l, S, G>
impl<'r, 'l, S, G> TypeLayoutLoader for MoveOSSession<'r, 'l, S, G>
where
S: MoveOSResolver,
G: SwitchableGasMeter + ClassifiedGasMeter,
Expand Down

0 comments on commit 925a86a

Please sign in to comment.