|
| 1 | +import pytest |
| 2 | + |
| 3 | +from eth_utils import ( |
| 4 | + ValidationError, |
| 5 | +) |
| 6 | + |
| 7 | +from eth.beacon.state_machines.validation import ( |
| 8 | + validate_attestation_slot, |
| 9 | + validate_attestation_justified_block_root, |
| 10 | + validate_attestation_justified_slot, |
| 11 | +) |
| 12 | +from eth.beacon.types.attestation_data import ( |
| 13 | + AttestationData, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.parametrize( |
| 18 | + ( |
| 19 | + 'attestation_slot,current_slot,epoch_length,' |
| 20 | + 'min_attestation_inclusion_delay,is_valid' |
| 21 | + ), |
| 22 | + [ |
| 23 | + (0, 5, 5, 1, True), |
| 24 | + (0, 5, 5, 5, True), |
| 25 | + (0, 5, 5, 6, False), # not past min inclusion delay |
| 26 | + (7, 5, 10, 1, False), # attestation slot in future |
| 27 | + (10, 20, 10, 2, True), |
| 28 | + (9, 20, 10, 2, False), # more than epoch_length slots have past |
| 29 | + ] |
| 30 | +) |
| 31 | +def test_validate_attestation_slot(sample_attestation_data_params, |
| 32 | + attestation_slot, |
| 33 | + current_slot, |
| 34 | + epoch_length, |
| 35 | + min_attestation_inclusion_delay, |
| 36 | + is_valid): |
| 37 | + sample_attestation_data_params['slot'] = attestation_slot |
| 38 | + attestation_data = AttestationData(**sample_attestation_data_params) |
| 39 | + |
| 40 | + if is_valid: |
| 41 | + validate_attestation_slot( |
| 42 | + attestation_data, |
| 43 | + current_slot, |
| 44 | + epoch_length, |
| 45 | + min_attestation_inclusion_delay, |
| 46 | + ) |
| 47 | + else: |
| 48 | + with pytest.raises(ValidationError): |
| 49 | + validate_attestation_slot( |
| 50 | + attestation_data, |
| 51 | + current_slot, |
| 52 | + epoch_length, |
| 53 | + min_attestation_inclusion_delay, |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +@pytest.mark.parametrize( |
| 58 | + ( |
| 59 | + 'attestation_slot,' |
| 60 | + 'attestation_justified_slot,' |
| 61 | + 'current_slot,' |
| 62 | + 'previous_justified_slot,' |
| 63 | + 'justified_slot,' |
| 64 | + 'epoch_length,' |
| 65 | + 'is_valid' |
| 66 | + ), |
| 67 | + [ |
| 68 | + (13, 5, 14, 0, 5, 5, True), |
| 69 | + (13, 0, 14, 0, 5, 5, False), # targeting previous but should be targeting current |
| 70 | + (13, 20, 14, 0, 5, 5, False), # targeting future slot but should be targeting current |
| 71 | + (29, 10, 30, 10, 20, 10, True), |
| 72 | + (29, 20, 30, 10, 20, 10, False), # targeting current but should be targeting previous |
| 73 | + (29, 36, 30, 10, 20, 10, False), # targeting future slot but should be targeting previous |
| 74 | + (10, 10, 10, 10, 10, 10, True), |
| 75 | + ] |
| 76 | +) |
| 77 | +def test_validate_attestation_justified_slot(sample_attestation_data_params, |
| 78 | + attestation_slot, |
| 79 | + attestation_justified_slot, |
| 80 | + current_slot, |
| 81 | + previous_justified_slot, |
| 82 | + justified_slot, |
| 83 | + epoch_length, |
| 84 | + is_valid): |
| 85 | + sample_attestation_data_params['slot'] = attestation_slot |
| 86 | + sample_attestation_data_params['justified_slot'] = attestation_justified_slot |
| 87 | + attestation_data = AttestationData(**sample_attestation_data_params) |
| 88 | + |
| 89 | + if is_valid: |
| 90 | + validate_attestation_justified_slot( |
| 91 | + attestation_data, |
| 92 | + current_slot, |
| 93 | + previous_justified_slot, |
| 94 | + justified_slot, |
| 95 | + epoch_length, |
| 96 | + ) |
| 97 | + else: |
| 98 | + with pytest.raises(ValidationError): |
| 99 | + validate_attestation_justified_slot( |
| 100 | + attestation_data, |
| 101 | + current_slot, |
| 102 | + previous_justified_slot, |
| 103 | + justified_slot, |
| 104 | + epoch_length, |
| 105 | + ) |
| 106 | + |
| 107 | + |
| 108 | +@pytest.mark.parametrize( |
| 109 | + ( |
| 110 | + 'attestation_justified_block_root,' |
| 111 | + 'justified_block_root,' |
| 112 | + 'is_valid' |
| 113 | + ), |
| 114 | + [ |
| 115 | + (b'\x42' * 32, b'\x35' * 32, False), |
| 116 | + (b'\x42' * 32, b'\x42' * 32, True), |
| 117 | + ] |
| 118 | +) |
| 119 | +def test_validate_attestation_justified_block_root(sample_attestation_data_params, |
| 120 | + attestation_justified_block_root, |
| 121 | + justified_block_root, |
| 122 | + is_valid): |
| 123 | + sample_attestation_data_params['justified_block_hash'] = attestation_justified_block_root |
| 124 | + attestation_data = AttestationData(**sample_attestation_data_params) |
| 125 | + |
| 126 | + if is_valid: |
| 127 | + validate_attestation_justified_block_root( |
| 128 | + attestation_data, |
| 129 | + justified_block_root |
| 130 | + ) |
| 131 | + else: |
| 132 | + with pytest.raises(ValidationError): |
| 133 | + validate_attestation_justified_block_root( |
| 134 | + attestation_data, |
| 135 | + justified_block_root |
| 136 | + ) |
0 commit comments