Add more slot update notifications (#15734)

* Add more slot update notifications

* fix merge

* Address feedback and add integration test

* switch to datapoint

* remove unused shred method

* fix clippy

* new thread for rpc completed slots

* remove extra constant

* fixes

* rely on channel closing

* fix check
This commit is contained in:
Justin Starry
2021-03-12 21:44:06 +08:00
committed by GitHub
parent 28c27893b9
commit 918d04e3f0
15 changed files with 340 additions and 98 deletions

View File

@ -7,12 +7,13 @@ use serde_json::{json, Value};
use solana_account_decoder::UiAccount;
use solana_client::{
rpc_client::RpcClient,
rpc_response::{Response, RpcSignatureResult},
rpc_response::{Response, RpcSignatureResult, SlotUpdate},
};
use solana_core::{rpc_pubsub::gen_client::Client as PubsubClient, test_validator::TestValidator};
use solana_sdk::{
commitment_config::CommitmentConfig,
hash::Hash,
pubkey::Pubkey,
signature::{Keypair, Signer},
system_transaction,
transaction::Transaction,
@ -20,7 +21,7 @@ use solana_sdk::{
use std::{
collections::HashSet,
net::UdpSocket,
sync::mpsc::channel,
sync::{mpsc::channel, Arc},
thread::sleep,
time::{Duration, Instant},
};
@ -140,6 +141,70 @@ fn test_rpc_invalid_requests() {
assert!(the_value.is_null());
}
#[test]
fn test_rpc_slot_updates() {
solana_logger::setup();
let test_validator = TestValidator::with_no_fees(Pubkey::new_unique());
// Create the pub sub runtime
let rt = Runtime::new().unwrap();
let rpc_pubsub_url = test_validator.rpc_pubsub_url();
let (update_sender, update_receiver) = channel::<Arc<SlotUpdate>>();
// Subscribe to slot updates
rt.spawn(async move {
let connect = ws::try_connect::<PubsubClient>(&rpc_pubsub_url).unwrap();
let client = connect.await.unwrap();
tokio_02::spawn(async move {
let mut update_sub = client.slots_updates_subscribe().unwrap();
loop {
let response = update_sub.next().await.unwrap();
update_sender.send(response.unwrap()).unwrap();
}
});
});
let first_update = update_receiver
.recv_timeout(Duration::from_secs(2))
.unwrap();
// Verify that updates are received in order for an upcoming slot
let verify_slot = first_update.slot() + 2;
let mut expected_update_index = 0;
let expected_updates = vec![
"CreatedBank",
"Completed",
"Frozen",
"OptimisticConfirmation",
"Root",
];
let test_start = Instant::now();
loop {
assert!(test_start.elapsed() < Duration::from_secs(30));
let update = update_receiver
.recv_timeout(Duration::from_secs(2))
.unwrap();
if update.slot() == verify_slot {
let update_name = match *update {
SlotUpdate::CreatedBank { .. } => "CreatedBank",
SlotUpdate::Completed { .. } => "Completed",
SlotUpdate::Frozen { .. } => "Frozen",
SlotUpdate::OptimisticConfirmation { .. } => "OptimisticConfirmation",
SlotUpdate::Root { .. } => "Root",
_ => continue,
};
assert_eq!(update_name, expected_updates[expected_update_index]);
expected_update_index += 1;
if expected_update_index == expected_updates.len() {
break;
}
}
}
}
#[test]
fn test_rpc_subscriptions() {
solana_logger::setup();