mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-26 05:17:22 +00:00
120 lines
3.3 KiB
Go
120 lines
3.3 KiB
Go
package sync
|
|
|
|
import (
|
|
"context"
|
|
"math/rand"
|
|
"testing"
|
|
|
|
"github.com/prysmaticlabs/go-ssz"
|
|
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
|
|
p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing"
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
|
|
"github.com/prysmaticlabs/prysm/shared/bls"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
)
|
|
|
|
func setupValidAttesterSlashing(t *testing.T) (*ethpb.AttesterSlashing, *pb.BeaconState) {
|
|
deposits, privKeys := testutil.SetupInitialDeposits(t, 5)
|
|
state, err := state.GenesisBeaconState(deposits, 0, ðpb.Eth1Data{})
|
|
for _, vv := range state.Validators {
|
|
vv.WithdrawableEpoch = 1 * params.BeaconConfig().SlotsPerEpoch
|
|
}
|
|
|
|
att1 := ðpb.IndexedAttestation{
|
|
Data: ðpb.AttestationData{
|
|
Source: ðpb.Checkpoint{Epoch: 1},
|
|
Target: ðpb.Checkpoint{Epoch: 0},
|
|
Crosslink: ðpb.Crosslink{
|
|
Shard: 4,
|
|
},
|
|
},
|
|
CustodyBit_0Indices: []uint64{0, 1},
|
|
}
|
|
dataAndCustodyBit := &pb.AttestationDataAndCustodyBit{
|
|
Data: att1.Data,
|
|
CustodyBit: false,
|
|
}
|
|
hashTreeRoot, err := ssz.HashTreeRoot(dataAndCustodyBit)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
domain := helpers.Domain(state, 0, params.BeaconConfig().DomainAttestation)
|
|
sig0 := privKeys[0].Sign(hashTreeRoot[:], domain)
|
|
sig1 := privKeys[1].Sign(hashTreeRoot[:], domain)
|
|
aggregateSig := bls.AggregateSignatures([]*bls.Signature{sig0, sig1})
|
|
att1.Signature = aggregateSig.Marshal()[:]
|
|
|
|
att2 := ðpb.IndexedAttestation{
|
|
Data: ðpb.AttestationData{
|
|
Source: ðpb.Checkpoint{Epoch: 0},
|
|
Target: ðpb.Checkpoint{Epoch: 0},
|
|
Crosslink: ðpb.Crosslink{
|
|
Shard: 4,
|
|
},
|
|
},
|
|
CustodyBit_0Indices: []uint64{0, 1},
|
|
}
|
|
dataAndCustodyBit = &pb.AttestationDataAndCustodyBit{
|
|
Data: att2.Data,
|
|
CustodyBit: false,
|
|
}
|
|
hashTreeRoot, err = ssz.HashTreeRoot(dataAndCustodyBit)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
sig0 = privKeys[0].Sign(hashTreeRoot[:], domain)
|
|
sig1 = privKeys[1].Sign(hashTreeRoot[:], domain)
|
|
aggregateSig = bls.AggregateSignatures([]*bls.Signature{sig0, sig1})
|
|
att2.Signature = aggregateSig.Marshal()[:]
|
|
|
|
slashing := ðpb.AttesterSlashing{
|
|
Attestation_1: att1,
|
|
Attestation_2: att2,
|
|
}
|
|
|
|
currentSlot := 2 * params.BeaconConfig().SlotsPerEpoch
|
|
state.Slot = currentSlot
|
|
|
|
b := make([]byte, 32)
|
|
if _, err := rand.Read(b); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
return slashing, state
|
|
}
|
|
|
|
func TestValidateAttesterSlashing_ValidSlashing(t *testing.T) {
|
|
p2p := p2ptest.NewTestP2P(t)
|
|
ctx := context.Background()
|
|
|
|
slashing, s := setupValidAttesterSlashing(t)
|
|
|
|
r := &RegularSync{
|
|
p2p: p2p,
|
|
chain: &mock.ChainService{State: s},
|
|
}
|
|
|
|
if !r.validateAttesterSlashing(ctx, slashing, p2p) {
|
|
t.Error("Failed validation")
|
|
}
|
|
|
|
if !p2p.BroadcastCalled {
|
|
t.Error("Broadcast was not called")
|
|
}
|
|
|
|
// A second message with the same information should not be valid for processing or
|
|
// propagation.
|
|
p2p.BroadcastCalled = false
|
|
if r.validateAttesterSlashing(ctx, slashing, p2p) {
|
|
t.Error("Passed validation when should have failed")
|
|
}
|
|
|
|
if p2p.BroadcastCalled {
|
|
t.Error("broadcast was called when it should not have been called")
|
|
}
|
|
}
|