Skip to content

Commit 1269b8a

Browse files
committed
Fix logging bug and improve readability of smartfees.py
1 parent b9f34e8 commit 1269b8a

File tree

1 file changed

+29
-31
lines changed

1 file changed

+29
-31
lines changed

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)