Skip to content

Commit

Permalink
Merge pull request #612 from HenrikJannsen/make_all_network_data_and_…
Browse files Browse the repository at this point in the history
…messages_deterministic

Make all network data and messages deterministic
  • Loading branch information
alvasw authored Dec 22, 2022
2 parents 2feca70 + 6a010b8 commit 91604bb
Show file tree
Hide file tree
Showing 26 changed files with 143 additions and 101 deletions.
4 changes: 4 additions & 0 deletions account/src/main/java/bisq/account/accounts/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import bisq.account.settlement.SettlementMethod;
import bisq.common.currency.TradeCurrency;
import bisq.common.data.ByteArray;
import bisq.common.proto.Proto;
import bisq.common.proto.UnresolvableProtobufMessageException;
import bisq.common.util.StringUtils;
Expand All @@ -27,6 +28,7 @@
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -63,6 +65,8 @@ public Account(String id, long creationDate,
this.payload = payload;
this.settlementMethod = settlementMethod;
this.tradeCurrencies = tradeCurrencies;
// We need to sort deterministically as the data is used in the proof of work check
this.tradeCurrencies.sort(Comparator.comparing((TradeCurrency e) -> new ByteArray(e.serialize())));
}


Expand Down
6 changes: 3 additions & 3 deletions chat/src/main/java/bisq/chat/channel/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import bisq.chat.trade.priv.PrivateTradeChannel;
import bisq.chat.trade.pub.PublicTradeChannel;
import bisq.common.observable.Observable;
import bisq.common.observable.ObservableSet;
import bisq.common.observable.ObservableArray;
import bisq.common.proto.Proto;
import bisq.common.proto.UnresolvableProtobufMessageException;
import lombok.EqualsAndHashCode;
Expand Down Expand Up @@ -86,15 +86,15 @@ public static Channel<? extends ChatMessage> fromProto(bisq.chat.protobuf.Channe
case PUBLICSUPPORTCHANNEL: {
return PublicSupportChannel.fromProto(proto, proto.getPublicSupportChannel());
}

case MESSAGE_NOT_SET: {
throw new UnresolvableProtobufMessageException(proto);
}
}
throw new UnresolvableProtobufMessageException(proto);
}

abstract public ObservableSet<T> getChatMessages();
abstract public ObservableArray<T> getChatMessages();

abstract public void addChatMessage(T chatMessage);

Expand Down
11 changes: 7 additions & 4 deletions chat/src/main/java/bisq/chat/channel/PrivateChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
package bisq.chat.channel;

import bisq.chat.message.PrivateChatMessage;
import bisq.common.observable.ObservableSet;
import bisq.common.data.ByteArray;
import bisq.common.observable.ObservableArray;
import bisq.user.identity.UserIdentity;
import bisq.user.profile.UserProfile;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

import java.util.Set;
import java.util.Comparator;
import java.util.List;

@Getter
@ToString(callSuper = true)
Expand All @@ -35,17 +37,18 @@ public abstract class PrivateChannel<T extends PrivateChatMessage> extends Chann
protected final UserIdentity myUserIdentity;

// We persist the messages as they are NOT persisted in the P2P data store.
protected final ObservableSet<T> chatMessages = new ObservableSet<>();
protected final ObservableArray<T> chatMessages = new ObservableArray<>();

public PrivateChannel(String id,
UserProfile peer,
UserIdentity myUserIdentity,
Set<T> chatMessages,
List<T> chatMessages,
ChannelNotificationType channelNotificationType) {
super(id, channelNotificationType);
this.peer = peer;
this.myUserIdentity = myUserIdentity;
this.chatMessages.addAll(chatMessages);
this.chatMessages.sort(Comparator.comparing((T e) -> new ByteArray(e.serialize())));
}

public static String createChannelId(String peersId, String myId) {
Expand Down
4 changes: 2 additions & 2 deletions chat/src/main/java/bisq/chat/channel/PublicChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package bisq.chat.channel;

import bisq.chat.message.PublicChatMessage;
import bisq.common.observable.ObservableSet;
import bisq.common.observable.ObservableArray;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
Expand All @@ -28,7 +28,7 @@
@EqualsAndHashCode(callSuper = true, onlyExplicitlyIncluded = true)
public abstract class PublicChannel<M extends PublicChatMessage> extends Channel<M> {
// We do not persist the messages as they are persisted in the P2P data store.
protected transient final ObservableSet<M> chatMessages = new ObservableSet<>();
protected transient final ObservableArray<M> chatMessages = new ObservableArray<>();

public PublicChannel(String id, ChannelNotificationType channelNotificationType) {
super(id, channelNotificationType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
import lombok.Getter;
import lombok.ToString;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.List;
import java.util.stream.Collectors;

@ToString(callSuper = true)
Expand All @@ -39,14 +39,14 @@ public PrivateDiscussionChannel(UserProfile peer, UserIdentity myProfile) {
this(PrivateChannel.createChannelId(peer.getId(), myProfile.getId()),
peer,
myProfile,
new HashSet<>(),
new ArrayList<>(),
ChannelNotificationType.ALL);
}

private PrivateDiscussionChannel(String id,
UserProfile peer,
UserIdentity myProfile,
Set<PrivateDiscussionChatMessage> chatMessages,
List<PrivateDiscussionChatMessage> chatMessages,
ChannelNotificationType channelNotificationType) {
super(id, peer, myProfile, chatMessages, channelNotificationType);
}
Expand All @@ -70,7 +70,7 @@ public static PrivateDiscussionChannel fromProto(bisq.chat.protobuf.Channel base
UserIdentity.fromProto(proto.getMyUserIdentity()),
proto.getChatMessagesList().stream()
.map(PrivateDiscussionChatMessage::fromProto)
.collect(Collectors.toSet()),
.collect(Collectors.toList()),
ChannelNotificationType.fromProto(baseProto.getChannelNotificationType()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
import lombok.Getter;
import lombok.ToString;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.Comparator;
import java.util.List;

@Getter
@ToString(callSuper = true)
Expand All @@ -35,22 +36,22 @@ public final class PublicDiscussionChannel extends PublicChannel<PublicDiscussio
private final String channelName;
private final String description;
private final String channelAdminId;
private final Set<String> channelModeratorIds;
private final List<String> channelModeratorIds;

public PublicDiscussionChannel(String id) {
this(id,
Res.get("discussion." + id + ".name"),
Res.get("discussion." + id + ".description"),
"",
new HashSet<>(),
new ArrayList<>(),
ChannelNotificationType.MENTION);
}

public PublicDiscussionChannel(String id,
String channelName,
String description,
String channelAdminId,
Set<String> channelModeratorIds) {
List<String> channelModeratorIds) {
this(id,
channelName,
description,
Expand All @@ -63,14 +64,16 @@ private PublicDiscussionChannel(String id,
String channelName,
String description,
String channelAdminId,
Set<String> channelModeratorIds,
List<String> channelModeratorIds,
ChannelNotificationType channelNotificationType) {
super(id, channelNotificationType);

this.channelName = channelName;
this.description = description;
this.channelAdminId = channelAdminId;
this.channelModeratorIds = channelModeratorIds;
// We need to sort deterministically as the data is used in the proof of work check
this.channelModeratorIds.sort(Comparator.comparing((String e) -> e));
}

public bisq.chat.protobuf.Channel toProto() {
Expand All @@ -90,7 +93,7 @@ public static PublicDiscussionChannel fromProto(bisq.chat.protobuf.Channel baseP
proto.getChannelName(),
proto.getDescription(),
proto.getChannelAdminId(),
new HashSet<>(proto.getChannelModeratorIdsList()),
new ArrayList<>(proto.getChannelModeratorIdsList()),
ChannelNotificationType.fromProto(baseProto.getChannelNotificationType()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
import lombok.Getter;
import lombok.ToString;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.List;
import java.util.stream.Collectors;

@ToString(callSuper = true)
Expand All @@ -39,14 +39,14 @@ public PrivateEventsChannel(UserProfile peer, UserIdentity myProfile) {
this(PrivateChannel.createChannelId(peer.getId(), myProfile.getId()),
peer,
myProfile,
new HashSet<>(),
new ArrayList<>(),
ChannelNotificationType.ALL);
}

private PrivateEventsChannel(String id,
UserProfile peer,
UserIdentity myProfile,
Set<PrivateEventsChatMessage> chatMessages,
List<PrivateEventsChatMessage> chatMessages,
ChannelNotificationType channelNotificationType) {
super(id, peer, myProfile, chatMessages, channelNotificationType);
}
Expand All @@ -70,7 +70,7 @@ public static PrivateEventsChannel fromProto(bisq.chat.protobuf.Channel baseProt
UserIdentity.fromProto(proto.getMyUserIdentity()),
proto.getChatMessagesList().stream()
.map(PrivateEventsChatMessage::fromProto)
.collect(Collectors.toSet()),
.collect(Collectors.toList()),
ChannelNotificationType.fromProto(baseProto.getChannelNotificationType()));
}

Expand Down
17 changes: 10 additions & 7 deletions chat/src/main/java/bisq/chat/events/pub/PublicEventsChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
import lombok.Getter;
import lombok.ToString;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.Comparator;
import java.util.List;

@Getter
@ToString(callSuper = true)
Expand All @@ -35,22 +36,22 @@ public final class PublicEventsChannel extends PublicChannel<PublicEventsChatMes
private final String channelName;
private final String description;
private final String channelAdminId;
private final Set<String> channelModeratorIds;
private final List<String> channelModeratorIds;

public PublicEventsChannel(String id) {
this(id,
Res.get("events." + id + ".name"),
Res.get("events." + id + ".description"),
"",
new HashSet<>(),
new ArrayList<>(),
ChannelNotificationType.MENTION);
}

public PublicEventsChannel(String id,
String channelName,
String description,
String channelAdminId,
Set<String> channelModeratorIds) {
List<String> channelModeratorIds) {
this(id,
channelName,
description,
Expand All @@ -63,14 +64,16 @@ private PublicEventsChannel(String id,
String channelName,
String description,
String channelAdminId,
Set<String> channelModeratorIds,
List<String> channelModeratorIds,
ChannelNotificationType channelNotificationType) {
super(id, channelNotificationType);

this.channelName = channelName;
this.description = description;
this.channelAdminId = channelAdminId;
this.channelModeratorIds = channelModeratorIds;
// We need to sort deterministically as the data is used in the proof of work check
this.channelModeratorIds.sort(Comparator.comparing((String e) -> e));
}

public bisq.chat.protobuf.Channel toProto() {
Expand All @@ -90,7 +93,7 @@ public static PublicEventsChannel fromProto(bisq.chat.protobuf.Channel baseProto
proto.getChannelName(),
proto.getDescription(),
proto.getChannelAdminId(),
new HashSet<>(proto.getChannelModeratorIdsList()),
new ArrayList<>(proto.getChannelModeratorIdsList()),
ChannelNotificationType.fromProto(baseProto.getChannelNotificationType()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
import lombok.Getter;
import lombok.ToString;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.List;
import java.util.stream.Collectors;

@ToString(callSuper = true)
Expand All @@ -39,14 +39,14 @@ public PrivateSupportChannel(UserProfile peer, UserIdentity myProfile) {
this(PrivateChannel.createChannelId(peer.getId(), myProfile.getId()),
peer,
myProfile,
new HashSet<>(),
new ArrayList<>(),
ChannelNotificationType.ALL);
}

private PrivateSupportChannel(String id,
UserProfile peer,
UserIdentity myProfile,
Set<PrivateSupportChatMessage> chatMessages,
List<PrivateSupportChatMessage> chatMessages,
ChannelNotificationType channelNotificationType) {
super(id, peer, myProfile, chatMessages, channelNotificationType);
}
Expand All @@ -70,7 +70,7 @@ public static PrivateSupportChannel fromProto(bisq.chat.protobuf.Channel basePro
UserIdentity.fromProto(proto.getMyUserIdentity()),
proto.getChatMessagesList().stream()
.map(PrivateSupportChatMessage::fromProto)
.collect(Collectors.toSet()),
.collect(Collectors.toList()),
ChannelNotificationType.fromProto(baseProto.getChannelNotificationType()));
}

Expand Down
Loading

0 comments on commit 91604bb

Please sign in to comment.