From 84c4377baffc90af1025293db026a726298b5f3d Mon Sep 17 00:00:00 2001 From: Vu Vo Date: Fri, 24 Dec 2021 22:55:39 +0700 Subject: [PATCH] fixbug can't calculate gas when program start with label --- packages/runtime/src/parser/parser.ts | 1 + .../fixtures/basic-teal/assets/clear.teal | 4 ++ .../basic-teal/assets/label-first-line.teal | 26 ++++++++++ .../test/integration/gas-calculator.ts | 47 +++++++++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 packages/runtime/test/fixtures/basic-teal/assets/clear.teal create mode 100644 packages/runtime/test/fixtures/basic-teal/assets/label-first-line.teal create mode 100644 packages/runtime/test/integration/gas-calculator.ts diff --git a/packages/runtime/src/parser/parser.ts b/packages/runtime/src/parser/parser.ts index 723f2048f..2038c66cd 100644 --- a/packages/runtime/src/parser/parser.ts +++ b/packages/runtime/src/parser/parser.ts @@ -387,6 +387,7 @@ export function opcodeFromSentence (words: string[], counter: number, interprete if (opCodeMap[tealVersion][opCode.slice(0, opCode.length - 1)] !== undefined) { throw new RuntimeError(RUNTIME_ERRORS.TEAL.INVALID_LABEL, { line: counter }); // eg. `int:` is invalid label as `int` is an opcode } + interpreter.lineToCost[counter] = 0; return new Label([opCode], counter); } diff --git a/packages/runtime/test/fixtures/basic-teal/assets/clear.teal b/packages/runtime/test/fixtures/basic-teal/assets/clear.teal new file mode 100644 index 000000000..7c7e18ebc --- /dev/null +++ b/packages/runtime/test/fixtures/basic-teal/assets/clear.teal @@ -0,0 +1,4 @@ +#pragma version 5 +// default clear program +int 1 +return \ No newline at end of file diff --git a/packages/runtime/test/fixtures/basic-teal/assets/label-first-line.teal b/packages/runtime/test/fixtures/basic-teal/assets/label-first-line.teal new file mode 100644 index 000000000..1b7125234 --- /dev/null +++ b/packages/runtime/test/fixtures/basic-teal/assets/label-first-line.teal @@ -0,0 +1,26 @@ +#pragma version 5 + +txn ApplicationID +bz creation + +loop: +load 0 +int 10 +< +bz break + +load 0 +int 1 ++ +store 0 + +b loop + +break: + +int 1 +return + +creation: +int 1 +return \ No newline at end of file diff --git a/packages/runtime/test/integration/gas-calculator.ts b/packages/runtime/test/integration/gas-calculator.ts new file mode 100644 index 000000000..348c20047 --- /dev/null +++ b/packages/runtime/test/integration/gas-calculator.ts @@ -0,0 +1,47 @@ +import { types as rtypes } from '@algo-builder/runtime'; +import { types } from "@algo-builder/web"; +import { expect } from "chai"; + +import { AccountStore, Runtime } from "../../src/index"; +import { AppDeploymentFlags } from "../../src/types"; +import { useFixture } from "../helpers/integration"; + +describe("TEALv5: Pooled Opcode Cost calculation", function () { + useFixture("basic-teal"); + const john = new AccountStore(10e6); + + let runtime: Runtime; + let approvalProgramFileName: string; + let clearProgramFileName: string; + let flags: AppDeploymentFlags; + let appID: number; + let appCallParam: types.AppCallsParam; + this.beforeAll(async function () { + runtime = new Runtime([john]); // setup test + approvalProgramFileName = 'label-first-line.teal'; + clearProgramFileName = 'clear.teal'; + + flags = { + sender: john.account, + globalBytes: 1, + globalInts: 1, + localBytes: 1, + localInts: 1 + }; + + appID = runtime.deployApp(approvalProgramFileName, clearProgramFileName, flags, {}).appID; + + appCallParam = { + type: types.TransactionType.CallApp, + sign: types.SignType.SecretKey, + fromAccount: john.account, + appID: appID, + payFlags: { totalFee: 1000 } + }; + }); + + it("Gas should be number", function () { + const receipt = runtime.executeTx(appCallParam) as rtypes.TxReceipt; + expect(receipt.gas).to.equal(98); + }); +});