MCPcopy Create free account
hub / github.com/chain/Core / DownloadBlocks

Function DownloadBlocks

core/fetch/fetch.go:93–128  ·  view source on GitHub ↗

DownloadBlocks starts a goroutine to download blocks from the given peer, starting at the given height and incrementing from there. It will re-attempt downloads for the next block in the network until it is available. It returns two channels, one for reading blocks and the other for reading errors.

(ctx context.Context, peer *rpc.Client, height uint64)

Source from the content-addressed store, hash-verified

91// reading from both. DownloadBlocks will continue even if it encounters errors,
92// until its context is done.
93func DownloadBlocks(ctx context.Context, peer *rpc.Client, height uint64) (chan *legacy.Block, chan error) {
94 blockch := make(chan *legacy.Block)
95 errch := make(chan error)
96 go func() {
97 var nfailures uint // for backoff
98 var ntimeouts uint // for backoff
99 for {
100 select {
101 case <-ctx.Done():
102 close(blockch)
103 close(errch)
104 return
105 default:
106 block, err := getBlock(ctx, peer, height, timeoutBackoffDur(ntimeouts))
107 if err != nil {
108 errch <- err
109 nfailures++
110 time.Sleep(backoffDur(nfailures))
111 continue
112 }
113 if block == nil {
114 // Request time out. There might not have been any blocks published,
115 // or there was a network error or it just took too long to process the
116 // request.
117 ntimeouts++
118 continue
119 }
120
121 blockch <- block
122 ntimeouts, nfailures = 0, 0
123 height++
124 }
125 }
126 }()
127 return blockch, errch
128}
129
130func pollGeneratorHeight(ctx context.Context, peer *rpc.Client) {
131 updateGeneratorHeight(ctx, peer)

Callers 1

FetchFunction · 0.85

Calls 3

getBlockFunction · 0.85
timeoutBackoffDurFunction · 0.85
backoffDurFunction · 0.70

Tested by

no test coverage detected