Merge #15238: [QA] remove some magic mining constants in functional tests

b651ef7e1c submitheader: more directly test missing prev block header (Gregory Sanders)
1e7f741745 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
This commit is contained in:
MarcoFalke 2019-02-12 16:51:59 -05:00
commit 029d28a7aa
No known key found for this signature in database
GPG Key ID: D2EA4850E7528B25
3 changed files with 18 additions and 14 deletions

View File

@ -22,6 +22,8 @@ from test_framework.util import (
hex_str_to_bytes, hex_str_to_bytes,
) )
from test_framework.messages import BLOCK_HEADER_SIZE
class ReqType(Enum): class ReqType(Enum):
JSON = 1 JSON = 1
BIN = 2 BIN = 2
@ -214,26 +216,26 @@ class RESTTest (BitcoinTestFramework):
# Check binary format # Check binary format
response = self.test_rest_request("/block/{}".format(bb_hash), req_type=ReqType.BIN, ret_type=RetType.OBJ) response = self.test_rest_request("/block/{}".format(bb_hash), req_type=ReqType.BIN, ret_type=RetType.OBJ)
assert_greater_than(int(response.getheader('content-length')), 80) assert_greater_than(int(response.getheader('content-length')), BLOCK_HEADER_SIZE)
response_bytes = response.read() response_bytes = response.read()
# Compare with block header # Compare with block header
response_header = self.test_rest_request("/headers/1/{}".format(bb_hash), req_type=ReqType.BIN, ret_type=RetType.OBJ) response_header = self.test_rest_request("/headers/1/{}".format(bb_hash), req_type=ReqType.BIN, ret_type=RetType.OBJ)
assert_equal(int(response_header.getheader('content-length')), 80) assert_equal(int(response_header.getheader('content-length')), BLOCK_HEADER_SIZE)
response_header_bytes = response_header.read() response_header_bytes = response_header.read()
assert_equal(response_bytes[:80], response_header_bytes) assert_equal(response_bytes[:BLOCK_HEADER_SIZE], response_header_bytes)
# Check block hex format # Check block hex format
response_hex = self.test_rest_request("/block/{}".format(bb_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ) response_hex = self.test_rest_request("/block/{}".format(bb_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ)
assert_greater_than(int(response_hex.getheader('content-length')), 160) assert_greater_than(int(response_hex.getheader('content-length')), BLOCK_HEADER_SIZE*2)
response_hex_bytes = response_hex.read().strip(b'\n') response_hex_bytes = response_hex.read().strip(b'\n')
assert_equal(binascii.hexlify(response_bytes), response_hex_bytes) assert_equal(binascii.hexlify(response_bytes), response_hex_bytes)
# Compare with hex block header # Compare with hex block header
response_header_hex = self.test_rest_request("/headers/1/{}".format(bb_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ) response_header_hex = self.test_rest_request("/headers/1/{}".format(bb_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ)
assert_greater_than(int(response_header_hex.getheader('content-length')), 160) assert_greater_than(int(response_header_hex.getheader('content-length')), BLOCK_HEADER_SIZE*2)
response_header_hex_bytes = response_header_hex.read(160) response_header_hex_bytes = response_header_hex.read(BLOCK_HEADER_SIZE*2)
assert_equal(binascii.hexlify(response_bytes[:80]), response_header_hex_bytes) assert_equal(binascii.hexlify(response_bytes[:BLOCK_HEADER_SIZE]), response_header_hex_bytes)
# Check json format # Check json format
block_json_obj = self.test_rest_request("/block/{}".format(bb_hash)) block_json_obj = self.test_rest_request("/block/{}".format(bb_hash))

View File

@ -15,6 +15,7 @@ from test_framework.blocktools import create_coinbase
from test_framework.messages import ( from test_framework.messages import (
CBlock, CBlock,
CBlockHeader, CBlockHeader,
BLOCK_HEADER_SIZE
) )
from test_framework.mininode import ( from test_framework.mininode import (
P2PDataStore, P2PDataStore,
@ -131,10 +132,9 @@ class MiningTest(BitcoinTestFramework):
self.log.info("getblocktemplate: Test bad tx count") self.log.info("getblocktemplate: Test bad tx count")
# The tx count is immediately after the block header # The tx count is immediately after the block header
TX_COUNT_OFFSET = 80
bad_block_sn = bytearray(block.serialize()) bad_block_sn = bytearray(block.serialize())
assert_equal(bad_block_sn[TX_COUNT_OFFSET], 1) assert_equal(bad_block_sn[BLOCK_HEADER_SIZE], 1)
bad_block_sn[TX_COUNT_OFFSET] += 1 bad_block_sn[BLOCK_HEADER_SIZE] += 1
assert_raises_rpc_error(-22, "Block decode failed", node.getblocktemplate, {'data': b2x(bad_block_sn), 'mode': 'proposal', 'rules': ['segwit']}) assert_raises_rpc_error(-22, "Block decode failed", node.getblocktemplate, {'data': b2x(bad_block_sn), 'mode': 'proposal', 'rules': ['segwit']})
self.log.info("getblocktemplate: Test bad bits") self.log.info("getblocktemplate: Test bad bits")
@ -164,9 +164,9 @@ class MiningTest(BitcoinTestFramework):
assert_submitblock(bad_block, 'prev-blk-not-found', 'prev-blk-not-found') assert_submitblock(bad_block, 'prev-blk-not-found', 'prev-blk-not-found')
self.log.info('submitheader tests') self.log.info('submitheader tests')
assert_raises_rpc_error(-22, 'Block header decode failed', lambda: node.submitheader(hexdata='xx' * 80)) assert_raises_rpc_error(-22, 'Block header decode failed', lambda: node.submitheader(hexdata='xx' * BLOCK_HEADER_SIZE))
assert_raises_rpc_error(-22, 'Block header decode failed', lambda: node.submitheader(hexdata='ff' * 78)) assert_raises_rpc_error(-22, 'Block header decode failed', lambda: node.submitheader(hexdata='ff' * (BLOCK_HEADER_SIZE-2)))
assert_raises_rpc_error(-25, 'Must submit previous header', lambda: node.submitheader(hexdata='ff' * 80)) assert_raises_rpc_error(-25, 'Must submit previous header', lambda: node.submitheader(hexdata=super(CBlock, bad_block).serialize().hex()))
block.nTime += 1 block.nTime += 1
block.solve() block.solve()

View File

@ -28,7 +28,7 @@ import struct
import time import time
from test_framework.siphash import siphash256 from test_framework.siphash import siphash256
from test_framework.util import hex_str_to_bytes, bytes_to_hex_str from test_framework.util import hex_str_to_bytes, bytes_to_hex_str, assert_equal
MIN_VERSION_SUPPORTED = 60001 MIN_VERSION_SUPPORTED = 60001
MY_VERSION = 70014 # past bip-31 for ping/pong MY_VERSION = 70014 # past bip-31 for ping/pong
@ -591,6 +591,8 @@ class CBlockHeader:
% (self.nVersion, self.hashPrevBlock, self.hashMerkleRoot, % (self.nVersion, self.hashPrevBlock, self.hashMerkleRoot,
time.ctime(self.nTime), self.nBits, self.nNonce) time.ctime(self.nTime), self.nBits, self.nNonce)
BLOCK_HEADER_SIZE = len(CBlockHeader().serialize())
assert_equal(BLOCK_HEADER_SIZE, 80)
class CBlock(CBlockHeader): class CBlock(CBlockHeader):
__slots__ = ("vtx",) __slots__ = ("vtx",)