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

Add verification on transaction dry_run that they don't spend more than block gas limit #2151

Merged
merged 22 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added
- [2135](https://github.com/FuelLabs/fuel-core/pull/2135): Added metrics logging for number of blocks served over the p2p req/res protocol.

- [2151](https://github.com/FuelLabs/fuel-core/pull/2151): Added limitations on gas used during dry_run in API.

## [Version 0.35.0]

Expand Down
37 changes: 33 additions & 4 deletions crates/fuel-core/src/schema/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ use fuel_core_txpool::{
use fuel_core_types::{
fuel_tx::{
Cacheable,
Chargeable,
FeeParameters,
GasCosts,
Transaction as FuelTx,
UniqueIdentifier,
},
Expand Down Expand Up @@ -276,17 +279,25 @@ impl TxMutation {
gas_price: Option<U64>,
) -> async_graphql::Result<Vec<DryRunTransactionExecutionStatus>> {
let block_producer = ctx.data_unchecked::<BlockProducer>();
let params = ctx
let consensus_params = ctx
.data_unchecked::<ConsensusProvider>()
.latest_consensus_params();
let block_gas_limit = consensus_params.block_gas_limit();

let mut transactions = txs
.iter()
.map(|tx| FuelTx::from_bytes(&tx.0))
.collect::<Result<Vec<FuelTx>, _>>()?;
for transaction in &mut transactions {
transaction.precompute(&params.chain_id())?;
}
let fee = consensus_params.fee_params();
transactions.iter_mut().try_fold::<_, _, async_graphql::Result<u64>>(0u64, |acc, tx| {
let gas = max_gas(tx, &consensus_params.gas_costs(), fee)?;
tx.precompute(&consensus_params.chain_id())?;
let gas = gas.saturating_add(acc);
if gas > block_gas_limit {
return Err(anyhow::anyhow!("The sum of the gas usable by the transactions is greater than the block gas limit").into());
}
Ok(gas)
})?;

let tx_statuses = block_producer
.dry_run_txs(
Expand Down Expand Up @@ -404,6 +415,24 @@ impl TxStatusSubscription {
}
}

fn max_gas(
tx: &FuelTx,
gas_costs: &GasCosts,
fee: &FeeParameters,
) -> async_graphql::Result<u64> {
match tx {
FuelTx::Script(tx) => Ok(tx.max_gas(gas_costs, fee)),
FuelTx::Create(tx) => Ok(tx.max_gas(gas_costs, fee)),
FuelTx::Mint(_) => Err(anyhow::anyhow!(
"Mint transactions can't be executed in a dry-run"
)
.into()),
FuelTx::Upgrade(tx) => Ok(tx.max_gas(gas_costs, fee)),
FuelTx::Upload(tx) => Ok(tx.max_gas(gas_costs, fee)),
FuelTx::Blob(tx) => Ok(tx.max_gas(gas_costs, fee)),
}
}

async fn submit_and_await_status<'a>(
ctx: &'a Context<'a>,
tx: HexString,
Expand Down
36 changes: 36 additions & 0 deletions tests/tests/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,42 @@ async fn dry_run_create() {
assert_eq!(err.kind(), NotFound);
}

#[tokio::test]
async fn dry_run_above_block_gas_limit() {
let config = Config::local_node();
let srv = FuelService::new_node(config).await.unwrap();
let client = FuelClient::from(srv.bound_address);

// Given
let gas_limit = client
.consensus_parameters(0)
.await
.unwrap()
.unwrap()
.block_gas_limit();
let maturity = Default::default();

let script = [
op::addi(0x10, RegId::ZERO, 0xca),
op::addi(0x11, RegId::ZERO, 0xba),
op::log(0x10, 0x11, RegId::ZERO, RegId::ZERO),
op::ret(RegId::ONE),
];
let script = script.into_iter().collect();

let tx = TransactionBuilder::script(script, vec![])
.script_gas_limit(gas_limit)
.maturity(maturity)
.add_random_fee_input()
.finalize_as_transaction();

// When
match client.dry_run(&[tx.clone()]).await {
Ok(_) => panic!("Expected error"),
Err(e) => assert_eq!(e.to_string(), "Response errors; The sum of the gas usable by the transactions is greater than the block gas limit".to_owned()),
}
}

#[tokio::test]
async fn submit() {
let srv = FuelService::new_node(Config::local_node()).await.unwrap();
Expand Down