mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-16 06:58:20 +00:00
379 lines
10 KiB
Go
379 lines
10 KiB
Go
package sync
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
|
pubsubpb "github.com/libp2p/go-libp2p-pubsub/pb"
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
"github.com/prysmaticlabs/go-bitfield"
|
|
"github.com/prysmaticlabs/go-ssz"
|
|
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
|
dbtest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/operations/attestations"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
|
|
p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing"
|
|
mockSync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync/testing"
|
|
"github.com/prysmaticlabs/prysm/shared/bls"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
)
|
|
|
|
func TestVerifyIndexInCommittee_CanVerify(t *testing.T) {
|
|
ctx := context.Background()
|
|
params.UseMinimalConfig()
|
|
defer params.UseMainnetConfig()
|
|
|
|
validators := uint64(64)
|
|
s, _ := testutil.DeterministicGenesisState(t, validators)
|
|
s.Slot = params.BeaconConfig().SlotsPerEpoch
|
|
|
|
bf := []byte{0xff}
|
|
att := ðpb.Attestation{Data: ðpb.AttestationData{
|
|
Target: ðpb.Checkpoint{Epoch: 0}},
|
|
AggregationBits: bf}
|
|
|
|
committee, err := helpers.BeaconCommitteeFromState(s, att.Data.Slot, att.Data.CommitteeIndex)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
indices, err := helpers.AttestingIndices(att.AggregationBits, committee)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err := validateIndexInCommittee(ctx, s, att, indices[0]); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
wanted := "validator index 1000 is not within the committee"
|
|
if err := validateIndexInCommittee(ctx, s, att, 1000); !strings.Contains(err.Error(), wanted) {
|
|
t.Error("Did not receive wanted error")
|
|
}
|
|
}
|
|
|
|
func TestVerifySelection_NotAnAggregator(t *testing.T) {
|
|
ctx := context.Background()
|
|
params.UseMinimalConfig()
|
|
defer params.UseMainnetConfig()
|
|
validators := uint64(2048)
|
|
beaconState, privKeys := testutil.DeterministicGenesisState(t, validators)
|
|
|
|
sig := privKeys[0].Sign([]byte{}, 0)
|
|
data := ðpb.AttestationData{}
|
|
|
|
wanted := "validator is not an aggregator for slot"
|
|
if err := validateSelection(ctx, beaconState, data, 0, sig.Marshal()); !strings.Contains(err.Error(), wanted) {
|
|
t.Error("Did not receive wanted error")
|
|
}
|
|
}
|
|
|
|
func TestVerifySelection_BadSignature(t *testing.T) {
|
|
ctx := context.Background()
|
|
validators := uint64(256)
|
|
beaconState, privKeys := testutil.DeterministicGenesisState(t, validators)
|
|
|
|
sig := privKeys[0].Sign([]byte{}, 0)
|
|
data := ðpb.AttestationData{}
|
|
|
|
wanted := "could not validate slot signature"
|
|
if err := validateSelection(ctx, beaconState, data, 0, sig.Marshal()); !strings.Contains(err.Error(), wanted) {
|
|
t.Error("Did not receive wanted error")
|
|
}
|
|
}
|
|
|
|
func TestVerifySelection_CanVerify(t *testing.T) {
|
|
ctx := context.Background()
|
|
validators := uint64(256)
|
|
beaconState, privKeys := testutil.DeterministicGenesisState(t, validators)
|
|
|
|
data := ðpb.AttestationData{}
|
|
slotRoot, err := ssz.HashTreeRoot(data.Slot)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
domain := helpers.Domain(beaconState.Fork, 0, params.BeaconConfig().DomainBeaconAttester)
|
|
sig := privKeys[0].Sign(slotRoot[:], domain)
|
|
|
|
if err := validateSelection(ctx, beaconState, data, 0, sig.Marshal()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestValidateAggregateAndProof_NoBlock(t *testing.T) {
|
|
db := dbtest.SetupDB(t)
|
|
defer dbtest.TeardownDB(t, db)
|
|
p := p2ptest.NewTestP2P(t)
|
|
|
|
att := ðpb.Attestation{
|
|
Data: ðpb.AttestationData{
|
|
Source: ðpb.Checkpoint{Epoch: 0, Root: []byte("hello-world")},
|
|
Target: ðpb.Checkpoint{Epoch: 0, Root: []byte("hello-world")},
|
|
},
|
|
}
|
|
|
|
aggregateAndProof := ðpb.AggregateAttestationAndProof{
|
|
SelectionProof: []byte{'A'},
|
|
Aggregate: att,
|
|
AggregatorIndex: 0,
|
|
}
|
|
|
|
r := &Service{
|
|
p2p: p,
|
|
db: db,
|
|
initialSync: &mockSync.Sync{IsSyncing: false},
|
|
attPool: attestations.NewPool(),
|
|
}
|
|
|
|
buf := new(bytes.Buffer)
|
|
if _, err := p.Encoding().Encode(buf, aggregateAndProof); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
msg := &pubsub.Message{
|
|
Message: &pubsubpb.Message{
|
|
Data: buf.Bytes(),
|
|
TopicIDs: []string{
|
|
p2p.GossipTypeMapping[reflect.TypeOf(aggregateAndProof)],
|
|
},
|
|
},
|
|
}
|
|
|
|
if r.validateAggregateAndProof(context.Background(), "", msg) {
|
|
t.Error("Expected validate to fail")
|
|
}
|
|
}
|
|
|
|
func TestValidateAggregateAndProof_NotWithinSlotRange(t *testing.T) {
|
|
db := dbtest.SetupDB(t)
|
|
defer dbtest.TeardownDB(t, db)
|
|
p := p2ptest.NewTestP2P(t)
|
|
|
|
validators := uint64(256)
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, validators)
|
|
|
|
b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}}
|
|
db.SaveBlock(context.Background(), b)
|
|
root, _ := ssz.HashTreeRoot(b.Block)
|
|
|
|
aggBits := bitfield.NewBitlist(3)
|
|
aggBits.SetBitAt(0, true)
|
|
att := ðpb.Attestation{
|
|
Data: ðpb.AttestationData{
|
|
Slot: 1,
|
|
BeaconBlockRoot: root[:],
|
|
Source: ðpb.Checkpoint{Epoch: 0, Root: []byte("hello-world")},
|
|
Target: ðpb.Checkpoint{Epoch: 0, Root: []byte("hello-world")},
|
|
},
|
|
AggregationBits: aggBits,
|
|
}
|
|
|
|
aggregateAndProof := ðpb.AggregateAttestationAndProof{
|
|
Aggregate: att,
|
|
}
|
|
|
|
beaconState.GenesisTime = uint64(time.Now().Unix())
|
|
r := &Service{
|
|
p2p: p,
|
|
db: db,
|
|
initialSync: &mockSync.Sync{IsSyncing: false},
|
|
chain: &mock.ChainService{Genesis: time.Now(),
|
|
State: beaconState},
|
|
attPool: attestations.NewPool(),
|
|
}
|
|
|
|
buf := new(bytes.Buffer)
|
|
if _, err := p.Encoding().Encode(buf, aggregateAndProof); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
msg := &pubsub.Message{
|
|
Message: &pubsubpb.Message{
|
|
Data: buf.Bytes(),
|
|
TopicIDs: []string{
|
|
p2p.GossipTypeMapping[reflect.TypeOf(aggregateAndProof)],
|
|
},
|
|
},
|
|
}
|
|
|
|
if r.validateAggregateAndProof(context.Background(), "", msg) {
|
|
t.Error("Expected validate to fail")
|
|
}
|
|
|
|
att.Data.Slot = 1<<64 - 1
|
|
|
|
buf = new(bytes.Buffer)
|
|
if _, err := p.Encoding().Encode(buf, aggregateAndProof); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
msg = &pubsub.Message{
|
|
Message: &pubsubpb.Message{
|
|
Data: buf.Bytes(),
|
|
TopicIDs: []string{
|
|
p2p.GossipTypeMapping[reflect.TypeOf(aggregateAndProof)],
|
|
},
|
|
},
|
|
}
|
|
if r.validateAggregateAndProof(context.Background(), "", msg) {
|
|
t.Error("Expected validate to fail")
|
|
}
|
|
}
|
|
|
|
func TestValidateAggregateAndProof_ExistedInPool(t *testing.T) {
|
|
db := dbtest.SetupDB(t)
|
|
defer dbtest.TeardownDB(t, db)
|
|
p := p2ptest.NewTestP2P(t)
|
|
|
|
validators := uint64(256)
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, validators)
|
|
|
|
b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}}
|
|
db.SaveBlock(context.Background(), b)
|
|
root, _ := ssz.HashTreeRoot(b.Block)
|
|
|
|
aggBits := bitfield.NewBitlist(3)
|
|
aggBits.SetBitAt(0, true)
|
|
att := ðpb.Attestation{
|
|
Data: ðpb.AttestationData{
|
|
Slot: 1,
|
|
BeaconBlockRoot: root[:],
|
|
Source: ðpb.Checkpoint{Epoch: 0, Root: []byte("hello-world")},
|
|
Target: ðpb.Checkpoint{Epoch: 0, Root: []byte("hello-world")},
|
|
},
|
|
AggregationBits: aggBits,
|
|
}
|
|
|
|
aggregateAndProof := ðpb.AggregateAttestationAndProof{
|
|
Aggregate: att,
|
|
}
|
|
|
|
beaconState.GenesisTime = uint64(time.Now().Unix())
|
|
r := &Service{
|
|
attPool: attestations.NewPool(),
|
|
p2p: p,
|
|
db: db,
|
|
initialSync: &mockSync.Sync{IsSyncing: false},
|
|
chain: &mock.ChainService{Genesis: time.Now(),
|
|
State: beaconState},
|
|
}
|
|
|
|
buf := new(bytes.Buffer)
|
|
if _, err := p.Encoding().Encode(buf, aggregateAndProof); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
msg := &pubsub.Message{
|
|
Message: &pubsubpb.Message{
|
|
Data: buf.Bytes(),
|
|
TopicIDs: []string{
|
|
p2p.GossipTypeMapping[reflect.TypeOf(aggregateAndProof)],
|
|
},
|
|
},
|
|
}
|
|
|
|
if err := r.attPool.SaveBlockAttestation(att); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r.validateAggregateAndProof(context.Background(), "", msg) {
|
|
t.Error("Expected validate to fail")
|
|
}
|
|
}
|
|
|
|
func TestValidateAggregateAndProof_CanValidate(t *testing.T) {
|
|
db := dbtest.SetupDB(t)
|
|
defer dbtest.TeardownDB(t, db)
|
|
p := p2ptest.NewTestP2P(t)
|
|
|
|
validators := uint64(256)
|
|
beaconState, privKeys := testutil.DeterministicGenesisState(t, validators)
|
|
|
|
b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}}
|
|
db.SaveBlock(context.Background(), b)
|
|
root, _ := ssz.HashTreeRoot(b.Block)
|
|
|
|
aggBits := bitfield.NewBitlist(3)
|
|
aggBits.SetBitAt(0, true)
|
|
att := ðpb.Attestation{
|
|
Data: ðpb.AttestationData{
|
|
BeaconBlockRoot: root[:],
|
|
Source: ðpb.Checkpoint{Epoch: 0, Root: []byte("hello-world")},
|
|
Target: ðpb.Checkpoint{Epoch: 0, Root: []byte("hello-world")},
|
|
},
|
|
AggregationBits: aggBits,
|
|
}
|
|
|
|
committee, err := helpers.BeaconCommitteeFromState(beaconState, att.Data.Slot, att.Data.CommitteeIndex)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
attestingIndices, err := helpers.AttestingIndices(att.AggregationBits, committee)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
hashTreeRoot, err := ssz.HashTreeRoot(att.Data)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
domain := helpers.Domain(beaconState.Fork, 0, params.BeaconConfig().DomainBeaconAttester)
|
|
sigs := make([]*bls.Signature, len(attestingIndices))
|
|
for i, indice := range attestingIndices {
|
|
sig := privKeys[indice].Sign(hashTreeRoot[:], domain)
|
|
sigs[i] = sig
|
|
}
|
|
att.Signature = bls.AggregateSignatures(sigs).Marshal()[:]
|
|
|
|
slotRoot, err := ssz.HashTreeRoot(att.Data.Slot)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
sig := privKeys[154].Sign(slotRoot[:], domain)
|
|
aggregateAndProof := ðpb.AggregateAttestationAndProof{
|
|
SelectionProof: sig.Marshal(),
|
|
Aggregate: att,
|
|
AggregatorIndex: 154,
|
|
}
|
|
|
|
beaconState.GenesisTime = uint64(time.Now().Unix())
|
|
r := &Service{
|
|
p2p: p,
|
|
db: db,
|
|
initialSync: &mockSync.Sync{IsSyncing: false},
|
|
chain: &mock.ChainService{Genesis: time.Now(),
|
|
State: beaconState,
|
|
FinalizedCheckPoint: ðpb.Checkpoint{
|
|
Epoch: 0,
|
|
}},
|
|
attPool: attestations.NewPool(),
|
|
}
|
|
|
|
buf := new(bytes.Buffer)
|
|
if _, err := p.Encoding().Encode(buf, aggregateAndProof); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
msg := &pubsub.Message{
|
|
Message: &pubsubpb.Message{
|
|
Data: buf.Bytes(),
|
|
TopicIDs: []string{
|
|
p2p.GossipTypeMapping[reflect.TypeOf(aggregateAndProof)],
|
|
},
|
|
},
|
|
}
|
|
|
|
if !r.validateAggregateAndProof(context.Background(), "", msg) {
|
|
t.Fatal("Validated status is false")
|
|
}
|
|
|
|
if msg.ValidatorData == nil {
|
|
t.Error("Did not set validator data")
|
|
}
|
|
}
|