16
16
import pytest
17
17
18
18
from btclib .exceptions import BTClibValueError
19
- from btclib .script .engine import verify_input , verify_transaction
19
+ from btclib .script .engine import verify_amounts , verify_input , verify_transaction
20
20
from btclib .script .witness import Witness
21
- from btclib .tx . tx import Tx
21
+ from btclib .tx import OutPoint , Tx , TxIn
22
22
from btclib .tx .tx_out import ScriptPubKey , TxOut
23
23
from tests .script_engine import parse_script
24
24
@@ -107,13 +107,19 @@ def test_valid_legacy() -> None:
107
107
if f in flags :
108
108
flags .remove (f )
109
109
110
+ check_amounts = True
111
+
110
112
prevouts = []
111
113
for i in x [0 ]:
112
114
amount = 0 if len (i ) == 3 else i [3 ]
115
+ if not amount :
116
+ check_amounts = False
113
117
script_pub_key = parse_script (i [2 ])
114
118
prevouts .append (TxOut (amount , ScriptPubKey (script_pub_key )))
115
119
116
- verify_transaction (prevouts , tx , flags if flags != ["NONE" ] else None )
120
+ verify_transaction (
121
+ prevouts , tx , flags if flags != ["NONE" ] else None , check_amounts
122
+ )
117
123
118
124
119
125
def test_invalid_legacy () -> None :
@@ -136,13 +142,29 @@ def test_invalid_legacy() -> None:
136
142
137
143
flags = x [2 ].split ("," ) # different flags handling
138
144
145
+ check_amounts = True
146
+
139
147
prevouts = []
140
148
for i in x [0 ]:
141
149
amount = 0 if len (i ) == 3 else i [3 ]
150
+ if not amount :
151
+ check_amounts = False
142
152
with warnings .catch_warnings ():
143
153
warnings .simplefilter ("ignore" )
144
154
script_pub_key = parse_script (i [2 ])
145
155
prevouts .append (TxOut (amount , ScriptPubKey (script_pub_key )))
146
156
147
157
with pytest .raises ((BTClibValueError , IndexError , KeyError )):
148
- verify_transaction (prevouts , tx , flags if flags != ["NONE" ] else None )
158
+ verify_transaction (
159
+ prevouts , tx , flags if flags != ["NONE" ] else None , check_amounts
160
+ )
161
+
162
+
163
+ def test_invalid_amount () -> None :
164
+ prevout = TxOut (0 , ScriptPubKey ("" ))
165
+
166
+ tx = Tx (vin = [TxIn (OutPoint (b"1" * 32 , 1 ))], vout = [TxOut (10 , ScriptPubKey ("" ))])
167
+
168
+ # Output amount greater than sum of inputs
169
+ with pytest .raises (BTClibValueError ):
170
+ verify_amounts ([prevout ], tx )
0 commit comments