2021-12-03 09:00:31 -08:00
|
|
|
use {
|
2022-01-26 16:14:49 -08:00
|
|
|
futures_util::StreamExt,
|
2021-12-03 09:00:31 -08:00
|
|
|
serde_json::{json, Value},
|
|
|
|
serial_test::serial,
|
|
|
|
solana_client::{
|
2022-01-26 16:14:49 -08:00
|
|
|
nonblocking,
|
2021-12-03 09:00:31 -08:00
|
|
|
pubsub_client::PubsubClient,
|
|
|
|
rpc_client::RpcClient,
|
2021-12-17 15:03:09 -08:00
|
|
|
rpc_config::{
|
|
|
|
RpcAccountInfoConfig, RpcBlockSubscribeConfig, RpcBlockSubscribeFilter,
|
|
|
|
RpcProgramAccountsConfig,
|
|
|
|
},
|
|
|
|
rpc_response::{RpcBlockUpdate, SlotInfo},
|
2021-12-03 09:00:31 -08:00
|
|
|
},
|
2021-12-17 15:03:09 -08:00
|
|
|
solana_ledger::{blockstore::Blockstore, get_tmp_ledger_path},
|
2021-12-03 09:00:31 -08:00
|
|
|
solana_rpc::{
|
|
|
|
optimistically_confirmed_bank_tracker::OptimisticallyConfirmedBank,
|
2021-12-17 15:03:09 -08:00
|
|
|
rpc::create_test_transactions_and_populate_blockstore,
|
2021-12-03 09:00:31 -08:00
|
|
|
rpc_pubsub_service::{PubSubConfig, PubSubService},
|
|
|
|
rpc_subscriptions::RpcSubscriptions,
|
|
|
|
},
|
|
|
|
solana_runtime::{
|
|
|
|
bank::Bank,
|
|
|
|
bank_forks::BankForks,
|
|
|
|
commitment::{BlockCommitmentCache, CommitmentSlots},
|
|
|
|
genesis_utils::{create_genesis_config, GenesisConfigInfo},
|
|
|
|
},
|
|
|
|
solana_sdk::{
|
|
|
|
clock::Slot,
|
2021-12-17 15:03:09 -08:00
|
|
|
commitment_config::{CommitmentConfig, CommitmentLevel},
|
2021-12-03 09:00:31 -08:00
|
|
|
native_token::sol_to_lamports,
|
|
|
|
pubkey::Pubkey,
|
|
|
|
rpc_port,
|
|
|
|
signature::{Keypair, Signer},
|
|
|
|
system_program, system_transaction,
|
|
|
|
},
|
|
|
|
solana_streamer::socket::SocketAddrSpace,
|
|
|
|
solana_test_validator::TestValidator,
|
2021-12-17 15:03:09 -08:00
|
|
|
solana_transaction_status::{TransactionDetails, UiTransactionEncoding},
|
2021-12-03 09:00:31 -08:00
|
|
|
std::{
|
|
|
|
collections::HashSet,
|
|
|
|
net::{IpAddr, SocketAddr},
|
|
|
|
sync::{
|
2021-12-17 15:03:09 -08:00
|
|
|
atomic::{AtomicBool, AtomicU64, Ordering},
|
2021-12-03 09:00:31 -08:00
|
|
|
Arc, RwLock,
|
|
|
|
},
|
|
|
|
thread::sleep,
|
|
|
|
time::{Duration, Instant},
|
2020-02-06 11:16:30 -08:00
|
|
|
},
|
2021-12-03 09:00:31 -08:00
|
|
|
systemstat::Ipv4Addr,
|
2020-02-06 11:16:30 -08:00
|
|
|
};
|
2019-11-06 19:08:03 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rpc_client() {
|
|
|
|
solana_logger::setup();
|
|
|
|
|
2020-12-08 23:18:27 -08:00
|
|
|
let alice = Keypair::new();
|
2021-07-23 08:25:03 -07:00
|
|
|
let test_validator =
|
|
|
|
TestValidator::with_no_fees(alice.pubkey(), None, SocketAddrSpace::Unspecified);
|
2020-11-25 17:00:47 -08:00
|
|
|
|
2020-10-19 12:12:08 -07:00
|
|
|
let bob_pubkey = solana_sdk::pubkey::new_rand();
|
2019-11-06 19:08:03 -08:00
|
|
|
|
2020-11-25 17:00:47 -08:00
|
|
|
let client = RpcClient::new(test_validator.rpc_url());
|
2019-11-06 19:08:03 -08:00
|
|
|
|
|
|
|
assert_eq!(
|
2019-11-12 21:01:04 -08:00
|
|
|
client.get_version().unwrap().solana_core,
|
2020-10-03 08:30:26 -07:00
|
|
|
solana_version::semver!()
|
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
|
|
|
|
2020-11-25 14:14:45 -08:00
|
|
|
let original_alice_balance = client.get_balance(&alice.pubkey()).unwrap();
|
2019-11-06 19:08:03 -08:00
|
|
|
|
2021-08-13 09:08:20 -07:00
|
|
|
let blockhash = client.get_latest_blockhash().unwrap();
|
2019-11-06 19:08:03 -08:00
|
|
|
|
2020-11-16 22:27:36 -08:00
|
|
|
let tx = system_transaction::transfer(&alice, &bob_pubkey, sol_to_lamports(20.0), blockhash);
|
2019-11-06 19:08:03 -08: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 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);
|
|
|
|
|
2020-11-16 22:27:36 -08:00
|
|
|
assert_eq!(
|
|
|
|
client.get_balance(&bob_pubkey).unwrap(),
|
|
|
|
sol_to_lamports(20.0)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
client.get_balance(&alice.pubkey()).unwrap(),
|
2020-11-25 14:14:45 -08:00
|
|
|
original_alice_balance - sol_to_lamports(20.0)
|
2020-11-16 22:27:36 -08:00
|
|
|
);
|
2019-11-06 19:08:03 -08:00
|
|
|
}
|
2020-02-06 11:16:30 -08:00
|
|
|
|
|
|
|
#[test]
|
2021-10-29 10:11:20 -07:00
|
|
|
#[serial]
|
|
|
|
fn test_account_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 GenesisConfigInfo {
|
|
|
|
genesis_config,
|
|
|
|
mint_keypair: alice,
|
|
|
|
..
|
|
|
|
} = create_genesis_config(10_000);
|
|
|
|
let bank = Bank::new_for_tests(&genesis_config);
|
|
|
|
let blockhash = bank.last_blockhash();
|
|
|
|
let bank_forks = Arc::new(RwLock::new(BankForks::new(bank)));
|
|
|
|
let bank0 = bank_forks.read().unwrap().get(0).unwrap().clone();
|
|
|
|
let bank1 = Bank::new_from_parent(&bank0, &Pubkey::default(), 1);
|
|
|
|
bank_forks.write().unwrap().insert(bank1);
|
|
|
|
let bob = Keypair::new();
|
2021-12-17 15:03:09 -08:00
|
|
|
let max_complete_transaction_status_slot = Arc::new(AtomicU64::default());
|
2021-10-29 10:11:20 -07:00
|
|
|
let subscriptions = Arc::new(RpcSubscriptions::new_for_tests(
|
|
|
|
&exit,
|
2021-12-17 15:03:09 -08:00
|
|
|
max_complete_transaction_status_slot,
|
2021-10-29 10:11:20 -07:00
|
|
|
bank_forks.clone(),
|
|
|
|
Arc::new(RwLock::new(BlockCommitmentCache::default())),
|
|
|
|
OptimisticallyConfirmedBank::locked_from_bank_forks_root(&bank_forks),
|
|
|
|
));
|
|
|
|
let (trigger, pubsub_service) =
|
|
|
|
PubSubService::new(PubSubConfig::default(), &subscriptions, pubsub_addr);
|
|
|
|
std::thread::sleep(Duration::from_millis(400));
|
|
|
|
let config = Some(RpcAccountInfoConfig {
|
|
|
|
commitment: Some(CommitmentConfig::finalized()),
|
|
|
|
encoding: None,
|
|
|
|
data_slice: None,
|
|
|
|
});
|
|
|
|
let (mut client, receiver) = PubsubClient::account_subscribe(
|
|
|
|
&format!("ws://0.0.0.0:{}/", pubsub_addr.port()),
|
|
|
|
&bob.pubkey(),
|
|
|
|
config,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Transfer 100 lamports from alice to bob
|
|
|
|
let tx = system_transaction::transfer(&alice, &bob.pubkey(), 100, blockhash);
|
|
|
|
bank_forks
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.get(1)
|
|
|
|
.unwrap()
|
|
|
|
.process_transaction(&tx)
|
|
|
|
.unwrap();
|
|
|
|
let commitment_slots = CommitmentSlots {
|
|
|
|
slot: 1,
|
|
|
|
..CommitmentSlots::default()
|
|
|
|
};
|
|
|
|
subscriptions.notify_subscribers(commitment_slots);
|
|
|
|
let commitment_slots = CommitmentSlots {
|
|
|
|
slot: 2,
|
|
|
|
root: 1,
|
|
|
|
highest_confirmed_slot: 1,
|
|
|
|
highest_confirmed_root: 1,
|
|
|
|
};
|
|
|
|
subscriptions.notify_subscribers(commitment_slots);
|
|
|
|
|
|
|
|
let expected = json!({
|
|
|
|
"context": { "slot": 1 },
|
|
|
|
"value": {
|
|
|
|
"owner": system_program::id().to_string(),
|
|
|
|
"lamports": 100,
|
|
|
|
"data": "",
|
|
|
|
"executable": false,
|
|
|
|
"rentEpoch": 0,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// Read notification
|
|
|
|
let mut errors: Vec<(Value, Value)> = Vec::new();
|
|
|
|
let response = receiver.recv();
|
|
|
|
match response {
|
|
|
|
Ok(response) => {
|
|
|
|
let actual = serde_json::to_value(response).unwrap();
|
|
|
|
if expected != actual {
|
|
|
|
errors.push((expected, actual));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_) => eprintln!("unexpected websocket receive timeout"),
|
|
|
|
}
|
|
|
|
|
|
|
|
exit.store(true, Ordering::Relaxed);
|
|
|
|
trigger.cancel();
|
|
|
|
client.shutdown().unwrap();
|
|
|
|
pubsub_service.close().unwrap();
|
|
|
|
assert_eq!(errors, [].to_vec());
|
|
|
|
}
|
|
|
|
|
2021-12-17 15:03:09 -08:00
|
|
|
#[test]
|
|
|
|
#[serial]
|
|
|
|
fn test_block_subscription() {
|
|
|
|
// setup BankForks
|
|
|
|
let exit = Arc::new(AtomicBool::new(false));
|
|
|
|
let GenesisConfigInfo {
|
|
|
|
genesis_config,
|
|
|
|
mint_keypair: alice,
|
|
|
|
..
|
|
|
|
} = create_genesis_config(10_000);
|
|
|
|
let bank = Bank::new_for_tests(&genesis_config);
|
|
|
|
let bank_forks = Arc::new(RwLock::new(BankForks::new(bank)));
|
|
|
|
|
|
|
|
// setup Blockstore
|
|
|
|
let ledger_path = get_tmp_ledger_path!();
|
|
|
|
let blockstore = Blockstore::open(&ledger_path).unwrap();
|
|
|
|
let blockstore = Arc::new(blockstore);
|
|
|
|
|
|
|
|
// populate ledger with test txs
|
|
|
|
let bank = bank_forks.read().unwrap().working_bank();
|
|
|
|
let keypair1 = Keypair::new();
|
|
|
|
let keypair2 = Keypair::new();
|
|
|
|
let keypair3 = Keypair::new();
|
|
|
|
let max_complete_transaction_status_slot = Arc::new(AtomicU64::new(blockstore.max_root()));
|
|
|
|
let _confirmed_block_signatures = create_test_transactions_and_populate_blockstore(
|
|
|
|
vec![&alice, &keypair1, &keypair2, &keypair3],
|
|
|
|
0,
|
|
|
|
bank,
|
|
|
|
blockstore.clone(),
|
|
|
|
max_complete_transaction_status_slot,
|
|
|
|
);
|
|
|
|
let max_complete_transaction_status_slot = Arc::new(AtomicU64::default());
|
|
|
|
// setup RpcSubscriptions && PubSubService
|
|
|
|
let subscriptions = Arc::new(RpcSubscriptions::new_for_tests_with_blockstore(
|
|
|
|
&exit,
|
|
|
|
max_complete_transaction_status_slot,
|
|
|
|
blockstore.clone(),
|
|
|
|
bank_forks.clone(),
|
|
|
|
Arc::new(RwLock::new(BlockCommitmentCache::default())),
|
|
|
|
OptimisticallyConfirmedBank::locked_from_bank_forks_root(&bank_forks),
|
|
|
|
));
|
|
|
|
let pubsub_addr = SocketAddr::new(
|
|
|
|
IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
|
|
|
|
rpc_port::DEFAULT_RPC_PUBSUB_PORT,
|
|
|
|
);
|
|
|
|
let pub_cfg = PubSubConfig {
|
|
|
|
enable_block_subscription: true,
|
|
|
|
..PubSubConfig::default()
|
|
|
|
};
|
|
|
|
let (trigger, pubsub_service) = PubSubService::new(pub_cfg, &subscriptions, pubsub_addr);
|
|
|
|
|
|
|
|
std::thread::sleep(Duration::from_millis(400));
|
|
|
|
|
|
|
|
// setup PubsubClient
|
|
|
|
let (mut client, receiver) = PubsubClient::block_subscribe(
|
|
|
|
&format!("ws://0.0.0.0:{}/", pubsub_addr.port()),
|
|
|
|
RpcBlockSubscribeFilter::All,
|
|
|
|
Some(RpcBlockSubscribeConfig {
|
|
|
|
commitment: Some(CommitmentConfig {
|
|
|
|
commitment: CommitmentLevel::Confirmed,
|
|
|
|
}),
|
|
|
|
encoding: Some(UiTransactionEncoding::Json),
|
|
|
|
transaction_details: Some(TransactionDetails::Signatures),
|
|
|
|
show_rewards: None,
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// trigger Gossip notification
|
|
|
|
let slot = bank_forks.read().unwrap().highest_slot();
|
|
|
|
subscriptions.notify_gossip_subscribers(slot);
|
|
|
|
let maybe_actual = receiver.recv_timeout(Duration::from_millis(400));
|
|
|
|
match maybe_actual {
|
|
|
|
Ok(actual) => {
|
2022-01-13 23:24:41 -08:00
|
|
|
let versioned_block = blockstore.get_complete_block(slot, false).unwrap();
|
|
|
|
let legacy_block = versioned_block.into_legacy_block().unwrap();
|
|
|
|
let block = legacy_block.clone().configure(
|
2021-12-17 15:03:09 -08:00
|
|
|
UiTransactionEncoding::Json,
|
|
|
|
TransactionDetails::Signatures,
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
let expected = RpcBlockUpdate {
|
|
|
|
slot,
|
|
|
|
block: Some(block),
|
|
|
|
err: None,
|
|
|
|
};
|
2022-01-13 23:24:41 -08:00
|
|
|
let block = legacy_block.configure(
|
2021-12-17 15:03:09 -08:00
|
|
|
UiTransactionEncoding::Json,
|
|
|
|
TransactionDetails::Signatures,
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
assert_eq!(actual.value.slot, expected.slot);
|
|
|
|
assert!(block.eq(&actual.value.block.unwrap()));
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!("unexpected websocket receive timeout");
|
|
|
|
assert_eq!(Some(e), None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
exit.store(true, Ordering::Relaxed);
|
|
|
|
trigger.cancel();
|
|
|
|
client.shutdown().unwrap();
|
|
|
|
pubsub_service.close().unwrap();
|
|
|
|
}
|
|
|
|
|
2021-10-29 10:11:20 -07:00
|
|
|
#[test]
|
|
|
|
#[serial]
|
|
|
|
fn test_program_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 GenesisConfigInfo {
|
|
|
|
genesis_config,
|
|
|
|
mint_keypair: alice,
|
|
|
|
..
|
|
|
|
} = create_genesis_config(10_000);
|
|
|
|
let bank = Bank::new_for_tests(&genesis_config);
|
|
|
|
let blockhash = bank.last_blockhash();
|
|
|
|
let bank_forks = Arc::new(RwLock::new(BankForks::new(bank)));
|
|
|
|
let bank0 = bank_forks.read().unwrap().get(0).unwrap().clone();
|
|
|
|
let bank1 = Bank::new_from_parent(&bank0, &Pubkey::default(), 1);
|
|
|
|
bank_forks.write().unwrap().insert(bank1);
|
|
|
|
let bob = Keypair::new();
|
2021-12-17 15:03:09 -08:00
|
|
|
let max_complete_transaction_status_slot = Arc::new(AtomicU64::default());
|
2021-10-29 10:11:20 -07:00
|
|
|
let subscriptions = Arc::new(RpcSubscriptions::new_for_tests(
|
|
|
|
&exit,
|
2021-12-17 15:03:09 -08:00
|
|
|
max_complete_transaction_status_slot,
|
2021-10-29 10:11:20 -07:00
|
|
|
bank_forks.clone(),
|
|
|
|
Arc::new(RwLock::new(BlockCommitmentCache::default())),
|
|
|
|
OptimisticallyConfirmedBank::locked_from_bank_forks_root(&bank_forks),
|
|
|
|
));
|
|
|
|
let (trigger, pubsub_service) =
|
|
|
|
PubSubService::new(PubSubConfig::default(), &subscriptions, pubsub_addr);
|
|
|
|
std::thread::sleep(Duration::from_millis(400));
|
|
|
|
let config = Some(RpcProgramAccountsConfig {
|
|
|
|
..RpcProgramAccountsConfig::default()
|
|
|
|
});
|
|
|
|
|
2021-10-29 15:19:10 -07:00
|
|
|
let program_id = Pubkey::new_unique();
|
2021-10-29 10:11:20 -07:00
|
|
|
let (mut client, receiver) = PubsubClient::program_subscribe(
|
|
|
|
&format!("ws://0.0.0.0:{}/", pubsub_addr.port()),
|
2021-10-29 15:19:10 -07:00
|
|
|
&program_id,
|
2021-10-29 10:11:20 -07:00
|
|
|
config,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2021-10-29 15:19:10 -07:00
|
|
|
// Create new program account at bob's address
|
|
|
|
let tx = system_transaction::create_account(&alice, &bob, blockhash, 100, 0, &program_id);
|
2021-10-29 10:11:20 -07:00
|
|
|
bank_forks
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.get(1)
|
|
|
|
.unwrap()
|
|
|
|
.process_transaction(&tx)
|
|
|
|
.unwrap();
|
|
|
|
let commitment_slots = CommitmentSlots {
|
|
|
|
slot: 1,
|
|
|
|
..CommitmentSlots::default()
|
|
|
|
};
|
|
|
|
subscriptions.notify_subscribers(commitment_slots);
|
|
|
|
let commitment_slots = CommitmentSlots {
|
|
|
|
slot: 2,
|
|
|
|
root: 1,
|
|
|
|
highest_confirmed_slot: 1,
|
|
|
|
highest_confirmed_root: 1,
|
|
|
|
};
|
|
|
|
subscriptions.notify_subscribers(commitment_slots);
|
|
|
|
|
|
|
|
// Poll notifications generated by the transfer
|
|
|
|
let mut notifications = Vec::new();
|
|
|
|
let mut pubkeys = HashSet::new();
|
|
|
|
loop {
|
|
|
|
let response = receiver.recv_timeout(Duration::from_millis(100));
|
|
|
|
match response {
|
|
|
|
Ok(response) => {
|
|
|
|
notifications.push(response.clone());
|
|
|
|
pubkeys.insert(response.value.pubkey);
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown
|
|
|
|
exit.store(true, Ordering::Relaxed);
|
|
|
|
trigger.cancel();
|
|
|
|
client.shutdown().unwrap();
|
|
|
|
pubsub_service.close().unwrap();
|
|
|
|
|
2021-10-29 15:19:10 -07:00
|
|
|
assert_eq!(notifications.len(), 1);
|
2021-10-29 10:11:20 -07:00
|
|
|
assert!(pubkeys.contains(&bob.pubkey().to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[serial]
|
|
|
|
fn test_root_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 GenesisConfigInfo { genesis_config, .. } = create_genesis_config(10_000);
|
|
|
|
let bank = Bank::new_for_tests(&genesis_config);
|
|
|
|
let bank_forks = Arc::new(RwLock::new(BankForks::new(bank)));
|
|
|
|
let bank0 = bank_forks.read().unwrap().get(0).unwrap().clone();
|
|
|
|
let bank1 = Bank::new_from_parent(&bank0, &Pubkey::default(), 1);
|
|
|
|
bank_forks.write().unwrap().insert(bank1);
|
2021-12-17 15:03:09 -08:00
|
|
|
let max_complete_transaction_status_slot = Arc::new(AtomicU64::default());
|
2021-10-29 10:11:20 -07:00
|
|
|
let subscriptions = Arc::new(RpcSubscriptions::new_for_tests(
|
|
|
|
&exit,
|
2021-12-17 15:03:09 -08:00
|
|
|
max_complete_transaction_status_slot,
|
2021-10-29 10:11:20 -07:00
|
|
|
bank_forks.clone(),
|
|
|
|
Arc::new(RwLock::new(BlockCommitmentCache::default())),
|
|
|
|
OptimisticallyConfirmedBank::locked_from_bank_forks_root(&bank_forks),
|
|
|
|
));
|
|
|
|
let (trigger, pubsub_service) =
|
|
|
|
PubSubService::new(PubSubConfig::default(), &subscriptions, pubsub_addr);
|
|
|
|
std::thread::sleep(Duration::from_millis(400));
|
|
|
|
let (mut client, receiver) =
|
|
|
|
PubsubClient::root_subscribe(&format!("ws://0.0.0.0:{}/", pubsub_addr.port())).unwrap();
|
|
|
|
|
|
|
|
let roots = vec![1, 2, 3];
|
|
|
|
subscriptions.notify_roots(roots.clone());
|
|
|
|
|
|
|
|
// Read notifications
|
|
|
|
let mut errors: Vec<(Slot, Slot)> = Vec::new();
|
|
|
|
for expected in roots {
|
|
|
|
let response = receiver.recv();
|
|
|
|
match response {
|
|
|
|
Ok(response) => {
|
|
|
|
if expected != response {
|
|
|
|
errors.push((expected, response));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_) => eprintln!("unexpected websocket receive timeout"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exit.store(true, Ordering::Relaxed);
|
|
|
|
trigger.cancel();
|
|
|
|
client.shutdown().unwrap();
|
|
|
|
pubsub_service.close().unwrap();
|
|
|
|
assert_eq!(errors, [].to_vec());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[serial]
|
2020-02-06 11:16:30 -08:00
|
|
|
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-06 23:23:06 -07:00
|
|
|
let GenesisConfigInfo { genesis_config, .. } = create_genesis_config(10_000);
|
2021-08-05 06:42:38 -07:00
|
|
|
let bank = Bank::new_for_tests(&genesis_config);
|
2020-06-12 10:04:17 -07:00
|
|
|
let bank_forks = Arc::new(RwLock::new(BankForks::new(bank)));
|
2020-09-28 19:43:05 -07:00
|
|
|
let optimistically_confirmed_bank =
|
|
|
|
OptimisticallyConfirmedBank::locked_from_bank_forks_root(&bank_forks);
|
2021-12-17 15:03:09 -08:00
|
|
|
let max_complete_transaction_status_slot = Arc::new(AtomicU64::default());
|
2021-09-17 12:40:14 -07:00
|
|
|
let subscriptions = Arc::new(RpcSubscriptions::new_for_tests(
|
2020-03-30 16:53:25 -07:00
|
|
|
&exit,
|
2021-12-17 15:03:09 -08:00
|
|
|
max_complete_transaction_status_slot,
|
2020-05-06 23:23:06 -07:00
|
|
|
bank_forks,
|
2020-06-25 21:06:58 -07:00
|
|
|
Arc::new(RwLock::new(BlockCommitmentCache::default())),
|
2020-09-28 19:43:05 -07:00
|
|
|
optimistically_confirmed_bank,
|
2020-03-30 16:53:25 -07:00
|
|
|
));
|
2021-09-17 12:40:14 -07:00
|
|
|
let (trigger, pubsub_service) =
|
|
|
|
PubSubService::new(PubSubConfig::default(), &subscriptions, pubsub_addr);
|
2020-02-06 11:16:30 -08: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 17:14:45 -07:00
|
|
|
let mut errors: Vec<(SlotInfo, SlotInfo)> = Vec::new();
|
2020-02-06 11:16:30 -08: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 17:14:45 -07:00
|
|
|
let expected = SlotInfo {
|
2020-02-06 11:16:30 -08: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);
|
2021-09-17 12:40:14 -07:00
|
|
|
trigger.cancel();
|
2020-02-06 11:16:30 -08:00
|
|
|
client.shutdown().unwrap();
|
|
|
|
pubsub_service.close().unwrap();
|
|
|
|
|
|
|
|
assert_eq!(errors, [].to_vec());
|
|
|
|
}
|
2021-12-26 10:04:56 -08:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[serial]
|
|
|
|
async fn test_slot_subscription_async() {
|
|
|
|
let sync_service = Arc::new(AtomicU64::new(0));
|
|
|
|
let sync_client = Arc::clone(&sync_service);
|
|
|
|
fn wait_until(atomic: &Arc<AtomicU64>, value: u64) {
|
|
|
|
while atomic.load(Ordering::Relaxed) != value {
|
|
|
|
sleep(Duration::from_millis(1))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let pubsub_addr = SocketAddr::new(
|
|
|
|
IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
|
|
|
|
rpc_port::DEFAULT_RPC_PUBSUB_PORT,
|
|
|
|
);
|
|
|
|
|
|
|
|
tokio::task::spawn_blocking(move || {
|
|
|
|
let exit = Arc::new(AtomicBool::new(false));
|
|
|
|
let GenesisConfigInfo { genesis_config, .. } = create_genesis_config(10_000);
|
|
|
|
let bank = Bank::new_for_tests(&genesis_config);
|
|
|
|
let bank_forks = Arc::new(RwLock::new(BankForks::new(bank)));
|
|
|
|
let optimistically_confirmed_bank =
|
|
|
|
OptimisticallyConfirmedBank::locked_from_bank_forks_root(&bank_forks);
|
|
|
|
let max_complete_transaction_status_slot = Arc::new(AtomicU64::default());
|
|
|
|
let subscriptions = Arc::new(RpcSubscriptions::new_for_tests(
|
|
|
|
&exit,
|
|
|
|
max_complete_transaction_status_slot,
|
|
|
|
bank_forks,
|
|
|
|
Arc::new(RwLock::new(BlockCommitmentCache::default())),
|
|
|
|
optimistically_confirmed_bank,
|
|
|
|
));
|
|
|
|
let (trigger, pubsub_service) =
|
|
|
|
PubSubService::new(PubSubConfig::default(), &subscriptions, pubsub_addr);
|
|
|
|
sleep(Duration::from_millis(100));
|
|
|
|
sync_service.store(1, Ordering::Relaxed);
|
|
|
|
|
|
|
|
wait_until(&sync_service, 2);
|
|
|
|
subscriptions.notify_slot(1, 0, 0);
|
|
|
|
sync_service.store(3, Ordering::Relaxed);
|
|
|
|
|
|
|
|
wait_until(&sync_service, 4);
|
|
|
|
subscriptions.notify_slot(2, 1, 1);
|
|
|
|
sync_service.store(5, Ordering::Relaxed);
|
|
|
|
|
|
|
|
wait_until(&sync_service, 6);
|
|
|
|
exit.store(true, Ordering::Relaxed);
|
|
|
|
trigger.cancel();
|
|
|
|
pubsub_service.close().unwrap();
|
|
|
|
});
|
|
|
|
|
|
|
|
wait_until(&sync_client, 1);
|
|
|
|
let url = format!("ws://0.0.0.0:{}/", pubsub_addr.port());
|
2022-01-26 16:14:49 -08:00
|
|
|
let pubsub_client = nonblocking::pubsub_client::PubsubClient::new(&url)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2021-12-26 10:04:56 -08:00
|
|
|
let (mut notifications, unsubscribe) = pubsub_client.slot_subscribe().await.unwrap();
|
|
|
|
sync_client.store(2, Ordering::Relaxed);
|
|
|
|
|
|
|
|
wait_until(&sync_client, 3);
|
|
|
|
assert_eq!(
|
|
|
|
tokio::time::timeout(Duration::from_millis(25), notifications.next()).await,
|
|
|
|
Ok(Some(SlotInfo {
|
|
|
|
slot: 1,
|
|
|
|
parent: 0,
|
|
|
|
root: 0,
|
|
|
|
}))
|
|
|
|
);
|
|
|
|
sync_client.store(4, Ordering::Relaxed);
|
|
|
|
|
|
|
|
wait_until(&sync_client, 5);
|
|
|
|
assert_eq!(
|
|
|
|
tokio::time::timeout(Duration::from_millis(25), notifications.next()).await,
|
|
|
|
Ok(Some(SlotInfo {
|
|
|
|
slot: 2,
|
|
|
|
parent: 1,
|
|
|
|
root: 1,
|
|
|
|
}))
|
|
|
|
);
|
|
|
|
sync_client.store(6, Ordering::Relaxed);
|
|
|
|
|
|
|
|
unsubscribe().await;
|
|
|
|
}
|