github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/sync/subscriber_beacon_blocks_test.go (about)

     1  package sync
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/prysmaticlabs/go-bitfield"
     8  	chainMock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
     9  	"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
    10  	dbtest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
    11  	"github.com/prysmaticlabs/prysm/beacon-chain/operations/attestations"
    12  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
    13  	"github.com/prysmaticlabs/prysm/shared/testutil"
    14  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
    15  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    16  	"google.golang.org/protobuf/proto"
    17  )
    18  
    19  func TestDeleteAttsInPool(t *testing.T) {
    20  	r := &Service{
    21  		cfg: &Config{AttPool: attestations.NewPool()},
    22  	}
    23  
    24  	att1 := testutil.HydrateAttestation(&ethpb.Attestation{AggregationBits: bitfield.Bitlist{0b1101}})
    25  	att2 := testutil.HydrateAttestation(&ethpb.Attestation{AggregationBits: bitfield.Bitlist{0b1110}})
    26  	att3 := testutil.HydrateAttestation(&ethpb.Attestation{AggregationBits: bitfield.Bitlist{0b1011}})
    27  	att4 := testutil.HydrateAttestation(&ethpb.Attestation{AggregationBits: bitfield.Bitlist{0b1001}})
    28  	require.NoError(t, r.cfg.AttPool.SaveAggregatedAttestation(att1))
    29  	require.NoError(t, r.cfg.AttPool.SaveAggregatedAttestation(att2))
    30  	require.NoError(t, r.cfg.AttPool.SaveAggregatedAttestation(att3))
    31  	require.NoError(t, r.cfg.AttPool.SaveUnaggregatedAttestation(att4))
    32  
    33  	// Seen 1, 3 and 4 in block.
    34  	require.NoError(t, r.deleteAttsInPool([]*ethpb.Attestation{att1, att3, att4}))
    35  
    36  	// Only 2 should remain.
    37  	assert.DeepEqual(t, []*ethpb.Attestation{att2}, r.cfg.AttPool.AggregatedAttestations(), "Did not get wanted attestations")
    38  }
    39  
    40  func TestService_beaconBlockSubscriber(t *testing.T) {
    41  	pooledAttestations := []*ethpb.Attestation{
    42  		// Aggregated.
    43  		testutil.HydrateAttestation(&ethpb.Attestation{AggregationBits: bitfield.Bitlist{0b00011111}}),
    44  		// Unaggregated.
    45  		testutil.HydrateAttestation(&ethpb.Attestation{AggregationBits: bitfield.Bitlist{0b00010001}}),
    46  	}
    47  
    48  	type args struct {
    49  		msg proto.Message
    50  	}
    51  	tests := []struct {
    52  		name      string
    53  		args      args
    54  		wantedErr string
    55  		check     func(*testing.T, *Service)
    56  	}{
    57  		{
    58  			name: "invalid block does not remove attestations",
    59  			args: args{
    60  				msg: func() *ethpb.SignedBeaconBlock {
    61  					b := testutil.NewBeaconBlock()
    62  					b.Block.Body.Attestations = pooledAttestations
    63  					return b
    64  				}(),
    65  			},
    66  			wantedErr: "nil inner state",
    67  			check: func(t *testing.T, s *Service) {
    68  				if s.cfg.AttPool.AggregatedAttestationCount() == 0 {
    69  					t.Error("Expected at least 1 aggregated attestation in the pool")
    70  				}
    71  				if s.cfg.AttPool.UnaggregatedAttestationCount() == 0 {
    72  					t.Error("Expected at least 1 unaggregated attestation in the pool")
    73  				}
    74  			},
    75  		},
    76  	}
    77  	for _, tt := range tests {
    78  		t.Run(tt.name, func(t *testing.T) {
    79  			db := dbtest.SetupDB(t)
    80  			s := &Service{
    81  				cfg: &Config{
    82  					Chain: &chainMock.ChainService{
    83  						DB:   db,
    84  						Root: make([]byte, 32),
    85  					},
    86  					AttPool: attestations.NewPool(),
    87  				},
    88  			}
    89  			assert.NoError(t, s.initCaches())
    90  			// Set up attestation pool.
    91  			for _, att := range pooledAttestations {
    92  				if helpers.IsAggregated(att) {
    93  					assert.NoError(t, s.cfg.AttPool.SaveAggregatedAttestation(att))
    94  				} else {
    95  					assert.NoError(t, s.cfg.AttPool.SaveUnaggregatedAttestation(att))
    96  				}
    97  			}
    98  			// Perform method under test call.
    99  			err := s.beaconBlockSubscriber(context.Background(), tt.args.msg)
   100  			if tt.wantedErr != "" {
   101  				assert.ErrorContains(t, tt.wantedErr, err)
   102  			} else {
   103  				assert.NoError(t, err)
   104  			}
   105  			if tt.check != nil {
   106  				tt.check(t, s)
   107  			}
   108  		})
   109  	}
   110  }