@@ -515,15 +515,6 @@ func retrieveLatestRecord(recs []*dbCommon.AttestationRecord) *dbCommon.Attestat
515
515
// list of upcoming assignments needs to be updated. For example, at the
516
516
// beginning of a new epoch.
517
517
func (v * validator ) UpdateDuties (ctx context.Context ) error {
518
- // Set deadline to end of epoch.
519
- epoch := slots .ToEpoch (slots .CurrentSlot (v .genesisTime ) + 1 )
520
-
521
- ss , err := slots .EpochStart (epoch + 1 )
522
- if err != nil {
523
- return err
524
- }
525
- ctx , cancel := context .WithDeadline (ctx , v .SlotDeadline (ss ))
526
- defer cancel ()
527
518
ctx , span := trace .StartSpan (ctx , "validator.UpdateDuties" )
528
519
defer span .End ()
529
520
@@ -546,7 +537,7 @@ func (v *validator) UpdateDuties(ctx context.Context) error {
546
537
}
547
538
}
548
539
v .blacklistedPubkeysLock .RUnlock ()
549
-
540
+ epoch := slots . ToEpoch ( slots . CurrentSlot ( v . genesisTime ) + 1 )
550
541
req := & ethpb.DutiesRequest {
551
542
Epoch : epoch ,
552
543
PublicKeys : bytesutil .FromBytes48Array (filteredKeys ),
@@ -562,9 +553,13 @@ func (v *validator) UpdateDuties(ctx context.Context) error {
562
553
return err
563
554
}
564
555
556
+ ss , err := slots .EpochStart (epoch )
557
+ if err != nil {
558
+ return err
559
+ }
565
560
v .dutiesLock .Lock ()
566
561
v .duties = resp
567
- v .logDuties (ss - params . BeaconConfig (). SlotsPerEpoch , v .duties .CurrentEpochDuties , v .duties .NextEpochDuties )
562
+ v .logDuties (ss , v .duties .CurrentEpochDuties , v .duties .NextEpochDuties )
568
563
v .dutiesLock .Unlock ()
569
564
570
565
allExitedCounter := 0
@@ -1146,18 +1141,28 @@ func (v *validator) checkDependentRoots(ctx context.Context, head *structs.HeadE
1146
1141
if head == nil {
1147
1142
return errors .New ("received empty head event" )
1148
1143
}
1149
- if v .duties == nil {
1150
- return errors .New ("duties are not initialized" )
1151
- }
1152
- prevDepedentRoot , err := bytesutil .DecodeHexWithLength (head .PreviousDutyDependentRoot , fieldparams .RootLength )
1144
+ prevDependentRoot , err := bytesutil .DecodeHexWithLength (head .PreviousDutyDependentRoot , fieldparams .RootLength )
1153
1145
if err != nil {
1154
1146
return errors .Wrap (err , "failed to decode previous duty dependent root" )
1155
1147
}
1156
- if bytes .Equal (prevDepedentRoot , params .BeaconConfig ().ZeroHash [:]) {
1148
+ if bytes .Equal (prevDependentRoot , params .BeaconConfig ().ZeroHash [:]) {
1157
1149
return nil
1158
1150
}
1159
- if ! bytes .Equal (prevDepedentRoot , v .duties .PrevDependentRoot ) {
1160
- if err := v .UpdateDuties (ctx ); err != nil {
1151
+ epoch := slots .ToEpoch (slots .CurrentSlot (v .genesisTime ) + 1 )
1152
+ ss , err := slots .EpochStart (epoch + 1 )
1153
+ if err != nil {
1154
+ return errors .Wrap (err , "failed to get epoch start" )
1155
+ }
1156
+ deadline := v .SlotDeadline (ss - 1 )
1157
+ dutiesCtx , cancel := context .WithDeadline (ctx , deadline )
1158
+ defer cancel ()
1159
+ v .dutiesLock .RLock ()
1160
+ needsPrevDependentRootUpdate := v .duties == nil || ! bytes .Equal (prevDependentRoot , v .duties .PrevDependentRoot )
1161
+ v .dutiesLock .RUnlock ()
1162
+ if needsPrevDependentRootUpdate {
1163
+ // There's an edge case when the initial duties are not set yet
1164
+ // This routine will lock and recompute them right after the initial duties finishes.
1165
+ if err := v .UpdateDuties (dutiesCtx ); err != nil {
1161
1166
return errors .Wrap (err , "failed to update duties" )
1162
1167
}
1163
1168
log .Info ("Updated duties due to previous dependent root change" )
@@ -1170,13 +1175,16 @@ func (v *validator) checkDependentRoots(ctx context.Context, head *structs.HeadE
1170
1175
if bytes .Equal (currDepedentRoot , params .BeaconConfig ().ZeroHash [:]) {
1171
1176
return nil
1172
1177
}
1173
- if ! bytes .Equal (currDepedentRoot , v .duties .CurrDependentRoot ) {
1174
- if err := v .UpdateDuties (ctx ); err != nil {
1175
- return errors .Wrap (err , "failed to update duties" )
1176
- }
1177
- log .Info ("Updated duties due to current dependent root change" )
1178
+ v .dutiesLock .RLock ()
1179
+ needsCurrDependentRootUpdate := v .duties == nil || ! bytes .Equal (currDepedentRoot , v .duties .CurrDependentRoot )
1180
+ v .dutiesLock .RUnlock ()
1181
+ if ! needsCurrDependentRootUpdate {
1178
1182
return nil
1179
1183
}
1184
+ if err := v .UpdateDuties (dutiesCtx ); err != nil {
1185
+ return errors .Wrap (err , "failed to update duties" )
1186
+ }
1187
+ log .Info ("Updated duties due to current dependent root change" )
1180
1188
return nil
1181
1189
}
1182
1190
0 commit comments