surfpool_core/rpc/minimal.rs
1use jsonrpc_core::{BoxFuture, Result};
2use jsonrpc_derive::rpc;
3use solana_client::{
4 rpc_config::{
5 RpcContextConfig, RpcGetVoteAccountsConfig, RpcLeaderScheduleConfig,
6 RpcLeaderScheduleConfigWrapper,
7 },
8 rpc_custom_error::RpcCustomError,
9 rpc_response::{
10 RpcIdentity, RpcLeaderSchedule, RpcResponseContext, RpcSnapshotSlotInfo,
11 RpcVoteAccountStatus,
12 },
13};
14use solana_clock::Slot;
15use solana_commitment_config::{CommitmentConfig, CommitmentLevel};
16use solana_epoch_info::EpochInfo;
17use solana_rpc_client_api::response::Response as RpcResponse;
18
19use super::{RunloopContext, SurfnetRpcContext};
20use crate::{
21 rpc::{State, utils::verify_pubkey},
22 surfnet::{
23 FINALIZATION_SLOT_THRESHOLD, GetAccountResult, SURFPOOL_IDENTITY_PUBKEY,
24 locker::SvmAccessContext,
25 },
26};
27
28const SURFPOOL_VERSION: &str = env!("CARGO_PKG_VERSION");
29
30#[derive(Serialize, Deserialize, Clone, PartialEq, Eq)]
31#[serde(rename_all = "kebab-case")]
32pub struct SurfpoolRpcVersionInfo {
33 /// The current version of surfpool
34 pub surfnet_version: String,
35 /// The current version of solana-core
36 pub solana_core: String,
37 /// first 4 bytes of the FeatureSet identifier
38 pub feature_set: Option<u32>,
39}
40
41#[rpc]
42pub trait Minimal {
43 type Metadata;
44
45 /// Returns the balance (in lamports) of the account at the provided public key.
46 ///
47 /// This endpoint queries the current or historical balance of an account, depending on the optional commitment level provided in the config.
48 ///
49 /// ## Parameters
50 /// - `pubkey_str`: The base-58 encoded public key of the account to query.
51 /// - `_config` *(optional)*: [`RpcContextConfig`] specifying commitment level and/or minimum context slot.
52 ///
53 /// ## Returns
54 /// An [`RpcResponse<u64>`] where the value is the balance in lamports.
55 ///
56 /// ## Example Request
57 /// ```json
58 /// {
59 /// "jsonrpc": "2.0",
60 /// "id": 1,
61 /// "method": "getBalance",
62 /// "params": [
63 /// "4Nd1mXUmh23rQk8VN7wM9hEnfxqrrB1yrn11eW9gMoVr"
64 /// ]
65 /// }
66 /// ```
67 ///
68 /// ## Example Response
69 /// ```json
70 /// {
71 /// "jsonrpc": "2.0",
72 /// "result": {
73 /// "context": {
74 /// "slot": 1085597
75 /// },
76 /// "value": 20392800
77 /// },
78 /// "id": 1
79 /// }
80 /// ```
81 ///
82 /// # Notes
83 /// - 1 SOL = 1,000,000,000 lamports.
84 /// - Use commitment level in the config to specify whether the balance should be fetched from processed, confirmed, or finalized state.
85 ///
86 /// # See Also
87 /// - `getAccountInfo`, `getTokenAccountBalance`
88 #[rpc(meta, name = "getBalance")]
89 fn get_balance(
90 &self,
91 meta: Self::Metadata,
92 pubkey_str: String,
93 _config: Option<RpcContextConfig>,
94 ) -> BoxFuture<Result<RpcResponse<u64>>>;
95
96 /// Returns information about the current epoch.
97 ///
98 /// This endpoint provides epoch-related data such as the current epoch number, the total number of slots in the epoch,
99 /// the current slot index within the epoch, and the absolute slot number.
100 ///
101 /// ## Parameters
102 /// - `config` *(optional)*: [`RpcContextConfig`] for specifying commitment level and/or minimum context slot.
103 ///
104 /// ## Returns
105 /// An [`EpochInfo`] struct containing information about the current epoch.
106 ///
107 /// ## Example Request
108 /// ```json
109 /// {
110 /// "jsonrpc": "2.0",
111 /// "id": 1,
112 /// "method": "getEpochInfo"
113 /// }
114 /// ```
115 ///
116 /// ## Example Response
117 /// ```json
118 /// {
119 /// "jsonrpc": "2.0",
120 /// "result": {
121 /// "epoch": 278,
122 /// "slotIndex": 423,
123 /// "slotsInEpoch": 432000,
124 /// "absoluteSlot": 124390823,
125 /// "blockHeight": 18962432,
126 /// "transactionCount": 981234523
127 /// },
128 /// "id": 1
129 /// }
130 /// ```
131 ///
132 /// # Notes
133 /// - The `slotIndex` is the current slot's position within the epoch.
134 /// - `slotsInEpoch` may vary due to network adjustments (e.g., warm-up periods).
135 /// - The `commitment` field in the config can influence how recent the returned data is.
136 ///
137 /// # See Also
138 /// - `getEpochSchedule`, `getSlot`, `getBlockHeight`
139 #[rpc(meta, name = "getEpochInfo")]
140 fn get_epoch_info(
141 &self,
142 meta: Self::Metadata,
143 config: Option<RpcContextConfig>,
144 ) -> Result<EpochInfo>;
145
146 /// Returns the genesis hash of the blockchain.
147 ///
148 /// The genesis hash is a unique identifier that represents the state of the blockchain at the genesis block (the very first block).
149 /// This can be used to validate the integrity of the blockchain and ensure that a node is operating with the correct blockchain data.
150 ///
151 /// ## Parameters
152 /// - None.
153 ///
154 /// ## Returns
155 /// A `String` containing the base-58 encoded genesis hash.
156 ///
157 /// ## Example Request
158 /// ```json
159 /// {
160 /// "jsonrpc": "2.0",
161 /// "id": 1,
162 /// "method": "getGenesisHash"
163 /// }
164 /// ```
165 ///
166 /// ## Example Response
167 /// ```json
168 /// {
169 /// "jsonrpc": "2.0",
170 /// "result": "5eymX3jrWXcKqD1tsB2BzAB6gX9LP2pLrpVG6KwBSoZJ"
171 /// "id": 1
172 /// }
173 /// ```
174 ///
175 /// # Notes
176 /// - The genesis hash is a critical identifier for validating the blockchain’s origin and initial state.
177 /// - This endpoint does not require any parameters and provides a quick way to verify the genesis hash for blockchain verification or initial setup.
178 ///
179 /// # See Also
180 /// - `getEpochInfo`, `getBlock`, `getClusterNodes`
181 #[rpc(meta, name = "getGenesisHash")]
182 fn get_genesis_hash(&self, meta: Self::Metadata) -> BoxFuture<Result<String>>;
183
184 /// Returns the health status of the blockchain node.
185 ///
186 /// This method checks the health of the node and returns a status indicating whether the node is in a healthy state
187 /// or if it is experiencing any issues such as being out of sync with the network or encountering any failures.
188 ///
189 /// ## Parameters
190 /// - None.
191 ///
192 /// ## Returns
193 /// A `String` indicating the health status of the node:
194 /// - `"ok"`: The node is healthy and synchronized with the network.
195 /// - `"failed"`: The node is not healthy or is experiencing issues.
196 ///
197 /// ## Example Request
198 /// ```json
199 /// {
200 /// "jsonrpc": "2.0",
201 /// "id": 1,
202 /// "method": "getHealth"
203 /// }
204 /// ```
205 ///
206 /// ## Example Response
207 /// ```json
208 /// {
209 /// "jsonrpc": "2.0",
210 /// "result": "ok",
211 /// "id": 1
212 /// }
213 /// ```
214 ///
215 /// # Notes
216 /// - The `"ok"` response means that the node is fully operational and synchronized with the blockchain.
217 /// - The `"failed"` response indicates that the node is either out of sync, has encountered an error, or is not functioning properly.
218 /// - This is typically used to monitor the health of the node in production environments.
219 ///
220 /// # See Also
221 /// - `getGenesisHash`, `getEpochInfo`, `getBlock`
222 #[rpc(meta, name = "getHealth")]
223 fn get_health(&self, meta: Self::Metadata) -> Result<String>;
224
225 /// Returns the identity (public key) of the node.
226 ///
227 /// This method retrieves the current identity of the node, which is represented by a public key.
228 /// The identity is used to uniquely identify the node on the network.
229 ///
230 /// ## Parameters
231 /// - None.
232 ///
233 /// ## Returns
234 /// A `RpcIdentity` object containing the identity of the node:
235 /// - `identity`: The base-58 encoded public key of the node's identity.
236 ///
237 /// ## Example Request
238 /// ```json
239 /// {
240 /// "jsonrpc": "2.0",
241 /// "id": 1,
242 /// "method": "getIdentity"
243 /// }
244 /// ```
245 ///
246 /// ## Example Response
247 /// ```json
248 /// {
249 /// "jsonrpc": "2.0",
250 /// "result": {
251 /// "identity": "Base58EncodedPublicKeyHere"
252 /// },
253 /// "id": 1
254 /// }
255 /// ```
256 ///
257 /// # Notes
258 /// - The identity returned is a base-58 encoded public key representing the current node.
259 /// - This identity is often used for network identification and security.
260 ///
261 /// # See Also
262 /// - `getGenesisHash`, `getHealth`, `getBlock`
263 #[rpc(meta, name = "getIdentity")]
264 fn get_identity(&self, meta: Self::Metadata) -> Result<RpcIdentity>;
265
266 /// Returns the current slot of the ledger.
267 ///
268 /// This method retrieves the current slot number in the blockchain, which represents a point in the ledger's history.
269 /// Slots are used to organize and validate the timing of transactions in the network.
270 ///
271 /// ## Parameters
272 /// - `config` (optional): Configuration options for the request, such as commitment level or context slot. Defaults to `None`.
273 ///
274 /// ## Returns
275 /// A `Slot` value representing the current slot of the ledger.
276 ///
277 /// ## Example Request
278 /// ```json
279 /// {
280 /// "jsonrpc": "2.0",
281 /// "id": 1,
282 /// "method": "getSlot"
283 /// }
284 /// ```
285 ///
286 /// ## Example Response
287 /// ```json
288 /// {
289 /// "jsonrpc": "2.0",
290 /// "result": 12345678,
291 /// "id": 1
292 /// }
293 /// ```
294 ///
295 /// # Notes
296 /// - The slot represents the position in the ledger. It increments over time as new blocks are produced.
297 ///
298 /// # See Also
299 /// - `getBlock`, `getEpochInfo`, `getGenesisHash`
300 #[rpc(meta, name = "getSlot")]
301 fn get_slot(&self, meta: Self::Metadata, config: Option<RpcContextConfig>) -> Result<Slot>;
302
303 /// Returns the current block height.
304 ///
305 /// This method retrieves the height of the most recent block in the ledger, which is an indicator of how many blocks have been added to the blockchain. The block height is the number of blocks that have been produced since the genesis block.
306 ///
307 /// ## Parameters
308 /// - `config` (optional): Configuration options for the request, such as commitment level or context slot. Defaults to `None`.
309 ///
310 /// ## Returns
311 /// A `u64` representing the current block height of the ledger.
312 ///
313 /// ## Example Request
314 /// ```json
315 /// {
316 /// "jsonrpc": "2.0",
317 /// "id": 1,
318 /// "method": "getBlockHeight"
319 /// }
320 /// ```
321 ///
322 /// ## Example Response
323 /// ```json
324 /// {
325 /// "jsonrpc": "2.0",
326 /// "result": 12345678,
327 /// "id": 1
328 /// }
329 /// ```
330 ///
331 /// # Notes
332 /// - The block height reflects the number of blocks produced in the ledger, starting from the genesis block. It is incremented each time a new block is added.
333 ///
334 /// # See Also
335 /// - `getSlot`, `getEpochInfo`, `getGenesisHash`
336 #[rpc(meta, name = "getBlockHeight")]
337 fn get_block_height(
338 &self,
339 meta: Self::Metadata,
340 config: Option<RpcContextConfig>,
341 ) -> Result<u64>;
342
343 /// Returns information about the highest snapshot slot.
344 ///
345 /// This method retrieves information about the most recent snapshot slot, which refers to the slot in the blockchain where the most recent snapshot has been taken. A snapshot is a point-in-time capture of the state of the ledger, allowing for quicker validation of the state without processing every transaction.
346 ///
347 /// ## Parameters
348 /// - `meta`: Metadata passed with the request, such as the client’s request context.
349 ///
350 /// ## Returns
351 /// A `RpcSnapshotSlotInfo` containing information about the highest snapshot slot.
352 ///
353 /// ## Example Request
354 /// ```json
355 /// {
356 /// "jsonrpc": "2.0",
357 /// "id": 1,
358 /// "method": "getHighestSnapshotSlot"
359 /// }
360 /// ```
361 ///
362 /// ## Example Response
363 /// ```json
364 /// {
365 /// "jsonrpc": "2.0",
366 /// "result": {
367 /// "slot": 987654,
368 /// "root": "A9B7F1A4D1D55D0635B905E5AB6341C5D9F7F4D2A1160C53B5647B1E3259BB24"
369 /// },
370 /// "id": 1
371 /// }
372 /// ```
373 ///
374 /// # Notes
375 /// - The snapshot slot represents the most recent snapshot in the blockchain and is used for more efficient state validation and recovery.
376 /// - The result also includes the root, which is the blockhash at the snapshot point.
377 ///
378 /// # See Also
379 /// - `getBlock`, `getSnapshotInfo`
380 #[rpc(meta, name = "getHighestSnapshotSlot")]
381 fn get_highest_snapshot_slot(&self, meta: Self::Metadata) -> Result<RpcSnapshotSlotInfo>;
382
383 /// Returns the total number of transactions processed by the blockchain.
384 ///
385 /// This method retrieves the number of transactions that have been processed in the blockchain up to the current point. It provides a snapshot of the transaction throughput and can be useful for monitoring and performance analysis.
386 ///
387 /// ## Parameters
388 /// - `meta`: Metadata passed with the request, such as the client’s request context.
389 /// - `config`: Optional configuration for the request, such as commitment settings or minimum context slot.
390 ///
391 /// ## Returns
392 /// A `u64` representing the total transaction count.
393 ///
394 /// ## Example Request
395 /// ```json
396 /// {
397 /// "jsonrpc": "2.0",
398 /// "id": 1,
399 /// "method": "getTransactionCount"
400 /// }
401 /// ```
402 ///
403 /// ## Example Response
404 /// ```json
405 /// {
406 /// "jsonrpc": "2.0",
407 /// "result": 1234567890,
408 /// "id": 1
409 /// }
410 /// ```
411 ///
412 /// # Notes
413 /// - This method gives a cumulative count of all transactions in the blockchain from the start of the network.
414 ///
415 /// # See Also
416 /// - `getBlockHeight`, `getEpochInfo`
417 #[rpc(meta, name = "getTransactionCount")]
418 fn get_transaction_count(
419 &self,
420 meta: Self::Metadata,
421 config: Option<RpcContextConfig>,
422 ) -> Result<u64>;
423
424 /// Returns the current version of the server or application.
425 ///
426 /// This method retrieves the version information for the server or application. It provides details such as the version number and additional metadata that can help with compatibility checks or updates.
427 ///
428 /// ## Parameters
429 /// - `meta`: Metadata passed with the request, such as the client’s request context.
430 ///
431 /// ## Returns
432 /// A `SurfpoolRpcVersionInfo` object containing the version details.
433 ///
434 /// ## Example Request
435 /// ```json
436 /// {
437 /// "jsonrpc": "2.0",
438 /// "id": 1,
439 /// "method": "getVersion"
440 /// }
441 /// ```
442 ///
443 /// ## Example Response
444 /// ```json
445 /// {
446 /// "jsonrpc": "2.0",
447 /// "result": {
448 /// "surfnet_version": "1.2.3",
449 /// "solana_core": "1.9.0",
450 /// "feature_set": 12345
451 /// },
452 /// "id": 1
453 /// }
454 /// ```
455 ///
456 /// # Notes
457 /// - The version information typically includes the version number of `surfpool`, the version of `solana-core`, and a `feature_set` identifier (first 4 bytes).
458 /// - The `feature_set` field may not always be present, depending on whether a feature set identifier is available.
459 ///
460 /// # See Also
461 /// - `getHealth`, `getIdentity`
462 #[rpc(meta, name = "getVersion")]
463 fn get_version(&self, meta: Self::Metadata) -> Result<SurfpoolRpcVersionInfo>;
464
465 /// Returns vote account information.
466 ///
467 /// This method retrieves the current status of vote accounts, including information about the validator’s vote account and whether it is delinquent. The response includes vote account details such as the stake, commission, vote history, and more.
468 ///
469 /// ## Parameters
470 /// - `meta`: Metadata passed with the request, such as the client’s request context.
471 /// - `config`: Optional configuration parameters, such as specific vote account addresses or commitment settings.
472 ///
473 /// ## Returns
474 /// A `RpcVoteAccountStatus` object containing details about the current and delinquent vote accounts.
475 ///
476 /// ## Example Request
477 /// ```json
478 /// {
479 /// "jsonrpc": "2.0",
480 /// "id": 1,
481 /// "method": "getVoteAccounts",
482 /// "params": [{}]
483 /// }
484 /// ```
485 ///
486 /// ## Example Response
487 /// ```json
488 /// {
489 /// "jsonrpc": "2.0",
490 /// "result": {
491 /// "current": [
492 /// {
493 /// "votePubkey": "votePubkeyBase58",
494 /// "nodePubkey": "nodePubkeyBase58",
495 /// "activatedStake": 1000000,
496 /// "commission": 5,
497 /// "epochVoteAccount": true,
498 /// "epochCredits": [[1, 1000, 900], [2, 1100, 1000]],
499 /// "lastVote": 1000,
500 /// "rootSlot": 1200
501 /// }
502 /// ],
503 /// "delinquent": [
504 /// {
505 /// "votePubkey": "delinquentVotePubkeyBase58",
506 /// "nodePubkey": "delinquentNodePubkeyBase58",
507 /// "activatedStake": 0,
508 /// "commission": 10,
509 /// "epochVoteAccount": false,
510 /// "epochCredits": [[1, 500, 400]],
511 /// "lastVote": 0,
512 /// "rootSlot": 0
513 /// }
514 /// ]
515 /// },
516 /// "id": 1
517 /// }
518 /// ```
519 ///
520 /// # Notes
521 /// - The `current` field contains details about vote accounts that are active and have current stake.
522 /// - The `delinquent` field contains details about vote accounts that have become delinquent due to inactivity or other issues.
523 /// - The `epochCredits` field contains historical voting data.
524 ///
525 /// # See Also
526 /// - `getHealth`, `getIdentity`, `getVersion`
527 #[rpc(meta, name = "getVoteAccounts")]
528 fn get_vote_accounts(
529 &self,
530 meta: Self::Metadata,
531 config: Option<RpcGetVoteAccountsConfig>,
532 ) -> Result<RpcVoteAccountStatus>;
533
534 /// Returns the leader schedule for the given configuration or slot.
535 ///
536 /// This method retrieves the leader schedule for the given slot or configuration, providing a map of validator identities to slot indices within a given range.
537 ///
538 /// ## Parameters
539 /// - `meta`: Metadata passed with the request, such as the client’s request context.
540 /// - `options`: Optional configuration wrapper, which can either be a specific slot or a full configuration.
541 /// - `config`: Optional configuration containing the validator identity and commitment level.
542 ///
543 /// ## Returns
544 /// An `Option<RpcLeaderSchedule>` containing a map of leader identities (base-58 encoded pubkeys) to slot indices, relative to the first epoch slot.
545 ///
546 /// ## Example Request
547 /// ```json
548 /// {
549 /// "jsonrpc": "2.0",
550 /// "id": 1,
551 /// "method": "getLeaderSchedule",
552 /// "params": [{}]
553 /// }
554 /// ```
555 ///
556 /// ## Example Response
557 /// ```json
558 /// {
559 /// "jsonrpc": "2.0",
560 /// "result": {
561 /// "votePubkey1": [0, 2, 4],
562 /// "votePubkey2": [1, 3, 5]
563 /// },
564 /// "id": 1
565 /// }
566 /// ```
567 ///
568 /// # Notes
569 /// - The returned map contains validator identities as keys (base-58 encoded strings), with slot indices as values.
570 ///
571 /// # See Also
572 /// - `getSlot`, `getBlockHeight`, `getEpochInfo`
573 #[rpc(meta, name = "getLeaderSchedule")]
574 fn get_leader_schedule(
575 &self,
576 meta: Self::Metadata,
577 options: Option<RpcLeaderScheduleConfigWrapper>,
578 config: Option<RpcLeaderScheduleConfig>,
579 ) -> Result<Option<RpcLeaderSchedule>>;
580}
581
582#[derive(Clone)]
583pub struct SurfpoolMinimalRpc;
584impl Minimal for SurfpoolMinimalRpc {
585 type Metadata = Option<RunloopContext>;
586
587 fn get_balance(
588 &self,
589 meta: Self::Metadata,
590 pubkey_str: String,
591 _config: Option<RpcContextConfig>, // TODO: use config
592 ) -> BoxFuture<Result<RpcResponse<u64>>> {
593 let pubkey = match verify_pubkey(&pubkey_str) {
594 Ok(res) => res,
595 Err(e) => return e.into(),
596 };
597
598 let SurfnetRpcContext {
599 svm_locker,
600 remote_ctx,
601 } = match meta.get_rpc_context(CommitmentConfig::confirmed()) {
602 Ok(res) => res,
603 Err(e) => return e.into(),
604 };
605
606 Box::pin(async move {
607 let SvmAccessContext {
608 slot,
609 inner: account_update,
610 ..
611 } = svm_locker.get_account(&remote_ctx, &pubkey, None).await?;
612
613 let balance = match &account_update {
614 GetAccountResult::FoundAccount(_, account, _)
615 | GetAccountResult::FoundProgramAccount((_, account), _)
616 | GetAccountResult::FoundTokenAccount((_, account), _) => account.lamports,
617 GetAccountResult::None(_) => 0,
618 };
619
620 svm_locker.write_account_update(account_update);
621
622 Ok(RpcResponse {
623 context: RpcResponseContext::new(slot),
624 value: balance,
625 })
626 })
627 }
628
629 fn get_epoch_info(
630 &self,
631 meta: Self::Metadata,
632 _config: Option<RpcContextConfig>,
633 ) -> Result<EpochInfo> {
634 meta.with_svm_reader(|svm_reader| svm_reader.latest_epoch_info.clone())
635 .map_err(Into::into)
636 }
637
638 fn get_genesis_hash(&self, meta: Self::Metadata) -> BoxFuture<Result<String>> {
639 let SurfnetRpcContext {
640 svm_locker,
641 remote_ctx,
642 } = match meta.get_rpc_context(()) {
643 Ok(res) => res,
644 Err(e) => return e.into(),
645 };
646
647 Box::pin(async move {
648 Ok(svm_locker
649 .get_genesis_hash(&remote_ctx.map(|(client, _)| client))
650 .await?
651 .inner
652 .to_string())
653 })
654 }
655
656 fn get_health(&self, _meta: Self::Metadata) -> Result<String> {
657 // todo: we could check the time from the state clock and compare
658 Ok("ok".to_string())
659 }
660
661 fn get_identity(&self, _meta: Self::Metadata) -> Result<RpcIdentity> {
662 Ok(RpcIdentity {
663 identity: SURFPOOL_IDENTITY_PUBKEY.to_string(),
664 })
665 }
666
667 fn get_slot(&self, meta: Self::Metadata, config: Option<RpcContextConfig>) -> Result<Slot> {
668 let config = config.unwrap_or_default();
669 let latest_absolute_slot = meta
670 .with_svm_reader(|svm_reader| svm_reader.get_latest_absolute_slot())
671 .map_err(Into::<jsonrpc_core::Error>::into)?;
672 let slot = match config.commitment.unwrap_or_default().commitment {
673 CommitmentLevel::Processed => latest_absolute_slot,
674 CommitmentLevel::Confirmed => latest_absolute_slot - 1,
675 CommitmentLevel::Finalized => latest_absolute_slot - FINALIZATION_SLOT_THRESHOLD,
676 };
677
678 if let Some(min_context_slot) = config.min_context_slot {
679 if slot < min_context_slot {
680 return Err(RpcCustomError::MinContextSlotNotReached {
681 context_slot: min_context_slot,
682 }
683 .into());
684 }
685 }
686
687 Ok(slot)
688 }
689
690 fn get_block_height(
691 &self,
692 meta: Self::Metadata,
693 config: Option<RpcContextConfig>,
694 ) -> Result<u64> {
695 let config = config.unwrap_or_default();
696
697 if let Some(target_slot) = config.min_context_slot {
698 let block_exists = meta
699 .with_svm_reader(|svm_reader| svm_reader.blocks.contains_key(&target_slot))
700 .map_err(Into::<jsonrpc_core::Error>::into)?;
701
702 if !block_exists {
703 return Err(jsonrpc_core::Error::invalid_params(format!(
704 "Block not found for slot: {}",
705 target_slot
706 )));
707 }
708 }
709
710 meta.with_svm_reader(|svm_reader| {
711 if let Some(target_slot) = config.min_context_slot {
712 if let Some(block_header) = svm_reader.blocks.get(&target_slot) {
713 return block_header.block_height;
714 }
715 }
716
717 // default behavior: return the latest block height with commitment adjustments
718 let latest_block_height = svm_reader.latest_epoch_info.block_height;
719
720 match config.commitment.unwrap_or_default().commitment {
721 CommitmentLevel::Processed => latest_block_height,
722 CommitmentLevel::Confirmed => latest_block_height.saturating_sub(1),
723 CommitmentLevel::Finalized => {
724 latest_block_height.saturating_sub(FINALIZATION_SLOT_THRESHOLD)
725 }
726 }
727 })
728 .map_err(Into::into)
729 }
730
731 fn get_highest_snapshot_slot(&self, _meta: Self::Metadata) -> Result<RpcSnapshotSlotInfo> {
732 Err(jsonrpc_core::Error {
733 code: jsonrpc_core::ErrorCode::ServerError(-32008),
734 message: "No snapshot".into(),
735 data: None,
736 })
737 }
738
739 fn get_transaction_count(
740 &self,
741 meta: Self::Metadata,
742 _config: Option<RpcContextConfig>,
743 ) -> Result<u64> {
744 meta.with_svm_reader(|svm_reader| svm_reader.transactions_processed)
745 .map_err(Into::into)
746 }
747
748 fn get_version(&self, _: Self::Metadata) -> Result<SurfpoolRpcVersionInfo> {
749 let version = solana_version::Version::default();
750
751 Ok(SurfpoolRpcVersionInfo {
752 surfnet_version: SURFPOOL_VERSION.to_string(),
753 solana_core: version.to_string(),
754 feature_set: Some(version.feature_set),
755 })
756 }
757
758 fn get_vote_accounts(
759 &self,
760 _meta: Self::Metadata,
761 config: Option<RpcGetVoteAccountsConfig>,
762 ) -> Result<RpcVoteAccountStatus> {
763 // validate inputs if provided
764 if let Some(config) = config {
765 // validate vote_pubkey if provided
766 if let Some(vote_pubkey_str) = config.vote_pubkey {
767 verify_pubkey(&vote_pubkey_str)?;
768 }
769 }
770
771 // Return empty vote accounts
772 Ok(RpcVoteAccountStatus {
773 current: vec![],
774 delinquent: vec![],
775 })
776 }
777
778 fn get_leader_schedule(
779 &self,
780 meta: Self::Metadata,
781 options: Option<RpcLeaderScheduleConfigWrapper>,
782 config: Option<RpcLeaderScheduleConfig>,
783 ) -> Result<Option<RpcLeaderSchedule>> {
784 let (slot, maybe_config) = options.map(|options| options.unzip()).unwrap_or_default();
785 let config = maybe_config.or(config).unwrap_or_default();
786
787 if let Some(ref identity) = config.identity {
788 let _ = verify_pubkey(identity)?;
789 }
790
791 let svm_locker = meta.get_svm_locker()?;
792 let epoch_info = svm_locker.get_epoch_info();
793
794 let slot = slot.unwrap_or(epoch_info.absolute_slot);
795
796 let first_slot_in_epoch = epoch_info
797 .absolute_slot
798 .saturating_sub(epoch_info.slot_index);
799 let last_slot_in_epoch = first_slot_in_epoch + epoch_info.slots_in_epoch.saturating_sub(1);
800
801 if slot < first_slot_in_epoch || slot > last_slot_in_epoch {
802 return Ok(None);
803 }
804
805 // return empty leader schedule if epoch found and everything is valid
806 Ok(Some(std::collections::HashMap::new()))
807 }
808}
809
810#[cfg(test)]
811mod tests {
812 use jsonrpc_core::ErrorCode;
813 use solana_client::rpc_config::RpcContextConfig;
814 use solana_commitment_config::CommitmentConfig;
815 use solana_epoch_info::EpochInfo;
816 use solana_pubkey::Pubkey;
817 use solana_sdk::genesis_config::GenesisConfig;
818
819 use super::*;
820 use crate::tests::helpers::TestSetup;
821
822 #[test]
823 fn test_get_block_height_processed_commitment() {
824 let setup = TestSetup::new(SurfpoolMinimalRpc);
825
826 let config = RpcContextConfig {
827 commitment: Some(CommitmentConfig::processed()),
828 min_context_slot: None,
829 };
830
831 let expected_height = setup
832 .rpc
833 .get_block_height(Some(setup.context.clone()), Some(config));
834 assert!(expected_height.is_ok());
835
836 let latest_epoch_block_height = setup
837 .context
838 .svm_locker
839 .with_svm_reader(|svm_reader| svm_reader.latest_epoch_info.block_height);
840
841 assert_eq!(expected_height.unwrap(), latest_epoch_block_height);
842 }
843
844 #[test]
845 fn test_get_block_height_confirmed_commitment() {
846 let setup = TestSetup::new(SurfpoolMinimalRpc);
847
848 let config = RpcContextConfig {
849 commitment: Some(CommitmentConfig::confirmed()),
850 min_context_slot: None,
851 };
852
853 let expected_height = setup
854 .rpc
855 .get_block_height(Some(setup.context.clone()), Some(config));
856 assert!(expected_height.is_ok());
857
858 let latest_epoch_block_height = setup
859 .context
860 .svm_locker
861 .with_svm_reader(|svm_reader| svm_reader.latest_epoch_info.block_height);
862
863 assert_eq!(expected_height.unwrap(), latest_epoch_block_height - 1);
864 }
865
866 #[test]
867 fn test_get_block_height_with_min_context_slot() {
868 let setup = TestSetup::new(SurfpoolMinimalRpc);
869
870 // create blocks at specific slots with known block heights
871 let test_cases = vec![(100, 50), (200, 150), (300, 275)];
872
873 {
874 let mut svm_writer = setup.context.svm_locker.0.blocking_write();
875 for (slot, block_height) in &test_cases {
876 svm_writer.blocks.insert(
877 *slot,
878 crate::surfnet::BlockHeader {
879 hash: format!("hash_{}", slot),
880 previous_blockhash: format!("prev_hash_{}", slot - 1),
881 block_time: chrono::Utc::now().timestamp_millis(),
882 block_height: *block_height,
883 parent_slot: slot - 1,
884 signatures: Vec::new(),
885 },
886 );
887 }
888 }
889
890 for (slot, expected_height) in test_cases {
891 let config = RpcContextConfig {
892 commitment: None,
893 min_context_slot: Some(slot),
894 };
895
896 let result = setup
897 .rpc
898 .get_block_height(Some(setup.context.clone()), Some(config));
899 assert!(
900 result.is_ok(),
901 "failed to get block height for slot {}",
902 slot
903 );
904 assert_eq!(
905 result.unwrap(),
906 expected_height,
907 "Wrong block height for slot {}",
908 slot
909 );
910 }
911 }
912
913 #[test]
914 fn test_get_block_height_error_case_slot_not_found() {
915 let setup = TestSetup::new(SurfpoolMinimalRpc);
916
917 {
918 let mut svm_writer = setup.context.svm_locker.0.blocking_write();
919 svm_writer.blocks.insert(
920 100,
921 crate::surfnet::BlockHeader {
922 hash: "hash_100".to_string(),
923 previous_blockhash: "prev_hash_99".to_string(),
924 block_time: chrono::Utc::now().timestamp_millis(),
925 block_height: 50,
926 parent_slot: 99,
927 signatures: Vec::new(),
928 },
929 );
930 }
931
932 // slot that definitely doesn't exist
933 let nonexistent_slot = 999;
934 let config = RpcContextConfig {
935 commitment: None,
936 min_context_slot: Some(nonexistent_slot),
937 };
938
939 let result = setup
940 .rpc
941 .get_block_height(Some(setup.context), Some(config));
942
943 assert!(
944 result.is_err(),
945 "Expected error for nonexistent slot {}",
946 nonexistent_slot
947 );
948
949 let error = result.unwrap_err();
950
951 assert_eq!(error.code, jsonrpc_core::types::ErrorCode::InvalidParams);
952 assert!(
953 error.message.contains("Block not found for slot"),
954 "Error message should mention block not found, got: {}",
955 error.message
956 );
957 assert!(
958 error.message.contains(&nonexistent_slot.to_string()),
959 "Error message should include the slot number, got: {}",
960 error.message
961 );
962 }
963
964 #[test]
965 fn test_get_health() {
966 let setup = TestSetup::new(SurfpoolMinimalRpc);
967 let result = setup.rpc.get_health(Some(setup.context));
968 assert_eq!(result.unwrap(), "ok");
969 }
970
971 #[test]
972 fn test_get_transaction_count() {
973 let setup = TestSetup::new(SurfpoolMinimalRpc);
974 let transactions_processed = setup
975 .context
976 .svm_locker
977 .with_svm_reader(|svm_reader| svm_reader.transactions_processed);
978 let result = setup.rpc.get_transaction_count(Some(setup.context), None);
979 assert_eq!(result.unwrap(), transactions_processed);
980 }
981
982 #[test]
983 fn test_get_epoch_info() {
984 let info = EpochInfo {
985 epoch: 1,
986 slot_index: 1,
987 slots_in_epoch: 1,
988 absolute_slot: 1,
989 block_height: 1,
990 transaction_count: Some(1),
991 };
992 let setup = TestSetup::new_with_epoch_info(SurfpoolMinimalRpc, info.clone());
993 let result = setup.rpc.get_epoch_info(Some(setup.context), None).unwrap();
994 assert_eq!(result, info);
995 }
996
997 #[test]
998 fn test_get_slot() {
999 let setup = TestSetup::new(SurfpoolMinimalRpc);
1000 let result = setup.rpc.get_slot(Some(setup.context), None).unwrap();
1001 assert_eq!(result, 92);
1002 }
1003
1004 #[test]
1005 fn test_get_highest_snapshot_slot_returns_error() {
1006 let setup = TestSetup::new(SurfpoolMinimalRpc);
1007
1008 let result = setup.rpc.get_highest_snapshot_slot(Some(setup.context));
1009
1010 assert!(
1011 result.is_err(),
1012 "Expected get_highest_snapshot_slot to return an error"
1013 );
1014
1015 if let Err(error) = result {
1016 assert_eq!(
1017 error.code,
1018 jsonrpc_core::ErrorCode::ServerError(-32008),
1019 "Expected error code -32008, got {:?}",
1020 error.code
1021 );
1022 assert_eq!(
1023 error.message, "No snapshot",
1024 "Expected error message 'No snapshot', got '{}'",
1025 error.message
1026 );
1027 }
1028 }
1029
1030 #[tokio::test(flavor = "multi_thread")]
1031 async fn test_get_genesis_hash() {
1032 let setup = TestSetup::new(SurfpoolMinimalRpc);
1033
1034 let genesis_hash = setup
1035 .rpc
1036 .get_genesis_hash(Some(setup.context))
1037 .await
1038 .unwrap();
1039
1040 assert_eq!(genesis_hash, GenesisConfig::default().hash().to_string())
1041 }
1042
1043 #[test]
1044 fn test_get_identity() {
1045 let setup = TestSetup::new(SurfpoolMinimalRpc);
1046 let result = setup.rpc.get_identity(Some(setup.context)).unwrap();
1047 assert_eq!(result.identity, SURFPOOL_IDENTITY_PUBKEY.to_string());
1048 }
1049
1050 #[test]
1051 fn test_get_leader_schedule_valid_cases() {
1052 let setup = TestSetup::new(SurfpoolMinimalRpc);
1053
1054 setup.context.svm_locker.with_svm_writer(|svm_writer| {
1055 svm_writer.latest_epoch_info = EpochInfo {
1056 epoch: 100,
1057 slot_index: 50,
1058 slots_in_epoch: 432000,
1059 absolute_slot: 43200050,
1060 block_height: 43200050,
1061 transaction_count: None,
1062 };
1063 });
1064
1065 // test 1: Valid slot within current epoch, no identity
1066 let result = setup
1067 .rpc
1068 .get_leader_schedule(
1069 Some(setup.context.clone()),
1070 Some(RpcLeaderScheduleConfigWrapper::SlotOnly(Some(43200025))), // Within epoch
1071 None,
1072 )
1073 .unwrap();
1074
1075 assert!(result.is_some());
1076 assert!(result.unwrap().is_empty()); // Should return empty HashMap
1077
1078 // test 2: No slot provided (should use current slot), no identity
1079 let result = setup
1080 .rpc
1081 .get_leader_schedule(Some(setup.context.clone()), None, None)
1082 .unwrap();
1083
1084 assert!(result.is_some());
1085 assert!(result.unwrap().is_empty());
1086
1087 // test 3: Valid slot with valid identity
1088 let valid_pubkey = Pubkey::new_unique();
1089 let result = setup
1090 .rpc
1091 .get_leader_schedule(
1092 Some(setup.context.clone()),
1093 None,
1094 Some(RpcLeaderScheduleConfig {
1095 identity: Some(valid_pubkey.to_string()),
1096 commitment: None,
1097 }),
1098 )
1099 .unwrap();
1100
1101 assert!(result.is_some());
1102 assert!(result.unwrap().is_empty());
1103
1104 // test 4: Boundary cases - first slot in epoch
1105 let first_slot_in_epoch = 43200050 - 50;
1106 let result = setup
1107 .rpc
1108 .get_leader_schedule(
1109 Some(setup.context.clone()),
1110 Some(RpcLeaderScheduleConfigWrapper::SlotOnly(Some(
1111 first_slot_in_epoch,
1112 ))),
1113 None,
1114 )
1115 .unwrap();
1116
1117 assert!(result.is_some());
1118 assert!(result.unwrap().is_empty());
1119
1120 // test 5: Boundary cases - last slot in epoch
1121 let last_slot_in_epoch = first_slot_in_epoch + 432000 - 1;
1122 let result = setup
1123 .rpc
1124 .get_leader_schedule(
1125 Some(setup.context.clone()),
1126 Some(RpcLeaderScheduleConfigWrapper::SlotOnly(Some(
1127 last_slot_in_epoch,
1128 ))),
1129 None,
1130 )
1131 .unwrap();
1132
1133 assert!(result.is_some());
1134 assert!(result.unwrap().is_empty());
1135 }
1136
1137 #[test]
1138 fn test_get_leader_schedule_invalid_cases() {
1139 let setup = TestSetup::new(SurfpoolMinimalRpc);
1140
1141 setup.context.svm_locker.with_svm_writer(|svm_writer| {
1142 svm_writer.latest_epoch_info = EpochInfo {
1143 epoch: 100,
1144 slot_index: 50,
1145 slots_in_epoch: 432000,
1146 absolute_slot: 43200050,
1147 block_height: 43200050,
1148 transaction_count: None,
1149 };
1150 });
1151
1152 let first_slot_in_epoch = 43200050 - 50;
1153 let last_slot_in_epoch = first_slot_in_epoch + 432000 - 1;
1154
1155 // test 1: Slot before current epoch (should return None)
1156 let result = setup
1157 .rpc
1158 .get_leader_schedule(
1159 Some(setup.context.clone()),
1160 Some(RpcLeaderScheduleConfigWrapper::SlotOnly(Some(
1161 first_slot_in_epoch - 1,
1162 ))),
1163 None,
1164 )
1165 .unwrap();
1166
1167 assert!(result.is_none());
1168
1169 // test 2: Slot after current epoch (should return None)
1170 let result = setup
1171 .rpc
1172 .get_leader_schedule(
1173 Some(setup.context.clone()),
1174 Some(RpcLeaderScheduleConfigWrapper::SlotOnly(Some(
1175 last_slot_in_epoch + 1,
1176 ))),
1177 None,
1178 )
1179 .unwrap();
1180
1181 assert!(result.is_none());
1182
1183 // test 3: Way outside epoch range (should return None)
1184 let result = setup
1185 .rpc
1186 .get_leader_schedule(
1187 Some(setup.context.clone()),
1188 Some(RpcLeaderScheduleConfigWrapper::SlotOnly(Some(1000000))), // Very old slot
1189 None,
1190 )
1191 .unwrap();
1192
1193 assert!(result.is_none());
1194
1195 // test 4: Invalid identity pubkey (should return Error)
1196 let result = setup.rpc.get_leader_schedule(
1197 Some(setup.context.clone()),
1198 None,
1199 Some(RpcLeaderScheduleConfig {
1200 identity: Some("invalid_pubkey_string".to_string()),
1201 commitment: None,
1202 }),
1203 );
1204
1205 assert!(result.is_err());
1206 let error = result.unwrap_err();
1207 assert_eq!(error.code, ErrorCode::InvalidParams);
1208
1209 // test 5: Empty identity string (should return Error)
1210 let result = setup.rpc.get_leader_schedule(
1211 Some(setup.context.clone()),
1212 None,
1213 Some(RpcLeaderScheduleConfig {
1214 identity: Some("".to_string()),
1215 commitment: None,
1216 }),
1217 );
1218
1219 assert!(result.is_err());
1220 let error = result.unwrap_err();
1221 assert_eq!(error.code, ErrorCode::InvalidParams);
1222
1223 // test 6: No metadata (should return Error from get_svm_locker)
1224 let result = setup.rpc.get_leader_schedule(None, None, None);
1225
1226 assert!(result.is_err());
1227 }
1228
1229 #[test]
1230 fn test_get_vote_accounts_valid_config_returns_empty() {
1231 let setup = TestSetup::new(SurfpoolMinimalRpc);
1232
1233 // test with valid configuration including all optional parameters
1234 let config = RpcGetVoteAccountsConfig {
1235 vote_pubkey: Some("11111111111111111111111111111112".to_string()),
1236 commitment: Some(CommitmentConfig::processed()),
1237 keep_unstaked_delinquents: Some(true),
1238 delinquent_slot_distance: Some(100),
1239 };
1240
1241 let result = setup
1242 .rpc
1243 .get_vote_accounts(Some(setup.context.clone()), Some(config));
1244
1245 // should succeed with valid inputs
1246 assert!(result.is_ok());
1247
1248 let vote_accounts = result.unwrap();
1249
1250 // should return empty current and delinquent arrays
1251 assert_eq!(vote_accounts.current.len(), 0);
1252 assert_eq!(vote_accounts.delinquent.len(), 0);
1253 }
1254
1255 #[test]
1256 fn test_get_vote_accounts_invalid_pubkey_returns_error() {
1257 let setup = TestSetup::new(SurfpoolMinimalRpc);
1258
1259 // test with invalid vote pubkey that's not valid base58
1260 let config = RpcGetVoteAccountsConfig {
1261 vote_pubkey: Some("invalid_pubkey_not_base58".to_string()),
1262 commitment: Some(CommitmentConfig::finalized()),
1263 keep_unstaked_delinquents: Some(false),
1264 delinquent_slot_distance: Some(50),
1265 };
1266
1267 let result = setup
1268 .rpc
1269 .get_vote_accounts(Some(setup.context.clone()), Some(config));
1270
1271 // should fail due to invalid vote pubkey
1272 assert!(result.is_err());
1273
1274 let error = result.unwrap_err();
1275
1276 // should be invalid params error
1277 assert_eq!(error.code, jsonrpc_core::ErrorCode::InvalidParams);
1278 }
1279}