Skip to content

Commit

Permalink
Make public types versionable (#654)
Browse files Browse the repository at this point in the history
* Make types non-exhaustive and cover cases

* Update CHANGELOG

* Handle `unreachable` cases better

* Fix ref thing

* Fix no-std stuff
  • Loading branch information
MitchTurner authored Jan 11, 2024
1 parent f79b70f commit 434e946
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 48 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- [#653](https://github.com/FuelLabs/fuel-vm/pull/653): `ECAL` opcode handler can now hold internal state.

#### Breaking

- [#654](https://github.com/FuelLabs/fuel-vm/pull/654): Make public types versionable by making non-exhaustive.

## [Version 0.43.2]

### Changed
Expand Down
1 change: 1 addition & 0 deletions fuel-tx/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub type TxId = Bytes32;
#[derive(Debug, Clone, PartialEq, Eq, Hash, strum_macros::EnumCount)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[allow(clippy::large_enum_variant)]
#[non_exhaustive]
pub enum Transaction {
Script(Script),
Create(Create),
Expand Down
1 change: 1 addition & 0 deletions fuel-tx/src/transaction/types/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ where

#[derive(Debug, Clone, PartialEq, Eq, Hash, strum_macros::EnumCount)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum Input {
CoinSigned(CoinSigned),
CoinPredicate(CoinPredicate),
Expand Down
1 change: 1 addition & 0 deletions fuel-tx/src/transaction/types/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub use repr::OutputRepr;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, strum_macros::EnumCount)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Deserialize, Serialize)]
#[non_exhaustive]
pub enum Output {
Coin {
to: Address,
Expand Down
41 changes: 0 additions & 41 deletions fuel-tx/test-helpers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@ mod use_std {
Mint,
Output,
Script,
Transaction,
TransactionBuilder,
};
use fuel_types::canonical::Deserialize;
use rand::{
distributions::{
Distribution,
Expand Down Expand Up @@ -91,45 +89,6 @@ mod use_std {
let input_sampler = Uniform::from(0..Input::COUNT);
let output_sampler = Uniform::from(0..Output::COUNT);

// Trick to enforce coverage of all variants in compile-time
//
// When and if a new variant is added, this implementation enforces it will be
// listed here.
let empty: [u8; 0] = [];
debug_assert!({
Input::decode(&mut &empty[..])
.map(|i| match i {
Input::CoinSigned(_) => (),
Input::CoinPredicate(_) => (),
Input::Contract(_) => (),
Input::MessageCoinSigned(_) => (),
Input::MessageCoinPredicate(_) => (),
Input::MessageDataSigned(_) => (),
Input::MessageDataPredicate(_) => (),
})
.unwrap_or(());

Output::decode(&mut &empty[..])
.map(|o| match o {
Output::Coin { .. } => (),
Output::Contract(_) => (),
Output::Change { .. } => (),
Output::Variable { .. } => (),
Output::ContractCreated { .. } => (),
})
.unwrap_or(());

Transaction::decode(&mut &empty[..])
.map(|t| match t {
Transaction::Script(_) => (),
Transaction::Create(_) => (),
Transaction::Mint(_) => (),
})
.unwrap_or(());

true
});

Self {
rng,
input_sampler,
Expand Down
19 changes: 12 additions & 7 deletions fuel-vm/src/checked_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ pub enum CheckError {
Validity(ValidityError),
/// The predicate verification failed.
PredicateVerificationFailed(PredicateVerificationFailed),
/// Transaction is of an unknown variant
UnknownVariant(alloc::string::String),
}

/// Performs checks for a transaction
Expand Down Expand Up @@ -385,6 +387,7 @@ impl EstimatePredicates for Transaction {
Transaction::Script(script) => script.estimate_predicates(params),
Transaction::Create(create) => create.estimate_predicates(params),
Transaction::Mint(_) => Ok(()),
_ => Err(CheckError::UnknownVariant(alloc::format!("{:?}", &self))),
}
}

Expand All @@ -400,6 +403,7 @@ impl EstimatePredicates for Transaction {
create.estimate_predicates_async::<E>(params).await
}
Transaction::Mint(_) => Ok(()),
_ => Err(CheckError::UnknownVariant(alloc::format!("{:?}", &self))),
}
}
}
Expand Down Expand Up @@ -511,6 +515,7 @@ impl From<Checked<Transaction>> for CheckedTransaction {
(Transaction::Script(_), _) => unreachable!(),
(Transaction::Create(_), _) => unreachable!(),
(Transaction::Mint(_), _) => unreachable!(),
(_, _) => unreachable!(),
}
}
}
Expand Down Expand Up @@ -590,28 +595,28 @@ impl IntoChecked for Transaction {
block_height: BlockHeight,
consensus_params: &ConsensusParameters,
) -> Result<Checked<Self>, CheckError> {
let (transaction, metadata) = match self {
match self {
Transaction::Script(script) => {
let (transaction, metadata) = script
.into_checked_basic(block_height, consensus_params)?
.into();
(transaction.into(), metadata.into())
Ok((transaction.into(), metadata.into()))
}
Transaction::Create(create) => {
let (transaction, metadata) = create
.into_checked_basic(block_height, consensus_params)?
.into();
(transaction.into(), metadata.into())
Ok((transaction.into(), metadata.into()))
}
Transaction::Mint(mint) => {
let (transaction, metadata) = mint
.into_checked_basic(block_height, consensus_params)?
.into();
(transaction.into(), metadata.into())
Ok((transaction.into(), metadata.into()))
}
};

Ok(Checked::basic(transaction, metadata))
_ => Err(CheckError::UnknownVariant(alloc::format!("{:?}", self))),
}
.map(|(transaction, metadata)| Checked::basic(transaction, metadata))
}
}

Expand Down
1 change: 1 addition & 0 deletions fuel-vm/src/checked_transaction/balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ where
retryable_balance += *amount;
}
Input::Contract(_) => {}
_ => {}
}
}

Expand Down

0 comments on commit 434e946

Please sign in to comment.