-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathmod.rs
More file actions
370 lines (328 loc) · 11 KB
/
mod.rs
File metadata and controls
370 lines (328 loc) · 11 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
use std::{collections::HashMap, fmt::Display};
use crossbeam_channel::Sender;
use jsonrpc_core::Result as RpcError;
use locker::SurfnetSvmLocker;
use solana_account::Account;
use solana_account_decoder::{UiAccount, UiAccountEncoding};
use solana_client::{
rpc_config::RpcTransactionLogsFilter,
rpc_filter::RpcFilterType,
rpc_response::{RpcKeyedAccount, RpcLogsResponse},
};
use solana_clock::Slot;
use solana_commitment_config::CommitmentLevel;
use solana_epoch_info::EpochInfo;
use solana_pubkey::Pubkey;
use solana_signature::Signature;
use solana_transaction::versioned::VersionedTransaction;
use solana_transaction_error::TransactionError;
use solana_transaction_status::{EncodedConfirmedTransactionWithStatusMeta, TransactionStatus};
use svm::SurfnetSvm;
use crate::{
error::{SurfpoolError, SurfpoolResult},
types::{GeyserAccountUpdate, TransactionWithStatusMeta},
};
pub mod locker;
pub mod noop_program;
pub mod remote;
pub mod surfnet_lite_svm;
pub mod svm;
pub const FINALIZATION_SLOT_THRESHOLD: u64 = 31;
pub const SLOTS_PER_EPOCH: u64 = 432000;
pub type AccountFactory = Box<dyn Fn(SurfnetSvmLocker) -> GetAccountResult + Send + Sync>;
/// Slot status for geyser plugin notifications.
/// Mirrors `agave_geyser_plugin_interface::geyser_plugin_interface::SlotStatus`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GeyserSlotStatus {
/// Slot is being processed
Processed,
/// Slot has been rooted (finalized)
Rooted,
/// Slot has been confirmed
Confirmed,
}
/// Block metadata for geyser plugin notifications.
#[derive(Debug, Clone)]
pub struct GeyserBlockMetadata {
pub slot: Slot,
pub blockhash: String,
pub parent_slot: Slot,
pub parent_blockhash: String,
pub block_time: Option<i64>,
pub block_height: Option<u64>,
pub executed_transaction_count: u64,
pub entry_count: u64,
}
/// Entry info for geyser plugin notifications.
/// Surfpool emits one entry per block (simplified model).
#[derive(Debug, Clone)]
pub struct GeyserEntryInfo {
pub slot: Slot,
pub index: usize,
pub num_hashes: u64,
pub hash: Vec<u8>,
pub executed_transaction_count: u64,
pub starting_transaction_index: usize,
}
#[allow(clippy::large_enum_variant)]
pub enum GeyserEvent {
NotifyTransaction(TransactionWithStatusMeta, Option<VersionedTransaction>),
UpdateAccount(GeyserAccountUpdate),
/// Account update sent at startup (before block production begins).
/// These updates should be sent to geyser plugins with is_startup=true.
StartupAccountUpdate(GeyserAccountUpdate),
/// Notify plugins that startup is complete.
EndOfStartup,
/// Update slot status (processed, confirmed, rooted/finalized).
UpdateSlotStatus {
slot: Slot,
parent: Option<Slot>,
status: GeyserSlotStatus,
},
/// Notify plugins of block metadata.
NotifyBlockMetadata(GeyserBlockMetadata),
/// Notify plugins of entry execution.
NotifyEntry(GeyserEntryInfo),
}
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
pub struct BlockIdentifier {
pub index: u64,
pub hash: String,
}
impl BlockIdentifier {
pub fn zero() -> Self {
Self::new(
0,
"0000000000000000000000000000000000000000000000000000000000000000",
)
}
pub fn new(index: u64, hash: &str) -> Self {
Self {
index,
hash: hash.to_string(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BlockHeader {
pub hash: String,
pub previous_blockhash: String,
pub parent_slot: Slot,
pub block_time: i64,
pub block_height: u64,
pub signatures: Vec<Signature>,
}
#[derive(PartialEq, Eq, Clone)]
pub enum SurfnetDataConnection {
Offline,
Connected(String, EpochInfo),
}
pub type SignatureSubscriptionData = (
SignatureSubscriptionType,
Sender<(Slot, Option<TransactionError>)>,
);
pub type AccountSubscriptionData =
HashMap<Pubkey, Vec<(Option<UiAccountEncoding>, Sender<UiAccount>)>>;
pub type ProgramSubscriptionData = HashMap<
Pubkey,
Vec<(
Option<UiAccountEncoding>,
Option<Vec<RpcFilterType>>,
Sender<RpcKeyedAccount>,
)>,
>;
pub type LogsSubscriptionData = (
CommitmentLevel,
RpcTransactionLogsFilter,
Sender<(Slot, RpcLogsResponse)>,
);
pub type SnapshotSubscriptionData = Sender<SnapshotImportNotification>;
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SnapshotImportNotification {
pub snapshot_id: String,
pub status: SnapshotImportStatus,
pub accounts_loaded: u64,
pub total_accounts: u64,
pub error: Option<String>,
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum SnapshotImportStatus {
Started,
InProgress,
Completed,
Failed,
}
#[derive(Debug, Clone, PartialEq)]
pub enum SignatureSubscriptionType {
Received,
Commitment(CommitmentLevel),
}
impl Display for SignatureSubscriptionType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SignatureSubscriptionType::Received => write!(f, "received"),
SignatureSubscriptionType::Commitment(level) => write!(f, "{level}"),
}
}
}
type DoUpdateSvm = bool;
#[derive(Clone, Debug)]
/// Represents the result of a get_account operation.
pub enum GetAccountResult {
/// Represents that the account was not found.
None(Pubkey),
/// Represents that the account was found.
/// The `DoUpdateSvm` flag indicates whether the SVM should be updated after this account is found.
/// This is useful for cases where the account was fetched from a remote source and needs to be
/// updated in the SVM to reflect the latest state. However, when the account is found locally,
/// it likely does not need to be updated in the SVM.
FoundAccount(Pubkey, Account, DoUpdateSvm),
FoundProgramAccount((Pubkey, Account), (Pubkey, Option<Account>)),
FoundTokenAccount((Pubkey, Account), (Pubkey, Option<Account>)),
}
impl GetAccountResult {
pub fn expected_data(&self) -> &Vec<u8> {
match &self {
Self::None(_) => unreachable!(),
Self::FoundAccount(_, account, _)
| Self::FoundProgramAccount((_, account), _)
| Self::FoundTokenAccount((_, account), _) => &account.data,
}
}
pub fn apply_update<T>(&mut self, update: T) -> RpcError<()>
where
T: Fn(&mut Account) -> RpcError<()>,
{
match self {
Self::None(_) => unreachable!(),
Self::FoundAccount(_, account, do_update_account) => {
update(account)?;
*do_update_account = true;
}
Self::FoundProgramAccount((_, account), _) => {
update(account)?;
}
Self::FoundTokenAccount((_, account), _) => {
update(account)?;
}
}
Ok(())
}
pub fn map_account(self) -> SurfpoolResult<Account> {
match self {
Self::None(pubkey) => Err(SurfpoolError::account_not_found(pubkey)),
Self::FoundAccount(_, account, _)
| Self::FoundProgramAccount((_, account), _)
| Self::FoundTokenAccount((_, account), _) => Ok(account),
}
}
#[allow(clippy::type_complexity)]
pub fn map_account_with_token_data(
self,
) -> Option<((Pubkey, Account), Option<(Pubkey, Option<Account>)>)> {
match self {
Self::None(_) => None,
Self::FoundAccount(pubkey, account, _) => Some(((pubkey, account), None)),
Self::FoundProgramAccount((pubkey, account), _) => Some(((pubkey, account), None)),
Self::FoundTokenAccount((pubkey, account), token_data) => {
Some(((pubkey, account), Some(token_data)))
}
}
}
pub const fn is_none(&self) -> bool {
matches!(self, Self::None(_))
}
pub const fn requires_update(&self) -> bool {
match self {
Self::None(_) => false,
Self::FoundAccount(_, _, do_update) => *do_update,
Self::FoundProgramAccount(_, _) => true,
Self::FoundTokenAccount(_, _) => true,
}
}
}
impl From<GetAccountResult> for Result<Account, SurfpoolError> {
fn from(value: GetAccountResult) -> Self {
value.map_account()
}
}
impl SignatureSubscriptionType {
pub const fn received() -> Self {
SignatureSubscriptionType::Received
}
pub const fn processed() -> Self {
SignatureSubscriptionType::Commitment(CommitmentLevel::Processed)
}
pub const fn confirmed() -> Self {
SignatureSubscriptionType::Commitment(CommitmentLevel::Confirmed)
}
pub const fn finalized() -> Self {
SignatureSubscriptionType::Commitment(CommitmentLevel::Finalized)
}
}
#[allow(clippy::large_enum_variant)]
pub enum GetTransactionResult {
None(Signature),
FoundTransaction(
Signature,
EncodedConfirmedTransactionWithStatusMeta,
TransactionStatus,
),
}
impl GetTransactionResult {
pub fn found_transaction(
signature: Signature,
tx: EncodedConfirmedTransactionWithStatusMeta,
latest_absolute_slot: u64,
) -> Self {
let is_finalized = latest_absolute_slot >= tx.slot + FINALIZATION_SLOT_THRESHOLD;
let is_confirmed = latest_absolute_slot >= tx.slot + 1;
let (confirmation_status, confirmations) = if is_finalized {
(
Some(solana_transaction_status::TransactionConfirmationStatus::Finalized),
None,
)
} else if is_confirmed {
(
Some(solana_transaction_status::TransactionConfirmationStatus::Confirmed),
Some((latest_absolute_slot - tx.slot) as usize),
)
} else {
(
Some(solana_transaction_status::TransactionConfirmationStatus::Processed),
Some((latest_absolute_slot - tx.slot) as usize),
)
};
let status = TransactionStatus {
slot: tx.slot,
confirmations,
status: tx
.transaction
.clone()
.meta
.map_or(Ok(()), |m| m.status.map_err(|e| e.into())),
err: tx
.transaction
.clone()
.meta
.and_then(|m| m.err.map(|e| e.into())),
confirmation_status,
};
Self::FoundTransaction(signature, tx, status)
}
pub const fn is_none(&self) -> bool {
matches!(self, Self::None(_))
}
pub fn map_found_transaction(&self) -> SurfpoolResult<TransactionStatus> {
match self {
Self::None(sig) => Err(SurfpoolError::transaction_not_found(sig)),
Self::FoundTransaction(_, _, status) => Ok(status.clone()),
}
}
pub fn map_some_transaction_status(&self) -> Option<TransactionStatus> {
match self {
Self::None(_) => None,
Self::FoundTransaction(_, _, status) => Some(status.clone()),
}
}
}