github.com/prysmaticlabs/prysm@v1.4.4/slasher/cache/highest_attestation_cache_test.go (about)

     1  package cache
     2  
     3  import (
     4  	"testing"
     5  
     6  	types "github.com/prysmaticlabs/eth2-types"
     7  	ethereum_slashing "github.com/prysmaticlabs/prysm/proto/slashing"
     8  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
     9  )
    10  
    11  func TestStoringAndFetching(t *testing.T) {
    12  	cache, err := NewHighestAttestationCache(10, nil)
    13  	require.NoError(t, err)
    14  
    15  	// Cache a test attestation.
    16  	cache.Set(1, &ethereum_slashing.HighestAttestation{
    17  		ValidatorId:        1,
    18  		HighestSourceEpoch: 2,
    19  		HighestTargetEpoch: 3,
    20  	})
    21  
    22  	// Require it to exist.
    23  	require.Equal(t, true, cache.Has(1))
    24  
    25  	// fetch
    26  	res, b := cache.Get(1)
    27  	require.Equal(t, true, b)
    28  	require.Equal(t, uint64(1), res[1].ValidatorId)
    29  	require.Equal(t, types.Epoch(2), res[1].HighestSourceEpoch)
    30  	require.Equal(t, types.Epoch(3), res[1].HighestTargetEpoch)
    31  
    32  	// Delete it.
    33  	require.Equal(t, true, cache.Delete(1))
    34  	// Confirm deletion.
    35  	res2, b2 := cache.Get(1)
    36  	require.Equal(t, false, b2)
    37  	require.Equal(t, true, res2 == nil)
    38  }
    39  
    40  func TestPurge(t *testing.T) {
    41  	wasEvicted := false
    42  	onEvicted := func(key interface{}, value interface{}) {
    43  		wasEvicted = true
    44  	}
    45  	cache, err := NewHighestAttestationCache(10, onEvicted)
    46  	require.NoError(t, err)
    47  
    48  	// Cache several test attestation.
    49  	cache.Set(1, &ethereum_slashing.HighestAttestation{
    50  		ValidatorId:        1,
    51  		HighestSourceEpoch: 2,
    52  		HighestTargetEpoch: 3,
    53  	})
    54  	cache.Set(2, &ethereum_slashing.HighestAttestation{
    55  		ValidatorId:        4,
    56  		HighestSourceEpoch: 5,
    57  		HighestTargetEpoch: 6,
    58  	})
    59  	cache.Set(3, &ethereum_slashing.HighestAttestation{
    60  		ValidatorId:        7,
    61  		HighestSourceEpoch: 8,
    62  		HighestTargetEpoch: 9,
    63  	})
    64  
    65  	cache.Purge()
    66  
    67  	// Require all attestations to be deleted
    68  	require.Equal(t, false, cache.Has(1))
    69  	require.Equal(t, false, cache.Has(2))
    70  	require.Equal(t, false, cache.Has(3))
    71  
    72  	// Require the eviction function to be called.
    73  	require.Equal(t, true, wasEvicted)
    74  }
    75  
    76  func TestClear(t *testing.T) {
    77  	wasEvicted := false
    78  	onEvicted := func(key interface{}, value interface{}) {
    79  		wasEvicted = true
    80  	}
    81  	cache, err := NewHighestAttestationCache(10, onEvicted)
    82  	require.NoError(t, err)
    83  
    84  	// Cache several test attestation.
    85  	cache.Set(1, &ethereum_slashing.HighestAttestation{
    86  		ValidatorId:        1,
    87  		HighestSourceEpoch: 2,
    88  		HighestTargetEpoch: 3,
    89  	})
    90  	cache.Set(2, &ethereum_slashing.HighestAttestation{
    91  		ValidatorId:        4,
    92  		HighestSourceEpoch: 5,
    93  		HighestTargetEpoch: 6,
    94  	})
    95  	cache.Set(3, &ethereum_slashing.HighestAttestation{
    96  		ValidatorId:        7,
    97  		HighestSourceEpoch: 8,
    98  		HighestTargetEpoch: 9,
    99  	})
   100  
   101  	cache.Clear()
   102  
   103  	// Require all attestations to be deleted
   104  	require.Equal(t, false, cache.Has(1))
   105  	require.Equal(t, false, cache.Has(2))
   106  	require.Equal(t, false, cache.Has(3))
   107  
   108  	// Require the eviction function to be called.
   109  	require.Equal(t, true, wasEvicted)
   110  }