From 020895e35d03ae8615707039bd4a46ed398d9286 Mon Sep 17 00:00:00 2001 From: Alex Vlasov Date: Fri, 26 Mar 2021 00:03:21 +0300 Subject: [PATCH] Typing problems fixes (#2271) * 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 --- specs/altair/beacon-chain.md | 14 +++++++------- specs/altair/fork.md | 2 +- specs/phase0/beacon-chain.md | 2 +- specs/phase1/beacon-chain.md | 8 ++++---- specs/phase1/custody-game.md | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/specs/altair/beacon-chain.md b/specs/altair/beacon-chain.md index 81e7af14b..1ce78ad7c 100644 --- a/specs/altair/beacon-chain.md +++ b/specs/altair/beacon-chain.md @@ -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. @@ -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), @@ -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)) diff --git a/specs/altair/fork.md b/specs/altair/fork.md index 1fb87d554..df85f7c3c 100644 --- a/specs/altair/fork.md +++ b/specs/altair/fork.md @@ -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)) diff --git a/specs/phase0/beacon-chain.md b/specs/phase0/beacon-chain.md index e56a8ea22..e2a17dcfc 100644 --- a/specs/phase0/beacon-chain.md +++ b/specs/phase0/beacon-chain.md @@ -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 ) diff --git a/specs/phase1/beacon-chain.md b/specs/phase1/beacon-chain.md index 21e9751fe..5acc496d6 100644 --- a/specs/phase1/beacon-chain.md +++ b/specs/phase1/beacon-chain.md @@ -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 @@ -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` @@ -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] @@ -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) diff --git a/specs/phase1/custody-game.md b/specs/phase1/custody-game.md index 33c2a0397..136116c2f 100644 --- a/specs/phase1/custody-game.md +++ b/specs/phase1/custody-game.md @@ -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