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

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

Merged
merged 2 commits into from Feb 12, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -22,6 +22,8 @@
hex_str_to_bytes,
)

from test_framework.messages import BLOCK_HEADER_SIZE

class ReqType(Enum):
JSON = 1
BIN = 2
@@ -213,26 +215,26 @@ def run_test(self):

# Check binary format
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()

# Compare with block header
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()
assert_equal(response_bytes[:80], response_header_bytes)
assert_equal(response_bytes[:BLOCK_HEADER_SIZE], response_header_bytes)

# Check block hex format
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')
assert_equal(binascii.hexlify(response_bytes), response_hex_bytes)

# 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)
assert_greater_than(int(response_header_hex.getheader('content-length')), 160)
response_header_hex_bytes = response_header_hex.read(160)
assert_equal(binascii.hexlify(response_bytes[:80]), response_header_hex_bytes)
assert_greater_than(int(response_header_hex.getheader('content-length')), BLOCK_HEADER_SIZE*2)
response_header_hex_bytes = response_header_hex.read(BLOCK_HEADER_SIZE*2)
assert_equal(binascii.hexlify(response_bytes[:BLOCK_HEADER_SIZE]), response_header_hex_bytes)

# Check json format
block_json_obj = self.test_rest_request("/block/{}".format(bb_hash))
@@ -15,6 +15,7 @@
from test_framework.messages import (
CBlock,
CBlockHeader,
BLOCK_HEADER_SIZE

This comment has been minimized.

Copy link
@MarcoFalke

MarcoFalke Feb 12, 2019

Member

style-nit: Missing trailing , 👀

)
from test_framework.mininode import (
P2PDataStore,
@@ -131,10 +132,9 @@ def assert_submitblock(block, result_str_1, result_str_2=None):

self.log.info("getblocktemplate: Test bad tx count")
# The tx count is immediately after the block header
TX_COUNT_OFFSET = 80
bad_block_sn = bytearray(block.serialize())
assert_equal(bad_block_sn[TX_COUNT_OFFSET], 1)
bad_block_sn[TX_COUNT_OFFSET] += 1
assert_equal(bad_block_sn[BLOCK_HEADER_SIZE], 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']})

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

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='ff' * 78))
assert_raises_rpc_error(-25, 'Must submit previous header', lambda: node.submitheader(hexdata='ff' * 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' * (BLOCK_HEADER_SIZE-2)))
assert_raises_rpc_error(-25, 'Must submit previous header', lambda: node.submitheader(hexdata=super(CBlock, bad_block).serialize().hex()))
This conversation was marked as resolved by MarcoFalke

This comment has been minimized.

Copy link
@practicalswift

practicalswift Jan 24, 2019

Member

I'm not sure I follow here: which constructor is it that takes CBlock and bad_block as parameters?

This comment has been minimized.

Copy link
@instagibbs

instagibbs Jan 24, 2019

Author Member

The base class is CBlockHeader, so we're accessing its serialization function.

This comment has been minimized.

Copy link
@Empact

Empact Jan 25, 2019

Member

Maybe rename bad_block above to bad_hashPrevBlock_block or similar, and invoke the CBlockHeader constructor directly?

This comment has been minimized.

Copy link
@AkioNak

AkioNak Feb 18, 2019

Contributor

Currentry, minimum required version of Python (test) is 3.4.
https://github.com/bitcoin/bitcoin/blob/master/doc/dependencies.md

But hex() seems to be introdued in Python 3.5.
https://docs.python.org/3.5/library/stdtypes.html?highlight=bytes%20hex#bytes.hex

I got following error when executing this test on Python3.4.

AttributeError: 'bytes' object has no attribute 'hex'

block.nTime += 1
block.solve()
@@ -28,7 +28,7 @@
import time

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
MY_VERSION = 70014 # past bip-31 for ping/pong
@@ -591,6 +591,8 @@ def __repr__(self):
% (self.nVersion, self.hashPrevBlock, self.hashMerkleRoot,
time.ctime(self.nTime), self.nBits, self.nNonce)

BLOCK_HEADER_SIZE = len(CBlockHeader().serialize())
assert_equal(BLOCK_HEADER_SIZE, 80)

class CBlock(CBlockHeader):
__slots__ = ("vtx",)
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.