Skip to content

Commit 85cf80d

Browse files
committed
chain/ethereum: migrate block methods to alloy part 2
1 parent f79cffb commit 85cf80d

File tree

6 files changed

+84
-27
lines changed

6 files changed

+84
-27
lines changed

chain/ethereum/src/adapter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,8 +1252,8 @@ pub trait EthereumAdapter: Send + Sync + 'static {
12521252
&self,
12531253
logger: Logger,
12541254
chain_store: Arc<dyn ChainStore>,
1255-
block_hashes: HashSet<H256>,
1256-
) -> Result<Vec<Arc<LightEthereumBlock>>, Error>;
1255+
block_hashes: HashSet<B256>,
1256+
) -> Result<Vec<Arc<AlloyBlock>>, Error>;
12571257

12581258
/// Find a block by its hash.
12591259
async fn block_by_hash(

chain/ethereum/src/chain.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,7 @@ impl TriggersAdapterTrait<Chain> for TriggersAdapter {
11081108
.load_blocks(
11091109
self.logger.cheap_clone(),
11101110
self.chain_store.cheap_clone(),
1111-
HashSet::from_iter(Some(block.hash_as_h256())),
1111+
HashSet::from_iter(Some(block.hash_as_b256())),
11121112
)
11131113
.await?;
11141114
assert_eq!(blocks.len(), 1);

chain/ethereum/src/ethereum_adapter.rs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use graph::futures03::future::try_join_all;
1919
use graph::futures03::{
2020
self, compat::Future01CompatExt, FutureExt, StreamExt, TryFutureExt, TryStreamExt,
2121
};
22+
use graph::prelude::alloy::consensus::BlockHeader;
2223
use graph::prelude::{
2324
alloy::{
2425
self,
@@ -44,12 +45,13 @@ use graph::slog::o;
4445
use graph::tokio::sync::RwLock;
4546
use graph::tokio::time::timeout;
4647
use graph::util::conversions::alloy_block_to_web3_block;
48+
use graph::util::conversions::alloy_block_to_web3_block_arc;
4749
use graph::{
4850
blockchain::{block_stream::BlockWithTriggers, BlockPtr, IngestorError},
4951
prelude::{
5052
anyhow::{self, anyhow, bail, ensure, Context},
5153
async_trait, debug, error, hex, info, retry, serde_json as json, trace, warn,
52-
web3::types::{BlockId, Transaction, H256},
54+
web3::types::{Transaction, H256},
5355
BlockNumber, ChainStore, CheapClone, DynTryFuture, Error, EthereumCallCache, Logger,
5456
TimeoutError,
5557
},
@@ -718,12 +720,12 @@ impl EthereumAdapter {
718720
fn load_blocks_rpc(
719721
&self,
720722
logger: Logger,
721-
ids: Vec<H256>,
722-
) -> impl futures03::Stream<Item = Result<Arc<LightEthereumBlock>, Error>> + Send {
723-
let web3 = self.web3.clone();
723+
ids: Vec<B256>,
724+
) -> impl futures03::Stream<Item = Result<Arc<AlloyBlock>, Error>> + Send {
725+
let alloy = self.alloy.clone();
724726

725727
futures03::stream::iter(ids.into_iter().map(move |hash| {
726-
let web3 = web3.clone();
728+
let alloy = alloy.clone();
727729
let logger = logger.clone();
728730

729731
async move {
@@ -732,10 +734,11 @@ impl EthereumAdapter {
732734
.limit(ENV_VARS.request_retries)
733735
.timeout_secs(ENV_VARS.json_rpc_timeout.as_secs())
734736
.run(move || {
735-
let web3 = web3.cheap_clone();
737+
let alloy = alloy.cheap_clone();
736738
async move {
737-
web3.eth()
738-
.block_with_txs(BlockId::Hash(hash))
739+
alloy
740+
.get_block_by_hash(hash)
741+
.full()
739742
.await
740743
.map_err(Error::from)
741744
.and_then(|block| {
@@ -1615,11 +1618,11 @@ impl EthereumAdapterTrait for EthereumAdapter {
16151618
&self,
16161619
logger: Logger,
16171620
chain_store: Arc<dyn ChainStore>,
1618-
block_hashes: HashSet<H256>,
1619-
) -> Result<Vec<Arc<LightEthereumBlock>>, Error> {
1621+
block_hashes: HashSet<B256>,
1622+
) -> Result<Vec<Arc<AlloyBlock>>, Error> {
16201623
let block_hashes: Vec<_> = block_hashes.iter().cloned().collect();
16211624
// Search for the block in the store first then use json-rpc as a backup.
1622-
let mut blocks: Vec<Arc<LightEthereumBlock>> = chain_store
1625+
let mut blocks: Vec<Arc<AlloyBlock>> = chain_store
16231626
.cheap_clone()
16241627
.blocks(block_hashes.iter().map(|&b| b.into()).collect::<Vec<_>>())
16251628
.await
@@ -1633,18 +1636,18 @@ impl EthereumAdapterTrait for EthereumAdapter {
16331636
let missing_blocks = Vec::from_iter(
16341637
block_hashes
16351638
.into_iter()
1636-
.filter(|hash| !blocks.iter().any(|b| b.hash == Some(*hash))),
1639+
.filter(|hash| !blocks.iter().any(|b| b.header.hash == *hash)),
16371640
);
16381641

16391642
// Return a stream that lazily loads batches of blocks.
16401643
debug!(logger, "Requesting {} block(s)", missing_blocks.len());
1641-
let new_blocks: Vec<Arc<LightEthereumBlock>> = self
1644+
let new_blocks: Vec<Arc<AlloyBlock>> = self
16421645
.load_blocks_rpc(logger.clone(), missing_blocks)
16431646
.try_collect()
16441647
.await?;
16451648
let upsert_blocks: Vec<_> = new_blocks
16461649
.iter()
1647-
.map(|block| BlockFinality::Final(block.clone()))
1650+
.map(|block| BlockFinality::Final(alloy_block_to_web3_block_arc(block.clone())))
16481651
.collect();
16491652
let block_refs: Vec<_> = upsert_blocks
16501653
.iter()
@@ -1654,7 +1657,7 @@ impl EthereumAdapterTrait for EthereumAdapter {
16541657
error!(logger, "Error writing to block cache {}", e);
16551658
}
16561659
blocks.extend(new_blocks);
1657-
blocks.sort_by_key(|block| block.number);
1660+
blocks.sort_by_key(|block| block.header.number);
16581661
Ok(blocks)
16591662
}
16601663
}
@@ -1697,7 +1700,7 @@ pub(crate) async fn blocks_with_triggers(
16971700
debug!(logger, "Finding nearest valid `to` block to {}", to);
16981701

16991702
let to_ptr = eth.next_existing_ptr_to_number(&logger, to).await?;
1700-
let to_hash = to_ptr.hash_as_h256();
1703+
let to_hash = to_ptr.hash_as_b256();
17011704
let to = to_ptr.block_number();
17021705

17031706
// This is for `start` triggers which can be initialization handlers which needs to be run
@@ -1774,8 +1777,10 @@ pub(crate) async fn blocks_with_triggers(
17741777
.await
17751778
.with_context(|| format!("Failed to obtain triggers for block {}", to))?;
17761779

1777-
let mut block_hashes: HashSet<H256> =
1778-
triggers.iter().map(EthereumTrigger::block_hash).collect();
1780+
let mut block_hashes: HashSet<B256> = triggers
1781+
.iter()
1782+
.map(|trigger| h256_to_b256(trigger.block_hash()))
1783+
.collect();
17791784
let mut triggers_by_block: HashMap<BlockNumber, Vec<EthereumTrigger>> =
17801785
triggers.into_iter().fold(HashMap::new(), |mut map, t| {
17811786
map.entry(t.block_number()).or_default().push(t);
@@ -1795,15 +1800,15 @@ pub(crate) async fn blocks_with_triggers(
17951800
.await?
17961801
.into_iter()
17971802
.map(
1798-
move |block| match triggers_by_block.remove(&(block.number() as BlockNumber)) {
1803+
move |block| match triggers_by_block.remove(&(block.header.number() as BlockNumber)) {
17991804
Some(triggers) => Ok(BlockWithTriggers::new(
1800-
BlockFinality::Final(block),
1805+
BlockFinality::Final(alloy_block_to_web3_block_arc(block)),
18011806
triggers,
18021807
&logger2,
18031808
)),
18041809
None => Err(anyhow!(
18051810
"block {} not found in `triggers_by_block`",
1806-
block.block_ptr()
1811+
BlockPtr::new(block.header.hash.into(), block.header.number as i32)
18071812
)),
18081813
},
18091814
)

graph/src/blockchain/types.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use alloy::primitives::B256;
12
use anyhow::anyhow;
23
use diesel::deserialize::FromSql;
34
use diesel::pg::Pg;
@@ -95,8 +96,8 @@ impl From<Vec<u8>> for BlockHash {
9596
}
9697
}
9798

98-
impl From<alloy::primitives::B256> for BlockHash {
99-
fn from(hash: alloy::primitives::B256) -> Self {
99+
impl From<B256> for BlockHash {
100+
fn from(hash: B256) -> Self {
100101
BlockHash(hash.as_slice().into())
101102
}
102103
}
@@ -183,6 +184,10 @@ impl BlockPtr {
183184
H256::from_slice(&self.hash_slice()[..32])
184185
}
185186

187+
pub fn hash_as_b256(&self) -> B256 {
188+
B256::from_slice(&self.hash_slice()[..32])
189+
}
190+
186191
pub fn hash_slice(&self) -> &[u8] {
187192
self.hash.0.as_ref()
188193
}

graph/src/components/ethereum/types.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ use std::{convert::TryFrom, sync::Arc};
44
use web3::types::{Address, Block, Bytes, Log, Transaction, TransactionReceipt, H256, U256, U64};
55

66
use crate::{
7+
alloy_todo,
78
blockchain::{BlockPtr, BlockTime},
8-
prelude::{alloy_address_to_h160, b256_to_h256, BlockNumber},
9+
prelude::{
10+
alloy::rpc::types::Block as AlloyBlock, alloy_address_to_h160, b256_to_h256, BlockNumber,
11+
},
912
util::conversions::{alloy_bytes_to_web3_bytes, alloy_u256_to_web3_u256, u64_to_web3_u256},
1013
};
1114

1215
pub type LightEthereumBlock = Block<Transaction>;
16+
pub type LightEthereumBlockAlloy = AlloyBlock;
1317

1418
pub trait LightEthereumBlockExt {
1519
fn number(&self) -> BlockNumber;
@@ -21,6 +25,42 @@ pub trait LightEthereumBlockExt {
2125
fn timestamp(&self) -> BlockTime;
2226
}
2327

28+
impl LightEthereumBlockExt for LightEthereumBlockAlloy {
29+
fn number(&self) -> BlockNumber {
30+
alloy_todo!()
31+
}
32+
33+
fn timestamp(&self) -> BlockTime {
34+
alloy_todo!()
35+
}
36+
37+
fn transaction_for_log(&self, _log: &Log) -> Option<Transaction> {
38+
alloy_todo!()
39+
}
40+
41+
fn transaction_for_call(&self, _call: &EthereumCall) -> Option<Transaction> {
42+
alloy_todo!()
43+
}
44+
45+
fn parent_ptr(&self) -> Option<BlockPtr> {
46+
match self.header.number {
47+
0 => None,
48+
n => Some(BlockPtr::new(
49+
self.header.parent_hash.into(),
50+
(n - 1) as i32,
51+
)),
52+
}
53+
}
54+
55+
fn format(&self) -> String {
56+
alloy_todo!()
57+
}
58+
59+
fn block_ptr(&self) -> BlockPtr {
60+
alloy_todo!()
61+
}
62+
}
63+
2464
impl LightEthereumBlockExt for LightEthereumBlock {
2565
fn number(&self) -> BlockNumber {
2666
BlockNumber::try_from(self.number.unwrap().as_u64()).unwrap()

graph/src/util/conversions.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ pub fn alloy_log_to_web3_log(log: AlloyLog) -> Web3Log {
8888
}
8989
}
9090

91+
#[macro_export]
92+
macro_rules! alloy_todo {
93+
() => {
94+
todo!()
95+
};
96+
}
97+
9198
pub fn alloy_transaction_receipt_to_web3_transaction_receipt(
9299
_receipt: Arc<AlloyTransactionReceipt>,
93100
) -> Arc<Web3TransactionReceipt> {

0 commit comments

Comments
 (0)