Skip to content

Commit 029d28a

Browse files
author
MarcoFalke
committed
Merge #15238: [QA] remove some magic mining constants in functional tests
b651ef7 submitheader: more directly test missing prev block header (Gregory Sanders) 1e7f741 remove some magic mining constants in functional tests (Gregory Sanders) Pull request description: The fewer magic numbers the better. Also more directly tested a `submitheader` case of bad previous blockhash. Tree-SHA512: 52b01a6aa199fa909eea4e9e84409a901933e545724e33149cc4132c82168199fd678809b6d94d95c9ff6ad02238a9552363620d13b8beaa5d4b67ade9ef425c
2 parents d739184 + b651ef7 commit 029d28a

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

test/functional/interface_rest.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
hex_str_to_bytes,
2323
)
2424

25+
from test_framework.messages import BLOCK_HEADER_SIZE
26+
2527
class ReqType(Enum):
2628
JSON = 1
2729
BIN = 2
@@ -214,26 +216,26 @@ def run_test(self):
214216

215217
# Check binary format
216218
response = self.test_rest_request("/block/{}".format(bb_hash), req_type=ReqType.BIN, ret_type=RetType.OBJ)
217-
assert_greater_than(int(response.getheader('content-length')), 80)
219+
assert_greater_than(int(response.getheader('content-length')), BLOCK_HEADER_SIZE)
218220
response_bytes = response.read()
219221

220222
# Compare with block header
221223
response_header = self.test_rest_request("/headers/1/{}".format(bb_hash), req_type=ReqType.BIN, ret_type=RetType.OBJ)
222-
assert_equal(int(response_header.getheader('content-length')), 80)
224+
assert_equal(int(response_header.getheader('content-length')), BLOCK_HEADER_SIZE)
223225
response_header_bytes = response_header.read()
224-
assert_equal(response_bytes[:80], response_header_bytes)
226+
assert_equal(response_bytes[:BLOCK_HEADER_SIZE], response_header_bytes)
225227

226228
# Check block hex format
227229
response_hex = self.test_rest_request("/block/{}".format(bb_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ)
228-
assert_greater_than(int(response_hex.getheader('content-length')), 160)
230+
assert_greater_than(int(response_hex.getheader('content-length')), BLOCK_HEADER_SIZE*2)
229231
response_hex_bytes = response_hex.read().strip(b'\n')
230232
assert_equal(binascii.hexlify(response_bytes), response_hex_bytes)
231233

232234
# Compare with hex block header
233235
response_header_hex = self.test_rest_request("/headers/1/{}".format(bb_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ)
234-
assert_greater_than(int(response_header_hex.getheader('content-length')), 160)
235-
response_header_hex_bytes = response_header_hex.read(160)
236-
assert_equal(binascii.hexlify(response_bytes[:80]), response_header_hex_bytes)
236+
assert_greater_than(int(response_header_hex.getheader('content-length')), BLOCK_HEADER_SIZE*2)
237+
response_header_hex_bytes = response_header_hex.read(BLOCK_HEADER_SIZE*2)
238+
assert_equal(binascii.hexlify(response_bytes[:BLOCK_HEADER_SIZE]), response_header_hex_bytes)
237239

238240
# Check json format
239241
block_json_obj = self.test_rest_request("/block/{}".format(bb_hash))

test/functional/mining_basic.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from test_framework.messages import (
1616
CBlock,
1717
CBlockHeader,
18+
BLOCK_HEADER_SIZE
1819
)
1920
from test_framework.mininode import (
2021
P2PDataStore,
@@ -131,10 +132,9 @@ def assert_submitblock(block, result_str_1, result_str_2=None):
131132

132133
self.log.info("getblocktemplate: Test bad tx count")
133134
# The tx count is immediately after the block header
134-
TX_COUNT_OFFSET = 80
135135
bad_block_sn = bytearray(block.serialize())
136-
assert_equal(bad_block_sn[TX_COUNT_OFFSET], 1)
137-
bad_block_sn[TX_COUNT_OFFSET] += 1
136+
assert_equal(bad_block_sn[BLOCK_HEADER_SIZE], 1)
137+
bad_block_sn[BLOCK_HEADER_SIZE] += 1
138138
assert_raises_rpc_error(-22, "Block decode failed", node.getblocktemplate, {'data': b2x(bad_block_sn), 'mode': 'proposal', 'rules': ['segwit']})
139139

140140
self.log.info("getblocktemplate: Test bad bits")
@@ -164,9 +164,9 @@ def assert_submitblock(block, result_str_1, result_str_2=None):
164164
assert_submitblock(bad_block, 'prev-blk-not-found', 'prev-blk-not-found')
165165

166166
self.log.info('submitheader tests')
167-
assert_raises_rpc_error(-22, 'Block header decode failed', lambda: node.submitheader(hexdata='xx' * 80))
168-
assert_raises_rpc_error(-22, 'Block header decode failed', lambda: node.submitheader(hexdata='ff' * 78))
169-
assert_raises_rpc_error(-25, 'Must submit previous header', lambda: node.submitheader(hexdata='ff' * 80))
167+
assert_raises_rpc_error(-22, 'Block header decode failed', lambda: node.submitheader(hexdata='xx' * BLOCK_HEADER_SIZE))
168+
assert_raises_rpc_error(-22, 'Block header decode failed', lambda: node.submitheader(hexdata='ff' * (BLOCK_HEADER_SIZE-2)))
169+
assert_raises_rpc_error(-25, 'Must submit previous header', lambda: node.submitheader(hexdata=super(CBlock, bad_block).serialize().hex()))
170170

171171
block.nTime += 1
172172
block.solve()

test/functional/test_framework/messages.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import time
2929

3030
from test_framework.siphash import siphash256
31-
from test_framework.util import hex_str_to_bytes, bytes_to_hex_str
31+
from test_framework.util import hex_str_to_bytes, bytes_to_hex_str, assert_equal
3232

3333
MIN_VERSION_SUPPORTED = 60001
3434
MY_VERSION = 70014 # past bip-31 for ping/pong
@@ -591,6 +591,8 @@ def __repr__(self):
591591
% (self.nVersion, self.hashPrevBlock, self.hashMerkleRoot,
592592
time.ctime(self.nTime), self.nBits, self.nNonce)
593593

594+
BLOCK_HEADER_SIZE = len(CBlockHeader().serialize())
595+
assert_equal(BLOCK_HEADER_SIZE, 80)
594596

595597
class CBlock(CBlockHeader):
596598
__slots__ = ("vtx",)

0 commit comments

Comments
 (0)