Skip to content

Commit

Permalink
graph,runtime: Support different block type inputs to call filters
Browse files Browse the repository at this point in the history
  • Loading branch information
fordN committed Jun 29, 2020
1 parent eaf4b8c commit 26b3851
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
10 changes: 7 additions & 3 deletions graph/src/components/ethereum/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,10 @@ fn parse_block_triggers(
.iter()
.filter(move |call| call_filter.matches(call))
.map(move |call| {
EthereumTrigger::Block(block_ptr, EthereumBlockTriggerType::WithCallTo(call.to))
EthereumTrigger::Block(
block_ptr,
EthereumBlockTriggerType::WithCallTo(call.to, block_type),
)
})
.collect::<Vec<EthereumTrigger>>()
});
Expand Down Expand Up @@ -968,13 +971,14 @@ pub fn blocks_with_triggers(
// To determine which blocks include a call to addresses
// in the block filter, transform the `block_filter` into
// a `call_filter` and run `blocks_with_calls`
let block_type = block_filter.block_type.clone();
let call_filter = EthereumCallFilter::from(block_filter);
trigger_futs.push(Box::new(
eth.calls_in_block_range(&logger, subgraph_metrics.clone(), from, to, call_filter)
.map(|call| {
.map(move |call| {
EthereumTrigger::Block(
EthereumBlockPointer::from(&call),
EthereumBlockTriggerType::WithCallTo(call.to),
EthereumBlockTriggerType::WithCallTo(call.to, block_type.clone()),
)
})
.collect(),
Expand Down
4 changes: 2 additions & 2 deletions graph/src/components/ethereum/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ impl Default for BlockType {
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum EthereumBlockTriggerType {
Every(BlockType),
WithCallTo(Address),
WithCallTo(Address, BlockType),
}

impl EthereumTrigger {
Expand Down Expand Up @@ -892,7 +892,7 @@ mod test {
number: 0,
hash: H256::random(),
},
EthereumBlockTriggerType::WithCallTo(Address::random()),
EthereumBlockTriggerType::WithCallTo(Address::random(), BlockType::Light),
);

let mut call1 = EthereumCall::default();
Expand Down
37 changes: 17 additions & 20 deletions runtime/wasm/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ impl RuntimeHost {

fn matches_block_trigger(&self, block_trigger_type: &EthereumBlockTriggerType) -> bool {
let source_address_matches = match block_trigger_type {
EthereumBlockTriggerType::WithCallTo(address) => {
EthereumBlockTriggerType::WithCallTo(address, _block_type) => {
self.data_source_contract
.address
// Do not match if this datasource has no address
Expand Down Expand Up @@ -347,7 +347,7 @@ impl RuntimeHost {
trigger_type: &EthereumBlockTriggerType,
) -> Result<MappingBlockHandler, anyhow::Error> {
match trigger_type {
EthereumBlockTriggerType::Every(_type) => self
EthereumBlockTriggerType::Every(_block_type) => self
.data_source_block_handlers
.iter()
.find(move |handler| handler.filter == None)
Expand All @@ -359,7 +359,7 @@ impl RuntimeHost {
self.data_source_name,
)
}),
EthereumBlockTriggerType::WithCallTo(_address) => self
EthereumBlockTriggerType::WithCallTo(_address, _block_type) => self
.data_source_block_handlers
.iter()
.find(move |handler| {
Expand Down Expand Up @@ -577,23 +577,20 @@ impl RuntimeHostTrait for RuntimeHost {
) -> Result<BlockState, anyhow::Error> {
let block_handler = self.handler_for_block(trigger_type)?;
let mapping_block: EthereumBlockType = match trigger_type {
EthereumBlockTriggerType::Every(BlockType::FullWithReceipts) => {
match graph::block_on_allow_panic(
future::lazy(move || {
self.host_exports
.ethereum_adapter
.load_full_block(logger, block.light_block().clone())
})
.compat(),
) {
Ok(block) => Ok(EthereumBlockType::FullWithReceipts(block)),
Err(e) => Err(anyhow::anyhow!(
"Failed to load full block: {}, error: {}",
&block.number().to_string(),
e
)),
}?
}
EthereumBlockTriggerType::Every(BlockType::FullWithReceipts) => match self
.host_exports
.ethereum_adapter
.load_full_block(logger, block.light_block().clone())
.compat()
.await
{
Ok(block) => Ok(EthereumBlockType::FullWithReceipts(block)),
Err(e) => Err(anyhow::anyhow!(
"Failed to load full block: {}, error: {}",
&block.number().to_string(),
e
)),
}?,
EthereumBlockTriggerType::Every(BlockType::Full) => {
EthereumBlockType::Full(block.light_block().clone())
}
Expand Down

0 comments on commit 26b3851

Please sign in to comment.