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

Clean Up BeaconBlock's Validator Fields #1037

Merged
merged 2 commits into from
Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion beacon-chain/blockchain/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ func (c *ChainService) calculateNewBlockVotes(block *types.Block, beaconState *t
}
if !attesterExists {
blockVoteCache[h].VoterIndices = append(blockVoteCache[h].VoterIndices, attesterIndex)
blockVoteCache[h].VoteTotalDeposit += beaconState.Validators()[attesterIndex].Balance
blockVoteCache[h].VoteTotalDeposit += beaconState.ValidatorRegistry()[attesterIndex].Balance
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions beacon-chain/chaintest/backend/chain_test_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type TestBlock struct {

// TestAttestation --
type TestAttestation struct {
Block string `yaml:"block"`
Validators string `yaml:"validators"`
CommitteeSlot uint64 `yaml:"committee_slot"`
Block string `yaml:"block"`
ValidatorRegistry string `yaml:"validators"`
CommitteeSlot uint64 `yaml:"committee_slot"`
}
8 changes: 4 additions & 4 deletions beacon-chain/core/incentives/incentives.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TallyVoteBalances(

blockVoteBalance := blockVote.VoteTotalDeposit
voterIndices := blockVote.VoterIndices
newValidators := CalculateRewards(
newValidatorRegistry := CalculateRewards(
voterIndices,
activeValidatorIndices,
validators,
Expand All @@ -39,7 +39,7 @@ func TallyVoteBalances(
timeSinceFinality,
)

return blockVoteBalance, newValidators
return blockVoteBalance, newValidatorRegistry
}

// CalculateRewards adjusts validators balances by applying rewards or penalties
Expand All @@ -55,7 +55,7 @@ func CalculateRewards(
timeSinceFinality uint64,
) []*pb.ValidatorRecord {

newValidatorSet := v.CopyValidators(validators)
newValidatorSet := v.CopyValidatorRegistry(validators)

// Calculate the reward and penalty quotients for the validator set.
rewardQuotient := RewardQuotient(totalActiveValidatorDeposit)
Expand Down Expand Up @@ -117,7 +117,7 @@ func ApplyCrosslinkRewardsAndPenalties(
totalBalance uint64,
voteBalance uint64,
) ([]*pb.ValidatorRecord, error) {
newValidatorSet := v.CopyValidators(validators)
newValidatorSet := v.CopyValidatorRegistry(validators)

rewardQuotient := RewardQuotient(totalActiveValidatorDeposit)
for _, attesterIndex := range attesterIndices {
Expand Down
48 changes: 24 additions & 24 deletions beacon-chain/core/incentives/incentives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/prysmaticlabs/prysm/shared/params"
)

func newValidators() []*pb.ValidatorRecord {
func newValidatorRegistry() []*pb.ValidatorRecord {
var validators []*pb.ValidatorRecord
for i := 0; i < 10; i++ {
validator := &pb.ValidatorRecord{Balance: 32 * 1e9, Status: uint64(params.Active)}
Expand All @@ -19,7 +19,7 @@ func newValidators() []*pb.ValidatorRecord {
}

func TestComputeValidatorRewardsAndPenalties(t *testing.T) {
validators := newValidators()
validators := newValidatorRegistry()
defaultBalance := uint64(32 * 1e9)

participatedDeposit := 4 * defaultBalance
Expand All @@ -29,10 +29,10 @@ func TestComputeValidatorRewardsAndPenalties(t *testing.T) {
timeSinceFinality := uint64(5)

data := &pb.BeaconState{
Validators: validators,
ValidatorSetChangeSlot: 1,
LastJustifiedSlot: 4,
LastFinalizedSlot: 3,
ValidatorRegistry: validators,
ValidatorRegistryLastChangeSlot: 1,
LastJustifiedSlot: 4,
LastFinalizedSlot: 3,
}

activeValidatorIndices := make([]uint32, 0, len(validators))
Expand All @@ -42,32 +42,32 @@ func TestComputeValidatorRewardsAndPenalties(t *testing.T) {
}
}

rewardedValidators := CalculateRewards(
rewardedValidatorRegistry := CalculateRewards(
[]uint32{2, 3, 6, 9},
activeValidatorIndices,
data.Validators,
data.ValidatorRegistry,
totalDeposit,
participatedDeposit,
timeSinceFinality,
)

expectedBalance := defaultBalance - defaultBalance/uint64(rewQuotient)

if rewardedValidators[0].Balance != expectedBalance {
t.Fatalf("validator balance not updated correctly: %d, %d", rewardedValidators[0].Balance, expectedBalance)
if rewardedValidatorRegistry[0].Balance != expectedBalance {
t.Fatalf("validator balance not updated correctly: %d, %d", rewardedValidatorRegistry[0].Balance, expectedBalance)
}

expectedBalance = uint64(int64(defaultBalance) + int64(defaultBalance/rewQuotient)*(2*int64(participatedDeposit)-int64(totalDeposit))/int64(totalDeposit))

if rewardedValidators[6].Balance != expectedBalance {
t.Fatalf("validator balance not updated correctly: %d, %d", rewardedValidators[6].Balance, expectedBalance)
if rewardedValidatorRegistry[6].Balance != expectedBalance {
t.Fatalf("validator balance not updated correctly: %d, %d", rewardedValidatorRegistry[6].Balance, expectedBalance)
}

if rewardedValidators[9].Balance != expectedBalance {
t.Fatalf("validator balance not updated correctly: %d, %d", rewardedValidators[9].Balance, expectedBalance)
if rewardedValidatorRegistry[9].Balance != expectedBalance {
t.Fatalf("validator balance not updated correctly: %d, %d", rewardedValidatorRegistry[9].Balance, expectedBalance)
}

validators = newValidators()
validators = newValidatorRegistry()
timeSinceFinality = 200

activeValidatorIndices = make([]uint32, 0, len(validators))
Expand All @@ -77,30 +77,30 @@ func TestComputeValidatorRewardsAndPenalties(t *testing.T) {
}
}

rewardedValidators = CalculateRewards(
rewardedValidatorRegistry = CalculateRewards(
[]uint32{1, 2, 7, 8},
activeValidatorIndices,
validators,
totalDeposit,
participatedDeposit,
timeSinceFinality)

if rewardedValidators[1].Balance != defaultBalance {
t.Fatalf("validator balance not updated correctly: %d, %d", rewardedValidators[1].Balance, defaultBalance)
if rewardedValidatorRegistry[1].Balance != defaultBalance {
t.Fatalf("validator balance not updated correctly: %d, %d", rewardedValidatorRegistry[1].Balance, defaultBalance)
}

if rewardedValidators[7].Balance != defaultBalance {
t.Fatalf("validator balance not updated correctly: %d, %d", rewardedValidators[7].Balance, defaultBalance)
if rewardedValidatorRegistry[7].Balance != defaultBalance {
t.Fatalf("validator balance not updated correctly: %d, %d", rewardedValidatorRegistry[7].Balance, defaultBalance)
}

expectedBalance = defaultBalance - (defaultBalance/rewQuotient + defaultBalance*timeSinceFinality/penaltyQuotient)

if rewardedValidators[0].Balance != expectedBalance {
t.Fatalf("validator balance not updated correctly: %d, %d", rewardedValidators[0].Balance, expectedBalance)
if rewardedValidatorRegistry[0].Balance != expectedBalance {
t.Fatalf("validator balance not updated correctly: %d, %d", rewardedValidatorRegistry[0].Balance, expectedBalance)
}

if rewardedValidators[9].Balance != expectedBalance {
t.Fatalf("validator balance not updated correctly: %d, %d", rewardedValidators[9].Balance, expectedBalance)
if rewardedValidatorRegistry[9].Balance != expectedBalance {
t.Fatalf("validator balance not updated correctly: %d, %d", rewardedValidatorRegistry[9].Balance, expectedBalance)
}

}
Expand Down
10 changes: 5 additions & 5 deletions beacon-chain/core/state/processing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,20 @@ func TestProcessSpecialRecords(t *testing.T) {
validators[i] = &pb.ValidatorRecord{Status: uint64(params.Active)}
}

newValidators, err := ProcessSpecialRecords(99, validators, specialRecords)
newValidatorRegistry, err := ProcessSpecialRecords(99, validators, specialRecords)
if err != nil {
t.Fatalf("Failed to call process special records %v", err)
}
if newValidators[4].Status != uint64(params.PendingExit) {
if newValidatorRegistry[4].Status != uint64(params.PendingExit) {
t.Error("Validator 4 status is not PendingExit")
}
if newValidators[4].LatestStatusChangeSlot != 99 {
if newValidatorRegistry[4].LatestStatusChangeSlot != 99 {
t.Error("Validator 4 last status change slot is not 99")
}
if newValidators[5].Status != uint64(params.PendingExit) {
if newValidatorRegistry[5].Status != uint64(params.PendingExit) {
t.Error("Validator 5 status is not PendingExit")
}
if newValidators[5].LatestStatusChangeSlot != 99 {
if newValidatorRegistry[5].LatestStatusChangeSlot != 99 {
t.Error("Validator 5 last status change slot is not 99")
}
}
30 changes: 15 additions & 15 deletions beacon-chain/core/state/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewStateTransition(
justifiedSlot := st.LastJustifiedSlot()
finalizedSlot := st.LastFinalizedSlot()
timeSinceFinality := block.SlotNumber() - newState.LastFinalizedSlot()
newState.SetValidators(v.CopyValidators(newState.Validators()))
newState.SetValidatorRegistry(v.CopyValidatorRegistry(newState.ValidatorRegistry()))

newState.ClearAttestations(st.LastStateRecalculationSlot())
// Derive the new set of recent block hashes.
Expand Down Expand Up @@ -65,21 +65,21 @@ func NewStateTransition(
blockVoteBalance, validators := incentives.TallyVoteBalances(
common.BytesToHash(blockHash),
blockVoteCache,
newState.Validators(),
v.ActiveValidatorIndices(newState.Validators()),
v.TotalActiveValidatorDeposit(newState.Validators()),
newState.ValidatorRegistry(),
v.ActiveValidatorIndices(newState.ValidatorRegistry()),
v.TotalActiveValidatorDeposit(newState.ValidatorRegistry()),
timeSinceFinality,
)

newState.SetValidators(validators)
newState.SetValidatorRegistry(validators)

justifiedSlot, finalizedSlot, justifiedStreak = FinalizeAndJustifySlots(
slot,
justifiedSlot,
finalizedSlot,
justifiedStreak,
blockVoteBalance,
v.TotalActiveValidatorDeposit(st.Validators()),
v.TotalActiveValidatorDeposit(st.ValidatorRegistry()),
)
}

Expand All @@ -99,14 +99,14 @@ func NewStateTransition(
newState.SetJustifiedStreak(justifiedStreak)

// Exit the validators when their balance fall below min online deposit size.
newState.SetValidators(v.CheckValidatorMinDeposit(newState.Validators(), block.SlotNumber()))
newState.SetValidatorRegistry(v.CheckValidatorMinDeposit(newState.ValidatorRegistry(), block.SlotNumber()))

// Entering new validator set change transition.
if newState.IsValidatorSetChange(block.SlotNumber()) {
newState.SetValidatorSetChangeSlot(newState.LastStateRecalculationSlot())
newState.SetValidatorRegistryLastChangeSlot(newState.LastStateRecalculationSlot())
shardAndCommitteesForSlots, err := validatorSetRecalculations(
newState.ShardAndCommitteesForSlots(),
newState.Validators(),
newState.ValidatorRegistry(),
block.ParentHash(),
)
if err != nil {
Expand All @@ -116,7 +116,7 @@ func NewStateTransition(

period := block.SlotNumber() / params.BeaconConfig().MinWithdrawalPeriod
totalPenalties := newState.PenalizedETH(period)
newState.SetValidators(v.ChangeValidators(block.SlotNumber(), totalPenalties, newState.Validators()))
newState.SetValidatorRegistry(v.ChangeValidatorRegistry(block.SlotNumber(), totalPenalties, newState.ValidatorRegistry()))
}
}
newState.SetLastStateRecalculationSlot(newState.LastStateRecalculationSlot() + 1)
Expand Down Expand Up @@ -148,7 +148,7 @@ func crossLinkCalculations(
return nil, err
}

totalBalance, voteBalance, err := v.VotedBalanceInAttestation(st.Validators(), indices, attestation)
totalBalance, voteBalance, err := v.VotedBalanceInAttestation(st.ValidatorRegistry(), indices, attestation)
if err != nil {
return nil, err
}
Expand All @@ -158,15 +158,15 @@ func crossLinkCalculations(
currentSlot,
indices,
attestation,
st.Validators(),
v.TotalActiveValidatorDeposit(st.Validators()),
st.ValidatorRegistry(),
v.TotalActiveValidatorDeposit(st.ValidatorRegistry()),
totalBalance,
voteBalance,
)
if err != nil {
return nil, err
}
st.SetValidators(newValidatorSet)
st.SetValidatorRegistry(newValidatorSet)
crossLinkRecords = UpdateLatestCrosslinks(slot, voteBalance, totalBalance, attestation, crossLinkRecords)
}
return crossLinkRecords, nil
Expand All @@ -183,7 +183,7 @@ func validatorSetRecalculations(
crosslinkLastShard := shardAndCommittesForSlots[lastSlot].ArrayShardAndCommittee[lastCommitteeFromLastSlot].Shard
crosslinkNextShard := (crosslinkLastShard + 1) % params.BeaconConfig().ShardCount

newShardCommitteeArray, err := v.ShuffleValidatorsToCommittees(
newShardCommitteeArray, err := v.ShuffleValidatorRegistryToCommittees(
seed,
validators,
crosslinkNextShard,
Expand Down
16 changes: 8 additions & 8 deletions beacon-chain/core/state/state_transition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ func TestNextDeriveSlot(t *testing.T) {
t.Fatalf("failed to derive next crystallized state: %v", err)
}

beaconState.SetValidators([]*pb.ValidatorRecord{
beaconState.SetValidatorRegistry([]*pb.ValidatorRecord{
{Balance: uint64(params.BeaconConfig().DepositSize * params.BeaconConfig().Gwei),
Status: uint64(params.Active)},
})

totalDeposits := v.TotalActiveValidatorDeposit(beaconState.Validators())
totalDeposits := v.TotalActiveValidatorDeposit(beaconState.ValidatorRegistry())
recentShardBlockHashes := make([][]byte, 3*params.BeaconConfig().CycleLength)
for i := 0; i < int(params.BeaconConfig().CycleLength); i++ {
shardBlockHash := [32]byte{}
Expand Down Expand Up @@ -177,7 +177,7 @@ func TestProcessLatestCrosslinks(t *testing.T) {

beaconState := types.NewBeaconState(&pb.BeaconState{
LatestCrosslinks: clRecords,
Validators: validators,
ValidatorRegistry: validators,
ShardAndCommitteesForSlots: shardAndCommitteesForSlots,
})
newLatestCrosslinks, err := crossLinkCalculations(beaconState, pAttestations, 100)
Expand All @@ -199,7 +199,7 @@ func TestIsNewValidatorSetTransition(t *testing.T) {
if err != nil {
t.Fatalf("Failed to initialize state: %v", err)
}
beaconState.SetValidatorSetChangeSlot(1)
beaconState.SetValidatorRegistryLastChangeSlot(1)
if beaconState.IsValidatorSetChange(0) {
t.Errorf("Is new validator set change should be false, last changed slot greater than finalized slot")
}
Expand Down Expand Up @@ -245,17 +245,17 @@ func TestNewValidatorSetRecalculationsInvalid(t *testing.T) {
if err != nil {
t.Fatalf("Failed to initialize state: %v", err)
}
// Negative test case, shuffle validators with more than MaxValidators.
// Negative test case, shuffle validators with more than MaxValidatorRegistry.
size := params.BeaconConfig().ModuloBias + 1
validators := make([]*pb.ValidatorRecord, size)
validator := &pb.ValidatorRecord{Status: uint64(params.Active)}
for i := uint64(0); i < size; i++ {
validators[i] = validator
}
beaconState.SetValidators(validators)
beaconState.SetValidatorRegistry(validators)
if _, err := validatorSetRecalculations(
beaconState.ShardAndCommitteesForSlots(),
beaconState.Validators(),
beaconState.ValidatorRegistry(),
[32]byte{'A'},
); err == nil {
t.Error("Validator set change calculation should have failed with invalid validator count")
Expand Down Expand Up @@ -284,7 +284,7 @@ func TestNewValidatorSetRecalculations(t *testing.T) {

_, err = validatorSetRecalculations(
beaconState.ShardAndCommitteesForSlots(),
beaconState.Validators(),
beaconState.ValidatorRegistry(),
[32]byte{'A'},
)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/core/state/validity_conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func IsValidBlock(
return fmt.Errorf("could not get proposer index: %v", err)
}

stateProposerRandaoSeed := beaconState.Validators()[proposerIndex].RandaoCommitmentHash32
stateProposerRandaoSeed := beaconState.ValidatorRegistry()[proposerIndex].RandaoCommitmentHash32
blockRandaoRevealHash32 := block.RandaoRevealHash32()

// If this is a block created by the simulator service (while in development
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/core/state/validity_conditions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func TestBlockValidity(t *testing.T) {
}
randaoPreCommit := [32]byte{'A'}
hashedRandaoPreCommit := hashutil.Hash(randaoPreCommit[:])
validators := beaconState.Validators()
validators := beaconState.ValidatorRegistry()
validators[1].RandaoCommitmentHash32 = hashedRandaoPreCommit[:]
beaconState.SetValidators(validators)
beaconState.SetValidatorRegistry(validators)
beaconState.SetLatestBlockHashes(recentBlockHashes)

b := types.NewBlock(&pb.BeaconBlock{
Expand Down
Loading