set null fields to the default protobuffer values, otherwise Offer classes are not the same on both sides of the wire

This commit is contained in:
Mike Rosseel 2017-03-02 22:43:53 +01:00
parent c25e083cbe
commit 9041960e74

View file

@ -18,6 +18,8 @@
package io.bitsquare.messages.trade.offer.payload; package io.bitsquare.messages.trade.offer.payload;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import io.bitsquare.app.DevFlags; import io.bitsquare.app.DevFlags;
import io.bitsquare.app.Version; import io.bitsquare.app.Version;
import io.bitsquare.common.crypto.KeyRing; import io.bitsquare.common.crypto.KeyRing;
@ -52,10 +54,7 @@ import javax.annotation.Nullable;
import java.io.IOException; import java.io.IOException;
import java.security.PublicKey; import java.security.PublicKey;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.Date; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -192,9 +191,11 @@ public final class Offer implements StoragePayload, RequiresOwnerIsOnlinePayload
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
/** /**
* no nulls are allowed because protobuffer replaces them with "" on the other side,
* meaning it's null here and "" there => not good
* *
* @param id * @param id
* @param creationDate date of Offer creation, can be null in which case the current date/time will be used. * @param creationDate date of Offer creation, can be null in which case the current date/time will be used.
* @param offererNodeAddress * @param offererNodeAddress
* @param pubKeyRing * @param pubKeyRing
* @param direction * @param direction
@ -276,11 +277,11 @@ public final class Offer implements StoragePayload, RequiresOwnerIsOnlinePayload
this.arbitratorNodeAddresses = arbitratorNodeAddresses; this.arbitratorNodeAddresses = arbitratorNodeAddresses;
this.paymentMethodName = paymentMethodName; this.paymentMethodName = paymentMethodName;
this.offererPaymentAccountId = offererPaymentAccountId; this.offererPaymentAccountId = offererPaymentAccountId;
this.offerFeePaymentTxID = offerFeePaymentTxID; this.offerFeePaymentTxID = Optional.ofNullable(offerFeePaymentTxID).orElse("");
this.countryCode = countryCode; this.countryCode = Optional.ofNullable(countryCode).orElse("");
this.acceptedCountryCodes = acceptedCountryCodes; this.acceptedCountryCodes = Optional.ofNullable(acceptedCountryCodes).orElse(Lists.newArrayList());
this.bankId = bankId; this.bankId = Optional.ofNullable(bankId).orElse("");
this.acceptedBankIds = acceptedBankIds; this.acceptedBankIds = Optional.ofNullable(acceptedBankIds).orElse(Lists.newArrayList());
this.priceFeedService = priceFeedService; this.priceFeedService = priceFeedService;
this.versionNr = versionNr; this.versionNr = versionNr;
this.blockHeightAtOfferCreation = blockHeightAtOfferCreation; this.blockHeightAtOfferCreation = blockHeightAtOfferCreation;
@ -294,16 +295,10 @@ public final class Offer implements StoragePayload, RequiresOwnerIsOnlinePayload
this.lowerClosePrice = lowerClosePrice; this.lowerClosePrice = lowerClosePrice;
this.upperClosePrice = upperClosePrice; this.upperClosePrice = upperClosePrice;
this.isPrivateOffer = isPrivateOffer; this.isPrivateOffer = isPrivateOffer;
this.hashOfChallenge = hashOfChallenge; this.hashOfChallenge = Optional.ofNullable(hashOfChallenge).orElse("");
this.extraDataMap = extraDataMap; this.extraDataMap = Optional.ofNullable(extraDataMap).orElse(Maps.newHashMap());
this.date = Optional.ofNullable(creationDate).orElse(new Date().getTime());
protocolVersion = Version.TRADE_PROTOCOL_VERSION; this.protocolVersion = Version.TRADE_PROTOCOL_VERSION;
if(creationDate == null) {
this.date = new Date().getTime();
} else {
this.date = creationDate;
}
setState(State.UNDEFINED); setState(State.UNDEFINED);
init(); init();
@ -716,25 +711,12 @@ public final class Offer implements StoragePayload, RequiresOwnerIsOnlinePayload
} else { } else {
throw new RuntimeException("Offer is in invalid state: offerFeePaymentTxID is not set when adding to P2P network."); throw new RuntimeException("Offer is in invalid state: offerFeePaymentTxID is not set when adding to P2P network.");
} }
if (Objects.nonNull(countryCode)) { Optional.ofNullable(countryCode).ifPresent(offerBuilder::setCountryCode);
offerBuilder.setCountryCode(countryCode); Optional.ofNullable(bankId).ifPresent(offerBuilder::setBankId);
} Optional.ofNullable(acceptedCountryCodes).ifPresent(offerBuilder::addAllAcceptedCountryCodes);
if (Objects.nonNull(bankId)) { Optional.ofNullable(getAcceptedBankIds()).ifPresent(offerBuilder::addAllAcceptedBankIds);
offerBuilder.setBankId(bankId); Optional.ofNullable(hashOfChallenge).ifPresent(offerBuilder::setHashOfChallenge);
} Optional.ofNullable(extraDataMap).ifPresent(offerBuilder::putAllExtraDataMap);
if (Objects.nonNull(acceptedCountryCodes)) {
offerBuilder.addAllAcceptedCountryCodes(acceptedCountryCodes);
}
if (Objects.nonNull(getAcceptedBankIds())) {
offerBuilder.addAllAcceptedBankIds(getAcceptedBankIds());
}
if (Objects.nonNull(hashOfChallenge)) {
offerBuilder.setHashOfChallenge(hashOfChallenge);
}
if (Objects.nonNull(extraDataMap)) {
offerBuilder.putAllExtraDataMap(extraDataMap);
}
return Messages.StoragePayload.newBuilder().setOffer(offerBuilder).build(); return Messages.StoragePayload.newBuilder().setOffer(offerBuilder).build();
} }