-
Notifications
You must be signed in to change notification settings - Fork 917
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Send Slashing Objects to Beacon Node via RPC (#4866)
* submit slashing objects * tests complete
- Loading branch information
1 parent
4a44632
commit 38fed73
Showing
6 changed files
with
202 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package beaconclient | ||
|
||
import ( | ||
"context" | ||
|
||
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" | ||
"github.com/sirupsen/logrus" | ||
"go.opencensus.io/trace" | ||
) | ||
|
||
// subscribeDetectedProposerSlashings subscribes to an event feed for | ||
// slashing objects from the slasher runtime. Upon receiving | ||
// a proposer slashing from the feed, we submit the object to the | ||
// connected beacon node via a client RPC. | ||
func (bs *Service) subscribeDetectedProposerSlashings(ctx context.Context, ch chan *ethpb.ProposerSlashing) { | ||
ctx, span := trace.StartSpan(ctx, "beaconclient.submitProposerSlashing") | ||
defer span.End() | ||
stateSub := bs.proposerSlashingsFeed.Subscribe(ch) | ||
defer stateSub.Unsubscribe() | ||
for { | ||
select { | ||
case slashing := <-ch: | ||
if _, err := bs.client.SubmitProposerSlashing(ctx, slashing); err != nil { | ||
log.Error(err) | ||
} | ||
case <-stateSub.Err(): | ||
logrus.Error("Subscriber closed, exiting goroutine") | ||
return | ||
case <-ctx.Done(): | ||
logrus.Error("Context canceled") | ||
return | ||
} | ||
} | ||
} | ||
|
||
// subscribeDetectedAttesterSlashings subscribes to an event feed for | ||
// slashing objects from the slasher runtime. Upon receiving an | ||
// attester slashing from the feed, we submit the object to the | ||
// connected beacon node via a client RPC. | ||
func (bs *Service) subscribeDetectedAttesterSlashings(ctx context.Context, ch chan *ethpb.AttesterSlashing) { | ||
ctx, span := trace.StartSpan(ctx, "beaconclient.submitAttesterSlashing") | ||
defer span.End() | ||
stateSub := bs.attesterSlashingsFeed.Subscribe(ch) | ||
defer stateSub.Unsubscribe() | ||
for { | ||
select { | ||
case slashing := <-ch: | ||
if _, err := bs.client.SubmitAttesterSlashing(ctx, slashing); err != nil { | ||
log.Error(err) | ||
} | ||
case <-stateSub.Err(): | ||
logrus.Error("Subscriber closed, exiting goroutine") | ||
return | ||
case <-ctx.Done(): | ||
logrus.Error("Context canceled") | ||
return | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package beaconclient | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" | ||
"github.com/prysmaticlabs/prysm/shared/event" | ||
"github.com/prysmaticlabs/prysm/shared/mock" | ||
"github.com/prysmaticlabs/prysm/shared/testutil" | ||
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{ | ||
client: client, | ||
proposerSlashingsFeed: new(event.Feed), | ||
} | ||
|
||
slashing := ðpb.ProposerSlashing{ | ||
ProposerIndex: 5, | ||
Header_1: ðpb.SignedBeaconBlockHeader{ | ||
Header: ðpb.BeaconBlockHeader{ | ||
Slot: 5, | ||
}, | ||
Signature: make([]byte, 96), | ||
}, | ||
Header_2: ðpb.SignedBeaconBlockHeader{ | ||
Header: ðpb.BeaconBlockHeader{ | ||
Slot: 5, | ||
}, | ||
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 | ||
testutil.AssertLogsContain(t, hook, "Context canceled") | ||
} | ||
|
||
func TestService_SubscribeDetectedAttesterSlashings(t *testing.T) { | ||
hook := logTest.NewGlobal() | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
client := mock.NewMockBeaconChainClient(ctrl) | ||
|
||
bs := Service{ | ||
client: client, | ||
attesterSlashingsFeed: new(event.Feed), | ||
} | ||
|
||
slashing := ðpb.AttesterSlashing{ | ||
Attestation_1: ðpb.IndexedAttestation{ | ||
AttestingIndices: []uint64{1, 2, 3}, | ||
Data: nil, | ||
}, | ||
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 | ||
testutil.AssertLogsContain(t, hook, "Context canceled") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters