From edf335ebf0ef4182a06ee5ddce59f9c54c0ff11f Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Fri, 14 Dec 2018 15:13:50 +0800 Subject: [PATCH 1/3] Fix on startup functions --- specs/core/0_beacon-chain.md | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 3c2259559..33eb92628 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -1111,8 +1111,8 @@ A valid block with slot `INITIAL_SLOT_NUMBER` (a "genesis block") has the follow state_root=STARTUP_STATE_ROOT, randao_reveal=ZERO_HASH, candidate_pow_receipt_root=ZERO_HASH, - proposer_signature=EMPTY_SIGNATURE, - 'body': BeaconBlockBody( + signature=EMPTY_SIGNATURE, + body=BeaconBlockBody( proposer_slashings=[], casper_slashings=[], attestations=[], @@ -1125,7 +1125,8 @@ A valid block with slot `INITIAL_SLOT_NUMBER` (a "genesis block") has the follow `STARTUP_STATE_ROOT` (in the above "genesis block") is generated from the `get_initial_beacon_state` function below. When enough full deposits have been made to the deposit contract and the `ChainStart` log has been emitted, `get_initial_beacon_state` will execute to compute the `hash_tree_root` of `BeaconState`. ```python -def get_initial_beacon_state(initial_validator_deposits: List[Deposit], +def get_initial_beacon_state(initial_validator_registry: List[ValidatorRecord], + initial_validator_deposits: List[Deposit], genesis_time: int, processed_pow_receipt_root: Hash32) -> BeaconState: state = BeaconState( @@ -1179,8 +1180,8 @@ def get_initial_beacon_state(initial_validator_deposits: List[Deposit], withdrawal_credentials=deposit.deposit_data.deposit_input.withdrawal_credentials, randao_commitment=deposit.deposit_data.deposit_input.randao_commitment ) - if state.validator_registry[index].balance >= MAX_DEPOSIT * GWEI_PER_ETH: - update_validator_status(state, index, ACTIVE) + if state.validator_registry[validator_index].balance >= MAX_DEPOSIT * GWEI_PER_ETH: + update_validator_status(state, validator_index, ACTIVE) # set initial committee shuffling initial_shuffling = get_new_shuffling(ZERO_HASH, initial_validator_registry, 0) @@ -1218,7 +1219,7 @@ def process_deposit(state: BeaconState, Process a deposit from Ethereum 1.0. Note that this function mutates ``state``. """ - proof_of_possession_data = DepositInput( + deposit_input = DepositInput( pubkey=pubkey, withdrawal_credentials=withdrawal_credentials, randao_commitment=randao_commitment, @@ -1227,7 +1228,7 @@ def process_deposit(state: BeaconState, assert bls_verify( pubkey=pubkey, - message=hash_tree_root(proof_of_possession_data), + message=hash_tree_root(deposit_input), signature=proof_of_possession, domain=get_domain( state.fork_data, @@ -1250,10 +1251,10 @@ def process_deposit(state: BeaconState, exit_count=0 ) - index = min_empty_validator_index(validators_copy) + index = min_empty_validator_index(state.validator_registry, state.slot) if index is None: state.validator_registry.append(validator) - index = len(validators_copy) - 1 + index = len(state.validator_registry) - 1 else: state.validator_registry[index] = validator else: @@ -1303,7 +1304,7 @@ def activate_validator(state: BeaconState, validator.status = ACTIVE validator.latest_status_change_slot = state.slot state.validator_registry_delta_chain_tip = get_new_validator_registry_delta_chain_tip( - validator_registry_delta_chain_tip=state.validator_registry_delta_chain_tip, + current_validator_registry_delta_chain_tip=state.validator_registry_delta_chain_tip, validator_index=index, pubkey=validator.pubkey, flag=ACTIVATION, @@ -1351,14 +1352,14 @@ def exit_validator(state: BeaconState, whistleblower.balance += whistleblower_reward validator.balance -= whistleblower_reward - if prev_status == EXITED_WITHOUT_PENALTY + if prev_status == EXITED_WITHOUT_PENALTY: return # The following updates only occur if not previous exited state.validator_registry_exit_count += 1 validator.exit_count = state.validator_registry_exit_count state.validator_registry_delta_chain_tip = get_new_validator_registry_delta_chain_tip( - validator_registry_delta_chain_tip=state.validator_registry_delta_chain_tip, + current_validator_registry_delta_chain_tip=state.validator_registry_delta_chain_tip, validator_index=index, pubkey=validator.pubkey, flag=EXIT From 49f3746dc79208c23bd46da017b24894efaa32fb Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Tue, 18 Dec 2018 18:58:59 +0800 Subject: [PATCH 2/3] fix --- 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 33eb92628..700da0490 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -982,7 +982,7 @@ def get_new_validator_registry_delta_chain_tip(current_validator_registry_delta_ """ return hash_tree_root( ValidatorRegistryDeltaBlock( - validator_registry_delta_chain_tip=current_validator_registry_delta_chain_tip, + latest_registry_delta_root=current_validator_registry_delta_chain_tip, validator_index=validator_index, pubkey=pubkey, flag=flag, From bb1559a86f2b75f90e32fdcc205cd7c0b394d4a2 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Wed, 19 Dec 2018 01:18:04 +0800 Subject: [PATCH 3/3] PR feedback --- specs/core/0_beacon-chain.md | 54 +++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 700da0490..329deac90 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -1125,8 +1125,7 @@ A valid block with slot `INITIAL_SLOT_NUMBER` (a "genesis block") has the follow `STARTUP_STATE_ROOT` (in the above "genesis block") is generated from the `get_initial_beacon_state` function below. When enough full deposits have been made to the deposit contract and the `ChainStart` log has been emitted, `get_initial_beacon_state` will execute to compute the `hash_tree_root` of `BeaconState`. ```python -def get_initial_beacon_state(initial_validator_registry: List[ValidatorRecord], - initial_validator_deposits: List[Deposit], +def get_initial_beacon_state(initial_validator_deposits: List[Deposit], genesis_time: int, processed_pow_receipt_root: Hash32) -> BeaconState: state = BeaconState( @@ -1184,7 +1183,7 @@ def get_initial_beacon_state(initial_validator_registry: List[ValidatorRecord], update_validator_status(state, validator_index, ACTIVE) # set initial committee shuffling - initial_shuffling = get_new_shuffling(ZERO_HASH, initial_validator_registry, 0) + initial_shuffling = get_new_shuffling(ZERO_HASH, state.validator_registry, 0) state.shard_committees_at_slots = initial_shuffling + initial_shuffling # set initial persistent shuffling @@ -1196,7 +1195,7 @@ def get_initial_beacon_state(initial_validator_registry: List[ValidatorRecord], ### Routine for processing deposits -First, a helper function: +First, two helper functions: ```python def min_empty_validator_index(validators: List[ValidatorRecord], current_slot: int) -> int: @@ -1206,6 +1205,31 @@ def min_empty_validator_index(validators: List[ValidatorRecord], current_slot: i return None ``` +```python +def validate_proof_of_possession(state: BeaconState, + pubkey: int, + proof_of_possession: bytes, + withdrawal_credentials: Hash32, + randao_commitment: Hash32) -> bool: + proof_of_possession_data = DepositInput( + pubkey=pubkey, + withdrawal_credentials=withdrawal_credentials, + randao_commitment=randao_commitment, + proof_of_possession=EMPTY_SIGNATURE, + ) + + return bls_verify( + pubkey=pubkey, + message=hash_tree_root(proof_of_possession_data), + signature=proof_of_possession, + domain=get_domain( + state.fork_data, + state.slot, + DOMAIN_DEPOSIT, + ) + ) +``` + Now, to add a [validator](#dfn-validator) or top up an existing [validator](#dfn-validator)'s balance by some `deposit` amount: ```python @@ -1219,23 +1243,15 @@ def process_deposit(state: BeaconState, Process a deposit from Ethereum 1.0. Note that this function mutates ``state``. """ - deposit_input = DepositInput( - pubkey=pubkey, - withdrawal_credentials=withdrawal_credentials, - randao_commitment=randao_commitment, - proof_of_possession=EMPTY_SIGNATURE, + # Validate the given `proof_of_possession` + assert validate_proof_of_possession( + state, + pubkey, + proof_of_possession, + withdrawal_credentials, + randao_commitment, ) - assert bls_verify( - pubkey=pubkey, - message=hash_tree_root(deposit_input), - signature=proof_of_possession, - domain=get_domain( - state.fork_data, - state.slot, - DOMAIN_DEPOSIT - ) - ) validator_pubkeys = [v.pubkey for v in state.validator_registry] if pubkey not in validator_pubkeys: