// file: crates/ksp-onchain-transport-lib/unit_tests/rpc_blocks.rs // version: 2 #[test] fn transaction_details_and_get_block_config_preserve_all_modern_options() { assert_eq!(crate::SolanaTransactionDetails::Full.as_str(), "full"); assert_eq!(crate::SolanaTransactionDetails::Signatures.as_str(), "signatures"); assert_eq!(crate::SolanaTransactionDetails::None.as_str(), "none"); assert_eq!(crate::SolanaTransactionDetails::Accounts.as_str(), "accounts"); let config = crate::SolanaGetBlockConfig::new( std::option::Option::Some(crate::SolanaCommitment::Confirmed), std::option::Option::Some(crate::SolanaTransactionEncoding::JsonParsed), std::option::Option::Some(crate::SolanaTransactionDetails::Accounts), std::option::Option::Some(1), std::option::Option::Some(false), ); assert_eq!(config.commitment(), std::option::Option::Some(crate::SolanaCommitment::Confirmed)); assert_eq!(config.encoding(), std::option::Option::Some(crate::SolanaTransactionEncoding::JsonParsed)); assert_eq!(config.transaction_details(), std::option::Option::Some(crate::SolanaTransactionDetails::Accounts)); assert_eq!(config.max_supported_transaction_version(), std::option::Option::Some(1)); assert_eq!(config.rewards(), std::option::Option::Some(false)); assert_eq!( config.to_json_value(), serde_json::json!({ "commitment":"confirmed", "encoding":"jsonParsed", "transactionDetails":"accounts", "maxSupportedTransactionVersion":1, "rewards":false }) ); assert!(crate::SolanaGetBlockConfig::default().is_empty()); assert_eq!(crate::SolanaGetBlockConfig::default().to_json_value(), serde_json::json!({})); } #[test] fn block_production_config_preserves_identity_range_and_commitment() { let identity = "11111111111111111111111111111111".parse::().expect("fixture identity must parse"); let range = crate::SolanaBlockProductionRange::new(430_000_000, std::option::Option::Some(430_000_099)); let config = crate::SolanaBlockProductionConfig::new( std::option::Option::Some(crate::SolanaCommitment::Finalized), std::option::Option::Some(identity), std::option::Option::Some(range), ); assert_eq!(config.identity(), std::option::Option::Some(&identity)); assert_eq!(config.range().expect("range must be present").first_slot(), 430_000_000); assert_eq!(config.range().expect("range must be present").last_slot(), std::option::Option::Some(430_000_099)); assert_eq!( config.to_json_value(), serde_json::json!({ "commitment":"finalized", "identity":"11111111111111111111111111111111", "range":{"firstSlot":430000000,"lastSlot":430000099} }) ); assert!(crate::SolanaBlockProductionConfig::default().is_empty()); } #[test] fn block_commitment_fixture_preserves_nullable_distribution() { let values = serde_json::from_str::>(include_str!("../fixtures/http/block_commitment.variants.json")) .expect("block commitment fixtures must decode"); let present = crate::SolanaBlockCommitment::decode_wire("getBlockCommitment", values[0].clone()).expect("present commitment must decode"); let absent = crate::SolanaBlockCommitment::decode_wire("getBlockCommitment", values[1].clone()).expect("null commitment must decode"); assert_eq!(present.commitment(), std::option::Option::Some(&[1, 2, 3, 4][..])); assert_eq!(present.total_stake(), 1_000_000_000); assert_eq!(absent.commitment(), std::option::Option::None); } #[test] fn block_production_fixture_types_identity_keys_and_preserves_counts() { let value = serde_json::from_str::(include_str!("../fixtures/http/block_production.v4_2_1.json")).expect("block production fixture must decode"); let result = crate::SolanaBlockProduction::decode_wire("getBlockProduction", value).expect("block production result must decode"); let identity = "11111111111111111111111111111111".parse::().expect("fixture identity must parse"); assert_eq!(result.by_identity().get(&identity), std::option::Option::Some(&(8, 7))); assert_eq!(result.range().first_slot(), 430_000_000); assert_eq!(result.range().last_slot(), 430_000_099); } #[test] fn confirmed_block_fixture_preserves_rich_wire_and_simd_extensions() { let value = serde_json::from_str::(include_str!("../fixtures/http/confirmed_block.v4_2_1.json")).expect("confirmed block fixture must decode"); let block = crate::SolanaConfirmedBlock::decode_wire("getBlock", value).expect("confirmed block must decode"); assert_eq!(block.parent_slot(), 430_000_122); assert_eq!(block.block_time(), std::option::Option::Some(1_787_072_400)); assert_eq!(block.block_height(), std::option::Option::Some(410_000_000)); assert_eq!(block.num_reward_partitions().value(), std::option::Option::Some(&4)); let transactions = block.transactions().value().expect("transactions must be present"); assert_eq!(transactions.len(), 2); assert!(matches!(transactions[0].version(), crate::SolanaWireField::Value(crate::SolanaTransactionVersion::Number(1)))); assert!(transactions[1].version().is_omitted()); assert!(transactions[1].meta().is_null()); let rewards = block.rewards().value().expect("rewards must be present"); assert_eq!(rewards.len(), 2); assert_eq!(rewards[0].reward_type(), std::option::Option::Some("Fee")); assert_eq!(rewards[0].commission(), std::option::Option::None); assert_eq!(rewards[0].commission_bps().value(), std::option::Option::Some(&1_234)); assert_eq!(rewards[1].commission(), std::option::Option::Some(5)); assert!(rewards[1].commission_bps().is_omitted()); } #[test] fn confirmed_block_fixture_distinguishes_omitted_and_explicit_null_fields() { let values = serde_json::from_str::>(include_str!("../fixtures/http/confirmed_block.omissions.json")) .expect("confirmed block omission fixtures must decode"); let omitted = crate::SolanaConfirmedBlock::decode_wire("getBlock", values[0].clone()).expect("omitted-fields block must decode"); let nulls = crate::SolanaConfirmedBlock::decode_wire("getBlock", values[1].clone()).expect("null-fields block must decode"); assert!(omitted.transactions().is_omitted()); assert!(omitted.signatures().is_omitted()); assert!(omitted.rewards().is_omitted()); assert!(omitted.num_reward_partitions().is_omitted()); assert!(nulls.transactions().is_null()); assert!(nulls.signatures().is_null()); assert!(nulls.rewards().is_null()); assert!(nulls.num_reward_partitions().is_null()); } #[test] fn performance_sample_fixture_accepts_current_and_older_shapes() { let values = serde_json::from_str::>(include_str!("../fixtures/http/performance_sample.variants.json")) .expect("performance sample fixtures must decode"); let current = crate::SolanaPerformanceSample::decode_wire("getRecentPerformanceSamples", values[0].clone()).expect("current sample must decode"); let older = crate::SolanaPerformanceSample::decode_wire("getRecentPerformanceSamples", values[1].clone()).expect("older sample must decode"); assert_eq!(current.slot(), 430_000_123); assert_eq!(current.num_transactions(), 250_000); assert_eq!(current.num_non_vote_transactions(), std::option::Option::Some(175_000)); assert_eq!(current.num_slots(), 120); assert_eq!(current.sample_period_secs(), 60); assert_eq!(older.num_non_vote_transactions(), std::option::Option::None); } #[test] fn block_reward_rejects_invalid_pubkey_without_echoing_value() { let result = crate::SolanaConfirmedBlock::decode_wire( "getBlock", serde_json::json!({ "previousBlockhash":"previous", "blockhash":"block", "parentSlot":1, "rewards":[{"pubkey":"not-a-pubkey","lamports":1,"postBalance":2,"rewardType":"Fee","commission":null}], "blockTime":null, "blockHeight":null }), ); let error = result.expect_err("invalid reward pubkey must fail closed"); assert_eq!(error.code(), crate::ERROR_CODE_INVALID_RESPONSE); assert!(!error.to_string().contains("not-a-pubkey")); } 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) { 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::().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_block_commitment_serializes_slot_and_preserves_distribution() { let (url, handle) = serve_once(include_str!("../fixtures/http/get_block_commitment.success.json")); let pool = pool_for_url(url.as_str()); let result = pool.get_block_commitment(&crate::HttpRoleName::new("default"), 430_000_123).await.expect("block commitment fixture must succeed"); assert_eq!(result.commitment(), std::option::Option::Some(&[1, 2, 3, 4][..])); assert_eq!(result.total_stake(), 1_000_000_000); let request = handle.join().expect("fixture server must join"); let body = request_body(request.as_str()); assert_eq!(body["method"], serde_json::json!("getBlockCommitment")); assert_eq!(body["params"], serde_json::json!([430000123])); } #[tokio::test(flavor = "current_thread")] async fn typed_get_block_height_serializes_context_config() { let (url, handle) = serve_once(include_str!("../fixtures/http/get_block_height.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 height = pool .get_block_height(&crate::HttpRoleName::new("default"), std::option::Option::Some(&config)) .await .expect("block height fixture must succeed"); assert_eq!(height, 410_000_000); let request = handle.join().expect("fixture server must join"); assert_eq!(request_body(request.as_str())["params"], serde_json::json!([{"commitment":"finalized","minContextSlot":429000000}])); } #[tokio::test(flavor = "current_thread")] async fn typed_get_block_height_omits_explicitly_empty_config() { let (url, handle) = serve_once(include_str!("../fixtures/http/get_block_height.success.json")); let pool = pool_for_url(url.as_str()); let config = crate::SolanaContextConfig::default(); let height = pool .get_block_height(&crate::HttpRoleName::new("default"), std::option::Option::Some(&config)) .await .expect("block height fixture must succeed"); assert_eq!(height, 410_000_000); 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_block_time_preserves_timestamp_and_null() { let (url, handle) = serve_once(include_str!("../fixtures/http/get_block_time.success.json")); let pool = pool_for_url(url.as_str()); let timestamp = pool.get_block_time(&crate::HttpRoleName::new("default"), 430_000_123).await.expect("block time fixture must succeed"); assert_eq!(timestamp, std::option::Option::Some(1_787_072_400)); let request = handle.join().expect("fixture server must join"); assert_eq!(request_body(request.as_str())["params"], serde_json::json!([430000123])); let (url, handle) = serve_once(include_str!("../fixtures/http/get_block_time.null.json")); let pool = pool_for_url(url.as_str()); let timestamp = pool.get_block_time(&crate::HttpRoleName::new("default"), 430_000_124).await.expect("null block time fixture must succeed"); assert_eq!(timestamp, std::option::Option::None); handle.join().expect("fixture server must join"); } #[tokio::test(flavor = "current_thread")] async fn typed_get_first_available_block_has_no_params() { let (url, handle) = serve_once(include_str!("../fixtures/http/get_first_available_block.success.json")); let pool = pool_for_url(url.as_str()); let slot = pool.get_first_available_block(&crate::HttpRoleName::new("default")).await.expect("first available block fixture must succeed"); assert_eq!(slot, 250_000); let request = handle.join().expect("fixture server must join"); let body = request_body(request.as_str()); assert_eq!(body["method"], serde_json::json!("getFirstAvailableBlock")); assert_eq!(body["params"], serde_json::json!([])); } #[tokio::test(flavor = "current_thread")] async fn typed_minimum_ledger_slot_has_no_params() { let (url, handle) = serve_once(include_str!("../fixtures/http/minimum_ledger_slot.success.json")); let pool = pool_for_url(url.as_str()); let slot = pool.minimum_ledger_slot(&crate::HttpRoleName::new("default")).await.expect("minimum ledger slot fixture must succeed"); assert_eq!(slot, 123_456); let request = handle.join().expect("fixture server must join"); let body = request_body(request.as_str()); assert_eq!(body["method"], serde_json::json!("minimumLedgerSlot")); assert_eq!(body["params"], serde_json::json!([])); }