Skip to content

Commit

Permalink
Include Slashing Submission Endpoints + Slashing Pool in Beacon Node (#…
Browse files Browse the repository at this point in the history
…4858)

* add to workspace

* impl

* include tests for func

* fix broken build

* test passing, found 2 bugs

* add errors package

* added in mockgen

* we check for insertion into the pool based on attester slashings

* test passing

* proper test

* Update beacon-chain/rpc/beacon/slashings.go

* Update beacon-chain/rpc/beacon/slashings_test.go
  • Loading branch information
rauljordan committed Feb 14, 2020
1 parent 27ec40f commit 549b0f6
Show file tree
Hide file tree
Showing 16 changed files with 330 additions and 46 deletions.
2 changes: 1 addition & 1 deletion WORKSPACE
Expand Up @@ -1272,7 +1272,7 @@ go_repository(

go_repository(
name = "com_github_prysmaticlabs_ethereumapis",
commit = "79d7a99b999d1873431b2ae56f1e6ea6e730242f",
commit = "b7452dde4ca361809def4ed5924ab3cb7ad1299a",
importpath = "github.com/prysmaticlabs/ethereumapis",
patch_args = ["-p1"],
patches = [
Expand Down
1 change: 1 addition & 0 deletions beacon-chain/node/BUILD.bazel
Expand Up @@ -19,6 +19,7 @@ go_library(
"//beacon-chain/gateway:go_default_library",
"//beacon-chain/interop-cold-start:go_default_library",
"//beacon-chain/operations/attestations:go_default_library",
"//beacon-chain/operations/slashings:go_default_library",
"//beacon-chain/operations/voluntaryexits:go_default_library",
"//beacon-chain/p2p:go_default_library",
"//beacon-chain/powchain:go_default_library",
Expand Down
4 changes: 4 additions & 0 deletions beacon-chain/node/node.go
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/gateway"
interopcoldstart "github.com/prysmaticlabs/prysm/beacon-chain/interop-cold-start"
"github.com/prysmaticlabs/prysm/beacon-chain/operations/attestations"
"github.com/prysmaticlabs/prysm/beacon-chain/operations/slashings"
"github.com/prysmaticlabs/prysm/beacon-chain/operations/voluntaryexits"
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
"github.com/prysmaticlabs/prysm/beacon-chain/powchain"
Expand Down Expand Up @@ -62,6 +63,7 @@ type BeaconNode struct {
db db.Database
attestationPool attestations.Pool
exitPool *voluntaryexits.Pool
slashingsPool *slashings.Pool
depositCache *depositcache.DepositCache
stateFeed *event.Feed
blockFeed *event.Feed
Expand Down Expand Up @@ -109,6 +111,7 @@ func NewBeaconNode(ctx *cli.Context) (*BeaconNode, error) {
opFeed: new(event.Feed),
attestationPool: attestations.NewPool(),
exitPool: voluntaryexits.NewPool(),
slashingsPool: slashings.NewPool(),
}

if err := beacon.startDB(ctx); err != nil {
Expand Down Expand Up @@ -497,6 +500,7 @@ func (b *BeaconNode) registerRPCService(ctx *cli.Context) error {
GenesisTimeFetcher: chainService,
AttestationsPool: b.attestationPool,
ExitPool: b.exitPool,
SlashingsPool: b.slashingsPool,
POWChainService: web3Service,
ChainStartFetcher: chainStartFetcher,
MockEth1Votes: mockEth1DataVotes,
Expand Down
14 changes: 6 additions & 8 deletions beacon-chain/operations/slashings/service.go
@@ -1,6 +1,8 @@
package slashings

import (
"errors"
"fmt"
"sort"

ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
Expand Down Expand Up @@ -77,7 +79,7 @@ func (p *Pool) InsertAttesterSlashing(state *beaconstate.BeaconState, slashing *
// has been recently included in the pool of slashings, do not process this new
// slashing.
if !ok {
return nil
return fmt.Errorf("validator at index %d cannot be slashed", val)
}

// Check if the validator already exists in the list of slashings.
Expand Down Expand Up @@ -117,7 +119,7 @@ func (p *Pool) InsertProposerSlashing(state *beaconstate.BeaconState, slashing *
// has been recently included in the pool of slashings, do not process this new
// slashing.
if !ok {
return nil
return fmt.Errorf("validator at index %d cannot be slashed", idx)
}

// Check if the validator already exists in the list of slashings.
Expand All @@ -126,7 +128,7 @@ func (p *Pool) InsertProposerSlashing(state *beaconstate.BeaconState, slashing *
return p.pendingProposerSlashing[i].ProposerIndex == slashing.ProposerIndex
})
if found != len(p.pendingProposerSlashing) {
return nil
return errors.New("slashing object already exists in pending proposer slashings")
}

// Insert into pending list and sort again.
Expand Down Expand Up @@ -194,11 +196,7 @@ func (p *Pool) validatorSlashingPreconditionCheck(
return false, nil
}
// Checking if the validator has already been slashed.
slashedValidators := state.Slashings()
found := sort.Search(len(slashedValidators), func(i int) bool {
return slashedValidators[i] == valIdx
})
if found != len(slashedValidators) {
if validator.Slashed() {
return false, nil
}
return true, nil
Expand Down
24 changes: 17 additions & 7 deletions beacon-chain/operations/slashings/service_attester_test.go
Expand Up @@ -2,6 +2,7 @@ package slashings

import (
"reflect"
"strings"
"testing"

"github.com/gogo/protobuf/proto"
Expand Down Expand Up @@ -52,6 +53,8 @@ func TestPool_InsertAttesterSlashing(t *testing.T) {
type fields struct {
pending []*PendingAttesterSlashing
included map[uint64]bool
wantErr bool
err string
}
type args struct {
slashing *ethpb.AttesterSlashing
Expand Down Expand Up @@ -179,7 +182,7 @@ func TestPool_InsertAttesterSlashing(t *testing.T) {
},
},
{
name: "Slashing for exited validator ",
name: "Slashing for exited validator",
fields: fields{
pending: []*PendingAttesterSlashing{},
included: make(map[uint64]bool),
Expand All @@ -190,7 +193,7 @@ func TestPool_InsertAttesterSlashing(t *testing.T) {
want: []*PendingAttesterSlashing{},
},
{
name: "Slashing for futuristic exited validator ",
name: "Slashing for futuristic exited validator",
fields: fields{
pending: []*PendingAttesterSlashing{},
included: make(map[uint64]bool),
Expand All @@ -203,10 +206,12 @@ func TestPool_InsertAttesterSlashing(t *testing.T) {
},
},
{
name: "Slashing for slashed validator ",
name: "Slashing for slashed validator",
fields: fields{
pending: []*PendingAttesterSlashing{},
included: make(map[uint64]bool),
wantErr: true,
err: "cannot be slashed",
},
args: args{
slashing: attesterSlashingForValIdx(5),
Expand Down Expand Up @@ -259,6 +264,7 @@ func TestPool_InsertAttesterSlashing(t *testing.T) {
},
{ // 5 - Slashed.
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
Slashed: true,
},
}
for _, tt := range tests {
Expand All @@ -267,13 +273,17 @@ func TestPool_InsertAttesterSlashing(t *testing.T) {
pendingAttesterSlashing: tt.fields.pending,
included: tt.fields.included,
}
s, err := beaconstate.InitializeFromProtoUnsafe(&p2ppb.BeaconState{Validators: validators})
s, err := beaconstate.InitializeFromProtoUnsafe(&p2ppb.BeaconState{
Slot: 16 * params.BeaconConfig().SlotsPerEpoch,
Validators: validators,
})
if err != nil {
t.Fatal(err)
}
s.SetSlot(16 * params.BeaconConfig().SlotsPerEpoch)
s.SetSlashings([]uint64{5})
p.InsertAttesterSlashing(s, tt.args.slashing)
err = p.InsertAttesterSlashing(s, tt.args.slashing)
if err != nil && tt.fields.wantErr && !strings.Contains(err.Error(), tt.fields.err) {
t.Fatalf("Wanted err: %v, received %v", tt.fields.err, err)
}
if len(p.pendingAttesterSlashing) != len(tt.want) {
t.Fatalf("Mismatched lengths of pending list. Got %d, wanted %d.", len(p.pendingAttesterSlashing), len(tt.want))
}
Expand Down
20 changes: 16 additions & 4 deletions beacon-chain/operations/slashings/service_proposer_test.go
Expand Up @@ -2,6 +2,7 @@ package slashings

import (
"reflect"
"strings"
"testing"

"github.com/gogo/protobuf/proto"
Expand All @@ -27,6 +28,8 @@ func generateNProposerSlashings(n uint64) []*ethpb.ProposerSlashing {

func TestPool_InsertProposerSlashing(t *testing.T) {
type fields struct {
wantErr bool
err string
pending []*ethpb.ProposerSlashing
included map[uint64]bool
}
Expand Down Expand Up @@ -90,6 +93,8 @@ func TestPool_InsertProposerSlashing(t *testing.T) {
fields: fields{
pending: []*ethpb.ProposerSlashing{},
included: make(map[uint64]bool),
wantErr: true,
err: "cannot be slashed",
},
args: args{
slashing: proposerSlashingForValIdx(5),
Expand All @@ -103,6 +108,8 @@ func TestPool_InsertProposerSlashing(t *testing.T) {
included: map[uint64]bool{
1: true,
},
wantErr: true,
err: "cannot be slashed",
},
args: args{
slashing: proposerSlashingForValIdx(1),
Expand Down Expand Up @@ -146,6 +153,7 @@ func TestPool_InsertProposerSlashing(t *testing.T) {
},
{ // 5 - Slashed.
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
Slashed: true,
},
}
for _, tt := range tests {
Expand All @@ -154,13 +162,17 @@ func TestPool_InsertProposerSlashing(t *testing.T) {
pendingProposerSlashing: tt.fields.pending,
included: tt.fields.included,
}
beaconState, err := beaconstate.InitializeFromProtoUnsafe(&p2ppb.BeaconState{Validators: validators})
beaconState, err := beaconstate.InitializeFromProtoUnsafe(&p2ppb.BeaconState{
Slot: 16 * params.BeaconConfig().SlotsPerEpoch,
Validators: validators,
})
if err != nil {
t.Fatal(err)
}
beaconState.SetSlot(16 * params.BeaconConfig().SlotsPerEpoch)
beaconState.SetSlashings([]uint64{5})
p.InsertProposerSlashing(beaconState, tt.args.slashing)
err = p.InsertProposerSlashing(beaconState, tt.args.slashing)
if err != nil && tt.fields.wantErr && !strings.Contains(err.Error(), tt.fields.err) {
t.Fatalf("Wanted err: %v, received %v", tt.fields.err, err)
}
if len(p.pendingProposerSlashing) != len(tt.want) {
t.Fatalf("Mismatched lengths of pending list. Got %d, wanted %d.", len(p.pendingProposerSlashing), len(tt.want))
}
Expand Down
1 change: 1 addition & 0 deletions beacon-chain/rpc/BUILD.bazel
Expand Up @@ -14,6 +14,7 @@ go_library(
"//beacon-chain/core/feed/state:go_default_library",
"//beacon-chain/db:go_default_library",
"//beacon-chain/operations/attestations:go_default_library",
"//beacon-chain/operations/slashings:go_default_library",
"//beacon-chain/operations/voluntaryexits:go_default_library",
"//beacon-chain/p2p:go_default_library",
"//beacon-chain/powchain:go_default_library",
Expand Down
5 changes: 5 additions & 0 deletions beacon-chain/rpc/beacon/BUILD.bazel
Expand Up @@ -9,6 +9,7 @@ go_library(
"committees.go",
"config.go",
"server.go",
"slashings.go",
"validators.go",
],
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/rpc/beacon",
Expand All @@ -26,12 +27,14 @@ go_library(
"//beacon-chain/db/filters:go_default_library",
"//beacon-chain/flags:go_default_library",
"//beacon-chain/operations/attestations:go_default_library",
"//beacon-chain/operations/slashings:go_default_library",
"//beacon-chain/powchain:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//shared/bytesutil:go_default_library",
"//shared/hashutil:go_default_library",
"//shared/pagination:go_default_library",
"//shared/params:go_default_library",
"//shared/sliceutil:go_default_library",
"//shared/slotutil:go_default_library",
"@com_github_gogo_protobuf//types:go_default_library",
"@com_github_pkg_errors//:go_default_library",
Expand All @@ -50,6 +53,7 @@ go_test(
"blocks_test.go",
"committees_test.go",
"config_test.go",
"slashings_test.go",
"validators_test.go",
],
embed = [":go_default_library"],
Expand All @@ -65,6 +69,7 @@ go_test(
"//beacon-chain/db/testing:go_default_library",
"//beacon-chain/flags:go_default_library",
"//beacon-chain/operations/attestations:go_default_library",
"//beacon-chain/operations/slashings:go_default_library",
"//beacon-chain/rpc/testing:go_default_library",
"//beacon-chain/state:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/rpc/beacon/attestations.go
Expand Up @@ -119,7 +119,7 @@ func (bs *Server) StreamAttestations(
for {
select {
case <-bs.SlotTicker.C():
atts := bs.Pool.AggregatedAttestations()
atts := bs.AttestationsPool.AggregatedAttestations()
for i := 0; i < len(atts); i++ {
if err := stream.Send(atts[i]); err != nil {
return status.Errorf(codes.Unavailable, "Could not send over stream: %v", err)
Expand Down Expand Up @@ -153,7 +153,7 @@ func (bs *Server) AttestationPool(
flags.Get().MaxPageSize,
)
}
atts := bs.Pool.AggregatedAttestations()
atts := bs.AttestationsPool.AggregatedAttestations()
numAtts := len(atts)
if numAtts == 0 {
return &ethpb.AttestationPoolResponse{
Expand Down
20 changes: 10 additions & 10 deletions beacon-chain/rpc/beacon/attestations_test.go
Expand Up @@ -551,15 +551,15 @@ func TestServer_AttestationPool_Pagination_ExceedsMaxPageSize(t *testing.T) {
func TestServer_AttestationPool_Pagination_OutOfRange(t *testing.T) {
ctx := context.Background()
bs := &Server{
Pool: attestations.NewPool(),
AttestationsPool: attestations.NewPool(),
}

atts := []*ethpb.Attestation{
{Data: &ethpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b1101}},
{Data: &ethpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b1101}},
{Data: &ethpb.AttestationData{Slot: 3}, AggregationBits: bitfield.Bitlist{0b1101}},
}
if err := bs.Pool.SaveAggregatedAttestations(atts); err != nil {
if err := bs.AttestationsPool.SaveAggregatedAttestations(atts); err != nil {
t.Fatal(err)
}

Expand All @@ -576,7 +576,7 @@ func TestServer_AttestationPool_Pagination_OutOfRange(t *testing.T) {
func TestServer_AttestationPool_Pagination_DefaultPageSize(t *testing.T) {
ctx := context.Background()
bs := &Server{
Pool: attestations.NewPool(),
AttestationsPool: attestations.NewPool(),
}

atts := make([]*ethpb.Attestation, params.BeaconConfig().DefaultPageSize+1)
Expand All @@ -586,7 +586,7 @@ func TestServer_AttestationPool_Pagination_DefaultPageSize(t *testing.T) {
AggregationBits: bitfield.Bitlist{0b1101},
}
}
if err := bs.Pool.SaveAggregatedAttestations(atts); err != nil {
if err := bs.AttestationsPool.SaveAggregatedAttestations(atts); err != nil {
t.Fatal(err)
}

Expand All @@ -610,7 +610,7 @@ func TestServer_AttestationPool_Pagination_DefaultPageSize(t *testing.T) {
func TestServer_AttestationPool_Pagination_CustomPageSize(t *testing.T) {
ctx := context.Background()
bs := &Server{
Pool: attestations.NewPool(),
AttestationsPool: attestations.NewPool(),
}

numAtts := 100
Expand All @@ -621,7 +621,7 @@ func TestServer_AttestationPool_Pagination_CustomPageSize(t *testing.T) {
AggregationBits: bitfield.Bitlist{0b1101},
}
}
if err := bs.Pool.SaveAggregatedAttestations(atts); err != nil {
if err := bs.AttestationsPool.SaveAggregatedAttestations(atts); err != nil {
t.Fatal(err)
}
tests := []struct {
Expand Down Expand Up @@ -716,17 +716,17 @@ func TestServer_StreamAttestations_OnSlotTick(t *testing.T) {
Channel: make(chan uint64),
}
server := &Server{
Ctx: ctx,
SlotTicker: ticker,
Pool: attestations.NewPool(),
Ctx: ctx,
SlotTicker: ticker,
AttestationsPool: attestations.NewPool(),
}

atts := []*ethpb.Attestation{
{Data: &ethpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b1101}},
{Data: &ethpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b1101}},
{Data: &ethpb.AttestationData{Slot: 3}, AggregationBits: bitfield.Bitlist{0b1101}},
}
if err := server.Pool.SaveAggregatedAttestations(atts); err != nil {
if err := server.AttestationsPool.SaveAggregatedAttestations(atts); err != nil {
t.Fatal(err)
}

Expand Down

0 comments on commit 549b0f6

Please sign in to comment.