2022-03-09 18:33:05 -08:00
|
|
|
//! Simple client that connects to a given UDP port with the QUIC protocol and provides
|
|
|
|
//! an interface for sending transactions which is restricted by the server's flow control.
|
|
|
|
|
|
|
|
use {
|
2022-04-12 20:04:40 -07:00
|
|
|
crate::{
|
2022-05-19 12:29:27 -07:00
|
|
|
connection_cache::ConnectionCacheStats,
|
2022-06-28 08:01:49 -07:00
|
|
|
nonblocking::{
|
|
|
|
quic_client::{
|
|
|
|
QuicClient, QuicLazyInitializedEndpoint,
|
|
|
|
QuicTpuConnection as NonblockingQuicTpuConnection,
|
|
|
|
},
|
|
|
|
tpu_connection::TpuConnection as NonblockingTpuConnection,
|
|
|
|
},
|
|
|
|
tpu_connection::TpuConnection,
|
2022-04-12 20:04:40 -07:00
|
|
|
},
|
2022-04-12 07:15:59 -07:00
|
|
|
lazy_static::lazy_static,
|
2022-06-10 09:25:24 -07:00
|
|
|
solana_sdk::transport::Result as TransportResult,
|
2022-06-01 16:20:11 -07:00
|
|
|
std::{net::SocketAddr, sync::Arc},
|
2022-03-09 18:33:05 -08:00
|
|
|
tokio::runtime::Runtime,
|
|
|
|
};
|
|
|
|
|
2022-04-12 07:15:59 -07:00
|
|
|
lazy_static! {
|
|
|
|
static ref RUNTIME: Runtime = tokio::runtime::Builder::new_multi_thread()
|
|
|
|
.enable_all()
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
}
|
2022-03-09 18:33:05 -08:00
|
|
|
|
|
|
|
pub struct QuicTpuConnection {
|
2022-06-28 08:01:49 -07:00
|
|
|
inner: Arc<NonblockingQuicTpuConnection>,
|
2022-03-09 18:33:05 -08:00
|
|
|
}
|
2022-04-12 20:04:40 -07:00
|
|
|
impl QuicTpuConnection {
|
2022-06-10 09:25:24 -07:00
|
|
|
pub fn new(
|
|
|
|
endpoint: Arc<QuicLazyInitializedEndpoint>,
|
|
|
|
tpu_addr: SocketAddr,
|
|
|
|
connection_stats: Arc<ConnectionCacheStats>,
|
|
|
|
) -> Self {
|
2022-06-28 08:01:49 -07:00
|
|
|
let inner = Arc::new(NonblockingQuicTpuConnection::new(
|
|
|
|
endpoint,
|
|
|
|
tpu_addr,
|
|
|
|
connection_stats,
|
|
|
|
));
|
|
|
|
Self { inner }
|
|
|
|
}
|
2022-03-09 18:33:05 -08:00
|
|
|
|
2022-06-28 08:01:49 -07:00
|
|
|
pub fn new_with_client(
|
|
|
|
client: Arc<QuicClient>,
|
|
|
|
connection_stats: Arc<ConnectionCacheStats>,
|
|
|
|
) -> Self {
|
|
|
|
let inner = Arc::new(NonblockingQuicTpuConnection::new_with_client(
|
2022-05-19 12:29:27 -07:00
|
|
|
client,
|
|
|
|
connection_stats,
|
2022-06-28 08:01:49 -07:00
|
|
|
));
|
|
|
|
Self { inner }
|
2022-03-09 18:33:05 -08:00
|
|
|
}
|
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 QuicTpuConnection {
|
2022-03-09 18:33:05 -08:00
|
|
|
fn tpu_addr(&self) -> &SocketAddr {
|
2022-06-28 08:01:49 -07:00
|
|
|
self.inner.tpu_addr()
|
2022-03-09 18:33:05 -08:00
|
|
|
}
|
|
|
|
|
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
|
2022-06-28 08:01:49 -07:00
|
|
|
T: AsRef<[u8]> + Send + Sync,
|
2022-03-24 08:40:26 -07:00
|
|
|
{
|
2022-06-30 12:09:07 -07:00
|
|
|
RUNTIME.block_on(self.inner.send_wire_transaction_batch(buffers))?;
|
2022-04-12 07:15:59 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-05-26 08:21:16 -07:00
|
|
|
fn send_wire_transaction_async(&self, wire_transaction: Vec<u8>) -> TransportResult<()> {
|
2022-06-28 08:01:49 -07:00
|
|
|
let inner = self.inner.clone();
|
2022-04-14 19:20:34 -07:00
|
|
|
//drop and detach the task
|
2022-06-28 08:01:49 -07:00
|
|
|
let _ = RUNTIME.spawn(async move { inner.send_wire_transaction(wire_transaction).await });
|
2022-04-14 19:20:34 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-05-26 08:21:16 -07:00
|
|
|
fn send_wire_transaction_batch_async(&self, buffers: Vec<Vec<u8>>) -> TransportResult<()> {
|
2022-06-28 08:01:49 -07:00
|
|
|
let inner = self.inner.clone();
|
2022-04-14 19:20:34 -07:00
|
|
|
//drop and detach the task
|
2022-06-28 08:01:49 -07:00
|
|
|
let _ = RUNTIME.spawn(async move { inner.send_wire_transaction_batch(&buffers).await });
|
2022-03-09 18:33:05 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|