Merge pull request #4102 from stejbac/tidy-persistable-envelope-inheritance

Tidy PersistableEnvelope inheritance
This commit is contained in:
Christoph Atteneder 2020-04-28 16:22:41 +02:00 committed by GitHub
commit 91ce44ad96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 36 additions and 131 deletions

View File

@ -18,11 +18,10 @@
package bisq.common.proto; package bisq.common.proto;
import bisq.common.Payload; import bisq.common.Payload;
import bisq.common.proto.persistable.PersistableEnvelope; import bisq.common.proto.persistable.PersistablePayload;
public interface ProtoResolver { public interface ProtoResolver {
Payload fromProto(protobuf.PaymentAccountPayload proto); Payload fromProto(protobuf.PaymentAccountPayload proto);
PersistableEnvelope fromProto(protobuf.PersistableNetworkPayload proto); PersistablePayload fromProto(protobuf.PersistableNetworkPayload proto);
} }

View File

@ -1,50 +0,0 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.common.proto.persistable;
import com.google.protobuf.Message;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Delegate;
public class PersistableHashMap<K, V extends PersistablePayload> implements PersistableEnvelope {
@Delegate
@Getter
private Map<K, V> map = new HashMap<>();
@Setter
private Function<Map<K, V>, Message> toProto;
public PersistableHashMap(Map<K, V> map) {
this.map = map;
}
public PersistableHashMap(Map<K, V> map, Function<Map<K, V>, Message> toProto) {
this(map);
this.toProto = toProto;
}
@Override
public Message toProtoMessage() {
return toProto.apply(map);
}
}

View File

@ -17,12 +17,8 @@
package bisq.common.proto.persistable; package bisq.common.proto.persistable;
import com.google.protobuf.Message;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream; import java.util.stream.Stream;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
@ -31,13 +27,11 @@ import lombok.Setter;
import lombok.experimental.Delegate; import lombok.experimental.Delegate;
@EqualsAndHashCode @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) @Delegate(excludes = ExcludesDelegateMethods.class)
@Getter @Getter
@Setter @Setter
private List<T> list; private List<T> list;
@Setter
private Function<List<T>, Message> toProto;
public PersistableList() { public PersistableList() {
list = new ArrayList<>(); list = new ArrayList<>();
@ -47,30 +41,11 @@ public class PersistableList<T extends PersistablePayload> implements Persistabl
this.list = 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 // this.stream() does not compile for unknown reasons, so add that manual delegate method
public Stream<T> stream() { public Stream<T> stream() {
return list.stream(); return list.stream();
} }
@Override
public Message toProtoMessage() {
return toProto.apply(list);
}
private interface ExcludesDelegateMethods<T> { private interface ExcludesDelegateMethods<T> {
Stream<T> stream(); Stream<T> stream();
} }

View File

@ -19,7 +19,7 @@ package bisq.common.proto.persistable;
import java.util.List; 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 { implements UserThreadMappedPersistableEnvelope {
public UserThreadMappedPersistableList(List<T> list) { public UserThreadMappedPersistableList(List<T> list) {

View File

@ -27,7 +27,6 @@ import bisq.common.app.Capabilities;
import bisq.common.app.Capability; import bisq.common.app.Capability;
import bisq.common.crypto.Hash; import bisq.common.crypto.Hash;
import bisq.common.proto.ProtoUtil; import bisq.common.proto.ProtoUtil;
import bisq.common.proto.persistable.PersistableEnvelope;
import bisq.common.util.Utilities; import bisq.common.util.Utilities;
import com.google.protobuf.ByteString; import com.google.protobuf.ByteString;
@ -45,7 +44,7 @@ import lombok.extern.slf4j.Slf4j;
// Supports signatures made from EC key (arbitrators) and signature created with DSA key. // Supports signatures made from EC key (arbitrators) and signature created with DSA key.
@Slf4j @Slf4j
@Value @Value
public class SignedWitness implements ProcessOncePersistableNetworkPayload, PersistableNetworkPayload, PersistableEnvelope, public class SignedWitness implements ProcessOncePersistableNetworkPayload, PersistableNetworkPayload,
DateTolerantPayload, CapabilityRequiringPayload { DateTolerantPayload, CapabilityRequiringPayload {
public enum VerificationMethod { public enum VerificationMethod {

View File

@ -22,7 +22,6 @@ import bisq.network.p2p.storage.payload.DateTolerantPayload;
import bisq.network.p2p.storage.payload.PersistableNetworkPayload; import bisq.network.p2p.storage.payload.PersistableNetworkPayload;
import bisq.network.p2p.storage.payload.ProcessOncePersistableNetworkPayload; import bisq.network.p2p.storage.payload.ProcessOncePersistableNetworkPayload;
import bisq.common.proto.persistable.PersistableEnvelope;
import bisq.common.util.Utilities; import bisq.common.util.Utilities;
import com.google.protobuf.ByteString; import com.google.protobuf.ByteString;
@ -40,7 +39,7 @@ import lombok.extern.slf4j.Slf4j;
// so only the newly added objects since the last release will be retrieved over the P2P network. // so only the newly added objects since the last release will be retrieved over the P2P network.
@Slf4j @Slf4j
@Value @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 static final long TOLERANCE = TimeUnit.DAYS.toMillis(1);
private final byte[] hash; // Ripemd160(Sha256(concatenated accountHash, signature and sigPubKey)); 20 bytes private final byte[] hash; // Ripemd160(Sha256(concatenated accountHash, signature and sigPubKey)); 20 bytes

View File

@ -19,7 +19,7 @@ package bisq.core.dao.governance.blindvote;
import bisq.core.dao.governance.ConsensusCritical; import bisq.core.dao.governance.ConsensusCritical;
import bisq.common.proto.persistable.PersistableList; import bisq.common.Proto;
import com.google.protobuf.InvalidProtocolBufferException; import com.google.protobuf.InvalidProtocolBufferException;
@ -27,23 +27,18 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import lombok.EqualsAndHashCode; import lombok.Value;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
/** /**
* We don't persist that list but use it only for encoding the VoteWithProposalTxId list * We encode the VoteWithProposalTxId list to PB bytes in the blindVote. The bytes get encrypted and later decrypted.
* to PB bytes in the blindVote. The bytes get encrypted and later decrypted. To use a ByteOutputStream * To use a ByteOutputStream and add all list elements would work for encryption but for decrypting we don't know the
* and add all list elements would work for encryption but for decrypting we don't know the length of a list entry * length of a list entry and it would make the process complicated (e.g. require a custom serialisation format).
* and it would make the process complicate (e.g. require a custom serialisation format).
*/ */
@Slf4j @Slf4j
@EqualsAndHashCode(callSuper = true) @Value
public class VoteWithProposalTxIdList extends PersistableList<VoteWithProposalTxId> implements ConsensusCritical { public class VoteWithProposalTxIdList implements Proto, ConsensusCritical {
private final List<VoteWithProposalTxId> list;
VoteWithProposalTxIdList(List<VoteWithProposalTxId> list) {
super(list);
}
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
// PROTO BUFFER // PROTO BUFFER

View File

@ -23,7 +23,6 @@ import bisq.core.dao.governance.blindvote.BlindVote;
import bisq.network.p2p.storage.payload.PersistableNetworkPayload; import bisq.network.p2p.storage.payload.PersistableNetworkPayload;
import bisq.common.crypto.Hash; import bisq.common.crypto.Hash;
import bisq.common.proto.persistable.PersistableEnvelope;
import bisq.common.util.Utilities; import bisq.common.util.Utilities;
import com.google.protobuf.ByteString; import com.google.protobuf.ByteString;
@ -43,7 +42,7 @@ import javax.annotation.concurrent.Immutable;
@Slf4j @Slf4j
@Getter @Getter
@EqualsAndHashCode @EqualsAndHashCode
public final class BlindVotePayload implements PersistableNetworkPayload, PersistableEnvelope, ConsensusCritical { public final class BlindVotePayload implements PersistableNetworkPayload, ConsensusCritical {
private final BlindVote blindVote; private final BlindVote blindVote;
protected final byte[] hash; // 20 byte protected final byte[] hash; // 20 byte

View File

@ -23,7 +23,6 @@ import bisq.core.dao.state.model.governance.Proposal;
import bisq.network.p2p.storage.payload.PersistableNetworkPayload; import bisq.network.p2p.storage.payload.PersistableNetworkPayload;
import bisq.common.crypto.Hash; import bisq.common.crypto.Hash;
import bisq.common.proto.persistable.PersistableEnvelope;
import bisq.common.util.Utilities; import bisq.common.util.Utilities;
import com.google.protobuf.ByteString; import com.google.protobuf.ByteString;
@ -40,7 +39,7 @@ import javax.annotation.concurrent.Immutable;
@Immutable @Immutable
@Slf4j @Slf4j
@Value @Value
public class ProposalPayload implements PersistableNetworkPayload, PersistableEnvelope, ConsensusCritical { public class ProposalPayload implements PersistableNetworkPayload, ConsensusCritical {
private final Proposal proposal; private final Proposal proposal;
protected final byte[] hash; // 20 byte protected final byte[] hash; // 20 byte

View File

@ -362,7 +362,7 @@ public class VoteResultService implements DaoStateListener, DaoSetupService {
// We convert the list to a map with proposalTxId as key and the vote as value. As the vote can be null we // 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. // 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()))); .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. // We make a map with proposalTxId as key and the ballot as value out of our stored ballot list.
@ -414,11 +414,11 @@ public class VoteResultService implements DaoStateListener, DaoSetupService {
// If we received a proposal after we had already voted we consider it as a proposal withhold attack and // 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. // treat the proposal as it was voted with a rejected vote.
ballotByTxIdMap.entrySet().stream() ballotByTxIdMap.entrySet().stream()
.filter(e -> !voteByTxIdMap.keySet().contains(e.getKey())) .filter(e -> !voteByTxIdMap.containsKey(e.getKey()))
.map(Map.Entry::getValue) .map(Map.Entry::getValue)
.forEach(ballot -> { .forEach(ballot -> {
log.warn("We have a proposal which was not part of our blind vote and reject it. " + 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))); ballots.add(new Ballot(ballot.getProposal(), new Vote(false)));
}); });
@ -776,7 +776,7 @@ public class VoteResultService implements DaoStateListener, DaoSetupService {
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
@Value @Value
public class HashWithStake { public static class HashWithStake {
private final byte[] hash; private final byte[] hash;
private final long stake; private final long stake;

View File

@ -20,7 +20,7 @@ package bisq.core.dao.state.model.governance;
import bisq.core.dao.governance.ConsensusCritical; import bisq.core.dao.governance.ConsensusCritical;
import bisq.core.dao.state.model.ImmutableDaoStateModel; import bisq.core.dao.state.model.ImmutableDaoStateModel;
import bisq.common.proto.persistable.PersistableList; import bisq.common.Proto;
import com.google.protobuf.InvalidProtocolBufferException; import com.google.protobuf.InvalidProtocolBufferException;
@ -28,20 +28,11 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import lombok.EqualsAndHashCode; import lombok.Value;
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);
}
@Value
public class MeritList implements Proto, ConsensusCritical, ImmutableDaoStateModel {
private final List<Merit> list;
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
// PROTO BUFFER // PROTO BUFFER

View File

@ -33,7 +33,7 @@ import bisq.core.payment.payload.FasterPaymentsAccountPayload;
import bisq.core.payment.payload.HalCashAccountPayload; import bisq.core.payment.payload.HalCashAccountPayload;
import bisq.core.payment.payload.InstantCryptoCurrencyPayload; import bisq.core.payment.payload.InstantCryptoCurrencyPayload;
import bisq.core.payment.payload.InteracETransferAccountPayload; 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.MoneyBeamAccountPayload;
import bisq.core.payment.payload.MoneyGramAccountPayload; import bisq.core.payment.payload.MoneyGramAccountPayload;
import bisq.core.payment.payload.NationalBankAccountPayload; import bisq.core.payment.payload.NationalBankAccountPayload;
@ -57,7 +57,7 @@ import bisq.core.trade.statistics.TradeStatistics2;
import bisq.common.proto.ProtoResolver; import bisq.common.proto.ProtoResolver;
import bisq.common.proto.ProtobufferRuntimeException; import bisq.common.proto.ProtobufferRuntimeException;
import bisq.common.proto.persistable.PersistableEnvelope; import bisq.common.proto.persistable.PersistablePayload;
import java.time.Clock; import java.time.Clock;
@ -165,7 +165,7 @@ public class CoreProtoResolver implements ProtoResolver {
} }
@Override @Override
public PersistableEnvelope fromProto(protobuf.PersistableNetworkPayload proto) { public PersistablePayload fromProto(protobuf.PersistableNetworkPayload proto) {
if (proto != null) { if (proto != null) {
switch (proto.getMessageCase()) { switch (proto.getMessageCase()) {
case ACCOUNT_AGE_WITNESS: case ACCOUNT_AGE_WITNESS:

View File

@ -31,7 +31,6 @@ import bisq.core.dao.governance.proposal.storage.appendonly.ProposalStore;
import bisq.core.dao.governance.proposal.storage.temp.TempProposalStore; import bisq.core.dao.governance.proposal.storage.temp.TempProposalStore;
import bisq.core.dao.state.DaoStateStore; import bisq.core.dao.state.DaoStateStore;
import bisq.core.dao.state.model.governance.BallotList; 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.dao.state.unconfirmed.UnconfirmedBsqChangeOutputList;
import bisq.core.payment.PaymentAccountList; import bisq.core.payment.PaymentAccountList;
import bisq.core.proto.CoreProtoResolver; import bisq.core.proto.CoreProtoResolver;
@ -144,8 +143,6 @@ public class CorePersistenceProtoResolver extends CoreProtoResolver implements P
return MyVoteList.fromProto(proto.getMyVoteList()); return MyVoteList.fromProto(proto.getMyVoteList());
case MY_BLIND_VOTE_LIST: case MY_BLIND_VOTE_LIST:
return MyBlindVoteList.fromProto(proto.getMyBlindVoteList()); return MyBlindVoteList.fromProto(proto.getMyBlindVoteList());
case MERIT_LIST:
return MeritList.fromProto(proto.getMeritList());
case DAO_STATE_STORE: case DAO_STATE_STORE:
return DaoStateStore.fromProto(proto.getDaoStateStore()); return DaoStateStore.fromProto(proto.getDaoStateStore());
case MY_REPUTATION_LIST: case MY_REPUTATION_LIST:

View File

@ -32,7 +32,6 @@ import bisq.common.app.Capabilities;
import bisq.common.app.Capability; import bisq.common.app.Capability;
import bisq.common.crypto.Hash; import bisq.common.crypto.Hash;
import bisq.common.proto.ProtoUtil; import bisq.common.proto.ProtoUtil;
import bisq.common.proto.persistable.PersistableEnvelope;
import bisq.common.util.CollectionUtils; import bisq.common.util.CollectionUtils;
import bisq.common.util.ExtraDataMapValidator; import bisq.common.util.ExtraDataMapValidator;
import bisq.common.util.JsonExclude; import bisq.common.util.JsonExclude;
@ -65,7 +64,8 @@ import static com.google.common.base.Preconditions.checkNotNull;
@Slf4j @Slf4j
@Value @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 MEDIATOR_ADDRESS = "medAddr";
public static final String REFUND_AGENT_ADDRESS = "refAddr"; public static final String REFUND_AGENT_ADDRESS = "refAddr";

View File

@ -21,7 +21,7 @@ import bisq.common.Payload;
import bisq.common.proto.network.NetworkEnvelope; import bisq.common.proto.network.NetworkEnvelope;
import bisq.common.proto.network.NetworkPayload; import bisq.common.proto.network.NetworkPayload;
import bisq.common.proto.network.NetworkProtoResolver; 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.KeyPair;
import java.security.KeyPairGenerator; import java.security.KeyPairGenerator;
@ -168,7 +168,7 @@ public class TestUtils {
} }
@Override @Override
public PersistableEnvelope fromProto(protobuf.PersistableNetworkPayload persistable) { public PersistablePayload fromProto(protobuf.PersistableNetworkPayload persistable) {
return null; return null;
} }
@ -188,7 +188,9 @@ public class TestUtils {
} }
@Override @Override
public Clock getClock() { return null; } public Clock getClock() {
return null;
}
}; };
} }
} }

View File

@ -1140,7 +1140,7 @@ message PersistableEnvelope {
BallotList ballot_list = 20; BallotList ballot_list = 20;
MyVoteList my_vote_list = 21; MyVoteList my_vote_list = 21;
MyBlindVoteList my_blind_vote_list = 22; MyBlindVoteList my_blind_vote_list = 22;
MeritList merit_list = 23; // MeritList merit_list = 23; // was not used here, but its class used to implement PersistableEnvelope via its super
DaoStateStore dao_state_store = 24; DaoStateStore dao_state_store = 24;
MyReputationList my_reputation_list = 25; MyReputationList my_reputation_list = 25;
MyProofOfBurnList my_proof_of_burn_list = 26; MyProofOfBurnList my_proof_of_burn_list = 26;