Fixed Sync With Simulator (#588)

This commit is contained in:
terence tsao
2018-10-01 12:14:10 -07:00
committed by Raul Jordan
parent c4ea6b8e13
commit 085b45626e
21 changed files with 125 additions and 108 deletions

View File

@@ -120,14 +120,14 @@ func (s *Service) fetchCurrentAssignmentsAndGenesisTime(client pb.BeaconServiceC
if err != nil {
// If this RPC request fails, the entire system should fatal as it is critical for
// the validator to begin this way.
log.Fatalf("could not fetch genesis time and latest canonical state from beacon node: %v", err)
log.Fatalf("Could not fetch genesis time and latest canonical state from beacon node: %v", err)
}
// Determine what slot the beacon node is in by checking the number of seconds
// since the genesis block.
genesisTimestamp, err := ptypes.Timestamp(res.GetGenesisTimestamp())
if err != nil {
log.Fatalf("cannot compute genesis timestamp: %v", err)
log.Fatalf("Cannot compute genesis timestamp: %v", err)
}
log.Infof("Setting validator genesis time to %s", genesisTimestamp.Format(time.UnixDate))
@@ -170,13 +170,19 @@ func (s *Service) listenForAssignmentChange(client pb.BeaconServiceClient) {
if err != nil {
log.Errorf("Could not receive latest validator assignment from stream: %v", err)
continue
break
}
for _, assign := range assignment.Assignments {
if bytes.Equal(assign.PublicKey.PublicKey, s.pubKey) {
s.role = assign.Role
s.assignedSlot = s.CurrentCycleStartSlot() + assign.AssignedSlot
// If the current cycle is genesis, we set the assigned slot to be
// params.CycleLength + assign.AssignedSlot
if s.CurrentCycleStartSlot() == 0 {
s.assignedSlot = params.DefaultConfig().CycleLength + assign.AssignedSlot
} else {
s.assignedSlot = s.CurrentCycleStartSlot() + assign.AssignedSlot
}
s.shardID = assign.ShardId
log.Infof("Validator with pub key 0x%s re-assigned to shard ID %d for %v duty at slot %d",
@@ -246,7 +252,7 @@ func (s *Service) listenForProcessedAttestations(client pb.BeaconServiceClient)
}
if err != nil {
log.Errorf("Could not receive latest attestation from stream: %v", err)
continue
break
}
log.WithField("slotNumber", attestation.GetSlot()).Info("Latest attestation slot number")
@@ -280,7 +286,10 @@ func (s *Service) PublicKey() []byte {
// CurrentBeaconSlot based on the genesis timestamp of the protocol.
func (s *Service) CurrentBeaconSlot() uint64 {
secondsSinceGenesis := time.Since(s.genesisTimestamp).Seconds()
return uint64(math.Floor(secondsSinceGenesis / params.DefaultConfig().SlotDuration))
if math.Floor(secondsSinceGenesis/8.0)-1 < 0 {
return 0
}
return uint64(math.Floor(secondsSinceGenesis/params.DefaultConfig().SlotDuration)) - 1
}
// CurrentCycleStartSlot returns the slot at which the current cycle started.

View File

@@ -249,21 +249,6 @@ func TestListenForProcessedAttestations(t *testing.T) {
testutil.AssertLogsContain(t, hook, "Latest attestation slot number")
// Testing an error coming from the stream.
stream = internal.NewMockBeaconService_LatestAttestationClient(ctrl)
stream.EXPECT().Recv().Return(&pbp2p.AggregatedAttestation{}, errors.New("stream error"))
stream.EXPECT().Recv().Return(&pbp2p.AggregatedAttestation{}, io.EOF)
mockServiceClient = internal.NewMockBeaconServiceClient(ctrl)
mockServiceClient.EXPECT().LatestAttestation(
gomock.Any(),
gomock.Any(),
).Return(stream, nil)
b.listenForProcessedAttestations(mockServiceClient)
testutil.AssertLogsContain(t, hook, "stream error")
// Creating a faulty stream will trigger error.
mockServiceClient = internal.NewMockBeaconServiceClient(ctrl)
mockServiceClient.EXPECT().LatestAttestation(
@@ -273,7 +258,6 @@ func TestListenForProcessedAttestations(t *testing.T) {
b.listenForProcessedAttestations(mockServiceClient)
testutil.AssertLogsContain(t, hook, "stream creation failed")
testutil.AssertLogsContain(t, hook, "Could not receive latest attestation from stream")
// Test that the routine exits when context is closed
stream = internal.NewMockBeaconService_LatestAttestationClient(ctrl)
@@ -319,21 +303,6 @@ func TestListenForAssignmentProposer(t *testing.T) {
testutil.AssertLogsContain(t, hook, "Validator with pub key 0xA re-assigned to shard ID 2 for PROPOSER duty")
// Testing an error coming from the stream.
stream = internal.NewMockBeaconService_ValidatorAssignmentsClient(ctrl)
stream.EXPECT().Recv().Return(&pb.ValidatorAssignmentResponse{}, errors.New("stream error"))
stream.EXPECT().Recv().Return(&pb.ValidatorAssignmentResponse{}, io.EOF)
mockServiceValidator = internal.NewMockBeaconServiceClient(ctrl)
mockServiceValidator.EXPECT().ValidatorAssignments(
gomock.Any(),
gomock.Any(),
).Return(stream, nil)
b.listenForAssignmentChange(mockServiceValidator)
testutil.AssertLogsContain(t, hook, "stream error")
// Creating a faulty stream will trigger error.
mockServiceValidator = internal.NewMockBeaconServiceClient(ctrl)
mockServiceValidator.EXPECT().ValidatorAssignments(
@@ -356,7 +325,7 @@ func TestListenForAssignmentProposer(t *testing.T) {
gomock.Any(),
).Return(stream, nil)
b.cancel()
//
b.listenForAssignmentChange(mockServiceValidator)
testutil.AssertLogsContain(t, hook, "Context has been canceled so shutting down the loop")
}

View File

@@ -12,7 +12,7 @@ func DefaultConfig() *Config {
return &Config{
CollationSizeLimit: DefaultCollationSizeLimit(),
SlotDuration: 8.0,
CycleLength: 64,
CycleLength: 5,
}
}