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

Refactor Casper Validators Into Core/Validators Stateless Package #875

Merged
merged 26 commits into from
Nov 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
dfb629b
refactored incentives
rauljordan Nov 20, 2018
b06f858
Merge branch 'master' into refactor-casper
rauljordan Nov 20, 2018
fca08ae
fix build
rauljordan Nov 20, 2018
8549bde
Merge branch 'master' into refactor-casper
terencechain Nov 21, 2018
bb37232
gazelle
terencechain Nov 21, 2018
ded1487
merged with Raul's refactor
terencechain Nov 21, 2018
96365bf
add new package
terencechain Nov 21, 2018
0186fbd
changes to current casper package
terencechain Nov 21, 2018
0921304
Merge branch 'master' into refactor-casper
rauljordan Nov 21, 2018
16ad34e
no need for slot in calculate rewards
rauljordan Nov 21, 2018
0a73068
Merge branch 'refactor-casper' of github.com:rauljordan/prysm into re…
rauljordan Nov 21, 2018
c5714e0
pure
rauljordan Nov 21, 2018
7b490f1
godocs
rauljordan Nov 21, 2018
a7a7951
Merge branch 'master' into refactor-casper
rauljordan Nov 21, 2018
5476172
Merge branch 'master' of github.com:prysmaticlabs/prysm into refactor…
terencechain Nov 21, 2018
0e19333
Merge branch 'refactor-casper' of https://github.com/rauljordan/prysm…
terencechain Nov 21, 2018
9c0004b
use the right package
terencechain Nov 21, 2018
ec1d795
Merge branch 'master' into refactor-casper-validator
terencechain Nov 21, 2018
d518a68
fixed visibility to private
terencechain Nov 21, 2018
d659558
godoc comments
terencechain Nov 21, 2018
87cfb0a
Merge branch 'refactor-casper-validator' of github.com:prysmaticlabs/…
terencechain Nov 21, 2018
044f913
extra line, ops
terencechain Nov 21, 2018
61b274a
removed logs
terencechain Nov 21, 2018
78627bf
merged with master
terencechain Nov 21, 2018
ebfbcb5
removed logs
terencechain Nov 21, 2018
f39ad53
gazelle
terencechain Nov 21, 2018
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/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ go_test(
srcs = ["service_test.go"],
embed = [":go_default_library"],
deps = [
"//beacon-chain/casper:go_default_library",
"//beacon-chain/core/validators:go_default_library",
"//beacon-chain/db:go_default_library",
"//beacon-chain/internal:go_default_library",
"//beacon-chain/powchain:go_default_library",
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/blockchain/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
gethTypes "github.com/ethereum/go-ethereum/core/types"
"github.com/prysmaticlabs/prysm/beacon-chain/casper"
v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
"github.com/prysmaticlabs/prysm/beacon-chain/internal"
"github.com/prysmaticlabs/prysm/beacon-chain/powchain"
Expand Down Expand Up @@ -243,7 +243,7 @@ func TestDoesPOWBlockExist(t *testing.T) {
}

func getShardForSlot(t *testing.T, cState *types.CrystallizedState, slot uint64) uint64 {
shardAndCommittee, err := casper.GetShardAndCommitteesForSlot(
shardAndCommittee, err := v.GetShardAndCommitteesForSlot(
cState.ShardAndCommitteesForSlots(),
cState.LastStateRecalculationSlot(),
slot)
Expand Down
8 changes: 2 additions & 6 deletions beacon-chain/casper/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@ go_library(
srcs = [
"sharding.go",
"state_transition.go",
"validator.go",
],
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/casper",
visibility = ["//beacon-chain:__subpackages__"],
deps = [
"//beacon-chain/core/incentives:go_default_library",
"//beacon-chain/core/validators:go_default_library",
"//beacon-chain/utils:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//proto/beacon/rpc/v1:go_default_library",
"//shared/bitutil:go_default_library",
"//shared/hashutil:go_default_library",
"//shared/params:go_default_library",
"@com_github_ethereum_go_ethereum//common:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
],
)

Expand All @@ -29,14 +26,13 @@ go_test(
srcs = [
"sharding_test.go",
"state_transition_test.go",
"validator_test.go",
],
embed = [":go_default_library"],
race = "off", # TODO(#604): fix issues with tests failing with race on.
deps = [
"//beacon-chain/core/validators:go_default_library",
"//beacon-chain/utils:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//shared/bitutil:go_default_library",
"//shared/bytes:go_default_library",
"//shared/params:go_default_library",
"@com_github_ethereum_go_ethereum//common:go_default_library",
Expand Down
3 changes: 2 additions & 1 deletion beacon-chain/casper/sharding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package casper

import (
"github.com/ethereum/go-ethereum/common"
v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators"
"github.com/prysmaticlabs/prysm/beacon-chain/utils"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/params"
)

// ShuffleValidatorsToCommittees shuffles validator indices and splits them by slot and shard.
func ShuffleValidatorsToCommittees(seed common.Hash, validators []*pb.ValidatorRecord, crosslinkStartShard uint64) ([]*pb.ShardAndCommitteeArray, error) {
indices := ActiveValidatorIndices(validators)
indices := v.ActiveValidatorIndices(validators)

// split the shuffled list for slot.
shuffledValidators, err := utils.ShuffleIndices(seed, indices)
Expand Down
7 changes: 4 additions & 3 deletions beacon-chain/casper/sharding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/ethereum/go-ethereum/common"
v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/params"
)
Expand All @@ -21,17 +22,17 @@ func TestGetShardAndCommitteesForSlots(t *testing.T) {
{Shard: 4, Committee: []uint32{5, 6, 7, 8, 9}},
}},
}}
if _, err := GetShardAndCommitteesForSlot(state.ShardAndCommitteesForSlots, state.LastStateRecalculationSlot, 1000); err == nil {
if _, err := v.GetShardAndCommitteesForSlot(state.ShardAndCommitteesForSlots, state.LastStateRecalculationSlot, 1000); err == nil {
t.Error("getShardAndCommitteesForSlot should have failed with invalid slot")
}
committee, err := GetShardAndCommitteesForSlot(state.ShardAndCommitteesForSlots, state.LastStateRecalculationSlot, 0)
committee, err := v.GetShardAndCommitteesForSlot(state.ShardAndCommitteesForSlots, state.LastStateRecalculationSlot, 0)
if err != nil {
t.Errorf("getShardAndCommitteesForSlot failed: %v", err)
}
if committee.ArrayShardAndCommittee[0].Shard != 1 {
t.Errorf("getShardAndCommitteesForSlot returns Shard should be 1, got: %v", committee.ArrayShardAndCommittee[0].Shard)
}
committee, _ = GetShardAndCommitteesForSlot(state.ShardAndCommitteesForSlots, state.LastStateRecalculationSlot, 1)
committee, _ = v.GetShardAndCommitteesForSlot(state.ShardAndCommitteesForSlots, state.LastStateRecalculationSlot, 1)
if committee.ArrayShardAndCommittee[0].Shard != 3 {
t.Errorf("getShardAndCommitteesForSlot returns Shard should be 3, got: %v", committee.ArrayShardAndCommittee[0].Shard)
}
Expand Down
9 changes: 5 additions & 4 deletions beacon-chain/casper/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/binary"

"github.com/prysmaticlabs/prysm/beacon-chain/core/incentives"
v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators"
"github.com/prysmaticlabs/prysm/beacon-chain/utils"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bitutil"
Expand All @@ -25,8 +26,8 @@ func TallyVoteBalances(

blockVoteBalance := blockVote.VoteTotalDeposit
voterIndices := blockVote.VoterIndices
activeValidatorIndices := ActiveValidatorIndices(validators)
totalDeposit := TotalActiveValidatorDeposit(validators)
activeValidatorIndices := v.ActiveValidatorIndices(validators)
totalDeposit := v.TotalActiveValidatorDeposit(validators)
validators = incentives.CalculateRewards(
voterIndices,
activeValidatorIndices,
Expand Down Expand Up @@ -76,7 +77,7 @@ func ApplyCrosslinkRewardsAndPenalties(
totalBalance uint64,
voteBalance uint64) error {

totalDeposit := TotalActiveValidatorDeposit(validators)
totalDeposit := v.TotalActiveValidatorDeposit(validators)
rewardQuotient := incentives.RewardQuotient(totalDeposit)

for _, attesterIndex := range attesterIndices {
Expand Down Expand Up @@ -121,7 +122,7 @@ func ProcessSpecialRecords(slotNumber uint64, validators []*pb.ValidatorRecord,
// Covers validators submitted logouts from last cycle.
if specialRecord.Kind == uint32(params.Logout) {
validatorIndex := binary.BigEndian.Uint64(specialRecord.Data[0])
exitedValidator := ExitValidator(validators[validatorIndex], slotNumber, false)
exitedValidator := v.ExitValidator(validators[validatorIndex], slotNumber, false)
validators[validatorIndex] = exitedValidator
// TODO(#633): Verify specialRecord.Data[1] as signature. BLSVerify(pubkey=validator.pubkey, msg=hash(LOGOUT_MESSAGE + bytes8(version))
}
Expand Down
26 changes: 26 additions & 0 deletions beacon-chain/core/validators/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
srcs = ["validator.go"],
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/core/validators",
visibility = ["//beacon-chain:__subpackages__"],
deps = [
"//proto/beacon/p2p/v1:go_default_library",
"//proto/beacon/rpc/v1:go_default_library",
"//shared/bitutil:go_default_library",
"//shared/hashutil:go_default_library",
"//shared/params:go_default_library",
],
)

go_test(
name = "go_default_test",
srcs = ["validator_test.go"],
embed = [":go_default_library"],
deps = [
"//proto/beacon/p2p/v1:go_default_library",
"//shared/bitutil:go_default_library",
"//shared/params:go_default_library",
],
)
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package casper
// Package validators defines helper functions to locate validator
// based on pubic key. Each validator is associated with a given index,
// shard ID and slot number to propose or attest. This package also defines
// functions to initialize validators, verify validator bit fields,
// and rotate validator in and out of committees.
package validators

import (
"bytes"
"fmt"

pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
pbrpc "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
"github.com/prysmaticlabs/prysm/shared/bitutil"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/sirupsen/logrus"
)

var log = logrus.WithField("prefix", "casper")

const bitsInByte = 8

// InitialValidators creates a new validator set that is used to
Expand Down Expand Up @@ -80,8 +81,6 @@ func GetShardAndCommitteesForSlot(shardCommittees []*pb.ShardAndCommitteeArray,
func AreAttesterBitfieldsValid(attestation *pb.AggregatedAttestation, attesterIndices []uint32) bool {
// Validate attester bit field has the correct length.
if bitutil.BitLength(len(attesterIndices)) != len(attestation.AttesterBitfield) {
log.Debugf("Attestation has incorrect bitfield length. Found %v, expected %v",
len(attestation.AttesterBitfield), bitutil.BitLength(len(attesterIndices)))
return false
}

Expand All @@ -95,12 +94,10 @@ func AreAttesterBitfieldsValid(attestation *pb.AggregatedAttestation, attesterIn
for i := 0; i < bitsInByte-remainingBits; i++ {
isBitSet, err := bitutil.CheckBit(attestation.AttesterBitfield, lastBit+i)
if err != nil {
log.Errorf("Bitfield check failed for attestation at index: %d with: %v", lastBit+i, err)
return false
}

if isBitSet {
log.Error("Attestation has non-zero trailing bits")
return false
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package casper
package validators

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/rpc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ go_library(
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/rpc",
visibility = ["//beacon-chain:__subpackages__"],
deps = [
"//beacon-chain/casper:go_default_library",
"//beacon-chain/core/validators:go_default_library",
"//beacon-chain/db:go_default_library",
"//beacon-chain/types:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
Expand Down
15 changes: 7 additions & 8 deletions beacon-chain/rpc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/golang/protobuf/ptypes/empty"
"github.com/prysmaticlabs/prysm/beacon-chain/casper"
v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
"github.com/prysmaticlabs/prysm/beacon-chain/types"
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
Expand Down Expand Up @@ -212,7 +212,7 @@ func (s *Service) ProposeBlock(ctx context.Context, req *pb.ProposeRequest) (*pb
return nil, fmt.Errorf("could not get crystallized state: %v", err)
}

_, prevProposerIndex, err := casper.ProposerShardAndIndex(
_, prevProposerIndex, err := v.ProposerShardAndIndex(
cState.ShardAndCommitteesForSlots(),
cState.LastStateRecalculationSlot(),
req.GetSlotNumber(),
Expand Down Expand Up @@ -290,7 +290,7 @@ func (s *Service) ValidatorShardID(ctx context.Context, req *pb.PublicKey) (*pb.
return nil, fmt.Errorf("could not get crystallized state: %v", err)
}

shardID, err := casper.ValidatorShardID(
shardID, err := v.ValidatorShardID(
req.PublicKey,
cState.Validators(),
cState.ShardAndCommitteesForSlots(),
Expand All @@ -313,7 +313,7 @@ func (s *Service) ValidatorSlotAndResponsibility(
return nil, fmt.Errorf("could not get crystallized state: %v", err)
}

slot, role, err := casper.ValidatorSlotAndRole(
slot, role, err := v.ValidatorSlotAndRole(
req.PublicKey,
cState.Validators(),
cState.ShardAndCommitteesForSlots(),
Expand All @@ -333,9 +333,8 @@ func (s *Service) ValidatorIndex(ctx context.Context, req *pb.PublicKey) (*pb.In
return nil, fmt.Errorf("could not get crystallized state: %v", err)
}

index, err := casper.ValidatorIndex(
index, err := v.ValidatorIndex(
req.PublicKey,

cState.Validators(),
)
if err != nil {
Expand Down Expand Up @@ -405,7 +404,7 @@ func assignmentsForPublicKeys(keys []*pb.PublicKey, cState *types.CrystallizedSt
// For the corresponding public key and current crystallized state,
// we determine the assigned slot for the validator and whether it
// should act as a proposer or attester.
assignedSlot, role, err := casper.ValidatorSlotAndRole(
assignedSlot, role, err := v.ValidatorSlotAndRole(
val.GetPublicKey(),
cState.Validators(),
cState.ShardAndCommitteesForSlots(),
Expand All @@ -416,7 +415,7 @@ func assignmentsForPublicKeys(keys []*pb.PublicKey, cState *types.CrystallizedSt

// We determine the assigned shard ID for the validator
// based on a public key and current crystallized state.
shardID, err := casper.ValidatorShardID(
shardID, err := v.ValidatorShardID(
val.GetPublicKey(),
cState.Validators(),
cState.ShardAndCommitteesForSlots(),
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/sync/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ go_library(
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/sync",
visibility = ["//beacon-chain:__subpackages__"],
deps = [
"//beacon-chain/casper:go_default_library",
"//beacon-chain/core/validators:go_default_library",
"//beacon-chain/types:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//shared/event:go_default_library",
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/sync/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"

"github.com/golang/protobuf/proto"
"github.com/prysmaticlabs/prysm/beacon-chain/casper"
v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators"
"github.com/prysmaticlabs/prysm/beacon-chain/types"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/event"
Expand Down Expand Up @@ -238,7 +238,7 @@ func (ss *Service) receiveBlock(msg p2p.Message) {
// Verify attestation coming from proposer then forward block to the subscribers.
attestation := types.NewAttestation(response.Attestation)

proposerShardID, _, err := casper.ProposerShardAndIndex(cState.ShardAndCommitteesForSlots(), cState.LastStateRecalculationSlot(), block.SlotNumber())
proposerShardID, _, err := v.ProposerShardAndIndex(cState.ShardAndCommitteesForSlots(), cState.LastStateRecalculationSlot(), block.SlotNumber())
if err != nil {
log.Errorf("Failed to get proposer shard ID: %v", err)
return
Expand Down
1 change: 1 addition & 0 deletions beacon-chain/types/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ go_library(
visibility = ["//beacon-chain:__subpackages__"],
deps = [
"//beacon-chain/casper:go_default_library",
"//beacon-chain/core/validators:go_default_library",
"//beacon-chain/utils:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//shared/bitutil:go_default_library",
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/types/active_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/gogo/protobuf/proto"
"github.com/prysmaticlabs/prysm/beacon-chain/casper"
v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
b "github.com/prysmaticlabs/prysm/shared/bytes"
"github.com/prysmaticlabs/prysm/shared/hashutil"
Expand Down Expand Up @@ -212,7 +212,7 @@ func (a *ActiveState) CalculateNewActiveState(

log.Debugf("Calculating new active state. Crystallized state lastStateRecalc is %d", cState.LastStateRecalculationSlot())

_, proposerIndex, err := casper.ProposerShardAndIndex(
_, proposerIndex, err := v.ProposerShardAndIndex(
cState.ShardAndCommitteesForSlots(),
cState.LastStateRecalculationSlot(),
parentSlot)
Expand Down
8 changes: 4 additions & 4 deletions beacon-chain/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/prysmaticlabs/prysm/beacon-chain/casper"
v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators"
"github.com/prysmaticlabs/prysm/beacon-chain/utils"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bitutil"
Expand Down Expand Up @@ -181,7 +181,7 @@ func (b *Block) IsValid(
return false
}

_, proposerIndex, err := casper.ProposerShardAndIndex(
_, proposerIndex, err := v.ProposerShardAndIndex(
cState.ShardAndCommitteesForSlots(),
cState.LastStateRecalculationSlot(),
b.SlotNumber())
Expand Down Expand Up @@ -216,7 +216,7 @@ func (b *Block) areAttestationsValid(db beaconDB, aState *ActiveState, cState *C
}

func (b *Block) doesParentProposerExist(cState *CrystallizedState, parentSlot uint64) bool {
_, parentProposerIndex, err := casper.ProposerShardAndIndex(
_, parentProposerIndex, err := v.ProposerShardAndIndex(
cState.ShardAndCommitteesForSlots(),
cState.LastStateRecalculationSlot(),
parentSlot)
Expand Down Expand Up @@ -288,7 +288,7 @@ func (b *Block) isAttestationValid(attestationIndex int, db beaconDB, aState *Ac
}

// Verify attester bitfields matches crystallized state's prev computed bitfield.
if !casper.AreAttesterBitfieldsValid(attestation, attesterIndices) {
if !v.AreAttesterBitfieldsValid(attestation, attesterIndices) {
log.Error("Unable to match attester bitfield with shard and committee bitfield")
return false
}
Expand Down
Loading