From c11a9a297496f5148675d1a9f187c720e145be1b Mon Sep 17 00:00:00 2001 From: Mikhail Zabaluev Date: Fri, 3 May 2024 16:39:21 -0700 Subject: [PATCH] aptos-vm: feature gate for sharded execution Put sharded block execution code and its associated rayon dependency behind the "sharded" feature. To avoid conditionally defined methods in the VMExecutor trait, factor out the SerialVMExecutor trait that is a supertrait of VMExecutor, gate VMExecutor with the "sharded" feature, and move the non-sharded methods to the unconditional supertrait. --- .../src/aptos_test_harness.rs | 2 +- aptos-move/aptos-vm/Cargo.toml | 5 +++-- aptos-move/aptos-vm/src/aptos_vm.rs | 22 +++++++++++++------ aptos-move/aptos-vm/src/lib.rs | 21 +++++++++++++----- 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/aptos-move/aptos-transactional-test-harness/src/aptos_test_harness.rs b/aptos-move/aptos-transactional-test-harness/src/aptos_test_harness.rs index bd753e64e7f33..5435272a7c442 100644 --- a/aptos-move/aptos-transactional-test-harness/src/aptos_test_harness.rs +++ b/aptos-move/aptos-transactional-test-harness/src/aptos_test_harness.rs @@ -26,7 +26,7 @@ use aptos_types::{ Script as TransactionScript, Transaction, TransactionOutput, TransactionStatus, }, }; -use aptos_vm::{data_cache::AsMoveResolver, AptosVM, VMExecutor}; +use aptos_vm::{data_cache::AsMoveResolver, AptosVM, SerialVMExecutor}; use aptos_vm_genesis::GENESIS_KEYPAIR; use clap::Parser; use move_binary_format::file_format::{CompiledModule, CompiledScript}; diff --git a/aptos-move/aptos-vm/Cargo.toml b/aptos-move/aptos-vm/Cargo.toml index 4949892ed9324..64f3d16d042d0 100644 --- a/aptos-move/aptos-vm/Cargo.toml +++ b/aptos-move/aptos-vm/Cargo.toml @@ -59,7 +59,7 @@ num_cpus = { workspace = true } once_cell = { workspace = true } ouroboros = { workspace = true } rand = { workspace = true } -rayon = { workspace = true } +rayon = { workspace = true, optional = true } serde = { workspace = true } serde_json = { workspace = true } smallvec = { workspace = true } @@ -74,7 +74,8 @@ proptest = { workspace = true } rand_core = { workspace = true } [features] -default = [] +default = ["sharded"] +sharded = ["rayon"] fuzzing = ["move-core-types/fuzzing", "move-binary-format/fuzzing", "move-vm-types/fuzzing", "aptos-framework/fuzzing", "aptos-types/fuzzing"] failpoints = ["fail/failpoints", "move-vm-runtime/failpoints"] testing = ["move-unit-test", "aptos-framework/testing"] diff --git a/aptos-move/aptos-vm/src/aptos_vm.rs b/aptos-move/aptos-vm/src/aptos_vm.rs index ff2b7c805fec8..644b6ad67f03a 100644 --- a/aptos-move/aptos-vm/src/aptos_vm.rs +++ b/aptos-move/aptos-vm/src/aptos_vm.rs @@ -17,12 +17,16 @@ use crate::{ }, AptosMoveResolver, MoveVmExt, SessionExt, SessionId, }, - sharded_block_executor::{executor_client::ExecutorClient, ShardedBlockExecutor}, system_module_names::*, transaction_metadata::TransactionMetadata, transaction_validation, verifier, verifier::randomness::has_randomness_attribute, - VMExecutor, VMValidator, + SerialVMExecutor, VMValidator, +}; +#[cfg(feature = "sharded")] +use crate::{ + sharded_block_executor::{executor_client::ExecutorClient, ShardedBlockExecutor}, + VMExecutor, }; use anyhow::anyhow; use aptos_block_executor::txn_commit_hook::NoOpTransactionCommitHook; @@ -42,9 +46,8 @@ use aptos_types::state_store::StateViewId; use aptos_types::{ account_config, account_config::{new_block_event_key, AccountResource}, - block_executor::{ - config::{BlockExecutorConfig, BlockExecutorConfigFromOnchain, BlockExecutorLocalConfig}, - partitioner::PartitionedTransactions, + block_executor::config::{ + BlockExecutorConfig, BlockExecutorConfigFromOnchain, BlockExecutorLocalConfig, }, block_metadata::BlockMetadata, block_metadata_ext::{BlockMetadataExt, BlockMetadataWithRandomness}, @@ -56,7 +59,7 @@ use aptos_types::{ TimedFeatureOverride, TimedFeatures, TimedFeaturesBuilder, }, randomness::Randomness, - state_store::{StateView, TStateView}, + state_store::StateView, transaction::{ authenticator::AnySignature, signature_verified_transaction::SignatureVerifiedTransaction, BlockOutput, EntryFunction, ExecutionError, ExecutionStatus, ModuleBundle, Multisig, @@ -66,6 +69,8 @@ use aptos_types::{ }, vm_status::{AbortLocation, StatusCode, VMStatus}, }; +#[cfg(feature = "sharded")] +use aptos_types::{block_executor::partitioner::PartitionedTransactions, state_store::TStateView}; use aptos_utils::{aptos_try, return_on_failure}; use aptos_vm_logging::{log_schema::AdapterLogSchema, speculative_error, speculative_log}; use aptos_vm_types::{ @@ -2263,7 +2268,7 @@ impl AptosVM { } // Executor external API -impl VMExecutor for AptosVM { +impl SerialVMExecutor for AptosVM { /// Execute a block of `transactions`. The output vector will have the exact same length as the /// input vector. The discarded transactions will be marked as `TransactionStatus::Discard` and /// have an empty `WriteSet`. Also `state_view` is immutable, and does not have interior @@ -2310,7 +2315,10 @@ impl VMExecutor for AptosVM { } ret } +} +#[cfg(feature = "sharded")] +impl VMExecutor for AptosVM { fn execute_block_sharded>( sharded_block_executor: &ShardedBlockExecutor, transactions: PartitionedTransactions, diff --git a/aptos-move/aptos-vm/src/lib.rs b/aptos-move/aptos-vm/src/lib.rs index 7709bf9e21e6f..919e55fb0e451 100644 --- a/aptos-move/aptos-vm/src/lib.rs +++ b/aptos-move/aptos-vm/src/lib.rs @@ -112,6 +112,7 @@ pub mod gas; mod keyless_validation; pub mod move_vm_ext; pub mod natives; +#[cfg(feature = "sharded")] pub mod sharded_block_executor; pub mod system_module_names; pub mod testing; @@ -121,11 +122,12 @@ pub mod validator_txns; pub mod verifier; pub use crate::aptos_vm::{AptosSimulationVM, AptosVM}; +#[cfg(feature = "sharded")] use crate::sharded_block_executor::{executor_client::ExecutorClient, ShardedBlockExecutor}; +#[cfg(feature = "sharded")] +use aptos_types::block_executor::partitioner::PartitionedTransactions; use aptos_types::{ - block_executor::{ - config::BlockExecutorConfigFromOnchain, partitioner::PartitionedTransactions, - }, + block_executor::config::BlockExecutorConfigFromOnchain, state_store::StateView, transaction::{ signature_verified_transaction::SignatureVerifiedTransaction, BlockOutput, @@ -133,7 +135,9 @@ use aptos_types::{ }, vm_status::VMStatus, }; -use std::{marker::Sync, sync::Arc}; +use std::marker::Sync; +#[cfg(feature = "sharded")] +use std::sync::Arc; pub use verifier::view_function::determine_is_view; /// This trait describes the VM's validation interfaces. @@ -146,8 +150,9 @@ pub trait VMValidator { ) -> VMValidatorResult; } -/// This trait describes the VM's execution interface. -pub trait VMExecutor: Send + Sync { +/// This trait describes the VM's execution interface, without the sharded +/// execution method which is only available when the `sharded` feature is enabled. +pub trait SerialVMExecutor: Send + Sync { // NOTE: At the moment there are no persistent caches that live past the end of a block (that's // why execute_block doesn't take &self.) // There are some cache invalidation issues around transactions publishing code that need to be @@ -173,7 +178,11 @@ pub trait VMExecutor: Send + Sync { ) .map(BlockOutput::into_transaction_outputs_forced) } +} +/// This trait augments `SerialVMExecutor` with sharded execution capabilities. +#[cfg(feature = "sharded")] +pub trait VMExecutor: SerialVMExecutor { /// Executes a block of transactions using a sharded block executor and returns the results. fn execute_block_sharded>( sharded_block_executor: &ShardedBlockExecutor,