Finish getpeerinfo RPC syncnode field.

This commit adds code to get the current sync peer from the block manager
for use in the getpeerinfo RPC.
This commit is contained in:
Dave Collins 2014-02-04 00:24:40 -06:00
parent 591b0f431d
commit ba5e457c38
2 changed files with 17 additions and 1 deletions

View file

@ -93,6 +93,8 @@ type blockManager struct {
processingReqs bool
syncPeer *peer
msgChan chan interface{}
syncPeerRequest chan bool
syncPeerResult chan *peer
wg sync.WaitGroup
quit chan bool
@ -902,6 +904,10 @@ out:
// bitch and whine.
}
// Return the current sync peer.
case <-b.syncPeerRequest:
b.syncPeerResult <- b.syncPeer
case <-b.quit:
break out
}
@ -1104,6 +1110,12 @@ func (b *blockManager) Stop() error {
return nil
}
// SyncPeer returns the current sync peer.
func (b *blockManager) SyncPeer() *peer {
b.syncPeerRequest <- true
return <-b.syncPeerResult
}
// newBlockManager returns a new bitcoin block manager.
// Use Start to begin processing asynchronous block and inv updates.
func newBlockManager(s *server) (*blockManager, error) {
@ -1119,6 +1131,8 @@ func newBlockManager(s *server) (*blockManager, error) {
requestedBlocks: make(map[btcwire.ShaHash]bool),
lastBlockLogTime: time.Now(),
msgChan: make(chan interface{}, cfg.MaxPeers*3),
syncPeerRequest: make(chan bool, 1),
syncPeerResult: make(chan *peer, 1),
headerList: list.New(),
quit: make(chan bool),
}

View file

@ -314,11 +314,13 @@ func (s *server) handleQuery(querymsg interface{}, state *peerState) {
msg.reply <- nconnected
case getPeerInfoMsg:
syncPeer := s.blockManager.SyncPeer()
infos := make([]*PeerInfo, 0, state.peers.Len())
state.forAllPeers(func(p *peer) {
if !p.Connected() {
return
}
// A lot of this will make the race detector go mad,
// however it is statistics for purely informational purposes
// and we don't really care if they are raced to get the new
@ -336,7 +338,7 @@ func (s *server) handleQuery(querymsg interface{}, state *peerState) {
Inbound: p.inbound,
StartingHeight: p.lastBlock,
BanScore: 0,
SyncNode: false, // TODO(oga) for now. bm knows this.
SyncNode: p == syncPeer,
}
p.pingStatsMtx.Lock()
info.PingTime = p.lastPingMicros