Skip to content

Commit

Permalink
Merge pull request Peergos#546 from Peergos/fix/pin-the-pki
Browse files Browse the repository at this point in the history
Make the pki node pin the pki in ipfs!!!
  • Loading branch information
ianopolous authored Aug 23, 2019
2 parents ba0fe21 + 088123a commit 2c9d478
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/peergos/server/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ public static void startPeergos(Args a) {
// build a mirroring proxying corenode, unless we are the pki node
boolean isPkiNode = nodeId.equals(pkiServerNodeId);
CoreNode core = isPkiNode ?
buildPkiCorenode(localPointers, localDht, a) :
buildPkiCorenode(new PinningMutablePointers(localPointers, localDht), localDht, a) :
new MirrorCoreNode(new HTTPCoreNode(ipfsGateway, pkiServerNodeId), proxingMutable, localDht,
peergosId, a.fromPeergosDir("pki-mirror-state-path","pki-state.cbor"));

Expand Down
67 changes: 67 additions & 0 deletions src/peergos/server/PinChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package peergos.server;

import peergos.server.storage.*;
import peergos.shared.*;
import peergos.shared.corenode.*;
import peergos.shared.crypto.hash.*;
import peergos.shared.io.ipfs.multihash.*;
import peergos.shared.user.*;

import java.net.*;
import java.util.*;
import java.util.stream.*;

/** This is utility to check that all the things we think should be pinned on the current node actually are.
*
*/
public class PinChecker {

public static void main(String[] args) throws Exception {
boolean fix = args.length > 0 && args[0].equals("-fix");
Crypto.initJava();

IPFS ipfs = new IPFS("localhost", 5001);
Set<Multihash> allPins = ipfs.pin.ls(IPFS.PinType.recursive).keySet();

NetworkAccess network = NetworkAccess.buildJava(new URL("http://localhost:8000")).get();
List<String> usernames = network.coreNode.getUsernames("").join();
Multihash id = network.dhtClient.id().join();
for (String username : usernames) {
List<UserPublicKeyLink> chain = network.coreNode.getChain(username).join();
if (chain.isEmpty()) {
System.out.println("Ignoring user with empty chain: " + username);
continue;
}
UserPublicKeyLink current = chain.get(chain.size() - 1);
Multihash storageNode = current.claim.storageProviders.get(0);
if (! id.equals(storageNode)) {
System.out.println("Ignoring user from different server " + username);
continue;
}

PublicKeyHash owner = current.owner;
try {
Set<PublicKeyHash> allWriters = WriterData.getOwnedKeysRecursive(owner, owner, network.mutable, network.dhtClient).join();
Set<Multihash> allRoots = allWriters.stream()
.map(w -> network.mutable.getPointerTarget(owner, w, network.dhtClient).join())
.filter(m -> m.isPresent())
.map(m -> m.get())
.collect(Collectors.toSet());

HashSet<Multihash> missing = new HashSet<>(allRoots);
missing.removeAll(allPins);
if (!missing.isEmpty())
System.out.println("Missing " + missing.size() + " pins for " + username + " - " + missing);
if (fix) {
System.out.println("Adding missing pins...");
for (Multihash h : missing) {
ipfs.pin.add(h);
}
}
} catch (Throwable t) {
System.err.println("Error handling user: " + username);
t.printStackTrace();
}
}
}
}

0 comments on commit 2c9d478

Please sign in to comment.