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

Update validator to use proposer slot #3919

Merged
merged 4 commits into from
Nov 5, 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
12 changes: 6 additions & 6 deletions beacon-chain/rpc/validator_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,17 @@ func (vs *ValidatorServer) CommitteeAssignment(ctx context.Context, req *pb.Assi

func (vs *ValidatorServer) assignment(idx uint64, beaconState *pbp2p.BeaconState, epoch uint64) (*pb.AssignmentResponse_ValidatorAssignment, error) {
// TODO(3865): Update ValidatorAssignments_CommitteeAssignment to take in proposer slot
committee, shard, slot, isProposer, _, err := helpers.CommitteeAssignment(beaconState, epoch, idx)
committee, shard, aSlot, _, pSlot, err := helpers.CommitteeAssignment(beaconState, epoch, idx)
if err != nil {
return nil, err
}
status := vs.assignmentStatus(idx, beaconState)
return &pb.AssignmentResponse_ValidatorAssignment{
Committee: committee,
Shard: shard,
Slot: slot,
IsProposer: isProposer,
Status: status,
Committee: committee,
Shard: shard,
AttesterSlot: aSlot,
ProposerSlot: pSlot,
Status: status,
}, nil
}

Expand Down
8 changes: 4 additions & 4 deletions beacon-chain/rpc/validator_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ func TestCommitteeAssignment_OK(t *testing.T) {
if err != nil {
t.Fatalf("Could not call epoch committee assignment %v", err)
}
if res.ValidatorAssignment[0].Slot > state.Slot+params.BeaconConfig().SlotsPerEpoch {
if res.ValidatorAssignment[0].AttesterSlot > state.Slot+params.BeaconConfig().SlotsPerEpoch {
t.Errorf("Assigned slot %d can't be higher than %d",
res.ValidatorAssignment[0].Slot, state.Slot+params.BeaconConfig().SlotsPerEpoch)
res.ValidatorAssignment[0].AttesterSlot, state.Slot+params.BeaconConfig().SlotsPerEpoch)
}

// Test the last validator in registry.
Expand All @@ -192,9 +192,9 @@ func TestCommitteeAssignment_OK(t *testing.T) {
if err != nil {
t.Fatalf("Could not call epoch committee assignment %v", err)
}
if res.ValidatorAssignment[0].Slot > state.Slot+params.BeaconConfig().SlotsPerEpoch {
if res.ValidatorAssignment[0].AttesterSlot > state.Slot+params.BeaconConfig().SlotsPerEpoch {
t.Errorf("Assigned slot %d can't be higher than %d",
res.ValidatorAssignment[0].Slot, state.Slot+params.BeaconConfig().SlotsPerEpoch)
res.ValidatorAssignment[0].AttesterSlot, state.Slot+params.BeaconConfig().SlotsPerEpoch)
}
}

Expand Down
250 changes: 124 additions & 126 deletions proto/beacon/rpc/v1/services.pb.go

Large diffs are not rendered by default.

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

message ValidatorIndexRequest {
Expand All @@ -113,8 +114,8 @@ message AssignmentResponse {
message ValidatorAssignment {
repeated uint64 committee = 1;
uint64 shard = 2;
uint64 slot = 3;
bool is_proposer = 4;
uint64 attester_slot = 3;
uint64 proposer_slot = 4;
bytes public_key = 5;
ValidatorStatus status = 6;
}
Expand Down
214 changes: 109 additions & 105 deletions proto/beacon/rpc/v1_gateway/services.pb.go

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion validator/client/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ go_test(
"//shared/testutil:go_default_library",
"//validator/accounts:go_default_library",
"//validator/internal:go_default_library",
"@com_github_gogo_protobuf//proto:go_default_library",
"@com_github_gogo_protobuf//types:go_default_library",
"@com_github_golang_mock//gomock:go_default_library",
"@com_github_prysmaticlabs_go_bitfield//:go_default_library",
Expand Down
4 changes: 3 additions & 1 deletion validator/client/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,12 @@ func run(ctx context.Context, v Validator) {
"role": role,
})
switch role {
case pb.ValidatorRole_BOTH:
go v.AttestToBlockHead(slotCtx, slot, id)
v.ProposeBlock(slotCtx, slot, id)
case pb.ValidatorRole_ATTESTER:
v.AttestToBlockHead(slotCtx, slot, id)
case pb.ValidatorRole_PROPOSER:
go v.AttestToBlockHead(slotCtx, slot, id)
v.ProposeBlock(slotCtx, slot, id)
case pb.ValidatorRole_UNKNOWN:
log.Debug("No active role, doing nothing")
Expand Down
2 changes: 1 addition & 1 deletion validator/client/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func TestBothProposesAndAttests_NextSlot(t *testing.T) {
slot := uint64(55)
ticker := make(chan uint64)
v.NextSlotRet = ticker
v.RoleAtRet = pb.ValidatorRole_PROPOSER
v.RoleAtRet = pb.ValidatorRole_BOTH
go func() {
ticker <- slot

Expand Down
25 changes: 12 additions & 13 deletions validator/client/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,10 @@ func (v *validator) UpdateAssignments(ctx context.Context, slot uint64) error {
"status": assignment.Status,
}
if assignment.Status == pb.ValidatorStatus_ACTIVE {
if assignment.IsProposer {
lFields["proposerSlot"] = assignment.Slot
if assignment.ProposerSlot > 0 {
lFields["proposerSlot"] = assignment.ProposerSlot
}
lFields["attesterSlot"] = assignment.Slot
lFields["attesterSlot"] = assignment.AttesterSlot
}
log.WithFields(lFields).Info("New assignment")
}
Expand All @@ -226,17 +226,16 @@ 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
if assignment == nil {
switch {
case assignment == nil:
role = pb.ValidatorRole_UNKNOWN
}
if assignment.Slot == slot {
// Note: A proposer also attests to the slot.
if assignment.IsProposer {
role = pb.ValidatorRole_PROPOSER
} else {
role = pb.ValidatorRole_ATTESTER
}
} else {
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 pubKey [48]byte
Expand Down
9 changes: 4 additions & 5 deletions validator/client/validator_attest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package client
import (
"context"
"errors"
"reflect"
"sync"
"testing"
"time"

"github.com/gogo/protobuf/proto"
"github.com/golang/mock/gomock"
bitfield "github.com/prysmaticlabs/go-bitfield"
ssz "github.com/prysmaticlabs/go-ssz"
"github.com/prysmaticlabs/go-bitfield"
"github.com/prysmaticlabs/go-ssz"
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
Expand Down Expand Up @@ -163,8 +163,7 @@ func TestAttestToBlockHead_AttestsCorrectly(t *testing.T) {

sig := validator.keys[validatorPubKey].SecretKey.Sign(root[:], 0).Marshal()
expectedAttestation.Signature = sig

if !proto.Equal(generatedAttestation, expectedAttestation) {
if !reflect.DeepEqual(generatedAttestation, expectedAttestation) {
t.Errorf("Incorrectly attested head, wanted %v, received %v", expectedAttestation, generatedAttestation)
}
testutil.AssertLogsContain(t, hook, "Submitted new attestation")
Expand Down
62 changes: 34 additions & 28 deletions validator/client/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,9 @@ func TestUpdateAssignments_DoesNothingWhenNotEpochStartAndAlreadyExistingAssignm
assignments: &pb.AssignmentResponse{
ValidatorAssignment: []*pb.AssignmentResponse_ValidatorAssignment{
{
Committee: []uint64{},
Slot: 10,
Shard: 20,
Committee: []uint64{},
AttesterSlot: 10,
Shard: 20,
},
},
},
Expand Down Expand Up @@ -449,11 +449,11 @@ func TestUpdateAssignments_OK(t *testing.T) {
resp := &pb.AssignmentResponse{
ValidatorAssignment: []*pb.AssignmentResponse_ValidatorAssignment{
{
Slot: params.BeaconConfig().SlotsPerEpoch,
Shard: 100,
Committee: []uint64{0, 1, 2, 3},
IsProposer: true,
PublicKey: []byte("testPubKey_1"),
AttesterSlot: params.BeaconConfig().SlotsPerEpoch,
Shard: 100,
Committee: []uint64{0, 1, 2, 3},
PublicKey: []byte("testPubKey_1"),
ProposerSlot: params.BeaconConfig().SlotsPerEpoch + 1,
},
},
}
Expand All @@ -469,15 +469,14 @@ func TestUpdateAssignments_OK(t *testing.T) {
if err := v.UpdateAssignments(context.Background(), slot); err != nil {
t.Fatalf("Could not update assignments: %v", err)
}

if v.assignments.ValidatorAssignment[0].Slot != params.BeaconConfig().SlotsPerEpoch {
t.Errorf("Unexpected validator assignments. want=%v got=%v", params.BeaconConfig().SlotsPerEpoch, v.assignments.ValidatorAssignment[0].Slot)
if v.assignments.ValidatorAssignment[0].ProposerSlot != params.BeaconConfig().SlotsPerEpoch+1 {
t.Errorf("Unexpected validator assignments. want=%v got=%v", params.BeaconConfig().SlotsPerEpoch+1, v.assignments.ValidatorAssignment[0].ProposerSlot)
}
if v.assignments.ValidatorAssignment[0].Shard != resp.ValidatorAssignment[0].Shard {
t.Errorf("Unexpected validator assignments. want=%v got=%v", resp.ValidatorAssignment[0].Shard, v.assignments.ValidatorAssignment[0].Slot)
if v.assignments.ValidatorAssignment[0].AttesterSlot != params.BeaconConfig().SlotsPerEpoch {
t.Errorf("Unexpected validator assignments. want=%v got=%v", params.BeaconConfig().SlotsPerEpoch, v.assignments.ValidatorAssignment[0].AttesterSlot)
}
if !v.assignments.ValidatorAssignment[0].IsProposer {
t.Errorf("Unexpected validator assignments. want: proposer=true")
if v.assignments.ValidatorAssignment[0].Shard != resp.ValidatorAssignment[0].Shard {
t.Errorf("Unexpected validator assignments. want=%v got=%v", resp.ValidatorAssignment[0].Shard, v.assignments.ValidatorAssignment[0].Shard)
}
}

Expand All @@ -487,33 +486,40 @@ func TestRolesAt_OK(t *testing.T) {
assignments: &pb.AssignmentResponse{
ValidatorAssignment: []*pb.AssignmentResponse_ValidatorAssignment{
{
Shard: 1,
Slot: 1,
IsProposer: true,
PublicKey: []byte{0x01},
Shard: 1,
AttesterSlot: 1,
PublicKey: []byte{0x01},
},
{
Shard: 2,
Slot: 1,
PublicKey: []byte{0x02},
Shard: 2,
ProposerSlot: 1,
PublicKey: []byte{0x02},
},
{
Shard: 1,
Slot: 2,
PublicKey: []byte{0x03},
Shard: 1,
AttesterSlot: 2,
PublicKey: []byte{0x03},
},
{
Shard: 2,
AttesterSlot: 1,
ProposerSlot: 1,
PublicKey: []byte{0x04},
},
},
},
}
roleMap := v.RolesAt(1)
if roleMap[[48]byte{0x01}] != pb.ValidatorRole_PROPOSER {
if roleMap[[48]byte{0x01}] != pb.ValidatorRole_ATTESTER {
t.Errorf("Unexpected validator role. want: ValidatorRole_PROPOSER")
}
if roleMap[[48]byte{0x02}] != pb.ValidatorRole_ATTESTER {
if roleMap[[48]byte{0x02}] != pb.ValidatorRole_PROPOSER {
t.Errorf("Unexpected validator role. want: ValidatorRole_ATTESTER")
}
if roleMap[[48]byte{0x03}] != 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")
}
}