2019-08-27 22:01:27 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-08-16 12:20:13 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v3/beacon-chain/state"
|
2022-09-16 22:17:46 +00:00
|
|
|
state_native "github.com/prysmaticlabs/prysm/v3/beacon-chain/state/state-native"
|
2022-08-16 12:20:13 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v3/config/params"
|
|
|
|
types "github.com/prysmaticlabs/prysm/v3/consensus-types/primitives"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/encoding/bytesutil"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/testing/require"
|
2021-05-17 18:32:04 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2019-08-27 22:01:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCheckpointStateCache_StateByCheckpoint(t *testing.T) {
|
|
|
|
cache := NewCheckpointStateCache()
|
|
|
|
|
2020-03-28 18:32:11 +00:00
|
|
|
cp1 := ðpb.Checkpoint{Epoch: 1, Root: bytesutil.PadTo([]byte{'A'}, 32)}
|
2022-09-16 22:17:46 +00:00
|
|
|
st, err := state_native.InitializeFromProtoPhase0(ðpb.BeaconState{
|
2020-04-14 20:27:03 +00:00
|
|
|
GenesisValidatorsRoot: params.BeaconConfig().ZeroHash[:],
|
|
|
|
Slot: 64,
|
2020-01-31 20:57:01 +00:00
|
|
|
})
|
2020-07-16 19:34:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-07-23 16:11:21 +00:00
|
|
|
s, err := cache.StateByCheckpoint(cp1)
|
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, "Expected state not to exist in empty cache")
|
2020-07-16 19:34:08 +00:00
|
|
|
|
2020-08-16 14:36:37 +00:00
|
|
|
require.NoError(t, cache.AddCheckpointState(cp1, st))
|
2019-08-27 22:01:27 +00:00
|
|
|
|
2021-07-23 16:11:21 +00:00
|
|
|
s, err = cache.StateByCheckpoint(cp1)
|
2020-07-16 19:34:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-09-16 22:17:46 +00:00
|
|
|
pbState1, err := state_native.ProtobufBeaconStatePhase0(s.InnerStateUnsafe())
|
2021-03-17 03:26:17 +00:00
|
|
|
require.NoError(t, err)
|
2022-09-16 22:17:46 +00:00
|
|
|
pbstate, err := state_native.ProtobufBeaconStatePhase0(st.InnerStateUnsafe())
|
2021-03-17 03:26:17 +00:00
|
|
|
require.NoError(t, err)
|
2021-07-23 16:11:21 +00:00
|
|
|
if !proto.Equal(pbState1, pbstate) {
|
2019-08-27 22:01:27 +00:00
|
|
|
t.Error("incorrectly cached state")
|
|
|
|
}
|
|
|
|
|
2020-03-28 18:32:11 +00:00
|
|
|
cp2 := ðpb.Checkpoint{Epoch: 2, Root: bytesutil.PadTo([]byte{'B'}, 32)}
|
2022-09-16 22:17:46 +00:00
|
|
|
st2, err := state_native.InitializeFromProtoPhase0(ðpb.BeaconState{
|
2020-01-31 20:57:01 +00:00
|
|
|
Slot: 128,
|
|
|
|
})
|
2020-07-16 19:34:08 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-16 14:36:37 +00:00
|
|
|
require.NoError(t, cache.AddCheckpointState(cp2, st2))
|
2020-07-16 19:34:08 +00:00
|
|
|
|
2021-07-23 16:11:21 +00:00
|
|
|
s, err = cache.StateByCheckpoint(cp2)
|
2020-07-16 19:34:08 +00:00
|
|
|
require.NoError(t, err)
|
2021-07-23 16:11:21 +00:00
|
|
|
assert.DeepEqual(t, st2.CloneInnerState(), s.CloneInnerState(), "incorrectly cached state")
|
2019-08-27 22:01:27 +00:00
|
|
|
|
2021-07-23 16:11:21 +00:00
|
|
|
s, err = cache.StateByCheckpoint(cp1)
|
2020-07-16 19:34:08 +00:00
|
|
|
require.NoError(t, err)
|
2021-07-23 16:11:21 +00:00
|
|
|
assert.DeepEqual(t, st.CloneInnerState(), s.CloneInnerState(), "incorrectly cached state")
|
2019-08-27 22:01:27 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 20:57:01 +00:00
|
|
|
func TestCheckpointStateCache_MaxSize(t *testing.T) {
|
2019-08-27 22:01:27 +00:00
|
|
|
c := NewCheckpointStateCache()
|
2022-09-16 22:17:46 +00:00
|
|
|
st, err := state_native.InitializeFromProtoPhase0(ðpb.BeaconState{
|
2020-01-31 20:57:01 +00:00
|
|
|
Slot: 0,
|
|
|
|
})
|
2020-07-16 19:34:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-08-16 14:36:37 +00:00
|
|
|
for i := uint64(0); i < uint64(maxCheckpointStateSize+100); i++ {
|
2021-02-16 07:45:34 +00:00
|
|
|
require.NoError(t, st.SetSlot(types.Slot(i)))
|
2021-02-09 10:05:22 +00:00
|
|
|
require.NoError(t, c.AddCheckpointState(ðpb.Checkpoint{Epoch: types.Epoch(i), Root: make([]byte, 32)}, st))
|
2019-08-27 22:01:27 +00:00
|
|
|
}
|
|
|
|
|
2020-08-16 14:36:37 +00:00
|
|
|
assert.Equal(t, maxCheckpointStateSize, len(c.cache.Keys()))
|
2019-08-27 22:01:27 +00:00
|
|
|
}
|