Skip to content

Describe a too-wide sequence, header version and median time past (closes #3001) (closes #3003) - #3007

Merged
fametrano merged 1 commit into
mainfrom
iss-3003-plain-fstring-int
Nov 9, 2026
Merged

fametrano merged 1 commit into
mainfrom
iss-3003-plain-fstring-int

Conversation

@fametrano

Copy link
Copy Markdown
Member

Three refusals wrote a caller's int through a plain f-string, outside the helpers #3004 changed, so a key passed in the int's place was printed in part or whole. op_checksequenceverify printed the low 16 bits of the input's sequence (relative lock time 65535 > 54713, where 54713 is k & 0xFFFF); invalid version (outdated) printed -k & 0xFFFFFFFF, the low four bytes of a negative scalar; ChainStart printed median_time_past in full above four bytes. Each now writes its value only where it fits its four bytes (a sequence in 0..2**32-1, a version in the signed four-byte range) and otherwise describes it by sign and bit length with _message_text. A value that fits reads as before.

What it breaks: ChainStart(median_time_past=2**32) now reads an int of 33 bits; the other two change only past their four bytes. The census behind #3003 read every f-string field of src/ that is not already routed through a helper (419 fields); the rest are script numbers of at most five bytes, values already checked, sizes, counts, positions and datetimes. The refusals that write a byte of the octets they refuse (script_pub_key.py, var_int.py, utreexo.py) are #3005, a different rule: the byte must be one the format defines. The scalar test now also looks for the low two and four bytes of the scalar, which a 12-character piece cannot match.

Evidence

Check Result
suite, -n 3, coverage floor exit 0, 24667 passed, 103 skipped, 2 xfailed, 100.00%
pre-commit run --all-files exit 0
docs sphinx-build -n -W exit 0
signature, git log --format='%h %G? %GS' 7a0ba29b.. 4e579c50 G
Sign-off script on 7a0ba29b..4e579c50 exit 0 (control exits 1)
9 mutants (always masked, never masked, mask dropped, boundary moved) all killed
dedicated local review CHANGES REQUESTED on 29eb0e38 (a docstring restated WINDOW; boundary rows missing), then CLEARED 4e579c50
fresh review in Codex missing: ERROR: Your workspace is out of credits. Add credits to continue., 2026-11-08 09:18 CET

The dedicated reviewer's CLEARED of the head stands in for the fresh review.

Closes #3001
Closes #3003

🤖 Generated with Claude Code

…oses #3001) (closes #3003)

Three refusals wrote a caller's int with a plain f-string, outside
`utils._message_text` and `utils._message_field`, so a 256-bit key
passed in the int's place reached the message. With `k` such a key:

- `op_checksequenceverify` wrote the low 16 bits of an unchecked input
  sequence: `relative lock time 65535 > 54713`, 54713 being
  `k & 0xFFFF`;
- `Block.assert_valid_contextual` wrote the low four bytes of an
  unchecked header version `-k` as `0xdff42a47` in
  `invalid version (outdated)`;
- `ChainStart` wrote `invalid median_time_past: <digits of k>`.

Section 7 of the btclib-org standard, *A refusal carries no secret*,
says an argument in the wrong place may be a key. The relative lock time
is now written only where the sequence fits its four bytes, and the
header version only where it fits its four signed bytes; a wider value
is described by its sign and bit length. A median time past is refused
only above four bytes, so it is always described.

What this breaks: `ChainStart(..., median_time_past=2**32)` reads
`invalid median_time_past: an int of 33 bits`, no longer `4294967296`.

The census read each f-string field in `src/` outside the two helpers,
with an `ast` walk, and judged it against a caller's int:

- fixed: `script_op_codes.py` CSV relative lock time, `block.py` header
  version, `headers_sync.py` median time past;
- a script number of at most 5 bytes, a parsed number or a value already
  bounded, left: the CLTV and CSV operands, OP_PICK and OP_ROLL depths,
  the script engine's multisig counts, the BIP137 recovery flag checked
  to 27..42, the block filter delta sum, total input and output amounts,
  a transaction version already checked to four bytes, a header's
  timestamp read from its `datetime`;
- the leaf version of `engine/__init__.py:205`, left: BIP341 calls
  `c[0] & 0xfe` of the control block the leaf version, and this one is
  read after the control block's commitment to the script was verified;
- sizes, counts, positions and indexes, left;
- a `datetime`, a `str` or a `Decimal`, left: no int, and a str,
  Decimal or float quoted whole is ISS #3002;
- `script_pub_key.py:435`, `:455` and `:480`, left: a byte of refused
  octets, not an int a caller passes. It is not one of the values the
  format defines, so section 7 bars it, as it bars the length markers
  beside it and the other byte-echoing refusals ISS
  #3005 lists;
- `var_int.py:56` and `:93` and `utreexo.py:578`, `:582` and `:755`,
  left: each writes up to eight bytes of refused octets read as a
  number, the shape ISS #3005 tracks.

`tests/huge_int_message_test.py` has a case for each fixed site, run with
`_HUGE` and with the scalar. The scalar test also looks for the low two
and four bytes of the scalar, in decimal and in hex, which the pieces
`assert_secret_free` looks for are too long to catch. Edge cases check
that the least header version is written and the version below it, and
a sequence one bit past four bytes, are described. A test in
`tests/script_engine/script_test.py` checks the low 16 bits are still
written for a sequence that fits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Ferdinando Ametrano <ferdinando@ametrano.net>
@coderabbitai

coderabbitai Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Error messages for out-of-range header versions and median-time values now describe oversized integers clearly instead of displaying potentially unwieldy numeric values.
    • Relative lock-time validation errors now report the relevant masked sequence value, making failures easier to interpret.
    • Validation rules and outcomes remain unchanged; these updates affect error-message formatting only.

Three validation paths now format out-of-range integer values in refusal messages with _message_text. In-range header versions and sequence values retain their field-specific formatting. Tests cover boundary values and error text.

Changes: Refusal message formatting

Layer / File(s) Summary
Update refusal message formatting
src/btclib/block/block.py, src/btclib/p2p/headers_sync.py, src/btclib/script/engine/script_op_codes.py
Header-version messages use hexadecimal formatting for values in the signed 32-bit range and _message_text otherwise. ChainStart uses _message_text for out-of-range median_time_past. Relative-lock-time messages use the masked sequence value when the full sequence fits in 32 bits and _message_text otherwise.
Test formatting boundaries and message contents
tests/huge_int_message_test.py, tests/p2p/headers_sync_test.py, tests/script_engine/script_test.py
Tests cover wide-value descriptions, header-version boundaries, median-time-past errors, relative-lock-time messages, and scalar byte fragments in chained exception text.

Estimated code review effort: 2 (Simple) | ~12 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

Possibly related PRs

  • btclib-org/btclib#3004: Both PRs change formatting at overlapping refusal sites. They differ on how values that fit a field are formatted.
Merge Risk: Minimal · up to 4e579

The changed refusal messages retain fitting-value formatting and describe wide values without printing them in full. No actionable issue is established before merge.

Pre-merge checks: Passed 3 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Linked Issues check Inconclusive [#3001] Met in the code. In op_checksequenceverify, the masked input lock time is written as a decimal only when 0 <= input sequence <= 0xFFFFFFFF. Otherwise the full sequence goes through _message_te… Confirm that headers_sync.py imports _message_text at the reviewed head. Re-run a census of f-string fields in src/ that interpolate caller-provided ints inside raise or error messages, or confirm the census list in the PR description again…
Passed Passed checks: 3 passed

Check name Status Explanation
Title check Passed The title names the three changed refusals: the sequence, the header version and the median time past. The changeset narrows each of these values so that out-of-range integers are described by sign an…
Description check Passed The description explains the change in detail. It names the three refusal sites (op_checksequenceverify, the outdated-version refusal, and ChainStart median_time_past). It states the new rule of writi…
Out of Scope Changes check Passed Every changed hunk connects to #3001 or #3003. script_op_codes.py: the sequence-bounded message in op_checksequenceverify (#3001). block.py: the outdated-version message, which writes a caller-provide…
Full details: Title check

Explanation

The title names the three changed refusals: the sequence, the header version and the median time past. The changeset narrows each of these values so that out-of-range integers are described by sign and bit length rather than printed in full. The title is specific and matches the main change, but it uses the phrase 'Describe a too-wide ...' and does not state the rule (values are described only when they exceed four bytes). It also does not follow the organization's title convention in the referenced standard, which I could not verify in this check, so the title is only partially verified against the required format.

Full details: Description check

Explanation

The description explains the change in detail. It names the three refusal sites (op_checksequenceverify, the outdated-version refusal, and ChainStart median_time_past). It states the new rule of writing values that fit four bytes and describing larger values by sign and bit length with _message_text. It also lists the test evidence, the scope exclusions, and the linked issues #3001 and #3003. The description is directly related to the changeset.

Full details: Out of Scope Changes check

Explanation

Every changed hunk connects to #3001 or #3003. script_op_codes.py: the sequence-bounded message in op_checksequenceverify (#3001). block.py: the outdated-version message, which writes a caller-provided header version in full (#3003). headers_sync.py: the median_time_past message in ChainStart (#3003). tests/huge_int_message_test.py: boundary and secrecy tests for those sites (#3001, #3003). tests/p2p/headers_sync_test.py: the expected text changes to 'an int of 33 bits' for the new ChainStart output. tests/script_engine/script_test.py: a tapscript OP_CHECKSEQUENCEVERIFY case with sequence 0x10001 that checks the refusal text (#3001). The PR does not change refusals that echo format bytes. It defers them to #3005, which is outside this PR. No unrelated change is evident in the reviewed diffs.

Full details: Linked Issues check

Explanation

[#3001] Met in the code. In op_checksequenceverify, the masked input lock time is written as a decimal only when 0 <= input sequence <= 0xFFFFFFFF. Otherwise the full sequence goes through _message_text. The scalar case is covered by the scalar secrecy test in tests/huge_int_message_test.py, as the PR summary describes. I did not read that test file directly.

[#3003] The three named sites now use _message_text: op_checksequenceverify (sequence), Block._assert_valid_contextual_header (outdated version, signed four-byte range), and ChainStart.assert_valid (median_time_past). Each has a boundary test according to the PR summary. The done-when clause is repository-wide: no refusal writes a caller's int in full. That clause rests on the census in the PR description (419 f-string fields, with exclusions by category). I did not re-run the census. My inspection budget covered only the three diffs. I also did not confirm that headers_sync.py imports _message_text, because the diff hunk adds no import. The reported passing suite implies the name resolves, but that is a self-reported result.

Resolution

Confirm that headers_sync.py imports _message_text at the reviewed head. Re-run a census of f-string fields in src/ that interpolate caller-provided ints inside raise or error messages, or confirm the census list in the PR description against the head. Confirm that no remaining site writes a caller's int in full. With that evidence, the status can become PASS.

Finishing Touches

Generate docstrings

  • Commit to this branch
  • Create a new PR
Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 7a0ba29 and 4e579c5.

Files selected for processing: 6

  • src/btclib/block/block.py
  • src/btclib/p2p/headers_sync.py
  • src/btclib/script/engine/script_op_codes.py
  • tests/huge_int_message_test.py
  • tests/p2p/headers_sync_test.py
  • tests/script_engine/script_test.py

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 5 remain after this review.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 4e579c5

@fametrano
fametrano merged commit c71a342 into main Nov 9, 2026
22 checks passed
@fametrano
fametrano deleted the iss-3003-plain-fstring-int branch November 9, 2026 00:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant