Skip to content

Commit

Permalink
final feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
dariorussi committed Mar 1, 2025
1 parent 16a43c5 commit ab4f906
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 89 deletions.
74 changes: 51 additions & 23 deletions crates/sui-framework/packages/sui-framework/sources/tx_context.move
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ const TX_HASH_LENGTH: u64 = 32;
/// Expected an tx hash of length 32, but found a different length
const EBadTxHashLength: u64 = 0;

#[test_only]
/// Attempt to get the most recent created object ID when none has been created.
const ENoIDsCreated: u64 = 1;

#[allow(unused_field)]
/// Information about the transaction currently being executed.
/// This cannot be constructed by a transaction--it is a privileged object created by
Expand Down Expand Up @@ -60,9 +56,8 @@ native fun native_epoch_timestamp_ms(): u64;

/// Return the adress of the transaction sponsor or `None` if there was no sponsor.
public fun sponsor(_self: &TxContext): Option<address> {
native_sponsor()
option_sponsor()
}
native fun native_sponsor(): Option<address>;

/// Create an `address` that has not been used. As it is an object address, it will never
/// occur as the address for a user.
Expand Down Expand Up @@ -100,7 +95,16 @@ public fun new(
ids_created: u64,
): TxContext {
assert!(tx_hash.length() == TX_HASH_LENGTH, EBadTxHashLength);
replace(sender, tx_hash, epoch, epoch_timestamp_ms, ids_created);
replace(
sender,
tx_hash,
epoch,
epoch_timestamp_ms,
ids_created,
native_gas_price(),
native_gas_budget(),
native_sponsor(),
);
// return an empty TxContext given all the info is held on the native side (call above)
TxContext {
sender: @0x0,
Expand Down Expand Up @@ -147,33 +151,50 @@ public fun get_ids_created(self: &TxContext): u64 {
#[test_only]
/// Return the most recent created object ID.
public fun last_created_object_id(self: &TxContext): address {
let ids_created = self.ids_created();
assert!(ids_created > 0, ENoIDsCreated);
derive_id(*self.digest(), ids_created - 1)
last_created_id()
}
#[test_only]
/// Native function for deriving an ID via hash(tx_hash || ids_created)
native fun derive_id(tx_hash: vector<u8>, ids_created: u64): address;
native fun last_created_id(): address;

#[test_only]
public fun increment_epoch_number(self: &mut TxContext) {
let sender = self.sender();
let tx_hash = *self.digest();
let epoch = self.epoch() + 1;
let epoch_timestamp_ms = self.epoch_timestamp_ms();
let ids_created = self.ids_created();
replace(sender, tx_hash, epoch, epoch_timestamp_ms, ids_created);
replace(
native_sender(),
self.tx_hash,
native_epoch(),
epoch,
native_ids_created(),
native_gas_price(),
native_gas_budget(),
native_sponsor(),
);
}

#[test_only]
public fun increment_epoch_timestamp(self: &mut TxContext, delta_ms: u64) {
let sender = self.sender();
let tx_hash = *self.digest();
let epoch = self.epoch();
let epoch_timestamp_ms = self.epoch_timestamp_ms() + delta_ms;
let ids_created = self.ids_created();
replace(sender, tx_hash, epoch, epoch_timestamp_ms, ids_created);
replace(
native_sender(),
self.tx_hash,
native_epoch(),
epoch_timestamp_ms,
native_ids_created(),
native_gas_price(),
native_gas_budget(),
native_sponsor(),
);
}

fun option_sponsor(): Option<address> {
let sponsor = native_sponsor();
if (sponsor.length() == 0) {
option::none()
} else {
option::some(sponsor[0])
}
}
native fun native_sponsor(): vector<address>;

#[test_only]
native fun replace(
Expand All @@ -182,4 +203,11 @@ native fun replace(
epoch: u64,
epoch_timestamp_ms: u64,
ids_created: u64,
);
gas_price: u64,
gas_budget: u64,
sponsor: vector<address>,
);

#[allow(unused_function)]
/// Native function for deriving an ID via hash(tx_hash || ids_created)
native fun derive_id(tx_hash: vector<u8>, ids_created: u64): address;
6 changes: 6 additions & 0 deletions crates/sui-types/src/base_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1199,12 +1199,18 @@ impl TxContext {
epoch: u64,
epoch_timestamp_ms: u64,
ids_created: u64,
gas_price: u64,
gas_budget: u64,
sponsor: Option<AccountAddress>,
) {
self.sender = sender;
self.digest = tx_hash;
self.epoch = epoch;
self.epoch_timestamp_ms = epoch_timestamp_ms;
self.ids_created = ids_created;
self.gas_price = gas_price;
self.gas_budget = gas_budget;
self.sponsor = sponsor;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1580,6 +1580,16 @@ impl VMValueCast<Vec<u64>> for Value {
}
}

impl VMValueCast<Vec<AccountAddress>> for Value {
fn cast(self) -> PartialVMResult<Vec<AccountAddress>> {
match self.0 {
ValueImpl::Container(Container::VecAddress(r)) => take_unique_ownership(r),
v => Err(PartialVMError::new(StatusCode::INTERNAL_TYPE_ERROR)
.with_message(format!("cannot cast {:?} to vector<AccountAddress>", v,))),
}
}
}

impl VMValueCast<Vec<Value>> for Value {
fn cast(self) -> PartialVMResult<Vec<Value>> {
match self.0 {
Expand Down
5 changes: 5 additions & 0 deletions sui-execution/latest/sui-move-natives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,11 @@ pub fn all_natives(silent: bool, protocol_config: &ProtocolConfig) -> NativeFunc
"receive_impl",
make_native!(transfer::receive_object_internal),
),
(
"tx_context",
"last_created_id",
make_native!(tx_context::last_created_id),
),
(
"tx_context",
"derive_id",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ impl TransactionContext {
epoch: u64,
epoch_timestamp_ms: u64,
ids_created: u64,
gas_price: u64,
gas_budget: u64,
sponsor: Option<AccountAddress>,
) -> PartialVMResult<()> {
if !self.test_only {
return Err(
Expand All @@ -94,6 +97,9 @@ impl TransactionContext {
epoch,
epoch_timestamp_ms,
ids_created,
gas_price,
gas_budget,
sponsor,
);
Ok(())
}
Expand Down
Loading

0 comments on commit ab4f906

Please sign in to comment.