Skip to content

Add Electrum coverage and wait_until helpers#1091

Open
moisesPompilio wants to merge 5 commits into
getfloresta:masterfrom
moisesPompilio:test-electrum
Open

Add Electrum coverage and wait_until helpers#1091
moisesPompilio wants to merge 5 commits into
getfloresta:masterfrom
moisesPompilio:test-electrum

Conversation

@moisesPompilio
Copy link
Copy Markdown
Collaborator

Description and Notes

This change adds integration test coverage for Electrum-related behavior, focusing on the blockchain.block.headers and blockchain.block.header endpoints. The new tests compare Electrum headers with RPC responses to validate consistency, while also checking error handling for invalid parameters, out-of-range requests, valid ranges, and maximum count limits. They also include random height validation to improve coverage.

In addition to the Electrum tests, this change introduces wait_until in places where the test suite needs to wait for asynchronous conditions, such as chain synchronization or peer connection state. This replaces manual waiting logic with a reusable helper that periodically evaluates a predicate until the condition is met or a timeout is reached.

Supporting changes were also made to improve the integration framework, including renaming ElectrumClient.get_headers() to block_headers(), changing the Electrum response key from hex to headers, and improving socket buffer reading and JSON-RPC error handling.

How to verify the changes you have done?

  • Run the integration test suite and confirm the new Electrum endpoint tests pass.
  • Verify that blockchain.block.headers and blockchain.block.header return results consistent with RPC responses.
  • Confirm that invalid and out-of-range requests return the expected errors.
  • Check that wait_until works correctly for asynchronous conditions such as chain synchronization and peer connection establishment.

Replaced manual waiting logic with the `wait_until` utility in the getblock,
gettxout, getblockhash, getblockcount, getbestblockhash and reorg_chain
tests and in the function wait_for_peers_connections in the test_framework/init.py.
The `wait_until` function periodically evaluates a given predicate and
returns `True` when the condition is met. If the condition is not met within
the timeout, it raises an exception.
Socket reads are now optimized with a 4KB buffer instead of byte-by-byte
reads, JSON-RPC error validation raises ValueError on server errors, and
only the result field is returned to simplify client usage.
Tests the blockchain.block.header endpoint by validating header
consistency between Electrum and RPC responses, ensuring proper error
handling for out-of-range heights, and including random height
validation for comprehensive coverage.
…endpoint

Add new test file blockchain_block_headers.py with full test coverage for
the blockchain.block.headers endpoint. The tests cover out-of-range
requests, invalid parameters, valid requests across different ranges, and
validation of the max count limit. Compares Electrum headers against RPC
responses to ensure consistency validation.

Additionally, rename the ElectrumClient method from get_headers() to
block_headers()
@moisesPompilio moisesPompilio self-assigned this May 26, 2026
@github-project-automation github-project-automation Bot moved this to Backlog in Floresta May 26, 2026
@moisesPompilio moisesPompilio moved this from Backlog to Needs review in Floresta May 26, 2026
@moisesPompilio moisesPompilio added this to the Q2/2026 milestone May 26, 2026
compare_headers(florestad, 0)
compare_headers(florestad, MINE_BLOCKS)

random_height = random.randint(1, MINE_BLOCKS - 1)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

randomness in tests is a big footgun, debug becomes really annoying if the test isn't reproducible

json_rpc_res!(request, {
"count": count,
"hex": String::from_iter(headers),
"headers": headers.collect::<Vec<_>>(),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this is a 1.6 change, we need to keep versioning in mind for these, see #1109

time.sleep(1)

wait_until(
predicate=lambda: florestad.rpc.get_block_count()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Perhaps you could give some specialized functions for common cases like this one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Needs review

Development

Successfully merging this pull request may close these issues.

2 participants