From 77770d95e2838d7665fa8f621e9e83d79f9b3196 Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Tue, 9 Jul 2019 11:58:43 -0400 Subject: [PATCH] test: Properly serialize BIP34 coinbase height --- test/functional/test_framework/blocktools.py | 28 ++++++++------------ 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/test/functional/test_framework/blocktools.py b/test/functional/test_framework/blocktools.py index 7ac044d0d..d741b00ba 100644 --- a/test/functional/test_framework/blocktools.py +++ b/test/functional/test_framework/blocktools.py @@ -22,13 +22,14 @@ from .messages import ( ToHex, hash256, hex_str_to_bytes, - ser_string, ser_uint256, sha256, uint256_from_str, ) from .script import ( CScript, + CScriptNum, + CScriptOp, OP_0, OP_1, OP_CHECKMULTISIG, @@ -89,20 +90,14 @@ def add_witness_commitment(block, nonce=0): block.hashMerkleRoot = block.calc_merkle_root() block.rehash() -def serialize_script_num(value): - r = bytearray(0) - if value == 0: - return r - neg = value < 0 - absvalue = -value if neg else value - while (absvalue): - r.append(int(absvalue & 0xff)) - absvalue >>= 8 - if r[-1] & 0x80: - r.append(0x80 if neg else 0) - elif neg: - r[-1] |= 0x80 - return r + +def script_BIP34_coinbase_height(height): + if height <= 16: + res = CScriptOp.encode_op_n(height) + # Append dummy to increase scriptSig size above 2 (see bad-cb-length consensus rule) + return CScript([res, OP_1]) + return CScript([CScriptNum(height)]) + def create_coinbase(height, pubkey=None): """Create a coinbase transaction, assuming no miner fees. @@ -110,8 +105,7 @@ def create_coinbase(height, pubkey=None): If pubkey is passed in, the coinbase output will be a P2PK output; otherwise an anyone-can-spend output.""" coinbase = CTransaction() - coinbase.vin.append(CTxIn(COutPoint(0, 0xffffffff), - ser_string(serialize_script_num(height)), 0xffffffff)) + coinbase.vin.append(CTxIn(COutPoint(0, 0xffffffff), script_BIP34_coinbase_height(height), 0xffffffff)) coinbaseoutput = CTxOut() coinbaseoutput.nValue = 50 * COIN halvings = int(height / 150) # regtest