Skip to content

Commit 4d355bf

Browse files
committed
[tests] tidy up blocktools.py
Moves function comments to docstrings, and moves the module-level constant to the top of the file.
1 parent cab8be5 commit 4d355bf

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

test/functional/test_framework/blocktools.py

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@
4040
)
4141
from .util import assert_equal
4242

43-
# Create a block (with regtest difficulty)
43+
# From BIP141
44+
WITNESS_COMMITMENT_HEADER = b"\xaa\x21\xa9\xed"
45+
4446
def create_block(hashprev, coinbase, ntime=None):
47+
"""Create a block (with regtest difficulty)."""
4548
block = CBlock()
4649
if ntime is None:
4750
import time
@@ -55,19 +58,16 @@ def create_block(hashprev, coinbase, ntime=None):
5558
block.calc_sha256()
5659
return block
5760

58-
# From BIP141
59-
WITNESS_COMMITMENT_HEADER = b"\xaa\x21\xa9\xed"
60-
61-
6261
def get_witness_script(witness_root, witness_nonce):
6362
witness_commitment = uint256_from_str(hash256(ser_uint256(witness_root) + ser_uint256(witness_nonce)))
6463
output_data = WITNESS_COMMITMENT_HEADER + ser_uint256(witness_commitment)
6564
return CScript([OP_RETURN, output_data])
6665

67-
68-
# According to BIP141, blocks with witness rules active must commit to the
69-
# hash of all in-block transactions including witness.
7066
def add_witness_commitment(block, nonce=0):
67+
"""Add a witness commitment to the block's coinbase transaction.
68+
69+
According to BIP141, blocks with witness rules active must commit to the
70+
hash of all in-block transactions including witness."""
7171
# First calculate the merkle root of the block's
7272
# transactions, with witnesses.
7373
witness_nonce = nonce
@@ -82,7 +82,6 @@ def add_witness_commitment(block, nonce=0):
8282
block.hashMerkleRoot = block.calc_merkle_root()
8383
block.rehash()
8484

85-
8685
def serialize_script_num(value):
8786
r = bytearray(0)
8887
if value == 0:
@@ -98,10 +97,11 @@ def serialize_script_num(value):
9897
r[-1] |= 0x80
9998
return r
10099

101-
# Create a coinbase transaction, assuming no miner fees.
102-
# If pubkey is passed in, the coinbase output will be a P2PK output;
103-
# otherwise an anyone-can-spend output.
104100
def create_coinbase(height, pubkey=None):
101+
"""Create a coinbase transaction, assuming no miner fees.
102+
103+
If pubkey is passed in, the coinbase output will be a P2PK output;
104+
otherwise an anyone-can-spend output."""
105105
coinbase = CTransaction()
106106
coinbase.vin.append(CTxIn(COutPoint(0, 0xffffffff),
107107
ser_string(serialize_script_num(height)), 0xffffffff))
@@ -117,9 +117,10 @@ def create_coinbase(height, pubkey=None):
117117
coinbase.calc_sha256()
118118
return coinbase
119119

120-
# Create a transaction.
121-
# If the script_pub_key is not specified, make it anyone-can-spend.
122120
def create_transaction(prevtx, n, sig, value, script_pub_key=CScript()):
121+
"""Create a transaction.
122+
123+
If the script_pub_key is not specified, make it anyone-can-spend."""
123124
tx = CTransaction()
124125
assert(n < len(prevtx.vout))
125126
tx.vin.append(CTxIn(COutPoint(prevtx.sha256, n), sig, 0xffffffff))
@@ -142,10 +143,12 @@ def get_legacy_sigopcount_tx(tx, accurate=True):
142143
count += CScript(j.scriptSig).GetSigOpCount(accurate)
143144
return count
144145

145-
# Create a scriptPubKey corresponding to either a P2WPKH output for the
146-
# given pubkey, or a P2WSH output of a 1-of-1 multisig for the given
147-
# pubkey. Returns the hex encoding of the scriptPubKey.
148146
def witness_script(use_p2wsh, pubkey):
147+
"""Create a scriptPubKey for a pay-to-wtiness TxOut.
148+
149+
This is either a P2WPKH output for the given pubkey, or a P2WSH output of a
150+
1-of-1 multisig for the given pubkey. Returns the hex encoding of the
151+
scriptPubKey."""
149152
if not use_p2wsh:
150153
# P2WPKH instead
151154
pubkeyhash = hash160(hex_str_to_bytes(pubkey))
@@ -157,9 +160,10 @@ def witness_script(use_p2wsh, pubkey):
157160
pkscript = CScript([OP_0, scripthash])
158161
return bytes_to_hex_str(pkscript)
159162

160-
# Return a transaction (in hex) that spends the given utxo to a segwit output,
161-
# optionally wrapping the segwit output using P2SH.
162163
def create_witness_tx(node, use_p2wsh, utxo, pubkey, encode_p2sh, amount):
164+
"""Return a transaction (in hex) that spends the given utxo to a segwit output.
165+
166+
Optionally wrap the segwit output using P2SH."""
163167
if use_p2wsh:
164168
program = CScript([OP_1, hex_str_to_bytes(pubkey), OP_1, OP_CHECKMULTISIG])
165169
addr = script_to_p2sh_p2wsh(program) if encode_p2sh else script_to_p2wsh(program)
@@ -169,12 +173,13 @@ def create_witness_tx(node, use_p2wsh, utxo, pubkey, encode_p2sh, amount):
169173
assert_equal(node.getaddressinfo(addr)['scriptPubKey'], witness_script(use_p2wsh, pubkey))
170174
return node.createrawtransaction([utxo], {addr: amount})
171175

172-
# Create a transaction spending a given utxo to a segwit output corresponding
173-
# to the given pubkey: use_p2wsh determines whether to use P2WPKH or P2WSH;
174-
# encode_p2sh determines whether to wrap in P2SH.
175-
# sign=True will have the given node sign the transaction.
176-
# insert_redeem_script will be added to the scriptSig, if given.
177176
def send_to_witness(use_p2wsh, node, utxo, pubkey, encode_p2sh, amount, sign=True, insert_redeem_script=""):
177+
"""Create a transaction spending a given utxo to a segwit output.
178+
179+
The output corresponds to the given pubkey: use_p2wsh determines whether to
180+
use P2WPKH or P2WSH; encode_p2sh determines whether to wrap in P2SH.
181+
sign=True will have the given node sign the transaction.
182+
insert_redeem_script will be added to the scriptSig, if given."""
178183
tx_to_witness = create_witness_tx(node, use_p2wsh, utxo, pubkey, encode_p2sh, amount)
179184
if (sign):
180185
signed = node.signrawtransactionwithwallet(tx_to_witness)

0 commit comments

Comments
 (0)