Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust Maestro::getUnspentOutputByNFT flow to prevent race condition. #178

Merged
merged 1 commit into from
Sep 26, 2024

Conversation

clifforous
Copy link
Contributor

Race condition checking the utxos array length while a fetch may still be happening asynchronously highlighed below.

const utxos: TransactionUnspentOutput[] = [];
for (const maestroUTxO of response.data) {
  //...snip
  fetch(`${this.url}${query2}?with_cbor=true`, {
    headers: this.headers(),
  })
    .then((resp) => resp.json())
    .then((json) => {
        //...snip
        utxos.push(new TransactionUnspentOutput(txIn, txOut));
       //...snip
    });
}
// This check can happen before the async fetch in the for loop above completes
if (utxos.length !== 1)  {
  throw new Error(
    "getUnspentOutputByNFT: Expected 1 UTxO, got " + utxos.length,
  );
}

I moved the array length check to before the fetch using the initial response.data array instead which should have the same behavior. I then removed the for loop since it is guaranteed after that check to be a single utxo in the array.

Copy link

changeset-bot bot commented Sep 17, 2024

🦋 Changeset detected

Latest commit: eb28df8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@blaze-cardano/query Patch
@blaze-cardano/emulator Patch
@blaze-cardano/sdk Patch
@blaze-cardano/wallet Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@micahkendall micahkendall merged commit 719b768 into butaneprotocol:main Sep 26, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants