Skip to content

Commit 352416c

Browse files
committed
graph, chain/ethereum: Use AnyNetwork with concrete AlloyProvider type
1 parent 7344266 commit 352416c

File tree

9 files changed

+214
-76
lines changed

9 files changed

+214
-76
lines changed

chain/ethereum/src/adapter.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use anyhow::Error;
22
use graph::abi;
33
use graph::blockchain::ChainIdentifier;
4+
use graph::components::ethereum::AnyBlock;
45
use graph::components::subgraph::MappingError;
56
use graph::data::store::ethereum::call;
67
use graph::data_source::common::ContractCall;
78
use graph::firehose::CallToFilter;
89
use graph::firehose::CombinedFilter;
910
use graph::firehose::LogFilter;
1011
use graph::prelude::alloy::primitives::{Address, B256};
11-
use graph::prelude::alloy::rpc::types::Block as AlloyBlock;
1212
use graph::prelude::alloy::rpc::types::Log;
1313
use graph::prelude::alloy::transports::{RpcError, TransportErrorKind};
1414
use itertools::Itertools;
@@ -1118,19 +1118,19 @@ pub trait EthereumAdapter: Send + Sync + 'static {
11181118
&self,
11191119
logger: &Logger,
11201120
block_hash: B256,
1121-
) -> Result<Option<AlloyBlock>, Error>;
1121+
) -> Result<Option<AnyBlock>, Error>;
11221122

11231123
async fn block_by_number(
11241124
&self,
11251125
logger: &Logger,
11261126
block_number: BlockNumber,
1127-
) -> Result<Option<AlloyBlock>, Error>;
1127+
) -> Result<Option<AnyBlock>, Error>;
11281128

11291129
/// Load full information for the specified `block` (in particular, transaction receipts).
11301130
async fn load_full_block(
11311131
&self,
11321132
logger: &Logger,
1133-
block: AlloyBlock,
1133+
block: AnyBlock,
11341134
) -> Result<EthereumBlock, bc::IngestorError>;
11351135

11361136
/// Finds the hash and number of the lowest non-null block with height greater than or equal to

chain/ethereum/src/codec.rs

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ use graph::{
77
blockchain::{
88
self, Block as BlockchainBlock, BlockPtr, BlockTime, ChainStoreBlock, ChainStoreData,
99
},
10+
components::ethereum::{AnyBlock, AnyHeader, AnyRpcHeader, AnyRpcTransaction, AnyTxEnvelope},
1011
prelude::{
1112
alloy::{
1213
self,
13-
consensus::{ReceiptEnvelope, ReceiptWithBloom, TxEnvelope, TxType},
14+
consensus::{ReceiptWithBloom, TxEnvelope, TxType},
15+
network::AnyReceiptEnvelope,
1416
primitives::{aliases::B2048, Address, Bloom, Bytes, LogData, B256, U256},
15-
rpc::types::{
16-
AccessList, AccessListItem, Block as AlloyBlock, Transaction,
17-
TransactionReceipt as AlloyTransactionReceipt,
18-
},
17+
rpc::types::{self as alloy_rpc_types, AccessList, AccessListItem, Transaction},
18+
serde::WithOtherFields,
1919
},
2020
BlockNumber, Error, EthereumBlock, EthereumBlockWithCalls, EthereumCall,
2121
LightEthereumBlock,
@@ -378,10 +378,10 @@ impl TryInto<BlockFinality> for &Block {
378378
}
379379
}
380380

381-
impl TryInto<AlloyBlock> for &Block {
381+
impl TryInto<AnyBlock> for &Block {
382382
type Error = Error;
383383

384-
fn try_into(self) -> Result<AlloyBlock, Self::Error> {
384+
fn try_into(self) -> Result<AnyBlock, Self::Error> {
385385
let header = self.header();
386386

387387
let block_hash = self.hash.try_decode_proto("block hash")?;
@@ -466,19 +466,31 @@ impl TryInto<AlloyBlock> for &Block {
466466
.map(|u| u.hash.try_decode_proto("uncle hash"))
467467
.collect::<Result<Vec<B256>, _>>()?;
468468

469-
Ok(AlloyBlock::new(
470-
rpc_header,
471-
alloy::rpc::types::BlockTransactions::Full(transactions),
472-
)
473-
.with_uncles(uncles))
469+
use alloy::rpc::types::Block;
470+
471+
let any_header: AnyRpcHeader = rpc_header.map(AnyHeader::from);
472+
473+
let any_transactions: Vec<AnyRpcTransaction> = transactions
474+
.into_iter()
475+
.map(|tx| AnyRpcTransaction::new(WithOtherFields::new(tx.map(AnyTxEnvelope::Ethereum))))
476+
.collect();
477+
478+
let any_block = Block {
479+
header: any_header,
480+
transactions: alloy::rpc::types::BlockTransactions::Full(any_transactions),
481+
uncles,
482+
withdrawals: None,
483+
};
484+
485+
Ok(AnyBlock::new(WithOtherFields::new(any_block)))
474486
}
475487
}
476488

477489
impl TryInto<EthereumBlockWithCalls> for &Block {
478490
type Error = Error;
479491

480492
fn try_into(self) -> Result<EthereumBlockWithCalls, Self::Error> {
481-
let alloy_block: AlloyBlock = self.try_into()?;
493+
let alloy_block: AnyBlock = self.try_into()?;
482494

483495
let transaction_receipts = self
484496
.transaction_traces
@@ -494,7 +506,7 @@ impl TryInto<EthereumBlockWithCalls> for &Block {
494506
#[allow(unreachable_code)]
495507
let block = EthereumBlockWithCalls {
496508
ethereum_block: EthereumBlock {
497-
block: Arc::new(LightEthereumBlock::new(alloy_block.into())),
509+
block: Arc::new(LightEthereumBlock::new(alloy_block)),
498510
transaction_receipts,
499511
},
500512
// Comment (437a9f17-67cc-478f-80a3-804fe554b227): This Some() will avoid calls in the triggers_in_block
@@ -521,7 +533,7 @@ impl TryInto<EthereumBlockWithCalls> for &Block {
521533
fn transaction_trace_to_alloy_txn_reciept(
522534
t: &TransactionTrace,
523535
block: &Block,
524-
) -> Result<Option<AlloyTransactionReceipt<ReceiptEnvelope<alloy::rpc::types::Log>>>, Error> {
536+
) -> Result<Option<alloy::network::AnyTransactionReceipt>, Error> {
525537
use alloy::consensus::{Eip658Value, Receipt};
526538
let r = t.receipt.as_ref();
527539

@@ -600,15 +612,20 @@ fn transaction_trace_to_alloy_txn_reciept(
600612
)
601613
})?;
602614

603-
let envelope = match tx_type {
604-
TxType::Legacy => ReceiptEnvelope::Legacy(receipt_with_bloom),
605-
TxType::Eip2930 => ReceiptEnvelope::Eip2930(receipt_with_bloom),
606-
TxType::Eip1559 => ReceiptEnvelope::Eip1559(receipt_with_bloom),
607-
TxType::Eip4844 => ReceiptEnvelope::Eip4844(receipt_with_bloom),
608-
TxType::Eip7702 => ReceiptEnvelope::Eip7702(receipt_with_bloom),
615+
let ty = match tx_type {
616+
TxType::Legacy => 0,
617+
TxType::Eip2930 => 1,
618+
TxType::Eip1559 => 2,
619+
TxType::Eip4844 => 3,
620+
TxType::Eip7702 => 4,
621+
};
622+
623+
let any_envelope = AnyReceiptEnvelope {
624+
inner: receipt_with_bloom,
625+
r#type: ty,
609626
};
610627

611-
Ok(Some(AlloyTransactionReceipt {
628+
let receipt = alloy_rpc_types::TransactionReceipt {
612629
transaction_hash: t.hash.try_decode_proto("transaction hash")?,
613630
transaction_index: Some(t.index as u64),
614631
block_hash: Some(block.hash.try_decode_proto("transaction block hash")?),
@@ -626,8 +643,10 @@ fn transaction_trace_to_alloy_txn_reciept(
626643
let val: U256 = x.into();
627644
val.to::<u128>()
628645
}),
629-
inner: envelope,
630-
}))
646+
inner: any_envelope,
647+
};
648+
649+
Ok(Some(WithOtherFields::new(receipt)))
631650
}
632651

633652
impl BlockHeader {

0 commit comments

Comments
 (0)