Last active
April 18, 2022 22:57
-
-
Save rcmorano/11d47a46557e783bc1d7ff68961fae4f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# apt install -y netcat-traditional mtr jq curl | |
function find-optimal-peers-get-relay-hops() { | |
# This will stop counting hops on 5th imcp-unreachable hop | |
# or on finished, successful traceroute | |
mtr --json -c 1 -n -Z 1 -G 1 -U 5 ${1} | jq -r .report.hubs[].count | sort -n | tail -n1 | awk '{print $1}' | |
} | |
function find-optimal-peers-get-relay-avg-latency() { | |
# Let's assume we're going to get as bad latency as the average of the | |
# slowest of 3 probes from the hub | |
mtr --json -c 3 -n -Z 1 -G 1 -U 5 ${1} | jq -r .report.hubs[].Avg | sort -n | tail -n1 | |
} | |
function find-optimal-peers-by-hops() { | |
find-optimal-peers hops | |
} | |
function find-optimal-peers-by-avg-latency() { | |
find-optimal-peers avg-latency | |
} | |
function find-optimal-peers-check-depends() { | |
# TODO: | |
# apt install -y netcat-traditional mtr jq curl | |
true | |
} | |
function find-optimal-peers-init() { | |
MAX_PEERS=10 | |
PROBE_CONN_TIMEOUT=1 | |
SOURCE_ROUTE_POINTER=4 | |
TOPOLOGY_JSON_URL=https://explorer.mainnet.cardano.org/relays/topology.json | |
TOPOLOGY_JSON_FILE=/tmp/topology.json | |
PROXIMITY_TOPOLOGY_JSON=/tmp/proximity-topology.json | |
} | |
function find-optimal-peers() { | |
find-optimal-peers-init | |
export METRIC=${1:-avg-latency} | |
curl -sLo ${TOPOLOGY_JSON_FILE} ${TOPOLOGY_JSON_URL} | |
echo '{"Producers":[]}' > ${TOPOLOGY_JSON_FILE}.tmp | |
jq -r '.Producers[] | "\(.addr):\(.port)"' ${TOPOLOGY_JSON_FILE} | while read line | |
do | |
ADDR=$(echo ${line} | awk -F: '{print $1}') | |
PORT=$(echo ${line} | awk -F: '{print $2}') | |
nc -G ${SOURCE_ROUTE_POINTER} -w ${PROBE_CONN_TIMEOUT} -zv ${ADDR} ${PORT} &> /dev/null | |
# if connected, extract valency from the peers file | |
if [ $? -eq 0 ] | |
then | |
VALENCY=$(jq -r \ | |
--arg addr ${ADDR} \ | |
--arg port ${PORT} \ | |
'.Producers[] | select((.addr==($addr | tostring)) and (.port==($port | tonumber))) | .valency' 2> /dev/null \ | |
${TOPOLOGY_JSON_FILE}) | |
if [[ "${VALENCY}" == "null" ]] || [[ "${VALENCY}" == "" ]] | |
then | |
VALENCY=1 | |
fi | |
jq -r \ | |
--arg addr ${ADDR} \ | |
--arg port ${PORT} \ | |
--arg valency ${VALENCY} \ | |
'.Producers += [{"addr":$addr,"port":($port|tonumber),"valency":($valency|tonumber)}]' \ | |
${TOPOLOGY_JSON_FILE}.tmp > ${TOPOLOGY_JSON_FILE} | |
mv ${TOPOLOGY_JSON_FILE} ${TOPOLOGY_JSON_FILE}.tmp | |
fi | |
done | |
cp -a ${TOPOLOGY_JSON_FILE}.tmp ${TOPOLOGY_JSON_FILE} | |
jq -r .Producers[].addr ${TOPOLOGY_JSON_FILE} | sort | uniq | \ | |
while read relay | |
do | |
VALUE=$(find-optimal-peers-get-relay-${METRIC} ${relay}) | |
echo "${VALUE} - ${relay}" | |
done | sort -n -k1 > /tmp/find-optimal-peers-${METRIC}.log | |
echo '{"Producers":[]}' > ${PROXIMITY_TOPOLOGY_JSON}.tmp | |
head -n ${MAX_PEERS} /tmp/find-optimal-peers-${METRIC}.log | awk '{print $3}' | while read peer | |
do | |
PEER_OBJECTS=$(jq -r \ | |
--arg addr ${peer} \ | |
'.Producers[] | select(.addr==($addr | tostring))' \ | |
${TOPOLOGY_JSON_FILE} | \ | |
jq -r --slurp . | \ | |
awk '{printf "%s",$0} END {print ""}') | |
jq -r \ | |
--argjson peer "${PEER_OBJECTS}" \ | |
'.Producers += $peer' \ | |
${PROXIMITY_TOPOLOGY_JSON}.tmp > ${PROXIMITY_TOPOLOGY_JSON} | |
mv ${PROXIMITY_TOPOLOGY_JSON} ${PROXIMITY_TOPOLOGY_JSON}.tmp | |
done | |
jq -r . ${PROXIMITY_TOPOLOGY_JSON}.tmp > ${PROXIMITY_TOPOLOGY_JSON} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment