Skip to content

Commit

Permalink
LatestState, LatestStateSummary
Browse files Browse the repository at this point in the history
  • Loading branch information
msmouse committed Dec 3, 2024
1 parent b7bcef3 commit 5d8e1ac
Show file tree
Hide file tree
Showing 25 changed files with 346 additions and 260 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.

28 changes: 9 additions & 19 deletions execution/executor-types/src/execution_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
};
use aptos_drop_helper::DropHelper;
use aptos_storage_interface::state_store::{
state::State, state_view::cached_state_view::ShardedStateCache,
state::LatestState, state_view::cached_state_view::ShardedStateCache,
};
use aptos_types::{
contract_event::ContractEvent,
Expand All @@ -36,23 +36,18 @@ impl ExecutionOutput {
to_commit: TransactionsToKeep,
to_discard: TransactionsWithOutput,
to_retry: TransactionsWithOutput,
last_checkpoint_state: Option<State>,
result_state: State,
result_state: LatestState,
state_reads: ShardedStateCache,
block_end_info: Option<BlockEndInfo>,
next_epoch_state: Option<EpochState>,
subscribable_events: Planned<Vec<ContractEvent>>,
) -> Self {
let next_version = first_version + to_commit.len() as Version;
assert_eq!(next_version, result_state.next_version());
assert_eq!(next_version, result_state.state().next_version());
if is_block {
// If it's a block, ensure it ends with state checkpoint.
assert!(to_commit.is_empty() || to_commit.ends_with_sole_checkpoint());
assert!(last_checkpoint_state.is_some());
assert!(last_checkpoint_state
.as_ref()
.unwrap()
.is_the_same(&result_state));
assert!(result_state.is_checkpoint());
} else {
// If it's not, there shouldn't be any transaction to be discarded or retried.
assert!(to_discard.is_empty() && to_retry.is_empty());
Expand All @@ -65,7 +60,6 @@ impl ExecutionOutput {
to_commit,
to_discard,
to_retry,
last_checkpoint_state,
result_state,
state_reads,
block_end_info,
Expand All @@ -74,16 +68,15 @@ impl ExecutionOutput {
})
}

pub fn new_empty(parent_state: State) -> Self {
pub fn new_empty(state: LatestState) -> Self {
Self::new_impl(Inner {
is_block: false,
first_version: parent_state.next_version(),
first_version: state.next_version(),
statuses_for_input_txns: vec![],
to_commit: TransactionsToKeep::new_empty(),
to_discard: TransactionsWithOutput::new_empty(),
to_retry: TransactionsWithOutput::new_empty(),
last_checkpoint_state: None,
result_state: parent_state,
result_state: state,
state_reads: ShardedStateCache::default(),
block_end_info: None,
next_epoch_state: None,
Expand All @@ -101,8 +94,7 @@ impl ExecutionOutput {
to_commit: TransactionsToKeep::new_dummy_success(txns),
to_discard: TransactionsWithOutput::new_empty(),
to_retry: TransactionsWithOutput::new_empty(),
last_checkpoint_state: None,
result_state: State::new_empty(),
result_state: LatestState::new_empty(),
state_reads: ShardedStateCache::default(),
block_end_info: None,
next_epoch_state: None,
Expand All @@ -122,7 +114,6 @@ impl ExecutionOutput {
to_commit: TransactionsToKeep::new_empty(),
to_discard: TransactionsWithOutput::new_empty(),
to_retry: TransactionsWithOutput::new_empty(),
last_checkpoint_state: None,
result_state: self.result_state.clone(),
state_reads: ShardedStateCache::default(),
block_end_info: None,
Expand Down Expand Up @@ -163,8 +154,7 @@ pub struct Inner {
pub to_discard: TransactionsWithOutput,
pub to_retry: TransactionsWithOutput,

pub last_checkpoint_state: Option<State>,
pub result_state: State,
pub result_state: LatestState,
/// State items read during execution, useful for calculating the state storge usage and
/// indices used by the db pruner.
pub state_reads: ShardedStateCache,
Expand Down
20 changes: 8 additions & 12 deletions execution/executor-types/src/state_checkpoint_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use aptos_crypto::HashValue;
use aptos_drop_helper::DropHelper;
use aptos_storage_interface::state_store::state_summary::StateSummary;
use aptos_storage_interface::state_store::state_summary::LatestStateSummary;
use derive_more::Deref;
use std::sync::Arc;

Expand All @@ -17,27 +17,24 @@ pub struct StateCheckpointOutput {

impl StateCheckpointOutput {
pub fn new(
last_state_checkpoint_summary: Option<StateSummary>,
result_state_summary: StateSummary,
state_summary: LatestStateSummary,
state_checkpoint_hashes: Vec<Option<HashValue>>,
) -> Self {
Self::new_impl(Inner {
last_state_checkpoint_summary,
result_state_summary,
state_summary,
state_checkpoint_hashes,
})
}

pub fn new_empty(parent_state_summary: StateSummary) -> Self {
pub fn new_empty(parent_state_summary: LatestStateSummary) -> Self {
Self::new_impl(Inner {
last_state_checkpoint_summary: None,
result_state_summary: parent_state_summary,
state_summary: parent_state_summary,
state_checkpoint_hashes: vec![],
})
}

pub fn new_dummy() -> Self {
Self::new_empty(StateSummary::new_empty())
Self::new_empty(LatestStateSummary::new_empty())
}

fn new_impl(inner: Inner) -> Self {
Expand All @@ -47,13 +44,12 @@ impl StateCheckpointOutput {
}

pub fn reconfig_suffix(&self) -> Self {
Self::new_empty(self.result_state_summary.clone())
Self::new_empty(self.state_summary.clone())
}
}

#[derive(Debug)]
pub struct Inner {
pub last_state_checkpoint_summary: Option<StateSummary>,
pub result_state_summary: StateSummary,
pub state_summary: LatestStateSummary,
pub state_checkpoint_hashes: Vec<Option<HashValue>>,
}
5 changes: 3 additions & 2 deletions execution/executor/src/block_executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ where
CachedStateView::new(
StateViewId::BlockExecution { block_id },
Arc::clone(&self.db.reader),
parent_output.execution_output.result_state.clone(),
parent_output.execution_output.result_state.state().clone(),
)?
};

Expand All @@ -229,6 +229,7 @@ where
DoGetExecutionOutput::by_transaction_execution(
&self.block_executor,
transactions,
&parent_output.execution_output.result_state,
state_view,
onchain_config.clone(),
TransactionSliceMetadata::block(parent_block_id, block_id),
Expand Down Expand Up @@ -293,7 +294,7 @@ where
});
output.set_state_checkpoint_output(DoStateCheckpoint::run(
&output.execution_output,
parent_block.output.expect_result_state_summary().clone(),
parent_block.output.expect_result_state_summary(),
Option::<Vec<_>>::None,
)?);
output.set_ledger_update_output(DoLedgerUpdate::run(
Expand Down
12 changes: 6 additions & 6 deletions execution/executor/src/chunk_executor/chunk_commit_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
use anyhow::{anyhow, ensure, Result};
use aptos_metrics_core::TimerHelper;
use aptos_storage_interface::{
state_store::{state::State, state_summary::StateSummary},
state_store::{state::LatestState, state_summary::LatestStateSummary},
DbReader, LedgerSummary,
};
use aptos_types::{proof::accumulator::InMemoryTransactionAccumulator, transaction::Version};
Expand All @@ -39,8 +39,8 @@ pub(crate) struct ChunkToUpdateLedger {
///
pub struct ChunkCommitQueue {
/// Notice that latest_state and latest_txn_accumulator are at different versions.
latest_state: State,
latest_state_summary: StateSummary,
latest_state: LatestState,
latest_state_summary: LatestStateSummary,
latest_txn_accumulator: Arc<InMemoryTransactionAccumulator>,
to_commit: VecDeque<Option<ExecutedChunk>>,
to_update_ledger: VecDeque<Option<ChunkToUpdateLedger>>,
Expand All @@ -63,7 +63,7 @@ impl ChunkCommitQueue {
})
}

pub(crate) fn latest_state(&self) -> &State {
pub(crate) fn latest_state(&self) -> &LatestState {
&self.latest_state
}

Expand Down Expand Up @@ -91,7 +91,7 @@ impl ChunkCommitQueue {
pub(crate) fn next_chunk_to_update_ledger(
&mut self,
) -> Result<(
StateSummary,
LatestStateSummary,
Arc<InMemoryTransactionAccumulator>,
ChunkToUpdateLedger,
)> {
Expand Down Expand Up @@ -125,7 +125,7 @@ impl ChunkCommitQueue {
self.latest_state_summary = chunk
.output
.expect_state_checkpoint_output()
.result_state_summary
.state_summary
.clone();
self.latest_txn_accumulator = chunk
.output
Expand Down
10 changes: 6 additions & 4 deletions execution/executor/src/chunk_executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ impl<V: VMBlockExecutor> ChunkExecutorInner<V> {

let num_txns = chunk.len();

let state_view = self.state_view(&parent_state)?;
let execution_output = chunk.into_output::<V>(state_view)?;
let state_view = self.state_view(parent_state.state())?;
let execution_output = chunk.into_output::<V>(&parent_state, state_view)?;
let output = PartialStateComputeResult::new(execution_output);

// Enqueue for next stage.
Expand Down Expand Up @@ -336,7 +336,7 @@ impl<V: VMBlockExecutor> ChunkExecutorInner<V> {

output.set_state_checkpoint_output(DoStateCheckpoint::run(
&output.execution_output,
parent_state_summary,
&parent_state_summary,
Some(
chunk_verifier
.transaction_infos()
Expand Down Expand Up @@ -580,7 +580,8 @@ impl<V: VMBlockExecutor> ChunkExecutorInner<V> {
verify_execution_mode: &VerifyExecutionMode,
) -> Result<Version> {
// Execute transactions.
let state_view = self.state_view(self.commit_queue.lock().latest_state())?;
let parent_state = self.commit_queue.lock().latest_state().clone();
let state_view = self.state_view(parent_state.state())?;
let txns = transactions
.iter()
.take((end_version - begin_version) as usize)
Expand All @@ -592,6 +593,7 @@ impl<V: VMBlockExecutor> ChunkExecutorInner<V> {
let execution_output = DoGetExecutionOutput::by_transaction_execution::<V>(
&V::new(),
txns.into(),
&parent_state,
state_view,
BlockExecutorConfigFromOnchain::new_no_block_limit(),
TransactionSliceMetadata::chunk(begin_version, end_version),
Expand Down
15 changes: 13 additions & 2 deletions execution/executor/src/chunk_executor/transaction_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use anyhow::Result;
use aptos_executor_types::execution_output::ExecutionOutput;
use aptos_experimental_runtimes::thread_manager::optimal_min_len;
use aptos_metrics_core::TimerHelper;
use aptos_storage_interface::state_store::state_view::cached_state_view::CachedStateView;
use aptos_storage_interface::state_store::{
state::LatestState, state_view::cached_state_view::CachedStateView,
};
use aptos_types::{
block_executor::{
config::BlockExecutorConfigFromOnchain,
Expand Down Expand Up @@ -43,6 +45,7 @@ pub trait TransactionChunk {

fn into_output<V: VMBlockExecutor>(
self,
parent_state: &LatestState,
state_view: CachedStateView,
) -> Result<ExecutionOutput>;
}
Expand All @@ -63,6 +66,7 @@ impl TransactionChunk for ChunkToExecute {

fn into_output<V: VMBlockExecutor>(
self,
parent_state: &LatestState,
state_view: CachedStateView,
) -> Result<ExecutionOutput> {
let ChunkToExecute {
Expand All @@ -89,6 +93,7 @@ impl TransactionChunk for ChunkToExecute {
DoGetExecutionOutput::by_transaction_execution::<V>(
&V::new(),
sig_verified_txns.into(),
parent_state,
state_view,
BlockExecutorConfigFromOnchain::new_no_block_limit(),
TransactionSliceMetadata::unknown(),
Expand All @@ -113,6 +118,7 @@ impl TransactionChunk for ChunkToApply {

fn into_output<V: VMBlockExecutor>(
self,
parent_state: &LatestState,
state_view: CachedStateView,
) -> Result<ExecutionOutput> {
let Self {
Expand All @@ -121,6 +127,11 @@ impl TransactionChunk for ChunkToApply {
first_version: _,
} = self;

DoGetExecutionOutput::by_transaction_output(transactions, transaction_outputs, state_view)
DoGetExecutionOutput::by_transaction_output(
transactions,
transaction_outputs,
parent_state,
state_view,
)
}
}
3 changes: 2 additions & 1 deletion execution/executor/src/db_bootstrapper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ pub fn calculate_genesis<V: VMBlockExecutor>(
let execution_output = DoGetExecutionOutput::by_transaction_execution::<V>(
&V::new(),
vec![genesis_txn.clone().into()].into(),
&ledger_summary.state,
base_state_view,
BlockExecutorConfigFromOnchain::new_no_block_limit(),
TransactionSliceMetadata::unknown(),
Expand All @@ -160,7 +161,7 @@ pub fn calculate_genesis<V: VMBlockExecutor>(
let state_view = CachedStateView::new(
StateViewId::Miscellaneous,
Arc::clone(&db.reader),
output.execution_output.result_state.clone(),
output.execution_output.result_state.state().clone(),
)?;
let next_epoch = epoch
.checked_add(1)
Expand Down
4 changes: 4 additions & 0 deletions execution/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
// SPDX-License-Identifier: Apache-2.0

#![forbid(unsafe_code)]
// FIXME(aldenhu)
#![allow(dead_code)]
#![allow(unused_imports)]
#![allow(unused_variables)]

#[cfg(any(test, feature = "fuzzing"))]
pub mod fuzzing;
Expand Down
9 changes: 9 additions & 0 deletions execution/executor/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ fn apply_transaction_by_writeset(
db: &DbReaderWriter,
transactions_and_writesets: Vec<(Transaction, WriteSet)>,
) {
/*
let ledger_summary: LedgerSummary = db.reader.get_pre_committed_ledger_summary().unwrap();
let (txns, txn_outs) = transactions_and_writesets
Expand Down Expand Up @@ -505,6 +506,10 @@ fn apply_transaction_by_writeset(
true, /* sync_commit */
)
.unwrap();
FIXME(aldenhu)
*/

todo!()
}

#[test]
Expand Down Expand Up @@ -677,6 +682,7 @@ fn run_transactions_naive(
transactions: Vec<SignatureVerifiedTransaction>,
block_executor_onchain_config: BlockExecutorConfigFromOnchain,
) -> HashValue {
/*
let executor = TestExecutor::new();
let db = &executor.db;
Expand Down Expand Up @@ -710,6 +716,9 @@ fn run_transactions_naive(
.unwrap()
.transaction_accumulator
.root_hash()
FIXME(aldenhu)
*/
todo!()
}

proptest! {
Expand Down
Loading

0 comments on commit 5d8e1ac

Please sign in to comment.