Skip to content

fix: use random bytes for randomUtxo/randomToken/randomNFT ids (fixes… - #476

Open
AhmadReza6610 wants to merge 1 commit into
CashScript:masterfrom
AhmadReza6610:fix/random-test-utxo-collisions
Open

AhmadReza6610 wants to merge 1 commit into
CashScript:masterfrom
AhmadReza6610:fix/random-test-utxo-collisions

Conversation

@AhmadReza6610

@AhmadReza6610 AhmadReza6610 commented Oct 25, 2026 •

Copy link
Copy Markdown

Fixes #471

randomUtxo, randomToken and randomNFT derived txids, token categories and NFT commitments from randomInt(), which has only 10,000 values, so tests with many random UTXOs or tokens got shared outpoints and categories.

As proposed in the issue, they now use crypto.getRandomValues: 32 bytes for txids and categories, 4 bytes for NFT commitments (the same length as before). Satoshi and token amounts still use randomInt(). The API is unchanged and defaults still override every field. sha256 and bigIntToVmNumber are no longer used in utils.ts, so their imports are removed.

Tests (packages/cashscript/test/util.test.ts, new random test helpers block):

  • 2,000 random UTXOs have distinct outpoints, and 2,000 random tokens and NFTs have distinct categories (with the old helpers these fail every run).
  • Txids and categories are 32-byte hex, and commitments are 4-byte hex.
  • Defaults still override the random fields.

I checked that the three uniqueness tests fail on master and pass with this change. The cashscript package tests and lint pass.

The pr-stats bot notes there's no release-notes entry. Since v0.14.0 is already released, I didn't want to guess the next version heading. Happy to add a line under whichever heading you prefer, e.g. ":bug: Fix bug where randomUtxo(), randomToken() and randomNFT() could return colliding txids, token categories and NFT commitments."

…ashScript#471)

randomUtxo, randomToken and randomNFT derived txids, token categories and
NFT commitments from randomInt(), which has only 10,000 values, so tests
with many random UTXOs or tokens got shared outpoints and categories.
Use crypto.getRandomValues instead: 32 bytes for txids and categories,
4 bytes for NFT commitments. Amounts still use randomInt().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vercel

vercel Bot commented Oct 25, 2026

Copy link
Copy Markdown

@AhmadReza6610 is attempting to deploy a commit to the Kalis Software Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions

Copy link
Copy Markdown

Pull request stats

Source Tests Total Net Share
SDK (cashscript) +8 −6 +41 −0 +49 −6 +43 100%
Total +8 −6 +41 −0 +49 −6 +43 100%

Reviewable churn: 55 lines (net +43), version bumps and generated files excluded.
Test lines per line of source: 2.93.
Comments: 2 of 7 added source lines, 29%.

Package source changed without an update to website/docs/releases/release-notes.md.

@codecov

codecov Bot commented Oct 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.25%. Comparing base (5f0eaff) to head (250e1d5).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #476   +/-   ##
=======================================
  Coverage   89.25%   89.25%           
=======================================
  Files          61       61           
  Lines        5081     5082    +1     
  Branches      950      950           
=======================================
+ Hits         4535     4536    +1     
  Misses        421      421           
  Partials      125      125           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This branch has not been deployed

No deployments
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.

randomUtxo / randomToken / randomNFT draw from only 10,000 values, so larger test setups get colliding outpoints and token categories

1 participant