Make use of the new btcdb functions.

This commit switches the handleGetHeadersMsg function to make use of the
new FetchBlockHeightBySha and FetchBlockHeaderBySha functions in btcdb.

Also, while here, nuke the header copy which is no longer required due to
the recent btcwire changes.
This commit is contained in:
Dave Collins 2014-01-18 21:11:35 -06:00
parent 33bb455365
commit f309e899f3

27
peer.go
View file

@ -722,9 +722,9 @@ func (p *peer) handleGetBlocksMsg(msg *btcwire.MsgGetBlocks) {
func (p *peer) handleGetHeadersMsg(msg *btcwire.MsgGetHeaders) { func (p *peer) handleGetHeadersMsg(msg *btcwire.MsgGetHeaders) {
// Attempt to look up the height of the provided stop hash. // Attempt to look up the height of the provided stop hash.
endIdx := btcdb.AllShas endIdx := btcdb.AllShas
block, err := p.server.db.FetchBlockBySha(&msg.HashStop) height, err := p.server.db.FetchBlockHeightBySha(&msg.HashStop)
if err == nil { if err == nil {
endIdx = block.Height() + 1 endIdx = height + 1
} }
// There are no block locators so a specific header is being requested // There are no block locators so a specific header is being requested
@ -737,10 +737,16 @@ func (p *peer) handleGetHeadersMsg(msg *btcwire.MsgGetHeaders) {
return return
} }
// Send the requested block header. // Fetch and send the requested block header.
header, err := p.server.db.FetchBlockHeaderBySha(&msg.HashStop)
if err != nil {
peerLog.Warnf("Lookup of known block hash failed: %v",
err)
return
}
headersMsg := btcwire.NewMsgHeaders() headersMsg := btcwire.NewMsgHeaders()
hdr := block.MsgBlock().Header // copy headersMsg.AddBlockHeader(header)
headersMsg.AddBlockHeader(&hdr)
p.QueueMessage(headersMsg, nil) p.QueueMessage(headersMsg, nil)
return return
} }
@ -752,10 +758,10 @@ func (p *peer) handleGetHeadersMsg(msg *btcwire.MsgGetHeaders) {
// This mirrors the behavior in the reference implementation. // This mirrors the behavior in the reference implementation.
startIdx := int64(1) startIdx := int64(1)
for _, hash := range msg.BlockLocatorHashes { for _, hash := range msg.BlockLocatorHashes {
block, err := p.server.db.FetchBlockBySha(hash) height, err := p.server.db.FetchBlockHeightBySha(hash)
if err == nil { if err == nil {
// Start with the next hash since we know this one. // Start with the next hash since we know this one.
startIdx = block.Height() + 1 startIdx = height + 1
break break
} }
} }
@ -767,7 +773,7 @@ func (p *peer) handleGetHeadersMsg(msg *btcwire.MsgGetHeaders) {
// Generate headers message and send it. // Generate headers message and send it.
// //
// The FetchBlockBySha call is limited to a maximum number of hashes // The FetchHeightRange call is limited to a maximum number of hashes
// per invocation. Since the maximum number of headers per message // per invocation. Since the maximum number of headers per message
// might be larger, call it multiple times with the appropriate indices // might be larger, call it multiple times with the appropriate indices
// as needed. // as needed.
@ -788,14 +794,13 @@ func (p *peer) handleGetHeadersMsg(msg *btcwire.MsgGetHeaders) {
// Add headers to the message. // Add headers to the message.
for _, hash := range hashList { for _, hash := range hashList {
block, err := p.server.db.FetchBlockBySha(&hash) header, err := p.server.db.FetchBlockHeaderBySha(&hash)
if err != nil { if err != nil {
peerLog.Warnf("Lookup of known block hash "+ peerLog.Warnf("Lookup of known block hash "+
"failed: %v", err) "failed: %v", err)
continue continue
} }
hdr := block.MsgBlock().Header // copy headersMsg.AddBlockHeader(header)
headersMsg.AddBlockHeader(&hdr)
} }
// Start at the next block header after the latest one on the // Start at the next block header after the latest one on the