Support changed peeraddress after reconnect

This commit is contained in:
Manfred Karrer 2015-04-07 19:30:37 +02:00
parent 973aa73c76
commit bea3dbc445
8 changed files with 95 additions and 58 deletions

View File

@ -24,6 +24,7 @@ import io.bitsquare.btc.WalletService;
import io.bitsquare.common.taskrunner.Model;
import io.bitsquare.crypto.SignatureService;
import io.bitsquare.offer.Offer;
import io.bitsquare.p2p.AddressService;
import io.bitsquare.p2p.MailboxMessage;
import io.bitsquare.p2p.MessageService;
import io.bitsquare.p2p.Peer;
@ -152,6 +153,7 @@ abstract public class Trade implements Model, Serializable {
public void init(MessageService messageService,
WalletService walletService,
AddressService addressService,
TradeWalletService tradeWalletService,
BlockChainService blockChainService,
SignatureService signatureService,
@ -160,6 +162,7 @@ abstract public class Trade implements Model, Serializable {
processModel.onAllServicesInitialized(offer,
messageService,
addressService,
walletService,
tradeWalletService,
blockChainService,
@ -170,7 +173,7 @@ abstract public class Trade implements Model, Serializable {
createProtocol();
tradeProtocol.checkPayoutTxTimeLock(this);
if (mailboxMessage != null) {
tradeProtocol.applyMailboxMessage(mailboxMessage, this);
// After applied to protocol we remove it

View File

@ -484,6 +484,7 @@ public class TradeManager {
private void initTrade(Trade trade) {
trade.init(messageService,
walletService,
addressService,
tradeWalletService,
blockChainService,
signatureService,

View File

@ -76,17 +76,20 @@ public class BuyerAsOffererProtocol extends TradeProtocol implements BuyerProtoc
@Override
public void applyMailboxMessage(MailboxMessage mailboxMessage, Trade trade) {
if (trade == null)
this.trade = trade;
this.trade = trade;
log.debug("setMailboxMessage " + mailboxMessage);
// Might be called twice, so check that its only processed once
if (!processModel.isMailboxMessageProcessed()) {
processModel.mailboxMessageProcessed();
if (mailboxMessage instanceof RequestFinalizePayoutTxMessage) {
handle((RequestFinalizePayoutTxMessage) mailboxMessage);
}
}
// Find first the actual peer address, as it might have changed in the meantime
findPeerAddress(processModel.tradingPeer.getP2pSigPubKey(),
() -> {
if (mailboxMessage instanceof RequestFinalizePayoutTxMessage) {
handle((RequestFinalizePayoutTxMessage) mailboxMessage);
}
},
(errorMessage -> {
log.error(errorMessage);
}));
}

View File

@ -74,17 +74,20 @@ public class BuyerAsTakerProtocol extends TradeProtocol implements BuyerProtocol
@Override
public void applyMailboxMessage(MailboxMessage mailboxMessage, Trade trade) {
if (trade == null)
this.trade = trade;
this.trade = trade;
log.debug("setMailboxMessage " + mailboxMessage);
// Might be called twice, so check that its only processed once
if (!processModel.isMailboxMessageProcessed()) {
processModel.mailboxMessageProcessed();
if (mailboxMessage instanceof RequestFinalizePayoutTxMessage) {
handle((RequestFinalizePayoutTxMessage) mailboxMessage);
}
}
// Find first the actual peer address, as it might have changed in the meantime
findPeerAddress(trade.getOffer().getP2pSigPubKey(),
() -> {
if (mailboxMessage instanceof RequestFinalizePayoutTxMessage) {
handle((RequestFinalizePayoutTxMessage) mailboxMessage);
}
},
(errorMessage -> {
log.error(errorMessage);
}));
}
@Override

View File

@ -26,6 +26,7 @@ import io.bitsquare.common.taskrunner.Model;
import io.bitsquare.crypto.SignatureService;
import io.bitsquare.fiat.FiatAccount;
import io.bitsquare.offer.Offer;
import io.bitsquare.p2p.AddressService;
import io.bitsquare.p2p.MessageService;
import io.bitsquare.trade.protocol.trade.messages.TradeMessage;
import io.bitsquare.user.User;
@ -55,6 +56,7 @@ public class ProcessModel implements Model, Serializable {
// Transient/Immutable
transient private MessageService messageService;
transient private AddressService addressService;
transient private WalletService walletService;
transient private TradeWalletService tradeWalletService;
transient private BlockChainService blockChainService;
@ -65,7 +67,6 @@ public class ProcessModel implements Model, Serializable {
// Mutable
public final TradingPeer tradingPeer;
transient private boolean mailboxMessageProcessed;
transient private TradeMessage tradeMessage;
private String takeOfferFeeTxId;
private List<TransactionOutput> connectedOutputsForAllInputs;
@ -88,6 +89,7 @@ public class ProcessModel implements Model, Serializable {
public void onAllServicesInitialized(Offer offer,
MessageService messageService,
AddressService addressService,
WalletService walletService,
TradeWalletService tradeWalletService,
BlockChainService blockChainService,
@ -96,6 +98,7 @@ public class ProcessModel implements Model, Serializable {
User user) {
this.offer = offer;
this.messageService = messageService;
this.addressService = addressService;
this.walletService = walletService;
this.tradeWalletService = tradeWalletService;
this.blockChainService = blockChainService;
@ -155,15 +158,6 @@ public class ProcessModel implements Model, Serializable {
return tradeMessage;
}
public void mailboxMessageProcessed() {
this.mailboxMessageProcessed = true;
}
@Nullable
public boolean isMailboxMessageProcessed() {
return mailboxMessageProcessed;
}
@Nullable
public Transaction getTakeOfferFeeTx() {
return takeOfferFeeTx;
@ -265,4 +259,8 @@ public class ProcessModel implements Model, Serializable {
@Override
public void onComplete() {
}
public AddressService getAddressService() {
return addressService;
}
}

View File

@ -77,20 +77,22 @@ public class SellerAsOffererProtocol extends TradeProtocol implements SellerProt
@Override
public void applyMailboxMessage(MailboxMessage mailboxMessage, Trade trade) {
if (trade == null)
this.trade = trade;
this.trade = trade;
log.debug("setMailboxMessage " + mailboxMessage);
// Might be called twice, so check that its only processed once
if (!processModel.isMailboxMessageProcessed()) {
processModel.mailboxMessageProcessed();
if (mailboxMessage instanceof FiatTransferStartedMessage) {
handle((FiatTransferStartedMessage) mailboxMessage);
}
else if (mailboxMessage instanceof DepositTxPublishedMessage) {
handle((DepositTxPublishedMessage) mailboxMessage);
}
}
// Find first the actual peer address, as it might have changed in the meantime
findPeerAddress(processModel.tradingPeer.getP2pSigPubKey(),
() -> {
if (mailboxMessage instanceof FiatTransferStartedMessage) {
handle((FiatTransferStartedMessage) mailboxMessage);
}
else if (mailboxMessage instanceof DepositTxPublishedMessage) {
handle((DepositTxPublishedMessage) mailboxMessage);
}
},
(errorMessage -> {
log.error(errorMessage);
}));
}

View File

@ -76,23 +76,26 @@ public class SellerAsTakerProtocol extends TradeProtocol implements SellerProtoc
@Override
public void applyMailboxMessage(MailboxMessage mailboxMessage, Trade trade) {
if (trade == null)
this.trade = trade;
this.trade = trade;
log.debug("setMailboxMessage " + mailboxMessage);
// Might be called twice, so check that its only processed once
if (!processModel.isMailboxMessageProcessed()) {
processModel.mailboxMessageProcessed();
if (mailboxMessage instanceof FiatTransferStartedMessage) {
handle((FiatTransferStartedMessage) mailboxMessage);
}
else if (mailboxMessage instanceof DepositTxPublishedMessage) {
handle((DepositTxPublishedMessage) mailboxMessage);
}
else if (mailboxMessage instanceof PayoutTxFinalizedMessage) {
handle((PayoutTxFinalizedMessage) mailboxMessage);
}
}
// Find first the actual peer address, as it might have changed in the meantime
findPeerAddress(trade.getOffer().getP2pSigPubKey(),
() -> {
if (mailboxMessage instanceof FiatTransferStartedMessage) {
handle((FiatTransferStartedMessage) mailboxMessage);
}
else if (mailboxMessage instanceof DepositTxPublishedMessage) {
handle((DepositTxPublishedMessage) mailboxMessage);
}
else if (mailboxMessage instanceof PayoutTxFinalizedMessage) {
handle((PayoutTxFinalizedMessage) mailboxMessage);
}
},
(errorMessage -> {
log.error(errorMessage);
}));
}
@Override

View File

@ -17,8 +17,12 @@
package io.bitsquare.trade.protocol.trade;
import io.bitsquare.common.handlers.ErrorMessageHandler;
import io.bitsquare.common.handlers.ResultHandler;
import io.bitsquare.p2p.MailboxMessage;
import io.bitsquare.p2p.MessageHandler;
import io.bitsquare.p2p.Peer;
import io.bitsquare.p2p.listener.GetPeerAddressListener;
import io.bitsquare.trade.OffererTrade;
import io.bitsquare.trade.TakerTrade;
import io.bitsquare.trade.Trade;
@ -28,6 +32,8 @@ import io.bitsquare.trade.states.TakerTradeState;
import org.bitcoinj.utils.Threading;
import java.security.PublicKey;
import java.util.Timer;
import java.util.TimerTask;
@ -55,9 +61,27 @@ public abstract class TradeProtocol {
abstract public void applyMailboxMessage(MailboxMessage mailboxMessage, Trade trade);
protected void findPeerAddress(PublicKey p2pSigPubKey, ResultHandler resultHandler, ErrorMessageHandler errorMessageHandler) {
try {
processModel.getAddressService().findPeerAddress(p2pSigPubKey, new GetPeerAddressListener() {
@Override
public void onResult(Peer peer) {
trade.setTradingPeer(peer);
resultHandler.handleResult();
}
@Override
public void onFailed() {
errorMessageHandler.handleErrorMessage("findPeerAddress failed");
}
});
} catch (Throwable t) {
errorMessageHandler.handleErrorMessage("findPeerAddress failed with error: " + t.getMessage());
}
}
public void checkPayoutTxTimeLock(Trade trade) {
if (trade == null)
this.trade = trade;
this.trade = trade;
boolean needPayoutTxBroadcast = false;
if (trade instanceof TakerTrade)