set_code_hash.spec.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // SPDX-License-Identifier: Apache-2.0
  2. import expect from 'expect';
  3. import { createConnection, deploy, aliceKeypair, query, debug_buffer, weight, transaction, } from './index';
  4. import { ContractPromise } from '@polkadot/api-contract';
  5. import { ApiPromise } from '@polkadot/api';
  6. import { KeyringPair } from '@polkadot/keyring/types';
  7. import { U8aFixed } from '@polkadot/types';
  8. describe('Deploy the SetCodeCounter contracts and test for the upgrade to work', () => {
  9. let conn: ApiPromise;
  10. let counter: ContractPromise;
  11. let hashes: [U8aFixed, U8aFixed];
  12. let alice: KeyringPair;
  13. before(async function () {
  14. alice = aliceKeypair();
  15. conn = await createConnection();
  16. const counterV1 = await deploy(conn, alice, 'SetCodeCounterV1.contract', 0n, 1336n);
  17. const counterV2 = await deploy(conn, alice, 'SetCodeCounterV2.contract', 0n, 0n);
  18. hashes = [counterV1.abi.info.source.wasmHash, counterV2.abi.info.source.wasmHash];
  19. counter = new ContractPromise(conn, counterV1.abi, counterV1.address);
  20. });
  21. after(async function () {
  22. await conn.disconnect();
  23. });
  24. it('can switch out implementation using set_code_hash', async function () {
  25. // Code hash should be V1, expect to increment
  26. let gasLimit = await weight(conn, counter, 'inc', []);
  27. await transaction(counter.tx.inc({ gasLimit }), alice);
  28. let count = await query(conn, alice, counter, "count");
  29. expect(BigInt(count.output?.toString() ?? "")).toStrictEqual(1337n);
  30. // Switching to V2
  31. gasLimit = await weight(conn, counter, 'set_code', [hashes[1]]);
  32. await transaction(counter.tx.setCode({ gasLimit }, hashes[1]), alice);
  33. // Code hash should be V2, expect to decrement
  34. gasLimit = await weight(conn, counter, 'inc', []);
  35. await transaction(counter.tx.inc({ gasLimit }), alice);
  36. count = await query(conn, alice, counter, "count");
  37. expect(BigInt(count.output?.toString() ?? "")).toStrictEqual(1336n);
  38. // Switching to V1
  39. gasLimit = await weight(conn, counter, 'set_code', [hashes[0]]);
  40. await transaction(counter.tx.setCode({ gasLimit }, hashes[0]), alice);
  41. // Code hash should be V1, expect to increment
  42. gasLimit = await weight(conn, counter, 'inc', []);
  43. await transaction(counter.tx.inc({ gasLimit }), alice);
  44. count = await query(conn, alice, counter, "count");
  45. expect(BigInt(count.output?.toString() ?? "")).toStrictEqual(1337n);
  46. });
  47. });