Skip to content

Instantly share code, notes, and snippets.

@petersjt014
Last active March 30, 2018 05:24
Show Gist options
  • Save petersjt014/57debf01da5064f6120616ac983139d0 to your computer and use it in GitHub Desktop.
Save petersjt014/57debf01da5064f6120616ac983139d0 to your computer and use it in GitHub Desktop.
for finding availibility of ipfs dags
#!/bin/sh
# After taking in an IPFS multihash, this script calculates an 'availability' metric by:
# Traversing the hash's dag to find leaves,
# summing the number of providers per leaf (each rounded down to 10 for performance reasons), and
# returning providers/leaves*10.
# since `ipfs pin add` can be run non-recursively, a peer that advertises a given value
# may not actually posess the entire dag, which can be inconvenient.
# This script will return a value between 0-1 indicating how much of the dag can actually be found on the network.
# This was written as a learning experience: It probably works correctly, but definitely not optimally--I don't recommend doing multi-GB hashes.
# I can't put this file's multihash inside itself (that'd be a neat trick), but a mostly commentless version is at:
# QmQr8isLeokaYsXvgvyvFstexkqeiVAiMXuevN6sj9nSJu
# This version (and its hash) can also be found here, where new versions (this is v2018.03.30) may occasionally be posted:
# https://gist.github.com/petersjt014/57debf01da5064f6120616ac983139d0
# If you want to refactor or port it to another language to make it less of a horrorshow, feel free to post a comment there.
# small test hash: QmRCJXG7HSmprrYwDrK1GctXHgbV7EYpVcJPQPwevoQuqF
# SVG diagram of the test hash: Qma8EX27uA27ASxseUuvnLPp8nUHtSDqJJZwCnU4D3uZHn
# more info on graphmd, the tool used to make the diagram:
# https://gateway.ipfs.io/ipfs/QmSrCRJmzE4zE1nAfWPbzVfanKQNBhp7ZWmMnEdbiLvYNh/mdown#/ipfs/QmRFTtbyEp3UaT67ByYW299Suw7HKKnWK6NJMdNFzDjYdX/graphmd/README.md
echo; echo "starting..."
peers=0; leafcount=0
recur(){
echo "recursing"
local refs=$(ipfs refs $1)
local nrefs=$(echo $refs | wc -w);
echo "--$1--"
if [ $nrefs -eq 0 ]
then
echo; echo "has no refs (leaf node)"
((peers+=$(ipfs dht findprovs -n 10 $1 | wc -l)))
((leafcount+=1))
else
echo; echo "has $nrefs refs: $refs"
for x in $refs; do
recur $x
done
fi
}
export -f recur
recur $1
echo "result: $((peers/leafcount*10))"
@petersjt014
Copy link
Author

hash of current version:

QmWzedmwrcDdoAHWLU32iyrVdbVQjVyekoBxrFJckUcVaV

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment