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

83 lines (82 loc) 2.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.executeFuzzTest = executeFuzzTest; const core_1 = require("@ton/core"); const int_strategy_1 = require("./fuzz/int-strategy"); const tlb_parser_1 = require("@ton-community/tlb-parser"); const tlb_codegen_1 = require("@ton-community/tlb-codegen"); const utils_1 = require("./utils"); const DEFAULT_RUNS = 10; async function executeFuzzTest(executor, code, data, methodId, annotations, fixtures, argTypes) { console.log('Annotations:', annotations); const runs = annotations.runs ?? DEFAULT_RUNS; let lastResult = { output: { success: false, error: 'Not run yet', }, logs: '', debugLogs: '', input: [], }; for (let i = 0; i < runs; i++) { const trees = generateFuzzedTrees(argTypes, fixtures, annotations.fuzzTlb); const stack = getStackFromTrees(argTypes, trees); 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 !== 0) { // TODO: try to simplify the case break; } } return lastResult; } function generateFuzzedTrees(argTypes, fixtures, fuzzTlb) { return argTypes.map((argType) => { if (argType.type === 'int') { const { bits, signed } = fuzzTlb ? extractIntFieldFromTLB(fuzzTlb, argType.name) : { bits: 256, signed: true }; return (0, int_strategy_1.generateIntTree)(bits, signed, fixtures['fixture_' + argType.name]); } throw new Error(`Unsupported type: ${argType.type}`); }); } function getStackFromTrees(argTypes, fuzzedTrees) { const builder = new core_1.TupleBuilder(); argTypes.forEach((argType, index) => { if (argType.type === 'int') { builder.writeNumber(fuzzedTrees[index].current()); } else { throw new Error(`Unsupported type: ${argType}`); } }); return builder.build(); } function extractIntFieldFromTLB(tlb, fieldName) { const tlbTree = (0, tlb_parser_1.ast)(tlb); const tlbCode = (0, tlb_codegen_1.getTLBCodeByAST)(tlbTree, tlb); const field = getFieldFromTLBCode(tlbCode, fieldName); if (field?.fieldType?.kind !== 'TLBNumberType') { throw new Error(`Field ${fieldName} is not of type TLBNumberType`); } return { bits: field.fieldType.maxBits ?? 256, signed: field.fieldType.signed ?? true, }; } function getFieldFromTLBCode(tlbCode, fieldName) { return tlbCode?.types .get('Args') ?.constructors[0].fields.find((field) => field.name === fieldName); }