Skip to content

Commit

Permalink
Typing problems fixes (#2271)
Browse files Browse the repository at this point in the history
* Typing problem fixed: `process_block_header` passes `Bytes32()` to `state_root` of `BeaconBlockHeader`, which type is `Root`

* Typing problem fixed in `initialize_beacon_state_from_eth1`: `len` returns an `int` value, while `deposit_count=uint64` of `Eth1Data` has type `uint64`

* Typing problem fixed in `process_rewards_and_penalties`: `numerator` of type `int` passed to `weight` parameter of `get_flag_index_deltas`, which has type `uint64`

* Typing problem fixed in `process_attestation`; `False` passes as `crosslink_success` parameter of `PendingAttestation`, which has type `boolean`. `False` is an instance of `(python.)bool` and is not an instance of `(ssz.)boolean`

* Typing problem fixed: `shard_data_roots` of `ShardTransition` has type `List[Bytes32]`, but its elements are used as if they were `Root` values, e.g. in `process_chunk_challenge` method: passed to `data_root` of `CustodyChunkChallengeRecord` which has type `Root`

* Typing problem fixed in `process_custody_final_updates`: `index` has type `int`, while `validator_indices_in_records` has type `Set[ValidatorIndex]`, so tesing whether `index in validator_indices_in_records` can be risky, depending on implementation details. `ValidatorIndex(index) in validator_indices_in_records` is a safer variant.

* Typing problem fixed: `slashed` parameter of `pack_compact_validator` has type `(python.)bool`, however in `committee_to_compact_committee` a value of `(ssz.)boolean` is passed as a value of the parameter

* Typing problem fixed: `inactivity_scores` is a `List[uint64,...]`, while it is intialized/appended with values of `(python.)int` type

* fixed according to @protolambda suggestions

* changed types of _WEIGHT constants and appropriate variables/parameters, according to @protolambda suggestions

* revert code formatting back

* Introduced ZERO_ROOT according to @protolambda 's suggestion

* Reverted back to , according to @protolambda comments
  • Loading branch information
ericsson49 authored Mar 25, 2021
1 parent e095090 commit 020895e
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
14 changes: 7 additions & 7 deletions specs/altair/beacon-chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ Altair is the first beacon chain hard fork. Its main features are:

| Name | Value |
| - | - |
| `TIMELY_HEAD_WEIGHT` | `12` |
| `TIMELY_SOURCE_WEIGHT` | `12` |
| `TIMELY_TARGET_WEIGHT` | `24` |
| `SYNC_REWARD_WEIGHT` | `8` |
| `WEIGHT_DENOMINATOR` | `64` |
| `TIMELY_HEAD_WEIGHT` | `uint64(12)` |
| `TIMELY_SOURCE_WEIGHT` | `uint64(12)` |
| `TIMELY_TARGET_WEIGHT` | `uint64(24)` |
| `SYNC_REWARD_WEIGHT` | `uint64(8)` |
| `WEIGHT_DENOMINATOR` | `uint64(64)` |

*Note*: The sum of the weight fractions (7/8) plus the proposer inclusion fraction (1/8) equals 1.

Expand Down Expand Up @@ -234,7 +234,7 @@ def eth2_fast_aggregate_verify(pubkeys: Sequence[BLSPubkey], message: Bytes32, s
#### `get_flag_indices_and_weights`

```python
def get_flag_indices_and_weights() -> Sequence[Tuple[int, int]]:
def get_flag_indices_and_weights() -> Sequence[Tuple[int, uint64]]:
return (
(TIMELY_HEAD_FLAG_INDEX, TIMELY_HEAD_WEIGHT),
(TIMELY_SOURCE_FLAG_INDEX, TIMELY_SOURCE_WEIGHT),
Expand Down Expand Up @@ -517,7 +517,7 @@ def process_deposit(state: BeaconState, deposit: Deposit) -> None:
state.balances.append(amount)
state.previous_epoch_participation.append(ParticipationFlags(0b0000_0000))
state.current_epoch_participation.append(ParticipationFlags(0b0000_0000))
state.inactivity_scores.append(0)
state.inactivity_scores.append(uint64(0))
else:
# Increase balance by deposit amount
index = ValidatorIndex(validator_pubkeys.index(pubkey))
Expand Down
2 changes: 1 addition & 1 deletion specs/altair/fork.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def upgrade_to_altair(pre: phase0.BeaconState) -> BeaconState:
current_justified_checkpoint=pre.current_justified_checkpoint,
finalized_checkpoint=pre.finalized_checkpoint,
# Inactivity
inactivity_scores=[0 for _ in range(len(pre.validators))],
inactivity_scores=[uint64(0) for _ in range(len(pre.validators))],
)
# Fill in sync committees
post.current_sync_committee = get_sync_committee(post, get_current_epoch(post))
Expand Down
2 changes: 1 addition & 1 deletion specs/phase0/beacon-chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,7 @@ def initialize_beacon_state_from_eth1(eth1_block_hash: Bytes32,
state = BeaconState(
genesis_time=eth1_timestamp + GENESIS_DELAY,
fork=fork,
eth1_data=Eth1Data(block_hash=eth1_block_hash, deposit_count=len(deposits)),
eth1_data=Eth1Data(block_hash=eth1_block_hash, deposit_count=uint64(len(deposits))),
latest_block_header=BeaconBlockHeader(body_root=hash_tree_root(BeaconBlockBody())),
randao_mixes=[eth1_block_hash] * EPOCHS_PER_HISTORICAL_VECTOR, # Seed RANDAO with Eth1 entropy
)
Expand Down
8 changes: 4 additions & 4 deletions specs/phase1/beacon-chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ class ShardTransition(Container):
shard_block_lengths: List[uint64, MAX_SHARD_BLOCKS_PER_ATTESTATION]
# Shard data roots
# The root is of ByteList[MAX_SHARD_BLOCK_SIZE]
shard_data_roots: List[Bytes32, MAX_SHARD_BLOCKS_PER_ATTESTATION]
shard_data_roots: List[Root, MAX_SHARD_BLOCKS_PER_ATTESTATION]
# Intermediate shard states
shard_states: List[ShardState, MAX_SHARD_BLOCKS_PER_ATTESTATION]
# Proposer signature aggregate
Expand Down Expand Up @@ -431,7 +431,7 @@ def pack_compact_validator(index: ValidatorIndex, slashed: bool, balance_in_incr
Takes as input balance-in-increments (// EFFECTIVE_BALANCE_INCREMENT) to preserve symmetry with
the unpacking function.
"""
return (index << 16) + (slashed << 15) + balance_in_increments
return (index << 16) + (uint64(slashed) << 15) + balance_in_increments
```

#### `unpack_compact_validator`
Expand All @@ -457,7 +457,7 @@ def committee_to_compact_committee(state: BeaconState, committee: Sequence[Valid
"""
validators = [state.validators[i] for i in committee]
compact_validators = [
pack_compact_validator(i, v.slashed, v.effective_balance // EFFECTIVE_BALANCE_INCREMENT)
pack_compact_validator(i, bool(v.slashed), v.effective_balance // EFFECTIVE_BALANCE_INCREMENT)
for i, v in zip(committee, validators)
]
pubkeys = [v.pubkey for v in validators]
Expand Down Expand Up @@ -807,7 +807,7 @@ def process_attestation(state: BeaconState, attestation: Attestation) -> None:
data=attestation.data,
inclusion_delay=state.slot - attestation.data.slot,
proposer_index=get_beacon_proposer_index(state),
crosslink_success=False, # To be filled in during process_shard_transitions
crosslink_success=boolean(False), # To be filled in during process_shard_transitions
)
if attestation.data.target.epoch == get_current_epoch(state):
state.current_epoch_attestations.append(pending_attestation)
Expand Down
2 changes: 1 addition & 1 deletion specs/phase1/custody-game.md
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ def process_custody_final_updates(state: BeaconState) -> None:
for index, validator in enumerate(state.validators):
if validator.exit_epoch != FAR_FUTURE_EPOCH:
not_all_secrets_are_revealed = validator.all_custody_secrets_revealed_epoch == FAR_FUTURE_EPOCH
if index in validator_indices_in_records or not_all_secrets_are_revealed:
if ValidatorIndex(index) in validator_indices_in_records or not_all_secrets_are_revealed:
# Delay withdrawable epochs if challenge records are not empty or not all
# custody secrets revealed
validator.withdrawable_epoch = FAR_FUTURE_EPOCH
Expand Down

0 comments on commit 020895e

Please sign in to comment.