UNPKG

@kstasi/jest-tolk

Version:

<p align="center"> <picture> <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/tonkite/tonkite/main/assets/logo-dark.svg"> <img alt="tonkite logo" src="https://raw.githubusercontent.com/tonkite/tonkite/main/a

81 lines (80 loc) 2.97 kB
"use strict"; /** * Copyright 2024 Scaleton Labs * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.executeFuzzTest = executeFuzzTest; const core_1 = require("@ton/core"); const int_strategy_1 = require("./int-strategy"); const utils_1 = require("./utils"); const slice_strategy_1 = require("./slice-strategy"); const tlb_1 = require("./tlb"); const DEFAULT_RUNS = 10; async function executeFuzzTest(executor, code, data, methodId, annotations, fixtures, argTypes) { const runs = annotations.runs ?? DEFAULT_RUNS; let lastResult = { output: { success: false, error: 'Not run yet', }, logs: '', debugLogs: '', input: [], }; const tlbCode = annotations.fuzzTlb ? (0, tlb_1.extractTLBCode)(annotations.fuzzTlb) : undefined; for (let i = 0; i < runs; i++) { const stack = generateFuzzedStack(argTypes, fixtures, tlbCode); let result = await (0, utils_1.runGetMethodWithDefaults)({ executor, code, data, methodId, unixTime: annotations.unixTime, balance: annotations.balance, stack, gasLimit: annotations.gasLimit, }); lastResult = result; if (result.output.vm_exit_code !== (annotations.exitCode ?? 0)) { break; } } return lastResult; } function generateFuzzedStack(argTypes, fixtures, tlbCode) { const builder = new core_1.TupleBuilder(); argTypes.forEach((argType) => { switch (argType.type) { case 'int': const { bits, signed } = tlbCode ? (0, tlb_1.extractIntFieldFromTLB)(tlbCode, argType.name) : { bits: 256, signed: true }; builder.writeNumber((0, int_strategy_1.generateInt)(bits, signed, fixtures['fixture_' + argType.name])); break; case 'slice': const type = tlbCode ? (0, tlb_1.extractSliceFieldFromTLB)(tlbCode, argType.name) : 'slice'; builder.writeSlice((0, slice_strategy_1.generateSlice)(type, fixtures['fixture_' + argType.name])); break; default: throw new Error(`Unsupported type: ${argType.type}`); } }); return builder.build(); }