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 all 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
22 changes: 16 additions & 6 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 @@ -151,11 +155,13 @@ pub trait Chargeable: field::Inputs + field::Witnesses + field::Policies {
fee: &FeeParameters,
gas_price: Word,
) -> u128 {
gas_to_fee(
let tip = self.tip();
let gas_fee = gas_to_fee(
self.min_gas(gas_costs, fee),
gas_price,
fee.gas_price_factor,
)
);
gas_fee.saturating_add(tip as u128)
}

/// Returns the maximum possible fee after the end of transaction execution.
Expand All @@ -167,11 +173,13 @@ pub trait Chargeable: field::Inputs + field::Witnesses + field::Policies {
fee: &FeeParameters,
gas_price: Word,
) -> u128 {
gas_to_fee(
let tip = self.tip();
let gas_fee = gas_to_fee(
self.max_gas(gas_costs, fee),
gas_price,
fee.gas_price_factor,
)
);
gas_fee.saturating_add(tip as u128)
}

/// Returns the fee amount that can be refunded back based on the `used_gas` and
Expand All @@ -190,7 +198,9 @@ pub trait Chargeable: field::Inputs + field::Witnesses + field::Policies {
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, gas_price, fee.gas_price_factor);
let tip = self.policies().get(PolicyType::Tip).unwrap_or(0);
let used_fee = gas_to_fee(total_used_gas, gas_price, fee.gas_price_factor)
.saturating_add(tip as u128);

let refund = self
.max_fee(gas_costs, fee, gas_price)
Expand Down
100 changes: 91 additions & 9 deletions fuel-vm/src/checked_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,16 +646,19 @@ impl From<PredicateVerificationFailed> for CheckError {
}

#[cfg(feature = "random")]
#[allow(non_snake_case)]
#[cfg(test)]
mod tests {
#![allow(clippy::cast_possible_truncation)]

use super::*;
use alloc::vec;
use fuel_asm::op;
use fuel_crypto::SecretKey;
use fuel_tx::{
field::{
ScriptGasLimit,
Tip,
WitnessLimit,
Witnesses,
},
Expand Down Expand Up @@ -919,6 +922,7 @@ mod tests {
gas_limit: u64,
input_amount: u64,
gas_price_factor: u64,
tip: u64,
seed: u64,
) -> TestResult {
// dont divide by zero
Expand All @@ -930,7 +934,7 @@ mod tests {
let gas_costs = GasCosts::default();
let fee_params = FeeParameters::DEFAULT.with_gas_price_factor(gas_price_factor);
let base_asset_id = rng.gen();
let tx = predicate_message_coin_tx(rng, gas_limit, input_amount);
let tx = predicate_message_coin_tx(rng, gas_limit, input_amount, tip);

if let Ok(valid) =
is_valid_max_fee(&tx, gas_price, &gas_costs, &fee_params, &base_asset_id)
Expand All @@ -949,6 +953,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 @@ -958,7 +963,7 @@ mod tests {
let rng = &mut StdRng::seed_from_u64(seed);
let gas_costs = GasCosts::default();
let fee_params = FeeParameters::DEFAULT.with_gas_price_factor(gas_price_factor);
let tx = predicate_message_coin_tx(rng, gas_limit, input_amount);
let tx = predicate_message_coin_tx(rng, gas_limit, input_amount, tip);

// Given
let used_gas = 0;
Expand All @@ -985,6 +990,7 @@ mod tests {
input_amount: u64,
gas_price: u64,
gas_price_factor: u64,
tip: u64,
seed: u64,
) -> TestResult {
// verify min fee a transaction can consume based on bytes is correct
Expand All @@ -997,7 +1003,7 @@ mod tests {
let gas_costs = GasCosts::default();
let fee_params = FeeParameters::DEFAULT.with_gas_price_factor(gas_price_factor);
let base_asset_id = rng.gen();
let tx = predicate_message_coin_tx(rng, gas_limit, input_amount);
let tx = predicate_message_coin_tx(rng, gas_limit, input_amount, tip);

if let Ok(valid) =
is_valid_min_fee(&tx, &gas_costs, &fee_params, &base_asset_id, gas_price)
Expand Down Expand Up @@ -1466,6 +1472,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 Expand Up @@ -1526,7 +1602,7 @@ mod tests {
CheckError::Validity(ValidityError::InsufficientInputAmount {
asset: any_asset,
expected: input_amount + 1,
provided: input_amount
provided: input_amount,
}),
checked
);
Expand Down Expand Up @@ -1563,7 +1639,7 @@ mod tests {
.into_checked(
block_height,
&ConsensusParameters::standard_with_id(chain_id),
arb_gas_price
arb_gas_price,
)
.unwrap()
// Sets Checks::Signatures
Expand Down Expand Up @@ -1596,11 +1672,11 @@ mod tests {
.into_checked(
block_height,
&consensus_params,
arb_gas_price
arb_gas_price,
)
.unwrap()
// Sets Checks::Predicates
.check_predicates( &check_predicate_params)
.check_predicates(&check_predicate_params)
.unwrap();
assert!(checked
.checks()
Expand Down Expand Up @@ -1655,7 +1731,9 @@ mod tests {
.try_into()
.map_err(|_| ValidityError::BalanceOverflow)?;

let result = max_fee == available_balances.fee.max_fee();
let max_fee_with_tip = max_fee + tx.tip();

let result = max_fee_with_tip == available_balances.fee.max_fee();
Ok(result)
}

Expand Down Expand Up @@ -1701,7 +1779,9 @@ mod tests {
.try_into()
.map_err(|_| ValidityError::BalanceOverflow)?;

Ok(min_fee == available_balances.fee.min_fee())
let min_fee_with_tip = min_fee + tx.tip();

Ok(min_fee_with_tip == available_balances.fee.min_fee())
}

fn valid_coin_tx(
Expand Down Expand Up @@ -1783,8 +1863,10 @@ mod tests {
rng: &mut StdRng,
gas_limit: u64,
input_amount: u64,
tip: u64,
) -> Script {
TransactionBuilder::script(vec![], vec![])
.tip(tip)
.script_gas_limit(gas_limit)
.add_input(Input::message_coin_predicate(
rng.gen(),
Expand Down
Loading