7
7
from collections import OrderedDict
8
8
from test_framework .test_framework import BitcoinTestFramework
9
9
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
10
12
11
13
# Construct 2 trivial P2SH's and the ScriptSigs that spend them
12
14
# So we can create many many transactions without needing to spend
13
15
# 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
+
16
21
# 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 ])]
19
23
20
24
global log
21
25
@@ -35,39 +39,30 @@ def small_txpuzzle_randfee(from_node, conflist, unconflist, amount, min_fee, fee
35
39
rand_fee = float (fee_increment )* (1.1892 ** random .randint (0 ,28 ))
36
40
# Total fee ranges from min_fee to min_fee + 127*fee_increment
37
41
fee = min_fee - fee_increment + satoshi_round (rand_fee )
38
- inputs = []
42
+ tx = CTransaction ()
39
43
total_in = Decimal ("0.00000000" )
40
44
while total_in <= (amount + fee ) and len (conflist ) > 0 :
41
45
t = conflist .pop (0 )
42
46
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"" ) )
44
48
if total_in <= amount + fee :
45
49
while total_in <= (amount + fee ) and len (unconflist ) > 0 :
46
50
t = unconflist .pop (0 )
47
51
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"" ) )
49
53
if total_in <= amount + fee :
50
54
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 )
67
62
unconflist .append ({ "txid" : txid , "vout" : 0 , "amount" : total_in - amount - fee })
68
63
unconflist .append ({ "txid" : txid , "vout" : 1 , "amount" : amount })
69
64
70
- return (completetx , fee )
65
+ return (ToHex ( tx ) , fee )
71
66
72
67
def split_inputs (from_node , txins , txouts , initial_split = False ):
73
68
"""
@@ -78,18 +73,21 @@ def split_inputs(from_node, txins, txouts, initial_split = False):
78
73
a high coin age when the notion of priority still existed.
79
74
"""
80
75
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
+
83
79
half_change = satoshi_round (prevtxout ["amount" ]/ 2 )
84
80
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
+
87
84
# 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
89
86
if (initial_split ) :
90
- completetx = from_node .signrawtransaction (rawtx )["hex" ]
87
+ completetx = from_node .signrawtransaction (ToHex ( tx ) )["hex" ]
91
88
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 )
93
91
txid = from_node .sendrawtransaction (completetx , True )
94
92
txouts .append ({ "txid" : txid , "vout" : 0 , "amount" : half_change })
95
93
txouts .append ({ "txid" : txid , "vout" : 1 , "amount" : rem_change })
0 commit comments