Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Sep 28, 2022
1 parent e260793 commit 0046932
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 5 deletions.
16 changes: 15 additions & 1 deletion packages/network/src/createTxQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export function createTxQueue<C extends Contracts>(

const populatedTx = await member(...argsWithoutOverrides, configOverrides);
populatedTx.nonce = nonce;
populatedTx.chainId = await (await target.provider.getNetwork()).chainId;
const signedTx = await target.signer.signTransaction(populatedTx);
const hash = await target.provider.perform("sendTransaction", { signedTransaction: signedTx });
resolve({ hash });
Expand Down Expand Up @@ -140,25 +141,35 @@ export function createTxQueue<C extends Contracts>(
// Increase utilization to prevent executing more tx than allowed by capacity
utilization++;

// Start processing another request from the queue
// Note: we start processing again after increasing the utilization to process up to `concurrency` tx request in parallel.
// Then we call process queue again after decreasing the utilization to trigger waiting tx requests.
processQueue();

// Run exclusive to avoid two tx requests awaiting the nonce in parallel and submitting with the same nonce.
console.log("txqueue waiting to submit", txRequest);
const txResult = await submissionMutex.runExclusive(async () => {
console.log("inside mutex", txRequest);
// Define variables in scope visible to finally block
let error: any;
const stateMutability = txRequest.stateMutability;

// Await gas estimation to avoid increasing nonce before tx is actually sent
let gasLimit: BigNumberish;
try {
console.log("txqueue estimate gas", txRequest);
gasLimit = await txRequest.estimateGas();
} catch (e) {
console.error("GAS ESTIMATION ERROR", e);
return txRequest.cancel();
}

// Wait if nonce is not ready
console.log("txqueue waiting ready state", txRequest);
const { nonce } = await awaitValue(readyState);

try {
console.log("txqueue awaiting execute", txRequest);
return await txRequest.execute(nonce, gasLimit);
} catch (e: any) {
console.warn("TXQUEUE EXECUTION FAILED", e);
Expand All @@ -181,11 +192,12 @@ export function createTxQueue<C extends Contracts>(
if (shouldResetNonce) await resetNonce();
}
});
console.log("txqueue await confirmation", txRequest);

// Await confirmation
if (txResult?.hash) {
try {
// await txResult.wait();
await txResult.wait();
} catch (e) {
console.warn("tx failed in block", e);

Expand Down Expand Up @@ -213,6 +225,8 @@ export function createTxQueue<C extends Contracts>(
}
}

console.log("txqueue done", txRequest);

utilization--;

// Check if there are any transactions waiting to be processed
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/services/protobuf/go/ecs-stream/ecs-stream.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,21 @@ export function createActionSystem<M = undefined>(world: World, txReduced$: Obse

try {
// Execute the action
console.log("txqueue Actionqueue awaiting execution");
const tx = await action.execute(requirementResult);

// If the result includes a hash key (single tx) or hashes (multiple tx) key, wait for the transactions to complete before removing the pending actions
//
console.log("txqueue Actionqueue awaiting events");
if (tx) {
// Wait for all tx events to be reduced
updateComponent(Action, action.entityIndex, { state: ActionState.WaitingForTxEvents });
await awaitStreamValue(txReduced$, (v) => v === tx.hash);
updateComponent(Action, action.entityIndex, { state: ActionState.TxReduced });
}

console.log("txqueue Actionqueue done");

updateComponent(Action, action.entityIndex, { state: ActionState.Complete });
} catch (e) {
handleError(action);
Expand Down

0 comments on commit 0046932

Please sign in to comment.