2019-03-12 18:26:07 -06: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.
|
|
|
|
|
2019-03-16 22:37:20 -07:00
|
|
|
use crate::rpc_client::RpcClient;
|
2019-03-24 22:51:56 -07:00
|
|
|
use bincode::{serialize_into, serialized_size};
|
2019-03-12 18:26:07 -06:00
|
|
|
use log::*;
|
2019-06-13 22:30:51 -07:00
|
|
|
use solana_sdk::account::Account;
|
2019-04-11 00:25:14 -07:00
|
|
|
use solana_sdk::client::{AsyncClient, Client, SyncClient};
|
2019-05-13 12:49:37 -07:00
|
|
|
use solana_sdk::fee_calculator::FeeCalculator;
|
2019-03-12 18:26:07 -06:00
|
|
|
use solana_sdk::hash::Hash;
|
2019-04-05 21:57:59 -06:00
|
|
|
use solana_sdk::instruction::Instruction;
|
|
|
|
use solana_sdk::message::Message;
|
2019-03-12 18:26:07 -06:00
|
|
|
use solana_sdk::packet::PACKET_DATA_SIZE;
|
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2019-04-05 21:57:59 -06:00
|
|
|
use solana_sdk::signature::{Keypair, KeypairUtil, Signature};
|
|
|
|
use solana_sdk::system_instruction;
|
2019-06-24 16:46:34 -07:00
|
|
|
use solana_sdk::timing::{duration_as_ms, MAX_PROCESSING_AGE};
|
2019-04-05 21:57:59 -06:00
|
|
|
use solana_sdk::transaction::{self, Transaction};
|
|
|
|
use solana_sdk::transport::Result as TransportResult;
|
2019-03-12 18:26:07 -06:00
|
|
|
use std::io;
|
|
|
|
use std::net::{SocketAddr, UdpSocket};
|
2019-05-27 20:54:44 -07:00
|
|
|
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
|
|
|
use std::sync::RwLock;
|
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
|
|
|
|
struct ClientOptimizer {
|
|
|
|
cur_index: AtomicUsize,
|
|
|
|
experiment_index: AtomicUsize,
|
|
|
|
experiment_done: AtomicBool,
|
|
|
|
times: RwLock<Vec<u64>>,
|
|
|
|
num_clients: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ClientOptimizer {
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.best()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn report(&self, index: usize, time_ms: u64) {
|
|
|
|
if self.num_clients > 1
|
|
|
|
&& (!self.experiment_done.load(Ordering::Relaxed) || time_ms == std::u64::MAX)
|
|
|
|
{
|
|
|
|
trace!(
|
|
|
|
"report {} with {} exp: {}",
|
|
|
|
index,
|
|
|
|
time_ms,
|
|
|
|
self.experiment_index.load(Ordering::Relaxed)
|
|
|
|
);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn best(&self) -> usize {
|
|
|
|
self.cur_index.load(Ordering::Relaxed)
|
|
|
|
}
|
|
|
|
}
|
2019-03-12 18:26:07 -06:00
|
|
|
|
|
|
|
/// An object for querying and sending transactions to the network.
|
|
|
|
pub struct ThinClient {
|
|
|
|
transactions_socket: UdpSocket,
|
2019-05-27 20:54:44 -07:00
|
|
|
transactions_addrs: Vec<SocketAddr>,
|
|
|
|
rpc_clients: Vec<RpcClient>,
|
|
|
|
optimizer: ClientOptimizer,
|
2019-03-12 18:26:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ThinClient {
|
|
|
|
/// Create a new ThinClient that will interface with the Rpc at `rpc_addr` using TCP
|
|
|
|
/// and the Tpu at `transactions_addr` over `transactions_socket` using UDP.
|
|
|
|
pub fn new(
|
|
|
|
rpc_addr: SocketAddr,
|
|
|
|
transactions_addr: SocketAddr,
|
|
|
|
transactions_socket: UdpSocket,
|
|
|
|
) -> Self {
|
|
|
|
Self::new_from_client(
|
|
|
|
transactions_addr,
|
|
|
|
transactions_socket,
|
2019-03-15 22:42:36 -07:00
|
|
|
RpcClient::new_socket(rpc_addr),
|
2019-03-12 18:26:07 -06:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-03-15 22:42:36 -07:00
|
|
|
pub fn new_socket_with_timeout(
|
2019-03-12 18:26:07 -06:00
|
|
|
rpc_addr: SocketAddr,
|
|
|
|
transactions_addr: SocketAddr,
|
|
|
|
transactions_socket: UdpSocket,
|
|
|
|
timeout: Duration,
|
|
|
|
) -> Self {
|
2019-03-15 22:42:36 -07:00
|
|
|
let rpc_client = RpcClient::new_socket_with_timeout(rpc_addr, timeout);
|
2019-03-16 08:31:19 -07:00
|
|
|
Self::new_from_client(transactions_addr, transactions_socket, rpc_client)
|
2019-03-12 18:26:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn new_from_client(
|
|
|
|
transactions_addr: SocketAddr,
|
|
|
|
transactions_socket: UdpSocket,
|
|
|
|
rpc_client: RpcClient,
|
|
|
|
) -> Self {
|
2019-03-16 21:51:41 -07:00
|
|
|
Self {
|
2019-03-12 18:26:07 -06:00
|
|
|
transactions_socket,
|
2019-05-27 20:54:44 -07:00
|
|
|
transactions_addrs: vec![transactions_addr],
|
|
|
|
rpc_clients: vec![rpc_client],
|
|
|
|
optimizer: ClientOptimizer::new(0),
|
2019-03-12 18:26:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-27 20:54:44 -07:00
|
|
|
pub fn new_from_addrs(
|
|
|
|
transactions_addrs: Vec<SocketAddr>,
|
|
|
|
transactions_socket: UdpSocket,
|
|
|
|
rpc_sockets: Vec<SocketAddr>,
|
|
|
|
) -> Self {
|
|
|
|
assert!(!transactions_addrs.is_empty());
|
|
|
|
assert!(!rpc_sockets.is_empty());
|
|
|
|
assert_eq!(rpc_sockets.len(), transactions_addrs.len());
|
|
|
|
let rpc_len = rpc_sockets.len();
|
|
|
|
let rpc_clients: Vec<_> = rpc_sockets.into_iter().map(RpcClient::new_socket).collect();
|
|
|
|
Self {
|
|
|
|
transactions_addrs,
|
|
|
|
transactions_socket,
|
|
|
|
rpc_clients,
|
|
|
|
optimizer: ClientOptimizer::new(rpc_len),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn transactions_addr(&self) -> &SocketAddr {
|
|
|
|
&self.transactions_addrs[self.optimizer.best()]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rpc_client(&self) -> &RpcClient {
|
|
|
|
&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,
|
|
|
|
) -> io::Result<Signature> {
|
2019-04-05 21:57:59 -06:00
|
|
|
self.send_and_confirm_transaction(&[keypair], transaction, tries, min_confirmed_blocks)
|
2019-03-21 07:43:21 -07:00
|
|
|
}
|
|
|
|
|
2019-04-05 21:57:59 -06:00
|
|
|
/// Retry sending a signed Transaction with one signing Keypair to the server for processing.
|
2019-03-12 18:26:07 -06:00
|
|
|
pub fn retry_transfer(
|
2019-03-16 08:31:19 -07:00
|
|
|
&self,
|
2019-03-12 18:26:07 -06:00
|
|
|
keypair: &Keypair,
|
|
|
|
transaction: &mut Transaction,
|
|
|
|
tries: usize,
|
2019-04-05 21:57:59 -06:00
|
|
|
) -> io::Result<Signature> {
|
|
|
|
self.send_and_confirm_transaction(&[keypair], transaction, tries, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Retry sending a signed Transaction to the server for processing
|
|
|
|
pub fn send_and_confirm_transaction(
|
|
|
|
&self,
|
|
|
|
keypairs: &[&Keypair],
|
|
|
|
transaction: &mut Transaction,
|
|
|
|
tries: usize,
|
|
|
|
min_confirmed_blocks: usize,
|
2019-03-12 18:26:07 -06:00
|
|
|
) -> io::Result<Signature> {
|
|
|
|
for x in 0..tries {
|
2019-06-24 16:46:34 -07:00
|
|
|
let now = Instant::now();
|
2019-03-24 22:51:56 -07:00
|
|
|
let mut buf = vec![0; serialized_size(&transaction).unwrap() as usize];
|
2019-03-12 18:26:07 -06:00
|
|
|
let mut wr = std::io::Cursor::new(&mut buf[..]);
|
|
|
|
serialize_into(&mut wr, &transaction)
|
|
|
|
.expect("serialize Transaction in pub fn transfer_signed");
|
2019-06-24 16:46:34 -07:00
|
|
|
// resend the same transaction until the transaction has no chance of succeeding
|
|
|
|
while now.elapsed().as_secs() < MAX_PROCESSING_AGE as u64 {
|
|
|
|
self.transactions_socket
|
|
|
|
.send_to(&buf[..], &self.transactions_addr())?;
|
|
|
|
if self
|
|
|
|
.poll_for_signature_confirmation(
|
|
|
|
&transaction.signatures[0],
|
|
|
|
min_confirmed_blocks,
|
|
|
|
)
|
|
|
|
.is_ok()
|
|
|
|
{
|
|
|
|
return Ok(transaction.signatures[0]);
|
|
|
|
}
|
2019-03-12 18:26:07 -06:00
|
|
|
}
|
2019-05-27 20:54:44 -07:00
|
|
|
info!(
|
|
|
|
"{} tries failed transfer to {}",
|
|
|
|
x,
|
|
|
|
self.transactions_addr()
|
|
|
|
);
|
|
|
|
let (blockhash, _fee_calculator) = self.rpc_client().get_recent_blockhash()?;
|
2019-05-13 12:49:37 -07:00
|
|
|
transaction.sign(keypairs, blockhash);
|
2019-03-12 18:26:07 -06:00
|
|
|
}
|
|
|
|
Err(io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
2019-04-05 21:57:59 -06:00
|
|
|
format!("retry_transfer failed in {} retries", tries),
|
2019-03-12 18:26:07 -06:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn poll_balance_with_timeout(
|
2019-03-16 08:31:19 -07:00
|
|
|
&self,
|
2019-03-12 18:26:07 -06:00
|
|
|
pubkey: &Pubkey,
|
|
|
|
polling_frequency: &Duration,
|
|
|
|
timeout: &Duration,
|
|
|
|
) -> io::Result<u64> {
|
2019-05-27 20:54:44 -07:00
|
|
|
self.rpc_client()
|
2019-03-16 08:31:19 -07:00
|
|
|
.poll_balance_with_timeout(pubkey, polling_frequency, timeout)
|
2019-03-12 18:26:07 -06:00
|
|
|
}
|
|
|
|
|
2019-03-16 08:31:19 -07:00
|
|
|
pub fn poll_get_balance(&self, pubkey: &Pubkey) -> io::Result<u64> {
|
2019-05-27 20:54:44 -07:00
|
|
|
self.rpc_client().poll_get_balance(pubkey)
|
2019-03-12 18:26:07 -06:00
|
|
|
}
|
|
|
|
|
2019-03-16 23:29:11 -07:00
|
|
|
pub fn wait_for_balance(&self, pubkey: &Pubkey, expected_balance: Option<u64>) -> Option<u64> {
|
2019-05-27 20:54:44 -07:00
|
|
|
self.rpc_client().wait_for_balance(pubkey, expected_balance)
|
2019-03-16 23:29:11 -07:00
|
|
|
}
|
|
|
|
|
2019-03-21 07:43:21 -07:00
|
|
|
/// Check a signature in the bank. This method blocks
|
|
|
|
/// until the server sends a response.
|
2019-03-16 08:31:19 -07:00
|
|
|
pub fn check_signature(&self, signature: &Signature) -> bool {
|
2019-05-27 20:54:44 -07:00
|
|
|
self.rpc_client().check_signature(signature)
|
2019-03-12 18:26:07 -06:00
|
|
|
}
|
2019-03-16 08:31:19 -07:00
|
|
|
|
|
|
|
pub fn fullnode_exit(&self) -> io::Result<bool> {
|
2019-05-27 20:54:44 -07:00
|
|
|
self.rpc_client().fullnode_exit()
|
2019-03-12 18:26:07 -06:00
|
|
|
}
|
2019-05-27 20:54:44 -07:00
|
|
|
|
2019-03-21 07:43:21 -07:00
|
|
|
pub fn get_num_blocks_since_signature_confirmation(
|
|
|
|
&mut self,
|
|
|
|
sig: &Signature,
|
|
|
|
) -> io::Result<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)
|
|
|
|
}
|
2019-03-12 18:26:07 -06:00
|
|
|
}
|
|
|
|
|
2019-04-19 15:04:36 -06:00
|
|
|
impl Client for ThinClient {
|
|
|
|
fn transactions_addr(&self) -> String {
|
2019-05-27 20:54:44 -07:00
|
|
|
self.transactions_addr().to_string()
|
2019-04-19 15:04:36 -06:00
|
|
|
}
|
|
|
|
}
|
2019-04-11 00:25:14 -07:00
|
|
|
|
2019-04-05 21:57:59 -06:00
|
|
|
impl SyncClient for ThinClient {
|
|
|
|
fn send_message(&self, keypairs: &[&Keypair], message: Message) -> TransportResult<Signature> {
|
2019-05-13 12:49:37 -07:00
|
|
|
let (blockhash, _fee_calculator) = self.get_recent_blockhash()?;
|
2019-04-05 21:57:59 -06:00
|
|
|
let mut transaction = Transaction::new(&keypairs, message, blockhash);
|
|
|
|
let signature = self.send_and_confirm_transaction(keypairs, &mut transaction, 5, 0)?;
|
|
|
|
Ok(signature)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send_instruction(
|
|
|
|
&self,
|
|
|
|
keypair: &Keypair,
|
|
|
|
instruction: Instruction,
|
|
|
|
) -> TransportResult<Signature> {
|
|
|
|
let message = Message::new(vec![instruction]);
|
|
|
|
self.send_message(&[keypair], message)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn transfer(
|
|
|
|
&self,
|
|
|
|
lamports: u64,
|
|
|
|
keypair: &Keypair,
|
|
|
|
pubkey: &Pubkey,
|
|
|
|
) -> TransportResult<Signature> {
|
|
|
|
let transfer_instruction =
|
|
|
|
system_instruction::transfer(&keypair.pubkey(), pubkey, lamports);
|
|
|
|
self.send_instruction(keypair, transfer_instruction)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 21:57:59 -06:00
|
|
|
}
|
|
|
|
|
2019-06-13 22:30:51 -07:00
|
|
|
fn get_account(&self, pubkey: &Pubkey) -> TransportResult<Option<Account>> {
|
|
|
|
Ok(self.rpc_client().get_account(pubkey).ok())
|
|
|
|
}
|
|
|
|
|
2019-04-05 21:57:59 -06:00
|
|
|
fn get_balance(&self, pubkey: &Pubkey) -> TransportResult<u64> {
|
2019-05-27 20:54:44 -07:00
|
|
|
let balance = self.rpc_client().get_balance(pubkey)?;
|
2019-04-05 21:57:59 -06:00
|
|
|
Ok(balance)
|
|
|
|
}
|
|
|
|
|
2019-06-13 22:30:51 -07:00
|
|
|
fn get_recent_blockhash(&self) -> TransportResult<(Hash, FeeCalculator)> {
|
|
|
|
let index = self.optimizer.experiment();
|
|
|
|
let now = Instant::now();
|
|
|
|
let recent_blockhash = self.rpc_clients[index].get_recent_blockhash();
|
|
|
|
match recent_blockhash {
|
|
|
|
Ok(recent_blockhash) => {
|
|
|
|
self.optimizer.report(index, duration_as_ms(&now.elapsed()));
|
|
|
|
Ok(recent_blockhash)
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
self.optimizer.report(index, std::u64::MAX);
|
|
|
|
Err(e)?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 21:57:59 -06: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()
|
2019-04-05 21:57:59 -06:00
|
|
|
.get_signature_status(&signature.to_string())
|
|
|
|
.map_err(|err| {
|
|
|
|
io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
2019-04-25 11:29:44 -06:00
|
|
|
format!("send_transaction failed with error {:?}", err),
|
2019-04-05 21:57:59 -06:00
|
|
|
)
|
|
|
|
})?;
|
|
|
|
Ok(status)
|
|
|
|
}
|
2019-04-11 00:25:14 -07:00
|
|
|
|
2019-06-12 16:43:05 -07:00
|
|
|
fn get_slot(&self) -> TransportResult<u64> {
|
|
|
|
let slot = self.rpc_client().get_slot().map_err(|err| {
|
|
|
|
io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
|
|
|
format!("send_transaction failed with error {:?}", err),
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
Ok(slot)
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
Err(e)?
|
|
|
|
}
|
|
|
|
}
|
2019-04-11 00:25:14 -07:00
|
|
|
}
|
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,
|
|
|
|
) -> TransportResult<()> {
|
|
|
|
Ok(self
|
2019-05-27 20:54:44 -07:00
|
|
|
.rpc_client()
|
2019-04-25 12:46:40 -07:00
|
|
|
.poll_for_signature_confirmation(signature, min_confirmed_blocks)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll_for_signature(&self, signature: &Signature) -> TransportResult<()> {
|
2019-05-27 20:54:44 -07:00
|
|
|
Ok(self.rpc_client().poll_for_signature(signature)?)
|
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)> {
|
2019-05-27 20:54:44 -07:00
|
|
|
let new_blockhash = self.rpc_client().get_new_blockhash(blockhash)?;
|
|
|
|
Ok(new_blockhash)
|
2019-04-27 08:39:29 -07:00
|
|
|
}
|
2019-04-05 21:57:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AsyncClient for ThinClient {
|
|
|
|
fn async_send_transaction(&self, transaction: Transaction) -> io::Result<Signature> {
|
|
|
|
let mut buf = vec![0; serialized_size(&transaction).unwrap() as usize];
|
|
|
|
let mut wr = std::io::Cursor::new(&mut buf[..]);
|
|
|
|
serialize_into(&mut wr, &transaction)
|
|
|
|
.expect("serialize Transaction in pub fn transfer_signed");
|
|
|
|
assert!(buf.len() < PACKET_DATA_SIZE);
|
|
|
|
self.transactions_socket
|
2019-05-27 20:54:44 -07:00
|
|
|
.send_to(&buf[..], &self.transactions_addr())?;
|
2019-04-05 21:57:59 -06:00
|
|
|
Ok(transaction.signatures[0])
|
|
|
|
}
|
2019-04-11 00:25:14 -07:00
|
|
|
fn async_send_message(
|
|
|
|
&self,
|
|
|
|
keypairs: &[&Keypair],
|
|
|
|
message: Message,
|
|
|
|
recent_blockhash: Hash,
|
|
|
|
) -> io::Result<Signature> {
|
|
|
|
let transaction = Transaction::new(&keypairs, message, recent_blockhash);
|
2019-04-05 21:57:59 -06:00
|
|
|
self.async_send_transaction(transaction)
|
|
|
|
}
|
|
|
|
fn async_send_instruction(
|
|
|
|
&self,
|
|
|
|
keypair: &Keypair,
|
|
|
|
instruction: Instruction,
|
2019-04-11 00:25:14 -07:00
|
|
|
recent_blockhash: Hash,
|
2019-04-05 21:57:59 -06:00
|
|
|
) -> io::Result<Signature> {
|
|
|
|
let message = Message::new(vec![instruction]);
|
2019-04-11 00:25:14 -07:00
|
|
|
self.async_send_message(&[keypair], message, recent_blockhash)
|
2019-04-05 21:57:59 -06:00
|
|
|
}
|
|
|
|
fn async_transfer(
|
|
|
|
&self,
|
|
|
|
lamports: u64,
|
|
|
|
keypair: &Keypair,
|
|
|
|
pubkey: &Pubkey,
|
2019-04-11 00:25:14 -07:00
|
|
|
recent_blockhash: Hash,
|
2019-04-05 21:57:59 -06:00
|
|
|
) -> io::Result<Signature> {
|
|
|
|
let transfer_instruction =
|
|
|
|
system_instruction::transfer(&keypair.pubkey(), pubkey, lamports);
|
2019-04-11 00:25:14 -07:00
|
|
|
self.async_send_instruction(keypair, transfer_instruction, recent_blockhash)
|
2019-04-05 21:57:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 22:13:44 -07:00
|
|
|
pub fn create_client((rpc, tpu): (SocketAddr, SocketAddr), range: (u16, u16)) -> ThinClient {
|
|
|
|
let (_, transactions_socket) = solana_netutil::bind_in_range(range).unwrap();
|
|
|
|
ThinClient::new(rpc, tpu, transactions_socket)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_client_with_timeout(
|
|
|
|
(rpc, tpu): (SocketAddr, SocketAddr),
|
|
|
|
range: (u16, u16),
|
|
|
|
timeout: Duration,
|
|
|
|
) -> ThinClient {
|
|
|
|
let (_, transactions_socket) = solana_netutil::bind_in_range(range).unwrap();
|
|
|
|
ThinClient::new_socket_with_timeout(rpc, tpu, transactions_socket, timeout)
|
|
|
|
}
|
2019-05-27 20:54:44 -07:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use rayon::prelude::*;
|
|
|
|
|
|
|
|
#[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);
|
|
|
|
}
|
|
|
|
}
|