2019-08-27 22:01:27 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-04-14 20:27:03 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2021-02-16 07:45:34 +00:00
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
2019-11-27 05:08:18 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
2020-01-31 20:57:01 +00:00
|
|
|
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
|
2021-03-08 22:37:33 +00:00
|
|
|
iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface"
|
2019-08-27 22:01:27 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2020-04-14 20:27:03 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
2020-04-20 16:35:04 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2020-07-16 19:34:08 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
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)}
|
2020-01-31 20:57:01 +00:00
|
|
|
st, err := stateTrie.InitializeFromProto(&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)
|
|
|
|
|
2020-11-03 21:18:15 +00:00
|
|
|
state, err := cache.StateByCheckpoint(cp1)
|
2020-07-16 19:34:08 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-10 20:38:16 +00:00
|
|
|
assert.Equal(t, iface.BeaconState(nil), state, "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
|
|
|
|
2020-11-03 21:18:15 +00:00
|
|
|
state, err = cache.StateByCheckpoint(cp1)
|
2020-07-16 19:34:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-08-16 14:36:37 +00:00
|
|
|
if !proto.Equal(state.InnerStateUnsafe(), st.InnerStateUnsafe()) {
|
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)}
|
2020-01-31 20:57:01 +00:00
|
|
|
st2, err := stateTrie.InitializeFromProto(&pb.BeaconState{
|
|
|
|
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
|
|
|
|
2020-11-03 21:18:15 +00:00
|
|
|
state, err = cache.StateByCheckpoint(cp2)
|
2020-07-16 19:34:08 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-16 14:36:37 +00:00
|
|
|
assert.DeepEqual(t, st2.CloneInnerState(), state.CloneInnerState(), "incorrectly cached state")
|
2019-08-27 22:01:27 +00:00
|
|
|
|
2020-11-03 21:18:15 +00:00
|
|
|
state, err = cache.StateByCheckpoint(cp1)
|
2020-07-16 19:34:08 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-16 14:36:37 +00:00
|
|
|
assert.DeepEqual(t, st.CloneInnerState(), state.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()
|
2020-01-31 20:57:01 +00:00
|
|
|
st, err := stateTrie.InitializeFromProto(&pb.BeaconState{
|
|
|
|
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
|
|
|
}
|