Skip to content

Commit

Permalink
Add sponsor to executor API and TxContext
Browse files Browse the repository at this point in the history
  • Loading branch information
dariorussi committed Jan 22, 2025
1 parent ac40d67 commit 32864a2
Show file tree
Hide file tree
Showing 20 changed files with 152 additions and 19 deletions.
1 change: 1 addition & 0 deletions crates/simulacrum/src/epoch_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ impl EpochState {
kind,
signer,
tx_digest,
None,
))
}
}
28 changes: 28 additions & 0 deletions crates/sui-core/src/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1664,6 +1664,14 @@ impl AuthorityState {
let protocol_config = epoch_store.protocol_config();
let transaction_data = &certificate.data().intent_message().value;
let (kind, signer, gas) = transaction_data.execution_parts();
let sponsor = {
let gas_owner = tx_data.gas_owner();
if gas_owner == signer {
None
} else {
Some(gas_owner)
}
};

#[allow(unused_mut)]
let (inner_temp_store, _, mut effects, execution_error_opt) =
Expand All @@ -1688,6 +1696,7 @@ impl AuthorityState {
kind,
signer,
tx_digest,
sponsor,
);

fail_point_if!("cp_execution_nondeterminism", || {
Expand Down Expand Up @@ -1845,6 +1854,14 @@ impl AuthorityState {

let protocol_config = epoch_store.protocol_config();
let (kind, signer, _) = transaction.execution_parts();
let sponsor = {
let gas_owner = transaction.gas_owner();
if gas_owner == signer {
None
} else {
Some(gas_owner)
}
};

let silent = true;
let executor = sui_execution::executor(protocol_config, silent, None)
Expand All @@ -1869,6 +1886,7 @@ impl AuthorityState {
kind,
signer,
transaction_digest,
sponsor,
);
let tx_digest = *effects.transaction_digest();

Expand Down Expand Up @@ -2031,6 +2049,14 @@ impl AuthorityState {

let protocol_config = epoch_store.protocol_config();
let (kind, signer, _) = transaction.execution_parts();
let sponsor = {
let gas_owner = transaction.gas_owner();
if gas_owner == signer {
None
} else {
Some(gas_owner)
}
};

let silent = true;
let executor = sui_execution::executor(protocol_config, silent, None)
Expand All @@ -2055,6 +2081,7 @@ impl AuthorityState {
kind,
signer,
transaction.digest(),
sponsor,
);

Ok(SimulateTransactionResult {
Expand Down Expand Up @@ -2240,6 +2267,7 @@ impl AuthorityState {
sender,
transaction_digest,
skip_checks,
gas_sponsor,
);

let raw_effects = if show_raw_txn_data_and_effects {
Expand Down
20 changes: 16 additions & 4 deletions crates/sui-core/src/unit_tests/authority_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1690,8 +1690,14 @@ async fn test_publish_dependent_module_ok() {
);
let transaction = to_sender_signed_transaction(data, &sender_key);

let dependent_module_id =
TxContext::new(&sender, transaction.digest(), &EpochData::new_test()).fresh_id();
let dependent_module_id = TxContext::new(
&sender,
transaction.digest(),
&EpochData::new_test(),
rgp,
None,
)
.fresh_id();

// Object does not exist
assert!(authority.get_object(&dependent_module_id).await.is_none());
Expand Down Expand Up @@ -1739,8 +1745,14 @@ async fn test_publish_module_no_dependencies_ok() {
rgp,
);
let transaction = to_sender_signed_transaction(data, &sender_key);
let _module_object_id =
TxContext::new(&sender, transaction.digest(), &EpochData::new_test()).fresh_id();
let _module_object_id = TxContext::new(
&sender,
transaction.digest(),
&EpochData::new_test(),
rgp,
None,
)
.fresh_id();
let signed_effects = send_and_confirm_transaction(&authority, transaction)
.await
.unwrap()
Expand Down
1 change: 1 addition & 0 deletions crates/sui-genesis-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,7 @@ fn create_genesis_transaction(
kind,
signer,
genesis_digest,
None,
);
assert!(inner_temp_store.input_objects.is_empty());
assert!(inner_temp_store.mutable_inputs.is_empty());
Expand Down
21 changes: 21 additions & 0 deletions crates/sui-replay/src/replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,7 @@ impl LocalExec {
transaction_kind.clone(),
tx_info.sender,
*tx_digest,
tx_info.sponsor,
);

if let Err(err) = self.pretty_print_for_tracing(
Expand Down Expand Up @@ -850,6 +851,7 @@ impl LocalExec {
tx_info.sender,
tx_info.sender_signed_data.digest(),
skip_checks,
tx_info.sponsor,
)
.3
.unwrap_or_default(),
Expand Down Expand Up @@ -938,6 +940,7 @@ impl LocalExec {
kind,
signer,
*executable.digest(),
pre_run_sandbox.transaction_info.sponsor,
);

let effects =
Expand Down Expand Up @@ -1475,6 +1478,14 @@ impl LocalExec {

let raw_tx_bytes = tx_info.clone().raw_transaction;
let orig_tx: SenderSignedData = bcs::from_bytes(&raw_tx_bytes).unwrap();
let sponsor = {
let gas_sponsor = orig_tx.transaction_data().gas_owner();
if gas_sponsor == sender {
None
} else {
Some(gas_sponsor)
}
};
let input_objs = orig_tx
.transaction_data()
.input_objects()
Expand Down Expand Up @@ -1523,6 +1534,7 @@ impl LocalExec {
Ok(OnChainTransactionInfo {
kind: tx_kind_orig.clone(),
sender,
sponsor,
modified_at_versions,
input_objects: input_objs,
shared_object_refs,
Expand Down Expand Up @@ -1559,6 +1571,14 @@ impl LocalExec {
.transaction_data()
.sender();
let orig_tx = dp.node_state_dump.sender_signed_data.clone();
let sponsor = {
let gas_sponsor = orig_tx.transaction_data().gas_owner();
if gas_sponsor == sender {
None
} else {
Some(gas_sponsor)
}
};
let effects = dp.node_state_dump.computed_effects.clone();
let effects = SuiTransactionBlockEffects::try_from(effects).unwrap();
// Config objects don't show up in the node state dump so they need to be provided.
Expand Down Expand Up @@ -1612,6 +1632,7 @@ impl LocalExec {
Ok(OnChainTransactionInfo {
kind: tx_kind_orig.clone(),
sender,
sponsor,
modified_at_versions,
input_objects: input_objs,
shared_object_refs,
Expand Down
1 change: 1 addition & 0 deletions crates/sui-replay/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub struct OnChainTransactionInfo {
pub tx_digest: TransactionDigest,
pub sender_signed_data: SenderSignedData,
pub sender: SuiAddress,
pub sponsor: Option<SuiAddress>,
pub input_objects: Vec<InputObjectKind>,
pub kind: TransactionKind,
pub modified_at_versions: Vec<(ObjectID, SequenceNumber)>,
Expand Down
9 changes: 9 additions & 0 deletions crates/sui-single-node-benchmark/src/single_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ impl SingleValidator {
)
.unwrap();
let (kind, signer, gas) = executable.transaction_data().execution_parts();
let sponsor = {
let gas_owner = executable.transaction_data().gas_owner();
if gas_owner == signer {
None
} else {
Some(gas_owner)
}
};
let (inner_temp_store, _, effects, _) =
self.epoch_store.executor().execute_transaction_to_effects(
&store,
Expand All @@ -221,6 +229,7 @@ impl SingleValidator {
kind,
signer,
*executable.digest(),
sponsor,
);
assert!(effects.status().is_ok());
store.commit_objects(inner_temp_store);
Expand Down
1 change: 1 addition & 0 deletions crates/sui-swarm-config/src/network_config_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ mod test {
kind,
signer,
genesis_digest,
None,
);

assert_eq!(&effects, genesis.effects());
Expand Down
32 changes: 17 additions & 15 deletions crates/sui-types/src/base_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,10 @@ pub struct TxContext {
epoch_timestamp_ms: CheckpointTimestamp,
/// Number of `ObjectID`'s generated during execution of the current transaction
ids_created: u64,
// gas price passed to transaction as input
gas_price: u64,
// address of the sponsor if any
sponsor: Option<AccountAddress>,
}

#[derive(PartialEq, Eq, Clone, Copy)]
Expand All @@ -994,12 +998,20 @@ pub enum TxContextKind {
}

impl TxContext {
pub fn new(sender: &SuiAddress, digest: &TransactionDigest, epoch_data: &EpochData) -> Self {
pub fn new(
sender: &SuiAddress,
digest: &TransactionDigest,
epoch_data: &EpochData,
gas_price: u64,
sponsor: Option<SuiAddress>,
) -> Self {
Self::new_from_components(
sender,
digest,
&epoch_data.epoch_id(),
epoch_data.epoch_start_timestamp(),
gas_price,
sponsor,
)
}

Expand All @@ -1008,13 +1020,17 @@ impl TxContext {
digest: &TransactionDigest,
epoch_id: &EpochId,
epoch_timestamp_ms: u64,
gas_price: u64,
sponsor: Option<SuiAddress>,
) -> Self {
Self {
sender: AccountAddress::new(sender.0),
digest: digest.into_inner().to_vec(),
epoch: *epoch_id,
epoch_timestamp_ms,
ids_created: 0,
gas_price,
sponsor: sponsor.map(|s| AccountAddress::new(s.0)),
}
}

Expand Down Expand Up @@ -1090,20 +1106,6 @@ impl TxContext {
self.ids_created = other.ids_created;
Ok(())
}

// Generate a random TxContext for testing.
pub fn random_for_testing_only() -> Self {
Self::new(
&SuiAddress::random_for_testing_only(),
&TransactionDigest::random(),
&EpochData::new_test(),
)
}

/// Generate a TxContext for testing with a specific sender.
pub fn with_sender_for_testing_only(sender: &SuiAddress) -> Self {
Self::new(sender, &TransactionDigest::random(), &EpochData::new_test())
}
}

// TODO: rename to version
Expand Down
7 changes: 7 additions & 0 deletions crates/sui-types/src/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub mod checked {
fn bucketize_computation(&mut self) -> Result<(), ExecutionError>;
fn summary(&self) -> GasCostSummary;
fn gas_budget(&self) -> u64;
fn gas_price(&self) -> u64;
fn storage_gas_units(&self) -> u64;
fn storage_rebate(&self) -> u64;
fn unmetered_storage_rebate(&self) -> u64;
Expand Down Expand Up @@ -107,6 +108,12 @@ pub mod checked {
Self::V2(status) => status.check_gas_balance(gas_objs, gas_budget),
}
}

pub fn gas_price(&self) -> u64 {
match self {
Self::V2(status) => status.gas_price(),
}
}
}

/// Summary of the charges in a transaction.
Expand Down
4 changes: 4 additions & 0 deletions crates/sui-types/src/gas_model/gas_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,10 @@ mod checked {
self.gas_budget
}

fn gas_price(&self) -> u64 {
self.gas_price
}

fn storage_gas_units(&self) -> u64 {
self.per_object_storage
.iter()
Expand Down
4 changes: 4 additions & 0 deletions sui-execution/latest/sui-adapter/src/execution_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ mod checked {
metrics: Arc<LimitsMetrics>,
enable_expensive_checks: bool,
certificate_deny_set: &HashSet<TransactionDigest>,
sponsor: Option<SuiAddress>,
) -> (
InnerTemporaryStore,
SuiGasStatus,
Expand Down Expand Up @@ -119,6 +120,7 @@ mod checked {
*epoch_id,
);

let gas_price = gas_status.gas_price();
let mut gas_charger =
GasCharger::new(transaction_digest, gas_coins, gas_status, protocol_config);

Expand All @@ -127,6 +129,8 @@ mod checked {
&transaction_digest,
epoch_id,
epoch_timestamp_ms,
gas_price,
sponsor,
);

let is_epoch_change = transaction_kind.is_end_of_epoch_tx();
Expand Down
2 changes: 2 additions & 0 deletions sui-execution/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub trait Executor {
transaction_kind: TransactionKind,
transaction_signer: SuiAddress,
transaction_digest: TransactionDigest,
sponsor: Option<SuiAddress>,
) -> (
InnerTemporaryStore,
SuiGasStatus,
Expand Down Expand Up @@ -68,6 +69,7 @@ pub trait Executor {
transaction_signer: SuiAddress,
transaction_digest: TransactionDigest,
skip_all_checks: bool,
sponsor: Option<SuiAddress>,
) -> (
InnerTemporaryStore,
SuiGasStatus,
Expand Down
Loading

0 comments on commit 32864a2

Please sign in to comment.