88from .script import CScript , OP_TRUE , OP_CHECKSIG , OP_RETURN
99
1010# Create a block (with regtest difficulty)
11- def create_block (hashprev , coinbase , nTime = None ):
11+ def create_block (hashprev , coinbase , nTime = None , height = 0 ):
1212 block = CBlock ()
1313 if nTime is None :
1414 import time
1515 block .nTime = int (time .time ()+ 600 )
1616 else :
1717 block .nTime = nTime
1818 block .hashPrevBlock = hashprev
19- block .nBits = 0x207fffff # Will break after a difficulty adjustment...
2019 block .vtx .append (coinbase )
2120 block .hashMerkleRoot = block .calc_merkle_root ()
21+ block .nHeight = height
22+ block .proof .challenge = CScript ([OP_TRUE ])
23+ block .proof .solution = b''
2224 block .calc_sha256 ()
2325 return block
2426
@@ -39,39 +41,28 @@ def add_witness_commitment(block, nonce=0):
3941
4042 # witness commitment is the last OP_RETURN output in coinbase
4143 output_data = WITNESS_COMMITMENT_HEADER + ser_uint256 (witness_commitment )
42- block .vtx [0 ].vout .append (CTxOut (0 , CScript ([OP_RETURN , output_data ])))
44+ block .vtx [0 ].vout .append (CTxOut (CTxOutValue ( 0 ) , CScript ([OP_RETURN , output_data ])))
4345 block .vtx [0 ].rehash ()
4446 block .hashMerkleRoot = block .calc_merkle_root ()
4547 block .rehash ()
4648
47-
48- def serialize_script_num (value ):
49- r = bytearray (0 )
50- if value == 0 :
51- return r
52- neg = value < 0
53- absvalue = - value if neg else value
54- while (absvalue ):
55- r .append (int (absvalue & 0xff ))
56- absvalue >>= 8
57- if r [- 1 ] & 0x80 :
58- r .append (0x80 if neg else 0 )
59- elif neg :
60- r [- 1 ] |= 0x80
61- return r
62-
6349# Create a coinbase transaction, assuming no miner fees.
6450# If pubkey is passed in, the coinbase output will be a P2PK output;
6551# otherwise an anyone-can-spend output.
66- def create_coinbase (height , pubkey = None ):
52+ def create_coinbase (height , pubkey = None , amount = 0 ):
6753 coinbase = CTransaction ()
68- coinbase .vin .append (CTxIn (COutPoint (0 , 0xffffffff ),
69- ser_string (serialize_script_num (height )), 0xffffffff ))
54+ # coinbase transaction scriptsigs must be at least 2 bytes
55+ coinbase .vin .append (CTxIn (COutPoint (0 , 0xffffffff ),
56+ CScript ([height , OP_TRUE ]), 0xffffffff ))
7057 coinbaseoutput = CTxOut ()
71- coinbaseoutput .nValue = 50 * COIN
72- halvings = int (height / 150 ) # regtest
73- coinbaseoutput .nValue >>= halvings
74- if (pubkey != None ):
58+
59+ coinbaseoutput .nValue .setToAmount (amount )
60+ coinbaseoutput .nAsset .setToAsset (b'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' )
61+
62+ if amount == 0 :
63+ # zero utxo's must be made unspendable
64+ coinbaseoutput .scriptPubKey = CScript ([OP_RETURN ])
65+ elif pubkey != None :
7566 coinbaseoutput .scriptPubKey = CScript ([pubkey , OP_CHECKSIG ])
7667 else :
7768 coinbaseoutput .scriptPubKey = CScript ([OP_TRUE ])
@@ -85,7 +76,7 @@ def create_transaction(prevtx, n, sig, value, scriptPubKey=CScript()):
8576 tx = CTransaction ()
8677 assert (n < len (prevtx .vout ))
8778 tx .vin .append (CTxIn (COutPoint (prevtx .sha256 , n ), sig , 0xffffffff ))
88- tx .vout .append (CTxOut (value , scriptPubKey ))
79+ tx .vout .append (CTxOut (CTxOutValue ( value ) , scriptPubKey ))
8980 tx .calc_sha256 ()
9081 return tx
9182
0 commit comments