2023-07-20 00:28:55 -07:00
|
|
|
use anyhow::Context;
|
2023-08-08 07:36:24 -07:00
|
|
|
use itertools::Itertools;
|
2023-07-20 00:28:55 -07:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2023-08-08 07:36:24 -07:00
|
|
|
use solana_sdk::signature::Signature;
|
2023-07-20 00:28:55 -07:00
|
|
|
use solana_sdk::transaction::VersionedTransaction;
|
2023-08-08 07:36:24 -07:00
|
|
|
use std::collections::hash_map::DefaultHasher;
|
2023-07-31 05:26:21 -07:00
|
|
|
use std::fmt;
|
|
|
|
use std::fmt::Display;
|
2023-08-08 07:36:24 -07:00
|
|
|
use std::hash::{Hash, Hasher};
|
2023-07-31 05:26:21 -07:00
|
|
|
use std::net::SocketAddr;
|
2023-07-20 00:28:55 -07:00
|
|
|
|
|
|
|
///
|
|
|
|
/// lite-rpc to proxy wire format
|
|
|
|
/// compat info: non-public format ATM
|
|
|
|
/// initial version
|
2023-08-09 08:00:17 -07:00
|
|
|
pub const FORMAT_VERSION1: u16 = 2500;
|
2023-08-08 07:36:24 -07:00
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
|
|
pub struct TxData(Signature, Vec<u8>);
|
2023-07-20 00:28:55 -07:00
|
|
|
|
2023-08-09 08:00:17 -07:00
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
|
|
pub struct TpuNode {
|
|
|
|
pub tpu_socket_addr: SocketAddr,
|
|
|
|
pub identity_tpunode: Pubkey, // note: this is only used for debugging
|
|
|
|
}
|
|
|
|
|
2023-07-20 00:28:55 -07:00
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
|
|
pub struct TpuForwardingRequest {
|
|
|
|
format_version: u16,
|
2023-08-10 01:31:31 -07:00
|
|
|
// note: this data gets stale
|
2023-08-09 08:00:17 -07:00
|
|
|
tpu_nodes: Vec<TpuNode>,
|
2023-08-08 07:36:24 -07:00
|
|
|
transactions: Vec<TxData>,
|
2023-07-20 00:28:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for TpuForwardingRequest {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2023-07-31 05:26:21 -07:00
|
|
|
write!(
|
|
|
|
f,
|
2023-08-09 08:00:17 -07:00
|
|
|
"TpuForwardingRequest t9 {} tpu nodes",
|
|
|
|
&self.tpu_nodes.len(),
|
2023-07-31 05:26:21 -07:00
|
|
|
)
|
2023-07-20 00:28:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TpuForwardingRequest {
|
2023-07-31 05:26:21 -07:00
|
|
|
pub fn new(
|
2023-08-09 08:00:17 -07:00
|
|
|
tpu_fanout_nodes: Vec<(SocketAddr, Pubkey)>,
|
2023-07-31 05:26:21 -07:00
|
|
|
transactions: Vec<VersionedTransaction>,
|
|
|
|
) -> Self {
|
2023-07-20 00:28:55 -07:00
|
|
|
TpuForwardingRequest {
|
|
|
|
format_version: FORMAT_VERSION1,
|
2023-08-09 08:00:17 -07:00
|
|
|
tpu_nodes: tpu_fanout_nodes
|
|
|
|
.iter()
|
|
|
|
.map(|(tpu_addr, identity)| TpuNode {
|
|
|
|
tpu_socket_addr: *tpu_addr,
|
|
|
|
identity_tpunode: *identity,
|
|
|
|
})
|
|
|
|
.collect_vec(),
|
|
|
|
transactions: transactions
|
|
|
|
.iter()
|
|
|
|
.map(|tx| TxData(tx.signatures[0], bincode::serialize(tx).unwrap()))
|
|
|
|
.collect_vec(),
|
2023-07-20 00:28:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-09 08:00:17 -07:00
|
|
|
// test only
|
2023-08-08 06:41:29 -07:00
|
|
|
pub fn try_serialize_wire_format(&self) -> anyhow::Result<Vec<u8>> {
|
|
|
|
bincode::serialize(&self)
|
|
|
|
.context("serialize proxy request")
|
|
|
|
.map_err(anyhow::Error::from)
|
2023-08-08 06:30:42 -07:00
|
|
|
}
|
|
|
|
|
2023-08-08 06:41:29 -07:00
|
|
|
pub fn try_deserialize_from_wire_format(
|
|
|
|
raw_proxy_request: &[u8],
|
|
|
|
) -> anyhow::Result<TpuForwardingRequest> {
|
|
|
|
let request = bincode::deserialize::<TpuForwardingRequest>(raw_proxy_request);
|
2023-08-08 06:30:42 -07:00
|
|
|
|
2023-08-08 06:41:29 -07:00
|
|
|
if let Ok(ref req) = request {
|
|
|
|
assert_eq!(req.format_version, FORMAT_VERSION1);
|
|
|
|
}
|
2023-08-08 06:30:42 -07:00
|
|
|
|
|
|
|
request
|
2023-08-08 06:41:29 -07:00
|
|
|
.context("deserialize proxy request")
|
|
|
|
.map_err(anyhow::Error::from)
|
2023-08-08 06:30:42 -07:00
|
|
|
}
|
|
|
|
|
2023-08-09 08:00:17 -07:00
|
|
|
pub fn get_tpu_nodes(&self) -> &Vec<TpuNode> {
|
|
|
|
&self.tpu_nodes
|
2023-07-20 00:28:55 -07:00
|
|
|
}
|
|
|
|
|
2023-08-08 07:36:24 -07:00
|
|
|
pub fn get_transaction_bytes(&self) -> Vec<Vec<u8>> {
|
|
|
|
self.transactions
|
|
|
|
.iter()
|
|
|
|
.map(|tx| tx.1.clone())
|
|
|
|
.collect_vec()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_hash(&self) -> u64 {
|
|
|
|
let mut hasher = DefaultHasher::new();
|
|
|
|
// note: assumes that there are transactions with >=0 signatures
|
|
|
|
self.transactions[0].0.hash(&mut hasher);
|
|
|
|
hasher.finish()
|
2023-07-20 00:28:55 -07:00
|
|
|
}
|
|
|
|
}
|