tendermint/blockchain/pool.go

586 lines
14 KiB
Go
Raw Normal View History

package blockchain
2015-03-22 03:30:22 -07:00
import (
"errors"
"fmt"
"math"
2015-03-24 11:02:30 -07:00
"sync"
"sync/atomic"
2015-03-22 03:30:22 -07:00
"time"
2018-07-01 19:36:49 -07:00
cmn "github.com/tendermint/tendermint/libs/common"
flow "github.com/tendermint/tendermint/libs/flowrate"
"github.com/tendermint/tendermint/libs/log"
2018-01-03 02:29:19 -08:00
"github.com/tendermint/tendermint/p2p"
"github.com/tendermint/tendermint/types"
2015-03-22 03:30:22 -07:00
)
/*
eg, L = latency = 0.1s
P = num peers = 10
FN = num full nodes
BS = 1kB block size
CB = 1 Mbit/s = 128 kB/s
CB/P = 12.8 kB
B/S = CB/P/BS = 12.8 blocks/s
12.8 * 0.1 = 1.28 blocks on conn
*/
2015-03-22 03:30:22 -07:00
const (
requestIntervalMS = 2
maxTotalRequesters = 600
maxPendingRequests = maxTotalRequesters
maxPendingRequestsPerPeer = 20
// Minimum recv rate to ensure we're receiving blocks from a peer fast
// enough. If a peer is not sending us data at at least that rate, we
// consider them to have timedout and we disconnect.
//
// Assuming a DSL connection (not a good choice) 128 Kbps (upload) ~ 15 KB/s,
// sending data across atlantic ~ 7.5 KB/s.
minRecvRate = 7680
// Maximum difference between current and new block's height.
maxDiffBetweenCurrentAndReceivedBlockHeight = 100
2015-03-22 19:20:54 -07:00
)
var peerTimeout = 15 * time.Second // not const so we can override with tests
/*
2015-07-10 08:39:49 -07:00
Peers self report their heights when we join the block pool.
Starting from our latest pool.height, we request blocks
in sequence from peers that reported higher heights than ours.
Every so often we ask peers what height they're on so we can keep going.
2017-01-13 01:16:50 -08:00
Requests are continuously made for blocks of higher heights until
2017-06-28 08:12:45 -07:00
the limit is reached. If most of the requests have no available peers, and we
are not at peer limits, we can probably switch to consensus reactor
*/
2015-03-22 03:30:22 -07:00
type BlockPool struct {
2017-10-04 13:40:45 -07:00
cmn.BaseService
2015-09-11 19:00:27 -07:00
startTime time.Time
2016-06-28 18:02:27 -07:00
mtx sync.Mutex
2015-03-24 11:02:30 -07:00
// block requests
requesters map[int64]*bpRequester
height int64 // the lowest key in requesters.
2015-03-24 11:02:30 -07:00
// peers
2018-01-01 18:27:38 -08:00
peers map[p2p.ID]*bpPeer
maxPeerHeight int64
2015-03-24 11:02:30 -07:00
// atomic
numPending int32 // number of requests pending assignment or block response
2015-03-24 11:02:30 -07:00
requestsCh chan<- BlockRequest
errorsCh chan<- peerError
2015-03-22 03:30:22 -07:00
}
func NewBlockPool(start int64, requestsCh chan<- BlockRequest, errorsCh chan<- peerError) *BlockPool {
bp := &BlockPool{
2018-01-01 18:27:38 -08:00
peers: make(map[p2p.ID]*bpPeer),
2015-03-24 11:02:30 -07:00
requesters: make(map[int64]*bpRequester),
height: start,
numPending: 0,
2015-03-22 03:30:22 -07:00
requestsCh: requestsCh,
errorsCh: errorsCh,
2015-03-22 03:30:22 -07:00
}
2017-10-04 13:40:45 -07:00
bp.BaseService = *cmn.NewBaseService(nil, "BlockPool", bp)
return bp
2015-03-22 03:30:22 -07:00
}
func (pool *BlockPool) OnStart() error {
go pool.makeRequestersRoutine()
2015-09-11 19:00:27 -07:00
pool.startTime = time.Now()
return nil
2015-03-22 03:30:22 -07:00
}
2017-10-20 12:56:21 -07:00
func (pool *BlockPool) OnStop() {}
2015-03-22 03:30:22 -07:00
// Run spawns requesters as needed.
func (pool *BlockPool) makeRequestersRoutine() {
2015-03-22 03:30:22 -07:00
for {
if !pool.IsRunning() {
break
2015-03-24 11:02:30 -07:00
}
2016-06-24 17:21:44 -07:00
_, numPending, lenRequesters := pool.GetStatus()
2015-05-28 03:18:13 -07:00
if numPending >= maxPendingRequests {
2015-03-24 11:02:30 -07:00
// sleep for a bit.
time.Sleep(requestIntervalMS * time.Millisecond)
2015-09-09 21:44:48 -07:00
// check for timed out peers
pool.removeTimedoutPeers()
2016-06-24 17:21:44 -07:00
} else if lenRequesters >= maxTotalRequesters {
2015-03-24 11:02:30 -07:00
// sleep for a bit.
time.Sleep(requestIntervalMS * time.Millisecond)
2015-09-09 21:44:48 -07:00
// check for timed out peers
pool.removeTimedoutPeers()
2015-03-24 11:02:30 -07:00
} else {
// request for more blocks.
pool.makeNextRequester()
2015-03-22 03:30:22 -07:00
}
}
}
2015-09-09 21:44:48 -07:00
func (pool *BlockPool) removeTimedoutPeers() {
2016-06-28 18:02:27 -07:00
pool.mtx.Lock()
defer pool.mtx.Unlock()
2015-09-09 21:44:48 -07:00
for _, peer := range pool.peers {
if !peer.didTimeout && peer.numPending > 0 {
curRate := peer.recvMonitor.Status().CurRate
2018-02-09 01:02:44 -08:00
// curRate can be 0 on start
if curRate != 0 && curRate < minRecvRate {
err := errors.New("peer is not sending us data fast enough")
pool.sendError(err, peer.id)
pool.Logger.Error("SendTimeout", "peer", peer.id,
"reason", err,
"curRate", fmt.Sprintf("%d KB/s", curRate/1024),
"minRate", fmt.Sprintf("%d KB/s", minRecvRate/1024))
2015-09-09 21:44:48 -07:00
peer.didTimeout = true
}
}
if peer.didTimeout {
pool.removePeer(peer.id)
}
}
}
func (pool *BlockPool) GetStatus() (height int64, numPending int32, lenRequesters int) {
2016-06-28 18:02:27 -07:00
pool.mtx.Lock()
defer pool.mtx.Unlock()
return pool.height, atomic.LoadInt32(&pool.numPending), len(pool.requesters)
}
// TODO: relax conditions, prevent abuse.
func (pool *BlockPool) IsCaughtUp() bool {
2016-06-28 18:02:27 -07:00
pool.mtx.Lock()
defer pool.mtx.Unlock()
2016-06-24 17:21:44 -07:00
2016-01-02 16:21:40 -08:00
// Need at least 1 peer to be considered caught up.
if len(pool.peers) == 0 {
2017-05-02 00:53:32 -07:00
pool.Logger.Debug("Blockpool has no peers")
2016-01-02 16:21:40 -08:00
return false
}
2017-06-23 19:34:38 -07:00
// some conditions to determine if we're caught up
receivedBlockOrTimedOut := (pool.height > 0 || time.Since(pool.startTime) > 5*time.Second)
ourChainIsLongestAmongPeers := pool.maxPeerHeight == 0 || pool.height >= pool.maxPeerHeight
2017-06-23 19:34:38 -07:00
isCaughtUp := receivedBlockOrTimedOut && ourChainIsLongestAmongPeers
2016-02-07 16:56:59 -08:00
return isCaughtUp
2015-03-24 11:02:30 -07:00
}
2015-03-22 16:23:24 -07:00
2016-04-02 09:10:16 -07:00
// We need to see the second block's Commit to validate the first block.
2015-03-24 11:02:30 -07:00
// So we peek two blocks at a time.
2016-04-02 09:10:16 -07:00
// The caller will verify the commit.
func (pool *BlockPool) PeekTwoBlocks() (first *types.Block, second *types.Block) {
2016-06-28 18:02:27 -07:00
pool.mtx.Lock()
defer pool.mtx.Unlock()
2015-03-24 11:02:30 -07:00
if r := pool.requesters[pool.height]; r != nil {
2015-09-12 08:47:59 -07:00
first = r.getBlock()
2015-03-24 11:02:30 -07:00
}
if r := pool.requesters[pool.height+1]; r != nil {
2015-09-12 08:47:59 -07:00
second = r.getBlock()
2015-03-22 03:30:22 -07:00
}
2015-03-24 11:02:30 -07:00
return
2015-03-22 03:30:22 -07:00
}
// Pop the first block at pool.height
2016-04-02 09:10:16 -07:00
// It must have been validated by 'second'.Commit from PeekTwoBlocks().
func (pool *BlockPool) PopRequest() {
2016-06-28 18:02:27 -07:00
pool.mtx.Lock()
defer pool.mtx.Unlock()
2015-03-24 11:02:30 -07:00
if r := pool.requesters[pool.height]; r != nil {
/* The block can disappear at any time, due to removePeer().
if r := pool.requesters[pool.height]; r == nil || r.block == nil {
PanicSanity("PopRequest() requires a valid block")
}
*/
r.Stop()
delete(pool.requesters, pool.height)
pool.height++
} else {
2018-02-26 05:33:55 -08:00
panic(fmt.Sprintf("Expected requester to pop, got nothing at height %v", pool.height))
2015-03-22 03:30:22 -07:00
}
2015-03-22 12:46:53 -07:00
}
// Invalidates the block at pool.height,
// Remove the peer and redo request from others.
// Returns the ID of the removed peer.
func (pool *BlockPool) RedoRequest(height int64) p2p.ID {
2016-06-28 18:02:27 -07:00
pool.mtx.Lock()
defer pool.mtx.Unlock()
request := pool.requesters[height]
peerID := request.getPeerID()
if peerID != p2p.ID("") {
// RemovePeer will redo all requesters associated with this peer.
pool.removePeer(peerID)
2015-03-22 03:30:22 -07:00
}
return peerID
2015-03-22 03:30:22 -07:00
}
// TODO: ensure that blocks come in order for each peer.
2018-01-01 18:27:38 -08:00
func (pool *BlockPool) AddBlock(peerID p2p.ID, block *types.Block, blockSize int) {
2016-06-28 18:02:27 -07:00
pool.mtx.Lock()
defer pool.mtx.Unlock()
2015-03-24 11:02:30 -07:00
requester := pool.requesters[block.Height]
if requester == nil {
pool.Logger.Info("peer sent us a block we didn't expect", "peer", peerID, "curHeight", pool.height, "blockHeight", block.Height)
diff := pool.height - block.Height
if diff < 0 {
diff *= -1
}
if diff > maxDiffBetweenCurrentAndReceivedBlockHeight {
pool.sendError(errors.New("peer sent us a block we didn't expect with a height too far ahead/behind"), peerID)
}
2015-03-24 11:02:30 -07:00
return
2015-03-22 12:46:53 -07:00
}
if requester.setBlock(block, peerID) {
atomic.AddInt32(&pool.numPending, -1)
2016-06-28 18:02:27 -07:00
peer := pool.peers[peerID]
fix invalid memory address or nil pointer dereference error (Refs #762) https://github.com/tendermint/tendermint/issues/762#issuecomment-338276055 ``` E[10-19|04:52:38.969] Stopping peer for error module=p2p peer="Peer{MConn{178.62.46.14:46656} B14916FAF38A out}" err="Error: runtime error: invalid memory address or nil pointer dereference\nStack: goroutine 529485 [running]:\nruntime/debug.Stack(0xc4355cfb38, 0xb463e0, 0x11b1c30)\n\t/usr/local/go/src/runtime/debug/stack.go:24 +0xa7\ngithub.com/cosmos/gaia/vendor/github.com/tendermint/tendermint/p2p.(*MConnection)._recover(0xc439a28870)\n\t/home/ubuntu/go/src/github.com/cosmos/gaia/vendor/github.com/tendermint/tendermint/p2p/connection.go:206 +0x6e\npanic(0xb463e0, 0x11b1c30)\n\t/usr/local/go/src/runtime/panic.go:491 +0x283\ngithub.com/cosmos/gaia/vendor/github.com/tendermint/tendermint/blockchain.(*bpPeer).decrPending(0x0, 0x381)\n\t/home/ubuntu/go/src/github.com/cosmos/gaia/vendor/github.com/tendermint/tendermint/blockchain/pool.go:376 +0x22\ngithub.com/cosmos/gaia/vendor/github.com/tendermint/tendermint/blockchain.(*BlockPool).AddBlock(0xc4200e4000, 0xc4266d1f00, 0x40, 0xc432ac9640, 0x381)\n\t/home/ubuntu/go/src/github.com/cosmos/gaia/vendor/github.com/tendermint/tendermint/blockchain/pool.go:215 +0x139\ngithub.com/cosmos/gaia/vendor/github.com/tendermint/tendermint/blockchain.(*BlockchainReactor).Receive(0xc42050a780, 0xc420257740, 0x1171be0, 0xc42ff302d0, 0xc4384b2000, 0x381, 0x1000)\n\t/home/ubuntu/go/src/github.com/cosmos/gaia/vendor/github.com/tendermint/tendermint/blockchain/reactor.go:160 +0x712\ngithub.com/cosmos/gaia/vendor/github.com/tendermint/tendermint/p2p.createMConnection.func1(0x11e5040, 0xc4384b2000, 0x381, 0x1000)\n\t/home/ubuntu/go/src/github.com/cosmos/gaia/vendor/github.com/tendermint/tendermint/p2p/peer.go:334 +0xbd\ngithub.com/cosmos/gaia/vendor/github.com/tendermint/tendermint/p2p.(*MConnection).recvRoutine(0xc439a28870)\n\t/home/ubuntu/go/src/github.com/cosmos/gaia/vendor/github.com/tendermint/tendermint/p2p/connection.go:475 +0x4a3\ncreated by github.com/cosmos/gaia/vendor/github.com/tendermint/tendermint/p2p.(*MConnection).OnStart\n\t/home/ubuntu/go/src/github.com/cosmos/gaia/vendor/github.com/tendermint/tendermint/p2p/connection.go:170 +0x187\n" ```
2017-10-20 10:56:10 -07:00
if peer != nil {
peer.decrPending(blockSize)
}
2017-06-23 18:36:47 -07:00
} else {
// Bad peer?
}
}
2017-10-28 08:07:59 -07:00
// MaxPeerHeight returns the highest height reported by a peer.
func (pool *BlockPool) MaxPeerHeight() int64 {
pool.mtx.Lock()
defer pool.mtx.Unlock()
return pool.maxPeerHeight
}
// Sets the peer's alleged blockchain height.
2018-01-01 18:27:38 -08:00
func (pool *BlockPool) SetPeerHeight(peerID p2p.ID, height int64) {
2016-06-28 18:02:27 -07:00
pool.mtx.Lock()
defer pool.mtx.Unlock()
2015-03-24 11:02:30 -07:00
peer := pool.peers[peerID]
if peer != nil {
peer.height = height
} else {
peer = newBPPeer(pool, peerID, height)
2017-05-02 00:53:32 -07:00
peer.setLogger(pool.Logger.With("peer", peerID))
pool.peers[peerID] = peer
2015-03-24 11:02:30 -07:00
}
if height > pool.maxPeerHeight {
pool.maxPeerHeight = height
}
2015-03-24 11:02:30 -07:00
}
2018-01-01 18:27:38 -08:00
func (pool *BlockPool) RemovePeer(peerID p2p.ID) {
2016-06-28 18:02:27 -07:00
pool.mtx.Lock()
defer pool.mtx.Unlock()
2015-03-24 11:02:30 -07:00
pool.removePeer(peerID)
2015-03-22 12:46:53 -07:00
}
2018-01-01 18:27:38 -08:00
func (pool *BlockPool) removePeer(peerID p2p.ID) {
for _, requester := range pool.requesters {
if requester.getPeerID() == peerID {
requester.redo()
2015-03-22 12:46:53 -07:00
}
}
delete(pool.peers, peerID)
2015-03-22 12:46:53 -07:00
}
2015-03-24 11:02:30 -07:00
// Pick an available peer with at least the given minHeight.
// If no peers are available, returns nil.
func (pool *BlockPool) pickIncrAvailablePeer(minHeight int64) *bpPeer {
2016-06-28 18:02:27 -07:00
pool.mtx.Lock()
defer pool.mtx.Unlock()
2015-03-24 11:02:30 -07:00
for _, peer := range pool.peers {
2016-06-28 18:02:27 -07:00
if peer.didTimeout {
pool.removePeer(peer.id)
continue
}
if peer.numPending >= maxPendingRequestsPerPeer {
2015-03-24 11:02:30 -07:00
continue
}
if peer.height < minHeight {
continue
}
2016-06-28 18:02:27 -07:00
peer.incrPending()
2015-03-24 11:02:30 -07:00
return peer
2015-03-22 03:30:22 -07:00
}
2015-03-24 11:02:30 -07:00
return nil
2015-03-22 03:30:22 -07:00
}
func (pool *BlockPool) makeNextRequester() {
2016-06-28 18:02:27 -07:00
pool.mtx.Lock()
defer pool.mtx.Unlock()
2015-03-22 03:30:22 -07:00
2017-11-30 11:08:38 -08:00
nextHeight := pool.height + pool.requestersLen()
request := newBPRequester(pool, nextHeight)
// request.SetLogger(pool.Logger.With("height", nextHeight))
2015-03-24 11:02:30 -07:00
pool.requesters[nextHeight] = request
atomic.AddInt32(&pool.numPending, 1)
err := request.Start()
2017-09-06 10:11:47 -07:00
if err != nil {
2017-10-03 15:12:17 -07:00
request.Logger.Error("Error starting request", "err", err)
2017-09-06 10:11:47 -07:00
}
2015-03-22 03:30:22 -07:00
}
func (pool *BlockPool) requestersLen() int64 {
return int64(len(pool.requesters))
2017-11-30 11:08:38 -08:00
}
2018-01-01 18:27:38 -08:00
func (pool *BlockPool) sendRequest(height int64, peerID p2p.ID) {
if !pool.IsRunning() {
2015-03-24 11:02:30 -07:00
return
2015-03-22 03:30:22 -07:00
}
pool.requestsCh <- BlockRequest{height, peerID}
2015-03-22 03:30:22 -07:00
}
func (pool *BlockPool) sendError(err error, peerID p2p.ID) {
if !pool.IsRunning() {
2015-03-24 11:02:30 -07:00
return
}
pool.errorsCh <- peerError{err, peerID}
2015-03-24 11:02:30 -07:00
}
// unused by tendermint; left for debugging purposes
2017-06-23 18:36:47 -07:00
func (pool *BlockPool) debug() string {
pool.mtx.Lock()
defer pool.mtx.Unlock()
2015-03-24 11:02:30 -07:00
str := ""
2017-11-30 11:08:38 -08:00
nextHeight := pool.height + pool.requestersLen()
for h := pool.height; h < nextHeight; h++ {
if pool.requesters[h] == nil {
str += fmt.Sprintf("H(%v):X ", h)
2015-03-24 11:02:30 -07:00
} else {
str += fmt.Sprintf("H(%v):", h)
str += fmt.Sprintf("B?(%v) ", pool.requesters[h].block != nil)
2015-03-24 11:02:30 -07:00
}
}
return str
2017-06-23 18:36:47 -07:00
}
2015-03-22 03:30:22 -07:00
//-------------------------------------
2015-03-24 11:02:30 -07:00
type bpPeer struct {
pool *BlockPool
2018-01-01 18:27:38 -08:00
id p2p.ID
recvMonitor *flow.Monitor
2016-06-28 18:02:27 -07:00
height int64
2016-06-28 18:02:27 -07:00
numPending int32
timeout *time.Timer
didTimeout bool
2017-05-02 00:53:32 -07:00
logger log.Logger
2015-03-22 03:30:22 -07:00
}
2018-01-01 18:27:38 -08:00
func newBPPeer(pool *BlockPool, peerID p2p.ID, height int64) *bpPeer {
peer := &bpPeer{
pool: pool,
id: peerID,
height: height,
numPending: 0,
2017-05-02 00:53:32 -07:00
logger: log.NewNopLogger(),
}
return peer
}
2017-05-02 00:53:32 -07:00
func (peer *bpPeer) setLogger(l log.Logger) {
peer.logger = l
}
2015-09-12 08:47:59 -07:00
func (peer *bpPeer) resetMonitor() {
peer.recvMonitor = flow.New(time.Second, time.Second*40)
2017-09-11 12:45:12 -07:00
initialValue := float64(minRecvRate) * math.E
2015-09-12 08:47:59 -07:00
peer.recvMonitor.SetREMA(initialValue)
}
2016-06-28 18:02:27 -07:00
func (peer *bpPeer) resetTimeout() {
2015-09-12 08:47:59 -07:00
if peer.timeout == nil {
peer.timeout = time.AfterFunc(peerTimeout, peer.onTimeout)
} else {
peer.timeout.Reset(peerTimeout)
}
}
2016-06-28 18:02:27 -07:00
func (peer *bpPeer) incrPending() {
2015-09-12 08:47:59 -07:00
if peer.numPending == 0 {
peer.resetMonitor()
2016-06-28 18:02:27 -07:00
peer.resetTimeout()
}
2015-09-12 08:47:59 -07:00
peer.numPending++
}
2016-06-28 18:02:27 -07:00
func (peer *bpPeer) decrPending(recvSize int) {
2015-09-12 08:47:59 -07:00
peer.numPending--
if peer.numPending == 0 {
peer.timeout.Stop()
} else {
2015-09-12 08:47:59 -07:00
peer.recvMonitor.Update(recvSize)
2016-06-28 18:02:27 -07:00
peer.resetTimeout()
}
}
2015-09-12 08:47:59 -07:00
func (peer *bpPeer) onTimeout() {
2016-06-28 18:02:27 -07:00
peer.pool.mtx.Lock()
defer peer.pool.mtx.Unlock()
err := errors.New("peer did not send us anything")
peer.pool.sendError(err, peer.id)
peer.logger.Error("SendTimeout", "reason", err, "timeout", peerTimeout)
2015-09-12 08:47:59 -07:00
peer.didTimeout = true
}
2015-03-24 11:02:30 -07:00
//-------------------------------------
type bpRequester struct {
2017-10-04 13:40:45 -07:00
cmn.BaseService
pool *BlockPool
height int64
gotBlockCh chan struct{}
redoCh chan struct{}
mtx sync.Mutex
2018-01-01 18:27:38 -08:00
peerID p2p.ID
block *types.Block
}
func newBPRequester(pool *BlockPool, height int64) *bpRequester {
bpr := &bpRequester{
pool: pool,
height: height,
gotBlockCh: make(chan struct{}, 1),
redoCh: make(chan struct{}, 1),
peerID: "",
block: nil,
}
2017-10-04 13:40:45 -07:00
bpr.BaseService = *cmn.NewBaseService(nil, "bpRequester", bpr)
return bpr
}
func (bpr *bpRequester) OnStart() error {
go bpr.requestRoutine()
return nil
}
// Returns true if the peer matches and block doesn't already exist.
2018-01-01 18:27:38 -08:00
func (bpr *bpRequester) setBlock(block *types.Block, peerID p2p.ID) bool {
bpr.mtx.Lock()
if bpr.block != nil || bpr.peerID != peerID {
bpr.mtx.Unlock()
return false
}
bpr.block = block
bpr.mtx.Unlock()
select {
case bpr.gotBlockCh <- struct{}{}:
default:
}
return true
}
2015-09-12 08:47:59 -07:00
func (bpr *bpRequester) getBlock() *types.Block {
bpr.mtx.Lock()
defer bpr.mtx.Unlock()
return bpr.block
}
2018-01-01 18:27:38 -08:00
func (bpr *bpRequester) getPeerID() p2p.ID {
bpr.mtx.Lock()
defer bpr.mtx.Unlock()
return bpr.peerID
}
// This is called from the requestRoutine, upon redo().
func (bpr *bpRequester) reset() {
bpr.mtx.Lock()
defer bpr.mtx.Unlock()
if bpr.block != nil {
atomic.AddInt32(&bpr.pool.numPending, 1)
}
bpr.peerID = ""
bpr.block = nil
}
// Tells bpRequester to pick another peer and try again.
// NOTE: Nonblocking, and does nothing if another redo
// was already requested.
func (bpr *bpRequester) redo() {
select {
case bpr.redoCh <- struct{}{}:
default:
}
}
2015-03-24 11:02:30 -07:00
// Responsible for making more requests as necessary
// Returns only when a block is found (e.g. AddBlock() is called)
func (bpr *bpRequester) requestRoutine() {
OUTER_LOOP:
2015-03-24 11:02:30 -07:00
for {
// Pick a peer to send request to.
2018-01-03 02:29:19 -08:00
var peer *bpPeer
PICK_PEER_LOOP:
2015-03-24 11:02:30 -07:00
for {
if !bpr.IsRunning() || !bpr.pool.IsRunning() {
2015-03-24 11:02:30 -07:00
return
}
peer = bpr.pool.pickIncrAvailablePeer(bpr.height)
2015-03-24 11:02:30 -07:00
if peer == nil {
2015-07-19 14:49:13 -07:00
//log.Info("No peers available", "height", height)
2015-03-24 11:02:30 -07:00
time.Sleep(requestIntervalMS * time.Millisecond)
continue PICK_PEER_LOOP
2015-03-24 11:02:30 -07:00
}
break PICK_PEER_LOOP
2015-03-24 11:02:30 -07:00
}
bpr.mtx.Lock()
bpr.peerID = peer.id
bpr.mtx.Unlock()
// Send request and wait.
bpr.pool.sendRequest(bpr.height, peer.id)
WAIT_LOOP:
for {
select {
case <-bpr.pool.Quit():
bpr.Stop()
2015-03-24 11:02:30 -07:00
return
case <-bpr.Quit():
2015-03-24 11:02:30 -07:00
return
case <-bpr.redoCh:
bpr.reset()
continue OUTER_LOOP
case <-bpr.gotBlockCh:
// We got a block!
// Continue the for-loop and wait til Quit.
continue WAIT_LOOP
2015-03-24 11:02:30 -07:00
}
}
}
}
//-------------------------------------
type BlockRequest struct {
Height int64
2018-01-01 18:27:38 -08:00
PeerID p2p.ID
2015-03-22 03:30:22 -07:00
}