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"); expect(governanceHeader?.action).toBe("SetFee");
// Wrong magic number // Wrong magic number
governanceHeader = decodeHeader( expect(() =>
Buffer.from([0, 0, 0, 0, 0, 0, 0, 26, 0, 0, 0, 0]) decodeHeader(Buffer.from([0, 0, 0, 0, 0, 0, 0, 26, 0, 0, 0, 0]))
); ).toThrow("Wrong magic number");
expect(governanceHeader).toBeUndefined();
// Wrong chain // Wrong chain
governanceHeader = decodeHeader( expect(() =>
Buffer.from([80, 84, 71, 77, 0, 0, 255, 255, 0, 0, 0, 0]) decodeHeader(Buffer.from([80, 84, 71, 77, 0, 0, 255, 255, 0, 0, 0, 0]))
); ).toThrow("Chain Id not found");
expect(governanceHeader).toBeUndefined();
// Wrong module/action combination // Wrong module/action combination
governanceHeader = decodeHeader( expect(() =>
Buffer.from([80, 84, 71, 77, 0, 1, 0, 26, 0, 0, 0, 0]) decodeHeader(Buffer.from([80, 84, 71, 77, 0, 1, 0, 26, 0, 0, 0, 0]))
); ).toThrow("Invalid header, action doesn't match module");
expect(governanceHeader).toBeUndefined();
// Decode executePostVaa with empty instructions // Decode executePostVaa with empty instructions
let expectedExecuteVaaArgs = { let expectedExecuteVaaArgs = {

View File

@ -83,17 +83,11 @@ export type ExecutePostedVaaArgs = {
}; };
/** Decode ExecutePostedVaaArgs and return undefined if it failed */ /** Decode ExecutePostedVaaArgs and return undefined if it failed */
export function decodeExecutePostedVaa( export function decodeExecutePostedVaa(data: Buffer): ExecutePostedVaaArgs {
data: Buffer
): ExecutePostedVaaArgs | undefined {
let deserialized = executePostedVaaLayout.decode(data); let deserialized = executePostedVaaLayout.decode(data);
let header = verifyHeader(deserialized.header); let header = verifyHeader(deserialized.header);
if (!header) {
return undefined;
}
let instructions: TransactionInstruction[] = deserialized.instructions.map( let instructions: TransactionInstruction[] = deserialized.instructions.map(
(ix) => { (ix) => {
let programId: PublicKey = new PublicKey(ix.programId); 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 */ /** 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); let deserialized = governanceHeaderLayout().decode(data);
return verifyHeader(deserialized); return verifyHeader(deserialized);
} }
@ -111,27 +111,23 @@ export function verifyHeader(
action: number; action: number;
chain: ChainId; chain: ChainId;
}> }>
) { ): PythGovernanceHeader {
if (deserialized.magicNumber !== MAGIC_NUMBER) { if (deserialized.magicNumber !== MAGIC_NUMBER) {
return undefined; throw new Error("Wrong magic number");
} }
if (!toChainName(deserialized.chain)) { if (!toChainName(deserialized.chain)) {
return undefined; throw new Error("Chain Id not found");
} }
try { let governanceHeader: PythGovernanceHeader = {
let governanceHeader: PythGovernanceHeader = { targetChainId: toChainName(deserialized.chain),
targetChainId: toChainName(deserialized.chain), action: toActionName({
action: toActionName({ actionId: deserialized.action,
actionId: deserialized.action, moduleId: deserialized.module,
moduleId: deserialized.module, }),
}), };
}; return governanceHeader;
return governanceHeader;
} catch {
return undefined;
}
} }
export { decodeExecutePostedVaa } from "./ExecutePostedVaa"; export { decodeExecutePostedVaa } from "./ExecutePostedVaa";