Skip to content

fix(l2): limit block to blob size #2292

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

Merged
merged 52 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
c9f49f5
Add withdrawals and deposits size calculcation
avilagaston9 Mar 18, 2025
9d5e584
Finish calculation and clone context
avilagaston9 Mar 19, 2025
13cacee
Finish basic logic
avilagaston9 Mar 20, 2025
cb50131
Fix check condition and debug
avilagaston9 Mar 21, 2025
63e85aa
Use levm and improve logging
avilagaston9 Mar 21, 2025
64e24d0
Clone context to avoid using levm
avilagaston9 Mar 21, 2025
0711be5
Count in bytes instead of bits
avilagaston9 Mar 21, 2025
d951b34
Merge branch 'main' into l2/limit_block_to_blob_size
avilagaston9 Mar 21, 2025
f9785eb
Remove unused import
avilagaston9 Mar 21, 2025
756e9e4
refactor: move l2 functionallity to the bottom
avilagaston9 Mar 21, 2025
29ab5d1
Add missing cfg
avilagaston9 Mar 21, 2025
4735726
Remove debugs
avilagaston9 Mar 21, 2025
af20ff5
Merge branch 'main' into l2/limit_block_to_blob_size
avilagaston9 Mar 21, 2025
4e3c59a
Update count to include header fields
avilagaston9 Mar 21, 2025
6824549
improve code readability
avilagaston9 Mar 21, 2025
b5db7a7
Merge branch 'main' into l2/limit_block_to_blob_size
avilagaston9 Mar 25, 2025
a3a1a9b
Move fill transactions to l2
avilagaston9 Mar 25, 2025
7e505af
Use l2 implementation in block producer
avilagaston9 Mar 25, 2025
a0c79cb
Fix linter
avilagaston9 Mar 25, 2025
0e8c0f0
Improve logging
avilagaston9 Mar 25, 2025
e5d8b34
Rename acc counters
avilagaston9 Mar 25, 2025
72828a2
Merge branch 'main' into l2/limit_block_to_blob_size
avilagaston9 Mar 25, 2025
b5e6a68
Restore feature flag
avilagaston9 Mar 25, 2025
3f7b17f
fix convertion from u64 to usize
avilagaston9 Mar 25, 2025
b5e9801
Fix throughput logs
avilagaston9 Mar 26, 2025
358b530
Merge branch 'main' into l2/limit_block_to_blob_size
avilagaston9 Mar 27, 2025
7bf995f
Merge branch 'main' into l2/limit_block_to_blob_size
avilagaston9 Mar 27, 2025
680b81b
Merge branch 'main' into l2/limit_block_to_blob_size
avilagaston9 Mar 27, 2025
6e69ba5
perf: add accounts_info cache
avilagaston9 Mar 28, 2025
c1313c4
Merge branch 'main' into l2/limit_block_to_blob_size
avilagaston9 Mar 28, 2025
b64abc6
Remove expect
avilagaston9 Mar 28, 2025
855de6b
Remove unnecesary annotation
avilagaston9 Mar 28, 2025
651ed90
Improve function docs
avilagaston9 Mar 31, 2025
f571482
Merge branch 'main' into l2/limit_block_to_blob_size
avilagaston9 Mar 31, 2025
c7e0d4d
Merge branch 'main' into l2/limit_block_to_blob_size
avilagaston9 Mar 31, 2025
621592b
Fix typo
avilagaston9 Mar 31, 2025
3169adb
Add state diff size short circuit
avilagaston9 Mar 31, 2025
70a3237
Merge branch 'main' into l2/limit_block_to_blob_size
avilagaston9 Mar 31, 2025
9640363
Merge branch 'main' into l2/limit_block_to_blob_size
avilagaston9 Apr 3, 2025
dc9d5d8
Add comment to EvmState clone()
avilagaston9 Apr 3, 2025
13ebbba
Fix linter
avilagaston9 Apr 3, 2025
cdb1eec
Update comment
avilagaston9 Apr 3, 2025
5a92c47
Merge branch 'main' into l2/limit_block_to_blob_size
avilagaston9 Apr 4, 2025
65a00ff
Make block_producer::build_payload async
avilagaston9 Apr 4, 2025
81d4446
Merge branch 'main' into l2/limit_block_to_blob_size
avilagaston9 Apr 11, 2025
5876e9e
Merge branch 'main' into l2/limit_block_to_blob_size
avilagaston9 Apr 15, 2025
482fa00
Merge branch 'main' into l2/limit_block_to_blob_size
avilagaston9 Apr 15, 2025
3908c26
Merge branch 'main' into l2/limit_block_to_blob_size
avilagaston9 Apr 15, 2025
ef531cf
Merge branch 'main' into l2/limit_block_to_blob_size
avilagaston9 Apr 15, 2025
ff33e10
Remove REVM comment
avilagaston9 Apr 16, 2025
0a1050a
Add doc comments on mutable variables
avilagaston9 Apr 16, 2025
29265cf
Fix linter
avilagaston9 Apr 16, 2025
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
48 changes: 24 additions & 24 deletions crates/blockchain/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,9 @@ pub fn calc_gas_limit(parent_gas_limit: u64) -> u64 {
limit
}

pub struct PayloadBuildContext<'a> {
pub payload: &'a mut Block,
#[derive(Clone)]
pub struct PayloadBuildContext {
pub payload: Block,
pub remaining_gas: u64,
pub receipts: Vec<Receipt>,
pub requests: Vec<EncodedRequests>,
Expand All @@ -173,12 +174,8 @@ pub struct PayloadBuildContext<'a> {
pub account_updates: Vec<AccountUpdate>,
}

impl<'a> PayloadBuildContext<'a> {
fn new(
payload: &'a mut Block,
evm_engine: EvmEngine,
storage: &Store,
) -> Result<Self, EvmError> {
impl PayloadBuildContext {
pub fn new(payload: Block, evm_engine: EvmEngine, storage: &Store) -> Result<Self, EvmError> {
let config = storage.get_chain_config()?;
let base_fee_per_blob_gas = calculate_base_fee_per_blob_gas(
payload.header.excess_blob_gas.unwrap_or_default(),
Expand All @@ -205,12 +202,12 @@ impl<'a> PayloadBuildContext<'a> {
}
}

impl<'a> PayloadBuildContext<'a> {
impl PayloadBuildContext {
fn parent_hash(&self) -> BlockHash {
self.payload.header.parent_hash
}

fn block_number(&self) -> BlockNumber {
pub fn block_number(&self) -> BlockNumber {
self.payload.header.number
}

Expand All @@ -229,16 +226,18 @@ pub struct PayloadBuildResult {
pub receipts: Vec<Receipt>,
pub requests: Vec<EncodedRequests>,
pub account_updates: Vec<AccountUpdate>,
pub payload: Block,
}

impl<'a> From<PayloadBuildContext<'a>> for PayloadBuildResult {
impl From<PayloadBuildContext> for PayloadBuildResult {
fn from(value: PayloadBuildContext) -> Self {
let PayloadBuildContext {
blobs_bundle,
block_value,
requests,
receipts,
account_updates,
payload,
..
} = value;

Expand All @@ -248,13 +247,14 @@ impl<'a> From<PayloadBuildContext<'a>> for PayloadBuildResult {
requests,
receipts,
account_updates,
payload,
}
}
}

impl Blockchain {
/// Completes the payload building process, return the block value
pub fn build_payload(&self, payload: &mut Block) -> Result<PayloadBuildResult, ChainError> {
pub fn build_payload(&self, payload: Block) -> Result<PayloadBuildResult, ChainError> {
let since = Instant::now();
let gas_limit = payload.header.gas_limit;

Expand Down Expand Up @@ -284,7 +284,7 @@ impl Blockchain {
Ok(context.into())
}

fn apply_withdrawals(&self, context: &mut PayloadBuildContext) -> Result<(), EvmError> {
pub fn apply_withdrawals(&self, context: &mut PayloadBuildContext) -> Result<(), EvmError> {
let binding = Vec::new();
let withdrawals = context
.payload
Expand All @@ -310,7 +310,7 @@ impl Blockchain {

/// Fetches suitable transactions from the mempool
/// Returns two transaction queues, one for plain and one for blob txs
fn fetch_mempool_transactions(
pub fn fetch_mempool_transactions(
&self,
context: &mut PayloadBuildContext,
) -> Result<(TransactionQueue, TransactionQueue), ChainError> {
Expand Down Expand Up @@ -445,7 +445,7 @@ impl Blockchain {

/// Executes the transaction, updates gas-related context values & return the receipt
/// The payload build context should have enough remaining gas to cover the transaction's gas_limit
fn apply_transaction(
pub fn apply_transaction(
&self,
head: &HeadTransaction,
context: &mut PayloadBuildContext,
Expand Down Expand Up @@ -523,7 +523,7 @@ impl Blockchain {
Ok(())
}

fn finalize_payload(&self, context: &mut PayloadBuildContext) -> Result<(), ChainError> {
pub fn finalize_payload(&self, context: &mut PayloadBuildContext) -> Result<(), ChainError> {
let parent_hash = context.payload.header.parent_hash;
let account_updates = context.vm.get_state_transitions(parent_hash)?;

Expand All @@ -543,7 +543,7 @@ impl Blockchain {

/// A struct representing suitable mempool transactions waiting to be included in a block
// TODO: Consider using VecDequeue instead of Vec
struct TransactionQueue {
pub struct TransactionQueue {
// The first transaction for each account along with its tip, sorted by highest tip
heads: Vec<HeadTransaction>,
// The remaining txs grouped by account and sorted by nonce
Expand All @@ -553,8 +553,8 @@ struct TransactionQueue {
}

#[derive(Clone, Debug, Eq, PartialEq)]
struct HeadTransaction {
tx: MempoolTransaction,
pub struct HeadTransaction {
pub tx: MempoolTransaction,
tip: u64,
}

Expand Down Expand Up @@ -603,24 +603,24 @@ impl TransactionQueue {
}

/// Remove all transactions from the queue
fn clear(&mut self) {
pub fn clear(&mut self) {
self.heads.clear();
self.txs.clear();
}

/// Returns true if there are no more transactions in the queue
fn is_empty(&self) -> bool {
pub fn is_empty(&self) -> bool {
self.heads.is_empty()
}

/// Returns the head transaction with the highest tip
/// If there is more than one transaction with the highest tip, return the one with the lowest timestamp
fn peek(&self) -> Option<HeadTransaction> {
pub fn peek(&self) -> Option<HeadTransaction> {
self.heads.first().cloned()
}

/// Removes current head transaction and all transactions from the given sender
fn pop(&mut self) {
pub fn pop(&mut self) {
if !self.is_empty() {
let sender = self.heads.remove(0).tx.sender();
self.txs.remove(&sender);
Expand All @@ -629,7 +629,7 @@ impl TransactionQueue {

/// Remove the top transaction
/// Add a tx from the same sender to the head transactions
fn shift(&mut self) -> Result<(), ChainError> {
pub fn shift(&mut self) -> Result<(), ChainError> {
let tx = self.heads.remove(0);
if let Some(txs) = self.txs.get_mut(&tx.tx.sender()) {
// Fetch next head
Expand Down
6 changes: 3 additions & 3 deletions crates/blockchain/smoke_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,9 @@ mod blockchain_integration_test {
// Create blockchain
let blockchain = Blockchain::default_with_store(store.clone().clone());

let mut block = create_payload(&args, store).unwrap();
blockchain.build_payload(&mut block).unwrap();
block
let block = create_payload(&args, store).unwrap();
let result = blockchain.build_payload(block).unwrap();
result.payload
}

fn test_store() -> Store {
Expand Down
13 changes: 9 additions & 4 deletions crates/l2/sequencer/block_producer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod payload_builder;
use std::{
sync::Arc,
time::{Duration, SystemTime, UNIX_EPOCH},
Expand All @@ -12,6 +13,7 @@ use ethrex_common::Address;
use ethrex_storage::Store;
use ethrex_vm::backends::BlockExecutionResult;
use keccak_hash::H256;
use payload_builder::build_payload;
use tokio::time::sleep;
use tracing::{debug, error, info};

Expand Down Expand Up @@ -99,14 +101,17 @@ impl BlockProducer {
beacon_root: Some(head_beacon_block_root),
version,
};
let mut payload = create_payload(&args, &store)?;
let payload = create_payload(&args, &store)?;

// Blockchain builds the payload from mempool txs and executes them
let payload_build_result = blockchain.build_payload(&mut payload)?;
info!("Built payload for new block {}", payload.header.number);
let payload_build_result = build_payload(blockchain.clone(), payload, &store)?;
info!(
"Built payload for new block {}",
payload_build_result.payload.header.number
);

// Blockchain stores block
let block = payload;
let block = payload_build_result.payload;
let chain_config = store.get_chain_config()?;
validate_block(&block, &head_header, &chain_config)?;

Expand Down
Loading