From ab494782594c72c245a977181d3466f20b1eff83 Mon Sep 17 00:00:00 2001 From: aac Date: Sat, 17 Jul 2021 02:56:49 +1000 Subject: [PATCH] ts: Check if execution context stack is empty in event parser (#524) --- .travis.yml | 1 + ts/package.json | 3 ++- ts/src/program/event.ts | 2 +- ts/tests/events.spec.ts | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 ts/tests/events.spec.ts diff --git a/.travis.yml b/.travis.yml index 076e6c3d3..82394b0f4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -44,6 +44,7 @@ jobs: - cargo fmt -- --check - cargo clippy --all-targets -- -D warnings - cargo test + - pushd ts && yarn && yarn test && popd - <<: *examples name: Runs the examples 1 script: diff --git a/ts/package.json b/ts/package.json index 31e585550..23675bff8 100644 --- a/ts/package.json +++ b/ts/package.json @@ -18,7 +18,8 @@ "lint:fix": "prettier src/** -w", "watch": "tsc -p tsconfig.cjs.json --watch", "prepublishOnly": "yarn build", - "docs": "typedoc --excludePrivate --includeVersion --out ../docs/src/.vuepress/dist/ts/ --readme none src/index.ts" + "docs": "typedoc --excludePrivate --includeVersion --out ../docs/src/.vuepress/dist/ts/ --readme none src/index.ts", + "test": "jest tests --detectOpenHandles" }, "dependencies": { "@project-serum/borsh": "^0.2.2", diff --git a/ts/src/program/event.ts b/ts/src/program/event.ts index d9e1be30f..697b2aef2 100644 --- a/ts/src/program/event.ts +++ b/ts/src/program/event.ts @@ -58,7 +58,7 @@ export class EventParser { log: string ): [Event | null, string | null, boolean] { // Executing program is this program. - if (execution.program() === this.programId.toString()) { + if (execution.stack.length > 0 && execution.program() === this.programId.toString()) { return this.handleProgramLog(log); } // Executing program is not this program. diff --git a/ts/tests/events.spec.ts b/ts/tests/events.spec.ts new file mode 100644 index 000000000..c2d5992cb --- /dev/null +++ b/ts/tests/events.spec.ts @@ -0,0 +1,33 @@ +import { PublicKey } from "@solana/web3.js"; +import { EventParser } from "../src/program/event"; +import { Coder } from "../src"; + +describe("Events", () => { + it("Parses multiple instructions", async () => { + const logs = [ + "Program 11111111111111111111111111111111 invoke [1]", + "Program 11111111111111111111111111111111 success", + "Program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54 invoke [1]", + "Program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54 consumed 17867 of 200000 compute units", + "Program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54 success", + ]; + const idl = { + version: "0.0.0", + name: "basic_0", + instructions: [ + { + name: "initialize", + accounts: [], + args: [], + }, + ], + }; + const coder = new Coder(idl); + const programId = PublicKey.default; + const eventParser = new EventParser(coder, programId); + + eventParser.parseLogs(logs, () => { + throw new Error("Should never find logs"); + }); + }); +});