Skip to content

Commit

Permalink
Compress state response message in state syncing
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchengxu committed Aug 14, 2024
1 parent 0cd577b commit 2556fef
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
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.

1 change: 1 addition & 0 deletions substrate/client/network/sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ smallvec = { workspace = true, default-features = true }
thiserror = { workspace = true }
tokio-stream = { workspace = true }
tokio = { features = ["macros", "time"], workspace = true, default-features = true }
zstd = { workspace = true }
fork-tree = { workspace = true, default-features = true }
prometheus-endpoint = { workspace = true, default-features = true }
sc-client-api = { workspace = true, default-features = true }
Expand Down
6 changes: 4 additions & 2 deletions substrate/client/network/sync/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1207,8 +1207,10 @@ where
Ok(request.encode_to_vec())
}

fn decode_state_response(response: &[u8]) -> Result<OpaqueStateResponse, String> {
let response = StateResponse::decode(response)
fn decode_state_response(compressed_response: &[u8]) -> Result<OpaqueStateResponse, String> {
let response = zstd::stream::decode_all(compressed_response)
.map_err(|error| format!("Failed to decompress state response: {error}"))?;
let response = StateResponse::decode(response.as_slice())
.map_err(|error| format!("Failed to decode state response: {error}"))?;

Ok(OpaqueStateResponse(Box::new(response)))
Expand Down
9 changes: 8 additions & 1 deletion substrate/client/network/sync/src/state_request_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,11 @@ where

let mut data = Vec::with_capacity(response.encoded_len());
response.encode(&mut data)?;
Ok(data)

let compressed_response = zstd::stream::encode_all(data.as_slice(), 0)
.map_err(HandleRequestError::Compress)?;

Ok(compressed_response)
} else {
Err(())
};
Expand All @@ -291,4 +295,7 @@ enum HandleRequestError {

#[error("Failed to send response.")]
SendResponse,

#[error("Failed to compress response: {0}.")]
Compress(std::io::Error),
}

0 comments on commit 2556fef

Please sign in to comment.