-
Notifications
You must be signed in to change notification settings - Fork 27
/
test_tx_unbalanced.py
472 lines (405 loc) · 16.9 KB
/
test_tx_unbalanced.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
"""Tests for unbalanced transactions."""
import logging
import typing as tp
import allure
import hypothesis
import hypothesis.strategies as st
import pytest
from cardano_clusterlib import clusterlib
from cardano_node_tests.cluster_management import cluster_management
from cardano_node_tests.tests import common
from cardano_node_tests.tests import tx_common
from cardano_node_tests.utils import clusterlib_utils
from cardano_node_tests.utils import helpers
from cardano_node_tests.utils.versions import VERSIONS
LOGGER = logging.getLogger(__name__)
MAX_LOVELACE_AMOUNT = common.MAX_UINT64
@pytest.mark.testnets
@pytest.mark.smoke
class TestUnbalanced:
"""Tests for unbalanced transactions."""
def _build_transfer_amount_bellow_minimum(
self,
cluster: clusterlib.ClusterLib,
temp_template: str,
payment_addrs: tp.List[clusterlib.AddressRecord],
pbt_highest_utxo: clusterlib.UTXOData,
amount: int,
):
src_address = payment_addrs[0].address
dst_address = payment_addrs[1].address
err_str = ""
try:
cluster.cli(
[
"transaction",
"build",
"--tx-in",
f"{pbt_highest_utxo.utxo_hash}#{pbt_highest_utxo.utxo_ix}",
"--change-address",
src_address,
"--tx-out",
f"{dst_address}+{amount}",
"--out-file",
f"{temp_template}_tx.body",
*cluster.g_transaction.tx_era_arg,
*cluster.magic_args,
]
)
except clusterlib.CLIError as err:
err_str = str(err)
if amount < 0:
assert "Negative quantity" in err_str, err_str
else:
assert "Minimum UTxO threshold not met for tx output" in err_str, err_str
@pytest.fixture
def payment_addrs(
self,
cluster_manager: cluster_management.ClusterManager,
cluster: clusterlib.ClusterLib,
) -> tp.List[clusterlib.AddressRecord]:
"""Create 2 new payment addresses."""
with cluster_manager.cache_fixture() as fixture_cache:
if fixture_cache.value:
return fixture_cache.value # type: ignore
addrs = clusterlib_utils.create_payment_addr_records(
f"addr_not_balanced_ci{cluster_manager.cluster_instance_num}_0",
f"addr_not_balanced_ci{cluster_manager.cluster_instance_num}_1",
cluster_obj=cluster,
)
fixture_cache.value = addrs
# fund source addresses
clusterlib_utils.fund_from_faucet(
addrs[0],
cluster_obj=cluster,
faucet_data=cluster_manager.cache.addrs_data["user1"],
)
return addrs
@pytest.fixture
def pbt_highest_utxo(
self,
cluster: clusterlib.ClusterLib,
payment_addrs: tp.List[clusterlib.AddressRecord],
) -> clusterlib.UTXOData:
"""Get UTxO with highest amount of Lovelace.
Meant for property-based tests, so this expensive operation gets executed only once.
"""
return cluster.g_query.get_utxo_with_highest_amount(payment_addrs[0].address)
@allure.link(helpers.get_vcs_link())
def test_negative_change(
self,
cluster: clusterlib.ClusterLib,
payment_addrs: tp.List[clusterlib.AddressRecord],
):
"""Try to build a transaction with a negative change.
Check that it is not possible to build such transaction.
"""
temp_template = common.get_test_id(cluster)
src_address = payment_addrs[0].address
dst_address = payment_addrs[1].address
tx_files = clusterlib.TxFiles(signing_key_files=[payment_addrs[0].skey_file])
ttl = cluster.g_transaction.calculate_tx_ttl()
fee = cluster.g_transaction.calculate_tx_fee(
src_address=src_address,
tx_name=temp_template,
dst_addresses=[dst_address],
tx_files=tx_files,
ttl=ttl,
)
src_addr_highest_utxo = cluster.g_query.get_utxo_with_highest_amount(src_address)
# use only the UTxO with the highest amount
txins = [src_addr_highest_utxo]
# try to transfer +1 Lovelace more than available and use a negative change (-1)
txouts = [
clusterlib.TxOut(address=dst_address, amount=src_addr_highest_utxo.amount - fee + 1),
clusterlib.TxOut(address=src_address, amount=-1),
]
assert txins[0].amount - txouts[0].amount - fee == txouts[-1].amount
with pytest.raises(clusterlib.CLIError) as excinfo:
cluster.g_transaction.build_raw_tx_bare(
out_file=f"{helpers.get_timestamped_rand_str()}_tx.body",
txins=txins,
txouts=txouts,
tx_files=tx_files,
fee=fee,
ttl=ttl,
)
exc_val = str(excinfo.value)
assert (
"option --tx-out: Failed reading" in exc_val
or "TxOutAdaOnly" in exc_val
or "AdaAssetId,-1" in exc_val
or "Negative quantity" in exc_val # cardano-node >= 8.7.0
)
@allure.link(helpers.get_vcs_link())
@common.SKIPIF_BUILD_UNUSABLE
@hypothesis.given(transfer_add=st.integers(min_value=1, max_value=MAX_LOVELACE_AMOUNT // 2))
@hypothesis.example(transfer_add=1)
@common.hypothesis_settings(100)
def test_build_transfer_unavailable_funds(
self,
cluster: clusterlib.ClusterLib,
payment_addrs: tp.List[clusterlib.AddressRecord],
pbt_highest_utxo: clusterlib.UTXOData,
transfer_add: int,
):
"""Try to build a transaction with more funds than available `transaction build`.
Check that it is not possible to build such transaction.
"""
temp_template = f"{common.get_test_id(cluster)}_{common.unique_time_str()}"
src_address = payment_addrs[0].address
dst_address = payment_addrs[1].address
tx_files = clusterlib.TxFiles(signing_key_files=[payment_addrs[0].skey_file])
# use only the UTxO with the highest amount
txins = [pbt_highest_utxo]
amount = min(MAX_LOVELACE_AMOUNT, pbt_highest_utxo.amount + transfer_add)
# try to transfer whole balance
txouts = [clusterlib.TxOut(address=dst_address, amount=amount)]
with pytest.raises(clusterlib.CLIError) as excinfo:
cluster.g_transaction.build_tx(
src_address=src_address,
tx_name=temp_template,
txins=txins,
txouts=txouts,
tx_files=tx_files,
)
exc_val = str(excinfo.value)
assert "The net balance of the transaction is negative" in exc_val, exc_val
@allure.link(helpers.get_vcs_link())
@hypothesis.given(change_amount=st.integers(min_value=2_000_000, max_value=MAX_LOVELACE_AMOUNT))
@hypothesis.example(change_amount=2_000_000)
@hypothesis.example(change_amount=MAX_LOVELACE_AMOUNT)
@common.hypothesis_settings(300)
def test_wrong_balance(
self,
cluster: clusterlib.ClusterLib,
payment_addrs: tp.List[clusterlib.AddressRecord],
pbt_highest_utxo: clusterlib.UTXOData,
change_amount: int,
):
"""Build a transaction with unbalanced change (property-based test).
* build an unbalanced transaction
* check that it is not possible to submit such transaction
"""
temp_template = f"{common.get_test_id(cluster)}_{common.unique_time_str()}"
src_address = payment_addrs[0].address
dst_address = payment_addrs[1].address
fee = 200_000
transferred_amount = pbt_highest_utxo.amount - fee
out_file_tx = f"{temp_template}_tx.body"
tx_files = clusterlib.TxFiles(signing_key_files=[payment_addrs[0].skey_file])
ttl = cluster.g_transaction.calculate_tx_ttl()
# use only the UTxO with the highest amount
txins = [pbt_highest_utxo]
txouts = [
clusterlib.TxOut(address=dst_address, amount=transferred_amount),
# Add the value from test's parameter to unbalance the transaction. Since the correct
# change amount here is 0, the value from test's parameter can be used directly.
clusterlib.TxOut(address=src_address, amount=change_amount),
]
cluster.g_transaction.build_raw_tx_bare(
out_file=out_file_tx,
txins=txins,
txouts=txouts,
tx_files=tx_files,
fee=fee,
ttl=ttl,
)
out_file_signed = cluster.g_transaction.sign_tx(
tx_body_file=out_file_tx,
signing_key_files=tx_files.signing_key_files,
tx_name=temp_template,
)
# it should NOT be possible to submit an unbalanced transaction
with pytest.raises(clusterlib.CLIError) as excinfo:
cluster.g_transaction.submit_tx_bare(out_file_signed)
exc_val = str(excinfo.value)
# TODO: see https://github.com/IntersectMBO/cardano-node/issues/2555
assert "ValueNotConservedUTxO" in exc_val or "DeserialiseFailure" in exc_val, exc_val
@allure.link(helpers.get_vcs_link())
@hypothesis.given(change_amount=st.integers(min_value=MAX_LOVELACE_AMOUNT + 1))
@hypothesis.example(change_amount=2_000_000)
@hypothesis.example(change_amount=MAX_LOVELACE_AMOUNT + 1)
@common.hypothesis_settings(300)
def test_out_of_bounds_amount(
self,
cluster: clusterlib.ClusterLib,
payment_addrs: tp.List[clusterlib.AddressRecord],
pbt_highest_utxo: clusterlib.UTXOData,
change_amount: int,
):
"""Try to build a transaction with output Lovelace amount that is out of bounds."""
temp_template = f"{common.get_test_id(cluster)}_{common.unique_time_str()}"
fee = 200_000
out_file_tx = f"{temp_template}_tx.body"
tx_files = clusterlib.TxFiles(signing_key_files=[payment_addrs[0].skey_file])
ttl = cluster.g_transaction.calculate_tx_ttl()
# use only the UTxO with the highest amount
txins = [pbt_highest_utxo]
txouts = [
clusterlib.TxOut(address=payment_addrs[0].address, amount=change_amount),
]
try:
cluster.g_transaction.build_raw_tx_bare(
out_file=out_file_tx,
txins=txins,
txouts=txouts,
tx_files=tx_files,
fee=fee,
ttl=ttl,
)
except clusterlib.CLIError as exc:
exc_val = str(exc)
assert "out of bounds" in exc_val or "exceeds the max bound" in exc_val, exc_val
@allure.link(helpers.get_vcs_link())
@common.SKIPIF_BUILD_UNUSABLE
# See https://github.com/IntersectMBO/cardano-node/issues/4061
@hypothesis.given(
amount=st.integers(min_value=0, max_value=tx_common.MIN_UTXO_VALUE[1] - 1_000)
)
@hypothesis.example(amount=0)
@hypothesis.example(amount=tx_common.MIN_UTXO_VALUE[1] - 1_000)
@common.hypothesis_settings(max_examples=200)
def test_build_transfer_amount_bellow_minimum(
self,
cluster: clusterlib.ClusterLib,
payment_addrs: tp.List[clusterlib.AddressRecord],
pbt_highest_utxo: clusterlib.UTXOData,
amount: int,
):
"""Try to build a transaction with amount bellow the minimum lovelace required.
Uses `cardano-cli transaction build` command for building the transactions.
Expect failure.
"""
temp_template = f"{common.get_test_id(cluster)}_{common.unique_time_str()}"
self._build_transfer_amount_bellow_minimum(
cluster=cluster,
temp_template=temp_template,
payment_addrs=payment_addrs,
pbt_highest_utxo=pbt_highest_utxo,
amount=amount,
)
@allure.link(helpers.get_vcs_link())
@common.SKIPIF_BUILD_UNUSABLE
@hypothesis.given(amount=st.integers(min_value=-MAX_LOVELACE_AMOUNT, max_value=-1))
@hypothesis.example(amount=-MAX_LOVELACE_AMOUNT)
@hypothesis.example(amount=-1)
@common.hypothesis_settings(max_examples=300)
def test_build_transfer_negative_amount(
self,
cluster: clusterlib.ClusterLib,
payment_addrs: tp.List[clusterlib.AddressRecord],
pbt_highest_utxo: clusterlib.UTXOData,
amount: int,
):
"""Try to build a transaction with negative Lovelace amount.
Uses `cardano-cli transaction build` command for building the transactions.
Expect failure.
"""
temp_template = f"{common.get_test_id(cluster)}_{common.unique_time_str()}"
self._build_transfer_amount_bellow_minimum(
cluster=cluster,
temp_template=temp_template,
payment_addrs=payment_addrs,
pbt_highest_utxo=pbt_highest_utxo,
amount=amount,
)
@allure.link(helpers.get_vcs_link())
# See https://github.com/IntersectMBO/cardano-node/issues/4061
@hypothesis.given(
amount=st.integers(min_value=0, max_value=tx_common.MIN_UTXO_VALUE[1] - 1_000)
)
@hypothesis.example(amount=0)
@hypothesis.example(amount=tx_common.MIN_UTXO_VALUE[1] - 1_000)
@common.hypothesis_settings(max_examples=400)
def test_transfer_amount_bellow_minimum(
self,
cluster: clusterlib.ClusterLib,
payment_addrs: tp.List[clusterlib.AddressRecord],
pbt_highest_utxo: clusterlib.UTXOData,
amount: int,
):
"""Try to build a transaction with amount bellow the minimum lovelace required.
Uses `cardano-cli transaction build-raw` command for building the transactions.
Expect failure.
"""
temp_template = f"{common.get_test_id(cluster)}_{common.unique_time_str()}"
src_address = payment_addrs[0].address
dst_address = payment_addrs[1].address
fee = 200_000
out_file = f"{temp_template}.body"
build_args = [
"transaction",
"build-raw",
"--fee",
f"{fee}",
"--tx-in",
f"{pbt_highest_utxo.utxo_hash}#{pbt_highest_utxo.utxo_ix}",
"--tx-out",
f"{dst_address}+{amount}",
"--tx-out",
f"{src_address}+{pbt_highest_utxo.amount - amount - fee}",
*cluster.g_transaction.tx_era_arg,
"--out-file",
out_file,
]
if VERSIONS.transaction_era < VERSIONS.ALLEGRA:
build_args.extend(
["--invalid-hereafter", str(cluster.g_transaction.calculate_tx_ttl())]
)
cluster.cli(build_args)
# create signed transaction
out_file_signed = cluster.g_transaction.sign_tx(
tx_body_file=out_file,
signing_key_files=[payment_addrs[0].skey_file],
tx_name=f"{temp_template}_signed",
)
with pytest.raises(clusterlib.CLIError) as excinfo_build:
# submit the signed transaction
cluster.g_transaction.submit_tx(tx_file=out_file_signed, txins=[pbt_highest_utxo])
exc_val = str(excinfo_build.value)
assert "OutputTooSmallUTxO" in exc_val, exc_val
@allure.link(helpers.get_vcs_link())
@hypothesis.given(amount=st.integers(min_value=-MAX_LOVELACE_AMOUNT, max_value=-1))
@hypothesis.example(amount=-1)
@hypothesis.example(amount=-MAX_LOVELACE_AMOUNT)
@common.hypothesis_settings(max_examples=500)
def test_transfer_negative_amount(
self,
cluster: clusterlib.ClusterLib,
payment_addrs: tp.List[clusterlib.AddressRecord],
pbt_highest_utxo: clusterlib.UTXOData,
amount: int,
):
"""Try to build a transaction with negative Lovelace amount.
Uses `cardano-cli transaction build-raw` command for building the transactions.
Expect failure.
"""
temp_template = f"{common.get_test_id(cluster)}_{common.unique_time_str()}"
src_address = payment_addrs[0].address
dst_address = payment_addrs[1].address
fee = 200_000
out_file = f"{temp_template}.body"
build_args = [
"transaction",
"build-raw",
"--fee",
f"{fee}",
"--tx-in",
f"{pbt_highest_utxo.utxo_hash}#{pbt_highest_utxo.utxo_ix}",
"--tx-out",
f"{dst_address}+{amount}",
"--tx-out",
f"{src_address}+2000000",
*cluster.g_transaction.tx_era_arg,
"--out-file",
out_file,
]
if VERSIONS.transaction_era < VERSIONS.ALLEGRA:
build_args.extend(
["--invalid-hereafter", str(cluster.g_transaction.calculate_tx_ttl())]
)
with pytest.raises(clusterlib.CLIError) as excinfo_build:
cluster.cli(build_args)
err_str_build = str(excinfo_build.value)
assert "Negative quantity" in err_str_build, err_str_build