From 81751471ffda85e80cd67545e104ef2693fd98ef Mon Sep 17 00:00:00 2001 From: Turner Date: Wed, 7 Feb 2024 17:25:33 -0800 Subject: [PATCH 01/43] WIP --- .../src/transaction/consensus_parameters.rs | 16 +++++------ fuel-tx/src/transaction/fee.rs | 28 ++++++++----------- fuel-tx/src/transaction/types/create.rs | 3 +- fuel-tx/src/transaction/types/mint.rs | 1 + fuel-tx/src/transaction/types/script.rs | 3 +- fuel-tx/src/transaction/validity.rs | 14 ++++++---- fuel-vm/src/checked_transaction.rs | 4 ++- fuel-vm/src/checked_transaction/balances.rs | 3 +- fuel-vm/src/checked_transaction/types.rs | 5 +++- fuel-vm/src/interpreter.rs | 3 +- fuel-vm/src/interpreter/post_execution.rs | 2 ++ 11 files changed, 47 insertions(+), 35 deletions(-) diff --git a/fuel-tx/src/transaction/consensus_parameters.rs b/fuel-tx/src/transaction/consensus_parameters.rs index 11550da99d..03f5bef92f 100644 --- a/fuel-tx/src/transaction/consensus_parameters.rs +++ b/fuel-tx/src/transaction/consensus_parameters.rs @@ -136,7 +136,7 @@ impl ConsensusParameters { #[cfg_attr(feature = "serde", serde(default))] pub struct FeeParameters { /// Factor to convert between gas and transaction assets value. - pub gas_price_factor: u64, + // pub gas_price_factor: u64, /// A fixed ratio linking metered bytes to gas price pub gas_per_byte: u64, } @@ -144,15 +144,15 @@ pub struct FeeParameters { impl FeeParameters { /// Default consensus parameters with settings suggested in fuel-specs pub const DEFAULT: Self = Self { - gas_price_factor: 1_000_000_000, + // gas_price_factor: 1_000_000_000, gas_per_byte: 4, }; - /// Replace the gas price factor with the given argument - pub const fn with_gas_price_factor(mut self, gas_price_factor: u64) -> Self { - self.gas_price_factor = gas_price_factor; - self - } + // /// Replace the gas price factor with the given argument + // pub const fn with_gas_price_factor(mut self, gas_price_factor: u64) -> Self { + // self.gas_price_factor = gas_price_factor; + // self + // } pub const fn with_gas_per_byte(mut self, gas_per_byte: u64) -> Self { self.gas_per_byte = gas_per_byte; @@ -420,7 +420,7 @@ pub mod default_parameters { pub const MAX_GAS_PER_PREDICATE: u64 = PredicateParameters::DEFAULT.max_gas_per_predicate; - pub const GAS_PRICE_FACTOR: u64 = FeeParameters::DEFAULT.gas_price_factor; + // pub const GAS_PRICE_FACTOR: u64 = FeeParameters::DEFAULT.gas_price_factor; pub const GAS_PER_BYTE: u64 = FeeParameters::DEFAULT.gas_per_byte; pub const CHAIN_ID: ChainId = ChainId::new(0); diff --git a/fuel-tx/src/transaction/fee.rs b/fuel-tx/src/transaction/fee.rs index 65720d92d7..2a9fab524a 100644 --- a/fuel-tx/src/transaction/fee.rs +++ b/fuel-tx/src/transaction/fee.rs @@ -90,14 +90,15 @@ impl TransactionFee { gas_costs: &GasCosts, params: &FeeParameters, tx: &T, + gas_price: u64, ) -> Option where T: Chargeable, { let min_gas = tx.min_gas(gas_costs, params); let max_gas = tx.max_gas(gas_costs, params); - let min_fee = tx.min_fee(gas_costs, params).try_into().ok()?; - let max_fee = tx.max_fee(gas_costs, params).try_into().ok()?; + let min_fee = tx.min_fee(gas_costs, params, gas_price).try_into().ok()?; + let max_fee = tx.max_fee(gas_costs, params, gas_price).try_into().ok()?; if min_fee > max_fee { return None @@ -152,23 +153,15 @@ pub trait Chargeable: field::Inputs + field::Witnesses + field::Policies { } /// Returns the minimum fee required to start transaction execution. - fn min_fee(&self, gas_costs: &GasCosts, fee: &FeeParameters) -> u128 { - gas_to_fee( - self.min_gas(gas_costs, fee), - self.price(), - fee.gas_price_factor, - ) + fn min_fee(&self, gas_costs: &GasCosts, fee: &FeeParameters, gas_price: u64) -> u128 { + gas_to_fee(self.min_gas(gas_costs, fee), self.price(), gas_price) } /// Returns the maximum possible fee after the end of transaction execution. /// /// The function guarantees that the value is not less than [Self::min_fee]. - fn max_fee(&self, gas_costs: &GasCosts, fee: &FeeParameters) -> u128 { - gas_to_fee( - self.max_gas(gas_costs, fee), - self.price(), - fee.gas_price_factor, - ) + fn max_fee(&self, gas_costs: &GasCosts, fee: &FeeParameters, gas_price: u64) -> u128 { + gas_to_fee(self.max_gas(gas_costs, fee), self.price(), gas_price) } /// Returns the fee amount that can be refunded back based on the `used_gas` and @@ -180,15 +173,18 @@ pub trait Chargeable: field::Inputs + field::Witnesses + field::Policies { gas_costs: &GasCosts, fee: &FeeParameters, used_gas: Word, + gas_price: u64, ) -> Option { // We've already charged the user for witnesses as part of the minimal gas and all // execution required to validate transaction validity rules. let min_gas = self.min_gas(gas_costs, fee); let total_used_gas = min_gas.saturating_add(used_gas); - let used_fee = gas_to_fee(total_used_gas, self.price(), fee.gas_price_factor); + let used_fee = gas_to_fee(total_used_gas, self.price(), gas_price); - let refund = self.max_fee(gas_costs, fee).saturating_sub(used_fee); + let refund = self + .max_fee(gas_costs, fee, gas_price) + .saturating_sub(used_fee); // It is okay to saturate everywhere above because it only can decrease the value // of `refund`. But here, because we need to return the amount we // want to refund, we need to handle the overflow caused by the price. diff --git a/fuel-tx/src/transaction/types/create.rs b/fuel-tx/src/transaction/types/create.rs index e5155ae055..8f21c41aff 100644 --- a/fuel-tx/src/transaction/types/create.rs +++ b/fuel-tx/src/transaction/types/create.rs @@ -222,6 +222,7 @@ impl FormatValidityChecks for Create { &self, block_height: BlockHeight, consensus_params: &ConsensusParameters, + gas_price: u64, ) -> Result<(), ValidityError> { let ConsensusParameters { contract_params, @@ -230,7 +231,7 @@ impl FormatValidityChecks for Create { .. } = consensus_params; - check_common_part(self, block_height, consensus_params)?; + check_common_part(self, block_height, consensus_params, gas_price)?; let bytecode_witness_len = self .witnesses diff --git a/fuel-tx/src/transaction/types/mint.rs b/fuel-tx/src/transaction/types/mint.rs index b4b961b0b5..59c90e14df 100644 --- a/fuel-tx/src/transaction/types/mint.rs +++ b/fuel-tx/src/transaction/types/mint.rs @@ -97,6 +97,7 @@ impl FormatValidityChecks for Mint { &self, block_height: BlockHeight, consensus_params: &ConsensusParameters, + gas_price: u64, ) -> Result<(), ValidityError> { check_size(self, consensus_params.tx_params())?; diff --git a/fuel-tx/src/transaction/types/script.rs b/fuel-tx/src/transaction/types/script.rs index 869b401999..abc8907f60 100644 --- a/fuel-tx/src/transaction/types/script.rs +++ b/fuel-tx/src/transaction/types/script.rs @@ -176,8 +176,9 @@ impl FormatValidityChecks for Script { &self, block_height: BlockHeight, consensus_params: &ConsensusParameters, + gas_price: u64, ) -> Result<(), ValidityError> { - check_common_part(self, block_height, consensus_params)?; + check_common_part(self, block_height, consensus_params, gas_price)?; let script_params = consensus_params.script_params(); if self.script.len() as u64 > script_params.max_script_length { Err(ValidityError::TransactionScriptLength)?; diff --git a/fuel-tx/src/transaction/validity.rs b/fuel-tx/src/transaction/validity.rs index 8a6f23706a..8133aa2a10 100644 --- a/fuel-tx/src/transaction/validity.rs +++ b/fuel-tx/src/transaction/validity.rs @@ -246,8 +246,9 @@ pub trait FormatValidityChecks { &self, block_height: BlockHeight, consensus_params: &ConsensusParameters, + gas_price: u64, ) -> Result<(), ValidityError> { - self.check_without_signatures(block_height, consensus_params)?; + self.check_without_signatures(block_height, consensus_params, gas_price)?; self.check_signatures(&consensus_params.chain_id())?; Ok(()) @@ -263,6 +264,7 @@ pub trait FormatValidityChecks { &self, block_height: BlockHeight, consensus_params: &ConsensusParameters, + gas_price: u64, ) -> Result<(), ValidityError>; } @@ -279,16 +281,17 @@ impl FormatValidityChecks for Transaction { &self, block_height: BlockHeight, consensus_params: &ConsensusParameters, + gas_price: u64, ) -> Result<(), ValidityError> { match self { Transaction::Script(script) => { - script.check_without_signatures(block_height, consensus_params) + script.check_without_signatures(block_height, consensus_params, gas_price) } Transaction::Create(create) => { - create.check_without_signatures(block_height, consensus_params) + create.check_without_signatures(block_height, consensus_params, gas_price) } Transaction::Mint(mint) => { - mint.check_without_signatures(block_height, consensus_params) + mint.check_without_signatures(block_height, consensus_params, gas_price) } } } @@ -313,6 +316,7 @@ pub(crate) fn check_common_part( tx: &T, block_height: BlockHeight, consensus_params: &ConsensusParameters, + gas_price: u64, ) -> Result<(), ValidityError> where T: canonical::Serialize + Chargeable + field::Outputs, @@ -349,7 +353,7 @@ where } if let Some(max_fee_limit) = tx.policies().get(PolicyType::MaxFee) { - if tx.max_fee(gas_costs, fee_params) > max_fee_limit as u128 { + if tx.max_fee(gas_costs, fee_params, gas_price) > max_fee_limit as u128 { Err(ValidityError::TransactionMaxFeeLimitExceeded)? } } diff --git a/fuel-vm/src/checked_transaction.rs b/fuel-vm/src/checked_transaction.rs index aad20fe387..03c4f73c32 100644 --- a/fuel-vm/src/checked_transaction.rs +++ b/fuel-vm/src/checked_transaction.rs @@ -192,12 +192,13 @@ pub trait IntoChecked: FormatValidityChecks + Sized { self, block_height: BlockHeight, consensus_params: &ConsensusParameters, + gas_price: u64, ) -> Result, CheckError> where Checked: CheckPredicates, { let check_predicate_params = consensus_params.into(); - self.into_checked_basic(block_height, consensus_params)? + self.into_checked_basic(block_height, consensus_params, gas_price)? .check_signatures(&consensus_params.chain_id)? .check_predicates(&check_predicate_params) } @@ -207,6 +208,7 @@ pub trait IntoChecked: FormatValidityChecks + Sized { self, block_height: BlockHeight, consensus_params: &ConsensusParameters, + gas_price: u64, ) -> Result, CheckError>; } diff --git a/fuel-vm/src/checked_transaction/balances.rs b/fuel-vm/src/checked_transaction/balances.rs index f373e952cd..443b6433c7 100644 --- a/fuel-vm/src/checked_transaction/balances.rs +++ b/fuel-vm/src/checked_transaction/balances.rs @@ -32,6 +32,7 @@ pub(crate) fn initial_free_balances( gas_costs: &GasCosts, params: &FeeParameters, base_asset_id: &AssetId, + gas_price: u64, ) -> Result where T: Chargeable + field::Inputs + field::Outputs, @@ -67,7 +68,7 @@ where } // Deduct fee from base asset - let fee = TransactionFee::checked_from_tx(gas_costs, params, transaction) + let fee = TransactionFee::checked_from_tx(gas_costs, params, transaction, gas_price) .ok_or(ValidityError::BalanceOverflow)?; let base_asset_balance = non_retryable_balances.entry(*base_asset_id).or_default(); diff --git a/fuel-vm/src/checked_transaction/types.rs b/fuel-vm/src/checked_transaction/types.rs index 8271db80d6..9fbcf37f46 100644 --- a/fuel-vm/src/checked_transaction/types.rs +++ b/fuel-vm/src/checked_transaction/types.rs @@ -94,10 +94,11 @@ pub mod create { mut self, block_height: BlockHeight, consensus_params: &ConsensusParameters, + gas_price: u64, ) -> Result, CheckError> { let chain_id = consensus_params.chain_id(); self.precompute(&chain_id)?; - self.check_without_signatures(block_height, consensus_params)?; + self.check_without_signatures(block_height, consensus_params, gas_price)?; // validate fees and compute free balances let AvailableBalances { @@ -202,6 +203,7 @@ pub mod script { mut self, block_height: BlockHeight, consensus_params: &ConsensusParameters, + gas_price: u64, ) -> Result, CheckError> { let chain_id = consensus_params.chain_id(); self.precompute(&chain_id)?; @@ -217,6 +219,7 @@ pub mod script { consensus_params.gas_costs(), consensus_params.fee_params(), consensus_params.base_asset_id(), + gas_price, )?; let metadata = CheckedMetadata { diff --git a/fuel-vm/src/interpreter.rs b/fuel-vm/src/interpreter.rs index 1a539ba18c..3874fbe05a 100644 --- a/fuel-vm/src/interpreter.rs +++ b/fuel-vm/src/interpreter.rs @@ -424,12 +424,13 @@ pub trait ExecutableTransaction: gas_costs: &GasCosts, fee_params: &FeeParameters, base_asset_id: &AssetId, + gas_price: u64, ) -> Result<(), ValidityError> where I: for<'a> Index<&'a AssetId, Output = Word>, { let gas_refund = self - .refund_fee(gas_costs, fee_params, used_gas) + .refund_fee(gas_costs, fee_params, used_gas, gas_price) .ok_or(ValidityError::GasCostsCoinsOverflow)?; self.outputs_mut().iter_mut().try_for_each(|o| match o { diff --git a/fuel-vm/src/interpreter/post_execution.rs b/fuel-vm/src/interpreter/post_execution.rs index 00a41d03aa..0f28ada94e 100644 --- a/fuel-vm/src/interpreter/post_execution.rs +++ b/fuel-vm/src/interpreter/post_execution.rs @@ -46,6 +46,7 @@ where used_gas: Word, initial_balances: &InitialBalances, balances: &RuntimeBalances, + gas_price: u64, ) -> Result<(), RuntimeError> where Tx: ExecutableTransaction, @@ -58,6 +59,7 @@ where gas_costs, fee_params, base_asset_id, + gas_price, ) .map_err(|e| Bug::new(BugVariant::UncomputableRefund).with_message(e))?; From d1975eb49458dccec2d638a03b67eec3c7b48420 Mon Sep 17 00:00:00 2001 From: mitchell Date: Thu, 8 Feb 2024 10:59:11 -0800 Subject: [PATCH 02/43] Revert "WIP" This reverts commit e8ae0671c776c94807a755eefb4c6d46f953f5c7. --- .../src/transaction/consensus_parameters.rs | 16 +++++------ fuel-tx/src/transaction/fee.rs | 28 +++++++++++-------- fuel-tx/src/transaction/types/create.rs | 3 +- fuel-tx/src/transaction/types/mint.rs | 1 - fuel-tx/src/transaction/types/script.rs | 3 +- fuel-tx/src/transaction/validity.rs | 14 ++++------ fuel-vm/src/checked_transaction.rs | 4 +-- fuel-vm/src/checked_transaction/balances.rs | 3 +- fuel-vm/src/checked_transaction/types.rs | 5 +--- fuel-vm/src/interpreter.rs | 3 +- fuel-vm/src/interpreter/post_execution.rs | 2 -- 11 files changed, 35 insertions(+), 47 deletions(-) diff --git a/fuel-tx/src/transaction/consensus_parameters.rs b/fuel-tx/src/transaction/consensus_parameters.rs index 03f5bef92f..11550da99d 100644 --- a/fuel-tx/src/transaction/consensus_parameters.rs +++ b/fuel-tx/src/transaction/consensus_parameters.rs @@ -136,7 +136,7 @@ impl ConsensusParameters { #[cfg_attr(feature = "serde", serde(default))] pub struct FeeParameters { /// Factor to convert between gas and transaction assets value. - // pub gas_price_factor: u64, + pub gas_price_factor: u64, /// A fixed ratio linking metered bytes to gas price pub gas_per_byte: u64, } @@ -144,15 +144,15 @@ pub struct FeeParameters { impl FeeParameters { /// Default consensus parameters with settings suggested in fuel-specs pub const DEFAULT: Self = Self { - // gas_price_factor: 1_000_000_000, + gas_price_factor: 1_000_000_000, gas_per_byte: 4, }; - // /// Replace the gas price factor with the given argument - // pub const fn with_gas_price_factor(mut self, gas_price_factor: u64) -> Self { - // self.gas_price_factor = gas_price_factor; - // self - // } + /// Replace the gas price factor with the given argument + pub const fn with_gas_price_factor(mut self, gas_price_factor: u64) -> Self { + self.gas_price_factor = gas_price_factor; + self + } pub const fn with_gas_per_byte(mut self, gas_per_byte: u64) -> Self { self.gas_per_byte = gas_per_byte; @@ -420,7 +420,7 @@ pub mod default_parameters { pub const MAX_GAS_PER_PREDICATE: u64 = PredicateParameters::DEFAULT.max_gas_per_predicate; - // pub const GAS_PRICE_FACTOR: u64 = FeeParameters::DEFAULT.gas_price_factor; + pub const GAS_PRICE_FACTOR: u64 = FeeParameters::DEFAULT.gas_price_factor; pub const GAS_PER_BYTE: u64 = FeeParameters::DEFAULT.gas_per_byte; pub const CHAIN_ID: ChainId = ChainId::new(0); diff --git a/fuel-tx/src/transaction/fee.rs b/fuel-tx/src/transaction/fee.rs index 2a9fab524a..65720d92d7 100644 --- a/fuel-tx/src/transaction/fee.rs +++ b/fuel-tx/src/transaction/fee.rs @@ -90,15 +90,14 @@ impl TransactionFee { gas_costs: &GasCosts, params: &FeeParameters, tx: &T, - gas_price: u64, ) -> Option where T: Chargeable, { let min_gas = tx.min_gas(gas_costs, params); let max_gas = tx.max_gas(gas_costs, params); - let min_fee = tx.min_fee(gas_costs, params, gas_price).try_into().ok()?; - let max_fee = tx.max_fee(gas_costs, params, gas_price).try_into().ok()?; + let min_fee = tx.min_fee(gas_costs, params).try_into().ok()?; + let max_fee = tx.max_fee(gas_costs, params).try_into().ok()?; if min_fee > max_fee { return None @@ -153,15 +152,23 @@ pub trait Chargeable: field::Inputs + field::Witnesses + field::Policies { } /// Returns the minimum fee required to start transaction execution. - fn min_fee(&self, gas_costs: &GasCosts, fee: &FeeParameters, gas_price: u64) -> u128 { - gas_to_fee(self.min_gas(gas_costs, fee), self.price(), gas_price) + fn min_fee(&self, gas_costs: &GasCosts, fee: &FeeParameters) -> u128 { + gas_to_fee( + self.min_gas(gas_costs, fee), + self.price(), + fee.gas_price_factor, + ) } /// Returns the maximum possible fee after the end of transaction execution. /// /// The function guarantees that the value is not less than [Self::min_fee]. - fn max_fee(&self, gas_costs: &GasCosts, fee: &FeeParameters, gas_price: u64) -> u128 { - gas_to_fee(self.max_gas(gas_costs, fee), self.price(), gas_price) + fn max_fee(&self, gas_costs: &GasCosts, fee: &FeeParameters) -> u128 { + gas_to_fee( + self.max_gas(gas_costs, fee), + self.price(), + fee.gas_price_factor, + ) } /// Returns the fee amount that can be refunded back based on the `used_gas` and @@ -173,18 +180,15 @@ pub trait Chargeable: field::Inputs + field::Witnesses + field::Policies { gas_costs: &GasCosts, fee: &FeeParameters, used_gas: Word, - gas_price: u64, ) -> Option { // We've already charged the user for witnesses as part of the minimal gas and all // execution required to validate transaction validity rules. let min_gas = self.min_gas(gas_costs, fee); let total_used_gas = min_gas.saturating_add(used_gas); - let used_fee = gas_to_fee(total_used_gas, self.price(), gas_price); + let used_fee = gas_to_fee(total_used_gas, self.price(), fee.gas_price_factor); - let refund = self - .max_fee(gas_costs, fee, gas_price) - .saturating_sub(used_fee); + let refund = self.max_fee(gas_costs, fee).saturating_sub(used_fee); // It is okay to saturate everywhere above because it only can decrease the value // of `refund`. But here, because we need to return the amount we // want to refund, we need to handle the overflow caused by the price. diff --git a/fuel-tx/src/transaction/types/create.rs b/fuel-tx/src/transaction/types/create.rs index 8f21c41aff..e5155ae055 100644 --- a/fuel-tx/src/transaction/types/create.rs +++ b/fuel-tx/src/transaction/types/create.rs @@ -222,7 +222,6 @@ impl FormatValidityChecks for Create { &self, block_height: BlockHeight, consensus_params: &ConsensusParameters, - gas_price: u64, ) -> Result<(), ValidityError> { let ConsensusParameters { contract_params, @@ -231,7 +230,7 @@ impl FormatValidityChecks for Create { .. } = consensus_params; - check_common_part(self, block_height, consensus_params, gas_price)?; + check_common_part(self, block_height, consensus_params)?; let bytecode_witness_len = self .witnesses diff --git a/fuel-tx/src/transaction/types/mint.rs b/fuel-tx/src/transaction/types/mint.rs index 59c90e14df..b4b961b0b5 100644 --- a/fuel-tx/src/transaction/types/mint.rs +++ b/fuel-tx/src/transaction/types/mint.rs @@ -97,7 +97,6 @@ impl FormatValidityChecks for Mint { &self, block_height: BlockHeight, consensus_params: &ConsensusParameters, - gas_price: u64, ) -> Result<(), ValidityError> { check_size(self, consensus_params.tx_params())?; diff --git a/fuel-tx/src/transaction/types/script.rs b/fuel-tx/src/transaction/types/script.rs index abc8907f60..869b401999 100644 --- a/fuel-tx/src/transaction/types/script.rs +++ b/fuel-tx/src/transaction/types/script.rs @@ -176,9 +176,8 @@ impl FormatValidityChecks for Script { &self, block_height: BlockHeight, consensus_params: &ConsensusParameters, - gas_price: u64, ) -> Result<(), ValidityError> { - check_common_part(self, block_height, consensus_params, gas_price)?; + check_common_part(self, block_height, consensus_params)?; let script_params = consensus_params.script_params(); if self.script.len() as u64 > script_params.max_script_length { Err(ValidityError::TransactionScriptLength)?; diff --git a/fuel-tx/src/transaction/validity.rs b/fuel-tx/src/transaction/validity.rs index 8133aa2a10..8a6f23706a 100644 --- a/fuel-tx/src/transaction/validity.rs +++ b/fuel-tx/src/transaction/validity.rs @@ -246,9 +246,8 @@ pub trait FormatValidityChecks { &self, block_height: BlockHeight, consensus_params: &ConsensusParameters, - gas_price: u64, ) -> Result<(), ValidityError> { - self.check_without_signatures(block_height, consensus_params, gas_price)?; + self.check_without_signatures(block_height, consensus_params)?; self.check_signatures(&consensus_params.chain_id())?; Ok(()) @@ -264,7 +263,6 @@ pub trait FormatValidityChecks { &self, block_height: BlockHeight, consensus_params: &ConsensusParameters, - gas_price: u64, ) -> Result<(), ValidityError>; } @@ -281,17 +279,16 @@ impl FormatValidityChecks for Transaction { &self, block_height: BlockHeight, consensus_params: &ConsensusParameters, - gas_price: u64, ) -> Result<(), ValidityError> { match self { Transaction::Script(script) => { - script.check_without_signatures(block_height, consensus_params, gas_price) + script.check_without_signatures(block_height, consensus_params) } Transaction::Create(create) => { - create.check_without_signatures(block_height, consensus_params, gas_price) + create.check_without_signatures(block_height, consensus_params) } Transaction::Mint(mint) => { - mint.check_without_signatures(block_height, consensus_params, gas_price) + mint.check_without_signatures(block_height, consensus_params) } } } @@ -316,7 +313,6 @@ pub(crate) fn check_common_part( tx: &T, block_height: BlockHeight, consensus_params: &ConsensusParameters, - gas_price: u64, ) -> Result<(), ValidityError> where T: canonical::Serialize + Chargeable + field::Outputs, @@ -353,7 +349,7 @@ where } if let Some(max_fee_limit) = tx.policies().get(PolicyType::MaxFee) { - if tx.max_fee(gas_costs, fee_params, gas_price) > max_fee_limit as u128 { + if tx.max_fee(gas_costs, fee_params) > max_fee_limit as u128 { Err(ValidityError::TransactionMaxFeeLimitExceeded)? } } diff --git a/fuel-vm/src/checked_transaction.rs b/fuel-vm/src/checked_transaction.rs index 03c4f73c32..aad20fe387 100644 --- a/fuel-vm/src/checked_transaction.rs +++ b/fuel-vm/src/checked_transaction.rs @@ -192,13 +192,12 @@ pub trait IntoChecked: FormatValidityChecks + Sized { self, block_height: BlockHeight, consensus_params: &ConsensusParameters, - gas_price: u64, ) -> Result, CheckError> where Checked: CheckPredicates, { let check_predicate_params = consensus_params.into(); - self.into_checked_basic(block_height, consensus_params, gas_price)? + self.into_checked_basic(block_height, consensus_params)? .check_signatures(&consensus_params.chain_id)? .check_predicates(&check_predicate_params) } @@ -208,7 +207,6 @@ pub trait IntoChecked: FormatValidityChecks + Sized { self, block_height: BlockHeight, consensus_params: &ConsensusParameters, - gas_price: u64, ) -> Result, CheckError>; } diff --git a/fuel-vm/src/checked_transaction/balances.rs b/fuel-vm/src/checked_transaction/balances.rs index 443b6433c7..f373e952cd 100644 --- a/fuel-vm/src/checked_transaction/balances.rs +++ b/fuel-vm/src/checked_transaction/balances.rs @@ -32,7 +32,6 @@ pub(crate) fn initial_free_balances( gas_costs: &GasCosts, params: &FeeParameters, base_asset_id: &AssetId, - gas_price: u64, ) -> Result where T: Chargeable + field::Inputs + field::Outputs, @@ -68,7 +67,7 @@ where } // Deduct fee from base asset - let fee = TransactionFee::checked_from_tx(gas_costs, params, transaction, gas_price) + let fee = TransactionFee::checked_from_tx(gas_costs, params, transaction) .ok_or(ValidityError::BalanceOverflow)?; let base_asset_balance = non_retryable_balances.entry(*base_asset_id).or_default(); diff --git a/fuel-vm/src/checked_transaction/types.rs b/fuel-vm/src/checked_transaction/types.rs index 9fbcf37f46..8271db80d6 100644 --- a/fuel-vm/src/checked_transaction/types.rs +++ b/fuel-vm/src/checked_transaction/types.rs @@ -94,11 +94,10 @@ pub mod create { mut self, block_height: BlockHeight, consensus_params: &ConsensusParameters, - gas_price: u64, ) -> Result, CheckError> { let chain_id = consensus_params.chain_id(); self.precompute(&chain_id)?; - self.check_without_signatures(block_height, consensus_params, gas_price)?; + self.check_without_signatures(block_height, consensus_params)?; // validate fees and compute free balances let AvailableBalances { @@ -203,7 +202,6 @@ pub mod script { mut self, block_height: BlockHeight, consensus_params: &ConsensusParameters, - gas_price: u64, ) -> Result, CheckError> { let chain_id = consensus_params.chain_id(); self.precompute(&chain_id)?; @@ -219,7 +217,6 @@ pub mod script { consensus_params.gas_costs(), consensus_params.fee_params(), consensus_params.base_asset_id(), - gas_price, )?; let metadata = CheckedMetadata { diff --git a/fuel-vm/src/interpreter.rs b/fuel-vm/src/interpreter.rs index 3874fbe05a..1a539ba18c 100644 --- a/fuel-vm/src/interpreter.rs +++ b/fuel-vm/src/interpreter.rs @@ -424,13 +424,12 @@ pub trait ExecutableTransaction: gas_costs: &GasCosts, fee_params: &FeeParameters, base_asset_id: &AssetId, - gas_price: u64, ) -> Result<(), ValidityError> where I: for<'a> Index<&'a AssetId, Output = Word>, { let gas_refund = self - .refund_fee(gas_costs, fee_params, used_gas, gas_price) + .refund_fee(gas_costs, fee_params, used_gas) .ok_or(ValidityError::GasCostsCoinsOverflow)?; self.outputs_mut().iter_mut().try_for_each(|o| match o { diff --git a/fuel-vm/src/interpreter/post_execution.rs b/fuel-vm/src/interpreter/post_execution.rs index 0f28ada94e..00a41d03aa 100644 --- a/fuel-vm/src/interpreter/post_execution.rs +++ b/fuel-vm/src/interpreter/post_execution.rs @@ -46,7 +46,6 @@ where used_gas: Word, initial_balances: &InitialBalances, balances: &RuntimeBalances, - gas_price: u64, ) -> Result<(), RuntimeError> where Tx: ExecutableTransaction, @@ -59,7 +58,6 @@ where gas_costs, fee_params, base_asset_id, - gas_price, ) .map_err(|e| Bug::new(BugVariant::UncomputableRefund).with_message(e))?; From bf31410aee176d1b5541f7272443e7d82ec11484 Mon Sep 17 00:00:00 2001 From: mitchell Date: Thu, 8 Feb 2024 11:46:21 -0800 Subject: [PATCH 03/43] Replace gas price policy with tip, add gas price to transact interface --- fuel-asm/src/args.rs | 2 +- fuel-tx/src/builder.rs | 71 +++--------- fuel-tx/src/transaction.rs | 116 ++++--------------- fuel-tx/src/transaction/fee.rs | 55 +++++---- fuel-tx/src/transaction/policies.rs | 42 +++---- fuel-tx/src/transaction/types/create.rs | 62 ++++------ fuel-tx/src/transaction/types/mint.rs | 36 ++---- fuel-tx/src/transaction/types/script.rs | 57 +++------- fuel-tx/src/transaction/validity.rs | 78 ++++--------- fuel-vm/src/checked_transaction.rs | 67 ++++------- fuel-vm/src/checked_transaction/balances.rs | 25 +--- fuel-vm/src/checked_transaction/builder.rs | 35 +++--- fuel-vm/src/checked_transaction/types.rs | 65 ++++------- fuel-vm/src/interpreter.rs | 70 +++--------- fuel-vm/src/interpreter/executors/main.rs | 119 +++++++------------- fuel-vm/src/interpreter/metadata.rs | 47 ++------ fuel-vm/src/interpreter/post_execution.rs | 24 +--- fuel-vm/src/memory_client.rs | 28 ++--- fuel-vm/src/transactor.rs | 37 ++---- 19 files changed, 305 insertions(+), 731 deletions(-) diff --git a/fuel-asm/src/args.rs b/fuel-asm/src/args.rs index 035f6b0a3e..bda0caa48b 100644 --- a/fuel-asm/src/args.rs +++ b/fuel-asm/src/args.rs @@ -252,7 +252,7 @@ crate::enum_try_from! { PolicyTypes = 0x500, /// Set `$rA` to `tx.policies[0x00].gasPrice` - PolicyGasPrice = 0x501, + PolicyTip = 0x501, /// Set `$rA` to `tx.policies[count_ones(0b11 & tx.policyTypes) - 1].witnessLimit` PolicyWitnessLimit = 0x502, diff --git a/fuel-tx/src/builder.rs b/fuel-tx/src/builder.rs index f583c8019a..ce0c4af7c1 100644 --- a/fuel-tx/src/builder.rs +++ b/fuel-tx/src/builder.rs @@ -1,61 +1,24 @@ use crate::{ - input, - output, + input, output, transaction::{ field, - field::{ - BytecodeLength, - BytecodeWitnessIndex, - GasPrice, - Maturity, - Witnesses, - }, - Chargeable, - Create, - Executable, - Script, + field::{BytecodeLength, BytecodeWitnessIndex, Maturity, Tip, Witnesses}, + Chargeable, Create, Executable, Script, }, - ConsensusParameters, - ContractParameters, - FeeParameters, - GasCosts, - Input, - Mint, - Output, - PredicateParameters, - ScriptParameters, - StorageSlot, - Transaction, - TxParameters, - TxPointer, - Witness, + ConsensusParameters, ContractParameters, FeeParameters, GasCosts, Input, Mint, + Output, PredicateParameters, ScriptParameters, StorageSlot, Transaction, + TxParameters, TxPointer, Witness, }; -use crate::{ - Cacheable, - Signable, -}; +use crate::{Cacheable, Signable}; use crate::{ - field::{ - MaxFeeLimit, - WitnessLimit, - }, + field::{MaxFeeLimit, WitnessLimit}, policies::Policies, }; -use alloc::{ - collections::BTreeMap, - vec::Vec, -}; +use alloc::{collections::BTreeMap, vec::Vec}; use fuel_crypto::SecretKey; -use fuel_types::{ - AssetId, - BlockHeight, - ChainId, - Nonce, - Salt, - Word, -}; +use fuel_types::{AssetId, BlockHeight, ChainId, Nonce, Salt, Word}; pub trait BuildableAloc where @@ -119,7 +82,7 @@ impl TransactionBuilder