mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-17 07:41:23 +01:00
Take iterator in calculate_hash()
Avoids unnecessary allocations.
This commit is contained in:
parent
cce83beedb
commit
f1642c92d1
5 changed files with 24 additions and 33 deletions
|
|
@ -1860,7 +1860,7 @@ pub(crate) async fn create_invite_route(
|
||||||
events: vec![pdu.to_room_event()],
|
events: vec![pdu.to_room_event()],
|
||||||
txn_id:
|
txn_id:
|
||||||
base64::engine::general_purpose::URL_SAFE_NO_PAD
|
base64::engine::general_purpose::URL_SAFE_NO_PAD
|
||||||
.encode(utils::calculate_hash(&[pdu
|
.encode(utils::calculate_hash([pdu
|
||||||
.event_id()
|
.event_id()
|
||||||
.as_bytes()]))
|
.as_bytes()]))
|
||||||
.into(),
|
.into(),
|
||||||
|
|
|
||||||
|
|
@ -133,9 +133,8 @@ impl Service {
|
||||||
let previous_shortstatehash =
|
let previous_shortstatehash =
|
||||||
self.db.get_room_shortstatehash(room_id)?;
|
self.db.get_room_shortstatehash(room_id)?;
|
||||||
|
|
||||||
let state_hash = calculate_hash(
|
let state_hash =
|
||||||
&state_ids_compressed.iter().map(|s| &s[..]).collect::<Vec<_>>(),
|
calculate_hash(state_ids_compressed.iter().map(|s| &s[..]));
|
||||||
);
|
|
||||||
|
|
||||||
let (shortstatehash, already_existed) =
|
let (shortstatehash, already_existed) =
|
||||||
services().rooms.short.get_or_create_shortstatehash(&state_hash)?;
|
services().rooms.short.get_or_create_shortstatehash(&state_hash)?;
|
||||||
|
|
|
||||||
|
|
@ -283,10 +283,7 @@ impl Service {
|
||||||
services().rooms.state.get_room_shortstatehash(room_id)?;
|
services().rooms.state.get_room_shortstatehash(room_id)?;
|
||||||
|
|
||||||
let state_hash = utils::calculate_hash(
|
let state_hash = utils::calculate_hash(
|
||||||
&new_state_ids_compressed
|
new_state_ids_compressed.iter().map(|bytes| &bytes[..]),
|
||||||
.iter()
|
|
||||||
.map(|bytes| &bytes[..])
|
|
||||||
.collect::<Vec<_>>(),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
let (new_shortstatehash, already_existed) =
|
let (new_shortstatehash, already_existed) =
|
||||||
|
|
|
||||||
|
|
@ -755,17 +755,10 @@ async fn handle_appservice_event(
|
||||||
appservice::event::push_events::v1::Request {
|
appservice::event::push_events::v1::Request {
|
||||||
events: pdu_jsons,
|
events: pdu_jsons,
|
||||||
txn_id: general_purpose::URL_SAFE_NO_PAD
|
txn_id: general_purpose::URL_SAFE_NO_PAD
|
||||||
.encode(calculate_hash(
|
.encode(calculate_hash(events.iter().map(|e| match e {
|
||||||
&events
|
SendingEventType::Edu(b) => b.json().get().as_bytes(),
|
||||||
.iter()
|
SendingEventType::Pdu(b) => b.as_bytes(),
|
||||||
.map(|e| match e {
|
})))
|
||||||
SendingEventType::Edu(b) => {
|
|
||||||
b.json().get().as_bytes()
|
|
||||||
}
|
|
||||||
SendingEventType::Pdu(b) => b.as_bytes(),
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>(),
|
|
||||||
))
|
|
||||||
.into(),
|
.into(),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
@ -902,17 +895,10 @@ async fn handle_federation_event(
|
||||||
edus: edu_jsons,
|
edus: edu_jsons,
|
||||||
origin_server_ts: MilliSecondsSinceUnixEpoch::now(),
|
origin_server_ts: MilliSecondsSinceUnixEpoch::now(),
|
||||||
transaction_id: general_purpose::URL_SAFE_NO_PAD
|
transaction_id: general_purpose::URL_SAFE_NO_PAD
|
||||||
.encode(calculate_hash(
|
.encode(calculate_hash(events.iter().map(|e| match e {
|
||||||
&events
|
SendingEventType::Edu(b) => b.json().get().as_bytes(),
|
||||||
.iter()
|
SendingEventType::Pdu(b) => b.as_bytes(),
|
||||||
.map(|e| match e {
|
})))
|
||||||
SendingEventType::Edu(b) => {
|
|
||||||
b.json().get().as_bytes()
|
|
||||||
}
|
|
||||||
SendingEventType::Pdu(b) => b.as_bytes(),
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>(),
|
|
||||||
))
|
|
||||||
.into(),
|
.into(),
|
||||||
},
|
},
|
||||||
false,
|
false,
|
||||||
|
|
|
||||||
15
src/utils.rs
15
src/utils.rs
|
|
@ -106,9 +106,18 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tracing::instrument(skip(keys))]
|
#[tracing::instrument(skip(keys))]
|
||||||
pub(crate) fn calculate_hash(keys: &[&[u8]]) -> Vec<u8> {
|
pub(crate) fn calculate_hash<'a, I, T>(keys: I) -> Vec<u8>
|
||||||
// We only hash the pdu's event ids, not the whole pdu
|
where
|
||||||
let bytes = keys.join(&0xFF);
|
I: IntoIterator<Item = T>,
|
||||||
|
T: AsRef<[u8]>,
|
||||||
|
{
|
||||||
|
let mut bytes = Vec::new();
|
||||||
|
for (i, key) in keys.into_iter().enumerate() {
|
||||||
|
if i != 0 {
|
||||||
|
bytes.push(0xFF);
|
||||||
|
}
|
||||||
|
bytes.extend_from_slice(key.as_ref());
|
||||||
|
}
|
||||||
let hash = digest::digest(&digest::SHA256, &bytes);
|
let hash = digest::digest(&digest::SHA256, &bytes);
|
||||||
hash.as_ref().to_owned()
|
hash.as_ref().to_owned()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue