Skip to content

Commit

Permalink
Added test for getblockheader JSONRPC call (#398)
Browse files Browse the repository at this point in the history
This commit add a structure to test calls of `getblockheader` in
tests/test_framework/floresta_rpc.py, a simple test in
tests/floresta_cli_getblockheader.py and a helper in pyproject.toml
as `florestacli-getblockheader-test`.

This simple test just call the `getblockheader` with the hash of
regtest's genesis block and expect, among some data, a prev_blockhash
full of zeros.
  • Loading branch information
qlrd authored Mar 6, 2025
1 parent 4284d7d commit 63c2d63
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ example-test = "python tests/run_tests.py --test-name example_test"
restart-test = "python tests/run_tests.py --test-name restart"
florestacli-getblockchaininfo-test = "python tests/run_tests.py --test-name floresta_cli_getblockchaininfo"
florestacli-getblockhash-test = "python tests/run_tests.py --test-name floresta_cli_getblockhash"
florestacli-getblockheader-test = "python tests/run_tests.py --test-name floresta_cli_getblockheader"
florestacli-getblock-test = "python tests/run_tests.py --test-name floresta_cli_getblock"
florestacli-getpeerinfo-test = "python tests/run_tests.py --test-name floresta_cli_getpeerinfo"
florestacli-addnode-test = "python tests/run_tests.py --test-name floresta_cli_addnode"
Expand All @@ -49,6 +50,7 @@ tests = [
"restart-test",
"florestacli-getblockchaininfo-test",
"florestacli-getblockhash-test",
"florestacli-getblockheader-test",
"florestacli-getblock-test",
"florestacli-getpeerinfo-test",
"florestacli-addnode-test",
Expand Down
69 changes: 69 additions & 0 deletions tests/floresta_cli_getblockheader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""
floresta_cli_getblockheader.py
This functional test cli utility to interact with a Floresta node with `getblockheader`
"""

from test_framework.test_framework import FlorestaTestFramework
from test_framework.floresta_rpc import REGTEST_RPC_SERVER


class GetBlockheaderHeightZeroTest(FlorestaTestFramework):
"""
Test `getblockheader` with a fresh node and expect a result like this:
````bash
$> ./target/release floresta_cli --network=regtest getblockheader \
0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206
{
"version": 1,
"prev_blockhash": "0000000000000000000000000000000000000000000000000000000000000000",
"merkle_root": "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b",
"time": 1296688602,
"bits": 545259519,
"nonce": 2
}
```
"""

nodes = [-1]
version = 1
blockhash = "0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206"
prev_blockhash = "0000000000000000000000000000000000000000000000000000000000000000"
merkle_root = "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"
time = 1296688602
bits = 545259519
nonce = 2

def set_test_params(self):
"""
Setup a single node
"""
GetBlockheaderHeightZeroTest.nodes[0] = self.add_node_settings(
chain="regtest", extra_args=[], rpcserver=REGTEST_RPC_SERVER
)

def run_test(self):
"""
Run JSONRPC and get the header of the genesis block
"""
# Start node
self.run_node(GetBlockheaderHeightZeroTest.nodes[0])
self.wait_for_rpc_connection(GetBlockheaderHeightZeroTest.nodes[0])

# Test assertions
node = self.get_node(GetBlockheaderHeightZeroTest.nodes[0])
response = node.get_blockheader(GetBlockheaderHeightZeroTest.blockhash)
assert response["version"] == GetBlockheaderHeightZeroTest.version
assert response["prev_blockhash"] == GetBlockheaderHeightZeroTest.prev_blockhash
assert response["merkle_root"] == GetBlockheaderHeightZeroTest.merkle_root
assert response["time"] == GetBlockheaderHeightZeroTest.time
assert response["bits"] == GetBlockheaderHeightZeroTest.bits
assert response["nonce"] == GetBlockheaderHeightZeroTest.nonce

# Shutdown node
self.stop_node(GetBlockheaderHeightZeroTest.nodes[0])


if __name__ == "__main__":
GetBlockheaderHeightZeroTest().main()
10 changes: 10 additions & 0 deletions tests/test_framework/floresta_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,16 @@ def get_blockhash(self, height: int) -> dict:
"""
return self.perform_request("getblockhash", params=[height])

def get_blockheader(self, blockhash: str) -> dict:
"""
Get the header of a block, giving its hash performing
`perform_request('getblockheader', params=[<str>])`
"""
if not bool(re.fullmatch(r"^[a-f0-9]{64}$", blockhash)):
raise ValueError(f"Invalid blockhash '{blockhash}'.")

return self.perform_request("getblockheader", params=[blockhash])

def get_block(self, blockhash: str, verbosity: int = 1):
"""
Get a full block, given its hash performing
Expand Down

0 comments on commit 63c2d63

Please sign in to comment.