MCPcopy
hub / github.com/canopy-network/canopy / sendBlockRequests

Method sendBlockRequests

controller/consensus.go:207–242  ·  view source on GitHub ↗

Send requests for heights missing in the queue They can be missing because: - Sync has just started and queue isn't full yet - A previous request for a height timed out and was removed from queue - A previous request for a height was removed from the queue for processing Failure in processing leaves

(start, stop uint64, queue map[uint64]blockSyncRequest, limiter *lib.SimpleLimiter, peers []string)

Source from the content-addressed store, hash-verified

205// Failure in processing leaves that height missing in the queue, triggering another request
206// - Block height has advanced and there's room at the end of the queue
207func (c *Controller) sendBlockRequests(start, stop uint64, queue map[uint64]blockSyncRequest, limiter *lib.SimpleLimiter, peers []string) {
208 // Send requests to populate the queue
209 for height := start; height < stop; height++ {
210 // A block request has already been sent for this height
211 if _, ok := queue[height]; ok {
212 continue
213 }
214 // Find a random peer that is not rate limited
215 allowedPeer := getRandomAllowedPeer(peers, limiter)
216 if allowedPeer == "" {
217 // All peers rate-limited, cannot send any more requests
218 return
219 }
220 peerPublicKey, _ := lib.StringToBytes(allowedPeer)
221
222 c.log.Debugf("Requesting block for height %d 🔄 from %s", height, lib.BytesToTruncatedString(peerPublicKey))
223
224 // Send block request to selected peer
225 err := c.P2P.SendTo(peerPublicKey, BlockRequest, &lib.BlockRequestMessage{
226 ChainId: c.Config.ChainId,
227 Height: height,
228 HeightOnly: false,
229 })
230 if err != nil {
231 c.log.Errorf("Error requesting block for height %d 🔄 from %s", height, lib.BytesToTruncatedString(peerPublicKey))
232 break
233 }
234
235 // Add new request to queue
236 queue[height] = blockSyncRequest{
237 timestamp: time.Now(),
238 height: height,
239 peerPublicKey: peerPublicKey,
240 }
241 }
242}
243
244// applyTimeouts removes requests from the queue that have timed out
245func (c *Controller) applyTimeouts(queue map[uint64]blockSyncRequest) []blockSyncRequest {

Callers 1

SyncMethod · 0.95

Calls 6

StringToBytesFunction · 0.92
BytesToTruncatedStringFunction · 0.92
getRandomAllowedPeerFunction · 0.85
SendToMethod · 0.80
DebugfMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected