Skip to content

Commit

Permalink
fix tx validation in suavex_buildEthBlock[FromBundles]
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroXbrock committed Apr 9, 2024
1 parent 1745b31 commit fd5e745
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 120 deletions.
44 changes: 34 additions & 10 deletions crates/anvil/core/src/eth/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,55 @@ impl BlockInfo {
pub fn execution_envelope(
&self,
raw_transactions: &[Bytes],
args: BuildBlockArgs,
args: SuavexBuildBlockArgs,
fees: U256,
) -> ExecutionPayloadEnvelopeV3 {
let block_hash = self.block.header.hash();
ExecutionPayloadEnvelopeV3 {
execution_payload: ExecutionPayloadV3 {
payload_inner: ExecutionPayloadV2 {
payload_inner: ExecutionPayloadV1 {
parent_hash: self.block.header.parent_hash,
fee_recipient: self.block.header.beneficiary,
parent_hash: if args.parent == B256::ZERO {
self.block.header.parent_hash
} else {
args.parent
},
fee_recipient: if args.fee_recipient == Address::ZERO {
self.block.header.beneficiary
} else {
args.fee_recipient
},
state_root: self.block.header.state_root,
receipts_root: self.block.header.receipts_root,
logs_bloom: self.block.header.logs_bloom,
prev_randao: self.block.header.mix_hash,
prev_randao: if args.random == B256::ZERO {
self.block.header.mix_hash
} else {
args.random
},
block_number: self.block.header.number,
gas_limit: self.block.header.gas_limit,
gas_limit: if args.gas_limit == 0 {
self.block.header.gas_limit
} else {
args.gas_limit
},
gas_used: self.block.header.gas_used,
timestamp: self.block.header.timestamp,
extra_data: self.block.header.extra_data.to_owned(),
timestamp: if args.timestamp == 0 {
self.block.header.timestamp
} else {
args.timestamp
},
extra_data: if args.extra == Bytes::new() {
self.block.header.extra_data.to_owned()
} else {
args.extra
},
base_fee_per_gas: self
.block
.header
.base_fee_per_gas
.map(|f| U256::from(f))
.unwrap_or("1000000000".parse::<U256>().unwrap()),
.unwrap_or(U256::from(0)),
block_hash,
transactions: raw_transactions.to_vec(),
},
Expand All @@ -70,7 +94,7 @@ impl BlockInfo {
block_value: fees,
blobs_bundle: BlobsBundleV1 { commitments: vec![], blobs: vec![], proofs: vec![] },
should_override_builder: false,
parent_beacon_block_root: None,
parent_beacon_block_root: args.parent_beacon_block_root,
}
}
}
Expand Down Expand Up @@ -175,7 +199,7 @@ impl From<Header> for PartialHeader {

#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct BuildBlockArgs {
pub struct SuavexBuildBlockArgs {
pub slot: u64,
// #[serde(deserialize_with = "base64_to_bytes")] // TODO: use serde macro so we can use Bytes instead of String
pub proposer_pubkey: String,
Expand Down
8 changes: 4 additions & 4 deletions crates/anvil/core/src/eth/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
eth::subscription::SubscriptionId,
types::{EvmMineOptions, Forking, Index},
types::{EvmMineOptions, Forking, Index, TransactionSuave},
};
use alloy_primitives::{Address, Bytes, TxHash, B256, B64, U256};
use alloy_rpc_trace_types::geth::{GethDebugTracingOptions, GethDefaultTracingOptions};
Expand All @@ -24,7 +24,7 @@ pub mod serde_helpers;

#[cfg(feature = "serde")]
use self::serde_helpers::*;
use self::{block::BuildBlockArgs, bundle::SBundle};
use self::{block::SuavexBuildBlockArgs, bundle::SBundle};

#[cfg(feature = "serde")]
use foundry_common::serde_helpers::{
Expand Down Expand Up @@ -176,10 +176,10 @@ pub enum EthRequest {
SuavexCall(Address, String),

#[cfg_attr(feature = "serde", serde(rename = "suavex_buildEthBlock"))]
SuavexBuildEthBlock(Option<BuildBlockArgs>, Vec<TransactionRequest>),
SuavexBuildEthBlock(Option<SuavexBuildBlockArgs>, Vec<TransactionSuave>),

#[cfg_attr(feature = "serde", serde(rename = "suavex_buildEthBlockFromBundles"))]
SuavexBuildEthBlockFromBundles(BuildBlockArgs, Vec<SBundle>),
SuavexBuildEthBlockFromBundles(SuavexBuildBlockArgs, Vec<SBundle>),

#[cfg_attr(feature = "serde", serde(rename = "eth_createAccessList"))]
EthCreateAccessList(
Expand Down
69 changes: 68 additions & 1 deletion crates/anvil/core/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use alloy_primitives::{TxHash, B256, U256, U64};
use alloy_primitives::{Address, Bytes, TxHash, B256, U128, U256, U64};
use alloy_rpc_types::{other::OtherFields, AccessListItem, Signature};
use revm::primitives::SpecId;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

#[cfg(feature = "serde")]
Expand Down Expand Up @@ -232,6 +234,71 @@ pub struct ForkedNetwork {
pub fork_block_hash: TxHash,
}

/// Transaction object used in RPC (should be in alloy_rpc)
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TransactionSuave {
/// Hash
pub hash: B256,
/// Nonce
pub nonce: U64,
/// Block hash
pub block_hash: Option<B256>,
/// Block number
pub block_number: Option<U256>,
/// Transaction Index
pub transaction_index: Option<U256>,
/// Sender
pub from: Option<Address>,
/// Recipient
pub to: Option<Address>,
/// Transferred value
pub value: U256,
/// Gas Price
#[serde(skip_serializing_if = "Option::is_none")]
pub gas_price: Option<U128>,
/// Gas amount
pub gas: U256,
/// Max BaseFeePerGas the user is willing to pay.
#[serde(skip_serializing_if = "Option::is_none")]
pub max_fee_per_gas: Option<U128>,
/// The miner's tip.
#[serde(skip_serializing_if = "Option::is_none")]
pub max_priority_fee_per_gas: Option<U128>,
/// Configured max fee per blob gas for eip-4844 transactions
#[serde(skip_serializing_if = "Option::is_none")]
pub max_fee_per_blob_gas: Option<U128>,
/// Data
pub input: Bytes,
/// All _flattened_ fields of the transaction signature.
///
/// Note: this is an option so special transaction types without a signature (e.g. <https://github.com/ethereum-optimism/optimism/blob/0bf643c4147b43cd6f25a759d331ef3a2a61a2a3/specs/deposits.md#the-deposited-transaction-type>) can be supported.
#[serde(flatten, skip_serializing_if = "Option::is_none")]
pub signature: Option<Signature>,
/// The chain id of the transaction, if any.
pub chain_id: Option<U64>,
/// Contains the blob hashes for eip-4844 transactions.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub blob_versioned_hashes: Vec<B256>,
/// EIP2930
///
/// Pre-pay to warm storage access.
#[serde(skip_serializing_if = "Option::is_none")]
pub access_list: Option<Vec<AccessListItem>>,
/// EIP2718
///
/// Transaction type, Some(2) for EIP-1559 transaction,
/// Some(1) for AccessList transaction, None for Legacy
#[serde(rename = "type", skip_serializing_if = "Option::is_none")]
pub transaction_type: Option<U64>,

/// Arbitrary extra fields.
///
/// This captures fields that are not native to ethereum but included in ethereum adjacent networks, for example fields the [optimism `eth_getTransactionByHash` request](https://docs.alchemy.com/alchemy/apis/optimism/eth-gettransactionbyhash) returns additional fields that this type will capture
#[serde(flatten)]
pub other: OtherFields,
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading

0 comments on commit fd5e745

Please sign in to comment.