From bc53f0e386ce822278fe9b429a6d8b8589138e8d Mon Sep 17 00:00:00 2001 From: vbuterin Date: Thu, 22 Nov 2018 08:24:20 -0500 Subject: [PATCH 01/20] Fork choice rule Added the LMD GHOST fork choice rule. --- specs/core/0_beacon-chain.md | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 9ac39501c..423d26f1b 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -337,15 +337,33 @@ Beacon block production is significantly different because of the proof of stake ### Beacon chain fork choice rule -The beacon chain uses the Casper FFG fork choice rule of "favor the chain containing the highest-slot-number justified block". To choose between chains that are all descended from the same justified block, the chain uses "immediate message driven GHOST" (IMD GHOST) to choose the head of the chain. +The beacon chain uses a hybrid fork choice rule that combines together FFG justification/finality with latest-message-driven GHOST. -For a description see: **https://ethresear.ch/t/beacon-chain-casper-ffg-rpj-mini-spec/2760** +The following is a description of how to calculate the head at any given point in time. -For an implementation with a network simulator see: **https://github.com/ethereum/research/blob/master/clock_disparity/ghost_node.py** +1. Set `finalized_head` to the finalized block with the highest slot number. +2. Set `justified_head` be the descendant of `finalized_head` that is justified, and has been known to be justified for at least `SLOT_DURATION * CYCLE_LENGTH` seconds, with the highest slot number. +3. Return `LMD_GHOST(store, justified_head)`, where `store` contains all attestations and blocks that the validator has received. -Here's an example of its working (green is finalized blocks, yellow is justified, grey is attestations): +Let `get_most_recent_attestation_target(store, v)` be a function that returns the block that a validator attested to in the attestation in `store` that has the highest slot number. Let `get_ancestor(store, block, slot)` be the function that returns the ancestor of the given block at the given slot; it could 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)`. LMD GHOST is defined as follows: -![](https://vitalik.ca/files/RPJ.png) +```python +def lmd_ghost(store, start): + validators = [start.state.validators[i] for i in range(get_active_validators(start.state.validators, start.slot))] + latest_message_targets = [get_most_recent_attestation_target(store, v) for v in validators] + head = start + while 1: + c = get_children(head) + if len(c) == 0: + return head + + def get_vote_count(block): + return len([t for t in latest_message_targets if get_ancestor(store, t, block.slot) == block]) + + head = max(c, key=get_vote_count) +``` + +Note that the above is a definition, not an optimal implementation; implementations that determine the head in logarithmic time are possible. ## Beacon chain state transition function From 50458c1d3101e4807ffb58a2ff3261b4c434617a Mon Sep 17 00:00:00 2001 From: Justin Date: Thu, 22 Nov 2018 23:09:36 +0000 Subject: [PATCH 02/20] Various fixes regarding PoW receipt roots Question: What happens if the block proposer at the `POW_RECEIPT_ROOT_VOTING_PERIOD` boundary puts a bad receipt root? Does this give too much power to that block proposer? --- specs/core/0_beacon-chain.md | 52 ++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 9ac39501c..1a373eb2f 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -44,17 +44,17 @@ The primary source of load on the beacon chain are "attestations". Attestations | `MIN_VALIDATOR_SET_CHANGE_INTERVAL` | 2**8 (= 256) | slots | ~25 minutes | | `SHARD_PERSISTENT_COMMITTEE_CHANGE_PERIOD` | 2**17 (= 131,072) | slots | ~9 days | | `MIN_ATTESTATION_INCLUSION_DELAY` | 2**2 (= 4) | slots | ~24 seconds | -| `RANDAO_SLOTS_PER_LAYER` | 2**12 (= 4096) | slots | ~7 hours | +| `RANDAO_SLOTS_PER_LAYER` | 2**12 (= 4,096) | slots | ~7 hours | | `SQRT_E_DROP_TIME` | 2**18 (= 262,144) | slots | ~18 days | | `WITHDRAWALS_PER_CYCLE` | 2**2 (=4) | validators | 5.2m ETH in ~6 months | -| `MIN_WITHDRAWAL_PERIOD` | 2**13 (= 8192) | slots | ~14 hours | +| `MIN_WITHDRAWAL_PERIOD` | 2**13 (= 8,192) | slots | ~14 hours | | `DELETION_PERIOD` | 2**22 (= 4,194,304) | slots | ~290 days | | `COLLECTIVE_PENALTY_CALCULATION_PERIOD` | 2**20 (= 1,048,576) | slots | ~2.4 months | +| `POW_RECEIPT_ROOT_VOTING_PERIOD` | 2**10 (= 1,024) | slots | ~1.7 hours | | `SLASHING_WHISTLEBLOWER_REWARD_DENOMINATOR` | 2**9 (= 512) | | `BASE_REWARD_QUOTIENT` | 2**15 (= 32,768) | — | | `MAX_VALIDATOR_CHURN_QUOTIENT` | 2**5 (= 32) | — | -| `POW_HASH_VOTING_PERIOD` | 2**10 (=1024) | - | -| `POW_CONTRACT_MERKLE_TREE_DEPTH` | 2**5 (=32) | - | +| `POW_CONTRACT_MERKLE_TREE_DEPTH` | 2**5 (= 32) | - | | `LOGOUT_MESSAGE` | `"LOGOUT"` | — | | `INITIAL_FORK_VERSION` | 0 | — | @@ -109,7 +109,7 @@ A `BeaconBlock` has the following fields: 'slot': 'uint64', # Proposer RANDAO reveal 'randao_reveal': 'hash32', - # Recent PoW chain reference (receipt root) + # Recent PoW receipt root 'candidate_pow_receipt_root': 'hash32', # Skip list of previous beacon block hashes # i'th item is the most recent ancestor whose slot is a multiple of 2**i for i = 0, ..., 31 @@ -236,8 +236,8 @@ The `BeaconState` has the following fields: 'current_exit_seq': 'uint64', # Genesis time 'genesis_time': 'uint64', - # PoW chain reference - 'known_pow_receipt_root': 'hash32', + # PoW receipt root + 'processed_pow_receipt_root': 'hash32', 'candidate_pow_receipt_root': 'hash32', 'candidate_pow_receipt_root_votes': 'uint64', # Parameters relevant to hard forks / versioning. @@ -328,7 +328,7 @@ For a block on the beacon chain to be processed by a node, four conditions have * The parent pointed to by the `ancestor_hashes[0]` has already been processed and accepted * An attestation from the _proposer_ of the block (see later for definition) is included along with the block in the network message object -* The PoW chain block pointed to by the `pow_chain_reference` has already been processed and accepted +* The PoW chain block pointed to by the `processed_pow_receipt_root` has already been processed and accepted * The node's local clock time is greater than or equal to the minimum timestamp as computed by `GENESIS_TIME + block.slot * SLOT_DURATION` If these conditions are not met, the client should delay processing the beacon block until the conditions are all satisfied. @@ -551,8 +551,8 @@ def int_sqrt(n: int) -> int: The beacon chain is initialized when a condition is met inside a contract on the existing PoW chain. This contract's code in Vyper is as follows: ```python -HashChainValue: event({prev_tip: bytes32, data: bytes[2064], total_deposit_count: int128}) -ChainStart: event({hash_chain_tip: bytes32, time: bytes[8]}) +HashChainValue: event({previous_receipt_root: bytes32, data: bytes[2064], total_deposit_count: int128}) +ChainStart: event({receipt_root: bytes32, time: bytes[8]}) receipt_tree: bytes32[int128] total_deposit_count: int128 @@ -597,7 +597,7 @@ If the user wishes to deposit more than `DEPOSIT_SIZE` ETH, they would need to m * `initial_validator_entries` equal to the list of data records published as HashChainValue logs so far, in the order in which they were published (oldest to newest). * `genesis_time` equal to the `time` value published in the log -* `pow_hash_chain_tip` equal to the `hash_chain_tip` value published in the log +* `pow_receipt_root` equal to the `receipt_root` value published in the log ### On startup @@ -619,7 +619,7 @@ A valid block with slot `0` (the "genesis block") has the following values. Othe `STARTUP_STATE_ROOT` is the root of the initial state, computed by running the following code: ```python -def on_startup(initial_validator_entries: List[Any], genesis_time: uint64, pow_hash_chain_tip: Hash32) -> BeaconState: +def on_startup(initial_validator_entries: List[Any], genesis_time: uint64, pow_receipt_root: Hash32) -> BeaconState: # Induct validators validators = [] for pubkey, proof_of_possession, withdrawal_shard, withdrawal_address, \ @@ -655,14 +655,13 @@ def on_startup(initial_validator_entries: List[Any], genesis_time: uint64, pow_h persistent_committees=split(shuffle(validators, bytes([0] * 32)), SHARD_COUNT), persistent_committee_reassignments=[], deposits_penalized_in_period=[], - next_shuffling_seed=b'\x00'*32, + next_shuffling_seed=bytes([0] * 32), validator_set_delta_hash_chain=bytes([0] * 32), # stub current_exit_seq=0, genesis_time=genesis_time, - known_pow_hash_chain_tip=pow_hash_chain_tip, - processed_pow_hash_chain_tip=pow_hash_chain_tip, - candidate_pow_hash_chain_tip=bytes([0] * 32), - candidate_pow_hash_chain_tip_votes=0, + processed_pow_receipt_root=pow_receipt_root, + candidate_pow_receipt_root=bytes([0] * 32), + candidate_pow_receipt_root_votes=0, pre_fork_version=INITIAL_FORK_VERSION, post_fork_version=INITIAL_FORK_VERSION, fork_slot_number=0, @@ -790,7 +789,7 @@ def on_startup(initial_validator_entries: List[Any]) -> BeaconState: persistent_committees=split(shuffle(validators, bytes([0] * 32)), SHARD_COUNT), persistent_committee_reassignments=[], deposits_penalized_in_period=[], - next_shuffling_seed=b'\x00'*32, + next_shuffling_seed=bytes([0] * 32), validator_set_delta_hash_chain=bytes([0] * 32), # stub pre_fork_version=INITIAL_FORK_VERSION, post_fork_version=INITIAL_FORK_VERSION, @@ -860,11 +859,13 @@ Verify that `BLSVerify(pubkey=get_beacon_proposer(state, block.slot).pubkey, dat ### Verify and process RANDAO reveal * Let `repeat_hash(x, n) = x if n == 0 else repeat_hash(hash(x), n-1)`. -* Let `V = get_beacon_proposer(state, block.slot). +* Let `V = get_beacon_proposer(state, block.slot)`. * Verify that `repeat_hash(block.randao_reveal, (block.slot - V.randao_last_change) // RANDAO_SLOTS_PER_LAYER + 1) == V.randao_commitment` * Set `state.randao_mix = xor(state.randao_mix, block.randao_reveal)`, `V.randao_commitment = block.randao_reveal`, `V.randao_last_change = block.slot` -Finally, if `block.candidate_pow_hash_chain_tip = state.candidate_pow_hash_chain_tip`, set `state.candidate_hash_chain_tip_votes += 1`. +### Process PoW receipt root + +If `block.candidate_pow_receipt_root = state.candidate_pow_receipt_root` set `state.candidate_pow_receipt_root_votes += 1`. ### Process penalties, logouts and other special objects @@ -938,7 +939,7 @@ For each `proposal_signature`, verify that `BLSVerify(pubkey=validators[proposer Note that `deposit_data` in serialized form should be the `DepositParams` followed by 8 bytes for the `msg_value` and 8 bytes for the `timestamp`, or exactly the `deposit_data` in the PoW contract of which the hash was placed into the Merkle tree. -Use the following procedure to verify the `merkle_branch`, setting `leaf=serialized_deposit_data`, `depth=POW_CONTRACT_MERKLE_TREE_DEPTH` and `root=state.known_pow_receipt_root`: +Use the following procedure to verify the `merkle_branch`, setting `leaf=serialized_deposit_data`, `depth=POW_CONTRACT_MERKLE_TREE_DEPTH` and `root=state.processed_pow_receipt_root`: ```python def verify_merkle_branch(leaf: Hash32, branch: [Hash32], depth: int, index: int, root: Hash32) -> bool: @@ -1012,11 +1013,11 @@ For every shard number `shard` for which a crosslink committee exists in the cyc #### PoW chain related rules -If `last_state_recalculation_slot % POW_HASH_VOTING_PERIOD == 0`, then: +If `last_state_recalculation_slot % POW_RECEIPT_ROOT_VOTING_PERIOD == 0`, then: -* If `state.candidate_hash_chain_tip_votes * 3 >= POW_HASH_VOTING_PERIOD * 2`, set `state.hash_chain_tip = state.candidate_hash_chain_tip` -* Set `state.candidate_hash_chain_tip = block.candidate_pow_hash_chain_tip` -* Set `state.candidate_hash_chain_tip_votes = 0` +* If `state.candidate_pow_receipt_root_votes * 3 >= POW_RECEIPT_ROOT_VOTING_PERIOD * 2` set `state.processed_pow_receipt_root = state.candidate_pow_receipt_root`. +* Set `state.candidate_pow_receipt_root = block.candidate_pow_receipt_root`. +* Set `state.candidate_pow_receipt_root_votes = 0`. ### Validator set change @@ -1151,7 +1152,6 @@ Note: This spec is ~65% complete. **Missing** -* [ ] Specify the rules around acceptable values for `pow_chain_reference` ([issue 58](https://github.com/ethereum/eth2.0-specs/issues/58)) * [ ] Specify the shard chain blocks, blobs, proposers, etc. * [ ] Specify the deposit contract on the PoW chain in Vyper * [ ] Specify the beacon chain genesis rules ([issue 58](https://github.com/ethereum/eth2.0-specs/issues/58)) From f3094e179e1523e3b8bde078ca3231dfc04729c7 Mon Sep 17 00:00:00 2001 From: vbuterin Date: Fri, 23 Nov 2018 08:54:24 -0500 Subject: [PATCH 03/20] Resolved Justin's questions --- specs/core/0_beacon-chain.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 423d26f1b..ae067f588 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -341,11 +341,11 @@ The beacon chain uses a hybrid fork choice rule that combines together FFG justi The following is a description of how to calculate the head at any given point in time. -1. Set `finalized_head` to the finalized block with the highest slot number. -2. Set `justified_head` be the descendant of `finalized_head` that is justified, and has been known to be justified for at least `SLOT_DURATION * CYCLE_LENGTH` seconds, with the highest slot number. -3. Return `LMD_GHOST(store, justified_head)`, where `store` contains all attestations and blocks that the validator has received. +1. Set `finalized_head` to the finalized block with the highest slot number (as in, the block B with the highest slot number such that there exists a descendant of B the processing of which set B as finalized) +2. Set `justified_head` be the descendant of `finalized_head` that is justified, and has been known to be justified for at least `SLOT_DURATION * CYCLE_LENGTH` seconds, with the highest slot number (as in, the block B with the highest slot number such that there exists a descendant of B the processing of which set B as justified; if no such block exists, then it is just set to the `finalized_head`) +3. Return `LMD_GHOST(store, justified_head)`, where `store` contains all attestations and blocks that the validator has received, including those that have not been included in any chain. -Let `get_most_recent_attestation_target(store, v)` be a function that returns the block that a validator attested to in the attestation in `store` that has the highest slot number. Let `get_ancestor(store, block, slot)` be the function that returns the ancestor of the given block at the given slot; it could 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)`. LMD GHOST is defined as follows: +Let `get_most_recent_attestation_target(store, v)` be a function that returns the block that a validator attested to in the attestation in `store` with the highest slot number; only attestations pointing to blocks that the validator has verified (including recursively verifying ancestors) are considered. If more than one attestation exists in the same slot, `get_most_recent_attestation_target` should return the attestation that the client learned about earlier. Let `get_ancestor(store, block, slot)` be the function that returns the ancestor of the given block at the given slot; it could 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)`. LMD GHOST is defined as follows: ```python def lmd_ghost(store, start): From 92981e9714e0a8f6d62c90af35544edfacdbf0b4 Mon Sep 17 00:00:00 2001 From: Justin Date: Fri, 23 Nov 2018 14:46:25 +0000 Subject: [PATCH 04/20] Started cleaning up fork choice rule definition --- specs/core/0_beacon-chain.md | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index ae067f588..53845a3ba 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -337,15 +337,15 @@ Beacon block production is significantly different because of the proof of stake ### Beacon chain fork choice rule -The beacon chain uses a hybrid fork choice rule that combines together FFG justification/finality with latest-message-driven GHOST. +The beacon chain fork choice rule is a hybrid that combines justification and finality with Latest Message Driven (LMD) GHOST. At any point in time a validator `v` calculates the head as follows. -The following is a description of how to calculate the head at any given point in time. - -1. Set `finalized_head` to the finalized block with the highest slot number (as in, the block B with the highest slot number such that there exists a descendant of B the processing of which set B as finalized) -2. Set `justified_head` be the descendant of `finalized_head` that is justified, and has been known to be justified for at least `SLOT_DURATION * CYCLE_LENGTH` seconds, with the highest slot number (as in, the block B with the highest slot number such that there exists a descendant of B the processing of which set B as justified; if no such block exists, then it is just set to the `finalized_head`) -3. Return `LMD_GHOST(store, justified_head)`, where `store` contains all attestations and blocks that the validator has received, including those that have not been included in any chain. - -Let `get_most_recent_attestation_target(store, v)` be a function that returns the block that a validator attested to in the attestation in `store` with the highest slot number; only attestations pointing to blocks that the validator has verified (including recursively verifying ancestors) are considered. If more than one attestation exists in the same slot, `get_most_recent_attestation_target` should return the attestation that the client learned about earlier. Let `get_ancestor(store, block, slot)` be the function that returns the ancestor of the given block at the given slot; it could 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)`. LMD GHOST is defined as follows: +* Let `store` be the set of all attestations and blocks that the validator `v` has verified (in particular, ancestors must be recursively verified), including attestations not included in any chain. +* 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 `CYCLE_LENGTH` slots. (A block `B` is justified is 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_most_recent_attestation(store, validator)` be the attestation in `store` from `validator` with the highest slot number. If several such attestations exist use the one the validator `v` verified first. +* Let `get_most_recent_attestation_target(store, validator)` be the target block in `get_most_recent_attestation(store, validator)`. +* 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)`. +* 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 logarithmic time. ```python def lmd_ghost(store, start): @@ -363,8 +363,6 @@ def lmd_ghost(store, start): head = max(c, key=get_vote_count) ``` -Note that the above is a definition, not an optimal implementation; implementations that determine the head in logarithmic time are possible. - ## Beacon chain state transition function We now define the state transition function. At the high level, the state transition is made up of two parts: From 361bcd6be493d4933461cdf32aba9b33641845e8 Mon Sep 17 00:00:00 2001 From: Justin Date: Fri, 23 Nov 2018 15:23:22 +0000 Subject: [PATCH 05/20] More polishing to fork choice rule --- specs/core/0_beacon-chain.md | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 53845a3ba..7f4522360 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -337,30 +337,29 @@ Beacon block production is significantly different because of the proof of stake ### Beacon chain fork choice rule -The beacon chain fork choice rule is a hybrid that combines justification and finality with Latest Message Driven (LMD) GHOST. At any point in time a validator `v` calculates the head as follows. +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 `v` calculates the beacon chain head as follows. -* Let `store` be the set of all attestations and blocks that the validator `v` has verified (in particular, ancestors must be recursively verified), including attestations not included in any chain. +* Let `store` be the set of attestations and blocks that the 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`. * 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 `CYCLE_LENGTH` slots. (A block `B` is justified is 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_most_recent_attestation(store, validator)` be the attestation in `store` from `validator` with the highest slot number. If several such attestations exist use the one the validator `v` verified first. -* Let `get_most_recent_attestation_target(store, validator)` be the target block in `get_most_recent_attestation(store, validator)`. * 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 in `store` from `validator` with the highest slot number. If several such attestations exist use the one the validator `v` verified 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 logarithmic time. ```python def lmd_ghost(store, start): - validators = [start.state.validators[i] for i in range(get_active_validators(start.state.validators, start.slot))] - latest_message_targets = [get_most_recent_attestation_target(store, v) for v in validators] + active_validators = [start.state.validators[i] for i in range(get_active_validators(start.state.validators, start.slot))] + latest_attestation_targets = [get_latest_attestation_target(store, validator) for validator in active_validators] + def get_vote_count(block): + return len([target for target in latest_attestation_targets if get_ancestor(store, target, block.slot) == block]) + head = start while 1: - c = get_children(head) - if len(c) == 0: - return head - - def get_vote_count(block): - return len([t for t in latest_message_targets if get_ancestor(store, t, block.slot) == block]) - - head = max(c, key=get_vote_count) + children = get_children(head) + if len(children) == 0: + return head + head = max(children, key=get_vote_count) ``` ## Beacon chain state transition function @@ -1187,7 +1186,6 @@ Note: This spec is ~65% complete. **Possible modifications and additions** -* [ ] Replace the IMD fork choice rule with LMD * [ ] Homogenise types to `uint64` ([PR 36](https://github.com/ethereum/eth2.0-specs/pull/36)) * [ ] Reduce the slot duration to 8 seconds * [ ] Allow for the delayed inclusion of aggregated signatures From b76eae8a71d700ee6bd88867ba21790e6a18dfb9 Mon Sep 17 00:00:00 2001 From: Justin Date: Fri, 23 Nov 2018 15:37:37 +0000 Subject: [PATCH 06/20] Update 0_beacon-chain.md --- specs/core/0_beacon-chain.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 7f4522360..820c20edc 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -337,22 +337,23 @@ Beacon block production is significantly different because of the proof of stake ### Beacon chain fork choice rule -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 `v` calculates the beacon chain head as follows. +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 `v` subjectively calculates the beacon chain head as follows. * Let `store` be the set of attestations and blocks that the 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`. * 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 `CYCLE_LENGTH` slots. (A block `B` is justified is 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 in `store` from `validator` with the highest slot number. If several such attestations exist use the one the validator `v` verified first. +* 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 `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 logarithmic time. ```python def lmd_ghost(store, start): - active_validators = [start.state.validators[i] for i in range(get_active_validators(start.state.validators, start.slot))] - latest_attestation_targets = [get_latest_attestation_target(store, validator) for validator in active_validators] + validators = start.state.validators + active_validators = [validators[i] for i in range(get_active_validators(validators, start.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 latest_attestation_targets if get_ancestor(store, target, block.slot) == block]) + return len([target for target in attestation_targets if get_ancestor(store, target, block.slot) == block]) head = start while 1: From ea31ff3cea51830f8693770f69dab8b2ab49b45e Mon Sep 17 00:00:00 2001 From: Justin Date: Fri, 23 Nov 2018 16:05:30 +0000 Subject: [PATCH 07/20] Fix minor bug in `ghost_lmd` --- specs/core/0_beacon-chain.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 820c20edc..ba92b601b 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -350,7 +350,7 @@ The beacon chain fork choice rule is a hybrid that combines justification and fi ```python def lmd_ghost(store, start): validators = start.state.validators - active_validators = [validators[i] for i in range(get_active_validators(validators, start.slot))] + active_validators = [validators[i] for i in range(get_active_validator_indices(validators, start.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]) From 70b181fe320d72c9e681d7bb59fee9490e7a35c9 Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Fri, 23 Nov 2018 12:57:17 -0600 Subject: [PATCH 08/20] Small fixes * pick out committee when getting proposer * avoid unsigned underflow when chain is starting (since we're using uint) --- specs/core/0_beacon-chain.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index d674823a6..034f8123f 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -515,7 +515,7 @@ The following is a function that determines the proposer of a beacon block: ```python def get_beacon_proposer(state:BeaconState, slot: int) -> ValidatorRecord: - first_committee = get_shards_and_committees_for_slot(state, slot)[0] + first_committee = get_shards_and_committees_for_slot(state, slot)[0].committee index = first_committee[slot % len(first_committee)] return state.validators[index] ``` @@ -686,7 +686,7 @@ First, a helper function: ```python def min_empty_validator(validators: List[ValidatorRecord], current_slot: int): for i, v in enumerate(validators): - if v.status == WITHDRAWN and v.exit_slot <= current_slot - DELETION_PERIOD: + if v.status == WITHDRAWN and v.exit_slot + DELETION_PERIOD <= current_slot: return i return None ``` From 6f44db789f97b5cc054c4ae9528a726390096db6 Mon Sep 17 00:00:00 2001 From: vbuterin Date: Fri, 23 Nov 2018 14:57:43 -0500 Subject: [PATCH 09/20] s/=/==/ --- specs/core/0_beacon-chain.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 1a373eb2f..202e71fdc 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -865,7 +865,7 @@ Verify that `BLSVerify(pubkey=get_beacon_proposer(state, block.slot).pubkey, dat ### Process PoW receipt root -If `block.candidate_pow_receipt_root = state.candidate_pow_receipt_root` set `state.candidate_pow_receipt_root_votes += 1`. +If `block.candidate_pow_receipt_root == state.candidate_pow_receipt_root` set `state.candidate_pow_receipt_root_votes += 1`. ### Process penalties, logouts and other special objects From ee40888d2eebd0157ff518a367b98b0a4c8b185b Mon Sep 17 00:00:00 2001 From: vbuterin Date: Fri, 23 Nov 2018 15:01:15 -0500 Subject: [PATCH 10/20] Fix to fixes --- specs/core/0_beacon-chain.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index ba92b601b..ca80daa09 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -350,7 +350,7 @@ The beacon chain fork choice rule is a hybrid that combines justification and fi ```python def lmd_ghost(store, start): validators = start.state.validators - active_validators = [validators[i] for i in range(get_active_validator_indices(validators, start.slot))] + active_validators = [validators[i] for i in get_active_validator_indices(validators, start.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]) From 545e35e4b1bc12439a3a9a4c20c36aa6a2bd8360 Mon Sep 17 00:00:00 2001 From: vbuterin Date: Fri, 23 Nov 2018 15:10:03 -0500 Subject: [PATCH 11/20] Clarified "logarithmic" --- specs/core/0_beacon-chain.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index ca80daa09..72e733eed 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -345,7 +345,7 @@ The beacon chain fork choice rule is a hybrid that combines justification and fi * 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 `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 logarithmic time. +* 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. ```python def lmd_ghost(store, start): From c8e9ff3d87c22e3b0e4dce8f04b6c28fe8957021 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Thu, 22 Nov 2018 20:13:08 +0800 Subject: [PATCH 12/20] `shard_id` -> `shard` patch --- specs/core/0_beacon-chain.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 326bca17b..608abb564 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -160,8 +160,8 @@ A `ProposalSignedData` has the following fields: 'fork_version': 'uint64', # Slot number 'slot': 'uint64', - # Shard ID (or `2**64 - 1` for beacon chain) - 'shard_id': 'uint64', + # Shard number (or `2**64 - 1` for beacon chain) + 'shard': 'uint64', # Block hash 'block_hash': 'hash32', } From 4ba62bb80a38a620670d25e6db27b7eaa509d157 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Sun, 25 Nov 2018 00:22:28 +0800 Subject: [PATCH 13/20] Remove leftover --- specs/core/0_beacon-chain.md | 54 ------------------------------------ 1 file changed, 54 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 608abb564..28fee7b20 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -764,60 +764,6 @@ def exit_validator(index, state, block, penalize, current_slot): add_validator_set_change_record(state, index, validator.pubkey, EXIT) ``` -## On startup - -Run the following code: - -```python -def on_startup(initial_validator_entries: List[Any]) -> BeaconState: - # Induct validators - validators = [] - for pubkey, proof_of_possession, withdrawal_shard, withdrawal_address, \ - randao_commitment in initial_validator_entries: - add_validator( - validators=validators, - pubkey=pubkey, - proof_of_possession=proof_of_possession, - withdrawal_shard=withdrawal_shard, - withdrawal_address=withdrawal_address, - randao_commitment=randao_commitment, - current_slot=0, - status=ACTIVE, - ) - # Setup state - x = get_new_shuffling(bytes([0] * 32), validators, 0) - crosslinks = [ - CrosslinkRecord( - slot=0, - hash=bytes([0] * 32) - ) - for i in range(SHARD_COUNT) - ] - state = BeaconState( - validator_set_change_slot=0, - validators=validators, - crosslinks=crosslinks, - last_state_recalculation_slot=0, - last_finalized_slot=0, - last_justified_slot=0, - justified_streak=0, - shard_and_committee_for_slots=x + x, - persistent_committees=split(shuffle(validators, bytes([0] * 32)), SHARD_COUNT), - persistent_committee_reassignments=[], - deposits_penalized_in_period=[], - next_shuffling_seed=bytes([0] * 32), - validator_set_delta_hash_chain=bytes([0] * 32), # stub - pre_fork_version=INITIAL_FORK_VERSION, - post_fork_version=INITIAL_FORK_VERSION, - fork_slot_number=0, - pending_attestations=[], - recent_block_hashes=[bytes([0] * 32) for _ in range(CYCLE_LENGTH * 2)], - randao_mix=bytes([0] * 32) # stub - ) - - return state -``` - ## Per-block processing This procedure should be carried out every beacon block. From d0f7937089df34934c95ed0b37b0fca72588f354 Mon Sep 17 00:00:00 2001 From: Justin Date: Sat, 24 Nov 2018 19:54:05 +0000 Subject: [PATCH 14/20] Remove TODO See for example https://github.com/ethereum/eth2.0-specs/issues/128 --- specs/core/0_beacon-chain.md | 36 ------------------------------------ 1 file changed, 36 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 28fee7b20..33271cf35 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -1108,42 +1108,6 @@ while len(state.persistent_committee_reassignments) > 0 and state.persistent_com state.persistent_committees[rec.shard].append(rec.validator_index) ``` -### TODO - -Note: This spec is ~65% complete. - -**Missing** - -* [ ] Specify the shard chain blocks, blobs, proposers, etc. -* [ ] Specify the deposit contract on the PoW chain in Vyper -* [ ] Specify the beacon chain genesis rules ([issue 58](https://github.com/ethereum/eth2.0-specs/issues/58)) -* [ ] Specify the logic for proofs of custody, including slashing conditions -* [ ] Specify BLSVerify and rework the spec for BLS12-381 throughout -* [ ] Specify the constraints for `SpecialRecord`s ([issue 43](https://github.com/ethereum/eth2.0-specs/issues/43)) -* [ ] Specify the calculation and validation of `BeaconBlock.state_root` -* [ ] Undergo peer review, security audits and formal verification - -**Documentation** - -* [ ] Specify the various assumptions (global clock, networking latency, validator honesty, validator liveness, etc.) -* [ ] Add an appendix on gossip networks and the offchain signature aggregation logic -* [ ] Add a glossary (in a separate `glossary.md`) to comprehensively and precisely define all the terms -* [ ] Clearly document the various edge cases, e.g. with committee sizing -* [ ] Rework the document for readability - -**Possible modifications and additions** - -* [ ] Homogenise types to `uint64` ([PR 36](https://github.com/ethereum/eth2.0-specs/pull/36)) -* [ ] Reduce the slot duration to 8 seconds -* [ ] Allow for the delayed inclusion of aggregated signatures -* [ ] Introduce a RANDAO slashing condition for early reveals -* [ ] Use a separate hash function for the proof of possession -* [ ] Rework the `ShardAndCommittee` data structures -* [ ] Add a double-batched Merkle accumulator for historical beacon chain blocks -* [ ] Allow for deposits larger than 32 ETH, as well as deposit top-ups -* [ ] Add penalties for deposits below 32 ETH (or some other threshold) -* [ ] Add a `SpecialRecord` to (re)register - # Appendix ## Appendix A - Hash function From 6bdff4e82eca1e6e9ada9e6f4e8a99b14d1e88cb Mon Sep 17 00:00:00 2001 From: vbuterin Date: Sat, 24 Nov 2018 16:04:07 -0500 Subject: [PATCH 15/20] Fix Justin's issues 10 and 12 --- specs/core/0_beacon-chain.md | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 33271cf35..796360404 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -94,7 +94,7 @@ The primary source of load on the beacon chain are "attestations". Attestations ### PoW chain registration contract -The initial deployment phases of Ethereum 2.0 are implemented without consensus changes to the PoW chain. A registration contract is added to the PoW chain to deposit ETH. This contract has a `registration` function which takes as arguments `pubkey`, `withdrawal_shard`, `withdrawal_address`, `randao_commitment` as defined in a `ValidatorRecord` below. A BLS `proof_of_possession` of types `bytes` is given as a final argument. +The initial deployment phases of Ethereum 2.0 are implemented without consensus changes to the PoW chain. A registration contract is added to the PoW chain to deposit ETH. This contract has a `registration` function which takes as arguments `pubkey`, `withdrawal_credentials`, `randao_commitment` as defined in a `ValidatorRecord` below. A BLS `proof_of_possession` of types `bytes` is given as a final argument. The registration contract emits a log with the various arguments for consumption by the beacon chain. It does not do validation, pushing the registration logic to the beacon chain. In particular, the proof of possession (based on the BLS12-381 curve) is not verified by the registration contract. @@ -260,10 +260,8 @@ A `ValidatorRecord` has the following fields: { # BLS public key 'pubkey': 'uint256', - # Withdrawal shard number - 'withdrawal_shard': 'uint16', - # Withdrawal address - 'withdrawal_address': 'address', + # Withdrawal credentials + 'withdrawal_credentials': 'hash32', # RANDAO commitment 'randao_commitment': 'hash32', # Slot the RANDAO commitment was last changed @@ -603,8 +601,7 @@ The contract is at address `DEPOSIT_CONTRACT_ADDRESS`. When a user wishes to bec { 'pubkey': 'int256', 'proof_of_possession': ['int256'], - 'withdrawal_shard': 'int64', - 'withdrawal_address`: 'bytes20', + 'withdrawal_credentials`: 'hash32', 'randao_commitment`: 'hash32' } ``` @@ -638,14 +635,13 @@ A valid block with slot `0` (the "genesis block") has the following values. Othe def on_startup(initial_validator_entries: List[Any], genesis_time: uint64, pow_receipt_root: Hash32) -> BeaconState: # Induct validators validators = [] - for pubkey, proof_of_possession, withdrawal_shard, withdrawal_address, \ + for pubkey, proof_of_possession, withdrawal_credentials, \ randao_commitment in initial_validator_entries: add_validator( validators=validators, pubkey=pubkey, proof_of_possession=proof_of_possession, - withdrawal_shard=withdrawal_shard, - withdrawal_address=withdrawal_address, + withdrawal_credentials=withdrawal_credentials, randao_commitment=randao_commitment, current_slot=0, status=ACTIVE, @@ -712,14 +708,13 @@ Now, to add a validator: def add_validator(validators: List[ValidatorRecord], pubkey: int, proof_of_possession: bytes, - withdrawal_shard: int, - withdrawal_address: Address, + withdrawal_credentials: Hash32, randao_commitment: Hash32, status: int, current_slot: int) -> int: # if following assert fails, validator induction failed # move on to next validator registration log - signed_message = bytes32(pubkey) + bytes2(withdrawal_shard) + withdrawal_address + randao_commitment + signed_message = bytes32(pubkey) + bytes2(withdrawal_shard) + withdrawal_credentials + randao_commitment assert BLSVerify(pub=pubkey, msg=hash(signed_message), sig=proof_of_possession) @@ -727,8 +722,7 @@ def add_validator(validators: List[ValidatorRecord], assert pubkey not in [v.pubkey for v in validators] rec = ValidatorRecord( pubkey=pubkey, - withdrawal_shard=withdrawal_shard, - withdrawal_address=withdrawal_address, + withdrawal_credentials=withdrawal_credentials, randao_commitment=randao_commitment, randao_last_change=current_slot, balance=DEPOSIT_SIZE * GWEI_PER_ETH, @@ -916,7 +910,7 @@ def verify_merkle_branch(leaf: Hash32, branch: [Hash32], depth: int, index: int, Verify that `deposit_data.msg_value == DEPOSIT_SIZE` and `block.slot - (deposit_data.timestamp - state.genesis_time) // SLOT_DURATION < DELETION_PERIOD`. -Run `add_validator(validators, deposit_data.deposit_params.pubkey, deposit_data.deposit_params.proof_of_possession, deposit_data.deposit_params.withdrawal_shard, data.deposit_params.withdrawal_address, deposit_data.deposit_params.randao_commitment, PENDING_ACTIVATION, block.slot)`. +Run `add_validator(validators, deposit_data.deposit_params.pubkey, deposit_data.deposit_params.proof_of_possession, deposit_data.deposit_params.withdrawal_credentials, deposit_data.deposit_params.randao_commitment, PENDING_ACTIVATION, block.slot)`. ## State recalculations (every `CYCLE_LENGTH` slots) @@ -977,7 +971,7 @@ For every shard number `shard` for which a crosslink committee exists in the cyc If `last_state_recalculation_slot % POW_RECEIPT_ROOT_VOTING_PERIOD == 0`, then: -* If `state.candidate_pow_receipt_root_votes * 3 >= POW_RECEIPT_ROOT_VOTING_PERIOD * 2` set `state.processed_pow_receipt_root = state.candidate_pow_receipt_root`. +* If `state.candidate_pow_receipt_root_votes * 2 >= POW_RECEIPT_ROOT_VOTING_PERIOD` set `state.processed_pow_receipt_root = state.candidate_pow_receipt_root`. * Set `state.candidate_pow_receipt_root = block.candidate_pow_receipt_root`. * Set `state.candidate_pow_receipt_root_votes = 0`. From b92889cf8391ba3e0dd5ea63eee780a888dd87d1 Mon Sep 17 00:00:00 2001 From: vbuterin Date: Sat, 24 Nov 2018 16:07:25 -0500 Subject: [PATCH 16/20] Add a minimum deposit time equal to the SHARD_PERSISTENT_COMMITTEE_CHANGE_PERIOD --- specs/core/0_beacon-chain.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 796360404..1efd98d4b 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -270,8 +270,8 @@ A `ValidatorRecord` has the following fields: 'balance': 'uint64', # Status code 'status': 'uint8', - # Slot when validator exited (or 0) - 'exit_slot': 'uint64' + # Slot when validator last changed status (or 0) + 'last_status_change_slot': 'uint64' # Sequence number when validator exited (or 0) 'exit_seq': 'uint64' } @@ -697,7 +697,7 @@ First, a helper function: ```python def min_empty_validator(validators: List[ValidatorRecord], current_slot: int): for i, v in enumerate(validators): - if v.status == WITHDRAWN and v.exit_slot + DELETION_PERIOD <= current_slot: + if v.status == WITHDRAWN and v.last_status_change_slot + DELETION_PERIOD <= current_slot: return i return None ``` @@ -727,7 +727,7 @@ def add_validator(validators: List[ValidatorRecord], randao_last_change=current_slot, balance=DEPOSIT_SIZE * GWEI_PER_ETH, status=status, - exit_slot=0, + last_status_change_slot=current_slot, exit_seq=0 ) index = min_empty_validator(validators) @@ -744,7 +744,7 @@ def add_validator(validators: List[ValidatorRecord], ```python def exit_validator(index, state, block, penalize, current_slot): validator = state.validators[index] - validator.exit_slot = current_slot + validator.last_status_change_slot = current_slot validator.exit_seq = state.current_exit_seq state.current_exit_seq += 1 if penalize: @@ -841,6 +841,7 @@ Perform the following checks: * Let `fork_version = pre_fork_version if block.slot < fork_slot_number else post_fork_version`. Verify that `BLSVerify(pubkey=validators[data.validator_index].pubkey, msg=hash(LOGOUT_MESSAGE + bytes8(fork_version)), sig=data.signature)` * Verify that `validators[validator_index].status == ACTIVE`. +* Verify that `block.slot >= last_status_change_slot + SHARD_PERSISTENT_COMMITTEE_CHANGE_PERIOD` Run `exit_validator(data.validator_index, state, block, penalize=False, current_slot=block.slot)`. @@ -1010,7 +1011,7 @@ def change_validators(validators: List[ValidatorRecord], current_slot: int) -> N ) if validators[i].status == PENDING_EXIT: validators[i].status = PENDING_WITHDRAW - validators[i].exit_slot = current_slot + validators[i].last_status_change_slot = current_slot total_changed += validators[i].balance add_validator_set_change_record( state=state, @@ -1032,14 +1033,14 @@ def change_validators(validators: List[ValidatorRecord], current_slot: int) -> N # calculate their penalties if they were slashed def withdrawable(v): - return v.status in (PENDING_WITHDRAW, PENALIZED) and current_slot >= v.exit_slot + MIN_WITHDRAWAL_PERIOD + return v.status in (PENDING_WITHDRAW, PENALIZED) and current_slot >= v.last_status_change_slot + MIN_WITHDRAWAL_PERIOD withdrawable_validators = sorted(filter(withdrawable, validators), key=lambda v: v.exit_seq) for v in withdrawable_validators[:WITHDRAWALS_PER_CYCLE]: if v.status == PENALIZED: v.balance -= v.balance * min(total_penalties * 3, total_balance) // total_balance v.status = WITHDRAWN - v.exit_slot = current_slot + v.last_status_change_slot = current_slot withdraw_amount = v.balance ... From 4be17dea143c104a4f81940852dd6aed2ff9eb61 Mon Sep 17 00:00:00 2001 From: vbuterin Date: Sat, 24 Nov 2018 16:12:27 -0500 Subject: [PATCH 17/20] Made candidate PoW receipt roots into a map --- specs/core/0_beacon-chain.md | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 1efd98d4b..cc4113aad 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -238,8 +238,7 @@ The `BeaconState` has the following fields: 'genesis_time': 'uint64', # PoW receipt root 'processed_pow_receipt_root': 'hash32', - 'candidate_pow_receipt_root': 'hash32', - 'candidate_pow_receipt_root_votes': 'uint64', + 'candidate_pow_receipt_roots': [CandidateReceiptRootRecord], # Parameters relevant to hard forks / versioning. # Should be updated only by hard forks. 'pre_fork_version': 'uint64', @@ -312,6 +311,15 @@ A `ShardReassignmentRecord` object has the following fields: } ``` +A `CandidateReceiptRootRecord` object contains the following fields: + +```python +{ + 'receipt_root': 'hash32', + 'votes': 'uint64', +} +``` + ## Beacon chain processing The beacon chain is the "main chain" of the PoS system. The beacon chain's main responsibilities are: @@ -620,7 +628,7 @@ A valid block with slot `0` (the "genesis block") has the following values. Othe { 'slot': 0, 'randao_reveal': bytes32(0), - 'candidate_pow_receipt_root': bytes32(0), + 'candidate_pow_receipt_roots': [], 'ancestor_hashes': [bytes32(0) for i in range(32)], 'state_root': STARTUP_STATE_ROOT, 'attestations': [], @@ -672,8 +680,7 @@ def on_startup(initial_validator_entries: List[Any], genesis_time: uint64, pow_r current_exit_seq=0, genesis_time=genesis_time, processed_pow_receipt_root=pow_receipt_root, - candidate_pow_receipt_root=bytes([0] * 32), - candidate_pow_receipt_root_votes=0, + candidate_pow_receipt_roots=[], pre_fork_version=INITIAL_FORK_VERSION, post_fork_version=INITIAL_FORK_VERSION, fork_slot_number=0, @@ -821,7 +828,7 @@ Verify that `BLSVerify(pubkey=get_beacon_proposer(state, block.slot).pubkey, dat ### Process PoW receipt root -If `block.candidate_pow_receipt_root == state.candidate_pow_receipt_root` set `state.candidate_pow_receipt_root_votes += 1`. +If `block.candidate_pow_receipt_root` is `x.receipt_root` for some `x` in `state.candidate_pow_receipt_roots`, set `x.votes += 1`. Otherwise, append to `state.candidate_pow_receipt_roots` a new `CandidateReceiptRootRecord(receipt_root=block.candidate_pow_receipt_root, votes=1)`. ### Process penalties, logouts and other special objects @@ -972,9 +979,8 @@ For every shard number `shard` for which a crosslink committee exists in the cyc If `last_state_recalculation_slot % POW_RECEIPT_ROOT_VOTING_PERIOD == 0`, then: -* If `state.candidate_pow_receipt_root_votes * 2 >= POW_RECEIPT_ROOT_VOTING_PERIOD` set `state.processed_pow_receipt_root = state.candidate_pow_receipt_root`. -* Set `state.candidate_pow_receipt_root = block.candidate_pow_receipt_root`. -* Set `state.candidate_pow_receipt_root_votes = 0`. +* If for any `x` in `state.candidate_pow_receipt_root`, `x.votes * 2 >= POW_RECEIPT_ROOT_VOTING_PERIOD` set `state.processed_pow_receipt_root = x.receipt_root`. +* Set `state.candidate_pow_receipt_roots = []`. ### Validator set change From 40d289f1a3004caafdecabeb4e9faa74b40c435f Mon Sep 17 00:00:00 2001 From: vbuterin Date: Sat, 24 Nov 2018 16:16:27 -0500 Subject: [PATCH 18/20] Resolved Justin's #13 --- specs/core/0_beacon-chain.md | 1 + 1 file changed, 1 insertion(+) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 1efd98d4b..1600519f5 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -803,6 +803,7 @@ For each `AttestationRecord` object: * Derive a group public key by adding the public keys of all of the attesters in `attestation_indices` for whom the corresponding bit in `attester_bitfield` (the ith bit is `(attester_bitfield[i // 8] >> (7 - (i %8))) % 2`) equals 1. * Let `fork_version = pre_fork_version if slot < fork_slot_number else post_fork_version`. * Verify that `aggregate_sig` verifies using the group pubkey generated and the serialized form of `AttestationSignedData(fork_version, slot, shard, parent_hashes, shard_block_hash, last_crosslinked_hash, shard_block_combined_data_root, justified_slot)` as the message. +* [TO BE REMOVED IN PHASE 1] Verify that `shard_block_hash == bytes([0] * 32)` Extend the list of `AttestationRecord` objects in the `state` with those included in the block, ordering the new additions in the same order as they came in the block. From 5ba47b476a0ded767cc8b23d263c881c06986614 Mon Sep 17 00:00:00 2001 From: vbuterin Date: Sat, 24 Nov 2018 16:20:20 -0500 Subject: [PATCH 19/20] Shard uint16 -> uint64 --- specs/core/0_beacon-chain.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 1600519f5..791faf84d 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -132,7 +132,7 @@ An `AttestationRecord` has the following fields: # Slot number 'slot': 'uint64', # Shard number - 'shard': 'uint16', + 'shard': 'uint64', # Beacon block hashes not part of the current chain, oldest to newest 'oblique_parent_hashes': ['hash32'], # Shard block hash being attested to @@ -176,7 +176,7 @@ An `AttestationSignedData` has the following fields: # Slot number 'slot': 'uint64', # Shard number - 'shard': 'uint16', + 'shard': 'uint64', # CYCLE_LENGTH parent hashes 'parent_hashes': ['hash32'], # Shard block hash @@ -293,7 +293,7 @@ A `ShardAndCommittee` object has the following fields: ```python { # Shard number - 'shard': 'uint16', + 'shard': 'uint64', # Validator indices 'committee': ['uint24'] } @@ -306,7 +306,7 @@ A `ShardReassignmentRecord` object has the following fields: # Which validator to reassign 'validator_index': 'uint24', # To which shard - 'shard': 'uint16', + 'shard': 'uint64', # When 'slot': 'uint64' } From 755eadb20a3645368c15c369d5d8ce8c15234cfa Mon Sep 17 00:00:00 2001 From: Justin Date: Sat, 24 Nov 2018 21:41:53 +0000 Subject: [PATCH 20/20] Fixes to PoW receipt roots --- specs/core/0_beacon-chain.md | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index cc4113aad..5d0c31309 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -238,7 +238,7 @@ The `BeaconState` has the following fields: 'genesis_time': 'uint64', # PoW receipt root 'processed_pow_receipt_root': 'hash32', - 'candidate_pow_receipt_roots': [CandidateReceiptRootRecord], + 'candidate_pow_receipt_roots': [CandidatePoWReceiptRootRecord], # Parameters relevant to hard forks / versioning. # Should be updated only by hard forks. 'pre_fork_version': 'uint64', @@ -311,12 +311,14 @@ A `ShardReassignmentRecord` object has the following fields: } ``` -A `CandidateReceiptRootRecord` object contains the following fields: +A `CandidatePoWReceiptRootRecord` object contains the following fields: ```python { - 'receipt_root': 'hash32', - 'votes': 'uint64', + # Candidate PoW receipt root + 'candidate_pow_receipt_root': 'hash32', + # Vote count + 'votes': 'uint64' } ``` @@ -618,7 +620,7 @@ If the user wishes to deposit more than `DEPOSIT_SIZE` ETH, they would need to m * `initial_validator_entries` equal to the list of data records published as HashChainValue logs so far, in the order in which they were published (oldest to newest). * `genesis_time` equal to the `time` value published in the log -* `pow_receipt_root` equal to the `receipt_root` value published in the log +* `processed_pow_receipt_root` equal to the `receipt_root` value published in the log ### On startup @@ -640,7 +642,7 @@ A valid block with slot `0` (the "genesis block") has the following values. Othe `STARTUP_STATE_ROOT` is the root of the initial state, computed by running the following code: ```python -def on_startup(initial_validator_entries: List[Any], genesis_time: uint64, pow_receipt_root: Hash32) -> BeaconState: +def on_startup(initial_validator_entries: List[Any], genesis_time: uint64, processed_pow_receipt_root: Hash32) -> BeaconState: # Induct validators validators = [] for pubkey, proof_of_possession, withdrawal_credentials, \ @@ -679,7 +681,7 @@ def on_startup(initial_validator_entries: List[Any], genesis_time: uint64, pow_r validator_set_delta_hash_chain=bytes([0] * 32), # stub current_exit_seq=0, genesis_time=genesis_time, - processed_pow_receipt_root=pow_receipt_root, + processed_pow_receipt_root=processed_pow_receipt_root, candidate_pow_receipt_roots=[], pre_fork_version=INITIAL_FORK_VERSION, post_fork_version=INITIAL_FORK_VERSION, @@ -828,7 +830,7 @@ Verify that `BLSVerify(pubkey=get_beacon_proposer(state, block.slot).pubkey, dat ### Process PoW receipt root -If `block.candidate_pow_receipt_root` is `x.receipt_root` for some `x` in `state.candidate_pow_receipt_roots`, set `x.votes += 1`. Otherwise, append to `state.candidate_pow_receipt_roots` a new `CandidateReceiptRootRecord(receipt_root=block.candidate_pow_receipt_root, votes=1)`. +If `block.candidate_pow_receipt_root` is `x.candidate_pow_receipt_root` for some `x` in `state.candidate_pow_receipt_roots`, set `x.votes += 1`. Otherwise, append to `state.candidate_pow_receipt_roots` a new `CandidatePoWReceiptRootRecord(candidate_pow_receipt_root=block.candidate_pow_receipt_root, votes=1)`. ### Process penalties, logouts and other special objects