Skip to content

Commit

Permalink
[RFC] Add native function create_uuid (#8401)
Browse files Browse the repository at this point in the history
* Add native function create_guid

* Append a constant to hash_arg

* Add move test to verify uniqueness of guids

* Charging gas for create_guid method

* Add gas for create guid

* Add create_guid to object.move

* Change GUID to UUID

* Change GUID to UUID in tests

* Change GUID to UUID in tests

* Add create_object method in object.move

* Fix typo

* Fix typo

* fix trigger condition for build jobs

* Add native_get_txn_hash

* Add native_get_txn_hash

* Deprecate the old create_object_from_object and create_object_from_account

* Bumped the latest_gas_version to 10

* Bumped gas version to 10

* Changed gas feature version to 9

* Moved transaction hash hashing to authenticator.rs

* Add documentation to create_uuid method

* Use AuthenticationKey struct for create_uuid

* Remove deprecated comment

* Updatd comments in object.move

* Changed create_uuid to use only one hash

* Add create_unique_address function

* Add drop, store capabilities to UUID

* Resetting the create_object_from_account function

* Add comment for create_object function

* feature gating

* deprecated tag

* Add transaction context spec

* Add a test case in transaction_context

* Updated move test case

* Add create_token method

* Update test cases

* rust lint

* Changed comments

* Remove create_token method

* Add feature flag for test

* fix a unit test

* Fix unit test

* moved unit test to object.move

* changing uuid to auid

* rust lint

* Minor changes

* Change uuid to auid in tests

* Change uuid to auid in tests

* changed name to generate_unique_address

* Fixed the specs which timeout

* Fixed a unit test

* Shift test from e2e-move-tests to transaction_context.move

* Add a comment in object.move

* rust lint

* Add create_token method in token.move (#8825)

* Add create_token method in token.move

* add deprecated flag to create_token_from_account

* Add test case and rename to create

* Add feature flag condition in test case

* enabled feature flag in the unit test

* Enabling auid feature flag in ambassador unit tests

* Enable auid flag in an ambassador.move unit test

* Changed gas cost

* changed name from generate_unique_address to generate_auid_address

* Updated a comment

* Feature gate get_txn_hash

* changing names in test cases

---------

Co-authored-by: geekflyer <[email protected]>
Co-authored-by: Junkil Park <[email protected]>
  • Loading branch information
3 people authored Jun 26, 2023
1 parent 7c2e43a commit b0b041c
Show file tree
Hide file tree
Showing 23 changed files with 880 additions and 137 deletions.
186 changes: 94 additions & 92 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions aptos-move/aptos-gas/src/aptos_framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ crate::natives::define_gas_parameters_for_natives!(GasParameters, "aptos_framewo
[.util.from_bytes.base, "util.from_bytes.base", 300 * MUL],
[.util.from_bytes.per_byte, "util.from_bytes.per_byte", 5 * MUL],

[.transaction_context.get_txn_hash.base, { 10.. => "transaction_context.get_txn_hash.base" }, 200 * MUL],
[.transaction_context.get_script_hash.base, "transaction_context.get_script_hash.base", 200 * MUL],
// Based on SHA3-256's cost
[.transaction_context.generate_unique_address.base, { 10.. => "transaction_context.generate_unique_address.base" }, 4000 * MUL],

[.code.request_publish.base, "code.request_publish.base", 500 * MUL],
[.code.request_publish.per_byte, "code.request_publish.per_byte", 2 * MUL],
Expand Down
1 change: 1 addition & 0 deletions aptos-move/aptos-gas/src/gas_meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use std::collections::BTreeMap;

// Change log:
// - V10
// - Added generate_unique_address and get_txn_hash native functions
// - Storage gas charges (excluding "storage fees") stop respecting the storage gas curves
// - V9
// - Accurate tracking of the cost of loading resource groups
Expand Down
6 changes: 5 additions & 1 deletion aptos-move/aptos-vm/src/move_vm_ext/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ impl MoveVmExt {
_ => vec![],
};

extensions.add(NativeTransactionContext::new(script_hash, self.chain_id));
extensions.add(NativeTransactionContext::new(
txn_hash.to_vec(),
script_hash,
self.chain_id,
));
extensions.add(NativeCodeContext::default());
extensions.add(NativeStateStorageContext::new(remote));

Expand Down
6 changes: 5 additions & 1 deletion aptos-move/aptos-vm/src/natives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ pub fn configure_for_unit_test() {
#[cfg(feature = "testing")]
fn unit_test_extensions_hook(exts: &mut NativeContextExtensions) {
exts.add(NativeCodeContext::default());
exts.add(NativeTransactionContext::new(vec![1], ChainId::test().id())); // We use the testing environment chain ID here
exts.add(NativeTransactionContext::new(
vec![1],
vec![1],
ChainId::test().id(),
)); // We use the testing environment chain ID here
exts.add(NativeAggregatorContext::new(
[0; 32],
&*DUMMY_RESOLVER,
Expand Down
60 changes: 56 additions & 4 deletions aptos-move/framework/aptos-framework/doc/object.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ make it so that a reference to a global object can be returned from a function.
- [Function `convert`](#0x1_object_convert)
- [Function `create_named_object`](#0x1_object_create_named_object)
- [Function `create_user_derived_object`](#0x1_object_create_user_derived_object)
- [Function `create_object`](#0x1_object_create_object)
- [Function `create_object_from_account`](#0x1_object_create_object_from_account)
- [Function `create_object_from_object`](#0x1_object_create_object_from_object)
- [Function `create_object_from_guid`](#0x1_object_create_object_from_guid)
Expand Down Expand Up @@ -87,6 +88,7 @@ make it so that a reference to a global object can be returned from a function.
<b>use</b> <a href="guid.md#0x1_guid">0x1::guid</a>;
<b>use</b> <a href="../../aptos-stdlib/../move-stdlib/doc/hash.md#0x1_hash">0x1::hash</a>;
<b>use</b> <a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">0x1::signer</a>;
<b>use</b> <a href="transaction_context.md#0x1_transaction_context">0x1::transaction_context</a>;
<b>use</b> <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">0x1::vector</a>;
</code></pre>

Expand Down Expand Up @@ -227,8 +229,7 @@ This is a one time ability given to the creator to configure the object as neces
<code>can_delete: bool</code>
</dt>
<dd>
Set to true so long as deleting the object is possible. For example, the object was
created via create_object_from_guid.
True if the object can be deleted. Named objects are not deletable.
</dd>
</dl>

Expand Down Expand Up @@ -427,6 +428,16 @@ Emitted whenever the object's owner field is changed.
## Constants


<a name="0x1_object_DERIVE_AUID_ADDRESS_SCHEME"></a>

generate_unique_address uses this for domain separation within its native implementation


<pre><code><b>const</b> <a href="object.md#0x1_object_DERIVE_AUID_ADDRESS_SCHEME">DERIVE_AUID_ADDRESS_SCHEME</a>: u8 = 251;
</code></pre>



<a name="0x1_object_ECANNOT_DELETE"></a>

The object does not allow for deletion
Expand Down Expand Up @@ -828,16 +839,51 @@ Derivde objects, similar to named objects, cannot be deleted.



</details>

<a name="0x1_object_create_object"></a>

## Function `create_object`

Create a new object by generating a random unique address based on transaction hash.
The unique address is computed sha3_256([transaction hash | auid counter | 0xFB]).
The created object is deletable as we can guarantee the same unique address can
never be regenerated with future txs.


<pre><code><b>public</b> <b>fun</b> <a href="object.md#0x1_object_create_object">create_object</a>(owner_address: <b>address</b>): <a href="object.md#0x1_object_ConstructorRef">object::ConstructorRef</a>
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="object.md#0x1_object_create_object">create_object</a>(owner_address: <b>address</b>): <a href="object.md#0x1_object_ConstructorRef">ConstructorRef</a> {
<b>let</b> unique_address = <a href="transaction_context.md#0x1_transaction_context_generate_auid_address">transaction_context::generate_auid_address</a>();
<a href="object.md#0x1_object_create_object_internal">create_object_internal</a>(owner_address, unique_address, <b>true</b>)
}
</code></pre>



</details>

<a name="0x1_object_create_object_from_account"></a>

## Function `create_object_from_account`

Use <code>create_object</code> instead.
Create a new object from a GUID generated by an account.
As the GUID creation internally increments a counter, two transactions that executes
<code>create_object_from_account</code> function for the same creator run sequentially.
Therefore, using <code>create_object</code> method for creating objects is preferrable as it
doesn't have the same bottlenecks.


<pre><code><b>public</b> <b>fun</b> <a href="object.md#0x1_object_create_object_from_account">create_object_from_account</a>(creator: &<a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a>): <a href="object.md#0x1_object_ConstructorRef">object::ConstructorRef</a>
<pre><code>#[deprecated]
<b>public</b> <b>fun</b> <a href="object.md#0x1_object_create_object_from_account">create_object_from_account</a>(creator: &<a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a>): <a href="object.md#0x1_object_ConstructorRef">object::ConstructorRef</a>
</code></pre>


Expand All @@ -860,10 +906,16 @@ Create a new object from a GUID generated by an account.

## Function `create_object_from_object`

Use <code>create_object</code> instead.
Create a new object from a GUID generated by an object.
As the GUID creation internally increments a counter, two transactions that executes
<code>create_object_from_object</code> function for the same creator run sequentially.
Therefore, using <code>create_object</code> method for creating objects is preferrable as it
doesn't have the same bottlenecks.


<pre><code><b>public</b> <b>fun</b> <a href="object.md#0x1_object_create_object_from_object">create_object_from_object</a>(creator: &<a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a>): <a href="object.md#0x1_object_ConstructorRef">object::ConstructorRef</a>
<pre><code>#[deprecated]
<b>public</b> <b>fun</b> <a href="object.md#0x1_object_create_object_from_object">create_object_from_object</a>(creator: &<a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a>): <a href="object.md#0x1_object_ConstructorRef">object::ConstructorRef</a>
</code></pre>


Expand Down
3 changes: 2 additions & 1 deletion aptos-move/framework/aptos-framework/doc/staking_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -1191,7 +1191,8 @@ StakingRewardsConfig does not exist under the aptos_framework before creating it



<pre><code><b>aborts_if</b> !<b>exists</b>&lt;<a href="staking_config.md#0x1_staking_config_StakingRewardsConfig">StakingRewardsConfig</a>&gt;(@aptos_framework);
<pre><code><b>pragma</b> verify_duration_estimate = 120;
<b>aborts_if</b> !<b>exists</b>&lt;<a href="staking_config.md#0x1_staking_config_StakingRewardsConfig">StakingRewardsConfig</a>&gt;(@aptos_framework);
<b>aborts_if</b> !<a href="../../aptos-stdlib/../move-stdlib/doc/features.md#0x1_features_spec_periodical_reward_rate_decrease_enabled">features::spec_periodical_reward_rate_decrease_enabled</a>();
<b>include</b> <a href="staking_config.md#0x1_staking_config_StakingRewardsConfigRequirement">StakingRewardsConfigRequirement</a>;
</code></pre>
Expand Down
Loading

0 comments on commit b0b041c

Please sign in to comment.