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

View file

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