forked from prysmaticlabs/prysm
/
pending_blocks_queue.go
188 lines (162 loc) · 5.32 KB
/
pending_blocks_queue.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
package sync
import (
"context"
"encoding/hex"
"sort"
"sync"
"time"
"github.com/pkg/errors"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-ssz"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/runutil"
"github.com/prysmaticlabs/prysm/shared/traceutil"
"github.com/sirupsen/logrus"
"go.opencensus.io/trace"
"golang.org/x/exp/rand"
)
var processPendingBlocksPeriod = time.Duration(params.BeaconConfig().SecondsPerSlot/3) * time.Second
// processes pending blocks queue on every processPendingBlocksPeriod
func (r *Service) processPendingBlocksQueue() {
ctx := context.Background()
locker := new(sync.Mutex)
runutil.RunEvery(r.ctx, processPendingBlocksPeriod, func() {
locker.Lock()
r.processPendingBlocks(ctx)
locker.Unlock()
})
}
// processes the block tree inside the queue
func (r *Service) processPendingBlocks(ctx context.Context) error {
ctx, span := trace.StartSpan(ctx, "processPendingBlocks")
defer span.End()
pids := r.p2p.Peers().Connected()
if err := r.validatePendingSlots(); err != nil {
return errors.Wrap(err, "could not validate pending slots")
}
slots := r.sortedPendingSlots()
span.AddAttributes(
trace.Int64Attribute("numSlots", int64(len(slots))),
trace.Int64Attribute("numPeers", int64(len(pids))),
)
for _, s := range slots {
ctx, span := trace.StartSpan(ctx, "processPendingBlocks.InnerLoop")
span.AddAttributes(trace.Int64Attribute("slot", int64(s)))
r.pendingQueueLock.RLock()
b := r.slotToPendingBlocks[uint64(s)]
// Skip if block does not exist.
if b == nil || b.Block == nil {
r.pendingQueueLock.RUnlock()
span.End()
continue
}
r.pendingQueueLock.RUnlock()
inPendingQueue := r.seenPendingBlocks[bytesutil.ToBytes32(b.Block.ParentRoot)]
inDB := r.db.HasBlock(ctx, bytesutil.ToBytes32(b.Block.ParentRoot))
hasPeer := len(pids) != 0
// Only request for missing parent block if it's not in DB, not in pending cache
// and has peer in the peer list.
if !inPendingQueue && !inDB && hasPeer {
log.WithFields(logrus.Fields{
"currentSlot": b.Block.Slot,
"parentRoot": hex.EncodeToString(bytesutil.Trunc(b.Block.ParentRoot)),
}).Info("Requesting parent block")
req := [][32]byte{bytesutil.ToBytes32(b.Block.ParentRoot)}
// Start with a random peer to query, but choose the first peer in our unsorted list that claims to
// have a head slot newer than the block slot we are requesting.
pid := pids[rand.Int()%len(pids)]
for _, p := range pids {
if cs, _ := r.p2p.Peers().ChainState(p); cs != nil && cs.HeadSlot >= uint64(s) {
pid = p
break
}
}
if err := r.sendRecentBeaconBlocksRequest(ctx, req, pid); err != nil {
traceutil.AnnotateError(span, err)
log.Errorf("Could not send recent block request: %v", err)
}
span.End()
continue
}
if !inDB {
span.End()
continue
}
if err := r.chain.ReceiveBlockNoPubsub(ctx, b); err != nil {
log.Errorf("Could not process block from slot %d: %v", b.Block.Slot, err)
traceutil.AnnotateError(span, err)
}
// Broadcasting the block again once a node is able to process it.
if err := r.p2p.Broadcast(ctx, b); err != nil {
log.WithError(err).Error("Failed to broadcast block")
}
blkRoot, err := ssz.HashTreeRoot(b.Block)
if err != nil {
traceutil.AnnotateError(span, err)
span.End()
return err
}
r.pendingQueueLock.Lock()
delete(r.slotToPendingBlocks, uint64(s))
delete(r.seenPendingBlocks, blkRoot)
r.pendingQueueLock.Unlock()
log.WithFields(logrus.Fields{
"slot": s,
"blockRoot": hex.EncodeToString(bytesutil.Trunc(blkRoot[:])),
}).Info("Processed pending block and cleared it in cache")
span.End()
}
return nil
}
func (r *Service) sortedPendingSlots() []int {
r.pendingQueueLock.RLock()
defer r.pendingQueueLock.RUnlock()
slots := make([]int, 0, len(r.slotToPendingBlocks))
for s := range r.slotToPendingBlocks {
slots = append(slots, int(s))
}
sort.Ints(slots)
return slots
}
// validatePendingSlots validates the pending blocks
// by their slot. If they are before the current finalized
// checkpoint, these blocks are removed from the queue.
func (r *Service) validatePendingSlots() error {
r.pendingQueueLock.Lock()
defer r.pendingQueueLock.Unlock()
oldBlockRoots := make(map[[32]byte]bool)
finalizedEpoch := r.chain.FinalizedCheckpt().Epoch
for s, b := range r.slotToPendingBlocks {
epoch := helpers.SlotToEpoch(s)
// remove all descendant blocks of old blocks
if oldBlockRoots[bytesutil.ToBytes32(b.Block.ParentRoot)] {
root, err := ssz.HashTreeRoot(b.Block)
if err != nil {
return err
}
oldBlockRoots[root] = true
delete(r.slotToPendingBlocks, s)
delete(r.seenPendingBlocks, root)
continue
}
// don't process old blocks
if finalizedEpoch > 0 && epoch <= finalizedEpoch {
blkRoot, err := ssz.HashTreeRoot(b.Block)
if err != nil {
return err
}
oldBlockRoots[blkRoot] = true
delete(r.slotToPendingBlocks, s)
delete(r.seenPendingBlocks, blkRoot)
}
}
return nil
}
func (r *Service) clearPendingSlots() {
r.pendingQueueLock.Lock()
defer r.pendingQueueLock.Unlock()
r.slotToPendingBlocks = make(map[uint64]*ethpb.SignedBeaconBlock)
r.seenPendingBlocks = make(map[[32]byte]bool)
}