Skip to content
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

Added test for getblockheader JSONRPC call #398

Merged
merged 1 commit into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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