Skip to content

Commit 7344266

Browse files
committed
graph, chain/ethereum: Use AnyRpcBlock instead of Ethereum Specific Block
1 parent ae8295e commit 7344266

File tree

11 files changed

+118
-80
lines changed

11 files changed

+118
-80
lines changed

chain/ethereum/src/codec.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,8 @@ impl<'a> TryInto<Transaction> for TransactionTraceAt<'a> {
298298
.map(|hash| B256::from_slice(hash))
299299
.collect();
300300

301-
let max_fee_per_blob_gas_u128 = self
302-
.trace
303-
.blob_gas_fee_cap
304-
.as_ref()
305-
.map_or(0u128, |x| {
301+
let max_fee_per_blob_gas_u128 =
302+
self.trace.blob_gas_fee_cap.as_ref().map_or(0u128, |x| {
306303
let val: U256 = x.into();
307304
val.to::<u128>()
308305
});
@@ -422,20 +419,28 @@ impl TryInto<AlloyBlock> for &Block {
422419
withdrawals_root: if header.withdrawals_root.is_empty() {
423420
None
424421
} else {
425-
Some(header.withdrawals_root.try_decode_proto("withdrawals root")?)
422+
Some(
423+
header
424+
.withdrawals_root
425+
.try_decode_proto("withdrawals root")?,
426+
)
426427
},
427428
blob_gas_used: header.blob_gas_used,
428429
excess_blob_gas: header.excess_blob_gas,
429430
parent_beacon_block_root: if header.parent_beacon_root.is_empty() {
430431
None
431432
} else {
432-
Some(header.parent_beacon_root.try_decode_proto("parent beacon root")?)
433+
Some(
434+
header
435+
.parent_beacon_root
436+
.try_decode_proto("parent beacon root")?,
437+
)
433438
},
434439
requests_hash: if header.requests_hash.is_empty() {
435440
None
436441
} else {
437442
Some(header.requests_hash.try_decode_proto("requests hash")?)
438-
}
443+
},
439444
};
440445

441446
let rpc_header = alloy::rpc::types::Header {
@@ -489,7 +494,7 @@ impl TryInto<EthereumBlockWithCalls> for &Block {
489494
#[allow(unreachable_code)]
490495
let block = EthereumBlockWithCalls {
491496
ethereum_block: EthereumBlock {
492-
block: Arc::new(LightEthereumBlock::new(alloy_block)),
497+
block: Arc::new(LightEthereumBlock::new(alloy_block.into())),
493498
transaction_receipts,
494499
},
495500
// Comment (437a9f17-67cc-478f-80a3-804fe554b227): This Some() will avoid calls in the triggers_in_block

chain/ethereum/src/data_source.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use graph::abi;
44
use graph::abi::EventExt;
55
use graph::abi::FunctionExt;
66
use graph::blockchain::{BlockPtr, TriggerWithHandler};
7+
use graph::components::ethereum::AnyTransaction;
78
use graph::components::link_resolver::LinkResolverContext;
89
use graph::components::metrics::subgraph::SubgraphInstanceMetrics;
910
use graph::components::store::{EthereumCallCache, StoredDynamicDataSource};
@@ -21,8 +22,9 @@ use graph::futures03::stream::FuturesOrdered;
2122
use graph::futures03::TryStreamExt;
2223
use graph::prelude::alloy::{
2324
consensus::{TxEnvelope, TxLegacy},
24-
primitives::{Address, B256},
25-
rpc::types::{Log, Transaction},
25+
network::TransactionResponse,
26+
primitives::{Address, B256, U256},
27+
rpc::types::Log,
2628
};
2729
use graph::prelude::{alloy, Link, SubgraphManifestValidationError};
2830
use graph::slog::{debug, error, o, trace};
@@ -440,11 +442,12 @@ fn create_dummy_transaction(
440442
block_hash: B256,
441443
transaction_index: Option<u64>,
442444
transaction_hash: Option<B256>,
443-
) -> Result<Transaction<TxEnvelope>, anyhow::Error> {
445+
) -> Result<AnyTransaction, anyhow::Error> {
446+
use alloy::serde::WithOtherFields;
447+
use graph::components::ethereum::AnyTxEnvelope;
444448
use graph::prelude::alloy::{
445-
consensus::transaction::Recovered,
446-
consensus::Signed,
447-
primitives::{Signature, U256},
449+
consensus::transaction::Recovered, consensus::Signed, primitives::Signature,
450+
rpc::types::Transaction,
448451
};
449452

450453
let tx = TxLegacy::default();
@@ -454,17 +457,22 @@ fn create_dummy_transaction(
454457

455458
let tx_hash = transaction_hash.ok_or(anyhow!("Log has no transaction hash"))?;
456459
let signed_tx = Signed::new_unchecked(tx, signature, tx_hash);
457-
let envelope = TxEnvelope::Legacy(signed_tx);
460+
let eth_envelope = TxEnvelope::Legacy(signed_tx);
458461

459-
let recovered = Recovered::new_unchecked(envelope, Address::ZERO);
462+
// Wrap in AnyTxEnvelope
463+
let any_envelope = AnyTxEnvelope::Ethereum(eth_envelope);
460464

461-
Ok(Transaction {
465+
let recovered = Recovered::new_unchecked(any_envelope, Address::ZERO);
466+
467+
let inner_tx = Transaction {
462468
inner: recovered,
463469
block_hash: Some(block_hash),
464470
block_number: Some(block_number),
465-
transaction_index: transaction_index,
471+
transaction_index,
466472
effective_gas_price: None,
467-
})
473+
};
474+
475+
Ok(AnyTransaction::new(WithOtherFields::new(inner_tx)))
468476
}
469477

470478
impl DataSource {

chain/ethereum/src/ethereum_adapter.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ use graph::futures03::{
2121
self, compat::Future01CompatExt, FutureExt, StreamExt, TryFutureExt, TryStreamExt,
2222
};
2323
use graph::prelude::alloy::primitives::Address;
24-
use graph::prelude::alloy::rpc::types::Transaction;
2524
use graph::prelude::{
2625
alloy::{
2726
self,
27+
network::TransactionResponse,
2828
primitives::B256,
2929
providers::{
3030
ext::TraceApi,
@@ -732,7 +732,7 @@ impl EthereumAdapter {
732732
.map_err(Error::from)
733733
.and_then(|block| {
734734
block
735-
.map(|b| Arc::new(LightEthereumBlock::new(b)))
735+
.map(|b| Arc::new(LightEthereumBlock::new(b.into())))
736736
.ok_or_else(|| {
737737
anyhow::anyhow!(
738738
"Ethereum node did not find block {:?}",
@@ -1334,7 +1334,7 @@ impl EthereumAdapterTrait for EthereumAdapter {
13341334
if block.transactions.is_empty() {
13351335
trace!(logger, "Block {} contains no transactions", block_hash);
13361336
return Ok(EthereumBlock {
1337-
block: Arc::new(LightEthereumBlock::new(block)),
1337+
block: Arc::new(LightEthereumBlock::new(block.into())),
13381338
transaction_receipts: Vec::new(),
13391339
});
13401340
}
@@ -1353,7 +1353,7 @@ impl EthereumAdapterTrait for EthereumAdapter {
13531353
fetch_receipts_with_retry(alloy, hashes, block_hash, logger, supports_block_receipts)
13541354
.await
13551355
.map(|transaction_receipts| EthereumBlock {
1356-
block: Arc::new(LightEthereumBlock::new(block)),
1356+
block: Arc::new(LightEthereumBlock::new(block.into())),
13571357
transaction_receipts: transaction_receipts
13581358
.into_iter()
13591359
.map(|receipt| receipt)
@@ -2043,13 +2043,13 @@ async fn filter_call_triggers_from_unsuccessful_transactions(
20432043
}
20442044

20452045
// And obtain all Transaction values for the calls in this block.
2046-
let transactions: Vec<&Transaction> = {
2046+
let transactions: Vec<&AnyTransaction> = {
20472047
match &block.block {
20482048
BlockFinality::Final(ref block) => block
20492049
.transactions()
20502050
.ok_or_else(|| anyhow!("Block transactions not available"))?
20512051
.iter()
2052-
.filter(|transaction| transaction_hashes.contains(transaction.inner.tx_hash()))
2052+
.filter(|transaction| transaction_hashes.contains(&transaction.tx_hash()))
20532053
.collect(),
20542054
BlockFinality::NonFinal(_block_with_calls) => {
20552055
unreachable!(
@@ -2079,21 +2079,21 @@ async fn filter_call_triggers_from_unsuccessful_transactions(
20792079
.collect::<BTreeMap<B256, LightTransactionReceipt>>();
20802080

20812081
// Do we have a receipt for each transaction under analysis?
2082-
let mut receipts_and_transactions: Vec<(&Transaction, LightTransactionReceipt)> = Vec::new();
2083-
let mut transactions_without_receipt: Vec<&Transaction> = Vec::new();
2082+
let mut receipts_and_transactions: Vec<(&AnyTransaction, LightTransactionReceipt)> = Vec::new();
2083+
let mut transactions_without_receipt: Vec<&AnyTransaction> = Vec::new();
20842084
for transaction in transactions.iter() {
2085-
if let Some(receipt) = receipts.remove(transaction.inner.tx_hash()) {
2086-
receipts_and_transactions.push((transaction, receipt));
2085+
if let Some(receipt) = receipts.remove(&transaction.tx_hash()) {
2086+
receipts_and_transactions.push((*transaction, receipt));
20872087
} else {
2088-
transactions_without_receipt.push(transaction);
2088+
transactions_without_receipt.push(*transaction);
20892089
}
20902090
}
20912091

20922092
// When some receipts are missing, we then try to fetch them from our client.
20932093
let futures = transactions_without_receipt
20942094
.iter()
20952095
.map(|transaction| async move {
2096-
fetch_receipt_from_ethereum_client(eth, *transaction.inner.tx_hash())
2096+
fetch_receipt_from_ethereum_client(eth, transaction.tx_hash())
20972097
.await
20982098
.map(|receipt| (transaction, receipt))
20992099
});
@@ -2108,9 +2108,9 @@ async fn filter_call_triggers_from_unsuccessful_transactions(
21082108
// additional Ethereum API calls for future scans on this block.
21092109

21102110
// With all transactions and receipts in hand, we can evaluate the success of each transaction
2111-
let mut transaction_success: BTreeMap<&B256, bool> = BTreeMap::new();
2111+
let mut transaction_success: BTreeMap<B256, bool> = BTreeMap::new();
21122112
for (transaction, receipt) in receipts_and_transactions.into_iter() {
2113-
transaction_success.insert(&transaction.inner.tx_hash(), receipt.status);
2113+
transaction_success.insert(transaction.tx_hash(), receipt.status);
21142114
}
21152115

21162116
// Confidence check: Did we inspect the status of all transactions?
@@ -2623,7 +2623,7 @@ mod tests {
26232623

26242624
let block = EthereumBlockWithCalls {
26252625
ethereum_block: EthereumBlock {
2626-
block: Arc::new(LightEthereumBlock::new(block)),
2626+
block: Arc::new(LightEthereumBlock::new(block.into())),
26272627
..Default::default()
26282628
},
26292629
calls: Some(vec![EthereumCall {
@@ -2765,7 +2765,7 @@ mod tests {
27652765
#[allow(unreachable_code)]
27662766
let block = EthereumBlockWithCalls {
27672767
ethereum_block: EthereumBlock {
2768-
block: Arc::new(LightEthereumBlock::new(block)),
2768+
block: Arc::new(LightEthereumBlock::new(block.into())),
27692769
..Default::default()
27702770
},
27712771
calls: Some(vec![EthereumCall {
@@ -2796,7 +2796,7 @@ mod tests {
27962796
#[allow(unreachable_code)]
27972797
let block = EthereumBlockWithCalls {
27982798
ethereum_block: EthereumBlock {
2799-
block: Arc::new(LightEthereumBlock::new(block)),
2799+
block: Arc::new(LightEthereumBlock::new(block.into())),
28002800
..Default::default()
28012801
},
28022802
calls: Some(vec![EthereumCall {

chain/ethereum/src/runtime/abi.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -510,9 +510,9 @@ impl<'a> ToAscObj<AscEthereumTransaction_0_0_1> for EthereumTransactionData<'a>
510510
gas: &GasCounter,
511511
) -> Result<AscEthereumTransaction_0_0_1, HostExportError> {
512512
Ok(AscEthereumTransaction_0_0_1 {
513-
hash: asc_new(heap, self.hash(), gas)?,
513+
hash: asc_new(heap, &self.hash(), gas)?,
514514
index: asc_new(heap, &BigInt::from(self.index()), gas)?,
515-
from: asc_new(heap, self.from(), gas)?,
515+
from: asc_new(heap, &self.from(), gas)?,
516516
to: self
517517
.to()
518518
.map(|to| asc_new(heap, &to, gas))
@@ -531,9 +531,9 @@ impl<'a> ToAscObj<AscEthereumTransaction_0_0_2> for EthereumTransactionData<'a>
531531
gas: &GasCounter,
532532
) -> Result<AscEthereumTransaction_0_0_2, HostExportError> {
533533
Ok(AscEthereumTransaction_0_0_2 {
534-
hash: asc_new(heap, self.hash(), gas)?,
534+
hash: asc_new(heap, &self.hash(), gas)?,
535535
index: asc_new(heap, &BigInt::from(self.index()), gas)?,
536-
from: asc_new(heap, self.from(), gas)?,
536+
from: asc_new(heap, &self.from(), gas)?,
537537
to: self
538538
.to()
539539
.map(|to| asc_new(heap, &to, gas))
@@ -553,9 +553,9 @@ impl<'a> ToAscObj<AscEthereumTransaction_0_0_6> for EthereumTransactionData<'a>
553553
gas: &GasCounter,
554554
) -> Result<AscEthereumTransaction_0_0_6, HostExportError> {
555555
Ok(AscEthereumTransaction_0_0_6 {
556-
hash: asc_new(heap, self.hash(), gas)?,
556+
hash: asc_new(heap, &self.hash(), gas)?,
557557
index: asc_new(heap, &BigInt::from(self.index()), gas)?,
558-
from: asc_new(heap, self.from(), gas)?,
558+
from: asc_new(heap, &self.from(), gas)?,
559559
to: self
560560
.to()
561561
.map(|to| asc_new(heap, &to, gas))

chain/ethereum/src/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ fn test_trigger_ordering() {
123123

124124
let b = Block::default();
125125

126-
let b = LightEthereumBlock::new(b);
126+
let b = LightEthereumBlock::new(b.into());
127127

128128
// Test that `BlockWithTriggers` sorts the triggers.
129129
let block_with_triggers = BlockWithTriggers::<crate::Chain>::new(
@@ -203,7 +203,7 @@ fn test_trigger_dedup() {
203203
let b = Block::default();
204204

205205
#[allow(unreachable_code)]
206-
let b = LightEthereumBlock::new(b);
206+
let b = LightEthereumBlock::new(b.into());
207207

208208
// Test that `BlockWithTriggers` sorts the triggers.
209209
let block_with_triggers = BlockWithTriggers::<crate::Chain>::new(

0 commit comments

Comments
 (0)