Skip to content

Commit

Permalink
Add a transaction context function to get the raw transaction hash
Browse files Browse the repository at this point in the history
This commit adds a transaction context function to get the raw transaction hash.
  • Loading branch information
junkil-park committed Jun 12, 2024
1 parent bbce0f1 commit 3f6dae2
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 3 deletions.
41 changes: 38 additions & 3 deletions aptos-move/aptos-vm/src/transaction_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
// Parts of the project are originally copyright © Meta Platforms, Inc.
// SPDX-License-Identifier: Apache-2.0

use aptos_crypto::HashValue;
use aptos_crypto::{hash::CryptoHash, HashValue};
use aptos_gas_algebra::{FeePerGasUnit, Gas, NumBytes};
use aptos_types::{
account_address::AccountAddress,
chain_id::ChainId,
transaction::{
user_transaction_context::UserTransactionContext, EntryFunction, Multisig,
SignedTransaction, TransactionPayload,
authenticator::TransactionAuthenticator::{FeePayer, MultiAgent},
user_transaction_context::UserTransactionContext,
EntryFunction, Multisig, RawTransactionWithData, SignedTransaction, TransactionPayload,
},
};

Expand All @@ -32,6 +33,7 @@ pub struct TransactionMetadata {
pub is_keyless: bool,
pub entry_function_payload: Option<EntryFunction>,
pub multisig_payload: Option<Multisig>,
pub raw_txn_hash: Vec<u8>,
}

impl TransactionMetadata {
Expand Down Expand Up @@ -82,6 +84,34 @@ impl TransactionMetadata {
TransactionPayload::Multisig(m) => Some(m.clone()),
_ => None,
},
raw_txn_hash: match txn.authenticator_ref() {

Check warning on line 87 in aptos-move/aptos-vm/src/transaction_metadata.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/aptos-vm/src/transaction_metadata.rs#L87

Added line #L87 was not covered by tests
MultiAgent {
sender,
secondary_signer_addresses,
secondary_signers,
} => {
let raw_txn = RawTransactionWithData::new_multi_agent(
txn.clone().into_raw_transaction(),
secondary_signer_addresses.clone(),
);
raw_txn.hash().to_vec()

Check warning on line 97 in aptos-move/aptos-vm/src/transaction_metadata.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/aptos-vm/src/transaction_metadata.rs#L89-L97

Added lines #L89 - L97 were not covered by tests
},
FeePayer {
sender,
secondary_signer_addresses,
secondary_signers,
fee_payer_address,
fee_payer_signer,
} => {
let raw_txn = RawTransactionWithData::new_fee_payer(
txn.clone().into_raw_transaction(),
secondary_signer_addresses.clone(),
*fee_payer_address,
);
raw_txn.hash().to_vec()

Check warning on line 111 in aptos-move/aptos-vm/src/transaction_metadata.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/aptos-vm/src/transaction_metadata.rs#L100-L111

Added lines #L100 - L111 were not covered by tests
},
_ => txn.raw_transaction_ref().hash().to_vec(),

Check warning on line 113 in aptos-move/aptos-vm/src/transaction_metadata.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/aptos-vm/src/transaction_metadata.rs#L113

Added line #L113 was not covered by tests
},
}
}

Expand Down Expand Up @@ -155,6 +185,10 @@ impl TransactionMetadata {
self.multisig_payload.clone()
}

pub fn raw_txn_hash(&self) -> Vec<u8> {
self.raw_txn_hash.clone()
}

Check warning on line 190 in aptos-move/aptos-vm/src/transaction_metadata.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/aptos-vm/src/transaction_metadata.rs#L188-L190

Added lines #L188 - L190 were not covered by tests

pub fn as_user_transaction_context(&self) -> UserTransactionContext {
UserTransactionContext::new(
self.sender,
Expand All @@ -167,6 +201,7 @@ impl TransactionMetadata {
.map(|entry_func| entry_func.as_entry_function_payload()),
self.multisig_payload()
.map(|multisig| multisig.as_multisig_payload()),
self.raw_txn_hash(),

Check warning on line 204 in aptos-move/aptos-vm/src/transaction_metadata.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/aptos-vm/src/transaction_metadata.rs#L204

Added line #L204 was not covered by tests
)
}
}
49 changes: 49 additions & 0 deletions aptos-move/framework/aptos-framework/doc/transaction_context.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
- [Function `multisig_payload_internal`](#0x1_transaction_context_multisig_payload_internal)
- [Function `multisig_address`](#0x1_transaction_context_multisig_address)
- [Function `inner_entry_function_payload`](#0x1_transaction_context_inner_entry_function_payload)
- [Function `raw_txn_hash`](#0x1_transaction_context_raw_txn_hash)
- [Function `raw_txn_hash_internal`](#0x1_transaction_context_raw_txn_hash_internal)
- [Specification](#@Specification_1)
- [Function `get_txn_hash`](#@Specification_1_get_txn_hash)
- [Function `get_transaction_hash`](#@Specification_1_get_transaction_hash)
Expand Down Expand Up @@ -963,6 +965,53 @@ Returns the inner entry function payload of the multisig payload.



</details>

<a id="0x1_transaction_context_raw_txn_hash"></a>

## Function `raw_txn_hash`

Returns the hash of the current raw transaction.


<pre><code><b>public</b> <b>fun</b> <a href="transaction_context.md#0x1_transaction_context_raw_txn_hash">raw_txn_hash</a>(): <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="transaction_context.md#0x1_transaction_context_raw_txn_hash">raw_txn_hash</a>(): <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt; {
<a href="transaction_context.md#0x1_transaction_context_raw_txn_hash_internal">raw_txn_hash_internal</a>()
}
</code></pre>



</details>

<a id="0x1_transaction_context_raw_txn_hash_internal"></a>

## Function `raw_txn_hash_internal`



<pre><code><b>fun</b> <a href="transaction_context.md#0x1_transaction_context_raw_txn_hash_internal">raw_txn_hash_internal</a>(): <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>native</b> <b>fun</b> <a href="transaction_context.md#0x1_transaction_context_raw_txn_hash_internal">raw_txn_hash_internal</a>(): <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;;
</code></pre>



</details>

<a id="@Specification_1"></a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ module aptos_framework::transaction_context {
payload.entry_function_payload
}

/// Returns the hash of the current raw transaction.
public fun raw_txn_hash(): vector<u8> {
raw_txn_hash_internal()
}
native fun raw_txn_hash_internal(): vector<u8>;

#[test()]
fun test_auid_uniquess() {
use std::vector;
Expand Down Expand Up @@ -259,4 +265,11 @@ module aptos_framework::transaction_context {
// expected to fail with the error code of `invalid_state(E_TRANSACTION_CONTEXT_NOT_AVAILABLE)`
let _multisig = multisig_payload();
}

#[test]
#[expected_failure(abort_code=196609, location = Self)]
fun test_call_raw_txn_hash() {
// expected to fail with the error code of `invalid_state(E_TRANSACTION_CONTEXT_NOT_AVAILABLE)`
let _multisig = multisig_payload();
}
}
20 changes: 20 additions & 0 deletions aptos-move/framework/src/natives/transaction_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,25 @@ fn native_multisig_payload_internal(
}
}

fn native_raw_txn_hash_internal(
context: &mut SafeNativeContext,
_ty_args: Vec<Type>,
_args: VecDeque<Value>,
) -> SafeNativeResult<SmallVec<[Value; 1]>> {
context.charge(TRANSACTION_CONTEXT_SENDER_BASE)?;

Check warning on line 376 in aptos-move/framework/src/natives/transaction_context.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/framework/src/natives/transaction_context.rs#L371-L376

Added lines #L371 - L376 were not covered by tests

let user_transaction_context_opt = get_user_transaction_context_opt_from_context(context);
if let Some(transaction_context) = user_transaction_context_opt {
Ok(smallvec![Value::vector_u8(
transaction_context.raw_txn_hash()

Check warning on line 381 in aptos-move/framework/src/natives/transaction_context.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/framework/src/natives/transaction_context.rs#L378-L381

Added lines #L378 - L381 were not covered by tests
)])
} else {
Err(SafeNativeError::Abort {
abort_code: error::invalid_state(abort_codes::ETRANSACTION_CONTEXT_NOT_AVAILABLE),
})

Check warning on line 386 in aptos-move/framework/src/natives/transaction_context.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/framework/src/natives/transaction_context.rs#L384-L386

Added lines #L384 - L386 were not covered by tests
}
}

Check warning on line 388 in aptos-move/framework/src/natives/transaction_context.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/framework/src/natives/transaction_context.rs#L388

Added line #L388 was not covered by tests

fn get_user_transaction_context_opt_from_context<'a>(
context: &'a SafeNativeContext,
) -> &'a Option<UserTransactionContext> {
Expand Down Expand Up @@ -405,6 +424,7 @@ pub fn make_all(
"multisig_payload_internal",
native_multisig_payload_internal,
),
("raw_txn_hash_internal", native_raw_txn_hash_internal),
];

builder.make_named_natives(natives)
Expand Down
7 changes: 7 additions & 0 deletions types/src/transaction/user_transaction_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct UserTransactionContext {
chain_id: u8,
entry_function_payload: Option<EntryFunctionPayload>,
multisig_payload: Option<MultisigPayload>,
raw_txn_hash: Vec<u8>,
}

impl UserTransactionContext {
Expand All @@ -25,6 +26,7 @@ impl UserTransactionContext {
chain_id: u8,
entry_function_payload: Option<EntryFunctionPayload>,
multisig_payload: Option<MultisigPayload>,
raw_txn_hash: Vec<u8>,

Check warning on line 29 in types/src/transaction/user_transaction_context.rs

View check run for this annotation

Codecov / codecov/patch

types/src/transaction/user_transaction_context.rs#L29

Added line #L29 was not covered by tests
) -> Self {
Self {
sender,
Expand All @@ -35,6 +37,7 @@ impl UserTransactionContext {
chain_id,
entry_function_payload,
multisig_payload,
raw_txn_hash,

Check warning on line 40 in types/src/transaction/user_transaction_context.rs

View check run for this annotation

Codecov / codecov/patch

types/src/transaction/user_transaction_context.rs#L40

Added line #L40 was not covered by tests
}
}

Expand Down Expand Up @@ -69,6 +72,10 @@ impl UserTransactionContext {
pub fn multisig_payload(&self) -> Option<MultisigPayload> {
self.multisig_payload.clone()
}

pub fn raw_txn_hash(&self) -> Vec<u8> {
self.raw_txn_hash.clone()
}

Check warning on line 78 in types/src/transaction/user_transaction_context.rs

View check run for this annotation

Codecov / codecov/patch

types/src/transaction/user_transaction_context.rs#L76-L78

Added lines #L76 - L78 were not covered by tests
}

#[derive(Debug, Clone)]
Expand Down

0 comments on commit 3f6dae2

Please sign in to comment.