Merge pull request #477 from ethereum/type_hinting

Add custom type hinting
This commit is contained in:
Hsiao-Wei Wang 2019-01-26 16:09:22 +08:00 committed by GitHub
commit c26a92672e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 70 additions and 43 deletions

View File

@ -48,6 +48,7 @@
- [`Fork`](#fork)
- [`Eth1Data`](#eth1data)
- [`Eth1DataVote`](#eth1datavote)
- [Custom Types](#custom-types)
- [Ethereum 1.0 deposit contract](#ethereum-10-deposit-contract)
- [Deposit arguments](#deposit-arguments)
- [Withdrawal credentials](#withdrawal-credentials)
@ -115,7 +116,7 @@
- [Attestation inclusion](#attestation-inclusion)
- [Crosslinks](#crosslinks-1)
- [Ejections](#ejections)
- [Validator registry](#validator-registry)
- [Validator registry and shuffling seed data](#validator-registry-and-shuffling-seed-data)
- [Final updates](#final-updates)
- [State root processing](#state-root-processing)
- [References](#references)
@ -248,6 +249,8 @@ Code snippets appearing in `this style` are to be interpreted as Python code. Be
## Data structures
The following data structures are defined as [SimpleSerialize (SSZ)](https://github.com/ethereum/eth2.0-specs/blob/master/specs/simple-serialize.md) objects.
### Beacon chain operations
#### Proposer slashings
@ -576,9 +579,9 @@ Code snippets appearing in `this style` are to be interpreted as Python code. Be
```python
{
# Root of the deposit tree
'deposit_root': 'hash32',
'deposit_root': 'bytes32',
# Block hash
'block_hash': 'hash32',
'block_hash': 'bytes32',
}
```
@ -593,6 +596,20 @@ Code snippets appearing in `this style` are to be interpreted as Python code. Be
}
```
## Custom Types
We define the following Python custom types for type hinting and readability:
| Name | Type | Description |
| - | - | - |
| `SlotNumber` | unsigned 64-bit integer | the number of a slot |
| `ShardNumber` | unsigned 64-bit integer | the number of a shard |
| `ValidatorIndex` | unsigned 24-bit integer | the index number of a validator in the registry |
| `Gwei` | unsigned 64-bit integer | an amount in Gwei |
| `Bytes32` | 32-byte data | binary data with 32-byte length |
| `BLSPubkey` | 48-byte data | a public key in BLS signature scheme |
| `BLSSignature` | 96-byte data | a signature in BLS signature scheme |
## Ethereum 1.0 deposit contract
The initial deployment phases of Ethereum 2.0 are implemented without consensus changes to Ethereum 1.0. A deposit contract at address `DEPOSIT_CONTRACT_ADDRESS` is added to Ethereum 1.0 for deposits of ETH to the beacon chain. Validator balances will be withdrawable to the shards in phase 2, i.e. when the EVM2.0 is deployed and the shards have state.
@ -711,28 +728,38 @@ Beacon block production is significantly different because of the proof of stake
The beacon chain fork choice rule is a hybrid that combines justification and finality with Latest Message Driven (LMD) Greediest Heaviest Observed SubTree (GHOST). At any point in time a [validator](#dfn-validator) `v` subjectively calculates the beacon chain head as follows.
* Let `store` be the set of attestations and blocks that the [validator](#dfn-validator) `v` has observed and verified (in particular, block ancestors must be recursively verified). Attestations not part of any chain are still included in `store`.
* Abstractly define `Store` as the type of storage object for the chain data and `store` be the set of attestations and blocks that the [validator](#dfn-validator) `v` has observed and verified (in particular, block ancestors must be recursively verified). Attestations not yet included in any chain are still included in `store`.
* Let `finalized_head` be the finalized block with the highest slot number. (A block `B` is finalized if there is a descendant of `B` in `store` the processing of which sets `B` as finalized.)
* Let `justified_head` be the descendant of `finalized_head` with the highest slot number that has been justified for at least `EPOCH_LENGTH` slots. (A block `B` is justified if there is a descendant of `B` in `store` the processing of which sets `B` as justified.) If no such descendant exists set `justified_head` to `finalized_head`.
* Let `get_ancestor(store, block, slot)` be the ancestor of `block` with slot number `slot`. The `get_ancestor` function can be defined recursively as `def get_ancestor(store, block, slot): return block if block.slot == slot else get_ancestor(store, store.get_parent(block), slot)`.
* Let `get_latest_attestation(store, validator)` be the attestation with the highest slot number in `store` from `validator`. If several such attestations exist, use the one the [validator](#dfn-validator) `v` observed first.
* Let `get_latest_attestation_target(store, validator)` be the target block in the attestation `get_latest_attestation(store, validator)`.
* The head is `lmd_ghost(store, justified_head)` where the function `lmd_ghost` is defined below. Note that the implementation below is suboptimal; there are implementations that compute the head in time logarithmic in slot count.
* Let `get_ancestor(store: Store, block: BeaconBlock, slot: SlotNumber) -> BeaconBlock` be the ancestor of `block` with slot number `slot`. The `get_ancestor` function can be defined recursively as `def get_ancestor(store: Store, block: BeaconBlock, slot: SlotNumber) -> BeaconBlock: return block if block.slot == slot else get_ancestor(store, store.get_parent(block), slot)`.
* Let `get_latest_attestation(store: Store, validator: Validator) -> Attestation` be the attestation with the highest slot number in `store` from `validator`. If several such attestations exist, use the one the [validator](#dfn-validator) `v` observed first.
* Let `get_latest_attestation_target(store: Store, validator: Validator) -> BeaconBlock` be the target block in the attestation `get_latest_attestation(store, validator)`.
* Let `get_children(store: Store, block: BeaconBlock) -> List[BeaconBlock]` returns the child blocks of the given `block`.
* Let `justified_head_state` be the resulting `BeaconState` object from processing the chain up to the `justified_head`.
* The `head` is `lmd_ghost(store, justified_head_state, justified_head)` where the function `lmd_ghost` is defined below. Note that the implementation below is suboptimal; there are implementations that compute the head in time logarithmic in slot count.
```python
def lmd_ghost(store, start):
validators = start.state.validator_registry
active_validators = [validators[i] for i in
get_active_validator_indices(validators, start.state.slot)]
attestation_targets = [get_latest_attestation_target(store, validator)
for validator in active_validators]
def get_vote_count(block):
return len([target for target in attestation_targets if
get_ancestor(store, target, block.slot) == block])
def lmd_ghost(store: Store, start_state: BeaconState, start_block: BeaconBlock) -> BeaconBlock:
validators = start_state.validator_registry
active_validators = [
validators[i]
for i in get_active_validator_indices(validators, start_state.slot)
]
attestation_targets = [
get_latest_attestation_target(store, validator)
for validator in active_validators
]
head = start
def get_vote_count(block: BeaconBlock) -> int:
return len([
target
for target in attestation_targets
if get_ancestor(store, target, block.slot) == block
])
head = start_block
while 1:
children = get_children(head)
children = get_children(store, head)
if len(children) == 0:
return head
head = max(children, key=get_vote_count)
@ -759,11 +786,11 @@ Note: We aim to migrate to a S[T/N]ARK-friendly hash function in a future Ethere
#### `hash_tree_root`
`hash_tree_root` is a function for hashing objects into a single root utilizing a hash tree structure. `hash_tree_root` is defined in the [SimpleSerialize spec](https://github.com/ethereum/eth2.0-specs/blob/master/specs/simple-serialize.md#tree-hash).
`def hash_tree_root(object: SSZSerializable) -> Bytes32` is a function for hashing objects into a single root utilizing a hash tree structure. `hash_tree_root` is defined in the [SimpleSerialize spec](https://github.com/ethereum/eth2.0-specs/blob/master/specs/simple-serialize.md#tree-hash).
#### `is_active_validator`
```python
def is_active_validator(validator: Validator, slot: int) -> bool:
def is_active_validator(validator: Validator, slot: SlotNumber) -> bool:
"""
Checks if ``validator`` is active.
"""
@ -773,7 +800,7 @@ def is_active_validator(validator: Validator, slot: int) -> bool:
#### `get_active_validator_indices`
```python
def get_active_validator_indices(validators: [Validator], slot: int) -> List[int]:
def get_active_validator_indices(validators: List[Validator], slot: SlotNumber) -> List[ValidatorIndex]:
"""
Gets indices of active validators from ``validators``.
"""
@ -865,7 +892,7 @@ def get_committee_count_per_slot(active_validator_count: int) -> int:
```python
def get_shuffling(seed: Bytes32,
validators: List[Validator],
slot: int) -> List[List[int]]
slot: SlotNumber) -> List[List[ValidatorIndex]]
"""
Shuffles ``validators`` into crosslink committees seeded by ``seed`` and ``slot``.
Returns a list of ``EPOCH_LENGTH * committees_per_slot`` committees where each
@ -917,7 +944,7 @@ def get_current_epoch_committee_count_per_slot(state: BeaconState) -> int:
```python
def get_crosslink_committees_at_slot(state: BeaconState,
slot: int) -> List[Tuple[List[int], int]]:
slot: SlotNumber) -> List[Tuple[List[ValidatorIndex], ShardNumber]]:
"""
Returns the list of ``(committee, shard)`` tuples for the ``slot``.
"""
@ -958,7 +985,7 @@ def get_crosslink_committees_at_slot(state: BeaconState,
```python
def get_block_root(state: BeaconState,
slot: int) -> Bytes32:
slot: SlotNumber) -> Bytes32:
"""
Returns the block root at a recent ``slot``.
"""
@ -973,7 +1000,7 @@ def get_block_root(state: BeaconState,
```python
def get_randao_mix(state: BeaconState,
slot: int) -> Bytes32:
slot: SlotNumber) -> Bytes32:
"""
Returns the randao mix at a recent ``slot``.
"""
@ -986,7 +1013,7 @@ def get_randao_mix(state: BeaconState,
```python
def get_active_index_root(state: BeaconState,
slot: int) -> Bytes32:
slot: SlotNumber) -> Bytes32:
"""
Returns the index root at a recent ``slot``.
"""
@ -1001,7 +1028,7 @@ def get_active_index_root(state: BeaconState,
```python
def get_beacon_proposer_index(state: BeaconState,
slot: int) -> int:
slot: SlotNumber) -> ValidatorIndex:
"""
Returns the beacon proposer index for the ``slot``.
"""
@ -1027,7 +1054,7 @@ def merkle_root(values: List[Bytes32]) -> Bytes32:
```python
def get_attestation_participants(state: BeaconState,
attestation_data: AttestationData,
aggregation_bitfield: bytes) -> List[int]:
aggregation_bitfield: bytes) -> List[ValidatorIndex]:
"""
Returns the participant indices at for the ``attestation_data`` and ``aggregation_bitfield``.
"""
@ -1055,7 +1082,7 @@ def get_attestation_participants(state: BeaconState,
#### `get_effective_balance`
```python
def get_effective_balance(state: State, index: int) -> int:
def get_effective_balance(state: State, index: ValidatorIndex) -> Gwei:
"""
Returns the effective balance (also known as "balance at stake") for a ``validator`` with the given ``index``.
"""
@ -1066,7 +1093,7 @@ def get_effective_balance(state: State, index: int) -> int:
```python
def get_fork_version(fork: Fork,
slot: int) -> int:
slot: SlotNumber) -> int:
if slot < fork.slot:
return fork.previous_version
else:
@ -1077,7 +1104,7 @@ def get_fork_version(fork: Fork,
```python
def get_domain(fork: Fork,
slot: int,
slot: SlotNumber,
domain_type: int) -> int:
return get_fork_version(
fork,
@ -1293,8 +1320,8 @@ First, a helper function:
```python
def validate_proof_of_possession(state: BeaconState,
pubkey: Bytes48,
proof_of_possession: Bytes96,
pubkey: BLSPubkey,
proof_of_possession: BLSSignature,
withdrawal_credentials: Bytes32) -> bool:
proof_of_possession_data = DepositInput(
pubkey=pubkey,
@ -1318,9 +1345,9 @@ Now, to add a [validator](#dfn-validator) or top up an existing [validator](#dfn
```python
def process_deposit(state: BeaconState,
pubkey: Bytes48,
amount: int,
proof_of_possession: Bytes96,
pubkey: BLSPubkey,
amount: Gwei,
proof_of_possession: BLSSignature,
withdrawal_credentials: Bytes32) -> None:
"""
Process a deposit from Ethereum 1.0.
@ -1368,20 +1395,20 @@ def process_deposit(state: BeaconState,
Note: All functions in this section mutate `state`.
```python
def activate_validator(state: BeaconState, index: int, genesis: bool) -> None:
def activate_validator(state: BeaconState, index: ValidatorIndex, genesis: bool) -> None:
validator = state.validator_registry[index]
validator.activation_slot = GENESIS_SLOT if genesis else entry_exit_effect_slot(state.slot)
```
```python
def initiate_validator_exit(state: BeaconState, index: int) -> None:
def initiate_validator_exit(state: BeaconState, index: ValidatorIndex) -> None:
validator = state.validator_registry[index]
validator.status_flags |= INITIATED_EXIT
```
```python
def exit_validator(state: BeaconState, index: int) -> None:
def exit_validator(state: BeaconState, index: ValidatorIndex) -> None:
validator = state.validator_registry[index]
# The following updates only occur if not previous exited
@ -1395,7 +1422,7 @@ def exit_validator(state: BeaconState, index: int) -> None:
```
```python
def penalize_validator(state: BeaconState, index: int) -> None:
def penalize_validator(state: BeaconState, index: ValidatorIndex) -> None:
exit_validator(state, index)
validator = state.validator_registry[index]
state.latest_penalized_balances[(state.slot // EPOCH_LENGTH) % LATEST_PENALIZED_EXIT_LENGTH] += get_effective_balance(state, index)
@ -1408,7 +1435,7 @@ def penalize_validator(state: BeaconState, index: int) -> None:
```
```python
def prepare_validator_for_withdrawal(state: BeaconState, index: int) -> None:
def prepare_validator_for_withdrawal(state: BeaconState, index: ValidatorIndex) -> None:
validator = state.validator_registry[index]
validator.status_flags |= WITHDRAWABLE
```
@ -1519,7 +1546,7 @@ For each `deposit` in `block.body.deposits`:
* Verify that `verify_merkle_branch(hash(serialized_deposit_data), deposit.branch, DEPOSIT_CONTRACT_TREE_DEPTH, deposit.index, state.latest_eth1_data.deposit_root)` is `True`.
```python
def verify_merkle_branch(leaf: Bytes32, branch: [Bytes32], depth: int, index: int, root: Bytes32) -> bool:
def verify_merkle_branch(leaf: Bytes32, branch: List[Bytes32], depth: int, index: int, root: Bytes32) -> bool:
value = leaf
for i in range(depth):
if index // (2**i) % 2: