Check if all validators are exited on every slot (#7719)

* check if validators are exited in every loop iteration

* regression test

* fix mock validator

* handle context.WithValue key in a more idiomatic way

Co-authored-by: terence tsao <terence@prysmaticlabs.com>
This commit is contained in:
Radosław Kapka 2020-11-04 20:41:47 +01:00 committed by GitHub
parent 3a06f6e228
commit 3485f3b8b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 10 deletions

View File

@ -43,6 +43,10 @@ type FakeValidator struct {
PubkeysToStatusesMap map[[48]byte]ethpb.ValidatorStatus
}
type ctxKey string
var allValidatorsAreExitedCtxKey = ctxKey("exited")
// Done for mocking.
func (fv *FakeValidator) Done() {
fv.DoneCalled = true
@ -178,6 +182,9 @@ func (fv *FakeValidator) PubkeysToStatuses(_ context.Context) map[[48]byte]ethpb
}
// AllValidatorsAreExited for mocking
func (fv *FakeValidator) AllValidatorsAreExited(_ context.Context) (bool, error) {
return false, nil
func (fv *FakeValidator) AllValidatorsAreExited(ctx context.Context) (bool, error) {
if ctx.Value(allValidatorsAreExitedCtxKey) == nil {
return false, nil
}
return ctx.Value(allValidatorsAreExitedCtxKey).(bool), nil
}

View File

@ -86,9 +86,6 @@ func run(ctx context.Context, v Validator) {
handleAssignmentError(err, headSlot)
}
// We initially assume not all validators are exited
allExited := false
for {
ctx, span := trace.StartSpan(ctx, "validator.processSlot")
@ -100,15 +97,12 @@ func run(ctx context.Context, v Validator) {
case slot := <-v.NextSlot():
span.AddAttributes(trace.Int64Attribute("slot", int64(slot)))
if allExited {
log.Info("All validators are exited, no more work to perform...")
continue
}
allExited, err = v.AllValidatorsAreExited(ctx)
allExited, err := v.AllValidatorsAreExited(ctx)
if err != nil {
log.WithError(err).Error("Could not check if validators are exited")
}
if allExited {
log.Info("All validators are exited, no more work to perform...")
continue
}

View File

@ -177,3 +177,20 @@ func TestBothProposesAndAttests_NextSlot(t *testing.T) {
require.Equal(t, true, v.ProposeBlockCalled, "ProposeBlock(%d) was not called", slot)
assert.Equal(t, slot, v.ProposeBlockArg1, "ProposeBlock was called with wrong arg")
}
func TestAllValidatorsAreExited_NextSlot(t *testing.T) {
v := &FakeValidator{}
ctx, cancel := context.WithCancel(context.WithValue(context.Background(), allValidatorsAreExitedCtxKey, true))
hook := logTest.NewGlobal()
slot := uint64(55)
ticker := make(chan uint64)
v.NextSlotRet = ticker
go func() {
ticker <- slot
cancel()
}()
run(ctx, v)
assert.LogsContain(t, hook, "All validators are exited")
}