Skip to content

Commit 1fd185a

Browse files
committed
graph,runtime: Support three data input structs for blockhandlers
1 parent 8258b27 commit 1fd185a

File tree

9 files changed

+146
-54
lines changed

9 files changed

+146
-54
lines changed

graph/src/components/ethereum/adapter.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ impl From<EthereumBlockFilter> for EthereumCallFilter {
412412
pub struct EthereumBlockFilter {
413413
pub contract_addresses: HashSet<(u64, Address)>,
414414
pub trigger_every_block: bool,
415-
pub full_block: bool,
415+
pub block_type: BlockType,
416416
}
417417

418418
impl EthereumBlockFilter {
@@ -435,18 +435,32 @@ impl EthereumBlockFilter {
435435
.iter()
436436
.any(|block_handler| block_handler.filter.is_none());
437437

438-
let has_block_handler_with_fullblock = data_source
438+
let block_type = if data_source
439+
.mapping
440+
.block_handlers
441+
.iter()
442+
.any(|block_handler| match block_handler.input {
443+
BlockHandlerData::FullBlockWithReceipts => return true,
444+
_ => return false,
445+
}) {
446+
BlockType::FullWithReceipts
447+
} else if data_source
439448
.mapping
440449
.block_handlers
441450
.iter()
442451
.any(|block_handler| match block_handler.input {
443452
BlockHandlerData::FullBlock => return true,
444453
_ => return false,
445-
});
454+
})
455+
{
456+
BlockType::Full
457+
} else {
458+
BlockType::Light
459+
};
446460

447461
filter_opt.extend(Self {
448462
trigger_every_block: has_block_handler_without_filter,
449-
full_block: has_block_handler_with_fullblock,
463+
block_type,
450464
contract_addresses: if has_block_handler_with_call_filter {
451465
vec![(
452466
data_source.source.start_block,
@@ -813,10 +827,7 @@ fn parse_block_triggers(
813827
) -> Vec<EthereumTrigger> {
814828
let block_ptr = EthereumBlockPointer::from(&block.ethereum_block);
815829
let trigger_every_block = block_filter.trigger_every_block;
816-
let block_type = match block_filter.full_block {
817-
true => BlockType::Full,
818-
false => BlockType::Light,
819-
};
830+
let block_type = block_filter.block_type;
820831
let call_filter = EthereumCallFilter::from(block_filter);
821832
let mut triggers = block.calls.as_ref().map_or(vec![], |calls| {
822833
calls
@@ -938,9 +949,7 @@ pub fn blocks_with_triggers(
938949
.map(|ptr| {
939950
EthereumTrigger::Block(
940951
ptr,
941-
EthereumBlockTriggerType::Every(BlockType::from(
942-
block_filter.full_block,
943-
)),
952+
EthereumBlockTriggerType::Every(block_filter.block_type),
944953
)
945954
})
946955
.collect()

graph/src/components/ethereum/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ 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, EthereumBlockPointer,
16+
BlockFinality, BlockType, EthereumBlock, EthereumBlockData, FullEthereumBlockDataWithReceipts, EthereumBlockPointer,
1717
EthereumBlockTriggerType, EthereumBlockType, EthereumBlockWithCalls, EthereumBlockWithTriggers,
1818
EthereumCall, EthereumCallData, EthereumEventData, EthereumTransactionData,
1919
EthereumTransactionReceiptData, EthereumTrigger, FullEthereumBlockData, LightEthereumBlock,

graph/src/components/ethereum/types.rs

Lines changed: 101 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ impl LightEthereumBlockExt for LightEthereumBlock {
6060
impl<'a> From<&'a EthereumBlockType> for LightEthereumBlock {
6161
fn from(block: &'a EthereumBlockType) -> LightEthereumBlock {
6262
match block {
63-
EthereumBlockType::Full(block) => block.block.clone(),
63+
EthereumBlockType::FullWithReceipts(block) => block.block.clone(),
64+
EthereumBlockType::Full(block) => block.clone(),
6465
EthereumBlockType::Light(block) => block.clone(),
6566
}
6667
}
@@ -207,7 +208,9 @@ impl Eq for EthereumTrigger {}
207208
pub enum EthereumBlockType {
208209
Light(LightEthereumBlock),
209210

210-
Full(EthereumBlock),
211+
Full(LightEthereumBlock),
212+
213+
FullWithReceipts(EthereumBlock),
211214
}
212215

213216
impl Default for EthereumBlockType {
@@ -216,16 +219,17 @@ impl Default for EthereumBlockType {
216219
}
217220
}
218221

219-
impl<'a> From<&'a LightEthereumBlock> for EthereumBlockType {
220-
fn from(block: &'a LightEthereumBlock) -> EthereumBlockType {
221-
EthereumBlockType::Light(block.clone())
222-
}
223-
}
224-
225-
#[derive(Clone, Debug, PartialEq, Eq)]
222+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
226223
pub enum BlockType {
227224
Light,
228225
Full,
226+
FullWithReceipts,
227+
}
228+
229+
impl Default for BlockType {
230+
fn default() -> BlockType {
231+
BlockType::Light
232+
}
229233
}
230234

231235
#[derive(Clone, Debug, PartialEq, Eq)]
@@ -317,7 +321,7 @@ pub struct EthereumTransactionReceiptData {
317321
pub input: Bytes,
318322
}
319323

320-
pub struct FullEthereumBlockData {
324+
pub struct FullEthereumBlockDataWithReceipts {
321325
pub hash: H256,
322326
pub parent_hash: H256,
323327
pub uncles_hash: H256,
@@ -335,14 +339,17 @@ pub struct FullEthereumBlockData {
335339
pub transaction_receipts: Vec<EthereumTransactionReceiptData>,
336340
}
337341

338-
impl<'a> TryFrom<&'a EthereumBlockType> for FullEthereumBlockData {
342+
impl<'a> TryFrom<&'a EthereumBlockType> for FullEthereumBlockDataWithReceipts {
339343
type Error = String;
340344

341-
fn try_from(block: &'a EthereumBlockType) -> Result<FullEthereumBlockData, Self::Error> {
345+
fn try_from(block: &'a EthereumBlockType) -> Result<FullEthereumBlockDataWithReceipts, Self::Error> {
342346
let fullblock = match block {
343-
EthereumBlockType::Full(full_block) => full_block,
347+
EthereumBlockType::FullWithReceipts(full_block) => full_block,
348+
EthereumBlockType::Full(_) => return Err(format!(
349+
"Failed to convert EthereumBlockType to FullEthereumBlockDataWithReceipts, requires a EthereumBlockType::FullWithReceipts()"
350+
)),
344351
EthereumBlockType::Light(_) => return Err(format!(
345-
"Failed to convert EthereumBlockType to FUllEthereumBlockData, requires a EthereumBlockType::Full()"
352+
"Failed to convert EthereumBlockType to FullEthereumBlockDataWithReceipts, requires a EthereumBlockType::FullWithReceipts()"
346353
))
347354
};
348355
let block = &fullblock.block;
@@ -376,7 +383,7 @@ impl<'a> TryFrom<&'a EthereumBlockType> for FullEthereumBlockData {
376383
})
377384
.collect::<Vec<EthereumTransactionReceiptData>>();
378385

379-
Ok(FullEthereumBlockData {
386+
Ok(FullEthereumBlockDataWithReceipts {
380387
hash: block.hash.unwrap(),
381388
parent_hash: block.parent_hash,
382389
uncles_hash: block.uncles_hash,
@@ -396,8 +403,8 @@ impl<'a> TryFrom<&'a EthereumBlockType> for FullEthereumBlockData {
396403
}
397404
}
398405

399-
impl<'a> From<&'a EthereumBlock> for FullEthereumBlockData {
400-
fn from(block: &'a EthereumBlock) -> FullEthereumBlockData {
406+
impl<'a> From<&'a EthereumBlock> for FullEthereumBlockDataWithReceipts {
407+
fn from(block: &'a EthereumBlock) -> FullEthereumBlockDataWithReceipts {
401408
let transaction_receipts_data = block
402409
.block
403410
.transactions
@@ -430,7 +437,7 @@ impl<'a> From<&'a EthereumBlock> for FullEthereumBlockData {
430437
.collect::<Vec<EthereumTransactionReceiptData>>();
431438
let block = &block.block;
432439

433-
FullEthereumBlockData {
440+
FullEthereumBlockDataWithReceipts {
434441
hash: block.hash.unwrap(),
435442
parent_hash: block.parent_hash,
436443
uncles_hash: block.uncles_hash,
@@ -452,7 +459,7 @@ impl<'a> From<&'a EthereumBlock> for FullEthereumBlockData {
452459

453460
/// Ethereum block data.
454461
#[derive(Clone, Debug, Default)]
455-
pub struct EthereumBlockData {
462+
pub struct FullEthereumBlockData {
456463
pub hash: H256,
457464
pub parent_hash: H256,
458465
pub uncles_hash: H256,
@@ -470,9 +477,9 @@ pub struct EthereumBlockData {
470477
pub transactions: Vec<EthereumTransactionData>,
471478
}
472479

473-
impl<'a> From<&'a LightEthereumBlock> for EthereumBlockData {
474-
fn from(block: &'a LightEthereumBlock) -> EthereumBlockData {
475-
EthereumBlockData {
480+
impl<'a> From<&'a LightEthereumBlock> for FullEthereumBlockData {
481+
fn from(block: &'a LightEthereumBlock) -> FullEthereumBlockData {
482+
FullEthereumBlockData {
476483
hash: block.hash.unwrap(),
477484
parent_hash: block.parent_hash,
478485
uncles_hash: block.uncles_hash,
@@ -496,14 +503,15 @@ impl<'a> From<&'a LightEthereumBlock> for EthereumBlockData {
496503
}
497504
}
498505

499-
impl<'a> From<&'a EthereumBlockType> for EthereumBlockData {
500-
fn from(block: &'a EthereumBlockType) -> EthereumBlockData {
506+
impl<'a> From<&'a EthereumBlockType> for FullEthereumBlockData {
507+
fn from(block: &'a EthereumBlockType) -> FullEthereumBlockData {
501508
let block = match block {
502-
EthereumBlockType::Full(full_block) => &full_block.block,
509+
EthereumBlockType::FullWithReceipts(full_block) => &full_block.block,
510+
EthereumBlockType::Full(block) => &block,
503511
EthereumBlockType::Light(light_block) => light_block,
504512
};
505513

506-
EthereumBlockData {
514+
FullEthereumBlockData {
507515
hash: block.hash.unwrap(),
508516
parent_hash: block.parent_hash,
509517
uncles_hash: block.uncles_hash,
@@ -527,6 +535,73 @@ impl<'a> From<&'a EthereumBlockType> for EthereumBlockData {
527535
}
528536
}
529537

538+
/// Ethereum block data.
539+
#[derive(Clone, Debug, Default)]
540+
pub struct EthereumBlockData {
541+
pub hash: H256,
542+
pub parent_hash: H256,
543+
pub uncles_hash: H256,
544+
pub author: H160,
545+
pub state_root: H256,
546+
pub transactions_root: H256,
547+
pub receipts_root: H256,
548+
pub number: U64,
549+
pub gas_used: U256,
550+
pub gas_limit: U256,
551+
pub timestamp: U256,
552+
pub difficulty: U256,
553+
pub total_difficulty: U256,
554+
pub size: Option<U256>,
555+
}
556+
557+
impl<'a> From<&'a LightEthereumBlock> for EthereumBlockData {
558+
fn from(block: &'a LightEthereumBlock) -> EthereumBlockData {
559+
EthereumBlockData {
560+
hash: block.hash.unwrap(),
561+
parent_hash: block.parent_hash,
562+
uncles_hash: block.uncles_hash,
563+
author: block.author,
564+
state_root: block.state_root,
565+
transactions_root: block.transactions_root,
566+
receipts_root: block.receipts_root,
567+
number: block.number.unwrap(),
568+
gas_used: block.gas_used,
569+
gas_limit: block.gas_limit,
570+
timestamp: block.timestamp,
571+
difficulty: block.difficulty,
572+
total_difficulty: block.total_difficulty.unwrap_or_default(),
573+
size: block.size,
574+
}
575+
}
576+
}
577+
578+
impl<'a> From<&'a EthereumBlockType> for EthereumBlockData {
579+
fn from(block: &'a EthereumBlockType) -> EthereumBlockData {
580+
let block = match block {
581+
EthereumBlockType::FullWithReceipts(full_block) => &full_block.block,
582+
EthereumBlockType::Full(full_block) => &full_block,
583+
EthereumBlockType::Light(light_block) => light_block,
584+
};
585+
586+
EthereumBlockData {
587+
hash: block.hash.unwrap(),
588+
parent_hash: block.parent_hash,
589+
uncles_hash: block.uncles_hash,
590+
author: block.author,
591+
state_root: block.state_root,
592+
transactions_root: block.transactions_root,
593+
receipts_root: block.receipts_root,
594+
number: block.number.unwrap(),
595+
gas_used: block.gas_used,
596+
gas_limit: block.gas_limit,
597+
timestamp: block.timestamp,
598+
difficulty: block.difficulty,
599+
total_difficulty: block.total_difficulty.unwrap_or_default(),
600+
size: block.size,
601+
}
602+
}
603+
}
604+
530605
/// Ethereum transaction data.
531606
#[derive(Clone, Debug)]
532607
pub struct EthereumTransactionData {

graph/src/data/subgraph/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,13 +522,15 @@ pub struct MappingBlockHandler {
522522
pub enum BlockHandlerData {
523523
Block,
524524
FullBlock,
525+
FullBlockWithReceipts,
525526
}
526527

527528
impl From<EthereumBlockHandlerData> for BlockHandlerData {
528529
fn from(data: EthereumBlockHandlerData) -> Self {
529530
match data {
531+
EthereumBlockHandlerData::FullBlockWithReceipts => BlockHandlerData::FullBlockWithReceipts,
530532
EthereumBlockHandlerData::FullBlock => BlockHandlerData::FullBlock,
531-
_ => BlockHandlerData::Block,
533+
EthereumBlockHandlerData::Block => BlockHandlerData::Block,
532534
}
533535
}
534536
}

graph/src/data/subgraph/schema.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,7 @@ impl TryFromValue for EthereumBlockHandlerFilterEntity {
11141114
pub enum EthereumBlockHandlerData {
11151115
Block,
11161116
FullBlock,
1117+
FullBlockWithReceipts
11171118
}
11181119

11191120
impl Default for EthereumBlockHandlerData {
@@ -1140,6 +1141,7 @@ impl FromStr for EthereumBlockHandlerData {
11401141
impl From<EthereumBlockHandlerData> for String {
11411142
fn from(data: EthereumBlockHandlerData) -> String {
11421143
match data {
1144+
EthereumBlockHandlerData::FullBlockWithReceipts => "FullBlockWithReceipts".into(),
11431145
EthereumBlockHandlerData::FullBlock => "FullBlock".into(),
11441146
EthereumBlockHandlerData::Block => "Block".into(),
11451147
}

runtime/wasm/src/asc_abi/class.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ pub(crate) struct AscEthereumBlock {
420420

421421
#[repr(C)]
422422
#[derive(AscType)]
423-
pub(crate) struct AscEthereumBlock_0_0_5 {
423+
pub(crate) struct AscFullEthereumBlock {
424424
pub hash: AscPtr<AscH256>,
425425
pub parent_hash: AscPtr<AscH256>,
426426
pub uncles_hash: AscPtr<AscH256>,
@@ -440,7 +440,7 @@ pub(crate) struct AscEthereumBlock_0_0_5 {
440440

441441
#[repr(C)]
442442
#[derive(AscType)]
443-
pub(crate) struct AscFullEthereumBlock {
443+
pub(crate) struct AscFullEthereumBlockWithReceipts {
444444
pub hash: AscPtr<AscH256>,
445445
pub parent_hash: AscPtr<AscH256>,
446446
pub uncles_hash: AscPtr<AscH256>,

runtime/wasm/src/host.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ impl RuntimeHostTrait for RuntimeHost {
561561
outputs,
562562
handler: call_handler.clone(),
563563
},
564-
&Arc::new(EthereumBlockType::from(block.as_ref())),
564+
&Arc::new(EthereumBlockType::Light(LightEthereumBlock::from(block.as_ref().clone()))),
565565
proof_of_indexing,
566566
)
567567
.await
@@ -577,22 +577,23 @@ impl RuntimeHostTrait for RuntimeHost {
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::Full) => match graph::block_on_allow_panic(
580+
EthereumBlockTriggerType::Every(BlockType::FullWithReceipts) => match graph::block_on_allow_panic(
581581
future::lazy(move || {
582582
self.host_exports
583583
.ethereum_adapter
584584
.load_full_block(logger, block.as_ref().clone())
585585
})
586586
.compat(),
587587
) {
588-
Ok(block) => Ok(EthereumBlockType::Full(block)),
588+
Ok(block) => Ok(EthereumBlockType::FullWithReceipts(block)),
589589
Err(e) => Err(anyhow::anyhow!(
590590
"Failed to load full block: {}, error: {}",
591591
&block.number.unwrap().to_string(),
592592
e
593593
)),
594594
}?,
595-
_ => EthereumBlockType::from(block.as_ref()),
595+
EthereumBlockTriggerType::Every(BlockType::Full) => EthereumBlockType::Full(LightEthereumBlock::from(block.as_ref().clone())),
596+
_ => EthereumBlockType::Light(LightEthereumBlock::from(block.as_ref().clone()))
596597
};
597598

598599
self.send_mapping_request(
@@ -720,7 +721,7 @@ impl RuntimeHostTrait for RuntimeHost {
720721
params,
721722
handler: event_handler.clone(),
722723
},
723-
&Arc::new(EthereumBlockType::from(block.as_ref())),
724+
&Arc::new(EthereumBlockType::Light(block.as_ref()))),
724725
proof_of_indexing,
725726
)
726727
.await

0 commit comments

Comments
 (0)