Skip to content

Commit

Permalink
Merge pull request #4975 from chimp1984/fix-isPubKeyValid-check
Browse files Browse the repository at this point in the history
Improve CleanupMailboxMessages
  • Loading branch information
sqrrm authored Dec 21, 2020
2 parents fa46a16 + 4e10838 commit b934688
Showing 1 changed file with 12 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,21 @@ private void cleanupMailboxMessages(List<Trade> trades) {

private void handleDecryptedMessageWithPubKey(DecryptedMessageWithPubKey decryptedMessageWithPubKey,
List<Trade> trades) {
trades.forEach(trade -> handleDecryptedMessageWithPubKey(decryptedMessageWithPubKey, trade));
trades.stream()
.filter(trade -> isMessageForTrade(decryptedMessageWithPubKey, trade))
.filter(trade -> isPubKeyValid(decryptedMessageWithPubKey, trade))
.forEach(trade -> removeEntryFromMailbox(decryptedMessageWithPubKey, trade));
}

private void handleDecryptedMessageWithPubKey(DecryptedMessageWithPubKey decryptedMessageWithPubKey,
Trade trade) {
private boolean isMessageForTrade(DecryptedMessageWithPubKey decryptedMessageWithPubKey, Trade trade) {
NetworkEnvelope networkEnvelope = decryptedMessageWithPubKey.getNetworkEnvelope();
if (!isPubKeyValid(decryptedMessageWithPubKey, trade)) {
return;
}

if (networkEnvelope instanceof TradeMessage &&
isMyMessage((TradeMessage) networkEnvelope, trade)) {
removeEntryFromMailbox(decryptedMessageWithPubKey, trade);
} else if (networkEnvelope instanceof AckMessage &&
isMyMessage((AckMessage) networkEnvelope, trade)) {
removeEntryFromMailbox(decryptedMessageWithPubKey, trade);
if (networkEnvelope instanceof TradeMessage) {
return isMyMessage((TradeMessage) networkEnvelope, trade);
} else if (networkEnvelope instanceof AckMessage) {
return isMyMessage((AckMessage) networkEnvelope, trade);
}
// Instance must be TradeMessage or AckMessage.
return false;
}

private void removeEntryFromMailbox(DecryptedMessageWithPubKey decryptedMessageWithPubKey, Trade trade) {
Expand All @@ -124,7 +122,7 @@ private boolean isPubKeyValid(DecryptedMessageWithPubKey message, Trade trade) {
if (peersPubKeyRing != null &&
!message.getSignaturePubKey().equals(peersPubKeyRing.getSignaturePubKey())) {
isValid = false;
log.error("SignaturePubKey in message does not match the SignaturePubKey we have set for our trading peer.");
log.warn("SignaturePubKey in message does not match the SignaturePubKey we have set for our trading peer.");
}
return isValid;
}
Expand Down

0 comments on commit b934688

Please sign in to comment.