|
| 1 | +//! RPC-backed account source support for `hpsvm`. |
| 2 | +
|
| 3 | +use std::{ |
| 4 | + collections::HashMap, |
| 5 | + sync::{ |
| 6 | + Arc, |
| 7 | + atomic::{AtomicUsize, Ordering}, |
| 8 | + }, |
| 9 | +}; |
| 10 | + |
| 11 | +use hpsvm::{AccountSource, AccountSourceError}; |
| 12 | +use parking_lot::Mutex; |
| 13 | +use solana_account::AccountSharedData; |
| 14 | +use solana_address::Address; |
| 15 | +use solana_rpc_client::rpc_client::RpcClient; |
| 16 | +use solana_rpc_client_api::config::RpcAccountInfoConfig; |
| 17 | + |
| 18 | +/// Read-through account source backed by a Solana RPC endpoint and a local cache. |
| 19 | +#[derive(Clone)] |
| 20 | +pub struct RpcForkSource { |
| 21 | + client: Arc<RpcClient>, |
| 22 | + slot: u64, |
| 23 | + cache: Arc<Mutex<HashMap<Address, AccountSharedData>>>, |
| 24 | + cache_hits: Arc<AtomicUsize>, |
| 25 | + cache_misses: Arc<AtomicUsize>, |
| 26 | +} |
| 27 | + |
| 28 | +impl std::fmt::Debug for RpcForkSource { |
| 29 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 30 | + f.debug_struct("RpcForkSource") |
| 31 | + .field("client", &"RpcClient") |
| 32 | + .field("slot", &self.slot) |
| 33 | + .field("cache_len", &self.cache.lock().len()) |
| 34 | + .field("cache_hits", &self.cache_hits()) |
| 35 | + .field("cache_misses", &self.cache_misses()) |
| 36 | + .finish() |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl RpcForkSource { |
| 41 | + /// Creates a builder for an RPC-backed account source. |
| 42 | + pub fn builder() -> RpcForkSourceBuilder { |
| 43 | + RpcForkSourceBuilder::default() |
| 44 | + } |
| 45 | + |
| 46 | + /// Returns the number of reads served directly from the local cache. |
| 47 | + pub fn cache_hits(&self) -> usize { |
| 48 | + self.cache_hits.load(Ordering::Relaxed) |
| 49 | + } |
| 50 | + |
| 51 | + /// Returns the number of cache misses that triggered an RPC read. |
| 52 | + pub fn cache_misses(&self) -> usize { |
| 53 | + self.cache_misses.load(Ordering::Relaxed) |
| 54 | + } |
| 55 | + |
| 56 | + /// Returns the minimum RPC context slot used for fetches. |
| 57 | + pub const fn slot(&self) -> u64 { |
| 58 | + self.slot |
| 59 | + } |
| 60 | + |
| 61 | + fn fetch_account( |
| 62 | + &self, |
| 63 | + pubkey: &Address, |
| 64 | + ) -> Result<Option<AccountSharedData>, AccountSourceError> { |
| 65 | + #[expect(deprecated)] |
| 66 | + self.client |
| 67 | + .get_account_with_config( |
| 68 | + pubkey, |
| 69 | + RpcAccountInfoConfig { |
| 70 | + min_context_slot: Some(self.slot), |
| 71 | + ..RpcAccountInfoConfig::default() |
| 72 | + }, |
| 73 | + ) |
| 74 | + .map(|response| response.value.map(Into::into)) |
| 75 | + .map_err(|error| AccountSourceError::new(error.to_string())) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl AccountSource for RpcForkSource { |
| 80 | + fn get_account( |
| 81 | + &self, |
| 82 | + pubkey: &Address, |
| 83 | + ) -> Result<Option<AccountSharedData>, AccountSourceError> { |
| 84 | + if let Some(account) = self.cache.lock().get(pubkey).cloned() { |
| 85 | + self.cache_hits.fetch_add(1, Ordering::Relaxed); |
| 86 | + return Ok(Some(account)); |
| 87 | + } |
| 88 | + |
| 89 | + self.cache_misses.fetch_add(1, Ordering::Relaxed); |
| 90 | + let account = self.fetch_account(pubkey)?; |
| 91 | + if let Some(account) = &account { |
| 92 | + self.cache.lock().insert(*pubkey, account.clone()); |
| 93 | + } |
| 94 | + Ok(account) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/// Builder for [`RpcForkSource`]. |
| 99 | +#[derive(Default)] |
| 100 | +pub struct RpcForkSourceBuilder { |
| 101 | + rpc_url: Option<String>, |
| 102 | + client: Option<Arc<RpcClient>>, |
| 103 | + slot: Option<u64>, |
| 104 | +} |
| 105 | + |
| 106 | +impl std::fmt::Debug for RpcForkSourceBuilder { |
| 107 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 108 | + f.debug_struct("RpcForkSourceBuilder") |
| 109 | + .field("rpc_url", &self.rpc_url) |
| 110 | + .field("client", &self.client.as_ref().map(|_| "RpcClient")) |
| 111 | + .field("slot", &self.slot) |
| 112 | + .finish() |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +impl RpcForkSourceBuilder { |
| 117 | + /// Configures the RPC URL used when no explicit client is provided. |
| 118 | + pub fn with_rpc_url(mut self, rpc_url: impl Into<String>) -> Self { |
| 119 | + self.rpc_url = Some(rpc_url.into()); |
| 120 | + self |
| 121 | + } |
| 122 | + |
| 123 | + /// Reuses an existing RPC client, including mock clients in tests. |
| 124 | + pub fn with_client(mut self, client: RpcClient) -> Self { |
| 125 | + self.client = Some(Arc::new(client)); |
| 126 | + self |
| 127 | + } |
| 128 | + |
| 129 | + /// Sets the minimum context slot used for remote account reads. |
| 130 | + pub fn with_slot(mut self, slot: u64) -> Self { |
| 131 | + self.slot = Some(slot); |
| 132 | + self |
| 133 | + } |
| 134 | + |
| 135 | + /// Builds the configured RPC-backed account source. |
| 136 | + pub fn build(self) -> RpcForkSource { |
| 137 | + let client = self.client.unwrap_or_else(|| { |
| 138 | + Arc::new(RpcClient::new( |
| 139 | + self.rpc_url.unwrap_or_else(|| "http://127.0.0.1:8899".to_owned()), |
| 140 | + )) |
| 141 | + }); |
| 142 | + |
| 143 | + RpcForkSource { |
| 144 | + client, |
| 145 | + slot: self.slot.unwrap_or_default(), |
| 146 | + cache: Arc::new(Mutex::new(HashMap::new())), |
| 147 | + cache_hits: Arc::new(AtomicUsize::new(0)), |
| 148 | + cache_misses: Arc::new(AtomicUsize::new(0)), |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments