mirror of
https://github.com/bisq-network/bisq.git
synced 2025-02-24 15:10:44 +01:00
Refactor handling of DecryptedMessageWithPubKey
This commit is contained in:
parent
d353140e7a
commit
c229c3f014
1 changed files with 54 additions and 42 deletions
|
@ -40,6 +40,8 @@ import bisq.common.crypto.PubKeyRing;
|
||||||
import bisq.common.proto.network.NetworkEnvelope;
|
import bisq.common.proto.network.NetworkEnvelope;
|
||||||
import bisq.common.taskrunner.Task;
|
import bisq.common.taskrunner.Task;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
@ -86,8 +88,7 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D
|
||||||
// change and leave that for a later PR
|
// change and leave that for a later PR
|
||||||
UserThread.runAfter(() -> {
|
UserThread.runAfter(() -> {
|
||||||
mailboxMessageService.addDecryptedMailboxListener(this);
|
mailboxMessageService.addDecryptedMailboxListener(this);
|
||||||
mailboxMessageService.getMyDecryptedMailboxMessages()
|
handleMailboxCollection(mailboxMessageService.getMyDecryptedMailboxMessages());
|
||||||
.forEach(this::handleDecryptedMailboxMessageWithPubKey);
|
|
||||||
}, 100, TimeUnit.MILLISECONDS);
|
}, 100, TimeUnit.MILLISECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,15 +107,19 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDirectMessage(DecryptedMessageWithPubKey message, NodeAddress peer) {
|
public void onDirectMessage(DecryptedMessageWithPubKey decryptedMessageWithPubKey, NodeAddress peer) {
|
||||||
NetworkEnvelope networkEnvelope = message.getNetworkEnvelope();
|
if (!isPubKeyValid(decryptedMessageWithPubKey)) {
|
||||||
if (networkEnvelope instanceof TradeMessage &&
|
return;
|
||||||
isMyMessage((TradeMessage) networkEnvelope) &&
|
}
|
||||||
isPubKeyValid(message)) {
|
|
||||||
|
NetworkEnvelope networkEnvelope = decryptedMessageWithPubKey.getNetworkEnvelope();
|
||||||
|
if (!isMyMessage(networkEnvelope)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (networkEnvelope instanceof TradeMessage) {
|
||||||
onTradeMessage((TradeMessage) networkEnvelope, peer);
|
onTradeMessage((TradeMessage) networkEnvelope, peer);
|
||||||
} else if (networkEnvelope instanceof AckMessage &&
|
} else if (networkEnvelope instanceof AckMessage) {
|
||||||
isMyMessage((AckMessage) networkEnvelope) &&
|
|
||||||
isPubKeyValid(message)) {
|
|
||||||
onAckMessage((AckMessage) networkEnvelope, peer);
|
onAckMessage((AckMessage) networkEnvelope, peer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -125,46 +130,43 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onMailboxMessageAdded(DecryptedMessageWithPubKey message, NodeAddress peer) {
|
public void onMailboxMessageAdded(DecryptedMessageWithPubKey decryptedMessageWithPubKey, NodeAddress peer) {
|
||||||
handleDecryptedMailboxMessageWithPubKey(message, peer);
|
handleMailboxCollection(Collections.singletonList(decryptedMessageWithPubKey));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleDecryptedMailboxMessageWithPubKey(DecryptedMessageWithPubKey decryptedMessageWithPubKey) {
|
private void handleMailboxCollection(Collection<DecryptedMessageWithPubKey> collection) {
|
||||||
MailboxMessage mailboxMessage = (MailboxMessage) decryptedMessageWithPubKey.getNetworkEnvelope();
|
collection.stream()
|
||||||
NodeAddress senderNodeAddress = mailboxMessage.getSenderNodeAddress();
|
.filter(this::isPubKeyValid)
|
||||||
handleDecryptedMailboxMessageWithPubKey(decryptedMessageWithPubKey, senderNodeAddress);
|
.map(DecryptedMessageWithPubKey::getNetworkEnvelope)
|
||||||
|
.filter(this::isMyMessage)
|
||||||
|
.filter(e -> e instanceof MailboxMessage)
|
||||||
|
.map(e -> (MailboxMessage) e)
|
||||||
|
.forEach(this::handleMailboxMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void handleDecryptedMailboxMessageWithPubKey(DecryptedMessageWithPubKey decryptedMessageWithPubKey,
|
private void handleMailboxMessage(MailboxMessage mailboxMessage) {
|
||||||
NodeAddress peer) {
|
if (mailboxMessage instanceof TradeMessage) {
|
||||||
NetworkEnvelope networkEnvelope = decryptedMessageWithPubKey.getNetworkEnvelope();
|
TradeMessage tradeMessage = (TradeMessage) mailboxMessage;
|
||||||
if (networkEnvelope instanceof MailboxMessage && networkEnvelope instanceof TradeMessage) {
|
|
||||||
TradeMessage tradeMessage = (TradeMessage) networkEnvelope;
|
|
||||||
if (isMyMessage(tradeMessage) && isPubKeyValid(decryptedMessageWithPubKey)) {
|
|
||||||
// We only remove here if we have already completed the trade.
|
// We only remove here if we have already completed the trade.
|
||||||
// Otherwise removal is done after successfully applied the task runner.
|
// Otherwise removal is done after successfully applied the task runner.
|
||||||
if (trade.isWithdrawn()) {
|
if (trade.isWithdrawn()) {
|
||||||
MailboxMessage mailboxMessage = (MailboxMessage) tradeMessage;
|
|
||||||
processModel.getP2PService().getMailboxMessageService().removeMailboxMsg(mailboxMessage);
|
processModel.getP2PService().getMailboxMessageService().removeMailboxMsg(mailboxMessage);
|
||||||
log.info("Remove {} from the P2P network as trade is already completed.",
|
log.info("Remove {} from the P2P network as trade is already completed.",
|
||||||
tradeMessage.getClass().getSimpleName());
|
tradeMessage.getClass().getSimpleName());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
onMailboxMessage(tradeMessage, peer);
|
onMailboxMessage(tradeMessage, mailboxMessage.getSenderNodeAddress());
|
||||||
}
|
} else if (mailboxMessage instanceof AckMessage) {
|
||||||
} else if (networkEnvelope instanceof AckMessage) {
|
AckMessage ackMessage = (AckMessage) mailboxMessage;
|
||||||
AckMessage ackMessage = (AckMessage) networkEnvelope;
|
|
||||||
if (isMyMessage(ackMessage) && isPubKeyValid(decryptedMessageWithPubKey)) {
|
|
||||||
if (!trade.isWithdrawn()) {
|
if (!trade.isWithdrawn()) {
|
||||||
// We only apply the msg if we have not already completed the trade
|
// We only apply the msg if we have not already completed the trade
|
||||||
onAckMessage(ackMessage, peer);
|
onAckMessage(ackMessage, mailboxMessage.getSenderNodeAddress());
|
||||||
}
|
}
|
||||||
// In any case we remove the msg
|
// In any case we remove the msg
|
||||||
processModel.getP2PService().getMailboxMessageService().removeMailboxMsg(ackMessage);
|
processModel.getP2PService().getMailboxMessageService().removeMailboxMsg(ackMessage);
|
||||||
log.info("Remove {} from the P2P network.", ackMessage.getClass().getSimpleName());
|
log.info("Remove {} from the P2P network.", ackMessage.getClass().getSimpleName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void removeMailboxMessageAfterProcessing(TradeMessage tradeMessage) {
|
public void removeMailboxMessageAfterProcessing(TradeMessage tradeMessage) {
|
||||||
if (tradeMessage instanceof MailboxMessage) {
|
if (tradeMessage instanceof MailboxMessage) {
|
||||||
|
@ -380,6 +382,16 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D
|
||||||
cleanup();
|
cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isMyMessage(NetworkEnvelope message) {
|
||||||
|
if (message instanceof TradeMessage) {
|
||||||
|
return isMyMessage((TradeMessage) message);
|
||||||
|
} else if (message instanceof AckMessage) {
|
||||||
|
return isMyMessage((AckMessage) message);
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private boolean isMyMessage(TradeMessage message) {
|
private boolean isMyMessage(TradeMessage message) {
|
||||||
return message.getTradeId().equals(trade.getId());
|
return message.getTradeId().equals(trade.getId());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue