Skip to content

Commit

Permalink
feat: add EIP-4844 transaction when auto-mining (#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wodann authored May 9, 2024
1 parent bd992ad commit a4d5b62
Show file tree
Hide file tree
Showing 54 changed files with 2,661 additions and 578 deletions.
5 changes: 5 additions & 0 deletions .changeset/curly-carpets-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/edr": minor
---

Added support for blob transactions (EIP-4844) when auto-mining is enabled and the mempool is empty
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion book/src/02_development/03_local_release.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ These are instructions for releasing the [EDR NPM package](../../crates/edr_napi
2. Go to the [edr_napi](../../crates/edr_napi/) directory.
3. Run `pnpm build`.
4. Look for the NAPI binary that was built for your platform. It has the format `edr.<PLATFORM>.node`. For example on Apple Silicon Macs, it's called `edr.darwin-arm64.node`.
5. Move the NAPI binary to the appropriate platform-specifc package in the [npm](../../crates/edr_napi/npm) directory. For example, on Apple Silicon Macs: `mv edr.darwin-arm64.node npm/darwin-arm64`.
5. Move the NAPI binary to the appropriate platform-specific package in the [npm](../../crates/edr_napi/npm) directory. For example, on Apple Silicon Macs: `mv edr.darwin-arm64.node npm/darwin-arm64`.
6. Complete the Verdaccio [publish steps](./02_verdaccio.md#usage) in the [edr_napi](../../crates/edr_napi/) directory. You can ignore the warnings about not finding NAPI binaries for other platforms.
8 changes: 5 additions & 3 deletions crates/edr_eth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
anyhow = "1.0.75"
alloy-rlp = { version = "0.3", default-features = false, features = ["derive"] }
c-kzg = { version = "1.0.0", default-features = false }
futures = { version = "0.3.28", default-features = false }
hash-db = { version = "0.15.2", default-features = false }
hash256-std-hasher = { version = "0.15.2", default-features = false }
Expand All @@ -20,9 +21,10 @@ reqwest = { version = "0.11", features = ["blocking", "json"] }
reqwest-middleware = { version = "0.2.4", default-features = false }
reqwest-retry = { version = "0.3.0", default-features = false }
reqwest-tracing = { version = "0.4.7", default-features = false, optional = true }
revm-primitives = { git = "https://github.com/NomicFoundation/revm", rev = "aceb093", version = "3.1", default-features = false, features = ["hashbrown"] }
revm-primitives = { git = "https://github.com/NomicFoundation/revm", rev = "aceb093", version = "3.1", default-features = false, features = ["c-kzg", "hashbrown"] }
serde = { version = "1.0.147", default-features = false, features = ["derive"], optional = true }
serde_json = { version = "1.0.89", optional = true }
sha2 = { version = "0.10.8", default-features = false }
sha3 = { version = "0.10.8", default-features = false }
thiserror = { version = "1.0.37", default-features = false }
tokio = { version = "1.21.2", default-features = false, features = ["fs", "macros", "sync"] }
Expand All @@ -48,7 +50,7 @@ walkdir = { version = "2.3.3", default-features = false }
[features]
default = ["std"]
rand = ["revm-primitives/rand"]
serde = ["dep:serde", "revm-primitives/serde", "serde_json"]
std = ["futures/std", "hash256-std-hasher/std", "hash-db/std", "hex/std", "itertools/use_std", "k256/std", "k256/precomputed-tables", "revm-primitives/std", "serde?/std", "sha3/std", "triehash/std", "uuid/std"]
serde = ["dep:serde", "c-kzg/serde", "revm-primitives/serde", "serde_json"]
std = ["futures/std", "hash256-std-hasher/std", "hash-db/std", "hex/std", "itertools/use_std", "k256/std", "k256/precomputed-tables", "revm-primitives/std", "serde?/std", "sha2/std", "sha3/std", "triehash/std", "uuid/std"]
tracing = ["dep:tracing", "reqwest-tracing"]
test-remote = ["serde"]
2 changes: 1 addition & 1 deletion crates/edr_eth/src/access_list.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Part of this code was adapted from ethers-rs and is distributed under their
// licenss:
// license:
// - https://github.com/gakonst/ethers-rs/blob/cba6f071aedafb766e82e4c2f469ed5e4638337d/LICENSE-APACHE
// - https://github.com/gakonst/ethers-rs/blob/cba6f071aedafb766e82e4c2f469ed5e4638337d/LICENSE-MIT
// For the original context see: https://github.com/gakonst/ethers-rs/blob/3d9c3290d42b77c510e5b5d0b6f7a2f72913bfff/ethers-core/src/types/transaction/eip2930.rs
Expand Down
2 changes: 1 addition & 1 deletion crates/edr_eth/src/account.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Part of this code was adapted from foundry and is distributed under their
// licenss:
// license:
// - https://github.com/foundry-rs/foundry/blob/01b16238ff87dc7ca8ee3f5f13e389888c2a2ee4/LICENSE-APACHE
// - https://github.com/foundry-rs/foundry/blob/01b16238ff87dc7ca8ee3f5f13e389888c2a2ee4/LICENSE-MIT
// For the original context see: https://github.com/foundry-rs/foundry/blob/01b16238ff87dc7ca8ee3f5f13e389888c2a2ee4/anvil/core/src/eth/proof.rs
Expand Down
19 changes: 15 additions & 4 deletions crates/edr_eth/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod reorg;
mod reward;

use alloy_rlp::{BufMut, Decodable, RlpDecodable, RlpEncodable};
use revm_primitives::{calc_excess_blob_gas, keccak256};
use revm_primitives::{calc_blob_gasprice, calc_excess_blob_gas, keccak256};

use self::difficulty::calculate_ethash_canonical_difficulty;
pub use self::{
Expand Down Expand Up @@ -243,7 +243,7 @@ impl PartialHeader {
base_fee: options.base_fee.or_else(|| {
if spec_id >= SpecId::LONDON {
Some(if let Some(parent) = &parent {
calculate_next_base_fee(parent)
calculate_next_base_fee_per_gas(parent)
} else {
// Initial base fee from https://eips.ethereum.org/EIPS/eip-1559
U256::from(1_000_000_000)
Expand Down Expand Up @@ -338,7 +338,7 @@ impl From<Header> for PartialHeader {
/// # Panics
///
/// Panics if the parent header does not contain a base fee.
pub fn calculate_next_base_fee(parent: &Header) -> U256 {
pub fn calculate_next_base_fee_per_gas(parent: &Header) -> U256 {
let elasticity = 2;
let base_fee_max_change_denominator = U256::from(8);

Expand Down Expand Up @@ -370,6 +370,17 @@ pub fn calculate_next_base_fee(parent: &Header) -> U256 {
}
}

/// Calculates the next base fee per blob gas for a post-Cancun block, given the
/// parent's header.
pub fn calculate_next_base_fee_per_blob_gas(parent: &Header) -> U256 {
parent
.blob_gas
.as_ref()
.map_or(U256::ZERO, |BlobGas { excess_gas, .. }| {
U256::from(calc_blob_gasprice(*excess_gas))
})
}

#[cfg(test)]
mod tests {
use std::str::FromStr;
Expand Down Expand Up @@ -410,7 +421,7 @@ mod tests {

assert_eq!(
U256::from(next_base_fee),
calculate_next_base_fee(&parent_header)
calculate_next_base_fee_per_gas(&parent_header)
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/edr_eth/src/rlp.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub use alloy_rlp::{Decodable, Encodable, Error};
pub use alloy_rlp::{encode, Decodable, Encodable, Error};
63 changes: 60 additions & 3 deletions crates/edr_eth/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,69 @@
//! transaction related data
mod fake_signature;
/// Types for transaction gossip (aka pooled transactions)
pub mod pooled;
mod request;
mod signed;
mod r#type;

pub use revm_primitives::alloy_primitives::TxKind;
use revm_primitives::B256;

pub use self::{request::*, signed::*};
pub use self::{r#type::TransactionType, request::*, signed::*};
use crate::{access_list::AccessListItem, Address, Bytes, U256};

pub trait Transaction {
/// The effective gas price of the transaction, calculated using the
/// provided block base fee.
fn effective_gas_price(&self, block_base_fee: U256) -> U256;

/// The maximum amount of gas the transaction can use.
fn gas_limit(&self) -> u64;

/// The gas price the sender is willing to pay.
fn gas_price(&self) -> U256;

/// The maximum fee per gas the sender is willing to pay. Only applicable
/// for post-EIP-1559 transactions.
fn max_fee_per_gas(&self) -> Option<U256>;

/// The maximum fee per blob gas the sender is willing to pay. Only
/// applicable for EIP-4844 transactions.
fn max_fee_per_blob_gas(&self) -> Option<U256>;

/// The maximum priority fee per gas the sender is willing to pay. Only
/// applicable for post-EIP-1559 transactions.
fn max_priority_fee_per_gas(&self) -> Option<U256>;

/// The transaction's nonce.
fn nonce(&self) -> u64;

/// The address that receives the call, if any.
fn to(&self) -> Option<Address>;

/// The total amount of blob gas used by the transaction. Only applicable
/// for EIP-4844 transactions.
fn total_blob_gas(&self) -> Option<u64>;

/// The hash of the transaction.
fn transaction_hash(&self) -> &B256;

/// The type of the transaction.
fn transaction_type(&self) -> TransactionType;

/// The value of the transaction.
fn value(&self) -> U256;
}

pub fn max_cost(transaction: &impl Transaction) -> U256 {
U256::from(transaction.gas_limit()).saturating_mul(transaction.gas_price())
}

pub fn upfront_cost(transaction: &impl Transaction) -> U256 {
max_cost(transaction).saturating_add(transaction.value())
}

/// Represents _all_ transaction requests received from RPC
#[derive(Clone, Debug, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand Down Expand Up @@ -50,8 +104,11 @@ pub struct EthTransactionRequest {
#[cfg_attr(feature = "serde", serde(default))]
pub access_list: Option<Vec<AccessListItem>>,
/// EIP-2718 type
#[cfg_attr(feature = "serde", serde(default, rename = "type"))]
pub transaction_type: Option<U256>,
#[cfg_attr(
feature = "serde",
serde(default, rename = "type", with = "crate::serde::optional_u64")
)]
pub transaction_type: Option<u64>,
/// Blobs (EIP-4844)
pub blobs: Option<Vec<Bytes>>,
/// Blob versioned hashes (EIP-4844)
Expand Down
Loading

0 comments on commit a4d5b62

Please sign in to comment.