Refactor: Use task runner for peer publish delayed payout tx

This commit is contained in:
chimp1984 2020-09-25 23:55:45 -05:00
parent 561abd7a49
commit fded97fb9c
No known key found for this signature in database
GPG Key ID: 9801B4EC591F90E3
3 changed files with 165 additions and 73 deletions

View File

@ -17,12 +17,6 @@
package bisq.core.trade.protocol;
import bisq.core.btc.exceptions.TxBroadcastException;
import bisq.core.btc.model.AddressEntry;
import bisq.core.btc.wallet.BtcWalletService;
import bisq.core.btc.wallet.TradeWalletService;
import bisq.core.btc.wallet.TxBroadcaster;
import bisq.core.btc.wallet.WalletService;
import bisq.core.trade.Trade;
import bisq.core.trade.messages.MediatedPayoutTxPublishedMessage;
import bisq.core.trade.messages.MediatedPayoutTxSignatureMessage;
@ -30,6 +24,8 @@ import bisq.core.trade.messages.PeerPublishedDelayedPayoutTxMessage;
import bisq.core.trade.messages.TradeMessage;
import bisq.core.trade.protocol.tasks.ApplyFilter;
import bisq.core.trade.protocol.tasks.ProcessPeerPublishedDelayedPayoutTxMessage;
import bisq.core.trade.protocol.tasks.arbitration.PublishedDelayedPayoutTx;
import bisq.core.trade.protocol.tasks.arbitration.SendPeerPublishedDelayedPayoutTxMessage;
import bisq.core.trade.protocol.tasks.mediation.BroadcastMediatedPayoutTx;
import bisq.core.trade.protocol.tasks.mediation.FinalizeMediatedPayoutTx;
import bisq.core.trade.protocol.tasks.mediation.ProcessMediatedPayoutSignatureMessage;
@ -40,16 +36,10 @@ import bisq.core.trade.protocol.tasks.mediation.SetupMediatedPayoutTxListener;
import bisq.core.trade.protocol.tasks.mediation.SignMediatedPayoutTx;
import bisq.network.p2p.NodeAddress;
import bisq.network.p2p.P2PService;
import bisq.network.p2p.SendMailboxMessageListener;
import bisq.common.handlers.ErrorMessageHandler;
import bisq.common.handlers.ResultHandler;
import org.bitcoinj.core.Transaction;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@ -57,7 +47,8 @@ public class MediationProtocol extends TradeProtocol {
enum DisputeEvent implements FluentProtocol.Event {
MEDIATION_RESULT_ACCEPTED,
MEDIATION_RESULT_REJECTED
MEDIATION_RESULT_REJECTED,
ARBITRATION_REQUESTED
}
public MediationProtocol(Trade trade) {
@ -159,67 +150,26 @@ public class MediationProtocol extends TradeProtocol {
// Delayed payout tx
///////////////////////////////////////////////////////////////////////////////////////////
// TODO use task
public void onPublishDelayedPayoutTx(ResultHandler resultHandler, ErrorMessageHandler errorMessageHandler) {
Transaction delayedPayoutTx = trade.getDelayedPayoutTx();
if (delayedPayoutTx == null) {
return;
}
BtcWalletService btcWalletService = processModel.getBtcWalletService();
TradeWalletService tradeWalletService = processModel.getTradeWalletService();
P2PService p2PService = processModel.getP2PService();
String tradeId = trade.getId();
// We have spent the funds from the deposit tx with the delayedPayoutTx
btcWalletService.swapTradeEntryToAvailableEntry(trade.getId(), AddressEntry.Context.MULTI_SIG);
// We might receive funds on AddressEntry.Context.TRADE_PAYOUT so we don't swap that
Transaction committedDelayedPayoutTx = WalletService.maybeAddSelfTxToWallet(delayedPayoutTx, btcWalletService.getWallet());
tradeWalletService.broadcastTx(committedDelayedPayoutTx, new TxBroadcaster.Callback() {
@Override
public void onSuccess(Transaction transaction) {
log.info("publishDelayedPayoutTx onSuccess " + transaction);
NodeAddress tradingPeerNodeAddress = trade.getTradingPeerNodeAddress();
PeerPublishedDelayedPayoutTxMessage msg = new PeerPublishedDelayedPayoutTxMessage(UUID.randomUUID().toString(),
tradeId,
tradingPeerNodeAddress);
p2PService.sendEncryptedMailboxMessage(
tradingPeerNodeAddress,
trade.getProcessModel().getTradingPeer().getPubKeyRing(),
msg,
new SendMailboxMessageListener() {
@Override
public void onArrived() {
resultHandler.handleResult();
log.info("SendMailboxMessageListener onArrived tradeId={} at peer {}",
tradeId, tradingPeerNodeAddress);
}
@Override
public void onStoredInMailbox() {
resultHandler.handleResult();
log.info("SendMailboxMessageListener onStoredInMailbox tradeId={} at peer {}",
tradeId, tradingPeerNodeAddress);
}
@Override
public void onFault(String errorMessage) {
log.error("SendMailboxMessageListener onFault tradeId={} at peer {}",
tradeId, tradingPeerNodeAddress);
errorMessageHandler.handleErrorMessage(errorMessage);
}
}
);
}
@Override
public void onFailure(TxBroadcastException exception) {
log.error("publishDelayedPayoutTx onFailure", exception);
errorMessageHandler.handleErrorMessage(exception.toString());
}
});
DisputeEvent event = DisputeEvent.ARBITRATION_REQUESTED;
expect(anyPhase(Trade.Phase.DEPOSIT_CONFIRMED,
Trade.Phase.FIAT_SENT,
Trade.Phase.FIAT_RECEIVED)
.with(event)
.preCondition(trade.getDelayedPayoutTx() != null))
.setup(tasks(PublishedDelayedPayoutTx.class,
SendPeerPublishedDelayedPayoutTxMessage.class)
.using(new TradeTaskRunner(trade,
() -> {
resultHandler.handleResult();
handleTaskRunnerSuccess(event);
},
errorMessage -> {
errorMessageHandler.handleErrorMessage(errorMessage);
handleTaskRunnerFault(event, errorMessage);
}))
.withTimeout(30))
.executeTasks();
}

View File

@ -0,0 +1,71 @@
/*
* 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 bisq.core.trade.protocol.tasks.arbitration;
import bisq.core.btc.exceptions.TxBroadcastException;
import bisq.core.btc.model.AddressEntry;
import bisq.core.btc.wallet.BtcWalletService;
import bisq.core.btc.wallet.TxBroadcaster;
import bisq.core.btc.wallet.WalletService;
import bisq.core.trade.Trade;
import bisq.core.trade.protocol.tasks.TradeTask;
import bisq.common.taskrunner.TaskRunner;
import org.bitcoinj.core.Transaction;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class PublishedDelayedPayoutTx extends TradeTask {
public PublishedDelayedPayoutTx(TaskRunner<Trade> taskHandler, Trade trade) {
super(taskHandler, trade);
}
@Override
protected void run() {
try {
runInterceptHook();
Transaction delayedPayoutTx = trade.getDelayedPayoutTx();
BtcWalletService btcWalletService = processModel.getBtcWalletService();
// We have spent the funds from the deposit tx with the delayedPayoutTx
btcWalletService.swapTradeEntryToAvailableEntry(trade.getId(), AddressEntry.Context.MULTI_SIG);
// We might receive funds on AddressEntry.Context.TRADE_PAYOUT so we don't swap that
Transaction committedDelayedPayoutTx = WalletService.maybeAddSelfTxToWallet(delayedPayoutTx, btcWalletService.getWallet());
processModel.getTradeWalletService().broadcastTx(committedDelayedPayoutTx, new TxBroadcaster.Callback() {
@Override
public void onSuccess(Transaction transaction) {
log.info("publishDelayedPayoutTx onSuccess " + transaction);
complete();
}
@Override
public void onFailure(TxBroadcastException exception) {
log.error("publishDelayedPayoutTx onFailure", exception);
failed(exception.toString());
}
});
} catch (Throwable t) {
failed(t);
}
}
}

View File

@ -0,0 +1,71 @@
/*
* 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 bisq.core.trade.protocol.tasks.arbitration;
import bisq.core.trade.Trade;
import bisq.core.trade.messages.PeerPublishedDelayedPayoutTxMessage;
import bisq.core.trade.messages.TradeMessage;
import bisq.core.trade.protocol.tasks.SendMailboxMessageTask;
import bisq.common.taskrunner.TaskRunner;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class SendPeerPublishedDelayedPayoutTxMessage extends SendMailboxMessageTask {
public SendPeerPublishedDelayedPayoutTxMessage(TaskRunner<Trade> taskHandler, Trade trade) {
super(taskHandler, trade);
}
@Override
protected TradeMessage getMessage(String id) {
return new PeerPublishedDelayedPayoutTxMessage(UUID.randomUUID().toString(),
trade.getId(),
trade.getTradingPeerNodeAddress());
}
@Override
protected void setStateSent() {
}
@Override
protected void setStateArrived() {
}
@Override
protected void setStateStoredInMailbox() {
}
@Override
protected void setStateFault() {
}
@Override
protected void run() {
try {
runInterceptHook();
super.run();
} catch (Throwable t) {
failed(t);
}
}
}