Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions chia/_tests/core/consensus/test_pot_iterations.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def test_calculate_ip_iters(self):
assert ip_iters == (sp_iters + test_constants.NUM_SP_INTERVALS_EXTRA * sp_interval_iters + required_iters) % ssi
assert sp_iters > ip_iters

# TODO: todo_v2_plots test this for v2 plots as well
def test_win_percentage(self):
"""
Tests that the percentage of blocks won is proportional to the space of each farmer,
Expand All @@ -95,7 +96,7 @@ def test_win_percentage(self):
uint8(35): 100,
uint8(36): 100,
}
farmer_space = {k: _expected_plot_size(uint8(k)) * count for k, count in farmer_ks.items()}
farmer_space = {k: _expected_plot_size(PlotSize.make_v1(k)) * count for k, count in farmer_ks.items()}
total_space = sum(farmer_space.values())
percentage_space = {k: float(sp / total_space) for k, sp in farmer_space.items()}
wins = {k: 0 for k in farmer_ks.keys()}
Expand All @@ -111,7 +112,6 @@ def test_win_percentage(self):
for k, count in farmer_ks.items():
for farmer_index in range(count):
quality = std_hash(slot_index.to_bytes(4, "big") + k.to_bytes(1, "big") + bytes(farmer_index))
# TODO: todo_v2_plots
required_iters = calculate_iterations_quality(
constants, quality, PlotSize.make_v1(k), difficulty, sp_hash, uint64(100000000), uint32(0)
)
Expand Down Expand Up @@ -151,3 +151,24 @@ def test_calculate_phase_out(self):
max_uint32_height = uint32(0xFFFFFFFF)
result_max_height = calculate_phase_out(constants, sub_slot_iters, max_uint32_height)
assert result_max_height == sp_interval # Should cap at sp_interval


def test_expected_plot_size_v1() -> None:
last_size = 4800000
for k in range(18, 50):
plot_size = _expected_plot_size(PlotSize.make_v1(k))
assert plot_size > last_size
last_size = plot_size


def test_expected_plot_size_v2() -> None:
last_size = 1700000000
for k in range(18, 32, 2):
plot_size = _expected_plot_size(PlotSize.make_v2(k))
# TODO: todo_v2_plots remove this special case once we support smaller k-sizes
if k < 28:
assert plot_size == 0
continue

assert plot_size > last_size
last_size = plot_size
31 changes: 16 additions & 15 deletions chia/_tests/core/custom_types/test_proof_of_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import Optional

import pytest
from chia_rs import G1Element, PlotSize, ProofOfSpace
from chia_rs import G1Element, PlotSize
from chia_rs.sized_bytes import bytes32, bytes48
from chia_rs.sized_ints import uint8, uint32

Expand All @@ -15,6 +15,7 @@
calculate_plot_difficulty,
calculate_prefix_bits,
check_plot_size,
make_pos,
passes_plot_filter,
verify_and_get_quality_string,
)
Expand All @@ -24,7 +25,7 @@
class ProofOfSpaceCase:
id: str
pos_challenge: bytes32
plot_size: uint8
plot_size: PlotSize
plot_public_key: G1Element
pool_public_key: Optional[G1Element] = None
pool_contract_puzzle_hash: Optional[bytes32] = None
Expand All @@ -46,14 +47,14 @@ def b32(key: str) -> bytes32:
ProofOfSpaceCase(
id="Neither pool public key nor pool contract puzzle hash",
pos_challenge=bytes32(b"1" * 32),
plot_size=uint8(0),
plot_size=PlotSize.make_v1(0),
plot_public_key=G1Element(),
expected_error="Expected pool public key or pool contract puzzle hash but got neither",
),
ProofOfSpaceCase(
id="Both pool public key and pool contract puzzle hash",
pos_challenge=bytes32(b"1" * 32),
plot_size=uint8(0),
plot_size=PlotSize.make_v1(0),
plot_public_key=G1Element(),
pool_public_key=G1Element(),
pool_contract_puzzle_hash=bytes32(b"1" * 32),
Expand All @@ -62,31 +63,31 @@ def b32(key: str) -> bytes32:
ProofOfSpaceCase(
id="Lower than minimum plot size",
pos_challenge=bytes32(b"1" * 32),
plot_size=uint8(31),
plot_size=PlotSize.make_v1(31),
plot_public_key=G1Element(),
pool_public_key=G1Element(),
expected_error="Plot size is lower than the minimum",
),
ProofOfSpaceCase(
id="Higher than maximum plot size",
pos_challenge=bytes32(b"1" * 32),
plot_size=uint8(51),
plot_size=PlotSize.make_v1(51),
plot_public_key=G1Element(),
pool_public_key=G1Element(),
expected_error="Plot size is higher than the maximum",
),
ProofOfSpaceCase(
id="Different challenge",
pos_challenge=bytes32(b"1" * 32),
plot_size=uint8(42),
plot_size=PlotSize.make_v1(42),
pool_public_key=G1Element(),
plot_public_key=G1Element(),
expected_error="Calculated pos challenge doesn't match the provided one",
),
ProofOfSpaceCase(
id="Not passing the plot filter with size 9",
pos_challenge=b32("08b23cc2844dfb92d2eedaa705a1ce665d571ee753bd81cbb67b92caa6d34722"),
plot_size=uint8(42),
plot_size=PlotSize.make_v1(42),
pool_public_key=g1(
"b6449c2c68df97c19e884427e42ee7350982d4020571ead08732615ff39bd216bfd630b6460784982bec98b49fea79d0"
),
Expand All @@ -99,7 +100,7 @@ def b32(key: str) -> bytes32:
ProofOfSpaceCase(
id="Passing the plot filter with size 8",
pos_challenge=b32("08b23cc2844dfb92d2eedaa705a1ce665d571ee753bd81cbb67b92caa6d34722"),
plot_size=uint8(42),
plot_size=PlotSize.make_v1(42),
pool_public_key=g1(
"b6449c2c68df97c19e884427e42ee7350982d4020571ead08732615ff39bd216bfd630b6460784982bec98b49fea79d0"
),
Expand All @@ -111,23 +112,23 @@ def b32(key: str) -> bytes32:
ProofOfSpaceCase(
id="v2 plot size 0",
pos_challenge=bytes32(b"1" * 32),
plot_size=uint8(0x80),
plot_size=PlotSize.make_v2(0),
plot_public_key=G1Element(),
pool_public_key=G1Element(),
expected_error="Plot size is lower than the minimum",
),
ProofOfSpaceCase(
id="v2 plot size 34",
pos_challenge=bytes32(b"1" * 32),
plot_size=uint8(0x80 | 34),
plot_size=PlotSize.make_v2(34),
plot_public_key=G1Element(),
pool_public_key=G1Element(),
expected_error="Plot size is higher than the maximum",
),
ProofOfSpaceCase(
id="Not passing the plot filter v2",
pos_challenge=b32("3d29ea79d19b3f7e99ebf764ae53697cbe143603909873946af6ab1ece606861"),
plot_size=uint8(0x80 | 32),
plot_size=PlotSize.make_v2(32),
pool_public_key=g1(
"b6449c2c68df97c19e884427e42ee7350982d4020571ead08732615ff39bd216bfd630b6460784982bec98b49fea79d0"
),
Expand All @@ -138,7 +139,7 @@ def b32(key: str) -> bytes32:
),
)
def test_verify_and_get_quality_string(caplog: pytest.LogCaptureFixture, case: ProofOfSpaceCase) -> None:
pos = ProofOfSpace(
pos = make_pos(
challenge=case.pos_challenge,
pool_public_key=case.pool_public_key,
pool_contract_puzzle_hash=case.pool_contract_puzzle_hash,
Expand All @@ -160,7 +161,7 @@ def test_verify_and_get_quality_string(caplog: pytest.LogCaptureFixture, case: P
@datacases(
ProofOfSpaceCase(
id="v2 plot are not implemented",
plot_size=uint8(0x80 | 30),
plot_size=PlotSize.make_v2(30),
pos_challenge=b32("47deb938e145d25d7b3b3c85ca9e3972b76c01aeeb78a02fe5d3b040d282317e"),
plot_public_key=g1(
"afa3aaf09c03885154be49216ee7fb2e4581b9c4a4d7e9cc402e27280bf0cfdbdf1b9ba674e301fd1d1450234b3b1868"
Expand All @@ -172,7 +173,7 @@ def test_verify_and_get_quality_string(caplog: pytest.LogCaptureFixture, case: P
),
)
def test_verify_and_get_quality_string_v2(caplog: pytest.LogCaptureFixture, case: ProofOfSpaceCase) -> None:
pos = ProofOfSpace(
pos = make_pos(
challenge=case.pos_challenge,
pool_public_key=case.pool_public_key,
pool_contract_puzzle_hash=case.pool_contract_puzzle_hash,
Expand Down
11 changes: 6 additions & 5 deletions chia/_tests/farmer_harvester/test_farmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from unittest.mock import ANY

import pytest
from chia_rs import AugSchemeMPL, G1Element, G2Element, PrivateKey, ProofOfSpace
from chia_rs import AugSchemeMPL, G1Element, G2Element, PlotSize, PrivateKey, ProofOfSpace
from chia_rs.sized_bytes import bytes32
from chia_rs.sized_ints import uint8, uint16, uint32, uint64
from pytest_mock import MockerFixture
Expand All @@ -31,6 +31,7 @@
from chia.simulator.block_tools import BlockTools
from chia.types.blockchain_format.proof_of_space import (
generate_plot_public_key,
make_pos,
verify_and_get_quality_string,
)
from chia.util.config import load_config, save_config
Expand Down Expand Up @@ -133,7 +134,7 @@ class NewProofOfSpaceCase:
plot_identifier: str
signage_point_index: uint8
plot_id: bytes32
plot_size: uint8
plot_size: PlotSize
plot_challenge: bytes32
plot_public_key: G1Element
pool_public_key: Optional[G1Element]
Expand Down Expand Up @@ -188,7 +189,7 @@ def create_verified_quality_case(
plot_identifier="test",
signage_point_index=uint8(1),
plot_id=bytes32.fromhex("baaa6780c53d4b3739b8807b4ae79a76644ddf0d9e03dc7d0a6a0e613e764d9f"),
plot_size=uint8(32),
plot_size=PlotSize.make_v1(32),
plot_challenge=bytes32.fromhex("7580e4c366dc2c94c37ce44943f9629a3cd6e027d7b24cd014adeaa578d4b0a2"),
plot_public_key=G1Element.from_bytes(
bytes.fromhex(
Expand Down Expand Up @@ -574,7 +575,7 @@ async def test_farmer_new_proof_of_space_for_pool_stats(
peak_height=uint32(1),
last_tx_height=uint32(0),
)
pos = ProofOfSpace(
pos = make_pos(
challenge=case.plot_challenge,
pool_public_key=case.pool_public_key,
pool_contract_puzzle_hash=case.pool_contract_puzzle_hash,
Expand Down Expand Up @@ -715,7 +716,7 @@ def create_valid_pos(farmer: Farmer) -> tuple[farmer_protocol.NewSignagePoint, P
peak_height=uint32(1),
last_tx_height=uint32(0),
)
pos = ProofOfSpace(
pos = make_pos(
challenge=case.plot_challenge,
pool_public_key=case.pool_public_key,
pool_contract_puzzle_hash=case.pool_contract_puzzle_hash,
Expand Down
6 changes: 4 additions & 2 deletions chia/_tests/plot_sync/test_plot_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ async def plot_sync_callback(self, peer_id: bytes32, delta: Optional[Delta]) ->
plot = harvester.plot_manager.plots.get(Path(path), None)
assert plot is not None
assert plot.prover.get_filename() == delta.valid.additions[path].filename
assert plot.prover.get_size() == delta.valid.additions[path].size
# TODO: todo_v2_plots support v2 plots
assert plot.prover.get_size().size_v1 == delta.valid.additions[path].size
assert plot.prover.get_id() == delta.valid.additions[path].plot_id
assert plot.prover.get_compression_level() == delta.valid.additions[path].compression_level
assert plot.pool_public_key == delta.valid.additions[path].pool_public_key
Expand Down Expand Up @@ -256,7 +257,8 @@ async def run_sync_test(self) -> None:
for path, plot_info in plot_manager.plots.items():
assert str(path) in receiver.plots()
assert plot_info.prover.get_filename() == receiver.plots()[str(path)].filename
assert plot_info.prover.get_size() == receiver.plots()[str(path)].size
# TODO: todo_v2_plots support v2 plots
assert plot_info.prover.get_size().size_v1 == receiver.plots()[str(path)].size
assert plot_info.prover.get_id() == receiver.plots()[str(path)].plot_id
assert plot_info.prover.get_compression_level() == receiver.plots()[str(path)].compression_level
assert plot_info.pool_public_key == receiver.plots()[str(path)].pool_public_key
Expand Down
20 changes: 16 additions & 4 deletions chia/_tests/plot_sync/test_receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import Any, Callable, Union

import pytest
from chia_rs import G1Element
from chia_rs import G1Element, PlotSize
from chia_rs.sized_bytes import bytes32
from chia_rs.sized_ints import uint8, uint32, uint64

Expand Down Expand Up @@ -185,8 +185,12 @@ def plot_sync_setup(seeded_random: random.Random) -> tuple[Receiver, list[SyncSt
# Manually add the plots we want to remove in tests
receiver._plots = {plot_info.filename: plot_info for plot_info in plot_info_list[0:10]}
receiver._total_plot_size = sum(plot.file_size for plot in receiver.plots().values())
# TODO: todo_v2_plots support v2 plots
receiver._total_effective_plot_size = int(
sum(UI_ACTUAL_SPACE_CONSTANT_FACTOR * int(_expected_plot_size(plot.size)) for plot in receiver.plots().values())
sum(
UI_ACTUAL_SPACE_CONSTANT_FACTOR * int(_expected_plot_size(PlotSize.make_v1(plot.size)))
for plot in receiver.plots().values()
)
)
sync_steps: list[SyncStepData] = [
SyncStepData(
Expand Down Expand Up @@ -266,7 +270,11 @@ async def test_to_dict(counts_only: bool, seeded_random: random.Random) -> None:
assert get_list_or_len(plot_sync_dict_1["duplicates"], not counts_only) == 0
assert plot_sync_dict_1["total_plot_size"] == sum(plot.file_size for plot in receiver.plots().values())
assert plot_sync_dict_1["total_effective_plot_size"] == int(
sum(UI_ACTUAL_SPACE_CONSTANT_FACTOR * int(_expected_plot_size(plot.size)) for plot in receiver.plots().values())
# TODO: todo_v2_plots support v2 plots
sum(
UI_ACTUAL_SPACE_CONSTANT_FACTOR * int(_expected_plot_size(PlotSize.make_v1(plot.size)))
for plot in receiver.plots().values()
)
)
assert plot_sync_dict_1["syncing"] is None
assert plot_sync_dict_1["last_sync_time"] is None
Expand Down Expand Up @@ -312,8 +320,12 @@ async def test_to_dict(counts_only: bool, seeded_random: random.Random) -> None:
assert get_list_or_len(sync_steps[State.duplicates].args[0], counts_only) == plot_sync_dict_3["duplicates"]

assert plot_sync_dict_3["total_plot_size"] == sum(plot.file_size for plot in receiver.plots().values())
# TODO: todo_v2_plots support v2 plots
assert plot_sync_dict_3["total_effective_plot_size"] == int(
sum(UI_ACTUAL_SPACE_CONSTANT_FACTOR * int(_expected_plot_size(plot.size)) for plot in receiver.plots().values())
sum(
UI_ACTUAL_SPACE_CONSTANT_FACTOR * int(_expected_plot_size(PlotSize.make_v1(plot.size)))
for plot in receiver.plots().values()
)
)
assert plot_sync_dict_3["last_sync_time"] > 0
assert plot_sync_dict_3["syncing"] is None
Expand Down
3 changes: 2 additions & 1 deletion chia/_tests/plotting/test_plot_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,8 @@ async def test_plot_info_caching(environment, bt):
assert plot_manager.plots[path].prover.get_filename() == plot_info.prover.get_filename()
assert plot_manager.plots[path].prover.get_id() == plot_info.prover.get_id()
assert plot_manager.plots[path].prover.get_memo() == plot_info.prover.get_memo()
assert plot_manager.plots[path].prover.get_size() == plot_info.prover.get_size()
assert plot_manager.plots[path].prover.get_size().size_v1 == plot_info.prover.get_size().size_v1
assert plot_manager.plots[path].prover.get_size().size_v2 == plot_info.prover.get_size().size_v2
assert plot_manager.plots[path].prover.get_compression_level() == plot_info.prover.get_compression_level()
assert plot_manager.plots[path].pool_public_key == plot_info.pool_public_key
assert plot_manager.plots[path].pool_contract_puzzle_hash == plot_info.pool_contract_puzzle_hash
Expand Down
17 changes: 9 additions & 8 deletions chia/_tests/util/test_full_block_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,15 @@ def vdf_proof() -> VDFProof:
def get_proof_of_space() -> Generator[ProofOfSpace, None, None]:
for pool_pk in [g1(), None]:
for plot_hash in [hsh(), None]:
yield ProofOfSpace(
hsh(), # challenge
pool_pk,
plot_hash,
g1(), # plot_public_key
uint8(32),
random.randbytes(8 * 32),
)
for pos_version in [0, 0x80]:
yield ProofOfSpace(
hsh(), # challenge
pool_pk,
plot_hash,
g1(), # plot_public_key
uint8(pos_version | 32), # this is version and k-size
random.randbytes(8 * 32),
)


def get_reward_chain_block(height: uint32) -> Generator[RewardChainBlock, None, None]:
Expand Down
23 changes: 21 additions & 2 deletions chia/consensus/pos_quality.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
from __future__ import annotations

from chia_rs import PlotSize
from chia_rs.sized_ints import uint64

# The actual space in bytes of a plot, is _expected_plot_size(k) * UI_ACTUAL_SPACE_CONSTANT_FACTO
# This is not used in consensus, only for display purposes
UI_ACTUAL_SPACE_CONSTANT_FACTOR = 0.78

# these values are from CHIP-48
v2_plot_sizes: dict[int, uint64] = {
28: uint64(1717986918),
30: uint64(4509715660),
32: uint64(11381663334),
}

def _expected_plot_size(k: int) -> uint64:

def _expected_plot_size(size: PlotSize) -> uint64:
"""
Given the plot size parameter k (which is between 32 and 59), computes the
expected size of the plot in bytes (times a constant factor). This is based on efficient encoding
Expand All @@ -16,4 +24,15 @@ def _expected_plot_size(k: int) -> uint64:
is necessary to store the entries in the plot.
"""

return uint64(((2 * k) + 1) * (2 ** (k - 1)))
k: int
if size.size_v1 is not None:
k = size.size_v1
return uint64(((2 * k) + 1) * (2 ** (k - 1)))
else:
assert size.size_v2 is not None
k = size.size_v2
if k in v2_plot_sizes:
return v2_plot_sizes[k]
else:
# TODO: todo_v2_plots support test plots with lower k-values
return uint64(0)
Loading
Loading