Skip to content

Commit ec56d02

Browse files
committed
core,graph,runtime: Create FullBlock struct and pass to mappings
1 parent 98e39bb commit ec56d02

File tree

10 files changed

+381
-24
lines changed

10 files changed

+381
-24
lines changed

graph/src/components/ethereum/adapter.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ impl From<EthereumBlockFilter> for EthereumCallFilter {
411411
pub struct EthereumBlockFilter {
412412
pub contract_addresses: HashSet<(u64, Address)>,
413413
pub trigger_every_block: bool,
414+
pub full_block: bool,
414415
}
415416

416417
impl EthereumBlockFilter {
@@ -435,8 +436,10 @@ impl EthereumBlockFilter {
435436
.into_iter()
436437
.any(|block_handler| block_handler.filter.is_none());
437438

439+
// TODO: process data source to get full_block boolean
438440
filter_opt.extend(Self {
439441
trigger_every_block: has_block_handler_without_filter,
442+
full_block: true,
440443
contract_addresses: if has_block_handler_with_call_filter {
441444
vec![(
442445
data_source.source.start_block,
@@ -816,7 +819,7 @@ fn parse_block_triggers(
816819
if trigger_every_block {
817820
triggers.push(EthereumTrigger::Block(
818821
block_ptr,
819-
EthereumBlockTriggerType::Every,
822+
EthereumBlockTriggerType::Every(BlockType::Full),
820823
));
821824
}
822825
triggers
@@ -921,7 +924,12 @@ pub fn blocks_with_triggers(
921924
.block_range_to_ptrs(logger.clone(), from, to)
922925
.map(move |ptrs| {
923926
ptrs.into_iter()
924-
.map(|ptr| EthereumTrigger::Block(ptr, EthereumBlockTriggerType::Every))
927+
.map(|ptr| {
928+
EthereumTrigger::Block(
929+
ptr,
930+
EthereumBlockTriggerType::Every(BlockType::Full),
931+
)
932+
})
925933
.collect()
926934
}),
927935
))

graph/src/components/ethereum/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ 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, EthereumBlock, EthereumBlockData, EthereumBlockPointer,
16+
EthereumBlockType, BlockFinality, BlockType, EthereumBlock, EthereumBlockData, EthereumBlockPointer,
1717
EthereumBlockTriggerType, EthereumBlockWithCalls, EthereumBlockWithTriggers, EthereumCall,
18-
EthereumCallData, EthereumEventData, EthereumTransactionData, EthereumTrigger,
19-
LightEthereumBlock, LightEthereumBlockExt,
18+
EthereumCallData, EthereumEventData, EthereumTransactionData, EthereumTransactionReceiptData,
19+
EthereumTrigger, FullEthereumBlockData, LightEthereumBlock, LightEthereumBlockExt,
2020
};

graph/src/components/ethereum/types.rs

Lines changed: 221 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize};
33
use stable_hash::prelude::*;
44
use stable_hash::utils::AsBytes;
55
use std::cmp::Ordering;
6+
use std::convert::TryFrom;
67
use std::fmt;
78
use web3::types::*;
89

@@ -56,6 +57,15 @@ impl LightEthereumBlockExt for LightEthereumBlock {
5657
}
5758
}
5859

60+
impl<'a> From<&'a EthereumBlockType> for LightEthereumBlock {
61+
fn from(block: &'a EthereumBlockType) -> LightEthereumBlock {
62+
match block {
63+
EthereumBlockType::Full(block) => block.block.clone(),
64+
EthereumBlockType::Light(block) => block.clone(),
65+
}
66+
}
67+
}
68+
5969
/// This is used in `EthereumAdapter::triggers_in_block`, called when re-processing a block for
6070
/// newly created data sources. This allows the re-processing to be reorg safe without having to
6171
/// always fetch the full block data.
@@ -193,9 +203,37 @@ impl PartialEq for EthereumTrigger {
193203

194204
impl Eq for EthereumTrigger {}
195205

206+
/// This is used in `EthereumAdapter::triggers_in_block`, called when re-processing a block for
207+
/// newly created data sources. This allows the re-processing to be reorg safe without having to
208+
/// always fetch the full block data.
209+
#[derive(Clone, Debug)]
210+
pub enum EthereumBlockType {
211+
Light(LightEthereumBlock),
212+
213+
Full(EthereumBlock),
214+
}
215+
216+
impl Default for EthereumBlockType {
217+
fn default() -> Self {
218+
EthereumBlockType::Light(LightEthereumBlock::default())
219+
}
220+
}
221+
222+
impl<'a> From<&'a LightEthereumBlock> for EthereumBlockType {
223+
fn from(block: &'a LightEthereumBlock) -> EthereumBlockType {
224+
EthereumBlockType::Light(block.clone())
225+
}
226+
}
227+
228+
#[derive(Clone, Debug, PartialEq, Eq)]
229+
pub enum BlockType {
230+
Light,
231+
Full,
232+
}
233+
196234
#[derive(Clone, Debug, PartialEq, Eq)]
197235
pub enum EthereumBlockTriggerType {
198-
Every,
236+
Every(BlockType),
199237
WithCallTo(Address),
200238
}
201239

@@ -263,6 +301,158 @@ impl PartialOrd for EthereumTrigger {
263301
}
264302
}
265303

304+
pub struct EthereumTransactionReceiptData {
305+
// from receipts
306+
pub transaction_hash: H256,
307+
pub transaction_index: Index,
308+
pub cumulative_gas_used: U256,
309+
pub gas_used: Option<U256>,
310+
pub contract_address: Option<H160>,
311+
pub status: Option<U64>,
312+
pub root: Option<H256>,
313+
314+
// from txs
315+
pub from: H160,
316+
pub to: Option<H160>,
317+
pub value: U256,
318+
pub gas_price: U256,
319+
pub gas: U256,
320+
pub input: Bytes,
321+
}
322+
323+
pub struct FullEthereumBlockData {
324+
pub hash: H256,
325+
pub parent_hash: H256,
326+
pub uncles_hash: H256,
327+
pub author: H160,
328+
pub state_root: H256,
329+
pub transactions_root: H256,
330+
pub receipts_root: H256,
331+
pub number: U64,
332+
pub gas_used: U256,
333+
pub gas_limit: U256,
334+
pub timestamp: U256,
335+
pub difficulty: U256,
336+
pub total_difficulty: U256,
337+
pub size: Option<U256>,
338+
pub transaction_receipts: Vec<EthereumTransactionReceiptData>,
339+
}
340+
341+
impl<'a> TryFrom<&'a EthereumBlockType> for FullEthereumBlockData {
342+
type Error = String;
343+
344+
fn try_from(block: &'a EthereumBlockType) -> Result<FullEthereumBlockData, Self::Error> {
345+
let fullblock = match block {
346+
EthereumBlockType::Full(full_block) => full_block,
347+
EthereumBlockType::Light(_) => return Err(format!(
348+
"Failed to convert EthereumBlockType to FUllEthereumBlockData, requires a EthereumBlockType::Full()"
349+
))
350+
};
351+
let block = &fullblock.block;
352+
let transaction_receipts_data = block
353+
.transactions
354+
.iter()
355+
.cloned()
356+
.zip(fullblock.transaction_receipts.iter().cloned())
357+
.map(|transaction_and_receipt| {
358+
assert_eq!(
359+
transaction_and_receipt.0.hash,
360+
transaction_and_receipt.1.transaction_hash
361+
);
362+
EthereumTransactionReceiptData {
363+
transaction_hash: transaction_and_receipt.0.hash,
364+
transaction_index: transaction_and_receipt.1.transaction_index,
365+
cumulative_gas_used: transaction_and_receipt.1.cumulative_gas_used,
366+
gas_used: transaction_and_receipt.1.gas_used,
367+
contract_address: transaction_and_receipt.1.contract_address,
368+
status: transaction_and_receipt.1.status,
369+
root: transaction_and_receipt.1.root,
370+
371+
// from txs
372+
from: transaction_and_receipt.0.from,
373+
to: transaction_and_receipt.0.to,
374+
value: transaction_and_receipt.0.value,
375+
gas_price: transaction_and_receipt.0.gas_price,
376+
gas: transaction_and_receipt.0.gas,
377+
input: transaction_and_receipt.0.input,
378+
}
379+
})
380+
.collect::<Vec<EthereumTransactionReceiptData>>();
381+
382+
Ok(FullEthereumBlockData {
383+
hash: block.hash.unwrap(),
384+
parent_hash: block.parent_hash,
385+
uncles_hash: block.uncles_hash,
386+
author: block.author,
387+
state_root: block.state_root,
388+
transactions_root: block.transactions_root,
389+
receipts_root: block.receipts_root,
390+
number: block.number.unwrap(),
391+
gas_used: block.gas_used,
392+
gas_limit: block.gas_limit,
393+
timestamp: block.timestamp,
394+
difficulty: block.difficulty,
395+
total_difficulty: block.total_difficulty.unwrap_or_default(),
396+
size: block.size,
397+
transaction_receipts: transaction_receipts_data,
398+
})
399+
}
400+
}
401+
402+
impl<'a> From<&'a EthereumBlock> for FullEthereumBlockData {
403+
fn from(block: &'a EthereumBlock) -> FullEthereumBlockData {
404+
let transaction_receipts_data = block
405+
.block
406+
.transactions
407+
.iter()
408+
.cloned()
409+
.zip(block.transaction_receipts.iter().cloned())
410+
.map(|transaction_and_receipt| {
411+
assert_eq!(
412+
transaction_and_receipt.0.hash,
413+
transaction_and_receipt.1.transaction_hash
414+
);
415+
EthereumTransactionReceiptData {
416+
transaction_hash: transaction_and_receipt.0.hash,
417+
transaction_index: transaction_and_receipt.1.transaction_index,
418+
cumulative_gas_used: transaction_and_receipt.1.cumulative_gas_used,
419+
gas_used: transaction_and_receipt.1.gas_used,
420+
contract_address: transaction_and_receipt.1.contract_address,
421+
status: transaction_and_receipt.1.status,
422+
root: transaction_and_receipt.1.root,
423+
424+
// from txs
425+
from: transaction_and_receipt.0.from,
426+
to: transaction_and_receipt.0.to,
427+
value: transaction_and_receipt.0.value,
428+
gas_price: transaction_and_receipt.0.gas_price,
429+
gas: transaction_and_receipt.0.gas,
430+
input: transaction_and_receipt.0.input,
431+
}
432+
})
433+
.collect::<Vec<EthereumTransactionReceiptData>>();
434+
let block = &block.block;
435+
436+
FullEthereumBlockData {
437+
hash: block.hash.unwrap(),
438+
parent_hash: block.parent_hash,
439+
uncles_hash: block.uncles_hash,
440+
author: block.author,
441+
state_root: block.state_root,
442+
transactions_root: block.transactions_root,
443+
receipts_root: block.receipts_root,
444+
number: block.number.unwrap(),
445+
gas_used: block.gas_used,
446+
gas_limit: block.gas_limit,
447+
timestamp: block.timestamp,
448+
difficulty: block.difficulty,
449+
total_difficulty: block.total_difficulty.unwrap_or_default(),
450+
size: block.size,
451+
transaction_receipts: transaction_receipts_data,
452+
}
453+
}
454+
}
455+
266456
/// Ethereum block data.
267457
#[derive(Clone, Debug, Default)]
268458
pub struct EthereumBlockData {
@@ -303,6 +493,32 @@ impl<'a, T> From<&'a Block<T>> for EthereumBlockData {
303493
}
304494
}
305495

496+
impl<'a> From<&'a EthereumBlockType> for EthereumBlockData {
497+
fn from(block: &'a EthereumBlockType) -> EthereumBlockData {
498+
let block = match block {
499+
EthereumBlockType::Full(full_block) => &full_block.block,
500+
EthereumBlockType::Light(light_block) => light_block,
501+
};
502+
503+
EthereumBlockData {
504+
hash: block.hash.unwrap(),
505+
parent_hash: block.parent_hash,
506+
uncles_hash: block.uncles_hash,
507+
author: block.author,
508+
state_root: block.state_root,
509+
transactions_root: block.transactions_root,
510+
receipts_root: block.receipts_root,
511+
number: block.number.unwrap(),
512+
gas_used: block.gas_used,
513+
gas_limit: block.gas_limit,
514+
timestamp: block.timestamp,
515+
difficulty: block.difficulty,
516+
total_difficulty: block.total_difficulty.unwrap_or_default(),
517+
size: block.size,
518+
}
519+
}
520+
}
521+
306522
/// Ethereum transaction data.
307523
#[derive(Clone, Debug)]
308524
pub struct EthereumTransactionData {
@@ -536,7 +752,9 @@ impl ToEntityKey for EthereumBlockPointer {
536752

537753
#[cfg(test)]
538754
mod test {
539-
use super::{EthereumBlockPointer, EthereumBlockTriggerType, EthereumCall, EthereumTrigger};
755+
use super::{
756+
BlockType, EthereumBlockPointer, EthereumBlockTriggerType, EthereumCall, EthereumTrigger,
757+
};
540758
use web3::types::*;
541759

542760
#[test]
@@ -546,7 +764,7 @@ mod test {
546764
number: 1,
547765
hash: H256::random(),
548766
},
549-
EthereumBlockTriggerType::Every,
767+
EthereumBlockTriggerType::Every(BlockType::Light),
550768
);
551769

552770
let block2 = EthereumTrigger::Block(

graph/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ pub mod prelude {
8080
EthereumBlockPointer, EthereumBlockTriggerType, EthereumBlockWithCalls,
8181
EthereumBlockWithTriggers, EthereumCall, EthereumCallData, EthereumCallFilter,
8282
EthereumContractCall, EthereumContractCallError, EthereumEventData, EthereumLogFilter,
83-
EthereumNetworkIdentifier, EthereumTransactionData, EthereumTrigger, LightEthereumBlock,
84-
LightEthereumBlockExt, ProviderEthRpcMetrics, SubgraphEthRpcMetrics,
83+
EthereumNetworkIdentifier, EthereumTransactionData, EthereumTrigger, FullEthereumBlockData,
84+
LightEthereumBlock, LightEthereumBlockExt, ProviderEthRpcMetrics, SubgraphEthRpcMetrics,
8585
};
8686
pub use crate::components::graphql::{
8787
GraphQlRunner, QueryResultFuture, SubscriptionResultFuture,

runtime/wasm/src/asc_abi/class.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,26 @@ pub(crate) struct AscEthereumBlock {
418418
pub size: AscPtr<AscBigInt>,
419419
}
420420

421+
#[repr(C)]
422+
#[derive(AscType)]
423+
pub(crate) struct AscFullEthereumBlock {
424+
pub hash: AscPtr<AscH256>,
425+
pub parent_hash: AscPtr<AscH256>,
426+
pub uncles_hash: AscPtr<AscH256>,
427+
pub author: AscPtr<AscH160>,
428+
pub state_root: AscPtr<AscH256>,
429+
pub transactions_root: AscPtr<AscH256>,
430+
pub receipts_root: AscPtr<AscH256>,
431+
pub number: AscPtr<AscBigInt>,
432+
pub gas_used: AscPtr<AscBigInt>,
433+
pub gas_limit: AscPtr<AscBigInt>,
434+
pub timestamp: AscPtr<AscBigInt>,
435+
pub difficulty: AscPtr<AscBigInt>,
436+
pub total_difficulty: AscPtr<AscBigInt>,
437+
pub size: AscPtr<AscBigInt>,
438+
pub transaction_receipts: AscPtr<Array<AscPtr<AscEthereumTransactionReceipt>>>,
439+
}
440+
421441
#[repr(C)]
422442
#[derive(AscType)]
423443
pub(crate) struct AscEthereumTransaction {
@@ -430,6 +450,25 @@ pub(crate) struct AscEthereumTransaction {
430450
pub gas_price: AscPtr<AscBigInt>,
431451
}
432452

453+
#[repr(C)]
454+
#[derive(AscType)]
455+
pub(crate) struct AscEthereumTransactionReceipt {
456+
pub transaction_hash: AscPtr<AscH256>,
457+
pub transaction_index: AscPtr<AscBigInt>,
458+
pub cumulative_gas_used: AscPtr<AscBigInt>,
459+
pub gas_used: AscPtr<AscBigInt>,
460+
pub contract_address: AscPtr<AscH160>,
461+
pub status: AscPtr<AscBigInt>,
462+
pub root: AscPtr<AscH256>,
463+
464+
pub from: AscPtr<AscH160>,
465+
pub to: AscPtr<AscH160>,
466+
pub value: AscPtr<AscBigInt>,
467+
pub gas_price: AscPtr<AscBigInt>,
468+
pub gas: AscPtr<AscBigInt>,
469+
pub input: AscPtr<AscBigInt>,
470+
}
471+
433472
#[repr(C)]
434473
#[derive(AscType)]
435474
pub(crate) struct AscEthereumTransaction_0_0_2 {

0 commit comments

Comments
 (0)