Skip to content

Commit

Permalink
refactor: rename runtime apply_transactions -> apply_chunk (#10394)
Browse files Browse the repository at this point in the history
`apply_transactions` is confusing for multiple reasons:
- this function processes not only transactions but also receipts
- transactions are not directly applied, but converted to receipts
  • Loading branch information
pugachAG authored Jan 9, 2024
1 parent c03c9eb commit dfaec0d
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 121 deletions.
45 changes: 20 additions & 25 deletions chain/chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use crate::state_snapshot_actor::SnapshotCallbacks;
use crate::store::{ChainStore, ChainStoreAccess, ChainStoreUpdate};

use crate::types::{
AcceptedBlock, ApplyTransactionResult, ApplyTransactionsBlockContext, BlockEconomicsConfig,
ChainConfig, RuntimeAdapter, StorageDataSource,
AcceptedBlock, ApplyChunkBlockContext, ApplyChunkResult, BlockEconomicsConfig, ChainConfig,
RuntimeAdapter, StorageDataSource,
};
use crate::update_shard::{
apply_new_chunk, process_missing_chunks_range, process_shard_update, NewChunkData,
Expand Down Expand Up @@ -2878,12 +2878,12 @@ impl Chain {

/// For given pair of block headers and shard id, return information about
/// block necessary for processing shard update.
fn get_apply_transactions_block_context(
fn get_apply_chunk_block_context(
&self,
block_header: &BlockHeader,
prev_block_header: &BlockHeader,
is_new_chunk: bool,
) -> Result<ApplyTransactionsBlockContext, Error> {
) -> Result<ApplyChunkBlockContext, Error> {
let epoch_id = block_header.epoch_id();
let protocol_version = self.epoch_manager.get_epoch_protocol_version(epoch_id)?;
// Before `FixApplyChunks` feature, gas price was taken from current
Expand All @@ -2896,7 +2896,7 @@ impl Chain {
prev_block_header.next_gas_price()
};

Ok(ApplyTransactionsBlockContext::from_header(block_header, gas_price))
Ok(ApplyChunkBlockContext::from_header(block_header, gas_price))
}

fn block_catch_up_postprocess(
Expand Down Expand Up @@ -3296,7 +3296,7 @@ impl Chain {
let cares_about_shard_next_epoch =
self.shard_tracker.will_care_about_shard(me.as_ref(), prev_hash, shard_id, true);
let will_shard_layout_change = self.epoch_manager.will_shard_layout_change(prev_hash)?;
let should_apply_transactions = get_should_apply_transactions(
let should_apply_chunk = get_should_apply_chunk(
mode,
cares_about_shard_this_epoch,
cares_about_shard_next_epoch,
Expand All @@ -3307,7 +3307,7 @@ impl Chain {
shard_uid,
cares_about_shard_this_epoch,
will_shard_layout_change,
should_apply_transactions,
should_apply_chunk,
need_to_reshard,
})
}
Expand All @@ -3330,16 +3330,16 @@ impl Chain {
let shard_context = self.get_shard_context(me, block.header(), shard_id, mode)?;

// We can only perform resharding when states are ready, i.e., mode != ApplyChunksMode::NotCaughtUp
// 1) if should_apply_transactions == true && resharding_state_roots.is_some(),
// 1) if should_apply_chunk == true && resharding_state_roots.is_some(),
// that means children shards are ready.
// `apply_resharding_state_changes` will apply updates to the children shards
// 2) if should_apply_transactions == true && resharding_state_roots.is_none(),
// 2) if should_apply_chunk == true && resharding_state_roots.is_none(),
// that means children shards are not ready yet.
// `apply_resharding_state_changes` will return `state_changes_for_resharding`,
// which will be stored to the database in `process_apply_chunks`
// 3) if should_apply_transactions == false && resharding_state_roots.is_some()
// 3) if should_apply_chunk == false && resharding_state_roots.is_some()
// This implies mode == CatchingUp and cares_about_shard_this_epoch == true,
// otherwise should_apply_transactions will be true
// otherwise should_apply_chunk will be true
// That means transactions have already been applied last time when apply_chunks are
// called with mode NotCaughtUp, therefore `state_changes_for_resharding` have been
// stored in the database. Then we can safely read that and apply that to the split
Expand All @@ -3352,8 +3352,8 @@ impl Chain {
};

let is_new_chunk = chunk_header.height_included() == block.header().height();
let shard_update_reason = if shard_context.should_apply_transactions {
let block_context = self.get_apply_transactions_block_context(
let shard_update_reason = if shard_context.should_apply_chunk {
let block_context = self.get_apply_chunk_block_context(
block.header(),
prev_block.header(),
is_new_chunk,
Expand Down Expand Up @@ -3490,11 +3490,10 @@ impl Chain {
shard_id: ShardId,
mode: ApplyChunksMode,
return_chunk_extra: bool,
) -> Result<(Vec<(ApplyTransactionsBlockContext, ShardContext)>, Option<ChunkExtra>), Error>
{
) -> Result<(Vec<(ApplyChunkBlockContext, ShardContext)>, Option<ChunkExtra>), Error> {
let blocks_to_execute =
self.get_blocks_until_height(chunk_prev_hash, prev_chunk_height_included, false)?;
let mut execution_contexts: Vec<(ApplyTransactionsBlockContext, ShardContext)> = vec![];
let mut execution_contexts: Vec<(ApplyChunkBlockContext, ShardContext)> = vec![];
for block_hash in blocks_to_execute {
let block_header = self.get_block_header(&block_hash)?;
let prev_block_header = self.get_previous_header(&block_header)?;
Expand All @@ -3505,11 +3504,7 @@ impl Chain {
)));
}
execution_contexts.push((
self.get_apply_transactions_block_context(
&block_header,
&prev_block_header,
false,
)?,
self.get_apply_chunk_block_context(&block_header, &prev_block_header, false)?,
shard_context,
));
}
Expand Down Expand Up @@ -3565,7 +3560,7 @@ impl Chain {
let is_new_chunk = chunk_header.height_included() == block.header().height();

// If we don't track a shard or there is no chunk, there is nothing to validate.
if !last_shard_context.should_apply_transactions || !is_new_chunk {
if !last_shard_context.should_apply_chunk || !is_new_chunk {
return Ok(None);
}

Expand Down Expand Up @@ -3667,7 +3662,7 @@ impl Chain {
return Ok(None);
}
(
self.get_apply_transactions_block_context(&block_header, &prev_block_header, true)?,
self.get_apply_chunk_block_context(&block_header, &prev_block_header, true)?,
shard_context,
)
};
Expand Down Expand Up @@ -3729,7 +3724,7 @@ impl Chain {
epoch_manager.as_ref(),
)?;
let (outcome_root, _) =
ApplyTransactionResult::compute_outcomes_proof(&apply_result.outcomes);
ApplyChunkResult::compute_outcomes_proof(&apply_result.outcomes);
current_chunk_extra = ChunkExtra::new(
&apply_result.new_root,
outcome_root,
Expand Down Expand Up @@ -3865,7 +3860,7 @@ fn sync_hash_not_first_hash(sync_hash: CryptoHash) -> Error {
/// ApplyChunksMode::NotCaughtUp once with ApplyChunksMode::CatchingUp. Note
/// that it does not guard whether the children shards are ready or not, see the
/// comments before `need_to_reshard`
fn get_should_apply_transactions(
fn get_should_apply_chunk(
mode: ApplyChunksMode,
cares_about_shard_this_epoch: bool,
cares_about_shard_next_epoch: bool,
Expand Down
23 changes: 10 additions & 13 deletions chain/chain/src/chain_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::metrics::{SHARD_LAYOUT_NUM_SHARDS, SHARD_LAYOUT_VERSION};
use crate::store::{ChainStore, ChainStoreAccess, ChainStoreUpdate};

use crate::types::{
ApplyTransactionResult, ApplyTransactionsBlockContext, ApplyTransactionsChunkContext,
ReshardingResults, RuntimeAdapter, RuntimeStorageConfig,
ApplyChunkBlockContext, ApplyChunkResult, ApplyChunkShardContext, ReshardingResults,
RuntimeAdapter, RuntimeStorageConfig,
};
use crate::update_shard::{
NewChunkResult, OldChunkResult, ReshardingResult, ShardBlockUpdateResult, ShardUpdateResult,
Expand Down Expand Up @@ -330,7 +330,7 @@ impl<'a> ChainUpdate<'a> {
resharding_results,
}) => {
let (outcome_root, outcome_paths) =
ApplyTransactionResult::compute_outcomes_proof(&apply_result.outcomes);
ApplyChunkResult::compute_outcomes_proof(&apply_result.outcomes);
let shard_id = shard_uid.shard_id();

// Save state root after applying transactions.
Expand Down Expand Up @@ -732,16 +732,16 @@ impl<'a> ChainUpdate<'a> {
// TODO(nikurt): Determine the value correctly.
let is_first_block_with_chunk_of_version = false;

let apply_result = self.runtime_adapter.apply_transactions(
let apply_result = self.runtime_adapter.apply_chunk(
RuntimeStorageConfig::new(chunk_header.prev_state_root(), true),
ApplyTransactionsChunkContext {
ApplyChunkShardContext {
shard_id,
gas_limit,
last_validator_proposals: chunk_header.prev_validator_proposals(),
is_first_block_with_chunk_of_version,
is_new_chunk: true,
},
ApplyTransactionsBlockContext {
ApplyChunkBlockContext {
height: chunk_header.height_included(),
block_hash: *block_header.hash(),
prev_block_hash: *chunk_header.prev_block_hash(),
Expand All @@ -755,7 +755,7 @@ impl<'a> ChainUpdate<'a> {
)?;

let (outcome_root, outcome_proofs) =
ApplyTransactionResult::compute_outcomes_proof(&apply_result.outcomes);
ApplyChunkResult::compute_outcomes_proof(&apply_result.outcomes);

self.chain_store_update.save_chunk(chunk);

Expand Down Expand Up @@ -829,19 +829,16 @@ impl<'a> ChainUpdate<'a> {
let chunk_extra =
self.chain_store_update.get_chunk_extra(prev_block_header.hash(), &shard_uid)?;

let apply_result = self.runtime_adapter.apply_transactions(
let apply_result = self.runtime_adapter.apply_chunk(
RuntimeStorageConfig::new(*chunk_extra.state_root(), true),
ApplyTransactionsChunkContext {
ApplyChunkShardContext {
shard_id,
last_validator_proposals: chunk_extra.validator_proposals(),
gas_limit: chunk_extra.gas_limit(),
is_new_chunk: false,
is_first_block_with_chunk_of_version: false,
},
ApplyTransactionsBlockContext::from_header(
&block_header,
prev_block_header.next_gas_price(),
),
ApplyChunkBlockContext::from_header(&block_header, prev_block_header.next_gas_price()),
&[],
&[],
)?;
Expand Down
14 changes: 7 additions & 7 deletions chain/chain/src/test_utils/kv_runtime.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::ValidatorSchedule;
use crate::types::{
ApplyResultForResharding, ApplyTransactionResult, ApplyTransactionsBlockContext,
ApplyTransactionsChunkContext, RuntimeAdapter, RuntimeStorageConfig,
ApplyChunkBlockContext, ApplyChunkResult, ApplyChunkShardContext, ApplyResultForResharding,
RuntimeAdapter, RuntimeStorageConfig,
};
use crate::BlockHeader;
use borsh::{BorshDeserialize, BorshSerialize};
Expand Down Expand Up @@ -1040,14 +1040,14 @@ impl RuntimeAdapter for KeyValueRuntime {
Ok(res)
}

fn apply_transactions(
fn apply_chunk(
&self,
storage_config: RuntimeStorageConfig,
chunk: ApplyTransactionsChunkContext,
block: ApplyTransactionsBlockContext,
chunk: ApplyChunkShardContext,
block: ApplyChunkBlockContext,
receipts: &[Receipt],
transactions: &[SignedTransaction],
) -> Result<ApplyTransactionResult, Error> {
) -> Result<ApplyChunkResult, Error> {
assert!(!storage_config.record_storage);
let mut tx_results = vec![];
let shard_id = chunk.shard_id;
Expand Down Expand Up @@ -1181,7 +1181,7 @@ impl RuntimeAdapter for KeyValueRuntime {
self.state.write().unwrap().insert(state_root, state);
self.state_size.write().unwrap().insert(state_root, state_size);

Ok(ApplyTransactionResult {
Ok(ApplyChunkResult {
trie_changes: WrappedTrieChanges::new(
self.get_tries(),
ShardUId { version: 0, shard_id: shard_id as u32 },
Expand Down
23 changes: 12 additions & 11 deletions chain/chain/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub enum ReshardingResults {
}

#[derive(Debug)]
pub struct ApplyTransactionResult {
pub struct ApplyChunkResult {
pub trie_changes: WrappedTrieChanges,
pub new_root: StateRoot,
pub outcomes: Vec<ExecutionOutcomeWithId>,
Expand All @@ -112,7 +112,7 @@ pub struct ApplyTransactionResult {
pub processed_delayed_receipts: Vec<Receipt>,
}

impl ApplyTransactionResult {
impl ApplyChunkResult {
/// Returns root and paths for all the outcomes in the result.
pub fn compute_outcomes_proof(
outcomes: &[ExecutionOutcomeWithId],
Expand Down Expand Up @@ -279,7 +279,7 @@ impl RuntimeStorageConfig {
}

#[derive(Clone)]
pub struct ApplyTransactionsBlockContext {
pub struct ApplyChunkBlockContext {
pub height: BlockHeight,
pub block_hash: CryptoHash,
pub prev_block_hash: CryptoHash,
Expand All @@ -289,7 +289,7 @@ pub struct ApplyTransactionsBlockContext {
pub random_seed: CryptoHash,
}

impl ApplyTransactionsBlockContext {
impl ApplyChunkBlockContext {
pub fn from_header(header: &BlockHeader, gas_price: Balance) -> Self {
Self {
height: header.height(),
Expand All @@ -303,7 +303,7 @@ impl ApplyTransactionsBlockContext {
}
}

pub struct ApplyTransactionsChunkContext<'a> {
pub struct ApplyChunkShardContext<'a> {
pub shard_id: ShardId,
pub last_validator_proposals: ValidatorStakeIter<'a>,
pub gas_limit: Gas,
Expand Down Expand Up @@ -387,16 +387,17 @@ pub trait RuntimeAdapter: Send + Sync {
/// Get the block height for which garbage collection should not go over
fn get_gc_stop_height(&self, block_hash: &CryptoHash) -> BlockHeight;

/// Apply transactions to given state root and return store update and new state root.
/// Apply transactions and receipts to given state root and return store update
/// and new state root.
/// Also returns transaction result for each transaction and new receipts.
fn apply_transactions(
fn apply_chunk(
&self,
storage: RuntimeStorageConfig,
chunk: ApplyTransactionsChunkContext,
block: ApplyTransactionsBlockContext,
chunk: ApplyChunkShardContext,
block: ApplyChunkBlockContext,
receipts: &[Receipt],
transactions: &[SignedTransaction],
) -> Result<ApplyTransactionResult, Error>;
) -> Result<ApplyChunkResult, Error>;

/// Query runtime with given `path` and `data`.
fn query(
Expand Down Expand Up @@ -548,7 +549,7 @@ mod tests {
},
};
let outcomes = vec![outcome1, outcome2];
let (outcome_root, paths) = ApplyTransactionResult::compute_outcomes_proof(&outcomes);
let (outcome_root, paths) = ApplyChunkResult::compute_outcomes_proof(&outcomes);
for (outcome_with_id, path) in outcomes.into_iter().zip(paths.into_iter()) {
assert!(verify_path(outcome_root, &path, &outcome_with_id.to_hashes()));
}
Expand Down
Loading

0 comments on commit dfaec0d

Please sign in to comment.