|
| 1 | +import os |
| 2 | +import pickle |
| 3 | +from collections import defaultdict |
1 | 4 | from pathlib import Path
|
2 | 5 | from unittest.mock import Mock
|
3 | 6 |
|
4 | 7 | import pytest
|
5 | 8 |
|
6 |
| -from src.modules.csm.state import AttestationsAccumulator, State |
7 |
| -from src.types import EpochNumber, ValidatorIndex |
| 9 | +from src import variables |
| 10 | +from src.modules.csm.state import AttestationsAccumulator, State, InvalidState |
| 11 | +from src.types import ValidatorIndex |
8 | 12 | from src.utils.range import sequence
|
9 | 13 |
|
10 | 14 |
|
11 |
| -@pytest.fixture() |
12 |
| -def state_file_path(tmp_path: Path) -> Path: |
13 |
| - return (tmp_path / "mock").with_suffix(State.EXTENSION) |
| 15 | +@pytest.fixture(autouse=True) |
| 16 | +def remove_state_files(): |
| 17 | + state_file = Path("/tmp/state.pkl") |
| 18 | + state_buf = Path("/tmp/state.buf") |
| 19 | + state_file.unlink(missing_ok=True) |
| 20 | + state_buf.unlink(missing_ok=True) |
| 21 | + yield |
| 22 | + state_file.unlink(missing_ok=True) |
| 23 | + state_buf.unlink(missing_ok=True) |
| 24 | + |
| 25 | + |
| 26 | +def test_load_restores_state_from_file(monkeypatch): |
| 27 | + monkeypatch.setattr("src.modules.csm.state.State.file", lambda _=None: Path("/tmp/state.pkl")) |
| 28 | + state = State() |
| 29 | + state.data = { |
| 30 | + (0, 31): defaultdict(AttestationsAccumulator, {ValidatorIndex(1): AttestationsAccumulator(10, 5)}), |
| 31 | + } |
| 32 | + state.commit() |
| 33 | + loaded_state = State.load() |
| 34 | + assert loaded_state.data == state.data |
14 | 35 |
|
15 | 36 |
|
16 |
| -@pytest.fixture(autouse=True) |
17 |
| -def mock_state_file(state_file_path: Path): |
18 |
| - State.file = Mock(return_value=state_file_path) |
| 37 | +def test_load_returns_new_instance_if_file_not_found(monkeypatch): |
| 38 | + monkeypatch.setattr("src.modules.csm.state.State.file", lambda: Path("/non/existent/path")) |
| 39 | + state = State.load() |
| 40 | + assert state.is_empty |
19 | 41 |
|
20 | 42 |
|
21 |
| -def test_attestation_aggregate_perf(): |
22 |
| - aggr = AttestationsAccumulator(included=333, assigned=777) |
23 |
| - assert aggr.perf == pytest.approx(0.4285, abs=1e-4) |
| 43 | +def test_load_returns_new_instance_if_empty_object(monkeypatch, tmp_path): |
| 44 | + with open('/tmp/state.pkl', "wb") as f: |
| 45 | + pickle.dump(None, f) |
| 46 | + monkeypatch.setattr("src.modules.csm.state.State.file", lambda: Path("/tmp/state.pkl")) |
| 47 | + state = State.load() |
| 48 | + assert state.is_empty |
| 49 | + |
| 50 | + |
| 51 | +def test_commit_saves_state_to_file(monkeypatch): |
| 52 | + state = State() |
| 53 | + state.data = { |
| 54 | + (0, 31): defaultdict(AttestationsAccumulator, {ValidatorIndex(1): AttestationsAccumulator(10, 5)}), |
| 55 | + } |
| 56 | + monkeypatch.setattr("src.modules.csm.state.State.file", lambda _: Path("/tmp/state.pkl")) |
| 57 | + monkeypatch.setattr("os.replace", Mock(side_effect=os.replace)) |
| 58 | + state.commit() |
| 59 | + with open("/tmp/state.pkl", "rb") as f: |
| 60 | + loaded_state = pickle.load(f) |
| 61 | + assert loaded_state.data == state.data |
| 62 | + os.replace.assert_called_once_with(Path("/tmp/state.buf"), Path("/tmp/state.pkl")) |
| 63 | + |
| 64 | + |
| 65 | +def test_file_returns_correct_path(monkeypatch): |
| 66 | + monkeypatch.setattr(variables, "CACHE_PATH", Path("/tmp")) |
| 67 | + assert State.file() == Path("/tmp/cache.pkl") |
| 68 | + |
| 69 | + |
| 70 | +def test_buffer_returns_correct_path(monkeypatch): |
| 71 | + monkeypatch.setattr(variables, "CACHE_PATH", Path("/tmp")) |
| 72 | + state = State() |
| 73 | + assert state.buffer == Path("/tmp/cache.buf") |
| 74 | + |
| 75 | + |
| 76 | +def test_is_empty_returns_true_for_empty_state(): |
| 77 | + state = State() |
| 78 | + assert state.is_empty |
| 79 | + |
| 80 | + |
| 81 | +def test_is_empty_returns_false_for_non_empty_state(): |
| 82 | + state = State() |
| 83 | + state.data = {(0, 31): defaultdict(AttestationsAccumulator)} |
| 84 | + assert not state.is_empty |
| 85 | + |
| 86 | + |
| 87 | +def test_unprocessed_epochs_raises_error_if_epochs_not_set(): |
| 88 | + state = State() |
| 89 | + with pytest.raises(ValueError, match="Epochs to process are not set"): |
| 90 | + state.unprocessed_epochs |
| 91 | + |
| 92 | + |
| 93 | +def test_unprocessed_epochs_returns_correct_set(): |
| 94 | + state = State() |
| 95 | + state._epochs_to_process = tuple(sequence(0, 95)) |
| 96 | + state._processed_epochs = set(sequence(0, 63)) |
| 97 | + assert state.unprocessed_epochs == set(sequence(64, 95)) |
| 98 | + |
| 99 | + |
| 100 | +def test_is_fulfilled_returns_true_if_no_unprocessed_epochs(): |
| 101 | + state = State() |
| 102 | + state._epochs_to_process = tuple(sequence(0, 95)) |
| 103 | + state._processed_epochs = set(sequence(0, 95)) |
| 104 | + assert state.is_fulfilled |
| 105 | + |
| 106 | + |
| 107 | +def test_is_fulfilled_returns_false_if_unprocessed_epochs_exist(): |
| 108 | + state = State() |
| 109 | + state._epochs_to_process = tuple(sequence(0, 95)) |
| 110 | + state._processed_epochs = set(sequence(0, 63)) |
| 111 | + assert not state.is_fulfilled |
| 112 | + |
| 113 | + |
| 114 | +def test_calculate_frames_handles_exact_frame_size(): |
| 115 | + epochs = tuple(range(10)) |
| 116 | + frames = State.calculate_frames(epochs, 5) |
| 117 | + assert frames == [(0, 4), (5, 9)] |
| 118 | + |
| 119 | + |
| 120 | +def test_calculate_frames_raises_error_for_insufficient_epochs(): |
| 121 | + epochs = tuple(range(8)) |
| 122 | + with pytest.raises(ValueError, match="Insufficient epochs to form a frame"): |
| 123 | + State.calculate_frames(epochs, 5) |
| 124 | + |
| 125 | + |
| 126 | +def test_clear_resets_state_to_empty(): |
| 127 | + state = State() |
| 128 | + state.data = {(0, 31): defaultdict(AttestationsAccumulator, {ValidatorIndex(1): AttestationsAccumulator(10, 5)})} |
| 129 | + state.clear() |
| 130 | + assert state.is_empty |
| 131 | + |
| 132 | + |
| 133 | +def test_find_frame_returns_correct_frame(): |
| 134 | + state = State() |
| 135 | + state.data = {(0, 31): defaultdict(AttestationsAccumulator)} |
| 136 | + assert state.find_frame(15) == (0, 31) |
24 | 137 |
|
25 | 138 |
|
26 |
| -def test_state_avg_perf(): |
| 139 | +def test_find_frame_raises_error_for_out_of_range_epoch(): |
27 | 140 | state = State()
|
| 141 | + state.data = {(0, 31): defaultdict(AttestationsAccumulator)} |
| 142 | + with pytest.raises(ValueError, match="Epoch 32 is out of frames range"): |
| 143 | + state.find_frame(32) |
28 | 144 |
|
29 |
| - frame = (0, 999) |
30 | 145 |
|
31 |
| - with pytest.raises(ValueError): |
32 |
| - state.get_network_aggr(frame) |
| 146 | +def test_increment_duty_adds_duty_correctly(): |
| 147 | + state = State() |
| 148 | + frame = (0, 31) |
| 149 | + state.data = { |
| 150 | + frame: defaultdict(AttestationsAccumulator, {ValidatorIndex(1): AttestationsAccumulator(10, 5)}), |
| 151 | + } |
| 152 | + state.increment_duty(frame, ValidatorIndex(1), True) |
| 153 | + assert state.data[frame][ValidatorIndex(1)].assigned == 11 |
| 154 | + assert state.data[frame][ValidatorIndex(1)].included == 6 |
33 | 155 |
|
| 156 | + |
| 157 | +def test_increment_duty_creates_new_validator_entry(): |
34 | 158 | state = State()
|
35 |
| - state.init_or_migrate(*frame, 1000, 1) |
| 159 | + frame = (0, 31) |
36 | 160 | state.data = {
|
37 |
| - frame: { |
38 |
| - ValidatorIndex(0): AttestationsAccumulator(included=0, assigned=0), |
39 |
| - ValidatorIndex(1): AttestationsAccumulator(included=0, assigned=0), |
40 |
| - } |
| 161 | + frame: defaultdict(AttestationsAccumulator), |
41 | 162 | }
|
| 163 | + state.increment_duty(frame, ValidatorIndex(2), True) |
| 164 | + assert state.data[frame][ValidatorIndex(2)].assigned == 1 |
| 165 | + assert state.data[frame][ValidatorIndex(2)].included == 1 |
42 | 166 |
|
43 |
| - assert state.get_network_aggr(frame).perf == 0 |
44 | 167 |
|
| 168 | +def test_increment_duty_handles_non_included_duty(): |
| 169 | + state = State() |
| 170 | + frame = (0, 31) |
45 | 171 | state.data = {
|
46 |
| - frame: { |
47 |
| - ValidatorIndex(0): AttestationsAccumulator(included=333, assigned=777), |
48 |
| - ValidatorIndex(1): AttestationsAccumulator(included=167, assigned=223), |
49 |
| - } |
| 172 | + frame: defaultdict(AttestationsAccumulator, {ValidatorIndex(1): AttestationsAccumulator(10, 5)}), |
50 | 173 | }
|
| 174 | + state.increment_duty(frame, ValidatorIndex(1), False) |
| 175 | + assert state.data[frame][ValidatorIndex(1)].assigned == 11 |
| 176 | + assert state.data[frame][ValidatorIndex(1)].included == 5 |
51 | 177 |
|
52 |
| - assert state.get_network_aggr(frame).perf == 0.5 |
53 | 178 |
|
| 179 | +def test_increment_duty_raises_error_for_out_of_range_epoch(): |
| 180 | + state = State() |
| 181 | + state.data = { |
| 182 | + (0, 31): defaultdict(AttestationsAccumulator), |
| 183 | + } |
| 184 | + with pytest.raises(ValueError, match="is not found in the state"): |
| 185 | + state.increment_duty((0, 32), ValidatorIndex(1), True) |
54 | 186 |
|
55 |
| -def test_state_attestations(): |
56 |
| - state = State( |
57 |
| - { |
58 |
| - (0, 999): { |
59 |
| - ValidatorIndex(0): AttestationsAccumulator(included=333, assigned=777), |
60 |
| - ValidatorIndex(1): AttestationsAccumulator(included=167, assigned=223), |
61 |
| - } |
62 |
| - } |
63 |
| - ) |
64 | 187 |
|
65 |
| - network_aggr = state.get_network_aggr((0, 999)) |
| 188 | +def test_add_processed_epoch_adds_epoch_to_processed_set(): |
| 189 | + state = State() |
| 190 | + state.add_processed_epoch(5) |
| 191 | + assert 5 in state._processed_epochs |
66 | 192 |
|
67 |
| - assert network_aggr.assigned == 1000 |
68 |
| - assert network_aggr.included == 500 |
69 | 193 |
|
| 194 | +def test_add_processed_epoch_does_not_duplicate_epochs(): |
| 195 | + state = State() |
| 196 | + state.add_processed_epoch(5) |
| 197 | + state.add_processed_epoch(5) |
| 198 | + assert len(state._processed_epochs) == 1 |
70 | 199 |
|
71 |
| -def test_state_load(): |
72 |
| - orig = State( |
73 |
| - { |
74 |
| - (0, 999): { |
75 |
| - ValidatorIndex(0): AttestationsAccumulator(included=333, assigned=777), |
76 |
| - ValidatorIndex(1): AttestationsAccumulator(included=167, assigned=223), |
77 |
| - } |
78 |
| - } |
79 |
| - ) |
80 | 200 |
|
81 |
| - orig.commit() |
82 |
| - copy = State.load() |
83 |
| - assert copy.data == orig.data |
| 201 | +def test_init_or_migrate_discards_data_on_version_change(): |
| 202 | + state = State() |
| 203 | + state._consensus_version = 1 |
| 204 | + state.clear = Mock() |
| 205 | + state.commit = Mock() |
| 206 | + state.init_or_migrate(0, 63, 32, 2) |
| 207 | + state.clear.assert_called_once() |
| 208 | + state.commit.assert_called_once() |
84 | 209 |
|
85 | 210 |
|
86 |
| -def test_state_clear(): |
87 |
| - state = State( |
88 |
| - { |
89 |
| - (0, 999): { |
90 |
| - ValidatorIndex(0): AttestationsAccumulator(included=333, assigned=777), |
91 |
| - ValidatorIndex(1): AttestationsAccumulator(included=167, assigned=223), |
92 |
| - } |
93 |
| - } |
94 |
| - ) |
| 211 | +def test_init_or_migrate_no_migration_needed(): |
| 212 | + state = State() |
| 213 | + state._consensus_version = 1 |
| 214 | + state._epochs_to_process = tuple(sequence(0, 63)) |
| 215 | + state._epochs_per_frame = 32 |
| 216 | + state.data = { |
| 217 | + (0, 31): defaultdict(AttestationsAccumulator), |
| 218 | + (32, 63): defaultdict(AttestationsAccumulator), |
| 219 | + } |
| 220 | + state.commit = Mock() |
| 221 | + state.init_or_migrate(0, 63, 32, 1) |
| 222 | + state.commit.assert_not_called() |
95 | 223 |
|
96 |
| - state._epochs_to_process = (EpochNumber(1), EpochNumber(33)) |
97 |
| - state._processed_epochs = {EpochNumber(42), EpochNumber(17)} |
98 | 224 |
|
99 |
| - state.clear() |
100 |
| - assert state.is_empty |
101 |
| - assert not state.data |
| 225 | +def test_init_or_migrate_migrates_data(): |
| 226 | + state = State() |
| 227 | + state._consensus_version = 1 |
| 228 | + state._epochs_to_process = tuple(sequence(0, 63)) |
| 229 | + state._epochs_per_frame = 32 |
| 230 | + state.data = { |
| 231 | + (0, 31): defaultdict(AttestationsAccumulator, {ValidatorIndex(1): AttestationsAccumulator(10, 5)}), |
| 232 | + (32, 63): defaultdict(AttestationsAccumulator, {ValidatorIndex(1): AttestationsAccumulator(20, 15)}), |
| 233 | + } |
| 234 | + state.commit = Mock() |
| 235 | + state.init_or_migrate(0, 63, 64, 1) |
| 236 | + assert state.data == { |
| 237 | + (0, 63): defaultdict(AttestationsAccumulator, {ValidatorIndex(1): AttestationsAccumulator(30, 20)}), |
| 238 | + } |
| 239 | + state.commit.assert_called_once() |
| 240 | + |
| 241 | + |
| 242 | +def test_init_or_migrate_invalidates_unmigrated_frames(): |
| 243 | + state = State() |
| 244 | + state._consensus_version = 1 |
| 245 | + state._epochs_to_process = tuple(sequence(0, 63)) |
| 246 | + state._epochs_per_frame = 64 |
| 247 | + state.data = { |
| 248 | + (0, 63): defaultdict(AttestationsAccumulator, {ValidatorIndex(1): AttestationsAccumulator(30, 20)}), |
| 249 | + } |
| 250 | + state.commit = Mock() |
| 251 | + state.init_or_migrate(0, 31, 32, 1) |
| 252 | + assert state.data == { |
| 253 | + (0, 31): defaultdict(AttestationsAccumulator), |
| 254 | + } |
| 255 | + assert state._processed_epochs == set() |
| 256 | + state.commit.assert_called_once() |
| 257 | + |
| 258 | + |
| 259 | +def test_init_or_migrate_discards_unmigrated_frame(): |
| 260 | + state = State() |
| 261 | + state._consensus_version = 1 |
| 262 | + state._epochs_to_process = tuple(sequence(0, 95)) |
| 263 | + state._epochs_per_frame = 32 |
| 264 | + state.data = { |
| 265 | + (0, 31): defaultdict(AttestationsAccumulator, {ValidatorIndex(1): AttestationsAccumulator(10, 5)}), |
| 266 | + (32, 63): defaultdict(AttestationsAccumulator, {ValidatorIndex(1): AttestationsAccumulator(20, 15)}), |
| 267 | + (64, 95): defaultdict(AttestationsAccumulator, {ValidatorIndex(1): AttestationsAccumulator(30, 25)}), |
| 268 | + } |
| 269 | + state._processed_epochs = set(sequence(0, 95)) |
| 270 | + state.commit = Mock() |
| 271 | + state.init_or_migrate(0, 63, 32, 1) |
| 272 | + assert state.data == { |
| 273 | + (0, 31): defaultdict(AttestationsAccumulator, {ValidatorIndex(1): AttestationsAccumulator(10, 5)}), |
| 274 | + (32, 63): defaultdict(AttestationsAccumulator, {ValidatorIndex(1): AttestationsAccumulator(20, 15)}), |
| 275 | + } |
| 276 | + assert state._processed_epochs == set(sequence(0, 63)) |
| 277 | + state.commit.assert_called_once() |
| 278 | + |
| 279 | + |
| 280 | +def test_migrate_frames_data_creates_new_data_correctly(): |
| 281 | + state = State() |
| 282 | + current_frames = [(0, 31), (32, 63)] |
| 283 | + new_frames = [(0, 63)] |
| 284 | + state.data = { |
| 285 | + (0, 31): defaultdict(AttestationsAccumulator, {ValidatorIndex(1): AttestationsAccumulator(10, 5)}), |
| 286 | + (32, 63): defaultdict(AttestationsAccumulator, {ValidatorIndex(1): AttestationsAccumulator(20, 15)}), |
| 287 | + } |
| 288 | + new_data, migration_status = state._migrate_frames_data(current_frames, new_frames) |
| 289 | + assert new_data == { |
| 290 | + (0, 63): defaultdict(AttestationsAccumulator, {ValidatorIndex(1): AttestationsAccumulator(30, 20)}) |
| 291 | + } |
| 292 | + assert migration_status == {(0, 31): True, (32, 63): True} |
| 293 | + |
| 294 | + |
| 295 | +def test_migrate_frames_data_handles_no_migration(): |
| 296 | + state = State() |
| 297 | + current_frames = [(0, 31)] |
| 298 | + new_frames = [(0, 31)] |
| 299 | + state.data = { |
| 300 | + (0, 31): defaultdict(AttestationsAccumulator, {ValidatorIndex(1): AttestationsAccumulator(10, 5)}), |
| 301 | + } |
| 302 | + new_data, migration_status = state._migrate_frames_data(current_frames, new_frames) |
| 303 | + assert new_data == { |
| 304 | + (0, 31): defaultdict(AttestationsAccumulator, {ValidatorIndex(1): AttestationsAccumulator(10, 5)}) |
| 305 | + } |
| 306 | + assert migration_status == {(0, 31): True} |
| 307 | + |
| 308 | + |
| 309 | +def test_migrate_frames_data_handles_partial_migration(): |
| 310 | + state = State() |
| 311 | + current_frames = [(0, 31), (32, 63)] |
| 312 | + new_frames = [(0, 31), (32, 95)] |
| 313 | + state.data = { |
| 314 | + (0, 31): defaultdict(AttestationsAccumulator, {ValidatorIndex(1): AttestationsAccumulator(10, 5)}), |
| 315 | + (32, 63): defaultdict(AttestationsAccumulator, {ValidatorIndex(1): AttestationsAccumulator(20, 15)}), |
| 316 | + } |
| 317 | + new_data, migration_status = state._migrate_frames_data(current_frames, new_frames) |
| 318 | + assert new_data == { |
| 319 | + (0, 31): defaultdict(AttestationsAccumulator, {ValidatorIndex(1): AttestationsAccumulator(10, 5)}), |
| 320 | + (32, 95): defaultdict(AttestationsAccumulator, {ValidatorIndex(1): AttestationsAccumulator(20, 15)}), |
| 321 | + } |
| 322 | + assert migration_status == {(0, 31): True, (32, 63): True} |
| 323 | + |
| 324 | + |
| 325 | +def test_migrate_frames_data_handles_no_data(): |
| 326 | + state = State() |
| 327 | + current_frames = [(0, 31)] |
| 328 | + new_frames = [(0, 31)] |
| 329 | + state.data = {frame: defaultdict(AttestationsAccumulator) for frame in current_frames} |
| 330 | + new_data, migration_status = state._migrate_frames_data(current_frames, new_frames) |
| 331 | + assert new_data == {(0, 31): defaultdict(AttestationsAccumulator)} |
| 332 | + assert migration_status == {(0, 31): True} |
| 333 | + |
| 334 | + |
| 335 | +def test_migrate_frames_data_handles_wider_old_frame(): |
| 336 | + state = State() |
| 337 | + current_frames = [(0, 63)] |
| 338 | + new_frames = [(0, 31), (32, 63)] |
| 339 | + state.data = { |
| 340 | + (0, 63): defaultdict(AttestationsAccumulator, {ValidatorIndex(1): AttestationsAccumulator(30, 20)}), |
| 341 | + } |
| 342 | + new_data, migration_status = state._migrate_frames_data(current_frames, new_frames) |
| 343 | + assert new_data == { |
| 344 | + (0, 31): defaultdict(AttestationsAccumulator), |
| 345 | + (32, 63): defaultdict(AttestationsAccumulator), |
| 346 | + } |
| 347 | + assert migration_status == {(0, 63): False} |
| 348 | + |
| 349 | + |
| 350 | +def test_validate_raises_error_if_state_not_fulfilled(): |
| 351 | + state = State() |
| 352 | + state._epochs_to_process = tuple(sequence(0, 95)) |
| 353 | + state._processed_epochs = set(sequence(0, 94)) |
| 354 | + with pytest.raises(InvalidState, match="State is not fulfilled"): |
| 355 | + state.validate(0, 95) |
| 356 | + |
| 357 | + |
| 358 | +def test_validate_raises_error_if_processed_epoch_out_of_range(): |
| 359 | + state = State() |
| 360 | + state._epochs_to_process = tuple(sequence(0, 95)) |
| 361 | + state._processed_epochs = set(sequence(0, 95)) |
| 362 | + state._processed_epochs.add(96) |
| 363 | + with pytest.raises(InvalidState, match="Processed epoch 96 is out of range"): |
| 364 | + state.validate(0, 95) |
| 365 | + |
| 366 | + |
| 367 | +def test_validate_raises_error_if_epoch_missing_in_processed_epochs(): |
| 368 | + state = State() |
| 369 | + state._epochs_to_process = tuple(sequence(0, 94)) |
| 370 | + state._processed_epochs = set(sequence(0, 94)) |
| 371 | + with pytest.raises(InvalidState, match="Epoch 95 missing in processed epochs"): |
| 372 | + state.validate(0, 95) |
102 | 373 |
|
103 | 374 |
|
104 |
| -def test_state_add_processed_epoch(): |
| 375 | +def test_validate_passes_for_fulfilled_state(): |
105 | 376 | state = State()
|
106 |
| - state.add_processed_epoch(EpochNumber(42)) |
107 |
| - state.add_processed_epoch(EpochNumber(17)) |
108 |
| - assert state._processed_epochs == {EpochNumber(42), EpochNumber(17)} |
| 377 | + state._epochs_to_process = tuple(sequence(0, 95)) |
| 378 | + state._processed_epochs = set(sequence(0, 95)) |
| 379 | + state.validate(0, 95) |
109 | 380 |
|
110 | 381 |
|
111 |
| -def test_state_inc(): |
112 |
| - |
113 |
| - frame_0 = (0, 999) |
114 |
| - frame_1 = (1000, 1999) |
115 |
| - |
116 |
| - state = State( |
117 |
| - { |
118 |
| - frame_0: { |
119 |
| - ValidatorIndex(0): AttestationsAccumulator(included=333, assigned=777), |
120 |
| - ValidatorIndex(1): AttestationsAccumulator(included=167, assigned=223), |
121 |
| - }, |
122 |
| - frame_1: { |
123 |
| - ValidatorIndex(0): AttestationsAccumulator(included=1, assigned=1), |
124 |
| - ValidatorIndex(1): AttestationsAccumulator(included=0, assigned=1), |
125 |
| - }, |
126 |
| - } |
127 |
| - ) |
128 |
| - |
129 |
| - state.increment_duty(999, ValidatorIndex(0), True) |
130 |
| - state.increment_duty(999, ValidatorIndex(0), False) |
131 |
| - state.increment_duty(999, ValidatorIndex(1), True) |
132 |
| - state.increment_duty(999, ValidatorIndex(1), True) |
133 |
| - state.increment_duty(999, ValidatorIndex(1), False) |
134 |
| - state.increment_duty(999, ValidatorIndex(2), True) |
135 |
| - |
136 |
| - state.increment_duty(1000, ValidatorIndex(2), False) |
137 |
| - |
138 |
| - assert tuple(state.data[frame_0].values()) == ( |
139 |
| - AttestationsAccumulator(included=334, assigned=779), |
140 |
| - AttestationsAccumulator(included=169, assigned=226), |
141 |
| - AttestationsAccumulator(included=1, assigned=1), |
142 |
| - ) |
143 |
| - |
144 |
| - assert tuple(state.data[frame_1].values()) == ( |
145 |
| - AttestationsAccumulator(included=1, assigned=1), |
146 |
| - AttestationsAccumulator(included=0, assigned=1), |
147 |
| - AttestationsAccumulator(included=0, assigned=1), |
148 |
| - ) |
149 |
| - |
150 |
| - |
151 |
| -def test_state_file_is_path(): |
152 |
| - assert isinstance(State.file(), Path) |
153 |
| - |
154 |
| - |
155 |
| -class TestStateTransition: |
156 |
| - """Tests for State's transition for different l_epoch, r_epoch values""" |
157 |
| - |
158 |
| - @pytest.fixture(autouse=True) |
159 |
| - def no_commit(self, monkeypatch: pytest.MonkeyPatch): |
160 |
| - monkeypatch.setattr(State, "commit", Mock()) |
161 |
| - |
162 |
| - def test_empty_to_new_frame(self): |
163 |
| - state = State() |
164 |
| - assert state.is_empty |
165 |
| - |
166 |
| - l_epoch = EpochNumber(1) |
167 |
| - r_epoch = EpochNumber(255) |
168 |
| - |
169 |
| - state.init_or_migrate(l_epoch, r_epoch, 255, 1) |
170 |
| - |
171 |
| - assert not state.is_empty |
172 |
| - assert state.unprocessed_epochs == set(sequence(l_epoch, r_epoch)) |
173 |
| - |
174 |
| - @pytest.mark.parametrize( |
175 |
| - ("l_epoch_old", "r_epoch_old", "l_epoch_new", "r_epoch_new"), |
176 |
| - [ |
177 |
| - pytest.param(1, 255, 256, 510, id="Migrate a..bA..B"), |
178 |
| - pytest.param(1, 255, 32, 510, id="Migrate a..A..b..B"), |
179 |
| - pytest.param(32, 510, 1, 255, id="Migrate: A..a..B..b"), |
180 |
| - ], |
181 |
| - ) |
182 |
| - def test_new_frame_requires_discarding_state(self, l_epoch_old, r_epoch_old, l_epoch_new, r_epoch_new): |
183 |
| - state = State() |
184 |
| - state.clear = Mock(side_effect=state.clear) |
185 |
| - state.init_or_migrate(l_epoch_old, r_epoch_old, r_epoch_old - l_epoch_old + 1, 1) |
186 |
| - state.clear.assert_not_called() |
187 |
| - |
188 |
| - state.init_or_migrate(l_epoch_new, r_epoch_new, r_epoch_new - l_epoch_new + 1, 1) |
189 |
| - state.clear.assert_called_once() |
190 |
| - |
191 |
| - assert state.unprocessed_epochs == set(sequence(l_epoch_new, r_epoch_new)) |
192 |
| - |
193 |
| - @pytest.mark.parametrize( |
194 |
| - ("l_epoch_old", "r_epoch_old", "l_epoch_new", "r_epoch_new", "epochs_per_frame"), |
195 |
| - [ |
196 |
| - pytest.param(1, 255, 1, 510, 255, id="Migrate Aa..b..B"), |
197 |
| - ], |
198 |
| - ) |
199 |
| - def test_new_frame_extends_old_state(self, l_epoch_old, r_epoch_old, l_epoch_new, r_epoch_new, epochs_per_frame): |
200 |
| - state = State() |
201 |
| - state.clear = Mock(side_effect=state.clear) |
202 |
| - |
203 |
| - state.init_or_migrate(l_epoch_old, r_epoch_old, epochs_per_frame, 1) |
204 |
| - state.clear.assert_not_called() |
205 |
| - |
206 |
| - state.init_or_migrate(l_epoch_new, r_epoch_new, epochs_per_frame, 1) |
207 |
| - state.clear.assert_not_called() |
208 |
| - |
209 |
| - assert state.unprocessed_epochs == set(sequence(l_epoch_new, r_epoch_new)) |
210 |
| - assert len(state.data) == 2 |
211 |
| - assert list(state.data.keys()) == [(l_epoch_old, r_epoch_old), (r_epoch_old + 1, r_epoch_new)] |
212 |
| - assert state.calculate_frames(state._epochs_to_process, epochs_per_frame) == [ |
213 |
| - (l_epoch_old, r_epoch_old), |
214 |
| - (r_epoch_old + 1, r_epoch_new), |
215 |
| - ] |
216 |
| - |
217 |
| - @pytest.mark.parametrize( |
218 |
| - ("l_epoch_old", "r_epoch_old", "epochs_per_frame_old", "l_epoch_new", "r_epoch_new", "epochs_per_frame_new"), |
219 |
| - [ |
220 |
| - pytest.param(32, 510, 479, 1, 510, 510, id="Migrate: A..a..b..B"), |
221 |
| - ], |
222 |
| - ) |
223 |
| - def test_new_frame_extends_old_state_with_single_frame( |
224 |
| - self, l_epoch_old, r_epoch_old, epochs_per_frame_old, l_epoch_new, r_epoch_new, epochs_per_frame_new |
225 |
| - ): |
226 |
| - state = State() |
227 |
| - state.clear = Mock(side_effect=state.clear) |
228 |
| - |
229 |
| - state.init_or_migrate(l_epoch_old, r_epoch_old, epochs_per_frame_old, 1) |
230 |
| - state.clear.assert_not_called() |
231 |
| - |
232 |
| - state.init_or_migrate(l_epoch_new, r_epoch_new, epochs_per_frame_new, 1) |
233 |
| - state.clear.assert_not_called() |
234 |
| - |
235 |
| - assert state.unprocessed_epochs == set(sequence(l_epoch_new, r_epoch_new)) |
236 |
| - assert len(state.data) == 1 |
237 |
| - assert list(state.data.keys())[0] == (l_epoch_new, r_epoch_new) |
238 |
| - assert state.calculate_frames(state._epochs_to_process, epochs_per_frame_new) == [(l_epoch_new, r_epoch_new)] |
239 |
| - |
240 |
| - @pytest.mark.parametrize( |
241 |
| - ("old_version", "new_version"), |
242 |
| - [ |
243 |
| - pytest.param(2, 3, id="Increase consensus version"), |
244 |
| - pytest.param(3, 2, id="Decrease consensus version"), |
245 |
| - ], |
246 |
| - ) |
247 |
| - def test_consensus_version_change(self, old_version, new_version): |
248 |
| - state = State() |
249 |
| - state.clear = Mock(side_effect=state.clear) |
250 |
| - state._consensus_version = old_version |
251 |
| - |
252 |
| - l_epoch = r_epoch = EpochNumber(255) |
253 |
| - |
254 |
| - state.init_or_migrate(l_epoch, r_epoch, 1, old_version) |
255 |
| - state.clear.assert_not_called() |
256 |
| - |
257 |
| - state.init_or_migrate(l_epoch, r_epoch, 1, new_version) |
258 |
| - state.clear.assert_called_once() |
| 382 | +def test_attestation_aggregate_perf(): |
| 383 | + aggr = AttestationsAccumulator(included=333, assigned=777) |
| 384 | + assert aggr.perf == pytest.approx(0.4285, abs=1e-4) |
| 385 | + |
| 386 | + |
| 387 | +def test_get_network_aggr_computes_correctly(): |
| 388 | + state = State() |
| 389 | + state.data = { |
| 390 | + (0, 31): defaultdict( |
| 391 | + AttestationsAccumulator, |
| 392 | + {ValidatorIndex(1): AttestationsAccumulator(10, 5), ValidatorIndex(2): AttestationsAccumulator(20, 15)}, |
| 393 | + ) |
| 394 | + } |
| 395 | + aggr = state.get_network_aggr((0, 31)) |
| 396 | + assert aggr.assigned == 30 |
| 397 | + assert aggr.included == 20 |
| 398 | + |
| 399 | + |
| 400 | +def test_get_network_aggr_raises_error_for_invalid_accumulator(): |
| 401 | + state = State() |
| 402 | + state.data = {(0, 31): defaultdict(AttestationsAccumulator, {ValidatorIndex(1): AttestationsAccumulator(10, 15)})} |
| 403 | + with pytest.raises(ValueError, match="Invalid accumulator"): |
| 404 | + state.get_network_aggr((0, 31)) |
| 405 | + |
| 406 | + |
| 407 | +def test_get_network_aggr_raises_error_for_missing_frame_data(): |
| 408 | + state = State() |
| 409 | + with pytest.raises(ValueError, match="No data for frame"): |
| 410 | + state.get_network_aggr((0, 31)) |
| 411 | + |
| 412 | + |
| 413 | +def test_get_network_aggr_handles_empty_frame_data(): |
| 414 | + state = State() |
| 415 | + state.data = {(0, 31): defaultdict(AttestationsAccumulator)} |
| 416 | + aggr = state.get_network_aggr((0, 31)) |
| 417 | + assert aggr.assigned == 0 |
| 418 | + assert aggr.included == 0 |
0 commit comments