Skip to content

Commit

Permalink
Add convenience API for getting transaction ID without ConsensusParam…
Browse files Browse the repository at this point in the history
…eters (#419)

add api to fetch cached transaction ID, to avoid passing consensus parameters every time
  • Loading branch information
Voxelot authored Apr 7, 2023
1 parent 5aae338 commit 6dcb0ac
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
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

0 comments on commit 6dcb0ac

Please sign in to comment.