update-guardian-set.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env bash
  2. # This script submits a guardian set update using the VAA injection admin command.
  3. # First argument is the number of guardians for the new guardian set.
  4. set -e
  5. # wait for the guardians to establish networking
  6. sleep 20
  7. newNumGuardians=$1
  8. echo "new number of guardians: ${newNumGuardians}"
  9. webHost=$2
  10. echo "webHost ${webHost}"
  11. namespace=$3
  12. echo "namespace ${namespace}"
  13. # file & path to save governance VAA
  14. tmpFile=$(mktemp -q)
  15. if [ $? -ne 0 ]; then
  16. echo "$0: Can't create temp file, bye.." 1>&2;
  17. exit 1
  18. fi
  19. trap 'rm -f -- "$tmpFile"' EXIT
  20. # the admin socket of the devnet guardians. used for executing commands in guardian pods.
  21. sock=/tmp/admin.sock
  22. guardianPublicWebBaseUrl="${webHost}:7071"
  23. currentGuardianSetUrl="${guardianPublicWebBaseUrl}/v1/guardianset/current"
  24. # fetch result and parse json body:
  25. guardianSet=$(curl ${currentGuardianSetUrl} | jq ".guardianSet")
  26. currentIndex=$(echo ${guardianSet} | jq ".index")
  27. currentNumGuardians=$(echo ${guardianSet} | jq ".addresses | length")
  28. echo "currentIndex: ${currentIndex}"
  29. echo "currentNumGuardians ${currentNumGuardians}"
  30. if [ ${currentNumGuardians} == ${newNumGuardians} ]; then
  31. echo "number of guardians is as expected."
  32. exit 0
  33. fi
  34. echo "creating guardian set update governance message template prototext"
  35. minikube kubectl -- exec -n ${namespace} guardian-0 -c guardiand -- /guardiand template guardian-set-update --num=${newNumGuardians} --idx=${currentIndex} > ${tmpFile}
  36. # for i in $(seq ${newNumGuardians})
  37. for i in $(seq ${currentNumGuardians})
  38. do
  39. # create guardian index: [0-18]
  40. guardianIndex=$((i-1))
  41. # create the governance guardian set update prototxt file in the container
  42. echo "created governance file for guardian-${guardianIndex}"
  43. minikube kubectl -- cp ${tmpFile} ${namespace}/guardian-${guardianIndex}:${tmpFile} -c guardiand
  44. # inject the guardian set update
  45. minikube kubectl -- exec -n ${namespace} guardian-${guardianIndex} -c guardiand -- /guardiand admin governance-vaa-inject --socket $sock $tmpFile
  46. echo "injected governance VAA for guardian-${guardianIndex}"
  47. done
  48. # wait for the guardians to reach quorum about the new guardian set
  49. sleep 30 # probably overkill, but some waiting is required.
  50. function get_sequence_from_prototext {
  51. path=${1}
  52. while IFS= read -r line
  53. do
  54. parts=($line)
  55. if [ ${parts[0]} == "sequence:" ]; then
  56. echo "${parts[1]}"
  57. fi
  58. done < "$path"
  59. }
  60. sequence=$(get_sequence_from_prototext ${tmpFile})
  61. echo "got sequence: ${sequence} from ${tmpFile}"
  62. # get vaa
  63. governanceChain="1"
  64. governanceAddress="0000000000000000000000000000000000000000000000000000000000000004"
  65. vaaUrl="${guardianPublicWebBaseUrl}/v1/signed_vaa/${governanceChain}/${governanceAddress}/${sequence}"
  66. echo "going to call to fetch VAA: ${vaaUrl}"
  67. # proto endpoints supply a base64 encoded VAA
  68. b64Vaa=$(curl ${vaaUrl} | jq ".vaaBytes")
  69. echo "got bas64 VAA: ${b64Vaa}"
  70. function base64_to_hex {
  71. b64Str=${1}
  72. echo $b64Str | base64 -d -i | hexdump -v -e '/1 "%02x" '
  73. }
  74. # transform base54 to hex
  75. hexVaa=$(base64_to_hex ${b64Vaa})
  76. echo "got hex VAA: ${hexVaa}"
  77. # fire off the Golang script in clients/eth:
  78. ./scripts/send-vaa.sh $webHost $hexVaa
  79. # give some time for guardians to observe the tx and update their state
  80. sleep 30
  81. # fetch result and parse json body:
  82. echo "going to fetch current guardianset from ${currentGuardianSetUrl}"
  83. nextGuardianSet=$(curl ${currentGuardianSetUrl} | jq ".guardianSet")
  84. nextIndex=$(echo ${nexGuardianSet} | jq ".index")
  85. nextNumGuardians=$(echo ${nextGuardianSet} | jq ".addresses | length")
  86. echo "nextIndex: ${nextIndex}"
  87. echo "nextNumGuardians ${nextNumGuardians}"
  88. if [ ${nextNumGuardians} == ${newNumGuardians} ]; then
  89. echo "number of guardians is as expected."
  90. else
  91. echo "number of guardians is not as expected. number of guardians in set: ${nextNumGuardians}."
  92. exit 1
  93. fi
  94. echo "update-guardian-set.sh succeeded."