From 8eb2f5cec63366aa608b92def97b84426065e5e1 Mon Sep 17 00:00:00 2001 From: ameya-signal <163888879+ameya-signal@users.noreply.github.com> Date: Thu, 3 Sep 2026 09:15:55 -0700 Subject: [PATCH] Use content hash to compare message envelopes --- .../storage/MirroringMessageStream.java | 36 ++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/MirroringMessageStream.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/MirroringMessageStream.java index babd3be15..8192ba769 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/MirroringMessageStream.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/MirroringMessageStream.java @@ -5,9 +5,14 @@ package org.whispersystems.textsecuregcm.storage; +import com.google.common.hash.HashCode; +import com.google.common.hash.Hashing; +import com.google.protobuf.CodedOutputStream; import io.micrometer.core.instrument.Counter; import io.micrometer.core.instrument.Metrics; import io.micrometer.core.instrument.Tags; +import java.io.IOException; +import java.io.UncheckedIOException; import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashMap; @@ -55,8 +60,8 @@ public class MirroringMessageStream implements MessageStream { private static final int MAX_WINDOW_SIZE = 1024; - private final Map redisDynamoMessageWindow = new HashMap<>(); - private final Map foundationDbMessageWindow = new HashMap<>(); + private final Map redisDynamoMessageWindow = new HashMap<>(); + private final Map foundationDbMessageWindow = new HashMap<>(); private boolean stopAgreementVerification = false; @@ -238,8 +243,8 @@ public class MirroringMessageStream implements MessageStream { return; } - final Map thisStreamWindow; - final Map otherStreamWindow; + final Map thisStreamWindow; + final Map otherStreamWindow; if (Stream.REDIS_DYNAMO == stream) { thisStreamWindow = redisDynamoMessageWindow; otherStreamWindow = foundationDbMessageWindow; @@ -248,13 +253,15 @@ public class MirroringMessageStream implements MessageStream { otherStreamWindow = redisDynamoMessageWindow; } - final MessageProtos.Envelope otherStreamMessage = otherStreamWindow.remove(messageUUID); - if (otherStreamMessage != null) { - if (!otherStreamMessage.equals(envelope.message())) { + final HashCode thisStreamMessageHashCode = hashCode(envelope.message()); + + final HashCode otherStreamMessageHashCode = otherStreamWindow.remove(messageUUID); + if (otherStreamMessageHashCode != null) { + if (!otherStreamMessageHashCode.equals(thisStreamMessageHashCode)) { MESSAGE_MISMATCHES_COUNTER.increment(); } } else { - thisStreamWindow.put(messageUUID, envelope.message()); + thisStreamWindow.put(messageUUID, thisStreamMessageHashCode); if (thisStreamWindow.size() >= MAX_WINDOW_SIZE) { // Stream is way ahead of the other, stop comparing further stopAgreementVerification = true; @@ -301,4 +308,17 @@ public class MirroringMessageStream implements MessageStream { .increment(); } + private static HashCode hashCode(final MessageProtos.Envelope envelope) { + final byte[] serialized = new byte[envelope.getSerializedSize()]; + final CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(serialized); + codedOutputStream.useDeterministicSerialization(); + try { + envelope.writeTo(codedOutputStream); + codedOutputStream.flush(); + } catch (final IOException e) { + throw new UncheckedIOException(e); + } + return Hashing.murmur3_128().hashBytes(serialized); + } + }