Once we receive a file from a peer, add the mapping to the lookup table.

This allows other peers to find out where they can obtain these files if we were to stop hosting them later. Or even if we continue hosting copies, it still informs the network on other locations, for better decentralization.
This commit is contained in:
CalDescent 2021-11-13 12:50:26 +00:00
parent f5235938b7
commit 53466797a5

View File

@ -586,13 +586,21 @@ public class ArbitraryDataManager extends Thread {
Triple<String, Peer, Long> newEntry = new Triple<>(null, null, request.getC()); Triple<String, Peer, Long> newEntry = new Triple<>(null, null, request.getC());
arbitraryDataFileListRequests.put(message.getId(), newEntry); arbitraryDataFileListRequests.put(message.getId(), newEntry);
boolean receivedAtLeastOneFile = false;
// Now fetch actual data from this peer // Now fetch actual data from this peer
for (byte[] hash : hashes) { for (byte[] hash : hashes) {
if (!arbitraryDataFile.chunkExists(hash)) { if (!arbitraryDataFile.chunkExists(hash)) {
// Only request the file if we aren't already requesting it from someone else // Only request the file if we aren't already requesting it from someone else
if (!arbitraryDataFileRequests.containsKey(Base58.encode(hash))) { if (!arbitraryDataFileRequests.containsKey(Base58.encode(hash))) {
ArbitraryDataFile receivedArbitraryDataFile = fetchArbitraryDataFile(peer, hash); ArbitraryDataFile receivedArbitraryDataFile = fetchArbitraryDataFile(peer, hash);
LOGGER.info("Received data file {} from peer {}", receivedArbitraryDataFile, peer); if (receivedArbitraryDataFile != null) {
LOGGER.info("Received data file {} from peer {}", receivedArbitraryDataFile, peer);
receivedAtLeastOneFile = true;
}
else {
LOGGER.info("Peer {} didn't respond with data file {}", peer, hash);
}
} }
else { else {
LOGGER.info("Already requesting data file {}", arbitraryDataFile); LOGGER.info("Already requesting data file {}", arbitraryDataFile);
@ -600,6 +608,15 @@ public class ArbitraryDataManager extends Thread {
} }
} }
if (receivedAtLeastOneFile) {
// Update our lookup table to indicate that this peer holds data for this signature
String peerAddress = peer.getPeerData().getAddress().toString();
LOGGER.info("Adding arbitrary peer: {} for signature {}", peerAddress, Base58.encode(signature));
ArbitraryPeerData arbitraryPeerData = new ArbitraryPeerData(signature, peer);
repository.getArbitraryRepository().save(arbitraryPeerData);
repository.saveChanges();
}
// Check if we have all the chunks for this transaction // Check if we have all the chunks for this transaction
if (arbitraryDataFile.exists() || arbitraryDataFile.allChunksExist(arbitraryTransactionData.getChunkHashes())) { if (arbitraryDataFile.exists() || arbitraryDataFile.allChunksExist(arbitraryTransactionData.getChunkHashes())) {