solana/client/src/thin_client.rs

166 lines
5.6 KiB
Rust
Raw Normal View History

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.
use crate::rpc_client::RpcClient;
2019-03-12 17:26:07 -07:00
use bincode::serialize_into;
use log::*;
use solana_sdk::hash::Hash;
use solana_sdk::packet::PACKET_DATA_SIZE;
use solana_sdk::pubkey::Pubkey;
2019-03-16 13:36:47 -07:00
use solana_sdk::signature::{Keypair, Signature};
2019-03-12 17:26:07 -07:00
use solana_sdk::transaction::Transaction;
use std::error;
2019-03-12 17:26:07 -07:00
use std::io;
use std::net::{SocketAddr, UdpSocket};
2019-03-16 13:38:30 -07:00
use std::time::Duration;
2019-03-12 17:26:07 -07:00
/// An object for querying and sending transactions to the network.
pub struct ThinClient {
transactions_addr: SocketAddr,
transactions_socket: UdpSocket,
rpc_client: RpcClient,
}
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 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,
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);
Self::new_from_client(transactions_addr, transactions_socket, rpc_client)
2019-03-12 17:26:07 -07:00
}
fn new_from_client(
transactions_addr: SocketAddr,
transactions_socket: UdpSocket,
rpc_client: RpcClient,
) -> Self {
Self {
2019-03-12 17:26:07 -07:00
rpc_client,
transactions_addr,
transactions_socket,
}
}
/// Send a signed Transaction to the server for processing. This method
/// does not wait for a response.
pub fn transfer_signed(&self, transaction: &Transaction) -> io::Result<Signature> {
let mut buf = vec![0; transaction.serialized_size().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
.send_to(&buf[..], &self.transactions_addr)?;
Ok(transaction.signatures[0])
}
/// Retry a sending a signed Transaction to the server for processing.
pub fn retry_transfer(
&self,
2019-03-12 17:26:07 -07:00
keypair: &Keypair,
transaction: &mut Transaction,
tries: usize,
) -> io::Result<Signature> {
for x in 0..tries {
transaction.sign(&[keypair], self.get_recent_blockhash()?);
2019-03-12 17:26:07 -07:00
let mut buf = vec![0; transaction.serialized_size().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");
self.transactions_socket
.send_to(&buf[..], &self.transactions_addr)?;
if self.poll_for_signature(&transaction.signatures[0]).is_ok() {
return Ok(transaction.signatures[0]);
}
info!("{} tries failed transfer to {}", x, self.transactions_addr);
}
Err(io::Error::new(
io::ErrorKind::Other,
"retry_transfer failed",
))
}
pub fn get_account_data(&self, pubkey: &Pubkey) -> io::Result<Vec<u8>> {
self.rpc_client.get_account_data(pubkey)
2019-03-12 17:26:07 -07:00
}
pub fn get_balance(&self, pubkey: &Pubkey) -> io::Result<u64> {
self.rpc_client.get_balance(pubkey)
2019-03-12 17:26:07 -07:00
}
pub fn get_transaction_count(&self) -> Result<u64, Box<dyn error::Error>> {
self.rpc_client.get_transaction_count()
2019-03-12 17:26:07 -07:00
}
pub fn get_recent_blockhash(&self) -> io::Result<Hash> {
self.rpc_client.get_recent_blockhash()
2019-03-12 17:26:07 -07:00
}
pub fn get_new_blockhash(&self, blockhash: &Hash) -> io::Result<Hash> {
self.rpc_client.get_new_blockhash(blockhash)
2019-03-12 17:26:07 -07:00
}
pub fn poll_balance_with_timeout(
&self,
2019-03-12 17:26:07 -07:00
pubkey: &Pubkey,
polling_frequency: &Duration,
timeout: &Duration,
) -> io::Result<u64> {
self.rpc_client
.poll_balance_with_timeout(pubkey, polling_frequency, timeout)
2019-03-12 17:26:07 -07:00
}
pub fn poll_get_balance(&self, pubkey: &Pubkey) -> io::Result<u64> {
self.rpc_client.poll_get_balance(pubkey)
2019-03-12 17:26:07 -07:00
}
pub fn wait_for_balance(&self, pubkey: &Pubkey, expected_balance: Option<u64>) -> Option<u64> {
self.rpc_client.wait_for_balance(pubkey, expected_balance)
}
pub fn poll_for_signature(&self, signature: &Signature) -> io::Result<()> {
self.rpc_client.poll_for_signature(signature)
2019-03-12 17:26:07 -07:00
}
pub fn check_signature(&self, signature: &Signature) -> bool {
2019-03-16 13:38:30 -07:00
self.rpc_client.check_signature(signature)
2019-03-12 17:26:07 -07:00
}
pub fn fullnode_exit(&self) -> io::Result<bool> {
self.rpc_client.fullnode_exit()
2019-03-12 17:26:07 -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)
}