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

Add convenience API for getting transaction ID without ConsensusParameters #419

Merged
merged 1 commit into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 12 additions & 0 deletions fuel-tx/src/transaction/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ use fuel_types::Bytes32;
pub trait UniqueIdentifier {
/// The unique identifier of the transaction is based on its content.
fn id(&self, params: &ConsensusParameters) -> Bytes32;

/// The cached unique identifier of the transaction.
/// Returns None if transaction was not precomputed.
fn cached_id(&self) -> Option<Bytes32>;
}

impl UniqueIdentifier for Transaction {
Expand All @@ -18,6 +22,14 @@ impl UniqueIdentifier for Transaction {
Self::Mint(mint) => mint.id(params),
}
}

fn cached_id(&self) -> Option<Bytes32> {
match self {
Transaction::Script(script) => script.cached_id(),
Transaction::Create(create) => create.cached_id(),
Self::Mint(mint) => mint.cached_id(),
}
}
}

/// Means that transaction can be singed.
Expand Down
10 changes: 7 additions & 3 deletions fuel-tx/src/transaction/types/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::transaction::{
metadata::CommonMetadata,
validity::{check_common_part, FormatValidityChecks},
};
use crate::{Chargeable, CheckError, ConsensusParameters, Contract, Input, Output, StorageSlot, Witness};
use crate::{Chargeable, CheckError, ConsensusParameters, Contract, Input, Output, StorageSlot, TxId, Witness};
use derivative::Derivative;
use fuel_types::{bytes, AssetId, BlockHeight, Salt, Word};
use fuel_types::{
Expand Down Expand Up @@ -60,8 +60,8 @@ mem_layout!(

#[cfg(feature = "std")]
impl crate::UniqueIdentifier for Create {
fn id(&self, params: &ConsensusParameters) -> fuel_types::Bytes32 {
if let Some(CommonMetadata { id, .. }) = self.metadata {
fn id(&self, params: &ConsensusParameters) -> TxId {
if let Some(id) = self.cached_id() {
return id;
}

Expand All @@ -74,6 +74,10 @@ impl crate::UniqueIdentifier for Create {

compute_transaction_id(params, &mut clone)
}

fn cached_id(&self) -> Option<TxId> {
self.metadata.as_ref().map(|m| m.id)
}
}

impl Chargeable for Create {
Expand Down
6 changes: 5 additions & 1 deletion fuel-tx/src/transaction/types/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,17 @@ mem_layout!(
#[cfg(feature = "std")]
impl crate::UniqueIdentifier for Mint {
fn id(&self, params: &ConsensusParameters) -> Bytes32 {
if let Some(MintMetadata { id, .. }) = self.metadata {
if let Some(id) = self.cached_id() {
return id;
}

let mut clone = self.clone();
compute_transaction_id(params, &mut clone)
}

fn cached_id(&self) -> Option<Bytes32> {
self.metadata.as_ref().map(|m| m.id)
}
}

impl FormatValidityChecks for Mint {
Expand Down
10 changes: 5 additions & 5 deletions fuel-tx/src/transaction/types/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,7 @@ impl Default for Script {
#[cfg(feature = "std")]
impl crate::UniqueIdentifier for Script {
fn id(&self, params: &ConsensusParameters) -> Bytes32 {
if let Some(ScriptMetadata {
common: CommonMetadata { id, .. },
..
}) = self.metadata
{
if let Some(id) = self.cached_id() {
return id;
}

Expand All @@ -104,6 +100,10 @@ impl crate::UniqueIdentifier for Script {

compute_transaction_id(params, &mut clone)
}

fn cached_id(&self) -> Option<Bytes32> {
self.metadata.as_ref().map(|m| m.common.id)
}
}

impl Chargeable for Script {
Expand Down
9 changes: 9 additions & 0 deletions fuel-vm/src/checked_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ impl<Tx: IntoChecked> Checked<Tx> {
}
}

impl<Tx: IntoChecked + UniqueIdentifier> Checked<Tx> {
/// Returns the transaction ID from the computed metadata
pub fn id(&self) -> TxId {
self.transaction
.cached_id()
.expect("Transaction metadata should be computed for checked transactions")
}
}

#[cfg(feature = "test-helpers")]
impl<Tx: IntoChecked + Default> Default for Checked<Tx>
where
Expand Down