Advertisement
Guest User

testnet

a guest
Dec 4th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.28 KB | None | 0 0
  1. ## testnet.sh
  2. ## Tested with jq 1.5.1 on Ubuntu 16.04.1
  3. ##
  4. #!/bin/bash
  5.  
  6. ##SECRET="\"YOUR PASSPHRASE\"" ## Uncomment this line if you want this script to reenable forging when done
  7. SRV=127.0.0.1:7000
  8.  
  9. ## Thanks to cc001 and hagie for improvements here
  10. find_newest_snap_rebuild(){
  11.  
  12.     SNAPSHOTS=(
  13.       https://downloads.lisk.io/lisk/test/blockchain.db.gz  ## Official
  14.       https://testnet-snapshot.liskwallet.net/blockchain.db.gz      ## isabella
  15.       https://testnet-snapshot.lisknode.io/blockchain.db.gz         ## Gr33nDrag0n
  16.     )
  17.    
  18.     BESTSNAP=""
  19.     BESTTIMESTAMP=0
  20.     BESTSNAPLENGTH=0
  21.  
  22.     for SNAP in ${SNAPSHOTS[@]}
  23.     do
  24.       echo "$SNAP"
  25.       SNAPSTATUS=$(curl -sI "$SNAP" | grep "HTTP" | cut -f2 -d" ")
  26.       SNAPLENGTH=$(curl -sI "$SNAP" | grep "Length" | cut -f2 -d" ")
  27.       SNAPLENGTH="${SNAPLENGTH//[$'\t\r\n ']}"
  28.       if [ "$SNAPSTATUS" -eq "200" ]
  29.       then
  30.           TIME=$(curl -sI "$SNAP" | grep Last-Modified | cut -f2 -d:)
  31.           TIMESTAMP=$(date -d "$TIME" +"%s")
  32.           echo $TIMESTAMP
  33.           if [ "$TIMESTAMP" -gt "$BESTTIMESTAMP" ] && [ "$SNAPLENGTH" -gt "$BESTSNAPLENGTH" ]; ## Make sure it is the newest and the largest
  34.           then
  35.              BESTSNAP=$SNAP
  36.              BESTTIMESTAMP=$TIMESTAMP
  37.              BESTSNAPLENGTH=$SNAPLENGTH
  38.           fi
  39.        fi
  40.     done
  41.    
  42.     REPO=${BESTSNAP%/blockchain.db.gz}
  43.     echo "Newest snap: $BESTSNAP | Rebuilding from $REPO"
  44.  
  45.     ## bash lisk.sh stop ## Trying to figure out why rebuilding from block 0.  Attempting to stop first to make sure the DB shuts down too
  46.     ## sleep 5
  47.     bash lisk.sh rebuild -u $REPO
  48. }
  49.  
  50. top_height(){
  51.     ## Get height of your 100 peers and save the highest value
  52.     ## Thanks to wannabe_RoteBaron for this improvement
  53.     HEIGHT=$(curl -s http://$SRV/api/peers | jq '.peers[].height' | sort -nu | tail -n1)
  54.     ## Make sure height is not empty, if it is empty try the call until it is not empty
  55.     while [ -z "$HEIGHT" ]
  56.     do
  57.         sleep 1
  58.         HEIGHT=$(curl -s http://$SRV/api/peers | jq '.peers[].height' | sort -nu | tail -n1)
  59.     done
  60. }
  61.  
  62. ## Get height of this server and see if it's greater or within 4 of the highest
  63. local_height() {
  64.     ## Make sure local height is not empty, if it is empty try the call until it is not empty
  65.     CHECKSRV=`curl -s "http://$SRV/api/loader/status/sync"| jq '.height'`
  66.     while [ -z "$CHECKSRV" ]
  67.     do
  68.         sleep 1
  69.         CHECKSRV=`curl -s "http://$SRV/api/loader/status/sync"| jq '.height'`
  70.     done
  71.     diff=$(( $HEIGHT - $CHECKSRV ))
  72.     if [ "$diff" -gt "4" ]
  73.     then
  74.         echo "Reloading! Local: $CHECKSRV, Highest: $HEIGHT, Diff: $diff"
  75.         cd ~/lisk-test/
  76.         bash lisk.sh reload
  77.         sleep 60
  78.        
  79.         ## Make sure local height is not empty, if it is empty try the call until it is not empty
  80.         CHECKSRV=`curl -s "http://$SRV/api/loader/status/sync"| jq '.height'`
  81.         while [ -z "$CHECKSRV" ]
  82.         do
  83.             sleep 1
  84.             CHECKSRV=`curl -s "http://$SRV/api/loader/status/sync"| jq '.height'`
  85.         done
  86.        
  87.         ## Rebuild if still out of sync after reload
  88.         diff=$(( $HEIGHT - $CHECKSRV ))
  89.         if [ "$diff" -gt "6" ]
  90.         then
  91.             echo "Rebuilding! Local: $CHECKSRV, Highest: $HEIGHT, Diff: $diff"
  92.             find_newest_snap_rebuild
  93.             #sleep 420
  94.             ## Thank you corsaro for this improvement
  95.             while true; do
  96.                 s1=`curl -k -s "http://$SRV/api/loader/status/sync"| jq '.height'`
  97.                 sleep 60
  98.                 s2=`curl -k -s "http://$SRV/api/loader/status/sync"| jq '.height'`
  99.  
  100.                 diff=$(( $s2 - $s1 ))
  101.                 if [ "$diff" -gt "10" ];
  102.                 then
  103.                     echo "$s2" "is a lot greater then " "$s1"
  104.                     echo "It looks like rebuild has not finished yet. Waiting longer to continue"
  105.                 else
  106.                     echo "" "$s1" " " "$s2"
  107.                     echo "Looks like rebuilding finished. We can stop this"
  108.                     ##curl --connect-timeout 3 -k -H "Content-Type: application/json" -X POST -d '{"secret":'"$SECRET"'}' http://"$SRV"/api/delegates/forging/enable ## Uncomment this line if you want this script to reenable forging when done
  109.                     break
  110.                 fi
  111.             done
  112.         ##else ## Uncomment this line if you want this script to reenable forging when done
  113.             ##curl --connect-timeout 3 -k -H "Content-Type: application/json" -X POST -d '{"secret":'"$SECRET"'}' http://"$SRV"/api/delegates/forging/enable ## Uncomment this line if you want this script to reenable forging when done
  114.         fi
  115.     fi
  116. }
  117.  
  118. while true; do
  119.     ## Check that lisk is running first!!
  120.     top_height
  121.     local_height
  122.  
  123.     echo "Local: $CHECKSRV, Highest: $HEIGHT, Diff: $diff"
  124.     sleep 10
  125. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement