Files
chia-blockchain/tests/plot_sync/test_delta.py
T
ded9f68583 chia|tests|github: Implement, integrate and test plot sync protocol (#9695)
* protocols|server: Define new harvester plot refreshing protocol messages

* protocols: Bump `protocol_version` to `0.0.34`

* tests: Introduce `setup_farmer_multi_harvester`

Allows to run a test setup with 1 farmer and mutiple harvesters.

* plotting: Add an initial plot loading indication to `PlotManager`

* plotting|tests: Don't add removed duplicates to `total_result.removed`

`PlotRefreshResult.removed` should only contain plots that were loaded
properly before they were removed. It shouldn't contain e.g. removed
duplicates or invalid plots since those are synced in an extra sync step
and not as diff but as whole list every time.

* harvester: Reset `PlotManager` on shutdown

* plot_sync: Implement plot sync protocol

* farmer|harvester: Integrate and enable plot sync

* tests: Implement tests for the plot sync protocol

* farmer|tests: Drop obsolete harvester caching code

* setup: Add `chia.plot_sync` to packages

* plot_sync: Type hints in `DeltaType`

* plot_sync: Drop parameters in `super()` calls

* plot_sync: Introduce `send_response` helper in `Receiver._process`

* plot_sync: Add some parentheses

Co-authored-by: Kyle Altendorf <sda@fstab.net>

* plot_sync: Additional hint for a `Receiver.process_path_list` parameter

* plot_sync: Force named parameters in `Receiver.process_path_list`

* test: Fix fixtures after rebase

* tests: Fix sorting after rebase

* tests: Return type hint for `plot_sync_setup`

* tests: Rename `WSChiaConnection` and move it in the outer scope

* tests|plot_sync: More type hints

* tests: Rework some delta tests

* tests: Drop a `range` and iterate over the list directly

* tests: Use the proper flags to overwrite

* test: More missing duplicates tests

* tests: Drop `ExpectedResult.reset`

* tests: Reduce some asserts

* tests: Add messages to some `assert False` statements

* tests: Introduce `ErrorSimulation` enum in `test_sync_simulated.py`

* tests: Use `secrects` instead of `Crypto.Random`

* Fixes after rebase

* Import from `typing_extensions` to support python 3.7

* Drop task name to support python 3.7

* Introduce `Sender.syncing`, `Sender.connected` and a log about the task

* Add `tests/plot_sync/config.py`

* Align the multi harvester fixture with what we do in other places

* Update the workflows

Co-authored-by: Kyle Altendorf <sda@fstab.net>
2022-04-07 17:10:44 -07:00

91 lines
3.1 KiB
Python

import logging
from typing import List
import pytest
from blspy import G1Element
from chia.plot_sync.delta import Delta, DeltaType, PathListDelta, PlotListDelta
from chia.protocols.harvester_protocol import Plot
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.util.ints import uint8, uint64
log = logging.getLogger(__name__)
def dummy_plot(path: str) -> Plot:
return Plot(path, uint8(32), bytes32(b"\00" * 32), G1Element(), None, G1Element(), uint64(0), uint64(0))
@pytest.mark.parametrize(
["delta"],
[
pytest.param(PathListDelta(), id="path list"),
pytest.param(PlotListDelta(), id="plot list"),
],
)
def test_list_delta(delta: DeltaType) -> None:
assert delta.empty()
if type(delta) == PathListDelta:
assert delta.additions == []
elif type(delta) == PlotListDelta:
assert delta.additions == {}
else:
assert False
assert delta.removals == []
assert delta.empty()
if type(delta) == PathListDelta:
delta.additions.append("0")
elif type(delta) == PlotListDelta:
delta.additions["0"] = dummy_plot("0")
else:
assert False, "Invalid delta type"
assert not delta.empty()
delta.removals.append("0")
assert not delta.empty()
delta.additions.clear()
assert not delta.empty()
delta.clear()
assert delta.empty()
@pytest.mark.parametrize(
["old", "new", "result"],
[
[[], [], PathListDelta()],
[["1"], ["0"], PathListDelta(["0"], ["1"])],
[["1", "2", "3"], ["1", "2", "3"], PathListDelta([], [])],
[["2", "1", "3"], ["2", "3", "1"], PathListDelta([], [])],
[["2"], ["2", "3", "1"], PathListDelta(["3", "1"], [])],
[["2"], ["1", "3"], PathListDelta(["1", "3"], ["2"])],
[["1"], ["1", "2", "3"], PathListDelta(["2", "3"], [])],
[[], ["1", "2", "3"], PathListDelta(["1", "2", "3"], [])],
[["-1"], ["1", "2", "3"], PathListDelta(["1", "2", "3"], ["-1"])],
[["-1", "1"], ["2", "3"], PathListDelta(["2", "3"], ["-1", "1"])],
[["-1", "1", "2"], ["2", "3"], PathListDelta(["3"], ["-1", "1"])],
[["-1", "2", "3"], ["2", "3"], PathListDelta([], ["-1"])],
[["-1", "2", "3", "-2"], ["2", "3"], PathListDelta([], ["-1", "-2"])],
[["-2", "2", "3", "-1"], ["2", "3"], PathListDelta([], ["-2", "-1"])],
],
)
def test_path_list_delta_from_lists(old: List[str], new: List[str], result: PathListDelta) -> None:
assert PathListDelta.from_lists(old, new) == result
def test_delta_empty() -> None:
delta: Delta = Delta()
all_deltas: List[DeltaType] = [delta.valid, delta.invalid, delta.keys_missing, delta.duplicates]
assert delta.empty()
for d1 in all_deltas:
delta.valid.additions["0"] = dummy_plot("0")
delta.invalid.additions.append("0")
delta.keys_missing.additions.append("0")
delta.duplicates.additions.append("0")
assert not delta.empty()
for d2 in all_deltas:
if d2 is not d1:
d2.clear()
assert not delta.empty()
assert not delta.empty()
d1.clear()
assert delta.empty()