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