Skip to content

Commit 92c17ad

Browse files
authored
Xc admin/change error handling (#471)
* Switch from undefined to not undefined * Update tests
1 parent 80fe230 commit 92c17ad

File tree

3 files changed

+22
-35
lines changed

3 files changed

+22
-35
lines changed

xc-admin/packages/xc-admin-common/src/__tests__/GovernancePayload.test.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,19 @@ test("GovernancePayload ser/de", (done) => {
6060
expect(governanceHeader?.action).toBe("SetFee");
6161

6262
// Wrong magic number
63-
governanceHeader = decodeHeader(
64-
Buffer.from([0, 0, 0, 0, 0, 0, 0, 26, 0, 0, 0, 0])
65-
);
66-
expect(governanceHeader).toBeUndefined();
63+
expect(() =>
64+
decodeHeader(Buffer.from([0, 0, 0, 0, 0, 0, 0, 26, 0, 0, 0, 0]))
65+
).toThrow("Wrong magic number");
6766

6867
// Wrong chain
69-
governanceHeader = decodeHeader(
70-
Buffer.from([80, 84, 71, 77, 0, 0, 255, 255, 0, 0, 0, 0])
71-
);
72-
expect(governanceHeader).toBeUndefined();
68+
expect(() =>
69+
decodeHeader(Buffer.from([80, 84, 71, 77, 0, 0, 255, 255, 0, 0, 0, 0]))
70+
).toThrow("Chain Id not found");
7371

7472
// Wrong module/action combination
75-
governanceHeader = decodeHeader(
76-
Buffer.from([80, 84, 71, 77, 0, 1, 0, 26, 0, 0, 0, 0])
77-
);
78-
expect(governanceHeader).toBeUndefined();
73+
expect(() =>
74+
decodeHeader(Buffer.from([80, 84, 71, 77, 0, 1, 0, 26, 0, 0, 0, 0]))
75+
).toThrow("Invalid header, action doesn't match module");
7976

8077
// Decode executePostVaa with empty instructions
8178
let expectedExecuteVaaArgs = {

xc-admin/packages/xc-admin-common/src/governance_payload/ExecutePostedVaa.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,11 @@ export type ExecutePostedVaaArgs = {
8383
};
8484

8585
/** Decode ExecutePostedVaaArgs and return undefined if it failed */
86-
export function decodeExecutePostedVaa(
87-
data: Buffer
88-
): ExecutePostedVaaArgs | undefined {
86+
export function decodeExecutePostedVaa(data: Buffer): ExecutePostedVaaArgs {
8987
let deserialized = executePostedVaaLayout.decode(data);
9088

9189
let header = verifyHeader(deserialized.header);
9290

93-
if (!header) {
94-
return undefined;
95-
}
96-
9791
let instructions: TransactionInstruction[] = deserialized.instructions.map(
9892
(ix) => {
9993
let programId: PublicKey = new PublicKey(ix.programId);

xc-admin/packages/xc-admin-common/src/governance_payload/index.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export function governanceHeaderLayout(): BufferLayout.Structure<
7575
}
7676

7777
/** Decode Pyth Governance Header and return undefined if the header is invalid */
78-
export function decodeHeader(data: Buffer): PythGovernanceHeader | undefined {
78+
export function decodeHeader(data: Buffer): PythGovernanceHeader {
7979
let deserialized = governanceHeaderLayout().decode(data);
8080
return verifyHeader(deserialized);
8181
}
@@ -111,27 +111,23 @@ export function verifyHeader(
111111
action: number;
112112
chain: ChainId;
113113
}>
114-
) {
114+
): PythGovernanceHeader {
115115
if (deserialized.magicNumber !== MAGIC_NUMBER) {
116-
return undefined;
116+
throw new Error("Wrong magic number");
117117
}
118118

119119
if (!toChainName(deserialized.chain)) {
120-
return undefined;
120+
throw new Error("Chain Id not found");
121121
}
122122

123-
try {
124-
let governanceHeader: PythGovernanceHeader = {
125-
targetChainId: toChainName(deserialized.chain),
126-
action: toActionName({
127-
actionId: deserialized.action,
128-
moduleId: deserialized.module,
129-
}),
130-
};
131-
return governanceHeader;
132-
} catch {
133-
return undefined;
134-
}
123+
let governanceHeader: PythGovernanceHeader = {
124+
targetChainId: toChainName(deserialized.chain),
125+
action: toActionName({
126+
actionId: deserialized.action,
127+
moduleId: deserialized.module,
128+
}),
129+
};
130+
return governanceHeader;
135131
}
136132

137133
export { decodeExecutePostedVaa } from "./ExecutePostedVaa";

0 commit comments

Comments
 (0)