Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/Can not issue itxn in clear program #667

Merged
merged 3 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/runtime/src/errors/errors-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,12 @@ maximun uint128`,
title: PARSE_ERROR,
description: `value %exp% is not base64Url`,
},
ISSUE_ITXN_WHEN_CLEAR_PROGRAM: {
number: 1060,
message: "Clear state programs can not issue inner transactions.",
title: "Clear state programs can not issue inner transactions.",
description: "Clear state programs can not issue inner transactions",
},
};

const runtimeGeneralErrors = {
Expand Down
5 changes: 5 additions & 0 deletions packages/runtime/src/interpreter/opcode-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3966,6 +3966,11 @@ export class ITxnBegin extends Op {
});
}

// cannot issue itxn when clear state application.
if (this.interpreter.runtime.ctx.tx.apan === Number(TxOnComplete.ClearState)) {
throw new RuntimeError(RUNTIME_ERRORS.TEAL.ISSUE_ITXN_WHEN_CLEAR_PROGRAM);
}

this.interpreter.currentInnerTxnGroup = [addInnerTransaction(this.interpreter, this.line)];
}
}
Expand Down
30 changes: 26 additions & 4 deletions packages/runtime/test/src/interpreter/inner-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { AccountStore } from "../../../src/account";
import { RUNTIME_ERRORS } from "../../../src/errors/errors-list";
import { Runtime } from "../../../src/index";
import { Interpreter } from "../../../src/interpreter/interpreter";
import { ALGORAND_ACCOUNT_MIN_BALANCE, TransactionTypeEnum } from "../../../src/lib/constants";
import { AccountAddress, AccountStoreI, ExecutionMode } from "../../../src/types";
import { ALGORAND_ACCOUNT_MIN_BALANCE } from "../../../src/lib/constants";
import { AccountAddress, AccountStoreI, ExecutionMode, TxOnComplete } from "../../../src/types";
import { expectRuntimeError } from "../../helpers/runtime-errors";
import { elonMuskAccount, johnAccount } from "../../mocks/account";
import { accInfo } from "../../mocks/stateful";
Expand Down Expand Up @@ -76,10 +76,10 @@ describe("Inner Transactions", function () {
reset();
};

const executeTEAL = (tealCode: string): void => {
const executeTEAL = (tealCode: string, onComplete = TxOnComplete.NoOp): void => {
// reset interpreter
reset();

interpreter.runtime.ctx.tx.apan = Number(onComplete);
interpreter.execute(tealCode, ExecutionMode.APPLICATION, interpreter.runtime, 0);
};

Expand Down Expand Up @@ -285,6 +285,28 @@ describe("Inner Transactions", function () {
);
});

it("should fail: issue itxn when clear state txn", function () {
// increase balance
const acc = interpreter.runtime.ctx.state.accounts.get(appAccAddr);
if (acc) {
acc.amount = BigInt(ALGORAND_ACCOUNT_MIN_BALANCE) * 10n;
}

interpreter.runtime.ctx.tx.apan = Number(TxOnComplete.ClearState);
tealCode = `
itxn_begin
byte "pay"
itxn_field Type
itxn_submit
int 1
`;

expectRuntimeError(
() => executeTEAL(tealCode, TxOnComplete.ClearState),
RUNTIME_ERRORS.TEAL.ISSUE_ITXN_WHEN_CLEAR_PROGRAM
);
});

it(`should pass if app account is funded`, function () {
// increase balance
const acc = interpreter.runtime.ctx.state.accounts.get(appAccAddr);
Expand Down