test(verify): cover invalid bits, the mainnet and testnet limits, and the strong threshold

Open Beginner friendly
#99 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@omarelshamy25x is already working on this.

Since Dec 3, 2026.

  • #118 by @omarelshamy25x — open

Assessment

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

Research direction

Read tests/test_verify.py, especially _api(...), and inspect the verification checks and Verification grading referenced in the issue. Add offline unittest cases for invalid bits, mainnet and testnet limits, and the strong threshold, using the suggested mocks for difficulty and updated block hashes. Run the test commands in CONTRIBUTING.md; done means the new tests pass and each fails under the code changes described in the acceptance criteria.

Written by the indexing model from the issue text.

Description

good first issue

What

verify_tx checks the block header's proof of work (verify.py L265-L274) and grades it "strong" or "weak" (L58, L168-L169). Three parts have no test:

  • Invalid bits. A header whose bits field encodes a negative or a zero target must fail with a VerificationError. bits_to_target itself is tested in test_bits; what is missing is a test of verify_tx turning its ValueError into a VerificationError (the except ValueError branch).
  • The mainnet and testnet limits. A target easier than the network allows is rejected; only signet's limit is tested today.
  • The strong threshold. Nothing fails if STRONG_DIFFICULTY is changed (say from 50e12 to 1e12), or if the >= becomes >.

What to do

Add tests to tests/test_verify.py (offline, unittest, like the existing ones):

  1. Headers whose bits encode a negative target (sign bit 0x00800000 set, with a non-zero mantissa, e.g. 0x1d80ffff) and a zero target (e.g. 0x1d000000) are rejected with a VerificationError.
  2. A header whose target is above the limit (e.g. bits of 0x1d01ffff) is rejected with "easier than the network allows" on mainnet and on testnet.
  3. A Verification whose header difficulty is exactly STRONG_DIFFICULTY is "strong" on mainnet, just below it is "weak", and on testnet it is always "weak".

Acceptance

  • The new tests pass, and each fails when the code it covers is changed: the try/except ValueError around header.target removed; the mainnet or testnet value in _POW_LIMIT loosened (e.g. set to signet's); STRONG_DIFFICULTY changed or >= turned into >. Please say in the PR that you checked this.
  • No change under btc_toolkit/.

Hints

  • _api(...) in tests/test_verify.py mocks the API calls with the genesis block; pass your own header and status.
  • bits are bytes 72–75 of the 80-byte header, little-endian. Changing them changes the header hash, so pass the new hash as block_hash in status, or verification stops earlier, at "header hashes to".
  • The limit is checked before the hash, so these headers need no real proof of work.
  • For item 3 you don't need a mined header. No bits value gives a difficulty of exactly 50e12 (the closest are 0x1705a121, just above, and 0x1705a122, just below), so a header built from bits cannot tell >= from >. Patch Header.difficulty instead: patch.object(verify.Header, "difficulty", new_callable=PropertyMock, return_value=50e12) for "exactly", and math.nextafter(50e12, 0) for "just below".
  • Hard-code 50e12 in the test: a test that reads verify.STRONG_DIFFICULTY cannot catch a changed constant.

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.