Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Constants Explicit and Minor Cleanups #2898

Merged
merged 5 commits into from
Jul 2, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions beacon-chain/core/blocks/block_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ func ConvertToIndexed(state *pb.BeaconState, attestation *pb.Attestation) (*pb.I
// if len(custody_bit_1_indices) > 0: # [TO BE REMOVED IN PHASE 1]
// return False
//
// if not (1 <= len(custody_bit_0_indices) + len(custody_bit_1_indices) <= MAX_INDICES_PER_ATTESTATION):
// if not (1 <= len(custody_bit_0_indices) + len(custody_bit_1_indices) <= MAX_VALIDATORS_PER_COMMITTEE):
// return False
//
// return bls_verify_multiple(
Expand All @@ -634,7 +634,7 @@ func VerifyIndexedAttestation(indexedAtt *pb.IndexedAttestation, verifySignature
return fmt.Errorf("expected no bit 1 indices, received %v", len(custodyBit1Indices))
}

maxIndices := params.BeaconConfig().MaxIndicesPerAttestation
maxIndices := params.BeaconConfig().MaxValidatorsPerCommittee
totalIndicesLength := uint64(len(custodyBit0Indices) + len(custodyBit1Indices))
if maxIndices < totalIndicesLength || totalIndicesLength < 1 {
return fmt.Errorf("over max number of allowed indices per attestation: %d", totalIndicesLength)
Expand Down
8 changes: 4 additions & 4 deletions beacon-chain/core/blocks/block_operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ func TestProcessAttesterSlashings_IndexedAttestationFailedToVerify(t *testing.T)
Shard: 4,
},
},
CustodyBit_0Indices: make([]uint64, params.BeaconConfig().MaxIndicesPerAttestation+1),
CustodyBit_0Indices: make([]uint64, params.BeaconConfig().MaxValidatorsPerCommittee+1),
},
Attestation_2: &pb.IndexedAttestation{
Data: &pb.AttestationData{
Expand All @@ -742,7 +742,7 @@ func TestProcessAttesterSlashings_IndexedAttestationFailedToVerify(t *testing.T)
Shard: 4,
},
},
CustodyBit_0Indices: make([]uint64, params.BeaconConfig().MaxIndicesPerAttestation+1),
CustodyBit_0Indices: make([]uint64, params.BeaconConfig().MaxValidatorsPerCommittee+1),
},
},
}
Expand Down Expand Up @@ -1254,11 +1254,11 @@ func TestConvertToIndexed_OK(t *testing.T) {

func TestValidateIndexedAttestation_AboveMaxLength(t *testing.T) {
indexedAtt1 := &pb.IndexedAttestation{
CustodyBit_0Indices: make([]uint64, params.BeaconConfig().MaxIndicesPerAttestation+5),
CustodyBit_0Indices: make([]uint64, params.BeaconConfig().MaxValidatorsPerCommittee+5),
CustodyBit_1Indices: []uint64{},
}

for i := uint64(0); i < params.BeaconConfig().MaxIndicesPerAttestation+5; i++ {
for i := uint64(0); i < params.BeaconConfig().MaxValidatorsPerCommittee+5; i++ {
indexedAtt1.CustodyBit_0Indices[i] = i
}

Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/core/epoch/epoch_processing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,7 @@ func TestProcessRegistryUpdates_ActivationCompletes(t *testing.T) {
func TestProcessRegistryUpdates_CanExits(t *testing.T) {
epoch := uint64(5)
exitEpoch := helpers.DelayedActivationExitEpoch(epoch)
minWithdrawalDelay := params.BeaconConfig().MinValidatorWithdrawalDelay
minWithdrawalDelay := params.BeaconConfig().MinValidatorWithdrawabilityDelay
state := &pb.BeaconState{
Slot: epoch * params.BeaconConfig().SlotsPerEpoch,
Validators: []*pb.Validator{
Expand Down
1 change: 1 addition & 0 deletions beacon-chain/core/helpers/committee.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package helpers
import (
"errors"
"fmt"

"github.com/prysmaticlabs/prysm/beacon-chain/cache"
"github.com/prysmaticlabs/prysm/beacon-chain/utils"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
Expand Down
1 change: 0 additions & 1 deletion beacon-chain/core/state/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ go_library(
"//beacon-chain/core/blocks:go_default_library",
"//beacon-chain/core/epoch:go_default_library",
"//beacon-chain/core/helpers:go_default_library",
"//beacon-chain/core/validators:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//shared/blockutil:go_default_library",
"//shared/bytesutil:go_default_library",
Expand Down
7 changes: 2 additions & 5 deletions beacon-chain/core/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/params"
Expand Down Expand Up @@ -141,10 +140,8 @@ func GenesisBeaconState(deposits []*pb.Deposit, genesisTime uint64, eth1Data *pb
for i := 0; i < len(state.Validators); i++ {
if state.Validators[i].EffectiveBalance >=
params.BeaconConfig().MaxEffectiveBalance {
state, err = v.ActivateValidator(state, uint64(i), true)
if err != nil {
return nil, fmt.Errorf("could not activate validator: %v", err)
}
state.Validators[i].ActivationEligibilityEpoch = 0
state.Validators[i].ActivationEpoch = 0
}
}
activeValidators, err := helpers.ActiveValidatorIndices(state, 0)
Expand Down
43 changes: 6 additions & 37 deletions beacon-chain/core/validators/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,6 @@ var VStore = validatorStore{
exitedValidators: make(map[uint64][]uint64),
}

// ActivateValidator takes in validator index and updates
// validator's activation slot.
//
// Spec pseudocode definition:
// def activate_validator(state: BeaconState, index: ValidatorIndex, is_genesis: bool) -> None:
// """
// Activate the validator of the given ``index``.
// Note that this function mutates ``state``.
// """
// validator = state.validator_registry[index]
//
// validator.activation_epoch = GENESIS_EPOCH if is_genesis else get_entry_exit_effect_epoch(get_current_epoch(state))
func ActivateValidator(state *pb.BeaconState, idx uint64, genesis bool) (*pb.BeaconState, error) {
validator := state.Validators[idx]
if genesis {
validator.ActivationEligibilityEpoch = 0
validator.ActivationEpoch = 0
} else {
validator.ActivationEpoch = helpers.DelayedActivationExitEpoch(helpers.CurrentEpoch(state))
}

state.Validators[idx] = validator

log.WithFields(logrus.Fields{
"index": idx,
"activationEpoch": validator.ActivationEpoch,
}).Info("Validator activated")

return state, nil
}

// InitiateValidatorExit takes in validator index and updates
// validator with correct voluntary exit parameters.
//
Expand Down Expand Up @@ -121,7 +90,7 @@ func InitiateValidatorExit(state *pb.BeaconState, idx uint64) (*pb.BeaconState,
exitQueueEpoch++
}
state.Validators[idx].ExitEpoch = exitQueueEpoch
state.Validators[idx].WithdrawableEpoch = exitQueueEpoch + params.BeaconConfig().MinValidatorWithdrawalDelay
state.Validators[idx].WithdrawableEpoch = exitQueueEpoch + params.BeaconConfig().MinValidatorWithdrawabilityDelay
return state, nil
}

Expand Down Expand Up @@ -170,11 +139,11 @@ func ExitValidator(state *pb.BeaconState, idx uint64) *pb.BeaconState {
// proposer_index = get_beacon_proposer_index(state)
// if whistleblower_index is None:
// whistleblower_index = proposer_index
// whistleblowing_reward = slashed_balance // WHISTLEBLOWING_REWARD_QUOTIENT
// proposer_reward = whistleblowing_reward // PROPOSER_REWARD_QUOTIENT
// whistleblower_reward = slashed_balance // WHISTLEBLOWER_REWARD_QUOTIENT
// proposer_reward = whistleblower_reward // PROPOSER_REWARD_QUOTIENT
// increase_balance(state, proposer_index, proposer_reward)
// increase_balance(state, whistleblower_index, whistleblowing_reward - proposer_reward)
// decrease_balance(state, slashed_index, whistleblowing_reward)
// increase_balance(state, whistleblower_index, whistleblower_reward - proposer_reward)
// decrease_balance(state, slashed_index, whistleblower_reward)
func SlashValidator(state *pb.BeaconState, slashedIdx uint64, whistleBlowerIdx uint64) (*pb.BeaconState, error) {
state, err := InitiateValidatorExit(state, slashedIdx)
if err != nil {
Expand All @@ -194,7 +163,7 @@ func SlashValidator(state *pb.BeaconState, slashedIdx uint64, whistleBlowerIdx u
if whistleBlowerIdx == 0 {
whistleBlowerIdx = proposerIdx
}
whistleblowerReward := slashedBalance / params.BeaconConfig().WhistleBlowingRewardQuotient
whistleblowerReward := slashedBalance / params.BeaconConfig().WhistleblowerRewardQuotient
proposerReward := whistleblowerReward / params.BeaconConfig().ProposerRewardQuotient
state = helpers.IncreaseBalance(state, proposerIdx, proposerReward)
state = helpers.IncreaseBalance(state, whistleBlowerIdx, whistleblowerReward-proposerReward)
Expand Down
42 changes: 1 addition & 41 deletions beacon-chain/core/validators/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,46 +46,6 @@ func TestHasVoted_OK(t *testing.T) {
}
}

func TestActivateValidatorGenesis_OK(t *testing.T) {
state := &pb.BeaconState{
Validators: []*pb.Validator{
{Pubkey: []byte{'A'}},
},
}
newState, err := ActivateValidator(state, 0, true)
if err != nil {
t.Fatalf("could not execute activateValidator:%v", err)
}
if newState.Validators[0].ActivationEpoch != 0 {
t.Errorf("Wanted activation epoch = genesis epoch, got %d",
newState.Validators[0].ActivationEpoch)
}
if newState.Validators[0].ActivationEligibilityEpoch != 0 {
t.Errorf("Wanted activation eligibility epoch = genesis epoch, got %d",
newState.Validators[0].ActivationEligibilityEpoch)
}
}

func TestActivateValidator_OK(t *testing.T) {
state := &pb.BeaconState{
Slot: 100, // epoch 2
Validators: []*pb.Validator{
{Pubkey: []byte{'A'}},
},
}
newState, err := ActivateValidator(state, 0, false)
if err != nil {
t.Fatalf("could not execute activateValidator:%v", err)
}
currentEpoch := helpers.CurrentEpoch(state)
wantedEpoch := helpers.DelayedActivationExitEpoch(currentEpoch)
if newState.Validators[0].ActivationEpoch != wantedEpoch {
t.Errorf("Wanted activation slot = %d, got %d",
wantedEpoch,
newState.Validators[0].ActivationEpoch)
}
}

func TestInitiateValidatorExit_AlreadyExited(t *testing.T) {
exitEpoch := uint64(199)
state := &pb.BeaconState{Validators: []*pb.Validator{{
Expand Down Expand Up @@ -228,7 +188,7 @@ func TestSlashValidator_OK(t *testing.T) {
t.Errorf("Could not get proposer %v", err)
}

whistleblowerReward := slashedBalance / params.BeaconConfig().WhistleBlowingRewardQuotient
whistleblowerReward := slashedBalance / params.BeaconConfig().WhistleblowerRewardQuotient
proposerReward := whistleblowerReward / params.BeaconConfig().ProposerRewardQuotient

if state.Balances[proposer] != params.BeaconConfig().MaxEffectiveBalance+proposerReward {
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/rpc/validator_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ func TestValidatorStatus_InitiatedExit(t *testing.T) {
slot := uint64(10000)
epoch := helpers.SlotToEpoch(slot)
exitEpoch := helpers.DelayedActivationExitEpoch(epoch)
withdrawableEpoch := exitEpoch + params.BeaconConfig().MinValidatorWithdrawalDelay
withdrawableEpoch := exitEpoch + params.BeaconConfig().MinValidatorWithdrawabilityDelay
if err := db.SaveState(ctx, &pbp2p.BeaconState{
Slot: slot,
Validators: []*pbp2p.Validator{{
Expand Down
Loading