Skip to content

Commit 3cc13ea

Browse files
committed
Merge #9970: Improve readability of segwit.py, smartfees.py
1269b8a Fix logging bug and improve readability of smartfees.py (Suhas Daftuar) b9f34e8 Improve readability of segwit.py (Suhas Daftuar) Tree-SHA512: 2c8ff61678c6c407a95a6530e9bd650ae6bb7c9e52f6dd5f256e19253a1358dd1a7aa33a9639fcb07f443e3a21dae71b9f0865c5f1fcaacb2097a3c6766c7eef
2 parents 67ed40e + 1269b8a commit 3cc13ea

File tree

3 files changed

+74
-74
lines changed

3 files changed

+74
-74
lines changed

qa/rpc-tests/bumpfee.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def test_segwit_bumpfee_succeeds(rbf_node, dest_address):
102102
segwit_out = rbf_node.validateaddress(rbf_node.getnewaddress())
103103
rbf_node.addwitnessaddress(segwit_out["address"])
104104
segwitid = send_to_witness(
105-
version=0,
105+
use_p2wsh=False,
106106
node=rbf_node,
107107
utxo=segwit_in,
108108
pubkey=segwit_out["pubkey"],

qa/rpc-tests/segwit.py

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,59 +6,61 @@
66

77
from test_framework.test_framework import BitcoinTestFramework
88
from test_framework.util import *
9-
from test_framework.mininode import sha256, ripemd160, CTransaction, CTxIn, COutPoint, CTxOut
9+
from test_framework.mininode import sha256, ripemd160, CTransaction, CTxIn, COutPoint, CTxOut, COIN
1010
from test_framework.address import script_to_p2sh, key_to_p2pkh
11-
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
11+
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, hash160
1212
from io import BytesIO
13-
from test_framework.mininode import FromHex
13+
from test_framework.mininode import FromHex, ToHex
1414

1515
NODE_0 = 0
1616
NODE_1 = 1
1717
NODE_2 = 2
1818
WIT_V0 = 0
1919
WIT_V1 = 1
2020

21-
def witness_script(version, pubkey):
22-
if (version == 0):
23-
pubkeyhash = bytes_to_hex_str(ripemd160(sha256(hex_str_to_bytes(pubkey))))
24-
pkscript = "0014" + pubkeyhash
25-
elif (version == 1):
26-
# 1-of-1 multisig
27-
scripthash = bytes_to_hex_str(sha256(hex_str_to_bytes("5121" + pubkey + "51ae")))
28-
pkscript = "0020" + scripthash
21+
# Create a scriptPubKey corresponding to either a P2WPKH output for the
22+
# given pubkey, or a P2WSH output of a 1-of-1 multisig for the given
23+
# pubkey. Returns the hex encoding of the scriptPubKey.
24+
def witness_script(use_p2wsh, pubkey):
25+
if (use_p2wsh == False):
26+
# P2WPKH instead
27+
pubkeyhash = hash160(hex_str_to_bytes(pubkey))
28+
pkscript = CScript([OP_0, pubkeyhash])
2929
else:
30-
assert("Wrong version" == "0 or 1")
31-
return pkscript
32-
33-
def addlength(script):
34-
scriptlen = format(len(script)//2, 'x')
35-
assert(len(scriptlen) == 2)
36-
return scriptlen + script
37-
38-
def create_witnessprogram(version, node, utxo, pubkey, encode_p2sh, amount):
39-
pkscript = witness_script(version, pubkey)
30+
# 1-of-1 multisig
31+
witness_program = CScript([OP_1, hex_str_to_bytes(pubkey), OP_1, OP_CHECKMULTISIG])
32+
scripthash = sha256(witness_program)
33+
pkscript = CScript([OP_0, scripthash])
34+
return bytes_to_hex_str(pkscript)
35+
36+
# Return a transaction (in hex) that spends the given utxo to a segwit output,
37+
# optionally wrapping the segwit output using P2SH.
38+
def create_witnessprogram(use_p2wsh, utxo, pubkey, encode_p2sh, amount):
39+
pkscript = hex_str_to_bytes(witness_script(use_p2wsh, pubkey))
4040
if (encode_p2sh):
41-
p2sh_hash = bytes_to_hex_str(ripemd160(sha256(hex_str_to_bytes(pkscript))))
42-
pkscript = "a914"+p2sh_hash+"87"
43-
inputs = []
44-
outputs = {}
45-
inputs.append({ "txid" : utxo["txid"], "vout" : utxo["vout"]} )
46-
DUMMY_P2SH = "2MySexEGVzZpRgNQ1JdjdP5bRETznm3roQ2" # P2SH of "OP_1 OP_DROP"
47-
outputs[DUMMY_P2SH] = amount
48-
tx_to_witness = node.createrawtransaction(inputs,outputs)
49-
#replace dummy output with our own
50-
tx_to_witness = tx_to_witness[0:110] + addlength(pkscript) + tx_to_witness[-8:]
51-
return tx_to_witness
52-
53-
def send_to_witness(version, node, utxo, pubkey, encode_p2sh, amount, sign=True, insert_redeem_script=""):
54-
tx_to_witness = create_witnessprogram(version, node, utxo, pubkey, encode_p2sh, amount)
41+
p2sh_hash = hash160(pkscript)
42+
pkscript = CScript([OP_HASH160, p2sh_hash, OP_EQUAL])
43+
tx = CTransaction()
44+
tx.vin.append(CTxIn(COutPoint(int(utxo["txid"], 16), utxo["vout"]), b""))
45+
tx.vout.append(CTxOut(int(amount*COIN), pkscript))
46+
return ToHex(tx)
47+
48+
# Create a transaction spending a given utxo to a segwit output corresponding
49+
# to the given pubkey: use_p2wsh determines whether to use P2WPKH or P2WSH;
50+
# encode_p2sh determines whether to wrap in P2SH.
51+
# sign=True will have the given node sign the transaction.
52+
# insert_redeem_script will be added to the scriptSig, if given.
53+
def send_to_witness(use_p2wsh, node, utxo, pubkey, encode_p2sh, amount, sign=True, insert_redeem_script=""):
54+
tx_to_witness = create_witnessprogram(use_p2wsh, utxo, pubkey, encode_p2sh, amount)
5555
if (sign):
5656
signed = node.signrawtransaction(tx_to_witness)
5757
assert("errors" not in signed or len(["errors"]) == 0)
5858
return node.sendrawtransaction(signed["hex"])
5959
else:
6060
if (insert_redeem_script):
61-
tx_to_witness = tx_to_witness[0:82] + addlength(insert_redeem_script) + tx_to_witness[84:]
61+
tx = FromHex(CTransaction(), tx_to_witness)
62+
tx.vin[0].scriptSig += CScript([hex_str_to_bytes(insert_redeem_script)])
63+
tx_to_witness = ToHex(tx)
6264

6365
return node.sendrawtransaction(tx_to_witness)
6466

@@ -180,8 +182,8 @@ def run_test(self):
180182
self.fail_accept(self.nodes[0], p2sh_ids[NODE_0][WIT_V0][0], False)
181183
self.fail_accept(self.nodes[0], p2sh_ids[NODE_0][WIT_V1][0], False)
182184
# unsigned with redeem script
183-
self.fail_accept(self.nodes[0], p2sh_ids[NODE_0][WIT_V0][0], False, addlength(witness_script(0, self.pubkey[0])))
184-
self.fail_accept(self.nodes[0], p2sh_ids[NODE_0][WIT_V1][0], False, addlength(witness_script(1, self.pubkey[0])))
185+
self.fail_accept(self.nodes[0], p2sh_ids[NODE_0][WIT_V0][0], False, witness_script(False, self.pubkey[0]))
186+
self.fail_accept(self.nodes[0], p2sh_ids[NODE_0][WIT_V1][0], False, witness_script(True, self.pubkey[0]))
185187
# signed
186188
self.fail_accept(self.nodes[0], wit_ids[NODE_0][WIT_V0][0], True)
187189
self.fail_accept(self.nodes[0], wit_ids[NODE_0][WIT_V1][0], True)
@@ -205,8 +207,8 @@ def run_test(self):
205207
self.fail_accept(self.nodes[2], p2sh_ids[NODE_2][WIT_V1][1], False)
206208

207209
self.log.info("Verify unsigned p2sh witness txs with a redeem script in versionbits-settings blocks are valid before the fork")
208-
self.success_mine(self.nodes[2], p2sh_ids[NODE_2][WIT_V0][1], False, addlength(witness_script(0, self.pubkey[2]))) #block 430
209-
self.success_mine(self.nodes[2], p2sh_ids[NODE_2][WIT_V1][1], False, addlength(witness_script(1, self.pubkey[2]))) #block 431
210+
self.success_mine(self.nodes[2], p2sh_ids[NODE_2][WIT_V0][1], False, witness_script(False, self.pubkey[2])) #block 430
211+
self.success_mine(self.nodes[2], p2sh_ids[NODE_2][WIT_V1][1], False, witness_script(True, self.pubkey[2])) #block 431
210212

211213
self.log.info("Verify previous witness txs skipped for mining can now be mined")
212214
assert_equal(len(self.nodes[2].getrawmempool()), 4)
@@ -230,8 +232,8 @@ def run_test(self):
230232
self.log.info("Verify witness txs without witness data are invalid after the fork")
231233
self.fail_mine(self.nodes[2], wit_ids[NODE_2][WIT_V0][2], False)
232234
self.fail_mine(self.nodes[2], wit_ids[NODE_2][WIT_V1][2], False)
233-
self.fail_mine(self.nodes[2], p2sh_ids[NODE_2][WIT_V0][2], False, addlength(witness_script(0, self.pubkey[2])))
234-
self.fail_mine(self.nodes[2], p2sh_ids[NODE_2][WIT_V1][2], False, addlength(witness_script(1, self.pubkey[2])))
235+
self.fail_mine(self.nodes[2], p2sh_ids[NODE_2][WIT_V0][2], False, witness_script(False, self.pubkey[2]))
236+
self.fail_mine(self.nodes[2], p2sh_ids[NODE_2][WIT_V1][2], False, witness_script(True, self.pubkey[2]))
235237

236238
self.log.info("Verify default node can now use witness txs")
237239
self.success_mine(self.nodes[0], wit_ids[NODE_0][WIT_V0][0], True) #block 432

qa/rpc-tests/smartfees.py

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@
77
from collections import OrderedDict
88
from test_framework.test_framework import BitcoinTestFramework
99
from test_framework.util import *
10+
from test_framework.script import CScript, OP_1, OP_DROP, OP_2, OP_HASH160, OP_EQUAL, hash160, OP_TRUE
11+
from test_framework.mininode import CTransaction, CTxIn, CTxOut, COutPoint, ToHex, FromHex, COIN
1012

1113
# Construct 2 trivial P2SH's and the ScriptSigs that spend them
1214
# So we can create many many transactions without needing to spend
1315
# time signing.
14-
P2SH_1 = "2MySexEGVzZpRgNQ1JdjdP5bRETznm3roQ2" # P2SH of "OP_1 OP_DROP"
15-
P2SH_2 = "2NBdpwq8Aoo1EEKEXPNrKvr5xQr3M9UfcZA" # P2SH of "OP_2 OP_DROP"
16+
redeem_script_1 = CScript([OP_1, OP_DROP])
17+
redeem_script_2 = CScript([OP_2, OP_DROP])
18+
P2SH_1 = CScript([OP_HASH160, hash160(redeem_script_1), OP_EQUAL])
19+
P2SH_2 = CScript([OP_HASH160, hash160(redeem_script_2), OP_EQUAL])
20+
1621
# Associated ScriptSig's to spend satisfy P2SH_1 and P2SH_2
17-
# 4 bytes of OP_TRUE and push 2-byte redeem script of "OP_1 OP_DROP" or "OP_2 OP_DROP"
18-
SCRIPT_SIG = ["0451025175", "0451025275"]
22+
SCRIPT_SIG = [CScript([OP_TRUE, redeem_script_1]), CScript([OP_TRUE, redeem_script_2])]
1923

2024
global log
2125

@@ -35,39 +39,30 @@ def small_txpuzzle_randfee(from_node, conflist, unconflist, amount, min_fee, fee
3539
rand_fee = float(fee_increment)*(1.1892**random.randint(0,28))
3640
# Total fee ranges from min_fee to min_fee + 127*fee_increment
3741
fee = min_fee - fee_increment + satoshi_round(rand_fee)
38-
inputs = []
42+
tx = CTransaction()
3943
total_in = Decimal("0.00000000")
4044
while total_in <= (amount + fee) and len(conflist) > 0:
4145
t = conflist.pop(0)
4246
total_in += t["amount"]
43-
inputs.append({ "txid" : t["txid"], "vout" : t["vout"]} )
47+
tx.vin.append(CTxIn(COutPoint(int(t["txid"], 16), t["vout"]), b""))
4448
if total_in <= amount + fee:
4549
while total_in <= (amount + fee) and len(unconflist) > 0:
4650
t = unconflist.pop(0)
4751
total_in += t["amount"]
48-
inputs.append({ "txid" : t["txid"], "vout" : t["vout"]} )
52+
tx.vin.append(CTxIn(COutPoint(int(t["txid"], 16), t["vout"]), b""))
4953
if total_in <= amount + fee:
5054
raise RuntimeError("Insufficient funds: need %d, have %d"%(amount+fee, total_in))
51-
outputs = {}
52-
outputs = OrderedDict([(P2SH_1, total_in - amount - fee),
53-
(P2SH_2, amount)])
54-
rawtx = from_node.createrawtransaction(inputs, outputs)
55-
# createrawtransaction constructs a transaction that is ready to be signed.
56-
# These transactions don't need to be signed, but we still have to insert the ScriptSig
57-
# that will satisfy the ScriptPubKey.
58-
completetx = rawtx[0:10]
59-
inputnum = 0
60-
for inp in inputs:
61-
completetx += rawtx[10+82*inputnum:82+82*inputnum]
62-
completetx += SCRIPT_SIG[inp["vout"]]
63-
completetx += rawtx[84+82*inputnum:92+82*inputnum]
64-
inputnum += 1
65-
completetx += rawtx[10+82*inputnum:]
66-
txid = from_node.sendrawtransaction(completetx, True)
55+
tx.vout.append(CTxOut(int((total_in - amount - fee)*COIN), P2SH_1))
56+
tx.vout.append(CTxOut(int(amount*COIN), P2SH_2))
57+
# These transactions don't need to be signed, but we still have to insert
58+
# the ScriptSig that will satisfy the ScriptPubKey.
59+
for inp in tx.vin:
60+
inp.scriptSig = SCRIPT_SIG[inp.prevout.n]
61+
txid = from_node.sendrawtransaction(ToHex(tx), True)
6762
unconflist.append({ "txid" : txid, "vout" : 0 , "amount" : total_in - amount - fee})
6863
unconflist.append({ "txid" : txid, "vout" : 1 , "amount" : amount})
6964

70-
return (completetx, fee)
65+
return (ToHex(tx), fee)
7166

7267
def split_inputs(from_node, txins, txouts, initial_split = False):
7368
"""
@@ -78,18 +73,21 @@ def split_inputs(from_node, txins, txouts, initial_split = False):
7873
a high coin age when the notion of priority still existed.
7974
"""
8075
prevtxout = txins.pop()
81-
inputs = []
82-
inputs.append({ "txid" : prevtxout["txid"], "vout" : prevtxout["vout"] })
76+
tx = CTransaction()
77+
tx.vin.append(CTxIn(COutPoint(int(prevtxout["txid"], 16), prevtxout["vout"]), b""))
78+
8379
half_change = satoshi_round(prevtxout["amount"]/2)
8480
rem_change = prevtxout["amount"] - half_change - Decimal("0.00001000")
85-
outputs = OrderedDict([(P2SH_1, half_change), (P2SH_2, rem_change)])
86-
rawtx = from_node.createrawtransaction(inputs, outputs)
81+
tx.vout.append(CTxOut(int(half_change*COIN), P2SH_1))
82+
tx.vout.append(CTxOut(int(rem_change*COIN), P2SH_2))
83+
8784
# If this is the initial split we actually need to sign the transaction
88-
# Otherwise we just need to insert the property ScriptSig
85+
# Otherwise we just need to insert the proper ScriptSig
8986
if (initial_split) :
90-
completetx = from_node.signrawtransaction(rawtx)["hex"]
87+
completetx = from_node.signrawtransaction(ToHex(tx))["hex"]
9188
else :
92-
completetx = rawtx[0:82] + SCRIPT_SIG[prevtxout["vout"]] + rawtx[84:]
89+
tx.vin[0].scriptSig = SCRIPT_SIG[prevtxout["vout"]]
90+
completetx = ToHex(tx)
9391
txid = from_node.sendrawtransaction(completetx, True)
9492
txouts.append({ "txid" : txid, "vout" : 0 , "amount" : half_change})
9593
txouts.append({ "txid" : txid, "vout" : 1 , "amount" : rem_change})

0 commit comments

Comments
 (0)