refactor: use revm blob fee calc (#4637)

This commit is contained in:
Matthias Seitz
2023-09-18 18:50:11 +02:00
committed by GitHub
parent 69045fd665
commit 20455d0550
6 changed files with 16 additions and 68 deletions

View File

@ -176,7 +176,7 @@ impl PayloadBuilderAttributes {
parent.next_block_base_fee(chain_spec.base_fee_params).unwrap_or_default(),
),
// calculate excess gas based on parent block's blob gas usage
excess_blob_gas: parent.next_block_blob_fee().map(|fee| fee.saturating_to()),
excess_blob_gas: parent.next_block_blob_fee(),
};
(cfg, block_env)

View File

@ -1,6 +1,6 @@
//! [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844#parameters) protocol constants and utils for shard Blob Transactions.
use crate::{kzg::KzgSettings, U128};
use crate::kzg::KzgSettings;
use once_cell::sync::Lazy;
use std::{io::Write, sync::Arc};
@ -66,32 +66,6 @@ pub enum LoadKzgSettingsError {
KzgError(c_kzg::Error),
}
/// Calculates the blob fee for the given excess blob gas.
pub fn blob_fee(excess_blob_gas: u64) -> U128 {
fake_exponential(
U128::from(BLOB_TX_MIN_BLOB_GASPRICE),
U128::from(excess_blob_gas),
U128::from(BLOB_GASPRICE_UPDATE_FRACTION),
)
}
/// Approximates factor * e ** (numerator / denominator) using Taylor expansion.
///
/// This is used to calculate the blob price.
///
/// See also <https://eips.ethereum.org/EIPS/eip-4844#helpers>
pub fn fake_exponential(factor: U128, numerator: U128, denominator: U128) -> U128 {
let mut output = U128::ZERO;
let mut numerator_accum = factor.saturating_mul(denominator);
let mut i = U128::from(1u64);
while numerator_accum > U128::ZERO {
output += numerator_accum;
numerator_accum = numerator_accum * numerator / (denominator * i);
i += U128::from(1u64);
}
output / denominator
}
#[cfg(test)]
mod tests {
use super::*;
@ -100,29 +74,4 @@ mod tests {
fn ensure_load_kzg_settings() {
let _settings = Arc::clone(&MAINNET_KZG_TRUSTED_SETUP);
}
#[test]
fn test_fake_exp() {
// <https://github.com/ethereum/go-ethereum/blob/28857080d732857030eda80c69b9ba2c8926f221/consensus/misc/eip4844/eip4844_test.go#L78-L78>
for (factor, num, denom, expected) in &[
(1u64, 0u64, 1u64, 1u64),
(38493, 0, 1000, 38493),
(0, 1234, 2345, 0),
(1, 2, 1, 6), // approximate 7.389
(1, 4, 2, 6),
(1, 3, 1, 16), // approximate 20.09
(1, 6, 2, 18),
(1, 4, 1, 49), // approximate 54.60
(1, 8, 2, 50),
(10, 8, 2, 542), // approximate 540.598
(11, 8, 2, 596), // approximate 600.58
(1, 5, 1, 136), // approximate 148.4
(1, 5, 2, 11), // approximate 12.18
(2, 5, 2, 23), // approximate 24.36
(1, 50000000, 2225652, 5709098764),
] {
let res = fake_exponential(U128::from(*factor), U128::from(*num), U128::from(*denom));
assert_eq!(res, U128::from(*expected));
}
}
}

View File

@ -6,6 +6,9 @@ use crate::{
};
use sha2::{Digest, Sha256};
// re-exports from revm for calculating blob fee
pub use revm_primitives::calc_blob_fee;
/// Calculates the versioned hash for a KzgCommitment
///
/// Specified in [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844#header-extension)

View File

@ -1,14 +1,12 @@
use crate::{
basefee::calculate_next_block_base_fee,
eip4844::calculate_excess_blob_gas,
eip4844::{calc_blob_fee, calculate_excess_blob_gas},
keccak256,
proofs::{EMPTY_LIST_HASH, EMPTY_ROOT},
BaseFeeParams, BlockBodyRoots, BlockHash, BlockNumHash, BlockNumber, Bloom, Bytes, H160, H256,
H64, U128, U256,
H64, U256,
};
use bytes::{Buf, BufMut, BytesMut};
use crate::constants::eip4844::blob_fee;
use reth_codecs::{add_arbitrary_tests, derive_arbitrary, main_codec, Compact};
use reth_rlp::{length_of_length, Decodable, Encodable, EMPTY_LIST_CODE, EMPTY_STRING_CODE};
use serde::{Deserialize, Serialize};
@ -187,8 +185,8 @@ impl Header {
/// Returns the blob fee for _this_ block according to the EIP-4844 spec.
///
/// Returns `None` if `excess_blob_gas` is None
pub fn blob_fee(&self) -> Option<U128> {
self.excess_blob_gas.map(blob_fee)
pub fn blob_fee(&self) -> Option<u64> {
self.excess_blob_gas.map(calc_blob_fee)
}
/// Returns the blob fee for the next block according to the EIP-4844 spec.
@ -196,8 +194,8 @@ impl Header {
/// Returns `None` if `excess_blob_gas` is None.
///
/// See also [Self::next_block_excess_blob_gas]
pub fn next_block_blob_fee(&self) -> Option<U128> {
self.next_block_excess_blob_gas().map(blob_fee)
pub fn next_block_blob_fee(&self) -> Option<u64> {
self.next_block_excess_blob_gas().map(calc_blob_fee)
}
/// Calculate base fee for next block according to the EIP-1559 spec.

View File

@ -14,7 +14,7 @@ use crate::{
use async_trait::async_trait;
use reth_network_api::NetworkInfo;
use reth_primitives::{
constants::eip4844::blob_fee,
eip4844::calc_blob_fee,
Address, BlockId, BlockNumberOrTag, Bytes, FromRecoveredPooledTransaction, Header,
IntoRecoveredTransaction, Receipt, SealedBlock,
TransactionKind::{Call, Create},
@ -887,7 +887,7 @@ pub(crate) fn build_transaction_receipt_with_block_receipts(
status_code: if receipt.success { Some(U64::from(1)) } else { Some(U64::from(0)) },
// EIP-4844 fields
blob_gas_price: meta.excess_blob_gas.map(blob_fee),
blob_gas_price: meta.excess_blob_gas.map(calc_blob_fee).map(U128::from),
blob_gas_used: transaction.transaction.blob_gas_used().map(U128::from),
};

View File

@ -93,7 +93,7 @@ pub async fn maintain_transaction_pool<Client, P, St, Tasks>(
pending_basefee: latest
.next_block_base_fee(chain_spec.base_fee_params)
.unwrap_or_default(),
pending_blob_fee: latest.next_block_blob_fee().map(|fee| fee.saturating_to()),
pending_blob_fee: latest.next_block_blob_fee(),
};
pool.set_block_info(info);
}
@ -239,8 +239,7 @@ pub async fn maintain_transaction_pool<Client, P, St, Tasks>(
// fees for the next block: `new_tip+1`
let pending_block_base_fee =
new_tip.next_block_base_fee(chain_spec.base_fee_params).unwrap_or_default();
let pending_block_blob_fee =
new_tip.next_block_blob_fee().map(|fee| fee.saturating_to());
let pending_block_blob_fee = new_tip.next_block_blob_fee();
// we know all changed account in the new chain
let new_changed_accounts: HashSet<_> =
@ -321,8 +320,7 @@ pub async fn maintain_transaction_pool<Client, P, St, Tasks>(
// fees for the next block: `tip+1`
let pending_block_base_fee =
tip.next_block_base_fee(chain_spec.base_fee_params).unwrap_or_default();
let pending_block_blob_fee =
tip.next_block_blob_fee().map(|fee| fee.saturating_to());
let pending_block_blob_fee = tip.next_block_blob_fee();
let first_block = blocks.first();
trace!(