mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 12:57:18 +00:00
ec4de10001
* Starting * Merge branch 'master' of github.com:prysmaticlabs/prysm into domain-and-sign * core blocks tests: updated * core state tests: updated * rpc validator tests updated * sync tests updated * testutil updated * Beacon-chain: update tests * Shared: update tests * Rest of the tests * Merge branch 'master' of github.com:prysmaticlabs/prysm into domain-and-sign * Merge refs/heads/master into domain-and-sign * Merge refs/heads/master into domain-and-sign * Merge refs/heads/master into domain-and-sign * Merge refs/heads/master into domain-and-sign * Sync with master * Merge branch 'domain-and-sign' of github.com:prysmaticlabs/prysm into domain-and-sign * Merge refs/heads/master into domain-and-sign
605 lines
20 KiB
Go
605 lines
20 KiB
Go
package sync
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
lru "github.com/hashicorp/golang-lru"
|
|
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"
|
|
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
|
|
"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"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
|
|
mockSync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync/testing"
|
|
"github.com/prysmaticlabs/prysm/shared/attestationutil"
|
|
"github.com/prysmaticlabs/prysm/shared/bls"
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
"github.com/prysmaticlabs/prysm/shared/featureconfig"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
|
)
|
|
|
|
func TestVerifyIndexInCommittee_CanVerify(t *testing.T) {
|
|
ctx := context.Background()
|
|
params.UseMinimalConfig()
|
|
defer params.UseMainnetConfig()
|
|
|
|
validators := uint64(64)
|
|
s, _ := testutil.DeterministicGenesisState(t, validators)
|
|
require.NoError(t, s.SetSlot(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)
|
|
assert.NoError(t, err)
|
|
indices := attestationutil.AttestingIndices(att.AggregationBits, committee)
|
|
require.NoError(t, err)
|
|
require.NoError(t, validateIndexInCommittee(ctx, s, att, indices[0]))
|
|
|
|
wanted := "validator index 1000 is not within the committee"
|
|
assert.ErrorContains(t, wanted, validateIndexInCommittee(ctx, s, att, 1000))
|
|
}
|
|
|
|
func TestVerifyIndexInCommittee_ExistsInBeaconCommittee(t *testing.T) {
|
|
ctx := context.Background()
|
|
params.UseMinimalConfig()
|
|
defer params.UseMainnetConfig()
|
|
|
|
validators := uint64(64)
|
|
s, _ := testutil.DeterministicGenesisState(t, validators)
|
|
require.NoError(t, s.SetSlot(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)
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, validateIndexInCommittee(ctx, s, att, committee[0]))
|
|
|
|
wanted := "validator index 1000 is not within the committee"
|
|
assert.ErrorContains(t, wanted, validateIndexInCommittee(ctx, s, att, 1000))
|
|
}
|
|
|
|
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{'A'})
|
|
data := ðpb.AttestationData{}
|
|
|
|
wanted := "validator is not an aggregator for slot"
|
|
assert.ErrorContains(t, wanted, validateSelection(ctx, beaconState, data, 0, sig.Marshal()))
|
|
}
|
|
|
|
func TestVerifySelection_BadSignature(t *testing.T) {
|
|
ctx := context.Background()
|
|
validators := uint64(256)
|
|
beaconState, privKeys := testutil.DeterministicGenesisState(t, validators)
|
|
|
|
sig := privKeys[0].Sign([]byte{'A'})
|
|
data := ðpb.AttestationData{}
|
|
|
|
wanted := "signature did not verify"
|
|
assert.ErrorContains(t, wanted, validateSelection(ctx, beaconState, data, 0, sig.Marshal()))
|
|
}
|
|
|
|
func TestVerifySelection_CanVerify(t *testing.T) {
|
|
ctx := context.Background()
|
|
validators := uint64(256)
|
|
beaconState, privKeys := testutil.DeterministicGenesisState(t, validators)
|
|
|
|
data := ðpb.AttestationData{}
|
|
sig, err := helpers.ComputeDomainAndSign(beaconState, 0, data.Slot, params.BeaconConfig().DomainSelectionProof, privKeys[0])
|
|
require.NoError(t, err)
|
|
require.NoError(t, validateSelection(ctx, beaconState, data, 0, sig))
|
|
}
|
|
|
|
func TestValidateAggregateAndProof_NoBlock(t *testing.T) {
|
|
db, _ := dbtest.SetupDB(t)
|
|
p := p2ptest.NewTestP2P(t)
|
|
|
|
att := ðpb.Attestation{
|
|
Data: ðpb.AttestationData{
|
|
Source: ðpb.Checkpoint{Epoch: 0, Root: bytesutil.PadTo([]byte("hello-world"), 32)},
|
|
Target: ðpb.Checkpoint{Epoch: 0, Root: bytesutil.PadTo([]byte("hello-world"), 32)},
|
|
},
|
|
}
|
|
|
|
aggregateAndProof := ðpb.AggregateAttestationAndProof{
|
|
SelectionProof: bytesutil.PadTo([]byte{'A'}, 96),
|
|
Aggregate: att,
|
|
AggregatorIndex: 0,
|
|
}
|
|
signedAggregateAndProof := ðpb.SignedAggregateAttestationAndProof{Message: aggregateAndProof}
|
|
|
|
c, err := lru.New(10)
|
|
require.NoError(t, err)
|
|
r := &Service{
|
|
p2p: p,
|
|
db: db,
|
|
initialSync: &mockSync.Sync{IsSyncing: false},
|
|
attPool: attestations.NewPool(),
|
|
blkRootToPendingAtts: make(map[[32]byte][]*ethpb.SignedAggregateAttestationAndProof),
|
|
seenAttestationCache: c,
|
|
stateSummaryCache: cache.NewStateSummaryCache(),
|
|
chain: &mock.ChainService{},
|
|
}
|
|
err = r.initCaches()
|
|
require.NoError(t, err)
|
|
|
|
buf := new(bytes.Buffer)
|
|
_, err = p.Encoding().EncodeGossip(buf, signedAggregateAndProof)
|
|
require.NoError(t, err)
|
|
|
|
msg := &pubsub.Message{
|
|
Message: &pubsubpb.Message{
|
|
Data: buf.Bytes(),
|
|
TopicIDs: []string{
|
|
p2p.GossipTypeMapping[reflect.TypeOf(signedAggregateAndProof)],
|
|
},
|
|
},
|
|
}
|
|
|
|
if r.validateAggregateAndProof(context.Background(), "", msg) == pubsub.ValidationAccept {
|
|
t.Error("Expected validate to fail")
|
|
}
|
|
}
|
|
|
|
func TestValidateAggregateAndProof_NotWithinSlotRange(t *testing.T) {
|
|
db, _ := dbtest.SetupDB(t)
|
|
p := p2ptest.NewTestP2P(t)
|
|
|
|
validators := uint64(256)
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, validators)
|
|
|
|
b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}}
|
|
require.NoError(t, db.SaveBlock(context.Background(), b))
|
|
root, err := stateutil.BlockRoot(b.Block)
|
|
require.NoError(t, err)
|
|
s := testutil.NewBeaconState()
|
|
require.NoError(t, db.SaveState(context.Background(), s, root))
|
|
|
|
aggBits := bitfield.NewBitlist(3)
|
|
aggBits.SetBitAt(0, true)
|
|
att := ðpb.Attestation{
|
|
Data: ðpb.AttestationData{
|
|
Slot: 1,
|
|
BeaconBlockRoot: root[:],
|
|
Source: ðpb.Checkpoint{Epoch: 0, Root: bytesutil.PadTo([]byte("hello-world"), 32)},
|
|
Target: ðpb.Checkpoint{Epoch: 0, Root: bytesutil.PadTo([]byte("hello-world"), 32)},
|
|
},
|
|
AggregationBits: aggBits,
|
|
}
|
|
|
|
aggregateAndProof := ðpb.AggregateAttestationAndProof{
|
|
Aggregate: att,
|
|
}
|
|
signedAggregateAndProof := ðpb.SignedAggregateAttestationAndProof{Message: aggregateAndProof}
|
|
|
|
require.NoError(t, beaconState.SetGenesisTime(uint64(time.Now().Unix())))
|
|
|
|
c, err := lru.New(10)
|
|
require.NoError(t, err)
|
|
r := &Service{
|
|
p2p: p,
|
|
db: db,
|
|
initialSync: &mockSync.Sync{IsSyncing: false},
|
|
chain: &mock.ChainService{Genesis: time.Now(),
|
|
State: beaconState},
|
|
attPool: attestations.NewPool(),
|
|
seenAttestationCache: c,
|
|
stateSummaryCache: cache.NewStateSummaryCache(),
|
|
}
|
|
err = r.initCaches()
|
|
require.NoError(t, err)
|
|
|
|
buf := new(bytes.Buffer)
|
|
_, err = p.Encoding().EncodeGossip(buf, signedAggregateAndProof)
|
|
require.NoError(t, err)
|
|
|
|
msg := &pubsub.Message{
|
|
Message: &pubsubpb.Message{
|
|
Data: buf.Bytes(),
|
|
TopicIDs: []string{
|
|
p2p.GossipTypeMapping[reflect.TypeOf(signedAggregateAndProof)],
|
|
},
|
|
},
|
|
}
|
|
|
|
if r.validateAggregateAndProof(context.Background(), "", msg) == pubsub.ValidationAccept {
|
|
t.Error("Expected validate to fail")
|
|
}
|
|
|
|
att.Data.Slot = 1<<32 - 1
|
|
|
|
buf = new(bytes.Buffer)
|
|
_, err = p.Encoding().EncodeGossip(buf, signedAggregateAndProof)
|
|
require.NoError(t, err)
|
|
|
|
msg = &pubsub.Message{
|
|
Message: &pubsubpb.Message{
|
|
Data: buf.Bytes(),
|
|
TopicIDs: []string{
|
|
p2p.GossipTypeMapping[reflect.TypeOf(signedAggregateAndProof)],
|
|
},
|
|
},
|
|
}
|
|
if r.validateAggregateAndProof(context.Background(), "", msg) == pubsub.ValidationAccept {
|
|
t.Error("Expected validate to fail")
|
|
}
|
|
}
|
|
|
|
func TestValidateAggregateAndProof_ExistedInPool(t *testing.T) {
|
|
db, _ := dbtest.SetupDB(t)
|
|
p := p2ptest.NewTestP2P(t)
|
|
|
|
validators := uint64(256)
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, validators)
|
|
|
|
b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}}
|
|
require.NoError(t, db.SaveBlock(context.Background(), b))
|
|
root, err := stateutil.BlockRoot(b.Block)
|
|
require.NoError(t, err)
|
|
|
|
aggBits := bitfield.NewBitlist(3)
|
|
aggBits.SetBitAt(0, true)
|
|
att := ðpb.Attestation{
|
|
Data: ðpb.AttestationData{
|
|
Slot: 1,
|
|
BeaconBlockRoot: root[:],
|
|
Source: ðpb.Checkpoint{Epoch: 0, Root: bytesutil.PadTo([]byte("hello-world"), 32)},
|
|
Target: ðpb.Checkpoint{Epoch: 0, Root: bytesutil.PadTo([]byte("hello-world"), 32)},
|
|
},
|
|
AggregationBits: aggBits,
|
|
}
|
|
|
|
aggregateAndProof := ðpb.AggregateAttestationAndProof{
|
|
Aggregate: att,
|
|
}
|
|
signedAggregateAndProof := ðpb.SignedAggregateAttestationAndProof{Message: aggregateAndProof}
|
|
|
|
require.NoError(t, beaconState.SetGenesisTime(uint64(time.Now().Unix())))
|
|
c, err := lru.New(10)
|
|
require.NoError(t, err)
|
|
r := &Service{
|
|
attPool: attestations.NewPool(),
|
|
p2p: p,
|
|
db: db,
|
|
initialSync: &mockSync.Sync{IsSyncing: false},
|
|
chain: &mock.ChainService{Genesis: time.Now(),
|
|
State: beaconState},
|
|
seenAttestationCache: c,
|
|
blkRootToPendingAtts: make(map[[32]byte][]*ethpb.SignedAggregateAttestationAndProof),
|
|
}
|
|
err = r.initCaches()
|
|
require.NoError(t, err)
|
|
|
|
buf := new(bytes.Buffer)
|
|
_, err = p.Encoding().EncodeGossip(buf, signedAggregateAndProof)
|
|
require.NoError(t, err)
|
|
|
|
msg := &pubsub.Message{
|
|
Message: &pubsubpb.Message{
|
|
Data: buf.Bytes(),
|
|
TopicIDs: []string{
|
|
p2p.GossipTypeMapping[reflect.TypeOf(signedAggregateAndProof)],
|
|
},
|
|
},
|
|
}
|
|
|
|
require.NoError(t, r.attPool.SaveBlockAttestation(att))
|
|
if r.validateAggregateAndProof(context.Background(), "", msg) == pubsub.ValidationAccept {
|
|
t.Error("Expected validate to fail")
|
|
}
|
|
}
|
|
|
|
func TestValidateAggregateAndProofWithNewStateMgmt_CanValidate(t *testing.T) {
|
|
resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt: true})
|
|
defer resetCfg()
|
|
|
|
db, _ := dbtest.SetupDB(t)
|
|
p := p2ptest.NewTestP2P(t)
|
|
|
|
validators := uint64(256)
|
|
beaconState, privKeys := testutil.DeterministicGenesisState(t, validators)
|
|
|
|
b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}}
|
|
require.NoError(t, db.SaveBlock(context.Background(), b))
|
|
root, err := stateutil.BlockRoot(b.Block)
|
|
require.NoError(t, err)
|
|
s := testutil.NewBeaconState()
|
|
require.NoError(t, db.SaveState(context.Background(), s, root))
|
|
|
|
aggBits := bitfield.NewBitlist(3)
|
|
aggBits.SetBitAt(0, true)
|
|
att := ðpb.Attestation{
|
|
Data: ðpb.AttestationData{
|
|
BeaconBlockRoot: root[:],
|
|
Source: ðpb.Checkpoint{Epoch: 0, Root: bytesutil.PadTo([]byte("hello-world"), 32)},
|
|
Target: ðpb.Checkpoint{Epoch: 0, Root: bytesutil.PadTo([]byte("hello-world"), 32)},
|
|
},
|
|
AggregationBits: aggBits,
|
|
}
|
|
|
|
committee, err := helpers.BeaconCommitteeFromState(beaconState, att.Data.Slot, att.Data.CommitteeIndex)
|
|
assert.NoError(t, err)
|
|
attestingIndices := attestationutil.AttestingIndices(att.AggregationBits, committee)
|
|
assert.NoError(t, err)
|
|
attesterDomain, err := helpers.Domain(beaconState.Fork(), 0, params.BeaconConfig().DomainBeaconAttester, beaconState.GenesisValidatorRoot())
|
|
assert.NoError(t, err)
|
|
hashTreeRoot, err := helpers.ComputeSigningRoot(att.Data, attesterDomain)
|
|
assert.NoError(t, err)
|
|
sigs := make([]bls.Signature, len(attestingIndices))
|
|
for i, indice := range attestingIndices {
|
|
sig := privKeys[indice].Sign(hashTreeRoot[:])
|
|
sigs[i] = sig
|
|
}
|
|
att.Signature = bls.AggregateSignatures(sigs).Marshal()[:]
|
|
ai := committee[0]
|
|
sig, err := helpers.ComputeDomainAndSign(beaconState, 0, att.Data.Slot, params.BeaconConfig().DomainSelectionProof, privKeys[ai])
|
|
require.NoError(t, err)
|
|
aggregateAndProof := ðpb.AggregateAttestationAndProof{
|
|
SelectionProof: sig,
|
|
Aggregate: att,
|
|
AggregatorIndex: ai,
|
|
}
|
|
signedAggregateAndProof := ðpb.SignedAggregateAttestationAndProof{Message: aggregateAndProof}
|
|
signedAggregateAndProof.Signature, err = helpers.ComputeDomainAndSign(beaconState, 0, signedAggregateAndProof.Message, params.BeaconConfig().DomainAggregateAndProof, privKeys[ai])
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, beaconState.SetGenesisTime(uint64(time.Now().Unix())))
|
|
c, err := lru.New(10)
|
|
require.NoError(t, err)
|
|
r := &Service{
|
|
p2p: p,
|
|
db: db,
|
|
initialSync: &mockSync.Sync{IsSyncing: false},
|
|
chain: &mock.ChainService{Genesis: time.Now(),
|
|
State: beaconState,
|
|
ValidAttestation: true,
|
|
FinalizedCheckPoint: ðpb.Checkpoint{
|
|
Epoch: 0,
|
|
}},
|
|
attPool: attestations.NewPool(),
|
|
seenAttestationCache: c,
|
|
stateSummaryCache: cache.NewStateSummaryCache(),
|
|
}
|
|
err = r.initCaches()
|
|
require.NoError(t, err)
|
|
|
|
buf := new(bytes.Buffer)
|
|
_, err = p.Encoding().EncodeGossip(buf, signedAggregateAndProof)
|
|
require.NoError(t, err)
|
|
|
|
msg := &pubsub.Message{
|
|
Message: &pubsubpb.Message{
|
|
Data: buf.Bytes(),
|
|
TopicIDs: []string{
|
|
p2p.GossipTypeMapping[reflect.TypeOf(signedAggregateAndProof)],
|
|
},
|
|
},
|
|
}
|
|
|
|
assert.Equal(t, pubsub.ValidationAccept, r.validateAggregateAndProof(context.Background(), "", msg), "Validated status is false")
|
|
assert.NotNil(t, msg.ValidatorData, "Did not set validator data")
|
|
}
|
|
|
|
func TestVerifyIndexInCommittee_SeenAggregatorEpoch(t *testing.T) {
|
|
db, _ := dbtest.SetupDB(t)
|
|
p := p2ptest.NewTestP2P(t)
|
|
|
|
validators := uint64(256)
|
|
beaconState, privKeys := testutil.DeterministicGenesisState(t, validators)
|
|
|
|
b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}}
|
|
require.NoError(t, db.SaveBlock(context.Background(), b))
|
|
root, err := stateutil.BlockRoot(b.Block)
|
|
require.NoError(t, err)
|
|
s := testutil.NewBeaconState()
|
|
require.NoError(t, db.SaveState(context.Background(), s, root))
|
|
|
|
aggBits := bitfield.NewBitlist(3)
|
|
aggBits.SetBitAt(0, true)
|
|
att := ðpb.Attestation{
|
|
Data: ðpb.AttestationData{
|
|
BeaconBlockRoot: root[:],
|
|
Source: ðpb.Checkpoint{Epoch: 0, Root: bytesutil.PadTo([]byte("hello-world"), 32)},
|
|
Target: ðpb.Checkpoint{Epoch: 0, Root: bytesutil.PadTo([]byte("hello-world"), 32)},
|
|
},
|
|
AggregationBits: aggBits,
|
|
}
|
|
|
|
committee, err := helpers.BeaconCommitteeFromState(beaconState, att.Data.Slot, att.Data.CommitteeIndex)
|
|
require.NoError(t, err)
|
|
attestingIndices := attestationutil.AttestingIndices(att.AggregationBits, committee)
|
|
attesterDomain, err := helpers.Domain(beaconState.Fork(), 0, params.BeaconConfig().DomainBeaconAttester, beaconState.GenesisValidatorRoot())
|
|
require.NoError(t, err)
|
|
hashTreeRoot, err := helpers.ComputeSigningRoot(att.Data, attesterDomain)
|
|
assert.NoError(t, err)
|
|
sigs := make([]bls.Signature, len(attestingIndices))
|
|
for i, indice := range attestingIndices {
|
|
sig := privKeys[indice].Sign(hashTreeRoot[:])
|
|
sigs[i] = sig
|
|
}
|
|
att.Signature = bls.AggregateSignatures(sigs).Marshal()[:]
|
|
ai := committee[0]
|
|
sig, err := helpers.ComputeDomainAndSign(beaconState, 0, att.Data.Slot, params.BeaconConfig().DomainSelectionProof, privKeys[ai])
|
|
require.NoError(t, err)
|
|
aggregateAndProof := ðpb.AggregateAttestationAndProof{
|
|
SelectionProof: sig,
|
|
Aggregate: att,
|
|
AggregatorIndex: ai,
|
|
}
|
|
signedAggregateAndProof := ðpb.SignedAggregateAttestationAndProof{Message: aggregateAndProof}
|
|
signedAggregateAndProof.Signature, err = helpers.ComputeDomainAndSign(beaconState, 0, signedAggregateAndProof.Message, params.BeaconConfig().DomainAggregateAndProof, privKeys[ai])
|
|
require.NoError(t, err)
|
|
require.NoError(t, beaconState.SetGenesisTime(uint64(time.Now().Unix())))
|
|
|
|
c, err := lru.New(10)
|
|
require.NoError(t, err)
|
|
r := &Service{
|
|
p2p: p,
|
|
db: db,
|
|
initialSync: &mockSync.Sync{IsSyncing: false},
|
|
chain: &mock.ChainService{Genesis: time.Now(),
|
|
ValidatorsRoot: [32]byte{'A'},
|
|
State: beaconState,
|
|
ValidAttestation: true,
|
|
FinalizedCheckPoint: ðpb.Checkpoint{
|
|
Epoch: 0,
|
|
}},
|
|
|
|
attPool: attestations.NewPool(),
|
|
seenAttestationCache: c,
|
|
stateSummaryCache: cache.NewStateSummaryCache(),
|
|
}
|
|
err = r.initCaches()
|
|
require.NoError(t, err)
|
|
|
|
buf := new(bytes.Buffer)
|
|
_, err = p.Encoding().EncodeGossip(buf, signedAggregateAndProof)
|
|
require.NoError(t, err)
|
|
|
|
msg := &pubsub.Message{
|
|
Message: &pubsubpb.Message{
|
|
Data: buf.Bytes(),
|
|
TopicIDs: []string{
|
|
p2p.GossipTypeMapping[reflect.TypeOf(signedAggregateAndProof)],
|
|
},
|
|
},
|
|
}
|
|
|
|
require.Equal(t, pubsub.ValidationAccept, r.validateAggregateAndProof(context.Background(), "", msg), "Validated status is false")
|
|
|
|
// Should fail with another attestation in the same epoch.
|
|
signedAggregateAndProof.Message.Aggregate.Data.Slot++
|
|
buf = new(bytes.Buffer)
|
|
_, err = p.Encoding().EncodeGossip(buf, signedAggregateAndProof)
|
|
require.NoError(t, err)
|
|
msg = &pubsub.Message{
|
|
Message: &pubsubpb.Message{
|
|
Data: buf.Bytes(),
|
|
TopicIDs: []string{
|
|
p2p.GossipTypeMapping[reflect.TypeOf(signedAggregateAndProof)],
|
|
},
|
|
},
|
|
}
|
|
|
|
time.Sleep(10 * time.Millisecond) // Wait for cached value to pass through buffers.
|
|
if r.validateAggregateAndProof(context.Background(), "", msg) == pubsub.ValidationAccept {
|
|
t.Fatal("Validated status is true")
|
|
}
|
|
}
|
|
|
|
func TestValidateAggregateAndProof_BadBlock(t *testing.T) {
|
|
resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt: true})
|
|
defer resetCfg()
|
|
|
|
db, _ := dbtest.SetupDB(t)
|
|
p := p2ptest.NewTestP2P(t)
|
|
|
|
validators := uint64(256)
|
|
beaconState, privKeys := testutil.DeterministicGenesisState(t, validators)
|
|
|
|
b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}}
|
|
root, err := stateutil.BlockRoot(b.Block)
|
|
require.NoError(t, err)
|
|
s := testutil.NewBeaconState()
|
|
require.NoError(t, db.SaveState(context.Background(), s, root))
|
|
|
|
aggBits := bitfield.NewBitlist(3)
|
|
aggBits.SetBitAt(0, true)
|
|
att := ðpb.Attestation{
|
|
Data: ðpb.AttestationData{
|
|
BeaconBlockRoot: root[:],
|
|
Source: ðpb.Checkpoint{Epoch: 0, Root: bytesutil.PadTo([]byte("hello-world"), 32)},
|
|
Target: ðpb.Checkpoint{Epoch: 0, Root: bytesutil.PadTo([]byte("hello-world"), 32)},
|
|
},
|
|
AggregationBits: aggBits,
|
|
}
|
|
|
|
committee, err := helpers.BeaconCommitteeFromState(beaconState, att.Data.Slot, att.Data.CommitteeIndex)
|
|
assert.NoError(t, err)
|
|
attestingIndices := attestationutil.AttestingIndices(att.AggregationBits, committee)
|
|
assert.NoError(t, err)
|
|
attesterDomain, err := helpers.Domain(beaconState.Fork(), 0, params.BeaconConfig().DomainBeaconAttester, beaconState.GenesisValidatorRoot())
|
|
assert.NoError(t, err)
|
|
hashTreeRoot, err := helpers.ComputeSigningRoot(att.Data, attesterDomain)
|
|
assert.NoError(t, err)
|
|
sigs := make([]bls.Signature, len(attestingIndices))
|
|
for i, indice := range attestingIndices {
|
|
sig := privKeys[indice].Sign(hashTreeRoot[:])
|
|
sigs[i] = sig
|
|
}
|
|
att.Signature = bls.AggregateSignatures(sigs).Marshal()[:]
|
|
ai := committee[0]
|
|
sig, err := helpers.ComputeDomainAndSign(beaconState, 0, att.Data.Slot, params.BeaconConfig().DomainSelectionProof, privKeys[ai])
|
|
require.NoError(t, err)
|
|
|
|
aggregateAndProof := ðpb.AggregateAttestationAndProof{
|
|
SelectionProof: sig,
|
|
Aggregate: att,
|
|
AggregatorIndex: ai,
|
|
}
|
|
signedAggregateAndProof := ðpb.SignedAggregateAttestationAndProof{Message: aggregateAndProof}
|
|
signedAggregateAndProof.Signature, err = helpers.ComputeDomainAndSign(beaconState, 0, signedAggregateAndProof.Message, params.BeaconConfig().DomainAggregateAndProof, privKeys[ai])
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, beaconState.SetGenesisTime(uint64(time.Now().Unix())))
|
|
c, err := lru.New(10)
|
|
require.NoError(t, err)
|
|
r := &Service{
|
|
p2p: p,
|
|
db: db,
|
|
initialSync: &mockSync.Sync{IsSyncing: false},
|
|
chain: &mock.ChainService{Genesis: time.Now(),
|
|
State: beaconState,
|
|
ValidAttestation: true,
|
|
FinalizedCheckPoint: ðpb.Checkpoint{
|
|
Epoch: 0,
|
|
}},
|
|
attPool: attestations.NewPool(),
|
|
seenAttestationCache: c,
|
|
stateSummaryCache: cache.NewStateSummaryCache(),
|
|
}
|
|
err = r.initCaches()
|
|
require.NoError(t, err)
|
|
// Set beacon block as bad.
|
|
r.setBadBlock(root)
|
|
buf := new(bytes.Buffer)
|
|
_, err = p.Encoding().EncodeGossip(buf, signedAggregateAndProof)
|
|
require.NoError(t, err)
|
|
|
|
msg := &pubsub.Message{
|
|
Message: &pubsubpb.Message{
|
|
Data: buf.Bytes(),
|
|
TopicIDs: []string{
|
|
p2p.GossipTypeMapping[reflect.TypeOf(signedAggregateAndProof)],
|
|
},
|
|
},
|
|
}
|
|
|
|
assert.Equal(t, pubsub.ValidationReject, r.validateAggregateAndProof(context.Background(), "", msg), "Validated status is true")
|
|
}
|