A few cleanups

This commit is contained in:
Vitalik Buterin 2019-11-04 08:50:09 -08:00 committed by Danny Ryan
parent d723431616
commit 03ab1d5785
No known key found for this signature in database
GPG Key ID: 2765A792E42CE07A
1 changed files with 18 additions and 21 deletions

View File

@ -144,10 +144,8 @@ class Attestation(Container):
```python ```python
class IndexedAttestation(Container): class IndexedAttestation(Container):
participants: List[ValidatorIndex, MAX_VALIDATORS_PER_COMMITTEE] committee: List[ValidatorIndex, MAX_VALIDATORS_PER_COMMITTEE]
data: AttestationData attestation: Attestation
custody_bits: List[Bitlist[MAX_VALIDATORS_PER_COMMITTEE], MAX_SHARD_BLOCKS_PER_ATTESTATION]
signature: BLSSignature
``` ```
### `CompactCommittee` ### `CompactCommittee`
@ -232,8 +230,8 @@ def get_light_client_committee(beacon_state: BeaconState, epoch: Epoch) -> Seque
```python ```python
def get_indexed_attestation(beacon_state: BeaconState, attestation: Attestation) -> IndexedAttestation: def get_indexed_attestation(beacon_state: BeaconState, attestation: Attestation) -> IndexedAttestation:
attesting_indices = get_attesting_indices(state, attestation.data, attestation.aggregation_bits) committee = get_beacon_committee(beacon_state, attestation.data.slot, attestation.data.index)
return IndexedAttestation(attesting_indices, attestation.data, attestation.custody_bits, attestation.signature) return IndexedAttestation(committee, attestation)
``` ```
### `update_gasprice` ### `update_gasprice`
@ -259,18 +257,20 @@ def is_valid_indexed_attestation(state: BeaconState, indexed_attestation: Indexe
Check if ``indexed_attestation`` has valid indices and signature. Check if ``indexed_attestation`` has valid indices and signature.
""" """
# Verify indices are sorted
if indexed_attestation.participants != sorted(indexed_attestation.participants):
return False
# Verify aggregate signature # Verify aggregate signature
all_pubkeys = [] all_pubkeys = []
all_message_hashes = [] all_message_hashes = []
for i, custody_bits in enumerate(indexed_attestation.custody_bits): aggregation_bits = indexed_attestation.attestation.aggregation_bits
for participant, bit in zip(participants, custody_bits): assert len(aggregation_bits) == len(indexed_attestation.committee)
all_pubkeys.append(state.validators[participant].pubkey) for i, custody_bits in enumerate(indexed_attestation.attestation.custody_bits):
# Note: only 2N distinct message hashes assert len(custody_bits) == len(indexed_attestation.committee)
all_message_hashes.append(AttestationCustodyBitWrapper(hash_tree_root(indexed_attestation.data), i, bit)) for participant, abit, cbit in zip(indexed_attestation.committee, aggregation_bits, custody_bits):
if abit:
all_pubkeys.append(state.validators[participant].pubkey)
# Note: only 2N distinct message hashes
all_message_hashes.append(AttestationCustodyBitWrapper(hash_tree_root(indexed_attestation.data), i, cbit))
else:
assert cbit == False
return bls_verify_multiple( return bls_verify_multiple(
pubkeys=all_pubkeys, pubkeys=all_pubkeys,
@ -318,17 +318,13 @@ def validate_attestation(state: BeaconState, attestation: Attestation) -> bool:
proposer_index = get_beacon_proposer_index(state) proposer_index = get_beacon_proposer_index(state)
# Signature check # Signature check
committee = get_beacon_committee(state, get_current_epoch(state), shard)
for bits in attestation.custody_bits + [attestation.aggregation_bits]:
assert len(bits) == len(committee)
assert is_valid_indexed_attestation(state, get_indexed_attestation(state, attestation)) assert is_valid_indexed_attestation(state, get_indexed_attestation(state, attestation))
# Type 1: on-time attestations # Type 1: on-time attestations
if data.custody_bits != []: if data.custody_bits != []:
# Correct slot # Correct slot
assert data.slot == state.slot assert data.slot == state.slot
# Slot the attestation starts counting from
start_slot = state.shard_next_slots[shard]
# Correct data root count # Correct data root count
start_slot = state.shard_next_slots[shard]
offset_slots = [start_slot + x for x in SHARD_BLOCK_OFFSETS if start_slot + x < state.slot] offset_slots = [start_slot + x for x in SHARD_BLOCK_OFFSETS if start_slot + x < state.slot]
assert len(attestation.custody_bits) == len(offset_slots) assert len(attestation.custody_bits) == len(offset_slots)
# Correct parent block root # Correct parent block root
@ -379,7 +375,7 @@ def apply_shard_transition(state: BeaconState, shard: Shard, transition: ShardTr
assert len(chunks) == block_length // SHARD_BLOCK_CHUNK_SIZE assert len(chunks) == block_length // SHARD_BLOCK_CHUNK_SIZE
prev_gasprice = shard_state.gasprice prev_gasprice = shard_state.gasprice
# Verify combined signature # Verify combined proposer signature
assert bls_verify_multiple( assert bls_verify_multiple(
pubkeys=[state.validators[proposer].pubkey for proposer in proposers], pubkeys=[state.validators[proposer].pubkey for proposer in proposers],
message_hashes=[hash_tree_root(header) for header in headers], message_hashes=[hash_tree_root(header) for header in headers],
@ -420,6 +416,7 @@ def process_attestations(state: BeaconState, block: BeaconBlock, attestations: S
if ( if (
get_total_balance(state, online_indices.intersection(all_participants)) * 3 >= get_total_balance(state, online_indices.intersection(all_participants)) * 3 >=
get_total_balance(state, online_indices.intersection(this_shard_committee)) * 2 get_total_balance(state, online_indices.intersection(this_shard_committee)) * 2
and success is False
): ):
assert shard_transition_root == hash_tree_root(block.shard_transition) assert shard_transition_root == hash_tree_root(block.shard_transition)
process_crosslink(state, shard, block.shard_transition) process_crosslink(state, shard, block.shard_transition)