2020-07-03 17:29:30 +00:00
|
|
|
package stategen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-04-29 14:32:11 +00:00
|
|
|
types "github.com/prysmaticlabs/prysm/consensus-types/primitives"
|
2021-09-23 18:53:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/util"
|
2020-07-03 17:29:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEpochBoundaryStateCache_BadSlotKey(t *testing.T) {
|
2020-07-18 07:56:48 +00:00
|
|
|
_, err := slotKeyFn("sushi")
|
|
|
|
assert.ErrorContains(t, errNotSlotRootInfo.Error(), err, "Did not get wanted error")
|
2020-07-03 17:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEpochBoundaryStateCache_BadRootKey(t *testing.T) {
|
2020-07-18 07:56:48 +00:00
|
|
|
_, err := rootKeyFn("noodle")
|
|
|
|
assert.ErrorContains(t, errNotRootStateInfo.Error(), err, "Did not get wanted error")
|
2020-07-03 17:29:30 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 21:24:00 +00:00
|
|
|
func TestEpochBoundaryStateCache_CanSaveAndDelete(t *testing.T) {
|
2020-07-03 17:29:30 +00:00
|
|
|
e := newBoundaryStateCache()
|
2021-09-23 18:53:46 +00:00
|
|
|
s, err := util.NewBeaconState()
|
2021-02-08 20:00:09 +00:00
|
|
|
require.NoError(t, err)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, s.SetSlot(1))
|
2020-07-03 17:29:30 +00:00
|
|
|
r := [32]byte{'a'}
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, e.put(r, s))
|
2020-07-03 17:29:30 +00:00
|
|
|
|
|
|
|
got, exists, err := e.getByRoot([32]byte{'b'})
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, false, exists, "Should not exist")
|
|
|
|
assert.Equal(t, (*rootStateInfo)(nil), got, "Should not exist")
|
2020-07-03 17:29:30 +00:00
|
|
|
|
|
|
|
got, exists, err = e.getByRoot([32]byte{'a'})
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, true, exists, "Should exist")
|
2021-02-23 22:20:11 +00:00
|
|
|
assert.DeepSSZEqual(t, s.InnerStateUnsafe(), got.state.InnerStateUnsafe(), "Should have the same state")
|
2020-07-03 17:29:30 +00:00
|
|
|
|
|
|
|
got, exists, err = e.getBySlot(2)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, false, exists, "Should not exist")
|
|
|
|
assert.Equal(t, (*rootStateInfo)(nil), got, "Should not exist")
|
2020-07-03 17:29:30 +00:00
|
|
|
|
|
|
|
got, exists, err = e.getBySlot(1)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, true, exists, "Should exist")
|
2021-02-23 22:20:11 +00:00
|
|
|
assert.DeepSSZEqual(t, s.InnerStateUnsafe(), got.state.InnerStateUnsafe(), "Should have the same state")
|
2022-04-06 21:24:00 +00:00
|
|
|
|
|
|
|
require.NoError(t, e.delete(r))
|
|
|
|
got, exists, err = e.getByRoot([32]byte{'b'})
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, false, exists, "Should not exist")
|
|
|
|
assert.Equal(t, (*rootStateInfo)(nil), got, "Should not exist")
|
|
|
|
|
|
|
|
got, exists, err = e.getBySlot(1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, false, exists, "Should not exist")
|
|
|
|
assert.Equal(t, (*rootStateInfo)(nil), got, "Should not exist")
|
2020-07-03 17:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEpochBoundaryStateCache_CanTrim(t *testing.T) {
|
|
|
|
e := newBoundaryStateCache()
|
2021-02-16 07:45:34 +00:00
|
|
|
offSet := types.Slot(10)
|
|
|
|
for i := types.Slot(0); i < offSet.Add(maxCacheSize); i++ {
|
2021-09-23 18:53:46 +00:00
|
|
|
s, err := util.NewBeaconState()
|
2021-02-08 20:00:09 +00:00
|
|
|
require.NoError(t, err)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, s.SetSlot(i))
|
2020-07-03 17:29:30 +00:00
|
|
|
r := [32]byte{byte(i)}
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, e.put(r, s))
|
2020-07-03 17:29:30 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 07:56:48 +00:00
|
|
|
assert.Equal(t, int(maxCacheSize), len(e.rootStateCache.ListKeys()), "Did not trim to the correct amount")
|
|
|
|
assert.Equal(t, int(maxCacheSize), len(e.slotRootCache.ListKeys()), "Did not trim to the correct amount")
|
2020-07-03 17:29:30 +00:00
|
|
|
for _, l := range e.rootStateCache.List() {
|
|
|
|
i, ok := l.(*rootStateInfo)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.Equal(t, true, ok, "Bad type assertion")
|
2020-07-03 17:29:30 +00:00
|
|
|
if i.state.Slot() < offSet {
|
|
|
|
t.Error("Did not trim the correct state")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|