use of i2p.bote.packet.dht.DeletionRecord in project i2p.i2p-bote by i2p.
the class KademliaDHT method findDeleteAuthorizationKey.
@Override
public UniqueId findDeleteAuthorizationKey(Hash dhtKey, Hash verificationHash) throws InterruptedException {
final Collection<Destination> closeNodes = getClosestNodes(dhtKey);
log.info("Querying " + closeNodes.size() + " peers with DeletionQueries for Kademlia key " + dhtKey);
DhtStorageHandler storageHandler = storageHandlers.get(EncryptedEmailPacket.class);
if (storageHandler instanceof DeletionAwareDhtFolder) {
DeletionAwareDhtFolder<?> folder = (DeletionAwareDhtFolder<?>) storageHandler;
UniqueId delAuthorization = folder.getDeleteAuthorization(dhtKey);
if (delAuthorization != null)
return delAuthorization;
} else
log.error("StorageHandler for EncryptedEmailPackets is not a DeletionAwareDhtFolder!");
// Send the DeletionQueries
PacketBatch batch = new PacketBatch();
for (Destination node : closeNodes) if (// local has already been taken care of
!localDestination.equals(node))
batch.putPacket(new DeletionQuery(dhtKey), node);
sendQueue.send(batch);
batch.awaitSendCompletion();
// wait for replies
batch.awaitFirstReply(RESPONSE_TIMEOUT, TimeUnit.SECONDS);
log.info(batch.getResponses().size() + " response packets received for deletion query for hash " + dhtKey);
sendQueue.remove(batch);
Map<Destination, DataPacket> responses = batch.getResponses();
for (DataPacket response : responses.values()) if (response instanceof DeletionInfoPacket) {
DeletionInfoPacket delInfo = (DeletionInfoPacket) response;
DeletionRecord delRecord = delInfo.getEntry(dhtKey);
if (delRecord != null) {
boolean valid = Util.isDeleteAuthorizationValid(verificationHash, delRecord.delAuthorization);
if (valid)
return delRecord.delAuthorization;
}
}
return null;
}
use of i2p.bote.packet.dht.DeletionRecord in project i2p.i2p-bote by i2p.
the class IndexPacketFolderTest method testProcessDeleteRequest.
@Test
public void testProcessDeleteRequest() throws GeneralSecurityException, MalformedPacketException, PasswordException {
IndexPacketFolder folder = new IndexPacketFolder(folderDir);
// create another packet with the same destination as emailPacket1
EncryptedEmailPacket emailPacket3 = new EncryptedEmailPacket(unencryptedPacket2, destination1);
IndexPacket indexPacket = new IndexPacket(destination1);
indexPacket.put(emailPacket1);
indexPacket.put(emailPacket3);
folder.store(indexPacket);
assertEquals("Folder should have exactly one element!", 1, folder.getElements().size());
// add two entries and delete them via delete requests
Hash dest1Hash = destination1.getHash();
IndexPacketDeleteRequest delRequest = new IndexPacketDeleteRequest(dest1Hash);
Hash dhtKey1 = emailPacket1.getDhtKey();
Hash dhtKey3 = emailPacket3.getDhtKey();
delRequest.put(dhtKey1, unencryptedPacket1.getDeleteAuthorization());
delRequest.put(dhtKey3, unencryptedPacket2.getDeleteAuthorization());
folder.process(delRequest);
DhtStorablePacket storedPacket = folder.retrieve(dest1Hash);
assertTrue(storedPacket instanceof IndexPacket);
assertEquals("The index packet should have no entries!", 0, ((IndexPacket) storedPacket).getNumEntries());
// verify that there is one deletion file containing two entries
File[] files = folder.getStorageDirectory().listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.startsWith("DEL_");
}
});
assertEquals(1, files.length);
DataPacket dataPacket = DataPacket.createPacket(files[0]);
assertTrue(dataPacket instanceof DeletionInfoPacket);
DeletionInfoPacket delInfoPacket = (DeletionInfoPacket) dataPacket;
Iterator<DeletionRecord> delPacketIterator = delInfoPacket.iterator();
assertTrue("DeletionInfoPacket has no elements!", delPacketIterator.hasNext());
delPacketIterator.next();
assertTrue("DeletionInfoPacket has less than one element!", delPacketIterator.hasNext());
delPacketIterator.next();
assertFalse("DeletionInfoPacket has more than two elements!", delPacketIterator.hasNext());
// verify that the two deletion records match the DHT keys and auth keys of the deleted packets
DeleteRequest newDelRequest = folder.storeAndCreateDeleteRequest(indexPacket);
assertTrue(newDelRequest instanceof IndexPacketDeleteRequest);
IndexPacketDeleteRequest newIndexPacketDelRequest = (IndexPacketDeleteRequest) newDelRequest;
assertEquals(newIndexPacketDelRequest.getDeleteAuthorization(dhtKey1), unencryptedPacket1.getDeleteAuthorization());
assertEquals(newIndexPacketDelRequest.getDeleteAuthorization(dhtKey3), unencryptedPacket2.getDeleteAuthorization());
}
use of i2p.bote.packet.dht.DeletionRecord in project i2p.i2p-bote by i2p.
the class IndexPacketFolder method storeAndCreateDeleteRequest.
@Override
public DeleteRequest storeAndCreateDeleteRequest(DhtStorablePacket packetToStore) {
if (!(packetToStore instanceof IndexPacket))
throw new IllegalArgumentException("Invalid packet type: " + packetToStore.getClass().getSimpleName() + "; this folder only stores packets of type " + IndexPacket.class.getSimpleName() + ".");
IndexPacket indexPacketToStore = (IndexPacket) packetToStore;
// set the time stamps before merging so existing ones don't change
setTimeStamps(indexPacketToStore);
// use super.retrieve() because we don't want to delete time stamps here
DhtStorablePacket existingPacket = super.retrieve(packetToStore.getDhtKey());
// make a Delete Request that contains any known-to-be-deleted DHT keys from the store request
String delFileName = getDeletionFileName(packetToStore.getDhtKey());
DeletionInfoPacket delInfo = createDelInfoPacket(delFileName);
IndexPacketDeleteRequest delRequest = null;
if (delInfo != null) {
delRequest = null;
for (IndexPacketEntry entry : indexPacketToStore) {
DeletionRecord delRecord = delInfo.getEntry(entry.emailPacketKey);
if (delRecord != null) {
// leave delRequest null until a DeletionRecord is found
if (delRequest == null)
delRequest = new IndexPacketDeleteRequest(indexPacketToStore.getDhtKey());
delRequest.put(delRecord.dhtKey, delRecord.delAuthorization);
}
}
}
// remove deleted entries from indexPacketToStore so they are not re-stored
if (delInfo != null && !delInfo.isEmpty())
for (DeletionRecord delRecord : delInfo) indexPacketToStore.remove(delRecord.dhtKey);
// The resulting packet may be too big for a datagram but we don't split it until a peer asks for it.
if (existingPacket instanceof IndexPacket)
indexPacketToStore = new IndexPacket(indexPacketToStore, (IndexPacket) existingPacket);
else if (existingPacket != null)
log.error("Packet of type " + existingPacket.getClass().getSimpleName() + " found in IndexPacketFolder.");
// don't merge, but overwrite
super.store(indexPacketToStore);
return delRequest;
}
use of i2p.bote.packet.dht.DeletionRecord in project i2p.i2p-bote by i2p.
the class EmailPacketFolder method getDeleteAuthorization.
@Override
public UniqueId getDeleteAuthorization(Hash dhtKey) {
String delFileName = getDeletionFileName(dhtKey);
DeletionInfoPacket delInfo = createDelInfoPacket(delFileName);
if (delInfo != null) {
DeletionRecord delRecord = delInfo.getEntry(dhtKey);
if (delRecord != null)
return delRecord.delAuthorization;
}
return null;
}
use of i2p.bote.packet.dht.DeletionRecord in project i2p.i2p-bote by i2p.
the class EmailPacketFolder method storeAndCreateDeleteRequest.
@Override
public DeleteRequest storeAndCreateDeleteRequest(DhtStorablePacket packetToStore) {
if (!(packetToStore instanceof EncryptedEmailPacket))
throw new IllegalArgumentException("Invalid packet type: " + packetToStore.getClass().getSimpleName() + "; this folder only stores packets of type " + EncryptedEmailPacket.class.getSimpleName() + ".");
DeleteRequest delRequest = null;
// read the deletion info file for the email packet's DHT key
Hash dhtKey = packetToStore.getDhtKey();
String delFileName = getDeletionFileName(dhtKey);
DeletionInfoPacket delInfo = createDelInfoPacket(delFileName);
if (delInfo != null) {
DeletionRecord delRecord = delInfo.getEntry(dhtKey);
if (delRecord != null)
delRequest = new EmailPacketDeleteRequest(delRecord.dhtKey, delRecord.delAuthorization);
} else
// if the DHT key has not been recorded as deleted, store the email packet
store(packetToStore);
return delRequest;
}
Aggregations