Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Add native function create_uuid #8401

Merged
merged 71 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
2cd733c
Add native function create_guid
vusirikala May 26, 2023
3decce0
Append a constant to hash_arg
vusirikala May 27, 2023
c9b68ec
Add move test to verify uniqueness of guids
vusirikala May 27, 2023
4c4ef51
Charging gas for create_guid method
vusirikala May 27, 2023
fc9b372
Merge branch 'main' into satya/create-guid
vusirikala May 27, 2023
2284e55
Add gas for create guid
vusirikala May 27, 2023
2f0d2eb
Add create_guid to object.move
vusirikala May 28, 2023
80b7cbb
Change GUID to UUID
vusirikala May 31, 2023
4d7c564
Change GUID to UUID in tests
vusirikala May 31, 2023
b79d4bc
Change GUID to UUID in tests
vusirikala May 31, 2023
631d66d
Add create_object method in object.move
vusirikala May 31, 2023
bdfaa55
Fix typo
vusirikala May 31, 2023
90f30db
Fix typo
vusirikala May 31, 2023
98ad018
Merge branch 'main' into satya/create-guid
vusirikala May 31, 2023
0a00af6
fix trigger condition for build jobs
geekflyer Jun 4, 2023
f02858f
Add native_get_txn_hash
vusirikala Jun 5, 2023
b632592
Add native_get_txn_hash
vusirikala Jun 5, 2023
1c444d4
Merge branch 'main' into satya/create-guid
vusirikala Jun 5, 2023
3a616ba
Deprecate the old create_object_from_object and create_object_from_ac…
vusirikala Jun 5, 2023
f89370c
Bumped the latest_gas_version to 10
vusirikala Jun 5, 2023
83c4a21
Bumped gas version to 10
vusirikala Jun 5, 2023
8a6ff98
Changed gas feature version to 9
vusirikala Jun 7, 2023
bf29748
Moved transaction hash hashing to authenticator.rs
vusirikala Jun 7, 2023
ee02708
Add documentation to create_uuid method
vusirikala Jun 8, 2023
638e2af
Use AuthenticationKey struct for create_uuid
vusirikala Jun 8, 2023
331e728
Remove deprecated comment
vusirikala Jun 8, 2023
ce78b04
Updatd comments in object.move
vusirikala Jun 8, 2023
575e409
Changed create_uuid to use only one hash
vusirikala Jun 9, 2023
0e7206d
Merge branch 'main' into satya/create-guid
vusirikala Jun 11, 2023
16b52a4
Add create_unique_address function
vusirikala Jun 11, 2023
f1afb1e
Add drop, store capabilities to UUID
vusirikala Jun 11, 2023
29e0b62
Merge branch 'main' into satya/create-guid
vusirikala Jun 21, 2023
5308088
Resetting the create_object_from_account function
vusirikala Jun 21, 2023
9b1591b
Add comment for create_object function
vusirikala Jun 21, 2023
0424d8b
feature gating
vusirikala Jun 21, 2023
ad6f336
deprecated tag
vusirikala Jun 22, 2023
ea7fbb6
Add transaction context spec
vusirikala Jun 22, 2023
c267a23
Add a test case in transaction_context
vusirikala Jun 22, 2023
7568b15
Updated move test case
vusirikala Jun 22, 2023
f180aa0
Merge branch 'main' into satya/create-guid
vusirikala Jun 22, 2023
23f8776
Add create_token method
vusirikala Jun 22, 2023
fb9bce1
Update test cases
vusirikala Jun 22, 2023
82fec8a
rust lint
vusirikala Jun 22, 2023
41cb9af
Changed comments
vusirikala Jun 23, 2023
4df1c1c
Remove create_token method
vusirikala Jun 23, 2023
752caae
Add feature flag for test
vusirikala Jun 23, 2023
8cdf218
fix a unit test
vusirikala Jun 23, 2023
545283d
Fix unit test
vusirikala Jun 23, 2023
2cadd2c
moved unit test to object.move
vusirikala Jun 23, 2023
aad09cc
changing uuid to auid
vusirikala Jun 24, 2023
2b40d44
Merge branch 'main' into satya/create-guid
vusirikala Jun 24, 2023
06aa5b9
rust lint
vusirikala Jun 24, 2023
584cca2
Minor changes
vusirikala Jun 25, 2023
16252d8
Change uuid to auid in tests
vusirikala Jun 25, 2023
77b2002
Change uuid to auid in tests
vusirikala Jun 25, 2023
8fc34f6
changed name to generate_unique_address
vusirikala Jun 25, 2023
6cdd260
Merge branch 'main' into satya/create-guid
vusirikala Jun 25, 2023
53e4d3d
Fixed the specs which timeout
junkil-park Jun 26, 2023
c3f1a1a
Fixed a unit test
vusirikala Jun 26, 2023
6d7b0dd
Shift test from e2e-move-tests to transaction_context.move
vusirikala Jun 26, 2023
987f88f
Add a comment in object.move
vusirikala Jun 26, 2023
29725d6
rust lint
vusirikala Jun 26, 2023
b86849f
Merge branch 'main' into satya/create-guid
vusirikala Jun 26, 2023
18b4e3b
Add create_token method in token.move (#8825)
vusirikala Jun 26, 2023
e863b87
Enable auid flag in an ambassador.move unit test
vusirikala Jun 26, 2023
b5aecf0
Changed gas cost
vusirikala Jun 26, 2023
e2e05cf
changed name from generate_unique_address to generate_auid_address
vusirikala Jun 26, 2023
1e71ba9
Merge branch 'main' into satya/create-guid
vusirikala Jun 26, 2023
03b9fea
Updated a comment
vusirikala Jun 26, 2023
b54fa0c
Feature gate get_txn_hash
vusirikala Jun 26, 2023
3f0d40d
changing names in test cases
vusirikala Jun 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -112,7 +112,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
24 changes: 24 additions & 0 deletions aptos-move/framework/aptos-framework/doc/transaction_context.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@



- [Function `create_guid`](#0x1_transaction_context_create_guid)
- [Function `get_script_hash`](#0x1_transaction_context_get_script_hash)
- [Specification](#@Specification_0)
- [Function `get_script_hash`](#@Specification_0_get_script_hash)
Expand All @@ -14,6 +15,29 @@



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

## Function `create_guid`

Return a globally unique identifier


<pre><code><b>public</b> <b>fun</b> <a href="transaction_context.md#0x1_transaction_context_create_guid">create_guid</a>(): <b>address</b>
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>native</b> <b>fun</b> <a href="transaction_context.md#0x1_transaction_context_create_guid">create_guid</a>(): <b>address</b>;
</code></pre>



</details>

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

## Function `get_script_hash`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module aptos_framework::transaction_context {
/// Return a globally unique identifier
public native fun create_guid(): address;
Copy link
Contributor

Choose a reason for hiding this comment

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

Or provide get_tx_hash() and guid_counter() native functions, and implement the create_guid() in Move

Copy link
Contributor

Choose a reason for hiding this comment

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

If we go that route, then we can explore multiple solutions and leave it more to the user to dictate their preferred approach.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Isn't it generally preferable to provide as simple interface as possible, and reduce the cognitive load on the smart contract developer? In terms of functionality, the developer would achieve the same with both the options, but the developer has to put more effort into how to construct GUIDs if we expose get_tx_hash and guid_counter without an additional benefit.

Copy link
Contributor

Choose a reason for hiding this comment

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

The get_tx_hash is helpful in many scenarios, guid_counter may be just for the create_guid. And I think the new_table_handle is repeated with create_guid; if we do not reuse the guid_counter, it will produce the repeat table_handle and guid.

Copy link
Contributor

Choose a reason for hiding this comment

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


/// Return the script hash of the current entry function.
public native fun get_script_hash(): vector<u8>;
}
1 change: 1 addition & 0 deletions aptos-move/framework/src/natives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ impl GasParameters {
},
transaction_context: transaction_context::GasParameters {
get_script_hash: transaction_context::GetScriptHashGasParameters { base: 0.into() },
create_guid: transaction_context::CreateGUIDGasParameters { base: 0.into() },
},
code: code::GasParameters {
request_publish: code::RequestPublishGasParameters {
Expand Down
72 changes: 62 additions & 10 deletions aptos-move/framework/src/natives/transaction_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
use crate::natives::helpers::{make_safe_native, SafeNativeContext, SafeNativeResult};
use aptos_types::on_chain_config::{Features, TimedFeatures};
use better_any::{Tid, TidAble};
use move_core_types::gas_algebra::InternalGas;
use move_core_types::{account_address::AccountAddress, gas_algebra::InternalGas};
use move_vm_runtime::native_functions::NativeFunction;
use move_vm_types::{loaded_data::runtime_types::Type, values::Value};
use sha2::{Digest, Sha256};
use smallvec::{smallvec, SmallVec};
use std::{collections::VecDeque, fmt::Debug, sync::Arc};

Expand All @@ -15,15 +16,20 @@ use std::{collections::VecDeque, fmt::Debug, sync::Arc};
/// natives of this extension.
#[derive(Tid)]
pub struct NativeTransactionContext {
txn_hash: Vec<u8>,
/// This is the number of GUIDs (Globally unique identifiers) issued during the execution of this transaction
guid_counter: u64,
script_hash: Vec<u8>,
chain_id: u8,
}

impl NativeTransactionContext {
/// Create a new instance of a native transaction context. This must be passed in via an
/// extension into VM session functions.
pub fn new(script_hash: Vec<u8>, chain_id: u8) -> Self {
pub fn new(txn_hash: Vec<u8>, script_hash: Vec<u8>, chain_id: u8) -> Self {
Self {
txn_hash,
guid_counter: 0,
script_hash,
chain_id,
}
Expand All @@ -34,6 +40,40 @@ impl NativeTransactionContext {
}
}

/***************************************************************************************************
* native fun create_guid
*
* gas cost: base_cost
*
**************************************************************************************************/
#[derive(Clone, Debug)]
pub struct CreateGUIDGasParameters {
pub base: InternalGas,
}

fn native_create_guid(
gas_params: &CreateGUIDGasParameters,
context: &mut SafeNativeContext,
mut _ty_args: Vec<Type>,
_args: VecDeque<Value>,
) -> SafeNativeResult<SmallVec<[Value; 1]>> {
context.charge(gas_params.base)?;
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we can somehow use the charge formula from sha256 hashing instead as this is effectively just a hash operation? cc @alinush

Copy link
Contributor

@alinush alinush Jun 1, 2023

Choose a reason for hiding this comment

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

You could, sure! Just pass those SHA256 gas parameter along as an extra parameter when the native is created. We have an example of this in the algebra.move module. See here.

Copy link
Contributor

Choose a reason for hiding this comment

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

This would be a cleaner solution and would also address Alin's comment on the redundant gas schedule entry

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I talked to @vgao1996 today about both the options. He said "Both would work. Although I'd personally prefer the new gas parameter approach to decouple create_uuid from sha256."

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, my concern was is create_uuid and sha256 are conceptually separate things, and reusing the sha256 gas parameters for create_uuid seems like a leaky abstraction to me, as implies the create_uuid is implemented using sha256 under the hood. Not totally sure if this is possible at all, but what if we change the hashing algorithm in the future?

Copy link
Contributor

Choose a reason for hiding this comment

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

@alinush Ah I see your point. Again, I'm fine with either approach. Was suggesting the decoupling more from a general cleaness perspective, but if you prefer reusing the gas parameters, then just go ahead for it.

Copy link
Contributor

@alinush alinush Jun 8, 2023

Choose a reason for hiding this comment

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

Well, I defer to you if you think reusing gas parameters might NOT be a good idea. My first instinct is that duplicating gas parameters is likely to lead to forgetting to update them in all the places. Which is why I think we should re-use them....

Copy link
Contributor

Choose a reason for hiding this comment

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

Is the point here that we should be charging here cost of SHA2-256 + extra ? is that possible to specify, and have only extra be configurable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@alinush With the updated code, then UUID = Hash ( Hash(TransactionDerivedUUID) || 0xFB ). So, there are two hashes right? So, we should be charging more for UUID generation?

Copy link
Contributor

Choose a reason for hiding this comment

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

Correct, but like @igor-aptos is saying below, the CryptoHasher domain separation is not actually necessary. This is true AFAICT. So we can just do UUID = Hash ( Bytes(TransactionDerivedUUID) || 0xFB )


let mut transaction_context = context
.extensions_mut()
.get_mut::<NativeTransactionContext>();
transaction_context.guid_counter += 1;
let mut hash_arg = Vec::new();
hash_arg.extend(transaction_context.txn_hash.clone());
vusirikala marked this conversation as resolved.
Show resolved Hide resolved
hash_arg.extend(transaction_context.guid_counter.to_le_bytes().to_vec());
let hash_vec = Sha256::digest(hash_arg.as_slice()).to_vec();
Ok(smallvec![Value::address(AccountAddress::new(
hash_vec
.try_into()
.expect("Unable to convert hash vector into [u8]")
))])
}

/***************************************************************************************************
* native fun get_script_hash
*
Expand Down Expand Up @@ -67,22 +107,34 @@ fn native_get_script_hash(
#[derive(Debug, Clone)]
pub struct GasParameters {
pub get_script_hash: GetScriptHashGasParameters,
pub create_guid: CreateGUIDGasParameters,
}

pub fn make_all(
gas_params: GasParameters,
timed_features: TimedFeatures,
features: Arc<Features>,
) -> impl Iterator<Item = (String, NativeFunction)> {
let natives = [(
"get_script_hash",
make_safe_native(
gas_params.get_script_hash,
timed_features,
features,
native_get_script_hash,
let natives = [
(
"get_script_hash",
make_safe_native(
gas_params.get_script_hash,
timed_features.clone(),
features.clone(),
native_get_script_hash,
),
),
(
"create_guid",
make_safe_native(
gas_params.create_guid,
timed_features,
features,
native_create_guid,
),
),
)];
];

crate::natives::helpers::make_module_natives(natives)
}