mirror of
https://github.com/haveno-dex/haveno.git
synced 2025-04-19 15:26:03 -04:00
createOfferCoordinator updated
This commit is contained in:
parent
0fdab0d309
commit
7069fb6c4d
@ -641,8 +641,9 @@ public class WalletFacade
|
||||
printInputs("payRegistrationFee", tx);
|
||||
}
|
||||
|
||||
public String payCreateOfferFee(String offerId, FutureCallback<Transaction> callback) throws InsufficientMoneyException
|
||||
public Transaction createOfferFeeTx(String offerId) throws InsufficientMoneyException
|
||||
{
|
||||
log.trace("createOfferFeeTx");
|
||||
Transaction tx = new Transaction(params);
|
||||
Coin fee = FeePolicy.CREATE_OFFER_FEE.subtract(FeePolicy.TX_FEE);
|
||||
log.trace("fee: " + fee.toFriendlyString());
|
||||
@ -653,15 +654,17 @@ public class WalletFacade
|
||||
// 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(params, getAddressInfoByTradeID(offerId), true);
|
||||
sendRequest.changeAddress = getAddressInfoByTradeID(offerId).getAddress();
|
||||
Wallet.SendResult sendResult = wallet.sendCoins(sendRequest);
|
||||
Futures.addCallback(sendResult.broadcastComplete, callback);
|
||||
|
||||
wallet.completeTx(sendRequest);
|
||||
printInputs("payCreateOfferFee", tx);
|
||||
log.debug("tx=" + tx);
|
||||
|
||||
return tx.getHashAsString();
|
||||
return tx;
|
||||
}
|
||||
|
||||
public void broadcastCreateOfferFeeTx(Transaction tx, FutureCallback<Transaction> callback) throws InsufficientMoneyException
|
||||
{
|
||||
log.trace("broadcast tx");
|
||||
ListenableFuture<Transaction> future = walletAppKit.peerGroup().broadcastTransaction(tx);
|
||||
Futures.addCallback(future, callback);
|
||||
}
|
||||
|
||||
public String payTakeOfferFee(String offerId, FutureCallback<Transaction> callback) throws InsufficientMoneyException
|
||||
{
|
||||
|
@ -232,7 +232,7 @@ public class CreateOfferController implements Initializable, ChildController, Hi
|
||||
if (inputsValid())
|
||||
{
|
||||
placeOfferButton.setDisable(true);
|
||||
|
||||
|
||||
double price = BitSquareConverter.stringToDouble(priceTextField.getText());
|
||||
Coin amount = BitSquareFormatter.parseBtcToCoin(getAmountString());
|
||||
Coin minAmount = BitSquareFormatter.parseBtcToCoin(getMinAmountString());
|
||||
@ -241,7 +241,7 @@ public class CreateOfferController implements Initializable, ChildController, Hi
|
||||
price,
|
||||
amount,
|
||||
minAmount,
|
||||
(transactionId) -> setupSuccessScreen(transactionId),
|
||||
(transaction) -> setupSuccessScreen(transaction.getHashAsString()),
|
||||
errorMessage -> {
|
||||
Popups.openErrorPopup("An error occurred", errorMessage);
|
||||
placeOfferButton.setDisable(false);
|
||||
|
@ -13,7 +13,7 @@ import io.bitsquare.msg.listeners.TakeOfferRequestListener;
|
||||
import io.bitsquare.settings.Settings;
|
||||
import io.bitsquare.storage.Persistence;
|
||||
import io.bitsquare.trade.handlers.ErrorMessageHandler;
|
||||
import io.bitsquare.trade.handlers.PublishTransactionResultHandler;
|
||||
import io.bitsquare.trade.handlers.TransactionResultHandler;
|
||||
import io.bitsquare.trade.protocol.TradeMessage;
|
||||
import io.bitsquare.trade.protocol.createoffer.CreateOfferCoordinator;
|
||||
import io.bitsquare.trade.protocol.offerer.*;
|
||||
@ -131,7 +131,7 @@ public class TradeManager
|
||||
double price,
|
||||
Coin amount,
|
||||
Coin minAmount,
|
||||
PublishTransactionResultHandler resultHandler,
|
||||
TransactionResultHandler resultHandler,
|
||||
ErrorMessageHandler errorMessageHandler)
|
||||
{
|
||||
|
||||
@ -155,30 +155,31 @@ public class TradeManager
|
||||
}
|
||||
else
|
||||
{
|
||||
CreateOfferCoordinator createOfferCoordinator = new CreateOfferCoordinator(persistence,
|
||||
offer,
|
||||
walletFacade,
|
||||
messageFacade,
|
||||
(transactionId) -> {
|
||||
try
|
||||
{
|
||||
addOffer(offer);
|
||||
offer.setOfferFeePaymentTxID(transactionId.getHashAsString());
|
||||
createOfferCoordinatorMap.remove(offer.getId());
|
||||
|
||||
|
||||
CreateOfferCoordinator createOfferCoordinator = new CreateOfferCoordinator(offer, walletFacade, messageFacade);
|
||||
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());
|
||||
});
|
||||
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());
|
||||
});
|
||||
createOfferCoordinator.start();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,6 +0,0 @@
|
||||
package io.bitsquare.trade.handlers;
|
||||
|
||||
public interface PublishTransactionResultHandler
|
||||
{
|
||||
void onResult(String transactionId);
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package io.bitsquare.trade.handlers;
|
||||
|
||||
import com.google.bitcoin.core.Transaction;
|
||||
|
||||
public interface TransactionResultHandler
|
||||
{
|
||||
void onResult(Transaction transaction);
|
||||
}
|
@ -1,83 +1,126 @@
|
||||
package io.bitsquare.trade.protocol.createoffer;
|
||||
|
||||
import com.google.bitcoin.core.Transaction;
|
||||
import io.bitsquare.btc.WalletFacade;
|
||||
import io.bitsquare.msg.MessageFacade;
|
||||
import io.bitsquare.storage.Persistence;
|
||||
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.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.PublishOfferToDHT;
|
||||
import io.bitsquare.trade.protocol.createoffer.tasks.ValidateOffer;
|
||||
import java.io.Serializable;
|
||||
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.
|
||||
* It holds the model.state of the current process and support recovery if possible.
|
||||
*/
|
||||
//TODO recover policy, timer
|
||||
public class CreateOfferCoordinator
|
||||
{
|
||||
public enum State
|
||||
{
|
||||
INIT,
|
||||
OFFER_FEE_PAID,
|
||||
INITED,
|
||||
STARTED,
|
||||
VALIDATED,
|
||||
OFFER_FEE_TX_CREATED,
|
||||
OFFER_FEE_BROAD_CASTED,
|
||||
OFFER_PUBLISHED_TO_DHT
|
||||
}
|
||||
|
||||
static class Model implements Serializable
|
||||
{
|
||||
private static final long serialVersionUID = 3027720554200858916L;
|
||||
|
||||
private Persistence persistence;
|
||||
private State state;
|
||||
//TODO use tx id and make Transaction transient
|
||||
Transaction transaction;
|
||||
|
||||
Model(Persistence persistence)
|
||||
{
|
||||
this.persistence = persistence;
|
||||
}
|
||||
|
||||
public State getState()
|
||||
{
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(State state)
|
||||
{
|
||||
this.state = state;
|
||||
persistence.write(this);
|
||||
}
|
||||
}
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(CreateOfferCoordinator.class);
|
||||
|
||||
private final Offer offer;
|
||||
private final WalletFacade walletFacade;
|
||||
private final MessageFacade messageFacade;
|
||||
private PublishTransactionResultHandler resultHandler;
|
||||
private FaultHandler faultHandler;
|
||||
private final TransactionResultHandler resultHandler;
|
||||
private final FaultHandler faultHandler;
|
||||
private final Model model;
|
||||
|
||||
private State state;
|
||||
public CreateOfferCoordinator(Persistence persistence, Offer offer, WalletFacade walletFacade, MessageFacade messageFacade, TransactionResultHandler resultHandler, FaultHandler faultHandler)
|
||||
{
|
||||
this(offer, walletFacade, messageFacade, resultHandler, faultHandler, new Model(persistence));
|
||||
}
|
||||
|
||||
// result
|
||||
private String transactionId;
|
||||
|
||||
public CreateOfferCoordinator(Offer offer, WalletFacade walletFacade, MessageFacade messageFacade)
|
||||
// for recovery from model
|
||||
public CreateOfferCoordinator(Offer offer, WalletFacade walletFacade, MessageFacade messageFacade, TransactionResultHandler resultHandler, FaultHandler faultHandler, Model model)
|
||||
{
|
||||
this.offer = offer;
|
||||
this.walletFacade = walletFacade;
|
||||
this.messageFacade = messageFacade;
|
||||
}
|
||||
|
||||
public void start(PublishTransactionResultHandler resultHandler, FaultHandler faultHandler)
|
||||
{
|
||||
this.resultHandler = resultHandler;
|
||||
this.faultHandler = faultHandler;
|
||||
this.model = model;
|
||||
|
||||
state = State.INIT;
|
||||
model.setState(State.INITED);
|
||||
}
|
||||
|
||||
public void start()
|
||||
{
|
||||
model.setState(State.STARTED);
|
||||
ValidateOffer.run(this::onOfferValidated, this::onFailed, offer);
|
||||
}
|
||||
|
||||
private void onOfferValidated()
|
||||
{
|
||||
PayOfferFee.run(this::onOfferFeePaid, this::onFailed, walletFacade, offer);
|
||||
model.setState(State.VALIDATED);
|
||||
CreateOfferFeeTx.run(this::onOfferFeeTxCreated, this::onFailed, walletFacade, offer);
|
||||
}
|
||||
|
||||
private void onOfferFeePaid(String transactionId)
|
||||
private void onOfferFeeTxCreated(Transaction transaction)
|
||||
{
|
||||
state = State.OFFER_FEE_PAID;
|
||||
model.transaction = transaction;
|
||||
model.setState(State.OFFER_FEE_TX_CREATED);
|
||||
BroadCastOfferFeeTx.run(this::onOfferFeeTxBroadCasted, this::onFailed, walletFacade, transaction);
|
||||
}
|
||||
|
||||
private void onOfferFeeTxBroadCasted()
|
||||
{
|
||||
model.setState(State.OFFER_FEE_BROAD_CASTED);
|
||||
|
||||
this.transactionId = transactionId;
|
||||
PublishOfferToDHT.run(this::onOfferPublishedToDHT, this::onFailed, messageFacade, offer);
|
||||
}
|
||||
|
||||
private void onOfferPublishedToDHT()
|
||||
{
|
||||
state = State.OFFER_PUBLISHED_TO_DHT;
|
||||
model.setState(State.OFFER_PUBLISHED_TO_DHT);
|
||||
|
||||
resultHandler.onResult(transactionId);
|
||||
resultHandler.onResult(model.transaction);
|
||||
}
|
||||
|
||||
private void onFailed(String message, Throwable throwable)
|
||||
{
|
||||
//TODO recover policy, timer
|
||||
|
||||
faultHandler.onFault(message, throwable);
|
||||
}
|
||||
|
||||
@ -86,38 +129,31 @@ public class CreateOfferCoordinator
|
||||
// Recovery
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
public void recover(State lastState, String transactionId, PublishTransactionResultHandler resultHandler, FaultHandler faultHandler)
|
||||
public void recover()
|
||||
{
|
||||
this.transactionId = transactionId;
|
||||
this.resultHandler = resultHandler;
|
||||
this.faultHandler = faultHandler;
|
||||
switch (lastState)
|
||||
switch (model.getState())
|
||||
{
|
||||
case INIT:
|
||||
PayOfferFee.run(this::onOfferFeePaid, this::onFailed, walletFacade, offer);
|
||||
case INITED:
|
||||
case STARTED:
|
||||
// no need for recover, just call start
|
||||
break;
|
||||
case OFFER_FEE_PAID:
|
||||
PublishOfferToDHT.run(this::onOfferPublishedToDHT, this::onFailed, messageFacade, offer);
|
||||
case VALIDATED:
|
||||
onOfferValidated();
|
||||
break;
|
||||
case OFFER_FEE_TX_CREATED:
|
||||
onOfferFeeTxCreated(model.transaction);
|
||||
break;
|
||||
case OFFER_FEE_BROAD_CASTED:
|
||||
onOfferFeeTxBroadCasted();
|
||||
break;
|
||||
case OFFER_PUBLISHED_TO_DHT:
|
||||
// should be impossible
|
||||
resultHandler.onResult(transactionId);
|
||||
onOfferPublishedToDHT();
|
||||
break;
|
||||
default:
|
||||
log.error("Must not happen");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Getters for persisting state
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
public String getTransactionId()
|
||||
{
|
||||
return transactionId;
|
||||
}
|
||||
|
||||
public State getState()
|
||||
{
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
@ -4,22 +4,21 @@ 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 io.bitsquare.trade.handlers.ResultHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class PayOfferFee
|
||||
public class BroadCastOfferFeeTx
|
||||
{
|
||||
private static final Logger log = LoggerFactory.getLogger(PayOfferFee.class);
|
||||
private static final Logger log = LoggerFactory.getLogger(BroadCastOfferFeeTx.class);
|
||||
|
||||
public static void run(PublishTransactionResultHandler publishTransactionResultHandler, FaultHandler faultHandler, WalletFacade walletFacade, Offer offer)
|
||||
public static void run(ResultHandler resultHandler, FaultHandler faultHandler, WalletFacade walletFacade, Transaction tx)
|
||||
{
|
||||
try
|
||||
{
|
||||
walletFacade.payCreateOfferFee(offer.getId(), new FutureCallback<Transaction>()
|
||||
walletFacade.broadcastCreateOfferFeeTx(tx, new FutureCallback<Transaction>()
|
||||
{
|
||||
@Override
|
||||
public void onSuccess(@javax.annotation.Nullable Transaction transaction)
|
||||
@ -27,10 +26,9 @@ public class PayOfferFee
|
||||
log.info("sendResult onSuccess:" + transaction);
|
||||
if (transaction != null)
|
||||
{
|
||||
offer.setOfferFeePaymentTxID(transaction.getHashAsString());
|
||||
try
|
||||
{
|
||||
publishTransactionResultHandler.onResult(transaction.getHashAsString());
|
||||
resultHandler.onResult();
|
||||
} catch (Exception e)
|
||||
{
|
||||
faultHandler.onFault("Offer fee payment failed.", e);
|
@ -0,0 +1,28 @@
|
||||
package io.bitsquare.trade.protocol.createoffer.tasks;
|
||||
|
||||
import com.google.bitcoin.core.InsufficientMoneyException;
|
||||
import io.bitsquare.btc.WalletFacade;
|
||||
import io.bitsquare.trade.Offer;
|
||||
import io.bitsquare.trade.handlers.TransactionResultHandler;
|
||||
import io.bitsquare.trade.handlers.FaultHandler;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class CreateOfferFeeTx
|
||||
{
|
||||
private static final Logger log = LoggerFactory.getLogger(CreateOfferFeeTx.class);
|
||||
|
||||
public static void run(TransactionResultHandler resultHandler, FaultHandler faultHandler, WalletFacade walletFacade, Offer offer)
|
||||
{
|
||||
try
|
||||
{
|
||||
resultHandler.onResult(walletFacade.createOfferFeeTx(offer.getId()));
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -14,7 +14,7 @@ public class ValidateOffer
|
||||
{
|
||||
boolean isValid = offer.getAmount().isGreaterThan(offer.getAmount());
|
||||
|
||||
if (offer.getAmount().compareTo(offer.getMinAmount()) >= 0)
|
||||
if (offer.getAmount().compareTo(offer.getMinAmount()) < 0)
|
||||
{
|
||||
faultHandler.onFault("Offer validation failed: Min. amount is larger than amount.", new Exception("Offer validation failed: Min. amount is larger than amount."));
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user