// file: crates/ksp-onchain-transport-lib/unit_tests/rpc_blocks.rs // version: 6 #[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!([])); } #[tokio::test(flavor = "current_thread")] async fn typed_get_blocks_covers_all_four_overloads_and_preserves_server_order() { let role = crate::HttpRoleName::new("default"); let config = crate::SolanaContextConfig::new(std::option::Option::Some(crate::SolanaCommitment::Confirmed), std::option::Option::Some(429_999_999)); let empty_config = crate::SolanaContextConfig::default(); let cases = [ (std::option::Option::None, std::option::Option::None, serde_json::json!([430000100])), (std::option::Option::Some(430_000_109), std::option::Option::None, serde_json::json!([430000100, 430000109])), ( std::option::Option::None, std::option::Option::Some(&config), serde_json::json!([430000100,{"commitment":"confirmed","minContextSlot":429999999}]), ), ( std::option::Option::Some(430_000_109), std::option::Option::Some(&config), serde_json::json!([430000100,430000109,{"commitment":"confirmed","minContextSlot":429999999}]), ), (std::option::Option::None, std::option::Option::Some(&empty_config), serde_json::json!([430000100, {}])), ]; for (end_slot, config, expected_params) in cases { let (url, handle) = serve_once(include_str!("../fixtures/http/get_blocks.success.json")); let pool = pool_for_url(url.as_str()); let blocks = pool.get_blocks(&role, 430_000_100, end_slot, config).await.expect("getBlocks overload fixture must succeed"); assert_eq!(blocks, std::vec![430_000_100, 430_000_103, 430_000_109]); let request = handle.join().expect("fixture server must join"); let body = request_body(request.as_str()); assert_eq!(body["method"], serde_json::json!("getBlocks")); assert_eq!(body["params"], expected_params); } } #[tokio::test(flavor = "current_thread")] async fn typed_get_blocks_allows_reversed_and_boundary_ranges_but_rejects_oversized_before_io() { let role = crate::HttpRoleName::new("default"); let (url, handle) = serve_once(include_str!("../fixtures/http/get_blocks.empty.json")); let pool = pool_for_url(url.as_str()); let blocks = pool .get_blocks(&role, 430_000_100, std::option::Option::Some(430_000_099), std::option::Option::None) .await .expect("reversed getBlocks range must remain a valid RPC request"); assert!(blocks.is_empty()); let request = handle.join().expect("fixture server must join"); assert_eq!(request_body(request.as_str())["params"], serde_json::json!([430000100, 430000099])); let (url, handle) = serve_once(include_str!("../fixtures/http/get_blocks.success.json")); let pool = pool_for_url(url.as_str()); let blocks = pool .get_blocks(&role, 10, std::option::Option::Some(500_010), std::option::Option::None) .await .expect("500000-slot getBlocks range must remain valid"); assert_eq!(blocks, std::vec![430_000_100, 430_000_103, 430_000_109]); let request = handle.join().expect("fixture server must join"); assert_eq!(request_body(request.as_str())["params"], serde_json::json!([10, 500010])); let pool = pool_for_url("http://127.0.0.1:9"); let result = pool.get_blocks(&role, 10, std::option::Option::Some(500_011), std::option::Option::None).await; let error = result.expect_err("range above 500000 must reject before I/O"); assert_eq!(error.code(), crate::ERROR_CODE_INVALID_RPC_PARAMETERS); assert_eq!(error.context()[1].key(), "start_slot"); assert_eq!(error.context()[1].value(), "10"); assert_eq!(error.context()[2].key(), "end_slot"); assert_eq!(error.context()[2].value(), "500011"); } #[tokio::test(flavor = "current_thread")] async fn typed_block_range_wrappers_reject_processed_commitment_before_io() { let role = crate::HttpRoleName::new("default"); let pool = pool_for_url("http://127.0.0.1:9"); let config = crate::SolanaContextConfig::new(std::option::Option::Some(crate::SolanaCommitment::Processed), std::option::Option::None); let get_blocks = pool.get_blocks(&role, 1, std::option::Option::None, std::option::Option::Some(&config)).await; let error = get_blocks.expect_err("getBlocks processed commitment must reject before I/O"); assert_eq!(error.code(), crate::ERROR_CODE_INVALID_RPC_PARAMETERS); assert_eq!(error.context()[1].key(), "commitment"); assert_eq!(error.context()[1].value(), "processed"); let get_blocks_with_limit = pool.get_blocks_with_limit(&role, 1, 1, std::option::Option::Some(&config)).await; let error = get_blocks_with_limit.expect_err("getBlocksWithLimit processed commitment must reject before I/O"); assert_eq!(error.code(), crate::ERROR_CODE_INVALID_RPC_PARAMETERS); assert_eq!(error.context()[1].key(), "commitment"); assert_eq!(error.context()[1].value(), "processed"); } #[tokio::test(flavor = "current_thread")] async fn typed_get_blocks_with_limit_accepts_zero_and_maximum_and_rejects_above_maximum() { let role = crate::HttpRoleName::new("default"); let (url, handle) = serve_once(include_str!("../fixtures/http/get_blocks_with_limit.empty.json")); let pool = pool_for_url(url.as_str()); let blocks = pool.get_blocks_with_limit(&role, 430_000_200, 0, std::option::Option::None).await.expect("zero getBlocksWithLimit limit must remain valid"); assert!(blocks.is_empty()); let request = handle.join().expect("fixture server must join"); assert_eq!(request_body(request.as_str())["params"], serde_json::json!([430000200, 0])); let config = crate::SolanaContextConfig::new(std::option::Option::Some(crate::SolanaCommitment::Finalized), std::option::Option::Some(430_000_000)); let (url, handle) = serve_once(include_str!("../fixtures/http/get_blocks_with_limit.success.json")); let pool = pool_for_url(url.as_str()); let blocks = pool .get_blocks_with_limit(&role, 430_000_200, 500_000, std::option::Option::Some(&config)) .await .expect("maximum getBlocksWithLimit limit must remain valid"); assert_eq!(blocks, std::vec![430_000_200, 430_000_201, 430_000_205]); let request = handle.join().expect("fixture server must join"); assert_eq!(request_body(request.as_str())["params"], serde_json::json!([430000200,500000,{"commitment":"finalized","minContextSlot":430000000}])); let pool = pool_for_url("http://127.0.0.1:9"); let result = pool.get_blocks_with_limit(&role, 1, 500_001, std::option::Option::None).await; let error = result.expect_err("getBlocksWithLimit above 500000 must reject before I/O"); assert_eq!(error.code(), crate::ERROR_CODE_INVALID_RPC_PARAMETERS); assert_eq!(error.context()[1].key(), "limit"); assert_eq!(error.context()[1].value(), "500001"); } #[tokio::test(flavor = "current_thread")] async fn typed_get_recent_performance_samples_preserves_default_explicit_limit_order_and_older_shape() { let role = crate::HttpRoleName::new("default"); for (limit, expected_params) in [(std::option::Option::None, serde_json::json!([])), (std::option::Option::Some(720), serde_json::json!([720]))] { let (url, handle) = serve_once(include_str!("../fixtures/http/get_recent_performance_samples.success.json")); let pool = pool_for_url(url.as_str()); let samples = pool.get_recent_performance_samples(&role, limit).await.expect("performance sample fixture must succeed"); assert_eq!(samples.len(), 2); assert_eq!(samples[0].slot(), 430_000_123); assert_eq!(samples[0].num_transactions(), 250_000); assert_eq!(samples[0].num_non_vote_transactions(), std::option::Option::Some(175_000)); assert_eq!(samples[1].slot(), 430_000_000); assert_eq!(samples[1].num_non_vote_transactions(), 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!("getRecentPerformanceSamples")); assert_eq!(body["params"], expected_params); } } #[tokio::test(flavor = "current_thread")] async fn typed_get_recent_performance_samples_rejects_above_720_before_io() { let pool = pool_for_url("http://127.0.0.1:9"); let result = pool.get_recent_performance_samples(&crate::HttpRoleName::new("default"), std::option::Option::Some(721)).await; let error = result.expect_err("performance sample limit above 720 must reject before I/O"); assert_eq!(error.code(), crate::ERROR_CODE_INVALID_RPC_PARAMETERS); assert_eq!(error.context()[1].key(), "limit"); assert_eq!(error.context()[1].value(), "721"); } #[tokio::test(flavor = "current_thread")] async fn typed_get_block_production_serializes_full_config_and_decodes_contextual_result() { 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), ); let (url, handle) = serve_once(include_str!("../fixtures/http/get_block_production.success.json")); let pool = pool_for_url(url.as_str()); let response = pool .get_block_production(&crate::HttpRoleName::new("default"), std::option::Option::Some(&config)) .await .expect("block production fixture must succeed"); assert_eq!(response.context().slot(), 430_000_123); assert_eq!(response.context().api_version(), std::option::Option::Some("4.2.1")); assert_eq!(response.value().by_identity().get(&identity), std::option::Option::Some(&(8, 7))); assert_eq!(response.value().range().first_slot(), 430_000_000); assert_eq!(response.value().range().last_slot(), 430_000_099); let request = handle.join().expect("fixture server must join"); let body = request_body(request.as_str()); assert_eq!(body["method"], serde_json::json!("getBlockProduction")); assert_eq!( body["params"], serde_json::json!([{ "commitment":"finalized", "identity":"11111111111111111111111111111111", "range":{"firstSlot":430000000,"lastSlot":430000099} }]) ); } #[tokio::test(flavor = "current_thread")] async fn typed_get_block_production_omits_empty_config_and_supports_open_range_with_processed_commitment() { let role = crate::HttpRoleName::new("default"); let empty = crate::SolanaBlockProductionConfig::default(); let (url, handle) = serve_once(include_str!("../fixtures/http/get_block_production.success.json")); let pool = pool_for_url(url.as_str()); let response = pool.get_block_production(&role, std::option::Option::Some(&empty)).await.expect("empty block production config must succeed"); assert_eq!(response.value().range().last_slot(), 430_000_099); let request = handle.join().expect("fixture server must join"); assert_eq!(request_body(request.as_str())["params"], serde_json::json!([])); let range = crate::SolanaBlockProductionRange::new(430_000_000, std::option::Option::None); let config = crate::SolanaBlockProductionConfig::new( std::option::Option::Some(crate::SolanaCommitment::Processed), std::option::Option::None, std::option::Option::Some(range), ); let (url, handle) = serve_once(include_str!("../fixtures/http/get_block_production.success.json")); let pool = pool_for_url(url.as_str()); pool.get_block_production(&role, std::option::Option::Some(&config)).await.expect("open block production range must succeed"); let request = handle.join().expect("fixture server must join"); assert_eq!(request_body(request.as_str())["params"], serde_json::json!([{"commitment":"processed","range":{"firstSlot":430000000}}])); } #[tokio::test(flavor = "current_thread")] async fn typed_get_block_production_rejects_reversed_range_before_io() { let range = crate::SolanaBlockProductionRange::new(430_000_100, std::option::Option::Some(430_000_099)); let config = crate::SolanaBlockProductionConfig::new(std::option::Option::None, std::option::Option::None, std::option::Option::Some(range)); let pool = pool_for_url("http://127.0.0.1:9"); let result = pool.get_block_production(&crate::HttpRoleName::new("default"), std::option::Option::Some(&config)).await; let error = result.expect_err("reversed block production range must reject before I/O"); assert_eq!(error.code(), crate::ERROR_CODE_INVALID_RPC_PARAMETERS); assert_eq!(error.context()[1].key(), "first_slot"); assert_eq!(error.context()[1].value(), "430000100"); assert_eq!(error.context()[2].key(), "last_slot"); assert_eq!(error.context()[2].value(), "430000099"); } #[tokio::test(flavor = "current_thread")] async fn typed_get_block_production_rejects_invalid_wire_identity_without_echoing_value() { let (url, handle) = serve_once(include_str!("../fixtures/http/get_block_production.invalid_identity.json")); let pool = pool_for_url(url.as_str()); let result = pool.get_block_production(&crate::HttpRoleName::new("default"), std::option::Option::None).await; let error = result.expect_err("invalid block production identity must fail closed"); assert_eq!(error.code(), crate::ERROR_CODE_INVALID_RESPONSE); assert!(!error.to_string().contains("not-a-pubkey")); handle.join().expect("fixture server must join"); } #[tokio::test(flavor = "current_thread")] async fn typed_get_block_modern_full_config_preserves_rich_wire_and_simd_fields() { let config = crate::SolanaGetBlockConfig::new( std::option::Option::Some(crate::SolanaCommitment::Finalized), std::option::Option::Some(crate::SolanaTransactionEncoding::JsonParsed), std::option::Option::Some(crate::SolanaTransactionDetails::Full), std::option::Option::Some(1), std::option::Option::Some(true), ); let (url, handle) = serve_once(include_str!("../fixtures/http/get_block.full.success.json")); let pool = pool_for_url(url.as_str()); let block = pool .get_block(&crate::HttpRoleName::new("default"), 430_000_123, std::option::Option::Some(&config)) .await .expect("modern getBlock fixture must succeed") .expect("modern getBlock fixture must contain a block"); assert_eq!(block.parent_slot(), 430_000_122); assert_eq!(block.num_reward_partitions().value(), std::option::Option::Some(&4)); let transactions = block.transactions().value().expect("full block transactions must be present"); assert_eq!(transactions.len(), 2); assert!(matches!(transactions[0].version(), crate::SolanaWireField::Value(crate::SolanaTransactionVersion::Number(1)))); let first_meta = transactions[0].meta().value().expect("full transaction metadata must be present"); assert_eq!(first_meta["rewards"][0]["commissionBps"], serde_json::json!(1234)); let rewards = block.rewards().value().expect("block rewards must be present"); assert_eq!(rewards[0].commission(), std::option::Option::None); assert_eq!(rewards[0].commission_bps().value(), std::option::Option::Some(&1_234)); assert!(rewards[1].commission_bps().is_omitted()); let request = handle.join().expect("fixture server must join"); let body = request_body(request.as_str()); assert_eq!(body["method"], serde_json::json!("getBlock")); assert_eq!( body["params"], serde_json::json!([430000123,{"commitment":"finalized","encoding":"jsonParsed","transactionDetails":"full","maxSupportedTransactionVersion":1,"rewards":true}]) ); } #[tokio::test(flavor = "current_thread")] async fn typed_get_block_preserves_absent_and_explicit_empty_modern_config() { let role = crate::HttpRoleName::new("default"); for (config, expected_params) in [ (std::option::Option::None, serde_json::json!([430000123])), (std::option::Option::Some(crate::SolanaGetBlockConfig::default()), serde_json::json!([430000123, {}])), ] { let (url, handle) = serve_once(include_str!("../fixtures/http/get_block.null.json")); let pool = pool_for_url(url.as_str()); let block = pool.get_block(&role, 430_000_123, config.as_ref()).await.expect("nullable getBlock request must succeed"); assert!(block.is_none()); let request = handle.join().expect("fixture server must join"); assert_eq!(request_body(request.as_str())["params"], expected_params); } } #[allow(deprecated)] #[tokio::test(flavor = "current_thread")] async fn typed_get_block_legacy_covers_all_bare_encoding_labels_and_response_shapes() { let role = crate::HttpRoleName::new("default"); let cases = [ (crate::SolanaTransactionEncoding::Binary, "binary", include_str!("../fixtures/http/get_block.binary.success.json")), (crate::SolanaTransactionEncoding::Base58, "base58", include_str!("../fixtures/http/get_block.base58.success.json")), (crate::SolanaTransactionEncoding::Base64, "base64", include_str!("../fixtures/http/get_block.base64.success.json")), (crate::SolanaTransactionEncoding::Json, "json", include_str!("../fixtures/http/get_block.json.success.json")), (crate::SolanaTransactionEncoding::JsonParsed, "jsonParsed", include_str!("../fixtures/http/get_block.json_parsed.success.json")), ]; for (encoding, wire_label, response) in cases { let (url, handle) = serve_once(response); let pool = pool_for_url(url.as_str()); let block = pool .get_block_legacy(&role, 430_000_123, encoding) .await .expect("legacy getBlock encoding must succeed") .expect("legacy getBlock encoding must return a block"); let transactions = block.transactions().value().expect("legacy full response must contain transactions"); assert_eq!(transactions.len(), 1); match encoding { crate::SolanaTransactionEncoding::Binary => assert!(matches!(transactions[0].transaction(), crate::SolanaEncodedTransaction::LegacyBinary(_))), crate::SolanaTransactionEncoding::Base58 => assert!(matches!( transactions[0].transaction(), crate::SolanaEncodedTransaction::Binary { encoding: crate::SolanaTransactionBinaryEncoding::Base58, .. } )), crate::SolanaTransactionEncoding::Base64 => assert!(matches!( transactions[0].transaction(), crate::SolanaEncodedTransaction::Binary { encoding: crate::SolanaTransactionBinaryEncoding::Base64, .. } )), crate::SolanaTransactionEncoding::Json | crate::SolanaTransactionEncoding::JsonParsed => { assert!(matches!(transactions[0].transaction(), crate::SolanaEncodedTransaction::Json(_))); }, } let request = handle.join().expect("fixture server must join"); assert_eq!(request_body(request.as_str())["params"], serde_json::json!([430000123, wire_label])); } } #[tokio::test(flavor = "current_thread")] async fn typed_get_block_modern_covers_all_agave_v4_2_1_encoding_labels() { let role = crate::HttpRoleName::new("default"); let cases = [ (crate::SolanaTransactionEncoding::Binary, "binary", include_str!("../fixtures/http/get_block.binary.success.json")), (crate::SolanaTransactionEncoding::Base58, "base58", include_str!("../fixtures/http/get_block.base58.success.json")), (crate::SolanaTransactionEncoding::Base64, "base64", include_str!("../fixtures/http/get_block.base64.success.json")), (crate::SolanaTransactionEncoding::Json, "json", include_str!("../fixtures/http/get_block.json.success.json")), (crate::SolanaTransactionEncoding::JsonParsed, "jsonParsed", include_str!("../fixtures/http/get_block.json_parsed.success.json")), ]; for (encoding, wire_label, response) in cases { let config = crate::SolanaGetBlockConfig::new( std::option::Option::Some(crate::SolanaCommitment::Confirmed), std::option::Option::Some(encoding), std::option::Option::Some(crate::SolanaTransactionDetails::Full), std::option::Option::Some(1), std::option::Option::Some(true), ); let (url, handle) = serve_once(response); let pool = pool_for_url(url.as_str()); pool.get_block(&role, 430_000_123, std::option::Option::Some(&config)) .await .expect("modern getBlock encoding must succeed") .expect("modern getBlock encoding must return a block"); let request = handle.join().expect("fixture server must join"); assert_eq!( request_body(request.as_str())["params"], serde_json::json!([430000123,{"commitment":"confirmed","encoding":wire_label,"transactionDetails":"full","maxSupportedTransactionVersion":1,"rewards":true}]) ); } } #[tokio::test(flavor = "current_thread")] async fn typed_get_block_covers_full_signatures_none_and_accounts_transaction_details() { let role = crate::HttpRoleName::new("default"); let cases = [ (crate::SolanaTransactionDetails::Full, "full", include_str!("../fixtures/http/get_block.full_no_rewards.success.json")), (crate::SolanaTransactionDetails::Signatures, "signatures", include_str!("../fixtures/http/get_block.signatures.success.json")), (crate::SolanaTransactionDetails::None, "none", include_str!("../fixtures/http/get_block.none.success.json")), (crate::SolanaTransactionDetails::Accounts, "accounts", include_str!("../fixtures/http/get_block.accounts.success.json")), ]; for (details, wire_label, response) in cases { let config = crate::SolanaGetBlockConfig::new( std::option::Option::Some(crate::SolanaCommitment::Confirmed), std::option::Option::Some(crate::SolanaTransactionEncoding::JsonParsed), std::option::Option::Some(details), std::option::Option::Some(1), std::option::Option::Some(false), ); let (url, handle) = serve_once(response); let pool = pool_for_url(url.as_str()); let block = pool .get_block(&role, 430_000_123, std::option::Option::Some(&config)) .await .expect("transactionDetails getBlock fixture must succeed") .expect("transactionDetails getBlock fixture must return a block"); match details { crate::SolanaTransactionDetails::Full => { let transactions = block.transactions().value().expect("full transaction list must be present"); assert_eq!(transactions.len(), 1); assert!(block.signatures().is_omitted()); assert_eq!(transactions[0].meta().value().expect("full transaction metadata must be present")["rewards"], serde_json::Value::Null); }, crate::SolanaTransactionDetails::Signatures => { assert!(block.transactions().is_omitted()); assert_eq!(block.signatures().value().expect("signatures must be present"), &std::vec!["sig-a".to_owned(), "sig-b".to_owned()]); }, crate::SolanaTransactionDetails::None => { assert!(block.transactions().is_omitted()); assert!(block.signatures().is_omitted()); }, crate::SolanaTransactionDetails::Accounts => { let transactions = block.transactions().value().expect("accounts transaction list must be present"); assert_eq!(transactions.len(), 1); assert!(matches!(transactions[0].transaction(), crate::SolanaEncodedTransaction::Json(_))); if let crate::SolanaEncodedTransaction::Json(transaction) = transactions[0].transaction() { assert_eq!(transaction["accountKeys"][0]["pubkey"], serde_json::json!("11111111111111111111111111111111")); } assert!(matches!(transactions[0].version(), crate::SolanaWireField::Value(crate::SolanaTransactionVersion::Number(1)))); }, } assert!(block.rewards().is_omitted()); assert_eq!(block.num_reward_partitions().value(), std::option::Option::Some(&4)); let request = handle.join().expect("fixture server must join"); assert_eq!( request_body(request.as_str())["params"], serde_json::json!([430000123,{"commitment":"confirmed","encoding":"jsonParsed","transactionDetails":wire_label,"maxSupportedTransactionVersion":1,"rewards":false}]) ); } } #[tokio::test(flavor = "current_thread")] async fn typed_get_block_preserves_null_and_omitted_top_level_wire_states() { let role = crate::HttpRoleName::new("default"); for (response, expect_null) in [ (include_str!("../fixtures/http/get_block.omitted_fields.success.json"), false), (include_str!("../fixtures/http/get_block.null_fields.success.json"), true), ] { let (url, handle) = serve_once(response); let pool = pool_for_url(url.as_str()); let block = pool .get_block(&role, 430_000_123, std::option::Option::None) .await .expect("wire-state getBlock fixture must succeed") .expect("wire-state getBlock fixture must return a block"); if expect_null { assert!(block.transactions().is_null()); assert!(block.signatures().is_null()); assert!(block.rewards().is_null()); assert!(block.num_reward_partitions().is_null()); } else { assert!(block.transactions().is_omitted()); assert!(block.signatures().is_omitted()); assert!(block.rewards().is_omitted()); assert!(block.num_reward_partitions().is_omitted()); } handle.join().expect("fixture server must join"); } } #[tokio::test(flavor = "current_thread")] async fn typed_get_block_preserves_unsupported_version_rpc_error() { let config = crate::SolanaGetBlockConfig::new( std::option::Option::Some(crate::SolanaCommitment::Finalized), std::option::Option::Some(crate::SolanaTransactionEncoding::Base64), std::option::Option::Some(crate::SolanaTransactionDetails::Full), std::option::Option::Some(0), std::option::Option::Some(true), ); let (url, handle) = serve_once(include_str!("../fixtures/http/get_block.error_unsupported_version.json")); let pool = pool_for_url(url.as_str()); let result = pool.get_block(&crate::HttpRoleName::new("default"), 430_000_123, std::option::Option::Some(&config)).await; let error = result.expect_err("unsupported transaction version 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_block_preserves_block_not_available_rpc_error() { let (url, handle) = serve_once(include_str!("../fixtures/http/get_block.error_block_not_available.json")); let pool = pool_for_url(url.as_str()); let result = pool.get_block(&crate::HttpRoleName::new("default"), 430_000_123, std::option::Option::None).await; let error = result.expect_err("unavailable block 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_block_rejects_processed_commitment_before_io() { let config = crate::SolanaGetBlockConfig::new( std::option::Option::Some(crate::SolanaCommitment::Processed), std::option::Option::Some(crate::SolanaTransactionEncoding::Json), std::option::Option::None, std::option::Option::None, std::option::Option::None, ); let pool = pool_for_url("http://127.0.0.1:9"); let result = pool.get_block(&crate::HttpRoleName::new("default"), 430_000_123, std::option::Option::Some(&config)).await; let error = result.expect_err("processed getBlock commitment must reject before I/O"); assert_eq!(error.code(), crate::ERROR_CODE_INVALID_RPC_PARAMETERS); assert_eq!(error.context()[1].key(), "commitment"); assert_eq!(error.context()[1].value(), "processed"); }