WormholeTest.spec.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. describe("WormholeTest", () => {
  13. let code: Cell;
  14. beforeAll(async () => {
  15. code = await compile("WormholeTest");
  16. });
  17. let blockchain: Blockchain;
  18. let deployer: SandboxContract<TreasuryContract>;
  19. let wormholeTest: SandboxContract<WormholeTest>;
  20. beforeEach(async () => {
  21. blockchain = await Blockchain.create();
  22. deployer = await blockchain.treasury("deployer");
  23. });
  24. async function deployContract(
  25. guardianSetIndex: number = 0,
  26. guardianSet: string[] = GUARDIAN_SET_0,
  27. chainId: number = 1,
  28. governanceChainId: number = 1,
  29. governanceContract: string = "0000000000000000000000000000000000000000000000000000000000000004"
  30. ) {
  31. const config: WormholeTestConfig = {
  32. guardianSetIndex,
  33. guardianSet,
  34. chainId,
  35. governanceChainId,
  36. governanceContract,
  37. };
  38. wormholeTest = blockchain.openContract(
  39. WormholeTest.createFromConfig(config, code)
  40. );
  41. const deployResult = await wormholeTest.sendDeploy(
  42. deployer.getSender(),
  43. toNano("0.05")
  44. );
  45. expect(deployResult.transactions).toHaveTransaction({
  46. from: deployer.address,
  47. to: wormholeTest.address,
  48. deploy: true,
  49. success: true,
  50. });
  51. const guardianSetIndexRes = await wormholeTest.getCurrentGuardianSetIndex();
  52. expect(guardianSetIndexRes).toBe(guardianSetIndex);
  53. }
  54. it("should correctly parse encoded upgrade", async () => {
  55. await deployContract();
  56. const currentGuardianSetIndex = 3;
  57. const newGuardianSetIndex = 4;
  58. const chainId = 1; // Example chain ID
  59. const encodedUpgrade = createGuardianSetUpgradeBytes(
  60. chainId,
  61. newGuardianSetIndex,
  62. GUARDIAN_SET_4
  63. );
  64. const result = await wormholeTest.getParseEncodedUpgrade(
  65. currentGuardianSetIndex,
  66. encodedUpgrade
  67. );
  68. expect(result.action).toBe(2);
  69. expect(result.chain).toBe(chainId);
  70. expect(result.module.toString(16)).toBe("436f7265");
  71. expect(result.newGuardianSetIndex).toBeGreaterThan(currentGuardianSetIndex);
  72. expect(result.newGuardianSetIndex).toBe(newGuardianSetIndex);
  73. expect(result.newGuardianSetKeys).toEqual(GUARDIAN_SET_4);
  74. });
  75. it("should fail with invalid encoded upgrade", async () => {
  76. await deployContract();
  77. const currentGuardianSetIndex = 3;
  78. const newGuardianSetIndex = 4;
  79. const chainId = 1; // Example chain ID
  80. const encodedUpgrade = createGuardianSetUpgradeBytes(
  81. chainId,
  82. newGuardianSetIndex,
  83. GUARDIAN_SET_4
  84. );
  85. // Replace the first 32 bytes with zeros
  86. const zeroBytes = Buffer.alloc(32, 0);
  87. zeroBytes.copy(encodedUpgrade, 0, 0, 32);
  88. await expect(
  89. wormholeTest.getParseEncodedUpgrade(
  90. currentGuardianSetIndex,
  91. encodedUpgrade
  92. )
  93. ).rejects.toThrow("Unable to execute get method. Got exit_code: 1011"); // ERROR_INVALID_MODULE = 1011
  94. });
  95. it("should correctly parse and verify wormhole vm", async () => {
  96. await deployContract();
  97. const mainnet_upgrade_vaa_1 = MAINNET_UPGRADE_VAAS[0];
  98. const result = await wormholeTest.getParseAndVerifyWormholeVm(
  99. Buffer.from(mainnet_upgrade_vaa_1, "hex")
  100. );
  101. expect(result.version).toBe(1);
  102. expect(result.vm_guardian_set_index).toBe(0);
  103. expect(result.timestamp).toBe(1628094930);
  104. expect(result.nonce).toBe(3);
  105. expect(result.emitter_chain_id).toBe(1);
  106. expect(result.emitter_address.toString()).toBe(
  107. "0000000000000000000000000000000000000000000000000000000000000004"
  108. );
  109. expect(result.sequence).toBe(1337);
  110. expect(result.consistency_level).toBe(0);
  111. expect(result.payload).toBe(mainnet_upgrade_vaa_1.slice(246));
  112. expect(result.hash).toBe(
  113. "ed3a5600d44b9dcc889daf0178dd69ab1e9356308194ba3628a7b720ae48a8d5"
  114. );
  115. });
  116. it("should correctly update guardian set", async () => {
  117. await deployContract();
  118. const mainnet_upgrade_vaa_1 = MAINNET_UPGRADE_VAAS[0];
  119. const getUpdateGuardianSetResult = await wormholeTest.getUpdateGuardianSet(
  120. Buffer.from(mainnet_upgrade_vaa_1, "hex")
  121. );
  122. expect(getUpdateGuardianSetResult).toBe(-1);
  123. });
  124. it("should fail with wrong vaa", async () => {
  125. await deployContract();
  126. const invalid_mainnet_upgrade_vaa = "00" + MAINNET_UPGRADE_VAAS[0].slice(2);
  127. await expect(
  128. wormholeTest.getUpdateGuardianSet(
  129. Buffer.from(invalid_mainnet_upgrade_vaa, "hex")
  130. )
  131. ).rejects.toThrow("Unable to execute get method. Got exit_code: 1001"); // ERROR_INVALID_VERSION = 1001
  132. });
  133. it("should correctly get guardian set", async () => {
  134. await deployContract();
  135. const getGuardianSetResult = await wormholeTest.getGuardianSet(0);
  136. expect(getGuardianSetResult.keys).toEqual(GUARDIAN_SET_0);
  137. });
  138. });