Syncrhonize access to the blockPeer map.

This commit is contained in:
Dave Collins 2013-09-12 12:10:48 -05:00
parent f5c03696f7
commit e4577ab5f4

View file

@ -43,6 +43,7 @@ type blockManager struct {
shutdown bool
blockChain *btcchain.BlockChain
blockPeer map[btcwire.ShaHash]*peer
blockPeerMutex sync.Mutex
receivedLogBlocks int64
receivedLogTx int64
lastBlockLogTime time.Time
@ -208,13 +209,17 @@ func (b *blockManager) handleBlockMsg(bmsg *blockMsg) {
// Keep track of which peer the block was sent from so the notification
// handler can request the parent blocks from the appropriate peer.
blockSha, _ := bmsg.block.Sha()
b.blockPeerMutex.Lock()
b.blockPeer[*blockSha] = bmsg.peer
b.blockPeerMutex.Unlock()
// Process the block to include validation, best chain selection, orphan
// handling, etc.
err := b.blockChain.ProcessBlock(bmsg.block)
if err != nil {
b.blockPeerMutex.Lock()
delete(b.blockPeer, *blockSha)
b.blockPeerMutex.Unlock()
log.Warnf("[BMGR] Failed to process block %v: %v", blockSha, err)
return
}
@ -222,7 +227,9 @@ func (b *blockManager) handleBlockMsg(bmsg *blockMsg) {
// Don't keep track of the peer that sent the block any longer if it's
// not an orphan.
if !b.blockChain.IsKnownOrphan(blockSha) {
b.blockPeerMutex.Lock()
delete(b.blockPeer, *blockSha)
b.blockPeerMutex.Unlock()
}
// Log info about the new block height.
@ -271,6 +278,9 @@ func (b *blockManager) handleNotifyMsg(notification *btcchain.Notification) {
switch notification.Type {
// An orphan block has been accepted by the block chain.
case btcchain.NTOrphanBlock:
b.blockPeerMutex.Lock()
defer b.blockPeerMutex.Unlock()
orphanHash := notification.Data.(*btcwire.ShaHash)
if peer, exists := b.blockPeer[*orphanHash]; exists {
orphanRoot := b.blockChain.GetOrphanRoot(orphanHash)