Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* visibility added * register in node * fixed up imports * include detection listeners for feed * subscribe to blocks and todos * tests passing * todos * pkg comment
- Loading branch information
1 parent
38fed73
commit 868c8f5
Showing
15 changed files
with
288 additions
and
37 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
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,32 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "go_default_library", | ||
srcs = ["attestations.go"], | ||
importpath = "github.com/prysmaticlabs/prysm/slasher/detection/attestations", | ||
visibility = ["//slasher:__subpackages__"], | ||
deps = [ | ||
"//proto/slashing:go_default_library", | ||
"//shared/params:go_default_library", | ||
"@com_github_pkg_errors//:go_default_library", | ||
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", | ||
"@io_opencensus_go//trace:go_default_library", | ||
], | ||
) | ||
|
||
go_test( | ||
name = "go_default_test", | ||
srcs = [ | ||
"attestation_test.go", | ||
"attestations_bench_test.go", | ||
], | ||
embed = [":go_default_library"], | ||
deps = [ | ||
"//proto/slashing:go_default_library", | ||
"//shared/params:go_default_library", | ||
"//slasher/db/testing:go_default_library", | ||
"//slasher/flags:go_default_library", | ||
"@com_github_gogo_protobuf//proto:go_default_library", | ||
"@com_github_urfave_cli//:go_default_library", | ||
], | ||
) |
2 changes: 1 addition & 1 deletion
2
slasher/detection/attestation_test.go → ...etection/attestations/attestation_test.go
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package detection | ||
package attestations | ||
|
||
import ( | ||
"context" | ||
|
2 changes: 1 addition & 1 deletion
2
slasher/detection/attestations.go → ...er/detection/attestations/attestations.go
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package detection | ||
package attestations | ||
|
||
import ( | ||
"context" | ||
|
2 changes: 1 addition & 1 deletion
2
slasher/detection/attestations_bench_test.go → ...n/attestations/attestations_bench_test.go
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package detection | ||
package attestations | ||
|
||
import ( | ||
"context" | ||
|
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,63 @@ | ||
/* | ||
Package detection defines a service that reacts to incoming blocks/attestations | ||
by running slashing detection for double proposals, double votes, and surround votes | ||
according to the eth2 specification. As soon as slashing objects are found, they are | ||
sent over a feed for the beaconclient service to submit to a beacon node via gRPC. | ||
*/ | ||
package detection | ||
|
||
import ( | ||
"context" | ||
|
||
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" | ||
"go.opencensus.io/trace" | ||
) | ||
|
||
// detectIncomingBlocks subscribes to an event feed for | ||
// block objects from a notifier interface. Upon receiving | ||
// a signed beacon block from the feed, we run proposer slashing | ||
// detection on the block. | ||
func (ds *Service) detectIncomingBlocks(ctx context.Context, ch chan *ethpb.SignedBeaconBlock) { | ||
ctx, span := trace.StartSpan(ctx, "detection.detectIncomingBlocks") | ||
defer span.End() | ||
sub := ds.notifier.BlockFeed().Subscribe(ch) | ||
defer sub.Unsubscribe() | ||
for { | ||
select { | ||
case <-ch: | ||
log.Infof("Running detection on block...") | ||
// TODO(#4836): Run detection function for proposer slashings. | ||
case <-sub.Err(): | ||
log.Error("Subscriber closed, exiting goroutine") | ||
return | ||
case <-ctx.Done(): | ||
log.Error("Context canceled") | ||
return | ||
} | ||
} | ||
} | ||
|
||
// detectIncomingAttestations subscribes to an event feed for | ||
// attestation objects from a notifier interface. Upon receiving | ||
// an attestation from the feed, we run surround vote and double vote | ||
// detection on the attestation. | ||
func (ds *Service) detectIncomingAttestations(ctx context.Context, ch chan *ethpb.Attestation) { | ||
ctx, span := trace.StartSpan(ctx, "detection.detectIncomingAttestations") | ||
defer span.End() | ||
sub := ds.notifier.AttestationFeed().Subscribe(ch) | ||
defer sub.Unsubscribe() | ||
for { | ||
select { | ||
case <-ch: | ||
log.Infof("Running detection on attestation...") | ||
// TODO(#4836): Run detection function for attester double voting. | ||
// TODO(#4836): Run detection function for attester surround voting. | ||
case <-sub.Err(): | ||
log.Error("Subscriber closed, exiting goroutine") | ||
return | ||
case <-ctx.Done(): | ||
log.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,68 @@ | ||
package detection | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" | ||
"github.com/prysmaticlabs/prysm/shared/event" | ||
"github.com/prysmaticlabs/prysm/shared/testutil" | ||
logTest "github.com/sirupsen/logrus/hooks/test" | ||
) | ||
|
||
type mockNotifier struct{} | ||
|
||
func (m *mockNotifier) BlockFeed() *event.Feed { | ||
return new(event.Feed) | ||
} | ||
|
||
func (m *mockNotifier) AttestationFeed() *event.Feed { | ||
return new(event.Feed) | ||
} | ||
|
||
func TestService_DetectIncomingBlocks(t *testing.T) { | ||
hook := logTest.NewGlobal() | ||
ds := Service{ | ||
notifier: &mockNotifier{}, | ||
} | ||
blk := ðpb.SignedBeaconBlock{ | ||
Block: ðpb.BeaconBlock{Slot: 1}, | ||
Signature: make([]byte, 96), | ||
} | ||
exitRoutine := make(chan bool) | ||
blocksChan := make(chan *ethpb.SignedBeaconBlock) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
go func(tt *testing.T) { | ||
ds.detectIncomingBlocks(ctx, blocksChan) | ||
<-exitRoutine | ||
}(t) | ||
blocksChan <- blk | ||
cancel() | ||
exitRoutine <- true | ||
testutil.AssertLogsContain(t, hook, "Running detection on block") | ||
testutil.AssertLogsContain(t, hook, "Context canceled") | ||
} | ||
|
||
func TestService_DetectIncomingAttestations(t *testing.T) { | ||
hook := logTest.NewGlobal() | ||
ds := Service{ | ||
notifier: &mockNotifier{}, | ||
} | ||
att := ðpb.Attestation{ | ||
Data: ðpb.AttestationData{ | ||
Slot: 1, | ||
}, | ||
} | ||
exitRoutine := make(chan bool) | ||
attsChan := make(chan *ethpb.Attestation) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
go func(tt *testing.T) { | ||
ds.detectIncomingAttestations(ctx, attsChan) | ||
<-exitRoutine | ||
}(t) | ||
attsChan <- att | ||
cancel() | ||
exitRoutine <- true | ||
testutil.AssertLogsContain(t, hook, "Running detection on attestation") | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package detection | ||
|
||
import ( | ||
"context" | ||
|
||
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" | ||
"github.com/prysmaticlabs/prysm/shared/event" | ||
"github.com/prysmaticlabs/prysm/slasher/beaconclient" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
var log = logrus.WithField("prefix", "detection") | ||
|
||
// Service struct for the detection service of the slasher. | ||
type Service struct { | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
blocksChan chan *ethpb.SignedBeaconBlock | ||
attsChan chan *ethpb.Attestation | ||
notifier beaconclient.Notifier | ||
attesterSlashingsFeed *event.Feed | ||
proposerSlashingsFeed *event.Feed | ||
} | ||
|
||
// Config options for the detection service. | ||
type Config struct { | ||
Notifier beaconclient.Notifier | ||
AttesterSlashingsFeed *event.Feed | ||
ProposerSlashingsFeed *event.Feed | ||
} | ||
|
||
// NewDetectionService instantiation. | ||
func NewDetectionService(ctx context.Context, cfg *Config) *Service { | ||
ctx, cancel := context.WithCancel(ctx) | ||
return &Service{ | ||
ctx: ctx, | ||
cancel: cancel, | ||
notifier: cfg.Notifier, | ||
blocksChan: make(chan *ethpb.SignedBeaconBlock, 1), | ||
attsChan: make(chan *ethpb.Attestation, 1), | ||
attesterSlashingsFeed: cfg.AttesterSlashingsFeed, | ||
proposerSlashingsFeed: cfg.ProposerSlashingsFeed, | ||
} | ||
} | ||
|
||
// Stop the notifier service. | ||
func (ds *Service) Stop() error { | ||
ds.cancel() | ||
log.Info("Stopping service") | ||
return nil | ||
} | ||
|
||
// Status returns an error if there exists an error in | ||
// the notifier service. | ||
func (ds *Service) Status() error { | ||
return nil | ||
} | ||
|
||
// Start the detection service runtime. | ||
func (ds *Service) Start() { | ||
go ds.detectIncomingBlocks(ds.ctx, ds.blocksChan) | ||
go ds.detectIncomingAttestations(ds.ctx, ds.attsChan) | ||
} |
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
Oops, something went wrong.