Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/dapi-grpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 23 additions & 0 deletions packages/dashmate/templates/platform/gateway/envoy.yaml.dot
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions packages/dashmate/test/unit/templates/envoyTemplate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
'application/grpc-web',
'application/grpc-web+proto',
'application/grpc-web-text',
'application/grpc-web-text+proto',
]);
});
});
6 changes: 4 additions & 2 deletions packages/rs-dapi-client/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
21 changes: 16 additions & 5 deletions packages/rs-dapi-client/src/transport/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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
Expand All @@ -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);
}
Expand All @@ -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
Expand All @@ -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);
}
Expand Down Expand Up @@ -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";
Comment thread
PastaPastaPasta marked this conversation as resolved.
Resolved

impl CanRetry for dapi_grpc::tonic::Status {
fn can_retry(&self) -> bool {
// A failing SOCKS5 proxy fails every node alike: retrying elsewhere
Expand All @@ -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;
}
Comment thread
PastaPastaPasta marked this conversation as resolved.
Resolved

if matches!(code, Unknown | Internal)
&& OLDER_NODE_REQUEST_REFUSALS
.iter()
Expand Down
14 changes: 14 additions & 0 deletions packages/rs-dapi-client/src/transport/tonic_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,6 +14,19 @@ pub type PlatformGrpcClient = PlatformClient<Channel>;
/// Core Client using gRPC transport.
pub type CoreGrpcClient = CoreClient<Channel>;

/// 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;
Expand Down
11 changes: 11 additions & 0 deletions packages/rs-dapi-client/src/transport/wasm_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ pub type PlatformGrpcClient = PlatformClient<WasmClient>;
/// Core Client using gRPC transport.
pub type CoreGrpcClient = CoreClient<WasmClient>;

/// 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,
Expand Down
6 changes: 3 additions & 3 deletions packages/rs-dapi/doc/DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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

Expand Down
14 changes: 8 additions & 6 deletions packages/rs-dapi/src/clients/drive_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()),
}
Expand Down
Loading
Loading