Skip to content

Commit 31b4b6f

Browse files
committed
add v2 support to calculate_iterations_quality() and update the corresponding test
1 parent 35291ee commit 31b4b6f

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

chia/_tests/core/consensus/test_pot_iterations.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,22 @@ def test_calculate_ip_iters(self):
8383
assert ip_iters == (sp_iters + test_constants.NUM_SP_INTERVALS_EXTRA * sp_interval_iters + required_iters) % ssi
8484
assert sp_iters > ip_iters
8585

86-
# TODO: todo_v2_plots test this for v2 plots as well
8786
def test_win_percentage(self):
8887
"""
8988
Tests that the percentage of blocks won is proportional to the space of each farmer,
9089
with the assumption that all farmers have access to the same VDF speed.
9190
"""
9291
farmer_ks = {
93-
uint8(32): 100,
94-
uint8(33): 100,
95-
uint8(34): 100,
96-
uint8(35): 100,
97-
uint8(36): 100,
92+
PlotSize.make_v1(32): 100,
93+
PlotSize.make_v1(33): 100,
94+
PlotSize.make_v1(34): 100,
95+
PlotSize.make_v1(35): 100,
96+
PlotSize.make_v1(36): 100,
97+
PlotSize.make_v2(28): 100,
98+
PlotSize.make_v2(30): 100,
99+
PlotSize.make_v2(32): 100,
98100
}
99-
farmer_space = {k: _expected_plot_size(PlotSize.make_v1(k)) * count for k, count in farmer_ks.items()}
101+
farmer_space = {k: _expected_plot_size(k) * count for k, count in farmer_ks.items()}
100102
total_space = sum(farmer_space.values())
101103
percentage_space = {k: float(sp / total_space) for k, sp in farmer_space.items()}
102104
wins = {k: 0 for k in farmer_ks.keys()}
@@ -111,9 +113,12 @@ def test_win_percentage(self):
111113
sp_hash = std_hash(slot_index.to_bytes(4, "big") + sp_index.to_bytes(4, "big"))
112114
for k, count in farmer_ks.items():
113115
for farmer_index in range(count):
114-
quality = std_hash(slot_index.to_bytes(4, "big") + k.to_bytes(1, "big") + bytes(farmer_index))
116+
plot_k_val = k.size_v1 if k.size_v2 is None else k.size_v2
117+
quality = std_hash(
118+
slot_index.to_bytes(4, "big") + plot_k_val.to_bytes(1, "big") + bytes(farmer_index)
119+
)
115120
required_iters = calculate_iterations_quality(
116-
constants, quality, PlotSize.make_v1(k), difficulty, sp_hash, uint64(100000000), uint32(0)
121+
constants, quality, k, difficulty, sp_hash, uint64(100000000), uint32(0)
117122
)
118123
if required_iters < sp_interval_iters:
119124
wins[k] += 1

chia/consensus/pot_iterations.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,18 @@ def calculate_iterations_quality(
110110
"""
111111
if size.size_v1 is not None:
112112
assert size.size_v2 is None
113-
sp_quality_string: bytes32 = std_hash(quality_string + cc_sp_output_hash)
114113
phase_out = calculate_phase_out(constants, ssi, prev_transaction_block_height)
115-
iters = uint64(
116-
(
117-
int(difficulty)
118-
* int(constants.DIFFICULTY_CONSTANT_FACTOR)
119-
* int.from_bytes(sp_quality_string, "big", signed=False)
120-
// (int(pow(2, 256)) * int(_expected_plot_size(size)))
121-
)
122-
+ phase_out
123-
)
124-
return max(iters, uint64(1))
125114
else:
126-
# TODO: todo_v2_plots support v2 plots
127-
raise NotImplementedError
115+
phase_out = uint64(0)
116+
117+
sp_quality_string: bytes32 = std_hash(quality_string + cc_sp_output_hash)
118+
iters = uint64(
119+
(
120+
int(difficulty)
121+
* int(constants.DIFFICULTY_CONSTANT_FACTOR)
122+
* int.from_bytes(sp_quality_string, "big", signed=False)
123+
// (int(pow(2, 256)) * int(_expected_plot_size(size)))
124+
)
125+
+ phase_out
126+
)
127+
return max(iters, uint64(1))

0 commit comments

Comments
 (0)