Skip to content

Commit

Permalink
fix global_module_cache, client_resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
steelgeek091 committed Jan 10, 2025
1 parent d86715b commit 9e44e89
Show file tree
Hide file tree
Showing 14 changed files with 198 additions and 52 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.

9 changes: 7 additions & 2 deletions crates/rooch-executor/src/actor/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use function_name::named;
use move_core_types::vm_status::VMStatus;
use move_vm_runtime::RuntimeEnvironment;
use moveos::moveos::{MoveOS, MoveOSConfig};
use moveos::vm::module_cache::GlobalModuleCache;
use moveos::vm::vm_status_explainer::explain_vm_status;
use moveos_eventbus::bus::EventData;
use moveos_store::MoveOSStore;
Expand Down Expand Up @@ -48,9 +49,9 @@ use rooch_types::transaction::{
use std::str::FromStr;
use std::sync::Arc;

pub struct ExecutorActor {
pub struct ExecutorActor<'a> {
root: ObjectMeta,
moveos: MoveOS,
moveos: MoveOS<'a>,
moveos_store: MoveOSStore,
rooch_store: RoochStore,
metrics: Arc<ExecutorMetrics>,
Expand All @@ -71,12 +72,14 @@ impl ExecutorActor {
let gas_parameters = FrameworksGasParameters::load_from_chain(&resolver)?;

let runtime_environment = RuntimeEnvironment::new(gas_parameters.all_natives());
let global_module_cache = GlobalModuleCache::empty();

let moveos = MoveOS::new(
&runtime_environment,
moveos_store.clone(),
system_pre_execute_functions(),
system_post_execute_functions(),
&global_module_cache,
)?;

Ok(Self {
Expand Down Expand Up @@ -524,12 +527,14 @@ impl Handler<EventData> for ExecutorActor {
let gas_parameters = FrameworksGasParameters::load_from_chain(&resolver)?;

let runtime_environment = RuntimeEnvironment::new(gas_parameters.all_natives());
let global_module_cache = GlobalModuleCache::empty();

self.moveos = MoveOS::new(
&runtime_environment,
self.moveos_store.clone(),
system_pre_execute_functions(),
system_post_execute_functions(),
&global_module_cache,
)?;
}
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions crates/rooch-indexer/src/indexer_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ fn get_table_name_by_state_type(state_type: ObjectStateType) -> IndexerTableName
fn object_type_query(object_type: &StructTag) -> String {
let object_type_str = object_type.to_string();
// if the caller does not specify the type parameters, we will use the prefix match
if object_type.type_params.is_empty() {
if object_type.type_args.is_empty() {
let (first_bound, second_bound, upper_bound) =
optimize_object_type_like_query(object_type_str.as_str());
format!(
Expand All @@ -635,7 +635,7 @@ fn object_type_query(object_type: &StructTag) -> String {
fn not_object_type_query(object_type: &StructTag) -> String {
let object_type_str = object_type.to_string();
// if the caller does not specify the type parameters, we will use the prefix match
if object_type.type_params.is_empty() {
if object_type.type_args.is_empty() {
let (first_bound, second_bound, upper_bound) =
optimize_object_type_like_query(object_type_str.as_str());
format!(
Expand Down
2 changes: 2 additions & 0 deletions crates/rooch-rpc-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ serde_json = { workspace = true }
bitcoin = { workspace = true }
bitcoincore-rpc = { workspace = true }
tracing = { workspace = true }
bytes = { workspace = true }

move-core-types = { workspace = true }
move-vm-types = { workspace = true }

moveos-types = { workspace = true }

Expand Down
53 changes: 42 additions & 11 deletions crates/rooch-rpc-client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use anyhow::{ensure, Error, Result};
use anyhow::{anyhow, ensure, Error, Result};
use bytes::Bytes;
use jsonrpsee::core::client::ClientT;
use jsonrpsee::http_client::{HttpClient, HttpClientBuilder};
use move_core_types::account_address::AccountAddress;
use move_core_types::language_storage::{ModuleId, StructTag};
use move_core_types::metadata::Metadata;
use move_core_types::resolver::{ModuleResolver, ResourceResolver};
use move_core_types::value::MoveTypeLayout;
use move_core_types::vm_status::StatusCode;
use move_vm_types::natives::function::{PartialVMError, PartialVMResult};
use move_vm_types::resolver::ModuleResolver;
use move_vm_types::resolver::ResourceResolver;
use moveos_types::access_path::AccessPath;
use moveos_types::h256::H256;
use moveos_types::move_std::string::MoveString;
Expand Down Expand Up @@ -111,17 +116,30 @@ impl ModuleResolver for &Client {
Vec::new()
}

fn get_module(&self, id: &ModuleId) -> Result<Option<Vec<u8>>> {
fn get_module(&self, id: &ModuleId) -> PartialVMResult<Option<Bytes>> {
tokio::task::block_in_place(|| {
Handle::current().block_on(async {
let mut states = self.rooch.get_states(AccessPath::module(id), None).await?;
let mut states = match self.rooch.get_states(AccessPath::module(id), None).await {
Ok(states) => states,
Err(e) => {
return Err(
PartialVMError::new(StatusCode::ABORTED).with_message(e.to_string())
)
}
};
states
.pop()
.flatten()
.map(|state_view| {
let state = ObjectState::from(state_view);
let module = state.value_as_df::<MoveString, MoveModule>()?;
Ok(module.value.byte_codes)
let module = match state.value_as_df::<MoveString, MoveModule>() {
Ok(v) => v,
Err(e) => {
return Err(PartialVMError::new(StatusCode::ABORTED)
.with_message(e.to_string()))
}
};
Ok(Bytes::copy_from_slice(module.value.byte_codes.as_slice()))
})
.transpose()
})
Expand All @@ -139,15 +157,13 @@ impl ClientResolver {
pub fn new(client: Client, root: ObjectMeta) -> Self {
Self { root, client }
}
}

impl ResourceResolver for ClientResolver {
fn get_resource_with_metadata(
&self,
address: &AccountAddress,
resource_tag: &StructTag,
_metadata: &[Metadata],
) -> std::result::Result<(Option<Vec<u8>>, usize), Error> {
) -> std::result::Result<(Option<Bytes>, usize), Error> {
let account_object_id = Account::account_object_id(*address);

let key = FieldKey::derive_resource_key(resource_tag);
Expand All @@ -168,7 +184,7 @@ impl ResourceResolver for ClientResolver {
match result {
Ok(opt) => {
if let Some(data) = opt {
Ok((Some(data), 0))
Ok((Some(Bytes::copy_from_slice(data.as_slice())), 0))
} else {
Ok((None, 0))
}
Expand All @@ -178,12 +194,27 @@ impl ResourceResolver for ClientResolver {
}
}

impl ResourceResolver for ClientResolver {
fn get_resource_bytes_with_metadata_and_layout(
&self,
address: &AccountAddress,
struct_tag: &StructTag,
metadata: &[Metadata],
_layout: Option<&MoveTypeLayout>,
) -> PartialVMResult<(Option<Bytes>, usize)> {
match self.get_resource_with_metadata(address, struct_tag, metadata) {
Ok(v) => Ok(v),
Err(e) => Err(PartialVMError::new(StatusCode::ABORTED).with_message(format!("{}", e))),
}
}
}

impl ModuleResolver for ClientResolver {
fn get_module_metadata(&self, _module_id: &ModuleId) -> Vec<Metadata> {
vec![]
}

fn get_module(&self, id: &ModuleId) -> std::result::Result<Option<Vec<u8>>, Error> {
fn get_module(&self, id: &ModuleId) -> PartialVMResult<Option<Bytes>> {
(&self.client).get_module(id)
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/rooch/src/commands/move_cli/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ impl CommandAction<Option<Value>> for BuildCommand {

let config_cloned = config.clone();

let mut package = config.compile_package_no_exit(&rerooted_path, &mut std::io::stdout())?;
let (mut package, _) =
config.compile_package_no_exit(&rerooted_path, vec![], &mut std::io::stdout())?;

run_verifier(rerooted_path.clone(), config_cloned.clone(), &mut package)?;

Expand Down
32 changes: 26 additions & 6 deletions moveos/moveos/src/gas/abstract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use crate::gas::table::AbstractValueSizeGasParameter;
use move_core_types::gas_algebra::{Arg, GasQuantity, InternalGasUnit, UnitDiv};
use move_core_types::{account_address::AccountAddress, gas_algebra::NumArgs, u256::U256};
use move_vm_types::delayed_values::delayed_field_id::DelayedFieldID;
use move_vm_types::views::{ValueView, ValueVisitor};

pub enum AbstractValueUnit {}
Expand Down Expand Up @@ -51,6 +52,7 @@ where
V: ValueVisitor,
{
deref_visitor_delegate_simple!(
[visit_delayed, DelayedFieldID],
[visit_u8, u8],
[visit_u16, u16],
[visit_u32, u32],
Expand Down Expand Up @@ -103,6 +105,12 @@ impl<'a> AbstractValueSizeVisitor<'a> {
}

impl<'a> ValueVisitor for AbstractValueSizeVisitor<'a> {
#[inline]
fn visit_delayed(&mut self, _depth: usize, _id: DelayedFieldID) {
// TODO[agg_v2](cleanup): add a new abstract value size parameter?
self.size += self.params.u64;
}

#[inline]
fn visit_u8(&mut self, _depth: usize, _val: u8) {
self.size += self.params.u8;
Expand Down Expand Up @@ -155,6 +163,12 @@ impl<'a> ValueVisitor for AbstractValueSizeVisitor<'a> {
true
}

#[inline]
fn visit_ref(&mut self, _depth: usize, _is_global: bool) -> bool {
self.size += self.params.reference;
false
}

#[inline]
fn visit_vec_u8(&mut self, _depth: usize, vals: &[u8]) {
let mut size = self.params.per_u8_packed * NumArgs::new(vals.len() as u64);
Expand Down Expand Up @@ -207,12 +221,6 @@ impl<'a> ValueVisitor for AbstractValueSizeVisitor<'a> {
size += self.params.vector;
self.size += size;
}

#[inline]
fn visit_ref(&mut self, _depth: usize, _is_global: bool) -> bool {
self.size += self.params.reference;
false
}
}

impl AbstractValueSizeGasParameter {
Expand Down Expand Up @@ -240,6 +248,12 @@ impl AbstractValueSizeGasParameter {
}

impl<'a> ValueVisitor for Visitor<'a> {
#[inline]
fn visit_delayed(&mut self, _depth: usize, _val: DelayedFieldID) {
// TODO[agg_v2](cleanup): add a new abstract value size parameter?
self.res = Some(self.params.u64);
}

#[inline]
fn visit_u8(&mut self, _depth: usize, _val: u8) {
self.res = Some(self.params.u8);
Expand Down Expand Up @@ -356,6 +370,12 @@ impl AbstractValueSizeGasParameter {
}

impl<'a> ValueVisitor for Visitor<'a> {
#[inline]
fn visit_delayed(&mut self, _depth: usize, _val: DelayedFieldID) {
// TODO[agg_v2](cleanup): add a new abstract value size parameter?
self.res = Some(self.params.per_u64_packed * NumArgs::from(1));
}

#[inline]
fn visit_u8(&mut self, _depth: usize, _val: u8) {
self.res = Some(self.params.per_u8_packed * NumArgs::from(1));
Expand Down
40 changes: 36 additions & 4 deletions moveos/moveos/src/gas/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ use anyhow::Result;
use move_binary_format::errors::{PartialVMError, PartialVMResult};
use move_binary_format::file_format::CodeOffset;
use move_core_types::account_address::AccountAddress;
use move_core_types::gas_algebra::{
AbstractMemorySize, GasQuantity, InternalGas, InternalGasPerArg, InternalGasPerByte, NumArgs,
NumBytes,
};
use move_core_types::gas_algebra::{AbstractMemorySize, GasQuantity, InternalGas, InternalGasPerArg, InternalGasPerByte, NumArgs, NumBytes, NumTypeNodes};
use move_core_types::language_storage::ModuleId;
use move_core_types::vm_status::StatusCode;
use move_vm_types::gas::{GasMeter, SimpleInstruction};
Expand All @@ -26,6 +23,7 @@ use std::cell::RefCell;
use std::collections::BTreeMap;
use std::ops::{Add, Bound};
use std::rc::Rc;
use move_core_types::identifier::IdentStr;

/// The size in bytes for a reference on the stack
pub const REFERENCE_SIZE: AbstractMemorySize = AbstractMemorySize::new(8);
Expand Down Expand Up @@ -1350,6 +1348,40 @@ impl GasMeter for MoveOSGasMeter {
) -> PartialVMResult<()> {
Ok(())
}


fn charge_create_ty(&mut self, num_nodes: NumTypeNodes) -> PartialVMResult<()> {
/*
let (_cost, res) = self.delegate_charge(|base| base.charge_create_ty(num_nodes));
res
*/
Ok(())
}

fn charge_dependency(
&mut self,
is_new: bool,
addr: &AccountAddress,
name: &IdentStr,
size: NumBytes,
) -> PartialVMResult<()> {
/*
let (cost, res) =
self.delegate_charge(|base| base.charge_dependency(is_new, addr, name, size));
if !cost.is_zero() {
self.dependencies.push(Dependency {
is_new,
id: ModuleId::new(*addr, name.to_owned()),
size,
cost,
});
}
res
*/
Ok(())
}
}

impl SwitchableGasMeter for MoveOSGasMeter {
Expand Down
Loading

0 comments on commit 9e44e89

Please sign in to comment.