diff --git a/beacon-chain/state/stategen/getter.go b/beacon-chain/state/stategen/getter.go index 22f83a595..9da3ce41e 100644 --- a/beacon-chain/state/stategen/getter.go +++ b/beacon-chain/state/stategen/getter.go @@ -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 } diff --git a/beacon-chain/state/stategen/getter_test.go b/beacon-chain/state/stategen/getter_test.go index fa879bd0d..fb31dffa5 100644 --- a/beacon-chain/state/stategen/getter_test.go +++ b/beacon-chain/state/stategen/getter_test.go @@ -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) {