2023-06-15 01:41:58 -07:00
|
|
|
use log::warn;
|
|
|
|
use solana_client::connection_cache::ConnectionCache;
|
2023-06-23 06:30:02 -07:00
|
|
|
use solana_lite_rpc_services::transaction_service::TransactionService;
|
2023-04-19 04:29:26 -07:00
|
|
|
use solana_sdk::transaction::Transaction;
|
2023-03-15 08:13:25 -07:00
|
|
|
|
2023-06-15 01:41:58 -07:00
|
|
|
use tokio::sync::mpsc::UnboundedSender;
|
2023-03-14 05:39:19 -07:00
|
|
|
|
2023-06-15 01:41:58 -07:00
|
|
|
use crate::{states::TransactionSendRecord, stats::MangoSimulationStats};
|
2023-03-14 05:39:19 -07:00
|
|
|
pub type QuicConnectionCache = ConnectionCache;
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct TpuManager {
|
|
|
|
// why arc twice / one is so that we clone rwlock and other so that we can clone tpu client
|
2023-06-15 01:41:58 -07:00
|
|
|
transaction_service: TransactionService,
|
2023-03-15 08:13:25 -07:00
|
|
|
stats: MangoSimulationStats,
|
|
|
|
tx_send_record: UnboundedSender<TransactionSendRecord>,
|
2023-03-14 05:39:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TpuManager {
|
|
|
|
pub async fn new(
|
2023-06-15 01:41:58 -07:00
|
|
|
transaction_service: TransactionService,
|
2023-03-15 08:13:25 -07:00
|
|
|
stats: MangoSimulationStats,
|
|
|
|
tx_send_record: UnboundedSender<TransactionSendRecord>,
|
2023-06-15 01:41:58 -07:00
|
|
|
) -> anyhow::Result<Self> {
|
|
|
|
Ok(Self {
|
|
|
|
transaction_service,
|
2023-03-15 08:13:25 -07:00
|
|
|
stats,
|
|
|
|
tx_send_record,
|
2023-06-15 01:41:58 -07:00
|
|
|
})
|
2023-03-14 05:39:19 -07:00
|
|
|
}
|
|
|
|
|
2023-03-15 08:13:25 -07:00
|
|
|
pub async fn send_transaction(
|
2023-03-14 05:39:19 -07:00
|
|
|
&self,
|
2023-03-15 08:13:25 -07:00
|
|
|
transaction: &solana_sdk::transaction::Transaction,
|
|
|
|
transaction_sent_record: TransactionSendRecord,
|
|
|
|
) -> bool {
|
|
|
|
self.stats
|
|
|
|
.inc_send(&transaction_sent_record.keeper_instruction);
|
|
|
|
|
|
|
|
let tx_sent_record = self.tx_send_record.clone();
|
|
|
|
let sent = tx_sent_record.send(transaction_sent_record);
|
|
|
|
if sent.is_err() {
|
|
|
|
warn!(
|
|
|
|
"sending error on channel : {}",
|
|
|
|
sent.err().unwrap().to_string()
|
|
|
|
);
|
2023-03-14 05:39:19 -07:00
|
|
|
}
|
2023-06-15 01:41:58 -07:00
|
|
|
let transaction = bincode::serialize(transaction).unwrap();
|
2023-03-15 08:13:25 -07:00
|
|
|
|
2023-06-27 01:44:59 -07:00
|
|
|
let res = self.transaction_service
|
2023-06-23 06:30:02 -07:00
|
|
|
.send_transaction(transaction, None)
|
2023-06-27 01:44:59 -07:00
|
|
|
.await;
|
|
|
|
|
|
|
|
if let Err(e) = &res{
|
|
|
|
print!("error sending txs on custom tpu {e:?}");
|
|
|
|
}
|
|
|
|
res.is_ok()
|
|
|
|
|
2023-03-14 05:39:19 -07:00
|
|
|
}
|
2023-04-18 08:00:57 -07:00
|
|
|
|
2023-04-18 16:20:13 -07:00
|
|
|
pub async fn send_transaction_batch(
|
2023-04-18 08:00:57 -07:00
|
|
|
&self,
|
2023-04-19 04:29:26 -07:00
|
|
|
batch: &Vec<(Transaction, TransactionSendRecord)>,
|
2023-04-18 08:00:57 -07:00
|
|
|
) -> bool {
|
2023-06-15 01:41:58 -07:00
|
|
|
let mut value = true;
|
|
|
|
for (tx, record) in batch {
|
|
|
|
value &= self.send_transaction(tx, record.clone()).await;
|
2023-04-18 08:00:57 -07:00
|
|
|
}
|
2023-06-15 01:41:58 -07:00
|
|
|
value
|
|
|
|
}
|
2023-03-14 05:39:19 -07:00
|
|
|
}
|