-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.rs
272 lines (231 loc) · 9.07 KB
/
main.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
use anyhow::{format_err, Context, Error};
use chrono::DateTime;
use futures03::StreamExt;
use lazy_static::lazy_static;
use pb::sf::substreams::rpc::v2::{BlockScopedData, BlockUndoSignal};
use pb::sf::substreams::v1::Package;
use regex::Regex;
use semver::Version;
use prost::Message;
use std::{env, process::exit, sync::Arc};
use substreams::SubstreamsEndpoint;
use substreams_stream::{BlockResponse, SubstreamsStream};
mod pb;
mod substreams;
mod substreams_stream;
lazy_static! {
static ref MODULE_NAME_REGEXP: Regex = Regex::new(r"^([a-zA-Z][a-zA-Z0-9_-]{0,63})$").unwrap();
}
const REGISTRY_URL: &str = "https://spkg.io";
#[tokio::main]
async fn main() -> Result<(), Error> {
let args = env::args();
if args.len() < 4 || args.len() > 5 {
println!("usage: stream <endpoint> <spkg> <module> [<start>:<stop>]");
println!();
println!("<spkg> can either be the full spkg.io link or `spkg_package@version`");
println!("Example usage: stream mainnet.injective.streamingfast.io:443 [email protected] all_events 1:10");
println!("The environment variable SUBSTREAMS_API_TOKEN must be set also");
println!("and should contain a valid Substream API token.");
exit(1);
}
let mut endpoint_url = env::args().nth(1).unwrap();
let package_file = env::args().nth(2).unwrap();
let module_name = env::args().nth(3).unwrap();
if !endpoint_url.starts_with("http") {
endpoint_url = format!("{}://{}", "https", endpoint_url);
}
let token_env = env::var("SUBSTREAMS_API_TOKEN").unwrap_or("".to_string());
let mut token: Option<String> = None;
if token_env.len() > 0 {
token = Some(token_env);
}
let package = read_package(&package_file).await?;
let block_range = read_block_range(&package, &module_name)?;
let endpoint = Arc::new(SubstreamsEndpoint::new(&endpoint_url, token).await?);
let cursor: Option<String> = load_persisted_cursor()?;
let mut stream = SubstreamsStream::new(
endpoint,
cursor,
package.modules,
module_name.to_string(),
block_range.0,
block_range.1,
);
loop {
match stream.next().await {
None => {
println!("Stream consumed");
break;
}
Some(Ok(BlockResponse::New(data))) => {
process_block_scoped_data(&data)?;
persist_cursor(data.cursor)?;
}
Some(Ok(BlockResponse::Undo(undo_signal))) => {
process_block_undo_signal(&undo_signal)?;
persist_cursor(undo_signal.last_valid_cursor)?;
}
Some(Err(err)) => {
println!();
println!("Stream terminated with error");
println!("{:?}", err);
exit(1);
}
}
}
Ok(())
}
fn process_block_scoped_data(data: &BlockScopedData) -> Result<(), Error> {
let output = data.output.as_ref().unwrap().map_output.as_ref().unwrap();
// You can decode the actual Any type received using this code:
//
// let value = GeneratedStructName::decode(output.value.as_slice())?;
//
// Where GeneratedStructName is the Rust code generated for the Protobuf representing
// your type, so you will need generate it using `substreams protogen` and import it from the
// `src/pb` folder.
let clock = data.clock.as_ref().unwrap();
let timestamp = clock.timestamp.as_ref().unwrap();
let date = DateTime::from_timestamp(timestamp.seconds, timestamp.nanos as u32)
.expect("received timestamp should always be valid");
println!(
"Block #{} - Payload {} ({} bytes) - Drift {}s",
clock.number,
output.type_url.replace("type.googleapis.com/", ""),
output.value.len(),
date.signed_duration_since(chrono::offset::Utc::now())
.num_seconds()
* -1
);
Ok(())
}
fn process_block_undo_signal(_undo_signal: &BlockUndoSignal) -> Result<(), anyhow::Error> {
// `BlockUndoSignal` must be treated as "delete every data that has been recorded after
// block height specified by block in BlockUndoSignal". In the example above, this means
// you must delete changes done by `Block #7b` and `Block #6b`. The exact details depends
// on your own logic. If for example all your added record contain a block number, a
// simple way is to do `delete all records where block_num > 5` which is the block num
// received in the `BlockUndoSignal` (this is true for append only records, so when only `INSERT` are allowed).
unimplemented!("you must implement some kind of block undo handling, or request only final blocks (tweak substreams_stream.rs)")
}
fn persist_cursor(_cursor: String) -> Result<(), anyhow::Error> {
// FIXME: Handling of the cursor is missing here. It should be saved each time
// a full block has been correctly processed/persisted. The saving location
// is your responsibility.
//
// By making it persistent, we ensure that if we crash, on startup we are
// going to read it back from database and start back our SubstreamsStream
// with it ensuring we are continuously streaming without ever losing a single
// element.
Ok(())
}
fn load_persisted_cursor() -> Result<Option<String>, anyhow::Error> {
// FIXME: Handling of the cursor is missing here. It should be loaded from
// somewhere (local file, database, cloud storage) and then `SubstreamStream` will
// be able correctly resume from the right block.
Ok(None)
}
fn read_block_range(pkg: &Package, module_name: &str) -> Result<(i64, u64), anyhow::Error> {
let module = pkg
.modules
.as_ref()
.unwrap()
.modules
.iter()
.find(|m| m.name == module_name)
.ok_or_else(|| format_err!("module '{}' not found in package", module_name))?;
let mut input: String = "".to_string();
if let Some(range) = env::args().nth(4) {
input = range;
};
let (prefix, suffix) = match input.split_once(":") {
Some((prefix, suffix)) => (prefix.to_string(), suffix.to_string()),
None => ("".to_string(), input),
};
let start: i64 = match prefix.as_str() {
"" => module.initial_block as i64,
x if x.starts_with("+") => {
let block_count = x
.trim_start_matches("+")
.parse::<u64>()
.context("argument <stop> is not a valid integer")?;
(module.initial_block + block_count) as i64
}
x => x
.parse::<i64>()
.context("argument <start> is not a valid integer")?,
};
let stop: u64 = match suffix.as_str() {
"" => 0,
"-" => 0,
x if x.starts_with("+") => {
let block_count = x
.trim_start_matches("+")
.parse::<u64>()
.context("argument <stop> is not a valid integer")?;
start as u64 + block_count
}
x => x
.parse::<u64>()
.context("argument <stop> is not a valid integer")?,
};
return Ok((start, stop));
}
async fn read_package(input: &str) -> Result<Package, anyhow::Error> {
let mut mutable_input = input.to_string();
let val = parse_standard_package_and_version(input);
if val.is_ok() {
let package_and_version = val.unwrap();
mutable_input = format!(
"{}/v1/packages/{}/{}",
REGISTRY_URL, package_and_version.0, package_and_version.1
);
}
if mutable_input.starts_with("http") {
return read_http_package(&mutable_input).await;
}
// Assume it's a local file
let content = std::fs::read(&mutable_input)
.context(format_err!("read package from file '{}'", mutable_input))?;
Package::decode(content.as_ref()).context("decode command")
}
async fn read_http_package(input: &str) -> Result<Package, anyhow::Error> {
let body = reqwest::get(input).await?.bytes().await?;
Package::decode(body).context("decode command")
}
fn parse_standard_package_and_version(input: &str) -> Result<(String, String), Error> {
let parts: Vec<&str> = input.split('@').collect();
if parts.len() > 2 {
return Err(format_err!(
"package name: {} does not follow the convention of <package>@<version>",
input
));
}
let package_name = parts[0].to_string();
if !MODULE_NAME_REGEXP.is_match(&package_name) {
return Err(format_err!(
"package name {} does not match regexp {}",
package_name,
MODULE_NAME_REGEXP.as_str()
));
}
if parts.len() == 1
|| parts
.get(1)
.map_or(true, |v| v.is_empty() || *v == "latest")
{
return Ok((package_name, "latest".to_string()));
}
let version = parts[1];
if !is_valid_version(&version.replace("v", "")) {
return Err(format_err!(
"version '{}' is not valid Semver format",
version
));
}
Ok((package_name, version.to_string()))
}
fn is_valid_version(version: &str) -> bool {
Version::parse(version).is_ok()
}