Skip to content

Commit 5a6b974

Browse files
committed
core,graph: Ethereum struct improvements, impl copy, make clone explicit
1 parent ba48da4 commit 5a6b974

File tree

4 files changed

+29
-35
lines changed

4 files changed

+29
-35
lines changed

core/src/subgraph/instance.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,11 @@ where
165165

166166
let transaction = block
167167
.transaction_for_log(&log)
168-
.map(Arc::new)
168+
.map(|tx| Arc::new(tx.clone()))
169169
.context("Found no transaction for event")?;
170170
let matching_hosts = hosts.iter().filter(|host| host.matches_log(&log));
171171
// Process the log in each host in the same order the corresponding data
172172
// sources appear in the subgraph manifest
173-
let transaction = Arc::new(transaction);
174173
for host in matching_hosts {
175174
state = host
176175
.process_log(
@@ -189,8 +188,8 @@ where
189188

190189
let transaction = block
191190
.transaction_for_call(&call)
191+
.map(|tx| Arc::new(tx.clone()))
192192
.context("Found no transaction for call")?;
193-
let transaction = Arc::new(transaction);
194193
let matching_hosts = hosts.iter().filter(|host| host.matches_call(&call));
195194

196195
for host in matching_hosts {

graph/src/components/ethereum/adapter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,9 +438,9 @@ impl EthereumBlockFilter {
438438
.mapping
439439
.block_handlers
440440
.iter()
441-
.map(|handler| &handler.input)
441+
.map(|handler| handler.input)
442442
.max()
443-
.unwrap_or(&EthereumBlockHandlerData::Block)
443+
.unwrap_or(EthereumBlockHandlerData::Block)
444444
.into();
445445

446446
filter_opt.extend(Self {

graph/src/components/ethereum/types.rs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ pub enum EthereumBlockType {
200200

201201
FullWithReceipts(EthereumBlock),
202202
}
203+
203204
impl EthereumBlockType {
204205
pub fn light_block(&self) -> &LightEthereumBlock {
205206
match self {
@@ -217,26 +218,22 @@ impl EthereumBlockType {
217218
self.light_block().number.unwrap().as_u64()
218219
}
219220

220-
pub fn transaction_for_log(&self, log: &Log) -> Option<Transaction> {
221-
log.transaction_hash
222-
.and_then(|hash| {
223-
self.light_block()
224-
.transactions
225-
.iter()
226-
.find(|tx| tx.hash == hash)
227-
})
228-
.cloned()
221+
pub fn transaction_for_log(&self, log: &Log) -> Option<&Transaction> {
222+
log.transaction_hash.and_then(|hash| {
223+
self.light_block()
224+
.transactions
225+
.iter()
226+
.find(|tx| tx.hash == hash)
227+
})
229228
}
230229

231-
pub fn transaction_for_call(&self, call: &EthereumCall) -> Option<Transaction> {
232-
call.transaction_hash
233-
.and_then(|hash| {
234-
self.light_block()
235-
.transactions
236-
.iter()
237-
.find(|tx| tx.hash == hash)
238-
})
239-
.cloned()
230+
pub fn transaction_for_call(&self, call: &EthereumCall) -> Option<&Transaction> {
231+
call.transaction_hash.and_then(|hash| {
232+
self.light_block()
233+
.transactions
234+
.iter()
235+
.find(|tx| tx.hash == hash)
236+
})
240237
}
241238
}
242239

@@ -259,8 +256,8 @@ impl Default for BlockType {
259256
}
260257
}
261258

262-
impl<'a> From<&'a EthereumBlockHandlerData> for BlockType {
263-
fn from(block: &'a EthereumBlockHandlerData) -> BlockType {
259+
impl From<EthereumBlockHandlerData> for BlockType {
260+
fn from(block: EthereumBlockHandlerData) -> BlockType {
264261
match block {
265262
EthereumBlockHandlerData::Block => BlockType::Light,
266263
EthereumBlockHandlerData::FullBlock => BlockType::Full,
@@ -364,6 +361,7 @@ pub struct EthereumTransactionReceiptData {
364361
pub input: Bytes,
365362
}
366363

364+
/// Ethereum block data with transactions and their receipts.
367365
pub struct FullEthereumBlockDataWithReceipts {
368366
pub hash: H256,
369367
pub parent_hash: H256,
@@ -443,18 +441,15 @@ impl<'a> TryFrom<&'a EthereumBlockType> for FullEthereumBlockDataWithReceipts {
443441
) -> Result<FullEthereumBlockDataWithReceipts, Self::Error> {
444442
let fullblock = match block {
445443
EthereumBlockType::FullWithReceipts(full_block) => full_block,
446-
EthereumBlockType::Full(_) => return Err(anyhow::anyhow!(
444+
EthereumBlockType::Full(_) | EthereumBlockType::Light(_) => return Err(anyhow::anyhow!(
447445
"Failed to convert EthereumBlockType to FullEthereumBlockDataWithReceipts, requires an EthereumBlockType::FullWithReceipts()"
448446
)),
449-
EthereumBlockType::Light(_) => return Err(anyhow::anyhow!(
450-
"Failed to convert EthereumBlockType to FullEthereumBlockDataWithReceipts, requires an EthereumBlockType::FullWithReceipts()"
451-
))
452447
};
453448
Ok(fullblock.into())
454449
}
455450
}
456451

457-
/// Ethereum block data.
452+
/// Ethereum block data with transactions.
458453
#[derive(Clone, Debug, Default)]
459454
pub struct FullEthereumBlockData {
460455
pub hash: H256,

graph/src/data/subgraph/schema.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,7 @@ impl WriteOperations for EthereumBlockHandlerEntity {
10261026
if let Some(filter_id) = filter_id {
10271027
entity.set("filter", filter_id);
10281028
}
1029-
entity.set("input", String::from(self.input));
1029+
entity.set("input", self.input);
10301030
ops.add(Self::TYPENAME, id.to_owned(), entity);
10311031
}
10321032
}
@@ -1110,7 +1110,7 @@ impl TryFromValue for EthereumBlockHandlerFilterEntity {
11101110
}
11111111
}
11121112

1113-
#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, Deserialize)]
1113+
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, Deserialize)]
11141114
pub enum EthereumBlockHandlerData {
11151115
Block,
11161116
FullBlock,
@@ -1149,9 +1149,9 @@ impl From<EthereumBlockHandlerData> for String {
11491149
}
11501150
}
11511151

1152-
impl From<EthereumBlockHandlerData> for q::Value {
1153-
fn from(data: EthereumBlockHandlerData) -> q::Value {
1154-
q::Value::Enum(data.into())
1152+
impl From<EthereumBlockHandlerData> for Value {
1153+
fn from(data: EthereumBlockHandlerData) -> Value {
1154+
Value::String(data.into())
11551155
}
11561156
}
11571157

0 commit comments

Comments
 (0)