Skip to content
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added
- [897](https://github.com/FuelLabs/fuel-vm/pull/897): Add new method in `IntoChecked` trait `into_checked_basic_with_id` which allow to keep the id even in case of error.

### Breaking
- [900](https://github.com/FuelLabs/fuel-vm/pull/900): Change the error variant `DuplicateMessageInputId` to `DuplicateInputNonce` which now contains a nonce instead of `MessageId` for performance improvements.
- [897](https://github.com/FuelLabs/fuel-vm/pull/897): Return `TxId` when error in precompute of transactions. From: `ValidityError` to `(TxId, ValidityError)`.

### Fixed
- [895](https://github.com/FuelLabs/fuel-vm/pull/895): Fix elided lifetimes compilation warnings that became errors after the release of rust 1.83.0.
Expand Down
14 changes: 8 additions & 6 deletions fuel-tx/src/transaction/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use crate::{
ValidityError,
};

use super::TxId;

/// Entity support metadata computation to cache results.
pub trait Cacheable {
/// The cache is already computed.
Expand All @@ -19,7 +21,7 @@ pub trait Cacheable {
fn is_computed(&self) -> bool;

/// Computes the cache for the entity.
fn precompute(&mut self, chain_id: &ChainId) -> Result<(), ValidityError>;
fn precompute(&mut self, chain_id: &ChainId) -> Result<(), (TxId, ValidityError)>;
}

impl Cacheable for super::Transaction {
Expand All @@ -34,7 +36,7 @@ impl Cacheable for super::Transaction {
}
}

fn precompute(&mut self, chain_id: &ChainId) -> Result<(), ValidityError> {
fn precompute(&mut self, chain_id: &ChainId) -> Result<(), (TxId, ValidityError)> {
match self {
Self::Script(tx) => tx.precompute(chain_id),
Self::Create(tx) => tx.precompute(chain_id),
Expand Down Expand Up @@ -62,7 +64,7 @@ pub struct CommonMetadata {
impl CommonMetadata {
/// Computes the `Metadata` for the `tx` transaction.
/// Returns `None` if the transaction is invalid.
pub fn compute<Tx>(tx: &Tx, chain_id: &ChainId) -> Result<Self, ValidityError>
pub fn compute<Tx>(tx: &Tx, chain_id: &ChainId) -> Result<Self, (TxId, ValidityError)>
where
Tx: UniqueIdentifier,
Tx: field::Inputs,
Expand All @@ -86,7 +88,7 @@ impl CommonMetadata {
let i = offset;
offset = offset
.checked_add(input.size())
.ok_or(ValidityError::SerializedInputTooLarge { index })?;
.ok_or((id, ValidityError::SerializedInputTooLarge { index }))?;
inputs_offset_at.push(i);
}

Expand All @@ -96,7 +98,7 @@ impl CommonMetadata {
let i = offset;
offset = offset
.checked_add(output.size())
.ok_or(ValidityError::SerializedOutputTooLarge { index })?;
.ok_or((id, ValidityError::SerializedOutputTooLarge { index }))?;
outputs_offset_at.push(i);
}

Expand All @@ -106,7 +108,7 @@ impl CommonMetadata {
let i = offset;
offset = offset
.checked_add(witnesses.size())
.ok_or(ValidityError::SerializedWitnessTooLarge { index })?;
.ok_or((id, ValidityError::SerializedWitnessTooLarge { index }))?;
witnesses_offset_at.push(i);
}

Expand Down
3 changes: 2 additions & 1 deletion fuel-tx/src/transaction/types/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::{
Input,
Output,
TransactionRepr,
TxId,
ValidityError,
};
use educe::Educe;
Expand Down Expand Up @@ -171,7 +172,7 @@ impl crate::Cacheable for Blob {
self.metadata.is_some()
}

fn precompute(&mut self, chain_id: &ChainId) -> Result<(), ValidityError> {
fn precompute(&mut self, chain_id: &ChainId) -> Result<(), (TxId, ValidityError)> {
self.metadata = None;
self.metadata = Some(ChargeableMetadata {
common: CommonMetadata::compute(self, chain_id)?,
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 @@ -21,6 +21,7 @@ use crate::{
PrepareSign,
StorageSlot,
TransactionRepr,
TxId,
ValidityError,
};
use educe::Educe;
Expand Down Expand Up @@ -262,11 +263,14 @@ impl crate::Cacheable for Create {
self.metadata.is_some()
}

fn precompute(&mut self, chain_id: &ChainId) -> Result<(), ValidityError> {
fn precompute(&mut self, chain_id: &ChainId) -> Result<(), (TxId, ValidityError)> {
self.metadata = None;
let common_metadata = CommonMetadata::compute(self, chain_id)?;
let body_metadata =
CreateMetadata::compute(self).map_err(|e| (common_metadata.id, e))?;
self.metadata = Some(ChargeableMetadata {
common: CommonMetadata::compute(self, chain_id)?,
body: CreateMetadata::compute(self)?,
common: common_metadata,
body: body_metadata,
});
Ok(())
}
Expand Down
3 changes: 2 additions & 1 deletion fuel-tx/src/transaction/types/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
},
ConsensusParameters,
TransactionRepr,
TxId,
TxPointer,
ValidityError,
};
Expand Down Expand Up @@ -126,7 +127,7 @@ impl crate::Cacheable for Mint {
self.metadata.is_some()
}

fn precompute(&mut self, chain_id: &ChainId) -> Result<(), ValidityError> {
fn precompute(&mut self, chain_id: &ChainId) -> Result<(), (TxId, ValidityError)> {
self.metadata = None;
self.metadata = Some(MintMetadata::compute(self, chain_id));
Ok(())
Expand Down
3 changes: 2 additions & 1 deletion fuel-tx/src/transaction/types/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use crate::{
GasCosts,
Output,
TransactionRepr,
TxId,
ValidityError,
};
use educe::Educe;
Expand Down Expand Up @@ -210,7 +211,7 @@ impl crate::Cacheable for Script {
self.metadata.is_some()
}

fn precompute(&mut self, chain_id: &ChainId) -> Result<(), ValidityError> {
fn precompute(&mut self, chain_id: &ChainId) -> Result<(), (TxId, ValidityError)> {
self.metadata = None;
self.metadata = Some(ChargeableMetadata {
common: CommonMetadata::compute(self, chain_id)?,
Expand Down
10 changes: 7 additions & 3 deletions fuel-tx/src/transaction/types/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
Input,
Output,
TransactionRepr,
TxId,
ValidityError,
};
use educe::Educe;
Expand Down Expand Up @@ -271,11 +272,14 @@ impl crate::Cacheable for Upgrade {
self.metadata.is_some()
}

fn precompute(&mut self, chain_id: &ChainId) -> Result<(), ValidityError> {
fn precompute(&mut self, chain_id: &ChainId) -> Result<(), (TxId, ValidityError)> {
self.metadata = None;
let common_metadata = CommonMetadata::compute(self, chain_id)?;
let body_metadata =
UpgradeMetadata::compute(self).map_err(|e| (common_metadata.id, e))?;
self.metadata = Some(ChargeableMetadata {
common: CommonMetadata::compute(self, chain_id)?,
body: UpgradeMetadata::compute(self)?,
common: common_metadata,
body: body_metadata,
});
Ok(())
}
Expand Down
3 changes: 2 additions & 1 deletion fuel-tx/src/transaction/types/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::{
Input,
Output,
TransactionRepr,
TxId,
ValidityError,
};
use core::ops::Deref;
Expand Down Expand Up @@ -278,7 +279,7 @@ impl crate::Cacheable for Upload {
self.metadata.is_some()
}

fn precompute(&mut self, chain_id: &ChainId) -> Result<(), ValidityError> {
fn precompute(&mut self, chain_id: &ChainId) -> Result<(), (TxId, ValidityError)> {
self.metadata = None;
self.metadata = Some(ChargeableMetadata {
common: CommonMetadata::compute(self, chain_id)?,
Expand Down
30 changes: 20 additions & 10 deletions fuel-vm/src/checked_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ pub enum CheckError {
}

/// Performs checks for a transaction
pub trait IntoChecked: FormatValidityChecks + Sized {
pub trait IntoChecked: FormatValidityChecks + UniqueIdentifier + Sized {
/// Metadata produced during the check.
type Metadata: Sized;

Expand Down Expand Up @@ -344,7 +344,17 @@ pub trait IntoChecked: FormatValidityChecks + Sized {
self,
block_height: BlockHeight,
consensus_params: &ConsensusParameters,
) -> Result<Checked<Self>, CheckError>;
) -> Result<Checked<Self>, CheckError> {
self.into_checked_basic_with_id(block_height, consensus_params)
.map_err(|(_, err)| err)
}

/// Similar to `into_checked_basic` but keep the id in case of error.
fn into_checked_basic_with_id(
self,
block_height: BlockHeight,
consensus_params: &ConsensusParameters,
) -> Result<Checked<Self>, (TxId, CheckError)>;
}

/// The parameters needed for checking a predicate
Expand Down Expand Up @@ -864,45 +874,45 @@ impl From<<Blob as IntoChecked>::Metadata> for CheckedMetadata {
impl IntoChecked for Transaction {
type Metadata = CheckedMetadata;

fn into_checked_basic(
fn into_checked_basic_with_id(
self,
block_height: BlockHeight,
consensus_params: &ConsensusParameters,
) -> Result<Checked<Self>, CheckError> {
) -> Result<Checked<Self>, (TxId, CheckError)> {
match self {
Self::Script(tx) => {
let (transaction, metadata) = tx
.into_checked_basic(block_height, consensus_params)?
.into_checked_basic_with_id(block_height, consensus_params)?
.into();
Ok((transaction.into(), metadata.into()))
}
Self::Create(tx) => {
let (transaction, metadata) = tx
.into_checked_basic(block_height, consensus_params)?
.into_checked_basic_with_id(block_height, consensus_params)?
.into();
Ok((transaction.into(), metadata.into()))
}
Self::Mint(tx) => {
let (transaction, metadata) = tx
.into_checked_basic(block_height, consensus_params)?
.into_checked_basic_with_id(block_height, consensus_params)?
.into();
Ok((transaction.into(), metadata.into()))
}
Self::Upgrade(tx) => {
let (transaction, metadata) = tx
.into_checked_basic(block_height, consensus_params)?
.into_checked_basic_with_id(block_height, consensus_params)?
.into();
Ok((transaction.into(), metadata.into()))
}
Self::Upload(tx) => {
let (transaction, metadata) = tx
.into_checked_basic(block_height, consensus_params)?
.into_checked_basic_with_id(block_height, consensus_params)?
.into();
Ok((transaction.into(), metadata.into()))
}
Self::Blob(tx) => {
let (transaction, metadata) = tx
.into_checked_basic(block_height, consensus_params)?
.into_checked_basic_with_id(block_height, consensus_params)?
.into();
Ok((transaction.into(), metadata.into()))
}
Expand Down
Loading