prysm-pulse/slasher/rpc/server_test.go
Victor Farazdagi b5c4dc2a75
Refactor db teardown to testing.TB.Cleanup (#5725)
* init-sync updates
* slasher/db/kv tests
* beacon-chain/rpc/beacon tests
* update kv_test
* beacon-chain/rpc-validator tests updated
* slasher/db/kv - remove teardown method
* beacon-chain/sync tests updated
* beacon-chain/db/kv tests updated
* beacon-chain/blockchain tests updated
* beacon-chain/state/stategen tests updated
* beacon-chain/powchain updates
* updates rest of slasher tests
* validator/db tests
* rest of the tests
* minor comments update
* gazelle
* Merge refs/heads/master into teardowndb-to-cleanup
2020-05-04 01:14:34 +00:00

54 lines
1.6 KiB
Go

package rpc
import (
"context"
"testing"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
testDB "github.com/prysmaticlabs/prysm/slasher/db/testing"
"github.com/prysmaticlabs/prysm/slasher/detection"
)
func Test_DetectionFlow(t *testing.T) {
db := testDB.SetupSlasherDB(t, false)
savedAttestation := &ethpb.IndexedAttestation{
AttestingIndices: []uint64{3},
Data: &ethpb.AttestationData{
Source: &ethpb.Checkpoint{Epoch: 3},
Target: &ethpb.Checkpoint{Epoch: 4},
},
Signature: bytesutil.PadTo([]byte{1, 2}, 96),
}
incomingAtt := &ethpb.IndexedAttestation{
AttestingIndices: []uint64{3},
Data: &ethpb.AttestationData{
Source: &ethpb.Checkpoint{Epoch: 2},
Target: &ethpb.Checkpoint{Epoch: 4},
},
Signature: bytesutil.PadTo([]byte{1, 2}, 96),
}
cfg := &detection.Config{
SlasherDB: db,
}
ctx := context.Background()
ds := detection.NewDetectionService(ctx, cfg)
server := Server{ctx: ctx, detector: ds, slasherDB: db}
slashings, err := server.IsSlashableAttestation(ctx, savedAttestation)
if err != nil {
t.Fatalf("got error while trying to detect slashing: %v", err)
}
if len(slashings.AttesterSlashing) != 0 {
t.Fatalf("Found slashings while no slashing should have been found on first attestation: %v slashing found: %v", savedAttestation, slashings)
}
slashing, err := server.IsSlashableAttestation(ctx, incomingAtt)
if err != nil {
t.Fatalf("got error while trying to detect slashing: %v", err)
}
if len(slashing.AttesterSlashing) != 1 {
t.Fatalf("only one slashing should have been found. got: %v", len(slashing.AttesterSlashing))
}
}