-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathblock_stream.rs
More file actions
862 lines (748 loc) · 26.8 KB
/
block_stream.rs
File metadata and controls
862 lines (748 loc) · 26.8 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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
use crate::blockchain::SubgraphFilter;
use crate::data_source::{subgraph, CausalityRegion};
use anyhow::Error;
use async_stream::stream;
use async_trait::async_trait;
use futures03::Stream;
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::fmt;
use std::sync::Arc;
use thiserror::Error;
use tokio::sync::mpsc::{self, Receiver, Sender};
use super::{Block, BlockPtr, Blockchain, Trigger, TriggerFilterWrapper};
use crate::anyhow::Result;
use crate::components::store::{BlockNumber, DeploymentLocator, SourceableStore};
use crate::data::subgraph::UnifiedMappingApiVersion;
use crate::firehose::{self, FirehoseEndpoint};
use crate::futures03::stream::StreamExt as _;
use crate::schema::{EntityType, InputSchema};
use crate::{prelude::*, prometheus::labels};
pub const BUFFERED_BLOCK_STREAM_SIZE: usize = 100;
pub const FIREHOSE_BUFFER_STREAM_SIZE: usize = 1;
pub struct BufferedBlockStream<C: Blockchain> {
inner: Pin<Box<dyn Stream<Item = Result<BlockStreamEvent<C>, BlockStreamError>> + Send>>,
}
impl<C: Blockchain + 'static> BufferedBlockStream<C> {
pub fn spawn_from_stream(
size_hint: usize,
stream: Box<dyn BlockStream<C>>,
) -> Box<dyn BlockStream<C>> {
let (sender, receiver) =
mpsc::channel::<Result<BlockStreamEvent<C>, BlockStreamError>>(size_hint);
crate::spawn(async move { BufferedBlockStream::stream_blocks(stream, sender).await });
Box::new(BufferedBlockStream::new(receiver))
}
pub fn new(mut receiver: Receiver<Result<BlockStreamEvent<C>, BlockStreamError>>) -> Self {
let inner = stream! {
loop {
let event = match receiver.recv().await {
Some(evt) => evt,
None => return,
};
yield event
}
};
Self {
inner: Box::pin(inner),
}
}
pub async fn stream_blocks(
mut stream: Box<dyn BlockStream<C>>,
sender: Sender<Result<BlockStreamEvent<C>, BlockStreamError>>,
) -> Result<(), Error> {
while let Some(event) = stream.next().await {
match sender.send(event).await {
Ok(_) => continue,
Err(err) => {
return Err(anyhow!(
"buffered blockstream channel is closed, stopping. Err: {}",
err
))
}
}
}
Ok(())
}
}
impl<C: Blockchain> BlockStream<C> for BufferedBlockStream<C> {
fn buffer_size_hint(&self) -> usize {
unreachable!()
}
}
impl<C: Blockchain> Stream for BufferedBlockStream<C> {
type Item = Result<BlockStreamEvent<C>, BlockStreamError>;
fn poll_next(
mut self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Option<Self::Item>> {
self.inner.poll_next_unpin(cx)
}
}
pub trait BlockStream<C: Blockchain>:
Stream<Item = Result<BlockStreamEvent<C>, BlockStreamError>> + Unpin + Send
{
fn buffer_size_hint(&self) -> usize;
}
/// BlockRefetcher abstraction allows a chain to decide if a block must be refetched after a dynamic data source was added
#[async_trait]
pub trait BlockRefetcher<C: Blockchain>: Send + Sync {
fn required(&self, chain: &C) -> bool;
async fn get_block(
&self,
chain: &C,
logger: &Logger,
cursor: FirehoseCursor,
) -> Result<C::Block, Error>;
}
/// BlockStreamBuilder is an abstraction that would separate the logic for building streams from the blockchain trait
#[async_trait]
pub trait BlockStreamBuilder<C: Blockchain>: Send + Sync {
async fn build_firehose(
&self,
chain: &C,
deployment: DeploymentLocator,
block_cursor: FirehoseCursor,
start_blocks: Vec<BlockNumber>,
subgraph_current_block: Option<BlockPtr>,
filter: Arc<C::TriggerFilter>,
unified_api_version: UnifiedMappingApiVersion,
) -> Result<Box<dyn BlockStream<C>>>;
async fn build_polling(
&self,
chain: &C,
deployment: DeploymentLocator,
start_blocks: Vec<BlockNumber>,
source_subgraph_stores: Vec<Arc<dyn SourceableStore>>,
subgraph_current_block: Option<BlockPtr>,
filter: Arc<TriggerFilterWrapper<C>>,
unified_api_version: UnifiedMappingApiVersion,
) -> Result<Box<dyn BlockStream<C>>>;
async fn build_subgraph_block_stream(
&self,
chain: &C,
deployment: DeploymentLocator,
start_blocks: Vec<BlockNumber>,
source_subgraph_stores: Vec<Arc<dyn SourceableStore>>,
subgraph_current_block: Option<BlockPtr>,
filter: Arc<TriggerFilterWrapper<C>>,
unified_api_version: UnifiedMappingApiVersion,
) -> Result<Box<dyn BlockStream<C>>> {
self.build_polling(
chain,
deployment,
start_blocks,
source_subgraph_stores,
subgraph_current_block,
filter,
unified_api_version,
)
.await
}
}
#[derive(Debug, Clone)]
pub struct FirehoseCursor(Option<String>);
impl FirehoseCursor {
#[allow(non_upper_case_globals)]
pub const None: Self = FirehoseCursor(None);
pub fn is_none(&self) -> bool {
self.0.is_none()
}
}
impl fmt::Display for FirehoseCursor {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
f.write_str(self.0.as_deref().unwrap_or(""))
}
}
impl From<String> for FirehoseCursor {
fn from(cursor: String) -> Self {
// Treat a cursor of "" as None, not absolutely necessary for correctness since the firehose
// treats both as the same, but makes it a little clearer.
if cursor.is_empty() {
FirehoseCursor::None
} else {
FirehoseCursor(Some(cursor))
}
}
}
impl From<Option<String>> for FirehoseCursor {
fn from(cursor: Option<String>) -> Self {
match cursor {
None => FirehoseCursor::None,
Some(s) => FirehoseCursor::from(s),
}
}
}
impl AsRef<Option<String>> for FirehoseCursor {
fn as_ref(&self) -> &Option<String> {
&self.0
}
}
#[derive(Debug)]
pub struct BlockWithTriggers<C: Blockchain> {
pub block: C::Block,
pub trigger_data: Vec<Trigger<C>>,
}
impl<C: Blockchain> Clone for BlockWithTriggers<C>
where
C::TriggerData: Clone,
{
fn clone(&self) -> Self {
Self {
block: self.block.clone(),
trigger_data: self.trigger_data.clone(),
}
}
}
impl<C: Blockchain> BlockWithTriggers<C> {
/// Creates a BlockWithTriggers structure, which holds
/// the trigger data ordered and without any duplicates.
pub fn new(block: C::Block, trigger_data: Vec<C::TriggerData>, logger: &Logger) -> Self {
Self::new_with_triggers(
block,
trigger_data.into_iter().map(Trigger::Chain).collect(),
logger,
)
}
pub fn new_with_subgraph_triggers(
block: C::Block,
trigger_data: Vec<subgraph::TriggerData>,
logger: &Logger,
) -> Self {
Self::new_with_triggers(
block,
trigger_data.into_iter().map(Trigger::Subgraph).collect(),
logger,
)
}
fn new_with_triggers(
block: C::Block,
mut trigger_data: Vec<Trigger<C>>,
logger: &Logger,
) -> Self {
// This is where triggers get sorted.
trigger_data.sort();
let old_len = trigger_data.len();
// This is removing the duplicate triggers in the case of multiple
// data sources fetching the same event/call/etc.
trigger_data.dedup();
let new_len = trigger_data.len();
if new_len != old_len {
debug!(
logger,
"Trigger data had duplicate triggers";
"block_number" => block.number(),
"block_hash" => block.hash().hash_hex(),
"old_length" => old_len,
"new_length" => new_len,
);
}
Self {
block,
trigger_data,
}
}
pub fn trigger_count(&self) -> usize {
self.trigger_data.len()
}
pub fn ptr(&self) -> BlockPtr {
self.block.ptr()
}
pub fn parent_ptr(&self) -> Option<BlockPtr> {
self.block.parent_ptr()
}
pub fn extend_triggers(&mut self, triggers: Vec<Trigger<C>>) {
self.trigger_data.extend(triggers);
self.trigger_data.sort();
}
}
/// The `TriggersAdapterWrapper` wraps the chain-specific `TriggersAdapter`, enabling chain-agnostic
/// handling of subgraph datasource triggers. Without this wrapper, we would have to duplicate the same
/// logic for each chain, increasing code repetition.
pub struct TriggersAdapterWrapper<C: Blockchain> {
pub adapter: Arc<dyn TriggersAdapter<C>>,
pub source_subgraph_stores: HashMap<DeploymentHash, Arc<dyn SourceableStore>>,
}
impl<C: Blockchain> TriggersAdapterWrapper<C> {
pub fn new(
adapter: Arc<dyn TriggersAdapter<C>>,
source_subgraph_stores: Vec<Arc<dyn SourceableStore>>,
) -> Self {
let stores_map: HashMap<_, _> = source_subgraph_stores
.iter()
.map(|store| (store.input_schema().id().clone(), store.clone()))
.collect();
Self {
adapter,
source_subgraph_stores: stores_map,
}
}
pub async fn blocks_with_subgraph_triggers(
&self,
logger: &Logger,
filters: &[SubgraphFilter],
range: SubgraphTriggerScanRange<C>,
) -> Result<Vec<BlockWithTriggers<C>>, Error> {
if filters.is_empty() {
return Err(anyhow!("No subgraph filters provided"));
}
let (blocks, hash_to_entities) = match range {
SubgraphTriggerScanRange::Single(block) => {
let hash_to_entities = self
.fetch_entities_for_filters(filters, block.number(), block.number())
.await?;
(vec![block], hash_to_entities)
}
SubgraphTriggerScanRange::Range(from, to) => {
let hash_to_entities = self.fetch_entities_for_filters(filters, from, to).await?;
// Get block numbers that have entities
let mut block_numbers: BTreeSet<_> = hash_to_entities
.iter()
.flat_map(|(_, entities, _)| entities.keys().copied())
.collect();
// Always include the last block in the range
block_numbers.insert(to);
let blocks = self
.adapter
.load_block_ptrs_by_numbers(logger.clone(), block_numbers)
.await?;
(blocks, hash_to_entities)
}
};
create_subgraph_triggers::<C>(logger.clone(), blocks, hash_to_entities).await
}
async fn fetch_entities_for_filters(
&self,
filters: &[SubgraphFilter],
from: BlockNumber,
to: BlockNumber,
) -> Result<
Vec<(
DeploymentHash,
BTreeMap<BlockNumber, Vec<EntitySourceOperation>>,
u32,
)>,
Error,
> {
let futures = filters
.iter()
.filter_map(|filter| {
self.source_subgraph_stores
.get(&filter.subgraph)
.map(|store| {
let store = store.clone();
let schema = store.input_schema();
async move {
let entities =
get_entities_for_range(&store, filter, &schema, from, to).await?;
Ok::<_, Error>((filter.subgraph.clone(), entities, filter.manifest_idx))
}
})
})
.collect::<Vec<_>>();
if futures.is_empty() {
return Ok(Vec::new());
}
futures03::future::try_join_all(futures).await
}
}
fn create_subgraph_trigger_from_entities(
subgraph: &DeploymentHash,
entities: Vec<EntitySourceOperation>,
manifest_idx: u32,
) -> Vec<subgraph::TriggerData> {
entities
.into_iter()
.map(|entity| subgraph::TriggerData {
source: subgraph.clone(),
entity,
source_idx: manifest_idx,
})
.collect()
}
async fn create_subgraph_triggers<C: Blockchain>(
logger: Logger,
blocks: Vec<C::Block>,
subgraph_data: Vec<(
DeploymentHash,
BTreeMap<BlockNumber, Vec<EntitySourceOperation>>,
u32,
)>,
) -> Result<Vec<BlockWithTriggers<C>>, Error> {
let logger_clone = logger.cheap_clone();
let blocks: Vec<BlockWithTriggers<C>> = blocks
.into_iter()
.map(|block| {
let block_number = block.number();
let mut all_trigger_data = Vec::new();
for (hash, entities, manifest_idx) in subgraph_data.iter() {
if let Some(block_entities) = entities.get(&block_number) {
let trigger_data = create_subgraph_trigger_from_entities(
hash,
block_entities.clone(),
*manifest_idx,
);
all_trigger_data.extend(trigger_data);
}
}
BlockWithTriggers::new_with_subgraph_triggers(block, all_trigger_data, &logger_clone)
})
.collect();
Ok(blocks)
}
pub enum SubgraphTriggerScanRange<C: Blockchain> {
Single(C::Block),
Range(BlockNumber, BlockNumber),
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum EntityOperationKind {
Create,
Modify,
Delete,
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct EntitySourceOperation {
pub entity_op: EntityOperationKind,
pub entity_type: EntityType,
pub entity: Entity,
pub vid: i64,
}
async fn get_entities_for_range(
store: &Arc<dyn SourceableStore>,
filter: &SubgraphFilter,
schema: &InputSchema,
from: BlockNumber,
to: BlockNumber,
) -> Result<BTreeMap<BlockNumber, Vec<EntitySourceOperation>>, Error> {
let entity_types: Result<Vec<EntityType>> = filter
.entities
.iter()
.map(|name| schema.entity_type(name))
.collect();
Ok(store
.get_range(entity_types?, CausalityRegion::ONCHAIN, from..to)
.await?)
}
impl<C: Blockchain> TriggersAdapterWrapper<C> {
pub async fn ancestor_block(
&self,
ptr: BlockPtr,
offset: BlockNumber,
root: Option<BlockHash>,
) -> Result<Option<C::Block>, Error> {
self.adapter.ancestor_block(ptr, offset, root).await
}
pub async fn scan_triggers(
&self,
logger: &Logger,
from: BlockNumber,
to: BlockNumber,
filter: &Arc<TriggerFilterWrapper<C>>,
) -> Result<(Vec<BlockWithTriggers<C>>, BlockNumber), Error> {
if !filter.subgraph_filter.is_empty() {
let blocks_with_triggers = self
.blocks_with_subgraph_triggers(
logger,
&filter.subgraph_filter,
SubgraphTriggerScanRange::Range(from, to),
)
.await?;
return Ok((blocks_with_triggers, to));
}
self.adapter
.scan_triggers(from, to, &filter.chain_filter)
.await
}
pub async fn triggers_in_block(
&self,
logger: &Logger,
block: C::Block,
filter: &Arc<TriggerFilterWrapper<C>>,
) -> Result<BlockWithTriggers<C>, Error> {
trace!(
logger,
"triggers_in_block";
"block_number" => block.number(),
"block_hash" => block.hash().hash_hex(),
);
if !filter.subgraph_filter.is_empty() {
let blocks_with_triggers = self
.blocks_with_subgraph_triggers(
logger,
&filter.subgraph_filter,
SubgraphTriggerScanRange::Single(block),
)
.await?;
return Ok(blocks_with_triggers.into_iter().next().unwrap());
}
self.adapter
.triggers_in_block(logger, block, &filter.chain_filter)
.await
}
pub async fn is_on_main_chain(&self, ptr: BlockPtr) -> Result<Option<BlockPtr>, Error> {
self.adapter.is_on_main_chain(ptr).await
}
pub async fn parent_ptr(&self, block: &BlockPtr) -> Result<Option<BlockPtr>, Error> {
self.adapter.parent_ptr(block).await
}
pub async fn chain_head_ptr(&self) -> Result<Option<BlockPtr>, Error> {
if self.source_subgraph_stores.is_empty() {
return self.adapter.chain_head_ptr().await;
}
let ptrs = futures03::future::try_join_all(
self.source_subgraph_stores
.values()
.map(|store| store.block_ptr()),
)
.await?;
let min_ptr = ptrs.into_iter().flatten().min_by_key(|ptr| ptr.number);
Ok(min_ptr)
}
}
#[async_trait]
pub trait TriggersAdapter<C: Blockchain>: Send + Sync {
// Return the block that is `offset` blocks before the block pointed to by `ptr` from the local
// cache. An offset of 0 means the block itself, an offset of 1 means the block's parent etc. If
// `root` is passed, short-circuit upon finding a child of `root`. If the block is not in the
// local cache, return `None`.
async fn ancestor_block(
&self,
ptr: BlockPtr,
offset: BlockNumber,
root: Option<BlockHash>,
) -> Result<Option<C::Block>, Error>;
// Returns a sequence of blocks in increasing order of block number.
// Each block will include all of its triggers that match the given `filter`.
// The sequence may omit blocks that contain no triggers,
// but all returned blocks must part of a same chain starting at `chain_base`.
// At least one block will be returned, even if it contains no triggers.
// `step_size` is the suggested number blocks to be scanned.
async fn scan_triggers(
&self,
from: BlockNumber,
to: BlockNumber,
filter: &C::TriggerFilter,
) -> Result<(Vec<BlockWithTriggers<C>>, BlockNumber), Error>;
// Used for reprocessing blocks when creating a data source.
async fn triggers_in_block(
&self,
logger: &Logger,
block: C::Block,
filter: &C::TriggerFilter,
) -> Result<BlockWithTriggers<C>, Error>;
/// Check whether the block is on the main chain. Returns `None` if it
/// is, or `Some(revert_to)` with the canonical parent pointer to revert
/// to if the block has been reorged out.
async fn is_on_main_chain(&self, ptr: BlockPtr) -> Result<Option<BlockPtr>, Error>;
/// Get pointer to parent of `block`. This is called when reverting `block`.
async fn parent_ptr(&self, block: &BlockPtr) -> Result<Option<BlockPtr>, Error>;
/// Get pointer to parent of `block`. This is called when reverting `block`.
async fn chain_head_ptr(&self) -> Result<Option<BlockPtr>, Error>;
async fn load_block_ptrs_by_numbers(
&self,
logger: Logger,
block_numbers: BTreeSet<BlockNumber>,
) -> Result<Vec<C::Block>>;
}
#[async_trait]
pub trait FirehoseMapper<C: Blockchain>: Send + Sync {
fn trigger_filter(&self) -> &C::TriggerFilter;
async fn to_block_stream_event(
&self,
logger: &Logger,
response: &firehose::Response,
) -> Result<BlockStreamEvent<C>, FirehoseError>;
/// Returns the [BlockPtr] value for this given block number. This is the block pointer
/// of the longuest according to Firehose view of the blockchain state.
///
/// This is a thin wrapper around [FirehoseEndpoint#block_ptr_for_number] to make
/// it chain agnostic and callable from chain agnostic [FirehoseBlockStream].
async fn block_ptr_for_number(
&self,
logger: &Logger,
endpoint: &Arc<FirehoseEndpoint>,
number: BlockNumber,
) -> Result<BlockPtr, Error>;
/// Returns the closest final block ptr to the block ptr received.
/// On probablitics chain like Ethereum, final is determined by
/// the confirmations threshold configured for the Firehose stack (currently
/// hard-coded to 200).
///
/// On some other chain like NEAR, the actual final block number is determined
/// from the block itself since it contains information about which block number
/// is final against the current block.
///
/// To take an example, assuming we are on Ethereum, the final block pointer
/// for block #10212 would be the determined final block #10012 (10212 - 200 = 10012).
async fn final_block_ptr_for(
&self,
logger: &Logger,
endpoint: &Arc<FirehoseEndpoint>,
block: &C::Block,
) -> Result<BlockPtr, Error>;
}
#[async_trait]
pub trait BlockStreamMapper<C: Blockchain>: Send + Sync {
fn decode_block(&self, output: Option<&[u8]>) -> Result<Option<C::Block>, BlockStreamError>;
async fn block_with_triggers(
&self,
logger: &Logger,
block: C::Block,
) -> Result<BlockWithTriggers<C>, BlockStreamError>;
}
#[derive(Error, Debug)]
pub enum FirehoseError {
/// We were unable to decode the received block payload into the chain specific Block struct (e.g. chain_ethereum::pb::Block)
#[error("received gRPC block payload cannot be decoded: {0}")]
DecodingError(#[from] prost::DecodeError),
/// Some unknown error occurred
#[error("unknown error")]
UnknownError(#[from] anyhow::Error),
}
impl From<BlockStreamError> for FirehoseError {
fn from(value: BlockStreamError) -> Self {
match value {
BlockStreamError::ProtobufDecodingError(e) => FirehoseError::DecodingError(e),
e => FirehoseError::UnknownError(anyhow!(e.to_string())),
}
}
}
#[derive(Debug, Error)]
pub enum BlockStreamError {
#[error("Failed to decode protobuf {0}")]
ProtobufDecodingError(#[from] prost::DecodeError),
#[error("block stream error {0}")]
Unknown(#[from] anyhow::Error),
}
#[derive(Debug)]
pub enum BlockStreamEvent<C: Blockchain> {
// The payload is the block the subgraph should revert to, so it becomes the new subgraph head.
Revert(BlockPtr, FirehoseCursor),
ProcessBlock(BlockWithTriggers<C>, FirehoseCursor),
}
#[derive(Clone)]
pub struct BlockStreamMetrics {
pub deployment_head: Box<Gauge>,
pub reverted_blocks: Gauge,
pub stopwatch: StopwatchMetrics,
}
impl BlockStreamMetrics {
pub fn new(
registry: Arc<MetricsRegistry>,
deployment_id: &DeploymentHash,
network: String,
shard: String,
stopwatch: StopwatchMetrics,
) -> Self {
let reverted_blocks = registry
.new_deployment_gauge(
"deployment_reverted_blocks",
"Track the last reverted block for a subgraph deployment",
deployment_id.as_str(),
)
.expect("Failed to create `deployment_reverted_blocks` gauge");
let labels = labels! {
String::from("deployment") => deployment_id.to_string(),
String::from("network") => network,
String::from("shard") => shard
};
let deployment_head = registry
.new_gauge(
"deployment_head",
"Tracks the most recent block number processed by a deployment",
labels.clone(),
)
.expect("failed to create `deployment_head` gauge");
Self {
deployment_head,
reverted_blocks,
stopwatch,
}
}
}
/// Notifications about the chain head advancing. The block ingestor sends
/// an update on this stream whenever the head of the underlying chain
/// changes. The updates have no payload, receivers should call
/// `Store::chain_head_ptr` to check what the latest block is.
pub type ChainHeadUpdateStream = Box<dyn Stream<Item = ()> + Send + Unpin>;
pub trait ChainHeadUpdateListener: Send + Sync + 'static {
/// Subscribe to chain head updates for the given network.
fn subscribe(&self, network: String, logger: Logger) -> ChainHeadUpdateStream;
}
#[cfg(test)]
mod test {
use std::{collections::HashSet, task::Poll};
use futures03::{Stream, StreamExt, TryStreamExt};
use crate::{
blockchain::mock::{MockBlock, MockBlockchain},
ext::futures::{CancelableError, SharedCancelGuard, StreamExtension},
};
use super::{
BlockStream, BlockStreamError, BlockStreamEvent, BlockWithTriggers, BufferedBlockStream,
FirehoseCursor,
};
#[derive(Debug)]
struct TestStream {
number: u64,
}
impl BlockStream<MockBlockchain> for TestStream {
fn buffer_size_hint(&self) -> usize {
1
}
}
impl Stream for TestStream {
type Item = Result<BlockStreamEvent<MockBlockchain>, BlockStreamError>;
fn poll_next(
mut self: std::pin::Pin<&mut Self>,
_cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Option<Self::Item>> {
self.number += 1;
Poll::Ready(Some(Ok(BlockStreamEvent::ProcessBlock(
BlockWithTriggers::<MockBlockchain> {
block: MockBlock {
number: self.number - 1,
},
trigger_data: vec![],
},
FirehoseCursor::None,
))))
}
}
#[crate::test]
async fn consume_stream() {
let initial_block = 100;
let buffer_size = 5;
let stream = Box::new(TestStream {
number: initial_block,
});
let guard = SharedCancelGuard::new();
let mut stream = BufferedBlockStream::spawn_from_stream(buffer_size, stream)
.map_err(CancelableError::Error)
.cancelable(&guard);
let mut blocks = HashSet::<MockBlock>::new();
let mut count = 0;
loop {
match stream.next().await {
None if blocks.is_empty() => panic!("None before blocks"),
Some(Err(CancelableError::Cancel)) => {
assert!(guard.is_canceled(), "Guard shouldn't be called yet");
break;
}
Some(Ok(BlockStreamEvent::ProcessBlock(block_triggers, _))) => {
let block = block_triggers.block;
blocks.insert(block.clone());
count += 1;
if block.number > initial_block + buffer_size as u64 {
guard.cancel();
}
}
_ => panic!("Should not happen"),
};
}
assert!(
blocks.len() > buffer_size,
"should consume at least a full buffer, consumed {}",
count
);
assert_eq!(count, blocks.len(), "should not have duplicated blocks");
}
}