don't hardcode shard count, bad example, need upgradeability

This commit is contained in:
protolambda 2020-01-05 20:20:20 +01:00
parent 018927def0
commit a8276f683e
No known key found for this signature in database
GPG Key ID: EC89FDBB2B4C7623
2 changed files with 18 additions and 7 deletions

View File

@ -34,6 +34,7 @@
- [`committee_to_compact_committee`](#committee_to_compact_committee) - [`committee_to_compact_committee`](#committee_to_compact_committee)
- [`chunks_to_body_root`](#chunks_to_body_root) - [`chunks_to_body_root`](#chunks_to_body_root)
- [Beacon state accessors](#beacon-state-accessors) - [Beacon state accessors](#beacon-state-accessors)
- [`get_active_shard_count`](#get_active_shard_count)
- [`get_online_validator_indices`](#get_online_validator_indices) - [`get_online_validator_indices`](#get_online_validator_indices)
- [`get_shard_committee`](#get_shard_committee) - [`get_shard_committee`](#get_shard_committee)
- [`get_shard_proposer_index`](#get_shard_proposer_index) - [`get_shard_proposer_index`](#get_shard_proposer_index)
@ -420,6 +421,13 @@ def chunks_to_body_root(chunks: List[Bytes32, MAX_SHARD_BLOCK_CHUNKS]) -> Root:
### Beacon state accessors ### Beacon state accessors
#### `get_active_shard_count`
```python
def get_active_shard_count(state: BeaconState) -> uint64:
return len(state.shard_states) # May adapt in the future, or change over time.
```
#### `get_online_validator_indices` #### `get_online_validator_indices`
```python ```python
@ -437,7 +445,7 @@ def get_shard_committee(beacon_state: BeaconState, epoch: Epoch, shard: Shard) -
source_epoch -= SHARD_COMMITTEE_PERIOD source_epoch -= SHARD_COMMITTEE_PERIOD
active_validator_indices = get_active_validator_indices(beacon_state, source_epoch) active_validator_indices = get_active_validator_indices(beacon_state, source_epoch)
seed = get_seed(beacon_state, source_epoch, DOMAIN_SHARD_COMMITTEE) seed = get_seed(beacon_state, source_epoch, DOMAIN_SHARD_COMMITTEE)
return compute_committee(active_validator_indices, seed, shard, ACTIVE_SHARDS) return compute_committee(active_validator_indices, seed, shard, get_active_shard_count(beacon_state))
``` ```
#### `get_shard_proposer_index` #### `get_shard_proposer_index`
@ -458,7 +466,8 @@ def get_light_client_committee(beacon_state: BeaconState, epoch: Epoch) -> Seque
source_epoch -= LIGHT_CLIENT_COMMITTEE_PERIOD source_epoch -= LIGHT_CLIENT_COMMITTEE_PERIOD
active_validator_indices = get_active_validator_indices(beacon_state, source_epoch) active_validator_indices = get_active_validator_indices(beacon_state, source_epoch)
seed = get_seed(beacon_state, source_epoch, DOMAIN_LIGHT_CLIENT) seed = get_seed(beacon_state, source_epoch, DOMAIN_LIGHT_CLIENT)
return compute_committee(active_validator_indices, seed, 0, ACTIVE_SHARDS)[:TARGET_COMMITTEE_SIZE] active_shards = get_active_shard_count(beacon_state)
return compute_committee(active_validator_indices, seed, 0, active_shards)[:TARGET_COMMITTEE_SIZE]
``` ```
#### `get_indexed_attestation` #### `get_indexed_attestation`
@ -498,7 +507,8 @@ def get_start_shard(state: BeaconState, slot: Slot) -> Shard:
```python ```python
def get_shard(state: BeaconState, attestation: Attestation) -> Shard: def get_shard(state: BeaconState, attestation: Attestation) -> Shard:
return Shard((attestation.data.index + get_start_shard(state, attestation.data.slot)) % ACTIVE_SHARDS) active_shards = get_active_shard_count(state)
return Shard((attestation.data.index + get_start_shard(state, attestation.data.slot)) % active_shards)
``` ```
#### `get_next_slot_for_shard` #### `get_next_slot_for_shard`
@ -601,7 +611,7 @@ def process_operations(state: BeaconState, body: BeaconBlockBody) -> None:
def validate_attestation(state: BeaconState, attestation: Attestation) -> None: def validate_attestation(state: BeaconState, attestation: Attestation) -> None:
data = attestation.data data = attestation.data
assert data.index < get_committee_count_at_slot(state, data.slot) assert data.index < get_committee_count_at_slot(state, data.slot)
assert data.index < ACTIVE_SHARDS assert data.index < get_active_shard_count(state)
assert data.target.epoch in (get_previous_epoch(state), get_current_epoch(state)) assert data.target.epoch in (get_previous_epoch(state), get_current_epoch(state))
assert data.slot + MIN_ATTESTATION_INCLUSION_DELAY <= state.slot <= data.slot + SLOTS_PER_EPOCH assert data.slot + MIN_ATTESTATION_INCLUSION_DELAY <= state.slot <= data.slot + SLOTS_PER_EPOCH
@ -753,7 +763,7 @@ def process_crosslinks(state: BeaconState,
block_body: BeaconBlockBody, block_body: BeaconBlockBody,
attestations: Sequence[Attestation]) -> Set[Tuple[Shard, Root]]: attestations: Sequence[Attestation]) -> Set[Tuple[Shard, Root]]:
winners: Set[Tuple[Shard, Root]] = set() winners: Set[Tuple[Shard, Root]] = set()
for shard in map(Shard, range(ACTIVE_SHARDS)): for shard in range(get_active_shard_count(state)):
# All attestations in the block for this shard # All attestations in the block for this shard
shard_attestations = [ shard_attestations = [
attestation for attestation in attestations attestation for attestation in attestations
@ -839,7 +849,7 @@ def process_attester_slashing(state: BeaconState, attester_slashing: AttesterSla
```python ```python
def verify_shard_transition_false_positives(state: BeaconState, block_body: BeaconBlockBody) -> None: def verify_shard_transition_false_positives(state: BeaconState, block_body: BeaconBlockBody) -> None:
# Verify that a `shard_transition` in a block is empty if an attestation was not processed for it # Verify that a `shard_transition` in a block is empty if an attestation was not processed for it
for shard in range(ACTIVE_SHARDS): for shard in range(get_active_shard_count(state)):
if state.shard_states[shard].slot != state.slot - 1: if state.shard_states[shard].slot != state.slot - 1:
assert block_body.shard_transition[shard] == ShardTransition() assert block_body.shard_transition[shard] == ShardTransition()
``` ```

View File

@ -35,6 +35,7 @@ TODO: very unstable/experimental. PLACEHOLDER.
| Name | Value | Unit | | Name | Value | Unit |
| - | - | - | | - | - | - |
| `PHASE_1_FORK_VERSION` | `0x00000001` | `Version` | | `PHASE_1_FORK_VERSION` | `0x00000001` | `Version` |
| `INITIAL_ACTIVE_SHARDS` | `2**6` (= 64) | `uint64` |
| `INITIAL_GASPRICE` | `10` | `Gwei` | | `INITIAL_GASPRICE` | `10` | `Gwei` |
## Fork to Phase 1 ## Fork to Phase 1
@ -104,7 +105,7 @@ def upgrade_to_phase1(pre: phase0.BeaconState) -> BeaconState:
gasprice=INITIAL_GASPRICE, gasprice=INITIAL_GASPRICE,
data=Root(), data=Root(),
latest_block_root=Root(), latest_block_root=Root(),
) for i in range(ACTIVE_SHARDS) ) for i in range(INITIAL_ACTIVE_SHARDS)
), ),
online_countdown=ByteList[VALIDATOR_REGISTRY_LIMIT]( online_countdown=ByteList[VALIDATOR_REGISTRY_LIMIT](
ONLINE_PERIOD for i in range(len(pre.validators)) ONLINE_PERIOD for i in range(len(pre.validators))