Add onRemoved method to remove mailbox entries once a remove message arrives

Add republishMailBoxMessages method
This commit is contained in:
chimp1984 2021-01-10 19:48:37 -05:00
parent bfbc657c22
commit 6146362d71
No known key found for this signature in database
GPG Key ID: 9801B4EC591F90E3
2 changed files with 38 additions and 2 deletions

View File

@ -39,6 +39,7 @@ import bisq.network.p2p.storage.payload.CapabilityRequiringPayload;
import bisq.network.p2p.storage.payload.MailboxStoragePayload;
import bisq.network.p2p.storage.payload.ProtectedMailboxStorageEntry;
import bisq.network.p2p.storage.payload.ProtectedStorageEntry;
import bisq.network.p2p.storage.payload.ProtectedStoragePayload;
import bisq.common.UserThread;
import bisq.common.app.Capabilities;
@ -275,6 +276,7 @@ public class MailboxMessageService implements SetupListener, RequestDataManager.
isBootstrapped = true;
// As we do not expect a updated data request response we start here with addHashMapChangedListenerAndApply
addHashMapChangedListenerAndApply();
UserThread.runAfter(this::republishMailBoxMessages, 20);
}
}
@ -298,6 +300,7 @@ public class MailboxMessageService implements SetupListener, RequestDataManager.
// Only now we start listening and processing. The p2PDataStorage is our cache for data we have received
// after the hidden service was ready.
addHashMapChangedListenerAndApply();
UserThread.runAfter(this::republishMailBoxMessages, 20);
}
}
@ -325,6 +328,20 @@ public class MailboxMessageService implements SetupListener, RequestDataManager.
}
}
@Override
public void onRemoved(Collection<ProtectedStorageEntry> protectedStorageEntries) {
protectedStorageEntries.stream()
.filter(protectedStorageEntry -> protectedStorageEntry instanceof ProtectedMailboxStorageEntry)
.map(protectedStorageEntry -> (ProtectedMailboxStorageEntry) protectedStorageEntry)
.forEach(protectedMailboxStorageEntry -> {
// We can only remove the foreign mailbox messages as for our own we use the uid from the decrypted
// payload which is not available here. But own mailbox messages get removed anyway after processing.
String uid = protectedMailboxStorageEntry.getMailboxStoragePayload().getPrefixedSealedAndSignedMessage().getUid();
mailboxItemsByUid.remove(uid);
requestPersistence();
});
}
///////////////////////////////////////////////////////////////////////////////////////////
// Private
@ -508,6 +525,21 @@ public class MailboxMessageService implements SetupListener, RequestDataManager.
}
}
private void republishMailBoxMessages() {
mailboxItemsByUid.values().stream()
.filter(list -> !list.isEmpty())
.flatMap(Collection::stream)
.filter(e -> !e.isExpired(clock))
.map(MailboxItem::getProtectedMailboxStorageEntry)
.filter(protectedStorageEntry -> {
// We only republish in case we have not received a remove message for that mailbox message
ProtectedStoragePayload protectedStoragePayload = protectedStorageEntry.getProtectedStoragePayload();
P2PDataStorage.ByteArray hashOfPayload = P2PDataStorage.get32ByteHashAsByteArray(protectedStoragePayload);
return !p2PDataStorage.addOncePayloadGotAlreadyRemoved(protectedStoragePayload, hashOfPayload);
})
.forEach(storageEntry -> p2PDataStorage.addProtectedStorageEntry(storageEntry, networkNode.getNodeAddress(), null));
}
private void requestPersistence() {
persistenceManager.requestPersistence();
}

View File

@ -711,8 +711,7 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers
ProtectedStoragePayload protectedStoragePayload = protectedStorageEntry.getProtectedStoragePayload();
ByteArray hashOfPayload = get32ByteHashAsByteArray(protectedStoragePayload);
if (protectedStoragePayload instanceof AddOncePayload &&
removedAddOncePayloads.contains(hashOfPayload)) {
if (addOncePayloadGotAlreadyRemoved(protectedStoragePayload, hashOfPayload)) {
log.warn("We have already removed that AddOncePayload by a previous removeDataMessage. " +
"We ignore that message. ProtectedStoragePayload: {}", protectedStoragePayload.toString());
return false;
@ -769,6 +768,11 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers
return true;
}
public boolean addOncePayloadGotAlreadyRemoved(ProtectedStoragePayload protectedStoragePayload,
ByteArray hashOfPayload) {
return protectedStoragePayload instanceof AddOncePayload && removedAddOncePayloads.contains(hashOfPayload);
}
/**
* Updates a local RefreshOffer with TTL changes and broadcasts those changes to the network
*