Skip to content

Commit 4a55b7d

Browse files
rafal-chxgreenx
andauthored
Enforce the block size limit (#2195)
Closes #2133 ## Linked Issues/PRs Built on top of: #2188 ## Description This PR adds the handling and enforcement of then new consensus parameter added [here](#2188). Changes summary: - `TransactionsSource` and `transaction_selector` use the new `block_transaction_size_limit` parameter. - `ExecutionData` contains `used_size` in addition to `used_gas` - Introduces integration tests covering the expected behavior - E2E tests affected by the new limit are updated ## Checklist - [X] Breaking changes are clearly marked as such in the PR description and changelog - [X] New behavior is reflected in tests ### Before requesting review - [X] I have reviewed the code myself --------- Co-authored-by: green <[email protected]>
1 parent 7b8713c commit 4a55b7d

File tree

13 files changed

+404
-91
lines changed

13 files changed

+404
-91
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77
## [Unreleased]
88

99
### Added
10+
- [2195](https://github.com/FuelLabs/fuel-core/pull/2195): Added enforcement of the limit on the size of the L2 transactions per block according to the `block_transaction_size_limit` parameter.
1011
- [2131](https://github.com/FuelLabs/fuel-core/pull/2131): Add flow in TxPool in order to ask to newly connected peers to share their transaction pool
1112
- [2182](https://github.com/FuelLabs/fuel-core/pull/2151): Limit number of transactions that can be fetched via TxSource::next
1213
- [2189](https://github.com/FuelLabs/fuel-core/pull/2151): Select next DA height to never include more than u16::MAX -1 transactions from L1.

bin/e2e-test-client/tests/integration_tests.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ fn dev_config() -> Config {
119119
.consensus_parameters
120120
.set_tx_params(tx_parameters);
121121
chain_config.consensus_parameters.set_fee_params(fee_params);
122+
if let Err(_e) = chain_config
123+
.consensus_parameters
124+
.set_block_transaction_size_limit(u64::MAX)
125+
{
126+
eprintln!("failed to set block transaction size limit");
127+
}
122128
chain_config.state_transition_bytecode =
123129
fuel_core::upgradable_executor::WASM_BYTECODE.to_vec();
124130
let reader = reader.with_chain_config(chain_config);

crates/fuel-core/src/service/adapters/executor.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@ use fuel_core_types::{
99
};
1010

1111
impl fuel_core_executor::ports::TransactionsSource for TransactionsSource {
12-
// TODO: Use `size_limit` https://github.com/FuelLabs/fuel-core/issues/2133
1312
fn next(
1413
&self,
1514
gas_limit: u64,
1615
transactions_limit: u16,
17-
_: u32,
16+
block_transaction_size_limit: u32,
1817
) -> Vec<MaybeCheckedTransaction> {
1918
self.txpool
20-
.select_transactions(gas_limit, transactions_limit)
19+
.select_transactions(
20+
gas_limit,
21+
transactions_limit,
22+
block_transaction_size_limit,
23+
)
2124
.into_iter()
2225
.map(|tx| {
2326
MaybeCheckedTransaction::CheckedTransaction(

crates/services/executor/src/executor.rs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ impl TransactionsSource for OnceTransactionsSource {
209209
pub struct ExecutionData {
210210
coinbase: u64,
211211
used_gas: u64,
212+
used_size: u32,
212213
tx_count: u16,
213214
found_mint: bool,
214215
message_ids: Vec<MessageId>,
@@ -224,6 +225,7 @@ impl ExecutionData {
224225
ExecutionData {
225226
coinbase: 0,
226227
used_gas: 0,
228+
used_size: 0,
227229
tx_count: 0,
228230
found_mint: false,
229231
message_ids: Vec::new(),
@@ -296,6 +298,7 @@ where
296298
skipped_transactions,
297299
coinbase,
298300
used_gas,
301+
used_size,
299302
..
300303
} = execution_data;
301304

@@ -306,8 +309,8 @@ where
306309
let finalized_block_id = block.id();
307310

308311
debug!(
309-
"Block {:#x} fees: {} gas: {}",
310-
finalized_block_id, coinbase, used_gas
312+
"Block {:#x} fees: {} gas: {} tx_size: {}",
313+
finalized_block_id, coinbase, used_gas, used_size
311314
);
312315

313316
let result = ExecutionResult {
@@ -331,6 +334,7 @@ where
331334
let ExecutionData {
332335
coinbase,
333336
used_gas,
337+
used_size,
334338
tx_status,
335339
events,
336340
changes,
@@ -340,8 +344,8 @@ where
340344
let finalized_block_id = block.id();
341345

342346
debug!(
343-
"Block {:#x} fees: {} gas: {}",
344-
finalized_block_id, coinbase, used_gas
347+
"Block {:#x} fees: {} gas: {} tx_size: {}",
348+
finalized_block_id, coinbase, used_gas, used_size
345349
);
346350

347351
let result = ValidationResult { tx_status, events };
@@ -575,10 +579,15 @@ where
575579
..
576580
} = components;
577581
let block_gas_limit = self.consensus_params.block_gas_limit();
582+
let block_transaction_size_limit = self
583+
.consensus_params
584+
.block_transaction_size_limit()
585+
.try_into()
586+
.unwrap_or(u32::MAX);
578587

579588
let mut remaining_gas_limit = block_gas_limit.saturating_sub(data.used_gas);
580-
// TODO: Handle `remaining_size` https://github.com/FuelLabs/fuel-core/issues/2133
581-
let remaining_size = u32::MAX;
589+
let mut remaining_block_transaction_size_limit =
590+
block_transaction_size_limit.saturating_sub(data.used_size);
582591

583592
// We allow at most u16::MAX transactions in a block, including the mint transaction.
584593
// When processing l2 transactions, we must take into account transactions from the l1
@@ -587,7 +596,11 @@ where
587596
let mut remaining_tx_count = MAX_TX_COUNT.saturating_sub(data.tx_count);
588597

589598
let mut regular_tx_iter = l2_tx_source
590-
.next(remaining_gas_limit, remaining_tx_count, remaining_size)
599+
.next(
600+
remaining_gas_limit,
601+
remaining_tx_count,
602+
remaining_block_transaction_size_limit,
603+
)
591604
.into_iter()
592605
.take(remaining_tx_count as usize)
593606
.peekable();
@@ -614,11 +627,17 @@ where
614627
}
615628
}
616629
remaining_gas_limit = block_gas_limit.saturating_sub(data.used_gas);
630+
remaining_block_transaction_size_limit =
631+
block_transaction_size_limit.saturating_sub(data.used_size);
617632
remaining_tx_count = MAX_TX_COUNT.saturating_sub(data.tx_count);
618633
}
619634

620635
regular_tx_iter = l2_tx_source
621-
.next(remaining_gas_limit, remaining_tx_count, remaining_size)
636+
.next(
637+
remaining_gas_limit,
638+
remaining_tx_count,
639+
remaining_block_transaction_size_limit,
640+
)
622641
.into_iter()
623642
.take(remaining_tx_count as usize)
624643
.peekable();
@@ -1413,6 +1432,8 @@ where
14131432
tx_id: TxId,
14141433
) -> ExecutorResult<()> {
14151434
let (used_gas, tx_fee) = self.total_fee_paid(tx, &receipts, gas_price)?;
1435+
let used_size = tx.metered_bytes_size().try_into().unwrap_or(u32::MAX);
1436+
14161437
execution_data.coinbase = execution_data
14171438
.coinbase
14181439
.checked_add(tx_fee)
@@ -1421,6 +1442,10 @@ where
14211442
.used_gas
14221443
.checked_add(used_gas)
14231444
.ok_or(ExecutorError::GasOverflow)?;
1445+
execution_data.used_size = execution_data
1446+
.used_size
1447+
.checked_add(used_size)
1448+
.ok_or(ExecutorError::TxSizeOverflow)?;
14241449

14251450
if !reverted {
14261451
execution_data

crates/services/executor/src/ports.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,14 @@ impl TransactionExt for MaybeCheckedTransaction {
115115
}
116116

117117
pub trait TransactionsSource {
118-
/// Returns the next batch of transactions to satisfy the `gas_limit`.
118+
/// Returns the next batch of transactions to satisfy the `gas_limit` and `block_transaction_size_limit`.
119119
/// The returned batch has at most `tx_count_limit` transactions, none
120120
/// of which has a size in bytes greater than `size_limit`.
121121
fn next(
122122
&self,
123123
gas_limit: u64,
124124
tx_count_limit: u16,
125-
size_limit: u32,
125+
block_transaction_size_limit: u32,
126126
) -> Vec<MaybeCheckedTransaction>;
127127
}
128128

crates/services/txpool/src/service.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ use fuel_core_types::{
4141
txpool::{
4242
ArcPoolTx,
4343
InsertionResult,
44-
PoolTransaction,
4544
TransactionStatus,
4645
},
4746
},
@@ -396,13 +395,14 @@ impl<P2P, ViewProvider, WasmChecker, GasPriceProvider, ConsensusProvider, MP>
396395
&self,
397396
max_gas: u64,
398397
transactions_limit: u16,
398+
block_transaction_size_limit: u32,
399399
) -> Vec<ArcPoolTx> {
400400
let mut guard = self.txpool.lock();
401401
let txs = guard.includable();
402-
let sorted_txs: Vec<Arc<PoolTransaction>> = select_transactions(txs, max_gas)
403-
.into_iter()
404-
.take(transactions_limit as usize)
405-
.collect();
402+
let sorted_txs: Vec<_> =
403+
select_transactions(txs, max_gas, block_transaction_size_limit)
404+
.take(transactions_limit as usize)
405+
.collect();
406406

407407
for tx in sorted_txs.iter() {
408408
guard.remove_committed_tx(&tx.id());

0 commit comments

Comments
 (0)