From 7fffa81f0268af4b9922ae701667f8e849ade0d9 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Wed, 5 Nov 2014 13:41:39 +0100 Subject: [PATCH] Replace VerifyOffer class with Offer#validate method --- src/main/java/io/bitsquare/offer/Offer.java | 40 ++++++++-- .../createoffer/CreateOfferCoordinator.java | 15 ++-- .../createoffer/tasks/VerifyOffer.java | 74 ------------------- 3 files changed, 44 insertions(+), 85 deletions(-) delete mode 100644 src/main/java/io/bitsquare/trade/protocol/createoffer/tasks/VerifyOffer.java diff --git a/src/main/java/io/bitsquare/offer/Offer.java b/src/main/java/io/bitsquare/offer/Offer.java index 8cfa2940e4..a80e4e1dce 100644 --- a/src/main/java/io/bitsquare/offer/Offer.java +++ b/src/main/java/io/bitsquare/offer/Offer.java @@ -19,6 +19,7 @@ package io.bitsquare.offer; import io.bitsquare.arbitrator.Arbitrator; import io.bitsquare.bank.BankAccountType; +import io.bitsquare.btc.Restrictions; import io.bitsquare.locale.Country; import org.bitcoinj.core.Coin; @@ -34,6 +35,9 @@ import java.util.Date; import java.util.List; import java.util.Locale; +import static com.google.common.base.Preconditions.*; +import static io.bitsquare.btc.Restrictions.MIN_TRADE_AMOUNT; + //TODO flatten down? public class Offer implements Serializable { @@ -194,6 +198,37 @@ public class Offer implements Serializable { return bankAccountUID; } + public Date getCreationDate() { + return creationDate; + } + + public void validate() throws Exception { + checkNotNull(getAcceptedCountries(), "AcceptedCountries is null"); + checkNotNull(getAcceptedLanguageLocales(), "AcceptedLanguageLocales is null"); + checkNotNull(getAmount(), "Amount is null"); + checkNotNull(getArbitrators(), "Arbitrator is null"); + checkNotNull(getBankAccountId(), "BankAccountId is null"); + checkNotNull(getSecurityDeposit(), "SecurityDeposit is null"); + checkNotNull(getCreationDate(), "CreationDate is null"); + checkNotNull(getCurrency(), "Currency is null"); + checkNotNull(getDirection(), "Direction is null"); + checkNotNull(getId(), "Id is null"); + checkNotNull(getMessagePublicKey(), "MessagePublicKey is null"); + checkNotNull(getMinAmount(), "MinAmount is null"); + checkNotNull(getPrice(), "Price is null"); + + checkArgument(getMinAmount().compareTo(MIN_TRADE_AMOUNT) >= 0, "MinAmount is less then " + MIN_TRADE_AMOUNT); + checkArgument(getAmount().compareTo(MIN_TRADE_AMOUNT) >= 0, "Amount is less then " + MIN_TRADE_AMOUNT); + checkArgument(getAmount().compareTo(getMinAmount()) >= 0, "MinAmount is larger then Amount"); + checkArgument(getSecurityDeposit().isPositive(), "SecurityDeposit is not positive"); + checkArgument(getPrice().isPositive(), "Price is 0 or negative"); + + // TODO check balance + // securityDeposit + // Coin totalsToFund + // getAddressInfoByTradeID(offerId) + // TODO when offer is flattened continue here... + } @Override public String toString() { @@ -215,9 +250,4 @@ public class Offer implements Serializable { ", arbitrator=" + arbitrators + '}'; } - - - public Date getCreationDate() { - return creationDate; - } } diff --git a/src/main/java/io/bitsquare/trade/protocol/createoffer/CreateOfferCoordinator.java b/src/main/java/io/bitsquare/trade/protocol/createoffer/CreateOfferCoordinator.java index bdf58aee24..0bd14957ec 100644 --- a/src/main/java/io/bitsquare/trade/protocol/createoffer/CreateOfferCoordinator.java +++ b/src/main/java/io/bitsquare/trade/protocol/createoffer/CreateOfferCoordinator.java @@ -22,11 +22,10 @@ import io.bitsquare.msg.MessageFacade; import io.bitsquare.offer.Offer; import io.bitsquare.offer.OfferRepository; import io.bitsquare.persistence.Persistence; -import io.bitsquare.util.task.FaultHandler; import io.bitsquare.trade.handlers.TransactionResultHandler; import io.bitsquare.trade.protocol.createoffer.tasks.BroadCastOfferFeeTx; import io.bitsquare.trade.protocol.createoffer.tasks.CreateOfferFeeTx; -import io.bitsquare.trade.protocol.createoffer.tasks.VerifyOffer; +import io.bitsquare.util.task.FaultHandler; import org.bitcoinj.core.Transaction; @@ -115,11 +114,15 @@ public class CreateOfferCoordinator { public void start() { model.setState(State.STARTED); - VerifyOffer.run(this::onOfferValidated, faultHandler, offer); - } - private void onOfferValidated() { - model.setState(State.VALIDATED); + try { + offer.validate(); + model.setState(State.VALIDATED); + } catch (Exception ex) { + faultHandler.handleFault("Offer validation failed", ex); + return; + } + CreateOfferFeeTx.run(this::onOfferFeeTxCreated, faultHandler, walletFacade, offer.getId()); } diff --git a/src/main/java/io/bitsquare/trade/protocol/createoffer/tasks/VerifyOffer.java b/src/main/java/io/bitsquare/trade/protocol/createoffer/tasks/VerifyOffer.java deleted file mode 100644 index 7c6e8ea6c4..0000000000 --- a/src/main/java/io/bitsquare/trade/protocol/createoffer/tasks/VerifyOffer.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * This file is part of Bitsquare. - * - * Bitsquare 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. - * - * Bitsquare 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 Bitsquare. If not, see . - */ - -package io.bitsquare.trade.protocol.createoffer.tasks; - -import io.bitsquare.btc.Restrictions; -import io.bitsquare.offer.Offer; -import io.bitsquare.util.task.FaultHandler; -import io.bitsquare.util.task.ResultHandler; - -import javax.annotation.concurrent.Immutable; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import static com.google.common.base.Preconditions.*; - -@Immutable -public class VerifyOffer { - private static final Logger log = LoggerFactory.getLogger(VerifyOffer.class); - - public static void run(ResultHandler resultHandler, FaultHandler faultHandler, Offer offer) { - try { - checkNotNull(offer.getAcceptedCountries(), "AcceptedCountries is null"); - checkNotNull(offer.getAcceptedLanguageLocales(), "AcceptedLanguageLocales is null"); - checkNotNull(offer.getAmount(), "Amount is null"); - checkNotNull(offer.getArbitrators(), "Arbitrator is null"); - //checkNotNull(offer.getBankAccountCountry(), "BankAccountCountry is null"); - checkNotNull(offer.getBankAccountId(), "BankAccountId is null"); - checkNotNull(offer.getSecurityDeposit(), "SecurityDeposit is null"); - checkNotNull(offer.getCreationDate(), "CreationDate is null"); - checkNotNull(offer.getCurrency(), "Currency is null"); - checkNotNull(offer.getDirection(), "Direction is null"); - checkNotNull(offer.getId(), "Id is null"); - checkNotNull(offer.getMessagePublicKey(), "MessagePublicKey is null"); - checkNotNull(offer.getMinAmount(), "MinAmount is null"); - checkNotNull(offer.getPrice(), "Price is null"); - - //checkArgument(!offer.getAcceptedCountries().isEmpty(), "AcceptedCountries is empty"); - //checkArgument(!offer.getAcceptedLanguageLocales().isEmpty(), "AcceptedLanguageLocales is empty"); - checkArgument(offer.getMinAmount().compareTo(Restrictions.MIN_TRADE_AMOUNT) >= 0, - "MinAmount is less then " + Restrictions.MIN_TRADE_AMOUNT); - checkArgument(offer.getAmount().compareTo(Restrictions.MIN_TRADE_AMOUNT) >= 0, - "Amount is less then " + Restrictions.MIN_TRADE_AMOUNT); - checkArgument(offer.getAmount().compareTo(offer.getMinAmount()) >= 0, "MinAmount is larger then Amount"); - checkArgument(offer.getSecurityDeposit().isPositive(), "SecurityDeposit is not positive"); - checkArgument(offer.getPrice().isPositive(), "Price is 0 or negative"); - - // TODO check balance - // securityDeposit - // Coin totalsToFund - // getAddressInfoByTradeID(offerId) - // TODO when offer is flattened continue here... - - resultHandler.handleResult(); - } catch (Throwable t) { - faultHandler.handleFault("Offer validation failed.", t); - } - } -}