Repository navigation
Description
Summary
TransactionBuilder.debug() takes time roughly quadratic in the number of inputs. send() on MockNetworkProvider and the toFailRequire / toFailRequireWith matchers run the same path, so passing tests pay it too. Almost none of the time goes to running the VM. Nearly all of it goes to compiling the libauth template from CashAssembly text, once for every input in every per-input scenario.
On a real covenant transaction with about 390 inputs, a single toFailRequire check ran for over 20 minutes before we stopped it. A small patch, described below, makes the trivial benchmark 25× faster at 320 inputs, with identical error reporting.
Reproduction
A trivial contract, require(y == x), spent by N inputs through TransactionBuilder and MockNetworkProvider, with cashscript 0.14.0-next.7 on Node 24:
| inputs | debug() today |
with the patch below |
|---|---|---|
| 20 | 397 ms | 64 ms |
| 40 | 1,138 ms | 123 ms |
| 80 | 3,717 ms | 281 ms |
| 160 | 13,962 ms | 745 ms |
| 320 | 57,170 ms | 2,252 ms |
send() on the mock provider takes about the same time as debug() (4,241 ms at 80 inputs).
Benchmark script
// Trivial.cash: contract Trivial(int x) { function spend(int y) { require(y == x); } }
const provider = new MockNetworkProvider();
const contract = new Contract(artifact, [7n], { provider });
const tx = new TransactionBuilder({ provider });
for (let i = 0; i < n; i++) {
const u = randomUtxo({ satoshis: 10_000n });
u.txid = i.toString(16).padStart(64, '0'); u.vout = 0;
tx.addInput(provider.addUtxo(contract.address, u), contract.unlock.spend(7n));
}
tx.addOutput({ to: contract.address, amount: BigInt(n) * 9_000n });
const t0 = performance.now();
tx.debug();
console.log(n, performance.now() - t0);Root cause
A CPU profile of debug() at 80 inputs, as inclusive time:
createProgram (debugging.ts) |
77.7% |
compiler.generateScenario (libauth) |
71.7% |
compileScript / compileScriptContents (libauth, mostly the parsimmon parser) |
67.9% |
vm.debug (the actual execution) |
6.9% |
debugTemplate evaluates one scenario per input, with that input as the slot. In each scenario, generateTemplateScenarioTransaction and generateTemplateScenarioSourceOutputs describe every other input, and every output, as template script references with parameters (generateTemplateScenarioBytecode, generateTemplateScenarioBytecodeForSourceOutputs, generateTemplateScenarioTransactionOutputLockingBytecode). libauth then compiles each of those from CashAssembly. N scenarios, each compiling N scripts, is O(N²) parsing.
That compilation produces bytes cashscript already has. buildLibauthTransaction() has produced every input's final unlocking bytecode, signatures included, and every source output's locking bytecode is on the input UTXO.
A smaller per-input cost adds to it. createProgram rebuilds the compiler (walletTemplateToCompilerConfiguration and createCompiler) for every input from the same template, and generateTemplateScenarioTransaction recomputes generateLockingBytecodeParamsMapping for every scenario.
Proposed fix
- In the per-input scenarios, emit known bytecode as hex for everything except the slot.
- Non-slot inputs'
unlockingBytecodebecomesbinToHex(libauthInput.unlockingBytecode). - Non-slot source outputs'
lockingBytecodebecomescsInput.lockingBytecode. - Outputs'
lockingBytecodebecomesbinToHex(libauthOutput.lockingBytecode). - libauth accepts hex in scenarios, so each scenario compiles only its slot input, and generating each scenario becomes O(1) compilation.
- Non-slot inputs'
- Build the compiler and VM once per template in
debugTemplate, instead of once per input increateProgram. Also drop the per-scenariogenerateLockingBytecodeParamsMapping, which is unused after (1).
With both changes, the debug result for a failing input in our test is unchanged: the same FailedRequireError, input index, line and failing statement.
One trade-off: in a BitAuth IDE link, the non-slot inputs of each scenario show as raw bytecode instead of named scripts with parameters. If that matters, the readable form could be kept only for the template that getBitauthUri exports, and the hex form used for local debugging.
The remaining growth, about 3× per doubling at the top of the table, comes from each scenario still encoding the whole transaction. A further step would be to skip scenario generation on the happy path: evaluate each input with vm.debug({ inputIndex, sourceOutputs, transaction }) on the already-built transaction, and only build the template when an input fails, to attribute the error and produce the IDE link.
Workaround
We verify large transactions with libauth's createVirtualMachineBch2026(true).verify({ sourceOutputs, transaction }), and use the debug matchers only on small ones.
Activity