mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-06 01:32:18 +00:00
6fe814c5aa
* proposal detector * comment fixes * comment fixes * raul feedback * fix todo * gaz Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
115 lines
2.6 KiB
Go
115 lines
2.6 KiB
Go
package proposals
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"reflect"
|
|
"testing"
|
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
testDB "github.com/prysmaticlabs/prysm/slasher/db/testing"
|
|
"github.com/prysmaticlabs/prysm/slasher/detection/proposals/iface"
|
|
)
|
|
|
|
var _ = iface.ProposalsDetector(&ProposeDetector{})
|
|
|
|
func TestProposalsDetector_DetectSlashingsForBlockHeaders(t *testing.T) {
|
|
type testStruct struct {
|
|
name string
|
|
blk *ethpb.SignedBeaconBlockHeader
|
|
incomingBlk *ethpb.SignedBeaconBlockHeader
|
|
slashing *ethpb.ProposerSlashing
|
|
}
|
|
blk1epoch0, err := signedBlockHeader(startSlot(0), 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
blk2epoch0, err := signedBlockHeader(startSlot(0)+1, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
blk1epoch1, err := signedBlockHeader(startSlot(1), 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
//blk1epoch3, err := signedBlockHeader(startSlot(3), 0)
|
|
//if err != nil {
|
|
// t.Fatal(err)
|
|
//}
|
|
tests := []testStruct{
|
|
{
|
|
name: "same block sig dont slash",
|
|
blk: blk1epoch0,
|
|
incomingBlk: blk1epoch0,
|
|
slashing: nil,
|
|
},
|
|
{
|
|
name: "block from different epoch dont slash",
|
|
blk: blk1epoch0,
|
|
incomingBlk: blk1epoch1,
|
|
slashing: nil,
|
|
},
|
|
{
|
|
name: "different sig from same epoch slash",
|
|
blk: blk1epoch0,
|
|
incomingBlk: blk2epoch0,
|
|
slashing: ðpb.ProposerSlashing{ProposerIndex: 0, Header_1: blk2epoch0, Header_2: blk1epoch0},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
db := testDB.SetupSlasherDB(t, false)
|
|
defer testDB.TeardownSlasherDB(t, db)
|
|
ctx := context.Background()
|
|
|
|
sd := &ProposeDetector{
|
|
slasherDB: db,
|
|
}
|
|
|
|
if err := sd.slasherDB.SaveBlockHeader(ctx, 0, tt.blk); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
res, err := sd.DetectDoublePropose(ctx, tt.incomingBlk)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if !reflect.DeepEqual(res, tt.slashing) {
|
|
t.Errorf("Wanted: %v, received %v", tt.slashing, res)
|
|
}
|
|
|
|
})
|
|
}
|
|
}
|
|
|
|
func signedBlockHeader(slot uint64, proposerIdx uint64) (*ethpb.SignedBeaconBlockHeader, error) {
|
|
sig, err := genRandomSig()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
root := [32]byte{1, 2, 3}
|
|
return ðpb.SignedBeaconBlockHeader{
|
|
Header: ðpb.BeaconBlockHeader{
|
|
//ProposerIndex proposerIndex,
|
|
Slot: slot,
|
|
ParentRoot: root[:],
|
|
StateRoot: root[:],
|
|
BodyRoot: root[:],
|
|
},
|
|
Signature: sig,
|
|
}, nil
|
|
}
|
|
|
|
func genRandomSig() (blk []byte, err error) {
|
|
blk = make([]byte, 96)
|
|
_, err = rand.Read(blk)
|
|
return
|
|
}
|
|
|
|
func startSlot(epoch uint64) uint64 {
|
|
return epoch * params.BeaconConfig().SlotsPerEpoch
|
|
}
|