mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 04:47:18 +00:00
bb319e02e8
* Event support for `contribution_and_prrof` * event test * fix panic in tests * fix * Revert "Auxiliary commit to revert individual files from dc8d01a15f0056c1fb48733219feab6461f71695" This reverts commit f5f198564079781f80e1a045cefad7c27f89af25. * remove receiver * revive test * move sending events to sync package * remove receiver * remove notification test * build file * notifier tests * revert removal of exit event in API * simplify exit test * send notification in contribution API method * test fix Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> Co-authored-by: terence tsao <terence@prysmaticlabs.com>
198 lines
5.6 KiB
Go
198 lines
5.6 KiB
Go
package sync
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/rand"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
|
pubsubpb "github.com/libp2p/go-libp2p-pubsub/pb"
|
|
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/feed"
|
|
opfeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/operation"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/signing"
|
|
coreTime "github.com/prysmaticlabs/prysm/beacon-chain/core/time"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
|
|
p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state"
|
|
v1 "github.com/prysmaticlabs/prysm/beacon-chain/state/v1"
|
|
mockSync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync/testing"
|
|
lruwrpr "github.com/prysmaticlabs/prysm/cache/lru"
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
|
"github.com/prysmaticlabs/prysm/crypto/bls"
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
|
"github.com/prysmaticlabs/prysm/testing/assert"
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
|
)
|
|
|
|
func setupValidExit(t *testing.T) (*ethpb.SignedVoluntaryExit, state.BeaconState) {
|
|
exit := ðpb.SignedVoluntaryExit{
|
|
Exit: ðpb.VoluntaryExit{
|
|
ValidatorIndex: 0,
|
|
Epoch: 1 + params.BeaconConfig().ShardCommitteePeriod,
|
|
},
|
|
}
|
|
registry := []*ethpb.Validator{
|
|
{
|
|
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
|
|
ActivationEpoch: 0,
|
|
},
|
|
}
|
|
state, err := v1.InitializeFromProto(ðpb.BeaconState{
|
|
Validators: registry,
|
|
Fork: ðpb.Fork{
|
|
CurrentVersion: params.BeaconConfig().GenesisForkVersion,
|
|
PreviousVersion: params.BeaconConfig().GenesisForkVersion,
|
|
},
|
|
Slot: params.BeaconConfig().SlotsPerEpoch * 5,
|
|
})
|
|
require.NoError(t, err)
|
|
err = state.SetSlot(state.Slot() + params.BeaconConfig().SlotsPerEpoch.Mul(uint64(params.BeaconConfig().ShardCommitteePeriod)))
|
|
require.NoError(t, err)
|
|
|
|
priv, err := bls.RandKey()
|
|
require.NoError(t, err)
|
|
exit.Signature, err = signing.ComputeDomainAndSign(state, coreTime.CurrentEpoch(state), exit.Exit, params.BeaconConfig().DomainVoluntaryExit, priv)
|
|
require.NoError(t, err)
|
|
|
|
val, err := state.ValidatorAtIndex(0)
|
|
require.NoError(t, err)
|
|
val.PublicKey = priv.PublicKey().Marshal()
|
|
require.NoError(t, state.UpdateValidatorAtIndex(0, val))
|
|
|
|
b := make([]byte, 32)
|
|
_, err = rand.Read(b)
|
|
require.NoError(t, err)
|
|
|
|
return exit, state
|
|
}
|
|
|
|
func TestValidateVoluntaryExit_ValidExit(t *testing.T) {
|
|
p := p2ptest.NewTestP2P(t)
|
|
ctx := context.Background()
|
|
|
|
exit, s := setupValidExit(t)
|
|
|
|
r := &Service{
|
|
cfg: &Config{
|
|
P2P: p,
|
|
Chain: &mock.ChainService{
|
|
State: s,
|
|
Genesis: time.Now(),
|
|
},
|
|
InitialSync: &mockSync.Sync{IsSyncing: false},
|
|
OperationNotifier: (&mock.ChainService{}).OperationNotifier(),
|
|
},
|
|
seenExitCache: lruwrpr.New(10),
|
|
}
|
|
|
|
buf := new(bytes.Buffer)
|
|
_, err := p.Encoding().EncodeGossip(buf, exit)
|
|
require.NoError(t, err)
|
|
topic := p2p.GossipTypeMapping[reflect.TypeOf(exit)]
|
|
d, err := r.currentForkDigest()
|
|
assert.NoError(t, err)
|
|
topic = r.addDigestToTopic(topic, d)
|
|
m := &pubsub.Message{
|
|
Message: &pubsubpb.Message{
|
|
Data: buf.Bytes(),
|
|
Topic: &topic,
|
|
},
|
|
}
|
|
|
|
// Subscribe to operation notifications.
|
|
opChannel := make(chan *feed.Event, 1)
|
|
opSub := r.cfg.OperationNotifier.OperationFeed().Subscribe(opChannel)
|
|
defer opSub.Unsubscribe()
|
|
|
|
res, err := r.validateVoluntaryExit(ctx, "", m)
|
|
assert.NoError(t, err)
|
|
valid := res == pubsub.ValidationAccept
|
|
assert.Equal(t, true, valid, "Failed validation")
|
|
assert.NotNil(t, m.ValidatorData, "Decoded message was not set on the message validator data")
|
|
|
|
// Ensure the state notification was broadcast.
|
|
notificationFound := false
|
|
for !notificationFound {
|
|
select {
|
|
case event := <-opChannel:
|
|
if event.Type == opfeed.ExitReceived {
|
|
notificationFound = true
|
|
_, ok := event.Data.(*opfeed.ExitReceivedData)
|
|
assert.Equal(t, true, ok, "Entity is not of type *opfeed.ExitReceivedData")
|
|
}
|
|
case <-opSub.Err():
|
|
t.Error("Subscription to state notifier failed")
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestValidateVoluntaryExit_InvalidExitSlot(t *testing.T) {
|
|
p := p2ptest.NewTestP2P(t)
|
|
ctx := context.Background()
|
|
|
|
exit, s := setupValidExit(t)
|
|
// Set state slot to 1 to cause exit object fail to verify.
|
|
require.NoError(t, s.SetSlot(1))
|
|
r := &Service{
|
|
cfg: &Config{
|
|
P2P: p,
|
|
Chain: &mock.ChainService{
|
|
State: s,
|
|
},
|
|
InitialSync: &mockSync.Sync{IsSyncing: false},
|
|
},
|
|
seenExitCache: lruwrpr.New(10),
|
|
}
|
|
|
|
buf := new(bytes.Buffer)
|
|
_, err := p.Encoding().EncodeGossip(buf, exit)
|
|
require.NoError(t, err)
|
|
topic := p2p.GossipTypeMapping[reflect.TypeOf(exit)]
|
|
m := &pubsub.Message{
|
|
Message: &pubsubpb.Message{
|
|
Data: buf.Bytes(),
|
|
Topic: &topic,
|
|
},
|
|
}
|
|
res, err := r.validateVoluntaryExit(ctx, "", m)
|
|
_ = err
|
|
valid := res == pubsub.ValidationAccept
|
|
assert.Equal(t, false, valid, "passed validation")
|
|
}
|
|
|
|
func TestValidateVoluntaryExit_ValidExit_Syncing(t *testing.T) {
|
|
p := p2ptest.NewTestP2P(t)
|
|
ctx := context.Background()
|
|
|
|
exit, s := setupValidExit(t)
|
|
|
|
r := &Service{
|
|
cfg: &Config{
|
|
P2P: p,
|
|
Chain: &mock.ChainService{
|
|
State: s,
|
|
},
|
|
InitialSync: &mockSync.Sync{IsSyncing: true},
|
|
},
|
|
}
|
|
buf := new(bytes.Buffer)
|
|
_, err := p.Encoding().EncodeGossip(buf, exit)
|
|
require.NoError(t, err)
|
|
topic := p2p.GossipTypeMapping[reflect.TypeOf(exit)]
|
|
m := &pubsub.Message{
|
|
Message: &pubsubpb.Message{
|
|
Data: buf.Bytes(),
|
|
Topic: &topic,
|
|
},
|
|
}
|
|
res, err := r.validateVoluntaryExit(ctx, "", m)
|
|
_ = err
|
|
valid := res == pubsub.ValidationAccept
|
|
assert.Equal(t, false, valid, "Validation should have failed")
|
|
}
|