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'; } from '@coinweb/contract-kit';
import { CwebTake } from '@coinweb/contract-kit/dist/types/operations/take'; 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 { constructFundsClaimRangRead, constructFundsClaimStore } from '../../claims/funds';
import { constructResultBlockFilter, constructResultClaimTake, resultKey } from '../../claims/result'; import { constructResultBlockFilter, constructResultClaimTake, resultKey } from '../../claims/result';
import { context, getRawContext } from '../../context'; import { context, getRawContext } from '../../context';
@ -68,16 +68,18 @@ export const prepareInThreadTxs = ({
let callsPrepared = 0; let callsPrepared = 0;
const resolvedSlotOps = new Array(outThreadTasksCount).fill({ SlotOp: { ok: true } }) satisfies ResolvedSlotOp[]; const resolvedSlotOps = new Array(outThreadTasksCount).fill({ SlotOp: { ok: true } }) satisfies ResolvedSlotOp[];
//Mutex exec ops
const preparedExecOps: (PreparedExtendedStoreOp | GTake<CwebTake>)[] = []; const preparedExecOps: (PreparedExtendedStoreOp | GTake<CwebTake>)[] = [];
const excOpsIndexes: number[] = []; const excOpsIndexes: number[] = [];
const resolvedChildOps: ResolvedOp[] = [...context.ops, ...resolvedSlotOps]; // const resolvedChildOps: ResolvedOp[] = [...context.ops, ...resolvedSlotOps];
//Children
//Arg for the main call //Arg for the main call
const callArgs: PreparedOperation[] = []; const callArgs: PreparedOperation[] = [];
//Info for separate child call //Info for separate parallel calls
const childCalls: FullCallInfo[] = []; const parallelCalls: FullCallInfo[] = [];
const outThreadOps: PreparedOperation[] = []; const outThreadOps: PreparedOperation[] = [];
@ -89,14 +91,20 @@ export const prepareInThreadTxs = ({
callArgs.push(constructBlock([constructResultBlockFilter(id)]), constructResultClaimTake(id)); callArgs.push(constructBlock([constructResultBlockFilter(id)]), constructResultClaimTake(id));
txFee += 200n; txFee += 200n;
childCalls.push({ const childOps = [
...context.ops,
...resolvedSlotOps,
...ops.map((_, j) => (i === j ? { ExecOp: { id } } : { SlotOp: { ok: true } })),
];
parallelCalls.push({
callInfo: { callInfo: {
ref: constructContractRef(context.issuer, []), ref: constructContractRef(context.issuer, []),
methodInfo: { methodInfo: {
methodName: context.methodName, methodName: context.methodName,
methodArgs: [ methodArgs: [
context.initialArgs, context.initialArgs,
[...resolvedChildOps, { ExecOp: { id } }], childOps,
context.user, context.user,
id, id,
context.thisId, context.thisId,
@ -151,7 +159,7 @@ export const prepareInThreadTxs = ({
processId: context.thisId, processId: context.thisId,
}); });
childCalls.push({ callInfo }); parallelCalls.push({ callInfo });
callArgs.push(...inThreadOps); callArgs.push(...inThreadOps);
txFee += fee; txFee += fee;
@ -168,7 +176,7 @@ export const prepareInThreadTxs = ({
true true
); );
childCalls.push({ callInfo }); parallelCalls.push({ callInfo });
callArgs.push(...ops); callArgs.push(...ops);
txFee += fee; txFee += fee;
@ -178,8 +186,6 @@ export const prepareInThreadTxs = ({
callArgs.push(op); callArgs.push(op);
txFee += 100n; txFee += 100n;
} }
resolvedChildOps.push({ SlotOp: { ok: true } });
}); });
if (preparedExecOps.length > 0) { if (preparedExecOps.length > 0) {
@ -192,7 +198,7 @@ export const prepareInThreadTxs = ({
execId, execId,
}); });
childCalls.push({ callInfo }); parallelCalls.push({ callInfo });
txFee += fee; txFee += fee;
console.log(txFee); console.log(txFee);
@ -276,8 +282,8 @@ export const prepareInThreadTxs = ({
) )
); );
if (childCalls.length || outThreadOps.length) { if (parallelCalls.length || outThreadOps.length) {
returnTxs.push(constructContinueTx(getRawContext(), outThreadOps, childCalls)); 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 { constructResultClaim } from '../../claims/result';
import { opMarker } from '../../globals/promise'; import { opMarker } from '../../globals/promise';
import { setNextExec, stopExecution } from '../../runtime'; import { setNextExec, stopExecution } from '../../runtime';
import { import { getAwaitedTasksCount, pushAwaitedTask } from '../../runtime/awaitedTasks';
freezeAwaitedTasks, import { getUsedOps, startSavingUsedOps, stopSavingUsedOps, shiftResolvedOp } from '../../runtime/resolvedOps';
getAwaitedTasksCount,
pushAwaitedTask,
unfreezeAwaitedTasks,
} from '../../runtime/awaitedTasks';
import {
freezeResolvedOps,
getUsedOps,
saveUsedOps,
shiftResolvedOp,
unfreezeResolvedOps,
} from '../../runtime/resolvedOps';
import { isResolvedChildOp, isResolvedExecOp, isResolvedSlotOp } from '../../utils'; import { isResolvedChildOp, isResolvedExecOp, isResolvedSlotOp } from '../../utils';
import { uuid } from '../../utils'; import { uuid } from '../../utils';
@ -55,28 +44,21 @@ export const cwait = <TAsyncCallback extends (...args: any[]) => Promise<unknown
} }
if (isResolvedExecOp(op)) { if (isResolvedExecOp(op)) {
freezeAwaitedTasks();
freezeResolvedOps();
setNextExec(async () => { setNextExec(async () => {
unfreezeAwaitedTasks(); startSavingUsedOps();
unfreezeResolvedOps();
saveUsedOps();
await asyncCallback(...args); await asyncCallback(...args);
stopSavingUsedOps();
if (!getAwaitedTasksCount()) { if (!getAwaitedTasksCount()) {
console.log('push result claim'); console.log('push result claim');
pushAwaitedTask(constructStore(constructResultClaim(op.ExecOp.id, getUsedOps()))); pushAwaitedTask(constructStore(constructResultClaim(op.ExecOp.id, getUsedOps())));
stopExecution();
} }
}); });
stopExecution(); stopExecution(); //Check: maybe does no affect
return new Promise(() => null); return;
} }
if (isResolvedChildOp(op)) { if (isResolvedChildOp(op)) {

View File

@ -1,13 +1,8 @@
import { PreparedOp, Task } from '../../types'; import { PreparedOp, Task } from '../../types';
const awaitedTasks: Task[] = []; const awaitedTasks: Task[] = [];
let isFreezed = false;
export const pushAwaitedTask = (op: PreparedOp) => { export const pushAwaitedTask = (op: PreparedOp) => {
if (isFreezed) {
return;
}
awaitedTasks.push({ op, batchId: -1 }); awaitedTasks.push({ op, batchId: -1 });
}; };
@ -20,11 +15,3 @@ export const markTaskBatch = (count: number, batchId: number) => {
}; };
export const getAwaitedTasksCount = () => awaitedTasks.length; 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 getUsedOps = () => usedOps;
export const saveUsedOps = () => { export const startSavingUsedOps = () => {
usedOps = []; usedOps = [];
isSavingUsed = true; isSavingUsed = true;
}; };
export const stopSavingUsedOps = () => {
isSavingUsed = false;
};
export const freezeResolvedOps = () => { export const freezeResolvedOps = () => {
isFreezed = true; isFreezed = true;
}; };