2019-08-18 15:33:58 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
|
|
"github.com/karlseguin/ccache"
|
2019-10-01 15:13:04 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-08-18 15:33:58 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
|
2019-08-23 17:48:40 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
|
2019-08-18 15:33:58 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
|
2019-08-23 17:48:40 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2019-08-18 15:33:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// seenExits tracks exits we've already seen to prevent feedback loop.
|
|
|
|
var seenExits = ccache.New(ccache.Configure())
|
|
|
|
|
|
|
|
func exitCacheKey(exit *ethpb.VoluntaryExit) string {
|
|
|
|
return fmt.Sprintf("%d-%d", exit.Epoch, exit.ValidatorIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clients who receive a voluntary exit on this topic MUST validate the conditions within process_voluntary_exit before
|
|
|
|
// forwarding it across the network.
|
2019-10-01 15:13:04 +00:00
|
|
|
func (r *RegularSync) validateVoluntaryExit(ctx context.Context, msg proto.Message, p p2p.Broadcaster, fromSelf bool) (bool, error) {
|
2019-09-20 17:54:32 +00:00
|
|
|
// The head state will be too far away to validate any voluntary exit.
|
|
|
|
if r.initialSync.Syncing() {
|
2019-10-01 15:13:04 +00:00
|
|
|
return false, nil
|
2019-09-20 17:54:32 +00:00
|
|
|
}
|
|
|
|
|
2019-08-22 05:34:25 +00:00
|
|
|
exit, ok := msg.(*ethpb.VoluntaryExit)
|
|
|
|
if !ok {
|
2019-10-01 15:13:04 +00:00
|
|
|
return false, nil
|
2019-08-22 05:34:25 +00:00
|
|
|
}
|
|
|
|
cacheKey := exitCacheKey(exit)
|
|
|
|
invalidKey := invalid + cacheKey
|
|
|
|
if seenExits.Get(invalidKey) != nil {
|
2019-10-01 15:13:04 +00:00
|
|
|
return false, errors.New("previously seen invalid validator exit received")
|
2019-08-22 05:34:25 +00:00
|
|
|
}
|
|
|
|
if seenExits.Get(cacheKey) != nil {
|
2019-10-01 15:13:04 +00:00
|
|
|
return false, nil
|
2019-08-18 15:33:58 +00:00
|
|
|
}
|
2019-08-23 17:48:40 +00:00
|
|
|
|
|
|
|
// Retrieve head state, advance state to the epoch slot used specified in exit message.
|
2019-11-19 16:12:50 +00:00
|
|
|
s, err := r.chain.HeadState(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2019-08-23 17:48:40 +00:00
|
|
|
exitedEpochSlot := exit.Epoch * params.BeaconConfig().SlotsPerEpoch
|
|
|
|
if s.Slot < exitedEpochSlot {
|
|
|
|
var err error
|
|
|
|
s, err = state.ProcessSlots(ctx, s, exitedEpochSlot)
|
|
|
|
if err != nil {
|
2019-10-01 15:13:04 +00:00
|
|
|
return false, errors.Wrapf(err, "Failed to advance state to slot %d", exitedEpochSlot)
|
2019-08-23 17:48:40 +00:00
|
|
|
}
|
2019-08-18 15:33:58 +00:00
|
|
|
}
|
2019-08-23 17:48:40 +00:00
|
|
|
|
|
|
|
if err := blocks.VerifyExit(s, exit); err != nil {
|
2019-08-22 05:34:25 +00:00
|
|
|
seenExits.Set(invalidKey, true /*value*/, oneYear /*TTL*/)
|
2019-10-01 15:13:04 +00:00
|
|
|
return false, errors.Wrap(err, "Received invalid validator exit")
|
2019-08-18 15:33:58 +00:00
|
|
|
}
|
2019-08-22 05:34:25 +00:00
|
|
|
seenExits.Set(cacheKey, true /*value*/, oneYear /*TTL*/)
|
2019-08-18 15:33:58 +00:00
|
|
|
|
2019-09-04 00:22:15 +00:00
|
|
|
if fromSelf {
|
2019-10-01 15:13:04 +00:00
|
|
|
return false, nil
|
2019-09-04 00:22:15 +00:00
|
|
|
}
|
|
|
|
|
2019-08-19 21:20:56 +00:00
|
|
|
if err := p.Broadcast(ctx, exit); err != nil {
|
2019-08-18 15:33:58 +00:00
|
|
|
log.WithError(err).Error("Failed to propagate voluntary exit")
|
|
|
|
}
|
2019-10-01 15:13:04 +00:00
|
|
|
return true, nil
|
2019-08-18 15:33:58 +00:00
|
|
|
}
|