contract-upgrade-governance.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. #!/bin/bash
  2. # This tool automates the process of writing contract upgrade governance
  3. # proposals in markdown format.
  4. #
  5. # There are two ways to run this script: either in "one-shot" mode, where a
  6. # single governance VAA is generated:
  7. #
  8. # ./contract-upgrade-governance.sh -m token_bridge -c solana -a Hp1YjsMbapQ75qpLaHQHuAv5Q8QwPoXs63zQrrcgg2HL > governance.md
  9. #
  10. # or in "multi" mode, where multiple VAAs are created in the same proposal:
  11. #
  12. # ./contract-upgrade-governance.sh -m token_bridge -c solana -a Hp1YjsMbapQ75qpLaHQHuAv5Q8QwPoXs63zQrrcgg2HL -o my_proposal > governance.md
  13. # ./contract-upgrade-governance.sh -m token_bridge -c avalanche -a 0x45fC4b6DD26097F0E51B1C91bcc331E469Ca73c2 -o my_proposal > governance.md
  14. # ... -o my_proposal > governance.md
  15. #
  16. # In multi mode, there's an additional "-o" flag, which takes a directory name,
  17. # where intermediate progress is saved between runs. If the directory doesn't
  18. # exist, the tool will create it.
  19. #
  20. # In both one-shot and multi modes, the script outputs the markdown-formatted
  21. # proposal to STDOUT, so it's a good idea to pipe it into a file (as in the above examples).
  22. #
  23. # In multi-mode, it always outputs the most recent version, so it's safe to
  24. # override the previous files.
  25. #
  26. # Once a multi-mode run is completed, the directory specified with the -o flag can be deleted.
  27. set -euo pipefail
  28. usage() {
  29. cat <<-EOF >&2
  30. Usage: $(basename "$0") [OPTIONS]
  31. Generate governance proposal for a module to be upgraded to a given address.
  32. Options:
  33. -h, --help Show this help message
  34. -m, --module <module> Specify the module (bridge, token_bridge, nft_bridge)
  35. -c, --chain <chain_name> Specify the chain name
  36. -a, --address <address> Specify the new code address (e.g., 0x3f1a6729bb27350748f0a0bd85ca641a100bf0a1)
  37. -o, --output <output_dir> Specify the multi-mode output directory
  38. -f, --force Force: bypass dirty git repo check
  39. EOF
  40. exit 1
  41. }
  42. # Check if guardiand command exists. It's needed for generating the protoxt and
  43. # computing the digest.
  44. if ! command -v guardiand >/dev/null 2>&1; then
  45. echo "ERROR: guardiand binary not found" >&2
  46. exit 1
  47. fi
  48. # Check if the worm command exists. It's needed for computing the digest.
  49. if ! command -v worm >/dev/null 2>&1; then
  50. echo "ERROR: worm binary not found" >&2
  51. exit 1
  52. fi
  53. ### Parse command line options
  54. address=""
  55. module=""
  56. chain_name=""
  57. multi_mode=false
  58. out_dir=""
  59. allow_dirty=false
  60. while (( "$#" )); do
  61. case "$1" in
  62. -h|--help)
  63. usage
  64. ;;
  65. -m|--module)
  66. module="$2"
  67. shift 2
  68. ;;
  69. -c|--chain)
  70. chain_name="$2"
  71. shift 2
  72. ;;
  73. -a|--address)
  74. address="$2"
  75. shift 2
  76. ;;
  77. -o|--output)
  78. multi_mode=true
  79. out_dir="$2"
  80. shift 2
  81. ;;
  82. -f|--force)
  83. allow_dirty=true
  84. shift
  85. ;;
  86. --) # end of options
  87. shift
  88. break
  89. ;;
  90. -*)
  91. echo "Error: Unsupported option $1" >&2
  92. usage
  93. ;;
  94. *) # anything else
  95. echo "Error: Unsupported argument $1" >&2
  96. usage
  97. ;;
  98. esac
  99. done
  100. [ -z "$address" ] && usage
  101. [ -z "$chain_name" ] && usage
  102. [ -z "$module" ] && usage
  103. # Check if the git tree is dirty
  104. if [ "$allow_dirty" = false ]; then
  105. if ! git diff-index --quiet HEAD --; then
  106. echo "ERROR: git tree is dirty. Commit or stash your changes first." >&2
  107. echo "If you are sure you want to proceed, use the --force flag." >&2
  108. exit 1
  109. fi
  110. fi
  111. ### The script constructs the governance proposal in two different steps. First,
  112. ### the governance prototxt (for VAA injection by the guardiand tool), then the voting/verification instructions.
  113. gov_msg_file=""
  114. instructions_file=""
  115. if [ "$multi_mode" = true ]; then
  116. mkdir -p "$out_dir"
  117. gov_msg_file="$out_dir/governance.prototxt"
  118. instructions_file="$out_dir/instructions.md"
  119. else
  120. gov_msg_file=$(mktemp)
  121. instructions_file=$(mktemp)
  122. fi
  123. explorer=""
  124. evm=false
  125. # TODO: move to CLI
  126. case "$chain_name" in
  127. solana)
  128. chain=1
  129. explorer="https://explorer.solana.com/address/"
  130. extra=""
  131. ;;
  132. pythnet)
  133. chain=26
  134. explorer="https://explorer.solana.com/address/"
  135. extra="Be sure to choose \"Custom RPC\" as the cluster in the explorer and set it to https://pythnet.rpcpool.com"
  136. ;;
  137. ethereum)
  138. chain=2
  139. explorer="https://etherscan.io/address/"
  140. evm=true
  141. ;;
  142. terra)
  143. chain=3
  144. # This is not technically the explorer, but terra finder does not show
  145. # information about code ids, so this is the best we can do.
  146. explorer="https://terra-classic-lcd.publicnode.com/cosmwasm/wasm/v1/code/"
  147. ;;
  148. bsc)
  149. chain=4
  150. explorer="https://bscscan.com/address/"
  151. evm=true
  152. ;;
  153. polygon)
  154. chain=5
  155. explorer="https://polygonscan.com/address/"
  156. evm=true
  157. ;;
  158. avalanche)
  159. chain=6
  160. explorer="https://snowtrace.io/address/"
  161. evm=true
  162. ;;
  163. oasis)
  164. chain=7
  165. explorer="https://explorer.emerald.oasis.dev/address/"
  166. evm=true
  167. ;;
  168. aurora)
  169. chain=9
  170. explorer="https://aurorascan.dev/address/"
  171. evm=true
  172. ;;
  173. algorand)
  174. chain=8
  175. explorer="https://algoexplorer.io/address/"
  176. ;;
  177. fantom)
  178. chain=10
  179. explorer="https://ftmscan.com/address/"
  180. evm=true
  181. ;;
  182. karura)
  183. chain=11
  184. explorer="https://blockscout.karura.network/address/"
  185. evm=true
  186. ;;
  187. acala)
  188. chain=12
  189. explorer="https://blockscout.acala.network/address/"
  190. evm=true
  191. ;;
  192. klaytn)
  193. chain=13
  194. explorer="https://scope.klaytn.com/account/"
  195. evm=true
  196. ;;
  197. celo)
  198. chain=14
  199. explorer="https://celoscan.xyz/address/"
  200. evm=true
  201. ;;
  202. near)
  203. chain=15
  204. explorer="https://explorer.near.org/accounts/"
  205. ;;
  206. arbitrum)
  207. chain=23
  208. explorer="https://arbiscan.io/address/"
  209. evm=true
  210. ;;
  211. optimism)
  212. chain=24
  213. explorer="https://optimistic.etherscan.io/address/"
  214. evm=true
  215. ;;
  216. aptos)
  217. chain=22
  218. explorer="https://explorer.aptoslabs.com/account/"
  219. ;;
  220. base)
  221. echo "Need to specify the base explorer URL!"
  222. exit 1
  223. chain=30
  224. explorer="??/address/"
  225. evm=true
  226. ;;
  227. *)
  228. echo "Unknown chain: $chain_name" >&2
  229. exit 1
  230. ;;
  231. esac
  232. # On terra, the contract given is a decimal code id. We convert it to a 32 byte
  233. # hex first. The printf is escaped, which makes no difference when we actually
  234. # evaluate the governance command later, but shows up unevaluated in the
  235. # instructions (so it's easier to read)
  236. terra_code_id=""
  237. if [ "$chain_name" = "terra" ]; then
  238. terra_code_id="$address" # save code id for later
  239. address="\$(printf \"%064x\" $terra_code_id)"
  240. fi
  241. # Generate the command to create the governance prototxt
  242. function create_governance() {
  243. case "$module" in
  244. bridge|core)
  245. echo "\
  246. guardiand template contract-upgrade \\
  247. --chain-id $chain \\
  248. --new-address $address"
  249. ;;
  250. token_bridge)
  251. echo "\
  252. guardiand template token-bridge-upgrade-contract \\
  253. --chain-id $chain --module \"TokenBridge\" \\
  254. --new-address $address"
  255. ;;
  256. nft_bridge)
  257. echo "\
  258. guardiand template token-bridge-upgrade-contract \\
  259. --chain-id $chain --module \"NFTBridge\" \\
  260. --new-address $address"
  261. ;;
  262. wormhole_relayer)
  263. echo "\
  264. guardiand template token-bridge-upgrade-contract \\
  265. --chain-id $chain --module \"WormholeRelayer\" \\
  266. --new-address $address"
  267. ;;
  268. *) echo "unknown module $module" >&2
  269. usage
  270. ;;
  271. esac
  272. }
  273. function evm_artifact() {
  274. case "$module" in
  275. bridge|core)
  276. echo "build/contracts/Implementation.json"
  277. ;;
  278. token_bridge)
  279. echo "build/contracts/BridgeImplementation.json"
  280. ;;
  281. nft_bridge)
  282. echo "build/contracts/NFTBridgeImplementation.json"
  283. ;;
  284. *) echo "unknown module $module" >&2
  285. usage
  286. ;;
  287. esac
  288. }
  289. function solana_artifact() {
  290. case "$module" in
  291. bridge|core)
  292. echo "artifacts-mainnet/bridge.so"
  293. ;;
  294. token_bridge)
  295. echo "artifacts-mainnet/token_bridge.so"
  296. ;;
  297. nft_bridge)
  298. echo "artifacts-mainnet/nft_bridge.so"
  299. ;;
  300. *) echo "unknown module $module" >&2
  301. usage
  302. ;;
  303. esac
  304. }
  305. function near_artifact() {
  306. case "$module" in
  307. bridge|core)
  308. echo "artifacts/near_wormhole.wasm"
  309. ;;
  310. token_bridge)
  311. echo "artifacts/near_token_bridge.wasm"
  312. ;;
  313. *) echo "unknown module $module" >&2
  314. usage
  315. ;;
  316. esac
  317. }
  318. function algorand_artifact() {
  319. case "$module" in
  320. bridge|core)
  321. echo "artifacts/core_approve.teal.hash"
  322. ;;
  323. token_bridge)
  324. echo "artifacts/token_approve.teal.hash"
  325. ;;
  326. *) echo "unknown module $module" >&2
  327. usage
  328. ;;
  329. esac
  330. }
  331. function terra_artifact() {
  332. case "$module" in
  333. bridge|core)
  334. echo "artifacts/wormhole.wasm"
  335. ;;
  336. token_bridge)
  337. echo "artifacts/token_bridge_terra.wasm"
  338. ;;
  339. *) echo "unknown module $module" >&2
  340. usage
  341. ;;
  342. esac
  343. }
  344. ################################################################################
  345. # Construct the governance proto
  346. echo "# $module upgrade on $chain_name" >> "$gov_msg_file"
  347. # Append the new governance message to the gov file
  348. eval "$(create_governance)" >> "$gov_msg_file"
  349. # Multiple messages will include multiple 'current_set_index' fields, but the
  350. # proto format only takes one. This next part cleans up the file so there's only
  351. # a single 'current_set_index' field.
  352. # 1. we grab the first one and save it
  353. current_set_index=$(grep "current_set_index" "$gov_msg_file" | head -n 1)
  354. # 2. remove all 'current_set_index' fields
  355. rest=$(grep -v "current_set_index" "$gov_msg_file")
  356. # 3. write the set index
  357. echo "$current_set_index" > "$gov_msg_file"
  358. # 4. then the rest of the file
  359. echo "$rest" >> "$gov_msg_file"
  360. ################################################################################
  361. # Compute expected digests
  362. # just use the 'guardiand' command, which spits out a bunch of text to
  363. # stderr. We grab that output and pick out the VAA hashes
  364. verify=$(guardiand admin governance-vaa-verify "$gov_msg_file" 2>&1)
  365. digest=$(echo "$verify" | grep "VAA with digest" | cut -d' ' -f6 | sed 's/://g')
  366. # massage the digest into the same format that the inject command prints it
  367. digest=$(echo "$digest" | awk '{print toupper($0)}' | sed 's/^0X//')
  368. # we use the first 7 characters of the digest as an identifier for the prototxt file
  369. gov_id=$(echo "$digest" | cut -c1-7)
  370. ################################################################################
  371. # Print vote command and expected digests
  372. # This we only print to stdout, because in multi mode, it gets recomputed each
  373. # time. The rest of the output gets printed into the instructions file
  374. cat <<-EOD
  375. # Governance
  376. Shell command for voting:
  377. \`\`\`shell
  378. cat << EOF > governance-$gov_id.prototxt
  379. $(cat "$gov_msg_file")
  380. EOF
  381. guardiand admin governance-vaa-inject --socket /path/to/admin.sock governance-$gov_id.prototxt
  382. \`\`\`
  383. Expected digest(s):
  384. \`\`\`
  385. $digest
  386. \`\`\`
  387. EOD
  388. ################################################################################
  389. # Verification instructions
  390. # The rest of the output is printed to the instructions file (which then also
  391. # gets printed to stdout at the end)
  392. echo "# Verification steps ($chain_name $module)
  393. " >> "$instructions_file"
  394. # Print instructions on checking out the current git hash:
  395. git_hash=$(git rev-parse HEAD)
  396. echo "
  397. ## Checkout the current git hash
  398. \`\`\`shell
  399. git fetch
  400. git checkout $git_hash
  401. \`\`\`" >> "$instructions_file"
  402. # Verification steps depend on the chain.
  403. if [ "$evm" = true ]; then
  404. cat <<-EOF >> "$instructions_file"
  405. ## Build
  406. \`\`\`shell
  407. wormhole/ethereum $ make
  408. \`\`\`
  409. ## Verify
  410. Contract at [$explorer$address]($explorer$address)
  411. Next, use the \`verify\` script to verify that the deployed bytecodes we are upgrading to match the build artifacts:
  412. \`\`\`shell
  413. wormhole/ethereum $ ./verify -r $(worm info rpc mainnet $chain_name) -c $chain_name $(evm_artifact) $address
  414. \`\`\`
  415. EOF
  416. elif [ "$chain_name" = "solana" ] || [ "$chain_name" = "pythnet" ]; then
  417. cat <<-EOF >> "$instructions_file"
  418. ## Build
  419. \`\`\`shell
  420. wormhole/solana $ make clean
  421. wormhole/solana $ make NETWORK=mainnet artifacts
  422. \`\`\`
  423. This command will compile all the contracts into the \`artifacts-mainnet\` directory using Docker to ensure that the build artifacts are deterministic.
  424. ## Verify
  425. Contract at [$explorer$address]($explorer$address)
  426. $extra
  427. Next, use the \`verify\` script to verify that the deployed bytecodes we are upgrading to match the build artifacts:
  428. \`\`\`shell
  429. # $module
  430. wormhole/solana$ ./verify -n mainnet $(solana_artifact) $address
  431. \`\`\`
  432. EOF
  433. elif [ "$chain_name" = "near" ]; then
  434. cat <<-EOF >> "$instructions_file"
  435. ## Build
  436. \`\`\`shell
  437. wormhole/near $ make artifacts
  438. \`\`\`
  439. This command will compile all the contracts into the \`artifacts\` directory using Docker to ensure that the build artifacts are deterministic.
  440. Next, you can look at the checksums of the built .wasm files
  441. \`\`\`shell
  442. # $module
  443. wormhole/near$ sha256sum $(near_artifact)
  444. \`\`\`
  445. EOF
  446. elif [ "$chain_name" = "algorand" ]; then
  447. cat <<-EOF >> "$instructions_file"
  448. ## Build
  449. \`\`\`shell
  450. wormhole/algorand $ make artifacts
  451. \`\`\`
  452. This command will compile all the contracts into the \`artifacts\` directory using Docker to ensure that the build artifacts are deterministic.
  453. You can then review $(algorand_artifact) to confirm the supplied hash value
  454. EOF
  455. elif [ "$chain_name" = "terra" ]; then
  456. cat <<-EOF >> "$instructions_file"
  457. ## Build
  458. \`\`\`shell
  459. wormhole/terra $ make clean
  460. wormhole/terra $ make artifacts
  461. \`\`\`
  462. This command will compile all the contracts into the \`artifacts\` directory using Docker to ensure that the build artifacts are deterministic.
  463. ## Verify
  464. Contract at [$explorer$terra_code_id]($explorer$terra_code_id)
  465. Next, use the \`verify\` script to verify that the deployed bytecodes we are upgrading to match the build artifacts:
  466. \`\`\`shell
  467. # $module
  468. wormhole/terra$ ./verify -n mainnet $(terra_artifact) $terra_code_id
  469. \`\`\`
  470. EOF
  471. elif [ "$chain_name" = "aptos" ]; then
  472. cat <<-EOF >> "$instructions_file"
  473. ## Build
  474. \`\`\`shell
  475. wormhole/aptos $ docker build -f Dockerfile --target aptos -t aptos-build .
  476. \`\`\`
  477. This command will build a docker image that can compile the contracts reproducibly.
  478. ## Verify
  479. Next, run the following command to check that the contract hash matches the expected value ($address):
  480. \`\`\`shell
  481. # $module
  482. wormhole/aptos$ docker run -it aptos-build
  483. wormhole/aptos$ worm aptos hash-contracts /tmp/$module --named-addresses wormhole=0x5bc11445584a763c1fa7ed39081f1b920954da14e04b32440cba863d03e19625,deployer=0x0108bc32f7de18a5f6e1e7d6ee7aff9f5fc858d0d87ac0da94dd8d2a5d267d6b,token_bridge=0x576410486a2da45eee6c949c995670112ddf2fbeedab20350d506328eefc9d4f,nft_bridge=0x1bdffae984043833ed7fe223f7af7a3f8902d04129b14f801823e64827da7130
  484. wormhole/aptos$ exit
  485. \`\`\`
  486. EOF
  487. else
  488. echo "ERROR: no verification instructions for chain $chain_name" >&2
  489. exit 1
  490. fi
  491. cat <<-EOF >> "$instructions_file"
  492. ## Create governance
  493. \`\`\`shell
  494. $(create_governance)
  495. \`\`\`
  496. EOF
  497. # Finally print instructions to stdout
  498. cat "$instructions_file"