-
Notifications
You must be signed in to change notification settings - Fork 69
/
mod.rs
323 lines (280 loc) · 8.71 KB
/
mod.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//! Internal pipeline framework
use pallas::network::miniprotocols::Point;
use serde::Deserialize;
use serde_json::{json, Value as JsonValue};
use std::collections::VecDeque;
use std::fmt::Debug;
use std::path::PathBuf;
pub use crate::cursor::Config as CursorConfig;
// we use UtxoRpc as our canonical representation of a parsed Tx
pub use utxorpc::spec::cardano::Block as ParsedBlock;
pub use utxorpc::spec::cardano::Tx as ParsedTx;
// we use GenesisValues from Pallas as our ChainConfig
pub use pallas::ledger::traverse::wellknown::GenesisValues;
pub mod errors;
pub mod legacy_v1;
pub use errors::*;
#[derive(Clone)]
pub struct Breadcrumbs {
state: VecDeque<Point>,
max: usize,
}
impl Breadcrumbs {
pub fn new(max: usize) -> Self {
Self {
state: Default::default(),
max,
}
}
pub fn from_points(points: Vec<Point>, max: usize) -> Self {
Self {
state: VecDeque::from_iter(points),
max,
}
}
pub fn is_empty(&self) -> bool {
self.state.is_empty()
}
pub fn track(&mut self, point: Point) {
// if we have a rollback, retain only older points
self.state
.retain(|p| p.slot_or_default() < point.slot_or_default());
// add the new point we're tracking
self.state.push_front(point);
// if we have too many points, remove the older ones
if self.state.len() > self.max {
self.state.pop_back();
}
}
pub fn points(&self) -> Vec<Point> {
self.state.iter().map(Clone::clone).collect()
}
}
#[derive(Deserialize, Clone)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum ChainConfig {
Mainnet,
Testnet,
PreProd,
Preview,
Custom(GenesisValues),
}
impl Default for ChainConfig {
fn default() -> Self {
Self::Mainnet
}
}
impl From<ChainConfig> for GenesisValues {
fn from(other: ChainConfig) -> Self {
match other {
ChainConfig::Mainnet => GenesisValues::mainnet(),
ChainConfig::Testnet => GenesisValues::testnet(),
ChainConfig::PreProd => GenesisValues::preprod(),
ChainConfig::Preview => GenesisValues::preview(),
ChainConfig::Custom(x) => x,
}
}
}
pub struct Context {
pub chain: ChainConfig,
pub intersect: IntersectConfig,
pub finalize: Option<FinalizeConfig>,
pub current_dir: PathBuf,
pub breadcrumbs: Breadcrumbs,
}
#[derive(Debug, Clone)]
pub enum Record {
CborBlock(Vec<u8>),
CborTx(Vec<u8>),
GenericJson(JsonValue),
OuraV1Event(legacy_v1::Event),
ParsedTx(ParsedTx),
ParsedBlock(ParsedBlock),
}
impl From<Record> for JsonValue {
fn from(value: Record) -> Self {
match value {
Record::CborBlock(x) => json!({ "hex": hex::encode(x) }),
Record::CborTx(x) => json!({ "hex": hex::encode(x) }),
Record::ParsedBlock(x) => json!(x),
Record::ParsedTx(x) => json!(x),
Record::OuraV1Event(x) => json!(x),
Record::GenericJson(x) => x,
}
}
}
#[derive(Debug, Clone)]
pub enum ChainEvent {
Apply(Point, Record),
Undo(Point, Record),
Reset(Point),
}
impl ChainEvent {
pub fn apply(point: Point, record: impl Into<Record>) -> gasket::messaging::Message<Self> {
gasket::messaging::Message {
payload: Self::Apply(point, record.into()),
}
}
pub fn undo(point: Point, record: impl Into<Record>) -> gasket::messaging::Message<Self> {
gasket::messaging::Message {
payload: Self::Undo(point, record.into()),
}
}
pub fn reset(point: Point) -> gasket::messaging::Message<Self> {
gasket::messaging::Message {
payload: Self::Reset(point),
}
}
pub fn point(&self) -> &Point {
match self {
Self::Apply(x, _) => x,
Self::Undo(x, _) => x,
Self::Reset(x) => x,
}
}
pub fn record(&self) -> Option<&Record> {
match self {
Self::Apply(_, x) => Some(x),
Self::Undo(_, x) => Some(x),
_ => None,
}
}
pub fn map_record(self, f: fn(Record) -> Record) -> Self {
match self {
Self::Apply(p, x) => Self::Apply(p, f(x)),
Self::Undo(p, x) => Self::Undo(p, f(x)),
Self::Reset(x) => Self::Reset(x),
}
}
pub fn try_map_record<E>(self, f: fn(Record) -> Result<Record, E>) -> Result<Self, E> {
let out = match self {
Self::Apply(p, x) => Self::Apply(p, f(x)?),
Self::Undo(p, x) => Self::Undo(p, f(x)?),
Self::Reset(x) => Self::Reset(x),
};
Ok(out)
}
pub fn try_map_record_to_many<F, E>(self, f: F) -> Result<Vec<Self>, E>
where
F: FnOnce(Record) -> Result<Vec<Record>, E>,
{
let out = match self {
Self::Apply(p, x) => f(x)?
.into_iter()
.map(|i| Self::Apply(p.clone(), i))
.collect(),
Self::Undo(p, x) => f(x)?
.into_iter()
.map(|i| Self::Undo(p.clone(), i))
.collect(),
Self::Reset(x) => vec![Self::Reset(x)],
};
Ok(out)
}
}
fn point_to_json(point: Point) -> JsonValue {
match &point {
pallas::network::miniprotocols::Point::Origin => JsonValue::from("origin"),
pallas::network::miniprotocols::Point::Specific(slot, hash) => {
json!({ "slot": slot, "hash": hex::encode(hash)})
}
}
}
impl From<ChainEvent> for JsonValue {
fn from(value: ChainEvent) -> Self {
match value {
ChainEvent::Apply(point, record) => {
json!({
"event": "apply",
"point": point_to_json(point),
"record": JsonValue::from(record.clone())
})
}
ChainEvent::Undo(point, record) => {
json!({
"event": "undo",
"point": point_to_json(point),
"record": JsonValue::from(record.clone())
})
}
ChainEvent::Reset(point) => {
json!({
"event": "reset",
"point": point_to_json(point)
})
}
}
}
}
pub type SourceOutputPort = gasket::messaging::OutputPort<ChainEvent>;
pub type FilterInputPort = gasket::messaging::InputPort<ChainEvent>;
pub type FilterOutputPort = gasket::messaging::OutputPort<ChainEvent>;
pub type MapperInputPort = gasket::messaging::InputPort<ChainEvent>;
pub type MapperOutputPort = gasket::messaging::OutputPort<ChainEvent>;
pub type SinkInputPort = gasket::messaging::InputPort<ChainEvent>;
pub type SinkCursorPort = gasket::messaging::OutputPort<Point>;
#[derive(Debug, Deserialize, Clone)]
#[serde(tag = "type", content = "value")]
pub enum IntersectConfig {
Tip,
Origin,
Point(u64, String),
Breadcrumbs(Vec<(u64, String)>),
}
impl IntersectConfig {
pub fn points(&self) -> Option<Vec<Point>> {
match self {
IntersectConfig::Breadcrumbs(all) => {
let mapped = all
.iter()
.map(|(slot, hash)| {
let hash = hex::decode(hash).expect("valid hex hash");
Point::Specific(*slot, hash)
})
.collect();
Some(mapped)
}
IntersectConfig::Point(slot, hash) => {
let hash = hex::decode(hash).expect("valid hex hash");
Some(vec![Point::Specific(*slot, hash)])
}
_ => None,
}
}
}
/// Optional configuration to stop processing new blocks after processing:
/// 1. a block with the given hash
/// 2. the first block on or after a given absolute slot
/// 3. TODO: a total of X blocks
#[derive(Deserialize, Debug, Clone)]
pub struct FinalizeConfig {
until_hash: Option<String>,
max_block_slot: Option<u64>,
// max_block_quantity: Option<u64>,
}
pub fn should_finalize(
config: &Option<FinalizeConfig>,
last_point: &Point,
// block_count: u64,
) -> bool {
let config = match config {
Some(x) => x,
None => return false,
};
if let Some(expected) = &config.until_hash {
if let Point::Specific(_, current) = last_point {
return expected == &hex::encode(current);
}
}
if let Some(max) = config.max_block_slot {
if last_point.slot_or_default() >= max {
return true;
}
}
// if let Some(max) = config.max_block_quantity {
// if block_count >= max {
// return true;
// }
// }
false
}