refactored create offer process

This commit is contained in:
Manfred Karrer 2014-08-06 14:31:22 +02:00
parent 75581aa1fb
commit 7cac1eceec
42 changed files with 555 additions and 213 deletions

View File

@ -15,7 +15,7 @@ import org.slf4j.LoggerFactory;
* possible. This means that the transaction is the most likely to get confirmed. Note that this means we may end up
* "spending" more priority than would be required to get the transaction we are creating confirmed.
*/
class AddressBasedCoinSelector extends DefaultCoinSelector
public class AddressBasedCoinSelector extends DefaultCoinSelector
{
private static final Logger log = LoggerFactory.getLogger(AddressBasedCoinSelector.class);
private final NetworkParameters params;

View File

@ -1201,7 +1201,7 @@ public class WalletFacade
return tx;
}
private void printInputs(String tracePrefix, Transaction tx)
public static void printInputs(String tracePrefix, Transaction tx)
{
for (TransactionInput input : tx.getInputs())
{

View File

@ -0,0 +1,86 @@
package io.bitsquare.btc.tasks;
import com.google.bitcoin.core.*;
import com.google.common.util.concurrent.FutureCallback;
import io.bitsquare.btc.AddressBasedCoinSelector;
import io.bitsquare.btc.AddressEntry;
import io.bitsquare.btc.FeePolicy;
import io.bitsquare.btc.WalletFacade;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PayFeeTask
{
private static final Logger log = LoggerFactory.getLogger(PayFeeTask.class);
private enum State
{
INIT,
TX_COMPLETED,
TX_COMMITTED,
TX_BROAD_CASTED,
}
private State state;
public String start(Wallet wallet, FeePolicy feePolicy, AddressEntry addressEntry, FutureCallback<Transaction> callback)
{
state = State.INIT;
Transaction tx = new Transaction(wallet.getParams());
Coin fee = FeePolicy.CREATE_OFFER_FEE.subtract(FeePolicy.TX_FEE);
log.trace("fee: " + fee.toFriendlyString());
tx.addOutput(fee, feePolicy.getAddressForCreateOfferFee());
Wallet.SendRequest sendRequest = Wallet.SendRequest.forTx(tx);
sendRequest.shuffleOutputs = false;
// we allow spending of unconfirmed tx (double spend risk is low and usability would suffer if we need to wait for 1 confirmation)
sendRequest.coinSelector = new AddressBasedCoinSelector(wallet.getParams(), addressEntry, true);
sendRequest.changeAddress = addressEntry.getAddress();
try
{
Wallet.SendResult sendResult = wallet.sendCoins(sendRequest);
state = State.TX_COMPLETED;
} catch (IllegalStateException e)
{
e.printStackTrace();
} catch (InsufficientMoneyException e)
{
e.printStackTrace();
} catch (IllegalArgumentException e)
{
e.printStackTrace();
} catch (Wallet.DustySendRequested e)
{
e.printStackTrace();
} catch (Wallet.CouldNotAdjustDownwards e)
{
e.printStackTrace();
} catch (Wallet.ExceededMaxTransactionSize e)
{
e.printStackTrace();
} catch (VerificationException e)
{
e.printStackTrace();
}
/*
* @throws IllegalStateException if no transaction broadcaster has been configured.
* @throws InsufficientMoneyException if the request could not be completed due to not enough balance.
* @throws IllegalArgumentException if you try and complete the same SendRequest twice
* @throws DustySendRequested if the resultant transaction would violate the dust rules (an output that's too small to be worthwhile)
* @throws CouldNotAdjustDownwards if emptying the wallet was requested and the output can't be shrunk for fees without violating a protocol rule.
* @throws ExceededMaxTransactionSize if the resultant transaction is too big for Bitcoin to process (try breaking up the amounts of value)
*/
WalletFacade.printInputs("payCreateOfferFee", tx);
log.debug("tx=" + tx);
return tx.getHashAsString();
}
}

View File

@ -18,7 +18,7 @@ import io.bitsquare.msg.MessageFacade;
import io.bitsquare.msg.SeedNodeAddress;
import io.bitsquare.settings.Settings;
import io.bitsquare.storage.Persistence;
import io.bitsquare.trade.Trading;
import io.bitsquare.trade.TradeManager;
import io.bitsquare.trade.orderbook.OrderBook;
import io.bitsquare.trade.orderbook.OrderBookFilter;
import io.bitsquare.user.User;
@ -43,7 +43,7 @@ public class BitSquareModule extends AbstractModule
bind(BlockChainFacade.class).asEagerSingleton();
bind(MessageFacade.class).asEagerSingleton();
bind(Trading.class).asEagerSingleton();
bind(TradeManager.class).asEagerSingleton();
bind(BitSquareFormatter.class).asEagerSingleton();

View File

@ -15,7 +15,7 @@ import io.bitsquare.msg.BootstrapListener;
import io.bitsquare.msg.MessageFacade;
import io.bitsquare.storage.Persistence;
import io.bitsquare.trade.Direction;
import io.bitsquare.trade.Trading;
import io.bitsquare.trade.TradeManager;
import io.bitsquare.user.User;
import io.bitsquare.util.AWTSystemTray;
import java.io.IOException;
@ -48,7 +48,7 @@ public class MainController implements Initializable, NavigationController
private final User user;
private final WalletFacade walletFacade;
private final MessageFacade messageFacade;
private final Trading trading;
private final TradeManager tradeManager;
private final Persistence persistence;
private final ToggleGroup toggleGroup = new ToggleGroup();
@ -80,12 +80,12 @@ public class MainController implements Initializable, NavigationController
///////////////////////////////////////////////////////////////////////////////////////////
@Inject
private MainController(User user, WalletFacade walletFacade, MessageFacade messageFacade, Trading trading, Persistence persistence)
private MainController(User user, WalletFacade walletFacade, MessageFacade messageFacade, TradeManager tradeManager, Persistence persistence)
{
this.user = user;
this.walletFacade = walletFacade;
this.messageFacade = messageFacade;
this.trading = trading;
this.tradeManager = tradeManager;
this.persistence = persistence;
MainController.INSTANCE = this;
@ -212,7 +212,7 @@ public class MainController implements Initializable, NavigationController
if (messageFacadeInited) initialisationDone();
});
trading.addTakeOfferRequestListener(this::onTakeOfferRequested);
tradeManager.addTakeOfferRequestListener(this::onTakeOfferRequested);
}
private void initialisationDone()

View File

@ -1,9 +1,6 @@
package io.bitsquare.gui.market.createOffer;
import com.google.bitcoin.core.Coin;
import com.google.bitcoin.core.InsufficientMoneyException;
import com.google.bitcoin.core.Transaction;
import com.google.common.util.concurrent.FutureCallback;
import io.bitsquare.BitSquare;
import io.bitsquare.bank.BankAccount;
import io.bitsquare.btc.AddressEntry;
@ -24,11 +21,10 @@ import io.bitsquare.locale.Localisation;
import io.bitsquare.settings.Settings;
import io.bitsquare.trade.Direction;
import io.bitsquare.trade.Offer;
import io.bitsquare.trade.Trading;
import io.bitsquare.trade.TradeManager;
import io.bitsquare.trade.orderbook.OrderBookFilter;
import io.bitsquare.user.Arbitrator;
import io.bitsquare.user.User;
import java.io.IOException;
import java.net.URL;
import java.util.Random;
import java.util.ResourceBundle;
@ -40,7 +36,6 @@ import javafx.scene.control.TabPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javax.inject.Inject;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -48,7 +43,7 @@ public class CreateOfferController implements Initializable, ChildController, Hi
{
private static final Logger log = LoggerFactory.getLogger(CreateOfferController.class);
private final Trading trading;
private final TradeManager tradeManager;
private final WalletFacade walletFacade;
private final Settings settings;
private final User user;
@ -62,7 +57,7 @@ public class CreateOfferController implements Initializable, ChildController, Hi
@FXML
private AnchorPane rootContainer;
@FXML
private Label buyLabel, placeOfferTitle, confirmationLabel, txTitleLabel, collateralLabel;
private Label buyLabel, confirmationLabel, txTitleLabel, collateralLabel;
@FXML
private TextField volumeTextField, amountTextField, priceTextField, totalTextField;
@FXML
@ -80,9 +75,9 @@ public class CreateOfferController implements Initializable, ChildController, Hi
///////////////////////////////////////////////////////////////////////////////////////////
@Inject
private CreateOfferController(Trading trading, WalletFacade walletFacade, Settings settings, User user)
private CreateOfferController(TradeManager tradeManager, WalletFacade walletFacade, Settings settings, User user)
{
this.trading = trading;
this.tradeManager = tradeManager;
this.walletFacade = walletFacade;
this.settings = settings;
this.user = user;
@ -194,25 +189,52 @@ public class CreateOfferController implements Initializable, ChildController, Hi
// UI Handlers
///////////////////////////////////////////////////////////////////////////////////////////
public void onPlaceOffer()
public boolean inputsValid()
{
if (!inputValid())
boolean inputFieldsValid;
double priceAsDouble = BitSquareConverter.stringToDouble(priceTextField.getText());
double minAmountAsDouble = BitSquareConverter.stringToDouble(minAmountTextField.getText());
double amountAsDouble = BitSquareConverter.stringToDouble(getAmountString());
double collateralAsDouble = BitSquareConverter.stringToDouble(collateralTextField.getText());
inputFieldsValid = priceAsDouble > 0 &&
amountAsDouble > 0 &&
minAmountAsDouble > 0 &&
minAmountAsDouble <= amountAsDouble/* &&
collateralAsDouble >= settings.getMinCollateral() &&
collateralAsDouble <= settings.getMaxCollateral()*/;
if (!inputFieldsValid)
{
Popups.openWarningPopup("Invalid input", "Your input is invalid");
return;
return false;
}
//TODO will be derived form arbitrators
double collateral = getCollateral();
Arbitrator arbitrator = settings.getRandomArbitrator(collateral, getAmountAsCoin());
Arbitrator arbitrator = settings.getRandomArbitrator(getCollateral(), getAmountAsCoin());
if (arbitrator == null)
{
Popups.openWarningPopup("No arbitrator available", "No arbitrator from your arbitrator list does match the collateral and amount value.");
return;
return false;
}
if (user.getCurrentBankAccount() != null)
if (user.getCurrentBankAccount() == null)
{
log.error("Must never happen!");
Popups.openWarningPopup("No bank account selected", "No bank account selected.");
return false;
}
return true;
}
public void onPlaceOffer()
{
if (inputsValid())
{
placeOfferButton.setDisable(true);
double collateral = getCollateral();
Arbitrator arbitrator = settings.getRandomArbitrator(collateral, getAmountAsCoin());
Coin amountAsCoin = BitSquareFormatter.parseBtcToCoin(getAmountString());
Coin minAmountAsCoin = BitSquareFormatter.parseBtcToCoin(getMinAmountString());
@ -230,45 +252,12 @@ public class CreateOfferController implements Initializable, ChildController, Hi
settings.getAcceptedCountries(),
settings.getAcceptedLanguageLocales());
addressEntry.setTradeId(offer.getId());
try
{
walletFacade.payCreateOfferFee(offer.getId(), new FutureCallback<Transaction>()
{
@Override
public void onSuccess(@javax.annotation.Nullable Transaction transaction)
{
log.info("sendResult onSuccess:" + transaction);
if (transaction != null)
{
offer.setOfferFeePaymentTxID(transaction.getHashAsString());
setupSuccessScreen(transaction);
// placeOfferTitle.setText("Transaction sent:");
try
{
trading.addOffer(offer);
} catch (IOException e)
{
Popups.openErrorPopup("Error on adding offer", "Could not add offer to orderbook. " + e.getMessage());
}
}
}
@Override
public void onFailure(@NotNull Throwable t)
{
log.warn("sendResult onFailure:" + t);
Popups.openErrorPopup("Fee payment failed", "Fee payment failed. " + t);
placeOfferButton.setDisable(false);
}
});
placeOfferButton.setDisable(true);
} catch (InsufficientMoneyException e1)
{
Popups.openInsufficientMoneyPopup();
}
tradeManager.requestPlaceOffer(offer,
(transactionId) -> setupSuccessScreen(transactionId),
errorMessage -> {
Popups.openErrorPopup("An error occurred", errorMessage);
placeOfferButton.setDisable(false);
});
}
}
@ -286,7 +275,7 @@ public class CreateOfferController implements Initializable, ChildController, Hi
// Private methods
///////////////////////////////////////////////////////////////////////////////////////////
private void setupSuccessScreen(Transaction newTransaction)
private void setupSuccessScreen(String transactionId)
{
placeOfferButton.setVisible(false);
@ -296,7 +285,7 @@ public class CreateOfferController implements Initializable, ChildController, Hi
txTextField.setVisible(true);
closeButton.setVisible(true);
txTextField.setText(newTransaction.getHashAsString());
txTextField.setText(transactionId);
}
private void updateTotals()
@ -372,20 +361,5 @@ public class CreateOfferController implements Initializable, ChildController, Hi
return settings.getCollateral();
}
private boolean inputValid()
{
double priceAsDouble = BitSquareConverter.stringToDouble(priceTextField.getText());
double minAmountAsDouble = BitSquareConverter.stringToDouble(minAmountTextField.getText());
double amountAsDouble = BitSquareConverter.stringToDouble(getAmountString());
double collateralAsDouble = BitSquareConverter.stringToDouble(collateralTextField.getText());
return priceAsDouble > 0 &&
amountAsDouble > 0 &&
minAmountAsDouble > 0 &&
minAmountAsDouble <= amountAsDouble/* &&
collateralAsDouble >= settings.getMinCollateral() &&
collateralAsDouble <= settings.getMaxCollateral()*/;
}
}

View File

@ -15,7 +15,7 @@ import io.bitsquare.gui.util.BitSquareValidator;
import io.bitsquare.msg.MessageFacade;
import io.bitsquare.trade.Offer;
import io.bitsquare.trade.Trade;
import io.bitsquare.trade.Trading;
import io.bitsquare.trade.TradeManager;
import io.bitsquare.trade.protocol.taker.ProtocolForTakerAsSeller;
import io.bitsquare.trade.protocol.taker.ProtocolForTakerAsSellerListener;
import java.net.URL;
@ -33,7 +33,7 @@ public class TakerOfferController implements Initializable, ChildController
{
private static final Logger log = LoggerFactory.getLogger(TakerOfferController.class);
private final Trading trading;
private final TradeManager tradeManager;
private final WalletFacade walletFacade;
private final MessageFacade messageFacade;
@ -66,9 +66,9 @@ public class TakerOfferController implements Initializable, ChildController
///////////////////////////////////////////////////////////////////////////////////////////
@Inject
private TakerOfferController(Trading trading, WalletFacade walletFacade, MessageFacade messageFacade)
private TakerOfferController(TradeManager tradeManager, WalletFacade walletFacade, MessageFacade messageFacade)
{
this.trading = trading;
this.tradeManager = tradeManager;
this.walletFacade = walletFacade;
this.messageFacade = messageFacade;
}
@ -166,7 +166,7 @@ public class TakerOfferController implements Initializable, ChildController
{
Popups.openErrorPopup("Insufficient money", "You don't have enough funds for that trade.");
}
else if (trading.isOfferAlreadyInTrades(offer))
else if (tradeManager.isOfferAlreadyInTrades(offer))
{
Popups.openErrorPopup("Offer previously accepted", "You have that offer already taken. Open the offer section to find that trade.");
}
@ -174,7 +174,7 @@ public class TakerOfferController implements Initializable, ChildController
{
takeOfferButton.setDisable(true);
amountTextField.setEditable(false);
trading.takeOffer(amount, offer, new ProtocolForTakerAsSellerListener()
tradeManager.takeOffer(amount, offer, new ProtocolForTakerAsSellerListener()
{
@Override
public void onDepositTxPublished(String depositTxId)
@ -243,7 +243,7 @@ public class TakerOfferController implements Initializable, ChildController
@FXML
public void onReceivedFiat()
{
trading.onFiatReceived(tradeId);
tradeManager.onFiatReceived(tradeId);
}
@FXML

View File

@ -5,7 +5,7 @@ import io.bitsquare.gui.Hibernate;
import io.bitsquare.gui.NavigationController;
import io.bitsquare.gui.util.Icons;
import io.bitsquare.trade.Offer;
import io.bitsquare.trade.Trading;
import io.bitsquare.trade.TradeManager;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
@ -28,7 +28,7 @@ import org.slf4j.LoggerFactory;
public class OfferController implements Initializable, ChildController, Hibernate
{
private static final Logger log = LoggerFactory.getLogger(OfferController.class);
private final Trading trading;
private final TradeManager tradeManager;
private ObservableList<OfferListItem> offerListItems;
@FXML
private TableColumn<String, OfferListItem> offerIdColumn, dateColumn, amountColumn, priceColumn, volumeColumn, removeColumn;
@ -41,9 +41,9 @@ public class OfferController implements Initializable, ChildController, Hibernat
///////////////////////////////////////////////////////////////////////////////////////////
@Inject
private OfferController(Trading trading)
private OfferController(TradeManager tradeManager)
{
this.trading = trading;
this.tradeManager = tradeManager;
}
@ -90,7 +90,7 @@ public class OfferController implements Initializable, ChildController, Hibernat
public void awake()
{
offerListItems = FXCollections.observableArrayList();
Map<String, Offer> offerMap = trading.getOffers();
Map<String, Offer> offerMap = tradeManager.getOffers();
List<Offer> offerList = new ArrayList<>(offerMap.values());
offerListItems.addAll(offerList.stream().map(OfferListItem::new).collect(Collectors.toList()));
offerTable.setItems(offerListItems);
@ -109,7 +109,7 @@ public class OfferController implements Initializable, ChildController, Hibernat
private void removeOffer(OfferListItem offerListItem)
{
trading.removeOffer(offerListItem.getOffer());
tradeManager.removeOffer(offerListItem.getOffer());
offerListItems.remove(offerListItem);
}

View File

@ -20,7 +20,7 @@ import io.bitsquare.locale.Localisation;
import io.bitsquare.trade.Direction;
import io.bitsquare.trade.Offer;
import io.bitsquare.trade.Trade;
import io.bitsquare.trade.Trading;
import io.bitsquare.trade.TradeManager;
import io.bitsquare.util.AWTSystemTray;
import java.net.URL;
import java.util.*;
@ -47,7 +47,7 @@ public class PendingTradeController implements Initializable, ChildController, H
{
private static final Logger log = LoggerFactory.getLogger(PendingTradeController.class);
private Trading trading;
private TradeManager tradeManager;
private WalletFacade walletFacade;
private Trade currentTrade;
@ -78,9 +78,9 @@ public class PendingTradeController implements Initializable, ChildController, H
///////////////////////////////////////////////////////////////////////////////////////////
@Inject
public PendingTradeController(Trading trading, WalletFacade walletFacade)
public PendingTradeController(TradeManager tradeManager, WalletFacade walletFacade)
{
this.trading = trading;
this.tradeManager = tradeManager;
this.walletFacade = walletFacade;
}
@ -123,7 +123,7 @@ public class PendingTradeController implements Initializable, ChildController, H
@Override
public void awake()
{
Map<String, Trade> trades = trading.getTrades();
Map<String, Trade> trades = tradeManager.getTrades();
List<Trade> tradeList = new ArrayList<>(trades.values());
ObservableList<PendingTradesListItem> tradeItems = FXCollections.observableArrayList();
for (Iterator<Trade> iterator = tradeList.iterator(); iterator.hasNext(); )
@ -147,8 +147,8 @@ public class PendingTradeController implements Initializable, ChildController, H
}
});
trading.getNewTradeProperty().addListener((observableValue, oldTradeId, newTradeId) -> {
Trade newTrade = trading.getTrade(newTradeId);
tradeManager.getNewTradeProperty().addListener((observableValue, oldTradeId, newTradeId) -> {
Trade newTrade = tradeManager.getTrade(newTradeId);
if (newTrade != null)
{
tradeItems.add(new PendingTradesListItem(newTrade));
@ -159,7 +159,7 @@ public class PendingTradeController implements Initializable, ChildController, H
// select
Optional<PendingTradesListItem> currentTradeItemOptional = tradeItems.stream()
.filter((e) -> trading.getPendingTrade() != null && e.getTrade().getId().equals(trading.getPendingTrade().getId()))
.filter((e) -> tradeManager.getPendingTrade() != null && e.getTrade().getId().equals(tradeManager.getPendingTrade().getId()))
.findFirst();
if (currentTradeItemOptional.isPresent())
{
@ -180,7 +180,7 @@ public class PendingTradeController implements Initializable, ChildController, H
public void bankTransferInited()
{
trading.bankTransferInited(currentTrade.getId());
tradeManager.bankTransferInited(currentTrade.getId());
bankTransferInitedButton.setDisable(true);
}

View File

@ -43,6 +43,14 @@ import org.slf4j.LoggerFactory;
*/
public class MessageFacade implements MessageBroker
{
public static interface AddOfferListener
{
void onComplete(String offerId);
void onFailed(String reason, Throwable throwable);
}
private static final Logger log = LoggerFactory.getLogger(MessageFacade.class);
private static final String ARBITRATORS_ROOT = "ArbitratorsRoot";
@ -141,7 +149,7 @@ public class MessageFacade implements MessageBroker
// Offer
///////////////////////////////////////////////////////////////////////////////////////////
public void addOffer(Offer offer)
public void addOffer(Offer offer, AddOfferListener addOfferListener)
{
Number160 locationKey = Number160.createHash(offer.getCurrency().getCurrencyCode());
try
@ -158,29 +166,40 @@ public class MessageFacade implements MessageBroker
public void operationComplete(BaseFuture future) throws Exception
{
Platform.runLater(() -> {
orderBookListeners.stream().forEach(orderBookListener -> orderBookListener.onOfferAdded(data, future.isSuccess()));
addOfferListener.onComplete(offer.getId());
orderBookListeners.stream().forEach(listener -> listener.onOfferAdded(data, future.isSuccess()));
// TODO will be removed when we don't use polling anymore
setDirty(locationKey);
});
if (future.isSuccess())
{
log.trace("Add offer to DHT was successful. Stored data: [key: " + locationKey + ", value: " + data + "]");
Platform.runLater(() -> log.trace("Add offer to DHT was successful. Stored data: [key: " + locationKey + ", value: " + data + "]"));
}
else
{
log.error("Add offer to DHT failed. Reason: " + future.failedReason());
Platform.runLater(() -> {
addOfferListener.onFailed("Add offer to DHT failed.", new Exception("Add offer to DHT failed. Reason: " + future.failedReason()));
log.error("Add offer to DHT failed. Reason: " + future.failedReason());
});
}
}
@Override
public void exceptionCaught(Throwable t) throws Exception
{
log.error(t.toString());
Platform.runLater(() -> {
addOfferListener.onFailed("Add offer to DHT failed with an exception.", t);
log.error("Add offer to DHT failed with an exception: " + t.getMessage());
});
}
});
} catch (IOException | ClassNotFoundException e)
{
e.printStackTrace();
log.error(e.toString());
Platform.runLater(() -> {
addOfferListener.onFailed("Add offer to DHT failed with an exception.", e);
log.error("Add offer to DHT failed with an exception: " + e.getMessage());
});
}
}

View File

@ -11,7 +11,10 @@ import io.bitsquare.gui.popups.Popups;
import io.bitsquare.msg.MessageFacade;
import io.bitsquare.msg.listeners.TakeOfferRequestListener;
import io.bitsquare.storage.Persistence;
import io.bitsquare.trade.handlers.ErrorMessageHandler;
import io.bitsquare.trade.handlers.PublishTransactionResultHandler;
import io.bitsquare.trade.protocol.TradeMessage;
import io.bitsquare.trade.protocol.createoffer.CreateOfferCoordinator;
import io.bitsquare.trade.protocol.offerer.*;
import io.bitsquare.trade.protocol.taker.*;
import io.bitsquare.user.User;
@ -28,9 +31,9 @@ import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Trading
public class TradeManager
{
private static final Logger log = LoggerFactory.getLogger(Trading.class);
private static final Logger log = LoggerFactory.getLogger(TradeManager.class);
private final User user;
private final Persistence persistence;
@ -44,6 +47,7 @@ public class Trading
//TODO store TakerAsSellerProtocol in trade
private final Map<String, ProtocolForTakerAsSeller> takerAsSellerProtocolMap = new HashMap<>();
private final Map<String, ProtocolForOffererAsBuyer> offererAsBuyerProtocolMap = new HashMap<>();
private final Map<String, CreateOfferCoordinator> createOfferCoordinatorMap = new HashMap<>();
private final StringProperty newTradeProperty = new SimpleStringProperty();
@ -58,7 +62,7 @@ public class Trading
///////////////////////////////////////////////////////////////////////////////////////////
@Inject
public Trading(User user, Persistence persistence, MessageFacade messageFacade, BlockChainFacade blockChainFacade, WalletFacade walletFacade, CryptoFacade cryptoFacade)
public TradeManager(User user, Persistence persistence, MessageFacade messageFacade, BlockChainFacade blockChainFacade, WalletFacade walletFacade, CryptoFacade cryptoFacade)
{
this.user = user;
this.persistence = persistence;
@ -120,17 +124,46 @@ public class Trading
// Manage offers
///////////////////////////////////////////////////////////////////////////////////////////
public void addOffer(Offer offer) throws IOException
public void requestPlaceOffer(Offer offer, PublishTransactionResultHandler resultHandler, ErrorMessageHandler errorMessageHandler)
{
if (createOfferCoordinatorMap.containsKey(offer.getId()))
{
errorMessageHandler.onFault("A createOfferCoordinator for the offer with the id " + offer.getId() + " already exists.");
}
else
{
CreateOfferCoordinator createOfferCoordinator = new CreateOfferCoordinator(offer, walletFacade, messageFacade);
createOfferCoordinatorMap.put(offer.getId(), createOfferCoordinator);
createOfferCoordinator.start(
(transactionId) -> {
try
{
addOffer(offer);
offer.setOfferFeePaymentTxID(transactionId);
createOfferCoordinatorMap.remove(offer.getId());
resultHandler.onResult(transactionId);
} catch (Exception e)
{
//TODO retry policy
errorMessageHandler.onFault("Could not save offer. Reason: " + e.getMessage());
createOfferCoordinatorMap.remove(offer.getId());
}
},
(message, throwable) -> {
errorMessageHandler.onFault(message);
createOfferCoordinatorMap.remove(offer.getId());
});
}
}
private void addOffer(Offer offer) throws IOException
{
if (offers.containsKey(offer.getId()))
{
throw new IllegalStateException("offers contains already an offer with the ID " + offer.getId());
}
throw new IllegalStateException("An offer with the id " + offer.getId() + " already exists. ");
offers.put(offer.getId(), offer);
saveOffers();
messageFacade.addOffer(offer);
persistOffers();
}
public void removeOffer(Offer offer)
@ -141,7 +174,7 @@ public class Trading
}*/
offers.remove(offer.getId());
saveOffers();
persistOffers();
messageFacade.removeOffer(offer);
}
@ -401,7 +434,7 @@ public class Trading
// Private
///////////////////////////////////////////////////////////////////////////////////////////
private void saveOffers()
private void persistOffers()
{
persistence.write(this, "offers", offers);
}
@ -423,4 +456,6 @@ public class Trading
return null;
}
}
}

View File

@ -0,0 +1,9 @@
package io.bitsquare.trade.handlers;
/**
* For reporting error message only (UI)
*/
public interface ErrorMessageHandler
{
void onFault(String errorMessage);
}

View File

@ -0,0 +1,8 @@
package io.bitsquare.trade.handlers;
/**
* For reporting throwables only
*/
public interface ExceptionHandler
{
void onError(Throwable throwable);
}

View File

@ -0,0 +1,9 @@
package io.bitsquare.trade.handlers;
/**
* For reporting a description message and throwable
*/
public interface FaultHandler
{
void onFault(String message, Throwable throwable);
}

View File

@ -0,0 +1,6 @@
package io.bitsquare.trade.handlers;
public interface PublishTransactionResultHandler
{
void onResult(String transactionId);
}

View File

@ -1,4 +1,4 @@
package io.bitsquare.trade.protocol;
package io.bitsquare.trade.handlers;
public interface ResultHandler
{

View File

@ -9,7 +9,7 @@ import io.bitsquare.msg.listeners.OrderBookListener;
import io.bitsquare.settings.Settings;
import io.bitsquare.trade.Direction;
import io.bitsquare.trade.Offer;
import io.bitsquare.trade.Trading;
import io.bitsquare.trade.TradeManager;
import io.bitsquare.user.Arbitrator;
import io.bitsquare.user.User;
import java.io.IOException;
@ -42,7 +42,7 @@ public class OrderBook implements OrderBookListener
private final Settings settings;
private final User user;
private final MessageFacade messageFacade;
private final Trading trading;
private final TradeManager tradeManager;
///////////////////////////////////////////////////////////////////////////////////////////
@ -50,12 +50,12 @@ public class OrderBook implements OrderBookListener
///////////////////////////////////////////////////////////////////////////////////////////
@Inject
public OrderBook(Settings settings, User user, MessageFacade messageFacade, Trading trading)
public OrderBook(Settings settings, User user, MessageFacade messageFacade, TradeManager tradeManager)
{
this.settings = settings;
this.user = user;
this.messageFacade = messageFacade;
this.trading = trading;
this.tradeManager = tradeManager;
}
@ -87,7 +87,7 @@ public class OrderBook implements OrderBookListener
public void removeOffer(Offer offer)
{
trading.removeOffer(offer);
tradeManager.removeOffer(offer);
}
public void applyFilter(OrderBookFilter orderBookFilter)

View File

@ -1,6 +0,0 @@
package io.bitsquare.trade.protocol;
public interface FaultHandler
{
void onFault(Throwable throwable);
}

View File

@ -0,0 +1,112 @@
package io.bitsquare.trade.protocol.createoffer;
import io.bitsquare.btc.WalletFacade;
import io.bitsquare.msg.MessageFacade;
import io.bitsquare.trade.Offer;
import io.bitsquare.trade.handlers.FaultHandler;
import io.bitsquare.trade.handlers.PublishTransactionResultHandler;
import io.bitsquare.trade.protocol.createoffer.tasks.PayOfferFee;
import io.bitsquare.trade.protocol.createoffer.tasks.PublishOfferToDHT;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Responsible for coordinating tasks involved in the create offer process.
* It holds the state of the current process and support recovery if possible.
*/
//TODO recover policy, timer
public class CreateOfferCoordinator
{
private enum State
{
INIT,
OFFER_FEE_PAID,
OFFER_PUBLISHED_TO_DHT
}
private static final Logger log = LoggerFactory.getLogger(CreateOfferCoordinator.class);
public final Offer offer;
private final WalletFacade walletFacade;
private final MessageFacade messageFacade;
private PublishTransactionResultHandler resultHandler;
private FaultHandler faultHandler;
private String transactionId;
private State state;
public CreateOfferCoordinator(Offer offer, WalletFacade walletFacade, MessageFacade messageFacade)
{
this.offer = offer;
this.walletFacade = walletFacade;
this.messageFacade = messageFacade;
}
public void start(PublishTransactionResultHandler resultHandler, FaultHandler faultHandler)
{
this.resultHandler = resultHandler;
this.faultHandler = faultHandler;
state = State.INIT;
PayOfferFee.run(this::onOfferFeePaid, this::onFailed, walletFacade, offer);
}
public void recover(State lastState, String transactionId, PublishTransactionResultHandler resultHandler, FaultHandler faultHandler)
{
this.transactionId = transactionId;
this.resultHandler = resultHandler;
this.faultHandler = faultHandler;
switch (lastState)
{
case INIT:
PayOfferFee.run(this::onOfferFeePaid, this::onFailed, walletFacade, offer);
break;
case OFFER_FEE_PAID:
PublishOfferToDHT.run(this::onOfferPublishedToDHT, this::onFailed, messageFacade, offer);
break;
case OFFER_PUBLISHED_TO_DHT:
// should be impossible
resultHandler.onResult(transactionId);
break;
}
}
private void onOfferFeePaid(String transactionId)
{
state = State.OFFER_FEE_PAID;
this.transactionId = transactionId;
PublishOfferToDHT.run(this::onOfferPublishedToDHT, this::onFailed, messageFacade, offer);
}
private void onOfferPublishedToDHT()
{
state = State.OFFER_PUBLISHED_TO_DHT;
resultHandler.onResult(transactionId);
}
private void onFailed(String message, Throwable throwable)
{
//TODO recover policy, timer
faultHandler.onFault(message, throwable);
}
///////////////////////////////////////////////////////////////////////////////////////////
// Getters for persisting state
///////////////////////////////////////////////////////////////////////////////////////////
public String getTransactionId()
{
return transactionId;
}
public State getState()
{
return state;
}
}

View File

@ -0,0 +1,59 @@
package io.bitsquare.trade.protocol.createoffer.tasks;
import com.google.bitcoin.core.InsufficientMoneyException;
import com.google.bitcoin.core.Transaction;
import com.google.common.util.concurrent.FutureCallback;
import io.bitsquare.btc.WalletFacade;
import io.bitsquare.trade.Offer;
import io.bitsquare.trade.handlers.FaultHandler;
import io.bitsquare.trade.handlers.PublishTransactionResultHandler;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PayOfferFee
{
private static final Logger log = LoggerFactory.getLogger(PayOfferFee.class);
public static void run(PublishTransactionResultHandler publishTransactionResultHandler, FaultHandler faultHandler, WalletFacade walletFacade, Offer offer)
{
try
{
walletFacade.payCreateOfferFee(offer.getId(), new FutureCallback<Transaction>()
{
@Override
public void onSuccess(@javax.annotation.Nullable Transaction transaction)
{
log.info("sendResult onSuccess:" + transaction);
if (transaction != null)
{
offer.setOfferFeePaymentTxID(transaction.getHashAsString());
try
{
publishTransactionResultHandler.onResult(transaction.getHashAsString());
} catch (Exception e)
{
faultHandler.onFault("Offer fee payment failed.", e);
}
}
else
{
faultHandler.onFault("Offer fee payment failed.", new Exception("Offer fee payment failed. Transaction = null."));
}
}
@Override
public void onFailure(@NotNull Throwable t)
{
faultHandler.onFault("Offer fee payment failed with an exception.", t);
}
});
} catch (InsufficientMoneyException e)
{
faultHandler.onFault("Offer fee payment failed because there is insufficient money in the trade pocket. ", e);
} catch (Throwable t)
{
faultHandler.onFault("Offer fee payment failed because of an exception occurred. ", t);
}
}
}

View File

@ -0,0 +1,31 @@
package io.bitsquare.trade.protocol.createoffer.tasks;
import io.bitsquare.msg.MessageFacade;
import io.bitsquare.trade.Offer;
import io.bitsquare.trade.handlers.FaultHandler;
import io.bitsquare.trade.handlers.ResultHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PublishOfferToDHT
{
private static final Logger log = LoggerFactory.getLogger(PublishOfferToDHT.class);
public static void run(ResultHandler resultHandler, FaultHandler faultHandler, MessageFacade messageFacade, Offer offer)
{
messageFacade.addOffer(offer, new MessageFacade.AddOfferListener()
{
@Override
public void onComplete(String offerId)
{
resultHandler.onResult();
}
@Override
public void onFailed(String reason, Throwable throwable)
{
faultHandler.onFault("Publish offer to DHT failed.", throwable);
}
});
}
}

View File

@ -5,7 +5,7 @@ import com.google.bitcoin.core.InsufficientMoneyException;
import com.google.bitcoin.core.Transaction;
import com.google.bitcoin.core.Utils;
import io.bitsquare.btc.WalletFacade;
import io.bitsquare.trade.protocol.FaultHandler;
import io.bitsquare.trade.handlers.ExceptionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -14,7 +14,7 @@ public class CreateDepositTx
private static final Logger log = LoggerFactory.getLogger(CreateDepositTx.class);
public static void run(ResultHandler resultHandler,
FaultHandler faultHandler,
ExceptionHandler exceptionHandler,
WalletFacade walletFacade,
String tradeId,
Coin offererInputAmount,
@ -34,7 +34,7 @@ public class CreateDepositTx
} catch (InsufficientMoneyException e)
{
log.error("Create deposit tx faultHandler.onFault due InsufficientMoneyException " + e);
faultHandler.onFault(new Exception("Create deposit tx faultHandler.onFault due InsufficientMoneyException " + e));
exceptionHandler.onError(new Exception("Create deposit tx faultHandler.onFault due InsufficientMoneyException " + e));
}
}

View File

@ -3,7 +3,7 @@ package io.bitsquare.trade.protocol.offerer;
import io.bitsquare.msg.MessageFacade;
import io.bitsquare.msg.listeners.OutgoingTradeMessageListener;
import io.bitsquare.trade.Trade;
import io.bitsquare.trade.protocol.FaultHandler;
import io.bitsquare.trade.handlers.ExceptionHandler;
import net.tomp2p.peers.PeerAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -12,7 +12,7 @@ public class HandleTakeOfferRequest
{
private static final Logger log = LoggerFactory.getLogger(HandleTakeOfferRequest.class);
public static void run(ResultHandler resultHandler, FaultHandler faultHandler, PeerAddress peerAddress, MessageFacade messageFacade, Trade.State tradeState, String tradeId)
public static void run(ResultHandler resultHandler, ExceptionHandler exceptionHandler, PeerAddress peerAddress, MessageFacade messageFacade, Trade.State tradeState, String tradeId)
{
log.trace("Run task");
boolean takeOfferRequestAccepted = tradeState == Trade.State.OPEN;
@ -33,7 +33,7 @@ public class HandleTakeOfferRequest
public void onFailed()
{
log.error("AcceptTakeOfferRequestMessage did not arrive at peer");
faultHandler.onFault(new Exception("AcceptTakeOfferRequestMessage did not arrive at peer"));
exceptionHandler.onError(new Exception("AcceptTakeOfferRequestMessage did not arrive at peer"));
}
});
}

View File

@ -3,8 +3,8 @@ package io.bitsquare.trade.protocol.offerer;
import io.bitsquare.bank.BankAccount;
import io.bitsquare.msg.MessageFacade;
import io.bitsquare.msg.listeners.OutgoingTradeMessageListener;
import io.bitsquare.trade.protocol.FaultHandler;
import io.bitsquare.trade.protocol.ResultHandler;
import io.bitsquare.trade.handlers.ExceptionHandler;
import io.bitsquare.trade.handlers.ResultHandler;
import net.tomp2p.peers.PeerAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -14,7 +14,7 @@ public class RequestTakerDepositPayment
private static final Logger log = LoggerFactory.getLogger(RequestTakerDepositPayment.class);
public static void run(ResultHandler resultHandler,
FaultHandler faultHandler,
ExceptionHandler exceptionHandler,
PeerAddress peerAddress,
MessageFacade messageFacade,
String tradeId,
@ -39,7 +39,7 @@ public class RequestTakerDepositPayment
public void onFailed()
{
log.error("RequestTakerDepositPaymentMessage did not arrive at peer");
faultHandler.onFault(new Exception("RequestTakerDepositPaymentMessage did not arrive at peer"));
exceptionHandler.onError(new Exception("RequestTakerDepositPaymentMessage did not arrive at peer"));
}
});
}

View File

@ -4,8 +4,8 @@ import com.google.bitcoin.core.Transaction;
import com.google.bitcoin.core.Utils;
import io.bitsquare.msg.MessageFacade;
import io.bitsquare.msg.listeners.OutgoingTradeMessageListener;
import io.bitsquare.trade.protocol.FaultHandler;
import io.bitsquare.trade.protocol.ResultHandler;
import io.bitsquare.trade.handlers.ExceptionHandler;
import io.bitsquare.trade.handlers.ResultHandler;
import net.tomp2p.peers.PeerAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -14,7 +14,7 @@ public class SendDepositTxIdToTaker
{
private static final Logger log = LoggerFactory.getLogger(SendDepositTxIdToTaker.class);
public static void run(ResultHandler resultHandler, FaultHandler faultHandler, PeerAddress peerAddress, MessageFacade messageFacade, String tradeId, Transaction depositTransaction)
public static void run(ResultHandler resultHandler, ExceptionHandler exceptionHandler, PeerAddress peerAddress, MessageFacade messageFacade, String tradeId, Transaction depositTransaction)
{
log.trace("Run task");
DepositTxPublishedMessage tradeMessage = new DepositTxPublishedMessage(tradeId, Utils.HEX.encode(depositTransaction.bitcoinSerialize()));
@ -31,7 +31,7 @@ public class SendDepositTxIdToTaker
public void onFailed()
{
log.error("DepositTxPublishedMessage did not arrive at peer");
faultHandler.onFault(new Exception("DepositTxPublishedMessage did not arrive at peer"));
exceptionHandler.onError(new Exception("DepositTxPublishedMessage did not arrive at peer"));
}
});
}

View File

@ -5,8 +5,8 @@ import com.google.bitcoin.core.ECKey;
import io.bitsquare.btc.WalletFacade;
import io.bitsquare.msg.MessageFacade;
import io.bitsquare.msg.listeners.OutgoingTradeMessageListener;
import io.bitsquare.trade.protocol.FaultHandler;
import io.bitsquare.trade.protocol.ResultHandler;
import io.bitsquare.trade.handlers.ExceptionHandler;
import io.bitsquare.trade.handlers.ResultHandler;
import javafx.util.Pair;
import net.tomp2p.peers.PeerAddress;
import org.slf4j.Logger;
@ -17,7 +17,7 @@ public class SendSignedPayoutTx
private static final Logger log = LoggerFactory.getLogger(SendSignedPayoutTx.class);
public static void run(ResultHandler resultHandler,
FaultHandler faultHandler,
ExceptionHandler exceptionHandler,
PeerAddress peerAddress,
MessageFacade messageFacade,
WalletFacade walletFacade,
@ -62,14 +62,14 @@ public class SendSignedPayoutTx
public void onFailed()
{
log.error("BankTransferInitedMessage did not arrive at peer");
faultHandler.onFault(new Exception("BankTransferInitedMessage did not arrive at peer"));
exceptionHandler.onError(new Exception("BankTransferInitedMessage did not arrive at peer"));
}
});
} catch (Exception e)
{
log.error("Exception at OffererCreatesAndSignsPayoutTx " + e);
faultHandler.onFault(e);
exceptionHandler.onError(e);
}
}
}

View File

@ -2,8 +2,8 @@ package io.bitsquare.trade.protocol.offerer;
import com.google.bitcoin.core.Transaction;
import com.google.bitcoin.core.TransactionConfidence;
import io.bitsquare.trade.protocol.FaultHandler;
import io.bitsquare.trade.protocol.ResultHandler;
import io.bitsquare.trade.handlers.ExceptionHandler;
import io.bitsquare.trade.handlers.ResultHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -11,7 +11,7 @@ public class SetupListenerForBlockChainConfirmation
{
private static final Logger log = LoggerFactory.getLogger(SetupListenerForBlockChainConfirmation.class);
public static void run(ResultHandler resultHandler, FaultHandler faultHandler, Transaction depositTransaction, ProtocolForOffererAsBuyerListener listener)
public static void run(ResultHandler resultHandler, ExceptionHandler exceptionHandler, Transaction depositTransaction, ProtocolForOffererAsBuyerListener listener)
{
log.trace("Run task");
//TODO

View File

@ -3,7 +3,7 @@ package io.bitsquare.trade.protocol.offerer;
import com.google.bitcoin.core.Transaction;
import com.google.common.util.concurrent.FutureCallback;
import io.bitsquare.btc.WalletFacade;
import io.bitsquare.trade.protocol.FaultHandler;
import io.bitsquare.trade.handlers.ExceptionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -12,7 +12,7 @@ public class SignAndPublishDepositTx
private static final Logger log = LoggerFactory.getLogger(SignAndPublishDepositTx.class);
public static void run(ResultHandler resultHandler,
FaultHandler faultHandler,
ExceptionHandler exceptionHandler,
WalletFacade walletFacade,
String preparedOffererDepositTxAsHex,
String signedTakerDepositTxAsHex,
@ -43,13 +43,13 @@ public class SignAndPublishDepositTx
public void onFailure(Throwable t)
{
log.error("offererSignAndPublishTx faultHandler.onFault:" + t);
faultHandler.onFault(t);
exceptionHandler.onError(t);
}
});
} catch (Exception e)
{
log.error("offererSignAndPublishTx faultHandler.onFault:" + e);
faultHandler.onFault(e);
exceptionHandler.onError(e);
}
}

View File

@ -6,7 +6,7 @@ import io.bitsquare.bank.BankAccount;
import io.bitsquare.crypto.CryptoFacade;
import io.bitsquare.trade.Contract;
import io.bitsquare.trade.Offer;
import io.bitsquare.trade.protocol.FaultHandler;
import io.bitsquare.trade.handlers.ExceptionHandler;
import io.bitsquare.util.Utilities;
import java.security.PublicKey;
import org.slf4j.Logger;
@ -17,7 +17,7 @@ public class VerifyAndSignContract
private static final Logger log = LoggerFactory.getLogger(VerifyAndSignContract.class);
public static void run(ResultHandler resultHandler,
FaultHandler faultHandler,
ExceptionHandler exceptionHandler,
CryptoFacade cryptoFacade,
String accountId,
Coin tradeAmount,

View File

@ -1,8 +1,8 @@
package io.bitsquare.trade.protocol.offerer;
import io.bitsquare.btc.WalletFacade;
import io.bitsquare.trade.protocol.FaultHandler;
import io.bitsquare.trade.protocol.ResultHandler;
import io.bitsquare.trade.handlers.ExceptionHandler;
import io.bitsquare.trade.handlers.ResultHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -10,7 +10,7 @@ public class VerifyTakeOfferFeePayment
{
private static final Logger log = LoggerFactory.getLogger(VerifyTakeOfferFeePayment.class);
public static void run(ResultHandler resultHandler, FaultHandler faultHandler, WalletFacade walletFacade, String takeOfferFeeTxId)
public static void run(ResultHandler resultHandler, ExceptionHandler exceptionHandler, WalletFacade walletFacade, String takeOfferFeeTxId)
{
log.trace("Run task");
//TODO mocked yet, need a confidence listeners

View File

@ -2,8 +2,8 @@ package io.bitsquare.trade.protocol.offerer;
import io.bitsquare.bank.BankAccount;
import io.bitsquare.btc.BlockChainFacade;
import io.bitsquare.trade.protocol.FaultHandler;
import io.bitsquare.trade.protocol.ResultHandler;
import io.bitsquare.trade.handlers.ExceptionHandler;
import io.bitsquare.trade.handlers.ResultHandler;
import io.bitsquare.trade.protocol.shared.VerifyPeerAccount;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -12,10 +12,10 @@ public class VerifyTakerAccount
{
private static final Logger log = LoggerFactory.getLogger(VerifyTakerAccount.class);
public static void run(ResultHandler resultHandler, FaultHandler faultHandler, BlockChainFacade blockChainFacade, String peersAccountId, BankAccount peersBankAccount)
public static void run(ResultHandler resultHandler, ExceptionHandler exceptionHandler, BlockChainFacade blockChainFacade, String peersAccountId, BankAccount peersBankAccount)
{
log.trace("Run task");
VerifyPeerAccount.run(resultHandler, faultHandler, blockChainFacade, peersAccountId, peersBankAccount);
VerifyPeerAccount.run(resultHandler, exceptionHandler, blockChainFacade, peersAccountId, peersBankAccount);
}
}

View File

@ -2,8 +2,8 @@ package io.bitsquare.trade.protocol.shared;
import io.bitsquare.bank.BankAccount;
import io.bitsquare.btc.BlockChainFacade;
import io.bitsquare.trade.protocol.FaultHandler;
import io.bitsquare.trade.protocol.ResultHandler;
import io.bitsquare.trade.handlers.ExceptionHandler;
import io.bitsquare.trade.handlers.ResultHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -11,7 +11,7 @@ public class VerifyPeerAccount
{
private static final Logger log = LoggerFactory.getLogger(VerifyPeerAccount.class);
public static void run(ResultHandler resultHandler, FaultHandler faultHandler, BlockChainFacade blockChainFacade, String peersAccountId, BankAccount peersBankAccount)
public static void run(ResultHandler resultHandler, ExceptionHandler exceptionHandler, BlockChainFacade blockChainFacade, String peersAccountId, BankAccount peersBankAccount)
{
//TODO mocked yet
if (blockChainFacade.verifyAccountRegistration())
@ -19,7 +19,7 @@ public class VerifyPeerAccount
if (blockChainFacade.isAccountBlackListed(peersAccountId, peersBankAccount))
{
log.error("Taker is blacklisted");
faultHandler.onFault(new Exception("Taker is blacklisted"));
exceptionHandler.onError(new Exception("Taker is blacklisted"));
}
else
{
@ -29,7 +29,7 @@ public class VerifyPeerAccount
else
{
log.error("Account registration validation for peer faultHandler.onFault.");
faultHandler.onFault(new Exception("Account registration validation for peer faultHandler.onFault."));
exceptionHandler.onError(new Exception("Account registration validation for peer faultHandler.onFault."));
}
}

View File

@ -6,7 +6,7 @@ import io.bitsquare.bank.BankAccount;
import io.bitsquare.crypto.CryptoFacade;
import io.bitsquare.trade.Contract;
import io.bitsquare.trade.Offer;
import io.bitsquare.trade.protocol.FaultHandler;
import io.bitsquare.trade.handlers.ExceptionHandler;
import io.bitsquare.util.Utilities;
import java.security.PublicKey;
import org.slf4j.Logger;
@ -17,7 +17,7 @@ public class CreateAndSignContract
private static final Logger log = LoggerFactory.getLogger(CreateAndSignContract.class);
public static void run(ResultHandler resultHandler,
FaultHandler faultHandler,
ExceptionHandler exceptionHandler,
CryptoFacade cryptoFacade,
Offer offer,
Coin tradeAmount,
@ -41,7 +41,7 @@ public class CreateAndSignContract
} catch (Throwable t)
{
log.error("Exception at sign contract " + t);
faultHandler.onFault(t);
exceptionHandler.onError(t);
}
}

View File

@ -2,7 +2,7 @@ package io.bitsquare.trade.protocol.taker;
import io.bitsquare.msg.MessageFacade;
import io.bitsquare.msg.listeners.GetPeerAddressListener;
import io.bitsquare.trade.protocol.FaultHandler;
import io.bitsquare.trade.handlers.ExceptionHandler;
import java.security.PublicKey;
import net.tomp2p.peers.PeerAddress;
import org.slf4j.Logger;
@ -12,7 +12,7 @@ public class GetPeerAddress
{
private static final Logger log = LoggerFactory.getLogger(GetPeerAddress.class);
public static void run(ResultHandler resultHandler, FaultHandler faultHandler, MessageFacade messageFacade, PublicKey messagePublicKey)
public static void run(ResultHandler resultHandler, ExceptionHandler exceptionHandler, MessageFacade messageFacade, PublicKey messagePublicKey)
{
log.trace("Run task");
messageFacade.getPeerAddress(messagePublicKey, new GetPeerAddressListener()
@ -28,7 +28,7 @@ public class GetPeerAddress
public void onFailed()
{
log.error("Lookup for peer address faultHandler.onFault.");
faultHandler.onFault(new Exception("Lookup for peer address faultHandler.onFault."));
exceptionHandler.onError(new Exception("Lookup for peer address faultHandler.onFault."));
}
});
}

View File

@ -4,7 +4,7 @@ import com.google.bitcoin.core.Coin;
import com.google.bitcoin.core.InsufficientMoneyException;
import com.google.bitcoin.core.Transaction;
import io.bitsquare.btc.WalletFacade;
import io.bitsquare.trade.protocol.FaultHandler;
import io.bitsquare.trade.handlers.ExceptionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -13,7 +13,7 @@ public class PayDeposit
private static final Logger log = LoggerFactory.getLogger(PayDeposit.class);
public static void run(ResultHandler resultHandler,
FaultHandler faultHandler,
ExceptionHandler exceptionHandler,
WalletFacade walletFacade,
Coin collateral,
Coin tradeAmount,
@ -42,7 +42,7 @@ public class PayDeposit
} catch (InsufficientMoneyException e)
{
log.error("Pay deposit faultHandler.onFault due InsufficientMoneyException " + e);
faultHandler.onFault(new Exception("Pay deposit faultHandler.onFault due InsufficientMoneyException " + e));
exceptionHandler.onError(new Exception("Pay deposit faultHandler.onFault due InsufficientMoneyException " + e));
}
}

View File

@ -4,7 +4,7 @@ import com.google.bitcoin.core.InsufficientMoneyException;
import com.google.bitcoin.core.Transaction;
import com.google.common.util.concurrent.FutureCallback;
import io.bitsquare.btc.WalletFacade;
import io.bitsquare.trade.protocol.FaultHandler;
import io.bitsquare.trade.handlers.ExceptionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -12,7 +12,7 @@ public class PayTakeOfferFee
{
private static final Logger log = LoggerFactory.getLogger(PayTakeOfferFee.class);
public static void run(ResultHandler resultHandler, FaultHandler faultHandler, WalletFacade walletFacade, String tradeId)
public static void run(ResultHandler resultHandler, ExceptionHandler exceptionHandler, WalletFacade walletFacade, String tradeId)
{
log.trace("Run task");
try
@ -30,13 +30,13 @@ public class PayTakeOfferFee
public void onFailure(Throwable t)
{
log.error("Take offer fee paid faultHandler.onFault with exception: " + t);
faultHandler.onFault(new Exception("Take offer fee paid faultHandler.onFault with exception: " + t));
exceptionHandler.onError(new Exception("Take offer fee paid faultHandler.onFault with exception: " + t));
}
});
} catch (InsufficientMoneyException e)
{
log.error("Take offer fee paid faultHandler.onFault due InsufficientMoneyException " + e);
faultHandler.onFault(new Exception("Take offer fee paid faultHandler.onFault due InsufficientMoneyException " + e));
exceptionHandler.onError(new Exception("Take offer fee paid faultHandler.onFault due InsufficientMoneyException " + e));
}
}

View File

@ -2,8 +2,8 @@ package io.bitsquare.trade.protocol.taker;
import io.bitsquare.msg.MessageFacade;
import io.bitsquare.msg.listeners.OutgoingTradeMessageListener;
import io.bitsquare.trade.protocol.FaultHandler;
import io.bitsquare.trade.protocol.ResultHandler;
import io.bitsquare.trade.handlers.ExceptionHandler;
import io.bitsquare.trade.handlers.ResultHandler;
import net.tomp2p.peers.PeerAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -12,7 +12,7 @@ public class RequestTakeOffer
{
private static final Logger log = LoggerFactory.getLogger(RequestTakeOffer.class);
public static void run(ResultHandler resultHandler, FaultHandler faultHandler, PeerAddress peerAddress, MessageFacade messageFacade, String tradeId)
public static void run(ResultHandler resultHandler, ExceptionHandler exceptionHandler, PeerAddress peerAddress, MessageFacade messageFacade, String tradeId)
{
log.trace("Run task");
messageFacade.sendTradeMessage(peerAddress, new RequestTakeOfferMessage(tradeId), new OutgoingTradeMessageListener()
@ -28,7 +28,7 @@ public class RequestTakeOffer
public void onFailed()
{
log.error("RequestTakeOfferMessage did not arrive at peer");
faultHandler.onFault(new Exception("RequestTakeOfferMessage did not arrive at peer"));
exceptionHandler.onError(new Exception("RequestTakeOfferMessage did not arrive at peer"));
}
});
}

View File

@ -2,8 +2,8 @@ package io.bitsquare.trade.protocol.taker;
import io.bitsquare.msg.MessageFacade;
import io.bitsquare.msg.listeners.OutgoingTradeMessageListener;
import io.bitsquare.trade.protocol.FaultHandler;
import io.bitsquare.trade.protocol.ResultHandler;
import io.bitsquare.trade.handlers.ExceptionHandler;
import io.bitsquare.trade.handlers.ResultHandler;
import net.tomp2p.peers.PeerAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -12,7 +12,7 @@ public class SendPayoutTxToOfferer
{
private static final Logger log = LoggerFactory.getLogger(SendPayoutTxToOfferer.class);
public static void run(ResultHandler resultHandler, FaultHandler faultHandler, PeerAddress peerAddress, MessageFacade messageFacade, String tradeId, String payoutTxAsHex)
public static void run(ResultHandler resultHandler, ExceptionHandler exceptionHandler, PeerAddress peerAddress, MessageFacade messageFacade, String tradeId, String payoutTxAsHex)
{
log.trace("Run task");
PayoutTxPublishedMessage tradeMessage = new PayoutTxPublishedMessage(tradeId, payoutTxAsHex);
@ -29,7 +29,7 @@ public class SendPayoutTxToOfferer
public void onFailed()
{
log.error("PayoutTxPublishedMessage did not arrive at peer");
faultHandler.onFault(new Exception("PayoutTxPublishedMessage did not arrive at peer"));
exceptionHandler.onError(new Exception("PayoutTxPublishedMessage did not arrive at peer"));
}
});

View File

@ -6,8 +6,8 @@ import io.bitsquare.bank.BankAccount;
import io.bitsquare.btc.WalletFacade;
import io.bitsquare.msg.MessageFacade;
import io.bitsquare.msg.listeners.OutgoingTradeMessageListener;
import io.bitsquare.trade.protocol.FaultHandler;
import io.bitsquare.trade.protocol.ResultHandler;
import io.bitsquare.trade.handlers.ExceptionHandler;
import io.bitsquare.trade.handlers.ResultHandler;
import java.security.PublicKey;
import net.tomp2p.peers.PeerAddress;
import org.slf4j.Logger;
@ -18,7 +18,7 @@ public class SendSignedTakerDepositTxAsHex
private static final Logger log = LoggerFactory.getLogger(SendSignedTakerDepositTxAsHex.class);
public static void run(ResultHandler resultHandler,
FaultHandler faultHandler,
ExceptionHandler exceptionHandler,
PeerAddress peerAddress,
MessageFacade messageFacade,
WalletFacade walletFacade,
@ -62,7 +62,7 @@ public class SendSignedTakerDepositTxAsHex
public void onFailed()
{
log.error("RequestOffererDepositPublicationMessage did not arrive at peer");
faultHandler.onFault(new Exception("RequestOffererDepositPublicationMessage did not arrive at peer"));
exceptionHandler.onError(new Exception("RequestOffererDepositPublicationMessage did not arrive at peer"));
}
});
}

View File

@ -3,8 +3,8 @@ package io.bitsquare.trade.protocol.taker;
import com.google.bitcoin.core.Coin;
import io.bitsquare.msg.MessageFacade;
import io.bitsquare.msg.listeners.OutgoingTradeMessageListener;
import io.bitsquare.trade.protocol.FaultHandler;
import io.bitsquare.trade.protocol.ResultHandler;
import io.bitsquare.trade.handlers.ExceptionHandler;
import io.bitsquare.trade.handlers.ResultHandler;
import net.tomp2p.peers.PeerAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -14,7 +14,7 @@ public class SendTakeOfferFeePayedTxId
private static final Logger log = LoggerFactory.getLogger(SendTakeOfferFeePayedTxId.class);
public static void run(ResultHandler resultHandler,
FaultHandler faultHandler,
ExceptionHandler exceptionHandler,
PeerAddress peerAddress,
MessageFacade messageFacade,
String tradeId,
@ -38,7 +38,7 @@ public class SendTakeOfferFeePayedTxId
public void onFailed()
{
log.error("TakeOfferFeePayedMessage did not arrive at peer");
faultHandler.onFault(new Exception("TakeOfferFeePayedMessage did not arrive at peer"));
exceptionHandler.onError(new Exception("TakeOfferFeePayedMessage did not arrive at peer"));
}
});
}

View File

@ -5,7 +5,7 @@ import com.google.bitcoin.core.Transaction;
import com.google.bitcoin.core.Utils;
import com.google.common.util.concurrent.FutureCallback;
import io.bitsquare.btc.WalletFacade;
import io.bitsquare.trade.protocol.FaultHandler;
import io.bitsquare.trade.handlers.ExceptionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -14,7 +14,7 @@ public class SignAndPublishPayoutTx
private static final Logger log = LoggerFactory.getLogger(SignAndPublishPayoutTx.class);
public static void run(ResultHandler resultHandler,
FaultHandler faultHandler,
ExceptionHandler exceptionHandler,
WalletFacade walletFacade,
String tradeId,
String depositTxAsHex,
@ -49,13 +49,13 @@ public class SignAndPublishPayoutTx
public void onFailure(Throwable t)
{
log.error("Exception at takerSignsAndSendsTx " + t);
faultHandler.onFault(t);
exceptionHandler.onError(t);
}
});
} catch (Exception e)
{
log.error("Exception at takerSignsAndSendsTx " + e);
faultHandler.onFault(e);
exceptionHandler.onError(e);
}
}

View File

@ -2,8 +2,8 @@ package io.bitsquare.trade.protocol.taker;
import io.bitsquare.bank.BankAccount;
import io.bitsquare.btc.BlockChainFacade;
import io.bitsquare.trade.protocol.FaultHandler;
import io.bitsquare.trade.protocol.ResultHandler;
import io.bitsquare.trade.handlers.ExceptionHandler;
import io.bitsquare.trade.handlers.ResultHandler;
import io.bitsquare.trade.protocol.shared.VerifyPeerAccount;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -12,9 +12,9 @@ public class VerifyOffererAccount
{
private static final Logger log = LoggerFactory.getLogger(VerifyOffererAccount.class);
public static void run(ResultHandler resultHandler, FaultHandler faultHandler, BlockChainFacade blockChainFacade, String peersAccountId, BankAccount peersBankAccount)
public static void run(ResultHandler resultHandler, ExceptionHandler exceptionHandler, BlockChainFacade blockChainFacade, String peersAccountId, BankAccount peersBankAccount)
{
log.trace("Run task");
VerifyPeerAccount.run(resultHandler, faultHandler, blockChainFacade, peersAccountId, peersBankAccount);
VerifyPeerAccount.run(resultHandler, exceptionHandler, blockChainFacade, peersAccountId, peersBankAccount);
}
}