Skip to content

Commit

Permalink
fix: can't calculate gas when program start with label (#547)
Browse files Browse the repository at this point in the history
  • Loading branch information
vuvoth authored Dec 27, 2021
1 parent 22aad31 commit 510be6a
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/runtime/src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
4 changes: 4 additions & 0 deletions packages/runtime/test/fixtures/basic-teal/assets/clear.teal
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma version 5
// default clear program
int 1
return
Original file line number Diff line number Diff line change
@@ -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
47 changes: 47 additions & 0 deletions packages/runtime/test/integration/gas-calculator.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 510be6a

Please sign in to comment.