Filter duplicate getblocks requests.

This commit adds detection and filtering for back-to-back duplicate
getblocks requests.  This is needed because the trigger for requesting
more blocks is receiving an orphan.  When the peer is further behind than
the number of blocks advertised via a single inventory message, the same
orphan block will be sent multiple times.  When the peer receives the
final inventory message, it too contains the orphan that was previously
sent.  This leads to a duplicate getblocks request that must be filtered
to prevent requesting the final series of blocks again.
This commit is contained in:
Dave Collins 2013-09-18 13:33:54 -05:00
parent 0195306ff7
commit 346ff6f9e2

32
peer.go
View file

@ -108,6 +108,9 @@ type peer struct {
knownInventory *MruInventoryMap knownInventory *MruInventoryMap
knownInvMutex sync.Mutex knownInvMutex sync.Mutex
lastBlock int32 lastBlock int32
prevGetBlocksBegin *btcwire.ShaHash
prevGetBlocksStop *btcwire.ShaHash
prevGetBlockMutex sync.Mutex
requestQueue *list.List requestQueue *list.List
invSendQueue *list.List invSendQueue *list.List
continueHash *btcwire.ShaHash continueHash *btcwire.ShaHash
@ -340,8 +343,30 @@ func (p *peer) pushBlockMsg(sha btcwire.ShaHash) error {
} }
// pushGetBlocksMsg sends a getblocks message for the provided block locator // pushGetBlocksMsg sends a getblocks message for the provided block locator
// and stop hash. // and stop hash. It will ignore back-to-back duplicate requests.
func (p *peer) pushGetBlocksMsg(locator btcchain.BlockLocator, stopHash *btcwire.ShaHash) error { func (p *peer) pushGetBlocksMsg(locator btcchain.BlockLocator, stopHash *btcwire.ShaHash) error {
p.prevGetBlockMutex.Lock()
defer p.prevGetBlockMutex.Unlock()
// Extract the begin hash from the block locator, if one was specified,
// to use for filtering duplicate getblocks requests.
// request.
var beginHash *btcwire.ShaHash
if len(locator) > 0 {
beginHash = locator[0]
}
// Filter duplicate getblocks requests.
if p.prevGetBlocksStop != nil && p.prevGetBlocksBegin != nil &&
beginHash != nil && stopHash.IsEqual(p.prevGetBlocksStop) &&
beginHash.IsEqual(p.prevGetBlocksBegin) {
log.Tracef("[PEER] Filtering duplicate [getblocks] with begin "+
"hash %v, stop hash %v", beginHash, stopHash)
return nil
}
// Construct the getblocks request and queue it to be sent.
msg := btcwire.NewMsgGetBlocks(stopHash) msg := btcwire.NewMsgGetBlocks(stopHash)
for _, hash := range locator { for _, hash := range locator {
err := msg.AddBlockLocatorHash(hash) err := msg.AddBlockLocatorHash(hash)
@ -350,6 +375,11 @@ func (p *peer) pushGetBlocksMsg(locator btcchain.BlockLocator, stopHash *btcwire
} }
} }
p.QueueMessage(msg) p.QueueMessage(msg)
// Update the previous getblocks request information for filtering
// duplicates.
p.prevGetBlocksBegin = beginHash
p.prevGetBlocksStop = stopHash
return nil return nil
} }