Skip to content

Commit d27e522

Browse files
authored
feat(anvil): add support for anvil_getBlobSidecarsByBlockId (#11828)
* feat(beacon): add support for `anvil_getBlobSidecarsByBlockId` - Added primitive Engine REST API * refactor: Exclude Beacon Rest API from PR scope * refactor: return `BlobTransactionSidecar` instead of `BeaconBlobBundle` * refactor: return a single `BlobTransactionSidecar` instead of a vector
1 parent 90830a3 commit d27e522

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

crates/anvil/core/src/eth/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ pub enum EthRequest {
186186
#[serde(rename = "anvil_getBlobsByTransactionHash", with = "sequence")]
187187
GetBlobByTransactionHash(TxHash),
188188

189+
/// Returns the blobs for a given transaction hash.
190+
#[serde(rename = "anvil_getBlobSidecarsByBlockId", with = "sequence")]
191+
GetBlobSidecarsByBlockId(BlockId),
192+
189193
#[serde(rename = "eth_getTransactionByBlockHashAndIndex")]
190194
EthGetTransactionByBlockHashAndIndex(TxHash, Index),
191195

crates/anvil/src/eth/api.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use alloy_consensus::{
3636
use alloy_dyn_abi::TypedData;
3737
use alloy_eips::{
3838
eip2718::Encodable2718,
39+
eip4844::BlobTransactionSidecar,
3940
eip7910::{EthConfig, EthForkConfig},
4041
};
4142
use alloy_evm::overrides::{OverrideBlockHashes, apply_state_overrides};
@@ -284,6 +285,9 @@ impl EthApi {
284285
EthRequest::GetBlobByTransactionHash(hash) => {
285286
self.anvil_get_blob_by_tx_hash(hash).to_rpc_result()
286287
}
288+
EthRequest::GetBlobSidecarsByBlockId(block_id) => {
289+
self.anvil_get_blob_sidecars_by_block_id(block_id).to_rpc_result()
290+
}
287291
EthRequest::EthGetRawTransactionByBlockHashAndIndex(hash, index) => {
288292
self.raw_transaction_by_block_hash_and_index(hash, index).await.to_rpc_result()
289293
}
@@ -1352,6 +1356,15 @@ impl EthApi {
13521356
Ok(self.backend.get_blob_by_tx_hash(hash)?)
13531357
}
13541358

1359+
/// Handler for RPC call: `anvil_getBlobSidecarsByBlockId`
1360+
pub fn anvil_get_blob_sidecars_by_block_id(
1361+
&self,
1362+
block_id: BlockId,
1363+
) -> Result<Option<BlobTransactionSidecar>> {
1364+
node_info!("anvil_getBlobSidecarsByBlockId");
1365+
Ok(self.backend.get_blob_sidecars_by_block_id(block_id)?)
1366+
}
1367+
13551368
/// Get transaction by its hash.
13561369
///
13571370
/// This will check the storage for a matching transaction, if no transaction exists in storage

crates/anvil/src/eth/backend/mem/mod.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ use alloy_consensus::{
4242
};
4343
use alloy_eip5792::{Capabilities, DelegationCapability};
4444
use alloy_eips::{
45-
eip1559::BaseFeeParams, eip4844::kzg_to_versioned_hash, eip7840::BlobParams,
45+
eip1559::BaseFeeParams,
46+
eip4844::{BlobTransactionSidecar, kzg_to_versioned_hash},
47+
eip7840::BlobParams,
4648
eip7910::SystemContract,
4749
};
4850
use alloy_evm::{
@@ -3281,6 +3283,29 @@ impl Backend {
32813283
Ok(None)
32823284
}
32833285

3286+
pub fn get_blob_sidecars_by_block_id(
3287+
&self,
3288+
block_id: BlockId,
3289+
) -> Result<Option<BlobTransactionSidecar>> {
3290+
if let Some(full_block) = self.get_full_block(block_id) {
3291+
let sidecar = full_block
3292+
.into_transactions_iter()
3293+
.map(TypedTransaction::try_from)
3294+
.filter_map(|typed_tx_result| {
3295+
typed_tx_result.ok()?.sidecar().map(|sidecar| sidecar.sidecar().clone())
3296+
})
3297+
.fold(BlobTransactionSidecar::default(), |mut acc, sidecar| {
3298+
acc.blobs.extend(sidecar.blobs);
3299+
acc.commitments.extend(sidecar.commitments);
3300+
acc.proofs.extend(sidecar.proofs);
3301+
acc
3302+
});
3303+
Ok(Some(sidecar))
3304+
} else {
3305+
Ok(None)
3306+
}
3307+
}
3308+
32843309
pub fn get_blob_by_versioned_hash(&self, hash: B256) -> Result<Option<Blob>> {
32853310
let storage = self.blockchain.storage.read();
32863311
for block in storage.blocks.values() {

0 commit comments

Comments
 (0)