2020-03-04 22:09:21 +00:00
|
|
|
package cache_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
|
|
|
|
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
|
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2020-07-16 19:34:08 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2020-03-04 22:09:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHotStateCache_RoundTrip(t *testing.T) {
|
|
|
|
c := cache.NewHotStateCache()
|
|
|
|
root := [32]byte{'A'}
|
|
|
|
state := c.Get(root)
|
2020-07-16 19:34:08 +00:00
|
|
|
assert.Equal(t, (*stateTrie.BeaconState)(nil), state)
|
|
|
|
assert.Equal(t, false, c.Has(root), "Empty cache has an object")
|
2020-03-04 22:09:21 +00:00
|
|
|
|
|
|
|
state, err := stateTrie.InitializeFromProto(&pb.BeaconState{
|
|
|
|
Slot: 10,
|
|
|
|
})
|
2020-07-16 19:34:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-03-04 22:09:21 +00:00
|
|
|
c.Put(root, state)
|
2020-07-16 19:34:08 +00:00
|
|
|
assert.Equal(t, true, c.Has(root), "Empty cache does not have an object")
|
2020-03-04 22:09:21 +00:00
|
|
|
|
|
|
|
res := c.Get(root)
|
2020-07-16 19:34:08 +00:00
|
|
|
assert.NotNil(t, state)
|
|
|
|
assert.DeepEqual(t, res.CloneInnerState(), state.CloneInnerState(), "Expected equal protos to return from cache")
|
2020-05-08 19:02:48 +00:00
|
|
|
|
|
|
|
c.Delete(root)
|
2020-07-16 19:34:08 +00:00
|
|
|
assert.Equal(t, false, c.Has(root), "Cache not supposed to have the object")
|
2020-03-04 22:09:21 +00:00
|
|
|
}
|