v0.2.2-pre.005

This commit is contained in:
2026-08-18 08:17:10 +02:00
parent 1d1bc6a4d6
commit 83cb861e54
16 changed files with 662 additions and 14 deletions

View File

@@ -1,5 +1,5 @@
// file: crates/ksp-onchain-transport-lib/unit_tests/rpc_cluster.rs
// version: 2
// version: 3
#[test]
fn cluster_node_fixture_preserves_v4_client_id_and_optional_fields() {
@@ -111,3 +111,213 @@ fn staged_vote_status_and_config_helpers_preserve_wire_shapes() {
assert_eq!(status.current().len(), 1);
assert!(status.delinquent().is_empty());
}
fn pool_for_url(url: &str) -> crate::HttpTransportPool {
let role = crate::HttpEndpointRoleSettings::new(
crate::HttpRoleName::new("default"),
true,
std::vec![crate::HttpRequestKind::wildcard()],
10,
crate::HttpRoleLimits::new(std::option::Option::None, std::option::Option::None, std::option::Option::None, std::option::Option::None),
);
let endpoint = crate::HttpEndpointSettings::new(
"fixture",
true,
crate::HttpProviderName::new("fixture"),
crate::HttpClusterName::new("local"),
crate::HttpEndpointUrl::parse(url).expect("fixture URL must parse"),
std::time::Duration::from_secs(1),
std::time::Duration::from_secs(1),
std::option::Option::Some(1),
std::vec![role],
);
let settings = crate::HttpTransportSettings::new(
std::vec![endpoint],
crate::HttpRetrySettings::new(0, std::time::Duration::from_millis(1), std::time::Duration::from_millis(1)),
);
return crate::HttpTransportPool::new(settings).expect("fixture pool must build");
}
fn serve_once(body: &'static str) -> (std::string::String, std::thread::JoinHandle<std::string::String>) {
let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("fixture listener must bind");
let address = listener.local_addr().expect("fixture listener address must resolve");
let handle = std::thread::spawn(move || {
let (mut stream, _) = listener.accept().expect("fixture server must accept one request");
let request = read_request(&mut stream);
let response = format!("HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}", body.len(), body);
std::io::Write::write_all(&mut stream, response.as_bytes()).expect("fixture response must write");
return request;
});
return (format!("http://{address}"), handle);
}
fn read_request(stream: &mut std::net::TcpStream) -> std::string::String {
let mut bytes = std::vec::Vec::new();
let mut buffer = [0_u8; 1024];
loop {
let count = std::io::Read::read(stream, &mut buffer).expect("fixture request must read");
if count == 0 {
break;
}
bytes.extend_from_slice(&buffer[..count]);
if request_complete(bytes.as_slice()) {
break;
}
}
return std::string::String::from_utf8(bytes).expect("fixture request must be UTF-8");
}
fn request_complete(bytes: &[u8]) -> bool {
let text = match std::str::from_utf8(bytes) {
std::result::Result::Ok(text) => text,
std::result::Result::Err(_) => return false,
};
let header_end = match text.find("\r\n\r\n") {
std::option::Option::Some(value) => value,
std::option::Option::None => return false,
};
let mut content_length = 0_usize;
for line in text[..header_end].lines() {
let (name, value) = match line.split_once(':') {
std::option::Option::Some(parts) => parts,
std::option::Option::None => continue,
};
if name.eq_ignore_ascii_case("content-length") {
content_length = value.trim().parse::<usize>().expect("content length must parse");
}
}
return bytes.len() >= header_end.saturating_add(4).saturating_add(content_length);
}
fn request_body(request: &str) -> serde_json::Value {
let body = request.split("\r\n\r\n").nth(1).expect("fixture request body must exist");
return serde_json::from_str(body).expect("fixture request body must be JSON");
}
#[tokio::test(flavor = "current_thread")]
async fn typed_get_cluster_nodes_preserves_optional_v4_fields_and_omissions() {
let (url, handle) = serve_once(include_str!("../fixtures/http/get_cluster_nodes.success.json"));
let pool = pool_for_url(url.as_str());
let nodes = pool.get_cluster_nodes(&crate::HttpRoleName::new("default")).await.expect("cluster nodes fixture must succeed");
assert_eq!(nodes.len(), 2);
assert_eq!(nodes[0].client_id(), std::option::Option::Some("Agave"));
assert_eq!(nodes[0].serve_repair(), std::option::Option::Some("127.0.0.1:8004"));
assert_eq!(nodes[1].rpc(), std::option::Option::None);
assert_eq!(nodes[1].client_id(), std::option::Option::None);
let request = handle.join().expect("fixture server must join");
let body = request_body(request.as_str());
assert_eq!(body["method"], serde_json::json!("getClusterNodes"));
assert_eq!(body["params"], serde_json::json!([]));
}
#[tokio::test(flavor = "current_thread")]
async fn typed_get_cluster_nodes_rejects_invalid_node_pubkey() {
let (url, handle) = serve_once(include_str!("../fixtures/http/get_cluster_nodes.invalid_pubkey.json"));
let pool = pool_for_url(url.as_str());
let result = pool.get_cluster_nodes(&crate::HttpRoleName::new("default")).await;
let error = result.expect_err("invalid cluster node pubkey must reject typed response");
assert_eq!(error.code(), crate::ERROR_CODE_INVALID_RESPONSE);
handle.join().expect("fixture server must join");
}
#[tokio::test(flavor = "current_thread")]
async fn typed_get_epoch_info_serializes_context_config_and_preserves_nullable_transaction_count() {
let (url, handle) = serve_once(include_str!("../fixtures/http/get_epoch_info.success.json"));
let pool = pool_for_url(url.as_str());
let config = crate::SolanaContextConfig::new(std::option::Option::Some(crate::SolanaCommitment::Finalized), std::option::Option::Some(429_000_000));
let info = pool
.get_epoch_info(&crate::HttpRoleName::new("default"), std::option::Option::Some(&config))
.await
.expect("epoch info fixture must succeed");
assert_eq!(info.absolute_slot(), 430_000_001);
assert_eq!(info.transaction_count(), std::option::Option::None);
let request = handle.join().expect("fixture server must join");
let body = request_body(request.as_str());
assert_eq!(body["params"], serde_json::json!([{"commitment":"finalized","minContextSlot":429000000}]));
}
#[tokio::test(flavor = "current_thread")]
async fn typed_get_epoch_info_omits_explicitly_empty_config() {
let (url, handle) = serve_once(include_str!("../fixtures/http/get_epoch_info.success.json"));
let pool = pool_for_url(url.as_str());
let config = crate::SolanaContextConfig::default();
let info = pool
.get_epoch_info(&crate::HttpRoleName::new("default"), std::option::Option::Some(&config))
.await
.expect("epoch info fixture must succeed");
assert_eq!(info.epoch(), 995);
let request = handle.join().expect("fixture server must join");
assert_eq!(request_body(request.as_str())["params"], serde_json::json!([]));
}
#[tokio::test(flavor = "current_thread")]
async fn typed_get_epoch_schedule_preserves_fixed_wire_shape() {
let (url, handle) = serve_once(include_str!("../fixtures/http/get_epoch_schedule.success.json"));
let pool = pool_for_url(url.as_str());
let schedule = pool.get_epoch_schedule(&crate::HttpRoleName::new("default")).await.expect("epoch schedule fixture must succeed");
assert_eq!(schedule.slots_per_epoch(), 432_000);
assert!(!schedule.warmup());
let request = handle.join().expect("fixture server must join");
assert_eq!(request_body(request.as_str())["method"], serde_json::json!("getEpochSchedule"));
}
#[tokio::test(flavor = "current_thread")]
async fn typed_get_highest_snapshot_slot_preserves_nullable_incremental_slot() {
let (url, handle) = serve_once(include_str!("../fixtures/http/get_highest_snapshot_slot.success.json"));
let pool = pool_for_url(url.as_str());
let snapshot = pool.get_highest_snapshot_slot(&crate::HttpRoleName::new("default")).await.expect("snapshot fixture must succeed");
assert_eq!(snapshot.full(), 429_990_000);
assert_eq!(snapshot.incremental(), std::option::Option::None);
let request = handle.join().expect("fixture server must join");
assert_eq!(request_body(request.as_str())["params"], serde_json::json!([]));
}
#[tokio::test(flavor = "current_thread")]
async fn typed_get_highest_snapshot_slot_preserves_no_snapshot_rpc_error() {
let (url, handle) = serve_once(include_str!("../fixtures/http/get_highest_snapshot_slot.error.json"));
let pool = pool_for_url(url.as_str());
let result = pool.get_highest_snapshot_slot(&crate::HttpRoleName::new("default")).await;
let error = result.expect_err("no snapshot must remain an RPC application error");
assert_eq!(error.code(), crate::ERROR_CODE_RPC_APPLICATION_ERROR);
handle.join().expect("fixture server must join");
}
#[tokio::test(flavor = "current_thread")]
async fn typed_get_identity_decodes_pubkey_object() {
let (url, handle) = serve_once(include_str!("../fixtures/http/get_identity.success.json"));
let pool = pool_for_url(url.as_str());
let identity = pool.get_identity(&crate::HttpRoleName::new("default")).await.expect("identity fixture must succeed");
assert_eq!(identity.to_string(), "ComputeBudget111111111111111111111111111111");
let request = handle.join().expect("fixture server must join");
assert_eq!(request_body(request.as_str())["method"], serde_json::json!("getIdentity"));
}
#[tokio::test(flavor = "current_thread")]
async fn typed_get_identity_rejects_invalid_wire_pubkey() {
let (url, handle) = serve_once(include_str!("../fixtures/http/get_identity.invalid_pubkey.json"));
let pool = pool_for_url(url.as_str());
let result = pool.get_identity(&crate::HttpRoleName::new("default")).await;
let error = result.expect_err("invalid identity pubkey must reject typed response");
assert_eq!(error.code(), crate::ERROR_CODE_INVALID_RESPONSE);
handle.join().expect("fixture server must join");
}
#[tokio::test(flavor = "current_thread")]
async fn typed_get_max_cluster_slots_decode_u64_without_params() {
let (retransmit_url, retransmit_handle) = serve_once(include_str!("../fixtures/http/get_max_retransmit_slot.success.json"));
let retransmit_pool = pool_for_url(retransmit_url.as_str());
let retransmit = retransmit_pool.get_max_retransmit_slot(&crate::HttpRoleName::new("default")).await.expect("max retransmit slot fixture must succeed");
assert_eq!(retransmit, 430_000_010);
let retransmit_request = retransmit_handle.join().expect("fixture server must join");
let retransmit_body = request_body(retransmit_request.as_str());
assert_eq!(retransmit_body["method"], serde_json::json!("getMaxRetransmitSlot"));
assert_eq!(retransmit_body["params"], serde_json::json!([]));
let (shred_url, shred_handle) = serve_once(include_str!("../fixtures/http/get_max_shred_insert_slot.success.json"));
let shred_pool = pool_for_url(shred_url.as_str());
let shred = shred_pool.get_max_shred_insert_slot(&crate::HttpRoleName::new("default")).await.expect("max shred insert slot fixture must succeed");
assert_eq!(shred, 430_000_011);
let shred_request = shred_handle.join().expect("fixture server must join");
let shred_body = request_body(shred_request.as_str());
assert_eq!(shred_body["method"], serde_json::json!("getMaxShredInsertSlot"));
assert_eq!(shred_body["params"], serde_json::json!([]));
}