Skip to content

Commit 77770d9

Browse files
author
MarcoFalke
committed
test: Properly serialize BIP34 coinbase height
1 parent 4882040 commit 77770d9

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

test/functional/test_framework/blocktools.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@
2222
ToHex,
2323
hash256,
2424
hex_str_to_bytes,
25-
ser_string,
2625
ser_uint256,
2726
sha256,
2827
uint256_from_str,
2928
)
3029
from .script import (
3130
CScript,
31+
CScriptNum,
32+
CScriptOp,
3233
OP_0,
3334
OP_1,
3435
OP_CHECKMULTISIG,
@@ -89,29 +90,22 @@ def add_witness_commitment(block, nonce=0):
8990
block.hashMerkleRoot = block.calc_merkle_root()
9091
block.rehash()
9192

92-
def serialize_script_num(value):
93-
r = bytearray(0)
94-
if value == 0:
95-
return r
96-
neg = value < 0
97-
absvalue = -value if neg else value
98-
while (absvalue):
99-
r.append(int(absvalue & 0xff))
100-
absvalue >>= 8
101-
if r[-1] & 0x80:
102-
r.append(0x80 if neg else 0)
103-
elif neg:
104-
r[-1] |= 0x80
105-
return r
93+
94+
def script_BIP34_coinbase_height(height):
95+
if height <= 16:
96+
res = CScriptOp.encode_op_n(height)
97+
# Append dummy to increase scriptSig size above 2 (see bad-cb-length consensus rule)
98+
return CScript([res, OP_1])
99+
return CScript([CScriptNum(height)])
100+
106101

107102
def create_coinbase(height, pubkey=None):
108103
"""Create a coinbase transaction, assuming no miner fees.
109104
110105
If pubkey is passed in, the coinbase output will be a P2PK output;
111106
otherwise an anyone-can-spend output."""
112107
coinbase = CTransaction()
113-
coinbase.vin.append(CTxIn(COutPoint(0, 0xffffffff),
114-
ser_string(serialize_script_num(height)), 0xffffffff))
108+
coinbase.vin.append(CTxIn(COutPoint(0, 0xffffffff), script_BIP34_coinbase_height(height), 0xffffffff))
115109
coinbaseoutput = CTxOut()
116110
coinbaseoutput.nValue = 50 * COIN
117111
halvings = int(height / 150) # regtest

0 commit comments

Comments
 (0)