-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathstatus.rs
More file actions
235 lines (207 loc) · 6.77 KB
/
status.rs
File metadata and controls
235 lines (207 loc) · 6.77 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//! Support for the indexing status API
use super::schema::{SubgraphError, SubgraphHealth};
use crate::blockchain::BlockHash;
use crate::components::store::{BlockNumber, DeploymentId};
use crate::data::graphql::{object, IntoValue};
use crate::prelude::{r, BlockPtr, Value};
pub enum Filter {
/// Get all versions for the named subgraph
SubgraphName(String),
/// Get the current (`true`) or pending (`false`) version of the named
/// subgraph
SubgraphVersion(String, bool),
/// Get the status of all deployments whose the given given IPFS hashes
Deployments(Vec<String>),
/// Get the status of all deployments with the given ids
DeploymentIds(Vec<DeploymentId>),
}
/// Light wrapper around `EthereumBlockPointer` that is compatible with GraphQL values.
#[derive(Clone, Debug)]
pub struct EthereumBlock(BlockPtr);
impl EthereumBlock {
pub fn new(hash: BlockHash, number: BlockNumber) -> Self {
EthereumBlock(BlockPtr::new(hash, number))
}
pub fn to_ptr(self) -> BlockPtr {
self.0
}
pub fn number(&self) -> i32 {
self.0.number
}
}
impl IntoValue for EthereumBlock {
fn into_value(self) -> r::Value {
object! {
__typename: "EthereumBlock",
hash: self.0.hash_hex(),
number: format!("{}", self.0.number),
}
}
}
impl From<BlockPtr> for EthereumBlock {
fn from(ptr: BlockPtr) -> Self {
Self(ptr)
}
}
/// Indexing status information related to the chain. Right now, we only
/// support Ethereum, but once we support more chains, we'll have to turn this into
/// an enum
#[derive(Clone, Debug)]
pub struct ChainInfo {
/// The network name (e.g. `mainnet`, `ropsten`, `rinkeby`, `kovan` or `goerli`).
pub network: String,
/// The current head block of the chain.
pub chain_head_block: Option<EthereumBlock>,
/// The earliest block available for this subgraph (only the number).
pub earliest_block_number: BlockNumber,
/// The latest block that the subgraph has synced to.
pub latest_block: Option<EthereumBlock>,
}
impl IntoValue for ChainInfo {
fn into_value(self) -> r::Value {
let ChainInfo {
network,
chain_head_block,
earliest_block_number,
latest_block,
} = self;
object! {
// `__typename` is needed for the `ChainIndexingStatus` interface
// in GraphQL to work.
__typename: "EthereumIndexingStatus",
network: network,
chainHeadBlock: chain_head_block,
earliestBlock: object! {
__typename: "EarliestBlock",
number: earliest_block_number,
hash: "0x0"
},
latestBlock: latest_block,
}
}
}
#[derive(Debug)]
pub struct Info {
pub id: DeploymentId,
/// The deployment hash
pub subgraph: String,
/// Whether or not the subgraph has synced all the way to the current chain head.
pub synced: bool,
pub health: SubgraphHealth,
pub fatal_error: Option<SubgraphError>,
pub non_fatal_errors: Vec<SubgraphError>,
pub paused: Option<bool>,
/// Indexing status on different chains involved in the subgraph's data sources.
pub chains: Vec<ChainInfo>,
pub entity_count: u64,
/// ID of the Graph Node that the subgraph is indexed by.
pub node: Option<String>,
pub history_blocks: i32,
}
impl IntoValue for Info {
fn into_value(self) -> r::Value {
let Info {
id: _,
subgraph,
chains,
entity_count,
fatal_error,
health,
paused,
node,
non_fatal_errors,
synced,
history_blocks,
} = self;
let non_fatal_errors: Vec<_> = non_fatal_errors
.into_iter()
.map(subgraph_error_to_value)
.collect();
let fatal_error_val = fatal_error.map_or(r::Value::Null, subgraph_error_to_value);
object! {
__typename: "SubgraphIndexingStatus",
subgraph: subgraph,
synced: synced,
health: r::Value::from(health),
paused: paused,
fatalError: fatal_error_val,
nonFatalErrors: non_fatal_errors,
chains: chains.into_iter().map(|chain| chain.into_value()).collect::<Vec<_>>(),
entityCount: format!("{}", entity_count),
node: node,
historyBlocks: history_blocks,
}
}
}
fn subgraph_error_to_value(subgraph_error: SubgraphError) -> r::Value {
let SubgraphError {
subgraph_id,
message,
block_ptr,
handler,
deterministic,
} = subgraph_error;
let block_value = block_ptr
.map(|ptr| {
object! {
__typename: "Block",
number: ptr.number,
hash: r::Value::from(Value::Bytes(ptr.hash.into())),
}
})
.unwrap_or(r::Value::Null);
object! {
__typename: "SubgraphError",
subgraphId: subgraph_id.to_string(),
message: message,
handler: handler,
block: block_value,
deterministic: deterministic,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::prelude::DeploymentHash;
use web3::types::H256;
#[test]
fn subgraph_error_block_is_null_without_pointer() {
let deployment =
DeploymentHash::new("QmXoypizjW3WknFiJnKLwHCnL72vedxjQkDDP1mXWo6uco").unwrap();
let value = subgraph_error_to_value(SubgraphError {
subgraph_id: deployment,
message: "boom".to_string(),
block_ptr: None,
handler: None,
deterministic: true,
});
match value {
r::Value::Object(map) => {
assert_eq!(map.get("block"), Some(&r::Value::Null));
}
_ => panic!("expected object"),
}
}
#[test]
fn subgraph_error_block_contains_data_when_present() {
let deployment =
DeploymentHash::new("QmXoypizjW3WknFiJnKLwHCnL72vedxjQkDDP1mXWo6uco").unwrap();
let ptr = BlockPtr::new(H256::zero().into(), 42);
let value = subgraph_error_to_value(SubgraphError {
subgraph_id: deployment,
message: "boom".to_string(),
block_ptr: Some(ptr),
handler: None,
deterministic: true,
});
match value {
r::Value::Object(map) => match map.get("block").expect("block field present") {
r::Value::Object(block) => {
assert_eq!(block.get("number"), Some(&r::Value::Int(42.into())));
}
other => panic!("unexpected block value {other:?}"),
},
_ => panic!("expected object"),
}
}
}