Skip to content

Commit f79cffb

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

File tree

8 files changed

+69
-77
lines changed

8 files changed

+69
-77
lines changed

chain/ethereum/src/adapter.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use graph::data_source::common::ContractCall;
77
use graph::firehose::CallToFilter;
88
use graph::firehose::CombinedFilter;
99
use graph::firehose::LogFilter;
10+
use graph::prelude::alloy::primitives::B256;
11+
use graph::prelude::alloy::rpc::types::Block as AlloyBlock;
1012
use graph::prelude::alloy::transports::{RpcError, TransportErrorKind};
1113
use graph::prelude::web3::types::H160;
1214
use itertools::Itertools;
@@ -1244,12 +1246,6 @@ pub trait EthereumAdapter: Send + Sync + 'static {
12441246
/// Get the latest block, with only the header and transaction hashes.
12451247
async fn latest_block_ptr(&self, logger: &Logger) -> Result<BlockPtr, bc::IngestorError>;
12461248

1247-
async fn load_block(
1248-
&self,
1249-
logger: &Logger,
1250-
block_hash: H256,
1251-
) -> Result<LightEthereumBlock, Error>;
1252-
12531249
/// Load Ethereum blocks in bulk, returning results as they come back as a Stream.
12541250
/// May use the `chain_store` as a cache.
12551251
async fn load_blocks(
@@ -1263,20 +1259,20 @@ pub trait EthereumAdapter: Send + Sync + 'static {
12631259
async fn block_by_hash(
12641260
&self,
12651261
logger: &Logger,
1266-
block_hash: H256,
1267-
) -> Result<Option<LightEthereumBlock>, Error>;
1262+
block_hash: B256,
1263+
) -> Result<Option<AlloyBlock>, Error>;
12681264

12691265
async fn block_by_number(
12701266
&self,
12711267
logger: &Logger,
12721268
block_number: BlockNumber,
1273-
) -> Result<Option<LightEthereumBlock>, Error>;
1269+
) -> Result<Option<AlloyBlock>, Error>;
12741270

12751271
/// Load full information for the specified `block` (in particular, transaction receipts).
12761272
async fn load_full_block(
12771273
&self,
12781274
logger: &Logger,
1279-
block: LightEthereumBlock,
1275+
block: AlloyBlock,
12801276
) -> Result<EthereumBlock, bc::IngestorError>;
12811277

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

chain/ethereum/src/ethereum_adapter.rs

Lines changed: 33 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,18 @@ use graph::prelude::{
3232
},
3333
rpc::types::{
3434
trace::{filter::TraceFilter as AlloyTraceFilter, parity::LocalizedTransactionTrace},
35-
TransactionInput, TransactionRequest,
35+
Block as AlloyBlock, TransactionInput, TransactionRequest,
3636
},
3737
transports::{RpcError, TransportErrorKind},
3838
},
39-
alloy_log_to_web3_log, alloy_transaction_receipt_to_web3_transaction_receipt, b256_to_h256,
39+
alloy_log_to_web3_log, alloy_transaction_receipt_to_web3_transaction_receipt,
4040
h160_to_alloy_address, h256_to_b256,
4141
tokio::try_join,
4242
};
4343
use graph::slog::o;
4444
use graph::tokio::sync::RwLock;
4545
use graph::tokio::time::timeout;
46+
use graph::util::conversions::alloy_block_to_web3_block;
4647
use graph::{
4748
blockchain::{block_stream::BlockWithTriggers, BlockPtr, IngestorError},
4849
prelude::{
@@ -1258,27 +1259,12 @@ impl EthereumAdapterTrait for EthereumAdapter {
12581259
.await
12591260
}
12601261

1261-
async fn load_block(
1262-
&self,
1263-
logger: &Logger,
1264-
block_hash: H256,
1265-
) -> Result<LightEthereumBlock, Error> {
1266-
self.block_by_hash(logger, block_hash)
1267-
.await?
1268-
.ok_or_else(move || {
1269-
anyhow!(
1270-
"Ethereum node could not find block with hash {}",
1271-
block_hash
1272-
)
1273-
})
1274-
}
1275-
12761262
async fn block_by_hash(
12771263
&self,
12781264
logger: &Logger,
1279-
block_hash: H256,
1280-
) -> Result<Option<LightEthereumBlock>, Error> {
1281-
let web3 = self.web3.clone();
1265+
block_hash: B256,
1266+
) -> Result<Option<AlloyBlock>, Error> {
1267+
let alloy = self.alloy.clone();
12821268
let logger = logger.clone();
12831269
let retry_log_message = format!(
12841270
"eth_getBlockByHash RPC call for block hash {:?}",
@@ -1290,10 +1276,11 @@ impl EthereumAdapterTrait for EthereumAdapter {
12901276
.limit(ENV_VARS.request_retries)
12911277
.timeout_secs(ENV_VARS.json_rpc_timeout.as_secs())
12921278
.run(move || {
1293-
let web3 = web3.cheap_clone();
1279+
let alloy = alloy.cheap_clone();
12941280
async move {
1295-
web3.eth()
1296-
.block_with_txs(BlockId::Hash(block_hash))
1281+
alloy
1282+
.get_block_by_hash(block_hash)
1283+
.full()
12971284
.await
12981285
.map_err(Error::from)
12991286
}
@@ -1310,8 +1297,8 @@ impl EthereumAdapterTrait for EthereumAdapter {
13101297
&self,
13111298
logger: &Logger,
13121299
block_number: BlockNumber,
1313-
) -> Result<Option<LightEthereumBlock>, Error> {
1314-
let web3 = self.web3.clone();
1300+
) -> Result<Option<AlloyBlock>, Error> {
1301+
let alloy = self.alloy.clone();
13151302
let logger = logger.clone();
13161303
let retry_log_message = format!(
13171304
"eth_getBlockByNumber RPC call for block number {}",
@@ -1322,10 +1309,13 @@ impl EthereumAdapterTrait for EthereumAdapter {
13221309
.no_limit()
13231310
.timeout_secs(ENV_VARS.json_rpc_timeout.as_secs())
13241311
.run(move || {
1325-
let web3 = web3.cheap_clone();
1312+
let alloy = alloy.clone();
13261313
async move {
1327-
web3.eth()
1328-
.block_with_txs(BlockId::Number(block_number.into()))
1314+
alloy
1315+
.get_block_by_number(alloy::rpc::types::BlockNumberOrTag::Number(
1316+
block_number as u64,
1317+
))
1318+
.full()
13291319
.await
13301320
.map_err(Error::from)
13311321
}
@@ -1344,50 +1334,42 @@ impl EthereumAdapterTrait for EthereumAdapter {
13441334
async fn load_full_block(
13451335
&self,
13461336
logger: &Logger,
1347-
block: LightEthereumBlock,
1337+
block: AlloyBlock,
13481338
) -> Result<EthereumBlock, IngestorError> {
13491339
let alloy = self.alloy.clone();
13501340
let logger = logger.clone();
1351-
let block_hash = block.hash.expect("block is missing block hash");
1341+
let block_hash = block.header.hash;
13521342

13531343
// The early return is necessary for correctness, otherwise we'll
13541344
// request an empty batch which is not valid in JSON-RPC.
13551345
if block.transactions.is_empty() {
13561346
trace!(logger, "Block {} contains no transactions", block_hash);
13571347
return Ok(EthereumBlock {
1358-
block: Arc::new(block),
1348+
block: Arc::new(alloy_block_to_web3_block(block)),
13591349
transaction_receipts: Vec::new(),
13601350
});
13611351
}
1362-
let hashes: Vec<_> = block.transactions.iter().map(|txn| txn.hash).collect();
1352+
let hashes: Vec<_> = block.transactions.hashes().collect();
13631353

13641354
let supports_block_receipts = self
13651355
.check_block_receipt_support_and_update_cache(
13661356
alloy.clone(),
1367-
h256_to_b256(block_hash),
1357+
block_hash,
13681358
self.supports_eip_1898,
13691359
self.call_only,
13701360
logger.clone(),
13711361
)
13721362
.await;
13731363

1374-
let hashes_b256 = hashes.iter().map(|hash| h256_to_b256(*hash)).collect();
1375-
let block_hash_b256 = h256_to_b256(block_hash);
1376-
fetch_receipts_with_retry(
1377-
alloy,
1378-
hashes_b256,
1379-
block_hash_b256,
1380-
logger,
1381-
supports_block_receipts,
1382-
)
1383-
.await
1384-
.map(|transaction_receipts| EthereumBlock {
1385-
block: Arc::new(block),
1386-
transaction_receipts: transaction_receipts
1387-
.into_iter()
1388-
.map(|receipt| alloy_transaction_receipt_to_web3_transaction_receipt(receipt))
1389-
.collect(),
1390-
})
1364+
fetch_receipts_with_retry(alloy, hashes, block_hash, logger, supports_block_receipts)
1365+
.await
1366+
.map(|transaction_receipts| EthereumBlock {
1367+
block: Arc::new(alloy_block_to_web3_block(block)),
1368+
transaction_receipts: transaction_receipts
1369+
.into_iter()
1370+
.map(|receipt| alloy_transaction_receipt_to_web3_transaction_receipt(receipt))
1371+
.collect(),
1372+
})
13911373
}
13921374

13931375
async fn get_balance(
@@ -2447,7 +2429,7 @@ fn resolve_transaction_receipt(
24472429
// considers this block to be in the main chain. Nothing we can do from here except
24482430
// give up trying to ingest this block. There is no way to get the transaction
24492431
// receipt from this block.
2450-
Err(IngestorError::BlockUnavailable(b256_to_h256(block_hash)))
2432+
Err(IngestorError::BlockUnavailable(block_hash))
24512433
} else {
24522434
Ok(receipt)
24532435
}

chain/ethereum/src/ingestor.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ use crate::{EthereumAdapter, EthereumAdapterTrait as _};
33
use graph::blockchain::client::ChainClient;
44
use graph::blockchain::BlockchainKind;
55
use graph::components::network_provider::ChainName;
6+
use graph::prelude::alloy::primitives::B256;
67
use graph::slog::o;
78
use graph::util::backoff::ExponentialBackoff;
89
use graph::{
910
blockchain::{BlockHash, BlockIngestor, BlockPtr, IngestorError},
1011
cheap_clone::CheapClone,
1112
prelude::{
12-
async_trait, error, info, tokio, trace, warn, web3::types::H256, ChainStore, Error,
13-
EthereumBlockWithCalls, LogCode, Logger,
13+
async_trait, error, info, tokio, trace, warn, ChainStore, Error, EthereumBlockWithCalls,
14+
LogCode, Logger,
1415
},
1516
};
1617
use std::{sync::Arc, time::Duration};
@@ -168,8 +169,8 @@ impl PollingBlockIngestor {
168169
eth_adapter: &Arc<EthereumAdapter>,
169170
block_hash: &BlockHash,
170171
) -> Result<Option<BlockHash>, IngestorError> {
171-
// TODO: H256::from_slice can panic
172-
let block_hash = H256::from_slice(block_hash.as_slice());
172+
// TODO: B256::from_slice can panic
173+
let block_hash = B256::from_slice(block_hash.as_slice());
173174

174175
// Get the fully populated block
175176
let block = eth_adapter

graph/src/blockchain/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ use std::{
4747
str::FromStr,
4848
sync::Arc,
4949
};
50-
use web3::types::H256;
5150

5251
pub use block_stream::{ChainHeadUpdateListener, ChainHeadUpdateStream, TriggersAdapter};
5352
pub use builder::{BasicBlockchainBuilder, BlockchainBuilder};
@@ -225,7 +224,7 @@ pub enum IngestorError {
225224
/// The Ethereum node does not know about this block for some reason, probably because it
226225
/// disappeared in a chain reorg.
227226
#[error("Block data unavailable, block was likely uncled (block hash = {0:?})")]
228-
BlockUnavailable(H256),
227+
BlockUnavailable(B256),
229228

230229
/// The Ethereum node does not know about this block for some reason, probably because it
231230
/// disappeared in a chain reorg.

graph/src/blockchain/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl From<Vec<u8>> for BlockHash {
9797

9898
impl From<alloy::primitives::B256> for BlockHash {
9999
fn from(hash: alloy::primitives::B256) -> Self {
100-
BlockHash(hash.0.to_vec().into())
100+
BlockHash(hash.as_slice().into())
101101
}
102102
}
103103

graph/src/util/conversions.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,13 @@ pub fn alloy_transaction_receipt_to_web3_transaction_receipt(
9595
}
9696

9797
/// Converts alloy Block to web3 Block
98-
pub fn alloy_block_to_web3_block(_block: Arc<AlloyBlock>) -> Arc<Web3Block<Web3Transaction>> {
98+
pub fn alloy_block_to_web3_block_arc(_block: Arc<AlloyBlock>) -> Arc<Web3Block<Web3Transaction>> {
99+
unimplemented!(
100+
"Block conversion from alloy to web3 not yet implemented - will be done when needed"
101+
)
102+
}
103+
104+
pub fn alloy_block_to_web3_block(_block: AlloyBlock) -> Web3Block<Web3Transaction> {
99105
unimplemented!(
100106
"Block conversion from alloy to web3 not yet implemented - will be done when needed"
101107
)

node/src/manager/commands/chain.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ use graph::components::store::ChainIdStore;
1212
use graph::components::store::StoreError;
1313
use graph::prelude::BlockNumber;
1414
use graph::prelude::ChainStore as _;
15-
use graph::prelude::LightEthereumBlockExt;
1615
use graph::prelude::{anyhow, anyhow::bail};
1716
use graph::slog::Logger;
17+
use graph::util::conversions::alloy_block_to_web3_block;
1818
use graph::{
1919
components::store::BlockStore as _, components::store::ChainHeadStore as _,
2020
prelude::anyhow::Error,
@@ -276,14 +276,18 @@ pub async fn ingest(
276276
else {
277277
bail!("block number {number} not found");
278278
};
279-
let ptr = block.block_ptr();
279+
let hash = block.header.hash;
280+
let number = block.header.number;
280281
// For inserting the block, it doesn't matter whether the block is final or not.
281-
let block = Arc::new(BlockFinality::Final(Arc::new(block)));
282+
let block = Arc::new(BlockFinality::Final(Arc::new(alloy_block_to_web3_block(
283+
block,
284+
))));
282285
chain_store.upsert_block(block).await?;
283286

284-
let rows = chain_store.confirm_block_hash(ptr.number, &ptr.hash)?;
287+
let hash = hash.into();
288+
let rows = chain_store.confirm_block_hash(number as i32, &hash)?;
285289

286-
println!("Inserted block {}", ptr);
290+
println!("Inserted block {}", hash);
287291
if rows > 0 {
288292
println!(" (also deleted {rows} duplicate row(s) with that number)");
289293
}

node/src/manager/commands/check_blocks.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,10 @@ mod steps {
155155

156156
use graph::{
157157
anyhow::bail,
158-
prelude::serde_json::{self, Value},
158+
prelude::{
159+
h256_to_b256,
160+
serde_json::{self, Value},
161+
},
159162
};
160163
use json_structural_diff::{colorize as diff_to_string, JsonDiff};
161164

@@ -201,13 +204,14 @@ mod steps {
201204
ethereum_adapter: &EthereumAdapter,
202205
logger: &Logger,
203206
) -> anyhow::Result<Value> {
207+
let block_hash = h256_to_b256(*block_hash);
204208
let provider_block = ethereum_adapter
205-
.block_by_hash(logger, *block_hash)
209+
.block_by_hash(logger, block_hash)
206210
.await
207211
.with_context(|| format!("failed to fetch block {block_hash}"))?
208212
.ok_or_else(|| anyhow!("JRPC provider found no block with hash {block_hash:?}"))?;
209213
ensure!(
210-
provider_block.hash == Some(*block_hash),
214+
provider_block.header.hash == block_hash,
211215
"Provider responded with a different block hash"
212216
);
213217
serde_json::to_value(provider_block)

0 commit comments

Comments
 (0)