use of io.bitsquare.p2p.storage.storageentry.ProtectedStorageEntry in project bitsquare by bitsquare.
the class P2PDataStorage method init.
private void init(File storageDir) {
sequenceNumberMapStorage.setNumMaxBackupFiles(5);
persistedEntryMapStorage.setNumMaxBackupFiles(1);
HashMap<ByteArray, MapValue> persistedSequenceNumberMap = sequenceNumberMapStorage.<HashMap<ByteArray, MapValue>>initAndGetPersistedWithFileName("SequenceNumberMap");
if (persistedSequenceNumberMap != null)
sequenceNumberMap = getPurgedSequenceNumberMap(persistedSequenceNumberMap);
final String storageFileName = "PersistedP2PStorageData";
File dbDir = new File(storageDir.getAbsolutePath());
if (!dbDir.exists() && !dbDir.mkdir())
log.warn("make dir failed.\ndbDir=" + dbDir.getAbsolutePath());
final File destinationFile = new File(Paths.get(storageDir.getAbsolutePath(), storageFileName).toString());
if (!destinationFile.exists()) {
try {
FileUtil.resourceToFile(storageFileName, destinationFile);
} catch (ResourceNotFoundException | IOException e) {
e.printStackTrace();
log.error("Could not copy the " + storageFileName + " resource file to the db directory.\n" + e.getMessage());
}
} else {
log.debug(storageFileName + " file exists already.");
}
HashMap<ByteArray, ProtectedStorageEntry> persisted = persistedEntryMapStorage.<HashMap<ByteArray, MapValue>>initAndGetPersistedWithFileName(storageFileName);
if (persisted != null) {
persistedMap = persisted;
map.putAll(persistedMap);
// In case another object is already listening...
map.values().stream().forEach(protectedStorageEntry -> hashMapChangedListeners.stream().forEach(e -> e.onAdded(protectedStorageEntry)));
}
}
use of io.bitsquare.p2p.storage.storageentry.ProtectedStorageEntry in project bitsquare by bitsquare.
the class P2PDataStorage method refreshTTL.
public boolean refreshTTL(RefreshTTLMessage refreshTTLMessage, @Nullable NodeAddress sender, boolean isDataOwner) {
Log.traceCall();
byte[] hashOfDataAndSeqNr = refreshTTLMessage.hashOfDataAndSeqNr;
byte[] signature = refreshTTLMessage.signature;
ByteArray hashOfPayload = new ByteArray(refreshTTLMessage.hashOfPayload);
int sequenceNumber = refreshTTLMessage.sequenceNumber;
if (map.containsKey(hashOfPayload)) {
ProtectedStorageEntry storedData = map.get(hashOfPayload);
if (sequenceNumberMap.containsKey(hashOfPayload) && sequenceNumberMap.get(hashOfPayload).sequenceNr == sequenceNumber) {
log.trace("We got that message with that seq nr already from another peer. We ignore that message.");
return true;
} else {
PublicKey ownerPubKey = storedData.getStoragePayload().getOwnerPubKey();
final boolean checkSignature = checkSignature(ownerPubKey, hashOfDataAndSeqNr, signature);
final boolean hasSequenceNrIncreased = hasSequenceNrIncreased(sequenceNumber, hashOfPayload);
final boolean checkIfStoredDataPubKeyMatchesNewDataPubKey = checkIfStoredDataPubKeyMatchesNewDataPubKey(ownerPubKey, hashOfPayload);
boolean allValid = checkSignature && hasSequenceNrIncreased && checkIfStoredDataPubKeyMatchesNewDataPubKey;
// printData("before refreshTTL");
if (allValid) {
log.debug("refreshDate called for storedData:\n\t" + StringUtils.abbreviate(storedData.toString(), 100));
storedData.refreshTTL();
storedData.updateSequenceNumber(sequenceNumber);
storedData.updateSignature(signature);
printData("after refreshTTL");
sequenceNumberMap.put(hashOfPayload, new MapValue(sequenceNumber, System.currentTimeMillis()));
sequenceNumberMapStorage.queueUpForSave(new HashMap<>(sequenceNumberMap), 1000);
broadcast(refreshTTLMessage, sender, null, isDataOwner);
}
return allValid;
}
} else {
log.debug("We don't have data for that refresh message in our map. That is expected if we missed the data publishing.");
return false;
}
}
use of io.bitsquare.p2p.storage.storageentry.ProtectedStorageEntry in project bitsquare by bitsquare.
the class P2PDataStorage method checkIfStoredMailboxDataMatchesNewMailboxData.
private boolean checkIfStoredMailboxDataMatchesNewMailboxData(PublicKey receiversPubKey, ByteArray hashOfData) {
ProtectedStorageEntry storedData = map.get(hashOfData);
if (storedData instanceof ProtectedMailboxStorageEntry) {
ProtectedMailboxStorageEntry entry = (ProtectedMailboxStorageEntry) storedData;
// publicKey is not the same (stored: sender, new: receiver)
boolean result = entry.receiversPubKey.equals(receiversPubKey) && getHashAsByteArray(entry.getStoragePayload()).equals(hashOfData);
if (!result)
log.warn("New data entry does not match our stored data. entry.receiversPubKey=" + entry.receiversPubKey + ", receiversPubKey=" + receiversPubKey);
return result;
} else {
log.error("We expected a MailboxData but got other type. That must never happen. storedData=" + storedData);
return false;
}
}
use of io.bitsquare.p2p.storage.storageentry.ProtectedStorageEntry in project bitsquare by bitsquare.
the class ProtectedDataStorageTest method testRefreshTTL.
/* //@Test
public void testRePublish() throws InterruptedException, NoSuchAlgorithmException, CertificateException, KeyStoreException, IOException, CryptoException, SignatureException, InvalidKeyException, NoSuchProviderException {
mockData.ttl = (int) (P2PDataStorage.CHECK_TTL_INTERVAL_MILLIS * 1.5);
ProtectedData data = dataStorage1.getDataWithSignedSeqNr(mockData, storageSignatureKeyPair1);
Assert.assertTrue(dataStorage1.add(data, null));
Assert.assertEquals(1, dataStorage1.getMap().size());
Thread.sleep(P2PDataStorage.CHECK_TTL_INTERVAL_MILLIS);
log.debug("test 1");
Assert.assertEquals(1, dataStorage1.getMap().size());
data = dataStorage1.getDataWithSignedSeqNr(mockData, storageSignatureKeyPair1);
Assert.assertTrue(dataStorage1.rePublish(data, null));
Thread.sleep(P2PDataStorage.CHECK_TTL_INTERVAL_MILLIS);
log.debug("test 2");
Assert.assertEquals(1, dataStorage1.getMap().size());
data = dataStorage1.getDataWithSignedSeqNr(mockData, storageSignatureKeyPair1);
Assert.assertTrue(dataStorage1.rePublish(data, null));
Thread.sleep(P2PDataStorage.CHECK_TTL_INTERVAL_MILLIS);
log.debug("test 3");
Assert.assertEquals(1, dataStorage1.getMap().size());
Thread.sleep(P2PDataStorage.CHECK_TTL_INTERVAL_MILLIS);
log.debug("test 4");
Assert.assertEquals(1, dataStorage1.getMap().size());
Thread.sleep(P2PDataStorage.CHECK_TTL_INTERVAL_MILLIS * 2);
log.debug("test 5 removed");
Assert.assertEquals(0, dataStorage1.getMap().size());
}
*/
@Test
public void testRefreshTTL() throws InterruptedException, NoSuchAlgorithmException, CertificateException, KeyStoreException, IOException, CryptoException, SignatureException, InvalidKeyException, NoSuchProviderException {
mockData.ttl = (int) (P2PDataStorage.CHECK_TTL_INTERVAL_SEC * 1.5);
ProtectedStorageEntry data = dataStorage1.getProtectedData(mockData, storageSignatureKeyPair1);
Assert.assertTrue(dataStorage1.add(data, null, null, true));
Assert.assertEquals(1, dataStorage1.getMap().size());
Thread.sleep(P2PDataStorage.CHECK_TTL_INTERVAL_SEC);
log.debug("test 1");
Assert.assertEquals(1, dataStorage1.getMap().size());
RefreshTTLMessage refreshTTLMessage = dataStorage1.getRefreshTTLMessage(mockData, storageSignatureKeyPair1);
Assert.assertTrue(dataStorage1.refreshTTL(refreshTTLMessage, null, true));
Thread.sleep(P2PDataStorage.CHECK_TTL_INTERVAL_SEC);
log.debug("test 2");
Assert.assertEquals(1, dataStorage1.getMap().size());
refreshTTLMessage = dataStorage1.getRefreshTTLMessage(mockData, storageSignatureKeyPair1);
Assert.assertTrue(dataStorage1.refreshTTL(refreshTTLMessage, null, true));
Thread.sleep(P2PDataStorage.CHECK_TTL_INTERVAL_SEC);
log.debug("test 3");
Assert.assertEquals(1, dataStorage1.getMap().size());
Thread.sleep(P2PDataStorage.CHECK_TTL_INTERVAL_SEC);
log.debug("test 4");
Assert.assertEquals(1, dataStorage1.getMap().size());
Thread.sleep(P2PDataStorage.CHECK_TTL_INTERVAL_SEC * 2);
log.debug("test 5 removed");
Assert.assertEquals(0, dataStorage1.getMap().size());
}
use of io.bitsquare.p2p.storage.storageentry.ProtectedStorageEntry in project bitsquare by bitsquare.
the class ProtectedDataStorageTest method testAddAndRemove.
//@Test
public void testAddAndRemove() throws InterruptedException, NoSuchAlgorithmException, CertificateException, KeyStoreException, IOException, CryptoException, SignatureException, InvalidKeyException, NoSuchProviderException {
ProtectedStorageEntry data = dataStorage1.getProtectedData(mockData, storageSignatureKeyPair1);
Assert.assertTrue(dataStorage1.add(data, null, null, true));
Assert.assertEquals(1, dataStorage1.getMap().size());
int newSequenceNumber = data.sequenceNumber + 1;
byte[] hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(data.getStoragePayload(), newSequenceNumber));
byte[] signature = Sig.sign(storageSignatureKeyPair1.getPrivate(), hashOfDataAndSeqNr);
ProtectedStorageEntry dataToRemove = new ProtectedStorageEntry(data.getStoragePayload(), data.ownerPubKey, newSequenceNumber, signature);
Assert.assertTrue(dataStorage1.remove(dataToRemove, null, true));
Assert.assertEquals(0, dataStorage1.getMap().size());
}
Aggregations