2020-02-06 11:16:30 -08:00
|
|
|
use solana_client::{
|
|
|
|
pubsub_client::{PubsubClient, SlotInfoMessage},
|
|
|
|
rpc_client::RpcClient,
|
|
|
|
};
|
|
|
|
use solana_core::{
|
|
|
|
rpc_pubsub_service::PubSubService, rpc_subscriptions::RpcSubscriptions,
|
2020-02-25 20:23:54 -08:00
|
|
|
validator::TestValidator,
|
2020-02-06 11:16:30 -08:00
|
|
|
};
|
|
|
|
use solana_sdk::{
|
2020-02-20 13:28:55 -08:00
|
|
|
commitment_config::CommitmentConfig, pubkey::Pubkey, rpc_port, signature::Signer,
|
2020-02-06 11:16:30 -08:00
|
|
|
system_transaction,
|
|
|
|
};
|
|
|
|
use std::{
|
|
|
|
fs::remove_dir_all,
|
|
|
|
net::{IpAddr, SocketAddr},
|
|
|
|
sync::{
|
|
|
|
atomic::{AtomicBool, Ordering},
|
|
|
|
Arc,
|
|
|
|
},
|
|
|
|
thread::sleep,
|
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
|
|
|
use systemstat::Ipv4Addr;
|
2019-11-06 19:08:03 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rpc_client() {
|
|
|
|
solana_logger::setup();
|
|
|
|
|
2020-02-25 20:23:54 -08:00
|
|
|
let TestValidator {
|
|
|
|
server,
|
|
|
|
leader_data,
|
|
|
|
alice,
|
|
|
|
ledger_path,
|
|
|
|
..
|
|
|
|
} = TestValidator::run();
|
2019-11-06 19:08:03 -08:00
|
|
|
let bob_pubkey = Pubkey::new_rand();
|
|
|
|
|
|
|
|
let client = RpcClient::new_socket(leader_data.rpc);
|
|
|
|
|
|
|
|
assert_eq!(
|
2019-11-12 21:01:04 -08:00
|
|
|
client.get_version().unwrap().solana_core,
|
2019-11-13 20:10:38 -08:00
|
|
|
solana_clap_utils::version!()
|
2019-11-06 19:08:03 -08:00
|
|
|
);
|
|
|
|
|
2019-11-14 08:41:26 -08:00
|
|
|
assert!(client.get_account(&bob_pubkey).is_err());
|
|
|
|
|
2019-11-06 19:08:03 -08:00
|
|
|
assert_eq!(client.get_balance(&bob_pubkey).unwrap(), 0);
|
2019-11-13 21:26:49 -08:00
|
|
|
|
|
|
|
assert_eq!(client.get_balance(&alice.pubkey()).unwrap(), 1_000_000);
|
2019-11-06 19:08:03 -08:00
|
|
|
|
|
|
|
let (blockhash, _fee_calculator) = client.get_recent_blockhash().unwrap();
|
|
|
|
|
|
|
|
let tx = system_transaction::transfer(&alice, &bob_pubkey, 20, blockhash);
|
|
|
|
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 11:49:41 -08:00
|
|
|
let response = client
|
2020-03-27 15:46:00 -07:00
|
|
|
.confirm_transaction_with_commitment(&signature, CommitmentConfig::default())
|
2019-11-12 11:49:41 -08:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
if response.value {
|
|
|
|
confirmed_tx = true;
|
|
|
|
break;
|
2019-11-06 19:08:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
sleep(Duration::from_millis(500));
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(confirmed_tx);
|
|
|
|
|
|
|
|
assert_eq!(client.get_balance(&bob_pubkey).unwrap(), 20);
|
2019-11-13 21:26:49 -08:00
|
|
|
assert_eq!(client.get_balance(&alice.pubkey()).unwrap(), 999980);
|
2019-11-06 19:08:03 -08:00
|
|
|
|
|
|
|
server.close().unwrap();
|
|
|
|
remove_dir_all(ledger_path).unwrap();
|
|
|
|
}
|
2020-02-06 11:16:30 -08: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));
|
|
|
|
let subscriptions = Arc::new(RpcSubscriptions::new(&exit));
|
|
|
|
let pubsub_service = PubSubService::new(&subscriptions, pubsub_addr, &exit);
|
|
|
|
std::thread::sleep(Duration::from_millis(400));
|
|
|
|
|
|
|
|
let (mut client, receiver) =
|
|
|
|
PubsubClient::slot_subscribe(&format!("ws://0.0.0.0:{}/", pubsub_addr.port())).unwrap();
|
|
|
|
|
|
|
|
let mut errors: Vec<(SlotInfoMessage, SlotInfoMessage)> = Vec::new();
|
|
|
|
|
|
|
|
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) => {
|
|
|
|
let expected = SlotInfoMessage {
|
|
|
|
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());
|
|
|
|
}
|