Skip to content

Commit

Permalink
feat(anvil): add support for EIP4844 types (#7202)
Browse files Browse the repository at this point in the history
* feat(anvil-core): EIP4844 variant support

* chore: proper support when converting txs

* feat: add more type support

* chore: lock

* feat: missing type conversions, decoding test

* use correct eip check

* force no blob hashes for eip1559

* feat: support sidecar with 4844 types

* fmt

* feat: use main branch for consensus, update

* chore: rename

* lockfile

* fmt

* fmt

---------

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
  • Loading branch information
Evalir and mattsse authored Mar 12, 2024
1 parent edb3a4b commit eef87de
Show file tree
Hide file tree
Showing 10 changed files with 274 additions and 9 deletions.
4 changes: 4 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 crates/anvil/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ trie-db = "0.23"
hash-db = "0.15"
memory-db = "0.29"
alloy-primitives = { workspace = true, features = ["serde"] }
alloy-consensus.workspace = true
alloy-consensus = { workspace = true, features = ["k256", "kzg"] }
alloy-network.workspace = true
alloy-rlp.workspace = true
alloy-signer = { workspace = true, features = ["eip712", "mnemonic"] }
Expand Down
3 changes: 2 additions & 1 deletion crates/anvil/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ alloy-rpc-trace-types.workspace = true
alloy-rlp.workspace = true
alloy-eips.workspace = true
alloy-network = { workspace = true, features = ["k256"] }
alloy-consensus.workspace = true
alloy-consensus = { workspace = true, features = ["k256", "kzg"]}
alloy-dyn-abi = { workspace = true, features = ["std", "eip712"] }

serde = { workspace = true, optional = true }
serde_json.workspace = true
bytes = "1.4"
c-kzg = { version = "0.4.2", features = ["serde"] }

# trie
hash-db = { version = "0.15", default-features = false }
Expand Down
224 changes: 217 additions & 7 deletions crates/anvil/core/src/eth/transaction/mod.rs

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions crates/anvil/src/eth/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2585,6 +2585,7 @@ impl EthApi {
match &tx {
TypedTransaction::EIP2930(_) => self.backend.ensure_eip2930_active(),
TypedTransaction::EIP1559(_) => self.backend.ensure_eip1559_active(),
TypedTransaction::EIP4844(_) => self.backend.ensure_eip4844_active(),
TypedTransaction::Deposit(_) => self.backend.ensure_op_deposits_active(),
TypedTransaction::Legacy(_) => Ok(()),
}
Expand Down Expand Up @@ -2674,6 +2675,10 @@ fn determine_base_gas_by_kind(request: &TransactionRequest) -> U256 {
TxKind::Call(_) => MIN_TRANSACTION_GAS,
TxKind::Create => MIN_CREATE_GAS,
},
TypedTransactionRequest::EIP4844(req) => match req.tx().to {
TxKind::Call(_) => MIN_TRANSACTION_GAS,
TxKind::Create => MIN_CREATE_GAS,
},
TypedTransactionRequest::Deposit(req) => match req.kind {
TxKind::Call(_) => MIN_TRANSACTION_GAS,
TxKind::Create => MIN_CREATE_GAS,
Expand Down
8 changes: 8 additions & 0 deletions crates/anvil/src/eth/backend/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ impl ExecutedTransaction {
},
bloom,
}),
TypedTransaction::EIP4844(_) => TypedReceipt::EIP4844(ReceiptWithBloom {
receipt: Receipt {
success: status_code == 1,
cumulative_gas_used: used_gas.to::<u64>(),
logs,
},
bloom,
}),
TypedTransaction::Deposit(_) => TypedReceipt::Deposit(ReceiptWithBloom {
receipt: Receipt {
success: status_code == 1,
Expand Down
18 changes: 18 additions & 0 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,11 @@ impl Backend {
(self.spec_id() as u8) >= (SpecId::BERLIN as u8)
}

/// Returns true for post Cancun
pub fn is_eip4844(&self) -> bool {
(self.spec_id() as u8) >= (SpecId::CANCUN as u8)
}

/// Returns true if op-stack deposits are active
pub fn is_optimism(&self) -> bool {
self.env.read().handler_cfg.is_optimism
Expand All @@ -599,6 +604,13 @@ impl Backend {
Err(BlockchainError::EIP2930TransactionUnsupportedAtHardfork)
}

pub fn ensure_eip4844_active(&self) -> Result<(), BlockchainError> {
if self.is_eip4844() {
return Ok(());
}
Err(BlockchainError::EIP4844TransactionUnsupportedAtHardfork)
}

/// Returns an error if op-stack deposits are not active
pub fn ensure_op_deposits_active(&self) -> Result<(), BlockchainError> {
if self.is_optimism() {
Expand Down Expand Up @@ -1972,6 +1984,12 @@ impl Backend {
.map_or(self.base_fee().to::<u128>(), |b| b as u128)
.checked_add(t.max_priority_fee_per_gas)
.unwrap_or(u128::MAX),
TypedTransaction::EIP4844(t) => block
.header
.base_fee_per_gas
.map_or(self.base_fee().to::<u128>(), |b| b as u128)
.checked_add(t.tx().tx().max_priority_fee_per_gas)
.unwrap_or(u128::MAX),
TypedTransaction::Deposit(_) => 0_u128,
};

Expand Down
5 changes: 5 additions & 0 deletions crates/anvil/src/eth/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ pub enum BlockchainError {
EIP1559TransactionUnsupportedAtHardfork,
#[error("Access list received but is not supported by the current hardfork.\n\nYou can use it by running anvil with '--hardfork berlin' or later.")]
EIP2930TransactionUnsupportedAtHardfork,
#[error("EIP-4844 fields received but is not supported by the current hardfork.\n\nYou can use it by running anvil with '--hardfork cancun' or later.")]
EIP4844TransactionUnsupportedAtHardfork,
#[error("op-stack deposit tx received but is not supported.\n\nYou can use it by running anvil with '--optimism'.")]
DepositTransactionUnsupported,
#[error("Excess blob gas not set.")]
Expand Down Expand Up @@ -408,6 +410,9 @@ impl<T: Serialize> ToRpcResponseResult for Result<T> {
err @ BlockchainError::EIP2930TransactionUnsupportedAtHardfork => {
RpcError::invalid_params(err.to_string())
}
err @ BlockchainError::EIP4844TransactionUnsupportedAtHardfork => {
RpcError::invalid_params(err.to_string())
}
err @ BlockchainError::DepositTransactionUnsupported => {
RpcError::invalid_params(err.to_string())
}
Expand Down
9 changes: 9 additions & 0 deletions crates/anvil/src/eth/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,15 @@ impl FeeHistoryService {
.min(U256::from(t.max_fee_per_gas).saturating_sub(base_fee))
.to::<u64>()
}
// TODO: This probably needs to be extended to extract 4844 info.
Some(TypedTransaction::EIP4844(t)) => {
U256::from(t.tx().tx().max_priority_fee_per_gas)
.min(
U256::from(t.tx().tx().max_fee_per_gas)
.saturating_sub(base_fee),
)
.to::<u64>()
}
Some(TypedTransaction::Deposit(_)) => 0,
None => 0,
};
Expand Down
5 changes: 5 additions & 0 deletions crates/anvil/src/eth/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ impl Signer for DevSigner {
TypedTransactionRequest::Legacy(mut tx) => Ok(signer.sign_transaction_sync(&mut tx)?),
TypedTransactionRequest::EIP2930(mut tx) => Ok(signer.sign_transaction_sync(&mut tx)?),
TypedTransactionRequest::EIP1559(mut tx) => Ok(signer.sign_transaction_sync(&mut tx)?),
TypedTransactionRequest::EIP4844(mut tx) => Ok(signer.sign_transaction_sync(&mut tx)?),
TypedTransactionRequest::Deposit(mut tx) => Ok(signer.sign_transaction_sync(&mut tx)?),
}
}
Expand Down Expand Up @@ -134,6 +135,10 @@ pub fn build_typed_transaction(
let sighash = tx.signature_hash();
TypedTransaction::EIP1559(Signed::new_unchecked(tx, signature, sighash))
}
TypedTransactionRequest::EIP4844(tx) => {
let sighash = tx.signature_hash();
TypedTransaction::EIP4844(Signed::new_unchecked(tx, signature, sighash))
}
TypedTransactionRequest::Deposit(tx) => {
let DepositTransactionRequest {
from,
Expand Down

0 comments on commit eef87de

Please sign in to comment.