Skip to content

Commit f644ea4

Browse files
author
MarcoFalke
committed
Merge #13047: [trivial] Tidy blocktools.py
4d355bf [tests] tidy up blocktools.py (John Newbery) cab8be5 [tests] Fix flake8 warnings in blocktools.py (John Newbery) b184127 [doc][trivial] no retargeting in regtest mode (Jesse Cohen) Pull request description: Tidies up the blocktools.py module: - fixes flake8 warnings - changes function-level comments to docstrings. Takes in @skeees's commit b184127 Tree-SHA512: 0f4c59ac8ccc9057492ec1996381e73380d65e85240f2ba9607174c0743d3a1853c4ed35a9e1bc704b2b6d6d823ac77aa7e81bd150cf5033de79293c24b791b0
2 parents 8b262eb + 4d355bf commit f644ea4

File tree

1 file changed

+67
-45
lines changed

1 file changed

+67
-45
lines changed

test/functional/test_framework/blocktools.py

Lines changed: 67 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,24 @@
1010
script_to_p2sh_p2wsh,
1111
script_to_p2wsh,
1212
)
13-
from .mininode import *
13+
from .messages import (
14+
CBlock,
15+
COIN,
16+
COutPoint,
17+
CTransaction,
18+
CTxIn,
19+
CTxInWitness,
20+
CTxOut,
21+
FromHex,
22+
ToHex,
23+
bytes_to_hex_str,
24+
hash256,
25+
hex_str_to_bytes,
26+
ser_string,
27+
ser_uint256,
28+
sha256,
29+
uint256_from_str,
30+
)
1431
from .script import (
1532
CScript,
1633
OP_0,
@@ -23,34 +40,34 @@
2340
)
2441
from .util import assert_equal
2542

26-
# Create a block (with regtest difficulty)
27-
def create_block(hashprev, coinbase, nTime=None):
43+
# From BIP141
44+
WITNESS_COMMITMENT_HEADER = b"\xaa\x21\xa9\xed"
45+
46+
def create_block(hashprev, coinbase, ntime=None):
47+
"""Create a block (with regtest difficulty)."""
2848
block = CBlock()
29-
if nTime is None:
49+
if ntime is None:
3050
import time
31-
block.nTime = int(time.time()+600)
51+
block.nTime = int(time.time() + 600)
3252
else:
33-
block.nTime = nTime
53+
block.nTime = ntime
3454
block.hashPrevBlock = hashprev
35-
block.nBits = 0x207fffff # Will break after a difficulty adjustment...
55+
block.nBits = 0x207fffff # difficulty retargeting is disabled in REGTEST chainparams
3656
block.vtx.append(coinbase)
3757
block.hashMerkleRoot = block.calc_merkle_root()
3858
block.calc_sha256()
3959
return block
4060

41-
# From BIP141
42-
WITNESS_COMMITMENT_HEADER = b"\xaa\x21\xa9\xed"
43-
44-
4561
def get_witness_script(witness_root, witness_nonce):
46-
witness_commitment = uint256_from_str(hash256(ser_uint256(witness_root)+ser_uint256(witness_nonce)))
62+
witness_commitment = uint256_from_str(hash256(ser_uint256(witness_root) + ser_uint256(witness_nonce)))
4763
output_data = WITNESS_COMMITMENT_HEADER + ser_uint256(witness_commitment)
4864
return CScript([OP_RETURN, output_data])
4965

50-
51-
# According to BIP141, blocks with witness rules active must commit to the
52-
# hash of all in-block transactions including witness.
5366
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."""
5471
# First calculate the merkle root of the block's
5572
# transactions, with witnesses.
5673
witness_nonce = nonce
@@ -65,7 +82,6 @@ def add_witness_commitment(block, nonce=0):
6582
block.hashMerkleRoot = block.calc_merkle_root()
6683
block.rehash()
6784

68-
6985
def serialize_script_num(value):
7086
r = bytearray(0)
7187
if value == 0:
@@ -81,55 +97,59 @@ def serialize_script_num(value):
8197
r[-1] |= 0x80
8298
return r
8399

84-
# Create a coinbase transaction, assuming no miner fees.
85-
# If pubkey is passed in, the coinbase output will be a P2PK output;
86-
# otherwise an anyone-can-spend output.
87-
def create_coinbase(height, pubkey = None):
100+
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."""
88105
coinbase = CTransaction()
89-
coinbase.vin.append(CTxIn(COutPoint(0, 0xffffffff),
90-
ser_string(serialize_script_num(height)), 0xffffffff))
106+
coinbase.vin.append(CTxIn(COutPoint(0, 0xffffffff),
107+
ser_string(serialize_script_num(height)), 0xffffffff))
91108
coinbaseoutput = CTxOut()
92109
coinbaseoutput.nValue = 50 * COIN
93-
halvings = int(height/150) # regtest
110+
halvings = int(height / 150) # regtest
94111
coinbaseoutput.nValue >>= halvings
95-
if (pubkey != None):
112+
if (pubkey is not None):
96113
coinbaseoutput.scriptPubKey = CScript([pubkey, OP_CHECKSIG])
97114
else:
98115
coinbaseoutput.scriptPubKey = CScript([OP_TRUE])
99-
coinbase.vout = [ coinbaseoutput ]
116+
coinbase.vout = [coinbaseoutput]
100117
coinbase.calc_sha256()
101118
return coinbase
102119

103-
# Create a transaction.
104-
# If the scriptPubKey is not specified, make it anyone-can-spend.
105-
def create_transaction(prevtx, n, sig, value, scriptPubKey=CScript()):
120+
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."""
106124
tx = CTransaction()
107125
assert(n < len(prevtx.vout))
108126
tx.vin.append(CTxIn(COutPoint(prevtx.sha256, n), sig, 0xffffffff))
109-
tx.vout.append(CTxOut(value, scriptPubKey))
127+
tx.vout.append(CTxOut(value, script_pub_key))
110128
tx.calc_sha256()
111129
return tx
112130

113-
def get_legacy_sigopcount_block(block, fAccurate=True):
131+
def get_legacy_sigopcount_block(block, accurate=True):
114132
count = 0
115133
for tx in block.vtx:
116-
count += get_legacy_sigopcount_tx(tx, fAccurate)
134+
count += get_legacy_sigopcount_tx(tx, accurate)
117135
return count
118136

119-
def get_legacy_sigopcount_tx(tx, fAccurate=True):
137+
def get_legacy_sigopcount_tx(tx, accurate=True):
120138
count = 0
121139
for i in tx.vout:
122-
count += i.scriptPubKey.GetSigOpCount(fAccurate)
140+
count += i.scriptPubKey.GetSigOpCount(accurate)
123141
for j in tx.vin:
124142
# scriptSig might be of type bytes, so convert to CScript for the moment
125-
count += CScript(j.scriptSig).GetSigOpCount(fAccurate)
143+
count += CScript(j.scriptSig).GetSigOpCount(accurate)
126144
return count
127145

128-
# Create a scriptPubKey corresponding to either a P2WPKH output for the
129-
# given pubkey, or a P2WSH output of a 1-of-1 multisig for the given
130-
# pubkey. Returns the hex encoding of the scriptPubKey.
131146
def witness_script(use_p2wsh, pubkey):
132-
if (use_p2wsh == False):
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."""
152+
if not use_p2wsh:
133153
# P2WPKH instead
134154
pubkeyhash = hash160(hex_str_to_bytes(pubkey))
135155
pkscript = CScript([OP_0, pubkeyhash])
@@ -140,9 +160,10 @@ def witness_script(use_p2wsh, pubkey):
140160
pkscript = CScript([OP_0, scripthash])
141161
return bytes_to_hex_str(pkscript)
142162

143-
# Return a transaction (in hex) that spends the given utxo to a segwit output,
144-
# optionally wrapping the segwit output using P2SH.
145163
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."""
146167
if use_p2wsh:
147168
program = CScript([OP_1, hex_str_to_bytes(pubkey), OP_1, OP_CHECKMULTISIG])
148169
addr = script_to_p2sh_p2wsh(program) if encode_p2sh else script_to_p2wsh(program)
@@ -152,12 +173,13 @@ def create_witness_tx(node, use_p2wsh, utxo, pubkey, encode_p2sh, amount):
152173
assert_equal(node.getaddressinfo(addr)['scriptPubKey'], witness_script(use_p2wsh, pubkey))
153174
return node.createrawtransaction([utxo], {addr: amount})
154175

155-
# Create a transaction spending a given utxo to a segwit output corresponding
156-
# to the given pubkey: use_p2wsh determines whether to use P2WPKH or P2WSH;
157-
# encode_p2sh determines whether to wrap in P2SH.
158-
# sign=True will have the given node sign the transaction.
159-
# insert_redeem_script will be added to the scriptSig, if given.
160176
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."""
161183
tx_to_witness = create_witness_tx(node, use_p2wsh, utxo, pubkey, encode_p2sh, amount)
162184
if (sign):
163185
signed = node.signrawtransactionwithwallet(tx_to_witness)

0 commit comments

Comments
 (0)