Skip to content

Commit 18b49f8

Browse files
committed
core,graph,runtime: Pass EthereumBlockType into process_triggers()
- Rather converting to EthereumBlockType before sending the mapping request, pass EthereumBlockType down from process_block(). - Add utility functions to EthereumBlockType.
1 parent 1fd185a commit 18b49f8

File tree

12 files changed

+101
-56
lines changed

12 files changed

+101
-56
lines changed

core/src/subgraph/instance.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ where
135135
async fn process_trigger(
136136
&self,
137137
logger: &Logger,
138-
block: &Arc<LightEthereumBlock>,
138+
block: &Arc<EthereumBlockType>,
139139
trigger: EthereumTrigger,
140140
state: BlockState,
141141
proof_of_indexing: SharedProofOfIndexing,
@@ -154,7 +154,7 @@ where
154154
async fn process_trigger_in_runtime_hosts(
155155
logger: &Logger,
156156
hosts: &[Arc<T::Host>],
157-
block: &Arc<LightEthereumBlock>,
157+
block: &Arc<EthereumBlockType>,
158158
trigger: EthereumTrigger,
159159
mut state: BlockState,
160160
proof_of_indexing: SharedProofOfIndexing,

core/src/subgraph/instance_manager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ where
638638
}
639639

640640
// Obtain current and new block pointer (after this block is processed)
641-
let light_block = Arc::new(block.light_block());
641+
let light_block = Arc::new(EthereumBlockType::Light(block.light_block()));
642642
let block_ptr_after = EthereumBlockPointer::from(&block);
643643
let block_ptr_for_new_data_sources = block_ptr_after.clone();
644644

@@ -879,7 +879,7 @@ async fn process_triggers<B: BlockStreamBuilder, T: RuntimeHostBuilder, S: Send
879879
mut block_state: BlockState,
880880
proof_of_indexing: SharedProofOfIndexing,
881881
ctx: IndexingContext<B, T, S>,
882-
block: &Arc<LightEthereumBlock>,
882+
block: &Arc<EthereumBlockType>,
883883
triggers: Vec<EthereumTrigger>,
884884
) -> Result<(IndexingContext<B, T, S>, BlockState), CancelableError<Error>> {
885885
for trigger in triggers.into_iter() {

graph/src/components/ethereum/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ pub use self::adapter::{
1313
pub use self::listener::{ChainHeadUpdate, ChainHeadUpdateListener, ChainHeadUpdateStream};
1414
pub use self::stream::{BlockStream, BlockStreamBuilder, BlockStreamEvent};
1515
pub use self::types::{
16-
BlockFinality, BlockType, EthereumBlock, EthereumBlockData, FullEthereumBlockDataWithReceipts, EthereumBlockPointer,
16+
BlockFinality, BlockType, EthereumBlock, EthereumBlockData, EthereumBlockPointer,
1717
EthereumBlockTriggerType, EthereumBlockType, EthereumBlockWithCalls, EthereumBlockWithTriggers,
1818
EthereumCall, EthereumCallData, EthereumEventData, EthereumTransactionData,
19-
EthereumTransactionReceiptData, EthereumTrigger, FullEthereumBlockData, LightEthereumBlock,
20-
LightEthereumBlockExt,
19+
EthereumTransactionReceiptData, EthereumTrigger, FullEthereumBlockData,
20+
FullEthereumBlockDataWithReceipts, LightEthereumBlock, LightEthereumBlockExt,
2121
};

graph/src/components/ethereum/types.rs

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ pub type LightEthereumBlock = Block<Transaction>;
1313

1414
pub trait LightEthereumBlockExt {
1515
fn number(&self) -> u64;
16-
fn transaction_for_log(&self, log: &Log) -> Option<Transaction>;
17-
fn transaction_for_call(&self, call: &EthereumCall) -> Option<Transaction>;
1816
fn parent_ptr(&self) -> Option<EthereumBlockPointer>;
1917
fn format(&self) -> String;
2018
}
@@ -24,18 +22,6 @@ impl LightEthereumBlockExt for LightEthereumBlock {
2422
self.number.unwrap().as_u64()
2523
}
2624

27-
fn transaction_for_log(&self, log: &Log) -> Option<Transaction> {
28-
log.transaction_hash
29-
.and_then(|hash| self.transactions.iter().find(|tx| tx.hash == hash))
30-
.cloned()
31-
}
32-
33-
fn transaction_for_call(&self, call: &EthereumCall) -> Option<Transaction> {
34-
call.transaction_hash
35-
.and_then(|hash| self.transactions.iter().find(|tx| tx.hash == hash))
36-
.cloned()
37-
}
38-
3925
fn parent_ptr(&self) -> Option<EthereumBlockPointer> {
4026
match self.number() {
4127
0 => None,
@@ -212,6 +198,45 @@ pub enum EthereumBlockType {
212198

213199
FullWithReceipts(EthereumBlock),
214200
}
201+
impl EthereumBlockType {
202+
pub fn light_block(&self) -> &LightEthereumBlock {
203+
match self {
204+
EthereumBlockType::Light(block) => block,
205+
EthereumBlockType::Full(block) => block,
206+
EthereumBlockType::FullWithReceipts(block) => &block.block,
207+
}
208+
}
209+
210+
pub fn hash(&self) -> H256 {
211+
self.light_block().hash.unwrap()
212+
}
213+
214+
pub fn number(&self) -> u64 {
215+
self.light_block().number.unwrap().as_u64()
216+
}
217+
218+
pub fn transaction_for_log(&self, log: &Log) -> Option<Transaction> {
219+
log.transaction_hash
220+
.and_then(|hash| {
221+
self.light_block()
222+
.transactions
223+
.iter()
224+
.find(|tx| tx.hash == hash)
225+
})
226+
.cloned()
227+
}
228+
229+
pub fn transaction_for_call(&self, call: &EthereumCall) -> Option<Transaction> {
230+
call.transaction_hash
231+
.and_then(|hash| {
232+
self.light_block()
233+
.transactions
234+
.iter()
235+
.find(|tx| tx.hash == hash)
236+
})
237+
.cloned()
238+
}
239+
}
215240

216241
impl Default for EthereumBlockType {
217242
fn default() -> Self {
@@ -342,7 +367,9 @@ pub struct FullEthereumBlockDataWithReceipts {
342367
impl<'a> TryFrom<&'a EthereumBlockType> for FullEthereumBlockDataWithReceipts {
343368
type Error = String;
344369

345-
fn try_from(block: &'a EthereumBlockType) -> Result<FullEthereumBlockDataWithReceipts, Self::Error> {
370+
fn try_from(
371+
block: &'a EthereumBlockType,
372+
) -> Result<FullEthereumBlockDataWithReceipts, Self::Error> {
346373
let fullblock = match block {
347374
EthereumBlockType::FullWithReceipts(full_block) => full_block,
348375
EthereumBlockType::Full(_) => return Err(format!(
@@ -770,6 +797,16 @@ impl<'a> From<&'a EthereumBlock> for EthereumBlockPointer {
770797
}
771798
}
772799

800+
impl<'a> From<&'a EthereumBlockType> for EthereumBlockPointer {
801+
fn from(b: &'a EthereumBlockType) -> EthereumBlockPointer {
802+
match b {
803+
EthereumBlockType::Light(block) => EthereumBlockPointer::from(block),
804+
EthereumBlockType::Full(block) => EthereumBlockPointer::from(block),
805+
EthereumBlockType::FullWithReceipts(block) => EthereumBlockPointer::from(block),
806+
}
807+
}
808+
}
809+
773810
impl From<(H256, u64)> for EthereumBlockPointer {
774811
fn from((hash, number): (H256, u64)) -> EthereumBlockPointer {
775812
if number >= (1 << 63) {

graph/src/components/subgraph/host.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub trait RuntimeHost: Send + Sync + Debug + 'static {
2727
async fn process_log(
2828
&self,
2929
logger: &Logger,
30-
block: &Arc<LightEthereumBlock>,
30+
block: &Arc<EthereumBlockType>,
3131
transaction: &Arc<Transaction>,
3232
log: &Arc<Log>,
3333
state: BlockState,
@@ -38,7 +38,7 @@ pub trait RuntimeHost: Send + Sync + Debug + 'static {
3838
async fn process_call(
3939
&self,
4040
logger: &Logger,
41-
block: &Arc<LightEthereumBlock>,
41+
block: &Arc<EthereumBlockType>,
4242
transaction: &Arc<Transaction>,
4343
call: &Arc<EthereumCall>,
4444
state: BlockState,
@@ -49,7 +49,7 @@ pub trait RuntimeHost: Send + Sync + Debug + 'static {
4949
async fn process_block(
5050
&self,
5151
logger: &Logger,
52-
block: &Arc<LightEthereumBlock>,
52+
block: &Arc<EthereumBlockType>,
5353
trigger_type: &EthereumBlockTriggerType,
5454
state: BlockState,
5555
proof_of_indexing: SharedProofOfIndexing,

graph/src/components/subgraph/instance.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub trait SubgraphInstance<H: RuntimeHost> {
3838
async fn process_trigger(
3939
&self,
4040
logger: &Logger,
41-
block: &Arc<LightEthereumBlock>,
41+
block: &Arc<EthereumBlockType>,
4242
trigger: EthereumTrigger,
4343
state: BlockState,
4444
proof_of_indexing: SharedProofOfIndexing,
@@ -48,7 +48,7 @@ pub trait SubgraphInstance<H: RuntimeHost> {
4848
async fn process_trigger_in_runtime_hosts(
4949
logger: &Logger,
5050
hosts: &[Arc<H>],
51-
block: &Arc<LightEthereumBlock>,
51+
block: &Arc<EthereumBlockType>,
5252
trigger: EthereumTrigger,
5353
state: BlockState,
5454
proof_of_indexing: SharedProofOfIndexing,

graph/src/data/subgraph/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,9 @@ pub enum BlockHandlerData {
528528
impl From<EthereumBlockHandlerData> for BlockHandlerData {
529529
fn from(data: EthereumBlockHandlerData) -> Self {
530530
match data {
531-
EthereumBlockHandlerData::FullBlockWithReceipts => BlockHandlerData::FullBlockWithReceipts,
531+
EthereumBlockHandlerData::FullBlockWithReceipts => {
532+
BlockHandlerData::FullBlockWithReceipts
533+
}
532534
EthereumBlockHandlerData::FullBlock => BlockHandlerData::FullBlock,
533535
EthereumBlockHandlerData::Block => BlockHandlerData::Block,
534536
}

graph/src/data/subgraph/schema.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,7 @@ impl TryFromValue for EthereumBlockHandlerFilterEntity {
11141114
pub enum EthereumBlockHandlerData {
11151115
Block,
11161116
FullBlock,
1117-
FullBlockWithReceipts
1117+
FullBlockWithReceipts,
11181118
}
11191119

11201120
impl Default for EthereumBlockHandlerData {

graph/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub mod prelude {
7777
BlockFinality, BlockStream, BlockStreamBuilder, BlockStreamEvent, BlockStreamMetrics,
7878
ChainHeadUpdate, ChainHeadUpdateListener, ChainHeadUpdateStream, EthereumAdapter,
7979
EthereumAdapterError, EthereumBlock, EthereumBlockData, EthereumBlockFilter,
80-
EthereumBlockPointer, EthereumBlockTriggerType, EthereumBlockWithCalls,
80+
EthereumBlockPointer, EthereumBlockTriggerType, EthereumBlockType, EthereumBlockWithCalls,
8181
EthereumBlockWithTriggers, EthereumCall, EthereumCallData, EthereumCallFilter,
8282
EthereumContractCall, EthereumContractCallError, EthereumEventData, EthereumLogFilter,
8383
EthereumNetworkIdentifier, EthereumTransactionData, EthereumTrigger, FullEthereumBlockData,

runtime/wasm/src/host.rs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ impl RuntimeHostTrait for RuntimeHost {
474474
async fn process_call(
475475
&self,
476476
logger: &Logger,
477-
block: &Arc<LightEthereumBlock>,
477+
block: &Arc<EthereumBlockType>,
478478
transaction: &Arc<Transaction>,
479479
call: &Arc<EthereumCall>,
480480
state: BlockState,
@@ -561,7 +561,7 @@ impl RuntimeHostTrait for RuntimeHost {
561561
outputs,
562562
handler: call_handler.clone(),
563563
},
564-
&Arc::new(EthereumBlockType::Light(LightEthereumBlock::from(block.as_ref().clone()))),
564+
block,
565565
proof_of_indexing,
566566
)
567567
.await
@@ -570,37 +570,41 @@ impl RuntimeHostTrait for RuntimeHost {
570570
async fn process_block(
571571
&self,
572572
logger: &Logger,
573-
block: &Arc<LightEthereumBlock>,
573+
block: &Arc<EthereumBlockType>,
574574
trigger_type: &EthereumBlockTriggerType,
575575
state: BlockState,
576576
proof_of_indexing: SharedProofOfIndexing,
577577
) -> Result<BlockState, anyhow::Error> {
578578
let block_handler = self.handler_for_block(trigger_type)?;
579579
let mapping_block: EthereumBlockType = match trigger_type {
580-
EthereumBlockTriggerType::Every(BlockType::FullWithReceipts) => match graph::block_on_allow_panic(
581-
future::lazy(move || {
582-
self.host_exports
583-
.ethereum_adapter
584-
.load_full_block(logger, block.as_ref().clone())
585-
})
586-
.compat(),
587-
) {
588-
Ok(block) => Ok(EthereumBlockType::FullWithReceipts(block)),
589-
Err(e) => Err(anyhow::anyhow!(
590-
"Failed to load full block: {}, error: {}",
591-
&block.number.unwrap().to_string(),
592-
e
593-
)),
594-
}?,
595-
EthereumBlockTriggerType::Every(BlockType::Full) => EthereumBlockType::Full(LightEthereumBlock::from(block.as_ref().clone())),
596-
_ => EthereumBlockType::Light(LightEthereumBlock::from(block.as_ref().clone()))
580+
EthereumBlockTriggerType::Every(BlockType::FullWithReceipts) => {
581+
match graph::block_on_allow_panic(
582+
future::lazy(move || {
583+
self.host_exports
584+
.ethereum_adapter
585+
.load_full_block(logger, block.light_block().clone())
586+
})
587+
.compat(),
588+
) {
589+
Ok(block) => Ok(EthereumBlockType::FullWithReceipts(block)),
590+
Err(e) => Err(anyhow::anyhow!(
591+
"Failed to load full block: {}, error: {}",
592+
&block.number().to_string(),
593+
e
594+
)),
595+
}?
596+
}
597+
EthereumBlockTriggerType::Every(BlockType::Full) => {
598+
EthereumBlockType::Full(block.light_block().clone())
599+
}
600+
_ => block.as_ref().clone(),
597601
};
598602

599603
self.send_mapping_request(
600604
logger,
601605
o! {
602-
"hash" => block.hash.unwrap().to_string(),
603-
"number" => &block.number.unwrap().to_string(),
606+
"hash" => block.hash().to_string(),
607+
"number" => &block.number().to_string(),
604608
},
605609
state,
606610
&block_handler.handler,
@@ -616,7 +620,7 @@ impl RuntimeHostTrait for RuntimeHost {
616620
async fn process_log(
617621
&self,
618622
logger: &Logger,
619-
block: &Arc<LightEthereumBlock>,
623+
block: &Arc<EthereumBlockType>,
620624
transaction: &Arc<Transaction>,
621625
log: &Arc<Log>,
622626
state: BlockState,
@@ -721,7 +725,7 @@ impl RuntimeHostTrait for RuntimeHost {
721725
params,
722726
handler: event_handler.clone(),
723727
},
724-
&Arc::new(EthereumBlockType::Light(block.as_ref()))),
728+
block,
725729
proof_of_indexing,
726730
)
727731
.await

runtime/wasm/src/module/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,9 @@ impl WasmInstance {
166166
// Prepare an Ethereum Block for the WASM runtime
167167
let arg = match block.as_ref() {
168168
EthereumBlockType::FullWithReceipts(block) => self
169-
.asc_new::<AscFullEthereumBlockWithReceipts, _>(&FullEthereumBlockDataWithReceipts::from(block))
169+
.asc_new::<AscFullEthereumBlockWithReceipts, _>(
170+
&FullEthereumBlockDataWithReceipts::from(block),
171+
)
170172
.erase(),
171173
EthereumBlockType::Full(block) => self
172174
.asc_new::<AscFullEthereumBlock, _>(&FullEthereumBlockData::from(block))

runtime/wasm/src/to_from/external.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::collections::HashMap;
33

44
use graph::components::ethereum::{
55
EthereumBlockData, EthereumCallData, EthereumEventData, EthereumTransactionData,
6-
EthereumTransactionReceiptData, FullEthereumBlockData, FullEthereumBlockDataWithReceipts
6+
EthereumTransactionReceiptData, FullEthereumBlockData, FullEthereumBlockDataWithReceipts,
77
};
88
use graph::data::store;
99
use graph::prelude::anyhow::{ensure, Error};

0 commit comments

Comments
 (0)