Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[vm] StarcoinVM refactor remove mut reference for some functions #3916

Merged
merged 7 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion executor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn do_execute_block_transactions<S: StateView>(
) -> Result<Vec<TransactionOutput>> {
let mut vm = StarcoinVM::new(metrics);
let result = vm
.execute_block_transactions(chain_state, txns, block_gas_limit)?
.execute_block_transactions(&chain_state, txns, block_gas_limit)?
.into_iter()
.map(|(_, output)| {
debug! {"{:?}", output};
Expand Down
4 changes: 3 additions & 1 deletion vm/dev/src/playground.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use starcoin_resource_viewer::{AnnotatedMoveStruct, AnnotatedMoveValue, MoveValu
use starcoin_rpc_api::types::{DryRunOutputView, TransactionOutputView, WriteOpValueView};
use starcoin_state_api::StateNodeStore;
use starcoin_statedb::ChainStateDB;
use starcoin_vm_runtime::data_cache::{AsMoveResolver, StateViewCache};
use starcoin_vm_runtime::metrics::VMMetrics;
use starcoin_vm_runtime::starcoin_vm::StarcoinVM;
use starcoin_vm_types::file_format::CompiledModule;
Expand Down Expand Up @@ -97,7 +98,8 @@ pub fn dry_run<S: StateView>(
metrics: Option<VMMetrics>,
) -> Result<(VMStatus, TransactionOutput)> {
let mut vm = StarcoinVM::new(metrics);
vm.dry_run_transaction(state_view, txn)
let state_view_cache = StateViewCache::new(state_view);
vm.dry_run_transaction(&state_view_cache.as_move_resolver(), txn)
}

pub fn dry_run_explain<S: StateView>(
Expand Down
17 changes: 16 additions & 1 deletion vm/types/src/state_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ use move_core_types::{
language_storage::{ModuleId, StructTag},
};
use serde::de::DeserializeOwned;
use std::ops::Deref;

/// `StateView` is a trait that defines a read-only snapshot of the global state. It is passed to
/// the VM for transaction execution, during which the VM is guaranteed to read anything at the
/// given state.
pub trait StateView {
pub trait StateView: Sync {
/// Gets the state value for a given state key.
fn get_state_value(&self, state_key: &StateKey) -> Result<Option<Vec<u8>>>;

Expand All @@ -43,6 +44,20 @@ pub trait StateView {
fn is_genesis(&self) -> bool;
}

impl<R, S> StateView for R
where
R: Deref<Target = S> + Sync,
S: StateView,
{
fn get_state_value(&self, state_key: &StateKey) -> Result<Option<Vec<u8>>> {
self.deref().get_state_value(state_key)
}

fn is_genesis(&self) -> bool {
self.deref().is_genesis()
}
}

impl<T: ?Sized> StateReaderExt for T where T: StateView {}

pub trait StateReaderExt: StateView {
Expand Down
24 changes: 12 additions & 12 deletions vm/vm-runtime/src/data_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use starcoin_vm_types::{
access_path::AccessPath,
errors::*,
language_storage::{ModuleId, StructTag},
on_chain_config::ConfigStorage,
state_view::StateView,
vm_status::StatusCode,
write_set::{WriteOp, WriteSet},
Expand Down Expand Up @@ -136,12 +135,12 @@ impl<'a, S: StateView> ResourceResolver for RemoteStorage<'a, S> {
self.get(&ap).map_err(|e| e.finish(Location::Undefined))
}
}

impl<'a, S: StateView> ConfigStorage for RemoteStorage<'a, S> {
fn fetch_config(&self, access_path: AccessPath) -> Option<Vec<u8>> {
self.get(&access_path).ok()?
}
}
// TODO Note for Conflicting: conflicting implementation in crate `starcoin_vm_types`: - impl<V> ConfigStorage for V where V: StateView;
// impl<'a, S: StateView> ConfigStorage for RemoteStorage<'a, S> {
// fn fetch_config(&self, access_path: AccessPath) -> Option<Vec<u8>> {
// self.get(&access_path).ok()?
// }
// }

impl<'a, S> Deref for RemoteStorage<'a, S> {
type Target = S;
Expand Down Expand Up @@ -220,11 +219,12 @@ impl<S: StateView> TableResolver for RemoteStorageOwned<S> {
}
}

impl<S: StateView> ConfigStorage for RemoteStorageOwned<S> {
fn fetch_config(&self, access_path: AccessPath) -> Option<Vec<u8>> {
self.as_move_resolver().fetch_config(access_path)
}
}
// TODO Note for Conflicting: conflicting implementation in crate `starcoin_vm_types`: - impl<V> ConfigStorage for V where V: StateView;
// impl<S: StateView> ConfigStorage for RemoteStorageOwned<S> {
nkysg marked this conversation as resolved.
Show resolved Hide resolved
// fn fetch_config(&self, access_path: AccessPath) -> Option<Vec<u8>> {
// self.as_move_resolver().fetch_config(access_path)
// }
// }

pub trait IntoMoveResolver<S> {
fn into_move_resolver(self) -> RemoteStorageOwned<S>;
Expand Down
Loading