Skip to content

Commit

Permalink
update starcoin-vm-runtime
Browse files Browse the repository at this point in the history
* no-check for native_params for starcoin-vm
* add TraversalContext
* fix VMStatus::Error
* remove generic parameter from SessionAdapter
* fix vm-runtime verifier
* fix deserialize_args in vm-runtime
* fix some imported crates' errors
  • Loading branch information
simonjiao committed Oct 9, 2024
1 parent 4eb18cc commit c4b5fd5
Show file tree
Hide file tree
Showing 17 changed files with 259 additions and 235 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.

2 changes: 1 addition & 1 deletion executor/tests/readonly_function_call_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ fn test_readonly_function_call() -> Result<()> {
.map_err(|err| {
assert_eq!(
err.downcast::<VMStatus>().unwrap(),
VMStatus::Error(StatusCode::REJECTED_WRITE_SET)
VMStatus::error(StatusCode::REJECTED_WRITE_SET, None)
);
});

Expand Down
1 change: 1 addition & 0 deletions vm/starcoin-transactional-test-harness/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ move-binary-format = { workspace = true }
move-command-line-common = { workspace = true }
move-compiler = { workspace = true }
move-core-types = { workspace = true }
move-vm-types = { workspace = true }
move-transactional-test-runner = { workspace = true }
move-table-extension = { workspace = true }
once_cell = { workspace = true }
Expand Down
4 changes: 1 addition & 3 deletions vm/starcoin-transactional-test-harness/src/remote_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use anyhow::{anyhow, Result};
use jsonrpc_client_transports::RpcChannel;
use move_binary_format::errors::VMError;
use move_core_types::resolver::{ModuleResolver, ResourceResolver};
use move_vm_types::resolver::{ModuleResolver, ResourceResolver};
use starcoin_crypto::HashValue;

use move_table_extension::{TableHandle, TableResolver};
Expand Down Expand Up @@ -163,8 +163,6 @@ where
A: ModuleResolver,
B: ModuleResolver<Error = A::Error>,
{
type Error = A::Error;

fn get_module(&self, module_id: &ModuleId) -> Result<Option<Vec<u8>>, Self::Error> {
match self.a.get_module(module_id)? {
Some(d) => Ok(Some(d)),
Expand Down
4 changes: 2 additions & 2 deletions vm/vm-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ rand = { workspace = true }
rand_core = { default-features = false, workspace = true }
starcoin-logger = { workspace = true }
starcoin-frameworks = { workspace = true }
starcoin-framework = { workspace = true }
starcoin-types = { workspace = true }
starcoin-vm-types = { workspace = true }
move-stdlib = { workspace = true }
Expand All @@ -41,10 +42,9 @@ serde_bytes = { workspace = true }
rayon = { workspace = true }
num_cpus = { workspace = true }
hex = "0.4.3"
bytes = { workspace = true }
bytes = { workspace = true }
move-bytecode-verifier = { workspace = true }


[dev-dependencies]
stdlib = { workspace = true }

Expand Down
44 changes: 25 additions & 19 deletions vm/vm-runtime/src/data_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//! Scratchpad for on chain values during the execution.

use crate::create_access_path;
use anyhow::Error;
use bytes::Bytes;
use move_binary_format::deserializer::DeserializerConfig;
use move_binary_format::CompiledModule;
Expand All @@ -13,19 +12,24 @@ use move_core_types::value::MoveTypeLayout;
use move_table_extension::{TableHandle, TableResolver};
use starcoin_logger::prelude::*;
use starcoin_types::account_address::AccountAddress;
use starcoin_vm_types::state_store::state_key::StateKey;
use starcoin_vm_types::state_store::state_value::StateValue;
use starcoin_vm_types::state_store::TStateView;
use starcoin_vm_runtime_types::resource_group_adapter::ResourceGroupAdapter;
use starcoin_vm_types::state_store::{
errors::StateviewError, state_key::StateKey, state_storage_usage::StateStorageUsage,
state_value::StateValue, StateView, TStateView,
};
use starcoin_vm_types::{
access_path::AccessPath,
errors::*,
language_storage::{ModuleId, StructTag},
state_view::StateView,
vm_status::StatusCode,
write_set::{WriteOp, WriteSet},
};
use std::collections::btree_map::BTreeMap;
use std::ops::{Deref, DerefMut};
use std::{
cell::RefCell,
collections::btree_map::BTreeMap,
collections::HashSet,
ops::{Deref, DerefMut},
};

pub(crate) fn get_resource_group_from_metadata(
struct_tag: &StructTag,
Expand Down Expand Up @@ -101,7 +105,7 @@ impl<'block, S: TStateView> TStateView for StateViewCache<'block, S> {
type Key = StateKey;

// Get some data either through the cache or the `StateView` on a cache miss.
fn get_state_value(&self, state_key: &StateKey) -> anyhow::Result<Option<StateValue>> {
fn get_state_value(&self, state_key: &StateKey) -> Result<Option<StateValue>, StateviewError> {
match self.data_map.get(state_key) {
Some(opt_data) => Ok(opt_data.clone().map(|v| StateValue::from(v))),
None => match self.data_view.get_state_value(state_key) {
Expand All @@ -115,8 +119,8 @@ impl<'block, S: TStateView> TStateView for StateViewCache<'block, S> {
}
}

fn is_genesis(&self) -> bool {
self.data_view.is_genesis()
fn get_usage(&self) -> Result<StateStorageUsage, StateviewError> {
todo!()
}
}

Expand All @@ -127,7 +131,7 @@ impl<'block, S: StateView> ModuleResolver for StateViewCache<'block, S> {
RemoteStorage::new(self).get_module_metadata(module_id)
}

fn get_module(&self, module_id: &ModuleId) -> VMResult<Option<Vec<u8>>> {
fn get_module(&self, module_id: &ModuleId) -> Result<Option<Bytes>, Self::Error> {
RemoteStorage::new(self).get_module(module_id)
}
}
Expand All @@ -140,7 +144,7 @@ impl<'block, S: StateView> ResourceResolver for StateViewCache<'block, S> {
struct_tag: &StructTag,
metadata: &[Metadata],
layout: Option<&MoveTypeLayout>,
) -> Result<(Option<bytes::Bytes>, usize), Self::Error> {
) -> Result<(Option<Bytes>, usize), Self::Error> {
RemoteStorage::new(self)
.get_resource_bytes_with_metadata_and_layout(address, struct_tag, metadata, layout)
}
Expand Down Expand Up @@ -178,22 +182,23 @@ impl<'a, S: StateView> ModuleResolver for RemoteStorage<'a, S> {
compiled_module.metadata
}

fn get_module(&self, module_id: &ModuleId) -> VMResult<Option<StateValue>> {
fn get_module(&self, module_id: &ModuleId) -> Result<Option<Bytes>, Self::Error> {
// REVIEW: cache this?
let ap = AccessPath::from(module_id);
self.get(&ap).map_err(|e| e.finish(Location::Undefined))
self.get(&ap).map(|r| r.map(Bytes::from))
}
}
impl<'a, S: StateView> ResourceResolver for RemoteStorage<'a, S> {
type Error = VMError;

// TODO(simon): don't ignore metadata and layout
fn get_resource_bytes_with_metadata_and_layout(
&self,
address: &AccountAddress,
struct_tag: &StructTag,
_metadata: &[Metadata],
_layout: Option<&MoveTypeLayout>,
) -> PartialVMResult<(Option<Bytes>, usize)> {
) -> Result<(Option<Bytes>, usize), Self::Error> {
let ap = create_access_path(*address, struct_tag.clone());
let buf = self.get(&ap)?.map(Bytes::from);
let size = resource_size(&buf);
Expand Down Expand Up @@ -268,7 +273,7 @@ impl<S: StateView> ModuleResolver for RemoteStorageOwned<S> {
self.as_move_resolver().get_module_metadata(module_id)
}

fn get_module(&self, module_id: &ModuleId) -> Result<Option<Vec<u8>>, Self::Error> {
fn get_module(&self, module_id: &ModuleId) -> Result<Option<Bytes>, Self::Error> {
self.as_move_resolver().get_module(module_id)
}
}
Expand All @@ -282,8 +287,9 @@ impl<S: StateView> ResourceResolver for RemoteStorageOwned<S> {
struct_tag: &StructTag,
metadata: &[Metadata],
layout: Option<&MoveTypeLayout>,
) -> Result<(Option<bytes::bytes::Bytes>, usize), Self::Error> {
todo!()
) -> Result<(Option<Bytes>, usize), Self::Error> {
self.as_move_resolver()
.get_resource_bytes_with_metadata_and_layout(address, struct_tag, metadata, layout)
}
}

Expand All @@ -293,7 +299,7 @@ impl<S: StateView> TableResolver for RemoteStorageOwned<S> {
handle: &TableHandle,
key: &[u8],
maybe_layout: Option<&MoveTypeLayout>,
) -> Result<Option<bytes::Bytes>, PartialVMError> {
) -> Result<Option<Bytes>, PartialVMError> {
self.as_move_resolver()
.resolve_table_entry_bytes_with_layout(handle, key, maybe_layout)
}
Expand Down
10 changes: 5 additions & 5 deletions vm/vm-runtime/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ pub fn convert_prologue_runtime_error(error: VMError) -> Result<(), VMStatus> {
StatusCode::UNEXPECTED_ERROR_FROM_KNOWN_MOVE_FUNCTION
}
};
VMStatus::Error(new_major_status)
VMStatus::error(new_major_status, None)
}
status @ VMStatus::ExecutionFailure { .. } | status @ VMStatus::Error(_) => {
status @ VMStatus::ExecutionFailure { .. } | status @ VMStatus::Error { .. } => {
error!("[starcoin_vm] Unexpected prologue error: {:?}", status);
VMStatus::Error(StatusCode::UNEXPECTED_ERROR_FROM_KNOWN_MOVE_FUNCTION)
VMStatus::error(StatusCode::UNEXPECTED_ERROR_FROM_KNOWN_MOVE_FUNCTION, None)
}
})
}
Expand All @@ -127,7 +127,7 @@ pub fn convert_normal_success_epilogue_error(error: VMError) -> Result<(), VMSta
"[starcoin_vm] Unexpected success epilogue Move abort: {:?}::{:?} (Category: {:?} Reason: {:?})",
location, code, category, reason,
);
VMStatus::Error(StatusCode::UNEXPECTED_ERROR_FROM_KNOWN_MOVE_FUNCTION)
VMStatus::error(StatusCode::UNEXPECTED_ERROR_FROM_KNOWN_MOVE_FUNCTION, None)
}
}
}
Expand All @@ -139,7 +139,7 @@ pub fn convert_normal_success_epilogue_error(error: VMError) -> Result<(), VMSta
"[starcoin_vm] Unexpected success epilogue error: {:?}",
status
);
VMStatus::Error(StatusCode::UNEXPECTED_ERROR_FROM_KNOWN_MOVE_FUNCTION)
VMStatus::error(StatusCode::UNEXPECTED_ERROR_FROM_KNOWN_MOVE_FUNCTION, None)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion vm/vm-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use starcoin_vm_types::{
access_path::AccessPath,
account_address::AccountAddress,
language_storage::StructTag,
state_view::StateView,
state_store::StateView,
transaction::{Transaction, TransactionOutput},
};

Expand Down
2 changes: 1 addition & 1 deletion vm/vm-runtime/src/move_vm_ext/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
use move_binary_format::errors::PartialVMError;
use move_core_types::resolver::MoveResolver;
use move_table_extension::TableResolver;
use starcoin_vm_runtime_types::resolver::ExecutorView;
use starcoin_vm_types::on_chain_config::ConfigStorage;
use std::fmt::Debug;

/// A general resolver used by StarcoinVM. Allows to implement custom hooks on
/// top of storage, e.g. get resources from resource groups, etc.
Expand Down
12 changes: 0 additions & 12 deletions vm/vm-runtime/src/move_vm_ext/session.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
use crate::access_path_cache::AccessPathCache;
use crate::move_vm_ext::StarcoinMoveResolver;
use move_binary_format::errors::VMResult;
use move_core_types::account_address::AccountAddress;
use move_core_types::effects::{ChangeSet as MoveChangeSet, Op as MoveStorageOp};
use move_core_types::language_storage::ModuleId;
use move_core_types::vm_status::{StatusCode, VMStatus};
use move_table_extension::TableChangeSet;
use move_vm_runtime::session::Session;
use serde::{Deserialize, Serialize};
use starcoin_crypto::hash::{CryptoHash, CryptoHasher, PlainCryptoHash};
Expand All @@ -14,16 +9,9 @@ use starcoin_vm_runtime_types::{
change_set::VMChangeSet, storage::change_set_configs::ChangeSetConfigs,
};
use starcoin_vm_types::block_metadata::BlockMetadata;
use starcoin_vm_types::contract_event::ContractEvent;
use starcoin_vm_types::event::EventKey;
use starcoin_vm_types::on_chain_config::Features;
use starcoin_vm_types::state_store::state_key::StateKey;
use starcoin_vm_types::state_store::table::{TableHandle, TableInfo};
use starcoin_vm_types::transaction::SignatureCheckedTransaction;
use starcoin_vm_types::transaction_metadata::TransactionMetadata;
use starcoin_vm_types::write_set::{WriteOp, WriteSet, WriteSetMut};
use std::collections::BTreeMap;
use std::convert::TryFrom;
use std::ops::{Deref, DerefMut};
use std::sync::Arc;

Expand Down
6 changes: 3 additions & 3 deletions vm/vm-runtime/src/natives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ pub fn starcoin_natives_with_builder(builder: &mut SafeNativeBuilder) -> NativeF
CORE_CODE_ADDRESS,
builder,
))
.chain(aptos_table_natives::table_natives(
.chain(starcoin_table_natives::table_natives(
CORE_CODE_ADDRESS,
builder,
))
Expand All @@ -163,7 +163,7 @@ pub fn starcoin_natives_with_builder(builder: &mut SafeNativeBuilder) -> NativeF

pub fn assert_no_test_natives(err_msg: &str) {
assert!(
aptos_natives(
starcoin_natives(
LATEST_GAS_FEATURE_VERSION,
NativeGasParameters::zeros(),
MiscGasParameters::zeros(),
Expand Down Expand Up @@ -200,7 +200,7 @@ pub fn configure_for_unit_test() {

#[cfg(feature = "testing")]
fn unit_test_extensions_hook(exts: &mut NativeContextExtensions) {
use starcoin_table_natives::NativeTableContext;
//use starcoin_table_natives::NativeTableContext;

exts.add(NativeTableContext::new([0u8; 32], &*DUMMY_RESOLVER));
exts.add(NativeCodeContext::default());
Expand Down
5 changes: 3 additions & 2 deletions vm/vm-runtime/src/parallel_executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use starcoin_parallel_executor::{
};
use starcoin_vm_types::{
state_store::state_key::StateKey,
state_view::StateView,
state_store::StateView,
transaction::{Transaction, TransactionOutput, TransactionStatus},
write_set::{WriteOp, WriteSet},
};
Expand Down Expand Up @@ -103,8 +103,9 @@ impl ParallelStarcoinVM {
Some(err),
))
}
Err(Error::InvariantViolation) => Err(VMStatus::Error(
Err(Error::InvariantViolation) => Err(VMStatus::error(
StatusCode::UNKNOWN_INVARIANT_VIOLATION_ERROR,
None,
)),
Err(Error::UserError(err)) => Err(err),
}
Expand Down
14 changes: 7 additions & 7 deletions vm/vm-runtime/src/parallel_executor/storage_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

use crate::data_cache::{IntoMoveResolver, RemoteStorageOwned};
use starcoin_parallel_executor::executor::MVHashMapView;
use starcoin_vm_types::state_store::state_value::StateValue;
use starcoin_vm_types::state_store::TStateView;
use starcoin_vm_types::{
state_store::state_key::StateKey, state_view::StateView, write_set::WriteOp,
use starcoin_vm_types::state_store::{
errors::StateviewError, state_key::StateKey, state_storage_usage::StateStorageUsage,
state_value::StateValue, StateView, TStateView,
};
use starcoin_vm_types::write_set::WriteOp;

pub(crate) struct VersionedView<'a, S: StateView> {
base_view: &'a S,
Expand All @@ -31,7 +31,7 @@ impl<'a, S: TStateView> TStateView for VersionedView<'a, S> {
type Key = StateKey;

// Get some data either through the cache or the `StateView` on a cache miss.
fn get_state_value(&self, state_key: &StateKey) -> anyhow::Result<Option<StateValue>> {
fn get_state_value(&self, state_key: &StateKey) -> Result<Option<StateValue>, StateviewError> {
match self.hashmap_view.read(state_key) {
Some(v) => Ok(match v.as_ref() {
WriteOp::Value(w) => Some(w.clone().map(|v| StateValue::from(v))),
Expand All @@ -41,7 +41,7 @@ impl<'a, S: TStateView> TStateView for VersionedView<'a, S> {
}
}

fn is_genesis(&self) -> bool {
self.base_view.is_genesis()
fn get_usage(&self) -> Result<StateStorageUsage, StateviewError> {
todo!()
}
}
Loading

0 comments on commit c4b5fd5

Please sign in to comment.