Clean up verify_indexed_attestation

Cosmetic changes:

* Add 4 lines of comments (now every statement has a comment)
* Avoid unnecessary `assert` (the end goal for me is for `assert`s to be exclusive to the operation processing helpers).
* Merge `return`s into one (increase readability, reduce verbosity)
* Use shorter-named `bit_0_indices` and `bit_1_indices` helper variables

Substantive change:

* Remove the condition that `len(0_indices) + len(1_indices) > 0`. This condition is redundant in the context of `process_attester_slashing` because of `slashed_any`. It is largely artificial in `process_attestation` where validators are incentivised to maximise new attestations.
This commit is contained in:
Justin 2019-05-02 09:24:24 +01:00 committed by GitHub
parent cdcb16dc1c
commit c761fbc318
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1018,30 +1018,25 @@ def convert_to_indexed(state: BeaconState, attestation: Attestation) -> IndexedA
```python
def verify_indexed_attestation(state: BeaconState, indexed_attestation: IndexedAttestation) -> bool:
"""
Verify validity of ``indexed_attestation`` fields.
Verify validity of ``indexed_attestation``.
"""
custody_bit_0_indices = indexed_attestation.custody_bit_0_indices
custody_bit_1_indices = indexed_attestation.custody_bit_1_indices
bit_0_indices = indexed_attestation.custody_bit_0_indices
bit_1_indices = indexed_attestation.custody_bit_1_indices
# Ensure no duplicate indices across custody bits
assert len(set(custody_bit_0_indices).intersection(set(custody_bit_1_indices))) == 0
if len(custody_bit_1_indices) > 0: # [TO BE REMOVED IN PHASE 1]
return False
if not (1 <= len(custody_bit_0_indices) + len(custody_bit_1_indices) <= MAX_INDICES_PER_ATTESTATION):
return False
if custody_bit_0_indices != sorted(custody_bit_0_indices):
return False
if custody_bit_1_indices != sorted(custody_bit_1_indices):
return False
return bls_verify_multiple(
return (
# Verify no index has custody bit equal to 1 [to be removed in phase 1]
len(bit_1_indices) == 0 and
# Verify max number of indices
len(bit_0_indices) + len(bit_1_indices) <= MAX_INDICES_PER_ATTESTATION and
# Verify index sets are disjoint
len(set(bit_0_indices).intersection(bit_1_indices)) == 0 and
# Verify indices are sorted
bit_0_indices == sorted(bit_0_indices) and bit_1_indices == sorted(bit_1_indices) and
# Verify aggregate signature
bls_verify_multiple(
pubkeys=[
bls_aggregate_pubkeys([state.validator_registry[i].pubkey for i in custody_bit_0_indices]),
bls_aggregate_pubkeys([state.validator_registry[i].pubkey for i in custody_bit_1_indices]),
bls_aggregate_pubkeys([state.validator_registry[i].pubkey for i in bit_0_indices]),
bls_aggregate_pubkeys([state.validator_registry[i].pubkey for i in bit_1_indices]),
],
message_hashes=[
hash_tree_root(AttestationDataAndCustodyBit(data=indexed_attestation.data, custody_bit=0b0)),
@ -1050,6 +1045,7 @@ def verify_indexed_attestation(state: BeaconState, indexed_attestation: IndexedA
signature=indexed_attestation.signature,
domain=get_domain(state, DOMAIN_ATTESTATION, indexed_attestation.data.target_epoch),
)
)
```
### `is_slashable_attestation_data`