github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/operations/attestations/kv/forkchoice_test.go (about)

     1  package kv
     2  
     3  import (
     4  	"sort"
     5  	"testing"
     6  
     7  	"github.com/prysmaticlabs/go-bitfield"
     8  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
     9  	"github.com/prysmaticlabs/prysm/shared/testutil"
    10  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
    11  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    12  )
    13  
    14  func TestKV_Forkchoice_CanSaveRetrieve(t *testing.T) {
    15  	cache := NewAttCaches()
    16  
    17  	att1 := testutil.HydrateAttestation(&ethpb.Attestation{Data: &ethpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b1101}})
    18  	att2 := testutil.HydrateAttestation(&ethpb.Attestation{Data: &ethpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b1101}})
    19  	att3 := testutil.HydrateAttestation(&ethpb.Attestation{Data: &ethpb.AttestationData{Slot: 3}, AggregationBits: bitfield.Bitlist{0b1101}})
    20  	atts := []*ethpb.Attestation{att1, att2, att3}
    21  
    22  	for _, att := range atts {
    23  		require.NoError(t, cache.SaveForkchoiceAttestation(att))
    24  	}
    25  
    26  	returned := cache.ForkchoiceAttestations()
    27  
    28  	sort.Slice(returned, func(i, j int) bool {
    29  		return returned[i].Data.Slot < returned[j].Data.Slot
    30  	})
    31  
    32  	assert.DeepEqual(t, atts, returned)
    33  }
    34  
    35  func TestKV_Forkchoice_CanDelete(t *testing.T) {
    36  	cache := NewAttCaches()
    37  
    38  	att1 := testutil.HydrateAttestation(&ethpb.Attestation{Data: &ethpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b1101}})
    39  	att2 := testutil.HydrateAttestation(&ethpb.Attestation{Data: &ethpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b1101}})
    40  	att3 := testutil.HydrateAttestation(&ethpb.Attestation{Data: &ethpb.AttestationData{Slot: 3}, AggregationBits: bitfield.Bitlist{0b1101}})
    41  	atts := []*ethpb.Attestation{att1, att2, att3}
    42  
    43  	for _, att := range atts {
    44  		require.NoError(t, cache.SaveForkchoiceAttestation(att))
    45  	}
    46  
    47  	require.NoError(t, cache.DeleteForkchoiceAttestation(att1))
    48  	require.NoError(t, cache.DeleteForkchoiceAttestation(att3))
    49  
    50  	returned := cache.ForkchoiceAttestations()
    51  	wanted := []*ethpb.Attestation{att2}
    52  	assert.DeepEqual(t, wanted, returned)
    53  }
    54  
    55  func TestKV_Forkchoice_CanCount(t *testing.T) {
    56  	cache := NewAttCaches()
    57  
    58  	att1 := testutil.HydrateAttestation(&ethpb.Attestation{Data: &ethpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b1101}})
    59  	att2 := testutil.HydrateAttestation(&ethpb.Attestation{Data: &ethpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b1101}})
    60  	att3 := testutil.HydrateAttestation(&ethpb.Attestation{Data: &ethpb.AttestationData{Slot: 3}, AggregationBits: bitfield.Bitlist{0b1101}})
    61  	atts := []*ethpb.Attestation{att1, att2, att3}
    62  
    63  	for _, att := range atts {
    64  		require.NoError(t, cache.SaveForkchoiceAttestation(att))
    65  	}
    66  
    67  	require.Equal(t, 3, cache.ForkchoiceAttestationCount())
    68  }