carbon_core/
datasource.rs

1//! Provides traits and structures for managing and consuming data updates from
2//! various sources.
3//!
4//! The `datasource` module defines the `Datasource` trait and associated data
5//! types for handling updates related to accounts, transactions, and account
6//! deletions. This module allows for flexible data ingestion from various
7//! Solana data sources, enabling integration with the `carbon-core` processing
8//! pipeline.
9//!
10//! # Overview
11//!
12//! The core component of this module is the `Datasource` trait, which
13//! represents an interface for consuming data updates asynchronously.
14//! Implementations of `Datasource` provide the logic for fetching data updates
15//! and delivering them via a channel to the pipeline. Different types of
16//! updates are represented by the `Update` enum, including:
17//! - `AccountUpdate`: Represents updates to accounts, including the account's
18//!   public key, slot, and other account data.
19//! - `TransactionUpdate`: Represents transaction updates, including transaction
20//!   details, signature, and status metadata.
21//! - `AccountDeletion`: Represents account deletion events, indicating when an
22//!   account is removed from the blockchain state.
23//!
24//! The module also includes the `UpdateType` enum to categorize the kinds of
25//! updates that a data source can provide.
26//!
27//! # Notes
28//!
29//! - The `Datasource` trait is asynchronous and should be used within a Tokio
30//!   runtime.
31//! - Use the `Update` enum to encapsulate data updates of different types. This
32//!   helps centralize handling of all update kinds in the pipeline.
33//! - Ensure implementations handle errors gracefully, especially when fetching
34//!   data and sending updates to the pipeline.
35
36use {
37    crate::{error::CarbonResult, metrics::MetricsCollection},
38    async_trait::async_trait,
39    solana_sdk::{pubkey::Pubkey, signature::Signature},
40    std::sync::Arc,
41    tokio_util::sync::CancellationToken,
42};
43
44/// Defines the interface for data sources that produce updates for accounts,
45/// transactions, and account deletions.
46///
47/// The `Datasource` trait represents a data source that can be consumed
48/// asynchronously within a pipeline. Implementations of this trait are
49/// responsible for fetching updates and sending them through a channel to be
50/// processed further. Each datasource specifies the types of updates it
51/// supports by implementing the `update_types` method.
52///
53/// # Required Methods
54///
55/// - `consume`: Initiates the asynchronous consumption of updates. This method
56///   should send updates through the provided `sender` channel.
57/// - `update_types`: Returns a list of `UpdateType` variants indicating the
58///   types of updates the datasource can provide.
59///
60/// # Example
61///
62/// ```rust
63/// #[async_trait]
64/// impl Datasource for MyDatasource {
65///     async fn consume(
66///         &self,
67///         sender: &tokio::sync::mpsc::UnboundedSender<Update>,
68///         cancellation_token: CancellationToken,
69///         metrics: Arc<MetricsCollection>,
70///     ) -> CarbonResult<tokio::task::AbortHandle> {
71///         // Implement update fetching logic
72///     }
73///
74///     fn update_types(&self) -> Vec<UpdateType> {
75///         vec![UpdateType::AccountUpdate, UpdateType::Transaction]
76///     }
77/// }
78/// ```
79///
80/// # Notes
81///
82/// - This trait is marked with `async_trait`, so implementations must be
83///   asynchronous.
84/// - The `consume` method should handle errors and retries to ensure robust
85///   update delivery.
86#[async_trait]
87pub trait Datasource: Send + Sync {
88    async fn consume(
89        &self,
90        sender: &tokio::sync::mpsc::UnboundedSender<Update>,
91        cancellation_token: CancellationToken,
92        metrics: Arc<MetricsCollection>,
93    ) -> CarbonResult<()>;
94
95    fn update_types(&self) -> Vec<UpdateType>;
96}
97
98/// Represents a data update in the `carbon-core` pipeline, encompassing
99/// different update types.
100///
101/// - `Account`: Represents an update to an account's data.
102/// - `Transaction`: Represents a transaction-related update, including
103///   transaction metadata.
104/// - `AccountDeletion`: Represents an event where an account has been deleted.
105#[derive(Debug, Clone)]
106pub enum Update {
107    Account(AccountUpdate),
108    Transaction(Box<TransactionUpdate>),
109    AccountDeletion(AccountDeletion),
110}
111
112/// Enumerates the types of updates a datasource can provide.
113///
114/// The `UpdateType` enum categorizes updates into three types:
115/// - `AccountUpdate`: Indicates that the datasource provides account updates.
116/// - `Transaction`: Indicates that the datasource provides transaction updates.
117/// - `AccountDeletion`: Indicates that the datasource provides account deletion
118///   events.
119#[derive(Debug, Clone, PartialEq, Eq)]
120pub enum UpdateType {
121    AccountUpdate,
122    Transaction,
123    AccountDeletion,
124}
125
126/// Represents an update to a Solana account, including its public key, data,
127/// and slot information.
128///
129/// The `AccountUpdate` struct encapsulates the essential information for an
130/// account update, containing the account's `pubkey`, `account` data, and the
131/// `slot` at which the update occurred.
132///
133/// - `pubkey`: The public key of the account being updated.
134/// - `account`: The new state of the account.
135/// - `slot`: The slot number in which this account update was recorded.
136#[derive(Debug, Clone)]
137pub struct AccountUpdate {
138    pub pubkey: Pubkey,
139    pub account: solana_sdk::account::Account,
140    pub slot: u64,
141}
142
143/// Represents the deletion of a Solana account, containing the account's public
144/// key and slot information.
145///
146/// The `AccountDeletion` struct indicates that an account has been removed from
147/// the blockchain state, providing the `pubkey` of the deleted account and the
148/// `slot` in which the deletion occurred.
149///
150/// - `pubkey`: The public key of the deleted account.
151/// - `slot`: The slot number in which the account was deleted.
152#[derive(Debug, Clone)]
153pub struct AccountDeletion {
154    pub pubkey: Pubkey,
155    pub slot: u64,
156}
157
158/// Represents a transaction update in the Solana network, including transaction
159/// metadata, status, and slot information.
160///
161/// The `TransactionUpdate` struct provides detailed information about a
162/// transaction, including its `signature`, `transaction` data, `meta` status,
163/// and the `slot` where it was recorded. Additionally, it includes a `is_vote`
164/// flag to indicate whether the transaction is a voting transaction.
165///
166/// - `signature`: The unique signature of the transaction.
167/// - `transaction`: The complete `VersionedTransaction` data of the
168///   transaction.
169/// - `meta`: Metadata about the transaction's status, such as fee information
170///   and logs.
171/// - `is_vote`: A boolean indicating whether the transaction is a vote.
172/// - `slot`: The slot number in which the transaction was recorded.
173#[derive(Debug, Clone)]
174pub struct TransactionUpdate {
175    pub signature: Signature,
176    pub transaction: solana_sdk::transaction::VersionedTransaction,
177    pub meta: solana_transaction_status::TransactionStatusMeta,
178    pub is_vote: bool,
179    pub slot: u64,
180}