2020-01-21 02:19:42 +00:00
|
|
|
package cache_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-03-15 16:46:26 +00:00
|
|
|
"sync"
|
2020-01-21 02:19:42 +00:00
|
|
|
"testing"
|
|
|
|
|
2024-02-15 05:46:47 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v5/beacon-chain/cache"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/beacon-chain/state"
|
|
|
|
state_native "github.com/prysmaticlabs/prysm/v5/beacon-chain/state/state-native"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/testing/require"
|
2020-01-21 02:19:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSkipSlotCache_RoundTrip(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
c := cache.NewSkipSlotCache()
|
|
|
|
|
2020-10-28 03:09:05 +00:00
|
|
|
r := [32]byte{'a'}
|
2021-07-23 16:11:21 +00:00
|
|
|
s, err := c.Get(ctx, r)
|
2020-07-16 19:34:08 +00:00
|
|
|
require.NoError(t, err)
|
2021-07-23 16:11:21 +00:00
|
|
|
assert.Equal(t, state.BeaconState(nil), s, "Empty cache returned an object")
|
2020-01-21 02:19:42 +00:00
|
|
|
|
2020-10-28 03:09:05 +00:00
|
|
|
require.NoError(t, c.MarkInProgress(r))
|
2020-01-21 02:19:42 +00:00
|
|
|
|
2022-09-16 22:17:46 +00:00
|
|
|
s, err = state_native.InitializeFromProtoPhase0(ðpb.BeaconState{
|
2020-01-31 20:57:01 +00:00
|
|
|
Slot: 10,
|
|
|
|
})
|
2020-07-16 19:34:08 +00:00
|
|
|
require.NoError(t, err)
|
2020-01-21 02:19:42 +00:00
|
|
|
|
2021-11-15 15:13:52 +00:00
|
|
|
c.Put(ctx, r, s)
|
|
|
|
c.MarkNotInProgress(r)
|
2020-01-21 02:19:42 +00:00
|
|
|
|
2020-10-28 03:09:05 +00:00
|
|
|
res, err := c.Get(ctx, r)
|
2020-07-16 19:34:08 +00:00
|
|
|
require.NoError(t, err)
|
2022-10-19 14:37:45 +00:00
|
|
|
assert.DeepEqual(t, res.ToProto(), s.ToProto(), "Expected equal protos to return from cache")
|
2020-01-21 02:19:42 +00:00
|
|
|
}
|
2024-03-15 16:46:26 +00:00
|
|
|
|
|
|
|
func TestSkipSlotCache_DisabledAndEnabled(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
c := cache.NewSkipSlotCache()
|
|
|
|
|
|
|
|
r := [32]byte{'a'}
|
|
|
|
c.Disable()
|
|
|
|
|
|
|
|
require.NoError(t, c.MarkInProgress(r))
|
|
|
|
|
|
|
|
c.Enable()
|
|
|
|
wg := new(sync.WaitGroup)
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
// Get call will only terminate when
|
|
|
|
// it is not longer in progress.
|
|
|
|
obj, err := c.Get(ctx, r)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.IsNil(t, obj)
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
c.MarkNotInProgress(r)
|
|
|
|
wg.Wait()
|
|
|
|
}
|