Renaming of storage messages, add comments

This commit is contained in:
Manfred Karrer 2016-02-11 13:26:38 +01:00
parent fa2da8dbe5
commit 3dbcfb650a
22 changed files with 241 additions and 182 deletions

View file

@ -18,12 +18,12 @@
package io.bitsquare.alert; package io.bitsquare.alert;
import io.bitsquare.app.Version; import io.bitsquare.app.Version;
import io.bitsquare.p2p.storage.data.PubKeyProtectedExpirablePayload; import io.bitsquare.p2p.storage.data.StorageMessage;
import java.security.PublicKey; import java.security.PublicKey;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public final class Alert implements PubKeyProtectedExpirablePayload { public final class Alert implements StorageMessage {
// That object is sent over the wire, so we need to take care of version compatibility. // That object is sent over the wire, so we need to take care of version compatibility.
private static final long serialVersionUID = Version.P2P_NETWORK_VERSION; private static final long serialVersionUID = Version.P2P_NETWORK_VERSION;
@ -54,7 +54,7 @@ public final class Alert implements PubKeyProtectedExpirablePayload {
} }
@Override @Override
public PublicKey getPubKey() { public PublicKey getOwnerPubKey() {
return storagePublicKey; return storagePublicKey;
} }

View file

@ -62,7 +62,7 @@ public class AlertManager {
alertService.addHashSetChangedListener(new HashMapChangedListener() { alertService.addHashSetChangedListener(new HashMapChangedListener() {
@Override @Override
public void onAdded(ProtectedData entry) { public void onAdded(ProtectedData entry) {
Serializable data = entry.expirablePayload; Serializable data = entry.expirableMessage;
if (data instanceof Alert) { if (data instanceof Alert) {
Alert alert = (Alert) data; Alert alert = (Alert) data;
if (verifySignature(alert)) if (verifySignature(alert))
@ -72,7 +72,7 @@ public class AlertManager {
@Override @Override
public void onRemoved(ProtectedData entry) { public void onRemoved(ProtectedData entry) {
Serializable data = entry.expirablePayload; Serializable data = entry.expirableMessage;
if (data instanceof Alert) { if (data instanceof Alert) {
Alert alert = (Alert) data; Alert alert = (Alert) data;
if (verifySignature(alert)) if (verifySignature(alert))

View file

@ -20,7 +20,7 @@ package io.bitsquare.arbitration;
import io.bitsquare.app.Version; import io.bitsquare.app.Version;
import io.bitsquare.common.crypto.PubKeyRing; import io.bitsquare.common.crypto.PubKeyRing;
import io.bitsquare.p2p.NodeAddress; import io.bitsquare.p2p.NodeAddress;
import io.bitsquare.p2p.storage.data.PubKeyProtectedExpirablePayload; import io.bitsquare.p2p.storage.data.StorageMessage;
import java.security.PublicKey; import java.security.PublicKey;
import java.util.Arrays; import java.util.Arrays;
@ -28,7 +28,7 @@ import java.util.Date;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public final class Arbitrator implements PubKeyProtectedExpirablePayload { public final class Arbitrator implements StorageMessage {
// That object is sent over the wire, so we need to take care of version compatibility. // That object is sent over the wire, so we need to take care of version compatibility.
private static final long serialVersionUID = Version.P2P_NETWORK_VERSION; private static final long serialVersionUID = Version.P2P_NETWORK_VERSION;
@ -73,7 +73,7 @@ public final class Arbitrator implements PubKeyProtectedExpirablePayload {
} }
@Override @Override
public PublicKey getPubKey() { public PublicKey getOwnerPubKey() {
return pubKeyRing.getSignaturePubKey(); return pubKeyRing.getSignaturePubKey();
} }

View file

@ -84,8 +84,8 @@ public class ArbitratorService {
public Map<NodeAddress, Arbitrator> getArbitrators() { public Map<NodeAddress, Arbitrator> getArbitrators() {
Set<Arbitrator> arbitratorSet = p2PService.getDataMap().values().stream() Set<Arbitrator> arbitratorSet = p2PService.getDataMap().values().stream()
.filter(e -> e.expirablePayload instanceof Arbitrator) .filter(e -> e.expirableMessage instanceof Arbitrator)
.map(e -> (Arbitrator) e.expirablePayload) .map(e -> (Arbitrator) e.expirableMessage)
.collect(Collectors.toSet()); .collect(Collectors.toSet());
Map<NodeAddress, Arbitrator> map = new HashMap<>(); Map<NodeAddress, Arbitrator> map = new HashMap<>();

View file

@ -25,7 +25,7 @@ import io.bitsquare.common.handlers.ResultHandler;
import io.bitsquare.common.util.JsonExclude; import io.bitsquare.common.util.JsonExclude;
import io.bitsquare.locale.Country; import io.bitsquare.locale.Country;
import io.bitsquare.p2p.NodeAddress; import io.bitsquare.p2p.NodeAddress;
import io.bitsquare.p2p.storage.data.PubKeyProtectedExpirablePayload; import io.bitsquare.p2p.storage.data.StorageMessage;
import io.bitsquare.payment.PaymentMethod; import io.bitsquare.payment.PaymentMethod;
import io.bitsquare.trade.protocol.availability.OfferAvailabilityModel; import io.bitsquare.trade.protocol.availability.OfferAvailabilityModel;
import io.bitsquare.trade.protocol.availability.OfferAvailabilityProtocol; import io.bitsquare.trade.protocol.availability.OfferAvailabilityProtocol;
@ -46,7 +46,7 @@ import java.util.concurrent.TimeUnit;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
public final class Offer implements PubKeyProtectedExpirablePayload { public final class Offer implements StorageMessage {
// That object is sent over the wire, so we need to take care of version compatibility. // That object is sent over the wire, so we need to take care of version compatibility.
@JsonExclude @JsonExclude
private static final long serialVersionUID = Version.P2P_NETWORK_VERSION; private static final long serialVersionUID = Version.P2P_NETWORK_VERSION;
@ -254,7 +254,7 @@ public final class Offer implements PubKeyProtectedExpirablePayload {
} }
@Override @Override
public PublicKey getPubKey() { public PublicKey getOwnerPubKey() {
return pubKeyRing.getSignaturePubKey(); return pubKeyRing.getSignaturePubKey();
} }

View file

@ -85,8 +85,8 @@ public class OfferBookService {
public List<Offer> getOffers() { public List<Offer> getOffers() {
final List<Offer> offers = p2PService.getDataMap().values().stream() final List<Offer> offers = p2PService.getDataMap().values().stream()
.filter(e -> e.expirablePayload instanceof Offer) .filter(e -> e.expirableMessage instanceof Offer)
.map(e -> (Offer) e.expirablePayload) .map(e -> (Offer) e.expirableMessage)
.collect(Collectors.toList()); .collect(Collectors.toList());
return offers; return offers;
} }

View file

@ -57,7 +57,7 @@ public class OfferBook {
@Override @Override
public void onAdded(ProtectedData entry) { public void onAdded(ProtectedData entry) {
log.debug("onAdded " + entry); log.debug("onAdded " + entry);
Serializable data = entry.expirablePayload; Serializable data = entry.expirableMessage;
if (data instanceof Offer) { if (data instanceof Offer) {
Offer offer = (Offer) data; Offer offer = (Offer) data;
OfferBookListItem offerBookListItem = new OfferBookListItem(offer); OfferBookListItem offerBookListItem = new OfferBookListItem(offer);
@ -69,8 +69,8 @@ public class OfferBook {
@Override @Override
public void onRemoved(ProtectedData entry) { public void onRemoved(ProtectedData entry) {
log.debug("onRemoved " + entry); log.debug("onRemoved " + entry);
if (entry.expirablePayload instanceof Offer) { if (entry.expirableMessage instanceof Offer) {
Offer offer = (Offer) entry.expirablePayload; Offer offer = (Offer) entry.expirableMessage;
// Update state in case that that offer is used in the take offer screen, so it gets updated correctly // Update state in case that that offer is used in the take offer screen, so it gets updated correctly
offer.setState(Offer.State.REMOVED); offer.setState(Offer.State.REMOVED);

View file

@ -23,8 +23,8 @@ import io.bitsquare.p2p.peers.RequestDataManager;
import io.bitsquare.p2p.seed.SeedNodesRepository; import io.bitsquare.p2p.seed.SeedNodesRepository;
import io.bitsquare.p2p.storage.HashMapChangedListener; import io.bitsquare.p2p.storage.HashMapChangedListener;
import io.bitsquare.p2p.storage.P2PDataStorage; import io.bitsquare.p2p.storage.P2PDataStorage;
import io.bitsquare.p2p.storage.data.ExpirableMailboxPayload; import io.bitsquare.p2p.storage.data.ExpirableMessage;
import io.bitsquare.p2p.storage.data.ExpirablePayload; import io.bitsquare.p2p.storage.data.MailboxMessage;
import io.bitsquare.p2p.storage.data.ProtectedData; import io.bitsquare.p2p.storage.data.ProtectedData;
import io.bitsquare.p2p.storage.data.ProtectedMailboxData; import io.bitsquare.p2p.storage.data.ProtectedMailboxData;
import io.bitsquare.p2p.storage.messages.AddDataMessage; import io.bitsquare.p2p.storage.messages.AddDataMessage;
@ -421,16 +421,16 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis
// Seed nodes don't have set the encryptionService // Seed nodes don't have set the encryptionService
if (optionalEncryptionService.isPresent()) { if (optionalEncryptionService.isPresent()) {
Log.traceCall(); Log.traceCall();
ExpirablePayload expirablePayload = mailboxData.expirablePayload; ExpirableMessage expirableMessage = mailboxData.expirableMessage;
if (expirablePayload instanceof ExpirableMailboxPayload) { if (expirableMessage instanceof MailboxMessage) {
ExpirableMailboxPayload expirableMailboxPayload = (ExpirableMailboxPayload) expirablePayload; MailboxMessage expirableMailboxPayload = (MailboxMessage) expirableMessage;
PrefixedSealedAndSignedMessage prefixedSealedAndSignedMessage = expirableMailboxPayload.prefixedSealedAndSignedMessage; PrefixedSealedAndSignedMessage prefixedSealedAndSignedMessage = expirableMailboxPayload.prefixedSealedAndSignedMessage;
if (verifyAddressPrefixHash(prefixedSealedAndSignedMessage)) { if (verifyAddressPrefixHash(prefixedSealedAndSignedMessage)) {
try { try {
DecryptedMsgWithPubKey decryptedMsgWithPubKey = optionalEncryptionService.get().decryptAndVerify( DecryptedMsgWithPubKey decryptedMsgWithPubKey = optionalEncryptionService.get().decryptAndVerify(
prefixedSealedAndSignedMessage.sealedAndSigned); prefixedSealedAndSignedMessage.sealedAndSigned);
if (decryptedMsgWithPubKey.message instanceof MailboxMessage) { if (decryptedMsgWithPubKey.message instanceof io.bitsquare.p2p.messaging.MailboxMessage) {
MailboxMessage mailboxMessage = (MailboxMessage) decryptedMsgWithPubKey.message; io.bitsquare.p2p.messaging.MailboxMessage mailboxMessage = (io.bitsquare.p2p.messaging.MailboxMessage) decryptedMsgWithPubKey.message;
NodeAddress senderNodeAddress = mailboxMessage.getSenderNodeAddress(); NodeAddress senderNodeAddress = mailboxMessage.getSenderNodeAddress();
checkNotNull(senderNodeAddress, "senderAddress must not be null for mailbox messages"); checkNotNull(senderNodeAddress, "senderAddress must not be null for mailbox messages");
@ -455,7 +455,7 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis
} }
public void sendEncryptedMailboxMessage(NodeAddress peersNodeAddress, PubKeyRing peersPubKeyRing, public void sendEncryptedMailboxMessage(NodeAddress peersNodeAddress, PubKeyRing peersPubKeyRing,
MailboxMessage message, io.bitsquare.p2p.messaging.MailboxMessage message,
SendMailboxMessageListener sendMailboxMessageListener) { SendMailboxMessageListener sendMailboxMessageListener) {
Log.traceCall("message " + message); Log.traceCall("message " + message);
checkNotNull(peersNodeAddress, checkNotNull(peersNodeAddress,
@ -494,7 +494,7 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis
log.info("We cannot send message to peer. Peer might be offline. We will store message in mailbox."); log.info("We cannot send message to peer. Peer might be offline. We will store message in mailbox.");
log.trace("create MailboxEntry with peerAddress " + peersNodeAddress); log.trace("create MailboxEntry with peerAddress " + peersNodeAddress);
PublicKey receiverStoragePublicKey = peersPubKeyRing.getSignaturePubKey(); PublicKey receiverStoragePublicKey = peersPubKeyRing.getSignaturePubKey();
addMailboxData(new ExpirableMailboxPayload(prefixedSealedAndSignedMessage, addMailboxData(new MailboxMessage(prefixedSealedAndSignedMessage,
optionalKeyRing.get().getSignatureKeyPair().getPublic(), optionalKeyRing.get().getSignatureKeyPair().getPublic(),
receiverStoragePublicKey), receiverStoragePublicKey),
receiverStoragePublicKey, receiverStoragePublicKey,
@ -516,7 +516,7 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis
} }
private void addMailboxData(ExpirableMailboxPayload expirableMailboxPayload, private void addMailboxData(MailboxMessage expirableMailboxPayload,
PublicKey receiversPublicKey, PublicKey receiversPublicKey,
SendMailboxMessageListener sendMailboxMessageListener) { SendMailboxMessageListener sendMailboxMessageListener) {
Log.traceCall(); Log.traceCall();
@ -587,8 +587,8 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis
if (isBootstrapped()) { if (isBootstrapped()) {
if (mailboxMap.containsKey(decryptedMsgWithPubKey)) { if (mailboxMap.containsKey(decryptedMsgWithPubKey)) {
ProtectedMailboxData mailboxData = mailboxMap.get(decryptedMsgWithPubKey); ProtectedMailboxData mailboxData = mailboxMap.get(decryptedMsgWithPubKey);
if (mailboxData != null && mailboxData.expirablePayload instanceof ExpirableMailboxPayload) { if (mailboxData != null && mailboxData.expirableMessage instanceof MailboxMessage) {
ExpirableMailboxPayload expirableMailboxPayload = (ExpirableMailboxPayload) mailboxData.expirablePayload; MailboxMessage expirableMailboxPayload = (MailboxMessage) mailboxData.expirableMessage;
PublicKey receiversPubKey = mailboxData.receiversPubKey; PublicKey receiversPubKey = mailboxData.receiversPubKey;
checkArgument(receiversPubKey.equals(optionalKeyRing.get().getSignatureKeyPair().getPublic()), checkArgument(receiversPubKey.equals(optionalKeyRing.get().getSignatureKeyPair().getPublic()),
"receiversPubKey is not matching with our key. That must not happen."); "receiversPubKey is not matching with our key. That must not happen.");
@ -620,22 +620,22 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis
// Data storage // Data storage
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
public boolean addData(ExpirablePayload expirablePayload) { public boolean addData(ExpirableMessage expirableMessage) {
Log.traceCall(); Log.traceCall();
return doAddData(expirablePayload, false); return doAddData(expirableMessage, false);
} }
public boolean republishData(ExpirablePayload expirablePayload) { public boolean republishData(ExpirableMessage expirableMessage) {
Log.traceCall(); Log.traceCall();
return doAddData(expirablePayload, true); return doAddData(expirableMessage, true);
} }
private boolean doAddData(ExpirablePayload expirablePayload, boolean rePublish) { private boolean doAddData(ExpirableMessage expirableMessage, boolean rePublish) {
Log.traceCall(); Log.traceCall();
checkArgument(optionalKeyRing.isPresent(), "keyRing not set. Seems that is called on a seed node which must not happen."); checkArgument(optionalKeyRing.isPresent(), "keyRing not set. Seems that is called on a seed node which must not happen.");
if (isBootstrapped()) { if (isBootstrapped()) {
try { try {
ProtectedData protectedData = p2PDataStorage.getDataWithSignedSeqNr(expirablePayload, optionalKeyRing.get().getSignatureKeyPair()); ProtectedData protectedData = p2PDataStorage.getDataWithSignedSeqNr(expirableMessage, optionalKeyRing.get().getSignatureKeyPair());
if (rePublish) if (rePublish)
return p2PDataStorage.rePublish(protectedData, networkNode.getNodeAddress()); return p2PDataStorage.rePublish(protectedData, networkNode.getNodeAddress());
else else
@ -649,12 +649,12 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis
} }
} }
public boolean removeData(ExpirablePayload expirablePayload) { public boolean removeData(ExpirableMessage expirableMessage) {
Log.traceCall(); Log.traceCall();
checkArgument(optionalKeyRing.isPresent(), "keyRing not set. Seems that is called on a seed node which must not happen."); checkArgument(optionalKeyRing.isPresent(), "keyRing not set. Seems that is called on a seed node which must not happen.");
if (isBootstrapped()) { if (isBootstrapped()) {
try { try {
ProtectedData protectedData = p2PDataStorage.getDataWithSignedSeqNr(expirablePayload, optionalKeyRing.get().getSignatureKeyPair()); ProtectedData protectedData = p2PDataStorage.getDataWithSignedSeqNr(expirableMessage, optionalKeyRing.get().getSignatureKeyPair());
return p2PDataStorage.remove(protectedData, networkNode.getNodeAddress()); return p2PDataStorage.remove(protectedData, networkNode.getNodeAddress());
} catch (CryptoException e) { } catch (CryptoException e) {
log.error("Signing at getDataWithSignedSeqNr failed. That should never happen."); log.error("Signing at getDataWithSignedSeqNr failed. That should never happen.");

View file

@ -138,7 +138,7 @@ public class P2PDataStorage implements MessageListener {
private boolean doAdd(ProtectedData protectedData, @Nullable NodeAddress sender, boolean rePublish) { private boolean doAdd(ProtectedData protectedData, @Nullable NodeAddress sender, boolean rePublish) {
Log.traceCall(); Log.traceCall();
ByteArray hashOfPayload = getHashAsByteArray(protectedData.expirablePayload); ByteArray hashOfPayload = getHashAsByteArray(protectedData.expirableMessage);
boolean result = checkPublicKeys(protectedData, true) boolean result = checkPublicKeys(protectedData, true)
&& checkSignature(protectedData) && checkSignature(protectedData)
&& isSequenceNrValid(protectedData, hashOfPayload); && isSequenceNrValid(protectedData, hashOfPayload);
@ -178,7 +178,7 @@ public class P2PDataStorage implements MessageListener {
public boolean remove(ProtectedData protectedData, @Nullable NodeAddress sender) { public boolean remove(ProtectedData protectedData, @Nullable NodeAddress sender) {
Log.traceCall(); Log.traceCall();
ByteArray hashOfPayload = getHashAsByteArray(protectedData.expirablePayload); ByteArray hashOfPayload = getHashAsByteArray(protectedData.expirableMessage);
boolean containsKey = map.containsKey(hashOfPayload); boolean containsKey = map.containsKey(hashOfPayload);
if (!containsKey) log.debug("Remove data ignored as we don't have an entry for that data."); if (!containsKey) log.debug("Remove data ignored as we don't have an entry for that data.");
boolean result = containsKey boolean result = containsKey
@ -203,13 +203,13 @@ public class P2PDataStorage implements MessageListener {
public boolean removeMailboxData(ProtectedMailboxData protectedMailboxData, @Nullable NodeAddress sender) { public boolean removeMailboxData(ProtectedMailboxData protectedMailboxData, @Nullable NodeAddress sender) {
Log.traceCall(); Log.traceCall();
ByteArray hashOfData = getHashAsByteArray(protectedMailboxData.expirablePayload); ByteArray hashOfData = getHashAsByteArray(protectedMailboxData.expirableMessage);
boolean containsKey = map.containsKey(hashOfData); boolean containsKey = map.containsKey(hashOfData);
if (!containsKey) log.debug("Remove data ignored as we don't have an entry for that data."); if (!containsKey) log.debug("Remove data ignored as we don't have an entry for that data.");
boolean result = containsKey boolean result = containsKey
&& checkPublicKeys(protectedMailboxData, false) && checkPublicKeys(protectedMailboxData, false)
&& isSequenceNrValid(protectedMailboxData, hashOfData) && isSequenceNrValid(protectedMailboxData, hashOfData)
&& protectedMailboxData.receiversPubKey.equals(protectedMailboxData.ownerStoragePubKey) // at remove both keys are the same (only receiver is able to remove data) && protectedMailboxData.receiversPubKey.equals(protectedMailboxData.ownerPubKey) // at remove both keys are the same (only receiver is able to remove data)
&& checkSignature(protectedMailboxData) && checkSignature(protectedMailboxData)
&& checkIfStoredMailboxDataMatchesNewMailboxData(protectedMailboxData, hashOfData); && checkIfStoredMailboxDataMatchesNewMailboxData(protectedMailboxData, hashOfData);
@ -230,7 +230,7 @@ public class P2PDataStorage implements MessageListener {
return map; return map;
} }
public ProtectedData getDataWithSignedSeqNr(ExpirablePayload payload, KeyPair ownerStoragePubKey) public ProtectedData getDataWithSignedSeqNr(ExpirableMessage payload, KeyPair ownerStoragePubKey)
throws CryptoException { throws CryptoException {
Log.traceCall(); Log.traceCall();
ByteArray hashOfData = getHashAsByteArray(payload); ByteArray hashOfData = getHashAsByteArray(payload);
@ -245,7 +245,7 @@ public class P2PDataStorage implements MessageListener {
return new ProtectedData(payload, payload.getTTL(), ownerStoragePubKey.getPublic(), sequenceNumber, signature); return new ProtectedData(payload, payload.getTTL(), ownerStoragePubKey.getPublic(), sequenceNumber, signature);
} }
public ProtectedMailboxData getMailboxDataWithSignedSeqNr(ExpirableMailboxPayload expirableMailboxPayload, public ProtectedMailboxData getMailboxDataWithSignedSeqNr(MailboxMessage expirableMailboxPayload,
KeyPair storageSignaturePubKey, PublicKey receiversPublicKey) KeyPair storageSignaturePubKey, PublicKey receiversPublicKey)
throws CryptoException { throws CryptoException {
Log.traceCall(); Log.traceCall();
@ -301,9 +301,9 @@ public class P2PDataStorage implements MessageListener {
private boolean checkSignature(ProtectedData data) { private boolean checkSignature(ProtectedData data) {
Log.traceCall(); Log.traceCall();
byte[] hashOfDataAndSeqNr = Hash.getHash(new DataAndSeqNrPair(data.expirablePayload, data.sequenceNumber)); byte[] hashOfDataAndSeqNr = Hash.getHash(new DataAndSeqNrPair(data.expirableMessage, data.sequenceNumber));
try { try {
boolean result = Sig.verify(data.ownerStoragePubKey, hashOfDataAndSeqNr, data.signature); boolean result = Sig.verify(data.ownerPubKey, hashOfDataAndSeqNr, data.signature);
if (!result) if (!result)
log.error("Signature verification failed at checkSignature. " + log.error("Signature verification failed at checkSignature. " +
"That should not happen. Consider it might be an attempt of fraud."); "That should not happen. Consider it might be an attempt of fraud.");
@ -318,14 +318,14 @@ public class P2PDataStorage implements MessageListener {
private boolean checkPublicKeys(ProtectedData data, boolean isAddOperation) { private boolean checkPublicKeys(ProtectedData data, boolean isAddOperation) {
Log.traceCall(); Log.traceCall();
boolean result = false; boolean result = false;
if (data.expirablePayload instanceof ExpirableMailboxPayload) { if (data.expirableMessage instanceof MailboxMessage) {
ExpirableMailboxPayload expirableMailboxPayload = (ExpirableMailboxPayload) data.expirablePayload; MailboxMessage expirableMailboxPayload = (MailboxMessage) data.expirableMessage;
if (isAddOperation) if (isAddOperation)
result = expirableMailboxPayload.senderStoragePublicKey.equals(data.ownerStoragePubKey); result = expirableMailboxPayload.senderPubKeyForAddOperation.equals(data.ownerPubKey);
else else
result = expirableMailboxPayload.receiverStoragePublicKey.equals(data.ownerStoragePubKey); result = expirableMailboxPayload.receiverPubKeyForRemoveOperation.equals(data.ownerPubKey);
} else if (data.expirablePayload instanceof PubKeyProtectedExpirablePayload) { } else if (data.expirableMessage instanceof StorageMessage) {
result = ((PubKeyProtectedExpirablePayload) data.expirablePayload).getPubKey().equals(data.ownerStoragePubKey); result = ((StorageMessage) data.expirableMessage).getOwnerPubKey().equals(data.ownerPubKey);
} }
if (!result) if (!result)
@ -336,7 +336,7 @@ public class P2PDataStorage implements MessageListener {
private boolean checkIfStoredDataPubKeyMatchesNewDataPubKey(ProtectedData data, ByteArray hashOfData) { private boolean checkIfStoredDataPubKeyMatchesNewDataPubKey(ProtectedData data, ByteArray hashOfData) {
Log.traceCall(); Log.traceCall();
ProtectedData storedData = map.get(hashOfData); ProtectedData storedData = map.get(hashOfData);
boolean result = storedData.ownerStoragePubKey.equals(data.ownerStoragePubKey); boolean result = storedData.ownerPubKey.equals(data.ownerPubKey);
if (!result) if (!result)
log.error("New data entry does not match our stored data. Consider it might be an attempt of fraud"); log.error("New data entry does not match our stored data. Consider it might be an attempt of fraud");
@ -350,7 +350,7 @@ public class P2PDataStorage implements MessageListener {
ProtectedMailboxData storedMailboxData = (ProtectedMailboxData) storedData; ProtectedMailboxData storedMailboxData = (ProtectedMailboxData) storedData;
// publicKey is not the same (stored: sender, new: receiver) // publicKey is not the same (stored: sender, new: receiver)
boolean result = storedMailboxData.receiversPubKey.equals(data.receiversPubKey) boolean result = storedMailboxData.receiversPubKey.equals(data.receiversPubKey)
&& getHashAsByteArray(storedMailboxData.expirablePayload).equals(hashOfData); && getHashAsByteArray(storedMailboxData.expirableMessage).equals(hashOfData);
if (!result) if (!result)
log.error("New data entry does not match our stored data. Consider it might be an attempt of fraud"); log.error("New data entry does not match our stored data. Consider it might be an attempt of fraud");
@ -365,7 +365,7 @@ public class P2PDataStorage implements MessageListener {
broadcaster.broadcast(message, sender); broadcaster.broadcast(message, sender);
} }
private ByteArray getHashAsByteArray(ExpirablePayload payload) { private ByteArray getHashAsByteArray(ExpirableMessage payload) {
return new ByteArray(Hash.getHash(payload)); return new ByteArray(Hash.getHash(payload));
} }

View file

@ -1,54 +0,0 @@
package io.bitsquare.p2p.storage.data;
import io.bitsquare.app.Version;
import io.bitsquare.crypto.PrefixedSealedAndSignedMessage;
import java.security.PublicKey;
import java.util.concurrent.TimeUnit;
public final class ExpirableMailboxPayload implements ExpirablePayload {
// That object is sent over the wire, so we need to take care of version compatibility.
private static final long serialVersionUID = Version.P2P_NETWORK_VERSION;
private static final long TTL = TimeUnit.DAYS.toMillis(10);
public final PrefixedSealedAndSignedMessage prefixedSealedAndSignedMessage;
public final PublicKey senderStoragePublicKey;
public final PublicKey receiverStoragePublicKey;
public ExpirableMailboxPayload(PrefixedSealedAndSignedMessage prefixedSealedAndSignedMessage, PublicKey senderStoragePublicKey, PublicKey receiverStoragePublicKey) {
this.prefixedSealedAndSignedMessage = prefixedSealedAndSignedMessage;
this.senderStoragePublicKey = senderStoragePublicKey;
this.receiverStoragePublicKey = receiverStoragePublicKey;
}
@Override
public long getTTL() {
return TTL;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ExpirableMailboxPayload)) return false;
ExpirableMailboxPayload that = (ExpirableMailboxPayload) o;
return !(prefixedSealedAndSignedMessage != null ? !prefixedSealedAndSignedMessage.equals(that.prefixedSealedAndSignedMessage) : that.prefixedSealedAndSignedMessage != null);
}
@Override
public int hashCode() {
return prefixedSealedAndSignedMessage != null ? prefixedSealedAndSignedMessage.hashCode() : 0;
}
@Override
public String toString() {
return "ExpirableMailboxPayload{" +
"prefixedSealedAndSignedMessage=" + prefixedSealedAndSignedMessage +
", senderStoragePublicKey.hashCode()=" + senderStoragePublicKey.hashCode() +
", receiverStoragePublicKey.hashCode()=" + receiverStoragePublicKey.hashCode() +
'}';
}
}

View file

@ -0,0 +1,18 @@
package io.bitsquare.p2p.storage.data;
import java.io.Serializable;
/**
* Messages which support a time to live
* <p>
* Implementations:
*
* @see StorageMessage
* @see MailboxMessage
*/
public interface ExpirableMessage extends Serializable {
/**
* @return Time to live in milli seconds
*/
long getTTL();
}

View file

@ -1,7 +0,0 @@
package io.bitsquare.p2p.storage.data;
import java.io.Serializable;
public interface ExpirablePayload extends Serializable {
long getTTL();
}

View file

@ -0,0 +1,82 @@
package io.bitsquare.p2p.storage.data;
import io.bitsquare.app.Version;
import io.bitsquare.crypto.PrefixedSealedAndSignedMessage;
import io.bitsquare.p2p.NodeAddress;
import java.security.PublicKey;
import java.util.concurrent.TimeUnit;
/**
* Envelope message which support a time to live and sender and receivers pub keys for storage operations.
* It differs from the ProtectedExpirableMessage in the way that the sender is permitted to do an add operation
* but only the receiver is permitted to remove the data.
* That is the typical requirement for a mailbox like system.
* <p>
* Typical payloads are trade or dispute messages to be stored when the peer is offline.
*/
public final class MailboxMessage implements ExpirableMessage {
// That object is sent over the wire, so we need to take care of version compatibility.
private static final long serialVersionUID = Version.P2P_NETWORK_VERSION;
private static final long TTL = TimeUnit.DAYS.toMillis(10);
/**
* The encrypted and signed payload message
*/
public final PrefixedSealedAndSignedMessage prefixedSealedAndSignedMessage;
/**
* Used for check if the add operation is permitted.
* senderStoragePublicKey has to be equal to the ownerPubKey of the ProtectedData
*
* @see io.bitsquare.p2p.storage.data.ProtectedData#ownerPubKey
* @see io.bitsquare.p2p.storage.P2PDataStorage#add(ProtectedData, NodeAddress)
*/
public final PublicKey senderPubKeyForAddOperation;
/**
* Used for check if the remove operation is permitted.
* senderStoragePublicKey has to be equal to the ownerPubKey of the ProtectedData
*
* @see io.bitsquare.p2p.storage.data.ProtectedData#ownerPubKey
* @see io.bitsquare.p2p.storage.P2PDataStorage#remove(ProtectedData, NodeAddress)
*/
public final PublicKey receiverPubKeyForRemoveOperation;
public MailboxMessage(PrefixedSealedAndSignedMessage prefixedSealedAndSignedMessage, PublicKey senderPubKeyForAddOperation, PublicKey receiverPubKeyForRemoveOperation) {
this.prefixedSealedAndSignedMessage = prefixedSealedAndSignedMessage;
this.senderPubKeyForAddOperation = senderPubKeyForAddOperation;
this.receiverPubKeyForRemoveOperation = receiverPubKeyForRemoveOperation;
}
@Override
public long getTTL() {
return TTL;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof MailboxMessage)) return false;
MailboxMessage that = (MailboxMessage) o;
return !(prefixedSealedAndSignedMessage != null ? !prefixedSealedAndSignedMessage.equals(that.prefixedSealedAndSignedMessage) : that.prefixedSealedAndSignedMessage != null);
}
@Override
public int hashCode() {
return prefixedSealedAndSignedMessage != null ? prefixedSealedAndSignedMessage.hashCode() : 0;
}
@Override
public String toString() {
return "ExpirableMailboxPayload{" +
"prefixedSealedAndSignedMessage=" + prefixedSealedAndSignedMessage +
", senderStoragePublicKey.hashCode()=" + senderPubKeyForAddOperation.hashCode() +
", receiverStoragePublicKey.hashCode()=" + receiverPubKeyForRemoveOperation.hashCode() +
'}';
}
}

View file

@ -14,18 +14,18 @@ import java.util.Date;
public class ProtectedData implements Serializable { public class ProtectedData implements Serializable {
private static final Logger log = LoggerFactory.getLogger(P2PDataStorage.class); private static final Logger log = LoggerFactory.getLogger(P2PDataStorage.class);
public final ExpirablePayload expirablePayload; public final ExpirableMessage expirableMessage;
transient public long ttl; transient public long ttl;
public final PublicKey ownerStoragePubKey; public final PublicKey ownerPubKey;
public final int sequenceNumber; public final int sequenceNumber;
public final byte[] signature; public final byte[] signature;
@VisibleForTesting @VisibleForTesting
transient public Date date; transient public Date date;
public ProtectedData(ExpirablePayload expirablePayload, long ttl, PublicKey ownerStoragePubKey, int sequenceNumber, byte[] signature) { public ProtectedData(ExpirableMessage expirableMessage, long ttl, PublicKey ownerPubKey, int sequenceNumber, byte[] signature) {
this.expirablePayload = expirablePayload; this.expirableMessage = expirableMessage;
this.ttl = ttl; this.ttl = ttl;
this.ownerStoragePubKey = ownerStoragePubKey; this.ownerPubKey = ownerPubKey;
this.sequenceNumber = sequenceNumber; this.sequenceNumber = sequenceNumber;
this.signature = signature; this.signature = signature;
this.date = new Date(); this.date = new Date();
@ -34,7 +34,7 @@ public class ProtectedData implements Serializable {
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException { private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
try { try {
in.defaultReadObject(); in.defaultReadObject();
ttl = expirablePayload.getTTL(); ttl = expirableMessage.getTTL();
date = new Date(); date = new Date();
} catch (Throwable t) { } catch (Throwable t) {
@ -50,11 +50,11 @@ public class ProtectedData implements Serializable {
@Override @Override
public String toString() { public String toString() {
return "ProtectedData{" + return "ProtectedData{" +
"expirablePayload=" + expirablePayload + "expirablePayload=" + expirableMessage +
", ttl=" + ttl + ", ttl=" + ttl +
", date=" + date + ", date=" + date +
", sequenceNumber=" + sequenceNumber + ", sequenceNumber=" + sequenceNumber +
", ownerStoragePubKey.hashCode()=" + ownerStoragePubKey.hashCode() + ", ownerStoragePubKey.hashCode()=" + ownerPubKey.hashCode() +
", signature.hashCode()=" + Arrays.toString(signature).hashCode() + ", signature.hashCode()=" + Arrays.toString(signature).hashCode() +
'}'; '}';
} }

View file

@ -14,7 +14,7 @@ public class ProtectedMailboxData extends ProtectedData {
public final PublicKey receiversPubKey; public final PublicKey receiversPubKey;
public ProtectedMailboxData(ExpirableMailboxPayload data, long ttl, PublicKey ownerStoragePubKey, int sequenceNumber, byte[] signature, PublicKey receiversPubKey) { public ProtectedMailboxData(MailboxMessage data, long ttl, PublicKey ownerStoragePubKey, int sequenceNumber, byte[] signature, PublicKey receiversPubKey) {
super(data, ttl, ownerStoragePubKey, sequenceNumber, signature); super(data, ttl, ownerStoragePubKey, sequenceNumber, signature);
this.receiversPubKey = receiversPubKey; this.receiversPubKey = receiversPubKey;
@ -23,7 +23,7 @@ public class ProtectedMailboxData extends ProtectedData {
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException { private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
try { try {
in.defaultReadObject(); in.defaultReadObject();
ttl = expirablePayload.getTTL(); ttl = expirableMessage.getTTL();
// in case the reported creation date is in the future // in case the reported creation date is in the future
// we reset the date to the current time // we reset the date to the current time

View file

@ -1,7 +0,0 @@
package io.bitsquare.p2p.storage.data;
import java.security.PublicKey;
public interface PubKeyProtectedExpirablePayload extends ExpirablePayload {
PublicKey getPubKey();
}

View file

@ -0,0 +1,27 @@
package io.bitsquare.p2p.storage.data;
import io.bitsquare.p2p.NodeAddress;
import java.security.PublicKey;
/**
* Messages which support ownership protection (using signatures) and a time to live
* <p>
* Implementations:
* io.bitsquare.alert.Alert
* io.bitsquare.arbitration.Arbitrator
* io.bitsquare.trade.offer.Offer
*/
public interface StorageMessage extends ExpirableMessage {
/**
* Used for check if the add or remove operation is permitted.
* Only data owner can add or remove the data.
* OwnerPubKey has to be equal to the ownerPubKey of the ProtectedData
*
* @return The public key of the data owner.
* @see io.bitsquare.p2p.storage.data.ProtectedData#ownerPubKey
* @see io.bitsquare.p2p.storage.P2PDataStorage#add(ProtectedData, NodeAddress)
* @see io.bitsquare.p2p.storage.P2PDataStorage#remove(ProtectedData, NodeAddress)
*/
PublicKey getOwnerPubKey();
}

View file

@ -158,16 +158,16 @@ public class P2PServiceTest {
// try to manipulate seq nr. -> fails // try to manipulate seq nr. -> fails
ProtectedData origProtectedData = p2PService3.getDataMap().values().stream().findFirst().get(); ProtectedData origProtectedData = p2PService3.getDataMap().values().stream().findFirst().get();
ProtectedData protectedDataManipulated = new ProtectedData(origProtectedData.expirablePayload, origProtectedData.ttl, origProtectedData.ownerStoragePubKey, origProtectedData.sequenceNumber + 1, origProtectedData.signature); ProtectedData protectedDataManipulated = new ProtectedData(origProtectedData.expirableMessage, origProtectedData.ttl, origProtectedData.ownerPubKey, origProtectedData.sequenceNumber + 1, origProtectedData.signature);
Assert.assertFalse(p2PService3.removeData(protectedDataManipulated.expirablePayload)); Assert.assertFalse(p2PService3.removeData(protectedDataManipulated.expirableMessage));
Thread.sleep(sleepTime); Thread.sleep(sleepTime);
Assert.assertEquals(1, p2PService1.getDataMap().size()); Assert.assertEquals(1, p2PService1.getDataMap().size());
Assert.assertEquals(1, p2PService2.getDataMap().size()); Assert.assertEquals(1, p2PService2.getDataMap().size());
Assert.assertEquals(1, p2PService3.getDataMap().size()); Assert.assertEquals(1, p2PService3.getDataMap().size());
// try to manipulate seq nr. + pubKey -> fails // try to manipulate seq nr. + pubKey -> fails
protectedDataManipulated = new ProtectedData(origProtectedData.expirablePayload, origProtectedData.ttl, msgSignatureKeyPairAdversary.getPublic(), origProtectedData.sequenceNumber + 1, origProtectedData.signature); protectedDataManipulated = new ProtectedData(origProtectedData.expirableMessage, origProtectedData.ttl, msgSignatureKeyPairAdversary.getPublic(), origProtectedData.sequenceNumber + 1, origProtectedData.signature);
Assert.assertFalse(p2PService3.removeData(protectedDataManipulated.expirablePayload)); Assert.assertFalse(p2PService3.removeData(protectedDataManipulated.expirableMessage));
Thread.sleep(sleepTime); Thread.sleep(sleepTime);
Assert.assertEquals(1, p2PService1.getDataMap().size()); Assert.assertEquals(1, p2PService1.getDataMap().size());
Assert.assertEquals(1, p2PService2.getDataMap().size()); Assert.assertEquals(1, p2PService2.getDataMap().size());
@ -175,10 +175,10 @@ public class P2PServiceTest {
// try to manipulate seq nr. + pubKey + sig -> fails // try to manipulate seq nr. + pubKey + sig -> fails
int sequenceNumberManipulated = origProtectedData.sequenceNumber + 1; int sequenceNumberManipulated = origProtectedData.sequenceNumber + 1;
byte[] hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(origProtectedData.expirablePayload, sequenceNumberManipulated)); byte[] hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(origProtectedData.expirableMessage, sequenceNumberManipulated));
byte[] signature = Sig.sign(msgSignatureKeyPairAdversary.getPrivate(), hashOfDataAndSeqNr); byte[] signature = Sig.sign(msgSignatureKeyPairAdversary.getPrivate(), hashOfDataAndSeqNr);
protectedDataManipulated = new ProtectedData(origProtectedData.expirablePayload, origProtectedData.ttl, msgSignatureKeyPairAdversary.getPublic(), sequenceNumberManipulated, signature); protectedDataManipulated = new ProtectedData(origProtectedData.expirableMessage, origProtectedData.ttl, msgSignatureKeyPairAdversary.getPublic(), sequenceNumberManipulated, signature);
Assert.assertFalse(p2PService3.removeData(protectedDataManipulated.expirablePayload)); Assert.assertFalse(p2PService3.removeData(protectedDataManipulated.expirableMessage));
Thread.sleep(sleepTime); Thread.sleep(sleepTime);
Assert.assertEquals(1, p2PService1.getDataMap().size()); Assert.assertEquals(1, p2PService1.getDataMap().size());
Assert.assertEquals(1, p2PService2.getDataMap().size()); Assert.assertEquals(1, p2PService2.getDataMap().size());
@ -199,8 +199,8 @@ public class P2PServiceTest {
sequenceNumberManipulated = 0; sequenceNumberManipulated = 0;
hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(manipulatedData, sequenceNumberManipulated)); hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(manipulatedData, sequenceNumberManipulated));
signature = Sig.sign(msgSignatureKeyPairAdversary.getPrivate(), hashOfDataAndSeqNr); signature = Sig.sign(msgSignatureKeyPairAdversary.getPrivate(), hashOfDataAndSeqNr);
protectedDataManipulated = new ProtectedData(origProtectedData.expirablePayload, origProtectedData.ttl, msgSignatureKeyPairAdversary.getPublic(), sequenceNumberManipulated, signature); protectedDataManipulated = new ProtectedData(origProtectedData.expirableMessage, origProtectedData.ttl, msgSignatureKeyPairAdversary.getPublic(), sequenceNumberManipulated, signature);
Assert.assertFalse(p2PService3.addData(protectedDataManipulated.expirablePayload)); Assert.assertFalse(p2PService3.addData(protectedDataManipulated.expirableMessage));
Thread.sleep(sleepTime); Thread.sleep(sleepTime);
Assert.assertEquals(0, p2PService1.getDataMap().size()); Assert.assertEquals(0, p2PService1.getDataMap().size());
Assert.assertEquals(0, p2PService2.getDataMap().size()); Assert.assertEquals(0, p2PService2.getDataMap().size());
@ -211,8 +211,8 @@ public class P2PServiceTest {
sequenceNumberManipulated = 0; sequenceNumberManipulated = 0;
hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(manipulatedData, sequenceNumberManipulated)); hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(manipulatedData, sequenceNumberManipulated));
signature = Sig.sign(msgSignatureKeyPairAdversary.getPrivate(), hashOfDataAndSeqNr); signature = Sig.sign(msgSignatureKeyPairAdversary.getPrivate(), hashOfDataAndSeqNr);
protectedDataManipulated = new ProtectedData(origProtectedData.expirablePayload, origProtectedData.ttl, msgSignatureKeyPairAdversary.getPublic(), sequenceNumberManipulated, signature); protectedDataManipulated = new ProtectedData(origProtectedData.expirableMessage, origProtectedData.ttl, msgSignatureKeyPairAdversary.getPublic(), sequenceNumberManipulated, signature);
Assert.assertFalse(p2PService3.addData(protectedDataManipulated.expirablePayload)); Assert.assertFalse(p2PService3.addData(protectedDataManipulated.expirableMessage));
Thread.sleep(sleepTime); Thread.sleep(sleepTime);
Assert.assertEquals(0, p2PService1.getDataMap().size()); Assert.assertEquals(0, p2PService1.getDataMap().size());
Assert.assertEquals(0, p2PService2.getDataMap().size()); Assert.assertEquals(0, p2PService2.getDataMap().size());
@ -225,7 +225,7 @@ public class P2PServiceTest {
hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(manipulatedData, sequenceNumberManipulated)); hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(manipulatedData, sequenceNumberManipulated));
signature = Sig.sign(msgSignatureKeyPairAdversary.getPrivate(), hashOfDataAndSeqNr); signature = Sig.sign(msgSignatureKeyPairAdversary.getPrivate(), hashOfDataAndSeqNr);
protectedDataManipulated = new ProtectedData(manipulatedData, origProtectedData.ttl, msgSignatureKeyPairAdversary.getPublic(), sequenceNumberManipulated, signature); protectedDataManipulated = new ProtectedData(manipulatedData, origProtectedData.ttl, msgSignatureKeyPairAdversary.getPublic(), sequenceNumberManipulated, signature);
Assert.assertTrue(p2PService3.addData(protectedDataManipulated.expirablePayload)); Assert.assertTrue(p2PService3.addData(protectedDataManipulated.expirableMessage));
Thread.sleep(sleepTime); Thread.sleep(sleepTime);
Assert.assertEquals(1, p2PService1.getDataMap().size()); Assert.assertEquals(1, p2PService1.getDataMap().size());
Assert.assertEquals(1, p2PService2.getDataMap().size()); Assert.assertEquals(1, p2PService2.getDataMap().size());
@ -244,8 +244,8 @@ public class P2PServiceTest {
sequenceNumberManipulated = 0; sequenceNumberManipulated = 0;
hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(manipulatedData, sequenceNumberManipulated)); hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(manipulatedData, sequenceNumberManipulated));
signature = Sig.sign(msgSignatureKeyPairAdversary.getPrivate(), hashOfDataAndSeqNr); signature = Sig.sign(msgSignatureKeyPairAdversary.getPrivate(), hashOfDataAndSeqNr);
protectedDataManipulated = new ProtectedData(origProtectedData.expirablePayload, origProtectedData.ttl, msgSignatureKeyPairAdversary.getPublic(), sequenceNumberManipulated, signature); protectedDataManipulated = new ProtectedData(origProtectedData.expirableMessage, origProtectedData.ttl, msgSignatureKeyPairAdversary.getPublic(), sequenceNumberManipulated, signature);
Assert.assertFalse(p2PService3.addData(protectedDataManipulated.expirablePayload)); Assert.assertFalse(p2PService3.addData(protectedDataManipulated.expirableMessage));
Thread.sleep(sleepTime); Thread.sleep(sleepTime);
Assert.assertEquals(0, p2PService1.getDataMap().size()); Assert.assertEquals(0, p2PService1.getDataMap().size());
Assert.assertEquals(0, p2PService2.getDataMap().size()); Assert.assertEquals(0, p2PService2.getDataMap().size());
@ -256,14 +256,14 @@ public class P2PServiceTest {
hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(manipulatedData, sequenceNumberManipulated)); hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(manipulatedData, sequenceNumberManipulated));
signature = Sig.sign(msgSignatureKeyPairAdversary.getPrivate(), hashOfDataAndSeqNr); signature = Sig.sign(msgSignatureKeyPairAdversary.getPrivate(), hashOfDataAndSeqNr);
protectedDataManipulated = new ProtectedData(manipulatedData, origProtectedData.ttl, msgSignatureKeyPairAdversary.getPublic(), sequenceNumberManipulated, signature); protectedDataManipulated = new ProtectedData(manipulatedData, origProtectedData.ttl, msgSignatureKeyPairAdversary.getPublic(), sequenceNumberManipulated, signature);
Assert.assertTrue(p2PService3.addData(protectedDataManipulated.expirablePayload)); Assert.assertTrue(p2PService3.addData(protectedDataManipulated.expirableMessage));
Thread.sleep(sleepTime); Thread.sleep(sleepTime);
Assert.assertEquals(1, p2PService1.getDataMap().size()); Assert.assertEquals(1, p2PService1.getDataMap().size());
Assert.assertEquals(1, p2PService2.getDataMap().size()); Assert.assertEquals(1, p2PService2.getDataMap().size());
Assert.assertEquals(1, p2PService3.getDataMap().size()); Assert.assertEquals(1, p2PService3.getDataMap().size());
// lets reset map // lets reset map
Assert.assertTrue(p2PService3.removeData(protectedDataManipulated.expirablePayload)); Assert.assertTrue(p2PService3.removeData(protectedDataManipulated.expirableMessage));
Thread.sleep(sleepTime); Thread.sleep(sleepTime);
Assert.assertEquals(0, p2PService1.getDataMap().size()); Assert.assertEquals(0, p2PService1.getDataMap().size());
Assert.assertEquals(0, p2PService2.getDataMap().size()); Assert.assertEquals(0, p2PService2.getDataMap().size());

View file

@ -3,9 +3,9 @@ package io.bitsquare.p2p.mocks;
import io.bitsquare.app.Version; import io.bitsquare.app.Version;
import io.bitsquare.p2p.NodeAddress; import io.bitsquare.p2p.NodeAddress;
import io.bitsquare.p2p.messaging.MailboxMessage; import io.bitsquare.p2p.messaging.MailboxMessage;
import io.bitsquare.p2p.storage.data.ExpirablePayload; import io.bitsquare.p2p.storage.data.ExpirableMessage;
public final class MockMailboxMessage implements MailboxMessage, ExpirablePayload { public final class MockMailboxMessage implements MailboxMessage, ExpirableMessage {
private final int messageVersion = Version.getP2PMessageVersion(); private final int messageVersion = Version.getP2PMessageVersion();
public final String msg; public final String msg;
public final NodeAddress senderNodeAddress; public final NodeAddress senderNodeAddress;

View file

@ -2,9 +2,9 @@ package io.bitsquare.p2p.mocks;
import io.bitsquare.app.Version; import io.bitsquare.app.Version;
import io.bitsquare.p2p.Message; import io.bitsquare.p2p.Message;
import io.bitsquare.p2p.storage.data.ExpirablePayload; import io.bitsquare.p2p.storage.data.ExpirableMessage;
public final class MockMessage implements Message, ExpirablePayload { public final class MockMessage implements Message, ExpirableMessage {
public final String msg; public final String msg;
public long ttl; public long ttl;
private final int messageVersion = Version.getP2PMessageVersion(); private final int messageVersion = Version.getP2PMessageVersion();

View file

@ -11,7 +11,7 @@ import io.bitsquare.p2p.mocks.MockMessage;
import io.bitsquare.p2p.network.NetworkNode; import io.bitsquare.p2p.network.NetworkNode;
import io.bitsquare.p2p.peers.Broadcaster; import io.bitsquare.p2p.peers.Broadcaster;
import io.bitsquare.p2p.peers.PeerManager; import io.bitsquare.p2p.peers.PeerManager;
import io.bitsquare.p2p.storage.data.ExpirableMailboxPayload; import io.bitsquare.p2p.storage.data.MailboxMessage;
import io.bitsquare.p2p.storage.data.ProtectedData; import io.bitsquare.p2p.storage.data.ProtectedData;
import io.bitsquare.p2p.storage.data.ProtectedMailboxData; import io.bitsquare.p2p.storage.data.ProtectedMailboxData;
import io.bitsquare.p2p.storage.mocks.MockData; import io.bitsquare.p2p.storage.mocks.MockData;
@ -102,9 +102,9 @@ public class ProtectedDataStorageTest {
Assert.assertEquals(1, dataStorage1.getMap().size()); Assert.assertEquals(1, dataStorage1.getMap().size());
int newSequenceNumber = data.sequenceNumber + 1; int newSequenceNumber = data.sequenceNumber + 1;
byte[] hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(data.expirablePayload, newSequenceNumber)); byte[] hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(data.expirableMessage, newSequenceNumber));
byte[] signature = Sig.sign(storageSignatureKeyPair1.getPrivate(), hashOfDataAndSeqNr); byte[] signature = Sig.sign(storageSignatureKeyPair1.getPrivate(), hashOfDataAndSeqNr);
ProtectedData dataToRemove = new ProtectedData(data.expirablePayload, data.ttl, data.ownerStoragePubKey, newSequenceNumber, signature); ProtectedData dataToRemove = new ProtectedData(data.expirableMessage, data.ttl, data.ownerPubKey, newSequenceNumber, signature);
Assert.assertTrue(dataStorage1.remove(dataToRemove, null)); Assert.assertTrue(dataStorage1.remove(dataToRemove, null));
Assert.assertEquals(0, dataStorage1.getMap().size()); Assert.assertEquals(0, dataStorage1.getMap().size());
} }
@ -134,9 +134,9 @@ public class ProtectedDataStorageTest {
// add with date in future // add with date in future
data = dataStorage1.getDataWithSignedSeqNr(mockData, storageSignatureKeyPair1); data = dataStorage1.getDataWithSignedSeqNr(mockData, storageSignatureKeyPair1);
int newSequenceNumber = data.sequenceNumber + 1; int newSequenceNumber = data.sequenceNumber + 1;
byte[] hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(data.expirablePayload, newSequenceNumber)); byte[] hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(data.expirableMessage, newSequenceNumber));
byte[] signature = Sig.sign(storageSignatureKeyPair1.getPrivate(), hashOfDataAndSeqNr); byte[] signature = Sig.sign(storageSignatureKeyPair1.getPrivate(), hashOfDataAndSeqNr);
ProtectedData dataWithFutureDate = new ProtectedData(data.expirablePayload, data.ttl, data.ownerStoragePubKey, newSequenceNumber, signature); ProtectedData dataWithFutureDate = new ProtectedData(data.expirableMessage, data.ttl, data.ownerPubKey, newSequenceNumber, signature);
dataWithFutureDate.date = new Date(new Date().getTime() + 60 * 60 * sleepTime); dataWithFutureDate.date = new Date(new Date().getTime() + 60 * 60 * sleepTime);
// force serialisation (date check is done in readObject) // force serialisation (date check is done in readObject)
ProtectedData newData = Utilities.deserialize(Utilities.serialize(dataWithFutureDate)); ProtectedData newData = Utilities.deserialize(Utilities.serialize(dataWithFutureDate));
@ -155,65 +155,65 @@ public class ProtectedDataStorageTest {
// remove with not updated seq nr -> failure // remove with not updated seq nr -> failure
int newSequenceNumber = 0; int newSequenceNumber = 0;
byte[] hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(data.expirablePayload, newSequenceNumber)); byte[] hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(data.expirableMessage, newSequenceNumber));
byte[] signature = Sig.sign(storageSignatureKeyPair1.getPrivate(), hashOfDataAndSeqNr); byte[] signature = Sig.sign(storageSignatureKeyPair1.getPrivate(), hashOfDataAndSeqNr);
ProtectedData dataToRemove = new ProtectedData(data.expirablePayload, data.ttl, data.ownerStoragePubKey, newSequenceNumber, signature); ProtectedData dataToRemove = new ProtectedData(data.expirableMessage, data.ttl, data.ownerPubKey, newSequenceNumber, signature);
Assert.assertFalse(dataStorage1.remove(dataToRemove, null)); Assert.assertFalse(dataStorage1.remove(dataToRemove, null));
// remove with too high updated seq nr -> ok // remove with too high updated seq nr -> ok
newSequenceNumber = 2; newSequenceNumber = 2;
hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(data.expirablePayload, newSequenceNumber)); hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(data.expirableMessage, newSequenceNumber));
signature = Sig.sign(storageSignatureKeyPair1.getPrivate(), hashOfDataAndSeqNr); signature = Sig.sign(storageSignatureKeyPair1.getPrivate(), hashOfDataAndSeqNr);
dataToRemove = new ProtectedData(data.expirablePayload, data.ttl, data.ownerStoragePubKey, newSequenceNumber, signature); dataToRemove = new ProtectedData(data.expirableMessage, data.ttl, data.ownerPubKey, newSequenceNumber, signature);
Assert.assertTrue(dataStorage1.remove(dataToRemove, null)); Assert.assertTrue(dataStorage1.remove(dataToRemove, null));
// add to empty map, any seq nr. -> ok // add to empty map, any seq nr. -> ok
newSequenceNumber = 2; newSequenceNumber = 2;
hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(data.expirablePayload, newSequenceNumber)); hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(data.expirableMessage, newSequenceNumber));
signature = Sig.sign(storageSignatureKeyPair1.getPrivate(), hashOfDataAndSeqNr); signature = Sig.sign(storageSignatureKeyPair1.getPrivate(), hashOfDataAndSeqNr);
ProtectedData dataToAdd = new ProtectedData(data.expirablePayload, data.ttl, data.ownerStoragePubKey, newSequenceNumber, signature); ProtectedData dataToAdd = new ProtectedData(data.expirableMessage, data.ttl, data.ownerPubKey, newSequenceNumber, signature);
Assert.assertTrue(dataStorage1.add(dataToAdd, null)); Assert.assertTrue(dataStorage1.add(dataToAdd, null));
// add with updated seq nr below previous -> failure // add with updated seq nr below previous -> failure
newSequenceNumber = 1; newSequenceNumber = 1;
hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(data.expirablePayload, newSequenceNumber)); hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(data.expirableMessage, newSequenceNumber));
signature = Sig.sign(storageSignatureKeyPair1.getPrivate(), hashOfDataAndSeqNr); signature = Sig.sign(storageSignatureKeyPair1.getPrivate(), hashOfDataAndSeqNr);
dataToAdd = new ProtectedData(data.expirablePayload, data.ttl, data.ownerStoragePubKey, newSequenceNumber, signature); dataToAdd = new ProtectedData(data.expirableMessage, data.ttl, data.ownerPubKey, newSequenceNumber, signature);
Assert.assertFalse(dataStorage1.add(dataToAdd, null)); Assert.assertFalse(dataStorage1.add(dataToAdd, null));
// add with updated seq nr over previous -> ok // add with updated seq nr over previous -> ok
newSequenceNumber = 3; newSequenceNumber = 3;
hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(data.expirablePayload, newSequenceNumber)); hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(data.expirableMessage, newSequenceNumber));
signature = Sig.sign(storageSignatureKeyPair1.getPrivate(), hashOfDataAndSeqNr); signature = Sig.sign(storageSignatureKeyPair1.getPrivate(), hashOfDataAndSeqNr);
dataToAdd = new ProtectedData(data.expirablePayload, data.ttl, data.ownerStoragePubKey, newSequenceNumber, signature); dataToAdd = new ProtectedData(data.expirableMessage, data.ttl, data.ownerPubKey, newSequenceNumber, signature);
Assert.assertTrue(dataStorage1.add(dataToAdd, null)); Assert.assertTrue(dataStorage1.add(dataToAdd, null));
// add with same seq nr -> failure // add with same seq nr -> failure
newSequenceNumber = 3; newSequenceNumber = 3;
hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(data.expirablePayload, newSequenceNumber)); hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(data.expirableMessage, newSequenceNumber));
signature = Sig.sign(storageSignatureKeyPair1.getPrivate(), hashOfDataAndSeqNr); signature = Sig.sign(storageSignatureKeyPair1.getPrivate(), hashOfDataAndSeqNr);
dataToAdd = new ProtectedData(data.expirablePayload, data.ttl, data.ownerStoragePubKey, newSequenceNumber, signature); dataToAdd = new ProtectedData(data.expirableMessage, data.ttl, data.ownerPubKey, newSequenceNumber, signature);
Assert.assertFalse(dataStorage1.add(dataToAdd, null)); Assert.assertFalse(dataStorage1.add(dataToAdd, null));
// add with same data but higher seq nr. -> ok, ignore // add with same data but higher seq nr. -> ok, ignore
newSequenceNumber = 4; newSequenceNumber = 4;
hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(data.expirablePayload, newSequenceNumber)); hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(data.expirableMessage, newSequenceNumber));
signature = Sig.sign(storageSignatureKeyPair1.getPrivate(), hashOfDataAndSeqNr); signature = Sig.sign(storageSignatureKeyPair1.getPrivate(), hashOfDataAndSeqNr);
dataToAdd = new ProtectedData(data.expirablePayload, data.ttl, data.ownerStoragePubKey, newSequenceNumber, signature); dataToAdd = new ProtectedData(data.expirableMessage, data.ttl, data.ownerPubKey, newSequenceNumber, signature);
Assert.assertTrue(dataStorage1.add(dataToAdd, null)); Assert.assertTrue(dataStorage1.add(dataToAdd, null));
// remove with with same seq nr as prev. ignored -> failed // remove with with same seq nr as prev. ignored -> failed
newSequenceNumber = 4; newSequenceNumber = 4;
hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(data.expirablePayload, newSequenceNumber)); hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(data.expirableMessage, newSequenceNumber));
signature = Sig.sign(storageSignatureKeyPair1.getPrivate(), hashOfDataAndSeqNr); signature = Sig.sign(storageSignatureKeyPair1.getPrivate(), hashOfDataAndSeqNr);
dataToRemove = new ProtectedData(data.expirablePayload, data.ttl, data.ownerStoragePubKey, newSequenceNumber, signature); dataToRemove = new ProtectedData(data.expirableMessage, data.ttl, data.ownerPubKey, newSequenceNumber, signature);
Assert.assertFalse(dataStorage1.remove(dataToRemove, null)); Assert.assertFalse(dataStorage1.remove(dataToRemove, null));
// remove with with higher seq nr -> ok // remove with with higher seq nr -> ok
newSequenceNumber = 5; newSequenceNumber = 5;
hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(data.expirablePayload, newSequenceNumber)); hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(data.expirableMessage, newSequenceNumber));
signature = Sig.sign(storageSignatureKeyPair1.getPrivate(), hashOfDataAndSeqNr); signature = Sig.sign(storageSignatureKeyPair1.getPrivate(), hashOfDataAndSeqNr);
dataToRemove = new ProtectedData(data.expirablePayload, data.ttl, data.ownerStoragePubKey, newSequenceNumber, signature); dataToRemove = new ProtectedData(data.expirableMessage, data.ttl, data.ownerPubKey, newSequenceNumber, signature);
Assert.assertTrue(dataStorage1.remove(dataToRemove, null)); Assert.assertTrue(dataStorage1.remove(dataToRemove, null));
} }
@ -225,7 +225,7 @@ public class ProtectedDataStorageTest {
PrefixedSealedAndSignedMessage prefixedSealedAndSignedMessage = new PrefixedSealedAndSignedMessage(networkNode1.getNodeAddress(), PrefixedSealedAndSignedMessage prefixedSealedAndSignedMessage = new PrefixedSealedAndSignedMessage(networkNode1.getNodeAddress(),
encryptionService1.encryptAndSign(keyRing1.getPubKeyRing(), mockMessage), encryptionService1.encryptAndSign(keyRing1.getPubKeyRing(), mockMessage),
Hash.getHash("aa")); Hash.getHash("aa"));
ExpirableMailboxPayload expirableMailboxPayload = new ExpirableMailboxPayload(prefixedSealedAndSignedMessage, MailboxMessage expirableMailboxPayload = new MailboxMessage(prefixedSealedAndSignedMessage,
keyRing1.getSignatureKeyPair().getPublic(), keyRing1.getSignatureKeyPair().getPublic(),
keyRing2.getSignatureKeyPair().getPublic()); keyRing2.getSignatureKeyPair().getPublic());
@ -236,7 +236,7 @@ public class ProtectedDataStorageTest {
// receiver (storageSignatureKeyPair2) // receiver (storageSignatureKeyPair2)
int newSequenceNumber = data.sequenceNumber + 1; int newSequenceNumber = data.sequenceNumber + 1;
byte[] hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(data.expirablePayload, newSequenceNumber)); byte[] hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(data.expirableMessage, newSequenceNumber));
byte[] signature; byte[] signature;
ProtectedMailboxData dataToRemove; ProtectedMailboxData dataToRemove;
@ -253,7 +253,7 @@ public class ProtectedDataStorageTest {
// wrong signingKey // wrong signingKey
signature = Sig.sign(storageSignatureKeyPair2.getPrivate(), hashOfDataAndSeqNr); signature = Sig.sign(storageSignatureKeyPair2.getPrivate(), hashOfDataAndSeqNr);
dataToRemove = new ProtectedMailboxData(expirableMailboxPayload, data.ttl, data.ownerStoragePubKey, newSequenceNumber, signature, storageSignatureKeyPair2.getPublic()); dataToRemove = new ProtectedMailboxData(expirableMailboxPayload, data.ttl, data.ownerPubKey, newSequenceNumber, signature, storageSignatureKeyPair2.getPublic());
Assert.assertFalse(dataStorage1.removeMailboxData(dataToRemove, null)); Assert.assertFalse(dataStorage1.removeMailboxData(dataToRemove, null));
// wrong peerPubKey // wrong peerPubKey

View file

@ -1,10 +1,10 @@
package io.bitsquare.p2p.storage.mocks; package io.bitsquare.p2p.storage.mocks;
import io.bitsquare.p2p.storage.data.PubKeyProtectedExpirablePayload; import io.bitsquare.p2p.storage.data.StorageMessage;
import java.security.PublicKey; import java.security.PublicKey;
public class MockData implements PubKeyProtectedExpirablePayload { public class MockData implements StorageMessage {
public final String msg; public final String msg;
public final PublicKey publicKey; public final PublicKey publicKey;
public long ttl; public long ttl;
@ -43,7 +43,7 @@ public class MockData implements PubKeyProtectedExpirablePayload {
} }
@Override @Override
public PublicKey getPubKey() { public PublicKey getOwnerPubKey() {
return publicKey; return publicKey;
} }
} }