diff --git a/Cargo.lock b/Cargo.lock index cd709200bb9..f4e019e0fa6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8161,6 +8161,7 @@ dependencies = [ "axum 0.8.9", "base64 0.22.1", "bytes", + "flate2", "h2", "http", "http-body", diff --git a/packages/dapi-grpc/Cargo.toml b/packages/dapi-grpc/Cargo.toml index 811ba42861c..d4d6f7abc3b 100644 --- a/packages/dapi-grpc/Cargo.toml +++ b/packages/dapi-grpc/Cargo.toml @@ -35,7 +35,10 @@ client = ["platform"] # slice, but its hyper/rustls transport stack is not enabled. Native # networked consumers enable this explicitly (rs-dapi-client does so through # its non-wasm dependency); the wasm codegen path never emits `connect()`. +# `tonic/gzip` lets native clients and the DAPI server negotiate gzip +# per-message compression; browsers rely on HTTP Content-Encoding instead. transport = [ + "tonic/gzip", "tonic/channel", "tonic/transport", "tonic/tls-native-roots", diff --git a/packages/dashmate/templates/platform/gateway/envoy.yaml.dot b/packages/dashmate/templates/platform/gateway/envoy.yaml.dot index 1c280245da5..ffb6caa377a 100644 --- a/packages/dashmate/templates/platform/gateway/envoy.yaml.dot +++ b/packages/dashmate/templates/platform/gateway/envoy.yaml.dot @@ -139,6 +139,29 @@ - name: envoy.filters.http.cors typed_config: "@type": type.googleapis.com/envoy.extensions.filters.http.cors.v3.Cors + # gzip grpc-web responses for clients that send Accept-Encoding (browsers + # do it on their own and decompress transparently). It sits above grpc_web + # so it compresses the final grpc-web body, trailer frame included. Native + # gRPC (application/grpc) is not matched: rs-dapi compresses those messages + # itself when the client lists gzip in grpc-accept-encoding. + - name: envoy.filters.http.compressor + typed_config: + "@type": type.googleapis.com/envoy.extensions.filters.http.compressor.v3.Compressor + response_direction_config: + common_config: + content_type: + - application/grpc-web + - application/grpc-web+proto + - application/grpc-web-text + - application/grpc-web-text+proto + compressor_library: + name: gzip + typed_config: + "@type": type.googleapis.com/envoy.extensions.compression.gzip.compressor.v3.Gzip + # zlib's full 32 KiB window instead of Envoy's 4 KiB default: on mainnet + # responses above 64 KiB it is the difference between ~83% and ~6% + # savings. Costs ~144 KiB of zlib state per in-flight compressed response. + window_bits: 15 - name: envoy.filters.http.grpc_web typed_config: "@type": type.googleapis.com/envoy.extensions.filters.http.grpc_web.v3.GrpcWeb diff --git a/packages/dashmate/test/unit/templates/envoyTemplate.spec.js b/packages/dashmate/test/unit/templates/envoyTemplate.spec.js index e654d62a6f7..9358dd8d8db 100644 --- a/packages/dashmate/test/unit/templates/envoyTemplate.spec.js +++ b/packages/dashmate/test/unit/templates/envoyTemplate.spec.js @@ -21,4 +21,41 @@ describe('envoy template', () => { expect(envoyConfig).to.include('address: 127.0.0.1'); expect(envoyConfig).to.include('port_value: 9901'); }); + + it('should gzip only grpc-web responses, after grpc_web has framed them', () => { + const getBaseConfig = getBaseConfigFactory(HomeDir.createTemp()); + const config = getBaseConfig(); + + const renderTemplate = renderTemplateFactory(); + const renderServiceTemplates = renderServiceTemplatesFactory(renderTemplate); + const envoyConfig = renderServiceTemplates(config)['platform/gateway/envoy.yaml']; + + // Encoders run in reverse filter order, so a compressor listed between cors and + // grpc_web compresses the final grpc-web body, trailer frame included. + const cors = envoyConfig.indexOf('- name: envoy.filters.http.cors'); + const compressor = envoyConfig.indexOf('- name: envoy.filters.http.compressor'); + const grpcWeb = envoyConfig.indexOf('- name: envoy.filters.http.grpc_web'); + + expect(cors).to.be.greaterThan(-1); + expect(compressor).to.be.greaterThan(cors); + expect(grpcWeb).to.be.greaterThan(compressor); + + // Only grpc-web responses are compressed; rs-dapi compresses native gRPC itself. + const responses = envoyConfig.indexOf('response_direction_config:', compressor); + const library = envoyConfig.indexOf('compressor_library:', compressor); + + expect(responses).to.be.within(compressor, library); + + const contentTypes = (envoyConfig + .slice(responses, library) + .match(/^\s+- application\/[\w+-]+$/gm) ?? []) + .map((line) => line.trim().slice(2)); + + expect(contentTypes).to.have.members([ + 'application/grpc-web', + 'application/grpc-web+proto', + 'application/grpc-web-text', + 'application/grpc-web-text+proto', + ]); + }); }); diff --git a/packages/rs-dapi-client/src/transport.rs b/packages/rs-dapi-client/src/transport.rs index 0e8cb7507ef..b4f40fd1f0f 100644 --- a/packages/rs-dapi-client/src/transport.rs +++ b/packages/rs-dapi-client/src/transport.rs @@ -23,11 +23,13 @@ pub(crate) use proxy::PROXY_CONNECT_TIMEOUT; pub use proxy::{ProxyEndpoint, ProxyError, Socks5Auth, Socks5Proxy}; #[cfg(not(target_arch = "wasm32"))] pub use tonic_channel::{ - create_channel, CoreGrpcClient, PlatformGrpcClient, TokioBackonSleeper as BackonSleeper, + create_channel, new_core_client, new_platform_client, CoreGrpcClient, PlatformGrpcClient, + TokioBackonSleeper as BackonSleeper, }; #[cfg(target_arch = "wasm32")] pub use wasm_channel::{ - create_channel, CoreGrpcClient, PlatformGrpcClient, WasmBackonSleeper as BackonSleeper, + create_channel, new_core_client, new_platform_client, CoreGrpcClient, PlatformGrpcClient, + WasmBackonSleeper as BackonSleeper, }; /// Sleep for the given duration. diff --git a/packages/rs-dapi-client/src/transport/grpc.rs b/packages/rs-dapi-client/src/transport/grpc.rs index aade509bd59..2b5ea2041c4 100644 --- a/packages/rs-dapi-client/src/transport/grpc.rs +++ b/packages/rs-dapi-client/src/transport/grpc.rs @@ -2,9 +2,9 @@ use std::time::Duration; -use super::create_channel; #[cfg(not(target_arch = "wasm32"))] use super::proxy::is_proxy_failure; +use super::{create_channel, new_core_client, new_platform_client}; use super::{CanRetry, TransportClient, TransportError, TransportRequest}; use super::{CoreGrpcClient, PlatformGrpcClient}; use crate::connection_pool::{ConnectionPool, PoolPrefix}; @@ -23,7 +23,7 @@ impl TransportClient for PlatformGrpcClient { Ok(pool .get_or_create(PoolPrefix::Platform, &uri, None, || { match create_channel(uri.clone(), None) { - Ok(channel) => Ok(Self::new(channel).into()), + Ok(channel) => Ok(new_platform_client(channel).into()), Err(e) => Err(dapi_grpc::tonic::Status::invalid_argument(format!( "channel creation failed: {}", e @@ -50,7 +50,7 @@ impl TransportClient for PlatformGrpcClient { pool.get_or_create_with_generation(PoolPrefix::Platform, &uri, Some(settings), || { match create_channel(uri.clone(), Some(settings)) { Ok(channel) => { - let mut client = Self::new(channel); + let mut client = new_platform_client(channel); if let Some(max_size) = settings.max_decoding_message_size { client = client.max_decoding_message_size(max_size); } @@ -75,7 +75,7 @@ impl TransportClient for CoreGrpcClient { Ok(pool .get_or_create(PoolPrefix::Core, &uri, None, || { match create_channel(uri.clone(), None) { - Ok(channel) => Ok(Self::new(channel).into()), + Ok(channel) => Ok(new_core_client(channel).into()), Err(e) => Err(dapi_grpc::tonic::Status::invalid_argument(format!( "Channel creation failed: {}", e @@ -102,7 +102,7 @@ impl TransportClient for CoreGrpcClient { pool.get_or_create_with_generation(PoolPrefix::Core, &uri, Some(settings), || { match create_channel(uri.clone(), Some(settings)) { Ok(channel) => { - let mut client = Self::new(channel); + let mut client = new_core_client(channel); if let Some(max_size) = settings.max_decoding_message_size { client = client.max_decoding_message_size(max_size); } @@ -130,6 +130,13 @@ const OLDER_NODE_REQUEST_REFUSALS: [&str; 3] = [ "proved path queries can not be for limit 0", ]; +/// Start of the status tonic returns when a compressed response decompresses past the +/// client's `max_decoding_message_size`. The limit is the client's own, so every node +/// would fail the same way: this must not ban nodes like other `ResourceExhausted` +/// statuses (an uncompressed oversized response is `OutOfRange`, also not retried). +/// The text comes from tonic 0.14's `codec::decode`; re-check it on tonic upgrades. +const DECOMPRESS_LIMIT_ERROR: &str = "Error decompressing: size limit"; + impl CanRetry for dapi_grpc::tonic::Status { fn can_retry(&self) -> bool { // A failing SOCKS5 proxy fails every node alike: retrying elsewhere @@ -143,6 +150,10 @@ impl CanRetry for dapi_grpc::tonic::Status { use dapi_grpc::tonic::Code::*; + if code == ResourceExhausted && self.message().starts_with(DECOMPRESS_LIMIT_ERROR) { + return false; + } + if matches!(code, Unknown | Internal) && OLDER_NODE_REQUEST_REFUSALS .iter() diff --git a/packages/rs-dapi-client/src/transport/tonic_channel.rs b/packages/rs-dapi-client/src/transport/tonic_channel.rs index 7d311adb33c..05e7ec0e4f5 100644 --- a/packages/rs-dapi-client/src/transport/tonic_channel.rs +++ b/packages/rs-dapi-client/src/transport/tonic_channel.rs @@ -3,6 +3,7 @@ use super::TransportError; use crate::{request_settings::AppliedRequestSettings, Uri}; use dapi_grpc::core::v0::core_client::CoreClient; use dapi_grpc::platform::v0::platform_client::PlatformClient; +use dapi_grpc::tonic::codec::CompressionEncoding; use dapi_grpc::tonic::transport::{Certificate, Channel, ClientTlsConfig}; use std::error::Error as _; use std::sync::Arc; @@ -13,6 +14,19 @@ pub type PlatformGrpcClient = PlatformClient; /// Core Client using gRPC transport. pub type CoreGrpcClient = CoreClient; +/// Create a Platform client over `channel` that accepts gzip-compressed responses. +/// +/// Nodes that don't compress ignore the `grpc-accept-encoding` header, so this +/// is safe against every DAPI version. +pub fn new_platform_client(channel: Channel) -> PlatformGrpcClient { + PlatformClient::new(channel).accept_compressed(CompressionEncoding::Gzip) +} + +/// Create a Core client over `channel` that accepts gzip-compressed responses. +pub fn new_core_client(channel: Channel) -> CoreGrpcClient { + CoreClient::new(channel).accept_compressed(CompressionEncoding::Gzip) +} + /// backon::Sleeper // #[derive(Default, Clone, Debug)] pub type TokioBackonSleeper = backon::TokioSleeper; diff --git a/packages/rs-dapi-client/src/transport/wasm_channel.rs b/packages/rs-dapi-client/src/transport/wasm_channel.rs index 982aad8b70c..991d8d648ce 100644 --- a/packages/rs-dapi-client/src/transport/wasm_channel.rs +++ b/packages/rs-dapi-client/src/transport/wasm_channel.rs @@ -21,6 +21,17 @@ pub type PlatformGrpcClient = PlatformClient; /// Core Client using gRPC transport. pub type CoreGrpcClient = CoreClient; +/// Create a Platform client over `channel`. Browsers negotiate HTTP compression +/// themselves, and `grpc-accept-encoding` would fail the gateway's CORS preflight. +pub fn new_platform_client(channel: WasmClient) -> PlatformGrpcClient { + PlatformClient::new(channel) +} + +/// Create a Core client over `channel`. See [new_platform_client]. +pub fn new_core_client(channel: WasmClient) -> CoreGrpcClient { + CoreClient::new(channel) +} + /// Create a channel that will be used to communicate with the DAPI. pub fn create_channel( uri: Uri, diff --git a/packages/rs-dapi/doc/DESIGN.md b/packages/rs-dapi/doc/DESIGN.md index 9b31ab3065e..302913f3485 100644 --- a/packages/rs-dapi/doc/DESIGN.md +++ b/packages/rs-dapi/doc/DESIGN.md @@ -241,7 +241,7 @@ The Platform Service uses a modular structure where complex methods are separate Implementation notes: - Simple passthrough methods are generated by `drive_method!` with integrated LRU caching - `get_status`, `broadcast_state_transition`, `wait_for_state_transition_result`, and `subscribe_platform_events` are implemented as dedicated modules -- Drive client is configured with increased message size limits; compression is disabled at rs-dapi level (Envoy handles wire compression) +- Drive client is configured with increased message size limits; requests to Drive (on the same host) are not compressed #### Endpoints @@ -260,7 +260,7 @@ rs-dapi exposes a JSON-RPC gateway alongside gRPC. Axum powers JSON-RPC routing - Unit tests cover translation and error paths Operational notes: -- Compression: disabled at rs-dapi; Envoy handles edge compression +- Compression: rs-dapi gzip-compresses gRPC responses for clients that list gzip in `grpc-accept-encoding` (`server::grpc`); Envoy gzips grpc-web responses for clients that send `Accept-Encoding`. JSON-RPC responses are not compressed - Access logging: HTTP/JSON-RPC and gRPC traffic share the same access logging layer when configured, so all protocols emit uniform access entries - Platform event streaming is handled via a direct upstream proxy: @@ -426,7 +426,7 @@ The protocol translation layer is the key architectural component that enables u - **Direct Pass-through**: No translation required for native gRPC requests - **Metadata Preservation**: gRPC metadata and headers preserved - **Streaming Support**: Full bidirectional streaming support -- **Compression**: Native gRPC compression and optimization +- **Compression**: gzip responses when the client accepts them; compressed requests are not accepted ### 11. State Transition Processing diff --git a/packages/rs-dapi/src/clients/drive_client.rs b/packages/rs-dapi/src/clients/drive_client.rs index 8649054a8f7..a991f12d389 100644 --- a/packages/rs-dapi/src/clients/drive_client.rs +++ b/packages/rs-dapi/src/clients/drive_client.rs @@ -17,6 +17,8 @@ use tower_http::{ }; use tracing::{Level, debug, error, info, trace, warn}; +use crate::server::MAX_RESPONSE_BYTES; + /// gRPC client factory for interacting with Dash Platform Drive /// /// ## Cloning @@ -60,10 +62,10 @@ impl DriveClient { info!("Creating Drive client for: {}", uri); let channel = Self::create_channel(uri)?; - // Configure clients with larger message sizes. - // Compression (gzip) is intentionally DISABLED at rs-dapi level; Envoy handles it. - info!("Drive client compression: disabled (handled by Envoy)"); - const MAX_DECODING_BYTES: usize = 64 * 1024 * 1024; // 64 MiB + // Drive is local, so this hop is not compressed; responses to DAPI clients + // are. Their size limit applies after compression, so a Drive response is + // bounded here, before DAPI encodes or compresses it. + const MAX_DECODING_BYTES: usize = MAX_RESPONSE_BYTES; const MAX_ENCODING_BYTES: usize = 32 * 1024 * 1024; // 32 MiB let client = Self { @@ -166,10 +168,10 @@ impl DriveClient { pub(crate) fn from_test_channel(channel: DriveChannel) -> Self { Self { client: PlatformClient::new(channel.clone()) - .max_decoding_message_size(64 * 1024 * 1024) + .max_decoding_message_size(MAX_RESPONSE_BYTES) .max_encoding_message_size(32 * 1024 * 1024), internal_client: DriveInternalClient::new(channel) - .max_decoding_message_size(64 * 1024 * 1024) + .max_decoding_message_size(MAX_RESPONSE_BYTES) .max_encoding_message_size(32 * 1024 * 1024), base_url: Arc::new("http://drive.in-memory".to_owned()), } diff --git a/packages/rs-dapi/src/server/grpc.rs b/packages/rs-dapi/src/server/grpc.rs index 11e7c440dfa..8a32b17b3f8 100644 --- a/packages/rs-dapi/src/server/grpc.rs +++ b/packages/rs-dapi/src/server/grpc.rs @@ -10,11 +10,13 @@ use axum::http::{HeaderMap, Request, Response}; use dapi_grpc::core::v0::core_server::CoreServer; use dapi_grpc::platform::v0::platform_server::PlatformServer; use dapi_grpc::tonic::Status; +use dapi_grpc::tonic::codec::CompressionEncoding; use tower::layer::util::{Identity, Stack}; use tower::util::Either; use tower::{Layer, Service}; use super::DapiServer; +use crate::services::{CoreServiceImpl, PlatformServiceImpl}; /// Timeouts for regular requests - sync with envoy config if changed there const UNARY_TIMEOUT_SECS: u64 = 15; @@ -23,6 +25,44 @@ const STREAMING_TIMEOUT_SECS: u64 = 600; /// Safety margin to ensure we respond before client-side gRPC deadlines fire const GRPC_REQUEST_TIME_SAFETY_MARGIN: Duration = Duration::from_millis(50); +// Coarse DoS backstop on the *encoded* Platform request. Must stay +// strictly above every per-method app-layer budget plus protobuf +// envelope overhead, so that oversized-but-parseable requests reach +// the app-layer validators and get their precise errors instead of +// dying here with tonic's generic decode-limit status. The largest +// legitimate payloads are `getPathElements` (MAX_PATH_QUERY_BYTES, +// 64 KiB of raw components, ~65 KiB encoded) and broadcast state +// transitions (`max_state_transition_size`, 20 KiB). +const MAX_PLATFORM_DECODING_BYTES: usize = 128 * 1024; // 128 KiB +// Same principle for Core: sized above the largest app-layer budget +// (raw transaction wire cap, 400 KB) plus envelope overhead. +const MAX_CORE_DECODING_BYTES: usize = 512 * 1024; // 512 KiB +/// Largest response DAPI sends. With gzip on, tonic checks this against the +/// compressed message, so the Drive client bounds the raw response to it too. +pub(crate) const MAX_RESPONSE_BYTES: usize = 32 * 1024 * 1024; // 32 MiB + +/// Platform service with the message limits and response compression DAPI serves it with. +/// +/// Responses are gzip-compressed only for clients that list gzip in +/// `grpc-accept-encoding`; everyone else still gets them uncompressed. +/// Compressed requests are not accepted: requests are small, and refusing them +/// keeps decompression out of the request path. +pub(crate) fn platform_server(service: PlatformServiceImpl) -> PlatformServer { + PlatformServer::new(service) + .max_decoding_message_size(MAX_PLATFORM_DECODING_BYTES) + .max_encoding_message_size(MAX_RESPONSE_BYTES) + .send_compressed(CompressionEncoding::Gzip) +} + +/// Core service with the message limits and response compression DAPI serves it with. +/// See [platform_server]. +fn core_server(service: CoreServiceImpl) -> CoreServer { + CoreServer::new(service) + .max_decoding_message_size(MAX_CORE_DECODING_BYTES) + .max_encoding_message_size(MAX_RESPONSE_BYTES) + .send_compressed(CompressionEncoding::Gzip) +} + impl DapiServer { /// Start the unified gRPC server that exposes both Platform and Core services. /// Configures timeouts, message limits, optional access logging, and then awaits completion. @@ -37,20 +77,6 @@ impl DapiServer { let platform_service = self.platform_service.clone(); let core_service = self.core_service.clone(); - // Coarse DoS backstop on the *encoded* Platform request. Must stay - // strictly above every per-method app-layer budget plus protobuf - // envelope overhead, so that oversized-but-parseable requests reach - // the app-layer validators and get their precise errors instead of - // dying here with tonic's generic decode-limit status. The largest - // legitimate payloads are `getPathElements` (MAX_PATH_QUERY_BYTES, - // 64 KiB of raw components, ~65 KiB encoded) and broadcast state - // transitions (`max_state_transition_size`, 20 KiB). - const MAX_PLATFORM_DECODING_BYTES: usize = 128 * 1024; // 128 KiB - // Same principle for Core: sized above the largest app-layer budget - // (raw transaction wire cap, 400 KB) plus envelope overhead. - const MAX_CORE_DECODING_BYTES: usize = 512 * 1024; // 512 KiB - const MAX_ENCODING_BYTES: usize = 32 * 1024 * 1024; // 32 MiB - let builder = dapi_grpc::tonic::transport::Server::builder() .tcp_keepalive(Some(Duration::from_secs(25))) .timeout(Duration::from_secs( @@ -75,16 +101,8 @@ impl DapiServer { let mut builder = builder.layer(combined_layer); builder - .add_service( - PlatformServer::new(platform_service) - .max_decoding_message_size(MAX_PLATFORM_DECODING_BYTES) - .max_encoding_message_size(MAX_ENCODING_BYTES), - ) - .add_service( - CoreServer::new(core_service) - .max_decoding_message_size(MAX_CORE_DECODING_BYTES) - .max_encoding_message_size(MAX_ENCODING_BYTES), - ) + .add_service(platform_server(platform_service)) + .add_service(core_server(core_service)) .serve(addr) .await?; @@ -283,4 +301,87 @@ mod tests { status.message() ); } + + /// `core_server` is a separate constructor from `platform_server`, so its + /// compression needs its own check. Nothing runs behind the clients: + /// `getBlockchainStatus` still answers, with what it could not fetch left empty. + #[tokio::test] + async fn should_gzip_core_responses_for_clients_that_accept_gzip() { + use crate::clients::{CoreClient, DriveClient, TenderdashClient}; + use crate::config::Config; + use crate::services::StreamingServiceImpl; + use dapi_grpc::core::v0::GetBlockchainStatusRequest; + use dapi_grpc::core::v0::core_client::CoreClient as CoreGrpcClient; + use dapi_grpc::tonic::transport::{Endpoint, Server, Uri}; + use hyper_util::rt::TokioIo; + use rs_dapi_client::transport::new_core_client; + use std::sync::Arc; + use tokio_stream::StreamExt; + use tower::ServiceBuilder; + use tower_http::trace::TraceLayer; + use zeroize::Zeroizing; + + let config = Arc::new(Config::default()); + let unreachable = "http://127.0.0.1:1"; + let core_client = CoreClient::new( + unreachable.to_owned(), + "user".to_owned(), + Zeroizing::new("pass".to_owned()), + 1024, + ) + .expect("Core RPC client"); + let drive_client = DriveClient::from_test_channel( + ServiceBuilder::new() + .layer(TraceLayer::new_for_http()) + .service(Endpoint::from_static(unreachable).connect_lazy()), + ); + let streaming = StreamingServiceImpl::new( + drive_client, + Arc::new(TenderdashClient::without_test_connection()), + core_client.clone(), + Arc::clone(&config), + ) + .expect("streaming service"); + let service = CoreServiceImpl::new(Arc::new(streaming), config, core_client).await; + + let (client_io, server_io) = tokio::io::duplex(64 * 1024); + let server = tokio::spawn(async move { + let incoming = tokio_stream::iter([Ok::<_, std::io::Error>(server_io)]) + .chain(tokio_stream::pending()); + Server::builder() + .add_service(core_server(service)) + .serve_with_incoming(incoming) + .await + .expect("serve Core service"); + }); + let mut io = Some(client_io); + let channel = Endpoint::from_static("http://dapi.in-memory") + .timeout(Duration::from_secs(5)) + .connect_with_connector_lazy(tower::service_fn(move |_: Uri| { + let io = io.take(); + async move { + io.map(TokioIo::new) + .ok_or_else(|| std::io::Error::other("single-use fixture channel")) + } + })); + + let plain = CoreGrpcClient::new(channel.clone()) + .get_blockchain_status(GetBlockchainStatusRequest {}) + .await + .expect("client without grpc-accept-encoding"); + assert_eq!(plain.metadata().get("grpc-encoding"), None); + + let compressed = new_core_client(channel) + .get_blockchain_status(GetBlockchainStatusRequest {}) + .await + .expect("rs-dapi-client accepts gzip"); + assert_eq!( + compressed + .metadata() + .get("grpc-encoding") + .expect("gzip response"), + "gzip" + ); + server.abort(); + } } diff --git a/packages/rs-dapi/src/server/mod.rs b/packages/rs-dapi/src/server/mod.rs index 0cb0023d787..2abc76ad2a3 100644 --- a/packages/rs-dapi/src/server/mod.rs +++ b/packages/rs-dapi/src/server/mod.rs @@ -3,6 +3,11 @@ mod jsonrpc; mod metrics; mod state; +pub(crate) use grpc::MAX_RESPONSE_BYTES; + +#[cfg(test)] +pub(crate) use grpc::platform_server; + use futures::FutureExt; use std::sync::Arc; use tracing::{error, info}; diff --git a/packages/rs-dapi/src/services/platform_service/protobuf_request_errors.rs b/packages/rs-dapi/src/services/platform_service/protobuf_request_errors.rs index cdb1df6b2e3..5c60f4d65f6 100644 --- a/packages/rs-dapi/src/services/platform_service/protobuf_request_errors.rs +++ b/packages/rs-dapi/src/services/platform_service/protobuf_request_errors.rs @@ -5,6 +5,7 @@ use crate::cache::LruResponseCache; use crate::clients::TenderdashClient; use crate::clients::drive_client::{DriveChannel, DriveClient}; use crate::config::Config; +use crate::server::{MAX_RESPONSE_BYTES, platform_server}; use crate::services::streaming_service::SubscriberManager; use crate::sync::Workers; use bytes::{BufMut, Bytes}; @@ -16,7 +17,6 @@ use dapi_grpc::platform::v0::get_data_contracts_response::{ DataContractEntry, DataContracts, GetDataContractsResponseV0, Version, }; use dapi_grpc::platform::v0::platform_client::PlatformClient; -use dapi_grpc::platform::v0::platform_server::PlatformServer; use dapi_grpc::platform::v0::{GetDataContractsRequest, GetDataContractsResponse}; use dapi_grpc::tonic::client::Grpc; use dapi_grpc::tonic::codec::{BufferSettings, Codec, EncodeBuf, Encoder}; @@ -29,6 +29,7 @@ use dapi_grpc::tonic_prost::{ProstCodec, ProstDecoder}; use hyper_util::rt::TokioIo; use rs_dapi_client::transport::{ AppliedRequestSettings, BoxFuture, TransportClient, TransportError, TransportRequest, + new_platform_client, }; use rs_dapi_client::{ AddressList, CanRetry, ConnectionPool, DapiClient, DapiClientError, DapiRequestExecutor, @@ -46,6 +47,10 @@ use tower_http::trace::TraceLayer; #[derive(Clone, Copy)] enum DriveReply { Valid, + /// A valid response whose contract bytes compress almost to nothing. + Compressible, + /// Like `Compressible`, but larger than DAPI may send uncompressed. + Oversized, Corrupt, Fault(Code), } @@ -123,20 +128,27 @@ impl Harness { let mut stream = response .send_response(headers.body(()).expect("response"), false) .expect("send headers"); + let data_contract = match reply { + DriveReply::Compressible => Some(vec![0; 32 * 1024]), + DriveReply::Oversized => Some(vec![0; MAX_RESPONSE_BYTES + 1]), + _ => None, + }; let payload = match reply { DriveReply::Corrupt => vec![0], - DriveReply::Valid => GetDataContractsResponse { - version: Some(Version::V0(GetDataContractsResponseV0 { - result: Some(ContractsResult::DataContracts(DataContracts { - data_contract_entries: vec![DataContractEntry { - identifier: vec![1; 32], - data_contract: None, - }], + DriveReply::Valid | DriveReply::Compressible | DriveReply::Oversized => { + GetDataContractsResponse { + version: Some(Version::V0(GetDataContractsResponseV0 { + result: Some(ContractsResult::DataContracts(DataContracts { + data_contract_entries: vec![DataContractEntry { + identifier: vec![1; 32], + data_contract, + }], + })), + metadata: None, })), - metadata: None, - })), + } + .encode_to_vec() } - .encode_to_vec(), DriveReply::Fault(_) => unreachable!(), }; stream @@ -176,7 +188,7 @@ impl Harness { let incoming = tokio_stream::iter([Ok::<_, std::io::Error>(server_io)]) .chain(tokio_stream::pending()); Server::builder() - .add_service(PlatformServer::new(service).max_decoding_message_size(128 * 1024)) + .add_service(platform_server(service)) .serve_with_incoming(incoming) .await .expect("serve actual DAPI service"); @@ -206,14 +218,18 @@ impl Harness { async fn valid_request(&self) -> Result, Status> { PlatformClient::new(self.channel.clone()) - .get_data_contracts(GetDataContractsRequest::from(GetDataContractsRequestV0 { - ids: vec![vec![1; 32]], - prove: false, - })) + .get_data_contracts(valid_contracts_request()) .await } } +fn valid_contracts_request() -> GetDataContractsRequest { + GetDataContractsRequest::from(GetDataContractsRequestV0 { + ids: vec![vec![1; 32]], + prove: false, + }) +} + struct PayloadCodec(PhantomData); struct PayloadEncoder; @@ -318,6 +334,63 @@ async fn should_preserve_actual_dapi_request_cap_and_node_faults() { } } +#[tokio::test] +async fn should_gzip_responses_only_for_clients_that_accept_gzip() { + let harness = Harness::new(DriveReply::Valid).await; + let plain = harness + .valid_request() + .await + .expect("client without grpc-accept-encoding"); + assert_eq!(plain.metadata().get("grpc-encoding"), None); + + let compressed = new_platform_client(harness.channel.clone()) + .get_data_contracts(valid_contracts_request()) + .await + .expect("rs-dapi-client accepts gzip"); + assert_eq!( + compressed + .metadata() + .get("grpc-encoding") + .expect("gzip response"), + "gzip" + ); + assert_eq!(compressed.into_inner(), plain.into_inner()); +} + +#[tokio::test] +async fn should_not_ban_nodes_for_a_gzip_response_over_the_client_limit() { + let harness = Harness::new(DriveReply::Compressible).await; + // The compressed frame fits the limit; the decompressed message does not. + let status = new_platform_client(harness.channel.clone()) + .max_decoding_message_size(1024) + .get_data_contracts(valid_contracts_request()) + .await + .expect_err("response decompresses past the client limit"); + assert_eq!(status.code(), Code::ResourceExhausted); + assert!( + !status.can_retry(), + "the limit is the client's own, every node fails alike: {status:?}" + ); +} + +#[tokio::test] +async fn should_refuse_a_drive_response_over_the_limit_even_if_it_compresses() { + // tonic checks the response limit after compression, so the raw size has to be + // bounded where DAPI decodes the Drive response. + let harness = Harness::new(DriveReply::Oversized).await; + let status = new_platform_client(harness.channel.clone()) + .max_decoding_message_size(usize::MAX) + .get_data_contracts(valid_contracts_request()) + .await + .expect_err("response larger than DAPI may send"); + assert!( + status + .message() + .contains("decoded message length too large"), + "refused when DAPI decodes the Drive response: {status:?}" + ); +} + #[derive(Debug)] struct PoolNode { uri: Uri,