-
Notifications
You must be signed in to change notification settings - Fork 2k
/
block_tools.py
2032 lines (1851 loc) · 87.9 KB
/
block_tools.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
from __future__ import annotations
import asyncio
import copy
import logging
import os
import random
import shutil
import ssl
import sys
import tempfile
import time
from dataclasses import dataclass, replace
from pathlib import Path
from random import Random
from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple
import anyio
from chia_rs import ALLOW_BACKREFS, MEMPOOL_MODE, AugSchemeMPL, G1Element, G2Element, PrivateKey, solution_generator
from chia.consensus.block_creation import create_unfinished_block, unfinished_block_to_full_block
from chia.consensus.block_record import BlockRecord
from chia.consensus.blockchain_interface import BlockchainInterface
from chia.consensus.coinbase import create_puzzlehash_for_pk
from chia.consensus.condition_costs import ConditionCost
from chia.consensus.constants import ConsensusConstants, replace_str_to_bytes
from chia.consensus.default_constants import DEFAULT_CONSTANTS
from chia.consensus.deficit import calculate_deficit
from chia.consensus.full_block_to_block_record import block_to_block_record
from chia.consensus.make_sub_epoch_summary import next_sub_epoch_summary
from chia.consensus.pot_iterations import (
calculate_ip_iters,
calculate_iterations_quality,
calculate_sp_interval_iters,
calculate_sp_iters,
is_overflow_block,
)
from chia.consensus.vdf_info_computation import get_signage_point_vdf_info
from chia.daemon.keychain_proxy import KeychainProxy, connect_to_keychain_and_validate, wrap_local_keychain
from chia.full_node.bundle_tools import simple_solution_generator, simple_solution_generator_backrefs
from chia.full_node.signage_point import SignagePoint
from chia.plotting.create_plots import PlotKeys, create_plots
from chia.plotting.manager import PlotManager
from chia.plotting.util import (
Params,
PlotRefreshEvents,
PlotRefreshResult,
PlotsRefreshParameter,
add_plot_directory,
parse_plot_info,
)
from chia.server.server import ssl_context_for_client
from chia.simulator.socket import find_available_listen_port
from chia.simulator.ssl_certs import (
SSLTestCACertAndPrivateKey,
SSLTestCollateralWrapper,
SSLTestNodeCertsAndKeys,
get_next_nodes_certs_and_keys,
get_next_private_ca_cert_and_key,
)
from chia.simulator.wallet_tools import WalletTool
from chia.ssl.create_ssl import create_all_ssl
from chia.types.blockchain_format.classgroup import ClassgroupElement
from chia.types.blockchain_format.coin import Coin
from chia.types.blockchain_format.pool_target import PoolTarget
from chia.types.blockchain_format.program import INFINITE_COST, Program
from chia.types.blockchain_format.proof_of_space import (
ProofOfSpace,
calculate_pos_challenge,
calculate_prefix_bits,
generate_plot_public_key,
generate_taproot_sk,
passes_plot_filter,
verify_and_get_quality_string,
)
from chia.types.blockchain_format.serialized_program import SerializedProgram
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.types.blockchain_format.slots import (
ChallengeChainSubSlot,
InfusedChallengeChainSubSlot,
RewardChainSubSlot,
SubSlotProofs,
)
from chia.types.blockchain_format.sub_epoch_summary import SubEpochSummary
from chia.types.blockchain_format.vdf import VDFInfo, VDFProof
from chia.types.condition_opcodes import ConditionOpcode
from chia.types.end_of_slot_bundle import EndOfSubSlotBundle
from chia.types.full_block import FullBlock
from chia.types.generator_types import BlockGenerator
from chia.types.spend_bundle import SpendBundle
from chia.types.unfinished_block import UnfinishedBlock
from chia.util.bech32m import encode_puzzle_hash
from chia.util.block_cache import BlockCache
from chia.util.config import (
config_path_for_filename,
create_default_chia_config,
load_config,
lock_config,
override_config,
save_config,
)
from chia.util.default_root import DEFAULT_ROOT_PATH
from chia.util.hash import std_hash
from chia.util.ints import uint8, uint16, uint32, uint64, uint128
from chia.util.keychain import Keychain, bytes_to_mnemonic
from chia.util.ssl_check import fix_ssl
from chia.util.timing import adjusted_timeout, backoff_times
from chia.util.vdf_prover import get_vdf_info_and_proof
from chia.wallet.derive_keys import (
master_sk_to_farmer_sk,
master_sk_to_local_sk,
master_sk_to_pool_sk,
master_sk_to_wallet_sk,
)
from chia.wallet.puzzles.load_clvm import load_serialized_clvm_maybe_recompile
GENERATOR_MOD: SerializedProgram = load_serialized_clvm_maybe_recompile(
"rom_bootstrap_generator.clsp", package_or_requirement="chia.consensus.puzzles"
)
DESERIALIZE_MOD = load_serialized_clvm_maybe_recompile(
"chialisp_deserialisation.clsp", package_or_requirement="chia.consensus.puzzles"
)
test_constants = DEFAULT_CONSTANTS.replace(
MIN_PLOT_SIZE=uint8(18),
MIN_BLOCKS_PER_CHALLENGE_BLOCK=uint8(12),
DIFFICULTY_STARTING=uint64(2**10),
DISCRIMINANT_SIZE_BITS=uint16(16),
SUB_EPOCH_BLOCKS=uint32(170),
WEIGHT_PROOF_THRESHOLD=uint8(2),
WEIGHT_PROOF_RECENT_BLOCKS=uint32(380),
DIFFICULTY_CONSTANT_FACTOR=uint128(33554432),
NUM_SPS_SUB_SLOT=uint32(16), # Must be a power of 2
MAX_SUB_SLOT_BLOCKS=uint32(50),
EPOCH_BLOCKS=uint32(340),
# the block cache must contain at least 3 epochs in order for
# create_prev_sub_epoch_segments() to have access to all the blocks it needs
# from the cache
BLOCKS_CACHE_SIZE=uint32(340 * 3), # Coordinate with the above values
SUB_SLOT_TIME_TARGET=uint16(600), # The target number of seconds per slot, mainnet 600
SUB_SLOT_ITERS_STARTING=uint64(2**10), # Must be a multiple of 64
NUMBER_ZERO_BITS_PLOT_FILTER=uint8(1), # H(plot signature of the challenge) must start with these many zeroes
# Allows creating blockchains with timestamps up to 10 days in the future, for testing
MAX_FUTURE_TIME2=uint32(3600 * 24 * 10),
MEMPOOL_BLOCK_BUFFER=uint8(6),
)
def compute_additions_unchecked(sb: SpendBundle) -> List[Coin]:
ret: List[Coin] = []
for cs in sb.coin_spends:
parent_id = cs.coin.name()
_, r = cs.puzzle_reveal.run_with_cost(INFINITE_COST, cs.solution)
for cond in Program.to(r).as_iter():
atoms = cond.as_iter()
op = next(atoms).atom
if op != ConditionOpcode.CREATE_COIN.value:
continue
puzzle_hash = next(atoms).as_atom()
amount = uint64(next(atoms).as_int())
ret.append(Coin(parent_id, puzzle_hash, amount))
return ret
def make_spend_bundle(coins: List[Coin], wallet: WalletTool, rng: Random) -> Tuple[SpendBundle, List[Coin]]:
"""
makes a new spend bundle (block generator) spending some of the coins in the
list of coins. The list will be updated to have spent coins removed and new
coins appended.
"""
new_coins: List[Coin] = []
spend_bundles: List[SpendBundle] = []
to_spend = rng.sample(coins, min(5, len(coins)))
receiver = wallet.get_new_puzzlehash()
for c in to_spend:
bundle = wallet.generate_signed_transaction(uint64(c.amount // 2), receiver, c)
new_coins.extend(bundle.additions())
spend_bundles.append(bundle)
coins.remove(c)
coins.extend(new_coins)
return SpendBundle.aggregate(spend_bundles), new_coins
class BlockTools:
"""
Tools to generate blocks for testing.
"""
_block_cache_header: bytes32
_block_cache_height_to_hash: Dict[uint32, bytes32]
_block_cache_difficulty: uint64
_block_cache: Dict[bytes32, BlockRecord]
def __init__(
self,
constants: ConsensusConstants = test_constants,
root_path: Optional[Path] = None,
keychain: Optional[Keychain] = None,
config_overrides: Optional[Dict[str, Any]] = None,
automated_testing: bool = True,
plot_dir: str = "test-plots",
log: logging.Logger = logging.getLogger(__name__),
) -> None:
self._block_cache_header = bytes32([0] * 32)
self._tempdir = None
if root_path is None:
self._tempdir = tempfile.TemporaryDirectory()
root_path = Path(self._tempdir.name)
self.root_path = root_path
self.log = log
self.local_keychain = keychain
self.local_sk_cache: Dict[bytes32, Tuple[PrivateKey, Any]] = {}
self.automated_testing = automated_testing
self.plot_dir_name = plot_dir
if automated_testing:
# Hold onto the wrappers so that they can keep track of whether the certs/keys
# are in use by another BlockTools instance.
self.ssl_ca_cert_and_key_wrapper: SSLTestCollateralWrapper[SSLTestCACertAndPrivateKey] = (
get_next_private_ca_cert_and_key()
)
self.ssl_nodes_certs_and_keys_wrapper: SSLTestCollateralWrapper[SSLTestNodeCertsAndKeys] = (
get_next_nodes_certs_and_keys()
)
create_default_chia_config(root_path)
create_all_ssl(
root_path,
private_ca_crt_and_key=self.ssl_ca_cert_and_key_wrapper.collateral.cert_and_key,
node_certs_and_keys=self.ssl_nodes_certs_and_keys_wrapper.collateral.certs_and_keys,
)
fix_ssl(root_path)
with lock_config(root_path=root_path, filename="config.yaml"):
path = config_path_for_filename(root_path=root_path, filename="config.yaml")
path.write_text(path.read_text().replace("localhost", "127.0.0.1"))
self._config = load_config(self.root_path, "config.yaml")
if automated_testing:
if config_overrides is None:
config_overrides = {}
config_overrides["logging.log_stdout"] = True
config_overrides["selected_network"] = "testnet0"
for service in [
"harvester",
"farmer",
"full_node",
"wallet",
"introducer",
"timelord",
"pool",
"simulator",
]:
config_overrides[service + ".selected_network"] = "testnet0"
# some tests start the daemon, make sure it's on a free port
config_overrides["daemon_port"] = find_available_listen_port("BlockTools daemon")
self._config = override_config(self._config, config_overrides)
with lock_config(self.root_path, "config.yaml"):
save_config(self.root_path, "config.yaml", self._config)
overrides = self._config["network_overrides"]["constants"][self._config["selected_network"]]
updated_constants = replace_str_to_bytes(constants, **overrides)
self.constants = updated_constants
self.plot_dir: Path = get_plot_dir(self.plot_dir_name, self.automated_testing)
self.temp_dir: Path = get_plot_tmp_dir(self.plot_dir_name, self.automated_testing)
self.plot_dir.mkdir(parents=True, exist_ok=True)
self.temp_dir.mkdir(parents=True, exist_ok=True)
self.expected_plots: Dict[bytes32, Path] = {}
self.created_plots: int = 0
self.total_result = PlotRefreshResult()
def test_callback(event: PlotRefreshEvents, update_result: PlotRefreshResult) -> None:
assert update_result.duration < 120
if event == PlotRefreshEvents.started:
self.total_result = PlotRefreshResult()
if event == PlotRefreshEvents.batch_processed:
self.total_result.loaded += update_result.loaded
self.total_result.processed += update_result.processed
self.total_result.duration += update_result.duration
assert update_result.remaining >= len(self.expected_plots) - self.total_result.processed
assert len(update_result.loaded) <= self.plot_manager.refresh_parameter.batch_size
if event == PlotRefreshEvents.done:
assert self.total_result.loaded == update_result.loaded
assert self.total_result.processed == update_result.processed
assert self.total_result.duration == update_result.duration
assert update_result.remaining == 0
assert len(self.plot_manager.plots) == len(self.expected_plots)
self.plot_manager: PlotManager = PlotManager(
self.root_path,
refresh_parameter=PlotsRefreshParameter(batch_size=uint32(2)),
refresh_callback=test_callback,
match_str=str(self.plot_dir.relative_to(DEFAULT_ROOT_PATH.parent)) if not automated_testing else None,
)
async def setup_keys(self, fingerprint: Optional[int] = None, reward_ph: Optional[bytes32] = None) -> None:
keychain_proxy: Optional[KeychainProxy]
try:
if self.local_keychain:
keychain_proxy = wrap_local_keychain(self.local_keychain, log=self.log)
elif not self.automated_testing and fingerprint is not None:
keychain_proxy = await connect_to_keychain_and_validate(self.root_path, self.log)
else: # if we are automated testing or if we don't have a fingerprint.
keychain_proxy = await connect_to_keychain_and_validate(
self.root_path, self.log, user="testing-1.8.0", service="chia-testing-1.8.0"
)
assert keychain_proxy is not None
if fingerprint is None: # if we are not specifying an existing key
await keychain_proxy.delete_all_keys()
self.farmer_master_sk_entropy = std_hash(b"block_tools farmer key") # both entropies are only used here
self.pool_master_sk_entropy = std_hash(b"block_tools pool key")
self.farmer_master_sk = await keychain_proxy.add_key(bytes_to_mnemonic(self.farmer_master_sk_entropy))
self.pool_master_sk = await keychain_proxy.add_key(
bytes_to_mnemonic(self.pool_master_sk_entropy),
)
else:
sk = await keychain_proxy.get_key_for_fingerprint(fingerprint)
assert sk is not None
self.farmer_master_sk = sk
sk = await keychain_proxy.get_key_for_fingerprint(fingerprint)
assert sk is not None
self.pool_master_sk = sk
self.farmer_pk = master_sk_to_farmer_sk(self.farmer_master_sk).get_g1()
self.pool_pk = master_sk_to_pool_sk(self.pool_master_sk).get_g1()
if reward_ph is None:
self.farmer_ph: bytes32 = create_puzzlehash_for_pk(
master_sk_to_wallet_sk(self.farmer_master_sk, uint32(0)).get_g1()
)
self.pool_ph: bytes32 = create_puzzlehash_for_pk(
master_sk_to_wallet_sk(self.pool_master_sk, uint32(0)).get_g1()
)
else:
self.farmer_ph = reward_ph
self.pool_ph = reward_ph
if self.automated_testing:
self.all_sks: List[PrivateKey] = [sk for sk, _ in await keychain_proxy.get_all_private_keys()]
else:
self.all_sks = [self.farmer_master_sk] # we only want to include plots under the same fingerprint
self.pool_pubkeys: List[G1Element] = [master_sk_to_pool_sk(sk).get_g1() for sk in self.all_sks]
self.farmer_pubkeys: List[G1Element] = [master_sk_to_farmer_sk(sk).get_g1() for sk in self.all_sks]
if len(self.pool_pubkeys) == 0 or len(self.farmer_pubkeys) == 0:
raise RuntimeError("Keys not generated. Run `chia keys generate`")
self.plot_manager.set_public_keys(self.farmer_pubkeys, self.pool_pubkeys)
finally:
if keychain_proxy is not None:
await keychain_proxy.close() # close the keychain proxy
def change_config(self, new_config: Dict[str, Any]) -> None:
self._config = new_config
overrides = self._config["network_overrides"]["constants"][self._config["selected_network"]]
updated_constants = replace_str_to_bytes(self.constants, **overrides)
self.constants = updated_constants
with lock_config(self.root_path, "config.yaml"):
save_config(self.root_path, "config.yaml", self._config)
def add_plot_directory(self, path: Path) -> None:
# don't add to config if block_tools is user run and the directory is already in the config.
if str(path.resolve()) not in self._config["harvester"]["plot_directories"] or self.automated_testing:
self._config = add_plot_directory(self.root_path, str(path))
async def setup_plots(
self,
num_og_plots: int = 15,
num_pool_plots: int = 5,
num_non_keychain_plots: int = 3,
plot_size: int = 20,
bitfield: bool = True,
) -> bool:
self.add_plot_directory(self.plot_dir)
assert self.created_plots == 0
existing_plots: bool = True
# OG Plots
for i in range(num_og_plots):
plot = await self.new_plot(plot_size=plot_size, bitfield=bitfield)
if plot.new_plot:
existing_plots = False
# Pool Plots
for i in range(num_pool_plots):
plot = await self.new_plot(self.pool_ph, plot_size=plot_size, bitfield=bitfield)
if plot.new_plot:
existing_plots = False
# Some plots with keys that are not in the keychain
for i in range(num_non_keychain_plots):
plot = await self.new_plot(
path=self.plot_dir / "not_in_keychain",
plot_keys=PlotKeys(G1Element(), G1Element(), None),
exclude_plots=True,
plot_size=plot_size,
bitfield=bitfield,
)
if plot.new_plot:
existing_plots = False
await self.refresh_plots()
assert len(self.plot_manager.plots) == len(self.expected_plots)
return existing_plots
async def new_plot(
self,
pool_contract_puzzle_hash: Optional[bytes32] = None,
path: Optional[Path] = None,
tmp_dir: Optional[Path] = None,
plot_keys: Optional[PlotKeys] = None,
exclude_plots: bool = False,
plot_size: int = 20,
bitfield: bool = True,
) -> BlockToolsNewPlotResult:
final_dir = self.plot_dir
if path is not None:
final_dir = path
final_dir.mkdir(parents=True, exist_ok=True)
if tmp_dir is None:
tmp_dir = self.temp_dir
params = Params(
# Can't go much lower than 20, since plots start having no solutions and more buggy
size=plot_size,
# Uses many plots for testing, in order to guarantee proofs of space at every height
num=1,
buffer=100,
tmp_dir=Path(tmp_dir),
tmp2_dir=Path(tmp_dir),
final_dir=Path(final_dir),
plotid=None,
memo=None,
buckets=0,
stripe_size=2000,
num_threads=0,
nobitfield=not bitfield,
)
try:
if plot_keys is None:
pool_pk: Optional[G1Element] = None
pool_address: Optional[str] = None
if pool_contract_puzzle_hash is None:
pool_pk = self.pool_pk
else:
pool_address = encode_puzzle_hash(pool_contract_puzzle_hash, "xch")
plot_keys = PlotKeys(self.farmer_pk, pool_pk, pool_address)
# No datetime in the filename, to get deterministic filenames and not re-plot
created, existed = await create_plots(
params,
plot_keys,
use_datetime=False,
test_private_keys=[AugSchemeMPL.key_gen(std_hash(self.created_plots.to_bytes(2, "big")))],
)
self.created_plots += 1
plot_id_new: Optional[bytes32] = None
path_new: Optional[Path] = None
new_plot: bool = True
if len(created):
assert len(existed) == 0
plot_id_new, path_new = list(created.items())[0]
if len(existed):
assert len(created) == 0
plot_id_new, path_new = list(existed.items())[0]
new_plot = False
assert plot_id_new is not None
assert path_new is not None
if not exclude_plots:
self.expected_plots[plot_id_new] = path_new
return BlockToolsNewPlotResult(plot_id_new, new_plot)
except KeyboardInterrupt:
shutil.rmtree(self.temp_dir, ignore_errors=True)
sys.exit(1)
async def refresh_plots(self) -> None:
self.plot_manager.refresh_parameter = replace(
self.plot_manager.refresh_parameter, batch_size=uint32(4 if len(self.expected_plots) % 3 == 0 else 3)
) # Make sure we have at least some batches + a remainder
self.plot_manager.trigger_refresh()
assert self.plot_manager.needs_refresh()
self.plot_manager.start_refreshing(sleep_interval_ms=1)
with anyio.fail_after(delay=adjusted_timeout(120)):
for backoff in backoff_times():
if not self.plot_manager.needs_refresh():
break
await asyncio.sleep(backoff)
self.plot_manager.stop_refreshing()
assert not self.plot_manager.needs_refresh()
async def delete_plot(self, plot_id: bytes32) -> None:
assert plot_id in self.expected_plots
self.expected_plots[plot_id].unlink()
del self.expected_plots[plot_id]
await self.refresh_plots()
@property
def config(self) -> Dict[str, Any]:
return copy.deepcopy(self._config)
def get_daemon_ssl_context(self) -> ssl.SSLContext:
crt_path = self.root_path / self.config["daemon_ssl"]["private_crt"]
key_path = self.root_path / self.config["daemon_ssl"]["private_key"]
ca_cert_path = self.root_path / self.config["private_ssl_ca"]["crt"]
ca_key_path = self.root_path / self.config["private_ssl_ca"]["key"]
return ssl_context_for_client(ca_cert_path, ca_key_path, crt_path, key_path)
def get_plot_signature(self, m: bytes32, plot_pk: G1Element) -> G2Element:
"""
Returns the plot signature of the header data.
"""
farmer_sk = master_sk_to_farmer_sk(self.all_sks[0])
for plot_info in self.plot_manager.plots.values():
if plot_pk == plot_info.plot_public_key:
# Look up local_sk from plot to save locked memory
if plot_info.prover.get_id() in self.local_sk_cache:
local_master_sk, pool_pk_or_ph = self.local_sk_cache[plot_info.prover.get_id()]
else:
pool_pk_or_ph, _, local_master_sk = parse_plot_info(plot_info.prover.get_memo())
self.local_sk_cache[plot_info.prover.get_id()] = (local_master_sk, pool_pk_or_ph)
if isinstance(pool_pk_or_ph, G1Element):
include_taproot = False
else:
assert isinstance(pool_pk_or_ph, bytes32)
include_taproot = True
local_sk = master_sk_to_local_sk(local_master_sk)
agg_pk = generate_plot_public_key(local_sk.get_g1(), farmer_sk.get_g1(), include_taproot)
assert agg_pk == plot_pk
harv_share = AugSchemeMPL.sign(local_sk, m, agg_pk)
farm_share = AugSchemeMPL.sign(farmer_sk, m, agg_pk)
if include_taproot:
taproot_sk: PrivateKey = generate_taproot_sk(local_sk.get_g1(), farmer_sk.get_g1())
taproot_share: G2Element = AugSchemeMPL.sign(taproot_sk, m, agg_pk)
else:
taproot_share = G2Element()
return AugSchemeMPL.aggregate([harv_share, farm_share, taproot_share])
raise ValueError(f"Do not have key {plot_pk}")
def get_pool_key_signature(self, pool_target: PoolTarget, pool_pk: Optional[G1Element]) -> Optional[G2Element]:
# Returns the pool signature for the corresponding pk. If no pk is provided, returns None.
if pool_pk is None:
return None
for sk in self.all_sks:
sk_child = master_sk_to_pool_sk(sk)
if sk_child.get_g1() == pool_pk:
return AugSchemeMPL.sign(sk_child, bytes(pool_target))
raise ValueError(f"Do not have key {pool_pk}")
def get_farmer_wallet_tool(self) -> WalletTool:
return WalletTool(self.constants, self.farmer_master_sk)
def get_pool_wallet_tool(self) -> WalletTool:
return WalletTool(self.constants, self.pool_master_sk)
def get_consecutive_blocks(
self,
num_blocks: int,
block_list_input: Optional[List[FullBlock]] = None,
*,
farmer_reward_puzzle_hash: Optional[bytes32] = None,
pool_reward_puzzle_hash: Optional[bytes32] = None,
transaction_data: Optional[SpendBundle] = None,
seed: bytes = b"",
time_per_block: Optional[float] = None,
force_overflow: bool = False,
skip_slots: int = 0, # Force at least this number of empty slots before the first SB
guarantee_transaction_block: bool = False, # Force that this block must be a tx block
keep_going_until_tx_block: bool = False, # keep making new blocks until we find a tx block
normalized_to_identity_cc_eos: bool = False,
normalized_to_identity_icc_eos: bool = False,
normalized_to_identity_cc_sp: bool = False,
normalized_to_identity_cc_ip: bool = False,
current_time: bool = False,
block_refs: List[uint32] = [],
genesis_timestamp: Optional[uint64] = None,
force_plot_id: Optional[bytes32] = None,
dummy_block_references: bool = False,
include_transactions: bool = False,
) -> List[FullBlock]:
assert num_blocks > 0
if block_list_input is not None:
block_list = block_list_input.copy()
else:
block_list = []
tx_block_heights: List[uint32] = []
if dummy_block_references:
# block references can only point to transaction blocks, so we need
# to record which ones are
for b in block_list:
if b.transactions_generator is not None:
tx_block_heights.append(b.height)
constants = self.constants
transaction_data_included = False
if time_per_block is None:
time_per_block = float(constants.SUB_SLOT_TIME_TARGET) / float(constants.SLOT_BLOCKS_TARGET)
available_coins: List[Coin] = []
pending_rewards: List[Coin] = []
wallet: Optional[WalletTool] = None
rng: Optional[Random] = None
if include_transactions:
# when we generate transactions in the chain, the caller cannot also
# have ownership of the rewards and control the transactions
assert farmer_reward_puzzle_hash is None
assert pool_reward_puzzle_hash is None
assert transaction_data is None
for b in block_list:
for coin in b.get_included_reward_coins():
if coin.puzzle_hash == self.farmer_ph:
available_coins.append(coin)
print(
f"found {len(available_coins)} reward coins in existing chain."
"for simplicity, we assume the rewards are all unspent in the original chain"
)
wallet = self.get_farmer_wallet_tool()
rng = Random()
rng.seed(seed)
if farmer_reward_puzzle_hash is None:
farmer_reward_puzzle_hash = self.farmer_ph
if len(block_list) == 0:
if force_plot_id is not None:
raise ValueError("Cannot specify plot_id for genesis block")
initial_block_list_len = 0
genesis = self.create_genesis_block(
constants,
seed,
force_overflow=force_overflow,
skip_slots=skip_slots,
timestamp=(uint64(int(time.time())) if genesis_timestamp is None else genesis_timestamp),
)
self.log.info(f"Created block 0 iters: {genesis.total_iters}")
num_empty_slots_added = skip_slots
block_list = [genesis]
num_blocks -= 1
else:
initial_block_list_len = len(block_list)
num_empty_slots_added = uint32(0) # Allows forcing empty slots in the beginning, for testing purposes
if num_blocks == 0:
return block_list
blocks: Dict[bytes32, BlockRecord]
if block_list[-1].header_hash == self._block_cache_header:
height_to_hash = self._block_cache_height_to_hash
difficulty = self._block_cache_difficulty
blocks = self._block_cache
else:
height_to_hash, difficulty, blocks = load_block_list(block_list, constants)
latest_block: BlockRecord = blocks[block_list[-1].header_hash]
curr = latest_block
while not curr.is_transaction_block:
curr = blocks[curr.prev_hash]
assert curr.timestamp is not None
last_timestamp = float(curr.timestamp)
start_height = curr.height
curr = latest_block
blocks_added_this_sub_slot = 1
while not curr.first_in_sub_slot:
curr = blocks[curr.prev_hash]
blocks_added_this_sub_slot += 1
finished_sub_slots_at_sp: List[EndOfSubSlotBundle] = [] # Sub-slots since last block, up to signage point
finished_sub_slots_at_ip: List[EndOfSubSlotBundle] = [] # Sub-slots since last block, up to infusion point
sub_slot_iters: uint64 = latest_block.sub_slot_iters # The number of iterations in one sub-slot
same_slot_as_last = True # Only applies to first slot, to prevent old blocks from being added
sub_slot_start_total_iters: uint128 = latest_block.ip_sub_slot_total_iters(constants)
sub_slots_finished = 0
# this variable is true whenever there is a pending sub-epoch or epoch that needs to be added in the next block.
pending_ses: bool = False
# Start at the last block in block list
# Get the challenge for that slot
while True:
slot_cc_challenge, slot_rc_challenge = get_challenges(
constants,
blocks,
finished_sub_slots_at_sp,
latest_block.header_hash,
)
prev_num_of_blocks = num_blocks
if num_empty_slots_added < skip_slots:
# If did not reach the target slots to skip, don't make any proofs for this sub-slot
num_empty_slots_added += 1
else:
# Loop over every signage point (Except for the last ones, which are used for overflows)
for signage_point_index in range(0, constants.NUM_SPS_SUB_SLOT - constants.NUM_SP_INTERVALS_EXTRA):
curr = latest_block
while curr.total_iters > sub_slot_start_total_iters + calculate_sp_iters(
constants, sub_slot_iters, uint8(signage_point_index)
):
if curr.height == 0:
break
curr = blocks[curr.prev_hash]
if curr.total_iters > sub_slot_start_total_iters:
finished_sub_slots_at_sp = []
if same_slot_as_last:
if signage_point_index < latest_block.signage_point_index:
# Ignore this signage_point because it's in the past
continue
signage_point: SignagePoint = get_signage_point(
constants,
BlockCache(blocks),
latest_block,
sub_slot_start_total_iters,
uint8(signage_point_index),
finished_sub_slots_at_sp,
sub_slot_iters,
normalized_to_identity_cc_sp,
)
if signage_point_index == 0:
cc_sp_output_hash: bytes32 = slot_cc_challenge
else:
assert signage_point.cc_vdf is not None
cc_sp_output_hash = signage_point.cc_vdf.output.get_hash()
qualified_proofs: List[Tuple[uint64, ProofOfSpace]] = self.get_pospaces_for_challenge(
constants,
slot_cc_challenge,
cc_sp_output_hash,
seed,
difficulty,
sub_slot_iters,
curr.height,
force_plot_id=force_plot_id,
)
for required_iters, proof_of_space in sorted(qualified_proofs, key=lambda t: t[0]):
if blocks_added_this_sub_slot == constants.MAX_SUB_SLOT_BLOCKS or force_overflow:
break
if same_slot_as_last:
if signage_point_index == latest_block.signage_point_index:
# Ignore this block because it's in the past
if required_iters <= latest_block.required_iters:
continue
assert latest_block.header_hash in blocks
additions = None
removals = None
if transaction_data_included:
transaction_data = None
block_refs = []
if transaction_data is not None:
additions = compute_additions_unchecked(transaction_data)
removals = transaction_data.removals()
elif include_transactions:
assert wallet is not None
assert rng is not None
transaction_data, additions = make_spend_bundle(available_coins, wallet, rng)
removals = transaction_data.removals()
transaction_data_included = False
assert last_timestamp is not None
if proof_of_space.pool_contract_puzzle_hash is not None:
if pool_reward_puzzle_hash is not None:
# The caller wants to be paid to a specific address, but this PoSpace is tied to an
# address, so continue until a proof of space tied to a pk is found
continue
pool_target = PoolTarget(proof_of_space.pool_contract_puzzle_hash, uint32(0))
else:
if pool_reward_puzzle_hash is not None:
pool_target = PoolTarget(pool_reward_puzzle_hash, uint32(0))
else:
pool_target = PoolTarget(self.pool_ph, uint32(0))
block_generator: Optional[BlockGenerator]
if transaction_data is not None:
if start_height >= constants.HARD_FORK_HEIGHT:
block_generator = simple_solution_generator_backrefs(transaction_data)
block_refs = []
else:
block_generator = simple_solution_generator(transaction_data)
aggregate_signature = transaction_data.aggregated_signature
else:
block_generator = None
aggregate_signature = G2Element()
if dummy_block_references:
if block_generator is None:
program = SerializedProgram.from_bytes(solution_generator([]))
block_generator = BlockGenerator(program, [])
if len(tx_block_heights) > 4:
block_refs.extend(
[
tx_block_heights[1],
tx_block_heights[len(tx_block_heights) // 2],
tx_block_heights[-2],
]
)
(
full_block,
block_record,
new_timestamp,
) = get_full_block_and_block_record(
constants,
blocks,
sub_slot_start_total_iters,
uint8(signage_point_index),
proof_of_space,
slot_cc_challenge,
slot_rc_challenge,
farmer_reward_puzzle_hash,
pool_target,
last_timestamp,
start_height,
time_per_block,
block_generator,
aggregate_signature,
additions,
removals,
height_to_hash,
difficulty,
required_iters,
sub_slot_iters,
self.get_plot_signature,
self.get_pool_key_signature,
finished_sub_slots_at_ip,
signage_point,
latest_block,
seed,
normalized_to_identity_cc_ip=normalized_to_identity_cc_ip,
current_time=current_time,
block_refs=block_refs,
)
if block_record.is_transaction_block:
transaction_data_included = True
block_refs = []
keep_going_until_tx_block = False
assert full_block.foliage_transaction_block is not None
elif guarantee_transaction_block:
continue
# print(f"{full_block.height:4}: difficulty {difficulty} "
# f"time: {new_timestamp - last_timestamp:0.2f} "
# f"additions: {len(additions) if block_record.is_transaction_block else 0:2} "
# f"removals: {len(removals) if block_record.is_transaction_block else 0:2} "
# f"refs: {len(full_block.transactions_generator_ref_list):3} "
# f"tx: {block_record.is_transaction_block}")
last_timestamp = new_timestamp
block_list.append(full_block)
if include_transactions:
for coin in full_block.get_included_reward_coins():
if coin.puzzle_hash == self.farmer_ph:
pending_rewards.append(coin)
if full_block.is_transaction_block():
available_coins.extend(pending_rewards)
pending_rewards = []
if full_block.transactions_generator is not None:
tx_block_heights.append(full_block.height)
blocks_added_this_sub_slot += 1
blocks[full_block.header_hash] = block_record
self.log.info(
f"Created block {block_record.height} ove=False, iters {block_record.total_iters}"
)
height_to_hash[uint32(full_block.height)] = full_block.header_hash
latest_block = blocks[full_block.header_hash]
finished_sub_slots_at_ip = []
num_blocks -= 1
if num_blocks <= 0 and not keep_going_until_tx_block:
self._block_cache_header = block_list[-1].header_hash
self._block_cache_height_to_hash = height_to_hash
self._block_cache_difficulty = difficulty
self._block_cache = blocks
return block_list
# Finish the end of sub-slot and try again next sub-slot
# End of sub-slot logic
if len(finished_sub_slots_at_ip) == 0:
# Block has been created within this sub-slot
eos_iters: uint64 = uint64(sub_slot_iters - (latest_block.total_iters - sub_slot_start_total_iters))
cc_input: ClassgroupElement = latest_block.challenge_vdf_output
rc_challenge: bytes32 = latest_block.reward_infusion_new_challenge
else:
# No blocks were successfully created within this sub-slot
eos_iters = sub_slot_iters
cc_input = ClassgroupElement.get_default_element()
rc_challenge = slot_rc_challenge
cc_vdf, cc_proof = get_vdf_info_and_proof(
constants,
cc_input,
slot_cc_challenge,
eos_iters,
)
rc_vdf, rc_proof = get_vdf_info_and_proof(
constants,
ClassgroupElement.get_default_element(),
rc_challenge,
eos_iters,
)
eos_deficit: uint8 = (
latest_block.deficit if latest_block.deficit > 0 else constants.MIN_BLOCKS_PER_CHALLENGE_BLOCK
)
icc_eos_vdf, icc_ip_proof = get_icc(
constants,
uint128(sub_slot_start_total_iters + sub_slot_iters),
finished_sub_slots_at_ip,
latest_block,
blocks,
sub_slot_start_total_iters,
eos_deficit,
)
# End of slot vdf info for icc and cc have to be from challenge block or start of slot, respectively,
# in order for light clients to validate.
cc_vdf = VDFInfo(cc_vdf.challenge, sub_slot_iters, cc_vdf.output)
if normalized_to_identity_cc_eos:
_, cc_proof = get_vdf_info_and_proof(
constants,
ClassgroupElement.get_default_element(),
cc_vdf.challenge,
sub_slot_iters,
True,
)
# generate sub_epoch_summary, and if the last block was the last block of the sub-epoch or epoch
# include the hash in the next sub-slot
sub_epoch_summary: Optional[SubEpochSummary] = None
if not pending_ses: # if we just created a sub-epoch summary, we can at least skip another sub-slot
sub_epoch_summary = next_sub_epoch_summary(
constants,
BlockCache(blocks, height_to_hash=height_to_hash),
latest_block.required_iters,
block_list[-1],
False,
)
if sub_epoch_summary is not None: # the previous block is the last block of the sub-epoch or epoch
pending_ses = True
ses_hash: Optional[bytes32] = sub_epoch_summary.get_hash()
# if the last block is the last block of the epoch, we set the new sub-slot iters and difficulty
new_sub_slot_iters: Optional[uint64] = sub_epoch_summary.new_sub_slot_iters
new_difficulty: Optional[uint64] = sub_epoch_summary.new_difficulty
self.log.info(f"Sub epoch summary: {sub_epoch_summary} for block {latest_block.height+1}")
else: # the previous block is not the last block of the sub-epoch or epoch
pending_ses = False
ses_hash = None
new_sub_slot_iters = None
new_difficulty = None
if icc_eos_vdf is not None:
# Icc vdf (Deficit of latest block is <= 4)
if len(finished_sub_slots_at_ip) == 0:
# This means there are blocks in this sub-slot
curr = latest_block
while not curr.is_challenge_block(constants) and not curr.first_in_sub_slot:
curr = blocks[curr.prev_hash]
if curr.is_challenge_block(constants):
icc_eos_iters = uint64(sub_slot_start_total_iters + sub_slot_iters - curr.total_iters)
else:
icc_eos_iters = sub_slot_iters
else:
# This means there are no blocks in this sub-slot
icc_eos_iters = sub_slot_iters
icc_eos_vdf = VDFInfo(
icc_eos_vdf.challenge,
icc_eos_iters,
icc_eos_vdf.output,
)
if normalized_to_identity_icc_eos:
_, icc_ip_proof = get_vdf_info_and_proof(
constants,
ClassgroupElement.get_default_element(),
icc_eos_vdf.challenge,
icc_eos_iters,
True,
)
icc_sub_slot: Optional[InfusedChallengeChainSubSlot] = InfusedChallengeChainSubSlot(icc_eos_vdf)
assert icc_sub_slot is not None
icc_sub_slot_hash = icc_sub_slot.get_hash() if latest_block.deficit == 0 else None
cc_sub_slot = ChallengeChainSubSlot(
cc_vdf,
icc_sub_slot_hash,
ses_hash,
new_sub_slot_iters,
new_difficulty,
)