Stategen: always invalidate hot cache state in StateByRootInitialSync (#6698)

* Stategen: always invalidate hot cache state in StateByRootInitialSync
* Sanity test. @nisdas feedback
* Merge refs/heads/master into defer-invalidate
This commit is contained in:
Preston Van Loon 2020-07-22 21:25:50 -07:00 committed by GitHub
parent c72db6f96a
commit 2c11fcb242
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 4 deletions

View File

@ -51,6 +51,9 @@ func (s *State) StateByRootInitialSync(ctx context.Context, blockRoot [32]byte)
return s.beaconDB.State(ctx, blockRoot)
}
// To invalidate cache for parent root because pre state will get mutated.
defer s.hotStateCache.Delete(blockRoot)
if s.hotStateCache.Has(blockRoot) {
return s.hotStateCache.GetWithoutCopy(blockRoot), nil
}
@ -87,9 +90,6 @@ func (s *State) StateByRootInitialSync(ctx context.Context, blockRoot [32]byte)
return nil, errors.Wrap(err, "could not replay blocks for hot state using root")
}
// To invalidate cache for parent root because pre state will get mutated.
s.hotStateCache.Delete(blockRoot)
return startState, nil
}

View File

@ -139,11 +139,14 @@ func TestStateByRootInitialSync_UseCache(t *testing.T) {
require.NoError(t, service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{Root: r[:]}))
service.hotStateCache.Put(r, beaconState)
loadedState, err := service.StateByRoot(ctx, r)
loadedState, err := service.StateByRootInitialSync(ctx, r)
require.NoError(t, err)
if !proto.Equal(loadedState.InnerStateUnsafe(), beaconState.InnerStateUnsafe()) {
t.Error("Did not correctly cache state")
}
if service.hotStateCache.Has(r) {
t.Error("Hot state cache was not invalidated")
}
}
func TestStateByRootInitialSync_CanProcessUpTo(t *testing.T) {