(startHeight, stopHeight uint64, queue map[uint64]blockSyncRequest)
| 149 | } |
| 150 | |
| 151 | func (c *Controller) processQueue(startHeight, stopHeight uint64, queue map[uint64]blockSyncRequest) (maxReceivedHeight, minVDFIterations uint64) { |
| 152 | for height := startHeight; height < stopHeight; height++ { |
| 153 | // Get the next height to be sent to FSM. This height is the required next height |
| 154 | req, success := queue[height] |
| 155 | // If required block not present, break and keep waiting |
| 156 | if !success { |
| 157 | c.log.Debugf("Height %d not found in queue, queue size: %d", height, len(queue)) |
| 158 | break |
| 159 | } |
| 160 | // Request has been sent but response yet to be received |
| 161 | if req.blockMessage == nil { |
| 162 | break |
| 163 | } |
| 164 | // remove request from queue |
| 165 | delete(queue, height) |
| 166 | // convenience variable |
| 167 | blockMsg := req.blockMessage |
| 168 | // start timing the HandlePeerBlock call |
| 169 | start := time.Now() |
| 170 | // lock the controller |
| 171 | c.Lock() |
| 172 | // process the block message received from the peer |
| 173 | _, err := c.HandlePeerBlock(blockMsg, true) |
| 174 | // unlock controller |
| 175 | c.Unlock() |
| 176 | // check error from HandlePeerBlock |
| 177 | if err != nil { |
| 178 | h := blockMsg.BlockAndCertificate.Header.Height |
| 179 | // log this unexpected behavior |
| 180 | c.log.Warnf("Syncing peer block height %d invalid:\n%s", h, err.Error()) |
| 181 | // slash the reputation of the peer |
| 182 | c.P2P.ChangeReputation(req.message.Sender.Address.PublicKey, p2p.InvalidBlockRep) |
| 183 | break |
| 184 | } |
| 185 | // calculate and log the elapsed time |
| 186 | elapsed := time.Since(start) |
| 187 | c.log.Infof("Block %d sync complete. HandlePeerBlock took %s", height, elapsed) |
| 188 | // calculate and log the elapsed time |
| 189 | // success, increase the peer reputation |
| 190 | c.P2P.ChangeReputation(req.message.Sender.Address.PublicKey, p2p.GoodBlockRep) |
| 191 | // check if max height and minimum vdf iterations should be updated |
| 192 | if blockMsg.MaxHeight > maxReceivedHeight && blockMsg.TotalVdfIterations >= minVDFIterations { |
| 193 | // update the max height and vdf iterations |
| 194 | maxReceivedHeight, minVDFIterations = blockMsg.MaxHeight, blockMsg.TotalVdfIterations |
| 195 | } |
| 196 | } |
| 197 | return |
| 198 | } |
| 199 | |
| 200 | // Send requests for heights missing in the queue |
| 201 | // They can be missing because: |
no test coverage detected