2020-02-14 18:11:15 +00:00
|
|
|
package beaconclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/golang/mock/gomock"
|
2021-07-21 21:34:07 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2020-02-14 18:11:15 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/event"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/mock"
|
2020-08-13 16:22:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2020-02-14 18:11:15 +00:00
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestService_SubscribeDetectedProposerSlashings(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
client := mock.NewMockBeaconChainClient(ctrl)
|
|
|
|
|
|
|
|
bs := Service{
|
2021-03-21 17:53:17 +00:00
|
|
|
cfg: &Config{
|
|
|
|
BeaconClient: client,
|
|
|
|
ProposerSlashingsFeed: new(event.Feed),
|
|
|
|
},
|
2020-02-14 18:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
slashing := ðpb.ProposerSlashing{
|
|
|
|
Header_1: ðpb.SignedBeaconBlockHeader{
|
|
|
|
Header: ðpb.BeaconBlockHeader{
|
2020-04-14 20:27:03 +00:00
|
|
|
ProposerIndex: 5,
|
|
|
|
Slot: 5,
|
2020-02-14 18:11:15 +00:00
|
|
|
},
|
|
|
|
Signature: make([]byte, 96),
|
|
|
|
},
|
|
|
|
Header_2: ðpb.SignedBeaconBlockHeader{
|
|
|
|
Header: ðpb.BeaconBlockHeader{
|
2020-04-14 20:27:03 +00:00
|
|
|
ProposerIndex: 5,
|
|
|
|
Slot: 5,
|
2020-02-14 18:11:15 +00:00
|
|
|
},
|
|
|
|
Signature: make([]byte, 96),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
exitRoutine := make(chan bool)
|
|
|
|
slashingsChan := make(chan *ethpb.ProposerSlashing)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
client.EXPECT().SubmitProposerSlashing(gomock.Any(), slashing)
|
|
|
|
go func(tt *testing.T) {
|
|
|
|
bs.subscribeDetectedProposerSlashings(ctx, slashingsChan)
|
|
|
|
<-exitRoutine
|
|
|
|
}(t)
|
|
|
|
slashingsChan <- slashing
|
|
|
|
cancel()
|
|
|
|
exitRoutine <- true
|
2020-08-13 16:22:25 +00:00
|
|
|
require.LogsContain(t, hook, "Context canceled")
|
2020-02-14 18:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_SubscribeDetectedAttesterSlashings(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
client := mock.NewMockBeaconChainClient(ctrl)
|
|
|
|
|
|
|
|
bs := Service{
|
2021-03-21 17:53:17 +00:00
|
|
|
cfg: &Config{
|
|
|
|
BeaconClient: client,
|
|
|
|
AttesterSlashingsFeed: new(event.Feed),
|
|
|
|
},
|
2020-02-14 18:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
slashing := ðpb.AttesterSlashing{
|
|
|
|
Attestation_1: ðpb.IndexedAttestation{
|
|
|
|
AttestingIndices: []uint64{1, 2, 3},
|
2020-05-22 15:19:10 +00:00
|
|
|
Data: ðpb.AttestationData{
|
|
|
|
Source: ðpb.Checkpoint{
|
|
|
|
Epoch: 3,
|
|
|
|
},
|
|
|
|
Target: ðpb.Checkpoint{
|
|
|
|
Epoch: 4,
|
|
|
|
},
|
|
|
|
},
|
2020-02-14 18:11:15 +00:00
|
|
|
},
|
|
|
|
Attestation_2: ðpb.IndexedAttestation{
|
|
|
|
AttestingIndices: []uint64{3, 4, 5},
|
|
|
|
Data: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
exitRoutine := make(chan bool)
|
|
|
|
slashingsChan := make(chan *ethpb.AttesterSlashing)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
client.EXPECT().SubmitAttesterSlashing(gomock.Any(), slashing)
|
|
|
|
go func(tt *testing.T) {
|
|
|
|
bs.subscribeDetectedAttesterSlashings(ctx, slashingsChan)
|
|
|
|
<-exitRoutine
|
|
|
|
}(t)
|
|
|
|
slashingsChan <- slashing
|
|
|
|
cancel()
|
|
|
|
exitRoutine <- true
|
2020-08-13 16:22:25 +00:00
|
|
|
require.LogsContain(t, hook, "Context canceled")
|
2020-02-14 18:11:15 +00:00
|
|
|
}
|