Skip to content

bitcoin-util: Add evalscript subcommand - #35788

Open
ajtowns wants to merge 4 commits into
bitcoin:masterfrom
ajtowns:202607-evalscript
Open

ajtowns wants to merge 4 commits into
bitcoin:masterfrom
ajtowns:202607-evalscript

Conversation

@ajtowns

@ajtowns ajtowns commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Adds an evalscript subcommand for using bitcoin's consensus logic to evaluate script fragments. Intended for debugging and development.

Examples:

# check a script fragment's behaviour
$ bitcoin-util evalscript '1 2 ADD'
{
  "script": {
    "asm": "1 2 OP_ADD",
    "hex": "515293",
    "type": "nonstandard"
  },
  "sigversion": "witness_v0",
  "script_flags": [
    "CHECKLOCKTIMEVERIFY",
    "CHECKSEQUENCEVERIFY",
    "DERSIG",
    "NULLDUMMY",
    "P2SH",
    "TAPROOT",
    "WITNESS"
  ],
  "dummy_sig_checker": true,
  "stack_after": [
    "03"
  ],
  "sigop_cost": 0,
  "success": true
}

# validate a real signature
$ bitcoin-util -sigversion=base \
    -spentoutput=00f2052a0100000043410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac \
    -tx=0100000001c997a5e56e104102fa209c6a852dd90660a20b2d9c352423edce25857fcd3704000000004847304402204e45e16932b8af514961a1d3a1a25fdf3f4f7732e9d624c6c61548ab5fb8cd410220181522ec8eca07de4860a4acdd12909d831cc56cbbac4622082221a8768d1d0901ffffffff0200ca9a3b00000000434104ae1a62fe09c5f51b13905f07f06b99a2f7159b2225f374cd378d71302fa28414e7aab37397f554a7df5f142c21c1b7303b8a0626f1baded5c72a704f7e6cd84cac00286bee0000000043410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac00000000 \
    evalscript '0x410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3 CHECKSIG' \
    '304402204e45e16932b8af514961a1d3a1a25fdf3f4f7732e9d624c6c61548ab5fb8cd410220181522ec8eca07de4860a4acdd12909d831cc56cbbac4622082221a8768d1d0901'
{
  "script": {
    "asm": "0411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3 OP_CHECKSIG",
    "hex": "410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac",
    "type": "pubkey"
  },
  "sigversion": "base",
  "script_flags": [
    "CHECKLOCKTIMEVERIFY",
    "CHECKSEQUENCEVERIFY",
    "DERSIG",
    "NULLDUMMY",
    "P2SH",
    "TAPROOT",
    "WITNESS"
  ],
  "dummy_sig_checker": false,
  "stack_after": [
    "01"
  ],
  "sigop_cost": 4,
  "success": true
}

@DrahtBot

DrahtBot commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
Stale ACK jeanpablojp

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36603 (Improve loadtxoutset performance by ajtowns)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@ajtowns

ajtowns commented Jul 24, 2026

Copy link
Copy Markdown
Contributor Author

Previously included in bitcoin-inquisition (bitcoin-inquisition#58, also bitcoin-inquisition#64 and bitcoin-inquisition#79).

Makes minimal changes to consensus logic, but still has to duplicate a bunch of code to get equivalent behaviour, might be worthwhile simplifying that API in followups, and potentially exposing as a kernel API of some sort?

@jeanpablojp

Copy link
Copy Markdown
Contributor

tACK a64f2ac

Ran tool_utils, the script unit tests and feature_taproot on the head, and both examples from the description reproduce here.

The PR description mentions -tx, but I noticed no case in bitcoin-util-test.json uses -tx, so that path goes uncovered. To confirm, I disabled the -input range check and the suite still passes. Would it be worth adding a case like that?

@ajtowns

ajtowns commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

Rebased, added a test that uses -tx

@fametrano

Copy link
Copy Markdown

Tested 598427f.

I ran the 1214 rows of script_tests.json that evalscript can express, each as a chain of calls (scriptSig, then scriptPubKey, then the redeem or witness script) with -tx and -spentoutput. All 1214 gave the expected result, and so did the 5 tapscript rows. The other 18 rows fail in checks that VerifyScript makes outside EvalScript.

I also ran 10,000 random scripts under each sigversion with NONE, MANDATORY and STANDARD flags, 90,000 runs, and compared them with btclib's interpreter. Success and error matched on all but 246 runs. In those, both sides failed with different error codes, and all come from three btclib bugs. evalscript never crashed.

One trap: a script argument that is valid hex is read as raw bytes. evalscript 16 runs the byte 0x16 and fails with "Opcode missing or not understood", while evalscript '16 ' runs OP_16. Could the help say so, or raw hex need a 0x prefix?

Review with `git diff --color-moved=zebra --color-moved-ws=allow-indentation-change`
@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task tidy: https://github.com/bitcoin/bitcoin/actions/runs/45956875614/job/140365283975
LLM reason (✨ experimental): CI failed because clang-tidy treated r += "," in bitcoin-util.cpp:50 as an error; use the character overload r += ','.

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

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.

4 participants