mirror of
https://github.com/bisq-network/bisq.git
synced 2024-11-19 18:03:12 +01:00
Renaming of storage messages, add comments
This commit is contained in:
parent
fa2da8dbe5
commit
3dbcfb650a
@ -18,12 +18,12 @@
|
||||
package io.bitsquare.alert;
|
||||
|
||||
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.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.
|
||||
private static final long serialVersionUID = Version.P2P_NETWORK_VERSION;
|
||||
|
||||
@ -54,7 +54,7 @@ public final class Alert implements PubKeyProtectedExpirablePayload {
|
||||
}
|
||||
|
||||
@Override
|
||||
public PublicKey getPubKey() {
|
||||
public PublicKey getOwnerPubKey() {
|
||||
return storagePublicKey;
|
||||
}
|
||||
|
||||
|
@ -62,7 +62,7 @@ public class AlertManager {
|
||||
alertService.addHashSetChangedListener(new HashMapChangedListener() {
|
||||
@Override
|
||||
public void onAdded(ProtectedData entry) {
|
||||
Serializable data = entry.expirablePayload;
|
||||
Serializable data = entry.expirableMessage;
|
||||
if (data instanceof Alert) {
|
||||
Alert alert = (Alert) data;
|
||||
if (verifySignature(alert))
|
||||
@ -72,7 +72,7 @@ public class AlertManager {
|
||||
|
||||
@Override
|
||||
public void onRemoved(ProtectedData entry) {
|
||||
Serializable data = entry.expirablePayload;
|
||||
Serializable data = entry.expirableMessage;
|
||||
if (data instanceof Alert) {
|
||||
Alert alert = (Alert) data;
|
||||
if (verifySignature(alert))
|
||||
|
@ -20,7 +20,7 @@ package io.bitsquare.arbitration;
|
||||
import io.bitsquare.app.Version;
|
||||
import io.bitsquare.common.crypto.PubKeyRing;
|
||||
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.util.Arrays;
|
||||
@ -28,7 +28,7 @@ import java.util.Date;
|
||||
import java.util.List;
|
||||
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.
|
||||
private static final long serialVersionUID = Version.P2P_NETWORK_VERSION;
|
||||
|
||||
@ -73,7 +73,7 @@ public final class Arbitrator implements PubKeyProtectedExpirablePayload {
|
||||
}
|
||||
|
||||
@Override
|
||||
public PublicKey getPubKey() {
|
||||
public PublicKey getOwnerPubKey() {
|
||||
return pubKeyRing.getSignaturePubKey();
|
||||
}
|
||||
|
||||
|
@ -84,8 +84,8 @@ public class ArbitratorService {
|
||||
|
||||
public Map<NodeAddress, Arbitrator> getArbitrators() {
|
||||
Set<Arbitrator> arbitratorSet = p2PService.getDataMap().values().stream()
|
||||
.filter(e -> e.expirablePayload instanceof Arbitrator)
|
||||
.map(e -> (Arbitrator) e.expirablePayload)
|
||||
.filter(e -> e.expirableMessage instanceof Arbitrator)
|
||||
.map(e -> (Arbitrator) e.expirableMessage)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
Map<NodeAddress, Arbitrator> map = new HashMap<>();
|
||||
|
@ -25,7 +25,7 @@ import io.bitsquare.common.handlers.ResultHandler;
|
||||
import io.bitsquare.common.util.JsonExclude;
|
||||
import io.bitsquare.locale.Country;
|
||||
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.trade.protocol.availability.OfferAvailabilityModel;
|
||||
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.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.
|
||||
@JsonExclude
|
||||
private static final long serialVersionUID = Version.P2P_NETWORK_VERSION;
|
||||
@ -254,7 +254,7 @@ public final class Offer implements PubKeyProtectedExpirablePayload {
|
||||
}
|
||||
|
||||
@Override
|
||||
public PublicKey getPubKey() {
|
||||
public PublicKey getOwnerPubKey() {
|
||||
return pubKeyRing.getSignaturePubKey();
|
||||
}
|
||||
|
||||
|
@ -85,8 +85,8 @@ public class OfferBookService {
|
||||
|
||||
public List<Offer> getOffers() {
|
||||
final List<Offer> offers = p2PService.getDataMap().values().stream()
|
||||
.filter(e -> e.expirablePayload instanceof Offer)
|
||||
.map(e -> (Offer) e.expirablePayload)
|
||||
.filter(e -> e.expirableMessage instanceof Offer)
|
||||
.map(e -> (Offer) e.expirableMessage)
|
||||
.collect(Collectors.toList());
|
||||
return offers;
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ public class OfferBook {
|
||||
@Override
|
||||
public void onAdded(ProtectedData entry) {
|
||||
log.debug("onAdded " + entry);
|
||||
Serializable data = entry.expirablePayload;
|
||||
Serializable data = entry.expirableMessage;
|
||||
if (data instanceof Offer) {
|
||||
Offer offer = (Offer) data;
|
||||
OfferBookListItem offerBookListItem = new OfferBookListItem(offer);
|
||||
@ -69,8 +69,8 @@ public class OfferBook {
|
||||
@Override
|
||||
public void onRemoved(ProtectedData entry) {
|
||||
log.debug("onRemoved " + entry);
|
||||
if (entry.expirablePayload instanceof Offer) {
|
||||
Offer offer = (Offer) entry.expirablePayload;
|
||||
if (entry.expirableMessage instanceof Offer) {
|
||||
Offer offer = (Offer) entry.expirableMessage;
|
||||
|
||||
// Update state in case that that offer is used in the take offer screen, so it gets updated correctly
|
||||
offer.setState(Offer.State.REMOVED);
|
||||
|
@ -23,8 +23,8 @@ import io.bitsquare.p2p.peers.RequestDataManager;
|
||||
import io.bitsquare.p2p.seed.SeedNodesRepository;
|
||||
import io.bitsquare.p2p.storage.HashMapChangedListener;
|
||||
import io.bitsquare.p2p.storage.P2PDataStorage;
|
||||
import io.bitsquare.p2p.storage.data.ExpirableMailboxPayload;
|
||||
import io.bitsquare.p2p.storage.data.ExpirablePayload;
|
||||
import io.bitsquare.p2p.storage.data.ExpirableMessage;
|
||||
import io.bitsquare.p2p.storage.data.MailboxMessage;
|
||||
import io.bitsquare.p2p.storage.data.ProtectedData;
|
||||
import io.bitsquare.p2p.storage.data.ProtectedMailboxData;
|
||||
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
|
||||
if (optionalEncryptionService.isPresent()) {
|
||||
Log.traceCall();
|
||||
ExpirablePayload expirablePayload = mailboxData.expirablePayload;
|
||||
if (expirablePayload instanceof ExpirableMailboxPayload) {
|
||||
ExpirableMailboxPayload expirableMailboxPayload = (ExpirableMailboxPayload) expirablePayload;
|
||||
ExpirableMessage expirableMessage = mailboxData.expirableMessage;
|
||||
if (expirableMessage instanceof MailboxMessage) {
|
||||
MailboxMessage expirableMailboxPayload = (MailboxMessage) expirableMessage;
|
||||
PrefixedSealedAndSignedMessage prefixedSealedAndSignedMessage = expirableMailboxPayload.prefixedSealedAndSignedMessage;
|
||||
if (verifyAddressPrefixHash(prefixedSealedAndSignedMessage)) {
|
||||
try {
|
||||
DecryptedMsgWithPubKey decryptedMsgWithPubKey = optionalEncryptionService.get().decryptAndVerify(
|
||||
prefixedSealedAndSignedMessage.sealedAndSigned);
|
||||
if (decryptedMsgWithPubKey.message instanceof MailboxMessage) {
|
||||
MailboxMessage mailboxMessage = (MailboxMessage) decryptedMsgWithPubKey.message;
|
||||
if (decryptedMsgWithPubKey.message instanceof io.bitsquare.p2p.messaging.MailboxMessage) {
|
||||
io.bitsquare.p2p.messaging.MailboxMessage mailboxMessage = (io.bitsquare.p2p.messaging.MailboxMessage) decryptedMsgWithPubKey.message;
|
||||
NodeAddress senderNodeAddress = mailboxMessage.getSenderNodeAddress();
|
||||
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,
|
||||
MailboxMessage message,
|
||||
io.bitsquare.p2p.messaging.MailboxMessage message,
|
||||
SendMailboxMessageListener sendMailboxMessageListener) {
|
||||
Log.traceCall("message " + message);
|
||||
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.trace("create MailboxEntry with peerAddress " + peersNodeAddress);
|
||||
PublicKey receiverStoragePublicKey = peersPubKeyRing.getSignaturePubKey();
|
||||
addMailboxData(new ExpirableMailboxPayload(prefixedSealedAndSignedMessage,
|
||||
addMailboxData(new MailboxMessage(prefixedSealedAndSignedMessage,
|
||||
optionalKeyRing.get().getSignatureKeyPair().getPublic(),
|
||||
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,
|
||||
SendMailboxMessageListener sendMailboxMessageListener) {
|
||||
Log.traceCall();
|
||||
@ -587,8 +587,8 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis
|
||||
if (isBootstrapped()) {
|
||||
if (mailboxMap.containsKey(decryptedMsgWithPubKey)) {
|
||||
ProtectedMailboxData mailboxData = mailboxMap.get(decryptedMsgWithPubKey);
|
||||
if (mailboxData != null && mailboxData.expirablePayload instanceof ExpirableMailboxPayload) {
|
||||
ExpirableMailboxPayload expirableMailboxPayload = (ExpirableMailboxPayload) mailboxData.expirablePayload;
|
||||
if (mailboxData != null && mailboxData.expirableMessage instanceof MailboxMessage) {
|
||||
MailboxMessage expirableMailboxPayload = (MailboxMessage) mailboxData.expirableMessage;
|
||||
PublicKey receiversPubKey = mailboxData.receiversPubKey;
|
||||
checkArgument(receiversPubKey.equals(optionalKeyRing.get().getSignatureKeyPair().getPublic()),
|
||||
"receiversPubKey is not matching with our key. That must not happen.");
|
||||
@ -620,22 +620,22 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis
|
||||
// Data storage
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
public boolean addData(ExpirablePayload expirablePayload) {
|
||||
public boolean addData(ExpirableMessage expirableMessage) {
|
||||
Log.traceCall();
|
||||
return doAddData(expirablePayload, false);
|
||||
return doAddData(expirableMessage, false);
|
||||
}
|
||||
|
||||
public boolean republishData(ExpirablePayload expirablePayload) {
|
||||
public boolean republishData(ExpirableMessage expirableMessage) {
|
||||
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();
|
||||
checkArgument(optionalKeyRing.isPresent(), "keyRing not set. Seems that is called on a seed node which must not happen.");
|
||||
if (isBootstrapped()) {
|
||||
try {
|
||||
ProtectedData protectedData = p2PDataStorage.getDataWithSignedSeqNr(expirablePayload, optionalKeyRing.get().getSignatureKeyPair());
|
||||
ProtectedData protectedData = p2PDataStorage.getDataWithSignedSeqNr(expirableMessage, optionalKeyRing.get().getSignatureKeyPair());
|
||||
if (rePublish)
|
||||
return p2PDataStorage.rePublish(protectedData, networkNode.getNodeAddress());
|
||||
else
|
||||
@ -649,12 +649,12 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis
|
||||
}
|
||||
}
|
||||
|
||||
public boolean removeData(ExpirablePayload expirablePayload) {
|
||||
public boolean removeData(ExpirableMessage expirableMessage) {
|
||||
Log.traceCall();
|
||||
checkArgument(optionalKeyRing.isPresent(), "keyRing not set. Seems that is called on a seed node which must not happen.");
|
||||
if (isBootstrapped()) {
|
||||
try {
|
||||
ProtectedData protectedData = p2PDataStorage.getDataWithSignedSeqNr(expirablePayload, optionalKeyRing.get().getSignatureKeyPair());
|
||||
ProtectedData protectedData = p2PDataStorage.getDataWithSignedSeqNr(expirableMessage, optionalKeyRing.get().getSignatureKeyPair());
|
||||
return p2PDataStorage.remove(protectedData, networkNode.getNodeAddress());
|
||||
} catch (CryptoException e) {
|
||||
log.error("Signing at getDataWithSignedSeqNr failed. That should never happen.");
|
||||
|
@ -138,7 +138,7 @@ public class P2PDataStorage implements MessageListener {
|
||||
|
||||
private boolean doAdd(ProtectedData protectedData, @Nullable NodeAddress sender, boolean rePublish) {
|
||||
Log.traceCall();
|
||||
ByteArray hashOfPayload = getHashAsByteArray(protectedData.expirablePayload);
|
||||
ByteArray hashOfPayload = getHashAsByteArray(protectedData.expirableMessage);
|
||||
boolean result = checkPublicKeys(protectedData, true)
|
||||
&& checkSignature(protectedData)
|
||||
&& isSequenceNrValid(protectedData, hashOfPayload);
|
||||
@ -178,7 +178,7 @@ public class P2PDataStorage implements MessageListener {
|
||||
|
||||
public boolean remove(ProtectedData protectedData, @Nullable NodeAddress sender) {
|
||||
Log.traceCall();
|
||||
ByteArray hashOfPayload = getHashAsByteArray(protectedData.expirablePayload);
|
||||
ByteArray hashOfPayload = getHashAsByteArray(protectedData.expirableMessage);
|
||||
boolean containsKey = map.containsKey(hashOfPayload);
|
||||
if (!containsKey) log.debug("Remove data ignored as we don't have an entry for that data.");
|
||||
boolean result = containsKey
|
||||
@ -203,13 +203,13 @@ public class P2PDataStorage implements MessageListener {
|
||||
|
||||
public boolean removeMailboxData(ProtectedMailboxData protectedMailboxData, @Nullable NodeAddress sender) {
|
||||
Log.traceCall();
|
||||
ByteArray hashOfData = getHashAsByteArray(protectedMailboxData.expirablePayload);
|
||||
ByteArray hashOfData = getHashAsByteArray(protectedMailboxData.expirableMessage);
|
||||
boolean containsKey = map.containsKey(hashOfData);
|
||||
if (!containsKey) log.debug("Remove data ignored as we don't have an entry for that data.");
|
||||
boolean result = containsKey
|
||||
&& checkPublicKeys(protectedMailboxData, false)
|
||||
&& 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)
|
||||
&& checkIfStoredMailboxDataMatchesNewMailboxData(protectedMailboxData, hashOfData);
|
||||
|
||||
@ -230,7 +230,7 @@ public class P2PDataStorage implements MessageListener {
|
||||
return map;
|
||||
}
|
||||
|
||||
public ProtectedData getDataWithSignedSeqNr(ExpirablePayload payload, KeyPair ownerStoragePubKey)
|
||||
public ProtectedData getDataWithSignedSeqNr(ExpirableMessage payload, KeyPair ownerStoragePubKey)
|
||||
throws CryptoException {
|
||||
Log.traceCall();
|
||||
ByteArray hashOfData = getHashAsByteArray(payload);
|
||||
@ -245,7 +245,7 @@ public class P2PDataStorage implements MessageListener {
|
||||
return new ProtectedData(payload, payload.getTTL(), ownerStoragePubKey.getPublic(), sequenceNumber, signature);
|
||||
}
|
||||
|
||||
public ProtectedMailboxData getMailboxDataWithSignedSeqNr(ExpirableMailboxPayload expirableMailboxPayload,
|
||||
public ProtectedMailboxData getMailboxDataWithSignedSeqNr(MailboxMessage expirableMailboxPayload,
|
||||
KeyPair storageSignaturePubKey, PublicKey receiversPublicKey)
|
||||
throws CryptoException {
|
||||
Log.traceCall();
|
||||
@ -301,9 +301,9 @@ public class P2PDataStorage implements MessageListener {
|
||||
|
||||
private boolean checkSignature(ProtectedData data) {
|
||||
Log.traceCall();
|
||||
byte[] hashOfDataAndSeqNr = Hash.getHash(new DataAndSeqNrPair(data.expirablePayload, data.sequenceNumber));
|
||||
byte[] hashOfDataAndSeqNr = Hash.getHash(new DataAndSeqNrPair(data.expirableMessage, data.sequenceNumber));
|
||||
try {
|
||||
boolean result = Sig.verify(data.ownerStoragePubKey, hashOfDataAndSeqNr, data.signature);
|
||||
boolean result = Sig.verify(data.ownerPubKey, hashOfDataAndSeqNr, data.signature);
|
||||
if (!result)
|
||||
log.error("Signature verification failed at checkSignature. " +
|
||||
"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) {
|
||||
Log.traceCall();
|
||||
boolean result = false;
|
||||
if (data.expirablePayload instanceof ExpirableMailboxPayload) {
|
||||
ExpirableMailboxPayload expirableMailboxPayload = (ExpirableMailboxPayload) data.expirablePayload;
|
||||
if (data.expirableMessage instanceof MailboxMessage) {
|
||||
MailboxMessage expirableMailboxPayload = (MailboxMessage) data.expirableMessage;
|
||||
if (isAddOperation)
|
||||
result = expirableMailboxPayload.senderStoragePublicKey.equals(data.ownerStoragePubKey);
|
||||
result = expirableMailboxPayload.senderPubKeyForAddOperation.equals(data.ownerPubKey);
|
||||
else
|
||||
result = expirableMailboxPayload.receiverStoragePublicKey.equals(data.ownerStoragePubKey);
|
||||
} else if (data.expirablePayload instanceof PubKeyProtectedExpirablePayload) {
|
||||
result = ((PubKeyProtectedExpirablePayload) data.expirablePayload).getPubKey().equals(data.ownerStoragePubKey);
|
||||
result = expirableMailboxPayload.receiverPubKeyForRemoveOperation.equals(data.ownerPubKey);
|
||||
} else if (data.expirableMessage instanceof StorageMessage) {
|
||||
result = ((StorageMessage) data.expirableMessage).getOwnerPubKey().equals(data.ownerPubKey);
|
||||
}
|
||||
|
||||
if (!result)
|
||||
@ -336,7 +336,7 @@ public class P2PDataStorage implements MessageListener {
|
||||
private boolean checkIfStoredDataPubKeyMatchesNewDataPubKey(ProtectedData data, ByteArray hashOfData) {
|
||||
Log.traceCall();
|
||||
ProtectedData storedData = map.get(hashOfData);
|
||||
boolean result = storedData.ownerStoragePubKey.equals(data.ownerStoragePubKey);
|
||||
boolean result = storedData.ownerPubKey.equals(data.ownerPubKey);
|
||||
if (!result)
|
||||
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;
|
||||
// publicKey is not the same (stored: sender, new: receiver)
|
||||
boolean result = storedMailboxData.receiversPubKey.equals(data.receiversPubKey)
|
||||
&& getHashAsByteArray(storedMailboxData.expirablePayload).equals(hashOfData);
|
||||
&& getHashAsByteArray(storedMailboxData.expirableMessage).equals(hashOfData);
|
||||
if (!result)
|
||||
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);
|
||||
}
|
||||
|
||||
private ByteArray getHashAsByteArray(ExpirablePayload payload) {
|
||||
private ByteArray getHashAsByteArray(ExpirableMessage payload) {
|
||||
return new ByteArray(Hash.getHash(payload));
|
||||
}
|
||||
|
||||
|
@ -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() +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
package io.bitsquare.p2p.storage.data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public interface ExpirablePayload extends Serializable {
|
||||
long getTTL();
|
||||
}
|
@ -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() +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -14,18 +14,18 @@ import java.util.Date;
|
||||
public class ProtectedData implements Serializable {
|
||||
private static final Logger log = LoggerFactory.getLogger(P2PDataStorage.class);
|
||||
|
||||
public final ExpirablePayload expirablePayload;
|
||||
public final ExpirableMessage expirableMessage;
|
||||
transient public long ttl;
|
||||
public final PublicKey ownerStoragePubKey;
|
||||
public final PublicKey ownerPubKey;
|
||||
public final int sequenceNumber;
|
||||
public final byte[] signature;
|
||||
@VisibleForTesting
|
||||
transient public Date date;
|
||||
|
||||
public ProtectedData(ExpirablePayload expirablePayload, long ttl, PublicKey ownerStoragePubKey, int sequenceNumber, byte[] signature) {
|
||||
this.expirablePayload = expirablePayload;
|
||||
public ProtectedData(ExpirableMessage expirableMessage, long ttl, PublicKey ownerPubKey, int sequenceNumber, byte[] signature) {
|
||||
this.expirableMessage = expirableMessage;
|
||||
this.ttl = ttl;
|
||||
this.ownerStoragePubKey = ownerStoragePubKey;
|
||||
this.ownerPubKey = ownerPubKey;
|
||||
this.sequenceNumber = sequenceNumber;
|
||||
this.signature = signature;
|
||||
this.date = new Date();
|
||||
@ -34,7 +34,7 @@ public class ProtectedData implements Serializable {
|
||||
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
|
||||
try {
|
||||
in.defaultReadObject();
|
||||
ttl = expirablePayload.getTTL();
|
||||
ttl = expirableMessage.getTTL();
|
||||
date = new Date();
|
||||
|
||||
} catch (Throwable t) {
|
||||
@ -50,11 +50,11 @@ public class ProtectedData implements Serializable {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ProtectedData{" +
|
||||
"expirablePayload=" + expirablePayload +
|
||||
"expirablePayload=" + expirableMessage +
|
||||
", ttl=" + ttl +
|
||||
", date=" + date +
|
||||
", sequenceNumber=" + sequenceNumber +
|
||||
", ownerStoragePubKey.hashCode()=" + ownerStoragePubKey.hashCode() +
|
||||
", ownerStoragePubKey.hashCode()=" + ownerPubKey.hashCode() +
|
||||
", signature.hashCode()=" + Arrays.toString(signature).hashCode() +
|
||||
'}';
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ public class ProtectedMailboxData extends ProtectedData {
|
||||
|
||||
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);
|
||||
|
||||
this.receiversPubKey = receiversPubKey;
|
||||
@ -23,7 +23,7 @@ public class ProtectedMailboxData extends ProtectedData {
|
||||
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
|
||||
try {
|
||||
in.defaultReadObject();
|
||||
ttl = expirablePayload.getTTL();
|
||||
ttl = expirableMessage.getTTL();
|
||||
|
||||
// in case the reported creation date is in the future
|
||||
// we reset the date to the current time
|
||||
|
@ -1,7 +0,0 @@
|
||||
package io.bitsquare.p2p.storage.data;
|
||||
|
||||
import java.security.PublicKey;
|
||||
|
||||
public interface PubKeyProtectedExpirablePayload extends ExpirablePayload {
|
||||
PublicKey getPubKey();
|
||||
}
|
@ -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();
|
||||
}
|
@ -158,16 +158,16 @@ public class P2PServiceTest {
|
||||
|
||||
// try to manipulate seq nr. -> fails
|
||||
ProtectedData origProtectedData = p2PService3.getDataMap().values().stream().findFirst().get();
|
||||
ProtectedData protectedDataManipulated = new ProtectedData(origProtectedData.expirablePayload, origProtectedData.ttl, origProtectedData.ownerStoragePubKey, origProtectedData.sequenceNumber + 1, origProtectedData.signature);
|
||||
Assert.assertFalse(p2PService3.removeData(protectedDataManipulated.expirablePayload));
|
||||
ProtectedData protectedDataManipulated = new ProtectedData(origProtectedData.expirableMessage, origProtectedData.ttl, origProtectedData.ownerPubKey, origProtectedData.sequenceNumber + 1, origProtectedData.signature);
|
||||
Assert.assertFalse(p2PService3.removeData(protectedDataManipulated.expirableMessage));
|
||||
Thread.sleep(sleepTime);
|
||||
Assert.assertEquals(1, p2PService1.getDataMap().size());
|
||||
Assert.assertEquals(1, p2PService2.getDataMap().size());
|
||||
Assert.assertEquals(1, p2PService3.getDataMap().size());
|
||||
|
||||
// try to manipulate seq nr. + pubKey -> fails
|
||||
protectedDataManipulated = new ProtectedData(origProtectedData.expirablePayload, origProtectedData.ttl, msgSignatureKeyPairAdversary.getPublic(), origProtectedData.sequenceNumber + 1, origProtectedData.signature);
|
||||
Assert.assertFalse(p2PService3.removeData(protectedDataManipulated.expirablePayload));
|
||||
protectedDataManipulated = new ProtectedData(origProtectedData.expirableMessage, origProtectedData.ttl, msgSignatureKeyPairAdversary.getPublic(), origProtectedData.sequenceNumber + 1, origProtectedData.signature);
|
||||
Assert.assertFalse(p2PService3.removeData(protectedDataManipulated.expirableMessage));
|
||||
Thread.sleep(sleepTime);
|
||||
Assert.assertEquals(1, p2PService1.getDataMap().size());
|
||||
Assert.assertEquals(1, p2PService2.getDataMap().size());
|
||||
@ -175,10 +175,10 @@ public class P2PServiceTest {
|
||||
|
||||
// try to manipulate seq nr. + pubKey + sig -> fails
|
||||
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);
|
||||
protectedDataManipulated = new ProtectedData(origProtectedData.expirablePayload, origProtectedData.ttl, msgSignatureKeyPairAdversary.getPublic(), sequenceNumberManipulated, signature);
|
||||
Assert.assertFalse(p2PService3.removeData(protectedDataManipulated.expirablePayload));
|
||||
protectedDataManipulated = new ProtectedData(origProtectedData.expirableMessage, origProtectedData.ttl, msgSignatureKeyPairAdversary.getPublic(), sequenceNumberManipulated, signature);
|
||||
Assert.assertFalse(p2PService3.removeData(protectedDataManipulated.expirableMessage));
|
||||
Thread.sleep(sleepTime);
|
||||
Assert.assertEquals(1, p2PService1.getDataMap().size());
|
||||
Assert.assertEquals(1, p2PService2.getDataMap().size());
|
||||
@ -199,8 +199,8 @@ public class P2PServiceTest {
|
||||
sequenceNumberManipulated = 0;
|
||||
hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(manipulatedData, sequenceNumberManipulated));
|
||||
signature = Sig.sign(msgSignatureKeyPairAdversary.getPrivate(), hashOfDataAndSeqNr);
|
||||
protectedDataManipulated = new ProtectedData(origProtectedData.expirablePayload, origProtectedData.ttl, msgSignatureKeyPairAdversary.getPublic(), sequenceNumberManipulated, signature);
|
||||
Assert.assertFalse(p2PService3.addData(protectedDataManipulated.expirablePayload));
|
||||
protectedDataManipulated = new ProtectedData(origProtectedData.expirableMessage, origProtectedData.ttl, msgSignatureKeyPairAdversary.getPublic(), sequenceNumberManipulated, signature);
|
||||
Assert.assertFalse(p2PService3.addData(protectedDataManipulated.expirableMessage));
|
||||
Thread.sleep(sleepTime);
|
||||
Assert.assertEquals(0, p2PService1.getDataMap().size());
|
||||
Assert.assertEquals(0, p2PService2.getDataMap().size());
|
||||
@ -211,8 +211,8 @@ public class P2PServiceTest {
|
||||
sequenceNumberManipulated = 0;
|
||||
hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(manipulatedData, sequenceNumberManipulated));
|
||||
signature = Sig.sign(msgSignatureKeyPairAdversary.getPrivate(), hashOfDataAndSeqNr);
|
||||
protectedDataManipulated = new ProtectedData(origProtectedData.expirablePayload, origProtectedData.ttl, msgSignatureKeyPairAdversary.getPublic(), sequenceNumberManipulated, signature);
|
||||
Assert.assertFalse(p2PService3.addData(protectedDataManipulated.expirablePayload));
|
||||
protectedDataManipulated = new ProtectedData(origProtectedData.expirableMessage, origProtectedData.ttl, msgSignatureKeyPairAdversary.getPublic(), sequenceNumberManipulated, signature);
|
||||
Assert.assertFalse(p2PService3.addData(protectedDataManipulated.expirableMessage));
|
||||
Thread.sleep(sleepTime);
|
||||
Assert.assertEquals(0, p2PService1.getDataMap().size());
|
||||
Assert.assertEquals(0, p2PService2.getDataMap().size());
|
||||
@ -225,7 +225,7 @@ public class P2PServiceTest {
|
||||
hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(manipulatedData, sequenceNumberManipulated));
|
||||
signature = Sig.sign(msgSignatureKeyPairAdversary.getPrivate(), hashOfDataAndSeqNr);
|
||||
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);
|
||||
Assert.assertEquals(1, p2PService1.getDataMap().size());
|
||||
Assert.assertEquals(1, p2PService2.getDataMap().size());
|
||||
@ -244,8 +244,8 @@ public class P2PServiceTest {
|
||||
sequenceNumberManipulated = 0;
|
||||
hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(manipulatedData, sequenceNumberManipulated));
|
||||
signature = Sig.sign(msgSignatureKeyPairAdversary.getPrivate(), hashOfDataAndSeqNr);
|
||||
protectedDataManipulated = new ProtectedData(origProtectedData.expirablePayload, origProtectedData.ttl, msgSignatureKeyPairAdversary.getPublic(), sequenceNumberManipulated, signature);
|
||||
Assert.assertFalse(p2PService3.addData(protectedDataManipulated.expirablePayload));
|
||||
protectedDataManipulated = new ProtectedData(origProtectedData.expirableMessage, origProtectedData.ttl, msgSignatureKeyPairAdversary.getPublic(), sequenceNumberManipulated, signature);
|
||||
Assert.assertFalse(p2PService3.addData(protectedDataManipulated.expirableMessage));
|
||||
Thread.sleep(sleepTime);
|
||||
Assert.assertEquals(0, p2PService1.getDataMap().size());
|
||||
Assert.assertEquals(0, p2PService2.getDataMap().size());
|
||||
@ -256,14 +256,14 @@ public class P2PServiceTest {
|
||||
hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(manipulatedData, sequenceNumberManipulated));
|
||||
signature = Sig.sign(msgSignatureKeyPairAdversary.getPrivate(), hashOfDataAndSeqNr);
|
||||
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);
|
||||
Assert.assertEquals(1, p2PService1.getDataMap().size());
|
||||
Assert.assertEquals(1, p2PService2.getDataMap().size());
|
||||
Assert.assertEquals(1, p2PService3.getDataMap().size());
|
||||
|
||||
// lets reset map
|
||||
Assert.assertTrue(p2PService3.removeData(protectedDataManipulated.expirablePayload));
|
||||
Assert.assertTrue(p2PService3.removeData(protectedDataManipulated.expirableMessage));
|
||||
Thread.sleep(sleepTime);
|
||||
Assert.assertEquals(0, p2PService1.getDataMap().size());
|
||||
Assert.assertEquals(0, p2PService2.getDataMap().size());
|
||||
|
@ -3,9 +3,9 @@ package io.bitsquare.p2p.mocks;
|
||||
import io.bitsquare.app.Version;
|
||||
import io.bitsquare.p2p.NodeAddress;
|
||||
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();
|
||||
public final String msg;
|
||||
public final NodeAddress senderNodeAddress;
|
||||
|
@ -2,9 +2,9 @@ package io.bitsquare.p2p.mocks;
|
||||
|
||||
import io.bitsquare.app.Version;
|
||||
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 long ttl;
|
||||
private final int messageVersion = Version.getP2PMessageVersion();
|
||||
|
@ -11,7 +11,7 @@ import io.bitsquare.p2p.mocks.MockMessage;
|
||||
import io.bitsquare.p2p.network.NetworkNode;
|
||||
import io.bitsquare.p2p.peers.Broadcaster;
|
||||
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.ProtectedMailboxData;
|
||||
import io.bitsquare.p2p.storage.mocks.MockData;
|
||||
@ -102,9 +102,9 @@ public class ProtectedDataStorageTest {
|
||||
Assert.assertEquals(1, dataStorage1.getMap().size());
|
||||
|
||||
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);
|
||||
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.assertEquals(0, dataStorage1.getMap().size());
|
||||
}
|
||||
@ -134,9 +134,9 @@ public class ProtectedDataStorageTest {
|
||||
// add with date in future
|
||||
data = dataStorage1.getDataWithSignedSeqNr(mockData, storageSignatureKeyPair1);
|
||||
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);
|
||||
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);
|
||||
// force serialisation (date check is done in readObject)
|
||||
ProtectedData newData = Utilities.deserialize(Utilities.serialize(dataWithFutureDate));
|
||||
@ -155,65 +155,65 @@ public class ProtectedDataStorageTest {
|
||||
|
||||
// remove with not updated seq nr -> failure
|
||||
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);
|
||||
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));
|
||||
|
||||
// remove with too high updated seq nr -> ok
|
||||
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);
|
||||
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));
|
||||
|
||||
// add to empty map, any seq nr. -> ok
|
||||
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);
|
||||
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));
|
||||
|
||||
// add with updated seq nr below previous -> failure
|
||||
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);
|
||||
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));
|
||||
|
||||
// add with updated seq nr over previous -> ok
|
||||
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);
|
||||
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));
|
||||
|
||||
// add with same seq nr -> failure
|
||||
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);
|
||||
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));
|
||||
|
||||
// add with same data but higher seq nr. -> ok, ignore
|
||||
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);
|
||||
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));
|
||||
|
||||
// remove with with same seq nr as prev. ignored -> failed
|
||||
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);
|
||||
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));
|
||||
|
||||
// remove with with higher seq nr -> ok
|
||||
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);
|
||||
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));
|
||||
}
|
||||
|
||||
@ -225,7 +225,7 @@ public class ProtectedDataStorageTest {
|
||||
PrefixedSealedAndSignedMessage prefixedSealedAndSignedMessage = new PrefixedSealedAndSignedMessage(networkNode1.getNodeAddress(),
|
||||
encryptionService1.encryptAndSign(keyRing1.getPubKeyRing(), mockMessage),
|
||||
Hash.getHash("aa"));
|
||||
ExpirableMailboxPayload expirableMailboxPayload = new ExpirableMailboxPayload(prefixedSealedAndSignedMessage,
|
||||
MailboxMessage expirableMailboxPayload = new MailboxMessage(prefixedSealedAndSignedMessage,
|
||||
keyRing1.getSignatureKeyPair().getPublic(),
|
||||
keyRing2.getSignatureKeyPair().getPublic());
|
||||
|
||||
@ -236,7 +236,7 @@ public class ProtectedDataStorageTest {
|
||||
|
||||
// receiver (storageSignatureKeyPair2)
|
||||
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;
|
||||
ProtectedMailboxData dataToRemove;
|
||||
@ -253,7 +253,7 @@ public class ProtectedDataStorageTest {
|
||||
|
||||
// wrong signingKey
|
||||
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));
|
||||
|
||||
// wrong peerPubKey
|
||||
|
@ -1,10 +1,10 @@
|
||||
package io.bitsquare.p2p.storage.mocks;
|
||||
|
||||
import io.bitsquare.p2p.storage.data.PubKeyProtectedExpirablePayload;
|
||||
import io.bitsquare.p2p.storage.data.StorageMessage;
|
||||
|
||||
import java.security.PublicKey;
|
||||
|
||||
public class MockData implements PubKeyProtectedExpirablePayload {
|
||||
public class MockData implements StorageMessage {
|
||||
public final String msg;
|
||||
public final PublicKey publicKey;
|
||||
public long ttl;
|
||||
@ -43,7 +43,7 @@ public class MockData implements PubKeyProtectedExpirablePayload {
|
||||
}
|
||||
|
||||
@Override
|
||||
public PublicKey getPubKey() {
|
||||
public PublicKey getOwnerPubKey() {
|
||||
return publicKey;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user