Skip to content

Commit ed573ff

Browse files
authored
add in method for building a TpuClient for LocalCluster tests (pyth-network#258)
* add in method for building a TpuClient for LocalCluster tests * add cluster trait. leave dependency on solana_client::tpu_client
1 parent 2c12500 commit ed573ff

File tree

7 files changed

+72
-57
lines changed

7 files changed

+72
-57
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bench-tps/tests/bench_tps.rs

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@ use {
77
cli::{Config, InstructionPaddingConfig},
88
send_batch::generate_durable_nonce_accounts,
99
},
10-
solana_client::{
11-
connection_cache::ConnectionCache,
12-
tpu_client::{TpuClient, TpuClientConfig},
13-
},
10+
solana_client::tpu_client::{TpuClient, TpuClientConfig},
1411
solana_core::validator::ValidatorConfig,
1512
solana_faucet::faucet::run_local_faucet,
1613
solana_local_cluster::{
14+
cluster::Cluster,
1715
local_cluster::{ClusterConfig, LocalCluster},
1816
validator_configs::make_identical_validator_configs,
1917
},
@@ -78,24 +76,9 @@ fn test_bench_tps_local_cluster(config: Config) {
7876

7977
cluster.transfer(&cluster.funding_keypair, &faucet_pubkey, 100_000_000);
8078

81-
let ConnectionCache::Quic(cache) = &*cluster.connection_cache else {
82-
panic!("Expected a Quic ConnectionCache.");
83-
};
84-
85-
let rpc_pubsub_url = format!("ws://{}/", cluster.entry_point_info.rpc_pubsub().unwrap());
86-
let rpc_url = format!("http://{}", cluster.entry_point_info.rpc().unwrap());
87-
88-
let client = Arc::new(
89-
TpuClient::new_with_connection_cache(
90-
Arc::new(RpcClient::new(rpc_url)),
91-
rpc_pubsub_url.as_str(),
92-
TpuClientConfig::default(),
93-
cache.clone(),
94-
)
95-
.unwrap_or_else(|err| {
96-
panic!("Could not create TpuClient {err:?}");
97-
}),
98-
);
79+
let client = Arc::new(cluster.build_tpu_quic_client().unwrap_or_else(|err| {
80+
panic!("Could not create TpuClient with Quic Cache {err:?}");
81+
}));
9982

10083
let lamports_per_account = 100;
10184

client/src/tpu_client.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ pub use {
2121
solana_tpu_client::tpu_client::{TpuClientConfig, DEFAULT_FANOUT_SLOTS, MAX_FANOUT_SLOTS},
2222
};
2323

24+
pub type QuicTpuClient = TpuClient<QuicPool, QuicConnectionManager, QuicConfig>;
25+
2426
pub enum TpuClientWrapper {
2527
Quic(TpuClient<QuicPool, QuicConnectionManager, QuicConfig>),
2628
Udp(TpuClient<UdpPool, UdpConnectionManager, UdpConfig>),

dos/src/main.rs

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ fn main() {
818818
pub mod test {
819819
use {
820820
super::*,
821-
solana_client::tpu_client::TpuClient,
821+
solana_client::tpu_client::QuicTpuClient,
822822
solana_core::validator::ValidatorConfig,
823823
solana_faucet::faucet::run_local_faucet,
824824
solana_gossip::contact_info::LegacyContactInfo,
@@ -827,43 +827,16 @@ pub mod test {
827827
local_cluster::{ClusterConfig, LocalCluster},
828828
validator_configs::make_identical_validator_configs,
829829
},
830-
solana_quic_client::{QuicConfig, QuicConnectionManager, QuicPool},
831830
solana_rpc::rpc::JsonRpcConfig,
832831
solana_sdk::timing::timestamp,
833-
solana_tpu_client::tpu_client::TpuClientConfig,
834832
};
835833

836834
const TEST_SEND_BATCH_SIZE: usize = 1;
837835

838836
// thin wrapper for the run_dos function
839837
// to avoid specifying everywhere generic parameters
840838
fn run_dos_no_client(nodes: &[ContactInfo], iterations: usize, params: DosClientParameters) {
841-
run_dos::<TpuClient<QuicPool, QuicConnectionManager, QuicConfig>>(
842-
nodes, iterations, None, params,
843-
);
844-
}
845-
846-
fn build_tpu_quic_client(
847-
cluster: &LocalCluster,
848-
) -> Arc<TpuClient<QuicPool, QuicConnectionManager, QuicConfig>> {
849-
let rpc_pubsub_url = format!("ws://{}/", cluster.entry_point_info.rpc_pubsub().unwrap());
850-
let rpc_url = format!("http://{}", cluster.entry_point_info.rpc().unwrap());
851-
852-
let ConnectionCache::Quic(cache) = &*cluster.connection_cache else {
853-
panic!("Expected a Quic ConnectionCache.");
854-
};
855-
856-
Arc::new(
857-
TpuClient::new_with_connection_cache(
858-
Arc::new(RpcClient::new(rpc_url)),
859-
rpc_pubsub_url.as_str(),
860-
TpuClientConfig::default(),
861-
cache.clone(),
862-
)
863-
.unwrap_or_else(|err| {
864-
panic!("Could not create TpuClient with Quic Cache {err:?}");
865-
}),
866-
)
839+
run_dos::<QuicTpuClient>(nodes, iterations, None, params);
867840
}
868841

869842
#[test]
@@ -1003,7 +976,9 @@ pub mod test {
1003976
.unwrap();
1004977
let nodes_slice = [node];
1005978

1006-
let client = build_tpu_quic_client(&cluster);
979+
let client = Arc::new(cluster.build_tpu_quic_client().unwrap_or_else(|err| {
980+
panic!("Could not create TpuClient with Quic Cache {err:?}");
981+
}));
1007982

1008983
// creates one transaction with 8 valid signatures and sends it 10 times
1009984
run_dos(
@@ -1135,7 +1110,9 @@ pub mod test {
11351110
.unwrap();
11361111
let nodes_slice = [node];
11371112

1138-
let client = build_tpu_quic_client(&cluster);
1113+
let client = Arc::new(cluster.build_tpu_quic_client().unwrap_or_else(|err| {
1114+
panic!("Could not create TpuClient with Quic Cache {err:?}");
1115+
}));
11391116

11401117
// creates one transaction and sends it 10 times
11411118
// this is done in single thread

local-cluster/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ solana-gossip = { workspace = true }
2424
solana-ledger = { workspace = true }
2525
solana-logger = { workspace = true }
2626
solana-pubsub-client = { workspace = true }
27+
solana-quic-client = { workspace = true }
2728
solana-rpc-client = { workspace = true }
2829
solana-rpc-client-api = { workspace = true }
2930
solana-runtime = { workspace = true }

local-cluster/src/cluster.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use {
2-
solana_client::thin_client::ThinClient,
2+
solana_client::{thin_client::ThinClient, tpu_client::QuicTpuClient},
33
solana_core::validator::{Validator, ValidatorConfig},
44
solana_gossip::{cluster_info::Node, contact_info::ContactInfo},
55
solana_ledger::shred::Shred,
6-
solana_sdk::{pubkey::Pubkey, signature::Keypair},
6+
solana_sdk::{commitment_config::CommitmentConfig, pubkey::Pubkey, signature::Keypair},
77
solana_streamer::socket::SocketAddrSpace,
8-
std::{path::PathBuf, sync::Arc},
8+
std::{io::Result, path::PathBuf, sync::Arc},
99
};
1010

1111
pub struct ValidatorInfo {
@@ -38,6 +38,11 @@ impl ClusterValidatorInfo {
3838
pub trait Cluster {
3939
fn get_node_pubkeys(&self) -> Vec<Pubkey>;
4040
fn get_validator_client(&self, pubkey: &Pubkey) -> Option<ThinClient>;
41+
fn build_tpu_quic_client(&self) -> Result<QuicTpuClient>;
42+
fn build_tpu_quic_client_with_commitment(
43+
&self,
44+
commitment_config: CommitmentConfig,
45+
) -> Result<QuicTpuClient>;
4146
fn get_contact_info(&self, pubkey: &Pubkey) -> Option<&ContactInfo>;
4247
fn exit_node(&mut self, pubkey: &Pubkey) -> ClusterValidatorInfo;
4348
fn restart_node(

local-cluster/src/local_cluster.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ use {
77
itertools::izip,
88
log::*,
99
solana_accounts_db::utils::create_accounts_run_and_snapshot_dirs,
10-
solana_client::{connection_cache::ConnectionCache, thin_client::ThinClient},
10+
solana_client::{
11+
connection_cache::ConnectionCache,
12+
rpc_client::RpcClient,
13+
thin_client::ThinClient,
14+
tpu_client::{QuicTpuClient, TpuClient, TpuClientConfig},
15+
},
1116
solana_core::{
1217
consensus::tower_storage::FileTowerStorage,
1318
validator::{Validator, ValidatorConfig, ValidatorStartProgress},
@@ -802,6 +807,34 @@ impl LocalCluster {
802807
..SnapshotConfig::new_load_only()
803808
}
804809
}
810+
811+
fn build_tpu_client<F>(&self, rpc_client_builder: F) -> Result<QuicTpuClient>
812+
where
813+
F: FnOnce(String) -> Arc<RpcClient>,
814+
{
815+
let rpc_pubsub_url = format!("ws://{}/", self.entry_point_info.rpc_pubsub().unwrap());
816+
let rpc_url = format!("http://{}", self.entry_point_info.rpc().unwrap());
817+
818+
let cache = match &*self.connection_cache {
819+
ConnectionCache::Quic(cache) => cache,
820+
ConnectionCache::Udp(_) => {
821+
return Err(Error::new(
822+
ErrorKind::Other,
823+
"Expected a Quic ConnectionCache. Got UDP",
824+
))
825+
}
826+
};
827+
828+
let tpu_client = TpuClient::new_with_connection_cache(
829+
rpc_client_builder(rpc_url),
830+
rpc_pubsub_url.as_str(),
831+
TpuClientConfig::default(),
832+
cache.clone(),
833+
)
834+
.map_err(|err| Error::new(ErrorKind::Other, format!("TpuSenderError: {}", err)))?;
835+
836+
Ok(tpu_client)
837+
}
805838
}
806839

807840
impl Cluster for LocalCluster {
@@ -820,6 +853,19 @@ impl Cluster for LocalCluster {
820853
})
821854
}
822855

856+
fn build_tpu_quic_client(&self) -> Result<QuicTpuClient> {
857+
self.build_tpu_client(|rpc_url| Arc::new(RpcClient::new(rpc_url)))
858+
}
859+
860+
fn build_tpu_quic_client_with_commitment(
861+
&self,
862+
commitment_config: CommitmentConfig,
863+
) -> Result<QuicTpuClient> {
864+
self.build_tpu_client(|rpc_url| {
865+
Arc::new(RpcClient::new_with_commitment(rpc_url, commitment_config))
866+
})
867+
}
868+
823869
fn exit_node(&mut self, pubkey: &Pubkey) -> ClusterValidatorInfo {
824870
let mut node = self.validators.remove(pubkey).unwrap();
825871

0 commit comments

Comments
 (0)