test: Properly serialize BIP34 coinbase height

pull/764/head
MarcoFalke 5 years ago
parent 4882040182
commit 77770d95e2
No known key found for this signature in database
GPG Key ID: CE2B75697E69A548

@ -22,13 +22,14 @@ from .messages import (
ToHex, ToHex,
hash256, hash256,
hex_str_to_bytes, hex_str_to_bytes,
ser_string,
ser_uint256, ser_uint256,
sha256, sha256,
uint256_from_str, uint256_from_str,
) )
from .script import ( from .script import (
CScript, CScript,
CScriptNum,
CScriptOp,
OP_0, OP_0,
OP_1, OP_1,
OP_CHECKMULTISIG, OP_CHECKMULTISIG,
@ -89,20 +90,14 @@ def add_witness_commitment(block, nonce=0):
block.hashMerkleRoot = block.calc_merkle_root() block.hashMerkleRoot = block.calc_merkle_root()
block.rehash() block.rehash()
def serialize_script_num(value):
r = bytearray(0) def script_BIP34_coinbase_height(height):
if value == 0: if height <= 16:
return r res = CScriptOp.encode_op_n(height)
neg = value < 0 # Append dummy to increase scriptSig size above 2 (see bad-cb-length consensus rule)
absvalue = -value if neg else value return CScript([res, OP_1])
while (absvalue): return CScript([CScriptNum(height)])
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 create_coinbase(height, pubkey=None): def create_coinbase(height, pubkey=None):
"""Create a coinbase transaction, assuming no miner fees. """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; If pubkey is passed in, the coinbase output will be a P2PK output;
otherwise an anyone-can-spend output.""" otherwise an anyone-can-spend output."""
coinbase = CTransaction() coinbase = CTransaction()
coinbase.vin.append(CTxIn(COutPoint(0, 0xffffffff), coinbase.vin.append(CTxIn(COutPoint(0, 0xffffffff), script_BIP34_coinbase_height(height), 0xffffffff))
ser_string(serialize_script_num(height)), 0xffffffff))
coinbaseoutput = CTxOut() coinbaseoutput = CTxOut()
coinbaseoutput.nValue = 50 * COIN coinbaseoutput.nValue = 50 * COIN
halvings = int(height / 150) # regtest halvings = int(height / 150) # regtest

Loading…
Cancel
Save