Skip to content

Commit

Permalink
Add TxContext::get_last_created_object_id (#1371)
Browse files Browse the repository at this point in the history
  • Loading branch information
lxfind committed Apr 19, 2022
1 parent 03a4b00 commit c1e46f0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
20 changes: 17 additions & 3 deletions sui_programmability/framework/sources/TxContext.move
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ module Sui::TxContext {
use Std::Errors;
#[test_only]
use Std::Vector;
#[test_only]
use Sui::ID::ID;

/// Number of bytes in an tx hash (which will be the transaction digest)
const TX_HASH_LENGTH: u64 = 32;

/// Expected an tx hash of length 32, but found a different length
const EBAD_TX_HASH_LENGTH: u64 = 0;

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

/// Information about the transaction currently being executed.
/// This cannot be constructed by a transaction--it is a privileged object created by
/// the VM and passed in to the entrypoint of the transaction as `&mut TxContext`.
Expand Down Expand Up @@ -43,7 +49,7 @@ module Sui::TxContext {
/// Generate a new, globally unique object ID with version 0
public fun new_id(ctx: &mut TxContext): VersionedID {
let ids_created = ctx.ids_created;
let id = ID::new_versioned_id(fresh_id(*&ctx.tx_hash, ids_created));
let id = ID::new_versioned_id(derive_id(*&ctx.tx_hash, ids_created));
ctx.ids_created = ids_created + 1;
id
}
Expand All @@ -54,8 +60,8 @@ module Sui::TxContext {
self.ids_created
}

/// Native function for deriving an ID via hash(tx_hash || ids_created || domain_separator)
native fun fresh_id(tx_hash: vector<u8>, ids_created: u64): address;
/// Native function for deriving an ID via hash(tx_hash || ids_created)
native fun derive_id(tx_hash: vector<u8>, ids_created: u64): address;

// ==== test-only functions ====

Expand Down Expand Up @@ -100,6 +106,14 @@ module Sui::TxContext {
ids_created(self)
}

#[test_only]
/// Return the most recent created object ID.
public fun get_last_created_object_id(self: &TxContext): ID {
let ids_created = self.ids_created;
assert!(ids_created > 0, ENO_IDS_CREATED);
ID::new(derive_id(*&self.tx_hash, ids_created - 1))
}

#[test_only]
/// Test-only function for creating a new signer from `signer_address`.
native fun new_signer_from_address(signer_address: address): signer;
Expand Down
2 changes: 1 addition & 1 deletion sui_programmability/framework/src/natives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub fn all_natives(
("Transfer", "transfer_internal", transfer::transfer_internal),
("Transfer", "freeze_object", transfer::freeze_object),
("Transfer", "share_object", transfer::share_object),
("TxContext", "fresh_id", tx_context::fresh_id),
("TxContext", "derive_id", tx_context::derive_id),
(
"TxContext",
"new_signer_from_address",
Expand Down
2 changes: 1 addition & 1 deletion sui_programmability/framework/src/natives/tx_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use smallvec::smallvec;
use std::{collections::VecDeque, convert::TryFrom};
use sui_types::base_types::TransactionDigest;

pub fn fresh_id(
pub fn derive_id(
context: &mut NativeContext,
ty_args: Vec<Type>,
mut args: VecDeque<Value>,
Expand Down
15 changes: 15 additions & 0 deletions sui_programmability/framework/tests/TestScenarioTests.move
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Sui::TestScenarioTests {
use Sui::ID;
use Sui::TestScenario;
use Sui::Transfer;
use Sui::TxContext;

const ID_BYTES_MISMATCH: u64 = 0;
const VALUE_MISMATCH: u64 = 1;
Expand Down Expand Up @@ -248,6 +249,20 @@ module Sui::TestScenarioTests {
};
}

#[test]
fun test_get_last_created_object_id() {
let sender = @0x0;
let scenario = TestScenario::begin(&sender);
{
let versioned_id = TestScenario::new_id(&mut scenario);
let id = *ID::inner(&versioned_id);
let obj = Object { id: versioned_id, value: 10 };
Transfer::transfer(obj, copy sender);
let ctx = TestScenario::ctx(&mut scenario);
assert!(id == TxContext::get_last_created_object_id(ctx), 0);
};
}

#[test]
#[expected_failure(abort_code = 100 /* ETRANSFER_SHARED_OBJECT */)]
fun test_freeze_then_transfer() {
Expand Down

1 comment on commit c1e46f0

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bench results

�[0m�[0m�[1m�[32m Finished�[0m release [optimized] target(s) in 0.33s
�[0m�[0m�[1m�[32m Running�[0m target/release/bench microbench throughput
�[2m2022-04-19T18:34:15.944863Z�[0m �[32m INFO�[0m �[2msui::benchmark::transaction_creator�[0m�[2m:�[0m Open database on path: "/tmp/DB_F7FD8FF227FA45B00191CFBF8FC76D91B3DD8567"
�[2m2022-04-19T18:34:17.308046Z�[0m �[32m INFO�[0m �[2msui_network::transport�[0m�[2m:�[0m Listening to TCP traffic on 127.0.0.1:9555
Throughout: 23076.71183625319 tps

Please sign in to comment.