Skip to content

Commit

Permalink
graph, runtime: Add new MappingEntityTrigger struct that takes declar…
Browse files Browse the repository at this point in the history
…ed calls and wire it in match_and_decode
  • Loading branch information
incrypto32 authored and zorancv committed Dec 18, 2024
1 parent 13b0c11 commit 84e9b8a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 19 deletions.
4 changes: 2 additions & 2 deletions graph/src/data_source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ impl<C: Blockchain> DataSource<C> {
Ok(ds.match_and_decode(trigger))
}
(Self::Subgraph(ds), TriggerData::Subgraph(trigger)) => {
Ok(ds.match_and_decode(block, trigger))
ds.match_and_decode(block, trigger)
}
(Self::Onchain(_), TriggerData::Offchain(_))
| (Self::Offchain(_), TriggerData::Onchain(_))
Expand Down Expand Up @@ -573,7 +573,7 @@ impl<C: Blockchain> TriggerData<C> {
pub enum MappingTrigger<C: Blockchain> {
Onchain(C::MappingTrigger),
Offchain(offchain::TriggerData),
Subgraph(subgraph::TriggerData),
Subgraph(subgraph::MappingEntityTrigger),
}

impl<C: Blockchain> MappingTrigger<C> {
Expand Down
63 changes: 46 additions & 17 deletions graph/src/data_source/subgraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ use crate::{
subgraph::{calls_host_fn, SPEC_VERSION_1_3_0},
value::Word,
},
data_source,
data_source::{self, common::DeclaredCall},
ensure,
prelude::{CheapClone, DataSourceContext, DeploymentHash, Link},
};
use anyhow::{anyhow, Context, Error};
use anyhow::{anyhow, Context, Error, Result};
use futures03::{stream::FuturesOrdered, TryStreamExt};
use serde::Deserialize;
use slog::{info, Logger};
use std::{fmt, sync::Arc};

use super::{
common::{FindMappingABI, MappingABI, UnresolvedMappingABI},
common::{CallDecls, FindMappingABI, MappingABI, UnresolvedMappingABI},
DataSourceTemplateInfo, TriggerWithHandler,
};

Expand Down Expand Up @@ -74,25 +75,45 @@ impl DataSource {
&self,
block: &Arc<C::Block>,
trigger: &TriggerData,
) -> Option<TriggerWithHandler<super::MappingTrigger<C>>> {
) -> Result<Option<TriggerWithHandler<super::MappingTrigger<C>>>> {
if self.source.address != trigger.source {
return None;
return Ok(None);
}

let trigger_ref = self.mapping.handlers.iter().find_map(|handler| {
if handler.entity != trigger.entity_type() {
return None;
}
let mut matching_handlers: Vec<_> = self
.mapping
.handlers
.iter()
.filter(|handler| handler.entity == trigger.entity_type())
.collect();

Some(TriggerWithHandler::new(
data_source::MappingTrigger::Subgraph(trigger.clone()),
handler.handler.clone(),
block.ptr(),
block.timestamp(),
))
});
// Get the matching handler if any
let handler = match matching_handlers.pop() {
Some(handler) => handler,
None => return Ok(None),
};

ensure!(
matching_handlers.is_empty(),
format!(
"Multiple handlers defined for entity `{}`, only one is supported",
trigger.entity_type()
)
);

return trigger_ref;
let calls =
DeclaredCall::from_entity_trigger(&self.mapping, &handler.calls, &trigger.entity)?;
let mapping_trigger = MappingEntityTrigger {
data: trigger.clone(),
calls,
};

Ok(Some(TriggerWithHandler::new(
data_source::MappingTrigger::Subgraph(mapping_trigger),
handler.handler.clone(),
block.ptr(),
block.timestamp(),
)))
}

pub fn address(&self) -> Option<Vec<u8>> {
Expand Down Expand Up @@ -157,6 +178,8 @@ impl FindMappingABI for Mapping {
pub struct EntityHandler {
pub handler: String,
pub entity: String,
#[serde(default)]
pub calls: CallDecls,
}

#[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize)]
Expand Down Expand Up @@ -321,6 +344,12 @@ impl UnresolvedDataSourceTemplate {
}
}

#[derive(Clone, PartialEq, Debug)]
pub struct MappingEntityTrigger {
pub data: TriggerData,
pub calls: Vec<DeclaredCall>,
}

#[derive(Clone, PartialEq, Eq)]
pub struct TriggerData {
pub source: DeploymentHash,
Expand Down
10 changes: 10 additions & 0 deletions runtime/wasm/src/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ impl ToAscPtr for subgraph::TriggerData {
}
}

impl ToAscPtr for subgraph::MappingEntityTrigger {
fn to_asc_ptr<H: AscHeap>(
self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscPtr<()>, HostExportError> {
asc_new(heap, &self.data.entity, gas).map(|ptr| ptr.erase())
}
}

impl<C: Blockchain> ToAscPtr for MappingTrigger<C>
where
C::MappingTrigger: ToAscPtr,
Expand Down

0 comments on commit 84e9b8a

Please sign in to comment.