This commit is contained in:
Alex 2025-04-25 10:01:30 +03:00
parent eb32dcfe55
commit 5e9b66fe7f
9 changed files with 24 additions and 70 deletions

View File

@ -9,7 +9,6 @@ export const constructAddWordUiCommand = ({ word, contractId }: { word: string;
methodName: PUBLIC_METHODS.ADD_WORD,
methodArgs: [[word] satisfies AddWordArgs],
cost: FEE.ADD_WORD,
withQueue: false,
}),
]);
};

View File

@ -1,5 +1,5 @@
import { constructClaim } from '@coinweb/contract-kit';
import { readOp, storeOp, cwait, queue } from 'cwait';
import { readOp, storeOp, cwait } from 'cwait';
import { TypedClaim } from '../../../lib/dist/shared/types';
import { AddWordArgs, createWordKey, WordClaimBody } from '../offchain/shared';
@ -26,15 +26,13 @@ export const addWord = cwait(async (...[word]: AddWordArgs) => {
console.log('await storeOp');
await storeOp(constructClaim(createWordKey(id), { word }, '0x0'));
await queue(async () => {
console.log('await readOp');
const res = await readOp<TypedClaim<WordClaimBody>>(createWordKey(id));
console.log('await readOp');
const res = await readOp<TypedClaim<WordClaimBody>>(createWordKey(id));
console.log('await storeOp2');
await storeOp(
constructClaim(createWordKey(id + id), { word: (res?.body.word ?? '') + (res?.body.word ?? '') }, '0x0')
);
});
console.log('await storeOp2');
await storeOp(
constructClaim(createWordKey(id + id), { word: (res?.body.word ?? '') + (res?.body.word ?? '') }, '0x0')
);
console.log('await extraLogic');
const wordClaim = await extraLogic(id);

View File

@ -92,7 +92,4 @@ export const context = {
get needSaveResult() {
return getMethodArgs()[6] ?? false;
},
get isQueue() {
return getMethodArgs()[8] ?? false;
},
};

View File

@ -3,4 +3,3 @@ export * from './executor';
export * from './promisifiedOps';
export * from './createContract';
export * from './cwait';
export * from './queue';

View File

@ -1 +0,0 @@
export * from './queue';

View File

@ -1,21 +0,0 @@
const queues = new Set<number>();
let queueId = 0;
export const queue = <TCallback extends () => Promise<void>>(callback: TCallback) => {
const id = queueId++;
queues.add(id);
const promise = new Promise<void>((resolve) => {
callback().finally(() => {
queues.delete(id);
resolve();
});
});
return promise;
};
export const isQueueLocked = () => {
return queues.size > 0;
};

View File

@ -18,7 +18,6 @@ import { ExecutorMethodArgs, ResolvedOp, ResolvedSlotOp, Task } from '../../../t
import { constructFundsClaimRangRead, constructFundsClaimStore } from '../../claims/funds';
import { constructResultBlockFilter, constructResultClaimTake, resultKey } from '../../claims/result';
import { context, getRawContext } from '../../context';
import { isQueueLocked } from '../../queue/queue';
import { isPreparedBlockOp, isPreparedExecOp } from '../typeGuards';
export const prepareInThreadTxs = ({
@ -42,7 +41,7 @@ export const prepareInThreadTxs = ({
const restOfAvailableCweb = availableCweb - outThreadFee;
const restOfCweb = restOfAvailableCweb + storedCweb - BigInt(takeOps.length) * 100n - 3000n;
const restOfCweb = restOfAvailableCweb + storedCweb - BigInt(takeOps.length) * 100n - 3000n - queueCostFee;
const txs =
restOfCweb > 0n
@ -52,8 +51,9 @@ export const prepareInThreadTxs = ({
...takeOps,
...constructSendCweb(restOfCweb, user, null),
]),
...queue.gateway.unlock(getRawContext()),
]
: [];
: queue.gateway.unlock(getRawContext());
return { txs, calls: 0 };
}
@ -84,8 +84,7 @@ export const prepareInThreadTxs = ({
txFee += 200n;
childCalls.push({
callInfo: {
ref: constructContractRef(context.issuer, []),
callInfo: prepareQueueCall(context.issuer, {
methodInfo: {
methodName: context.methodName,
methodArgs: [
@ -96,15 +95,14 @@ export const prepareInThreadTxs = ({
context.thisId,
true,
[],
isQueueLocked(),
] satisfies ExecutorMethodArgs,
},
contractInfo: {
providedCweb: cwebPerCall - 700n,
providedCweb: cwebPerCall - 700n - queueCostFee,
authenticated: null,
},
contractArgs: [],
},
}),
});
callsPrepared++;
@ -156,17 +154,12 @@ export const prepareInThreadTxs = ({
txFee += 800n + outThreadFee + BigInt(takeOps.length) * 100n;
callsPrepared++;
const isNeedToLockQueue = !context.isQueue && isQueueLocked();
const isNeedToUnlockQueue = context.isQueue && !isQueueLocked();
const isNeedToResetQueue = !!childCalls.length;
if (isNeedToLockQueue) {
console.log('isNeedToLockQueue');
txFee += queueCostFee;
}
if (isNeedToUnlockQueue) {
console.log('isNeedToUnlockQueue');
if (isNeedToResetQueue) {
console.log('isNeedToResetQueue');
txFee += queueUnlockFee;
txFee += queueCostFee;
}
let callInfo: PreparedCallInfo = {
@ -181,7 +174,6 @@ export const prepareInThreadTxs = ({
context.parentId,
context.needSaveResult,
takeOps.map((op) => (op.TakeOp.key.second_part as [string])[0]),
isQueueLocked(),
] satisfies ExecutorMethodArgs,
},
contractInfo: {
@ -191,7 +183,7 @@ export const prepareInThreadTxs = ({
contractArgs: [constructFundsClaimRangRead(context.thisId), ...callArgs],
};
if (isNeedToLockQueue) {
if (isNeedToResetQueue) {
callInfo = prepareQueueCall(context.issuer, callInfo);
}
@ -211,7 +203,7 @@ export const prepareInThreadTxs = ({
returnTxs.push(constructContinueTx(getRawContext(), childBlocks, childCalls));
}
if (isNeedToUnlockQueue) {
if (isNeedToResetQueue) {
returnTxs.push(...queue.gateway.unlock(getRawContext()));
}
}

View File

@ -1,14 +1,8 @@
import {
constructContinueTx,
constructContractRef,
FullCallInfo,
NewTx,
PreparedOperation,
} from '@coinweb/contract-kit';
import { constructContinueTx, FullCallInfo, NewTx, PreparedOperation, prepareQueueCall } from '@coinweb/contract-kit';
import { queueCostFee } from 'lib/onchain';
import { ExecutorMethodArgs, Task } from '../../../types';
import { context, getRawContext } from '../../context';
import { isQueueLocked } from '../../queue/queue';
import { isPreparedExecOp } from '../typeGuards';
export const prepareOutThreadTxs = ({
@ -41,8 +35,7 @@ export const prepareOutThreadTxs = ({
const id = task.op.ExecOp.id;
const callInfo = {
callInfo: {
ref: constructContractRef(context.issuer, []),
callInfo: prepareQueueCall(context.issuer, {
methodInfo: {
methodName: context.methodName,
methodArgs: [
@ -53,15 +46,14 @@ export const prepareOutThreadTxs = ({
context.parentId ?? context.thisId,
false,
[],
isQueueLocked(),
] satisfies ExecutorMethodArgs,
},
contractInfo: {
providedCweb: cwebPerCall - 700n,
providedCweb: cwebPerCall - 700n - queueCostFee,
authenticated: null,
},
contractArgs: [],
},
}),
};
if (siblingTxInfo[task.batchId]) {

View File

@ -45,5 +45,4 @@ export type ExecutorMethodArgs = [
parentId?: string,
saveResult?: boolean,
takenFundsIds?: string[],
isQueue?: boolean,
];