github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/operations/attestations/kv/block_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_BlockAttestation_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.SaveBlockAttestation(att))
    24  	}
    25  	// Diff bit length should not panic.
    26  	att4 := testutil.HydrateAttestation(&ethpb.Attestation{Data: &ethpb.AttestationData{Slot: 3}, AggregationBits: bitfield.Bitlist{0b11011}})
    27  	if err := cache.SaveBlockAttestation(att4); err != bitfield.ErrBitlistDifferentLength {
    28  		t.Errorf("Unexpected error: wanted %v, got %v", bitfield.ErrBitlistDifferentLength, err)
    29  	}
    30  
    31  	returned := cache.BlockAttestations()
    32  
    33  	sort.Slice(returned, func(i, j int) bool {
    34  		return returned[i].Data.Slot < returned[j].Data.Slot
    35  	})
    36  
    37  	assert.DeepEqual(t, atts, returned)
    38  }
    39  
    40  func TestKV_BlockAttestation_CanDelete(t *testing.T) {
    41  	cache := NewAttCaches()
    42  
    43  	att1 := testutil.HydrateAttestation(&ethpb.Attestation{Data: &ethpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b1101}})
    44  	att2 := testutil.HydrateAttestation(&ethpb.Attestation{Data: &ethpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b1101}})
    45  	att3 := testutil.HydrateAttestation(&ethpb.Attestation{Data: &ethpb.AttestationData{Slot: 3}, AggregationBits: bitfield.Bitlist{0b1101}})
    46  	atts := []*ethpb.Attestation{att1, att2, att3}
    47  
    48  	for _, att := range atts {
    49  		require.NoError(t, cache.SaveBlockAttestation(att))
    50  	}
    51  
    52  	require.NoError(t, cache.DeleteBlockAttestation(att1))
    53  	require.NoError(t, cache.DeleteBlockAttestation(att3))
    54  
    55  	returned := cache.BlockAttestations()
    56  	wanted := []*ethpb.Attestation{att2}
    57  	assert.DeepEqual(t, wanted, returned)
    58  }