Skip to content

Commit

Permalink
Merge pull request #149 from Entropy-Foundation/fix/txn-app-hash
Browse files Browse the repository at this point in the history
Fixed transaction parent hash calculation to match txn hash in smr-moonshot
  • Loading branch information
aregng authored Dec 30, 2024
2 parents c167cf3 + 649018f commit a0ff2c2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion aptos-move/aptos-vm/src/transaction_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl TransactionMetadata {
.map(|res| !res.is_empty())
.unwrap_or(false),
payload_type_reference,
txn_app_hash: HashValue::sha3_256_of(
txn_app_hash: HashValue::keccak_256_of(
&bcs::to_bytes(&txn).expect("Unable to serialize SignedTransaction"),
)
.to_vec(),
Expand Down
13 changes: 11 additions & 2 deletions crates/aptos-crypto/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ use std::{
fmt,
str::FromStr,
};
use tiny_keccak::{Hasher, Sha3};
use tiny_keccak::{Hasher, Sha3, Keccak};

/// A prefix used to begin the salt of every hashable structure. The salt
/// consists in this global prefix, concatenated with the specified
Expand Down Expand Up @@ -182,6 +182,15 @@ impl HashValue {
HashValue::from_keccak(sha3)
}

/// Convenience function that computes a `HashValue` internally equal to
/// the keccak_256 of a byte buffer. It will handle hasher creation, data
/// feeding and finalization.
pub fn keccak_256_of(buffer: &[u8]) -> Self {
let mut keccak = Keccak::v256();
keccak.update(buffer);
HashValue::from_keccak(keccak)
}

#[cfg(test)]
pub fn from_iter_sha3<'a, I>(buffers: I) -> Self
where
Expand All @@ -198,7 +207,7 @@ impl HashValue {
&mut self.hash[..]
}

fn from_keccak(state: Sha3) -> Self {
fn from_keccak(state: impl Hasher) -> Self {
let mut hash = Self::zero();
state.finalize(hash.as_ref_mut());
hash
Expand Down
14 changes: 9 additions & 5 deletions types/src/transaction/automated_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl AutomatedTransaction {
/// Returns the hash of the transaction.
pub fn hash(&self) -> HashValue {
*self.hash.get_or_init(|| {
HashValue::sha3_256_of(
HashValue::keccak_256_of(
&bcs::to_bytes(&self).expect("Unable to serialize AutomatedTransaction"),
)
})
Expand Down Expand Up @@ -164,7 +164,7 @@ macro_rules! value_or_missing {
#[derive(Clone, Debug)]
pub enum BuilderResult {
Success(AutomatedTransaction),
GasPriceThresholdExceeded { threshold: u64, value: u64 },
GasPriceThresholdExceeded { task_index: u64, threshold: u64, value: u64 },
MissingValue(&'static str),
}

Expand All @@ -173,8 +173,8 @@ impl BuilderResult {
Self::Success(txn)
}

pub fn gas_price_threshold_exceeded(threshold: u64, value: u64) -> BuilderResult {
Self::GasPriceThresholdExceeded { threshold, value }
pub fn gas_price_threshold_exceeded(task_index: u64, threshold: u64, value: u64) -> BuilderResult {
Self::GasPriceThresholdExceeded { task_index, threshold, value }
}
pub fn missing_value(missing: &'static str) -> BuilderResult {
Self::MissingValue(missing)
Expand Down Expand Up @@ -271,6 +271,10 @@ impl AutomatedTransactionBuilder {
self
}

pub fn expiration_timestamp_secs(&self) -> u64 {
self.expiration_timestamp_secs.unwrap_or(0)
}

/// Build an [AutomatedTransaction] instance.
/// Fails if
/// - any of the mandatory fields is missing
Expand Down Expand Up @@ -299,7 +303,7 @@ impl AutomatedTransactionBuilder {
let expiration_timestamp_secs =
value_or_missing!(expiration_timestamp_secs, "expiration_timestamp_secs");
if gas_price_cap < gas_unit_price {
return BuilderResult::gas_price_threshold_exceeded(gas_price_cap, gas_unit_price);
return BuilderResult::gas_price_threshold_exceeded(sequence_number, gas_price_cap, gas_unit_price);
}
let raw_transaction = RawTransaction::new(
sender,
Expand Down
8 changes: 8 additions & 0 deletions types/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2043,6 +2043,14 @@ impl Transaction {
}
}

pub fn try_as_automated_txn(&self) -> Option<&AutomatedTransaction> {
match self {
Transaction::AutomatedTransaction(txn) => Some(txn),
_ => None,
}
}


pub fn type_name(&self) -> &'static str {
match self {
Transaction::UserTransaction(_) => "user_transaction",
Expand Down

0 comments on commit a0ff2c2

Please sign in to comment.