This commit is contained in:
Alex 2025-06-04 18:26:07 +03:00
parent 186b719a94
commit d110ffee46
4 changed files with 30 additions and 51 deletions

View File

@ -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<CwebTake>)[] = [];
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));
}
}
}

View File

@ -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 = <TAsyncCallback extends (...args: any[]) => Promise<unknown
}
if (isResolvedExecOp(op)) {
freezeAwaitedTasks();
freezeResolvedOps();
setNextExec(async () => {
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)) {

View File

@ -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;
};

View File

@ -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;
};