From d110ffee4688aa493d91a0d21320f809b6f17417 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 4 Jun 2025 18:26:07 +0300 Subject: [PATCH] wip --- .../constructTx/prepareInThreadTxs.ts | 32 +++++++++++-------- .../cwait/src/onchain/features/cwait/cwait.ts | 30 ++++------------- .../cwait/src/onchain/runtime/awaitedTasks.ts | 13 -------- .../cwait/src/onchain/runtime/resolvedOps.ts | 6 +++- 4 files changed, 30 insertions(+), 51 deletions(-) diff --git a/packages/cwait/src/onchain/executor/constructTx/prepareInThreadTxs.ts b/packages/cwait/src/onchain/executor/constructTx/prepareInThreadTxs.ts index 6c3c5b8..af304d7 100644 --- a/packages/cwait/src/onchain/executor/constructTx/prepareInThreadTxs.ts +++ b/packages/cwait/src/onchain/executor/constructTx/prepareInThreadTxs.ts @@ -11,7 +11,7 @@ import { } from '@coinweb/contract-kit'; import { CwebTake } from '@coinweb/contract-kit/dist/types/operations/take'; -import { ExecutorMethodArgs, PreparedExtendedStoreOp, PreparedOp, ResolvedOp, ResolvedSlotOp } from '../../../types'; +import { ExecutorMethodArgs, PreparedExtendedStoreOp, PreparedOp, ResolvedSlotOp } from '../../../types'; import { constructFundsClaimRangRead, constructFundsClaimStore } from '../../claims/funds'; import { constructResultBlockFilter, constructResultClaimTake, resultKey } from '../../claims/result'; import { context, getRawContext } from '../../context'; @@ -68,16 +68,18 @@ export const prepareInThreadTxs = ({ let callsPrepared = 0; const resolvedSlotOps = new Array(outThreadTasksCount).fill({ SlotOp: { ok: true } }) satisfies ResolvedSlotOp[]; + //Mutex exec ops const preparedExecOps: (PreparedExtendedStoreOp | GTake)[] = []; const excOpsIndexes: number[] = []; - const resolvedChildOps: ResolvedOp[] = [...context.ops, ...resolvedSlotOps]; + // const resolvedChildOps: ResolvedOp[] = [...context.ops, ...resolvedSlotOps]; + //Children //Arg for the main call const callArgs: PreparedOperation[] = []; - //Info for separate child call - const childCalls: FullCallInfo[] = []; + //Info for separate parallel calls + const parallelCalls: FullCallInfo[] = []; const outThreadOps: PreparedOperation[] = []; @@ -89,14 +91,20 @@ export const prepareInThreadTxs = ({ callArgs.push(constructBlock([constructResultBlockFilter(id)]), constructResultClaimTake(id)); txFee += 200n; - childCalls.push({ + const childOps = [ + ...context.ops, + ...resolvedSlotOps, + ...ops.map((_, j) => (i === j ? { ExecOp: { id } } : { SlotOp: { ok: true } })), + ]; + + parallelCalls.push({ callInfo: { ref: constructContractRef(context.issuer, []), methodInfo: { methodName: context.methodName, methodArgs: [ context.initialArgs, - [...resolvedChildOps, { ExecOp: { id } }], + childOps, context.user, id, context.thisId, @@ -151,7 +159,7 @@ export const prepareInThreadTxs = ({ processId: context.thisId, }); - childCalls.push({ callInfo }); + parallelCalls.push({ callInfo }); callArgs.push(...inThreadOps); txFee += fee; @@ -168,7 +176,7 @@ export const prepareInThreadTxs = ({ true ); - childCalls.push({ callInfo }); + parallelCalls.push({ callInfo }); callArgs.push(...ops); txFee += fee; @@ -178,8 +186,6 @@ export const prepareInThreadTxs = ({ callArgs.push(op); txFee += 100n; } - - resolvedChildOps.push({ SlotOp: { ok: true } }); }); if (preparedExecOps.length > 0) { @@ -192,7 +198,7 @@ export const prepareInThreadTxs = ({ execId, }); - childCalls.push({ callInfo }); + parallelCalls.push({ callInfo }); txFee += fee; console.log(txFee); @@ -276,8 +282,8 @@ export const prepareInThreadTxs = ({ ) ); - if (childCalls.length || outThreadOps.length) { - returnTxs.push(constructContinueTx(getRawContext(), outThreadOps, childCalls)); + if (parallelCalls.length || outThreadOps.length) { + returnTxs.push(constructContinueTx(getRawContext(), outThreadOps, parallelCalls)); } } } diff --git a/packages/cwait/src/onchain/features/cwait/cwait.ts b/packages/cwait/src/onchain/features/cwait/cwait.ts index d24bec3..6fe51a5 100644 --- a/packages/cwait/src/onchain/features/cwait/cwait.ts +++ b/packages/cwait/src/onchain/features/cwait/cwait.ts @@ -3,19 +3,8 @@ import { constructStore } from '@coinweb/contract-kit/dist/esm/operations/store' import { constructResultClaim } from '../../claims/result'; import { opMarker } from '../../globals/promise'; import { setNextExec, stopExecution } from '../../runtime'; -import { - freezeAwaitedTasks, - getAwaitedTasksCount, - pushAwaitedTask, - unfreezeAwaitedTasks, -} from '../../runtime/awaitedTasks'; -import { - freezeResolvedOps, - getUsedOps, - saveUsedOps, - shiftResolvedOp, - unfreezeResolvedOps, -} from '../../runtime/resolvedOps'; +import { getAwaitedTasksCount, pushAwaitedTask } from '../../runtime/awaitedTasks'; +import { getUsedOps, startSavingUsedOps, stopSavingUsedOps, shiftResolvedOp } from '../../runtime/resolvedOps'; import { isResolvedChildOp, isResolvedExecOp, isResolvedSlotOp } from '../../utils'; import { uuid } from '../../utils'; @@ -55,28 +44,21 @@ export const cwait = Promise { - unfreezeAwaitedTasks(); - unfreezeResolvedOps(); - - saveUsedOps(); - + startSavingUsedOps(); await asyncCallback(...args); + stopSavingUsedOps(); if (!getAwaitedTasksCount()) { console.log('push result claim'); pushAwaitedTask(constructStore(constructResultClaim(op.ExecOp.id, getUsedOps()))); - stopExecution(); } }); - stopExecution(); + stopExecution(); //Check: maybe does no affect - return new Promise(() => null); + return; } if (isResolvedChildOp(op)) { diff --git a/packages/cwait/src/onchain/runtime/awaitedTasks.ts b/packages/cwait/src/onchain/runtime/awaitedTasks.ts index d848ccc..f2b3934 100644 --- a/packages/cwait/src/onchain/runtime/awaitedTasks.ts +++ b/packages/cwait/src/onchain/runtime/awaitedTasks.ts @@ -1,13 +1,8 @@ import { PreparedOp, Task } from '../../types'; const awaitedTasks: Task[] = []; -let isFreezed = false; export const pushAwaitedTask = (op: PreparedOp) => { - if (isFreezed) { - return; - } - awaitedTasks.push({ op, batchId: -1 }); }; @@ -20,11 +15,3 @@ export const markTaskBatch = (count: number, batchId: number) => { }; export const getAwaitedTasksCount = () => awaitedTasks.length; - -export const freezeAwaitedTasks = () => { - isFreezed = true; -}; - -export const unfreezeAwaitedTasks = () => { - isFreezed = false; -}; diff --git a/packages/cwait/src/onchain/runtime/resolvedOps.ts b/packages/cwait/src/onchain/runtime/resolvedOps.ts index 189314f..96db33c 100644 --- a/packages/cwait/src/onchain/runtime/resolvedOps.ts +++ b/packages/cwait/src/onchain/runtime/resolvedOps.ts @@ -43,11 +43,15 @@ export const shiftResolvedOp = () => { export const getUsedOps = () => usedOps; -export const saveUsedOps = () => { +export const startSavingUsedOps = () => { usedOps = []; isSavingUsed = true; }; +export const stopSavingUsedOps = () => { + isSavingUsed = false; +}; + export const freezeResolvedOps = () => { isFreezed = true; };