-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathconftest.py
1226 lines (948 loc) · 43.3 KB
/
conftest.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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# flake8: noqa E402 # See imports after multiprocessing.set_start_method
from __future__ import annotations
import asyncio
import dataclasses
import datetime
import functools
import json
import math
import multiprocessing
import os
import random
import sysconfig
import tempfile
from contextlib import AsyncExitStack
from enum import Enum
from typing import Any, AsyncIterator, Callable, Dict, Iterator, List, Tuple, Union
import aiohttp
import pytest
# TODO: update after resolution in https://github.com/pytest-dev/pytest/issues/7469
from _pytest.fixtures import SubRequest
from pytest import MonkeyPatch
import tests
from chia.clvm.spend_sim import CostLogger
from chia.consensus.constants import ConsensusConstants
from chia.full_node.full_node import FullNode
from chia.full_node.full_node_api import FullNodeAPI
from chia.rpc.farmer_rpc_client import FarmerRpcClient
from chia.rpc.harvester_rpc_client import HarvesterRpcClient
from chia.rpc.wallet_rpc_client import WalletRpcClient
from chia.seeder.crawler import Crawler
from chia.seeder.crawler_api import CrawlerAPI
from chia.seeder.dns_server import DNSServer
from chia.server.server import ChiaServer
from chia.server.start_service import Service
from chia.simulator.full_node_simulator import FullNodeSimulator
from chia.simulator.setup_services import (
setup_crawler,
setup_daemon,
setup_full_node,
setup_introducer,
setup_seeder,
setup_timelord,
)
from chia.simulator.wallet_tools import WalletTool
from chia.timelord.timelord import Timelord
from chia.timelord.timelord_api import TimelordAPI
# Set spawn after stdlib imports, but before other imports
from chia.types.aliases import (
CrawlerService,
DataLayerService,
FarmerService,
FullNodeService,
HarvesterService,
SimulatorFullNodeService,
TimelordService,
WalletService,
)
from chia.types.peer_info import PeerInfo
from chia.util.config import create_default_chia_config, lock_and_load_config
from chia.util.db_wrapper import generate_in_memory_db_uri
from chia.util.ints import uint16, uint32, uint64
from chia.util.keychain import Keychain
from chia.util.task_timing import main as task_instrumentation_main
from chia.util.task_timing import start_task_instrumentation, stop_task_instrumentation
from chia.wallet.wallet_node import WalletNode
from chia.wallet.wallet_node_api import WalletNodeAPI
from tests import ether
from tests.core.data_layer.util import ChiaRoot
from tests.core.node_height import node_height_at_least
from tests.simulation.test_simulation import test_constants_modified
from tests.util.misc import BenchmarkRunner, GcMode, RecordingWebServer, TestId, _AssertRuntime, measure_overhead
from tests.util.setup_nodes import (
OldSimulatorsAndWallets,
SimulatorsAndWallets,
setup_full_system,
setup_n_nodes,
setup_simulators_and_wallets,
setup_simulators_and_wallets_service,
setup_two_nodes,
)
from tests.util.time_out_assert import time_out_assert
multiprocessing.set_start_method("spawn")
from pathlib import Path
from chia.simulator.block_tools import BlockTools, create_block_tools_async, test_constants
from chia.simulator.keyring import TempKeyring
from chia.util.keyring_wrapper import KeyringWrapper
from tests.util.setup_nodes import setup_farmer_multi_harvester
@pytest.fixture(name="ether_setup", autouse=True)
def ether_setup_fixture(request: SubRequest, record_property: Callable[[str, object], None]) -> Iterator[None]:
with MonkeyPatch.context() as monkeypatch_context:
monkeypatch_context.setattr(ether, "record_property", record_property)
monkeypatch_context.setattr(ether, "test_id", TestId.create(node=request.node))
yield
@pytest.fixture(autouse=True)
def ether_test_id_property_fixture(ether_setup: None, record_property: Callable[[str, object], None]) -> None:
assert ether.test_id is not None, "ether.test_id is None, did you forget to use the ether_setup fixture?"
record_property("test_id", json.dumps(ether.test_id.marshal(), ensure_ascii=True, sort_keys=True))
def make_old_setup_simulators_and_wallets(new: SimulatorsAndWallets) -> OldSimulatorsAndWallets:
return (
[simulator.peer_api for simulator in new.simulators],
[(wallet.node, wallet.peer_server) for wallet in new.wallets],
new.bt,
)
@pytest.fixture(scope="session")
def anyio_backend():
return "asyncio"
@pytest.fixture(name="event_loop")
async def event_loop_fixture() -> asyncio.events.AbstractEventLoop:
return asyncio.get_running_loop()
@pytest.fixture(name="seeded_random")
def seeded_random_fixture() -> random.Random:
seeded_random = random.Random()
seeded_random.seed(a=0, version=2)
return seeded_random
@pytest.fixture(name="benchmark_runner_overhead", scope="session")
def benchmark_runner_overhead_fixture() -> float:
return measure_overhead(
manager_maker=functools.partial(
_AssertRuntime,
gc_mode=GcMode.nothing,
seconds=math.inf,
print=False,
),
cycles=100,
)
@pytest.fixture(name="benchmark_runner")
def benchmark_runner_fixture(
benchmark_runner_overhead: float,
benchmark_repeat: int,
) -> BenchmarkRunner:
return BenchmarkRunner(
test_id=ether.test_id,
overhead=benchmark_runner_overhead,
)
@pytest.fixture(name="node_name_for_file")
def node_name_for_file_fixture(request: SubRequest) -> str:
# TODO: handle other characters banned on windows
return request.node.name.replace(os.sep, "_")
@pytest.fixture(name="test_time_for_file")
def test_time_for_file_fixture(request: SubRequest) -> str:
return datetime.datetime.now().isoformat().replace(":", "_")
@pytest.fixture(name="task_instrumentation")
def task_instrumentation_fixture(node_name_for_file: str, test_time_for_file: str) -> Iterator[None]:
target_directory = f"task-profile-{node_name_for_file}-{test_time_for_file}"
start_task_instrumentation()
yield
stop_task_instrumentation(target_dir=target_directory)
task_instrumentation_main(args=[target_directory])
@pytest.fixture(scope="session")
def get_keychain():
with TempKeyring() as keychain:
yield keychain
KeyringWrapper.cleanup_shared_instance()
class ConsensusMode(Enum):
PLAIN = 0
HARD_FORK_2_0 = 1
@pytest.fixture(
scope="session",
params=[ConsensusMode.PLAIN, ConsensusMode.HARD_FORK_2_0],
)
def consensus_mode(request):
return request.param
@pytest.fixture(scope="session")
def blockchain_constants(consensus_mode) -> ConsensusConstants:
if consensus_mode == ConsensusMode.PLAIN:
return test_constants
if consensus_mode == ConsensusMode.HARD_FORK_2_0:
return dataclasses.replace(
test_constants,
HARD_FORK_HEIGHT=uint32(2),
HARD_FORK_FIX_HEIGHT=uint32(2),
PLOT_FILTER_128_HEIGHT=uint32(10),
PLOT_FILTER_64_HEIGHT=uint32(15),
PLOT_FILTER_32_HEIGHT=uint32(20),
)
raise AssertionError("Invalid Blockchain mode in simulation")
@pytest.fixture(scope="session", name="bt")
async def block_tools_fixture(get_keychain, blockchain_constants, anyio_backend) -> BlockTools:
# Note that this causes a lot of CPU and disk traffic - disk, DB, ports, process creation ...
_shared_block_tools = await create_block_tools_async(constants=blockchain_constants, keychain=get_keychain)
return _shared_block_tools
# if you have a system that has an unusual hostname for localhost and you want
# to run the tests, change the `self_hostname` fixture
@pytest.fixture(scope="session")
def self_hostname():
return "127.0.0.1"
# NOTE:
# Instantiating the bt fixture results in an attempt to create the chia root directory
# which the build scripts symlink to a sometimes-not-there directory.
# When not there, Python complains since, well, the symlink is not a directory nor points to a directory.
#
# Now that we have removed the global at tests.setup_nodes.bt, we can move the imports out of
# the fixtures below. Just be aware of the filesystem modification during bt fixture creation
@pytest.fixture(scope="function")
async def empty_blockchain(latest_db_version, blockchain_constants):
"""
Provides a list of 10 valid blocks, as well as a blockchain with 9 blocks added to it.
"""
from tests.util.blockchain import create_blockchain
async with create_blockchain(blockchain_constants, latest_db_version) as (bc1, db_wrapper):
yield bc1
@pytest.fixture(scope="function")
def latest_db_version() -> int:
return 2
@pytest.fixture(scope="function", params=[2])
def db_version(request) -> int:
return request.param
SOFTFORK_HEIGHTS = [1000000, 5496000, 5496100]
@pytest.fixture(scope="function", params=SOFTFORK_HEIGHTS)
def softfork_height(request) -> int:
return request.param
saved_blocks_version = "2.0"
@pytest.fixture(scope="session")
def default_400_blocks(bt, consensus_mode):
version = ""
if consensus_mode == ConsensusMode.HARD_FORK_2_0:
version = "_hardfork"
from tests.util.blockchain import persistent_blocks
return persistent_blocks(400, f"test_blocks_400_{saved_blocks_version}{version}.db", bt, seed=b"400")
@pytest.fixture(scope="session")
def default_1000_blocks(bt, consensus_mode):
version = ""
if consensus_mode == ConsensusMode.HARD_FORK_2_0:
version = "_hardfork"
from tests.util.blockchain import persistent_blocks
return persistent_blocks(1000, f"test_blocks_1000_{saved_blocks_version}{version}.db", bt, seed=b"1000")
@pytest.fixture(scope="session")
def pre_genesis_empty_slots_1000_blocks(bt, consensus_mode):
version = ""
if consensus_mode == ConsensusMode.HARD_FORK_2_0:
version = "_hardfork"
from tests.util.blockchain import persistent_blocks
return persistent_blocks(
1000,
f"pre_genesis_empty_slots_1000_blocks{saved_blocks_version}{version}.db",
bt,
seed=b"empty_slots",
empty_sub_slots=1,
)
@pytest.fixture(scope="session")
def default_1500_blocks(bt, consensus_mode):
version = ""
if consensus_mode == ConsensusMode.HARD_FORK_2_0:
version = "_hardfork"
from tests.util.blockchain import persistent_blocks
return persistent_blocks(1500, f"test_blocks_1500_{saved_blocks_version}{version}.db", bt, seed=b"1500")
@pytest.fixture(scope="session")
def default_10000_blocks(bt, consensus_mode):
from tests.util.blockchain import persistent_blocks
version = ""
if consensus_mode == ConsensusMode.HARD_FORK_2_0:
version = "_hardfork"
return persistent_blocks(
10000,
f"test_blocks_10000_{saved_blocks_version}{version}.db",
bt,
seed=b"10000",
dummy_block_references=True,
)
# this long reorg chain shares the first 500 blocks with "default_10000_blocks"
# and has heavier weight blocks
@pytest.fixture(scope="session")
def test_long_reorg_blocks(bt, consensus_mode, default_10000_blocks):
version = ""
if consensus_mode == ConsensusMode.HARD_FORK_2_0:
version = "_hardfork"
from tests.util.blockchain import persistent_blocks
return persistent_blocks(
4500,
f"test_blocks_long_reorg_{saved_blocks_version}{version}.db",
bt,
block_list_input=default_10000_blocks[:500],
seed=b"reorg_blocks",
time_per_block=8,
dummy_block_references=True,
include_transactions=True,
)
# this long reorg chain shares the first 500 blocks with "default_10000_blocks"
# and has the same weight blocks
@pytest.fixture(scope="session")
def test_long_reorg_blocks_light(bt, consensus_mode, default_10000_blocks):
version = ""
if consensus_mode == ConsensusMode.HARD_FORK_2_0:
version = "_hardfork"
from tests.util.blockchain import persistent_blocks
return persistent_blocks(
4500,
f"test_blocks_long_reorg_light_{saved_blocks_version}{version}.db",
bt,
block_list_input=default_10000_blocks[:500],
seed=b"reorg_blocks2",
dummy_block_references=True,
include_transactions=True,
)
@pytest.fixture(scope="session")
def default_2000_blocks_compact(bt, consensus_mode):
version = ""
if consensus_mode == ConsensusMode.HARD_FORK_2_0:
version = "_hardfork"
from tests.util.blockchain import persistent_blocks
return persistent_blocks(
2000,
f"test_blocks_2000_compact_{saved_blocks_version}{version}.db",
bt,
normalized_to_identity_cc_eos=True,
normalized_to_identity_icc_eos=True,
normalized_to_identity_cc_ip=True,
normalized_to_identity_cc_sp=True,
seed=b"2000_compact",
)
@pytest.fixture(scope="session")
def default_10000_blocks_compact(bt, consensus_mode):
from tests.util.blockchain import persistent_blocks
version = ""
if consensus_mode == ConsensusMode.HARD_FORK_2_0:
version = "_hardfork"
return persistent_blocks(
10000,
f"test_blocks_10000_compact_{saved_blocks_version}{version}.db",
bt,
normalized_to_identity_cc_eos=True,
normalized_to_identity_icc_eos=True,
normalized_to_identity_cc_ip=True,
normalized_to_identity_cc_sp=True,
seed=b"1000_compact",
)
@pytest.fixture(scope="function")
def tmp_dir():
with tempfile.TemporaryDirectory() as folder:
yield Path(folder)
# For the below see https://stackoverflow.com/a/62563106/15133773
if os.getenv("_PYTEST_RAISE", "0") != "0":
@pytest.hookimpl(tryfirst=True)
def pytest_exception_interact(call):
raise call.excinfo.value
@pytest.hookimpl(tryfirst=True)
def pytest_internalerror(excinfo):
raise excinfo.value
def pytest_addoption(parser: pytest.Parser):
default_repeats = 1
group = parser.getgroup("chia")
group.addoption(
"--benchmark-repeats",
action="store",
default=default_repeats,
type=int,
help=f"The number of times to run each benchmark, default {default_repeats}.",
)
group.addoption(
"--time-out-assert-repeats",
action="store",
default=default_repeats,
type=int,
help=f"The number of times to run each test with time out asserts, default {default_repeats}.",
)
def pytest_configure(config):
config.addinivalue_line("markers", "benchmark: automatically assigned by the benchmark_runner fixture")
benchmark_repeats = config.getoption("--benchmark-repeats")
if benchmark_repeats != 1:
@pytest.fixture(
name="benchmark_repeat",
params=[pytest.param(repeat, id=f"benchmark_repeat{repeat:03d}") for repeat in range(benchmark_repeats)],
)
def benchmark_repeat_fixture(request: SubRequest) -> int:
return request.param
else:
@pytest.fixture(
name="benchmark_repeat",
)
def benchmark_repeat_fixture() -> int:
return 1
globals()[benchmark_repeat_fixture.__name__] = benchmark_repeat_fixture
time_out_assert_repeats = config.getoption("--time-out-assert-repeats")
if time_out_assert_repeats != 1:
@pytest.fixture(
name="time_out_assert_repeat",
autouse=True,
params=[
pytest.param(repeat, id=f"time_out_assert_repeat{repeat:03d}")
for repeat in range(time_out_assert_repeats)
],
)
def time_out_assert_repeat_fixture(request: SubRequest) -> int:
return request.param
globals()[time_out_assert_repeat_fixture.__name__] = time_out_assert_repeat_fixture
def pytest_collection_modifyitems(session, config: pytest.Config, items: List[pytest.Function]):
# https://github.com/pytest-dev/pytest/issues/3730#issuecomment-567142496
removed = []
kept = []
all_error_lines: List[str] = []
limit_consensus_modes_problems: List[str] = []
for item in items:
limit_consensus_modes_marker = item.get_closest_marker("limit_consensus_modes")
if limit_consensus_modes_marker is not None:
callspec = getattr(item, "callspec", None)
if callspec is None:
limit_consensus_modes_problems.append(item.name)
continue
mode = callspec.params.get("consensus_mode")
if mode is None:
limit_consensus_modes_problems.append(item.name)
continue
modes = limit_consensus_modes_marker.kwargs.get("allowed", [ConsensusMode.PLAIN])
if mode not in modes:
removed.append(item)
continue
kept.append(item)
if removed:
config.hook.pytest_deselected(items=removed)
items[:] = kept
if len(limit_consensus_modes_problems) > 0:
all_error_lines.append("@pytest.mark.limit_consensus_modes used without consensus_mode:")
all_error_lines.extend(f" {line}" for line in limit_consensus_modes_problems)
benchmark_problems: List[str] = []
for item in items:
existing_benchmark_mark = item.get_closest_marker("benchmark")
if existing_benchmark_mark is not None:
benchmark_problems.append(item.name)
if "benchmark_runner" in getattr(item, "fixturenames", ()):
item.add_marker("benchmark")
if len(benchmark_problems) > 0:
all_error_lines.append("use the benchmark_runner fixture, not @pytest.mark.benchmark:")
all_error_lines.extend(f" {line}" for line in benchmark_problems)
if len(all_error_lines) > 0:
all_error_lines.insert(0, "custom chia collection rules failed")
raise Exception("\n".join(all_error_lines))
@pytest.fixture(scope="function")
async def node_with_params(request, blockchain_constants: ConsensusConstants) -> AsyncIterator[FullNodeSimulator]:
params = {}
if request:
params = request.param
async with setup_simulators_and_wallets(1, 0, blockchain_constants, **params) as new:
yield new.simulators[0].peer_api
@pytest.fixture(scope="function")
async def two_nodes(db_version: int, self_hostname, blockchain_constants: ConsensusConstants):
async with setup_two_nodes(blockchain_constants, db_version=db_version, self_hostname=self_hostname) as _:
yield _
@pytest.fixture(scope="function")
async def setup_two_nodes_fixture(db_version: int, blockchain_constants: ConsensusConstants):
async with setup_simulators_and_wallets(2, 0, blockchain_constants, db_version=db_version) as new:
yield make_old_setup_simulators_and_wallets(new=new)
@pytest.fixture(scope="function")
async def three_nodes(db_version: int, self_hostname, blockchain_constants):
async with setup_n_nodes(blockchain_constants, 3, db_version=db_version, self_hostname=self_hostname) as _:
yield _
@pytest.fixture(scope="function")
async def five_nodes(db_version: int, self_hostname, blockchain_constants):
async with setup_n_nodes(blockchain_constants, 5, db_version=db_version, self_hostname=self_hostname) as _:
yield _
@pytest.fixture(scope="function")
async def wallet_nodes(blockchain_constants, consensus_mode):
constants = blockchain_constants
async with setup_simulators_and_wallets(
2,
1,
dataclasses.replace(blockchain_constants, MEMPOOL_BLOCK_BUFFER=1, MAX_BLOCK_COST_CLVM=400000000),
) as new:
(nodes, wallets, bt) = make_old_setup_simulators_and_wallets(new=new)
full_node_1 = nodes[0]
full_node_2 = nodes[1]
server_1 = full_node_1.full_node.server
server_2 = full_node_2.full_node.server
wallet_a = bt.get_pool_wallet_tool()
wallet_receiver = WalletTool(full_node_1.full_node.constants)
yield full_node_1, full_node_2, server_1, server_2, wallet_a, wallet_receiver, bt
@pytest.fixture(scope="function")
async def setup_four_nodes(db_version, blockchain_constants: ConsensusConstants):
async with setup_simulators_and_wallets(4, 0, blockchain_constants, db_version=db_version) as new:
yield make_old_setup_simulators_and_wallets(new=new)
@pytest.fixture(scope="function")
async def two_nodes_sim_and_wallets_services(blockchain_constants, consensus_mode):
async with setup_simulators_and_wallets_service(2, 0, blockchain_constants) as _:
yield _
@pytest.fixture(scope="function")
async def one_wallet_and_one_simulator_services(blockchain_constants: ConsensusConstants):
async with setup_simulators_and_wallets_service(1, 1, blockchain_constants) as _:
yield _
@pytest.fixture(scope="function")
async def wallet_node_100_pk(blockchain_constants: ConsensusConstants):
async with setup_simulators_and_wallets(1, 1, blockchain_constants, initial_num_public_keys=100) as new:
yield make_old_setup_simulators_and_wallets(new=new)
@pytest.fixture(scope="function")
async def simulator_and_wallet(
blockchain_constants: ConsensusConstants,
) -> AsyncIterator[Tuple[List[FullNodeSimulator], List[Tuple[WalletNode, ChiaServer]], BlockTools]]:
async with setup_simulators_and_wallets(1, 1, blockchain_constants) as new:
yield make_old_setup_simulators_and_wallets(new=new)
@pytest.fixture(scope="function")
async def two_wallet_nodes(request, blockchain_constants: ConsensusConstants):
params = {}
if request and request.param_index > 0:
params = request.param
async with setup_simulators_and_wallets(1, 2, blockchain_constants, **params) as new:
yield make_old_setup_simulators_and_wallets(new=new)
@pytest.fixture(scope="function")
async def two_wallet_nodes_services(
blockchain_constants: ConsensusConstants,
) -> AsyncIterator[Tuple[List[SimulatorFullNodeService], List[WalletService], BlockTools]]:
async with setup_simulators_and_wallets_service(1, 2, blockchain_constants) as _:
yield _
@pytest.fixture(scope="function")
async def two_wallet_nodes_custom_spam_filtering(
spam_filter_after_n_txs, xch_spam_amount, blockchain_constants: ConsensusConstants
):
async with setup_simulators_and_wallets(
1, 2, blockchain_constants, spam_filter_after_n_txs, xch_spam_amount
) as new:
yield make_old_setup_simulators_and_wallets(new=new)
@pytest.fixture(scope="function")
async def three_sim_two_wallets(blockchain_constants: ConsensusConstants):
async with setup_simulators_and_wallets(3, 2, blockchain_constants) as new:
yield make_old_setup_simulators_and_wallets(new=new)
@pytest.fixture(scope="function")
async def setup_two_nodes_and_wallet(blockchain_constants: ConsensusConstants):
async with setup_simulators_and_wallets(2, 1, blockchain_constants, db_version=2) as new:
yield make_old_setup_simulators_and_wallets(new=new)
@pytest.fixture(scope="function")
async def setup_two_nodes_and_wallet_fast_retry(blockchain_constants: ConsensusConstants):
async with setup_simulators_and_wallets(
1, 1, blockchain_constants, config_overrides={"wallet.tx_resend_timeout_secs": 1}, db_version=2
) as new:
yield make_old_setup_simulators_and_wallets(new=new)
@pytest.fixture(scope="function")
async def three_wallet_nodes(blockchain_constants: ConsensusConstants):
async with setup_simulators_and_wallets(1, 3, blockchain_constants) as new:
yield make_old_setup_simulators_and_wallets(new=new)
@pytest.fixture(scope="function")
async def wallet_two_node_simulator(blockchain_constants: ConsensusConstants):
async with setup_simulators_and_wallets(2, 1, blockchain_constants) as new:
yield make_old_setup_simulators_and_wallets(new=new)
@pytest.fixture(scope="function")
async def wallet_nodes_mempool_perf(bt):
key_seed = bt.farmer_master_sk_entropy
async with setup_simulators_and_wallets(1, 1, bt.constants, key_seed=key_seed) as new:
yield make_old_setup_simulators_and_wallets(new=new)
@pytest.fixture(scope="function")
async def two_nodes_two_wallets_with_same_keys(bt) -> AsyncIterator[OldSimulatorsAndWallets]:
key_seed = bt.farmer_master_sk_entropy
async with setup_simulators_and_wallets(2, 2, bt.constants, key_seed=key_seed) as new:
yield make_old_setup_simulators_and_wallets(new=new)
@pytest.fixture
async def wallet_nodes_perf(blockchain_constants: ConsensusConstants):
async with setup_simulators_and_wallets(
1, 1, blockchain_constants, config_overrides={"MEMPOOL_BLOCK_BUFFER": 1, "MAX_BLOCK_COST_CLVM": 11000000000}
) as new:
(nodes, wallets, bt) = make_old_setup_simulators_and_wallets(new=new)
full_node_1 = nodes[0]
server_1 = full_node_1.full_node.server
wallet_a = bt.get_pool_wallet_tool()
wallet_receiver = WalletTool(full_node_1.full_node.constants)
yield full_node_1, server_1, wallet_a, wallet_receiver, bt
@pytest.fixture(scope="function")
async def three_nodes_two_wallets(blockchain_constants: ConsensusConstants):
async with setup_simulators_and_wallets(3, 2, blockchain_constants) as new:
yield make_old_setup_simulators_and_wallets(new=new)
@pytest.fixture(scope="function")
async def one_node(
blockchain_constants: ConsensusConstants,
) -> AsyncIterator[Tuple[List[Service], List[FullNodeSimulator], BlockTools]]:
async with setup_simulators_and_wallets_service(1, 0, blockchain_constants) as _:
yield _
@pytest.fixture(scope="function")
async def one_node_one_block(
blockchain_constants: ConsensusConstants,
) -> AsyncIterator[Tuple[Union[FullNodeAPI, FullNodeSimulator], ChiaServer, BlockTools]]:
async with setup_simulators_and_wallets(1, 0, blockchain_constants) as new:
(nodes, _, bt) = make_old_setup_simulators_and_wallets(new=new)
full_node_1 = nodes[0]
server_1 = full_node_1.full_node.server
wallet_a = bt.get_pool_wallet_tool()
reward_ph = wallet_a.get_new_puzzlehash()
blocks = bt.get_consecutive_blocks(
1,
guarantee_transaction_block=True,
farmer_reward_puzzle_hash=reward_ph,
pool_reward_puzzle_hash=reward_ph,
genesis_timestamp=uint64(10000),
time_per_block=10,
)
assert blocks[0].height == 0
for block in blocks:
await full_node_1.full_node.add_block(block)
await time_out_assert(60, node_height_at_least, True, full_node_1, blocks[-1].height)
yield full_node_1, server_1, bt
@pytest.fixture(scope="function")
async def two_nodes_one_block(blockchain_constants: ConsensusConstants):
async with setup_simulators_and_wallets(2, 0, blockchain_constants) as new:
(nodes, _, bt) = make_old_setup_simulators_and_wallets(new=new)
full_node_1 = nodes[0]
full_node_2 = nodes[1]
server_1 = full_node_1.full_node.server
server_2 = full_node_2.full_node.server
wallet_a = bt.get_pool_wallet_tool()
reward_ph = wallet_a.get_new_puzzlehash()
blocks = bt.get_consecutive_blocks(
1,
guarantee_transaction_block=True,
farmer_reward_puzzle_hash=reward_ph,
pool_reward_puzzle_hash=reward_ph,
genesis_timestamp=uint64(10000),
time_per_block=10,
)
assert blocks[0].height == 0
for block in blocks:
await full_node_1.full_node.add_block(block)
await time_out_assert(60, node_height_at_least, True, full_node_1, blocks[-1].height)
yield full_node_1, full_node_2, server_1, server_2, bt
@pytest.fixture(scope="function")
async def farmer_one_harvester_simulator_wallet(
tmp_path: Path,
blockchain_constants: ConsensusConstants,
) -> AsyncIterator[Tuple[HarvesterService, FarmerService, SimulatorFullNodeService, WalletService, BlockTools,]]:
async with setup_simulators_and_wallets_service(1, 1, blockchain_constants) as (nodes, wallets, bt):
async with setup_farmer_multi_harvester(bt, 1, tmp_path, bt.constants, start_services=True) as (
harvester_services,
farmer_service,
_,
):
yield harvester_services[0], farmer_service, nodes[0], wallets[0], bt
FarmerOneHarvester = Tuple[List[HarvesterService], FarmerService, BlockTools]
@pytest.fixture(scope="function")
async def farmer_one_harvester(tmp_path: Path, get_b_tools: BlockTools) -> AsyncIterator[FarmerOneHarvester]:
async with setup_farmer_multi_harvester(get_b_tools, 1, tmp_path, get_b_tools.constants, start_services=True) as _:
yield _
@pytest.fixture(scope="function")
async def farmer_one_harvester_not_started(
tmp_path: Path, get_b_tools: BlockTools
) -> AsyncIterator[Tuple[List[Service], Service]]:
async with setup_farmer_multi_harvester(get_b_tools, 1, tmp_path, get_b_tools.constants, start_services=False) as _:
yield _
@pytest.fixture(scope="function")
async def farmer_two_harvester_not_started(
tmp_path: Path, get_b_tools: BlockTools
) -> AsyncIterator[Tuple[List[Service], Service]]:
async with setup_farmer_multi_harvester(get_b_tools, 2, tmp_path, get_b_tools.constants, start_services=False) as _:
yield _
@pytest.fixture(scope="function")
async def farmer_three_harvester_not_started(
tmp_path: Path, get_b_tools: BlockTools
) -> AsyncIterator[Tuple[List[Service], Service]]:
async with setup_farmer_multi_harvester(get_b_tools, 3, tmp_path, get_b_tools.constants, start_services=False) as _:
yield _
@pytest.fixture(scope="function")
async def get_daemon(bt):
async with setup_daemon(btools=bt) as _:
yield _
@pytest.fixture(scope="function")
def empty_keyring():
with TempKeyring(user="user-chia-1.8", service="chia-user-chia-1.8") as keychain:
yield keychain
KeyringWrapper.cleanup_shared_instance()
@pytest.fixture(scope="function")
def get_temp_keyring():
with TempKeyring() as keychain:
yield keychain
@pytest.fixture(scope="function")
async def get_b_tools_1(get_temp_keyring):
return await create_block_tools_async(constants=test_constants_modified, keychain=get_temp_keyring)
@pytest.fixture(scope="function")
async def get_b_tools(get_temp_keyring):
local_b_tools = await create_block_tools_async(constants=test_constants_modified, keychain=get_temp_keyring)
new_config = local_b_tools._config
local_b_tools.change_config(new_config)
return local_b_tools
@pytest.fixture(scope="function")
async def daemon_connection_and_temp_keychain(
get_b_tools: BlockTools,
) -> AsyncIterator[Tuple[aiohttp.ClientWebSocketResponse, Keychain]]:
async with setup_daemon(btools=get_b_tools) as daemon:
keychain = daemon.keychain_server._default_keychain
async with aiohttp.ClientSession() as session:
async with session.ws_connect(
f"wss://127.0.0.1:{get_b_tools._config['daemon_port']}",
autoclose=True,
autoping=True,
ssl=get_b_tools.get_daemon_ssl_context(),
max_msg_size=52428800,
) as ws:
yield ws, keychain
@pytest.fixture(scope="function")
async def wallets_prefarm_services(two_wallet_nodes_services, self_hostname, trusted, request):
"""
Sets up the node with 10 blocks, and returns a payer and payee wallet.
"""
try:
farm_blocks = request.param
except AttributeError:
farm_blocks = 3
buffer = 1
full_nodes, wallets, bt = two_wallet_nodes_services
full_node_api = full_nodes[0]._api
full_node_server = full_node_api.server
wallet_service_0 = wallets[0]
wallet_service_1 = wallets[1]
wallet_node_0 = wallet_service_0._node
wallet_node_1 = wallet_service_1._node
wallet_0 = wallet_node_0.wallet_state_manager.main_wallet
wallet_1 = wallet_node_1.wallet_state_manager.main_wallet
if trusted:
wallet_node_0.config["trusted_peers"] = {full_node_server.node_id.hex(): full_node_server.node_id.hex()}
wallet_node_1.config["trusted_peers"] = {full_node_server.node_id.hex(): full_node_server.node_id.hex()}
else:
wallet_node_0.config["trusted_peers"] = {}
wallet_node_1.config["trusted_peers"] = {}
wallet_0_rpc_client = await WalletRpcClient.create(
bt.config["self_hostname"],
wallet_service_0.rpc_server.listen_port,
wallet_service_0.root_path,
wallet_service_0.config,
)
wallet_1_rpc_client = await WalletRpcClient.create(
bt.config["self_hostname"],
wallet_service_1.rpc_server.listen_port,
wallet_service_1.root_path,
wallet_service_1.config,
)
await wallet_node_0.server.start_client(PeerInfo(self_hostname, full_node_server.get_port()), None)
await wallet_node_1.server.start_client(PeerInfo(self_hostname, full_node_server.get_port()), None)
wallet_0_rewards = await full_node_api.farm_blocks_to_wallet(count=farm_blocks, wallet=wallet_0)
wallet_1_rewards = await full_node_api.farm_blocks_to_wallet(count=farm_blocks, wallet=wallet_1)
await full_node_api.farm_blocks_to_puzzlehash(count=buffer, guarantee_transaction_blocks=True)
await full_node_api.wait_for_wallets_synced(wallet_nodes=[wallet_node_0, wallet_node_1], timeout=30)
assert await wallet_0.get_confirmed_balance() == wallet_0_rewards
assert await wallet_0.get_unconfirmed_balance() == wallet_0_rewards
assert await wallet_1.get_confirmed_balance() == wallet_1_rewards
assert await wallet_1.get_unconfirmed_balance() == wallet_1_rewards
return (
(wallet_node_0, wallet_0_rewards),
(wallet_node_1, wallet_1_rewards),
(wallet_0_rpc_client, wallet_1_rpc_client),
(wallet_service_0, wallet_service_1),
full_node_api,
)
@pytest.fixture(scope="function")
async def wallets_prefarm(wallets_prefarm_services):
return wallets_prefarm_services[0], wallets_prefarm_services[1], wallets_prefarm_services[4]
@pytest.fixture(scope="function")
async def three_wallets_prefarm(three_wallet_nodes, self_hostname, trusted):
"""
Sets up the node with 10 blocks, and returns a payer and payee wallet.
"""
farm_blocks = 3
buffer = 1
full_nodes, wallets, _ = three_wallet_nodes
full_node_api = full_nodes[0]
full_node_server = full_node_api.server
wallet_node_0, wallet_server_0 = wallets[0]
wallet_node_1, wallet_server_1 = wallets[1]
wallet_node_2, wallet_server_2 = wallets[2]
wallet_0 = wallet_node_0.wallet_state_manager.main_wallet
wallet_1 = wallet_node_1.wallet_state_manager.main_wallet
wallet_2 = wallet_node_2.wallet_state_manager.main_wallet
if trusted:
wallet_node_0.config["trusted_peers"] = {full_node_server.node_id.hex(): full_node_server.node_id.hex()}
wallet_node_1.config["trusted_peers"] = {full_node_server.node_id.hex(): full_node_server.node_id.hex()}
wallet_node_2.config["trusted_peers"] = {full_node_server.node_id.hex(): full_node_server.node_id.hex()}
else:
wallet_node_0.config["trusted_peers"] = {}
wallet_node_1.config["trusted_peers"] = {}
wallet_node_2.config["trusted_peers"] = {}
await wallet_server_0.start_client(PeerInfo(self_hostname, full_node_server.get_port()), None)
await wallet_server_1.start_client(PeerInfo(self_hostname, full_node_server.get_port()), None)
await wallet_server_2.start_client(PeerInfo(self_hostname, full_node_server.get_port()), None)
wallet_0_rewards = await full_node_api.farm_blocks_to_wallet(count=farm_blocks, wallet=wallet_0)
wallet_1_rewards = await full_node_api.farm_blocks_to_wallet(count=farm_blocks, wallet=wallet_1)
wallet_2_rewards = await full_node_api.farm_blocks_to_wallet(count=farm_blocks, wallet=wallet_2)
await full_node_api.farm_blocks_to_puzzlehash(count=buffer, guarantee_transaction_blocks=True)
await full_node_api.wait_for_wallets_synced(wallet_nodes=[wallet_node_0, wallet_node_1], timeout=30)
assert await wallet_0.get_confirmed_balance() == wallet_0_rewards
assert await wallet_0.get_unconfirmed_balance() == wallet_0_rewards
assert await wallet_1.get_confirmed_balance() == wallet_1_rewards
assert await wallet_1.get_unconfirmed_balance() == wallet_1_rewards
assert await wallet_2.get_confirmed_balance() == wallet_2_rewards
assert await wallet_2.get_unconfirmed_balance() == wallet_2_rewards
return (