Skip to content

fix: charge the function body to the operation cost on OP_DEFINE (workaround for bitauth/libauth#166) - #488

Open
mr-zwets wants to merge 2 commits into
masterfrom
fix/libauth-op-define-cost
Open

mr-zwets wants to merge 2 commits into
masterfrom
fix/libauth-op-define-cost

Conversation

@mr-zwets

@mr-zwets mr-zwets commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Problem

libauth's BCH 2026 VM does not add the length of the function body to the operation cost when OP_DEFINE runs, while BCHN does (bitauth/libauth#166). cashc emits OP_DEFINE for every function or global constant that is not inlined, and the SDK evaluates everything through libauth, so for those contracts:

  • getVmResourceUsage() under-reports operationCost by the total length of the defined bodies.
  • debug() and the MockNetworkProvider accept transactions that the network rejects for exceeding the op-cost budget.

The latest libauth prerelease (3.1.0-next.8) is from September 2025 and the upstream issue has no maintainer response, so this fixes it in the SDK. See #481 for the full analysis.

Change

createVirtualMachine in libauth-template/utils.ts is the single place where the SDK creates a VM. For BCH_2026_05 and BCH_SPEC it now builds the VM from libauth's instruction set with OP_DEFINE wrapped: when a function is defined, the wrapper adds the body length to metrics.stackPushedBytes, which libauth turns into operation cost and checks against the budget after each instruction. Only public libauth API is used. BCH_2023_05 and BCH_2025_05 are untouched, because they have no OP_DEFINE.

All three packages depend on ^3.1.0-next.8, so a user can get a fixed libauth without a new CashScript release. To keep the cost from being counted twice in that case, the wrapper only adds the part of the body that the wrapped OP_DEFINE did not already add to stackPushedBytes.

Known limit: this recognises an upstream fix that charges through stackPushedBytes inside OP_DEFINE, which is what the CHIP describes and what bitauth/libauth#166 suggests. A fix that charges through another metric, or outside the operation, would be counted twice. The "is still needed" test below fails on any upstream fix, so a libauth bump in this repo cannot pass unnoticed.

Behaviour change

Reported op-cost goes up for contracts with non-inlined functions or constants. A transaction close to its budget that passed debug() or the MockNetworkProvider before can now fail, as it would on a node. Compiled bytecode is unchanged.

Tests

Direct VM tests in the new test/libauth-template/virtual-machine.test.ts, for BCH_2026_05 and BCH_SPEC:

  • <body> <0> OP_DEFINE <1> costs 401 + 2 × body length.
  • An OP_DEFINE in a branch that is not executed is not charged, with a body and identifier on the stack.
  • On an instruction set whose OP_DEFINE already charges the body, the wrapper adds nothing.
  • Stock libauth still under-charges by the body length. This one fails once libauth is fixed, and says the workaround and the test can then be removed.

SDK tests in test/debugging.test.ts (VM Resources):

  • In a debug() trace of a compiled contract, the OP_DEFINE step costs 100 + body length.
  • For a contract with a 1,006-byte function body, the smallest padding that fits the budget is found. With one byte less, debug() rejects the transaction and so does MockNetworkProvider.sendRawTransaction(), and the measured usage shows it would have fit without the body charge.

Each part of the wrapper was checked by breaking it: with no charge 4 tests fail, with the charge applied to skipped branches 2 fail, with the already-charged bytes ignored 1 fails, and with the MockNetworkProvider on a stock VM 1 fails. The full test suite, lint and spellcheck pass.

Release notes

Not changed in this PR, because the docs deploy from master. Proposed entries for the next release (v0.14.1), including the other SDK fix merged since v0.14.0:

## v0.14.1

#### CashScript SDK
- :bug: Fix bug where the operation cost of defining a function (`OP_DEFINE`) was under-reported by the length of the function body. `getVmResourceUsage()`, `debug()` and the `MockNetworkProvider` now charge the function body like BCHN does, so a transaction of a contract with non-inlined functions that is close to its op-cost budget can now fail local evaluation.
- :bug: Fix bug where `randomUtxo()`, `randomToken()` and `randomNFT()` could generate duplicate transaction ids and token categories, because they were derived from only 10,000 possible values.

Not included

Part of #481.

🤖 Generated with Claude Code

@vercel

vercel Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
cashscript Ready Ready Preview Dec 3, 2026 11:13am UTC

Request Review

@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Pull request stats

Source Tests Total Net Share
SDK (cashscript) +53 −4 +196 −1 +249 −5 +244 100%
Total +53 −4 +196 −1 +249 −5 +244 100%

Reviewable churn: 254 lines (net +244), version bumps and generated files excluded.
Test lines per line of source: 3.46.
Comments: 7 of 47 added source lines, 15%.

New files: 1

  • packages/cashscript/test/libauth-template/virtual-machine.test.ts

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

@codecov

codecov Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.28%. Comparing base (f33fb37) to head (bf143af).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #488      +/-   ##
==========================================
+ Coverage   89.25%   89.28%   +0.02%     
==========================================
  Files          61       62       +1     
  Lines        5082     5096      +14     
  Branches      950      952       +2     
==========================================
+ Hits         4536     4550      +14     
  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.

libauth's BCH 2026 VM does not add the function body length to the
operation cost on OP_DEFINE, while BCHN does (bitauth/libauth#166). The
SDK now wraps OP_DEFINE when it creates the BCH_2026_05 and BCH_SPEC
VMs, so getVmResourceUsage(), debug() and the MockNetworkProvider charge
the body too. Only the part libauth did not charge itself is added, so a
fixed libauth is not counted twice.

Part of #481

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
virtual-machine.ts exports drop-in replacements for libauth's
createVirtualMachineBch2026 and createVirtualMachineBchSpec that charge
the function body on OP_DEFINE, so createVirtualMachine in utils.ts only
changes where it imports them from.

This branch was successfully deployed

1 active deployment
Preview — bf143afe Deployed Dec 3, 2026 by vercel[bot]
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