Merge is_genesis_trigger into get_genesis_state

This commit is contained in:
Justin Drake 2019-06-29 21:18:18 +01:00
parent 22476420f3
commit d00b5b9ea0
1 changed files with 17 additions and 41 deletions

View File

@ -94,7 +94,6 @@
- [`initiate_validator_exit`](#initiate_validator_exit) - [`initiate_validator_exit`](#initiate_validator_exit)
- [`slash_validator`](#slash_validator) - [`slash_validator`](#slash_validator)
- [Genesis](#genesis) - [Genesis](#genesis)
- [Genesis trigger](#genesis-trigger)
- [Genesis state](#genesis-state) - [Genesis state](#genesis-state)
- [Genesis block](#genesis-block) - [Genesis block](#genesis-block)
- [Beacon chain state transition function](#beacon-chain-state-transition-function) - [Beacon chain state transition function](#beacon-chain-state-transition-function)
@ -190,7 +189,7 @@ The following values are (non-configurable) constants used throughout the specif
| `MIN_PER_EPOCH_CHURN_LIMIT` | `2**2` (= 4) | | `MIN_PER_EPOCH_CHURN_LIMIT` | `2**2` (= 4) |
| `CHURN_LIMIT_QUOTIENT` | `2**16` (= 65,536) | | `CHURN_LIMIT_QUOTIENT` | `2**16` (= 65,536) |
| `SHUFFLE_ROUND_COUNT` | `90` | | `SHUFFLE_ROUND_COUNT` | `90` |
| `GENESIS_ACTIVE_VALIDATOR_COUNT` | `2**16` (= 65,536) | | `MIN_GENESIS_ACTIVE_VALIDATOR_COUNT` | `2**16` (= 65,536) |
| `MIN_GENESIS_TIME` | `1578009600` (Jan 3, 2020) | | `MIN_GENESIS_TIME` | `1578009600` (Jan 3, 2020) |
| `JUSTIFICATION_BITS_LENGTH` | `4` | | `JUSTIFICATION_BITS_LENGTH` | `4` |
@ -1091,35 +1090,28 @@ def slash_validator(state: BeaconState,
## Genesis ## Genesis
### Genesis trigger ### Genesis state
Before genesis has been triggered and for every Ethereum 1.0 block call `is_genesis_trigger(deposits: Sequence[Deposit], timestamp: uint64) -> bool` where: Before the Ethereum 2.0 genesis has been triggered and for every Ethereum 1.0 block, call `get_genesis_beacon_state(eth1_block_hash, eth1_timestamp, deposits)` where:
* `deposits` is the sequence of all deposits, ordered chronologically, up to and including the deposit triggering the latest `Deposit` log * `eth1_block_hash` is the hash of the Ethereum 1.0 block
* `timestamp` is the Unix timestamp in the Ethereum 1.0 block that emitted the latest `Deposit` log * `eth1_timestamp` is the Unix timestamp corresponding to `eth1_block_hash`
* `deposits` is the sequence of all deposits, ordered chronologically, up to the block with hash `eth1_block_hash`
When `is_genesis_trigger(deposits, timestamp) is True` for the first time, let: The genesis state `genesis_state` is defined as the return value of the first call to `get_genesis_beacon_state` that does not trigger an `assert`.
* `genesis_deposits = deposits` *Note*: The two constants `MIN_GENESIS_TIME` and `MIN_GENESIS_ACTIVE_VALIDATOR_COUNT` in `get_genesis_beacon_state` have yet to be agreed upon by the community, and can be updated as necessary.
* `genesis_time = timestamp - timestamp % SECONDS_PER_DAY + 2 * SECONDS_PER_DAY`
* `genesis_eth1_block_hash` is the Ethereum 1.0 block hash that emitted the log for the last deposit in `deposits`
*Note*: The function `is_genesis_trigger` has yet to be agreed upon by the community, and can be updated as necessary. We define the following testing placeholder:
```python ```python
def is_genesis_trigger(deposits: Sequence[Deposit], timestamp: uint64) -> bool: def get_genesis_beacon_state(eth1_block_hash: Hash, eth1_timestamp: int, deposits: Sequence[Deposit]) -> BeaconState:
# Do not deploy too early state = BeaconState(
if timestamp - timestamp % SECONDS_PER_DAY + 2 * SECONDS_PER_DAY < MIN_GENESIS_TIME: genesis_time=timestamp - timestamp % SECONDS_PER_DAY + 2 * SECONDS_PER_DAY,
return False eth1_data=Eth1Data(block_hash=genesis_eth1_block_hash),
# Process genesis deposits latest_block_header=BeaconBlockHeader(body_root=hash_tree_root(BeaconBlockBody())),
state = BeaconState() )
process_genesis_deposits(state, deposits) assert state.genesis_time >= MIN_GENESIS_TIME
# Check active validator count
return len(get_active_validator_indices(state, GENESIS_EPOCH)) >= GENESIS_ACTIVE_VALIDATOR_COUNT
```
```python # Process deposits
def process_genesis_deposits(state: BeaconState, deposits: Sequence[Deposit]) -> None:
leaves = list(map(lambda deposit: deposit.data, deposits)) leaves = list(map(lambda deposit: deposit.data, deposits))
for deposit_index, deposit in enumerate(deposits): for deposit_index, deposit in enumerate(deposits):
state.eth1_data.deposit_root = hash_tree_root( state.eth1_data.deposit_root = hash_tree_root(
@ -1133,23 +1125,7 @@ def process_genesis_deposits(state: BeaconState, deposits: Sequence[Deposit]) ->
if validator.effective_balance == MAX_EFFECTIVE_BALANCE: if validator.effective_balance == MAX_EFFECTIVE_BALANCE:
validator.activation_eligibility_epoch = GENESIS_EPOCH validator.activation_eligibility_epoch = GENESIS_EPOCH
validator.activation_epoch = GENESIS_EPOCH validator.activation_epoch = GENESIS_EPOCH
``` assert len(get_active_validator_indices(state, GENESIS_EPOCH)) >= MIN_GENESIS_ACTIVE_VALIDATOR_COUNT
### Genesis state
Let `genesis_state = get_genesis_beacon_state(genesis_deposits, genesis_time, genesis_eth1_block_hash)`.
```python
def get_genesis_beacon_state(deposits: Sequence[Deposit],
genesis_time: int,
genesis_eth1_block_hash: Hash) -> BeaconState:
# Process genesis deposits
state = BeaconState(
genesis_time=genesis_time,
eth1_data=Eth1Data(block_hash=genesis_eth1_block_hash),
latest_block_header=BeaconBlockHeader(body_root=hash_tree_root(BeaconBlockBody())),
)
process_genesis_deposits(state, deposits)
# Populate active_index_roots # Populate active_index_roots
genesis_active_index_root = hash_tree_root( genesis_active_index_root = hash_tree_root(