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 solana_program::hash::Hash;
37use solana_transaction_status::Rewards;
38use {
39 crate::{error::CarbonResult, metrics::MetricsCollection},
40 async_trait::async_trait,
41 solana_account::Account,
42 solana_pubkey::Pubkey,
43 solana_sdk::transaction::VersionedTransaction,
44 solana_signature::Signature,
45 solana_transaction_status::TransactionStatusMeta,
46 std::sync::Arc,
47 tokio_util::sync::CancellationToken,
48};
49
50/// Defines the interface for data sources that produce updates for accounts,
51/// transactions, and account deletions.
52///
53/// The `Datasource` trait represents a data source that can be consumed
54/// asynchronously within a pipeline. Implementations of this trait are
55/// responsible for fetching updates and sending them through a channel to be
56/// processed further. Each datasource specifies the types of updates it
57/// supports by implementing the `update_types` method.
58///
59/// # Required Methods
60///
61/// - `consume`: Initiates the asynchronous consumption of updates. This method
62/// should send updates through the provided `sender` channel.
63/// - `update_types`: Returns a list of `UpdateType` variants indicating the
64/// types of updates the datasource can provide.
65///
66/// # Example
67///
68/// ```ignore
69/// use std::sync::Arc;
70/// use carbon_core::datasource::UpdateType;
71/// use carbon_core::datasource::Update;
72/// use carbon_core::error::CarbonResult;
73/// use carbon_core::metrics::MetricsCollection;
74/// use carbon_core::datasource::Datasource;
75/// use tokio_util::sync::CancellationToken;
76/// use async_trait::async_trait;
77///
78/// #[async_trait]
79/// impl Datasource for MyDatasource {
80/// async fn consume(
81/// &self,
82/// sender: &tokio::sync::mpsc::UnboundedSender<Update>,
83/// cancellation_token: CancellationToken,
84/// metrics: Arc<MetricsCollection>,
85/// ) -> CarbonResult<tokio::task::AbortHandle> {
86/// // Implement update fetching logic
87/// }
88///
89/// fn update_types(&self) -> Vec<UpdateType> {
90/// vec![UpdateType::AccountUpdate, UpdateType::Transaction]
91/// }
92/// }
93/// ```
94///
95/// # Notes
96///
97/// - This trait is marked with `async_trait`, so implementations must be
98/// asynchronous.
99/// - The `consume` method should handle errors and retries to ensure robust
100/// update delivery.
101#[async_trait]
102pub trait Datasource: Send + Sync {
103 async fn consume(
104 &self,
105 sender: &tokio::sync::mpsc::Sender<Update>,
106 cancellation_token: CancellationToken,
107 metrics: Arc<MetricsCollection>,
108 ) -> CarbonResult<()>;
109
110 fn update_types(&self) -> Vec<UpdateType>;
111}
112
113/// Represents a data update in the `carbon-core` pipeline, encompassing
114/// different update types.
115///
116/// - `Account`: Represents an update to an account's data.
117/// - `Transaction`: Represents a transaction-related update, including
118/// transaction metadata.
119/// - `AccountDeletion`: Represents an event where an account has been deleted.
120#[derive(Debug, Clone)]
121pub enum Update {
122 Account(AccountUpdate),
123 Transaction(Box<TransactionUpdate>),
124 AccountDeletion(AccountDeletion),
125 BlockDetails(BlockDetails),
126}
127
128/// Enumerates the types of updates a datasource can provide.
129///
130/// The `UpdateType` enum categorizes updates into three types:
131/// - `AccountUpdate`: Indicates that the datasource provides account updates.
132/// - `Transaction`: Indicates that the datasource provides transaction updates.
133/// - `AccountDeletion`: Indicates that the datasource provides account deletion
134/// events.
135#[derive(Debug, Clone, PartialEq, Eq)]
136pub enum UpdateType {
137 AccountUpdate,
138 Transaction,
139 AccountDeletion,
140}
141
142/// Represents an update to a Solana account, including its public key, data,
143/// and slot information.
144///
145/// The `AccountUpdate` struct encapsulates the essential information for an
146/// account update, containing the account's `pubkey`, `account` data, and the
147/// `slot` at which the update occurred.
148///
149/// - `pubkey`: The public key of the account being updated.
150/// - `account`: The new state of the account.
151/// - `slot`: The slot number in which this account update was recorded.
152#[derive(Debug, Clone)]
153pub struct AccountUpdate {
154 pub pubkey: Pubkey,
155 pub account: Account,
156 pub slot: u64,
157}
158
159/// Represents the details of a Solana block, including its slot, hashes, rewards, and timing information.
160///
161/// The `BlockDetails` struct encapsulates the essential information for a block,
162/// providing details about its slot, blockhashes, rewards, and other metadata.
163///
164/// - `slot`: The slot number in which this block was recorded.
165/// - `previous_block_hash`: The hash of the previous block in the blockchain.
166/// - `block_hash`: The hash of the current block.
167/// - `rewards`: Optional rewards information associated with the block, such as staking rewards.
168/// - `num_reward_partitions`: Optional number of reward partitions in the block.
169/// - `block_time`: Optional Unix timestamp indicating when the block was processed.
170/// - `block_height`: Optional height of the block in the blockchain.#[derive(Debug, Clone)]
171#[derive(Debug, Clone)]
172pub struct BlockDetails {
173 pub slot: u64,
174 pub block_hash: Option<Hash>,
175 pub previous_block_hash: Option<Hash>,
176 pub rewards: Option<Rewards>,
177 pub num_reward_partitions: Option<u64>,
178 pub block_time: Option<i64>,
179 pub block_height: Option<u64>,
180}
181
182/// Represents the deletion of a Solana account, containing the account's public
183/// key and slot information.
184///
185/// The `AccountDeletion` struct indicates that an account has been removed from
186/// the blockchain state, providing the `pubkey` of the deleted account and the
187/// `slot` in which the deletion occurred.
188///
189/// - `pubkey`: The public key of the deleted account.
190/// - `slot`: The slot number in which the account was deleted.
191#[derive(Debug, Clone)]
192pub struct AccountDeletion {
193 pub pubkey: Pubkey,
194 pub slot: u64,
195}
196
197/// Represents a transaction update in the Solana network, including transaction
198/// metadata, status, slot information and block time.
199///
200/// The `TransactionUpdate` struct provides detailed information about a
201/// transaction, including its `signature`, `transaction` data, `meta` status,
202/// and the `slot` where it was recorded. Additionally, it includes a `is_vote`
203/// flag to indicate whether the transaction is a voting transaction.
204///
205/// - `signature`: The unique signature of the transaction.
206/// - `transaction`: The complete `VersionedTransaction` data of the
207/// transaction.
208/// - `meta`: Metadata about the transaction's status, such as fee information
209/// and logs.
210/// - `is_vote`: A boolean indicating whether the transaction is a vote.
211/// - `slot`: The slot number in which the transaction was recorded.
212/// - `block_time`: The Unix timestamp of when the transaction was processed.
213/// - `block_hash`: Block hash that can be used to detect a fork.
214///
215/// Note: The `block_time` field may not be returned in all scenarios.
216#[derive(Debug, Clone)]
217pub struct TransactionUpdate {
218 pub signature: Signature,
219 pub transaction: VersionedTransaction, // TODO: replace with solana_transaction crate after 2.2.0 release
220 pub meta: TransactionStatusMeta,
221 pub is_vote: bool,
222 pub slot: u64,
223 pub block_time: Option<i64>,
224 pub block_hash: Option<Hash>,
225}