Skip to content

Commit

Permalink
Add epoch ID to TxContext (MystenLabs#2285)
Browse files Browse the repository at this point in the history
  • Loading branch information
lxfind authored May 27, 2022
1 parent bf99c9b commit 609b567
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion crates/sui-adapter/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn get_framework_object_ref() -> ObjectRef {
}

pub fn get_genesis_context() -> TxContext {
TxContext::new(&SuiAddress::default(), &TransactionDigest::genesis())
TxContext::new(&SuiAddress::default(), &TransactionDigest::genesis(), 0)
}

/// Create and return objects wrapping the genesis modules for sui
Expand Down
1 change: 1 addition & 0 deletions crates/sui-core/src/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ impl AuthorityState {
&self.move_vm,
&self._native_functions,
gas_status,
self.committee.epoch,
)?;

self.metrics.total_effects.inc();
Expand Down
4 changes: 3 additions & 1 deletion crates/sui-core/src/execution_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::authority::AuthorityTemporaryStore;
use move_core_types::language_storage::ModuleId;
use move_vm_runtime::{move_vm::MoveVM, native_functions::NativeFunctionTable};
use sui_adapter::adapter;
use sui_types::committee::EpochId;
use sui_types::{
base_types::{ObjectID, ObjectRef, SuiAddress, TransactionDigest, TxContext},
error::SuiResult,
Expand All @@ -30,8 +31,9 @@ pub fn execute_transaction_to_effects<S: BackingPackageStore>(
move_vm: &Arc<MoveVM>,
native_functions: &NativeFunctionTable,
gas_status: SuiGasStatus,
epoch: EpochId,
) -> SuiResult<TransactionEffects> {
let mut tx_ctx = TxContext::new(&transaction_data.signer(), &transaction_digest);
let mut tx_ctx = TxContext::new(&transaction_data.signer(), &transaction_digest, epoch);

let gas_object_id = transaction_data.gas_payment_object_ref().0;
let status = execute_transaction(
Expand Down
4 changes: 2 additions & 2 deletions crates/sui-core/src/unit_tests/authority_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ async fn test_publish_dependent_module_ok() {
let signature = Signature::new(&data, &sender_key);
let transaction = Transaction::new(data, signature);

let dependent_module_id = TxContext::new(&sender, transaction.digest()).fresh_id();
let dependent_module_id = TxContext::new(&sender, transaction.digest(), 0).fresh_id();

// Object does not exist
assert!(authority
Expand Down Expand Up @@ -472,7 +472,7 @@ async fn test_publish_module_no_dependencies_ok() {
let data = TransactionData::new_module(sender, gas_payment_object_ref, module_bytes, MAX_GAS);
let signature = Signature::new(&data, &sender_key);
let transaction = Transaction::new(data, signature);
let _module_object_id = TxContext::new(&sender, transaction.digest()).fresh_id();
let _module_object_id = TxContext::new(&sender, transaction.digest(), 0).fresh_id();
let response = send_and_confirm_transaction(&authority, transaction)
.await
.unwrap();
Expand Down
18 changes: 17 additions & 1 deletion crates/sui-framework/sources/TxContext.move
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ module Sui::TxContext {
signer: signer,
/// Hash of the current transaction
tx_hash: vector<u8>,
/// The current epoch number.
epoch: u64,
/// Counter recording the number of fresh id's created while executing
/// this transaction. Always 0 at the start of a transaction
ids_created: u64
Expand All @@ -46,6 +48,10 @@ module Sui::TxContext {
&self.signer
}

public fun epoch(self: &TxContext): u64 {
self.epoch
}

/// Generate a new, globally unique object ID with version 0
public fun new_id(ctx: &mut TxContext): VersionedID {
let ids_created = ctx.ids_created;
Expand All @@ -72,7 +78,17 @@ module Sui::TxContext {
Vector::length(&tx_hash) == TX_HASH_LENGTH,
Errors::invalid_argument(EBadTxHashLength)
);
TxContext { signer, tx_hash, ids_created }
TxContext { signer, tx_hash, epoch: 0, ids_created }
}

#[test_only]
/// Create a `TxContext` for testing, with a potentially non-zero epoch number.
public fun new_with_epoch(signer: signer, tx_hash: vector<u8>, epoch: u64, ids_created: u64): TxContext {
assert!(
Vector::length(&tx_hash) == TX_HASH_LENGTH,
Errors::invalid_argument(EBadTxHashLength)
);
TxContext { signer, tx_hash, epoch, ids_created }
}

#[test_only]
Expand Down
2 changes: 2 additions & 0 deletions crates/sui-transactional-test-runner/src/test_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,8 @@ impl<'a> SuiTestAdapter<'a> {
&self.vm,
&self.native_functions,
gas_status,
// TODO: Support different epochs in transactional tests.
0,
)?;
let (_objects, _active_inputs, written, deleted, _events) = temporary_store.into_inner();
let created_set: BTreeSet<_> = created.iter().map(|((id, _, _), _)| *id).collect();
Expand Down
7 changes: 6 additions & 1 deletion crates/sui-types/src/base_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use schemars::JsonSchema;
use serde_with::serde_as;
use serde_with::Bytes;

use crate::committee::EpochId;
use sha3::Sha3_256;

#[cfg(test)]
Expand Down Expand Up @@ -224,15 +225,18 @@ pub struct TxContext {
sender: AccountAddress,
/// Digest of the current transaction
digest: Vec<u8>,
/// The current epoch number
epoch: EpochId,
/// Number of `ObjectID`'s generated during execution of the current transaction
ids_created: u64,
}

impl TxContext {
pub fn new(sender: &SuiAddress, digest: &TransactionDigest) -> Self {
pub fn new(sender: &SuiAddress, digest: &TransactionDigest, epoch: EpochId) -> Self {
Self {
sender: AccountAddress::new(sender.0),
digest: digest.0.to_vec(),
epoch,
ids_created: 0,
}
}
Expand Down Expand Up @@ -274,6 +278,7 @@ impl TxContext {
Self::new(
&SuiAddress::random_for_testing_only(),
&TransactionDigest::random(),
0,
)
}

Expand Down

0 comments on commit 609b567

Please sign in to comment.