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 validator roles into an array #4081

Merged
merged 8 commits into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 2 additions & 3 deletions proto/beacon/db/attestation_container.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions proto/beacon/db/finalized_block_root_container.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions proto/beacon/p2p/v1/messages.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions proto/beacon/p2p/v1/types.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

208 changes: 101 additions & 107 deletions proto/beacon/rpc/v1/services.pb.go

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion proto/beacon/rpc/v1/services.proto
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ message ChainStartResponse {
UNKNOWN = 0;
ATTESTER = 1;
PROPOSER = 2;
BOTH = 3;
}

message ValidatorIndexRequest {
Expand Down
204 changes: 99 additions & 105 deletions proto/beacon/rpc/v1_gateway/services.pb.go

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions proto/eth/v1alpha1/archive.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions proto/eth/v1alpha1/attestation.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions proto/eth/v1alpha1/beacon_block.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions proto/eth/v1alpha1/beacon_chain.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions proto/eth/v1alpha1/node.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions proto/eth/v1alpha1/slasher.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions proto/eth/v1alpha1/validator.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions proto/sharding/p2p/v1/messages.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions validator/client/fake_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type fakeValidator struct {
UpdateAssignmentsRet error
RoleAtCalled bool
RoleAtArg1 uint64
RoleAtRet pb.ValidatorRole
RolesAtRet []pb.ValidatorRole
AttestToBlockHeadCalled bool
AttestToBlockHeadArg1 uint64
ProposeBlockCalled bool
Expand Down Expand Up @@ -77,11 +77,11 @@ func (fv *fakeValidator) LogValidatorGainsAndLosses(_ context.Context, slot uint
return nil
}

func (fv *fakeValidator) RolesAt(slot uint64) map[[48]byte]pb.ValidatorRole {
func (fv *fakeValidator) RolesAt(slot uint64) map[[48]byte][]pb.ValidatorRole {
fv.RoleAtCalled = true
fv.RoleAtArg1 = slot
vr := make(map[[48]byte]pb.ValidatorRole)
vr[[48]byte{1}] = fv.RoleAtRet
vr := make(map[[48]byte][]pb.ValidatorRole)
vr[[48]byte{1}] = fv.RolesAtRet
return vr
}

Expand Down
39 changes: 16 additions & 23 deletions validator/client/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ package client

import (
"context"
"fmt"
"sync"
"time"

pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/sirupsen/logrus"
"go.opencensus.io/trace"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand All @@ -26,7 +23,7 @@ type Validator interface {
SlotDeadline(slot uint64) time.Time
LogValidatorGainsAndLosses(ctx context.Context, slot uint64) error
UpdateAssignments(ctx context.Context, slot uint64) error
RolesAt(slot uint64) map[[48]byte]pb.ValidatorRole // validator pubKey -> role
RolesAt(slot uint64) map[[48]byte][]pb.ValidatorRole // validator pubKey -> roles
SubmitAttestation(ctx context.Context, slot uint64, pubKey [48]byte)
ProposeBlock(ctx context.Context, slot uint64, pubKey [48]byte)
}
Expand Down Expand Up @@ -85,28 +82,24 @@ func run(ctx context.Context, v Validator) {
}

var wg sync.WaitGroup
for id, role := range v.RolesAt(slot) {
for id, roles := range v.RolesAt(slot) {
wg.Add(1)
go func(role pb.ValidatorRole, id [48]byte) {
log := log.WithFields(logrus.Fields{
"pubKey": fmt.Sprintf("%#x", bytesutil.Trunc(id[:])),
"role": role,
})
switch role {
case pb.ValidatorRole_BOTH:
go v.SubmitAttestation(slotCtx, slot, id)
v.ProposeBlock(slotCtx, slot, id)
case pb.ValidatorRole_ATTESTER:
v.SubmitAttestation(slotCtx, slot, id)
case pb.ValidatorRole_PROPOSER:
v.ProposeBlock(slotCtx, slot, id)
case pb.ValidatorRole_UNKNOWN:
log.Debug("No active role, doing nothing")
default:
log.Warn("Unhandled role")
go func(roles []pb.ValidatorRole, id [48]byte) {

for _, role := range roles {
switch role {
case pb.ValidatorRole_ATTESTER:
go v.SubmitAttestation(slotCtx, slot, id)
case pb.ValidatorRole_PROPOSER:
go v.ProposeBlock(slotCtx, slot, id)
case pb.ValidatorRole_UNKNOWN:
log.Debug("No active roles, doing nothing")
default:
log.Warnf("Unhandled role %v", role)
}
}

}(role, id)
}(roles, id)
}
// Wait for all processes to complete, then report span complete.
go func() {
Expand Down
6 changes: 3 additions & 3 deletions validator/client/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func TestAttests_NextSlot(t *testing.T) {
slot := uint64(55)
ticker := make(chan uint64)
v.NextSlotRet = ticker
v.RoleAtRet = pb.ValidatorRole_ATTESTER
v.RolesAtRet = []pb.ValidatorRole{pb.ValidatorRole_ATTESTER}
go func() {
ticker <- slot

Expand All @@ -138,7 +138,7 @@ func TestProposes_NextSlot(t *testing.T) {
slot := uint64(55)
ticker := make(chan uint64)
v.NextSlotRet = ticker
v.RoleAtRet = pb.ValidatorRole_PROPOSER
v.RolesAtRet = []pb.ValidatorRole{pb.ValidatorRole_PROPOSER}
go func() {
ticker <- slot

Expand All @@ -162,7 +162,7 @@ func TestBothProposesAndAttests_NextSlot(t *testing.T) {
slot := uint64(55)
ticker := make(chan uint64)
v.NextSlotRet = ticker
v.RoleAtRet = pb.ValidatorRole_BOTH
v.RolesAtRet = []pb.ValidatorRole{pb.ValidatorRole_ATTESTER, pb.ValidatorRole_PROPOSER}
go func() {
ticker <- slot

Expand Down
32 changes: 17 additions & 15 deletions validator/client/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,25 +259,27 @@ func (v *validator) UpdateAssignments(ctx context.Context, slot uint64) error {
// RolesAt slot returns the validator roles at the given slot. Returns nil if the
// validator is known to not have a roles at the at slot. Returns UNKNOWN if the
// validator assignments are unknown. Otherwise returns a valid ValidatorRole map.
func (v *validator) RolesAt(slot uint64) map[[48]byte]pb.ValidatorRole {
rolesAt := make(map[[48]byte]pb.ValidatorRole)
func (v *validator) RolesAt(slot uint64) map[[48]byte][]pb.ValidatorRole {
rolesAt := make(map[[48]byte][]pb.ValidatorRole)
for _, assignment := range v.assignments.ValidatorAssignment {
var role pb.ValidatorRole
switch {
case assignment == nil:
role = pb.ValidatorRole_UNKNOWN
case assignment.ProposerSlot == slot && assignment.AttesterSlot == slot:
role = pb.ValidatorRole_BOTH
case assignment.AttesterSlot == slot:
role = pb.ValidatorRole_ATTESTER
case assignment.ProposerSlot == slot:
role = pb.ValidatorRole_PROPOSER
default:
role = pb.ValidatorRole_UNKNOWN
var roles []pb.ValidatorRole

if assignment == nil {
continue
}
if assignment.ProposerSlot == slot {
roles = append(roles, pb.ValidatorRole_PROPOSER)
}
if assignment.AttesterSlot == slot {
roles = append(roles, pb.ValidatorRole_ATTESTER)
}
if len(roles) == 0 {
roles = append(roles, pb.ValidatorRole_UNKNOWN)
}

var pubKey [48]byte
copy(pubKey[:], assignment.PublicKey)
rolesAt[pubKey] = role
rolesAt[pubKey] = roles
}
return rolesAt
}
13 changes: 8 additions & 5 deletions validator/client/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,16 +579,19 @@ func TestRolesAt_OK(t *testing.T) {
},
}
roleMap := v.RolesAt(1)
if roleMap[[48]byte{0x01}] != pb.ValidatorRole_ATTESTER {
if roleMap[[48]byte{0x01}][0] != pb.ValidatorRole_ATTESTER {
t.Errorf("Unexpected validator role. want: ValidatorRole_PROPOSER")
}
if roleMap[[48]byte{0x02}] != pb.ValidatorRole_PROPOSER {
if roleMap[[48]byte{0x02}][0] != pb.ValidatorRole_PROPOSER {
t.Errorf("Unexpected validator role. want: ValidatorRole_ATTESTER")
}
if roleMap[[48]byte{0x03}] != pb.ValidatorRole_UNKNOWN {
if roleMap[[48]byte{0x03}][0] != pb.ValidatorRole_UNKNOWN {
t.Errorf("Unexpected validator role. want: UNKNOWN")
}
if roleMap[[48]byte{0x04}] != pb.ValidatorRole_BOTH {
t.Errorf("Unexpected validator role. want: BOTH")
if roleMap[[48]byte{0x04}][0] != pb.ValidatorRole_PROPOSER {
t.Errorf("Unexpected validator role. want: ValidatorRole_PROPOSER")
}
if roleMap[[48]byte{0x04}][1] != pb.ValidatorRole_ATTESTER {
t.Errorf("Unexpected validator role. want: ValidatorRole_ATTESTER")
}
}