Skip to content

Commit

Permalink
Generic StateView, Unify views, remove unnecessary crust
Browse files Browse the repository at this point in the history
  • Loading branch information
gelash committed Dec 4, 2022
1 parent 2487961 commit 2acca0f
Show file tree
Hide file tree
Showing 38 changed files with 638 additions and 642 deletions.
1 change: 1 addition & 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 aptos-move/aptos-aggregator/src/aggregator_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ mod test {
}
}

impl StateView for FakeTestStorage {
impl StateView<StateKey> for FakeTestStorage {
fn get_state_value(&self, state_key: &StateKey) -> anyhow::Result<Option<Vec<u8>>> {
Ok(self.data.get(state_key).cloned())
}
Expand Down
10 changes: 5 additions & 5 deletions aptos-move/aptos-aggregator/src/delta_change_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ impl DeltaOp {
/// Consumes a single delta and tries to materialize it with a given state
/// key. If materialization succeeds, a write op is produced. Otherwise, an
/// error VM status is returned.
pub fn try_into_write_op(
pub fn try_into_write_op<S: StateView<StateKey> + ?Sized>(
self,
state_view: &impl StateView,
state_view: &S,
state_key: &StateKey,
) -> anyhow::Result<WriteOp, VMStatus> {
state_view
Expand Down Expand Up @@ -316,9 +316,9 @@ impl DeltaChangeSet {
/// Consumes the delta change set and tries to materialize it. Returns a
/// mutable write set if materialization succeeds (mutability since we want
/// to merge these writes with transaction outputs).
pub fn try_into_write_set_mut(
pub fn try_into_write_set_mut<S: StateView<StateKey> + ?Sized>(
self,
state_view: &impl StateView,
state_view: &S,
) -> anyhow::Result<WriteSetMut, VMStatus> {
let mut materialized_write_set = vec![];
for (state_key, delta_op) in self.delta_change_set {
Expand Down Expand Up @@ -537,7 +537,7 @@ mod tests {
data: HashMap<StateKey, Vec<u8>>,
}

impl StateView for FakeView {
impl StateView<StateKey> for FakeView {
fn get_state_value(&self, state_key: &StateKey) -> anyhow::Result<Option<Vec<u8>>> {
Ok(self.data.get(state_key).cloned())
}
Expand Down
6 changes: 5 additions & 1 deletion aptos-move/aptos-aggregator/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::delta_change_set::{deserialize, DeltaChangeSet};
use anyhow::bail;
use aptos_state_view::StateView;
use aptos_types::{
state_store::state_key::StateKey,
transaction::{ChangeSet, TransactionOutput},
write_set::{TransactionWrite, WriteOp, WriteSet, WriteSetMut},
};
Expand Down Expand Up @@ -193,7 +194,10 @@ impl TransactionOutputExt {
/// TODO: ideally, we may want to expose this function to VM instead. Since
/// we do not care about rerunning the epilogue - it sufficies to have it
/// here for now.
pub fn into_transaction_output(self, state_view: &impl StateView) -> TransactionOutput {
pub fn into_transaction_output<S: StateView<StateKey> + ?Sized>(
self,
state_view: &S,
) -> TransactionOutput {
let (delta_change_set, txn_output) = self.into();

// First, check if output of transaction should be discarded or delta
Expand Down
4 changes: 2 additions & 2 deletions aptos-move/aptos-validator-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ pub use crate::storage_interface::DBDebuggerInterface;

use anyhow::{anyhow, Result};
use aptos_state_view::StateView;
use aptos_types::state_store::state_storage_usage::StateStorageUsage;
use aptos_types::{
account_address::AccountAddress,
account_config::CORE_CODE_ADDRESS,
account_state::AccountState,
account_view::AccountView,
on_chain_config::ValidatorSet,
state_store::state_storage_usage::StateStorageUsage,
state_store::{state_key::StateKey, state_value::StateValue},
transaction::{Transaction, Version},
};
Expand Down Expand Up @@ -170,7 +170,7 @@ impl DebuggerStateView {
}
}

impl StateView for DebuggerStateView {
impl StateView<StateKey> for DebuggerStateView {
fn get_state_value(&self, state_key: &StateKey) -> Result<Option<Vec<u8>>> {
self.get_state_value_internal(state_key, self.version)
}
Expand Down
23 changes: 10 additions & 13 deletions aptos-move/aptos-vm/src/adapter_common.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
// Copyright (c) Aptos
// SPDX-License-Identifier: Apache-2.0

use crate::{counters::*, data_cache::StateViewCache};
use anyhow::Result;
use aptos_aggregator::transaction::TransactionOutputExt;
use aptos_state_view::StateView;
use aptos_types::{
transaction::{SignatureCheckedTransaction, SignedTransaction, VMValidatorResult},
vm_status::{StatusCode, VMStatus},
};

use crate::{
counters::*,
data_cache::AsMoveResolver,
logging::AdapterLogSchema,
move_vm_ext::{MoveResolverExt, SessionExt, SessionId},
};
use anyhow::Result;
use aptos_aggregator::transaction::TransactionOutputExt;
use aptos_state_view::StateView;
use aptos_types::{
block_metadata::BlockMetadata,
state_store::state_key::StateKey,
transaction::{SignatureCheckedTransaction, SignedTransaction, VMValidatorResult},
transaction::{Transaction, TransactionOutput, TransactionStatus, WriteSetPayload},
vm_status::{StatusCode, VMStatus},
write_set::WriteSet,
};

Expand Down Expand Up @@ -53,7 +51,7 @@ pub trait VMAdapter {
fn should_restart_execution(output: &TransactionOutput) -> bool;

/// Execute a single transaction.
fn execute_single_transaction<S: MoveResolverExt + StateView>(
fn execute_single_transaction<S: MoveResolverExt + StateView<StateKey>>(
&self,
txn: &PreprocessedTransaction,
data_cache: &S,
Expand All @@ -70,7 +68,7 @@ pub trait VMAdapter {
pub fn validate_signed_transaction<A: VMAdapter>(
adapter: &A,
transaction: SignedTransaction,
state_view: &impl StateView,
state_view: &impl StateView<StateKey>,
) -> VMValidatorResult {
let _timer = TXN_VALIDATION_SECONDS.start_timer();
let log_context = AdapterLogSchema::new(state_view.id(), 0);
Expand All @@ -81,8 +79,7 @@ pub fn validate_signed_transaction<A: VMAdapter>(
}
};

let remote_cache = StateViewCache::new(state_view);
let resolver = remote_cache.as_move_resolver();
let resolver = state_view.as_move_resolver();
let mut session = adapter.new_session(&resolver, SessionId::txn(&txn));

let validation_result = validate_signature_checked_transaction(
Expand Down
27 changes: 14 additions & 13 deletions aptos-move/aptos-vm/src/aptos_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use aptos_types::{
account_config::new_block_event_key,
block_metadata::BlockMetadata,
on_chain_config::new_epoch_event_key,
state_store::state_key::StateKey,
transaction::{
ChangeSet, ExecutionStatus, ModuleBundle, SignatureCheckedTransaction, SignedTransaction,
Transaction, TransactionOutput, TransactionPayload, TransactionStatus, VMValidatorResult,
Expand Down Expand Up @@ -87,11 +88,11 @@ pub struct AptosVM(pub(crate) AptosVMImpl);
struct AptosSimulationVM(AptosVM);

impl AptosVM {
pub fn new<S: StateView>(state: &S) -> Self {
pub fn new<S: StateView<StateKey>>(state: &S) -> Self {
Self(AptosVMImpl::new(state))
}

pub fn new_for_validation<S: StateView>(state: &S) -> Self {
pub fn new_for_validation<S: StateView<StateKey>>(state: &S) -> Self {
info!(
AdapterLogSchema::new(state.id(), 0),
"Adapter created for Validation"
Expand Down Expand Up @@ -262,7 +263,7 @@ impl AptosVM {
}
}

fn success_transaction_cleanup<S: MoveResolverExt + StateView>(
fn success_transaction_cleanup<S: MoveResolverExt + StateView<StateKey>>(
&self,
storage: &S,
user_txn_change_set_ext: ChangeSetExt,
Expand Down Expand Up @@ -327,7 +328,7 @@ impl AptosVM {
))
}

fn execute_script_or_entry_function<S: MoveResolverExt + StateView>(
fn execute_script_or_entry_function<S: MoveResolverExt + StateView<StateKey>>(
&self,
storage: &S,
mut session: SessionExt<S>,
Expand Down Expand Up @@ -504,7 +505,7 @@ impl AptosVM {
/// Execute a module bundle load request.
/// TODO: this is going to be deprecated and removed in favor of code publishing via
/// NativeCodeContext
fn execute_modules<S: MoveResolverExt + StateView>(
fn execute_modules<S: MoveResolverExt + StateView<StateKey>>(
&self,
storage: &S,
mut session: SessionExt<S>,
Expand Down Expand Up @@ -652,7 +653,7 @@ impl AptosVM {
.finish(Location::Undefined)
}

pub(crate) fn execute_user_transaction<S: MoveResolverExt + StateView>(
pub(crate) fn execute_user_transaction<S: MoveResolverExt + StateView<StateKey>>(
&self,
storage: &S,
txn: &SignatureCheckedTransaction,
Expand Down Expand Up @@ -797,7 +798,7 @@ impl AptosVM {

fn read_writeset(
&self,
state_view: &impl StateView,
state_view: &impl StateView<StateKey>,
write_set: &WriteSet,
) -> Result<(), VMStatus> {
// All Move executions satisfy the read-before-write property. Thus we need to read each
Expand Down Expand Up @@ -833,7 +834,7 @@ impl AptosVM {
}
}

pub(crate) fn process_waypoint_change_set<S: MoveResolverExt + StateView>(
pub(crate) fn process_waypoint_change_set<S: MoveResolverExt + StateView<StateKey>>(
&self,
storage: &S,
writeset_payload: WriteSetPayload,
Expand Down Expand Up @@ -914,7 +915,7 @@ impl AptosVM {

pub fn simulate_signed_transaction(
txn: &SignedTransaction,
state_view: &impl StateView,
state_view: &impl StateView<StateKey>,
) -> (VMStatus, TransactionOutputExt) {
let vm = AptosVM::new(state_view);
let simulation_vm = AptosSimulationVM(vm);
Expand Down Expand Up @@ -960,7 +961,7 @@ impl VMExecutor for AptosVM {
/// transaction output.
fn execute_block(
transactions: Vec<Transaction>,
state_view: &impl StateView,
state_view: &impl StateView<StateKey>,
) -> Result<Vec<TransactionOutput>, VMStatus> {
fail_point!("move_adapter::execute_block", |_| {
Err(VMStatus::Error(
Expand Down Expand Up @@ -1002,7 +1003,7 @@ impl VMValidator for AptosVM {
fn validate_transaction(
&self,
transaction: SignedTransaction,
state_view: &impl StateView,
state_view: &impl StateView<StateKey>,
) -> VMValidatorResult {
validate_signed_transaction(self, transaction, state_view)
}
Expand Down Expand Up @@ -1055,7 +1056,7 @@ impl VMAdapter for AptosVM {
.any(|event| *event.key() == new_epoch_event_key)
}

fn execute_single_transaction<S: MoveResolverExt + StateView>(
fn execute_single_transaction<S: MoveResolverExt + StateView<StateKey>>(
&self,
txn: &PreprocessedTransaction,
data_cache: &S,
Expand Down Expand Up @@ -1161,7 +1162,7 @@ impl AptosSimulationVM {
/*
Executes a SignedTransaction without performing signature verification
*/
fn simulate_signed_transaction<S: MoveResolverExt + StateView>(
fn simulate_signed_transaction<S: MoveResolverExt + StateView<StateKey>>(
&self,
storage: &S,
txn: &SignedTransaction,
Expand Down
11 changes: 6 additions & 5 deletions aptos-move/aptos-vm/src/aptos_vm_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ use aptos_gas::{
};
use aptos_logger::prelude::*;
use aptos_state_view::StateView;
use aptos_types::chain_id::ChainId;
use aptos_types::on_chain_config::{FeatureFlag, Features};
use aptos_types::transaction::AbortInfo;
use aptos_types::{
account_config::{TransactionValidation, APTOS_TRANSACTION_VALIDATION, CORE_CODE_ADDRESS},
chain_id::ChainId,
on_chain_config::{
ApprovedExecutionHashes, GasSchedule, GasScheduleV2, OnChainConfig, StorageGasSchedule,
Version,
},
on_chain_config::{FeatureFlag, Features},
state_store::state_key::StateKey,
transaction::AbortInfo,
transaction::{ExecutionStatus, TransactionOutput, TransactionStatus},
vm_status::{StatusCode, VMStatus},
};
Expand Down Expand Up @@ -59,7 +60,7 @@ pub struct AptosVMImpl {

impl AptosVMImpl {
#[allow(clippy::new_without_default)]
pub fn new<S: StateView>(state: &S) -> Self {
pub fn new<S: StateView<StateKey>>(state: &S) -> Self {
let storage = StorageAdapter::new(state);

// Get the gas parameters
Expand Down Expand Up @@ -574,7 +575,7 @@ impl<'a> AptosVMInternals<'a> {
/// The `TransactionDataCache` can be used as a `ChainState`.
///
/// If you don't care about the transaction metadata, use `TransactionMetadata::default()`.
pub fn with_txn_data_cache<T, S: StateView>(
pub fn with_txn_data_cache<T, S: StateView<StateKey>>(
self,
state_view: &S,
f: impl for<'txn, 'r> FnOnce(SessionExt<'txn, 'r, StorageAdapter<S>>) -> T,
Expand Down
16 changes: 8 additions & 8 deletions aptos-move/aptos-vm/src/block_executor/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Aptos
// SPDX-License-Identifier: Apache-2.0

mod storage_wrapper;
pub(crate) mod vm_wrapper;

use crate::{
Expand All @@ -13,11 +12,12 @@ use aptos_aggregator::{delta_change_set::DeltaOp, transaction::TransactionOutput
use aptos_block_executor::{
errors::Error,
executor::{BlockExecutor, RAYON_EXEC_POOL},
output_delta_resolver::{OutputDeltaResolver, ResolvedData},
output_delta_resolver::OutputDeltaResolver,
task::{
Transaction as BlockExecutorTransaction,
TransactionOutput as BlockExecutorTransactionOutput,
},
view::ResolvedData,
};
use aptos_logger::debug;
use aptos_state_view::StateView;
Expand Down Expand Up @@ -53,7 +53,7 @@ impl AptosTransactionOutput {
}

impl BlockExecutorTransactionOutput for AptosTransactionOutput {
type T = PreprocessedTransaction;
type Txn = PreprocessedTransaction;

fn get_writes(&self) -> Vec<(StateKey, WriteOp)> {
self.0
Expand Down Expand Up @@ -86,7 +86,7 @@ impl BlockExecutorTransactionOutput for AptosTransactionOutput {
pub struct BlockAptosVM();

impl BlockAptosVM {
fn process_parallel_block_output<S: StateView>(
fn process_parallel_block_output<S: StateView<StateKey>>(
results: Vec<AptosTransactionOutput>,
delta_resolver: OutputDeltaResolver<StateKey, WriteOp>,
state_view: &S,
Expand Down Expand Up @@ -128,7 +128,7 @@ impl BlockAptosVM {
.collect()
}

pub fn execute_block<S: StateView>(
pub fn execute_block<S: StateView<StateKey>>(
transactions: Vec<Transaction>,
state_view: &S,
concurrency_level: usize,
Expand All @@ -149,21 +149,21 @@ impl BlockAptosVM {

let mut ret = if concurrency_level > 1 {
executor
.execute_transactions_parallel(state_view, &signature_verified_block)
.execute_transactions_parallel(state_view, &signature_verified_block, &state_view)
.map(|(results, delta_resolver)| {
Self::process_parallel_block_output(results, delta_resolver, state_view)
})
} else {
executor
.execute_transactions_sequential(state_view, &signature_verified_block)
.execute_transactions_sequential(state_view, &signature_verified_block, &state_view)
.map(Self::process_sequential_block_output)
};

if ret == Err(Error::ModulePathReadWrite) {
debug!("[Execution]: Module read & written, sequential fallback");

ret = executor
.execute_transactions_sequential(state_view, &signature_verified_block)
.execute_transactions_sequential(state_view, &signature_verified_block, &state_view)
.map(Self::process_sequential_block_output);
}

Expand Down

0 comments on commit 2acca0f

Please sign in to comment.