Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include tip in fee #682

Merged
merged 48 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
8175147
WIP
MitchTurner Feb 8, 2024
d1975eb
Revert "WIP"
MitchTurner Feb 8, 2024
bf31410
Replace gas price policy with tip, add gas price to transact interface
MitchTurner Feb 8, 2024
90f4729
Update CHANGELOG
MitchTurner Feb 8, 2024
f13e6bb
fmt
MitchTurner Feb 8, 2024
ba0b1eb
WIP update tests
MitchTurner Feb 8, 2024
9cbcadf
WIP
MitchTurner Feb 9, 2024
d1d11c8
Fix run_script to have free gas
MitchTurner Feb 10, 2024
d431e61
Deal with test gas prices that must be zero
MitchTurner Feb 10, 2024
ed7827b
Fix one test
MitchTurner Feb 12, 2024
c49d037
Fix predicate test
MitchTurner Feb 12, 2024
2d307d2
Fix output update test
MitchTurner Feb 12, 2024
d9091fe
Fix snapshot tests and some other gas price issues in tests
MitchTurner Feb 12, 2024
4ecb8fa
Maybe fix test?
MitchTurner Feb 13, 2024
b80bd39
Fix create test
MitchTurner Feb 14, 2024
fe7bd46
Removed todo
xgreenx Feb 14, 2024
3f5d950
Removed todo for tip GTF
xgreenx Feb 14, 2024
b963c6b
Fix
xgreenx Feb 14, 2024
0f756c3
Fix warnings
MitchTurner Feb 14, 2024
0c3c8ff
Make ci check changes
MitchTurner Feb 14, 2024
39a3036
Revert default impl changes
MitchTurner Feb 16, 2024
c548402
Replace gas_price in prop tests
MitchTurner Feb 19, 2024
32e187e
Replace some tip stuff for serialization and snapshots
MitchTurner Feb 19, 2024
f13085a
Merge branch 'master' into use-gas-price-in-fee-calc
MitchTurner Feb 19, 2024
394d89a
Fix function call
MitchTurner Feb 19, 2024
935ba82
Fix tests
MitchTurner Feb 19, 2024
cb6fb93
Add `gas_price` to `InterpreterParams`
MitchTurner Feb 20, 2024
295152c
Remove commented code
MitchTurner Feb 20, 2024
e2a26e8
Fix prop test to generate `gas_price`
MitchTurner Feb 21, 2024
9c1c3d2
Merge branch 'master' into use-gas-price-in-fee-calc
MitchTurner Feb 21, 2024
9dddcfe
Merge branch 'master' into use-gas-price-in-fee-calc
xgreenx Feb 21, 2024
d7546e2
Remove `gas_price` from predicate execution functions
MitchTurner Feb 21, 2024
fd747ba
Fix tests
MitchTurner Feb 21, 2024
81e91b8
Replace encoding stuff for tip
MitchTurner Feb 22, 2024
17cd96e
Replace the other encoding stuff
MitchTurner Feb 22, 2024
6ef9e8e
Add failing test :)
MitchTurner Feb 21, 2024
7a83a53
Add code for passing test
MitchTurner Feb 21, 2024
b00109a
Split test
MitchTurner Feb 22, 2024
357fa3e
Update CHANGELOG
MitchTurner Feb 22, 2024
9c61f28
Appease Clippy-sama
MitchTurner Feb 22, 2024
1c26eff
Merge remote-tracking branch 'origin' into include-tip-in-fee
MitchTurner Feb 22, 2024
5a766a6
Use public interfaces for UTs
MitchTurner Feb 22, 2024
cb65324
Merge branch 'master' into include-tip-in-fee
MitchTurner Feb 22, 2024
5494ee4
Update fuel-tx/src/transaction/fee.rs
MitchTurner Feb 23, 2024
529a0a5
Update fuel-tx/src/transaction/fee.rs
MitchTurner Feb 23, 2024
7bb425a
Update fuel-tx/src/transaction/fee.rs
MitchTurner Feb 23, 2024
822565f
Fix compilation, move tip into tx methods, include in more prop tests
MitchTurner Feb 23, 2024
da76d37
fmt
MitchTurner Feb 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Changed
#### Breaking

- [#682](https://github.com/FuelLabs/fuel-vm/pull/682): Include `Tip` policy in fee calculation
- [#683](https://github.com/FuelLabs/fuel-vm/pull/683): Simplify `InterpreterStorage` by removing dependency on `MerkleRootStorage` and removing `merkle_` prefix from method names.
- [#672](https://github.com/FuelLabs/fuel-vm/pull/672): Remove `GasPrice` policy
- [#672](https://github.com/FuelLabs/fuel-vm/pull/672): Add `gas_price` field to transaction execution
Expand Down
32 changes: 24 additions & 8 deletions fuel-tx/src/transaction/fee.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::{
field,
field::WitnessLimit,
field::{
Tip,
WitnessLimit,
},
input::{
coin::{
CoinPredicate,
Expand All @@ -13,6 +16,7 @@ use crate::{
MessageDataSigned,
},
},
policies::PolicyType,
FeeParameters,
GasCosts,
Input,
Expand Down Expand Up @@ -92,10 +96,17 @@ impl TransactionFee {
where
T: Chargeable,
{
let tip = tx.tip();
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, gas_price, tip)
.try_into()
.ok()?;
let max_fee = tx
.max_fee(gas_costs, params, gas_price, tip)
.try_into()
.ok()?;

if min_fee > max_fee {
return None;
Expand Down Expand Up @@ -150,12 +161,14 @@ pub trait Chargeable: field::Inputs + field::Witnesses + field::Policies {
gas_costs: &GasCosts,
fee: &FeeParameters,
gas_price: Word,
tip: Word,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you want to get it from the policies

) -> u128 {
gas_to_fee(
let gas_fee = gas_to_fee(
self.min_gas(gas_costs, fee),
gas_price,
fee.gas_price_factor,
)
);
gas_fee + tip as u128
MitchTurner marked this conversation as resolved.
Show resolved Hide resolved
}

/// Returns the maximum possible fee after the end of transaction execution.
Expand All @@ -166,12 +179,14 @@ pub trait Chargeable: field::Inputs + field::Witnesses + field::Policies {
gas_costs: &GasCosts,
fee: &FeeParameters,
gas_price: Word,
tip: Word,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you want to get it from the policies

) -> u128 {
gas_to_fee(
let gas_fee = gas_to_fee(
self.max_gas(gas_costs, fee),
gas_price,
fee.gas_price_factor,
)
);
gas_fee + tip as u128
MitchTurner marked this conversation as resolved.
Show resolved Hide resolved
}

/// Returns the fee amount that can be refunded back based on the `used_gas` and
Expand All @@ -192,8 +207,9 @@ pub trait Chargeable: field::Inputs + field::Witnesses + field::Policies {
let total_used_gas = min_gas.saturating_add(used_gas);
let used_fee = gas_to_fee(total_used_gas, gas_price, fee.gas_price_factor);
MitchTurner marked this conversation as resolved.
Show resolved Hide resolved

let tip = self.policies().get(PolicyType::Tip).unwrap_or(0);
let refund = self
.max_fee(gas_costs, fee, gas_price)
.max_fee(gas_costs, fee, gas_price, tip)
.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
Expand Down
3 changes: 2 additions & 1 deletion fuel-tx/src/transaction/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,8 @@ 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 {
let tip = tx.policies().get(PolicyType::Tip).unwrap_or(0);
if tx.max_fee(gas_costs, fee_params, gas_price, tip) > max_fee_limit as u128 {
Err(ValidityError::TransactionMaxFeeLimitExceeded)?
}
}
Expand Down
77 changes: 75 additions & 2 deletions fuel-vm/src/checked_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ impl From<PredicateVerificationFailed> for CheckError {
}

#[cfg(feature = "random")]
#[allow(non_snake_case)]
#[cfg(test)]
mod tests {
#![allow(clippy::cast_possible_truncation)]
Expand All @@ -656,6 +657,7 @@ mod tests {
use fuel_tx::{
field::{
ScriptGasLimit,
Tip,
WitnessLimit,
Witnesses,
},
Expand Down Expand Up @@ -949,6 +951,7 @@ mod tests {
input_amount: u64,
gas_price_factor: u64,
seed: u64,
tip: u64,
) -> TestResult {
// dont divide by zero
if gas_price_factor == 0 {
Expand All @@ -966,8 +969,8 @@ mod tests {
// When
let refund = tx.refund_fee(&gas_costs, &fee_params, used_gas, gas_price);

let min_fee = tx.min_fee(&gas_costs, &fee_params, gas_price);
let max_fee = tx.max_fee(&gas_costs, &fee_params, gas_price);
let min_fee = tx.min_fee(&gas_costs, &fee_params, gas_price, tip);
let max_fee = tx.max_fee(&gas_costs, &fee_params, gas_price, tip);

// Then
if let Some(refund) = refund {
Expand Down Expand Up @@ -1466,6 +1469,76 @@ mod tests {
assert_eq!(err, CheckError::Validity(ValidityError::BalanceOverflow));
}

fn arb_tx(rng: &mut StdRng) -> Script {
let input_amount = 1000;
let gas_limit = 1000;
base_asset_tx(rng, input_amount, gas_limit)
}

#[test]
fn into_checked_basic__min_fee_calc_includes_tip() {
let rng = &mut StdRng::seed_from_u64(2322u64);
let gas_price = 1;
let mut tx = arb_tx(rng);

// given
let tipless_tx = tx.clone();

let min_fee_without_tip = tipless_tx
.into_checked_basic(1.into(), &ConsensusParameters::standard(), gas_price)
.unwrap()
.metadata()
.fee
.min_fee();

let tip = 100;

// when
tx.set_tip(tip);

let min_fee_with_tip = tx
.into_checked_basic(1.into(), &ConsensusParameters::standard(), gas_price)
.unwrap()
.metadata()
.fee
.min_fee();

// then
assert_eq!(min_fee_without_tip + tip, min_fee_with_tip);
}

#[test]
fn into_checked_basic__max_fee_calc_includes_tip() {
let rng = &mut StdRng::seed_from_u64(2322u64);
let gas_price = 1;
let mut tx = arb_tx(rng);

// given
let tipless_tx = tx.clone();

let max_fee_without_tip = tipless_tx
.into_checked_basic(1.into(), &ConsensusParameters::standard(), gas_price)
.unwrap()
.metadata()
.fee
.max_fee();

let tip = 100;

// when
tx.set_tip(tip);

let max_fee_with_tip = tx
.into_checked_basic(1.into(), &ConsensusParameters::standard(), gas_price)
.unwrap()
.metadata()
.fee
.max_fee();

// then
assert_eq!(max_fee_without_tip + tip, max_fee_with_tip);
}

#[test]
fn gas_fee_cant_overflow() {
let rng = &mut StdRng::seed_from_u64(2322u64);
Expand Down
Loading