-
Notifications
You must be signed in to change notification settings - Fork 134
Add Electrum coverage and wait_until helpers #1091
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
b1804c1
5a14b9f
36c2c16
5abaac3
d0724fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
|
||
| """ | ||
| Tests for block header retrieval and validation in the Electrum server. | ||
|
|
||
| This tests the blockchain.block.header endpoint which returns a block header | ||
| at a given height, optionally with a merkle proof for verification. | ||
| """ | ||
|
|
||
| import random | ||
| import pytest | ||
| from test_framework.util import wait_until | ||
|
|
||
| MINE_BLOCKS = 100 | ||
|
|
||
|
|
||
| @pytest.mark.electrum | ||
| def test_block_header(florestad_utreexod): | ||
| """Test block header retrieval and validation.""" | ||
| florestad, utreexod = florestad_utreexod | ||
|
|
||
| utreexod.rpc.generate(MINE_BLOCKS) | ||
| wait_until(lambda: florestad.rpc.get_block_count() == MINE_BLOCKS) | ||
|
|
||
| with pytest.raises(ValueError): | ||
| florestad.electrum.block_header(MINE_BLOCKS + 1) | ||
|
|
||
| with pytest.raises(ValueError): | ||
| florestad.electrum.block_header(-1) | ||
|
|
||
| compare_headers(florestad, 0) | ||
| compare_headers(florestad, MINE_BLOCKS) | ||
|
|
||
| random_height = random.randint(1, MINE_BLOCKS - 1) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| compare_headers(florestad, random_height) | ||
|
|
||
|
|
||
| def compare_headers(florestad, height): | ||
| """Helper function to compare block headers from Electrum and RPC.""" | ||
| electrum_header = florestad.electrum.block_header(height) | ||
|
|
||
| block_hash = florestad.rpc.get_blockhash(height) | ||
| rpc_header = florestad.rpc.get_blockheader(block_hash, verbosity=False) | ||
|
|
||
| assert electrum_header == rpc_header | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
|
||
| """ | ||
| Tests for block headers retrieval and validation in the Electrum server. | ||
|
|
||
| This tests the blockchain.block.headers endpoint which returns a chunk | ||
| of block headers from the main chain. | ||
| """ | ||
|
|
||
| import random | ||
| import pytest | ||
| from test_framework.util import wait_until | ||
|
|
||
| MINE_BLOCKS = 100 | ||
| MAX_HEADERS = 2016 | ||
|
|
||
|
|
||
| @pytest.mark.electrum | ||
| def test_block_headers(setup_logging, florestad_utreexod): | ||
| """Test block headers retrieval and validation.""" | ||
| log = setup_logging | ||
| florestad, utreexod = florestad_utreexod | ||
|
|
||
| utreexod.rpc.generate(MINE_BLOCKS) | ||
| wait_until(lambda: florestad.rpc.get_block_count() == MINE_BLOCKS) | ||
|
|
||
| log.info("Testing out-of-range request...") | ||
| response = florestad.electrum.block_headers(MINE_BLOCKS + 1, 10) | ||
| assert response["count"] == 0 | ||
| assert response["headers"] == [] | ||
| assert response["max"] == MAX_HEADERS | ||
|
|
||
| log.info("Testing invalid parameters...") | ||
| with pytest.raises(ValueError): | ||
| florestad.electrum.block_headers(-1, 10) | ||
|
|
||
| with pytest.raises(ValueError): | ||
| florestad.electrum.block_headers(0, -1) | ||
|
|
||
| log.info("Testing valid requests...") | ||
| compare_headers_range(florestad, 0, 10) | ||
| compare_headers_range(florestad, MINE_BLOCKS - 10, 10) | ||
|
|
||
| log.info("Testing random range...") | ||
| random_start = random.randint(1, MINE_BLOCKS - 10) | ||
| compare_headers_range(florestad, random_start, 10) | ||
|
|
||
| log.info("Testing max count limit...") | ||
| response = florestad.electrum.block_headers(0, MAX_HEADERS * 2) | ||
| assert response["count"] <= response["max"] | ||
| assert response["max"] == MAX_HEADERS | ||
| assert response["count"] <= MAX_HEADERS | ||
|
|
||
|
|
||
| def compare_headers_range(florestad, start_height, count): | ||
| """Compare block headers from Electrum and RPC for a range.""" | ||
| response = florestad.electrum.block_headers(start_height, count) | ||
|
|
||
| # Validate response structure | ||
| assert "count" in response | ||
| assert "headers" in response | ||
| assert "max" in response | ||
| assert response["count"] == len(response["headers"]) | ||
| assert response["max"] == MAX_HEADERS | ||
|
|
||
| # Compare each header with RPC | ||
| for i, header in enumerate(response["headers"]): | ||
| height = start_height + i | ||
| block_hash = florestad.rpc.get_blockhash(height) | ||
| rpc_header = florestad.rpc.get_blockheader(block_hash, verbosity=False) | ||
|
|
||
| assert header == rpc_header |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,10 +7,9 @@ | |
| and utreexod, respectively. | ||
| """ | ||
|
|
||
| import time | ||
| import pytest | ||
|
|
||
| TIMEOUT_SECONDS = 20 | ||
| from test_framework.util import wait_until | ||
|
|
||
|
|
||
| @pytest.mark.rpc | ||
|
|
@@ -27,14 +26,11 @@ def test_get_best_block_hash(florestad_utreexod): | |
| assert floresta_best_block == utreexo_best_block | ||
|
|
||
| utreexod.rpc.generate(10) | ||
| end = time.time() + TIMEOUT_SECONDS | ||
| while time.time() < end: | ||
| floresta_block = florestad.rpc.get_block_count() | ||
| utreexo_block = utreexod.rpc.get_block_count() | ||
| if floresta_block == utreexo_block: | ||
| break | ||
|
|
||
| time.sleep(1) | ||
|
|
||
| wait_until( | ||
| predicate=lambda: florestad.rpc.get_block_count() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
| == utreexod.rpc.get_block_count() | ||
| ) | ||
|
|
||
| utreexo_chain = utreexod.rpc.get_blockchain_info() | ||
| floresta_best_block = florestad.rpc.get_bestblockhash() | ||
|
|
||
There was a problem hiding this comment.
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