From 190269f616c373705ef5663df30318d0f28897e7 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Tue, 8 Jul 2025 11:55:52 +0000 Subject: [PATCH] Simplify Pdu Hashes struct. Signed-off-by: Jason Volk --- src/api/client/sync/v3.rs | 2 +- src/core/matrix/pdu.rs | 9 ++------- src/core/matrix/pdu/hashes.rs | 12 ++++++++++++ src/core/matrix/state_res/benches.rs | 2 +- src/core/matrix/state_res/test_utils.rs | 4 ++-- src/service/rooms/timeline/create.rs | 2 +- 6 files changed, 19 insertions(+), 12 deletions(-) create mode 100644 src/core/matrix/pdu/hashes.rs diff --git a/src/api/client/sync/v3.rs b/src/api/client/sync/v3.rs index a192a44e..7fecf59a 100644 --- a/src/api/client/sync/v3.rs +++ b/src/api/client/sync/v3.rs @@ -458,7 +458,7 @@ async fn handle_left_room( depth: uint!(1), auth_events: vec![], redacts: None, - hashes: EventHash { sha256: String::new() }, + hashes: EventHash::default(), signatures: None, }; diff --git a/src/core/matrix/pdu.rs b/src/core/matrix/pdu.rs index cce9e286..9d4975b5 100644 --- a/src/core/matrix/pdu.rs +++ b/src/core/matrix/pdu.rs @@ -1,5 +1,6 @@ mod builder; mod count; +mod hashes; mod id; mod raw_id; mod redact; @@ -20,6 +21,7 @@ pub use self::{ Count as PduCount, Id as PduId, Pdu as PduEvent, RawId as RawPduId, builder::{Builder, Builder as PduBuilder}, count::Count, + hashes::EventHashes as EventHash, id::{ShortId, *}, raw_id::*, }; @@ -67,13 +69,6 @@ pub struct Pdu { pub signatures: Option>, } -/// Content hashes of a PDU. -#[derive(Clone, Debug, Deserialize, Serialize)] -pub struct EventHash { - /// The SHA-256 hash. - pub sha256: String, -} - impl Pdu { pub fn from_id_val(event_id: &EventId, mut json: CanonicalJsonObject) -> Result { let event_id = CanonicalJsonValue::String(event_id.into()); diff --git a/src/core/matrix/pdu/hashes.rs b/src/core/matrix/pdu/hashes.rs new file mode 100644 index 00000000..8f2fee8c --- /dev/null +++ b/src/core/matrix/pdu/hashes.rs @@ -0,0 +1,12 @@ +use serde::{Deserialize, Serialize}; + +use crate::arrayvec::ArrayString; + +/// Content hashes of a PDU. +#[derive(Clone, Debug, Default, Deserialize, Serialize)] +pub struct EventHashes { + /// The SHA-256 hash. + pub sha256: ArrayString, +} + +const SHA256_LEN: usize = 43; diff --git a/src/core/matrix/state_res/benches.rs b/src/core/matrix/state_res/benches.rs index dce91c23..1da4da40 100644 --- a/src/core/matrix/state_res/benches.rs +++ b/src/core/matrix/state_res/benches.rs @@ -418,7 +418,7 @@ where auth_events, prev_events, depth: uint!(0), - hashes: EventHash { sha256: String::new() }, + hashes: EventHash::default(), signatures: None, } } diff --git a/src/core/matrix/state_res/test_utils.rs b/src/core/matrix/state_res/test_utils.rs index 1c886c5b..91f28455 100644 --- a/src/core/matrix/state_res/test_utils.rs +++ b/src/core/matrix/state_res/test_utils.rs @@ -426,7 +426,7 @@ pub(crate) fn to_init_pdu_event( auth_events: vec![], prev_events: vec![], depth: uint!(0), - hashes: EventHash { sha256: "".to_owned() }, + hashes: EventHash::default(), signatures: None, } } @@ -474,7 +474,7 @@ where auth_events, prev_events, depth: uint!(0), - hashes: EventHash { sha256: "".to_owned() }, + hashes: EventHash::default(), signatures: None, } } diff --git a/src/service/rooms/timeline/create.rs b/src/service/rooms/timeline/create.rs index f2ad8ce8..d1ce68f7 100644 --- a/src/service/rooms/timeline/create.rs +++ b/src/service/rooms/timeline/create.rs @@ -127,7 +127,7 @@ pub async fn create_hash_and_sign_event( depth, redacts, unsigned, - hashes: EventHash { sha256: "aaa".to_owned() }, + hashes: EventHash::default(), signatures: None, prev_events, auth_events: auth_events