Skip to content

Commit 71b59f0

Browse files
committed
Remove unneccessary conversion functions
1 parent f1b9c49 commit 71b59f0

File tree

8 files changed

+43
-154
lines changed

8 files changed

+43
-154
lines changed

chain/ethereum/src/adapter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,7 @@ pub trait EthereumAdapter: Send + Sync + 'static {
11591159
logger: Logger,
11601160
chain_store: Arc<dyn ChainStore>,
11611161
block_hashes: HashSet<B256>,
1162-
) -> Result<Vec<Arc<AlloyBlock>>, Error>;
1162+
) -> Result<Vec<Arc<LightEthereumBlock>>, Error>;
11631163

11641164
/// Find a block by its hash.
11651165
async fn block_by_hash(

chain/ethereum/src/ethereum_adapter.rs

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use graph::futures03::future::try_join_all;
2020
use graph::futures03::{
2121
self, compat::Future01CompatExt, FutureExt, StreamExt, TryFutureExt, TryStreamExt,
2222
};
23-
use graph::prelude::alloy::consensus::BlockHeader;
2423
use graph::prelude::alloy::primitives::Address;
2524
use graph::prelude::alloy::rpc::types::Transaction;
2625
use graph::prelude::{
@@ -45,8 +44,6 @@ use graph::prelude::{
4544
use graph::slog::o;
4645
use graph::tokio::sync::RwLock;
4746
use graph::tokio::time::timeout;
48-
use graph::util::conversions::alloy_block_to_block;
49-
use graph::util::conversions::alloy_block_to_block_arc;
5047
use graph::{
5148
blockchain::{block_stream::BlockWithTriggers, BlockPtr, IngestorError},
5249
prelude::{
@@ -718,7 +715,7 @@ impl EthereumAdapter {
718715
&self,
719716
logger: Logger,
720717
ids: Vec<B256>,
721-
) -> impl futures03::Stream<Item = Result<Arc<AlloyBlock>, Error>> + Send {
718+
) -> impl futures03::Stream<Item = Result<Arc<LightEthereumBlock>, Error>> + Send {
722719
let alloy = self.alloy.clone();
723720

724721
futures03::stream::iter(ids.into_iter().map(move |hash| {
@@ -739,12 +736,14 @@ impl EthereumAdapter {
739736
.await
740737
.map_err(Error::from)
741738
.and_then(|block| {
742-
block.map(Arc::new).ok_or_else(|| {
743-
anyhow::anyhow!(
744-
"Ethereum node did not find block {:?}",
745-
hash
746-
)
747-
})
739+
block
740+
.map(|b| Arc::new(LightEthereumBlock::new(b)))
741+
.ok_or_else(|| {
742+
anyhow::anyhow!(
743+
"Ethereum node did not find block {:?}",
744+
hash
745+
)
746+
})
748747
})
749748
}
750749
})
@@ -1340,7 +1339,7 @@ impl EthereumAdapterTrait for EthereumAdapter {
13401339
if block.transactions.is_empty() {
13411340
trace!(logger, "Block {} contains no transactions", block_hash);
13421341
return Ok(EthereumBlock {
1343-
block: Arc::new(alloy_block_to_block(block)),
1342+
block: Arc::new(LightEthereumBlock::new(block)),
13441343
transaction_receipts: Vec::new(),
13451344
});
13461345
}
@@ -1359,7 +1358,7 @@ impl EthereumAdapterTrait for EthereumAdapter {
13591358
fetch_receipts_with_retry(alloy, hashes, block_hash, logger, supports_block_receipts)
13601359
.await
13611360
.map(|transaction_receipts| EthereumBlock {
1362-
block: Arc::new(alloy_block_to_block(block)),
1361+
block: Arc::new(LightEthereumBlock::new(block)),
13631362
transaction_receipts: transaction_receipts
13641363
.into_iter()
13651364
.map(|receipt| receipt)
@@ -1611,35 +1610,35 @@ impl EthereumAdapterTrait for EthereumAdapter {
16111610
logger: Logger,
16121611
chain_store: Arc<dyn ChainStore>,
16131612
block_hashes: HashSet<B256>,
1614-
) -> Result<Vec<Arc<AlloyBlock>>, Error> {
1613+
) -> Result<Vec<Arc<LightEthereumBlock>>, Error> {
16151614
let block_hashes: Vec<_> = block_hashes.iter().cloned().collect();
16161615
// Search for the block in the store first then use json-rpc as a backup.
1617-
let mut blocks: Vec<Arc<AlloyBlock>> = chain_store
1616+
let mut blocks: Vec<_> = chain_store
16181617
.cheap_clone()
16191618
.blocks(block_hashes.iter().map(|&b| b.into()).collect::<Vec<_>>())
16201619
.await
16211620
.map_err(|e| error!(&logger, "Error accessing block cache {}", e))
16221621
.unwrap_or_default()
16231622
.into_iter()
16241623
.filter_map(|value| json::from_value(value).ok())
1625-
.map(Arc::new)
1624+
.map(|b| Arc::new(LightEthereumBlock::new(b)))
16261625
.collect();
16271626

16281627
let missing_blocks = Vec::from_iter(
16291628
block_hashes
16301629
.into_iter()
1631-
.filter(|hash| !blocks.iter().any(|b| b.header.hash == *hash)),
1630+
.filter(|hash| !blocks.iter().any(|b| b.hash() == *hash)),
16321631
);
16331632

16341633
// Return a stream that lazily loads batches of blocks.
16351634
debug!(logger, "Requesting {} block(s)", missing_blocks.len());
1636-
let new_blocks: Vec<Arc<AlloyBlock>> = self
1635+
let new_blocks: Vec<_> = self
16371636
.load_blocks_rpc(logger.clone(), missing_blocks)
16381637
.try_collect()
16391638
.await?;
16401639
let upsert_blocks: Vec<_> = new_blocks
16411640
.iter()
1642-
.map(|block| BlockFinality::Final(alloy_block_to_block_arc(block.clone())))
1641+
.map(|block| BlockFinality::Final(block.clone()))
16431642
.collect();
16441643
let block_refs: Vec<_> = upsert_blocks
16451644
.iter()
@@ -1649,7 +1648,7 @@ impl EthereumAdapterTrait for EthereumAdapter {
16491648
error!(logger, "Error writing to block cache {}", e);
16501649
}
16511650
blocks.extend(new_blocks);
1652-
blocks.sort_by_key(|block| block.header.number);
1651+
blocks.sort_by_key(|block| block.number());
16531652
Ok(blocks)
16541653
}
16551654
}
@@ -1792,15 +1791,15 @@ pub(crate) async fn blocks_with_triggers(
17921791
.await?
17931792
.into_iter()
17941793
.map(
1795-
move |block| match triggers_by_block.remove(&(block.header.number() as BlockNumber)) {
1794+
move |block| match triggers_by_block.remove(&(block.number())) {
17961795
Some(triggers) => Ok(BlockWithTriggers::new(
1797-
BlockFinality::Final(alloy_block_to_block_arc(block)),
1796+
BlockFinality::Final(block),
17981797
triggers,
17991798
&logger2,
18001799
)),
18011800
None => Err(anyhow!(
18021801
"block {} not found in `triggers_by_block`",
1803-
BlockPtr::new(block.header.hash.into(), block.header.number as i32)
1802+
block.block_ptr()
18041803
)),
18051804
},
18061805
)
@@ -2612,12 +2611,11 @@ mod tests {
26122611
EthereumBlockWithCalls,
26132612
};
26142613
use graph::blockchain::BlockPtr;
2615-
use graph::components::ethereum::BlockWrapper;
26162614
use graph::prelude::alloy::primitives::{Address, Bytes, B256};
26172615
use graph::prelude::alloy::providers::mock::Asserter;
26182616
use graph::prelude::alloy::providers::ProviderBuilder;
26192617
use graph::prelude::tokio::{self};
2620-
use graph::prelude::{create_minimal_block_for_test, EthereumCall};
2618+
use graph::prelude::{create_minimal_block_for_test, EthereumCall, LightEthereumBlock};
26212619
use jsonrpc_core::serde_json::{self, Value};
26222620
use std::collections::HashSet;
26232621
use std::iter::FromIterator;
@@ -2627,10 +2625,9 @@ mod tests {
26272625
fn parse_block_triggers_every_block() {
26282626
let block = create_minimal_block_for_test(2, hash(2));
26292627

2630-
#[allow(unreachable_code)]
26312628
let block = EthereumBlockWithCalls {
26322629
ethereum_block: EthereumBlock {
2633-
block: Arc::new(BlockWrapper::new(block)),
2630+
block: Arc::new(LightEthereumBlock::new(block)),
26342631
..Default::default()
26352632
},
26362633
calls: Some(vec![EthereumCall {
@@ -2772,7 +2769,7 @@ mod tests {
27722769
#[allow(unreachable_code)]
27732770
let block = EthereumBlockWithCalls {
27742771
ethereum_block: EthereumBlock {
2775-
block: Arc::new(BlockWrapper::new(block)),
2772+
block: Arc::new(LightEthereumBlock::new(block)),
27762773
..Default::default()
27772774
},
27782775
calls: Some(vec![EthereumCall {
@@ -2803,7 +2800,7 @@ mod tests {
28032800
#[allow(unreachable_code)]
28042801
let block = EthereumBlockWithCalls {
28052802
ethereum_block: EthereumBlock {
2806-
block: Arc::new(BlockWrapper::new(block)),
2803+
block: Arc::new(LightEthereumBlock::new(block)),
28072804
..Default::default()
28082805
},
28092806
calls: Some(vec![EthereumCall {

chain/ethereum/src/tests.rs

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

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

127-
let b = BlockWrapper::new(b);
127+
let b = LightEthereumBlock::new(b);
128128

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

206206
#[allow(unreachable_code)]
207-
let b = BlockWrapper::new(b);
207+
let b = LightEthereumBlock::new(b);
208208

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

graph/src/util/conversions.rs

Lines changed: 1 addition & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,14 @@
1-
use std::sync::Arc;
2-
3-
use web3::types::Bytes;
4-
5-
use crate::components::ethereum::BlockWrapper;
6-
use crate::prelude::alloy::primitives::{Address as AlloyAddress, B256};
7-
use crate::prelude::alloy::rpc::types::{
8-
Block as AlloyBlock, Log as AlloyLog, TransactionReceipt as AlloyTransactionReceipt,
9-
};
101
/// Type conversion utilities between web3 and alloy types
11-
use crate::prelude::web3::types::{
12-
Address as Web3Address, Block as Web3Block, Log as Web3Log, Transaction as Web3Transaction,
13-
TransactionReceipt as Web3TransactionReceipt, H160, H256, U256, U64,
14-
};
15-
16-
/// Converts H256 to alloy B256
17-
pub fn h256_to_b256(h: H256) -> B256 {
18-
B256::from_slice(h.as_bytes())
19-
}
20-
21-
/// Converts alloy B256 to H256
22-
pub fn b256_to_h256(b: B256) -> H256 {
23-
H256::from_slice(b.as_slice())
24-
}
25-
26-
pub fn web3_u64_from_option(opt: Option<u64>) -> U64 {
27-
U64::from(opt.unwrap_or(0))
28-
}
29-
30-
pub fn u64_to_web3_u256(u: u64) -> U256 {
31-
U256::from(u)
32-
}
33-
34-
pub fn bool_to_web3_u64(b: bool) -> U64 {
35-
U64::from(if b { 1 } else { 0 })
36-
}
37-
38-
pub fn u64_to_web3_u64(u: u64) -> U64 {
39-
U64::from(u)
40-
}
41-
/// Converts web3 H160 to alloy Address
42-
pub fn h160_to_alloy_address(h: H160) -> AlloyAddress {
43-
AlloyAddress::from_slice(h.as_bytes())
44-
}
45-
46-
/// Converts alloy Address to web3 H160
47-
pub fn alloy_address_to_h160(addr: AlloyAddress) -> H160 {
48-
H160::from_slice(addr.as_slice())
49-
}
50-
51-
/// Converts web3 Address to alloy Address
52-
pub fn web3_address_to_alloy_address(addr: Web3Address) -> AlloyAddress {
53-
h160_to_alloy_address(addr)
54-
}
55-
56-
/// Converts alloy Address to web3 Address
57-
pub fn alloy_address_to_web3_address(addr: AlloyAddress) -> Web3Address {
58-
alloy_address_to_h160(addr)
59-
}
2+
use crate::prelude::web3::types::U256;
603

614
// u256 to web3 U256
625
pub fn alloy_u256_to_web3_u256(_u: alloy::primitives::U256) -> U256 {
636
unimplemented!();
647
}
658

66-
pub fn alloy_bytes_to_web3_bytes(_b: alloy::primitives::Bytes) -> Bytes {
67-
unimplemented!();
68-
}
69-
70-
// u256 to alloy U256
71-
pub fn web3_u256_to_alloy_u256(u: U256) -> U256 {
72-
U256::from(u)
73-
}
74-
75-
/// Converts alloy Log to web3 Log
76-
pub fn alloy_log_to_web3_log(log: AlloyLog) -> Web3Log {
77-
Web3Log {
78-
address: alloy_address_to_h160(log.address()),
79-
topics: log.topics().iter().map(|t| b256_to_h256(*t)).collect(),
80-
data: log.data().data.clone().into(),
81-
block_hash: log.block_hash.map(b256_to_h256),
82-
block_number: log.block_number.map(|n| U64::from(n)),
83-
transaction_hash: log.transaction_hash.map(b256_to_h256),
84-
transaction_index: log.transaction_index.map(|i| U64::from(i)),
85-
log_index: log.log_index.map(|i| U256::from(i)),
86-
transaction_log_index: None, // alloy Log doesn't have transaction_log_index
87-
log_type: None, // alloy Log doesn't have log_type
88-
removed: Some(log.removed),
89-
}
90-
}
91-
92-
pub fn alloy_log_ref_to_web3_log_ref(_log: &AlloyLog) -> &Web3Log {
93-
unimplemented!()
94-
}
95-
969
#[macro_export]
9710
macro_rules! alloy_todo {
9811
() => {
9912
todo!()
10013
};
10114
}
102-
103-
pub fn alloy_transaction_receipt_to_web3_transaction_receipt(
104-
_receipt: Arc<AlloyTransactionReceipt>,
105-
) -> Arc<Web3TransactionReceipt> {
106-
unimplemented!("TransactionReceipt conversion not yet implemented - will be done when needed")
107-
}
108-
109-
/// Converts alloy Block to web3 Block
110-
pub fn alloy_block_to_block_arc(_block: Arc<AlloyBlock>) -> Arc<BlockWrapper> {
111-
unimplemented!(
112-
"Block conversion from alloy to web3 not yet implemented - will be done when needed"
113-
)
114-
}
115-
116-
pub fn alloy_block_to_block(_block: AlloyBlock) -> BlockWrapper {
117-
unimplemented!(
118-
"Block conversion from alloy to web3 not yet implemented - will be done when needed"
119-
)
120-
}
121-
122-
/// Converts web3 Block to alloy Block
123-
pub fn web3_block_to_alloy_block(_block: Web3Block<Web3Transaction>) -> AlloyBlock {
124-
unimplemented!(
125-
"Block conversion from web3 to alloy not yet implemented - will be done when needed"
126-
)
127-
}

node/src/manager/commands/chain.rs

Lines changed: 4 additions & 2 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::LightEthereumBlock;
1516
use graph::prelude::{anyhow, anyhow::bail};
1617
use graph::slog::Logger;
17-
use graph::util::conversions::alloy_block_to_block;
1818
use graph::{
1919
components::store::BlockStore as _, components::store::ChainHeadStore as _,
2020
prelude::anyhow::Error,
@@ -279,7 +279,9 @@ pub async fn ingest(
279279
let hash = block.header.hash;
280280
let number = block.header.number;
281281
// For inserting the block, it doesn't matter whether the block is final or not.
282-
let block = Arc::new(BlockFinality::Final(Arc::new(alloy_block_to_block(block))));
282+
let block = Arc::new(BlockFinality::Final(Arc::new(LightEthereumBlock::new(
283+
block,
284+
))));
283285
chain_store.upsert_block(block).await?;
284286

285287
let hash = hash.into();

store/test-store/src/block_store.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std::{convert::TryFrom, str::FromStr, sync::Arc};
22

33
use graph::blockchain::{BlockTime, ChainIdentifier};
4-
use graph::components::ethereum::BlockWrapper;
54
use graph::prelude::alloy::consensus::Header as ConsensusHeader;
65
use graph::prelude::alloy::primitives::{Bloom, B256, U256};
76
use graph::prelude::alloy::rpc::types::{Block, Header};
7+
use graph::prelude::LightEthereumBlock;
88
use lazy_static::lazy_static;
99

1010
use graph::components::store::BlockStore;
@@ -125,7 +125,7 @@ impl FakeBlock {
125125
let block = Block::empty(rpc_header);
126126

127127
EthereumBlock {
128-
block: Arc::new(BlockWrapper::new(block)),
128+
block: Arc::new(LightEthereumBlock::new(block)),
129129
transaction_receipts: Vec::new(),
130130
}
131131
}

0 commit comments

Comments
 (0)