Skip to content

Commit

Permalink
Add deterministic sorting to Inventory as proof of work check compare…
Browse files Browse the repository at this point in the history
…s byte arrays of payload and could fail otherwise
  • Loading branch information
HenrikJannsen committed Dec 21, 2022
1 parent de78308 commit cda9954
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
8 changes: 7 additions & 1 deletion common/src/main/java/bisq/common/data/ByteArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
import com.google.protobuf.ByteString;
import lombok.Getter;

import java.math.BigInteger;
import java.util.Arrays;

@Getter
public final class ByteArray implements Proto {
public final class ByteArray implements Proto, Comparable<ByteArray> {
private final byte[] bytes;

public ByteArray(byte[] bytes) {
Expand Down Expand Up @@ -57,4 +58,9 @@ public int hashCode() {
public String toString() {
return Hex.encode(bytes);
}

@Override
public int compareTo(ByteArray o) {
return new BigInteger(this.getBytes()).compareTo(new BigInteger(o.getBytes()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,34 @@

package bisq.network.p2p.services.data.inventory;

import bisq.common.data.ByteArray;
import bisq.common.proto.Proto;
import bisq.network.p2p.services.data.DataRequest;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

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

@Getter
@ToString
@EqualsAndHashCode
@Slf4j
public final class Inventory implements Proto {
private final Set<? extends DataRequest> entries;
private final List<? extends DataRequest> entries;
private final int numDropped;

public Inventory(Set<? extends DataRequest> entries, int numDropped) {
this.entries = entries;
public Inventory(Collection<? extends DataRequest> entries, int numDropped) {
this.entries = new ArrayList<>(entries);
this.numDropped = numDropped;

// We need to sort deterministically as the data is used in the proof of work check
this.entries.sort(Comparator.comparing((DataRequest o) -> new ByteArray(o.serialize())));
}

public bisq.network.protobuf.Inventory toProto() {
Expand All @@ -50,9 +56,9 @@ public bisq.network.protobuf.Inventory toProto() {

public static Inventory fromProto(bisq.network.protobuf.Inventory proto) {
List<bisq.network.protobuf.DataRequest> entriesList = proto.getEntriesList();
Set<DataRequest> entries = entriesList.stream()
List<DataRequest> entries = entriesList.stream()
.map(DataRequest::fromProto)
.collect(Collectors.toSet());
.collect(Collectors.toList());
return new Inventory(entries, proto.getNumDropped());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,10 @@ public Inventory getInventoryFromStore(DataFilter dataFilter, DataStorageService

private Inventory getInventory(DataFilter dataFilter,
Set<? extends Map.Entry<ByteArray, ? extends DataRequest>> entrySet) {
HashSet<? extends DataRequest> result = entrySet.stream()
Set<? extends DataRequest> result = entrySet.stream()
.filter(mapEntry -> !dataFilter.getFilterEntries().contains(getFilterEntry(mapEntry)))
.map(Map.Entry::getValue)
.collect(Collectors.toCollection(HashSet::new));
.collect(Collectors.toSet());
return new Inventory(result, entrySet.size() - result.size());
}

Expand Down

0 comments on commit cda9954

Please sign in to comment.