-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathtrigger_processor.rs
96 lines (88 loc) · 2.88 KB
/
trigger_processor.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use std::sync::Arc;
use async_trait::async_trait;
use slog::Logger;
use crate::{
blockchain::Blockchain,
data_source::{MappingTrigger, TriggerData, TriggerWithHandler},
prelude::SubgraphInstanceMetrics,
};
use super::{
store::SubgraphFork,
subgraph::{BlockState, MappingError, RuntimeHost, RuntimeHostBuilder, SharedProofOfIndexing},
};
/// A trigger that is almost ready to run: we have a host to run it on, and
/// transformed the `TriggerData` into a `MappingTrigger`.
pub struct HostedTrigger<'a, C>
where
C: Blockchain,
{
pub host: &'a dyn RuntimeHost<C>,
pub mapping_trigger: TriggerWithHandler<MappingTrigger<C>>,
}
/// The `TriggerData` and the `HostedTriggers` that were derived from it. We
/// need to hang on to the `TriggerData` solely for error reporting.
pub struct RunnableTriggers<'a, C>
where
C: Blockchain,
{
pub trigger: TriggerData<C>,
pub hosted_triggers: Vec<HostedTrigger<'a, C>>,
}
#[async_trait]
pub trait TriggerProcessor<C, T>: Sync + Send
where
C: Blockchain,
T: RuntimeHostBuilder<C>,
{
async fn process_trigger<'a>(
&'a self,
logger: &Logger,
triggers: Vec<HostedTrigger<'a, C>>,
block: &Arc<C::Block>,
mut state: BlockState,
proof_of_indexing: &SharedProofOfIndexing,
causality_region: &str,
debug_fork: &Option<Arc<dyn SubgraphFork>>,
subgraph_metrics: &Arc<SubgraphInstanceMetrics>,
instrument: bool,
) -> Result<BlockState, MappingError>;
}
/// A trait for taking triggers as `TriggerData` (usually from the block
/// stream) and turning them into `HostedTrigger`s that are ready to run.
///
/// The output triggers will be run in the order in which they are returned.
pub trait Decoder<C, T>: Sync + Send
where
C: Blockchain,
T: RuntimeHostBuilder<C>,
{
fn match_and_decode<'a>(
&'a self,
logger: &Logger,
block: &Arc<C::Block>,
trigger: TriggerData<C>,
hosts: Box<dyn Iterator<Item = &'a T::Host> + Send + 'a>,
subgraph_metrics: &Arc<SubgraphInstanceMetrics>,
) -> Result<RunnableTriggers<'a, C>, MappingError>;
fn match_and_decode_many<'a, F>(
&'a self,
logger: &Logger,
block: &Arc<C::Block>,
triggers: Box<dyn Iterator<Item = TriggerData<C>>>,
hosts_filter: F,
subgraph_metrics: &Arc<SubgraphInstanceMetrics>,
) -> Result<Vec<RunnableTriggers<'a, C>>, MappingError>
where
F: Fn(&TriggerData<C>) -> Box<dyn Iterator<Item = &'a T::Host> + Send + 'a>,
{
let mut runnables = vec![];
for trigger in triggers {
let hosts = hosts_filter(&trigger);
match self.match_and_decode(logger, block, trigger, hosts, subgraph_metrics) {
Ok(runnable_triggers) => runnables.push(runnable_triggers),
Err(e) => return Err(e),
}
}
Ok(runnables)
}
}