v0.2.7-pre.011
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// file: crates/ksp-onchain-transport-lib/unit_tests/ws_cluster.rs
|
||||
// version: 1
|
||||
// version: 2
|
||||
|
||||
use futures_util::SinkExt; // rust-rules: trait-import
|
||||
use futures_util::StreamExt; // rust-rules: trait-import
|
||||
@@ -98,3 +98,136 @@ async fn stable_slot_and_root_wrappers_use_no_params_decode_exact_notifications_
|
||||
session.close().await.expect("session close must complete");
|
||||
server.await.expect("local server task must complete");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slots_update_decoder_preserves_all_known_variants_and_unknown_bounded_fallback() {
|
||||
let cases = [
|
||||
(serde_json::json!({"slot":1,"timestamp":10,"type":"firstShredReceived"}), "firstShredReceived"),
|
||||
(serde_json::json!({"slot":2,"timestamp":20,"type":"completed"}), "completed"),
|
||||
(serde_json::json!({"slot":3,"timestamp":30,"type":"createdBank","parent":2}), "createdBank"),
|
||||
(
|
||||
serde_json::json!({
|
||||
"slot":4,
|
||||
"timestamp":40,
|
||||
"type":"frozen",
|
||||
"stats":{"maxTransactionsPerEntry":64,"numFailedTransactions":1,"numSuccessfulTransactions":9,"numTransactionEntries":3}
|
||||
}),
|
||||
"frozen",
|
||||
),
|
||||
(serde_json::json!({"slot":5,"timestamp":50,"type":"dead","err":"fixture dead"}), "dead"),
|
||||
(serde_json::json!({"slot":6,"timestamp":60,"type":"optimisticConfirmation"}), "optimisticConfirmation"),
|
||||
(serde_json::json!({"slot":7,"timestamp":70,"type":"root"}), "root"),
|
||||
];
|
||||
for (wire, expected_type) in cases {
|
||||
let update = super::decode_slots_update_notification("slotsUpdatesSubscribe", wire).expect("known slot update must decode");
|
||||
assert_eq!(update.update_type(), expected_type);
|
||||
assert!(update.slot().is_some());
|
||||
assert!(update.timestamp().is_some());
|
||||
assert!(update.unknown_raw().is_none());
|
||||
}
|
||||
let frozen = super::decode_slots_update_notification(
|
||||
"slotsUpdatesSubscribe",
|
||||
serde_json::json!({
|
||||
"slot":4,
|
||||
"timestamp":40,
|
||||
"type":"frozen",
|
||||
"stats":{"maxTransactionsPerEntry":64,"numFailedTransactions":1,"numSuccessfulTransactions":9,"numTransactionEntries":3}
|
||||
}),
|
||||
)
|
||||
.expect("frozen update must decode");
|
||||
match frozen {
|
||||
crate::SolanaSlotUpdate::Frozen { stats, .. } => {
|
||||
assert_eq!(stats.max_transactions_per_entry(), 64);
|
||||
assert_eq!(stats.num_failed_transactions(), 1);
|
||||
assert_eq!(stats.num_successful_transactions(), 9);
|
||||
assert_eq!(stats.num_transaction_entries(), 3);
|
||||
},
|
||||
_ => panic!("fixture must decode as frozen"),
|
||||
}
|
||||
let unknown_wire = serde_json::json!({"slot":8,"timestamp":80,"type":"futureBankState","futureField":{"x":1}});
|
||||
let unknown = super::decode_slots_update_notification("slotsUpdatesSubscribe", unknown_wire.clone()).expect("unknown update must remain consumable");
|
||||
assert_eq!(unknown.update_type(), "futureBankState");
|
||||
assert_eq!(unknown.slot(), std::option::Option::Some(8));
|
||||
assert_eq!(unknown.timestamp(), std::option::Option::Some(80));
|
||||
assert_eq!(unknown.unknown_raw(), std::option::Option::Some(&unknown_wire));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slots_update_known_variants_require_their_variant_specific_fields() {
|
||||
assert!(super::decode_slots_update_notification("slotsUpdatesSubscribe", serde_json::json!({"slot":3,"timestamp":30,"type":"createdBank"})).is_err());
|
||||
assert!(super::decode_slots_update_notification("slotsUpdatesSubscribe", serde_json::json!({"slot":4,"timestamp":40,"type":"frozen"})).is_err());
|
||||
assert!(super::decode_slots_update_notification("slotsUpdatesSubscribe", serde_json::json!({"slot":5,"timestamp":50,"type":"dead"})).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vote_notification_decoder_preserves_timestamp_omitted_null_and_value() {
|
||||
let pubkey = "11111111111111111111111111111111";
|
||||
for (wire, expected_timestamp) in [
|
||||
(serde_json::json!({"votePubkey":pubkey,"slots":[1,2],"hash":"hash-a","signature":"sig-a"}), std::option::Option::None),
|
||||
(serde_json::json!({"votePubkey":pubkey,"slots":[1,2],"hash":"hash-b","timestamp":null,"signature":"sig-b"}), std::option::Option::None),
|
||||
(serde_json::json!({"votePubkey":pubkey,"slots":[1,2],"hash":"hash-c","timestamp":123,"signature":"sig-c"}), std::option::Option::Some(123)),
|
||||
] {
|
||||
let vote = super::decode_vote_notification("voteSubscribe", wire).expect("vote notification must decode");
|
||||
assert_eq!(vote.vote_pubkey().to_string(), pubkey);
|
||||
assert_eq!(vote.slots(), &[1, 2]);
|
||||
assert_eq!(vote.timestamp(), expected_timestamp);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn unstable_slots_updates_and_vote_wrappers_use_no_params_and_handle_unsubscribe() {
|
||||
let (listener, url) = bind_local_listener().await;
|
||||
let server = tokio::spawn(async move {
|
||||
let (stream, _) = listener.accept().await.expect("local server must accept client");
|
||||
let mut websocket = tokio_tungstenite::accept_async(stream).await.expect("local WebSocket handshake must succeed");
|
||||
let slots_subscribe = read_request(&mut websocket).await;
|
||||
assert_eq!(slots_subscribe["method"], serde_json::json!("slotsUpdatesSubscribe"));
|
||||
assert_eq!(slots_subscribe["params"], serde_json::json!([]));
|
||||
send_result(&mut websocket, &slots_subscribe, serde_json::json!(401)).await;
|
||||
send_notification(
|
||||
&mut websocket,
|
||||
"slotsUpdatesNotification",
|
||||
401,
|
||||
serde_json::json!({"slot":76,"timestamp":1625081266243_i64,"type":"optimisticConfirmation"}),
|
||||
)
|
||||
.await;
|
||||
let vote_subscribe = read_request(&mut websocket).await;
|
||||
assert_eq!(vote_subscribe["method"], serde_json::json!("voteSubscribe"));
|
||||
assert_eq!(vote_subscribe["params"], serde_json::json!([]));
|
||||
send_result(&mut websocket, &vote_subscribe, serde_json::json!(402)).await;
|
||||
send_notification(
|
||||
&mut websocket,
|
||||
"voteNotification",
|
||||
402,
|
||||
serde_json::json!({
|
||||
"votePubkey":"11111111111111111111111111111111",
|
||||
"slots":[75,76],
|
||||
"hash":"fixture-hash",
|
||||
"timestamp":null,
|
||||
"signature":"fixture-signature"
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
let slots_unsubscribe = read_request(&mut websocket).await;
|
||||
assert_eq!(slots_unsubscribe["method"], serde_json::json!("slotsUpdatesUnsubscribe"));
|
||||
assert_eq!(slots_unsubscribe["params"], serde_json::json!([401]));
|
||||
send_result(&mut websocket, &slots_unsubscribe, serde_json::json!(true)).await;
|
||||
let vote_unsubscribe = read_request(&mut websocket).await;
|
||||
assert_eq!(vote_unsubscribe["method"], serde_json::json!("voteUnsubscribe"));
|
||||
assert_eq!(vote_unsubscribe["params"], serde_json::json!([402]));
|
||||
send_result(&mut websocket, &vote_unsubscribe, serde_json::json!(true)).await;
|
||||
wait_for_close_frame(&mut websocket).await;
|
||||
});
|
||||
let session = crate::WsSession::connect(local_endpoint(url.as_str())).await.expect("client handshake must succeed");
|
||||
let mut slots_subscription = session.slots_updates_subscribe().await.expect("slotsUpdatesSubscribe must register");
|
||||
let update = slots_subscription.recv().await.expect("slots update must arrive").expect("slots update must decode");
|
||||
assert_eq!(update.update_type(), "optimisticConfirmation");
|
||||
let mut vote_subscription = session.vote_subscribe().await.expect("voteSubscribe must register");
|
||||
let vote = vote_subscription.recv().await.expect("vote notification must arrive").expect("vote notification must decode");
|
||||
assert_eq!(vote.slots(), &[75, 76]);
|
||||
assert!(vote.timestamp().is_none());
|
||||
assert!(slots_subscription.unsubscribe().await.expect("slots update unsubscribe must complete"));
|
||||
assert!(vote_subscription.unsubscribe().await.expect("vote unsubscribe must complete"));
|
||||
session.close().await.expect("session close must complete");
|
||||
server.await.expect("local server task must complete");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user