Skip to content

Commit 82b2712

Browse files
committed
[tests] move witness util functions to blocktools.py
This avoids importing from segwit.py to bumpfee.py
1 parent 1e10854 commit 82b2712

File tree

3 files changed

+76
-49
lines changed

3 files changed

+76
-49
lines changed

test/functional/bumpfee.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
make assumptions about execution order.
1515
"""
1616

17-
from segwit import send_to_witness
17+
from test_framework.blocktools import send_to_witness
1818
from test_framework.test_framework import BitcoinTestFramework
1919
from test_framework import blocktools
2020
from test_framework.mininode import CTransaction

test/functional/segwit.py

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test the SegWit changeover logic."""
66

7+
from test_framework.address import (
8+
key_to_p2sh_p2wpkh,
9+
key_to_p2wpkh,
10+
program_to_witness,
11+
script_to_p2sh_p2wsh,
12+
script_to_p2wsh,
13+
)
14+
from test_framework.blocktools import witness_script, send_to_witness
715
from test_framework.test_framework import BitcoinTestFramework
816
from test_framework.util import *
917
from test_framework.mininode import sha256, CTransaction, CTxIn, COutPoint, CTxOut, COIN, ToHex, FromHex
10-
from test_framework.address import script_to_p2sh, key_to_p2pkh, key_to_p2sh_p2wpkh, key_to_p2wpkh, script_to_p2sh_p2wsh, script_to_p2wsh, program_to_witness
18+
from test_framework.address import script_to_p2sh, key_to_p2pkh
1119
from test_framework.script import CScript, OP_HASH160, OP_CHECKSIG, OP_0, hash160, OP_EQUAL, OP_DUP, OP_EQUALVERIFY, OP_1, OP_2, OP_CHECKMULTISIG, OP_TRUE
1220
from io import BytesIO
1321

@@ -16,52 +24,6 @@
1624
WIT_V0 = 0
1725
WIT_V1 = 1
1826

19-
# Create a scriptPubKey corresponding to either a P2WPKH output for the
20-
# given pubkey, or a P2WSH output of a 1-of-1 multisig for the given
21-
# pubkey. Returns the hex encoding of the scriptPubKey.
22-
def witness_script(use_p2wsh, pubkey):
23-
if (use_p2wsh == False):
24-
# P2WPKH instead
25-
pubkeyhash = hash160(hex_str_to_bytes(pubkey))
26-
pkscript = CScript([OP_0, pubkeyhash])
27-
else:
28-
# 1-of-1 multisig
29-
witness_program = CScript([OP_1, hex_str_to_bytes(pubkey), OP_1, OP_CHECKMULTISIG])
30-
scripthash = sha256(witness_program)
31-
pkscript = CScript([OP_0, scripthash])
32-
return bytes_to_hex_str(pkscript)
33-
34-
# Return a transaction (in hex) that spends the given utxo to a segwit output,
35-
# optionally wrapping the segwit output using P2SH.
36-
def create_witness_tx(node, use_p2wsh, utxo, pubkey, encode_p2sh, amount):
37-
if use_p2wsh:
38-
program = CScript([OP_1, hex_str_to_bytes(pubkey), OP_1, OP_CHECKMULTISIG])
39-
addr = script_to_p2sh_p2wsh(program) if encode_p2sh else script_to_p2wsh(program)
40-
else:
41-
addr = key_to_p2sh_p2wpkh(pubkey) if encode_p2sh else key_to_p2wpkh(pubkey)
42-
if not encode_p2sh:
43-
assert_equal(node.validateaddress(addr)['scriptPubKey'], witness_script(use_p2wsh, pubkey))
44-
return node.createrawtransaction([utxo], {addr: amount})
45-
46-
# Create a transaction spending a given utxo to a segwit output corresponding
47-
# to the given pubkey: use_p2wsh determines whether to use P2WPKH or P2WSH;
48-
# encode_p2sh determines whether to wrap in P2SH.
49-
# sign=True will have the given node sign the transaction.
50-
# insert_redeem_script will be added to the scriptSig, if given.
51-
def send_to_witness(use_p2wsh, node, utxo, pubkey, encode_p2sh, amount, sign=True, insert_redeem_script=""):
52-
tx_to_witness = create_witness_tx(node, use_p2wsh, utxo, pubkey, encode_p2sh, amount)
53-
if (sign):
54-
signed = node.signrawtransaction(tx_to_witness)
55-
assert("errors" not in signed or len(["errors"]) == 0)
56-
return node.sendrawtransaction(signed["hex"])
57-
else:
58-
if (insert_redeem_script):
59-
tx = FromHex(CTransaction(), tx_to_witness)
60-
tx.vin[0].scriptSig += CScript([hex_str_to_bytes(insert_redeem_script)])
61-
tx_to_witness = ToHex(tx)
62-
63-
return node.sendrawtransaction(tx_to_witness)
64-
6527
def getutxo(txid):
6628
utxo = {}
6729
utxo["vout"] = 0

test/functional/test_framework/blocktools.py

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,27 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Utilities for manipulating blocks and transactions."""
66

7+
from .address import (
8+
key_to_p2sh_p2wpkh,
9+
key_to_p2sh_p2wpkh,
10+
key_to_p2wpkh,
11+
script_to_p2sh_p2wsh,
12+
script_to_p2wsh,
13+
)
714
from .mininode import *
8-
from .script import CScript, OP_TRUE, OP_CHECKSIG, OP_RETURN
15+
from .script import (
16+
CScript,
17+
OP_0,
18+
OP_1,
19+
OP_CHECKMULTISIG,
20+
OP_CHECKSIG,
21+
OP_EQUAL,
22+
OP_HASH160,
23+
OP_RETURN,
24+
OP_TRUE,
25+
hash160,
26+
)
27+
from .util import assert_equal
928

1029
# Create a block (with regtest difficulty)
1130
def create_block(hashprev, coinbase, nTime=None):
@@ -108,3 +127,49 @@ def get_legacy_sigopcount_tx(tx, fAccurate=True):
108127
# scriptSig might be of type bytes, so convert to CScript for the moment
109128
count += CScript(j.scriptSig).GetSigOpCount(fAccurate)
110129
return count
130+
131+
# Create a scriptPubKey corresponding to either a P2WPKH output for the
132+
# given pubkey, or a P2WSH output of a 1-of-1 multisig for the given
133+
# pubkey. Returns the hex encoding of the scriptPubKey.
134+
def witness_script(use_p2wsh, pubkey):
135+
if (use_p2wsh == False):
136+
# P2WPKH instead
137+
pubkeyhash = hash160(hex_str_to_bytes(pubkey))
138+
pkscript = CScript([OP_0, pubkeyhash])
139+
else:
140+
# 1-of-1 multisig
141+
witness_program = CScript([OP_1, hex_str_to_bytes(pubkey), OP_1, OP_CHECKMULTISIG])
142+
scripthash = sha256(witness_program)
143+
pkscript = CScript([OP_0, scripthash])
144+
return bytes_to_hex_str(pkscript)
145+
146+
# Return a transaction (in hex) that spends the given utxo to a segwit output,
147+
# optionally wrapping the segwit output using P2SH.
148+
def create_witness_tx(node, use_p2wsh, utxo, pubkey, encode_p2sh, amount):
149+
if use_p2wsh:
150+
program = CScript([OP_1, hex_str_to_bytes(pubkey), OP_1, OP_CHECKMULTISIG])
151+
addr = script_to_p2sh_p2wsh(program) if encode_p2sh else script_to_p2wsh(program)
152+
else:
153+
addr = key_to_p2sh_p2wpkh(pubkey) if encode_p2sh else key_to_p2wpkh(pubkey)
154+
if not encode_p2sh:
155+
assert_equal(node.validateaddress(addr)['scriptPubKey'], witness_script(use_p2wsh, pubkey))
156+
return node.createrawtransaction([utxo], {addr: amount})
157+
158+
# Create a transaction spending a given utxo to a segwit output corresponding
159+
# to the given pubkey: use_p2wsh determines whether to use P2WPKH or P2WSH;
160+
# encode_p2sh determines whether to wrap in P2SH.
161+
# sign=True will have the given node sign the transaction.
162+
# insert_redeem_script will be added to the scriptSig, if given.
163+
def send_to_witness(use_p2wsh, node, utxo, pubkey, encode_p2sh, amount, sign=True, insert_redeem_script=""):
164+
tx_to_witness = create_witness_tx(node, use_p2wsh, utxo, pubkey, encode_p2sh, amount)
165+
if (sign):
166+
signed = node.signrawtransaction(tx_to_witness)
167+
assert("errors" not in signed or len(["errors"]) == 0)
168+
return node.sendrawtransaction(signed["hex"])
169+
else:
170+
if (insert_redeem_script):
171+
tx = FromHex(CTransaction(), tx_to_witness)
172+
tx.vin[0].scriptSig += CScript([hex_str_to_bytes(insert_redeem_script)])
173+
tx_to_witness = ToHex(tx)
174+
175+
return node.sendrawtransaction(tx_to_witness)

0 commit comments

Comments
 (0)