yellowstone-block-machine 0.3.0

State machine for reconstructing Solana blocks from Geyser events
Documentation
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
use {
    crate::{
        dragonsmouth::wrapper::{BlocksStateMachineWrapper, RESERVED_FILTER_NAME},
        state_machine::{
            BlockStateMachineOutput, DeadBlockDetected, ForkDetected, SlotCommitmentStatusUpdate,
        },
    },
    derive_more::From,
    futures_util::{Sink, SinkExt, Stream, StreamExt, stream::BoxStream},
    rustc_hash::FxHashMap,
    solana_clock::Slot,
    solana_commitment_config::CommitmentLevel,
    std::{
        cmp::Ordering,
        collections::{HashMap, VecDeque},
        sync::Arc,
    },
    tokio::sync::mpsc,
    tokio_stream::wrappers::ReceiverStream,
    tokio_util::sync::PollSender,
    tonic::async_trait,
    yellowstone_grpc_client::{GeyserGrpcClient, GeyserGrpcClientError, Interceptor},
    yellowstone_grpc_proto::geyser::{
        CommitmentLevel as ProtoCommitmentLevel, SubscribeRequest, SubscribeRequestFilterAccounts,
        SubscribeRequestFilterSlots, SubscribeRequestFilterTransactions, SubscribeUpdate,
        subscribe_update::UpdateOneof,
    },
};

pub type BlockMachineResult = Result<BlockMachineOutput, BlockMachineError>;

#[async_trait]
pub trait GeyserGrpcExt<F> {
    fn this(&mut self) -> &mut GeyserGrpcClient<F>;

    async fn subscribe_block(
        &mut self,
        subscribe_request: SubscribeRequest,
    ) -> Result<mpsc::Receiver<BlockMachineResult>, GeyserGrpcClientError>;
}

pub struct SimplifiedSubscribeRequest {
    pub accounts: HashMap<String, SubscribeRequestFilterAccounts>,
    pub transactions: HashMap<String, SubscribeRequestFilterTransactions>,
}

#[derive(Debug, Default)]
struct BlockAccumulator {
    events: Vec<Arc<SubscribeUpdate>>,
    account_idx_map: Vec<usize>,
    transaction_idx_map: Vec<usize>,
    entry_idx_map: Vec<usize>,
}

impl BlockAccumulator {
    fn finish(self, slot: Slot) -> Block {
        Block {
            slot,
            events: self.events,
            account_idx_map: self.account_idx_map,
            transaction_idx_map: self.transaction_idx_map,
            entry_idx_map: self.entry_idx_map,
        }
    }
}

#[derive(Debug, Clone)]
pub struct Block {
    pub slot: Slot,
    pub events: Vec<Arc<SubscribeUpdate>>,
    pub account_idx_map: Vec<usize>,
    pub transaction_idx_map: Vec<usize>,
    entry_idx_map: Vec<usize>,
}

impl Block {
    pub fn txn_len(&self) -> usize {
        self.transaction_idx_map.len()
    }

    pub fn account_len(&self) -> usize {
        self.account_idx_map.len()
    }

    pub fn entry_len(&self) -> usize {
        self.entry_idx_map.len()
    }

    pub fn is_empty(&self) -> bool {
        self.events.is_empty()
    }

    pub fn len(&self) -> usize {
        self.events.len()
    }
}

struct BlockStream {
    inner: ReceiverStream<Result<BlockMachineOutput, BlockMachineError>>,
}

impl Stream for BlockStream {
    type Item = Result<BlockMachineOutput, BlockMachineError>;

    fn poll_next(
        mut self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Option<Self::Item>> {
        self.inner.poll_next_unpin(cx)
    }
}

pub const DEFAULT_SUBSCRIBE_BLOCK_CHANNEL_CAPACITY: usize = 1_000_000;

///
/// Errors that can occur in the block machine processing.
///
#[derive(Debug, thiserror::Error)]
pub enum BlockMachineError {
    ///
    /// An error originating from the gRPC stream.
    ///
    #[error(transparent)]
    GrpcError(#[from] tonic::Status),
}

///
/// The different types of outputs produced by the Dragon's mouth block machine.
///
#[derive(Debug, From)]
pub enum BlockMachineOutput {
    ///
    /// A fully reconstructed block, ready for processing.
    ///
    FrozenBlock(Block),
    ///
    /// An update on the commitment status of a slot.
    /// Note: This is sent when the slot reaches or exceeds the minimum commitment level set during initialization.
    /// It is guaranteed that the block for this slot has been sent before this update.
    ///  
    SlotCommitmentUpdate(SlotCommitmentStatusUpdate),
    ///
    /// A notification that a fork has been detected.
    ///
    ForkDetected(ForkDetected),
    ///
    /// A notification that a dead block has been detected.
    /// Note: All Dead blocks are Forks, but not all Forks are Dead blocks.
    /// Dead blocks mostly come from corrupted entries early in the replay process of a slot.
    ///
    DeadBlockDetect(DeadBlockDetected),
}

#[async_trait]
impl<F> GeyserGrpcExt<F> for GeyserGrpcClient<F>
where
    F: Interceptor + Send + Sync + 'static,
{
    fn this(&mut self) -> &mut GeyserGrpcClient<F> {
        self
    }

    async fn subscribe_block(
        &mut self,
        mut subscribe_request: SubscribeRequest,
    ) -> Result<mpsc::Receiver<BlockMachineResult>, GeyserGrpcClientError> {
        let proto_commitment_level =
            ProtoCommitmentLevel::try_from(subscribe_request.commitment.unwrap_or(0))
                .expect("Invalid commitment level in subscribe request");

        assert!(
            subscribe_request.blocks.is_empty(),
            "custom `blocks` filter is not compatible with block machine"
        );

        assert!(
            subscribe_request.slots.is_empty(),
            "custom `slots` filter is not compatible with block machine"
        );

        let commitment_level = match proto_commitment_level {
            ProtoCommitmentLevel::Processed => CommitmentLevel::Processed,
            ProtoCommitmentLevel::Confirmed => CommitmentLevel::Confirmed,
            ProtoCommitmentLevel::Finalized => CommitmentLevel::Finalized,
        };
        subscribe_request.slots.insert(
            RESERVED_FILTER_NAME.to_owned(),
            SubscribeRequestFilterSlots {
                interslot_updates: Some(true),
                ..Default::default()
            },
        );
        subscribe_request
            .blocks_meta
            .insert(RESERVED_FILTER_NAME.to_owned(), Default::default());

        subscribe_request
            .entry
            .insert(RESERVED_FILTER_NAME.to_owned(), Default::default());

        subscribe_request.commitment = Some(0); // Processed

        let (_sink, source) = self
            .this()
            .subscribe_with_request(Some(subscribe_request))
            .await?;

        let source = source.boxed();

        let machine = BlocksStateMachineWrapper::default();

        let (tx, rx) = mpsc::channel(DEFAULT_SUBSCRIBE_BLOCK_CHANNEL_CAPACITY);

        let driver = AsyncDragonsmouthDriver {
            min_commitment_level: commitment_level,
            source,
            sink: PollSender::new(tx),
            machine,
            storage: InMemoryBlockStore::default(),
        };

        tokio::spawn(async move {
            let _ = driver.run().await;
        });

        Ok(rx)
    }
}

///
/// The driver that connects the Geyser gRPC stream to the DragonsmouthBlockMachine.
///
/// It reads from the gRPC stream, feeds events into the state machine, and sends outputs
/// to the provided sink.
pub struct AsyncDragonsmouthDriver<Sink> {
    min_commitment_level: CommitmentLevel,
    source: BoxStream<'static, Result<SubscribeUpdate, tonic::Status>>,
    sink: Sink,
    machine: BlocksStateMachineWrapper,
    storage: InMemoryBlockStore,
}

#[derive(Debug, thiserror::Error)]
pub enum DragonsmouthDriverError<SnkErr> {
    #[error(transparent)]
    SinkError(SnkErr),
}

fn compare_commitment(cl1: CommitmentLevel, cl2: CommitmentLevel) -> Ordering {
    match (cl1, cl2) {
        (CommitmentLevel::Processed, CommitmentLevel::Processed) => Ordering::Equal,
        (CommitmentLevel::Confirmed, CommitmentLevel::Confirmed) => Ordering::Equal,
        (CommitmentLevel::Finalized, CommitmentLevel::Finalized) => Ordering::Equal,
        (CommitmentLevel::Processed, _) => Ordering::Less,
        (CommitmentLevel::Confirmed, CommitmentLevel::Processed) => Ordering::Greater,
        (CommitmentLevel::Finalized, CommitmentLevel::Processed) => Ordering::Greater,
        (CommitmentLevel::Finalized, CommitmentLevel::Confirmed) => Ordering::Greater,
        (CommitmentLevel::Confirmed, CommitmentLevel::Finalized) => Ordering::Less,
    }
}

impl<Snk, SnkErr> AsyncDragonsmouthDriver<Snk>
where
    Snk: Sink<Result<BlockMachineOutput, BlockMachineError>, Error = SnkErr>
        + Unpin
        + Send
        + 'static,
    SnkErr: std::error::Error + Send,
{
    fn insert_into_storage(&mut self, event: SubscribeUpdate) {
        let SubscribeUpdate {
            filters,
            created_at,
            update_oneof,
        } = event;
        let Some(update_oneof) = update_oneof else {
            return;
        };
        match update_oneof {
            UpdateOneof::Account(update) => {
                let slot = update.slot;
                self.storage.insert_block_data(
                    slot,
                    SubscribeUpdate {
                        filters,
                        created_at,
                        update_oneof: Some(UpdateOneof::Account(update)),
                    },
                );
            }
            UpdateOneof::Transaction(update) => {
                let slot = update.slot;
                self.storage.insert_block_data(
                    slot,
                    SubscribeUpdate {
                        filters,
                        created_at,
                        update_oneof: Some(UpdateOneof::Transaction(update)),
                    },
                );
            }
            UpdateOneof::Entry(update) => {
                let slot = update.slot;
                if filters.iter().any(|k| k != RESERVED_FILTER_NAME) {
                    self.storage.insert_block_data(
                        slot,
                        SubscribeUpdate {
                            filters,
                            created_at,
                            update_oneof: Some(UpdateOneof::Entry(update)),
                        },
                    );
                }
            }
            _ => {}
        }
    }

    async fn process_state_machine_output(
        &mut self,
        outputs: &mut VecDeque<BlockStateMachineOutput>,
    ) -> Result<(), SnkErr> {
        while let Some(output) = outputs.pop_front() {
            match output {
                BlockStateMachineOutput::FrozenBlock(frozen_block) => {
                    let slot = frozen_block.slot;
                    self.storage.mark_block_as_frozen(slot);
                }
                BlockStateMachineOutput::SlotStatus(slot_status) => {
                    let slot = slot_status.slot;
                    let cl = slot_status.commitment;
                    match compare_commitment(cl, self.min_commitment_level) {
                        Ordering::Less => continue,
                        _ => {
                            let commitment_level_update = SlotCommitmentStatusUpdate {
                                parent_slot: slot_status.parent_slot,
                                slot: slot_status.slot,
                                commitment: cl,
                            };
                            if let Some(block) = self.storage.finish_slot(slot) {
                                self.sink
                                    .send(Ok(BlockMachineOutput::FrozenBlock(block)))
                                    .await?;
                            }

                            self.sink
                                .send(Ok(BlockMachineOutput::SlotCommitmentUpdate(
                                    commitment_level_update,
                                )))
                                .await?;
                        }
                    }
                }
                BlockStateMachineOutput::ForksDetected(fork_detected) => {
                    self.storage.remove_slot(fork_detected.slot);
                    self.sink
                        .send(Ok(BlockMachineOutput::ForkDetected(fork_detected)))
                        .await?;
                }
                BlockStateMachineOutput::DeadSlotDetected(dead_block) => {
                    self.storage.remove_slot(dead_block.slot);
                    self.sink
                        .send(Ok(BlockMachineOutput::DeadBlockDetect(dead_block)))
                        .await?;
                }
            }
        }
        Ok(())
    }

    pub async fn run(mut self) -> Result<(), DragonsmouthDriverError<SnkErr>> {
        let mut batch = VecDeque::with_capacity(10);

        loop {
            if !batch.is_empty() {
                self.process_state_machine_output(&mut batch)
                    .await
                    .map_err(DragonsmouthDriverError::SinkError)?;
            }

            tokio::select! {
                maybe = self.source.next() => {
                    match maybe {
                        Some(result) => {
                            match result {
                                Ok(ev) => {
                                    if self.machine.handle_new_geyser_event(&ev).is_ok() {
                                        self.insert_into_storage(ev);
                                    }
                                },
                                Err(e) => {
                                    let _ = self.sink.send(Err(BlockMachineError::GrpcError(e))).await;
                                    break;
                                }
                            }
                        }
                        None => {
                            break;
                        }
                    }
                }
            }
            self.machine.drain_unprocess_output(&mut batch);
        }
        self.machine.drain_unprocess_output(&mut batch);

        self.process_state_machine_output(&mut batch)
            .await
            .map_err(DragonsmouthDriverError::SinkError)?;
        Ok(())
    }
}

///
/// An in-memory store for blocks being reconstructed.
///
/// It maintains active blocks (currently being reconstructed) and frozen blocks (fully reconstructed).
#[derive(Default)]
pub struct InMemoryBlockStore {
    active_block_map: FxHashMap<Slot, BlockAccumulator>,
    frozen_block_map: FxHashMap<Slot, BlockAccumulator>,
}

impl InMemoryBlockStore {
    fn insert_block_data(&mut self, slot: Slot, update: SubscribeUpdate) {
        let block = self.active_block_map.entry(slot).or_default();
        let idx = block.events.len();
        match &update.update_oneof {
            Some(UpdateOneof::Account(_)) => {
                block.account_idx_map.push(idx);
            }
            Some(UpdateOneof::Transaction(_)) => {
                block.transaction_idx_map.push(idx);
            }
            Some(UpdateOneof::Entry(_)) => {
                block.entry_idx_map.push(idx);
            }
            _ => {
                unreachable!("unsupported update type for block data insertion");
            }
        }
        block.events.push(Arc::new(update));
    }

    fn mark_block_as_frozen(&mut self, slot: Slot) {
        let Some(block) = self.active_block_map.remove(&slot) else {
            return;
        };
        self.frozen_block_map.insert(slot, block);
    }

    fn remove_slot(&mut self, slot: Slot) {
        self.active_block_map.remove(&slot);
        self.frozen_block_map.remove(&slot);
    }

    fn finish_slot(&mut self, slot: Slot) -> Option<Block> {
        let acc = self.frozen_block_map.remove(&slot)?;
        Some(acc.finish(slot))
    }
}