mirror of
https://github.com/bisq-network/bisq.git
synced 2025-02-24 15:10:44 +01:00
Handle casting errors at ProtoUtil. Rename ProtoCommonUtil to ProtoUtil, remove ProtoCoreUtil
This commit is contained in:
parent
f7ef638b1c
commit
028d26e23e
10 changed files with 67 additions and 144 deletions
|
@ -1,43 +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 io.bisq.common.proto;
|
||||
|
||||
import com.google.protobuf.ByteString;
|
||||
import io.bisq.common.locale.CurrencyUtil;
|
||||
import io.bisq.generated.protobuffer.PB;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ProtoCommonUtil {
|
||||
|
||||
public static Set<byte[]> getByteSet(List<ByteString> byteStringList) {
|
||||
return byteStringList.stream().map(ByteString::toByteArray).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
public static String getCurrencyCode(PB.OfferPayload pbOffer) {
|
||||
String currencyCode;
|
||||
if (CurrencyUtil.isCryptoCurrency(pbOffer.getBaseCurrencyCode()))
|
||||
currencyCode = pbOffer.getBaseCurrencyCode();
|
||||
else
|
||||
currencyCode = pbOffer.getCounterCurrencyCode();
|
||||
return currencyCode;
|
||||
}
|
||||
|
||||
}
|
|
@ -17,29 +17,28 @@
|
|||
|
||||
package io.bisq.common.proto;
|
||||
|
||||
import com.google.protobuf.ByteString;
|
||||
import com.google.protobuf.Message;
|
||||
import io.bisq.common.Payload;
|
||||
import io.bisq.common.locale.CurrencyUtil;
|
||||
import io.bisq.generated.protobuffer.PB;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
public class ProtoUtil {
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Convenience
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
public static <T extends Message> Iterable<T> collectionToProto(Collection<? extends Payload> collection) {
|
||||
return collection.stream().map(e -> (T) e.toProtoMessage()).collect(Collectors.toList());
|
||||
public static Set<byte[]> getByteSet(List<ByteString> byteStringList) {
|
||||
return byteStringList.stream().map(ByteString::toByteArray).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
public static <T> Iterable<T> collectionToProto(Collection<? extends Payload> collection, Function<? super Message, T> extra) {
|
||||
return collection.stream().map(o -> {
|
||||
return extra.apply(o.toProtoMessage());
|
||||
}).collect(Collectors.toList());
|
||||
public static String getCurrencyCode(PB.OfferPayload pbOffer) {
|
||||
return CurrencyUtil.isCryptoCurrency(pbOffer.getBaseCurrencyCode()) ? pbOffer.getBaseCurrencyCode() : pbOffer.getCounterCurrencyCode();
|
||||
}
|
||||
|
||||
public static <E extends Enum<E>> E enumLookup(Class<E> e, String id) {
|
||||
|
@ -52,4 +51,24 @@ public class ProtoUtil {
|
|||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static <T extends Message> Iterable<T> collectionToProto(Collection<? extends Payload> collection) {
|
||||
return collection.stream()
|
||||
.map(e -> {
|
||||
final Message message = e.toProtoMessage();
|
||||
try {
|
||||
//noinspection unchecked
|
||||
return (T) message;
|
||||
} catch (Throwable t) {
|
||||
log.error("message could not be casted. message=" + message);
|
||||
return null;
|
||||
}
|
||||
})
|
||||
.filter(e -> e != null)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static <T> Iterable<T> collectionToProto(Collection<? extends Payload> collection, Function<? super Message, T> extra) {
|
||||
return collection.stream().map(o -> extra.apply(o.toProtoMessage())).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,15 +21,12 @@ import com.google.protobuf.Message;
|
|||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.experimental.Delegate;
|
||||
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class PersistableHashMap<K extends PersistablePayload, V extends PersistablePayload> implements PersistableEnvelope {
|
||||
@Delegate
|
||||
@Getter
|
||||
@Delegate @Getter
|
||||
private HashMap<K, V> hashMap = new HashMap<>();
|
||||
@Setter
|
||||
private Function<HashMap<K, V>, Message> toProto;
|
||||
|
@ -40,15 +37,11 @@ public class PersistableHashMap<K extends PersistablePayload, V extends Persista
|
|||
|
||||
public PersistableHashMap(HashMap<K, V> hashMap, Function<HashMap<K, V>, Message> toProto) {
|
||||
this(hashMap);
|
||||
setToProto(toProto);
|
||||
this.toProto = toProto;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Message toProtoMessage() {
|
||||
if (Objects.isNull(toProto)) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
return toProto.apply(hashMap);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
|
||||
package io.bisq.core.proto;
|
||||
|
||||
import io.bisq.common.locale.CurrencyUtil;
|
||||
import io.bisq.core.arbitration.DisputeResult;
|
||||
import io.bisq.core.btc.AddressEntry;
|
||||
import io.bisq.core.dao.vote.VotingType;
|
||||
|
@ -25,40 +24,11 @@ import io.bisq.core.offer.AvailabilityResult;
|
|||
import io.bisq.core.offer.Offer;
|
||||
import io.bisq.core.offer.OfferPayload;
|
||||
import io.bisq.core.offer.OpenOffer;
|
||||
import io.bisq.core.payment.payload.BankAccountPayload;
|
||||
import io.bisq.core.payment.payload.CountryBasedPaymentAccountPayload;
|
||||
import io.bisq.core.trade.Trade;
|
||||
import io.bisq.generated.protobuffer.PB;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class ProtoCoreUtil {
|
||||
|
||||
|
||||
public static String getCurrencyCode(PB.OfferPayload pbOffer) {
|
||||
return CurrencyUtil.isCryptoCurrency(pbOffer.getBaseCurrencyCode()) ? pbOffer.getBaseCurrencyCode() : pbOffer.getCounterCurrencyCode();
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
// PaymentAccountPayload Utils
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
public static void fillInBankAccountPayload(PB.PaymentAccountPayload protoEntry, BankAccountPayload bankAccountPayload) {
|
||||
PB.BankAccountPayload bankProto = protoEntry.getCountryBasedPaymentAccountPayload().getBankAccountPayload();
|
||||
bankAccountPayload.setHolderName(bankProto.getHolderName());
|
||||
bankAccountPayload.setBankName(bankProto.getBankName());
|
||||
bankAccountPayload.setBankId(bankProto.getBankId());
|
||||
bankAccountPayload.setBranchId(bankProto.getBranchId());
|
||||
bankAccountPayload.setAccountNr(bankProto.getAccountNr());
|
||||
bankAccountPayload.setAccountType(bankProto.getAccountType());
|
||||
}
|
||||
|
||||
public static void fillInCountryBasedPaymentAccountPayload(PB.PaymentAccountPayload protoEntry,
|
||||
CountryBasedPaymentAccountPayload countryBasedPaymentAccountPayload) {
|
||||
countryBasedPaymentAccountPayload.setCountryCode(protoEntry.getCountryBasedPaymentAccountPayload().getCountryCode());
|
||||
}
|
||||
|
||||
public class ProtoDevUtil {
|
||||
// Util for auto generating enum values used in pb definition
|
||||
public static void printAllEnumsForPB() {
|
||||
StringBuilder sb = new StringBuilder("\n enum State {\n");
|
||||
|
@ -207,4 +177,5 @@ public class ProtoCoreUtil {
|
|||
|
||||
log.info(sb.toString());
|
||||
}
|
||||
|
||||
}
|
|
@ -18,7 +18,6 @@
|
|||
package io.bisq.core.trade;
|
||||
|
||||
import com.google.protobuf.Message;
|
||||
import io.bisq.common.proto.ProtoCollectionUtil;
|
||||
import io.bisq.common.proto.persistable.PersistableEnvelope;
|
||||
import io.bisq.common.storage.Storage;
|
||||
import io.bisq.core.btc.wallet.BtcWalletService;
|
||||
|
@ -69,8 +68,27 @@ public final class TradableList<T extends Tradable> implements PersistableEnvelo
|
|||
|
||||
@Override
|
||||
public Message toProtoMessage() {
|
||||
return PB.PersistableEnvelope.newBuilder().setTradableList(PB.TradableList.newBuilder()
|
||||
.addAllTradable(ProtoCollectionUtil.collectionToProto(list))).build();
|
||||
if (!list.isEmpty()) {
|
||||
if (list.get(0) instanceof OpenOffer) {
|
||||
return PB.PersistableEnvelope.newBuilder()
|
||||
.setOpenOfferList(PB.OpenOfferList.newBuilder()
|
||||
.addAllOpenOffer(list.stream()
|
||||
.map(e -> (PB.OpenOffer) e.toProtoMessage())
|
||||
.collect(Collectors.toList())))
|
||||
.build();
|
||||
} else {
|
||||
return PB.PersistableEnvelope.newBuilder()
|
||||
.setTradeList(PB.TradeList.newBuilder()
|
||||
.addAllTrade(list.stream()
|
||||
.map(e -> (PB.Trade) e.toProtoMessage())
|
||||
.collect(Collectors.toList())))
|
||||
.build();
|
||||
}
|
||||
} else {
|
||||
return PB.PersistableEnvelope.newBuilder()
|
||||
.setTradeList(PB.TradeList.newBuilder())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
public static TradableList fromProto(PB.TradableList proto,
|
||||
|
|
|
@ -20,7 +20,7 @@ package io.bisq.core.trade.protocol;
|
|||
import com.google.protobuf.ByteString;
|
||||
import io.bisq.common.crypto.KeyRing;
|
||||
import io.bisq.common.crypto.PubKeyRing;
|
||||
import io.bisq.common.proto.ProtoCollectionUtil;
|
||||
import io.bisq.common.proto.ProtoUtil;
|
||||
import io.bisq.common.proto.persistable.PersistablePayload;
|
||||
import io.bisq.common.taskrunner.Model;
|
||||
import io.bisq.core.btc.data.RawTransactionInput;
|
||||
|
@ -134,10 +134,10 @@ public class ProcessModel implements Model, PersistablePayload {
|
|||
.setUseSavingsWallet(useSavingsWallet);
|
||||
Optional.ofNullable(takeOfferFeeTxId).ifPresent(builder::setTakeOfferFeeTxId);
|
||||
Optional.ofNullable(payoutTxSignature).ifPresent(e -> builder.setPayoutTxSignature(ByteString.copyFrom(payoutTxSignature)));
|
||||
Optional.ofNullable(takerAcceptedArbitratorNodeAddresses).ifPresent(e -> builder.addAllTakerAcceptedArbitratorNodeAddresses(ProtoCollectionUtil.collectionToProto(takerAcceptedArbitratorNodeAddresses)));
|
||||
Optional.ofNullable(takerAcceptedMediatorNodeAddresses).ifPresent(e -> builder.addAllTakerAcceptedMediatorNodeAddresses(ProtoCollectionUtil.collectionToProto(takerAcceptedMediatorNodeAddresses)));
|
||||
Optional.ofNullable(takerAcceptedArbitratorNodeAddresses).ifPresent(e -> builder.addAllTakerAcceptedArbitratorNodeAddresses(ProtoUtil.collectionToProto(takerAcceptedArbitratorNodeAddresses)));
|
||||
Optional.ofNullable(takerAcceptedMediatorNodeAddresses).ifPresent(e -> builder.addAllTakerAcceptedMediatorNodeAddresses(ProtoUtil.collectionToProto(takerAcceptedMediatorNodeAddresses)));
|
||||
Optional.ofNullable(preparedDepositTx).ifPresent(e -> builder.setPreparedDepositTx(ByteString.copyFrom(preparedDepositTx)));
|
||||
Optional.ofNullable(rawTransactionInputs).ifPresent(e -> builder.addAllRawTransactionInputs(ProtoCollectionUtil.collectionToProto(rawTransactionInputs)));
|
||||
Optional.ofNullable(rawTransactionInputs).ifPresent(e -> builder.addAllRawTransactionInputs(ProtoUtil.collectionToProto(rawTransactionInputs)));
|
||||
Optional.ofNullable(changeOutputAddress).ifPresent(builder::setChangeOutputAddress);
|
||||
Optional.ofNullable(myMultiSigPubKey).ifPresent(e -> builder.setMyMultiSigPubKey(ByteString.copyFrom(myMultiSigPubKey)));
|
||||
Optional.ofNullable(tempTradingPeerNodeAddress).ifPresent(e -> builder.setTempTradingPeerNodeAddress(tempTradingPeerNodeAddress.toProtoMessage()));
|
||||
|
@ -249,41 +249,7 @@ public class ProcessModel implements Model, PersistablePayload {
|
|||
return takeOfferFeeTx;
|
||||
}
|
||||
|
||||
public void setTakeOfferFeeTx(Transaction takeOfferFeeTx) {
|
||||
this.takeOfferFeeTx = takeOfferFeeTx;
|
||||
takeOfferFeeTxId = takeOfferFeeTx.getHashAsString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Message toProtoMessage() {
|
||||
return PB.ProcessModel.newBuilder()
|
||||
.setTradingPeer((PB.TradingPeer) tradingPeer.toProtoMessage())
|
||||
.setOfferId(offerId)
|
||||
.setAccountId(accountId)
|
||||
.setPubKeyRing(pubKeyRing.toProtoMessage())
|
||||
.setTakeOfferFeeTxId(takeOfferFeeTxId)
|
||||
.setPayoutTxSignature(ByteString.copyFrom(payoutTxSignature))
|
||||
.addAllTakerAcceptedArbitratorNodeAddresses(ProtoCollectionUtil.collectionToProto(takerAcceptedArbitratorNodeAddresses))
|
||||
.addAllTakerAcceptedMediatorNodeAddresses(ProtoCollectionUtil.collectionToProto(takerAcceptedMediatorNodeAddresses))
|
||||
.setPreparedDepositTx(ByteString.copyFrom(preparedDepositTx))
|
||||
.addAllRawTransactionInputs(ProtoCollectionUtil.collectionToProto(rawTransactionInputs))
|
||||
.setChangeOutputValue(changeOutputValue)
|
||||
.setChangeOutputAddress(changeOutputAddress)
|
||||
.setUseSavingsWallet(useSavingsWallet)
|
||||
.setFundsNeededForTradeAsLong(fundsNeededForTradeAsLong)
|
||||
.setMyMultiSigPubKey(ByteString.copyFrom(myMultiSigPubKey))
|
||||
.setTempTradingPeerNodeAddress(tempTradingPeerNodeAddress.toProtoMessage())
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
public void removeMailboxMessageAfterProcessing(Trade trade) {
|
||||
if (tradeMessage instanceof MailboxMessage &&
|
||||
decryptedMessageWithPubKey != null &&
|
||||
decryptedMessageWithPubKey.getWireEnvelope().equals(tradeMessage)) {
|
||||
log.debug("Remove decryptedMsgWithPubKey from P2P network. decryptedMsgWithPubKey = " + decryptedMessageWithPubKey);
|
||||
p2PService.removeEntryFromMailbox(decryptedMessageWithPubKey);
|
||||
trade.removeDecryptedMessageWithPubKey(decryptedMessageWithPubKey);
|
||||
}
|
||||
public NodeAddress getMyNodeAddress() {
|
||||
return p2PService.getAddress();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ package io.bisq.core.trade.protocol;
|
|||
import com.google.protobuf.ByteString;
|
||||
import com.google.protobuf.Message;
|
||||
import io.bisq.common.crypto.PubKeyRing;
|
||||
import io.bisq.common.proto.ProtoCollectionUtil;
|
||||
import io.bisq.common.proto.ProtoUtil;
|
||||
import io.bisq.common.proto.persistable.PersistablePayload;
|
||||
import io.bisq.core.btc.data.RawTransactionInput;
|
||||
import io.bisq.core.payment.payload.PaymentAccountPayload;
|
||||
|
@ -74,7 +74,7 @@ public final class TradingPeer implements PersistablePayload {
|
|||
Optional.ofNullable(signature).ifPresent(e -> builder.setSignature(ByteString.copyFrom(signature)));
|
||||
Optional.ofNullable(pubKeyRing).ifPresent(e -> builder.setPubKeyRing(pubKeyRing.toProtoMessage()));
|
||||
Optional.ofNullable(multiSigPubKey).ifPresent(e -> builder.setMultiSigPubKey(ByteString.copyFrom(multiSigPubKey)));
|
||||
Optional.ofNullable(rawTransactionInputs).ifPresent(e -> builder.addAllRawTransactionInputs(ProtoCollectionUtil.collectionToProto(rawTransactionInputs)));
|
||||
Optional.ofNullable(rawTransactionInputs).ifPresent(e -> builder.addAllRawTransactionInputs(ProtoUtil.collectionToProto(rawTransactionInputs)));
|
||||
Optional.ofNullable(changeOutputAddress).ifPresent(builder::setChangeOutputAddress);
|
||||
return builder.build();
|
||||
}
|
||||
|
|
|
@ -18,7 +18,6 @@
|
|||
package io.bisq.core.util;
|
||||
|
||||
import io.bisq.common.proto.ProtoUtil;
|
||||
import io.bisq.core.offer.AvailabilityResult;
|
||||
import io.bisq.core.offer.OpenOffer;
|
||||
import io.bisq.generated.protobuffer.PB;
|
||||
import io.bisq.generated.protobuffer.PB.OfferPayload;
|
||||
|
@ -55,7 +54,7 @@ public class ProtoBufferUtilitiesTest {
|
|||
|
||||
@Test
|
||||
public void testUnknownEnumFix() {
|
||||
PB.OpenOffer.State result = PB.OpenOffer.State.UNKNOWN_FAILURE;
|
||||
PB.OpenOffer.State result = PB.OpenOffer.State.PB_ERROR;
|
||||
try {
|
||||
OpenOffer.State finalResult = ProtoUtil.enumLookup(OpenOffer.State.class, result.name());
|
||||
assertEquals(OpenOffer.State.AVAILABLE, ProtoUtil.enumLookup(OpenOffer.State.class, "AVAILABLE"));
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package io.bisq.network.p2p.peers.getdata.messages;
|
||||
|
||||
import com.google.protobuf.ByteString;
|
||||
import io.bisq.common.proto.ProtoCommonUtil;
|
||||
import io.bisq.common.proto.ProtoUtil;
|
||||
import io.bisq.common.proto.network.NetworkEnvelope;
|
||||
import io.bisq.generated.protobuffer.PB;
|
||||
import io.bisq.network.p2p.NodeAddress;
|
||||
|
@ -41,6 +41,6 @@ public final class GetUpdatedDataRequest implements SendersNodeAddressMessage, G
|
|||
public static GetUpdatedDataRequest fromProto(PB.GetUpdatedDataRequest getUpdatedDataRequest) {
|
||||
return new GetUpdatedDataRequest(NodeAddress.fromProto(getUpdatedDataRequest.getSenderNodeAddress()),
|
||||
getUpdatedDataRequest.getNonce(),
|
||||
ProtoCommonUtil.getByteSet(getUpdatedDataRequest.getExcludedKeysList()));
|
||||
ProtoUtil.getByteSet(getUpdatedDataRequest.getExcludedKeysList()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package io.bisq.network.p2p.peers.getdata.messages;
|
|||
|
||||
import com.google.protobuf.ByteString;
|
||||
import io.bisq.common.app.Capabilities;
|
||||
import io.bisq.common.proto.ProtoCommonUtil;
|
||||
import io.bisq.common.proto.ProtoUtil;
|
||||
import io.bisq.common.proto.network.NetworkEnvelope;
|
||||
import io.bisq.generated.protobuffer.PB;
|
||||
import io.bisq.network.p2p.AnonymousMessage;
|
||||
|
@ -38,6 +38,6 @@ public final class PreliminaryGetDataRequest implements AnonymousMessage, GetDat
|
|||
|
||||
public static PreliminaryGetDataRequest fromProto(PB.PreliminaryGetDataRequest proto) {
|
||||
return new PreliminaryGetDataRequest(proto.getNonce(),
|
||||
ProtoCommonUtil.getByteSet(proto.getExcludedKeysList()));
|
||||
ProtoUtil.getByteSet(proto.getExcludedKeysList()));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue