|
22 | 22 | ToHex,
|
23 | 23 | hash256,
|
24 | 24 | hex_str_to_bytes,
|
25 |
| - ser_string, |
26 | 25 | ser_uint256,
|
27 | 26 | sha256,
|
28 | 27 | uint256_from_str,
|
29 | 28 | )
|
30 | 29 | from .script import (
|
31 | 30 | CScript,
|
| 31 | + CScriptNum, |
| 32 | + CScriptOp, |
32 | 33 | OP_0,
|
33 | 34 | OP_1,
|
34 | 35 | OP_CHECKMULTISIG,
|
@@ -89,29 +90,22 @@ def add_witness_commitment(block, nonce=0):
|
89 | 90 | block.hashMerkleRoot = block.calc_merkle_root()
|
90 | 91 | block.rehash()
|
91 | 92 |
|
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 | + |
106 | 101 |
|
107 | 102 | def create_coinbase(height, pubkey=None):
|
108 | 103 | """Create a coinbase transaction, assuming no miner fees.
|
109 | 104 |
|
110 | 105 | If pubkey is passed in, the coinbase output will be a P2PK output;
|
111 | 106 | otherwise an anyone-can-spend output."""
|
112 | 107 | 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)) |
115 | 109 | coinbaseoutput = CTxOut()
|
116 | 110 | coinbaseoutput.nValue = 50 * COIN
|
117 | 111 | halvings = int(height / 150) # regtest
|
|
0 commit comments