Skip to content

Commit

Permalink
remove TxContext from executor
Browse files Browse the repository at this point in the history
  • Loading branch information
dariorussi committed Jan 21, 2025
1 parent c29efa3 commit 2e2c9b0
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 41 deletions.
48 changes: 24 additions & 24 deletions crates/sui-genesis-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ use sui_config::genesis::{
use sui_execution::{self, Executor};
use sui_framework::{BuiltInFramework, SystemPackage};
use sui_protocol_config::{Chain, ProtocolConfig, ProtocolVersion};
use sui_types::base_types::{
ExecutionDigests, ObjectID, SequenceNumber, SuiAddress, TransactionDigest, TxContext,
};
use sui_types::base_types::{ExecutionDigests, ObjectID, SequenceNumber, TransactionDigest};
use sui_types::bridge::{BridgeChainId, BRIDGE_CREATE_FUNCTION_NAME, BRIDGE_MODULE_NAME};
use sui_types::committee::Committee;
use sui_types::crypto::{
Expand Down Expand Up @@ -681,16 +679,15 @@ impl Builder {
}
}

// Create a Genesis Txn Context to be used when generating genesis objects by hashing all of the
// Create a Genesis Txn Digest to be used when generating genesis objects by hashing all of the
// inputs into genesis ans using that as our "Txn Digest". This is done to ensure that coin objects
// created between chains are unique
fn create_genesis_context(
epoch_data: &EpochData,
fn create_genesis_digest(
genesis_chain_parameters: &GenesisChainParameters,
genesis_validators: &[GenesisValidatorMetadata],
token_distribution_schedule: &TokenDistributionSchedule,
system_packages: &[SystemPackage],
) -> TxContext {
) -> TransactionDigest {
let mut hasher = DefaultHash::default();
hasher.update(b"sui-genesis");
hasher.update(bcs::to_bytes(genesis_chain_parameters).unwrap());
Expand All @@ -701,13 +698,7 @@ fn create_genesis_context(
}

let hash = hasher.finalize();
let genesis_transaction_digest = TransactionDigest::new(hash.into());

TxContext::new(
&SuiAddress::default(),
&genesis_transaction_digest,
epoch_data,
)
TransactionDigest::new(hash.into())
}

fn get_genesis_protocol_config(version: ProtocolVersion) -> ProtocolConfig {
Expand Down Expand Up @@ -755,8 +746,7 @@ fn build_unsigned_genesis_data(
// This is a no-op under normal conditions and only an issue with certain tests.
update_system_packages_from_objects(&mut system_packages, objects);

let mut genesis_ctx = create_genesis_context(
&epoch_data,
let genesis_digest = create_genesis_digest(
&genesis_chain_parameters,
&genesis_validators,
token_distribution_schedule,
Expand All @@ -768,7 +758,8 @@ fn build_unsigned_genesis_data(
let metrics = Arc::new(LimitsMetrics::new(&registry));

let objects = create_genesis_objects(
&mut genesis_ctx,
&epoch_data,
&genesis_digest,
objects,
&genesis_validators,
&genesis_chain_parameters,
Expand Down Expand Up @@ -957,7 +948,8 @@ fn create_genesis_transaction(
}

fn create_genesis_objects(
genesis_ctx: &mut TxContext,
epoch_data: &EpochData,
genesis_digest: &TransactionDigest,
input_objects: &[Object],
validators: &[GenesisValidatorMetadata],
parameters: &GenesisChainParameters,
Expand All @@ -982,7 +974,8 @@ fn create_genesis_objects(
process_package(
&mut store,
executor.as_ref(),
genesis_ctx,
epoch_data,
genesis_digest,
&system_package.modules(),
system_package.dependencies().to_vec(),
&protocol_config,
Expand All @@ -1001,7 +994,8 @@ fn create_genesis_objects(
&mut store,
executor.as_ref(),
validators,
genesis_ctx,
epoch_data,
genesis_digest,
parameters,
token_distribution_schedule,
metrics,
Expand All @@ -1014,7 +1008,8 @@ fn create_genesis_objects(
fn process_package(
store: &mut InMemoryStorage,
executor: &dyn Executor,
ctx: &mut TxContext,
epoch_data: &EpochData,
genesis_digest: &TransactionDigest,
modules: &[CompiledModule],
dependencies: Vec<ObjectID>,
protocol_config: &ProtocolConfig,
Expand Down Expand Up @@ -1070,7 +1065,9 @@ fn process_package(
&*store,
protocol_config,
metrics,
ctx,
epoch_data.epoch_id(),
epoch_data.epoch_start_timestamp(),
genesis_digest,
CheckedInputObjects::new_for_genesis(loaded_dependencies),
pt,
)?;
Expand All @@ -1084,7 +1081,8 @@ pub fn generate_genesis_system_object(
store: &mut InMemoryStorage,
executor: &dyn Executor,
genesis_validators: &[GenesisValidatorMetadata],
genesis_ctx: &mut TxContext,
epoch_data: &EpochData,
genesis_digest: &TransactionDigest,
genesis_chain_parameters: &GenesisChainParameters,
token_distribution_schedule: &TokenDistributionSchedule,
metrics: Arc<LimitsMetrics>,
Expand Down Expand Up @@ -1196,7 +1194,9 @@ pub fn generate_genesis_system_object(
&*store,
&protocol_config,
metrics,
genesis_ctx,
epoch_data.epoch_id(),
epoch_data.epoch_start_timestamp(),
genesis_digest,
CheckedInputObjects::new_for_genesis(vec![]),
pt,
)?;
Expand Down
20 changes: 11 additions & 9 deletions sui-execution/latest/sui-adapter/src/execution_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ mod checked {

let is_genesis_tx = matches!(transaction_kind, TransactionKind::Genesis(_));
let advance_epoch_gas_summary = transaction_kind.get_advance_epoch_tx_gas_summary();
let digest = tx_ctx.digest();

// We must charge object read here during transaction execution, because if this fails
// we must still ensure an effect is committed and all objects versions incremented
Expand Down Expand Up @@ -371,7 +372,7 @@ mod checked {
if let Err(e) = run_conservation_checks::<Mode>(
temporary_store,
gas_charger,
tx_ctx,
digest,
move_vm,
protocol_config.simple_conservation_checks(),
enable_expensive_checks,
Expand All @@ -390,7 +391,7 @@ mod checked {
fn run_conservation_checks<Mode: ExecutionMode>(
temporary_store: &mut TemporaryStore<'_>,
gas_charger: &mut GasCharger,
tx_ctx: &mut TxContext,
tx_digest: TransactionDigest,
move_vm: &Arc<MoveVM>,
simple_conservation_checks: bool,
enable_expensive_checks: bool,
Expand Down Expand Up @@ -449,7 +450,7 @@ mod checked {
// we will create or destroy SUI otherwise
panic!(
"SUI conservation fail in tx block {}: {}\nGas status is {}\nTx was ",
tx_ctx.digest(),
tx_digest,
recovery_err,
gas_charger.summary()
)
Expand Down Expand Up @@ -888,11 +889,11 @@ mod checked {

if result.is_err() {
tracing::error!(
"Failed to execute advance epoch transaction. Switching to safe mode. Error: {:?}. Input objects: {:?}. Tx data: {:?}",
result.as_ref().err(),
temporary_store.objects(),
change_epoch,
);
"Failed to execute advance epoch transaction. Switching to safe mode. Error: {:?}. Input objects: {:?}. Tx data: {:?}",
result.as_ref().err(),
temporary_store.objects(),
change_epoch,
);
temporary_store.drop_writes();
// Must reset the storage rebate since we are re-executing.
gas_charger.reset_storage_cost_and_rebate();
Expand Down Expand Up @@ -954,6 +955,7 @@ mod checked {
protocol_config: &ProtocolConfig,
metrics: Arc<LimitsMetrics>,
) {
let digest = tx_ctx.digest();
let binary_config = to_binary_config(protocol_config);
for (version, modules, dependencies) in change_epoch.system_packages.into_iter() {
let deserialized_modules: Vec<_> = modules
Expand Down Expand Up @@ -986,7 +988,7 @@ mod checked {
&deserialized_modules,
version,
dependencies,
tx_ctx.digest(),
digest,
);

info!(
Expand Down
4 changes: 4 additions & 0 deletions sui-execution/latest/sui-adapter/src/temporary_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ impl<'backing> TemporaryStore<'backing> {
&self.input_objects
}

pub fn objects_created_count(&self) -> usize {
self.execution_results.created_object_ids.len()
}

pub fn update_object_version_and_prev_tx(&mut self) {
self.execution_results.update_version_and_previous_tx(
self.lamport_timestamp,
Expand Down
9 changes: 6 additions & 3 deletions sui-execution/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{collections::HashSet, sync::Arc};
use sui_protocol_config::ProtocolConfig;
use sui_types::storage::BackingStore;
use sui_types::{
base_types::{ObjectRef, SuiAddress, TxContext},
base_types::{ObjectRef, SuiAddress},
committee::EpochId,
digests::TransactionDigest,
effects::TransactionEffects,
Expand Down Expand Up @@ -81,8 +81,11 @@ pub trait Executor {
// Configuration
protocol_config: &ProtocolConfig,
metrics: Arc<LimitsMetrics>,
// Genesis State
tx_context: &mut TxContext,
// Epoch
epoch_id: EpochId,
epoch_timestamp_ms: u64,
// Genesis Digest
transaction_digest: &TransactionDigest,
// Transaction
input_objects: CheckedInputObjects,
pt: ProgrammableTransaction,
Expand Down
12 changes: 10 additions & 2 deletions sui-execution/src/latest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,24 @@ impl executor::Executor for Executor {
store: &dyn BackingStore,
protocol_config: &ProtocolConfig,
metrics: Arc<LimitsMetrics>,
tx_context: &mut TxContext,
epoch_id: EpochId,
epoch_timestamp_ms: u64,
transaction_digest: &TransactionDigest,
input_objects: CheckedInputObjects,
pt: ProgrammableTransaction,
) -> Result<InnerTemporaryStore, ExecutionError> {
let mut tx_context = TxContext::new_from_components(
&SuiAddress::default(),
transaction_digest,
&epoch_id,
epoch_timestamp_ms,
);
execute_genesis_state_update(
store,
protocol_config,
metrics,
&self.0,
tx_context,
&mut tx_context,
input_objects,
pt,
)
Expand Down
10 changes: 9 additions & 1 deletion sui-execution/src/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,18 @@ impl executor::Executor for Executor {
store: &dyn BackingStore,
protocol_config: &ProtocolConfig,
metrics: Arc<LimitsMetrics>,
tx_context: &mut TxContext,
epoch_id: EpochId,
epoch_timestamp_ms: u64,
transaction_digest: &TransactionDigest,
input_objects: CheckedInputObjects,
pt: ProgrammableTransaction,
) -> Result<InnerTemporaryStore, ExecutionError> {
let tx_context = &mut TxContext::new_from_components(
&SuiAddress::default(),
transaction_digest,
&epoch_id,
epoch_timestamp_ms,
);
execute_genesis_state_update(
store,
protocol_config,
Expand Down
10 changes: 9 additions & 1 deletion sui-execution/src/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,18 @@ impl executor::Executor for Executor {
store: &dyn BackingStore,
protocol_config: &ProtocolConfig,
metrics: Arc<LimitsMetrics>,
tx_context: &mut TxContext,
epoch_id: EpochId,
epoch_timestamp_ms: u64,
transaction_digest: &TransactionDigest,
input_objects: CheckedInputObjects,
pt: ProgrammableTransaction,
) -> Result<InnerTemporaryStore, ExecutionError> {
let tx_context = &mut TxContext::new_from_components(
&SuiAddress::default(),
transaction_digest,
&epoch_id,
epoch_timestamp_ms,
);
execute_genesis_state_update(
store,
protocol_config,
Expand Down
10 changes: 9 additions & 1 deletion sui-execution/src/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,18 @@ impl executor::Executor for Executor {
store: &dyn BackingStore,
protocol_config: &ProtocolConfig,
metrics: Arc<LimitsMetrics>,
tx_context: &mut TxContext,
epoch_id: EpochId,
epoch_timestamp_ms: u64,
transaction_digest: &TransactionDigest,
input_objects: CheckedInputObjects,
pt: ProgrammableTransaction,
) -> Result<InnerTemporaryStore, ExecutionError> {
let tx_context = &mut TxContext::new_from_components(
&SuiAddress::default(),
transaction_digest,
&epoch_id,
epoch_timestamp_ms,
);
execute_genesis_state_update(
store,
protocol_config,
Expand Down

0 comments on commit 2e2c9b0

Please sign in to comment.