From 99ed193764156288d9aef278858cf8bf934a5071 Mon Sep 17 00:00:00 2001 From: guibescos <59208140+guibescos@users.noreply.github.com> Date: Thu, 13 Apr 2023 11:51:47 -0500 Subject: [PATCH] [xc-admin] Add ser/de for Cosmos Upgrades (#758) * Add ser/de for Cosmos Upgrades * Add comment * Fix print --- .../packages/xc_admin_cli/src/index.ts | 8 +++- .../src/__tests__/UpgradeContract.test.ts | 44 +++++++++++++++++++ .../src/governance_payload/UpgradeContract.ts | 38 ++++++++++++++++ .../src/governance_payload/index.ts | 4 ++ 4 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 governance/xc_admin/packages/xc_admin_common/src/__tests__/UpgradeContract.test.ts create mode 100644 governance/xc_admin/packages/xc_admin_common/src/governance_payload/UpgradeContract.ts diff --git a/governance/xc_admin/packages/xc_admin_cli/src/index.ts b/governance/xc_admin/packages/xc_admin_cli/src/index.ts index 7c94af01..16c6277b 100644 --- a/governance/xc_admin/packages/xc_admin_cli/src/index.ts +++ b/governance/xc_admin/packages/xc_admin_cli/src/index.ts @@ -286,7 +286,13 @@ program keys: ix.keys as AccountMeta[], }) ); - console.log(JSON.stringify(parsed, null, 2)); + console.log( + JSON.stringify( + parsed, + (key, value) => (typeof value === "bigint" ? value.toString() : value), // return everything else unchanged + 2 + ) + ); }); multisigCommand("approve", "Approve a transaction sitting in the multisig") diff --git a/governance/xc_admin/packages/xc_admin_common/src/__tests__/UpgradeContract.test.ts b/governance/xc_admin/packages/xc_admin_common/src/__tests__/UpgradeContract.test.ts new file mode 100644 index 00000000..fd0903d8 --- /dev/null +++ b/governance/xc_admin/packages/xc_admin_common/src/__tests__/UpgradeContract.test.ts @@ -0,0 +1,44 @@ +import { + decodeGovernancePayload, + PythGovernanceHeader, +} from "../governance_payload"; +import { CosmosUpgradeContract } from "../governance_payload/UpgradeContract"; + +test("Upgrade contract ser/de", (done) => { + jest.setTimeout(60000); + + const expectedUpgradeContract = new CosmosUpgradeContract( + "injective", + BigInt("18446744073709551614") + ); + const buffer = expectedUpgradeContract.encode(); + + console.log(buffer.toJSON()); + expect( + buffer.equals( + Buffer.from([ + 80, 84, 71, 77, 1, 0, 0, 19, 255, 255, 255, 255, 255, 255, 255, 254, + ]) + ) + ).toBeTruthy(); + + const actualHeader = PythGovernanceHeader.decode(buffer); + + if (actualHeader) { + expect(actualHeader.targetChainId).toBe("injective"); + expect(actualHeader.action).toBe("UpgradeContract"); + } else { + done("Not an instance of CosmosUpgradeContract"); + } + + const actualUpgradeContract = decodeGovernancePayload(buffer); + + if (actualUpgradeContract instanceof CosmosUpgradeContract) { + expect(actualUpgradeContract.targetChainId).toBe("injective"); + expect(actualUpgradeContract.codeId).toBe(BigInt("18446744073709551614")); + } else { + done("Not an instance of CosmosUpgradeContract"); + } + + done(); +}); diff --git a/governance/xc_admin/packages/xc_admin_common/src/governance_payload/UpgradeContract.ts b/governance/xc_admin/packages/xc_admin_common/src/governance_payload/UpgradeContract.ts new file mode 100644 index 00000000..5a392029 --- /dev/null +++ b/governance/xc_admin/packages/xc_admin_common/src/governance_payload/UpgradeContract.ts @@ -0,0 +1,38 @@ +import { ChainName } from "@certusone/wormhole-sdk"; +import { PythGovernanceAction, PythGovernanceHeader } from "."; + +export class CosmosUpgradeContract implements PythGovernanceAction { + readonly targetChainId: ChainName; + readonly codeId: bigint; + + constructor(targetChainId: ChainName, codeId: bigint) { + this.targetChainId = targetChainId; + this.codeId = codeId; + } + + static span: number = 8; + static decode(data: Buffer): CosmosUpgradeContract | undefined { + const header = PythGovernanceHeader.decode(data); + if (!header) return undefined; + + const codeId = data.subarray(PythGovernanceHeader.span).readBigUInt64BE(); + if (!codeId) return undefined; + + return new CosmosUpgradeContract(header.targetChainId, codeId); + } + + /** Encode CosmosUpgradeContract */ + encode(): Buffer { + const headerBuffer = new PythGovernanceHeader( + this.targetChainId, + "UpgradeContract" + ).encode(); + + const buffer = Buffer.alloc( + PythGovernanceHeader.span + CosmosUpgradeContract.span + ); + + const span = buffer.writeBigUInt64BE(this.codeId); + return Buffer.concat([headerBuffer, buffer.subarray(0, span)]); + } +} diff --git a/governance/xc_admin/packages/xc_admin_common/src/governance_payload/index.ts b/governance/xc_admin/packages/xc_admin_common/src/governance_payload/index.ts index e7019b15..1b78261e 100644 --- a/governance/xc_admin/packages/xc_admin_common/src/governance_payload/index.ts +++ b/governance/xc_admin/packages/xc_admin_common/src/governance_payload/index.ts @@ -7,6 +7,7 @@ import { import * as BufferLayout from "@solana/buffer-layout"; import { PACKET_DATA_SIZE } from "@solana/web3.js"; import { ExecutePostedVaa } from "./ExecutePostedVaa"; +import { CosmosUpgradeContract } from "./UpgradeContract"; export interface PythGovernanceAction { readonly targetChainId: ChainName; @@ -148,6 +149,9 @@ export function decodeGovernancePayload( switch (header.action) { case "ExecutePostedVaa": return ExecutePostedVaa.decode(data); + case "UpgradeContract": + //TO DO : Support non-cosmos upgrades + return CosmosUpgradeContract.decode(data); default: return undefined; }