Skip to content

Commit

Permalink
Merge pull request #611 from HenrikJannsen/fix_inventory_handling
Browse files Browse the repository at this point in the history
Fix inventory handling
  • Loading branch information
alvasw authored Dec 22, 2022
2 parents bf18c57 + cda9954 commit 2feca70
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 49 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
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ public static AddAuthenticatedDataRequest from(AuthenticatedDataStorageService s
}

@Getter
protected final AuthenticatedSequentialData authenticatedSequentialData;
private final AuthenticatedSequentialData authenticatedSequentialData;
@Getter
protected final byte[] signature; // 256 bytes
private final byte[] signature; // 256 bytes
@Getter
protected final byte[] ownerPublicKeyBytes; // 294 bytes
private final byte[] ownerPublicKeyBytes; // 294 bytes
@Nullable
transient protected PublicKey ownerPublicKey;
transient private PublicKey ownerPublicKey;

public AddAuthenticatedDataRequest(AuthenticatedSequentialData authenticatedSequentialData, byte[] signature, PublicKey ownerPublicKey) {
this(authenticatedSequentialData,
Expand All @@ -78,10 +78,10 @@ public AddAuthenticatedDataRequest(AuthenticatedSequentialData authenticatedSequ
ownerPublicKey);
}

protected AddAuthenticatedDataRequest(AuthenticatedSequentialData authenticatedSequentialData,
byte[] signature,
byte[] ownerPublicKeyBytes,
PublicKey ownerPublicKey) {
private AddAuthenticatedDataRequest(AuthenticatedSequentialData authenticatedSequentialData,
byte[] signature,
byte[] ownerPublicKeyBytes,
PublicKey ownerPublicKey) {
this.authenticatedSequentialData = authenticatedSequentialData;
this.signature = signature;
this.ownerPublicKeyBytes = ownerPublicKeyBytes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ public static RefreshAuthenticatedDataRequest from(AuthenticatedDataStorageServi
signature);
}

protected final MetaData metaData;
protected final byte[] hash;
protected final byte[] ownerPublicKeyBytes; // 442 bytes
transient protected final PublicKey ownerPublicKey;
protected final int sequenceNumber;
protected final byte[] signature; // 47 bytes
private final MetaData metaData;
private final byte[] hash;
private final byte[] ownerPublicKeyBytes; // 442 bytes
transient private final PublicKey ownerPublicKey;
private final int sequenceNumber;
private final byte[] signature; // 47 bytes

public RefreshAuthenticatedDataRequest(MetaData metaData,
byte[] hash,
Expand All @@ -72,12 +72,12 @@ public RefreshAuthenticatedDataRequest(MetaData metaData,
signature);
}

protected RefreshAuthenticatedDataRequest(MetaData metaData,
byte[] hash,
byte[] ownerPublicKeyBytes,
PublicKey ownerPublicKey,
int sequenceNumber,
byte[] signature) {
private RefreshAuthenticatedDataRequest(MetaData metaData,
byte[] hash,
byte[] ownerPublicKeyBytes,
PublicKey ownerPublicKey,
int sequenceNumber,
byte[] signature) {
this.metaData = metaData;
this.hash = hash;
this.ownerPublicKeyBytes = ownerPublicKeyBytes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ public static RemoveAuthenticatedDataRequest from(AuthenticatedDataStorageServic
signature);
}

protected final MetaData metaData;
protected final byte[] hash;
protected final byte[] ownerPublicKeyBytes; // 442 bytes
private final MetaData metaData;
private final byte[] hash;
private final byte[] ownerPublicKeyBytes; // 442 bytes
@Nullable
transient protected PublicKey ownerPublicKey;
protected final int sequenceNumber;
protected final byte[] signature; // 47 bytes
protected final long created;
transient private PublicKey ownerPublicKey;
private final int sequenceNumber;
private final byte[] signature; // 47 bytes
private final long created;

public RemoveAuthenticatedDataRequest(MetaData metaData,
byte[] hash,
Expand All @@ -73,12 +73,12 @@ public RemoveAuthenticatedDataRequest(MetaData metaData,
signature);
}

protected RemoveAuthenticatedDataRequest(MetaData metaData,
byte[] hash,
byte[] ownerPublicKeyBytes,
PublicKey ownerPublicKey,
int sequenceNumber,
byte[] signature) {
private RemoveAuthenticatedDataRequest(MetaData metaData,
byte[] hash,
byte[] ownerPublicKeyBytes,
PublicKey ownerPublicKey,
int sequenceNumber,
byte[] signature) {
this(metaData,
hash,
ownerPublicKeyBytes,
Expand All @@ -88,13 +88,13 @@ protected RemoveAuthenticatedDataRequest(MetaData metaData,
System.currentTimeMillis());
}

protected RemoveAuthenticatedDataRequest(MetaData metaData,
byte[] hash,
byte[] ownerPublicKeyBytes,
PublicKey ownerPublicKey,
int sequenceNumber,
byte[] signature,
long created) {
private RemoveAuthenticatedDataRequest(MetaData metaData,
byte[] hash,
byte[] ownerPublicKeyBytes,
PublicKey ownerPublicKey,
int sequenceNumber,
byte[] signature,
long created) {
this.metaData = metaData;
this.hash = hash;
this.ownerPublicKeyBytes = ownerPublicKeyBytes;
Expand Down

0 comments on commit 2feca70

Please sign in to comment.