test(rawtx): lock the txid of a real transaction with a scriptSig of 253 bytes or more

Open
#100 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
72/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
python

Research direction

Start with tests/test_rawtx.py and inspect the existing _varint and non-canonical CompactSize tests; btc_toolkit/rawtx.py is cited for the behavior under test. Find a real mainnet transaction with a scriptSig of at least 253 bytes and record its txid, size, and base_size in the test. Add the requested boundary cases and check the raw transaction hex source in the PR; done when the tests detect incorrect encodings and bound changes without modifying btc_toolkit/.

Written by the indexing model from the issue text.

Description

good first issue

What

decode_tx recomputes the txid by re-serializing the transaction without its witness data (rawtx.py L204). Lengths are written as CompactSize integers by _varint (L218-L225) and read by varint (L36-L44). The tests only exercise the 1-byte form of _varint (lengths below 253), and only the 3-byte form of the "non-canonical" check. A mistake in the other forms would go unnoticed: decode would print the wrong txid for such transactions, and decode --txid and verify would wrongly reject them as not hashing to their txid.

What to do

In tests/test_rawtx.py:

  1. Decode a real mainnet transaction whose scriptSig is 253 bytes or longer, and assert its txid, size and base_size. Large P2SH multisig spends (for example 3-of-5) are well above 253 bytes; a 2-of-3 spend can land just below, so check the length.
  2. Test _varint at the boundaries 252, 253, 0xFFFF, 0x10000, 0xFFFFFFFF and 0x100000000.
  3. Test each bound of the non-canonical check at its edge: fdfc00, feffff0000 and ffffffffff00000000 are rejected as non-canonical; fdfd00, fe00000100 and ff0000000001000000 are not: fed to decode_tx after a version, like the existing "01000000" + "fd0100" case, they fail later with "declares more items than it has bytes for" (_Reader(...).varint() alone accepts them). Today only fd0100 is tested, so moving a bound by one goes unnoticed.

Acceptance

  • The transaction is real and checkable: put its txid in the test, and say in the PR where the raw hex came from (for example https://mempool.space/api/tx/<txid>/hex).
  • The tests fail if a branch of _varint or a bound of the canonical check is broken (for example, writing a length of 253 to 65535 with the 0xFE prefix and 4 bytes instead of 0xFD and 2 bytes, or moving a bound by one).
  • No change under btc_toolkit/.

See CONTRIBUTING.md for the test, lint and type-check commands. Comment below before starting, so two people don't take the same issue.

Dominant language
Python
Stars
2
Forks
1
Avg merge
1h 45m
Merged PRs (30d)
23

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from devdavidejesus/btc-toolkit

All issues in devdavidejesus/btc-toolkit

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.