Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tidy PersistableEnvelope inheritance #4102

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions common/src/main/java/bisq/common/proto/ProtoResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
package bisq.common.proto;

import bisq.common.Payload;
import bisq.common.proto.persistable.PersistableEnvelope;

import bisq.common.proto.persistable.PersistablePayload;

public interface ProtoResolver {
Payload fromProto(protobuf.PaymentAccountPayload proto);

PersistableEnvelope fromProto(protobuf.PersistableNetworkPayload proto);
PersistablePayload fromProto(protobuf.PersistableNetworkPayload proto);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@

package bisq.common.proto.persistable;

import com.google.protobuf.Message;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;

import lombok.EqualsAndHashCode;
Expand All @@ -31,13 +27,11 @@
import lombok.experimental.Delegate;

@EqualsAndHashCode
public class PersistableList<T extends PersistablePayload> implements PersistableEnvelope, Iterable<T> {
public abstract class PersistableList<T extends PersistablePayload> implements PersistableEnvelope, Iterable<T> {
@Delegate(excludes = ExcludesDelegateMethods.class)
@Getter
@Setter
private List<T> list;
@Setter
private Function<List<T>, Message> toProto;

public PersistableList() {
list = new ArrayList<>();
Expand All @@ -47,30 +41,11 @@ public PersistableList(List<T> list) {
this.list = list;
}

public PersistableList(List<T> list, Function<List<T>, Message> toProto) {
this(list);
this.toProto = toProto;
}

public PersistableList(HashSet<T> set) {
this(new ArrayList<>(set));
}

public PersistableList(HashSet<T> set, Function<List<T>, Message> toProto) {
this(set);
this.toProto = toProto;
}

// this.stream() does not compile for unknown reasons, so add that manual delegate method
public Stream<T> stream() {
return list.stream();
}

@Override
public Message toProtoMessage() {
return toProto.apply(list);
}

private interface ExcludesDelegateMethods<T> {
Stream<T> stream();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import java.util.List;

public class UserThreadMappedPersistableList<T extends PersistablePayload> extends PersistableList<T>
public abstract class UserThreadMappedPersistableList<T extends PersistablePayload> extends PersistableList<T>
implements UserThreadMappedPersistableEnvelope {

public UserThreadMappedPersistableList(List<T> list) {
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/bisq/core/account/sign/SignedWitness.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import bisq.common.app.Capability;
import bisq.common.crypto.Hash;
import bisq.common.proto.ProtoUtil;
import bisq.common.proto.persistable.PersistableEnvelope;
import bisq.common.util.Utilities;

import com.google.protobuf.ByteString;
Expand All @@ -45,7 +44,7 @@
// Supports signatures made from EC key (arbitrators) and signature created with DSA key.
@Slf4j
@Value
public class SignedWitness implements ProcessOncePersistableNetworkPayload, PersistableNetworkPayload, PersistableEnvelope,
public class SignedWitness implements ProcessOncePersistableNetworkPayload, PersistableNetworkPayload,
DateTolerantPayload, CapabilityRequiringPayload {

public enum VerificationMethod {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import bisq.network.p2p.storage.payload.PersistableNetworkPayload;
import bisq.network.p2p.storage.payload.ProcessOncePersistableNetworkPayload;

import bisq.common.proto.persistable.PersistableEnvelope;
import bisq.common.util.Utilities;

import com.google.protobuf.ByteString;
Expand All @@ -40,7 +39,7 @@
// so only the newly added objects since the last release will be retrieved over the P2P network.
@Slf4j
@Value
public class AccountAgeWitness implements ProcessOncePersistableNetworkPayload, PersistableNetworkPayload, PersistableEnvelope, DateTolerantPayload {
public class AccountAgeWitness implements ProcessOncePersistableNetworkPayload, PersistableNetworkPayload, DateTolerantPayload {
private static final long TOLERANCE = TimeUnit.DAYS.toMillis(1);

private final byte[] hash; // Ripemd160(Sha256(concatenated accountHash, signature and sigPubKey)); 20 bytes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,26 @@

import bisq.core.dao.governance.ConsensusCritical;

import bisq.common.proto.persistable.PersistableList;
import bisq.common.Proto;

import com.google.protobuf.InvalidProtocolBufferException;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import lombok.EqualsAndHashCode;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;

/**
* We don't persist that list but use it only for encoding the VoteWithProposalTxId list
* to PB bytes in the blindVote. The bytes get encrypted and later decrypted. To use a ByteOutputStream
* and add all list elements would work for encryption but for decrypting we don't know the length of a list entry
* and it would make the process complicate (e.g. require a custom serialisation format).
* We encode the VoteWithProposalTxId list to PB bytes in the blindVote. The bytes get encrypted and later decrypted.
* To use a ByteOutputStream and add all list elements would work for encryption but for decrypting we don't know the
* length of a list entry and it would make the process complicated (e.g. require a custom serialisation format).
*/
@Slf4j
@EqualsAndHashCode(callSuper = true)
public class VoteWithProposalTxIdList extends PersistableList<VoteWithProposalTxId> implements ConsensusCritical {

VoteWithProposalTxIdList(List<VoteWithProposalTxId> list) {
super(list);
}

@Value
public class VoteWithProposalTxIdList implements Proto, ConsensusCritical {
private final List<VoteWithProposalTxId> list;

///////////////////////////////////////////////////////////////////////////////////////////
// PROTO BUFFER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import bisq.network.p2p.storage.payload.PersistableNetworkPayload;

import bisq.common.crypto.Hash;
import bisq.common.proto.persistable.PersistableEnvelope;
import bisq.common.util.Utilities;

import com.google.protobuf.ByteString;
Expand All @@ -43,7 +42,7 @@
@Slf4j
@Getter
@EqualsAndHashCode
public final class BlindVotePayload implements PersistableNetworkPayload, PersistableEnvelope, ConsensusCritical {
public final class BlindVotePayload implements PersistableNetworkPayload, ConsensusCritical {

private final BlindVote blindVote;
protected final byte[] hash; // 20 byte
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import bisq.network.p2p.storage.payload.PersistableNetworkPayload;

import bisq.common.crypto.Hash;
import bisq.common.proto.persistable.PersistableEnvelope;
import bisq.common.util.Utilities;

import com.google.protobuf.ByteString;
Expand All @@ -40,7 +39,7 @@
@Immutable
@Slf4j
@Value
public class ProposalPayload implements PersistableNetworkPayload, PersistableEnvelope, ConsensusCritical {
public class ProposalPayload implements PersistableNetworkPayload, ConsensusCritical {
private final Proposal proposal;
protected final byte[] hash; // 20 byte

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ private BallotList createBallotList(VoteWithProposalTxIdList voteWithProposalTxI

// We convert the list to a map with proposalTxId as key and the vote as value. As the vote can be null we
// wrap it into an optional.
Map<String, Optional<Vote>> voteByTxIdMap = voteWithProposalTxIdList.stream()
Map<String, Optional<Vote>> voteByTxIdMap = voteWithProposalTxIdList.getList().stream()
.collect(Collectors.toMap(VoteWithProposalTxId::getProposalTxId, e -> Optional.ofNullable(e.getVote())));

// We make a map with proposalTxId as key and the ballot as value out of our stored ballot list.
Expand Down Expand Up @@ -414,11 +414,11 @@ private BallotList createBallotList(VoteWithProposalTxIdList voteWithProposalTxI
// If we received a proposal after we had already voted we consider it as a proposal withhold attack and
// treat the proposal as it was voted with a rejected vote.
ballotByTxIdMap.entrySet().stream()
.filter(e -> !voteByTxIdMap.keySet().contains(e.getKey()))
.filter(e -> !voteByTxIdMap.containsKey(e.getKey()))
.map(Map.Entry::getValue)
.forEach(ballot -> {
log.warn("We have a proposal which was not part of our blind vote and reject it. " +
"Proposal ={}" + ballot.getProposal());
"Proposal={}", ballot.getProposal());
ballots.add(new Ballot(ballot.getProposal(), new Vote(false)));
});

Expand Down Expand Up @@ -776,7 +776,7 @@ private boolean isInVoteResultPhase(int chainHeight) {
///////////////////////////////////////////////////////////////////////////////////////////

@Value
public class HashWithStake {
public static class HashWithStake {
private final byte[] hash;
private final long stake;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,19 @@
import bisq.core.dao.governance.ConsensusCritical;
import bisq.core.dao.state.model.ImmutableDaoStateModel;

import bisq.common.proto.persistable.PersistableList;
import bisq.common.Proto;

import com.google.protobuf.InvalidProtocolBufferException;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import lombok.EqualsAndHashCode;

import javax.annotation.concurrent.Immutable;

// We don't persist that list but use it only for encoding the MeritList list
// to PB bytes in the blindVote.
@Immutable
@EqualsAndHashCode(callSuper = true)
public class MeritList extends PersistableList<Merit> implements ConsensusCritical, ImmutableDaoStateModel {

public MeritList(List<Merit> list) {
super(list);
}
import lombok.Value;

@Value
public class MeritList implements Proto, ConsensusCritical, ImmutableDaoStateModel {
private final List<Merit> list;

///////////////////////////////////////////////////////////////////////////////////////////
// PROTO BUFFER
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/bisq/core/proto/CoreProtoResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import bisq.core.payment.payload.HalCashAccountPayload;
import bisq.core.payment.payload.InstantCryptoCurrencyPayload;
import bisq.core.payment.payload.InteracETransferAccountPayload;
import bisq.core.payment.payload.JapanBankAccountPayload;;
import bisq.core.payment.payload.JapanBankAccountPayload;
import bisq.core.payment.payload.MoneyBeamAccountPayload;
import bisq.core.payment.payload.MoneyGramAccountPayload;
import bisq.core.payment.payload.NationalBankAccountPayload;
Expand All @@ -57,7 +57,7 @@

import bisq.common.proto.ProtoResolver;
import bisq.common.proto.ProtobufferRuntimeException;
import bisq.common.proto.persistable.PersistableEnvelope;
import bisq.common.proto.persistable.PersistablePayload;

import java.time.Clock;

Expand Down Expand Up @@ -165,7 +165,7 @@ public PaymentAccountPayload fromProto(protobuf.PaymentAccountPayload proto) {
}

@Override
public PersistableEnvelope fromProto(protobuf.PersistableNetworkPayload proto) {
public PersistablePayload fromProto(protobuf.PersistableNetworkPayload proto) {
if (proto != null) {
switch (proto.getMessageCase()) {
case ACCOUNT_AGE_WITNESS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import bisq.core.dao.governance.proposal.storage.temp.TempProposalStore;
import bisq.core.dao.state.DaoStateStore;
import bisq.core.dao.state.model.governance.BallotList;
import bisq.core.dao.state.model.governance.MeritList;
import bisq.core.dao.state.unconfirmed.UnconfirmedBsqChangeOutputList;
import bisq.core.payment.PaymentAccountList;
import bisq.core.proto.CoreProtoResolver;
Expand Down Expand Up @@ -144,8 +143,6 @@ public PersistableEnvelope fromProto(protobuf.PersistableEnvelope proto) {
return MyVoteList.fromProto(proto.getMyVoteList());
case MY_BLIND_VOTE_LIST:
return MyBlindVoteList.fromProto(proto.getMyBlindVoteList());
case MERIT_LIST:
return MeritList.fromProto(proto.getMeritList());
case DAO_STATE_STORE:
return DaoStateStore.fromProto(proto.getDaoStateStore());
case MY_REPUTATION_LIST:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import bisq.common.app.Capability;
import bisq.common.crypto.Hash;
import bisq.common.proto.ProtoUtil;
import bisq.common.proto.persistable.PersistableEnvelope;
import bisq.common.util.CollectionUtils;
import bisq.common.util.ExtraDataMapValidator;
import bisq.common.util.JsonExclude;
Expand Down Expand Up @@ -65,7 +64,8 @@

@Slf4j
@Value
public final class TradeStatistics2 implements ProcessOncePersistableNetworkPayload, PersistableNetworkPayload, PersistableEnvelope, CapabilityRequiringPayload, Comparable<TradeStatistics2> {
public final class TradeStatistics2 implements ProcessOncePersistableNetworkPayload, PersistableNetworkPayload,
CapabilityRequiringPayload, Comparable<TradeStatistics2> {

public static final String MEDIATOR_ADDRESS = "medAddr";
public static final String REFUND_AGENT_ADDRESS = "refAddr";
Expand Down
8 changes: 5 additions & 3 deletions p2p/src/test/java/bisq/network/p2p/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import bisq.common.proto.network.NetworkEnvelope;
import bisq.common.proto.network.NetworkPayload;
import bisq.common.proto.network.NetworkProtoResolver;
import bisq.common.proto.persistable.PersistableEnvelope;
import bisq.common.proto.persistable.PersistablePayload;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
Expand Down Expand Up @@ -168,7 +168,7 @@ public Payload fromProto(protobuf.PaymentAccountPayload proto) {
}

@Override
public PersistableEnvelope fromProto(protobuf.PersistableNetworkPayload persistable) {
public PersistablePayload fromProto(protobuf.PersistableNetworkPayload persistable) {
return null;
}

Expand All @@ -188,7 +188,9 @@ public NetworkPayload fromProto(protobuf.StorageEntryWrapper proto) {
}

@Override
public Clock getClock() { return null; }
public Clock getClock() {
return null;
}
};
}
}
Loading