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

fix(l1): update calc_excess_blob_gas #2094

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions crates/blockchain/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,11 @@ pub fn validate_block(
validate_block_header(&block.header, parent_header).map_err(InvalidBlockError::from)?;

if chain_config.is_prague_activated(block.header.timestamp) {
validate_prague_header_fields(&block.header, parent_header)
validate_prague_header_fields(&block.header, parent_header, chain_config)
.map_err(InvalidBlockError::from)?;
verify_blob_gas_usage(block, chain_config)?;
} else if chain_config.is_cancun_activated(block.header.timestamp) {
validate_cancun_header_fields(&block.header, parent_header)
validate_cancun_header_fields(&block.header, parent_header, chain_config)
.map_err(InvalidBlockError::from)?;
verify_blob_gas_usage(block, chain_config)?;
} else {
Expand Down
18 changes: 2 additions & 16 deletions crates/blockchain/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use std::{
use ethrex_common::{
constants::GAS_PER_BLOB,
types::{
calculate_base_fee_per_blob_gas, calculate_base_fee_per_gas, compute_receipts_root,
compute_transactions_root, compute_withdrawals_root,
calc_excess_blob_gas, calculate_base_fee_per_blob_gas, calculate_base_fee_per_gas,
compute_receipts_root, compute_transactions_root, compute_withdrawals_root,
requests::{compute_requests_hash, EncodedRequests, Requests},
BlobsBundle, Block, BlockBody, BlockHash, BlockHeader, BlockNumber, ChainConfig,
MempoolTransaction, Receipt, Transaction, Withdrawal, DEFAULT_OMMERS_HASH,
Expand Down Expand Up @@ -161,20 +161,6 @@ pub fn calc_gas_limit(parent_gas_limit: u64) -> u64 {
limit
}

pub fn calc_excess_blob_gas(
parent_excess_blob_gas: u64,
parent_blob_gas_used: u64,
target: u64,
) -> u64 {
let excess_blob_gas = parent_excess_blob_gas + parent_blob_gas_used;
let target_blob_gas_per_block = target * GAS_PER_BLOB;
if excess_blob_gas < target_blob_gas_per_block {
0
} else {
excess_blob_gas - target_blob_gas_per_block
}
}

pub struct PayloadBuildContext<'a> {
pub payload: &'a mut Block,
pub evm_state: &'a mut EvmState,
Expand Down
54 changes: 38 additions & 16 deletions crates/common/types/block.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::{
BASE_FEE_MAX_CHANGE_DENOMINATOR, ELASTICITY_MULTIPLIER, GAS_LIMIT_ADJUSTMENT_FACTOR,
GAS_LIMIT_MINIMUM, INITIAL_BASE_FEE,
ChainConfig, BASE_FEE_MAX_CHANGE_DENOMINATOR, ELASTICITY_MULTIPLIER,
GAS_LIMIT_ADJUSTMENT_FACTOR, GAS_LIMIT_MINIMUM, INITIAL_BASE_FEE,
};
use crate::{
constants::MIN_BASE_FEE_PER_BLOB_GAS,
constants::{GAS_PER_BLOB, MIN_BASE_FEE_PER_BLOB_GAS},
types::{Receipt, Transaction},
Address, H256, U256,
};
Expand Down Expand Up @@ -559,16 +559,16 @@ pub fn validate_block_header(
pub fn validate_prague_header_fields(
header: &BlockHeader,
parent_header: &BlockHeader,
chain_config: &ChainConfig,
) -> Result<(), InvalidBlockHeaderError> {
if header.excess_blob_gas.is_none() {
return Err(InvalidBlockHeaderError::ExcessBlobGasNotPresent);
}
if header.blob_gas_used.is_none() {
return Err(InvalidBlockHeaderError::BlobGasUsedNotPresent);
}
if header.excess_blob_gas.unwrap() != calc_excess_blob_gas(parent_header) {
return Err(InvalidBlockHeaderError::ExcessBlobGasIncorrect);
}
validate_excess_blob_gas(header, parent_header, chain_config)?;

if header.parent_beacon_block_root.is_none() {
return Err(InvalidBlockHeaderError::ParentBeaconBlockRootNotPresent);
}
Expand All @@ -583,16 +583,15 @@ pub fn validate_prague_header_fields(
pub fn validate_cancun_header_fields(
header: &BlockHeader,
parent_header: &BlockHeader,
chain_config: &ChainConfig,
) -> Result<(), InvalidBlockHeaderError> {
if header.excess_blob_gas.is_none() {
return Err(InvalidBlockHeaderError::ExcessBlobGasNotPresent);
}
if header.blob_gas_used.is_none() {
return Err(InvalidBlockHeaderError::BlobGasUsedNotPresent);
}
if header.excess_blob_gas.unwrap() != calc_excess_blob_gas(parent_header) {
return Err(InvalidBlockHeaderError::ExcessBlobGasIncorrect);
}
validate_excess_blob_gas(header, parent_header, chain_config)?;
if header.parent_beacon_block_root.is_none() {
return Err(InvalidBlockHeaderError::ParentBeaconBlockRootNotPresent);
}
Expand Down Expand Up @@ -622,15 +621,38 @@ pub fn validate_pre_cancun_header_fields(
Ok(())
}

fn calc_excess_blob_gas(parent_header: &BlockHeader) -> u64 {
let parent_excess_blob_gas = parent_header.excess_blob_gas.unwrap_or_default();
let parent_blob_gas_used = parent_header.blob_gas_used.unwrap_or_default();
let parent_blob_gas = parent_excess_blob_gas + parent_blob_gas_used;
fn validate_excess_blob_gas(
header: &BlockHeader,
parent_header: &BlockHeader,
chain_config: &ChainConfig,
) -> Result<(), InvalidBlockHeaderError> {
let expected_excess_blob_gas = chain_config
.get_fork_blob_schedule(header.timestamp)
.map(|schedule| {
calc_excess_blob_gas(
parent_header.excess_blob_gas.unwrap_or_default(),
parent_header.blob_gas_used.unwrap_or_default(),
schedule.target,
)
})
.unwrap_or_default();
if header.excess_blob_gas.unwrap() != expected_excess_blob_gas {
return Err(InvalidBlockHeaderError::ExcessBlobGasIncorrect);
}
Ok(())
}

if parent_blob_gas < 393216_u64 {
0u64
pub fn calc_excess_blob_gas(
parent_excess_blob_gas: u64,
parent_blob_gas_used: u64,
target: u64,
) -> u64 {
let excess_blob_gas = parent_excess_blob_gas + parent_blob_gas_used;
let target_blob_gas_per_block = target * GAS_PER_BLOB;
if excess_blob_gas < target_blob_gas_per_block {
0
} else {
parent_blob_gas - 393216_u64
excess_blob_gas - target_blob_gas_per_block
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/networking/rpc/eth/fee_market.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use ethrex_blockchain::payload::{calc_excess_blob_gas, calc_gas_limit};
use ethrex_blockchain::payload::calc_gas_limit;
use ethrex_common::{
constants::GAS_PER_BLOB,
types::{
calculate_base_fee_per_blob_gas, calculate_base_fee_per_gas, Block, BlockHeader,
Transaction,
calc_excess_blob_gas, calculate_base_fee_per_blob_gas, calculate_base_fee_per_gas, Block,
BlockHeader, Transaction,
},
};
use serde::Serialize;
Expand Down