solana_client/
thin_client.rs

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
//! The `thin_client` module is a client-side object that interfaces with
//! a server-side TPU.  Client code should use this object instead of writing
//! messages to the network directly. The binary encoding of its messages are
//! unstable and may change in future releases.

use {
    crate::connection_cache::{dispatch, ConnectionCache},
    solana_quic_client::{QuicConfig, QuicConnectionManager, QuicPool},
    solana_rpc_client::rpc_client::RpcClient,
    solana_rpc_client_api::config::RpcProgramAccountsConfig,
    solana_sdk::{
        account::Account,
        client::{AsyncClient, Client, SyncClient},
        clock::Slot,
        commitment_config::CommitmentConfig,
        epoch_info::EpochInfo,
        fee_calculator::{FeeCalculator, FeeRateGovernor},
        hash::Hash,
        instruction::Instruction,
        message::Message,
        pubkey::Pubkey,
        signature::{Keypair, Signature},
        signers::Signers,
        transaction::{self, Transaction, VersionedTransaction},
        transport::Result as TransportResult,
    },
    solana_thin_client::thin_client::ThinClient as BackendThinClient,
    solana_udp_client::{UdpConfig, UdpConnectionManager, UdpPool},
    std::{net::SocketAddr, sync::Arc, time::Duration},
};

/// A thin wrapper over thin-client/ThinClient to ease
/// construction of the ThinClient for code dealing both with udp and quic.
/// For the scenario only using udp or quic, use thin-client/ThinClient directly.
pub enum ThinClient {
    Quic(BackendThinClient<QuicPool, QuicConnectionManager, QuicConfig>),
    Udp(BackendThinClient<UdpPool, UdpConnectionManager, UdpConfig>),
}

impl ThinClient {
    /// Create a new ThinClient that will interface with the Rpc at `rpc_addr` using TCP
    /// and the Tpu at `tpu_addr` over `transactions_socket` using Quic or UDP
    /// (currently hardcoded to UDP)
    pub fn new(
        rpc_addr: SocketAddr,
        tpu_addr: SocketAddr,
        connection_cache: Arc<ConnectionCache>,
    ) -> Self {
        match &*connection_cache {
            ConnectionCache::Quic(connection_cache) => {
                let thin_client =
                    BackendThinClient::new(rpc_addr, tpu_addr, connection_cache.clone());
                ThinClient::Quic(thin_client)
            }
            ConnectionCache::Udp(connection_cache) => {
                let thin_client =
                    BackendThinClient::new(rpc_addr, tpu_addr, connection_cache.clone());
                ThinClient::Udp(thin_client)
            }
        }
    }

    pub fn new_socket_with_timeout(
        rpc_addr: SocketAddr,
        tpu_addr: SocketAddr,
        timeout: Duration,
        connection_cache: Arc<ConnectionCache>,
    ) -> Self {
        match &*connection_cache {
            ConnectionCache::Quic(connection_cache) => {
                let thin_client = BackendThinClient::new_socket_with_timeout(
                    rpc_addr,
                    tpu_addr,
                    timeout,
                    connection_cache.clone(),
                );
                ThinClient::Quic(thin_client)
            }
            ConnectionCache::Udp(connection_cache) => {
                let thin_client = BackendThinClient::new_socket_with_timeout(
                    rpc_addr,
                    tpu_addr,
                    timeout,
                    connection_cache.clone(),
                );
                ThinClient::Udp(thin_client)
            }
        }
    }

    pub fn new_from_addrs(
        rpc_addrs: Vec<SocketAddr>,
        tpu_addrs: Vec<SocketAddr>,
        connection_cache: Arc<ConnectionCache>,
    ) -> Self {
        match &*connection_cache {
            ConnectionCache::Quic(connection_cache) => {
                let thin_client = BackendThinClient::new_from_addrs(
                    rpc_addrs,
                    tpu_addrs,
                    connection_cache.clone(),
                );
                ThinClient::Quic(thin_client)
            }
            ConnectionCache::Udp(connection_cache) => {
                let thin_client = BackendThinClient::new_from_addrs(
                    rpc_addrs,
                    tpu_addrs,
                    connection_cache.clone(),
                );
                ThinClient::Udp(thin_client)
            }
        }
    }

    dispatch!(pub fn rpc_client(&self) -> &RpcClient);

    dispatch!(pub fn retry_transfer_until_confirmed(&self, keypair: &Keypair, transaction: &mut Transaction, tries: usize, min_confirmed_blocks: usize) -> TransportResult<Signature>);

    dispatch!(pub fn retry_transfer(
        &self,
        keypair: &Keypair,
        transaction: &mut Transaction,
        tries: usize
    ) -> TransportResult<Signature>);

    dispatch!(pub fn send_and_confirm_transaction<T: Signers + ?Sized>(
        &self,
        keypairs: &T,
        transaction: &mut Transaction,
        tries: usize,
        pending_confirmations: usize
    ) -> TransportResult<Signature>);

    dispatch!(pub fn poll_get_balance(&self, pubkey: &Pubkey) -> TransportResult<u64>);

    dispatch!(pub fn poll_get_balance_with_commitment(
        &self,
        pubkey: &Pubkey,
        commitment_config: CommitmentConfig
    ) -> TransportResult<u64>);

    dispatch!(pub fn wait_for_balance(&self, pubkey: &Pubkey, expected_balance: Option<u64>) -> Option<u64>);

    dispatch!(pub fn get_program_accounts_with_config(
        &self,
        pubkey: &Pubkey,
        config: RpcProgramAccountsConfig
    ) -> TransportResult<Vec<(Pubkey, Account)>>);

    dispatch!(pub fn wait_for_balance_with_commitment(
        &self,
        pubkey: &Pubkey,
        expected_balance: Option<u64>,
        commitment_config: CommitmentConfig
    ) -> Option<u64>);

    dispatch!(pub fn poll_for_signature_with_commitment(
        &self,
        signature: &Signature,
        commitment_config: CommitmentConfig
    ) -> TransportResult<()>);

    dispatch!(pub fn get_num_blocks_since_signature_confirmation(
        &mut self,
        sig: &Signature
    ) -> TransportResult<usize>);
}

impl Client for ThinClient {
    dispatch!(fn tpu_addr(&self) -> String);
}

impl SyncClient for ThinClient {
    dispatch!(fn send_and_confirm_message<T: Signers + ?Sized>(
        &self,
        keypairs: &T,
        message: Message
    ) -> TransportResult<Signature>);

    dispatch!(fn send_and_confirm_instruction(
        &self,
        keypair: &Keypair,
        instruction: Instruction
    ) -> TransportResult<Signature>);

    dispatch!(fn transfer_and_confirm(
        &self,
        lamports: u64,
        keypair: &Keypair,
        pubkey: &Pubkey
    ) -> TransportResult<Signature>);

    dispatch!(fn get_account_data(&self, pubkey: &Pubkey) -> TransportResult<Option<Vec<u8>>>);

    dispatch!(fn get_account(&self, pubkey: &Pubkey) -> TransportResult<Option<Account>>);

    dispatch!(fn get_account_with_commitment(
        &self,
        pubkey: &Pubkey,
        commitment_config: CommitmentConfig
    ) -> TransportResult<Option<Account>>);

    dispatch!(fn get_balance(&self, pubkey: &Pubkey) -> TransportResult<u64>);

    dispatch!(fn get_balance_with_commitment(
        &self,
        pubkey: &Pubkey,
        commitment_config: CommitmentConfig
    ) -> TransportResult<u64>);

    dispatch!(fn get_minimum_balance_for_rent_exemption(&self, data_len: usize) -> TransportResult<u64>);

    dispatch!(#[allow(deprecated)] fn get_recent_blockhash(&self) -> TransportResult<(Hash, FeeCalculator)>);

    dispatch!(#[allow(deprecated)] fn get_recent_blockhash_with_commitment(
        &self,
        commitment_config: CommitmentConfig
    ) -> TransportResult<(Hash, FeeCalculator, Slot)>);

    dispatch!(#[allow(deprecated)] fn get_fee_calculator_for_blockhash(
        &self,
        blockhash: &Hash
    ) -> TransportResult<Option<FeeCalculator>>);

    dispatch!(#[allow(deprecated)] fn get_fee_rate_governor(&self) -> TransportResult<FeeRateGovernor>);

    dispatch!(fn get_signature_status(
        &self,
        signature: &Signature
    ) -> TransportResult<Option<transaction::Result<()>>>);

    dispatch!(fn get_signature_status_with_commitment(
        &self,
        signature: &Signature,
        commitment_config: CommitmentConfig
    ) -> TransportResult<Option<transaction::Result<()>>>);

    dispatch!(fn get_slot(&self) -> TransportResult<u64>);

    dispatch!(fn get_slot_with_commitment(
        &self,
        commitment_config: CommitmentConfig
    ) -> TransportResult<u64>);

    dispatch!(fn get_epoch_info(&self) -> TransportResult<EpochInfo>);

    dispatch!(fn get_transaction_count(&self) -> TransportResult<u64>);

    dispatch!(fn get_transaction_count_with_commitment(
        &self,
        commitment_config: CommitmentConfig
    ) -> TransportResult<u64>);

    dispatch!(fn poll_for_signature_confirmation(
        &self,
        signature: &Signature,
        min_confirmed_blocks: usize
    ) -> TransportResult<usize>);

    dispatch!(fn poll_for_signature(&self, signature: &Signature) -> TransportResult<()>);

    dispatch!(#[allow(deprecated)] fn get_new_blockhash(&self, blockhash: &Hash) -> TransportResult<(Hash, FeeCalculator)>);

    dispatch!(fn get_latest_blockhash(&self) -> TransportResult<Hash>);

    dispatch!(fn get_latest_blockhash_with_commitment(
        &self,
        commitment_config: CommitmentConfig
    ) -> TransportResult<(Hash, u64)>);

    dispatch!(fn is_blockhash_valid(
        &self,
        blockhash: &Hash,
        commitment_config: CommitmentConfig
    ) -> TransportResult<bool>);

    dispatch!(fn get_fee_for_message(&self, message: &Message) -> TransportResult<u64>);
}

impl AsyncClient for ThinClient {
    dispatch!(fn async_send_versioned_transaction(
        &self,
        transaction: VersionedTransaction
    ) -> TransportResult<Signature>);

    dispatch!(fn async_send_versioned_transaction_batch(
        &self,
        batch: Vec<VersionedTransaction>
    ) -> TransportResult<()>);
}