|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +WITNET_BINARY=${1:-./witnet} |
| 4 | +WITNET_CONFIG_FILE=${2:-./witnet.toml} |
| 5 | + |
| 6 | +RECOVERY_BANNER=" |
| 7 | +
|
| 8 | +███████╗ ██████╗ ██████╗ ██╗ ██╗ ██╗ ██╗ ██╗ |
| 9 | +██╔════╝██╔═══██╗██╔══██╗██║ ██╔╝ ██║ ██║ ██║ |
| 10 | +█████╗ ██║ ██║██████╔╝█████╔╝ ██║ ██║ ██║ |
| 11 | +██╔══╝ ██║ ██║██╔══██╗██╔═██╗ ██║ ██║ ██║ |
| 12 | +██║ ╚██████╔╝██║ ██║██║ ██╗ ██║ ██║ ██║ |
| 13 | +╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚████████╔╝ |
| 14 | +██████╗ ███████╗ ██████╗ ██████╗ ╚█████╔╝ |
| 15 | +██╔══██╗██╔════╝██╔════╝██╔═══██╗ ╚██╔╝ |
| 16 | +██████╔╝█████╗ ██║ ██║ ██║ ██║ |
| 17 | +██╔══██╗██╔══╝ ██║ ██║ ██║ ██║ |
| 18 | +██║ ██║███████╗╚██████╗╚██████╔╝ ██║ |
| 19 | +╚═╝ ╚═╝╚══════╝ ╚═════╝ ╚═════╝ ██║ |
| 20 | +██╗ ██╗███████╗██████╗ ██╗ ██╗ ██║ |
| 21 | +██║ ██║██╔════╝██╔══██╗╚██╗ ██╔╝ ██║ |
| 22 | +██║ ██║█████╗ ██████╔╝ ╚████╔╝ ██║ |
| 23 | +╚██╗ ██╔╝██╔══╝ ██╔══██╗ ╚██╔╝ ██║ |
| 24 | + ╚████╔╝ ███████╗██║ ██║ ██║ ██║ |
| 25 | + ╚═══╝ ╚══════╝╚═╝ ╚═╝ ╚═╝ ╚═╝ |
| 26 | +
|
| 27 | +╔══════════════════════════════════════════════════════════╗ |
| 28 | +║ LOCAL CHAIN IS FORKED. PROCEEDING TO AUTOMATIC RECOVERY. ║ |
| 29 | +╠══════════════════════════════════════════════════════════╣ |
| 30 | +║ This process will sanitize the local chain state by ║ |
| 31 | +║ rewinding back to the point where the fork took place, ║ |
| 32 | +║ and then continue synchronizing and operating as usual. ║ |
| 33 | +╟──────────────────────────────────────────────────────────╢ |
| 34 | +║ This will take from 30 to 60 minutes depending on your ║ |
| 35 | +║ network, CPU, RAM and hard disk speeds. ║ |
| 36 | +╟──────────────────────────────────────────────────────────╢ |
| 37 | +║ Learn more about why this recovery is needed: ║ |
| 38 | +║ https://github.com/witnet/WIPs/blob/master/20211008.md ║ |
| 39 | +╟──────────────────────────────────────────────────────────╢ |
| 40 | +║ Feel free to ask any questions on: ║ |
| 41 | +║ Discord: https://discord.gg/X4uurfP ║ |
| 42 | +║ Telegram: https://t.me/witnetio ║ |
| 43 | +╚══════════════════════════════════════════════════════════╝ |
| 44 | +" |
| 45 | + |
| 46 | +KNOWN_PEERS='\n "45.43.29.114:21337",\n "45.43.30.194:21337",\n "45.154.212.50:21337",\n "51.91.8.100:21301",\n "51.91.11.234:21338",\n "51.91.11.234:21339",\n "51.91.11.234:21340",\n "51.255.51.101:21337",\n "51.255.51.101:21338",\n "51.255.51.101:21339",\n "52.166.178.145:21337",\n "52.166.178.145:22337",\n "78.20.135.129:21337",\n "82.213.207.211:21337",\n "104.218.233.82:21337",\n "104.218.233.114:21337",\n "136.243.19.123:22360",\n "157.90.131.55:21337",\n "159.69.74.123:22344",\n "161.97.112.213:21337",\n "173.249.3.178:21337",\n "173.249.3.178:22337",\n "173.249.3.178:41337",\n "173.249.8.65:20337",\n "173.249.8.65:21337",\n "173.249.8.65:41337",\n' |
| 47 | + |
| 48 | +# Just a pretty logging helper |
| 49 | +function log { |
| 50 | + echo "[20211008_RECOVERY] $1" |
| 51 | +} |
| 52 | + |
| 53 | +# A helper for calculating ETAs |
| 54 | +function eta { |
| 55 | + START=$1 |
| 56 | + NOW=$2 |
| 57 | + PROGRESS=$3 |
| 58 | + if [ "$PROGRESS" == "00" ]; then |
| 59 | + echo "will be shown as synchronization moves forward..." |
| 60 | + else |
| 61 | + ELAPSED=$(( NOW - START )) |
| 62 | + SPEED=$((PROGRESS * 1000 / ELAPSED)) |
| 63 | + if [ "$SPEED" == "0" ]; then |
| 64 | + SPEED=1 |
| 65 | + fi |
| 66 | + REMAINING_PROGRESS=$(( 10000 - PROGRESS )) |
| 67 | + REMAINING_TIME=$((REMAINING_PROGRESS * 1000 / SPEED + 30 )) |
| 68 | + echo $(( REMAINING_TIME / 60 )) minutes $((REMAINING_TIME % 60)) seconds aprox. |
| 69 | + fi |
| 70 | +} |
| 71 | + |
| 72 | +# This script can be skipped by setting environment variable SKIP_20211008_RECOVERY to "true" |
| 73 | +if [[ "$SKIP_WIP20211008_RECOVERY" == "true" ]]; then |
| 74 | + log "Skipping 20211008 recovery" |
| 75 | + exit 0 |
| 76 | +fi |
| 77 | + |
| 78 | +# Make sure the arguments make sense |
| 79 | +if ! command -v "$WITNET_BINARY" &> /dev/null; then |
| 80 | + log "ERROR: The provided witnet binary (first argument to this script) is not a valid executable file: $WITNET_BINARY" |
| 81 | + exit 1 |
| 82 | +fi |
| 83 | +if [ ! -f "$WITNET_CONFIG_FILE" ]; then |
| 84 | + log "ERROR: The provided witnet configuration file (second argument to this script) is not a valid configuration file: $WITNET_CONFIG_FILE" |
| 85 | + exit 2 |
| 86 | +fi |
| 87 | + |
| 88 | +# Read configuration (e.g. node server address) from config file |
| 89 | +log "Using configuration file at $WITNET_CONFIG_FILE" |
| 90 | +HOST=$(grep "server_addr" "$WITNET_CONFIG_FILE" | sed -En "s/server_addr = \"(.*)\"/\1/p" | sed -E "s/0\.0\.0.\0/127.0.0.1/g" ) |
| 91 | +ADDRESS=$(echo "$HOST" | cut -d':' -f1) |
| 92 | +PORT=$(echo "$HOST" | cut -d':' -f2) |
| 93 | + |
| 94 | +# Check connection to local witnet node |
| 95 | +TIME_TO_NEXT_RETRY=5 |
| 96 | +log "Checking connection to local witnet node at $HOST" |
| 97 | +while true |
| 98 | + if nc -zv "$ADDRESS" "$PORT" &>/dev/null; then |
| 99 | + log "Successful connection to local witnet node at $HOST" |
| 100 | + break |
| 101 | + else |
| 102 | + log "ERROR: Failed to connect to local witnet node at $HOST" |
| 103 | + log "Retrying in $TIME_TO_NEXT_RETRY seconds" |
| 104 | + sleep "$TIME_TO_NEXT_RETRY" |
| 105 | + TIME_TO_NEXT_RETRY=$(( 2 * TIME_TO_NEXT_RETRY )) |
| 106 | + fi |
| 107 | +do true; done |
| 108 | + |
| 109 | +# Check whether the local witnet node is below 20211008 "common checkpoint" (#441649) |
| 110 | +if ! $WITNET_BINARY node blockchain --epoch 683499 --limit 1 2>&1 | grep -q "block for epoch"; then |
| 111 | + log "The local witnet node at $HOST seems to be syncing blocks prior to the 20211008 fork. No recovery action is needed" |
| 112 | + exit 0 |
| 113 | +fi |
| 114 | + |
| 115 | +# Check whether the local witnet node is on the `A` chain, and if so, skip recovery |
| 116 | +if $WITNET_BINARY node blockchain --epoch 689769 --limit 2 2>&1 | grep -q '#689769 had digest 90b64edb'; then |
| 117 | + log "The local witnet node at $HOST seems to be on the leading chain. No recovery action is needed" |
| 118 | + exit 0 |
| 119 | +fi |
| 120 | + |
| 121 | +# There is no way back, recovery is needed |
| 122 | +echo "$RECOVERY_BANNER" |
| 123 | + |
| 124 | +# Update known peers in configuration file |
| 125 | +log "Updating known_peers in configuration file at $WITNET_CONFIG_FILE" |
| 126 | +sed -ziE "s/known_peers\s*=\s*\[\n.*\\,\n\]/known_peers = [$KNOWN_PEERS]/g" "$WITNET_CONFIG_FILE" && |
| 127 | +log "Successfully updated known_peers in configuration file" || |
| 128 | +log "ERROR: Failed to update known_peers in configuration file at $WITNET_CONFIG_FILE" |
| 129 | + |
| 130 | +# Rewind local chain back to the 20211008 "common checkpoint" (#683499) |
| 131 | +log "Triggering rewind of local block chain back to epoch #683499" |
| 132 | +$WITNET_BINARY node rewind --epoch 683499 &>/dev/null |
| 133 | +REWIND_START=$(date +%s) |
| 134 | + |
| 135 | +# Flush existing peers and inject new peers in runtime |
| 136 | +$WITNET_BINARY node clearPeers 2>&1 | grep -q "Successful" && |
| 137 | +log "Successfully cleared existing peers from buckets" || |
| 138 | +log "ERROR: Failed to clear existing peers from buckets" |
| 139 | +echo "$KNOWN_PEERS" | sed -r "s/\\\n\s*\"([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\:[0-9]+)\"\,/\1, /g" | "$WITNET_BINARY" node addPeers 2>&1 | grep -q "Successful" && |
| 140 | +log "Successfully added healthy peers" || |
| 141 | +log "ERROR: Failed to add new list of helthy peers" |
| 142 | + |
| 143 | +# Wait for the rewind to complete, showing progress and ETA |
| 144 | +while true |
| 145 | + STATS=$($WITNET_BINARY node nodeStats 2>&1) |
| 146 | + if echo "$STATS" | grep -q "synchronized"; then |
| 147 | + log "Successfully finished rewinding and synchronizing!" |
| 148 | + break |
| 149 | + else |
| 150 | + NOW=$(date +%s) |
| 151 | + PERCENTAGE=$(echo "$STATS" | sed -En "s/.*\:\s*(.*)\.(.*)\%.*/\1.\2%/p") |
| 152 | + PERCENTAGE_RAW=$(echo "$PERCENTAGE" | sed -En "s/0*(.*)\.(.*)\%/\1\2/p") |
| 153 | + log "Still rewinding and synchronizing. Progress: $PERCENTAGE. ETA: $(eta "$REWIND_START" "$NOW" "$PERCENTAGE_RAW")" |
| 154 | + sleep 30 |
| 155 | + fi |
| 156 | +do true; done |
0 commit comments