Skip to content

Commit

Permalink
Add ExcludeForHashAwareProto interface to DataAndSeqNrPair
Browse files Browse the repository at this point in the history
Signed-off-by: HenrikJannsen <boilingfrog@gmx.com>
  • Loading branch information
HenrikJannsen authored and alejandrogarcia83 committed Jun 12, 2024
1 parent 78287da commit 1a75e36
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions p2p/src/main/java/bisq/network/p2p/storage/P2PDataStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import bisq.network.p2p.storage.persistence.ResourceDataStoreService;
import bisq.network.p2p.storage.persistence.SequenceNumberMap;

import bisq.common.ExcludeForHashAwareProto;
import bisq.common.Timer;
import bisq.common.UserThread;
import bisq.common.app.Capabilities;
Expand All @@ -71,6 +72,8 @@
import bisq.common.util.Tuple2;
import bisq.common.util.Utilities;

import protobuf.StoragePayload;

import com.google.protobuf.ByteString;

import com.google.inject.name.Named;
Expand Down Expand Up @@ -1253,8 +1256,8 @@ public static byte[] get32ByteHash(NetworkPayload data) {
*/
@EqualsAndHashCode
@ToString
public static final class DataAndSeqNrPair implements NetworkPayload {
// data are only used for calculating cryptographic hash from both values so they are kept private
public static final class DataAndSeqNrPair implements NetworkPayload, ExcludeForHashAwareProto {
// data are only used for calculating cryptographic hash from both values, so they are kept private
private final ProtectedStoragePayload protectedStoragePayload;
private final int sequenceNumber;

Expand All @@ -1263,13 +1266,31 @@ public DataAndSeqNrPair(ProtectedStoragePayload protectedStoragePayload, int seq
this.sequenceNumber = sequenceNumber;
}

// Used only for calculating hash of byte array from PB object
@Override
public com.google.protobuf.Message toProtoMessage() {
public protobuf.DataAndSeqNrPair toProtoMessage() {
return toProto(false);
}

@Override
public protobuf.DataAndSeqNrPair toProto(boolean serializeForHash) {
return resolveProto(serializeForHash);
}

@Override
public protobuf.DataAndSeqNrPair.Builder getBuilder(boolean serializeForHash) {
return protobuf.DataAndSeqNrPair.newBuilder()
.setPayload((protobuf.StoragePayload) protectedStoragePayload.toProtoMessage())
.setSequenceNumber(sequenceNumber)
.build();
.setPayload(toStoragePayloadProto(serializeForHash))
.setSequenceNumber(sequenceNumber);
}

private protobuf.StoragePayload toStoragePayloadProto(boolean serializeForHash) {
if (protectedStoragePayload instanceof ExcludeForHashAwareProto) {
ExcludeForHashAwareProto proto = (ExcludeForHashAwareProto) protectedStoragePayload;
StoragePayload.Builder builder = (StoragePayload.Builder) proto.getBuilder(serializeForHash);
return resolveBuilder(builder, serializeForHash).build();
} else {
return (StoragePayload) protectedStoragePayload.toProtoMessage();
}
}
}

Expand Down

0 comments on commit 1a75e36

Please sign in to comment.