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
//! Provides traits and structures for managing and consuming data updates from
//! various sources.
//!
//! The `datasource` module defines the `Datasource` trait and associated data
//! types for handling updates related to accounts, transactions, and account
//! deletions. This module allows for flexible data ingestion from various
//! Solana data sources, enabling integration with the `carbon-core` processing
//! pipeline.
//!
//! # Overview
//!
//! The core component of this module is the `Datasource` trait, which
//! represents an interface for consuming data updates asynchronously.
//! Implementations of `Datasource` provide the logic for fetching data updates
//! and delivering them via a channel to the pipeline. Different types of
//! updates are represented by the `Update` enum, including:
//! - `AccountUpdate`: Represents updates to accounts, including the account's
//! public key, slot, and other account data.
//! - `TransactionUpdate`: Represents transaction updates, including transaction
//! details, signature, and status metadata.
//! - `AccountDeletion`: Represents account deletion events, indicating when an
//! account is removed from the blockchain state.
//!
//! The module also includes the `UpdateType` enum to categorize the kinds of
//! updates that a data source can provide.
//!
//! # Notes
//!
//! - The `Datasource` trait is asynchronous and should be used within a Tokio
//! runtime.
//! - Use the `Update` enum to encapsulate data updates of different types. This
//! helps centralize handling of all update kinds in the pipeline.
//! - Ensure implementations handle errors gracefully, especially when fetching
//! data and sending updates to the pipeline.
use Hash;
use Rewards;
use ;
/// Defines the interface for data sources that produce updates for accounts,
/// transactions, and account deletions.
///
/// The `Datasource` trait represents a data source that can be consumed
/// asynchronously within a pipeline. Implementations of this trait are
/// responsible for fetching updates and sending them through a channel to be
/// processed further. Each datasource specifies the types of updates it
/// supports by implementing the `update_types` method.
///
/// # Required Methods
///
/// - `consume`: Initiates the asynchronous consumption of updates. This method
/// should send updates through the provided `sender` channel.
/// - `update_types`: Returns a list of `UpdateType` variants indicating the
/// types of updates the datasource can provide.
///
/// # Example
///
/// ```ignore
/// use std::sync::Arc;
/// use carbon_core::datasource::UpdateType;
/// use carbon_core::datasource::Update;
/// use carbon_core::error::CarbonResult;
/// use carbon_core::metrics::MetricsCollection;
/// use carbon_core::datasource::Datasource;
/// use tokio_util::sync::CancellationToken;
/// use async_trait::async_trait;
///
/// #[async_trait]
/// impl Datasource for MyDatasource {
/// async fn consume(
/// &self,
/// sender: &tokio::sync::mpsc::UnboundedSender<Update>,
/// cancellation_token: CancellationToken,
/// metrics: Arc<MetricsCollection>,
/// ) -> CarbonResult<tokio::task::AbortHandle> {
/// // Implement update fetching logic
/// }
///
/// fn update_types(&self) -> Vec<UpdateType> {
/// vec![UpdateType::AccountUpdate, UpdateType::Transaction]
/// }
/// }
/// ```
///
/// # Notes
///
/// - This trait is marked with `async_trait`, so implementations must be
/// asynchronous.
/// - The `consume` method should handle errors and retries to ensure robust
/// update delivery.
/// A unique identifier for a datasource in the pipeline.
///
/// Datasource IDs are used to track the source of data updates and enable
/// filtering of updates based on their origin. This is particularly useful
/// when you have multiple datasources and want to process updates selectively.
///
/// # Examples
///
/// Creating a datasource with a unique ID:
/// ```
/// use carbon_core::datasource::DatasourceId;
///
/// let id = DatasourceId::new_unique();
/// println!("Generated ID: {:?}", id);
/// ```
///
/// Creating a datasource with a named ID:
/// ```
/// use carbon_core::datasource::DatasourceId;
///
/// let id = DatasourceId::new_named("mainnet-rpc");
/// println!("Named ID: {:?}", id);
/// ```
///
/// Using with filters:
/// ```
/// use carbon_core::{datasource::DatasourceId, filter::DatasourceFilter};
///
/// let datasource_id = DatasourceId::new_named("testnet");
/// let filter = DatasourceFilter::new(datasource_id);
/// ```
;
/// Represents a data update in the `carbon-core` pipeline, encompassing
/// different update types.
///
/// - `Account`: Represents an update to an account's data.
/// - `Transaction`: Represents a transaction-related update, including
/// transaction metadata.
/// - `AccountDeletion`: Represents an event where an account has been deleted.
/// Enumerates the types of updates a datasource can provide.
///
/// The `UpdateType` enum categorizes updates into three types:
/// - `AccountUpdate`: Indicates that the datasource provides account updates.
/// - `Transaction`: Indicates that the datasource provides transaction updates.
/// - `AccountDeletion`: Indicates that the datasource provides account deletion
/// events.
/// Represents an update to a Solana account, including its public key, data,
/// and slot information.
///
/// The `AccountUpdate` struct encapsulates the essential information for an
/// account update, containing the account's `pubkey`, `account` data, and the
/// `slot` at which the update occurred.
///
/// - `pubkey`: The public key of the account being updated.
/// - `account`: The new state of the account.
/// - `slot`: The slot number in which this account update was recorded.
/// - `transaction_signature`: Signature of the transaction that caused the update.
/// Represents the details of a Solana block, including its slot, hashes, rewards, and timing information.
///
/// The `BlockDetails` struct encapsulates the essential information for a block,
/// providing details about its slot, blockhashes, rewards, and other metadata.
///
/// - `slot`: The slot number in which this block was recorded.
/// - `previous_block_hash`: The hash of the previous block in the blockchain.
/// - `block_hash`: The hash of the current block.
/// - `rewards`: Optional rewards information associated with the block, such as staking rewards.
/// - `num_reward_partitions`: Optional number of reward partitions in the block.
/// - `block_time`: Optional Unix timestamp indicating when the block was processed.
/// - `block_height`: Optional height of the block in the blockchain.#[derive(Debug, Clone)]
/// Represents the deletion of a Solana account, containing the account's public
/// key and slot information.
///
/// The `AccountDeletion` struct indicates that an account has been removed from
/// the blockchain state, providing the `pubkey` of the deleted account and the
/// `slot` in which the deletion occurred.
///
/// - `pubkey`: The public key of the deleted account.
/// - `slot`: The slot number in which the account was deleted.
/// - `transaction_signature`: Signature of the transaction that caused the update.
/// Represents a transaction update in the Solana network, including transaction
/// metadata, status, slot information and block time.
///
/// The `TransactionUpdate` struct provides detailed information about a
/// transaction, including its `signature`, `transaction` data, `meta` status,
/// and the `slot` where it was recorded. Additionally, it includes a `is_vote`
/// flag to indicate whether the transaction is a voting transaction.
///
/// - `signature`: The unique signature of the transaction.
/// - `transaction`: The complete `VersionedTransaction` data of the
/// transaction.
/// - `meta`: Metadata about the transaction's status, such as fee information
/// and logs.
/// - `is_vote`: A boolean indicating whether the transaction is a vote.
/// - `slot`: The slot number in which the transaction was recorded.
/// - `block_time`: The Unix timestamp of when the transaction was processed.
/// - `block_hash`: Block hash that can be used to detect a fork.
///
/// Note: The `block_time` field may not be returned in all scenarios.