2019-03-12 17:26:07 -07:00
|
|
|
//! 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.
|
|
|
|
|
2021-03-26 20:26:06 -07:00
|
|
|
use {
|
|
|
|
log::*,
|
2022-08-23 22:34:35 -07:00
|
|
|
solana_rpc_client::rpc_client::RpcClient,
|
|
|
|
solana_rpc_client_api::{config::RpcProgramAccountsConfig, response::Response},
|
2021-03-26 20:26:06 -07:00
|
|
|
solana_sdk::{
|
|
|
|
account::Account,
|
|
|
|
client::{AsyncClient, Client, SyncClient},
|
|
|
|
clock::{Slot, MAX_PROCESSING_AGE},
|
|
|
|
commitment_config::CommitmentConfig,
|
|
|
|
epoch_info::EpochInfo,
|
|
|
|
fee_calculator::{FeeCalculator, FeeRateGovernor},
|
|
|
|
hash::Hash,
|
|
|
|
instruction::Instruction,
|
|
|
|
message::Message,
|
|
|
|
pubkey::Pubkey,
|
|
|
|
signature::{Keypair, Signature, Signer},
|
|
|
|
signers::Signers,
|
|
|
|
system_instruction,
|
|
|
|
timing::duration_as_ms,
|
2022-03-21 18:45:22 -07:00
|
|
|
transaction::{self, Transaction, VersionedTransaction},
|
2021-03-26 20:26:06 -07:00
|
|
|
transport::Result as TransportResult,
|
|
|
|
},
|
2022-11-18 11:21:45 -08:00
|
|
|
solana_tpu_client::{
|
|
|
|
tpu_connection::TpuConnection,
|
|
|
|
tpu_connection_cache::{ConnectionPool, TpuConnectionCache},
|
|
|
|
},
|
2021-03-26 20:26:06 -07:00
|
|
|
std::{
|
|
|
|
io,
|
2022-03-31 12:47:00 -07:00
|
|
|
net::SocketAddr,
|
2021-03-26 20:26:06 -07:00
|
|
|
sync::{
|
|
|
|
atomic::{AtomicBool, AtomicUsize, Ordering},
|
2022-06-08 04:57:12 -07:00
|
|
|
Arc, RwLock,
|
2021-03-26 20:26:06 -07:00
|
|
|
},
|
|
|
|
time::{Duration, Instant},
|
2019-11-06 13:15:00 -08:00
|
|
|
},
|
|
|
|
};
|
2019-05-27 20:54:44 -07:00
|
|
|
|
2022-11-18 11:21:45 -08:00
|
|
|
pub mod temporary_pub {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
pub struct ClientOptimizer {
|
|
|
|
cur_index: AtomicUsize,
|
|
|
|
experiment_index: AtomicUsize,
|
|
|
|
experiment_done: AtomicBool,
|
|
|
|
times: RwLock<Vec<u64>>,
|
|
|
|
num_clients: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ClientOptimizer {
|
|
|
|
pub fn new(num_clients: usize) -> Self {
|
|
|
|
Self {
|
|
|
|
cur_index: AtomicUsize::new(0),
|
|
|
|
experiment_index: AtomicUsize::new(0),
|
|
|
|
experiment_done: AtomicBool::new(false),
|
|
|
|
times: RwLock::new(vec![std::u64::MAX; num_clients]),
|
|
|
|
num_clients,
|
|
|
|
}
|
2019-05-27 20:54:44 -07:00
|
|
|
}
|
|
|
|
|
2022-11-18 11:21:45 -08:00
|
|
|
pub fn experiment(&self) -> usize {
|
|
|
|
if self.experiment_index.load(Ordering::Relaxed) < self.num_clients {
|
|
|
|
let old = self.experiment_index.fetch_add(1, Ordering::Relaxed);
|
|
|
|
if old < self.num_clients {
|
|
|
|
old
|
|
|
|
} else {
|
|
|
|
self.best()
|
|
|
|
}
|
2019-05-27 20:54:44 -07:00
|
|
|
} else {
|
|
|
|
self.best()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 11:21:45 -08:00
|
|
|
pub fn report(&self, index: usize, time_ms: u64) {
|
|
|
|
if self.num_clients > 1
|
|
|
|
&& (!self.experiment_done.load(Ordering::Relaxed) || time_ms == std::u64::MAX)
|
|
|
|
{
|
2019-05-27 20:54:44 -07:00
|
|
|
trace!(
|
2022-11-18 11:21:45 -08:00
|
|
|
"report {} with {} exp: {}",
|
|
|
|
index,
|
|
|
|
time_ms,
|
|
|
|
self.experiment_index.load(Ordering::Relaxed)
|
2019-05-27 20:54:44 -07:00
|
|
|
);
|
|
|
|
|
2022-11-18 11:21:45 -08:00
|
|
|
self.times.write().unwrap()[index] = time_ms;
|
|
|
|
|
|
|
|
if index == (self.num_clients - 1) || time_ms == std::u64::MAX {
|
|
|
|
let times = self.times.read().unwrap();
|
|
|
|
let (min_time, min_index) = min_index(×);
|
|
|
|
trace!(
|
|
|
|
"done experimenting min: {} time: {} times: {:?}",
|
|
|
|
min_index,
|
|
|
|
min_time,
|
|
|
|
times
|
|
|
|
);
|
|
|
|
|
|
|
|
// Only 1 thread should grab the num_clients-1 index, so this should be ok.
|
|
|
|
self.cur_index.store(min_index, Ordering::Relaxed);
|
|
|
|
self.experiment_done.store(true, Ordering::Relaxed);
|
|
|
|
}
|
2019-05-27 20:54:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 11:21:45 -08:00
|
|
|
pub fn best(&self) -> usize {
|
|
|
|
self.cur_index.load(Ordering::Relaxed)
|
|
|
|
}
|
2019-05-27 20:54:44 -07:00
|
|
|
}
|
|
|
|
}
|
2022-11-18 11:21:45 -08:00
|
|
|
use temporary_pub::*;
|
2019-03-12 17:26:07 -07:00
|
|
|
|
|
|
|
/// An object for querying and sending transactions to the network.
|
2022-11-18 11:21:45 -08:00
|
|
|
pub struct ThinClient<P: ConnectionPool> {
|
2019-05-27 20:54:44 -07:00
|
|
|
rpc_clients: Vec<RpcClient>,
|
2022-03-31 12:47:00 -07:00
|
|
|
tpu_addrs: Vec<SocketAddr>,
|
2019-05-27 20:54:44 -07:00
|
|
|
optimizer: ClientOptimizer,
|
2022-11-18 11:21:45 -08:00
|
|
|
connection_cache: Arc<TpuConnectionCache<P>>,
|
2019-03-12 17:26:07 -07:00
|
|
|
}
|
|
|
|
|
2022-11-18 11:21:45 -08:00
|
|
|
impl<P: ConnectionPool> ThinClient<P> {
|
2019-03-12 17:26:07 -07:00
|
|
|
/// Create a new ThinClient that will interface with the Rpc at `rpc_addr` using TCP
|
2022-03-09 18:33:05 -08:00
|
|
|
/// and the Tpu at `tpu_addr` over `transactions_socket` using Quic or UDP
|
|
|
|
/// (currently hardcoded to UDP)
|
2022-06-08 04:57:12 -07:00
|
|
|
pub fn new(
|
|
|
|
rpc_addr: SocketAddr,
|
|
|
|
tpu_addr: SocketAddr,
|
2022-11-18 11:21:45 -08:00
|
|
|
connection_cache: Arc<TpuConnectionCache<P>>,
|
2022-06-08 04:57:12 -07:00
|
|
|
) -> Self {
|
|
|
|
Self::new_from_client(RpcClient::new_socket(rpc_addr), tpu_addr, connection_cache)
|
2019-03-12 17:26:07 -07:00
|
|
|
}
|
|
|
|
|
2019-03-15 22:42:36 -07:00
|
|
|
pub fn new_socket_with_timeout(
|
2019-03-12 17:26:07 -07:00
|
|
|
rpc_addr: SocketAddr,
|
2019-09-06 09:07:40 -07:00
|
|
|
tpu_addr: SocketAddr,
|
2019-03-12 17:26:07 -07:00
|
|
|
timeout: Duration,
|
2022-11-18 11:21:45 -08:00
|
|
|
connection_cache: Arc<TpuConnectionCache<P>>,
|
2019-03-12 17:26:07 -07:00
|
|
|
) -> Self {
|
2019-03-15 22:42:36 -07:00
|
|
|
let rpc_client = RpcClient::new_socket_with_timeout(rpc_addr, timeout);
|
2022-06-08 04:57:12 -07:00
|
|
|
Self::new_from_client(rpc_client, tpu_addr, connection_cache)
|
2019-03-12 17:26:07 -07:00
|
|
|
}
|
|
|
|
|
2022-06-08 04:57:12 -07:00
|
|
|
fn new_from_client(
|
|
|
|
rpc_client: RpcClient,
|
|
|
|
tpu_addr: SocketAddr,
|
2022-11-18 11:21:45 -08:00
|
|
|
connection_cache: Arc<TpuConnectionCache<P>>,
|
2022-06-08 04:57:12 -07:00
|
|
|
) -> Self {
|
2019-03-16 21:51:41 -07:00
|
|
|
Self {
|
2019-05-27 20:54:44 -07:00
|
|
|
rpc_clients: vec![rpc_client],
|
2022-03-31 12:47:00 -07:00
|
|
|
tpu_addrs: vec![tpu_addr],
|
2019-05-27 20:54:44 -07:00
|
|
|
optimizer: ClientOptimizer::new(0),
|
2022-06-08 04:57:12 -07:00
|
|
|
connection_cache,
|
2019-03-12 17:26:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-08 04:57:12 -07:00
|
|
|
pub fn new_from_addrs(
|
|
|
|
rpc_addrs: Vec<SocketAddr>,
|
|
|
|
tpu_addrs: Vec<SocketAddr>,
|
2022-11-18 11:21:45 -08:00
|
|
|
connection_cache: Arc<TpuConnectionCache<P>>,
|
2022-06-08 04:57:12 -07:00
|
|
|
) -> Self {
|
2019-09-06 09:07:40 -07:00
|
|
|
assert!(!rpc_addrs.is_empty());
|
|
|
|
assert_eq!(rpc_addrs.len(), tpu_addrs.len());
|
|
|
|
|
|
|
|
let rpc_clients: Vec<_> = rpc_addrs.into_iter().map(RpcClient::new_socket).collect();
|
|
|
|
let optimizer = ClientOptimizer::new(rpc_clients.len());
|
2019-05-27 20:54:44 -07:00
|
|
|
Self {
|
|
|
|
rpc_clients,
|
2022-03-31 12:47:00 -07:00
|
|
|
tpu_addrs,
|
2019-09-06 09:07:40 -07:00
|
|
|
optimizer,
|
2022-06-08 04:57:12 -07:00
|
|
|
connection_cache,
|
2019-05-27 20:54:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-31 12:47:00 -07:00
|
|
|
fn tpu_addr(&self) -> &SocketAddr {
|
|
|
|
&self.tpu_addrs[self.optimizer.best()]
|
2019-05-27 20:54:44 -07:00
|
|
|
}
|
|
|
|
|
2022-04-11 12:45:40 -07:00
|
|
|
pub fn rpc_client(&self) -> &RpcClient {
|
2019-05-27 20:54:44 -07:00
|
|
|
&self.rpc_clients[self.optimizer.best()]
|
|
|
|
}
|
|
|
|
|
2019-03-21 07:43:21 -07:00
|
|
|
/// Retry a sending a signed Transaction to the server for processing.
|
|
|
|
pub fn retry_transfer_until_confirmed(
|
|
|
|
&self,
|
|
|
|
keypair: &Keypair,
|
|
|
|
transaction: &mut Transaction,
|
|
|
|
tries: usize,
|
|
|
|
min_confirmed_blocks: usize,
|
2020-03-12 23:20:49 -07:00
|
|
|
) -> TransportResult<Signature> {
|
2019-04-05 20:57:59 -07:00
|
|
|
self.send_and_confirm_transaction(&[keypair], transaction, tries, min_confirmed_blocks)
|
2019-03-21 07:43:21 -07:00
|
|
|
}
|
|
|
|
|
2019-04-05 20:57:59 -07:00
|
|
|
/// Retry sending a signed Transaction with one signing Keypair to the server for processing.
|
2019-03-12 17:26:07 -07:00
|
|
|
pub fn retry_transfer(
|
2019-03-16 08:31:19 -07:00
|
|
|
&self,
|
2019-03-12 17:26:07 -07:00
|
|
|
keypair: &Keypair,
|
|
|
|
transaction: &mut Transaction,
|
|
|
|
tries: usize,
|
2020-03-12 23:20:49 -07:00
|
|
|
) -> TransportResult<Signature> {
|
2019-04-05 20:57:59 -07:00
|
|
|
self.send_and_confirm_transaction(&[keypair], transaction, tries, 0)
|
|
|
|
}
|
|
|
|
|
2020-02-20 12:13:23 -08:00
|
|
|
pub fn send_and_confirm_transaction<T: Signers>(
|
2019-04-05 20:57:59 -07:00
|
|
|
&self,
|
2020-02-20 12:13:23 -08:00
|
|
|
keypairs: &T,
|
2019-04-05 20:57:59 -07:00
|
|
|
transaction: &mut Transaction,
|
|
|
|
tries: usize,
|
2019-07-02 20:56:10 -07:00
|
|
|
pending_confirmations: usize,
|
2020-03-12 23:20:49 -07:00
|
|
|
) -> TransportResult<Signature> {
|
2019-03-12 17:26:07 -07:00
|
|
|
for x in 0..tries {
|
2019-06-24 16:46:34 -07:00
|
|
|
let now = Instant::now();
|
2019-07-02 20:56:10 -07:00
|
|
|
let mut num_confirmed = 0;
|
|
|
|
let mut wait_time = MAX_PROCESSING_AGE;
|
2019-06-24 16:46:34 -07:00
|
|
|
// resend the same transaction until the transaction has no chance of succeeding
|
2022-03-21 18:45:22 -07:00
|
|
|
let wire_transaction =
|
|
|
|
bincode::serialize(&transaction).expect("transaction serialization failed");
|
2019-07-02 20:56:10 -07:00
|
|
|
while now.elapsed().as_secs() < wait_time as u64 {
|
|
|
|
if num_confirmed == 0 {
|
2022-06-08 04:57:12 -07:00
|
|
|
let conn = self.connection_cache.get_connection(self.tpu_addr());
|
2019-07-02 20:56:10 -07:00
|
|
|
// Send the transaction if there has been no confirmation (e.g. the first time)
|
2022-11-09 11:39:38 -08:00
|
|
|
#[allow(clippy::needless_borrow)]
|
2022-05-26 08:21:16 -07:00
|
|
|
conn.send_wire_transaction(&wire_transaction)?;
|
2019-07-02 20:56:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Ok(confirmed_blocks) = self.poll_for_signature_confirmation(
|
|
|
|
&transaction.signatures[0],
|
|
|
|
pending_confirmations,
|
|
|
|
) {
|
|
|
|
num_confirmed = confirmed_blocks;
|
|
|
|
if confirmed_blocks >= pending_confirmations {
|
|
|
|
return Ok(transaction.signatures[0]);
|
|
|
|
}
|
|
|
|
// Since network has seen the transaction, wait longer to receive
|
|
|
|
// all pending confirmations. Resending the transaction could result into
|
|
|
|
// extra transaction fees
|
|
|
|
wait_time = wait_time.max(
|
|
|
|
MAX_PROCESSING_AGE * pending_confirmations.saturating_sub(num_confirmed),
|
|
|
|
);
|
2019-06-24 16:46:34 -07:00
|
|
|
}
|
2019-03-12 17:26:07 -07:00
|
|
|
}
|
2019-09-06 09:07:40 -07:00
|
|
|
info!("{} tries failed transfer to {}", x, self.tpu_addr());
|
2021-08-13 09:08:20 -07:00
|
|
|
let blockhash = self.get_latest_blockhash()?;
|
2019-05-13 12:49:37 -07:00
|
|
|
transaction.sign(keypairs, blockhash);
|
2019-03-12 17:26:07 -07:00
|
|
|
}
|
|
|
|
Err(io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
2019-04-05 20:57:59 -07:00
|
|
|
format!("retry_transfer failed in {} retries", tries),
|
2020-03-12 23:20:49 -07:00
|
|
|
)
|
|
|
|
.into())
|
2019-03-12 17:26:07 -07:00
|
|
|
}
|
|
|
|
|
2020-03-12 23:20:49 -07:00
|
|
|
pub fn poll_get_balance(&self, pubkey: &Pubkey) -> TransportResult<u64> {
|
|
|
|
self.poll_get_balance_with_commitment(pubkey, CommitmentConfig::default())
|
2019-11-06 13:15:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn poll_get_balance_with_commitment(
|
|
|
|
&self,
|
|
|
|
pubkey: &Pubkey,
|
|
|
|
commitment_config: CommitmentConfig,
|
2020-03-12 23:20:49 -07:00
|
|
|
) -> TransportResult<u64> {
|
2019-11-06 13:15:00 -08:00
|
|
|
self.rpc_client()
|
|
|
|
.poll_get_balance_with_commitment(pubkey, commitment_config)
|
2020-03-12 23:20:49 -07:00
|
|
|
.map_err(|e| e.into())
|
2019-03-12 17:26:07 -07:00
|
|
|
}
|
|
|
|
|
2019-03-16 23:29:11 -07:00
|
|
|
pub fn wait_for_balance(&self, pubkey: &Pubkey, expected_balance: Option<u64>) -> Option<u64> {
|
2019-11-06 13:15:00 -08:00
|
|
|
self.rpc_client().wait_for_balance_with_commitment(
|
|
|
|
pubkey,
|
|
|
|
expected_balance,
|
|
|
|
CommitmentConfig::default(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-11-20 13:01:04 -08:00
|
|
|
pub fn get_program_accounts_with_config(
|
|
|
|
&self,
|
|
|
|
pubkey: &Pubkey,
|
|
|
|
config: RpcProgramAccountsConfig,
|
|
|
|
) -> TransportResult<Vec<(Pubkey, Account)>> {
|
|
|
|
self.rpc_client()
|
|
|
|
.get_program_accounts_with_config(pubkey, config)
|
|
|
|
.map_err(|e| e.into())
|
|
|
|
}
|
|
|
|
|
2019-11-06 13:15:00 -08:00
|
|
|
pub fn wait_for_balance_with_commitment(
|
|
|
|
&self,
|
|
|
|
pubkey: &Pubkey,
|
|
|
|
expected_balance: Option<u64>,
|
|
|
|
commitment_config: CommitmentConfig,
|
|
|
|
) -> Option<u64> {
|
|
|
|
self.rpc_client().wait_for_balance_with_commitment(
|
|
|
|
pubkey,
|
|
|
|
expected_balance,
|
|
|
|
commitment_config,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn poll_for_signature_with_commitment(
|
|
|
|
&self,
|
|
|
|
signature: &Signature,
|
|
|
|
commitment_config: CommitmentConfig,
|
|
|
|
) -> TransportResult<()> {
|
2020-03-12 23:20:49 -07:00
|
|
|
self.rpc_client()
|
|
|
|
.poll_for_signature_with_commitment(signature, commitment_config)
|
|
|
|
.map_err(|e| e.into())
|
2019-03-16 23:29:11 -07:00
|
|
|
}
|
|
|
|
|
2019-03-21 07:43:21 -07:00
|
|
|
pub fn get_num_blocks_since_signature_confirmation(
|
|
|
|
&mut self,
|
|
|
|
sig: &Signature,
|
2020-03-12 23:20:49 -07:00
|
|
|
) -> TransportResult<usize> {
|
2019-05-27 20:54:44 -07:00
|
|
|
self.rpc_client()
|
2019-03-21 07:43:21 -07:00
|
|
|
.get_num_blocks_since_signature_confirmation(sig)
|
2020-03-12 23:20:49 -07:00
|
|
|
.map_err(|e| e.into())
|
2019-03-21 07:43:21 -07:00
|
|
|
}
|
2019-03-12 17:26:07 -07:00
|
|
|
}
|
|
|
|
|
2022-11-18 11:21:45 -08:00
|
|
|
impl<P: ConnectionPool> Client for ThinClient<P> {
|
2019-09-06 09:07:40 -07:00
|
|
|
fn tpu_addr(&self) -> String {
|
2022-03-31 12:47:00 -07:00
|
|
|
self.tpu_addr().to_string()
|
2019-04-19 14:04:36 -07:00
|
|
|
}
|
|
|
|
}
|
2019-04-11 00:25:14 -07:00
|
|
|
|
2022-11-18 11:21:45 -08:00
|
|
|
impl<P: ConnectionPool> SyncClient for ThinClient<P> {
|
2020-06-24 20:35:38 -07:00
|
|
|
fn send_and_confirm_message<T: Signers>(
|
2020-02-20 12:13:23 -08:00
|
|
|
&self,
|
|
|
|
keypairs: &T,
|
|
|
|
message: Message,
|
|
|
|
) -> TransportResult<Signature> {
|
2021-08-13 09:08:20 -07:00
|
|
|
let blockhash = self.get_latest_blockhash()?;
|
2020-02-20 12:13:23 -08:00
|
|
|
let mut transaction = Transaction::new(keypairs, message, blockhash);
|
2019-04-05 20:57:59 -07:00
|
|
|
let signature = self.send_and_confirm_transaction(keypairs, &mut transaction, 5, 0)?;
|
|
|
|
Ok(signature)
|
|
|
|
}
|
|
|
|
|
2020-06-24 20:35:38 -07:00
|
|
|
fn send_and_confirm_instruction(
|
2019-04-05 20:57:59 -07:00
|
|
|
&self,
|
|
|
|
keypair: &Keypair,
|
|
|
|
instruction: Instruction,
|
|
|
|
) -> TransportResult<Signature> {
|
2020-06-24 13:52:38 -07:00
|
|
|
let message = Message::new(&[instruction], Some(&keypair.pubkey()));
|
2020-06-24 20:35:38 -07:00
|
|
|
self.send_and_confirm_message(&[keypair], message)
|
2019-04-05 20:57:59 -07:00
|
|
|
}
|
|
|
|
|
2020-06-24 20:35:38 -07:00
|
|
|
fn transfer_and_confirm(
|
2019-04-05 20:57:59 -07:00
|
|
|
&self,
|
|
|
|
lamports: u64,
|
|
|
|
keypair: &Keypair,
|
|
|
|
pubkey: &Pubkey,
|
|
|
|
) -> TransportResult<Signature> {
|
|
|
|
let transfer_instruction =
|
|
|
|
system_instruction::transfer(&keypair.pubkey(), pubkey, lamports);
|
2020-06-24 20:35:38 -07:00
|
|
|
self.send_and_confirm_instruction(keypair, transfer_instruction)
|
2019-04-05 20:57:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_account_data(&self, pubkey: &Pubkey) -> TransportResult<Option<Vec<u8>>> {
|
2019-05-27 20:54:44 -07:00
|
|
|
Ok(self.rpc_client().get_account_data(pubkey).ok())
|
2019-04-05 20:57:59 -07:00
|
|
|
}
|
|
|
|
|
2019-06-13 22:30:51 -07:00
|
|
|
fn get_account(&self, pubkey: &Pubkey) -> TransportResult<Option<Account>> {
|
2019-11-12 11:49:41 -08:00
|
|
|
let account = self.rpc_client().get_account(pubkey);
|
|
|
|
match account {
|
|
|
|
Ok(value) => Ok(Some(value)),
|
|
|
|
Err(_) => Ok(None),
|
|
|
|
}
|
2019-06-13 22:30:51 -07:00
|
|
|
}
|
|
|
|
|
2019-11-06 13:15:00 -08:00
|
|
|
fn get_account_with_commitment(
|
|
|
|
&self,
|
|
|
|
pubkey: &Pubkey,
|
|
|
|
commitment_config: CommitmentConfig,
|
|
|
|
) -> TransportResult<Option<Account>> {
|
2020-03-12 23:20:49 -07:00
|
|
|
self.rpc_client()
|
|
|
|
.get_account_with_commitment(pubkey, commitment_config)
|
|
|
|
.map_err(|e| e.into())
|
|
|
|
.map(|r| r.value)
|
2019-11-06 13:15:00 -08:00
|
|
|
}
|
|
|
|
|
2019-04-05 20:57:59 -07:00
|
|
|
fn get_balance(&self, pubkey: &Pubkey) -> TransportResult<u64> {
|
2020-03-12 23:20:49 -07:00
|
|
|
self.rpc_client().get_balance(pubkey).map_err(|e| e.into())
|
2019-04-05 20:57:59 -07:00
|
|
|
}
|
|
|
|
|
2019-11-06 13:15:00 -08:00
|
|
|
fn get_balance_with_commitment(
|
|
|
|
&self,
|
|
|
|
pubkey: &Pubkey,
|
|
|
|
commitment_config: CommitmentConfig,
|
|
|
|
) -> TransportResult<u64> {
|
2020-03-12 23:20:49 -07:00
|
|
|
self.rpc_client()
|
|
|
|
.get_balance_with_commitment(pubkey, commitment_config)
|
|
|
|
.map_err(|e| e.into())
|
|
|
|
.map(|r| r.value)
|
2019-11-06 13:15:00 -08:00
|
|
|
}
|
|
|
|
|
2020-12-10 16:39:28 -08:00
|
|
|
fn get_minimum_balance_for_rent_exemption(&self, data_len: usize) -> TransportResult<u64> {
|
|
|
|
self.rpc_client()
|
|
|
|
.get_minimum_balance_for_rent_exemption(data_len)
|
|
|
|
.map_err(|e| e.into())
|
|
|
|
}
|
|
|
|
|
2019-06-13 22:30:51 -07:00
|
|
|
fn get_recent_blockhash(&self) -> TransportResult<(Hash, FeeCalculator)> {
|
2021-08-13 09:08:20 -07:00
|
|
|
#[allow(deprecated)]
|
2020-06-09 17:07:32 -07:00
|
|
|
let (blockhash, fee_calculator, _last_valid_slot) =
|
|
|
|
self.get_recent_blockhash_with_commitment(CommitmentConfig::default())?;
|
|
|
|
Ok((blockhash, fee_calculator))
|
2019-11-06 13:15:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_recent_blockhash_with_commitment(
|
|
|
|
&self,
|
|
|
|
commitment_config: CommitmentConfig,
|
2020-06-09 17:07:32 -07:00
|
|
|
) -> TransportResult<(Hash, FeeCalculator, Slot)> {
|
2019-06-13 22:30:51 -07:00
|
|
|
let index = self.optimizer.experiment();
|
|
|
|
let now = Instant::now();
|
2021-08-13 09:08:20 -07:00
|
|
|
#[allow(deprecated)]
|
2019-11-06 13:15:00 -08:00
|
|
|
let recent_blockhash =
|
|
|
|
self.rpc_clients[index].get_recent_blockhash_with_commitment(commitment_config);
|
2019-06-13 22:30:51 -07:00
|
|
|
match recent_blockhash {
|
2019-11-12 11:49:41 -08:00
|
|
|
Ok(Response { value, .. }) => {
|
2019-06-13 22:30:51 -07:00
|
|
|
self.optimizer.report(index, duration_as_ms(&now.elapsed()));
|
2020-06-09 17:07:32 -07:00
|
|
|
Ok((value.0, value.1, value.2))
|
2019-06-13 22:30:51 -07:00
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
self.optimizer.report(index, std::u64::MAX);
|
2019-10-02 18:04:18 -07:00
|
|
|
Err(e.into())
|
2019-06-13 22:30:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-06 16:01:31 -08:00
|
|
|
fn get_fee_calculator_for_blockhash(
|
|
|
|
&self,
|
|
|
|
blockhash: &Hash,
|
|
|
|
) -> TransportResult<Option<FeeCalculator>> {
|
2021-08-13 09:08:20 -07:00
|
|
|
#[allow(deprecated)]
|
2020-03-12 23:20:49 -07:00
|
|
|
self.rpc_client()
|
|
|
|
.get_fee_calculator_for_blockhash(blockhash)
|
|
|
|
.map_err(|e| e.into())
|
2020-03-06 16:01:31 -08:00
|
|
|
}
|
|
|
|
|
2020-02-28 12:27:01 -08:00
|
|
|
fn get_fee_rate_governor(&self) -> TransportResult<FeeRateGovernor> {
|
2021-08-13 09:08:20 -07:00
|
|
|
#[allow(deprecated)]
|
2020-03-12 23:20:49 -07:00
|
|
|
self.rpc_client()
|
|
|
|
.get_fee_rate_governor()
|
|
|
|
.map_err(|e| e.into())
|
|
|
|
.map(|r| r.value)
|
2020-02-28 12:27:01 -08:00
|
|
|
}
|
|
|
|
|
2019-04-05 20:57:59 -07:00
|
|
|
fn get_signature_status(
|
|
|
|
&self,
|
|
|
|
signature: &Signature,
|
|
|
|
) -> TransportResult<Option<transaction::Result<()>>> {
|
|
|
|
let status = self
|
2019-05-27 20:54:44 -07:00
|
|
|
.rpc_client()
|
2021-06-18 06:34:46 -07:00
|
|
|
.get_signature_status(signature)
|
2019-04-05 20:57:59 -07:00
|
|
|
.map_err(|err| {
|
|
|
|
io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
2019-04-25 10:29:44 -07:00
|
|
|
format!("send_transaction failed with error {:?}", err),
|
2019-04-05 20:57:59 -07:00
|
|
|
)
|
|
|
|
})?;
|
|
|
|
Ok(status)
|
|
|
|
}
|
2019-04-11 00:25:14 -07:00
|
|
|
|
2019-11-06 13:15:00 -08:00
|
|
|
fn get_signature_status_with_commitment(
|
|
|
|
&self,
|
|
|
|
signature: &Signature,
|
|
|
|
commitment_config: CommitmentConfig,
|
|
|
|
) -> TransportResult<Option<transaction::Result<()>>> {
|
|
|
|
let status = self
|
|
|
|
.rpc_client()
|
2021-06-18 06:34:46 -07:00
|
|
|
.get_signature_status_with_commitment(signature, commitment_config)
|
2019-11-06 13:15:00 -08:00
|
|
|
.map_err(|err| {
|
|
|
|
io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
|
|
|
format!("send_transaction failed with error {:?}", err),
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
Ok(status)
|
|
|
|
}
|
|
|
|
|
2019-06-12 16:43:05 -07:00
|
|
|
fn get_slot(&self) -> TransportResult<u64> {
|
2019-11-06 13:15:00 -08:00
|
|
|
self.get_slot_with_commitment(CommitmentConfig::default())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_slot_with_commitment(
|
|
|
|
&self,
|
|
|
|
commitment_config: CommitmentConfig,
|
|
|
|
) -> TransportResult<u64> {
|
|
|
|
let slot = self
|
|
|
|
.rpc_client()
|
|
|
|
.get_slot_with_commitment(commitment_config)
|
|
|
|
.map_err(|err| {
|
|
|
|
io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
|
|
|
format!("send_transaction failed with error {:?}", err),
|
|
|
|
)
|
|
|
|
})?;
|
2019-06-12 16:43:05 -07:00
|
|
|
Ok(slot)
|
|
|
|
}
|
|
|
|
|
2020-05-20 16:42:46 -07:00
|
|
|
fn get_epoch_info(&self) -> TransportResult<EpochInfo> {
|
|
|
|
self.rpc_client().get_epoch_info().map_err(|e| e.into())
|
|
|
|
}
|
|
|
|
|
2019-04-11 00:25:14 -07:00
|
|
|
fn get_transaction_count(&self) -> TransportResult<u64> {
|
2019-05-27 20:54:44 -07:00
|
|
|
let index = self.optimizer.experiment();
|
|
|
|
let now = Instant::now();
|
|
|
|
match self.rpc_client().get_transaction_count() {
|
|
|
|
Ok(transaction_count) => {
|
|
|
|
self.optimizer.report(index, duration_as_ms(&now.elapsed()));
|
|
|
|
Ok(transaction_count)
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
self.optimizer.report(index, std::u64::MAX);
|
2019-10-02 18:04:18 -07:00
|
|
|
Err(e.into())
|
2019-05-27 20:54:44 -07:00
|
|
|
}
|
|
|
|
}
|
2019-04-11 00:25:14 -07:00
|
|
|
}
|
2019-04-25 12:46:40 -07:00
|
|
|
|
2019-11-06 13:15:00 -08:00
|
|
|
fn get_transaction_count_with_commitment(
|
|
|
|
&self,
|
|
|
|
commitment_config: CommitmentConfig,
|
|
|
|
) -> TransportResult<u64> {
|
|
|
|
let index = self.optimizer.experiment();
|
|
|
|
let now = Instant::now();
|
|
|
|
match self
|
|
|
|
.rpc_client()
|
|
|
|
.get_transaction_count_with_commitment(commitment_config)
|
|
|
|
{
|
|
|
|
Ok(transaction_count) => {
|
|
|
|
self.optimizer.report(index, duration_as_ms(&now.elapsed()));
|
|
|
|
Ok(transaction_count)
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
self.optimizer.report(index, std::u64::MAX);
|
|
|
|
Err(e.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-25 12:46:40 -07:00
|
|
|
/// Poll the server until the signature has been confirmed by at least `min_confirmed_blocks`
|
|
|
|
fn poll_for_signature_confirmation(
|
|
|
|
&self,
|
|
|
|
signature: &Signature,
|
|
|
|
min_confirmed_blocks: usize,
|
2019-07-02 20:56:10 -07:00
|
|
|
) -> TransportResult<usize> {
|
2020-03-12 23:20:49 -07:00
|
|
|
self.rpc_client()
|
|
|
|
.poll_for_signature_confirmation(signature, min_confirmed_blocks)
|
|
|
|
.map_err(|e| e.into())
|
2019-04-25 12:46:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn poll_for_signature(&self, signature: &Signature) -> TransportResult<()> {
|
2020-03-12 23:20:49 -07:00
|
|
|
self.rpc_client()
|
|
|
|
.poll_for_signature(signature)
|
|
|
|
.map_err(|e| e.into())
|
2019-04-25 12:46:40 -07:00
|
|
|
}
|
2019-04-27 08:39:29 -07:00
|
|
|
|
2019-05-13 12:49:37 -07:00
|
|
|
fn get_new_blockhash(&self, blockhash: &Hash) -> TransportResult<(Hash, FeeCalculator)> {
|
2021-08-13 09:08:20 -07:00
|
|
|
#[allow(deprecated)]
|
2020-03-12 23:20:49 -07:00
|
|
|
self.rpc_client()
|
|
|
|
.get_new_blockhash(blockhash)
|
|
|
|
.map_err(|e| e.into())
|
2019-04-27 08:39:29 -07:00
|
|
|
}
|
2021-08-13 09:08:20 -07:00
|
|
|
|
|
|
|
fn get_latest_blockhash(&self) -> TransportResult<Hash> {
|
|
|
|
let (blockhash, _) =
|
|
|
|
self.get_latest_blockhash_with_commitment(CommitmentConfig::default())?;
|
|
|
|
Ok(blockhash)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_latest_blockhash_with_commitment(
|
|
|
|
&self,
|
|
|
|
commitment_config: CommitmentConfig,
|
|
|
|
) -> TransportResult<(Hash, u64)> {
|
|
|
|
let index = self.optimizer.experiment();
|
|
|
|
let now = Instant::now();
|
|
|
|
match self.rpc_clients[index].get_latest_blockhash_with_commitment(commitment_config) {
|
|
|
|
Ok((blockhash, last_valid_block_height)) => {
|
|
|
|
self.optimizer.report(index, duration_as_ms(&now.elapsed()));
|
|
|
|
Ok((blockhash, last_valid_block_height))
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
self.optimizer.report(index, std::u64::MAX);
|
|
|
|
Err(e.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_blockhash_valid(
|
|
|
|
&self,
|
|
|
|
blockhash: &Hash,
|
|
|
|
commitment_config: CommitmentConfig,
|
|
|
|
) -> TransportResult<bool> {
|
|
|
|
self.rpc_client()
|
|
|
|
.is_blockhash_valid(blockhash, commitment_config)
|
|
|
|
.map_err(|e| e.into())
|
|
|
|
}
|
|
|
|
|
2021-10-13 13:10:58 -07:00
|
|
|
fn get_fee_for_message(&self, message: &Message) -> TransportResult<u64> {
|
2021-08-13 09:08:20 -07:00
|
|
|
self.rpc_client()
|
2021-10-13 13:10:58 -07:00
|
|
|
.get_fee_for_message(message)
|
2021-08-13 09:08:20 -07:00
|
|
|
.map_err(|e| e.into())
|
|
|
|
}
|
2019-04-05 20:57:59 -07:00
|
|
|
}
|
|
|
|
|
2022-11-18 11:21:45 -08:00
|
|
|
impl<P: ConnectionPool> AsyncClient for ThinClient<P> {
|
2022-04-01 20:12:02 -07:00
|
|
|
fn async_send_versioned_transaction(
|
|
|
|
&self,
|
|
|
|
transaction: VersionedTransaction,
|
|
|
|
) -> TransportResult<Signature> {
|
2022-06-08 04:57:12 -07:00
|
|
|
let conn = self.connection_cache.get_connection(self.tpu_addr());
|
2022-05-26 08:21:16 -07:00
|
|
|
conn.serialize_and_send_transaction(&transaction)?;
|
2019-04-05 20:57:59 -07:00
|
|
|
Ok(transaction.signatures[0])
|
|
|
|
}
|
2022-03-09 18:33:05 -08:00
|
|
|
|
2022-04-01 20:12:02 -07:00
|
|
|
fn async_send_versioned_transaction_batch(
|
|
|
|
&self,
|
|
|
|
batch: Vec<VersionedTransaction>,
|
|
|
|
) -> TransportResult<()> {
|
2022-06-08 04:57:12 -07:00
|
|
|
let conn = self.connection_cache.get_connection(self.tpu_addr());
|
2022-05-26 08:21:16 -07:00
|
|
|
conn.par_serialize_and_send_transaction_batch(&batch[..])?;
|
2022-03-21 18:45:22 -07:00
|
|
|
Ok(())
|
2022-03-09 18:33:05 -08:00
|
|
|
}
|
2019-04-05 20:57:59 -07:00
|
|
|
}
|
|
|
|
|
2022-11-18 11:21:45 -08:00
|
|
|
fn min_index(array: &[u64]) -> (u64, usize) {
|
|
|
|
let mut min_time = std::u64::MAX;
|
|
|
|
let mut min_index = 0;
|
|
|
|
for (i, time) in array.iter().enumerate() {
|
|
|
|
if *time < min_time {
|
|
|
|
min_time = *time;
|
|
|
|
min_index = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(min_time, min_index)
|
|
|
|
}
|
|
|
|
|
2019-05-27 20:54:44 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-12-03 09:00:31 -08:00
|
|
|
use {super::*, rayon::prelude::*};
|
2019-05-27 20:54:44 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_client_optimizer() {
|
|
|
|
solana_logger::setup();
|
|
|
|
|
|
|
|
const NUM_CLIENTS: usize = 5;
|
|
|
|
let optimizer = ClientOptimizer::new(NUM_CLIENTS);
|
|
|
|
(0..NUM_CLIENTS).into_par_iter().for_each(|_| {
|
|
|
|
let index = optimizer.experiment();
|
|
|
|
optimizer.report(index, (NUM_CLIENTS - index) as u64);
|
|
|
|
});
|
|
|
|
|
|
|
|
let index = optimizer.experiment();
|
|
|
|
optimizer.report(index, 50);
|
|
|
|
assert_eq!(optimizer.best(), NUM_CLIENTS - 1);
|
|
|
|
|
|
|
|
optimizer.report(optimizer.best(), std::u64::MAX);
|
|
|
|
assert_eq!(optimizer.best(), NUM_CLIENTS - 2);
|
|
|
|
}
|
|
|
|
}
|