Skip to content

Commit

Permalink
Remove non-exhaustive attribute for versionable types (#668)
Browse files Browse the repository at this point in the history
* Remove non-exhaustive attribute, appease Clippy-sama

* Update CHANGELOG

* Empty

* Fix publish check

* Missed , default-features = false

* Missed , default-features = false

* Revert some other related changes

* Just check

* Revert "Just check"

This reverts commit 66bff7d.

* Just check

* use config from `fuel-core`

* Revert "Just check"

This reverts commit 7986995.

* Revert "Revert "Just check""

This reverts commit 52f488f.

* Revert "Just check"

This reverts commit 66bff7d.

* Revert "Missed , default-features = false"

This reverts commit 4e152c4.

* Revert "Missed , default-features = false"

This reverts commit 8a99682.

* Revert "Fix publish check"

This reverts commit 68232e0.

---------

Co-authored-by: xgreenx <xgreenx9999@gmail.com>
  • Loading branch information
MitchTurner and xgreenx authored Jan 30, 2024
1 parent db87869 commit 766e6b8
Show file tree
Hide file tree
Showing 20 changed files with 76 additions and 45 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ jobs:
uses: xgreenx/publish-crates@v1
with:
dry-run: true
check-repo: false
ignore-unpublished-changes: true

publish:
# Only do this job if publishing a release and all checks pass.
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

#### Breaking

- [#668](https://github.com/FuelLabs/fuel-vm/pull/668): Remove `non_exhaustive` from versionable types for security reasons

## [Version 0.44.0]

#### Changed
Expand Down
8 changes: 4 additions & 4 deletions fuel-asm/src/args/wideint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ mod tests {
#[test]
fn decode_encode_compare() {
for imm in 0..Imm06::MAX.0 {
let bits = Imm06::try_from(imm).unwrap();
let bits = Imm06::from(imm);
if let Some(decoded) = CompareArgs::from_imm(bits) {
assert_eq!(decoded.to_imm().0, imm);
}
Expand All @@ -204,7 +204,7 @@ mod tests {
#[test]
fn decode_encode_mathop() {
for imm in 0..Imm06::MAX.0 {
let bits = Imm06::try_from(imm).unwrap();
let bits = Imm06::from(imm);
if let Some(decoded) = MathArgs::from_imm(bits) {
assert_eq!(decoded.to_imm().0, imm);
}
Expand All @@ -227,7 +227,7 @@ mod tests {
#[test]
fn decode_encode_mul() {
for imm in 0..Imm06::MAX.0 {
let bits = Imm06::try_from(imm).unwrap();
let bits = Imm06::from(imm);
if let Some(decoded) = MulArgs::from_imm(bits) {
assert_eq!(decoded.to_imm().0, imm);
}
Expand All @@ -244,7 +244,7 @@ mod tests {
#[test]
fn decode_encode_div() {
for imm in 0..Imm06::MAX.0 {
let bits = Imm06::try_from(imm).unwrap();
let bits = Imm06::from(imm);
if let Some(decoded) = DivArgs::from_imm(bits) {
assert_eq!(decoded.to_imm().0, imm);
}
Expand Down
7 changes: 3 additions & 4 deletions fuel-asm/src/encoding_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,16 @@ fn panic_reason_description() {

for r in PanicReason::iter() {
let b = r as u8;
let r_p = PanicReason::try_from(b).expect("Should get panic reason");
let r_p = PanicReason::from(b);
let w = Word::from(r as u8);
let r_q = PanicReason::try_from(u8::try_from(w).unwrap())
.expect("Should get panic reason");
let r_q = PanicReason::from(u8::try_from(w).unwrap());
assert_eq!(r, r_p);
assert_eq!(r, r_q);

let op = op::ji(imm24);
let pd = PanicInstruction::error(r, op.into());
let w = Word::from(pd);
let pd_p = PanicInstruction::try_from(w).expect("Should get panic reason");
let pd_p = PanicInstruction::from(w);
assert_eq!(pd, pd_p);

#[cfg(feature = "serde")]
Expand Down
2 changes: 1 addition & 1 deletion fuel-asm/src/panic_reason.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ mod tests {
assert_eq!(reason, PanicReason::UnknownPanicReason);

for i in 1..last_known_panic_reason {
let reason = PanicReason::try_from(i).unwrap();
let reason = PanicReason::from(i);
let i2 = reason as u8;
assert_eq!(i, i2);
}
Expand Down
2 changes: 1 addition & 1 deletion fuel-merkle/src/sparse/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ where
{
let mut hash = Hash::new();
hash.update(data);
hash.finalize().try_into().unwrap()
hash.finalize().into()
}
7 changes: 2 additions & 5 deletions fuel-merkle/src/sparse/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ impl MerkleTreeKey {
use digest::Digest;
let mut hash = sha2::Sha256::new();
hash.update(storage_key.as_ref());
let hash = hash
.finalize()
.try_into()
.expect("`sha2::Sha256` can't fail during hashing");
let hash = hash.finalize().into();

Self(hash)
}
Expand Down Expand Up @@ -400,7 +397,7 @@ where
let key = key.into();
let (path_nodes, side_nodes): (Vec<Node>, Vec<Node>) = self.path_set(key)?;

match path_nodes.get(0) {
match path_nodes.first() {
Some(node) if node.leaf_key() == &key => {
self.delete_with_path_set(
&key,
Expand Down
2 changes: 1 addition & 1 deletion fuel-merkle/src/sparse/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Node {
hash.update(prefix);
hash.update(bytes_lo);
hash.update(bytes_hi);
hash.finalize().try_into().unwrap()
hash.finalize().into()
}

pub fn max_height() -> u32 {
Expand Down
4 changes: 2 additions & 2 deletions fuel-merkle/src/sum/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn node_sum(lhs_fee: u64, lhs_data: &[u8], rhs_fee: u64, rhs_data: &[u8]) ->
hash.update(lhs_data);
hash.update(rhs_fee.to_be_bytes());
hash.update(rhs_data);
hash.finalize().try_into().unwrap()
hash.finalize().into()
}

// Merkle tree hash of a list with one entry
Expand All @@ -32,5 +32,5 @@ pub fn leaf_sum(fee: u64, data: &[u8]) -> Bytes32 {
hash.update(Prefix::Leaf);
hash.update(fee.to_be_bytes());
hash.update(data);
hash.finalize().try_into().unwrap()
hash.finalize().into()
}
5 changes: 2 additions & 3 deletions fuel-merkle/test-helpers/src/binary/hash.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use digest::Digest;
use sha2::Sha256 as Hash;
use std::convert::TryInto;

pub type Data = [u8; 32];

Expand All @@ -26,7 +25,7 @@ pub fn node_sum(lhs_data: &[u8], rhs_data: &[u8]) -> Data {
hash.update([NODE]);
hash.update(lhs_data);
hash.update(rhs_data);
hash.finalize().try_into().unwrap()
hash.finalize().into()
}

// Merkle tree hash of a list with one entry
Expand All @@ -35,5 +34,5 @@ pub fn leaf_sum(data: &[u8]) -> Data {
let mut hash = Hash::new();
hash.update([LEAF]);
hash.update(data);
hash.finalize().try_into().unwrap()
hash.finalize().into()
}
3 changes: 1 addition & 2 deletions fuel-merkle/test-helpers/src/suites/binary_proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ use rand::seq::IteratorRandom;
use rand_pcg::Pcg64;
use rand_seeder::Seeder;
use sha2::Sha256;
use std::convert::TryInto;

type Hash = Sha256;

pub fn sum(data: &[u8]) -> Bytes32 {
let mut hash = Hash::new();
hash.update(data);
hash.finalize().try_into().unwrap()
hash.finalize().into()
}

fn generate_test(
Expand Down
1 change: 0 additions & 1 deletion fuel-tx/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ 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: 0 additions & 1 deletion fuel-tx/src/transaction/types/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ 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
14 changes: 7 additions & 7 deletions fuel-tx/src/transaction/types/input/snapshot_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn tx_with_signed_coin_snapshot() {
.finalize_as_transaction();

let bytes = tx.to_bytes();
let hex = hex::encode(&bytes);
let hex = hex::encode(bytes);
insta::assert_snapshot!(hex);
}

Expand All @@ -51,7 +51,7 @@ fn tx_with_predicate_coin_snapshot() {
.finalize_as_transaction();

let bytes = tx.to_bytes();
let hex = hex::encode(&bytes);
let hex = hex::encode(bytes);
insta::assert_snapshot!(hex);
}

Expand All @@ -71,7 +71,7 @@ fn tx_with_contract_snapshot() {
.finalize_as_transaction();

let bytes = tx.to_bytes();
let hex = hex::encode(&bytes);
let hex = hex::encode(bytes);
insta::assert_snapshot!(hex);
}

Expand All @@ -96,7 +96,7 @@ fn tx_with_signed_message_coin() {
.finalize_as_transaction();

let bytes = tx.to_bytes();
let hex = hex::encode(&bytes);
let hex = hex::encode(bytes);
insta::assert_snapshot!(hex);
}

Expand All @@ -120,7 +120,7 @@ fn tx_with_predicate_message_coin() {
.finalize_as_transaction();

let bytes = tx.to_bytes();
let hex = hex::encode(&bytes);
let hex = hex::encode(bytes);
insta::assert_snapshot!(hex);
}

Expand All @@ -145,7 +145,7 @@ fn tx_with_signed_message_data() {
.finalize_as_transaction();

let bytes = tx.to_bytes();
let hex = hex::encode(&bytes);
let hex = hex::encode(bytes);
insta::assert_snapshot!(hex);
}

Expand All @@ -169,6 +169,6 @@ fn tx_with_predicate_message_data() {
.finalize_as_transaction();

let bytes = tx.to_bytes();
let hex = hex::encode(&bytes);
let hex = hex::encode(bytes);
insta::assert_snapshot!(hex);
}
1 change: 0 additions & 1 deletion fuel-tx/src/transaction/types/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ 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: 41 additions & 0 deletions fuel-tx/test-helpers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ mod use_std {
Mint,
Output,
Script,
Transaction,
TransactionBuilder,
};
use fuel_types::canonical::Deserialize;
use rand::{
distributions::{
Distribution,
Expand Down Expand Up @@ -89,6 +91,45 @@ 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
6 changes: 0 additions & 6 deletions fuel-vm/src/checked_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,6 @@ 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 @@ -387,7 +385,6 @@ 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 @@ -403,7 +400,6 @@ impl EstimatePredicates for Transaction {
create.estimate_predicates_async::<E>(params).await
}
Transaction::Mint(_) => Ok(()),
_ => Err(CheckError::UnknownVariant(alloc::format!("{:?}", &self))),
}
}
}
Expand Down Expand Up @@ -515,7 +511,6 @@ impl From<Checked<Transaction>> for CheckedTransaction {
(Transaction::Script(_), _) => unreachable!(),
(Transaction::Create(_), _) => unreachable!(),
(Transaction::Mint(_), _) => unreachable!(),
(_, _) => unreachable!(),
}
}
}
Expand Down Expand Up @@ -614,7 +609,6 @@ impl IntoChecked for Transaction {
.into();
Ok((transaction.into(), metadata.into()))
}
_ => Err(CheckError::UnknownVariant(alloc::format!("{:?}", self))),
}
.map(|(transaction, metadata)| Checked::basic(transaction, metadata))
}
Expand Down
1 change: 0 additions & 1 deletion fuel-vm/src/checked_transaction/balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ where
retryable_balance += *amount;
}
Input::Contract(_) => {}
_ => {}
}
}

Expand Down
2 changes: 1 addition & 1 deletion fuel-vm/src/tests/alu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ fn alu_wrapping(

assert_eq!(log_receipt.ra().expect("$ra expected"), expected);

let expected_of: u64 = expected_of.try_into().unwrap();
let expected_of: u64 = expected_of.into();
assert_eq!(
log_receipt.rb().expect("$rb (value of RegId::OF) expected"),
expected_of
Expand Down
Loading

0 comments on commit 766e6b8

Please sign in to comment.