Skip to content

Commit

Permalink
Make Constants Explicit and Minor Cleanups (#2898)
Browse files Browse the repository at this point in the history
* Rename outdated configs, make constants explicitly delcared

* Remove activate_validator, not needed

* Remove GenesisSlot and GenesisEpoch

* Remove unused import
  • Loading branch information
0xKiwi authored Jul 2, 2019
1 parent 24f630d commit f612b81
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 143 deletions.
2 changes: 1 addition & 1 deletion beacon-chain/core/blocks/block_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,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
36 changes: 2 additions & 34 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 @@ -172,7 +141,6 @@ func ExitValidator(state *pb.BeaconState, idx uint64) *pb.BeaconState {
// # Apply proposer and whistleblower rewards
// proposer_index = get_beacon_proposer_index(state)
// if whistleblower_index is None:
// whistleblower_index = proposer_index
// whistleblower_reward = Gwei(validator.effective_balance // WHISTLEBLOWER_REWARD_QUOTIENT)
// proposer_reward = Gwei(whistleblower_reward // PROPOSER_REWARD_QUOTIENT)
// increase_balance(state, proposer_index, proposer_reward)
Expand All @@ -196,7 +164,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

0 comments on commit f612b81

Please sign in to comment.