|
| 1 | +from eth_typing import ( |
| 2 | + Hash32 |
| 3 | +) |
| 4 | +from eth_utils import ( |
| 5 | + ValidationError, |
| 6 | +) |
| 7 | +import rlp |
| 8 | +from typing import Type |
| 9 | + |
| 10 | +from eth.constants import ( |
| 11 | + ZERO_HASH32, |
| 12 | +) |
| 13 | + |
| 14 | +from eth._utils import bls as bls |
| 15 | +from eth.beacon._utils.hash import ( |
| 16 | + hash_eth2, |
| 17 | +) |
| 18 | + |
| 19 | +from eth.beacon.enums import ( |
| 20 | + SignatureDomain, |
| 21 | +) |
| 22 | +from eth.beacon.helpers import ( |
| 23 | + get_attestation_participants, |
| 24 | + get_block_root, |
| 25 | + get_domain, |
| 26 | +) |
| 27 | +from eth.beacon.types.states import BeaconState # noqa: F401 |
| 28 | +from eth.beacon.types.attestations import Attestation # noqa: F401 |
| 29 | +from eth.beacon.types.attestation_data import AttestationData # noqa: F401 |
| 30 | + |
| 31 | + |
| 32 | +# |
| 33 | +# Attestation validation |
| 34 | +# |
| 35 | +def validate_serenity_attestation(state: Type[BeaconState], |
| 36 | + attestation: Attestation, |
| 37 | + epoch_length: int, |
| 38 | + min_attestation_inclusion_delay: int) -> None: |
| 39 | + """ |
| 40 | + Validate the given ``attestation``. |
| 41 | + Raise ``ValidationError`` if it's invalid. |
| 42 | + """ |
| 43 | + |
| 44 | + validate_serenity_attestation_slot( |
| 45 | + attestation.data, |
| 46 | + state.slot, |
| 47 | + epoch_length, |
| 48 | + min_attestation_inclusion_delay, |
| 49 | + ) |
| 50 | + |
| 51 | + validate_serenity_attestation_justified_slot( |
| 52 | + attestation.data, |
| 53 | + state.slot, |
| 54 | + state.previous_justified_slot, |
| 55 | + state.justified_slot, |
| 56 | + epoch_length, |
| 57 | + ) |
| 58 | + |
| 59 | + validate_serenity_attestation_justified_block_root( |
| 60 | + attestation.data, |
| 61 | + justified_block_root=get_block_root( |
| 62 | + state.latest_block_roots, |
| 63 | + current_slot=state.slot, |
| 64 | + slot=attestation.data.justified_slot, |
| 65 | + ), |
| 66 | + ) |
| 67 | + |
| 68 | + validate_serenity_attestation_latest_crosslink_root( |
| 69 | + attestation.data, |
| 70 | + latest_crosslink_root=state.latest_crosslinks[attestation.data.shard].shard_block_root, |
| 71 | + ) |
| 72 | + |
| 73 | + validate_serenity_attestation_shard_block_root(attestation.data) |
| 74 | + |
| 75 | + validate_serenity_attestation_aggregate_signature( |
| 76 | + state, |
| 77 | + attestation, |
| 78 | + epoch_length, |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +def validate_serenity_attestation_slot(attestation_data: AttestationData, |
| 83 | + current_slot: int, |
| 84 | + epoch_length: int, |
| 85 | + min_attestation_inclusion_delay: int) -> None: |
| 86 | + """ |
| 87 | + Validate ``slot`` field of ``attestation_data``. |
| 88 | + Raise ``ValidationError`` if it's invalid. |
| 89 | + """ |
| 90 | + if attestation_data.slot + min_attestation_inclusion_delay > current_slot: |
| 91 | + raise ValidationError( |
| 92 | + "Attestation slot plus min inclusion delay is too high:\n" |
| 93 | + "\tFound: %s (%s + %s), Needed less than or equal to %s" % |
| 94 | + ( |
| 95 | + attestation_data.slot + min_attestation_inclusion_delay, |
| 96 | + attestation_data.slot, |
| 97 | + min_attestation_inclusion_delay, |
| 98 | + current_slot, |
| 99 | + ) |
| 100 | + ) |
| 101 | + if attestation_data.slot + epoch_length < current_slot: |
| 102 | + raise ValidationError( |
| 103 | + "Attestation slot plus epoch length is too low:\n" |
| 104 | + "\tFound: %s (%s + %s), Needed greater than or equal to: %s" % |
| 105 | + ( |
| 106 | + attestation_data.slot + epoch_length, |
| 107 | + attestation_data.slot, |
| 108 | + epoch_length, |
| 109 | + current_slot, |
| 110 | + ) |
| 111 | + ) |
| 112 | + |
| 113 | + |
| 114 | +def validate_serenity_attestation_justified_slot(attestation_data: AttestationData, |
| 115 | + current_slot: int, |
| 116 | + previous_justified_slot: int, |
| 117 | + justified_slot: int, |
| 118 | + epoch_length: int) -> None: |
| 119 | + """ |
| 120 | + Validate ``justified_slot`` field of ``attestation_data``. |
| 121 | + Raise ``ValidationError`` if it's invalid. |
| 122 | + """ |
| 123 | + if attestation_data.slot >= current_slot - (current_slot % epoch_length): |
| 124 | + if attestation_data.justified_slot != justified_slot: |
| 125 | + raise ValidationError( |
| 126 | + "Attestation ``slot`` is after recent epoch transition but attestation" |
| 127 | + "``justified_slot`` is not targeting the ``justified_slot``:\n" |
| 128 | + "\tFound: %s, Expected %s" % |
| 129 | + (attestation_data.justified_slot, justified_slot) |
| 130 | + ) |
| 131 | + else: |
| 132 | + if attestation_data.justified_slot != previous_justified_slot: |
| 133 | + raise ValidationError( |
| 134 | + "Attestation ``slot`` is before recent epoch transition but attestation" |
| 135 | + "``justified_slot`` is not targeting the ``previous_justified_slot:\n" |
| 136 | + "\tFound: %s, Expected %s" % |
| 137 | + (attestation_data.justified_slot, previous_justified_slot) |
| 138 | + ) |
| 139 | + |
| 140 | + |
| 141 | +def validate_serenity_attestation_justified_block_root(attestation_data: AttestationData, |
| 142 | + justified_block_root: Hash32) -> None: |
| 143 | + """ |
| 144 | + Validate ``justified_block_root`` field of ``attestation_data``. |
| 145 | + Raise ``ValidationError`` if it's invalid. |
| 146 | + """ |
| 147 | + if attestation_data.justified_block_root != justified_block_root: |
| 148 | + raise ValidationError( |
| 149 | + "Attestation ``justified_block_root`` is not equal to the " |
| 150 | + "``justified_block_root`` at the ``justified_slot``:\n" |
| 151 | + "\tFound: %s, Expected %s at slot %s" % |
| 152 | + ( |
| 153 | + attestation_data.justified_block_root, |
| 154 | + justified_block_root, |
| 155 | + attestation_data.justified_slot, |
| 156 | + ) |
| 157 | + ) |
| 158 | + |
| 159 | + |
| 160 | +def validate_serenity_attestation_latest_crosslink_root(attestation_data: AttestationData, |
| 161 | + latest_crosslink_root: Hash32) -> None: |
| 162 | + """ |
| 163 | + Validate that either the attestation ``latest_crosslink_root`` or ``shard_block_root`` |
| 164 | + field of ``attestation_data`` is the provided ``latest_crosslink_root``. |
| 165 | + Raise ``ValidationError`` if it's invalid. |
| 166 | + """ |
| 167 | + acceptable_shard_block_roots = { |
| 168 | + attestation_data.latest_crosslink_root, |
| 169 | + attestation_data.shard_block_root, |
| 170 | + } |
| 171 | + if latest_crosslink_root not in acceptable_shard_block_roots: |
| 172 | + raise ValidationError( |
| 173 | + "Neither the attestation ``latest_crosslink_root`` nor the attestation " |
| 174 | + "``shard_block_root`` are equal to the ``latest_crosslink_root``.\n" |
| 175 | + "\tFound: %s and %s, Expected %s" % |
| 176 | + ( |
| 177 | + attestation_data.latest_crosslink_root, |
| 178 | + attestation_data.shard_block_root, |
| 179 | + latest_crosslink_root, |
| 180 | + ) |
| 181 | + ) |
| 182 | + |
| 183 | + |
| 184 | +def validate_serenity_attestation_shard_block_root(attestation_data: AttestationData) -> None: |
| 185 | + """ |
| 186 | + Validate ``shard_block_root`` field of `attestation_data`. |
| 187 | + Raise ``ValidationError`` if it's invalid. |
| 188 | +
|
| 189 | + Note: This is the Phase 0 version of ``shard_block_root`` validation. |
| 190 | + This is a built-in stub and will be changed in phase 1. |
| 191 | + """ |
| 192 | + if attestation_data.shard_block_root != ZERO_HASH32: |
| 193 | + raise ValidationError( |
| 194 | + "Attestation ``shard_block_root`` is not ZERO_HASH32.\n" |
| 195 | + "\tFound: %s, Expected %s" % |
| 196 | + ( |
| 197 | + attestation_data.shard_block_root, |
| 198 | + ZERO_HASH32, |
| 199 | + ) |
| 200 | + ) |
| 201 | + |
| 202 | + |
| 203 | +def validate_serenity_attestation_aggregate_signature(state: Type[BeaconState], |
| 204 | + attestation: Attestation, |
| 205 | + epoch_length: int) -> None: |
| 206 | + """ |
| 207 | + Validate ``aggregate_signature`` field of ``attestation``. |
| 208 | + Raise ``ValidationError`` if it's invalid. |
| 209 | +
|
| 210 | + Note: This is the phase 0 version of `aggregate_signature`` validation. |
| 211 | + All proof of custody bits are assumed to be 0 within the signed data. |
| 212 | + This will change to reflect real proof of custody bits in the Phase 1. |
| 213 | + """ |
| 214 | + participant_indices = get_attestation_participants( |
| 215 | + state=state, |
| 216 | + slot=attestation.data.slot, |
| 217 | + shard=attestation.data.shard, |
| 218 | + participation_bitfield=attestation.participation_bitfield, |
| 219 | + epoch_length=epoch_length, |
| 220 | + ) |
| 221 | + |
| 222 | + pubkeys = tuple( |
| 223 | + state.validator_registry[validator_index].pubkey |
| 224 | + for validator_index in participant_indices |
| 225 | + ) |
| 226 | + group_public_key = bls.aggregate_pubkeys(pubkeys) |
| 227 | + |
| 228 | + # TODO: change to tree hashing when we have SSZ |
| 229 | + # TODO: Replace with AttestationAndCustodyBit data structure |
| 230 | + message = hash_eth2( |
| 231 | + rlp.encode(attestation.data) + |
| 232 | + (0).to_bytes(1, "big") |
| 233 | + ) |
| 234 | + |
| 235 | + is_valid_signature = bls.verify( |
| 236 | + message=message, |
| 237 | + pubkey=group_public_key, |
| 238 | + signature=attestation.aggregate_signature, |
| 239 | + domain=get_domain( |
| 240 | + fork_data=state.fork_data, |
| 241 | + slot=attestation.data.slot, |
| 242 | + domain_type=SignatureDomain.DOMAIN_ATTESTATION, |
| 243 | + ), |
| 244 | + |
| 245 | + ) |
| 246 | + if not is_valid_signature: |
| 247 | + raise ValidationError( |
| 248 | + "Attestation ``aggregate_signature`` is invalid." |
| 249 | + ) |
0 commit comments