-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathcontext.rs
More file actions
97 lines (86 loc) · 2.66 KB
/
context.rs
File metadata and controls
97 lines (86 loc) · 2.66 KB
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
97
use std::sync::Arc;
use alloy::primitives::{BlockHash, BlockNumber};
use graph::{
amp::{log::Logger as _, Codec, Manifest},
cheap_clone::CheapClone,
components::store::WritableStore,
data::subgraph::DeploymentHash,
env::AmpEnv,
util::backoff::ExponentialBackoff,
};
use slog::Logger;
use super::Compat;
use crate::amp_subgraph::Metrics;
pub(in super::super) struct Context<AC> {
pub(super) logger: Logger,
pub(super) client: Arc<AC>,
pub(super) store: Arc<dyn WritableStore>,
pub(super) buffer_size: usize,
pub(super) block_range: usize,
pub(super) backoff: ExponentialBackoff,
pub(super) deployment: DeploymentHash,
pub(super) manifest: Manifest,
pub(super) metrics: Metrics,
pub(super) codec: Codec,
}
impl<AC> Context<AC> {
pub(in super::super) fn new(
logger: &Logger,
env: &AmpEnv,
client: Arc<AC>,
store: Arc<dyn WritableStore>,
deployment: DeploymentHash,
manifest: Manifest,
metrics: Metrics,
) -> Self {
let logger = logger.component("AmpSubgraphRunner");
let backoff = ExponentialBackoff::new(env.query_retry_min_delay, env.query_retry_max_delay);
let codec = Codec::new(manifest.schema.cheap_clone());
Self {
logger,
client,
store,
buffer_size: env.buffer_size,
block_range: env.block_range,
backoff,
deployment,
manifest,
metrics,
codec,
}
}
pub(super) fn indexing_completed(&self) -> bool {
let Some(last_synced_block) = self.latest_synced_block() else {
return false;
};
self.manifest
.data_sources
.iter()
.all(|data_source| last_synced_block >= data_source.source.end_block)
}
pub(super) fn latest_synced_block(&self) -> Option<BlockNumber> {
self.latest_synced_block_ptr()
.map(|(block_number, _)| block_number)
}
pub(super) fn latest_synced_block_ptr(&self) -> Option<(BlockNumber, BlockHash)> {
self.store
.block_ptr()
.map(|block_ptr| (block_ptr.number.compat(), block_ptr.hash.compat()))
}
pub(super) fn start_block(&self) -> BlockNumber {
self.manifest
.data_sources
.iter()
.map(|data_source| data_source.source.start_block)
.min()
.unwrap()
}
pub(super) fn end_block(&self) -> BlockNumber {
self.manifest
.data_sources
.iter()
.map(|data_source| data_source.source.end_block)
.max()
.unwrap()
}
}