Xc admin/change error handling (#471)

* Switch from undefined to not undefined

* Update tests
This commit is contained in:
guibescos 2023-01-11 09:01:37 -06:00 committed by GitHub
parent 80fe230563
commit 92c17ad7cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 35 deletions

View File

@ -60,22 +60,19 @@ test("GovernancePayload ser/de", (done) => {
expect(governanceHeader?.action).toBe("SetFee");
// Wrong magic number
governanceHeader = decodeHeader(
Buffer.from([0, 0, 0, 0, 0, 0, 0, 26, 0, 0, 0, 0])
);
expect(governanceHeader).toBeUndefined();
expect(() =>
decodeHeader(Buffer.from([0, 0, 0, 0, 0, 0, 0, 26, 0, 0, 0, 0]))
).toThrow("Wrong magic number");
// Wrong chain
governanceHeader = decodeHeader(
Buffer.from([80, 84, 71, 77, 0, 0, 255, 255, 0, 0, 0, 0])
);
expect(governanceHeader).toBeUndefined();
expect(() =>
decodeHeader(Buffer.from([80, 84, 71, 77, 0, 0, 255, 255, 0, 0, 0, 0]))
).toThrow("Chain Id not found");
// Wrong module/action combination
governanceHeader = decodeHeader(
Buffer.from([80, 84, 71, 77, 0, 1, 0, 26, 0, 0, 0, 0])
);
expect(governanceHeader).toBeUndefined();
expect(() =>
decodeHeader(Buffer.from([80, 84, 71, 77, 0, 1, 0, 26, 0, 0, 0, 0]))
).toThrow("Invalid header, action doesn't match module");
// Decode executePostVaa with empty instructions
let expectedExecuteVaaArgs = {

View File

@ -83,17 +83,11 @@ export type ExecutePostedVaaArgs = {
};
/** Decode ExecutePostedVaaArgs and return undefined if it failed */
export function decodeExecutePostedVaa(
data: Buffer
): ExecutePostedVaaArgs | undefined {
export function decodeExecutePostedVaa(data: Buffer): ExecutePostedVaaArgs {
let deserialized = executePostedVaaLayout.decode(data);
let header = verifyHeader(deserialized.header);
if (!header) {
return undefined;
}
let instructions: TransactionInstruction[] = deserialized.instructions.map(
(ix) => {
let programId: PublicKey = new PublicKey(ix.programId);

View File

@ -75,7 +75,7 @@ export function governanceHeaderLayout(): BufferLayout.Structure<
}
/** Decode Pyth Governance Header and return undefined if the header is invalid */
export function decodeHeader(data: Buffer): PythGovernanceHeader | undefined {
export function decodeHeader(data: Buffer): PythGovernanceHeader {
let deserialized = governanceHeaderLayout().decode(data);
return verifyHeader(deserialized);
}
@ -111,27 +111,23 @@ export function verifyHeader(
action: number;
chain: ChainId;
}>
) {
): PythGovernanceHeader {
if (deserialized.magicNumber !== MAGIC_NUMBER) {
return undefined;
throw new Error("Wrong magic number");
}
if (!toChainName(deserialized.chain)) {
return undefined;
throw new Error("Chain Id not found");
}
try {
let governanceHeader: PythGovernanceHeader = {
targetChainId: toChainName(deserialized.chain),
action: toActionName({
actionId: deserialized.action,
moduleId: deserialized.module,
}),
};
return governanceHeader;
} catch {
return undefined;
}
let governanceHeader: PythGovernanceHeader = {
targetChainId: toChainName(deserialized.chain),
action: toActionName({
actionId: deserialized.action,
moduleId: deserialized.module,
}),
};
return governanceHeader;
}
export { decodeExecutePostedVaa } from "./ExecutePostedVaa";