github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/state/stategen/epoch_boundary_state_cache_test.go (about)

     1  package stategen
     2  
     3  import (
     4  	"testing"
     5  
     6  	types "github.com/prysmaticlabs/eth2-types"
     7  	"github.com/prysmaticlabs/prysm/shared/testutil"
     8  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
     9  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    10  )
    11  
    12  func TestEpochBoundaryStateCache_BadSlotKey(t *testing.T) {
    13  	_, err := slotKeyFn("sushi")
    14  	assert.ErrorContains(t, errNotSlotRootInfo.Error(), err, "Did not get wanted error")
    15  }
    16  
    17  func TestEpochBoundaryStateCache_BadRootKey(t *testing.T) {
    18  	_, err := rootKeyFn("noodle")
    19  	assert.ErrorContains(t, errNotRootStateInfo.Error(), err, "Did not get wanted error")
    20  }
    21  
    22  func TestEpochBoundaryStateCache_CanSave(t *testing.T) {
    23  	e := newBoundaryStateCache()
    24  	s, err := testutil.NewBeaconState()
    25  	require.NoError(t, err)
    26  	require.NoError(t, s.SetSlot(1))
    27  	r := [32]byte{'a'}
    28  	require.NoError(t, e.put(r, s))
    29  
    30  	got, exists, err := e.getByRoot([32]byte{'b'})
    31  	require.NoError(t, err)
    32  	assert.Equal(t, false, exists, "Should not exist")
    33  	assert.Equal(t, (*rootStateInfo)(nil), got, "Should not exist")
    34  
    35  	got, exists, err = e.getByRoot([32]byte{'a'})
    36  	require.NoError(t, err)
    37  	assert.Equal(t, true, exists, "Should exist")
    38  	assert.DeepSSZEqual(t, s.InnerStateUnsafe(), got.state.InnerStateUnsafe(), "Should have the same state")
    39  
    40  	got, exists, err = e.getBySlot(2)
    41  	require.NoError(t, err)
    42  	assert.Equal(t, false, exists, "Should not exist")
    43  	assert.Equal(t, (*rootStateInfo)(nil), got, "Should not exist")
    44  
    45  	got, exists, err = e.getBySlot(1)
    46  	require.NoError(t, err)
    47  	assert.Equal(t, true, exists, "Should exist")
    48  	assert.DeepSSZEqual(t, s.InnerStateUnsafe(), got.state.InnerStateUnsafe(), "Should have the same state")
    49  }
    50  
    51  func TestEpochBoundaryStateCache_CanTrim(t *testing.T) {
    52  	e := newBoundaryStateCache()
    53  	offSet := types.Slot(10)
    54  	for i := types.Slot(0); i < offSet.Add(maxCacheSize); i++ {
    55  		s, err := testutil.NewBeaconState()
    56  		require.NoError(t, err)
    57  		require.NoError(t, s.SetSlot(i))
    58  		r := [32]byte{byte(i)}
    59  		require.NoError(t, e.put(r, s))
    60  	}
    61  
    62  	assert.Equal(t, int(maxCacheSize), len(e.rootStateCache.ListKeys()), "Did not trim to the correct amount")
    63  	assert.Equal(t, int(maxCacheSize), len(e.slotRootCache.ListKeys()), "Did not trim to the correct amount")
    64  	for _, l := range e.rootStateCache.List() {
    65  		i, ok := l.(*rootStateInfo)
    66  		require.Equal(t, true, ok, "Bad type assertion")
    67  		if i.state.Slot() < offSet {
    68  			t.Error("Did not trim the correct state")
    69  		}
    70  	}
    71  }