Search in sources :

Example 1 with ProtectedMailboxStorageEntry

use of io.bitsquare.p2p.storage.storageentry.ProtectedMailboxStorageEntry in project bitsquare by bitsquare.

the class P2PDataStorage method checkIfStoredMailboxDataMatchesNewMailboxData.

private boolean checkIfStoredMailboxDataMatchesNewMailboxData(PublicKey receiversPubKey, ByteArray hashOfData) {
    ProtectedStorageEntry storedData = map.get(hashOfData);
    if (storedData instanceof ProtectedMailboxStorageEntry) {
        ProtectedMailboxStorageEntry entry = (ProtectedMailboxStorageEntry) storedData;
        // publicKey is not the same (stored: sender, new: receiver)
        boolean result = entry.receiversPubKey.equals(receiversPubKey) && getHashAsByteArray(entry.getStoragePayload()).equals(hashOfData);
        if (!result)
            log.warn("New data entry does not match our stored data. entry.receiversPubKey=" + entry.receiversPubKey + ", receiversPubKey=" + receiversPubKey);
        return result;
    } else {
        log.error("We expected a MailboxData but got other type. That must never happen. storedData=" + storedData);
        return false;
    }
}
Also used : ProtectedMailboxStorageEntry(io.bitsquare.p2p.storage.storageentry.ProtectedMailboxStorageEntry) ProtectedStorageEntry(io.bitsquare.p2p.storage.storageentry.ProtectedStorageEntry)

Example 2 with ProtectedMailboxStorageEntry

use of io.bitsquare.p2p.storage.storageentry.ProtectedMailboxStorageEntry in project bitsquare by bitsquare.

the class P2PService method delayedRemoveEntryFromMailbox.

private void delayedRemoveEntryFromMailbox(DecryptedMsgWithPubKey decryptedMsgWithPubKey) {
    Log.traceCall();
    checkArgument(optionalKeyRing.isPresent(), "keyRing not set. Seems that is called on a seed node which must not happen.");
    if (isBootstrapped()) {
        MailboxMessage mailboxMessage = (MailboxMessage) decryptedMsgWithPubKey.message;
        String uid = mailboxMessage.getUID();
        if (mailboxMap.containsKey(uid)) {
            ProtectedMailboxStorageEntry mailboxData = mailboxMap.get(uid);
            if (mailboxData != null && mailboxData.getStoragePayload() instanceof MailboxStoragePayload) {
                MailboxStoragePayload expirableMailboxStoragePayload = (MailboxStoragePayload) mailboxData.getStoragePayload();
                PublicKey receiversPubKey = mailboxData.receiversPubKey;
                checkArgument(receiversPubKey.equals(optionalKeyRing.get().getSignatureKeyPair().getPublic()), "receiversPubKey is not matching with our key. That must not happen.");
                try {
                    ProtectedMailboxStorageEntry protectedMailboxStorageEntry = p2PDataStorage.getMailboxDataWithSignedSeqNr(expirableMailboxStoragePayload, optionalKeyRing.get().getSignatureKeyPair(), receiversPubKey);
                    p2PDataStorage.removeMailboxData(protectedMailboxStorageEntry, networkNode.getNodeAddress(), true);
                } catch (CryptoException e) {
                    log.error("Signing at getDataWithSignedSeqNr failed. That should never happen.");
                }
                mailboxMap.remove(uid);
                log.trace("Removed successfully decryptedMsgWithPubKey.");
            }
        } else {
            log.warn("uid for mailbox entry not found in mailboxMap. That should never happen." + "\n\tuid={}\n\tmailboxMap={}\n\tmailboxMessage={}", uid, mailboxMap, mailboxMessage);
        }
    } else {
        throw new NetworkNotReadyException();
    }
}
Also used : PublicKey(java.security.PublicKey) MailboxStoragePayload(io.bitsquare.p2p.storage.payload.MailboxStoragePayload) ProtectedMailboxStorageEntry(io.bitsquare.p2p.storage.storageentry.ProtectedMailboxStorageEntry) CryptoException(io.bitsquare.common.crypto.CryptoException)

Example 3 with ProtectedMailboxStorageEntry

use of io.bitsquare.p2p.storage.storageentry.ProtectedMailboxStorageEntry in project bitsquare by bitsquare.

the class P2PService method addMailboxData.

private void addMailboxData(MailboxStoragePayload expirableMailboxStoragePayload, PublicKey receiversPublicKey, SendMailboxMessageListener sendMailboxMessageListener) {
    Log.traceCall();
    checkArgument(optionalKeyRing.isPresent(), "keyRing not set. Seems that is called on a seed node which must not happen.");
    if (isBootstrapped()) {
        if (!networkNode.getAllConnections().isEmpty()) {
            try {
                ProtectedMailboxStorageEntry protectedMailboxStorageEntry = p2PDataStorage.getMailboxDataWithSignedSeqNr(expirableMailboxStoragePayload, optionalKeyRing.get().getSignatureKeyPair(), receiversPublicKey);
                BroadcastHandler.Listener listener = new BroadcastHandler.Listener() {

                    @Override
                    public void onBroadcasted(BroadcastMessage message, int numOfCompletedBroadcasts) {
                    }

                    @Override
                    public void onBroadcastedToFirstPeer(BroadcastMessage message) {
                        // We only want to notify our sendMailboxMessageListener for the calls he is interested in.
                        if (message instanceof AddDataMessage && ((AddDataMessage) message).protectedStorageEntry.equals(protectedMailboxStorageEntry)) {
                            // We delay a bit to give more time for sufficient propagation in the P2P network.
                            // This should help to avoid situations where a user closes the app too early and the msg
                            // does not arrive.
                            // We could use onBroadcastCompleted instead but it might take too long if one peer
                            // is very badly connected.
                            // TODO We could check for a certain threshold of no. of incoming messages of the same msg 
                            // to see how well it is propagated. BitcoinJ uses such an approach for tx propagation.
                            UserThread.runAfter(() -> {
                                log.info("Broadcasted to first peer (with 3 sec. delayed):  Message = {}", Utilities.toTruncatedString(message));
                                sendMailboxMessageListener.onStoredInMailbox();
                            }, 3);
                        }
                    }

                    @Override
                    public void onBroadcastCompleted(BroadcastMessage message, int numOfCompletedBroadcasts, int numOfFailedBroadcasts) {
                        log.info("Broadcast completed: Sent to {} peers (failed: {}). Message = {}", numOfCompletedBroadcasts, numOfFailedBroadcasts, Utilities.toTruncatedString(message));
                        if (numOfCompletedBroadcasts == 0)
                            sendMailboxMessageListener.onFault("Broadcast completed without any successful broadcast");
                    }

                    @Override
                    public void onBroadcastFailed(String errorMessage) {
                    // TODO investigate why not sending sendMailboxMessageListener.onFault. Related probably 
                    // to the logic from BroadcastHandler.sendToPeer
                    }
                };
                boolean result = p2PDataStorage.add(protectedMailboxStorageEntry, networkNode.getNodeAddress(), listener, true);
                if (!result) {
                    //TODO remove and add again with a delay to ensure the data will be broadcasted
                    // The p2PDataStorage.remove makes probably sense but need to be analysed more.
                    // Don't change that if it is not 100% clear.
                    sendMailboxMessageListener.onFault("Data already exists in our local database");
                    boolean removeResult = p2PDataStorage.remove(protectedMailboxStorageEntry, networkNode.getNodeAddress(), true);
                    log.debug("remove result=" + removeResult);
                }
            } catch (CryptoException e) {
                log.error("Signing at getDataWithSignedSeqNr failed. That should never happen.");
            }
        } else {
            sendMailboxMessageListener.onFault("There are no P2P network nodes connected. " + "Please check your internet connection.");
        }
    } else {
        throw new NetworkNotReadyException();
    }
}
Also used : AddDataMessage(io.bitsquare.p2p.storage.messages.AddDataMessage) HashMapChangedListener(io.bitsquare.p2p.storage.HashMapChangedListener) BroadcastMessage(io.bitsquare.p2p.storage.messages.BroadcastMessage) ProtectedMailboxStorageEntry(io.bitsquare.p2p.storage.storageentry.ProtectedMailboxStorageEntry) CryptoException(io.bitsquare.common.crypto.CryptoException) BroadcastHandler(io.bitsquare.p2p.peers.BroadcastHandler)

Aggregations

ProtectedMailboxStorageEntry (io.bitsquare.p2p.storage.storageentry.ProtectedMailboxStorageEntry)3 CryptoException (io.bitsquare.common.crypto.CryptoException)2 BroadcastHandler (io.bitsquare.p2p.peers.BroadcastHandler)1 HashMapChangedListener (io.bitsquare.p2p.storage.HashMapChangedListener)1 AddDataMessage (io.bitsquare.p2p.storage.messages.AddDataMessage)1 BroadcastMessage (io.bitsquare.p2p.storage.messages.BroadcastMessage)1 MailboxStoragePayload (io.bitsquare.p2p.storage.payload.MailboxStoragePayload)1 ProtectedStorageEntry (io.bitsquare.p2p.storage.storageentry.ProtectedStorageEntry)1 PublicKey (java.security.PublicKey)1