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

Fixed incorrect predicate estimation when max predicate gas is less than max tx gas #852

Merged
merged 2 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Fixed
- [#852](https://github.com/FuelLabs/fuel-vm/pull/852): Fixed incorrect predicate estimation when max predicate gas is less than max tx gas.

## [Version 0.58.0]

### Added
Expand Down
1 change: 1 addition & 0 deletions fuel-tx/src/transaction/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ mod tests;
pub use error::ValidityError;

impl Input {
#[cfg(any(feature = "typescript", test))]
pub fn check(
&self,
index: usize,
Expand Down
13 changes: 7 additions & 6 deletions fuel-vm/src/interpreter/executors/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,13 @@ pub mod predicates {
let mut checks = vec![];
let tx_offset = params.tx_offset;

let max_gas_per_tx = params.max_gas_per_tx;
let max_gas_per_predicate = params.max_gas_per_predicate;
let available_gas = core::cmp::min(max_gas_per_predicate, max_gas_per_tx);
let predicate_action = match kind {
PredicateRunKind::Verifying(_) => PredicateAction::Verifying,
PredicateRunKind::Estimating(_) => {
let max_gas_per_tx = params.max_gas_per_tx;
let max_gas_per_predicate = params.max_gas_per_predicate;
let available_gas = core::cmp::min(max_gas_per_predicate, max_gas_per_tx);

PredicateAction::Estimating { available_gas }
}
};
Expand Down Expand Up @@ -319,15 +320,15 @@ pub mod predicates {
let max_gas = kind.tx().max_gas(&params.gas_costs, &params.fee_params);
let max_gas_per_tx = params.max_gas_per_tx;
let max_gas_per_predicate = params.max_gas_per_predicate;
let mut available_gas =
core::cmp::min(max_gas_per_predicate, max_gas_per_tx).saturating_sub(max_gas);
let mut global_available_gas = max_gas_per_tx.saturating_sub(max_gas);

for index in 0..kind.tx().inputs().len() {
let tx = kind.tx().clone();

if let Some(predicate) =
RuntimePredicate::from_tx(&tx, params.tx_offset, index)
{
let available_gas = global_available_gas.min(max_gas_per_predicate);
let predicate_action = match kind {
PredicateRunKind::Verifying(_) => PredicateAction::Verifying,
PredicateRunKind::Estimating(_) => {
Expand All @@ -343,7 +344,7 @@ pub mod predicates {
memory.as_mut(),
storage,
);
available_gas = available_gas.saturating_sub(gas_used);
global_available_gas = global_available_gas.saturating_sub(gas_used);
let result = result.map(|_| (gas_used, index));
checks.push(result);
}
Expand Down
69 changes: 68 additions & 1 deletion fuel-vm/src/tests/predicate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![cfg(feature = "std")]
#![allow(non_snake_case)]

use fuel_asm::{
op,
Expand Down Expand Up @@ -36,7 +37,10 @@ use crate::{
storage::predicate::EmptyStorage,
};
use core::iter;
use fuel_tx::ConsensusParameters;
use fuel_tx::{
consensus_parameters::gas::GasCostsValuesV4,
ConsensusParameters,
};

pub struct TokioWithRayon;

Expand Down Expand Up @@ -158,6 +162,69 @@ where
}
}

#[test]
fn estimate_predicate_works_when_max_gas_per_predicate_less_than_tx_gas__10_inputs() {
let mut rng = StdRng::seed_from_u64(2322u64);
let predicate: Vec<u8> = iter::once(op::ret(0x01)).collect();
let predicate_data = vec![];
let predicate_owner = Input::predicate_owner(&predicate);

let mut builder = TransactionBuilder::script(vec![], vec![]);

const PREDICATE_COUNT: u64 = 10;
const MAX_PREDICATE_GAS: u64 = 1_000_000;
const MAX_TX_GAS: u64 = MAX_PREDICATE_GAS * PREDICATE_COUNT;

for _ in 0..PREDICATE_COUNT {
let input = Input::coin_predicate(
rng.gen(),
predicate_owner,
rng.gen(),
rng.gen(),
rng.gen(),
0,
predicate.clone(),
predicate_data.clone(),
);

builder.add_input(input.clone());
}

let mut script = builder.finalize();

// Given
let gas_costs = GasCostsValuesV4 {
ret: MAX_PREDICATE_GAS,
..GasCostsValuesV4::free()
};
let gas_costs = GasCosts::new(gas_costs.into());
let predicate_param =
PredicateParameters::default().with_max_gas_per_predicate(MAX_PREDICATE_GAS);
let tx_param = TxParameters::default().with_max_gas_per_tx(MAX_TX_GAS);
let fee_param = FeeParameters::default().with_gas_per_byte(0);

let mut consensus_params = ConsensusParameters::standard();
consensus_params.set_gas_costs(gas_costs.clone());
consensus_params.set_predicate_params(predicate_param);
consensus_params.set_tx_params(tx_param);
consensus_params.set_fee_params(fee_param);
let gas_before_estimation = script.max_gas(&gas_costs, &fee_param);

// When
script
.estimate_predicates(
&consensus_params.into(),
MemoryInstance::new(),
&EmptyStorage,
)
.unwrap();

// Then
let gas_after_estimation = script.max_gas(&gas_costs, &fee_param);
assert_eq!(gas_before_estimation, 0);
assert_eq!(gas_after_estimation, MAX_TX_GAS);
}

#[tokio::test]
async fn predicate_minimal() {
let predicate = iter::once(op::ret(0x01));
Expand Down
Loading