github.com/prysmaticlabs/prysm@v1.4.4/slasher/beaconclient/submit_test.go (about) 1 package beaconclient 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/golang/mock/gomock" 8 ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" 9 "github.com/prysmaticlabs/prysm/shared/event" 10 "github.com/prysmaticlabs/prysm/shared/mock" 11 "github.com/prysmaticlabs/prysm/shared/testutil/require" 12 logTest "github.com/sirupsen/logrus/hooks/test" 13 ) 14 15 func TestService_SubscribeDetectedProposerSlashings(t *testing.T) { 16 hook := logTest.NewGlobal() 17 ctrl := gomock.NewController(t) 18 defer ctrl.Finish() 19 client := mock.NewMockBeaconChainClient(ctrl) 20 21 bs := Service{ 22 cfg: &Config{ 23 BeaconClient: client, 24 ProposerSlashingsFeed: new(event.Feed), 25 }, 26 } 27 28 slashing := ðpb.ProposerSlashing{ 29 Header_1: ðpb.SignedBeaconBlockHeader{ 30 Header: ðpb.BeaconBlockHeader{ 31 ProposerIndex: 5, 32 Slot: 5, 33 }, 34 Signature: make([]byte, 96), 35 }, 36 Header_2: ðpb.SignedBeaconBlockHeader{ 37 Header: ðpb.BeaconBlockHeader{ 38 ProposerIndex: 5, 39 Slot: 5, 40 }, 41 Signature: make([]byte, 96), 42 }, 43 } 44 45 exitRoutine := make(chan bool) 46 slashingsChan := make(chan *ethpb.ProposerSlashing) 47 ctx, cancel := context.WithCancel(context.Background()) 48 client.EXPECT().SubmitProposerSlashing(gomock.Any(), slashing) 49 go func(tt *testing.T) { 50 bs.subscribeDetectedProposerSlashings(ctx, slashingsChan) 51 <-exitRoutine 52 }(t) 53 slashingsChan <- slashing 54 cancel() 55 exitRoutine <- true 56 require.LogsContain(t, hook, "Context canceled") 57 } 58 59 func TestService_SubscribeDetectedAttesterSlashings(t *testing.T) { 60 hook := logTest.NewGlobal() 61 ctrl := gomock.NewController(t) 62 defer ctrl.Finish() 63 client := mock.NewMockBeaconChainClient(ctrl) 64 65 bs := Service{ 66 cfg: &Config{ 67 BeaconClient: client, 68 AttesterSlashingsFeed: new(event.Feed), 69 }, 70 } 71 72 slashing := ðpb.AttesterSlashing{ 73 Attestation_1: ðpb.IndexedAttestation{ 74 AttestingIndices: []uint64{1, 2, 3}, 75 Data: ðpb.AttestationData{ 76 Source: ðpb.Checkpoint{ 77 Epoch: 3, 78 }, 79 Target: ðpb.Checkpoint{ 80 Epoch: 4, 81 }, 82 }, 83 }, 84 Attestation_2: ðpb.IndexedAttestation{ 85 AttestingIndices: []uint64{3, 4, 5}, 86 Data: nil, 87 }, 88 } 89 90 exitRoutine := make(chan bool) 91 slashingsChan := make(chan *ethpb.AttesterSlashing) 92 ctx, cancel := context.WithCancel(context.Background()) 93 client.EXPECT().SubmitAttesterSlashing(gomock.Any(), slashing) 94 go func(tt *testing.T) { 95 bs.subscribeDetectedAttesterSlashings(ctx, slashingsChan) 96 <-exitRoutine 97 }(t) 98 slashingsChan <- slashing 99 cancel() 100 exitRoutine <- true 101 require.LogsContain(t, hook, "Context canceled") 102 }