/
rpc_status.go
347 lines (325 loc) · 11.6 KB
/
rpc_status.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package sync
import (
"bytes"
"context"
"sync"
"time"
libp2pcore "github.com/libp2p/go-libp2p-core"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/pkg/errors"
types "github.com/prysmaticlabs/eth2-types"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/peers"
p2ptypes "github.com/prysmaticlabs/prysm/beacon-chain/p2p/types"
"github.com/prysmaticlabs/prysm/cmd/beacon-chain/flags"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/runutil"
"github.com/prysmaticlabs/prysm/shared/slotutil"
"github.com/prysmaticlabs/prysm/shared/timeutils"
"github.com/sirupsen/logrus"
)
// maintainPeerStatuses by infrequently polling peers for their latest status.
func (s *Service) maintainPeerStatuses() {
// Run twice per epoch.
interval := time.Duration(params.BeaconConfig().SlotsPerEpoch.Div(2).Mul(params.BeaconConfig().SecondsPerSlot)) * time.Second
runutil.RunEvery(s.ctx, interval, func() {
wg := new(sync.WaitGroup)
for _, pid := range s.cfg.P2P.Peers().Connected() {
wg.Add(1)
go func(id peer.ID) {
defer wg.Done()
// If our peer status has not been updated correctly we disconnect over here
// and set the connection state over here instead.
if s.cfg.P2P.Host().Network().Connectedness(id) != network.Connected {
s.cfg.P2P.Peers().SetConnectionState(id, peers.PeerDisconnecting)
if err := s.cfg.P2P.Disconnect(id); err != nil {
log.Debugf("Error when disconnecting with peer: %v", err)
}
s.cfg.P2P.Peers().SetConnectionState(id, peers.PeerDisconnected)
return
}
// Disconnect from peers that are considered bad by any of the registered scorers.
if s.cfg.P2P.Peers().IsBad(id) {
s.disconnectBadPeer(s.ctx, id)
return
}
// If the status hasn't been updated in the recent interval time.
lastUpdated, err := s.cfg.P2P.Peers().ChainStateLastUpdated(id)
if err != nil {
// Peer has vanished; nothing to do.
return
}
if timeutils.Now().After(lastUpdated.Add(interval)) {
if err := s.reValidatePeer(s.ctx, id); err != nil {
log.WithField("peer", id).WithError(err).Debug("Could not revalidate peer")
s.cfg.P2P.Peers().Scorers().BadResponsesScorer().Increment(id)
}
}
}(pid)
}
// Wait for all status checks to finish and then proceed onwards to
// pruning excess peers.
wg.Wait()
peerIds := s.cfg.P2P.Peers().PeersToPrune()
peerIds = s.filterNeededPeers(peerIds)
for _, id := range peerIds {
if err := s.sendGoodByeAndDisconnect(s.ctx, p2ptypes.GoodbyeCodeTooManyPeers, id); err != nil {
log.WithField("peer", id).WithError(err).Debug("Could not disconnect with peer")
}
}
})
}
// resyncIfBehind checks periodically to see if we are in normal sync but have fallen behind our peers
// by more than an epoch, in which case we attempt a resync using the initial sync method to catch up.
func (s *Service) resyncIfBehind() {
millisecondsPerEpoch := int64(params.BeaconConfig().SlotsPerEpoch.Mul(1000).Mul(params.BeaconConfig().SecondsPerSlot))
// Run sixteen times per epoch.
interval := time.Duration(millisecondsPerEpoch/16) * time.Millisecond
runutil.RunEvery(s.ctx, interval, func() {
if s.shouldReSync() {
syncedEpoch := helpers.SlotToEpoch(s.cfg.Chain.HeadSlot())
// Factor number of expected minimum sync peers, to make sure that enough peers are
// available to resync (some peers may go away between checking non-finalized peers and
// actual resyncing).
highestEpoch, _ := s.cfg.P2P.Peers().BestNonFinalized(flags.Get().MinimumSyncPeers*2, syncedEpoch)
// Check if the current node is more than 1 epoch behind.
if highestEpoch > (syncedEpoch + 1) {
log.WithFields(logrus.Fields{
"currentEpoch": helpers.SlotToEpoch(s.cfg.Chain.CurrentSlot()),
"syncedEpoch": syncedEpoch,
"peersEpoch": highestEpoch,
}).Info("Fallen behind peers; reverting to initial sync to catch up")
numberOfTimesResyncedCounter.Inc()
s.clearPendingSlots()
if err := s.cfg.InitialSync.Resync(); err != nil {
log.Errorf("Could not resync chain: %v", err)
}
}
}
})
}
// shouldReSync returns true if the node is not syncing and falls behind two epochs.
func (s *Service) shouldReSync() bool {
syncedEpoch := helpers.SlotToEpoch(s.cfg.Chain.HeadSlot())
currentEpoch := helpers.SlotToEpoch(s.cfg.Chain.CurrentSlot())
prevEpoch := types.Epoch(0)
if currentEpoch > 1 {
prevEpoch = currentEpoch - 1
}
return s.cfg.InitialSync != nil && !s.cfg.InitialSync.Syncing() && syncedEpoch < prevEpoch
}
// sendRPCStatusRequest for a given topic with an expected protobuf message type.
func (s *Service) sendRPCStatusRequest(ctx context.Context, id peer.ID) error {
ctx, cancel := context.WithTimeout(ctx, respTimeout)
defer cancel()
headRoot, err := s.cfg.Chain.HeadRoot(ctx)
if err != nil {
return err
}
forkDigest, err := s.forkDigest()
if err != nil {
return err
}
resp := &pb.Status{
ForkDigest: forkDigest[:],
FinalizedRoot: s.cfg.Chain.FinalizedCheckpt().Root,
FinalizedEpoch: s.cfg.Chain.FinalizedCheckpt().Epoch,
HeadRoot: headRoot,
HeadSlot: s.cfg.Chain.HeadSlot(),
}
stream, err := s.cfg.P2P.Send(ctx, resp, p2p.RPCStatusTopicV1, id)
if err != nil {
return err
}
defer closeStream(stream, log)
code, errMsg, err := ReadStatusCode(stream, s.cfg.P2P.Encoding())
if err != nil {
return err
}
if code != 0 {
s.cfg.P2P.Peers().Scorers().BadResponsesScorer().Increment(id)
return errors.New(errMsg)
}
// No-op for now with the rpc context.
_, err = readContextFromStream(stream, s.cfg.Chain)
if err != nil {
return err
}
msg := &pb.Status{}
if err := s.cfg.P2P.Encoding().DecodeWithMaxLength(stream, msg); err != nil {
return err
}
// If validation fails, validation error is logged, and peer status scorer will mark peer as bad.
err = s.validateStatusMessage(ctx, msg)
s.cfg.P2P.Peers().Scorers().PeerStatusScorer().SetPeerStatus(id, msg, err)
if s.cfg.P2P.Peers().IsBad(id) {
s.disconnectBadPeer(s.ctx, id)
}
return err
}
func (s *Service) reValidatePeer(ctx context.Context, id peer.ID) error {
s.cfg.P2P.Peers().Scorers().PeerStatusScorer().SetHeadSlot(s.cfg.Chain.HeadSlot())
if err := s.sendRPCStatusRequest(ctx, id); err != nil {
return err
}
// Do not return an error for ping requests.
if err := s.sendPingRequest(ctx, id); err != nil {
log.WithError(err).Debug("Could not ping peer")
}
return nil
}
// statusRPCHandler reads the incoming Status RPC from the peer and responds with our version of a status message.
// This handler will disconnect any peer that does not match our fork version.
func (s *Service) statusRPCHandler(ctx context.Context, msg interface{}, stream libp2pcore.Stream) error {
ctx, cancel := context.WithTimeout(ctx, ttfbTimeout)
defer cancel()
SetRPCStreamDeadlines(stream)
log := log.WithField("handler", "status")
m, ok := msg.(*pb.Status)
if !ok {
return errors.New("message is not type *pb.Status")
}
if err := s.rateLimiter.validateRequest(stream, 1); err != nil {
return err
}
s.rateLimiter.add(stream, 1)
remotePeer := stream.Conn().RemotePeer()
if err := s.validateStatusMessage(ctx, m); err != nil {
log.WithFields(logrus.Fields{
"peer": remotePeer,
"error": err,
}).Debug("Invalid status message from peer")
respCode := byte(0)
switch err {
case p2ptypes.ErrGeneric:
respCode = responseCodeServerError
case p2ptypes.ErrWrongForkDigestVersion:
// Respond with our status and disconnect with the peer.
s.cfg.P2P.Peers().SetChainState(remotePeer, m)
if err := s.respondWithStatus(ctx, stream); err != nil {
return err
}
// Close before disconnecting, and wait for the other end to ack our response.
closeStreamAndWait(stream, log)
if err := s.sendGoodByeAndDisconnect(ctx, p2ptypes.GoodbyeCodeWrongNetwork, remotePeer); err != nil {
return err
}
return nil
default:
respCode = responseCodeInvalidRequest
s.cfg.P2P.Peers().Scorers().BadResponsesScorer().Increment(remotePeer)
}
originalErr := err
resp, err := s.generateErrorResponse(respCode, err.Error())
if err != nil {
log.WithError(err).Debug("Could not generate a response error")
} else if _, err := stream.Write(resp); err != nil {
// The peer may already be ignoring us, as we disagree on fork version, so log this as debug only.
log.WithError(err).Debug("Could not write to stream")
}
closeStreamAndWait(stream, log)
if err := s.sendGoodByeAndDisconnect(ctx, p2ptypes.GoodbyeCodeGenericError, remotePeer); err != nil {
return err
}
return originalErr
}
s.cfg.P2P.Peers().SetChainState(remotePeer, m)
if err := s.respondWithStatus(ctx, stream); err != nil {
return err
}
closeStream(stream, log)
return nil
}
func (s *Service) respondWithStatus(ctx context.Context, stream network.Stream) error {
headRoot, err := s.cfg.Chain.HeadRoot(ctx)
if err != nil {
return err
}
forkDigest, err := s.forkDigest()
if err != nil {
return err
}
resp := &pb.Status{
ForkDigest: forkDigest[:],
FinalizedRoot: s.cfg.Chain.FinalizedCheckpt().Root,
FinalizedEpoch: s.cfg.Chain.FinalizedCheckpt().Epoch,
HeadRoot: headRoot,
HeadSlot: s.cfg.Chain.HeadSlot(),
}
if _, err := stream.Write([]byte{responseCodeSuccess}); err != nil {
log.WithError(err).Debug("Could not write to stream")
}
_, err = s.cfg.P2P.Encoding().EncodeWithMaxLength(stream, resp)
return err
}
func (s *Service) validateStatusMessage(ctx context.Context, msg *pb.Status) error {
forkDigest, err := s.forkDigest()
if err != nil {
return err
}
if !bytes.Equal(forkDigest[:], msg.ForkDigest) {
return p2ptypes.ErrWrongForkDigestVersion
}
genesis := s.cfg.Chain.GenesisTime()
finalizedEpoch := s.cfg.Chain.FinalizedCheckpt().Epoch
maxEpoch := slotutil.EpochsSinceGenesis(genesis)
// It would take a minimum of 2 epochs to finalize a
// previous epoch
maxFinalizedEpoch := types.Epoch(0)
if maxEpoch > 2 {
maxFinalizedEpoch = maxEpoch - 2
}
if msg.FinalizedEpoch > maxFinalizedEpoch {
return p2ptypes.ErrInvalidEpoch
}
// Exit early if the peer's finalized epoch
// is less than that of the remote peer's.
if finalizedEpoch < msg.FinalizedEpoch {
return nil
}
finalizedAtGenesis := msg.FinalizedEpoch == 0
rootIsEqual := bytes.Equal(params.BeaconConfig().ZeroHash[:], msg.FinalizedRoot)
// If peer is at genesis with the correct genesis root hash we exit.
if finalizedAtGenesis && rootIsEqual {
return nil
}
if !s.cfg.DB.IsFinalizedBlock(ctx, bytesutil.ToBytes32(msg.FinalizedRoot)) {
return p2ptypes.ErrInvalidFinalizedRoot
}
blk, err := s.cfg.DB.Block(ctx, bytesutil.ToBytes32(msg.FinalizedRoot))
if err != nil {
return p2ptypes.ErrGeneric
}
if blk == nil || blk.IsNil() {
return p2ptypes.ErrGeneric
}
if helpers.SlotToEpoch(blk.Block().Slot()) == msg.FinalizedEpoch {
return nil
}
startSlot, err := helpers.StartSlot(msg.FinalizedEpoch)
if err != nil {
return p2ptypes.ErrGeneric
}
if startSlot > blk.Block().Slot() {
childBlock, err := s.cfg.DB.FinalizedChildBlock(ctx, bytesutil.ToBytes32(msg.FinalizedRoot))
if err != nil {
return p2ptypes.ErrGeneric
}
// Is a valid finalized block if no
// other child blocks exist yet.
if childBlock == nil || childBlock.IsNil() {
return nil
}
// If child finalized block also has a smaller or
// equal slot number we return an error.
if startSlot >= childBlock.Block().Slot() {
return p2ptypes.ErrInvalidEpoch
}
return nil
}
return p2ptypes.ErrInvalidEpoch
}