WormholeTest.spec.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import { Blockchain, SandboxContract, TreasuryContract } from "@ton/sandbox";
  2. import { Cell, toNano } from "@ton/core";
  3. import { WormholeTest, WormholeTestConfig } from "../wrappers/WormholeTest";
  4. import "@ton/test-utils";
  5. import { compile } from "@ton/blueprint";
  6. import {
  7. createGuardianSetUpgradeBytes,
  8. GUARDIAN_SET_0,
  9. GUARDIAN_SET_4,
  10. MAINNET_UPGRADE_VAAS,
  11. } from "./utils/wormhole";
  12. const CHAIN_ID = 1;
  13. const GOVERNANCE_CHAIN_ID = 1;
  14. const GOVERNANCE_CONTRACT =
  15. "0000000000000000000000000000000000000000000000000000000000000004";
  16. describe("WormholeTest", () => {
  17. let code: Cell;
  18. beforeAll(async () => {
  19. code = await compile("WormholeTest");
  20. });
  21. let blockchain: Blockchain;
  22. let deployer: SandboxContract<TreasuryContract>;
  23. let wormholeTest: SandboxContract<WormholeTest>;
  24. beforeEach(async () => {
  25. blockchain = await Blockchain.create();
  26. deployer = await blockchain.treasury("deployer");
  27. });
  28. async function deployContract(
  29. guardianSetIndex: number = 0,
  30. guardianSet: string[] = GUARDIAN_SET_0,
  31. chainId: number = CHAIN_ID,
  32. governanceChainId: number = GOVERNANCE_CHAIN_ID,
  33. governanceContract: string = GOVERNANCE_CONTRACT
  34. ) {
  35. const config: WormholeTestConfig = {
  36. guardianSetIndex,
  37. guardianSet,
  38. chainId,
  39. governanceChainId,
  40. governanceContract,
  41. };
  42. wormholeTest = blockchain.openContract(
  43. WormholeTest.createFromConfig(config, code)
  44. );
  45. const deployResult = await wormholeTest.sendDeploy(
  46. deployer.getSender(),
  47. toNano("0.05")
  48. );
  49. expect(deployResult.transactions).toHaveTransaction({
  50. from: deployer.address,
  51. to: wormholeTest.address,
  52. deploy: true,
  53. success: true,
  54. });
  55. const guardianSetIndexRes = await wormholeTest.getCurrentGuardianSetIndex();
  56. expect(guardianSetIndexRes).toBe(guardianSetIndex);
  57. }
  58. it("should correctly parse encoded upgrade", async () => {
  59. await deployContract();
  60. const currentGuardianSetIndex = 3;
  61. const newGuardianSetIndex = 4;
  62. const chainId = 1; // Example chain ID
  63. const encodedUpgrade = createGuardianSetUpgradeBytes(
  64. chainId,
  65. newGuardianSetIndex,
  66. GUARDIAN_SET_4
  67. );
  68. const result = await wormholeTest.getParseEncodedUpgrade(
  69. currentGuardianSetIndex,
  70. encodedUpgrade
  71. );
  72. expect(result.action).toBe(2);
  73. expect(result.chain).toBe(chainId);
  74. expect(result.module.toString(16)).toBe("436f7265");
  75. expect(result.newGuardianSetIndex).toBeGreaterThan(currentGuardianSetIndex);
  76. expect(result.newGuardianSetIndex).toBe(newGuardianSetIndex);
  77. expect(result.newGuardianSetKeys).toEqual(GUARDIAN_SET_4);
  78. });
  79. it("should fail with invalid encoded upgrade", async () => {
  80. await deployContract();
  81. const currentGuardianSetIndex = 3;
  82. const newGuardianSetIndex = 4;
  83. const chainId = 1; // Example chain ID
  84. const encodedUpgrade = createGuardianSetUpgradeBytes(
  85. chainId,
  86. newGuardianSetIndex,
  87. GUARDIAN_SET_4
  88. );
  89. // Replace the first 32 bytes with zeros
  90. const zeroBytes = Buffer.alloc(32, 0);
  91. zeroBytes.copy(encodedUpgrade, 0, 0, 32);
  92. await expect(
  93. wormholeTest.getParseEncodedUpgrade(
  94. currentGuardianSetIndex,
  95. encodedUpgrade
  96. )
  97. ).rejects.toThrow("Unable to execute get method. Got exit_code: 1011"); // ERROR_INVALID_MODULE = 1011
  98. });
  99. it("should correctly parse and verify wormhole vm", async () => {
  100. await deployContract();
  101. const mainnet_upgrade_vaa_1 = MAINNET_UPGRADE_VAAS[0];
  102. const result = await wormholeTest.getParseAndVerifyWormholeVm(
  103. Buffer.from(mainnet_upgrade_vaa_1, "hex")
  104. );
  105. expect(result.version).toBe(1);
  106. expect(result.vm_guardian_set_index).toBe(0);
  107. expect(result.timestamp).toBe(1628094930);
  108. expect(result.nonce).toBe(3);
  109. expect(result.emitter_chain_id).toBe(1);
  110. expect(result.emitter_address.toString()).toBe(
  111. "0000000000000000000000000000000000000000000000000000000000000004"
  112. );
  113. expect(result.sequence).toBe(1337);
  114. expect(result.consistency_level).toBe(0);
  115. expect(result.payload).toBe(mainnet_upgrade_vaa_1.slice(246));
  116. expect(result.hash).toBe(
  117. "ed3a5600d44b9dcc889daf0178dd69ab1e9356308194ba3628a7b720ae48a8d5"
  118. );
  119. });
  120. it("should correctly update guardian set", async () => {
  121. await deployContract();
  122. const mainnet_upgrade_vaa_1 = MAINNET_UPGRADE_VAAS[0];
  123. const getUpdateGuardianSetResult = await wormholeTest.sendUpdateGuardianSet(
  124. deployer.getSender(),
  125. Buffer.from(mainnet_upgrade_vaa_1, "hex")
  126. );
  127. expect(getUpdateGuardianSetResult.transactions).toHaveTransaction({
  128. from: deployer.address,
  129. to: wormholeTest.address,
  130. success: true,
  131. });
  132. const getCurrentGuardianSetIndexResult =
  133. await wormholeTest.getCurrentGuardianSetIndex();
  134. expect(getCurrentGuardianSetIndexResult).toBe(1);
  135. });
  136. it("should fail with wrong vaa", async () => {
  137. await deployContract();
  138. const invalid_mainnet_upgrade_vaa = "00" + MAINNET_UPGRADE_VAAS[0].slice(2);
  139. const result = await wormholeTest.sendUpdateGuardianSet(
  140. deployer.getSender(),
  141. Buffer.from(invalid_mainnet_upgrade_vaa, "hex")
  142. );
  143. expect(result.transactions).toHaveTransaction({
  144. from: deployer.address,
  145. to: wormholeTest.address,
  146. success: false,
  147. exitCode: 1001, // ERROR_INVALID_VERSION = 1001
  148. });
  149. });
  150. it("should correctly get guardian set", async () => {
  151. await deployContract();
  152. const getGuardianSetResult = await wormholeTest.getGuardianSet(0);
  153. expect(getGuardianSetResult.keys).toEqual(GUARDIAN_SET_0);
  154. });
  155. it("should return the correct chain ID", async () => {
  156. await deployContract();
  157. const result = await wormholeTest.getChainId();
  158. expect(result).toEqual(CHAIN_ID);
  159. });
  160. it("should return the correct governance chain ID", async () => {
  161. await deployContract();
  162. const result = await wormholeTest.getGovernanceChainId();
  163. expect(result).toEqual(GOVERNANCE_CHAIN_ID);
  164. });
  165. it("should return the correct governance contract address", async () => {
  166. await deployContract();
  167. const result = await wormholeTest.getGovernanceContract();
  168. expect(result).toEqual(GOVERNANCE_CONTRACT);
  169. });
  170. it("should correctly check if a governance action is consumed", async () => {
  171. await deployContract();
  172. const hash = 12345n;
  173. let getGovernanceActionIsConsumedResult =
  174. await wormholeTest.getGovernanceActionIsConsumed(hash);
  175. expect(getGovernanceActionIsConsumedResult).toEqual(false);
  176. const mainnet_upgrade_vaa_1 = MAINNET_UPGRADE_VAAS[0];
  177. const getParseAndVerifyWormholeVmResult =
  178. await wormholeTest.getParseAndVerifyWormholeVm(
  179. Buffer.from(mainnet_upgrade_vaa_1, "hex")
  180. );
  181. expect(getParseAndVerifyWormholeVmResult.hash).toBe(
  182. "ed3a5600d44b9dcc889daf0178dd69ab1e9356308194ba3628a7b720ae48a8d5"
  183. );
  184. const sendUpdateGuardianSetResult =
  185. await wormholeTest.sendUpdateGuardianSet(
  186. deployer.getSender(),
  187. Buffer.from(mainnet_upgrade_vaa_1, "hex")
  188. );
  189. expect(sendUpdateGuardianSetResult.transactions).toHaveTransaction({
  190. from: deployer.address,
  191. to: wormholeTest.address,
  192. success: true,
  193. });
  194. getGovernanceActionIsConsumedResult =
  195. await wormholeTest.getGovernanceActionIsConsumed(
  196. BigInt("0x" + getParseAndVerifyWormholeVmResult.hash)
  197. );
  198. expect(getGovernanceActionIsConsumedResult).toEqual(true);
  199. });
  200. });