From 111a871d19042b7e547d66f7a3e8f486b9b5b910 Mon Sep 17 00:00:00 2001 From: Leo Eichhorn Date: Thu, 16 Jan 2025 11:04:53 +0000 Subject: [PATCH 1/6] fix --- rs/types/types/src/batch.rs | 53 ++++++++++++++++++--- rs/types/types/src/batch/ingress.rs | 5 +- rs/types/types/src/batch/self_validating.rs | 3 +- rs/types/types/src/batch/xnet.rs | 6 +++ 4 files changed, 59 insertions(+), 8 deletions(-) diff --git a/rs/types/types/src/batch.rs b/rs/types/types/src/batch.rs index 7676e5d084e..0ae2c96cd8e 100644 --- a/rs/types/types/src/batch.rs +++ b/rs/types/types/src/batch.rs @@ -165,10 +165,19 @@ impl BatchPayload { } pub fn is_empty(&self) -> bool { - self.ingress.is_empty() - && self.xnet.stream_slices.is_empty() - && self.self_validating.is_empty() - && self.canister_http.is_empty() + let BatchPayload { + ingress, + xnet, + self_validating, + canister_http, + query_stats, + } = &self; + + ingress.is_empty() + && xnet.is_empty() + && self_validating.is_empty() + && canister_http.is_empty() + && query_stats.is_empty() } } @@ -241,12 +250,44 @@ mod tests { use super::*; use crate::CountBytes; + /// This is a quick test to check the invariant, that the [`Default`] implementation + /// of a payload section actually produces the empty payload, + #[test] + fn default_batch_payload_is_zero_bytes() { + let BatchPayload { + ingress, + xnet: _, // No implementation of `CountBytes` for xnet. + self_validating, + canister_http, + query_stats, + } = BatchPayload::default(); + + assert_eq!(ingress.count_bytes(), 0); + assert_eq!(self_validating.count_bytes(), 0); + assert_eq!(canister_http.len(), 0); + assert_eq!(query_stats.len(), 0); + } + /// This is a quick test to check the invariant, that the [`Default`] implementation /// of a payload section actually produces the empty payload, #[test] fn default_batch_payload_is_empty() { - assert_eq!(IngressPayload::default().count_bytes(), 0); - assert_eq!(SelfValidatingPayload::default().count_bytes(), 0); + let payload = BatchPayload::default(); + assert!(payload.is_empty()); + + let BatchPayload { + ingress, + xnet, + self_validating, + canister_http, + query_stats, + } = &payload; + + assert!(ingress.is_empty()); + assert!(xnet.is_empty()); + assert!(self_validating.is_empty()); + assert!(canister_http.is_empty()); + assert!(query_stats.is_empty()); } #[test] diff --git a/rs/types/types/src/batch/ingress.rs b/rs/types/types/src/batch/ingress.rs index e1379a92870..ba4e2bf3a55 100644 --- a/rs/types/types/src/batch/ingress.rs +++ b/rs/types/types/src/batch/ingress.rs @@ -86,7 +86,10 @@ impl IngressPayload { /// Return true if the payload is empty. pub fn is_empty(&self) -> bool { - self.serialized_ingress_messages.is_empty() + let IngressPayload { + serialized_ingress_messages, + } = &self; + serialized_ingress_messages.is_empty() } /// Return the [`SignedIngress`] referenced by the [`IngressMessageId`]. diff --git a/rs/types/types/src/batch/self_validating.rs b/rs/types/types/src/batch/self_validating.rs index ca97598690f..1e3a8992314 100644 --- a/rs/types/types/src/batch/self_validating.rs +++ b/rs/types/types/src/batch/self_validating.rs @@ -35,7 +35,8 @@ impl SelfValidatingPayload { /// Returns true if the payload is empty pub fn is_empty(&self) -> bool { - self.0.is_empty() + let SelfValidatingPayload(responses) = &self; + responses.is_empty() } } diff --git a/rs/types/types/src/batch/xnet.rs b/rs/types/types/src/batch/xnet.rs index 317de7f135a..f435a7c64d0 100644 --- a/rs/types/types/src/batch/xnet.rs +++ b/rs/types/types/src/batch/xnet.rs @@ -72,4 +72,10 @@ impl XNetPayload { }) .sum() } + + /// Returns true if the payload is empty + pub fn is_empty(&self) -> bool { + let XNetPayload { stream_slices } = &self; + stream_slices.is_empty() + } } From bd76daede4f744402ec130e422519b07e37b5b21 Mon Sep 17 00:00:00 2001 From: Leo Eichhorn Date: Thu, 16 Jan 2025 11:24:23 +0000 Subject: [PATCH 2/6] count_bytes --- rs/bitcoin/replica_types/src/lib.rs | 26 ++++++++++++--------- rs/types/types/src/batch/ingress.rs | 5 +++- rs/types/types/src/batch/self_validating.rs | 3 ++- rs/types/types/src/canister_http.rs | 25 ++++++++++++++++---- 4 files changed, 41 insertions(+), 18 deletions(-) diff --git a/rs/bitcoin/replica_types/src/lib.rs b/rs/bitcoin/replica_types/src/lib.rs index 5213797bafe..ba18ae2626e 100644 --- a/rs/bitcoin/replica_types/src/lib.rs +++ b/rs/bitcoin/replica_types/src/lib.rs @@ -414,6 +414,7 @@ impl From for SendTransactionResponse { impl SendTransactionResponse { /// Returns the size of this `SendTransactionResponse` in bytes. pub fn count_bytes(&self) -> usize { + let SendTransactionResponse {} = &self; 0 } } @@ -429,7 +430,11 @@ pub struct BitcoinReject { impl BitcoinReject { /// Returns the size of this `RejectResponse` in bytes. pub fn count_bytes(&self) -> usize { - size_of_val(&self.reject_code) + self.message.len() + let BitcoinReject { + reject_code, + message, + } = &self; + size_of_val(reject_code) + message.len() } } @@ -598,7 +603,11 @@ impl TryFrom for BitcoinAdapterResponse { impl BitcoinAdapterResponse { /// Returns the size of this `BitcoinAdapterResponse` in bytes. pub fn count_bytes(&self) -> usize { - self.response.count_bytes() + std::mem::size_of::() + let BitcoinAdapterResponse { + response, + callback_id: _, + } = &self; + response.count_bytes() + std::mem::size_of::() } } @@ -718,15 +727,10 @@ pub struct GetSuccessorsResponseComplete { impl GetSuccessorsResponseComplete { /// Returns the size of this `SendTransactionResponse` in bytes. pub fn count_bytes(&self) -> usize { - self.count_blocks_bytes() + self.count_next_bytes() - } - - pub fn count_blocks_bytes(&self) -> usize { - self.blocks.iter().map(|b| b.len()).sum::() - } - - pub fn count_next_bytes(&self) -> usize { - self.next.iter().map(|n| n.len()).sum::() + let GetSuccessorsResponseComplete { blocks, next } = &self; + let blocks_bytes = blocks.iter().map(|b| b.len()).sum::(); + let next_bytes = next.iter().map(|n| n.len()).sum::(); + blocks_bytes + next_bytes } } diff --git a/rs/types/types/src/batch/ingress.rs b/rs/types/types/src/batch/ingress.rs index ba4e2bf3a55..154a110ac77 100644 --- a/rs/types/types/src/batch/ingress.rs +++ b/rs/types/types/src/batch/ingress.rs @@ -124,7 +124,10 @@ impl IngressPayload { impl CountBytes for IngressPayload { fn count_bytes(&self) -> usize { - self.serialized_ingress_messages + let IngressPayload { + serialized_ingress_messages, + } = &self; + serialized_ingress_messages .values() .map(|message| EXPECTED_MESSAGE_ID_LENGTH + message.len()) .sum() diff --git a/rs/types/types/src/batch/self_validating.rs b/rs/types/types/src/batch/self_validating.rs index 1e3a8992314..30f25975a84 100644 --- a/rs/types/types/src/batch/self_validating.rs +++ b/rs/types/types/src/batch/self_validating.rs @@ -62,6 +62,7 @@ impl TryFrom for SelfValidatingPayload { impl CountBytes for SelfValidatingPayload { fn count_bytes(&self) -> usize { - self.0.iter().map(|x| x.count_bytes()).sum() + let SelfValidatingPayload(responses) = &self; + responses.iter().map(|x| x.count_bytes()).sum() } } diff --git a/rs/types/types/src/canister_http.rs b/rs/types/types/src/canister_http.rs index ac019d51355..144b3ee865c 100644 --- a/rs/types/types/src/canister_http.rs +++ b/rs/types/types/src/canister_http.rs @@ -53,7 +53,7 @@ use ic_error_types::{ErrorCode, RejectCode, UserError}; #[cfg(test)] use ic_exhaustive_derive::ExhaustiveSet; use ic_management_canister_types::{ - CanisterHttpRequestArgs, HttpHeader, HttpMethod, TransformContext, + CanisterHttpRequestArgs, DataSize, HttpHeader, HttpMethod, TransformContext, }; use ic_protobuf::{ proxy::{try_from_option_field, ProxyDecodeError}, @@ -471,7 +471,16 @@ pub struct CanisterHttpResponse { impl CountBytes for CanisterHttpResponse { fn count_bytes(&self) -> usize { - size_of::() + size_of::