From 13837e9fd25bd9473e57ffb0c5ae034ebf7a9fe7 Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Tue, 7 Nov 2023 12:46:52 +0300 Subject: [PATCH 1/7] Split ArithmeticError --- fuel-asm/src/panic_reason.rs | 9 +++++- fuel-tx/src/transaction/validity/error.rs | 7 +++-- fuel-vm/src/arith.rs | 30 ------------------- fuel-vm/src/checked_transaction.rs | 4 +-- fuel-vm/src/checked_transaction/balances.rs | 2 +- fuel-vm/src/interpreter.rs | 6 ++-- fuel-vm/src/interpreter/balances.rs | 6 ++-- fuel-vm/src/interpreter/blockchain.rs | 7 ++--- .../src/interpreter/blockchain/other_tests.rs | 2 +- fuel-vm/src/interpreter/contract.rs | 4 +-- fuel-vm/src/interpreter/contract/tests.rs | 4 +-- fuel-vm/src/interpreter/flow.rs | 3 +- fuel-vm/src/interpreter/metadata.rs | 2 +- fuel-vm/src/lib.rs | 1 - 14 files changed, 31 insertions(+), 56 deletions(-) delete mode 100644 fuel-vm/src/arith.rs diff --git a/fuel-asm/src/panic_reason.rs b/fuel-asm/src/panic_reason.rs index 3298424930..7712c60f7c 100644 --- a/fuel-asm/src/panic_reason.rs +++ b/fuel-asm/src/panic_reason.rs @@ -103,7 +103,7 @@ enum_from! { ContractMismatch = 0x21, /// Attempting to send message data longer than `MAX_MESSAGE_DATA_LENGTH` MessageDataTooLong = 0x22, - /// Mathimatically invalid arguments where given to an arithmetic instruction. + /// Mathematically invalid arguments where given to an arithmetic instruction. /// For instance, division by zero produces this. /// These errors are ignored using the UNSAFEMATH flag. ArithmeticError = 0x23, @@ -115,6 +115,13 @@ enum_from! { InvalidInstruction = 0x26, /// Memory outside $is..$ssp range is not executable MemoryNotExecutable = 0x27, + /// Balance of a contract overflowed + BalanceOverflow = 0x28, + /// Block height value is invalid, typically because it is too large + InvalidBlockHeight = 0x29, + /// Attempt to use sequential memory instructions with too large slot count, + /// typically because it cannot fit into usize + TooManySlots = 0x2a, } } diff --git a/fuel-tx/src/transaction/validity/error.rs b/fuel-tx/src/transaction/validity/error.rs index ee1eeb9366..135f2df5c2 100644 --- a/fuel-tx/src/transaction/validity/error.rs +++ b/fuel-tx/src/transaction/validity/error.rs @@ -127,9 +127,10 @@ pub enum CheckError { /// The total amount provided by coin inputs provided: u64, }, - /// The user provided amounts for coins or gas prices that caused an arithmetic - /// overflow. - ArithmeticOverflow, + /// The given coins is too large + BalanceOverflow, + /// The given gas costs is are too large + GasCostsCoinsOverflow, /// Predicate verification failed PredicateVerificationFailed, /// Predicate used all available gas diff --git a/fuel-vm/src/arith.rs b/fuel-vm/src/arith.rs deleted file mode 100644 index 87c63959eb..0000000000 --- a/fuel-vm/src/arith.rs +++ /dev/null @@ -1,30 +0,0 @@ -//! Arithmetic functions for the Fuel VM - -use fuel_asm::{ - PanicReason, - Word, -}; - -/// Add two unchecked words, returning an error if overflow -#[inline(always)] -pub fn checked_add_word(a: Word, b: Word) -> Result { - a.checked_add(b).ok_or(PanicReason::ArithmeticOverflow) -} - -/// Subtract two unchecked words, returning an error if overflow -#[inline(always)] -pub fn checked_sub_word(a: Word, b: Word) -> Result { - a.checked_sub(b).ok_or(PanicReason::ArithmeticOverflow) -} - -/// Add two unchecked numbers, returning an error if overflow -#[inline(always)] -pub fn checked_add_usize(a: usize, b: usize) -> Result { - a.checked_add(b).ok_or(PanicReason::ArithmeticOverflow) -} - -/// Subtract two unchecked numbers, returning an error if overflow -#[inline(always)] -pub fn checked_sub_usize(a: usize, b: usize) -> Result { - a.checked_sub(b).ok_or(PanicReason::ArithmeticOverflow) -} diff --git a/fuel-vm/src/checked_transaction.rs b/fuel-vm/src/checked_transaction.rs index 7e69aa5ae6..6b3bd51178 100644 --- a/fuel-vm/src/checked_transaction.rs +++ b/fuel-vm/src/checked_transaction.rs @@ -1329,7 +1329,7 @@ mod tests { .into_checked(Default::default(), &consensus_params) .expect_err("overflow expected"); - assert_eq!(err, CheckError::ArithmeticOverflow); + assert_eq!(err, CheckError::BalanceOverflow); } #[test] @@ -1347,7 +1347,7 @@ mod tests { .into_checked(Default::default(), &consensus_params) .expect_err("overflow expected"); - assert_eq!(err, CheckError::ArithmeticOverflow); + assert_eq!(err, CheckError::BalanceOverflow); } #[test] diff --git a/fuel-vm/src/checked_transaction/balances.rs b/fuel-vm/src/checked_transaction/balances.rs index b334222fbd..c6dfe47f23 100644 --- a/fuel-vm/src/checked_transaction/balances.rs +++ b/fuel-vm/src/checked_transaction/balances.rs @@ -68,7 +68,7 @@ where // Deduct fee from base asset let fee = TransactionFee::checked_from_tx(gas_costs, params, transaction) - .ok_or(CheckError::ArithmeticOverflow)?; + .ok_or(CheckError::BalanceOverflow)?; let base_asset_balance = non_retryable_balances.entry(*base_asset_id).or_default(); diff --git a/fuel-vm/src/interpreter.rs b/fuel-vm/src/interpreter.rs index ff03e6dc0f..fff2cd48a4 100644 --- a/fuel-vm/src/interpreter.rs +++ b/fuel-vm/src/interpreter.rs @@ -431,7 +431,7 @@ pub trait ExecutableTransaction: { let gas_refund = TransactionFee::gas_refund_value(fee_params, remaining_gas, self.price()) - .ok_or(CheckError::ArithmeticOverflow)?; + .ok_or(CheckError::GasCostsCoinsOverflow)?; self.outputs_mut().iter_mut().try_for_each(|o| match o { // If revert, set base asset to initial balance and refund unused gas @@ -443,7 +443,7 @@ pub trait ExecutableTransaction: [base_asset_id] .checked_add(gas_refund) .map(|v| *amount = v) - .ok_or(CheckError::ArithmeticOverflow), + .ok_or(CheckError::BalanceOverflow), // If revert, reset any non-base asset to its initial balance Output::Change { @@ -459,7 +459,7 @@ pub trait ExecutableTransaction: } if asset_id == base_asset_id => balances[asset_id] .checked_add(gas_refund) .map(|v| *amount = v) - .ok_or(CheckError::ArithmeticOverflow), + .ok_or(CheckError::BalanceOverflow), // Set changes to the remainder provided balances Output::Change { diff --git a/fuel-vm/src/interpreter/balances.rs b/fuel-vm/src/interpreter/balances.rs index c256786fa2..8d9bbc8808 100644 --- a/fuel-vm/src/interpreter/balances.rs +++ b/fuel-vm/src/interpreter/balances.rs @@ -71,7 +71,7 @@ impl TryFrom for RuntimeBalances { let entry = balances.entry(retryable_amount.base_asset_id).or_default(); *entry = entry .checked_add(retryable_amount.amount) - .ok_or(CheckError::ArithmeticOverflow)?; + .ok_or(CheckError::BalanceOverflow)?; } Self::try_from_iter(balances) } @@ -96,7 +96,7 @@ impl RuntimeBalances { .entry(asset) .or_insert_with(|| Balance::new(0, offset)) .checked_add(balance) - .ok_or(CheckError::ArithmeticOverflow)?; + .ok_or(CheckError::BalanceOverflow)?; Ok(state) }) @@ -306,7 +306,7 @@ fn try_from_iter_wont_overflow() { let err = RuntimeBalances::try_from_iter(balances).expect_err("overflow set should fail"); - assert_eq!(CheckError::ArithmeticOverflow, err); + assert_eq!(CheckError::BalanceOverflow, err); } #[test] diff --git a/fuel-vm/src/interpreter/blockchain.rs b/fuel-vm/src/interpreter/blockchain.rs index 4179db7798..b14fb9dde4 100644 --- a/fuel-vm/src/interpreter/blockchain.rs +++ b/fuel-vm/src/interpreter/blockchain.rs @@ -30,7 +30,6 @@ use super::{ RuntimeBalances, }; use crate::{ - arith::checked_add_word, call::CallFrame, constraints::{ reg_key::*, @@ -652,7 +651,7 @@ where let asset_id = contract_id.asset_id(sub_id); let balance = balance(self.storage, contract_id, &asset_id)?; - let balance = checked_add_word(balance, a)?; + let balance = balance.checked_add(a).ok_or(PanicReason::BalanceOverflow)?; self.storage .merkle_contract_asset_id_balance_insert(contract_id, &asset_id, balance) @@ -728,7 +727,7 @@ pub(crate) fn block_hash( b: Word, ) -> IoResult<(), S::DataError> { let height = u32::try_from(b) - .map_err(|_| PanicReason::ArithmeticOverflow)? + .map_err(|_| PanicReason::InvalidBlockHeight)? .into(); let hash = storage.block_hash(height).map_err(RuntimeError::Storage)?; @@ -926,7 +925,7 @@ pub(crate) fn timestamp( b: Word, ) -> IoResult<(), S::DataError> { let b = u32::try_from(b) - .map_err(|_| PanicReason::ArithmeticOverflow)? + .map_err(|_| PanicReason::InvalidBlockHeight)? .into(); (b <= block_height) .then_some(()) diff --git a/fuel-vm/src/interpreter/blockchain/other_tests.rs b/fuel-vm/src/interpreter/blockchain/other_tests.rs index 9b170d01a5..60cebde88a 100644 --- a/fuel-vm/src/interpreter/blockchain/other_tests.rs +++ b/fuel-vm/src/interpreter/blockchain/other_tests.rs @@ -105,7 +105,7 @@ fn test_burn( #[test_case(false, 0, None, Word::MAX, [0; 32] => Ok(()); "Mint max from nothing")] #[test_case(false, 0, 0, Word::MAX, [0; 32] => Ok(()); "Mint max from zero")] #[test_case(true, 0, 100, 10, [0; 32] => Err(RuntimeError::Recoverable(PanicReason::ExpectedInternalContext)); "Can't mint from external context")] -#[test_case(false, 0, 1, Word::MAX, [0; 32] => Err(RuntimeError::Recoverable(PanicReason::ArithmeticOverflow)); "Can't mint too much")] +#[test_case(false, 0, 1, Word::MAX, [0; 32] => Err(RuntimeError::Recoverable(PanicReason::BalanceOverflow)); "Can't mint too much")] fn test_mint( external: bool, fp: Word, diff --git a/fuel-vm/src/interpreter/contract.rs b/fuel-vm/src/interpreter/contract.rs index 0186cefffc..6733d42512 100644 --- a/fuel-vm/src/interpreter/contract.rs +++ b/fuel-vm/src/interpreter/contract.rs @@ -291,7 +291,7 @@ impl<'vm, S, Tx> TransferCtx<'vm, S, Tx> { S: ContractsAssetsStorage, { let out_idx = - usize::try_from(output_index).map_err(|_| PanicReason::ArithmeticOverflow)?; + usize::try_from(output_index).map_err(|_| PanicReason::OutputNotFound)?; let to = Address::from(read_bytes(self.memory, recipient_offset)?); let asset_id = AssetId::from(read_bytes(self.memory, asset_id_offset)?); let amount = transfer_amount; @@ -386,7 +386,7 @@ where let balance = balance(storage, contract, asset_id)?; let balance = balance .checked_add(amount) - .ok_or(PanicReason::ArithmeticOverflow)?; + .ok_or(PanicReason::BalanceOverflow)?; storage .merkle_contract_asset_id_balance_insert(contract, asset_id, balance) .map_err(RuntimeError::Storage)?; diff --git a/fuel-vm/src/interpreter/contract/tests.rs b/fuel-vm/src/interpreter/contract/tests.rs index 5e23607aae..20cf9c7ea5 100644 --- a/fuel-vm/src/interpreter/contract/tests.rs +++ b/fuel-vm/src/interpreter/contract/tests.rs @@ -340,8 +340,8 @@ fn test_transfer_output( #[test_case(None, Word::MAX => Ok(()); "Can initialize balance to max")] #[test_case(0, Word::MAX => Ok(()); "Can add max to zero")] #[test_case(Word::MAX, 0 => Ok(()); "Can add zero to max")] -#[test_case(1, Word::MAX => Err(RuntimeError::Recoverable(PanicReason::ArithmeticOverflow)); "Overflowing add")] -#[test_case(Word::MAX, 1 => Err(RuntimeError::Recoverable(PanicReason::ArithmeticOverflow)); "Overflowing 1 add")] +#[test_case(1, Word::MAX => Err(RuntimeError::Recoverable(PanicReason::BalanceOverflow)); "Overflowing add")] +#[test_case(Word::MAX, 1 => Err(RuntimeError::Recoverable(PanicReason::BalanceOverflow)); "Overflowing 1 add")] fn test_balance_increase( initial: impl Into>, amount: Word, diff --git a/fuel-vm/src/interpreter/flow.rs b/fuel-vm/src/interpreter/flow.rs index 03509b3b73..f67dd2c306 100644 --- a/fuel-vm/src/interpreter/flow.rs +++ b/fuel-vm/src/interpreter/flow.rs @@ -23,7 +23,6 @@ use super::{ RuntimeBalances, }; use crate::{ - arith, call::{ Call, CallFrame, @@ -584,7 +583,7 @@ where ); let old_sp = *self.registers.system_registers.sp; - let new_sp = arith::checked_add_word(old_sp, len)?; + let new_sp = old_sp.checked_add(len).ok_or(PanicReason::MemoryOverflow)?; set_frame_pointer( self.context, diff --git a/fuel-vm/src/interpreter/metadata.rs b/fuel-vm/src/interpreter/metadata.rs index a6e0144b23..2bfc573810 100644 --- a/fuel-vm/src/interpreter/metadata.rs +++ b/fuel-vm/src/interpreter/metadata.rs @@ -145,7 +145,7 @@ impl GTFInput<'_, Tx> { where Tx: ExecutableTransaction, { - let b = usize::try_from(b).map_err(|_| PanicReason::ArithmeticOverflow)?; + let b = usize::try_from(b).map_err(|_| PanicReason::InvalidMetadataIdentifier)?; let args = GTFArgs::try_from(imm)?; let tx = self.tx; let ofs = self.tx_offset; diff --git a/fuel-vm/src/lib.rs b/fuel-vm/src/lib.rs index e9cf200e27..6630e1ff22 100644 --- a/fuel-vm/src/lib.rs +++ b/fuel-vm/src/lib.rs @@ -14,7 +14,6 @@ pub extern crate alloc; #[cfg(feature = "std")] extern crate libm as _; // Not needed with stdlib -pub mod arith; pub mod backtrace; pub mod call; pub mod checked_transaction; From 8bf6645b7611de576284a6906be2cedbaea19f62 Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Tue, 7 Nov 2023 15:15:32 +0300 Subject: [PATCH 2/7] Make depdendent gas costs work with zero dep unit cost --- fuel-tx/src/transaction/consensus_parameters/gas.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/fuel-tx/src/transaction/consensus_parameters/gas.rs b/fuel-tx/src/transaction/consensus_parameters/gas.rs index fa7de0d927..5e49d29439 100644 --- a/fuel-tx/src/transaction/consensus_parameters/gas.rs +++ b/fuel-tx/src/transaction/consensus_parameters/gas.rs @@ -599,7 +599,12 @@ impl DependentCost { } pub fn resolve(&self, units: Word) -> Word { - self.base + units.saturating_div(self.dep_per_unit) + self.base + + if self.dep_per_unit != 0 { + units.saturating_div(self.dep_per_unit) + } else { + 0 + } } } From 17f4d078f02dc01909930929230b80a1abdd385d Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Tue, 7 Nov 2023 15:49:59 +0300 Subject: [PATCH 3/7] Add changelog --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e414c45a49..60a84c18a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Added + +### Changed + +#### Breaking + +- [#625](https://github.com/FuelLabs/fuel-vm/pull/625): Use `ArithmeticError` only for arithmetic operations, and introduce new errors like `BalanceOverflow` for others. Whenever an error is internally caused by a type conversion to `usize`, so that an overflowing value wouldn't map to a valid index anyway, return the missing item error instead. Also, it's fixes a division by zero panic on depdendent costs with zero per-unit cost. + ## [Version 0.40.0] ### Added From d16eb5ffa6ec97c583aac8b6c8813bd2711c8b3a Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Tue, 7 Nov 2023 18:40:17 +0300 Subject: [PATCH 4/7] Make some to-usize conversions consistent on 32- and 64-bit platforms --- fuel-types/src/numeric_types.rs | 10 ---------- fuel-vm/src/convert.rs | 4 ++++ fuel-vm/src/interpreter/blockchain.rs | 9 +++++---- fuel-vm/src/interpreter/contract.rs | 3 ++- fuel-vm/src/interpreter/metadata.rs | 3 ++- fuel-vm/src/lib.rs | 1 + 6 files changed, 14 insertions(+), 16 deletions(-) create mode 100644 fuel-vm/src/convert.rs diff --git a/fuel-types/src/numeric_types.rs b/fuel-types/src/numeric_types.rs index e55fc331c8..6448f77862 100644 --- a/fuel-types/src/numeric_types.rs +++ b/fuel-types/src/numeric_types.rs @@ -63,16 +63,6 @@ macro_rules! key_methods { pub fn to_bytes(self) -> [u8; SIZE] { self.0.to_be_bytes() } - - /// Convert to usize. - pub const fn to_usize(self) -> usize { - self.0 as usize - } - - /// Convert to usize. - pub const fn as_usize(&self) -> usize { - self.0 as usize - } } #[cfg(feature = "typescript")] diff --git a/fuel-vm/src/convert.rs b/fuel-vm/src/convert.rs new file mode 100644 index 0000000000..fa22bf9d3b --- /dev/null +++ b/fuel-vm/src/convert.rs @@ -0,0 +1,4 @@ +/// Converts value to usize is a way that's consistet on 32-bit and 64-bit platforms. +pub(crate) fn to_usize(value: u64) -> Option { + usize::try_from(u32::try_from(value).ok()?).ok() +} diff --git a/fuel-vm/src/interpreter/blockchain.rs b/fuel-vm/src/interpreter/blockchain.rs index b14fb9dde4..38f27210e5 100644 --- a/fuel-vm/src/interpreter/blockchain.rs +++ b/fuel-vm/src/interpreter/blockchain.rs @@ -38,6 +38,7 @@ use crate::{ }, consts::*, context::Context, + convert, error::{ IoResult, RuntimeError, @@ -1042,8 +1043,7 @@ impl StateReadQWord { num_slots: Word, ownership_registers: OwnershipRegisters, ) -> SimpleResult { - let num_slots = - usize::try_from(num_slots).map_err(|_| PanicReason::ArithmeticOverflow)?; + let num_slots = convert::to_usize(num_slots).ok_or(PanicReason::TooManySlots)?; let destination_address_memory_range = MemoryRange::new( destination_memory_address, Bytes32::LEN.saturating_mul(num_slots), @@ -1166,8 +1166,9 @@ impl StateClearQWord { let start_storage_key_memory_range = CheckedMemConstLen::<{ Bytes32::LEN }>::new( start_storage_key_memory_address, )?; - let num_slots = - usize::try_from(num_slots).map_err(|_| PanicReason::ArithmeticOverflow)?; + + let num_slots = convert::to_usize(num_slots).ok_or(PanicReason::TooManySlots)?; + Ok(Self { start_storage_key_memory_range, num_slots, diff --git a/fuel-vm/src/interpreter/contract.rs b/fuel-vm/src/interpreter/contract.rs index 6733d42512..5f5200149a 100644 --- a/fuel-vm/src/interpreter/contract.rs +++ b/fuel-vm/src/interpreter/contract.rs @@ -21,6 +21,7 @@ use crate::{ }, consts::*, context::Context, + convert, error::{ IoResult, RuntimeError, @@ -291,7 +292,7 @@ impl<'vm, S, Tx> TransferCtx<'vm, S, Tx> { S: ContractsAssetsStorage, { let out_idx = - usize::try_from(output_index).map_err(|_| PanicReason::OutputNotFound)?; + convert::to_usize(output_index).ok_or(PanicReason::OutputNotFound)?; let to = Address::from(read_bytes(self.memory, recipient_offset)?); let asset_id = AssetId::from(read_bytes(self.memory, asset_id_offset)?); let amount = transfer_amount; diff --git a/fuel-vm/src/interpreter/metadata.rs b/fuel-vm/src/interpreter/metadata.rs index 2bfc573810..1e6a225bb0 100644 --- a/fuel-vm/src/interpreter/metadata.rs +++ b/fuel-vm/src/interpreter/metadata.rs @@ -8,6 +8,7 @@ use crate::{ constraints::reg_key::*, consts::*, context::Context, + convert, error::SimpleResult, }; @@ -145,7 +146,7 @@ impl GTFInput<'_, Tx> { where Tx: ExecutableTransaction, { - let b = usize::try_from(b).map_err(|_| PanicReason::InvalidMetadataIdentifier)?; + let b = convert::to_usize(b).ok_or(PanicReason::InvalidMetadataIdentifier)?; let args = GTFArgs::try_from(imm)?; let tx = self.tx; let ofs = self.tx_offset; diff --git a/fuel-vm/src/lib.rs b/fuel-vm/src/lib.rs index 6630e1ff22..abac9c0152 100644 --- a/fuel-vm/src/lib.rs +++ b/fuel-vm/src/lib.rs @@ -20,6 +20,7 @@ pub mod checked_transaction; pub mod constraints; pub mod consts; pub mod context; +mod convert; pub mod crypto; pub mod error; pub mod interpreter; From 6e972d8044cdd715c56a0aaf499733e678b6d6bd Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Wed, 8 Nov 2023 12:10:59 +0300 Subject: [PATCH 5/7] Update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a14b8da75..2a10fbc068 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). #### Breaking -- [#625](https://github.com/FuelLabs/fuel-vm/pull/625): Use `ArithmeticError` only for arithmetic operations, and introduce new errors like `BalanceOverflow` for others. Whenever an error is internally caused by a type conversion to `usize`, so that an overflowing value wouldn't map to a valid index anyway, return the missing item error instead. Also, it's fixes a division by zero panic on depdendent costs with zero per-unit cost. +- [#625](https://github.com/FuelLabs/fuel-vm/pull/625): Use `ArithmeticError` only for arithmetic operations, and introduce new errors like `BalanceOverflow` for others. Whenever an error is internally caused by a type conversion to `usize`, so that an overflowing value wouldn't map to a valid index anyway, return the missing item error instead. ## [Version 0.40.0] From e060e6e0698e1d8470773bd1aef0802efb873e0a Mon Sep 17 00:00:00 2001 From: xgreenx Date: Thu, 9 Nov 2023 15:16:34 +0000 Subject: [PATCH 6/7] Merge master --- fuel-tx/src/transaction.rs | 2 +- fuel-tx/src/transaction/policies.rs | 3 ++- fuel-vm/src/checked_transaction.rs | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/fuel-tx/src/transaction.rs b/fuel-tx/src/transaction.rs index ed11fd8dd1..3731e1524d 100644 --- a/fuel-tx/src/transaction.rs +++ b/fuel-tx/src/transaction.rs @@ -618,7 +618,7 @@ pub mod field { #[inline(always)] fn set_maturity(&mut self, block_height: BlockHeight) { self.policies_mut() - .set(PolicyType::Maturity, Some(block_height.as_usize() as u64)) + .set(PolicyType::Maturity, Some(*block_height.deref() as u64)) } } diff --git a/fuel-tx/src/transaction/policies.rs b/fuel-tx/src/transaction/policies.rs index a234382516..3d99b5cafe 100644 --- a/fuel-tx/src/transaction/policies.rs +++ b/fuel-tx/src/transaction/policies.rs @@ -9,6 +9,7 @@ use fuel_types::{ BlockHeight, Word, }; +use std::ops::Deref; #[cfg(feature = "random")] use rand::{ @@ -126,7 +127,7 @@ impl Policies { /// Sets the `maturity` policy. pub fn with_maturity(mut self, maturity: BlockHeight) -> Self { - self.set(PolicyType::Maturity, Some(maturity.as_usize() as u64)); + self.set(PolicyType::Maturity, Some(*maturity.deref() as u64)); self } diff --git a/fuel-vm/src/checked_transaction.rs b/fuel-vm/src/checked_transaction.rs index 9decd4f5da..fe56b14fb4 100644 --- a/fuel-vm/src/checked_transaction.rs +++ b/fuel-vm/src/checked_transaction.rs @@ -1588,7 +1588,7 @@ mod tests { .saturating_add(witness_limit_allowance); let max_fee: u64 = gas_to_fee(max_gas, tx.price(), fee_params.gas_price_factor) .try_into() - .map_err(|_| ValidityError::ArithmeticOverflow)?; + .map_err(|_| ValidityError::BalanceOverflow)?; let result = max_fee == available_balances.fee.max_fee(); Ok(result) @@ -1624,7 +1624,7 @@ mod tests { let rounded_fee = fee.saturating_add(fee_remainder); let min_fee: u64 = rounded_fee .try_into() - .map_err(|_| ValidityError::ArithmeticOverflow)?; + .map_err(|_| ValidityError::BalanceOverflow)?; Ok(min_fee == available_balances.fee.min_fee()) } From a70237e4420ea45e0d6e0f142034af51e8e961e4 Mon Sep 17 00:00:00 2001 From: xgreenx Date: Thu, 9 Nov 2023 15:17:04 +0000 Subject: [PATCH 7/7] Make CIP happy --- fuel-tx/src/transaction/policies.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fuel-tx/src/transaction/policies.rs b/fuel-tx/src/transaction/policies.rs index 3d99b5cafe..5c5a32948e 100644 --- a/fuel-tx/src/transaction/policies.rs +++ b/fuel-tx/src/transaction/policies.rs @@ -1,3 +1,4 @@ +use core::ops::Deref; use fuel_types::{ canonical::{ Deserialize, @@ -9,7 +10,6 @@ use fuel_types::{ BlockHeight, Word, }; -use std::ops::Deref; #[cfg(feature = "random")] use rand::{