2022-03-09 18:33:05 -08:00
|
|
|
//! Simple TPU client that communicates with the given UDP port with UDP and provides
|
|
|
|
//! an interface for sending transactions
|
|
|
|
|
|
|
|
use {
|
2022-05-26 08:21:16 -07:00
|
|
|
crate::{connection_cache::ConnectionCacheStats, tpu_connection::TpuConnection},
|
2022-03-24 08:40:26 -07:00
|
|
|
core::iter::repeat,
|
2022-05-16 11:13:18 -07:00
|
|
|
solana_net_utils::VALIDATOR_PORT_RANGE,
|
2022-03-21 18:45:22 -07:00
|
|
|
solana_sdk::transport::Result as TransportResult,
|
2022-03-24 08:40:26 -07:00
|
|
|
solana_streamer::sendmmsg::batch_send,
|
2022-04-12 20:04:40 -07:00
|
|
|
std::{
|
2022-05-16 11:13:18 -07:00
|
|
|
net::{IpAddr, Ipv4Addr, SocketAddr, UdpSocket},
|
2022-04-12 20:04:40 -07:00
|
|
|
sync::Arc,
|
|
|
|
},
|
2022-03-09 18:33:05 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
pub struct UdpTpuConnection {
|
|
|
|
socket: UdpSocket,
|
|
|
|
addr: SocketAddr,
|
|
|
|
}
|
|
|
|
|
2022-05-26 08:21:16 -07:00
|
|
|
impl UdpTpuConnection {
|
|
|
|
pub fn new(tpu_addr: SocketAddr, _connection_stats: Arc<ConnectionCacheStats>) -> Self {
|
2022-05-16 11:13:18 -07:00
|
|
|
let (_, client_socket) = solana_net_utils::bind_in_range(
|
|
|
|
IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
|
|
|
|
VALIDATOR_PORT_RANGE,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2022-03-09 18:33:05 -08:00
|
|
|
Self {
|
|
|
|
socket: client_socket,
|
|
|
|
addr: tpu_addr,
|
|
|
|
}
|
|
|
|
}
|
2022-05-26 08:21:16 -07:00
|
|
|
}
|
2022-03-09 18:33:05 -08:00
|
|
|
|
2022-05-26 08:21:16 -07:00
|
|
|
impl TpuConnection for UdpTpuConnection {
|
2022-03-09 18:33:05 -08:00
|
|
|
fn tpu_addr(&self) -> &SocketAddr {
|
|
|
|
&self.addr
|
|
|
|
}
|
|
|
|
|
2022-05-26 08:21:16 -07:00
|
|
|
fn send_wire_transaction_async(&self, wire_transaction: Vec<u8>) -> TransportResult<()> {
|
2022-04-12 07:15:59 -07:00
|
|
|
self.socket.send_to(wire_transaction.as_ref(), self.addr)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-05-26 08:21:16 -07:00
|
|
|
fn send_wire_transaction_batch<T>(&self, buffers: &[T]) -> TransportResult<()>
|
2022-03-24 08:40:26 -07:00
|
|
|
where
|
|
|
|
T: AsRef<[u8]>,
|
|
|
|
{
|
|
|
|
let pkts: Vec<_> = buffers.iter().zip(repeat(self.tpu_addr())).collect();
|
|
|
|
batch_send(&self.socket, &pkts)?;
|
2022-03-09 18:33:05 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-05-26 08:21:16 -07:00
|
|
|
fn send_wire_transaction_batch_async(&self, buffers: Vec<Vec<u8>>) -> TransportResult<()> {
|
2022-04-14 19:20:34 -07:00
|
|
|
let pkts: Vec<_> = buffers.into_iter().zip(repeat(self.tpu_addr())).collect();
|
|
|
|
batch_send(&self.socket, &pkts)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-03-09 18:33:05 -08:00
|
|
|
}
|