From f72de67f63112a41aa6f3bc3c37877fc42ce4da5 Mon Sep 17 00:00:00 2001 From: Dustin Brody Date: Tue, 10 Sep 2019 15:29:46 +0000 Subject: [PATCH] Proposer slashing is 0.8.3 (#425) * minimal refactor of proposer slashings to 0.8.3; no semantic difference * actually mark spec --- beacon_chain/spec/state_transition_block.nim | 75 +++++++++++--------- 1 file changed, 42 insertions(+), 33 deletions(-) diff --git a/beacon_chain/spec/state_transition_block.nim b/beacon_chain/spec/state_transition_block.nim index 9da912aa4..c369e4c43 100644 --- a/beacon_chain/spec/state_transition_block.nim +++ b/beacon_chain/spec/state_transition_block.nim @@ -139,7 +139,46 @@ func is_slashable_validator(validator: Validator, epoch: Epoch): bool = (validator.activation_epoch <= epoch) and (epoch < validator.withdrawable_epoch) -# https://github.com/ethereum/eth2.0-specs/blob/v0.6.3/specs/core/0_beacon-chain.md#proposer-slashings +# https://github.com/ethereum/eth2.0-specs/blob/v0.8.3/specs/core/0_beacon-chain.md#proposer-slashings +proc process_proposer_slashing( + state: var BeaconState, proposer_slashing: ProposerSlashing, + flags: UpdateFlags, stateCache: var StateCache): bool = + let proposer = state.validators[proposer_slashing.proposer_index.int] + + # Verify that the epoch is the same + if not (compute_epoch_of_slot(proposer_slashing.header_1.slot) == + compute_epoch_of_slot(proposer_slashing.header_2.slot)): + notice "PropSlash: epoch mismatch" + return false + + # But the headers are different + if not (proposer_slashing.header_1 != proposer_slashing.header_2): + notice "PropSlash: headers not different" + return false + + # Check proposer is slashable + if not is_slashable_validator(proposer, get_current_epoch(state)): + notice "PropSlash: slashed proposer" + return false + + # Signatures are valid + if skipValidation notin flags: + for i, header in @[proposer_slashing.header_1, proposer_slashing.header_2]: + if not bls_verify( + proposer.pubkey, + signing_root(header).data, + header.signature, + get_domain( + state, DOMAIN_BEACON_PROPOSER, compute_epoch_of_slot(header.slot))): + notice "PropSlash: invalid signature", + signature_index = i + return false + + slashValidator( + state, proposer_slashing.proposer_index.ValidatorIndex, stateCache) + + true + proc processProposerSlashings( state: var BeaconState, blck: BeaconBlock, flags: UpdateFlags, stateCache: var StateCache): bool = @@ -149,40 +188,10 @@ proc processProposerSlashings( return false for proposer_slashing in blck.body.proposer_slashings: - let proposer = state.validators[proposer_slashing.proposer_index.int] - - # Verify that the epoch is the same - if not (compute_epoch_of_slot(proposer_slashing.header_1.slot) == - compute_epoch_of_slot(proposer_slashing.header_2.slot)): - notice "PropSlash: epoch mismatch" + if not process_proposer_slashing( + state, proposer_slashing, flags, stateCache): return false - # But the headers are different - if not (proposer_slashing.header_1 != proposer_slashing.header_2): - notice "PropSlash: headers not different" - return false - - # Check proposer is slashable - if not is_slashable_validator(proposer, get_current_epoch(state)): - notice "PropSlash: slashed proposer" - return false - - # Signatures are valid - if skipValidation notin flags: - for i, header in @[proposer_slashing.header_1, proposer_slashing.header_2]: - if not bls_verify( - proposer.pubkey, - signing_root(header).data, - header.signature, - get_domain( - state, DOMAIN_BEACON_PROPOSER, compute_epoch_of_slot(header.slot))): - notice "PropSlash: invalid signature", - signature_index = i - return false - - slashValidator( - state, proposer_slashing.proposer_index.ValidatorIndex, stateCache) - true # https://github.com/ethereum/eth2.0-specs/blob/v0.8.3/specs/core/0_beacon-chain.md#is_slashable_attestation_data