| 
									
										
										
										
											2020-09-03 18:14:45 -06:00
										 |  |  | use solana_client::{pubsub_client::PubsubClient, rpc_client::RpcClient, rpc_response::SlotInfo};
 | 
					
						
							| 
									
										
										
										
											2020-02-06 14:16:30 -05:00
										 |  |  | use solana_core::{
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:43:05 -06:00
										 |  |  |     optimistically_confirmed_bank_tracker::OptimisticallyConfirmedBank,
 | 
					
						
							| 
									
										
										
										
											2020-10-01 12:36:58 -07:00
										 |  |  |     rpc_pubsub_service::{PubSubConfig, PubSubService},
 | 
					
						
							|  |  |  |     rpc_subscriptions::RpcSubscriptions,
 | 
					
						
							| 
									
										
										
										
											2020-09-18 22:21:44 -07:00
										 |  |  |     test_validator::TestValidator,
 | 
					
						
							| 
									
										
										
										
											2020-02-06 14:16:30 -05:00
										 |  |  | };
 | 
					
						
							| 
									
										
										
										
											2020-06-25 22:06:58 -06:00
										 |  |  | use solana_runtime::{
 | 
					
						
							|  |  |  |     bank::Bank,
 | 
					
						
							|  |  |  |     bank_forks::BankForks,
 | 
					
						
							|  |  |  |     commitment::BlockCommitmentCache,
 | 
					
						
							| 
									
										
										
										
											2020-05-07 00:23:06 -06:00
										 |  |  |     genesis_utils::{create_genesis_config, GenesisConfigInfo},
 | 
					
						
							|  |  |  | };
 | 
					
						
							| 
									
										
										
										
											2020-02-06 14:16:30 -05:00
										 |  |  | use solana_sdk::{
 | 
					
						
							| 
									
										
										
										
											2020-11-17 01:08:58 -07:00
										 |  |  |     commitment_config::CommitmentConfig, native_token::sol_to_lamports, rpc_port,
 | 
					
						
							|  |  |  |     signature::Signer, system_transaction,
 | 
					
						
							| 
									
										
										
										
											2020-02-06 14:16:30 -05:00
										 |  |  | };
 | 
					
						
							|  |  |  | use std::{
 | 
					
						
							|  |  |  |     fs::remove_dir_all,
 | 
					
						
							|  |  |  |     net::{IpAddr, SocketAddr},
 | 
					
						
							|  |  |  |     sync::{
 | 
					
						
							|  |  |  |         atomic::{AtomicBool, Ordering},
 | 
					
						
							| 
									
										
										
										
											2020-03-30 17:53:25 -06:00
										 |  |  |         Arc, RwLock,
 | 
					
						
							| 
									
										
										
										
											2020-02-06 14:16:30 -05:00
										 |  |  |     },
 | 
					
						
							|  |  |  |     thread::sleep,
 | 
					
						
							|  |  |  |     time::{Duration, Instant},
 | 
					
						
							|  |  |  | };
 | 
					
						
							|  |  |  | use systemstat::Ipv4Addr;
 | 
					
						
							| 
									
										
										
										
											2019-11-06 22:08:03 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | #[test]
 | 
					
						
							|  |  |  | fn test_rpc_client() {
 | 
					
						
							|  |  |  |     solana_logger::setup();
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-26 12:23:54 +08:00
										 |  |  |     let TestValidator {
 | 
					
						
							|  |  |  |         server,
 | 
					
						
							|  |  |  |         leader_data,
 | 
					
						
							|  |  |  |         alice,
 | 
					
						
							|  |  |  |         ledger_path,
 | 
					
						
							|  |  |  |         ..
 | 
					
						
							|  |  |  |     } = TestValidator::run();
 | 
					
						
							| 
									
										
										
										
											2020-10-22 05:08:01 +00:00
										 |  |  |     let bob_pubkey = solana_sdk::pubkey::new_rand();
 | 
					
						
							| 
									
										
										
										
											2019-11-06 22:08:03 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     let client = RpcClient::new_socket(leader_data.rpc);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert_eq!(
 | 
					
						
							| 
									
										
										
										
											2019-11-12 22:01:04 -07:00
										 |  |  |         client.get_version().unwrap().solana_core,
 | 
					
						
							| 
									
										
										
										
											2020-10-04 00:30:26 +09:00
										 |  |  |         solana_version::semver!()
 | 
					
						
							| 
									
										
										
										
											2019-11-06 22:08:03 -05:00
										 |  |  |     );
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-14 11:41:26 -05:00
										 |  |  |     assert!(client.get_account(&bob_pubkey).is_err());
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-06 22:08:03 -05:00
										 |  |  |     assert_eq!(client.get_balance(&bob_pubkey).unwrap(), 0);
 | 
					
						
							| 
									
										
										
										
											2019-11-14 10:56:49 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 01:08:58 -07:00
										 |  |  |     assert_eq!(
 | 
					
						
							|  |  |  |         client.get_balance(&alice.pubkey()).unwrap(),
 | 
					
						
							|  |  |  |         sol_to_lamports(1_000_000.0)
 | 
					
						
							|  |  |  |     );
 | 
					
						
							| 
									
										
										
										
											2019-11-06 22:08:03 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     let (blockhash, _fee_calculator) = client.get_recent_blockhash().unwrap();
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 01:08:58 -07:00
										 |  |  |     let tx = system_transaction::transfer(&alice, &bob_pubkey, sol_to_lamports(20.0), blockhash);
 | 
					
						
							| 
									
										
										
										
											2019-11-06 22:08:03 -05:00
										 |  |  |     let signature = client.send_transaction(&tx).unwrap();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let mut confirmed_tx = false;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let now = Instant::now();
 | 
					
						
							|  |  |  |     while now.elapsed().as_secs() <= 20 {
 | 
					
						
							| 
									
										
										
										
											2019-11-12 14:49:41 -05:00
										 |  |  |         let response = client
 | 
					
						
							| 
									
										
										
										
											2020-03-27 15:46:00 -07:00
										 |  |  |             .confirm_transaction_with_commitment(&signature, CommitmentConfig::default())
 | 
					
						
							| 
									
										
										
										
											2019-11-12 14:49:41 -05:00
										 |  |  |             .unwrap();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if response.value {
 | 
					
						
							|  |  |  |             confirmed_tx = true;
 | 
					
						
							|  |  |  |             break;
 | 
					
						
							| 
									
										
										
										
											2019-11-06 22:08:03 -05:00
										 |  |  |         }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         sleep(Duration::from_millis(500));
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert!(confirmed_tx);
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 01:08:58 -07:00
										 |  |  |     assert_eq!(
 | 
					
						
							|  |  |  |         client.get_balance(&bob_pubkey).unwrap(),
 | 
					
						
							|  |  |  |         sol_to_lamports(20.0)
 | 
					
						
							|  |  |  |     );
 | 
					
						
							|  |  |  |     assert_eq!(
 | 
					
						
							|  |  |  |         client.get_balance(&alice.pubkey()).unwrap(),
 | 
					
						
							|  |  |  |         sol_to_lamports(999_980.0)
 | 
					
						
							|  |  |  |     );
 | 
					
						
							| 
									
										
										
										
											2019-11-06 22:08:03 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     server.close().unwrap();
 | 
					
						
							|  |  |  |     remove_dir_all(ledger_path).unwrap();
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2020-02-06 14:16:30 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | #[test]
 | 
					
						
							|  |  |  | fn test_slot_subscription() {
 | 
					
						
							|  |  |  |     let pubsub_addr = SocketAddr::new(
 | 
					
						
							|  |  |  |         IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
 | 
					
						
							|  |  |  |         rpc_port::DEFAULT_RPC_PUBSUB_PORT,
 | 
					
						
							|  |  |  |     );
 | 
					
						
							|  |  |  |     let exit = Arc::new(AtomicBool::new(false));
 | 
					
						
							| 
									
										
										
										
											2020-05-07 00:23:06 -06:00
										 |  |  |     let GenesisConfigInfo { genesis_config, .. } = create_genesis_config(10_000);
 | 
					
						
							|  |  |  |     let bank = Bank::new(&genesis_config);
 | 
					
						
							| 
									
										
										
										
											2020-06-12 11:04:17 -06:00
										 |  |  |     let bank_forks = Arc::new(RwLock::new(BankForks::new(bank)));
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:43:05 -06:00
										 |  |  |     let optimistically_confirmed_bank =
 | 
					
						
							|  |  |  |         OptimisticallyConfirmedBank::locked_from_bank_forks_root(&bank_forks);
 | 
					
						
							| 
									
										
										
										
											2020-03-30 17:53:25 -06:00
										 |  |  |     let subscriptions = Arc::new(RpcSubscriptions::new(
 | 
					
						
							|  |  |  |         &exit,
 | 
					
						
							| 
									
										
										
										
											2020-05-07 00:23:06 -06:00
										 |  |  |         bank_forks,
 | 
					
						
							| 
									
										
										
										
											2020-06-25 22:06:58 -06:00
										 |  |  |         Arc::new(RwLock::new(BlockCommitmentCache::default())),
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:43:05 -06:00
										 |  |  |         optimistically_confirmed_bank,
 | 
					
						
							| 
									
										
										
										
											2020-03-30 17:53:25 -06:00
										 |  |  |     ));
 | 
					
						
							| 
									
										
										
										
											2020-10-01 12:36:58 -07:00
										 |  |  |     let pubsub_service =
 | 
					
						
							|  |  |  |         PubSubService::new(PubSubConfig::default(), &subscriptions, pubsub_addr, &exit);
 | 
					
						
							| 
									
										
										
										
											2020-02-06 14:16:30 -05:00
										 |  |  |     std::thread::sleep(Duration::from_millis(400));
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let (mut client, receiver) =
 | 
					
						
							|  |  |  |         PubsubClient::slot_subscribe(&format!("ws://0.0.0.0:{}/", pubsub_addr.port())).unwrap();
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-03 18:14:45 -06:00
										 |  |  |     let mut errors: Vec<(SlotInfo, SlotInfo)> = Vec::new();
 | 
					
						
							| 
									
										
										
										
											2020-02-06 14:16:30 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for i in 0..3 {
 | 
					
						
							|  |  |  |         subscriptions.notify_slot(i + 1, i, i);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         let maybe_actual = receiver.recv_timeout(Duration::from_millis(400));
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         match maybe_actual {
 | 
					
						
							|  |  |  |             Ok(actual) => {
 | 
					
						
							| 
									
										
										
										
											2020-09-03 18:14:45 -06:00
										 |  |  |                 let expected = SlotInfo {
 | 
					
						
							| 
									
										
										
										
											2020-02-06 14:16:30 -05:00
										 |  |  |                     slot: i + 1,
 | 
					
						
							|  |  |  |                     parent: i,
 | 
					
						
							|  |  |  |                     root: i,
 | 
					
						
							|  |  |  |                 };
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if actual != expected {
 | 
					
						
							|  |  |  |                     errors.push((actual, expected));
 | 
					
						
							|  |  |  |                 }
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |             Err(_err) => {
 | 
					
						
							|  |  |  |                 eprintln!("unexpected websocket receive timeout");
 | 
					
						
							|  |  |  |                 break;
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     exit.store(true, Ordering::Relaxed);
 | 
					
						
							|  |  |  |     client.shutdown().unwrap();
 | 
					
						
							|  |  |  |     pubsub_service.close().unwrap();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert_eq!(errors, [].to_vec());
 | 
					
						
							|  |  |  | }
 |