/*
 * 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.network.p2p.storage.persistence;

import bisq.network.p2p.storage.P2PDataStorage;
import bisq.network.p2p.storage.payload.MailboxStoragePayload;

import bisq.common.persistence.PersistenceManager;
import bisq.common.proto.persistable.PersistedDataHost;

import javax.inject.Inject;
import javax.inject.Singleton;

import lombok.extern.slf4j.Slf4j;

@Singleton
@Slf4j
public class RemovedPayloadsService implements PersistedDataHost {
    private final PersistenceManager<RemovedPayloadsMap> persistenceManager;
    private final RemovedPayloadsMap removedPayloadsMap = new RemovedPayloadsMap();

    @Inject
    public RemovedPayloadsService(PersistenceManager<RemovedPayloadsMap> persistenceManager) {
        this.persistenceManager = persistenceManager;

        this.persistenceManager.initialize(removedPayloadsMap, PersistenceManager.Source.PRIVATE_LOW_PRIO);
    }


    ///////////////////////////////////////////////////////////////////////////////////////////
    // PersistedDataHost
    ///////////////////////////////////////////////////////////////////////////////////////////

    @Override
    public void readPersisted(Runnable completeHandler) {
        long cutOffDate = System.currentTimeMillis() - MailboxStoragePayload.TTL;
        persistenceManager.readPersisted(persisted -> {
                    persisted.getDateByHashes().entrySet().stream()
                            .filter(e -> e.getValue() < cutOffDate)
                            .forEach(e -> removedPayloadsMap.getDateByHashes().put(e.getKey(), e.getValue()));
                    persistenceManager.requestPersistence();
                    completeHandler.run();
                },
                completeHandler);
    }

    public boolean wasRemoved(P2PDataStorage.ByteArray hashOfPayload) {
        return removedPayloadsMap.getDateByHashes().containsKey(hashOfPayload);
    }

    public void addHash(P2PDataStorage.ByteArray hashOfPayload) {
        removedPayloadsMap.getDateByHashes().put(hashOfPayload, System.currentTimeMillis());
        persistenceManager.requestPersistence();
    }
}

@@ -2233,24 +2233,115 @@
 tValue()));%0A
+                    log.trace(%22readPersisted: removedPayloadsMap=%7B%7D%22, removedPayloadsMap);%0A
             
@@ -2556,32 +2556,159 @@
 ashOfPayload) %7B%0A
+        log.trace(%22called wasRemoved: hashOfPayload=%7B%7D, removedPayloadsMap=%7B%7D%22, hashOfPayload.toString(), removedPayloadsMap);%0A
         return r
@@ -2836,32 +2836,156 @@
 ashOfPayload) %7B%0A
+        log.trace(%22called addHash: hashOfPayload=%7B%7D, removedPayloadsMap=%7B%7D%22, hashOfPayload.toString(), removedPayloadsMap);%0A
         removedP

Add logs