wire: Populate cfheaders message with filter hashes instead of headers.

This commit is contained in:
Jim Posen 2018-01-22 13:14:46 -08:00 committed by Olaoluwa Osuntokun
parent 175af18043
commit 4c991c8783
2 changed files with 22 additions and 23 deletions

View file

@ -820,11 +820,11 @@ func (sp *serverPeer) OnGetCFHeaders(_ *peer.Peer, msg *wire.MsgGetCFHeaders) {
hashPtrs[i] = &hashList[i]
}
// Fetch the raw filter header bytes from the database for all blocks.
filterHeaders, err := sp.server.cfIndex.FilterHeadersByBlockHashes(hashPtrs,
// Fetch the raw filter hash bytes from the database for all blocks.
filterHashes, err := sp.server.cfIndex.FilterHashesByBlockHashes(hashPtrs,
msg.FilterType)
if err != nil {
peerLog.Errorf("Error retrieving cfilters: %v", err)
peerLog.Errorf("Error retrieving cfilter hashes: %v", err)
return
}
@ -857,30 +857,29 @@ func (sp *serverPeer) OnGetCFHeaders(_ *peer.Peer, msg *wire.MsgGetCFHeaders) {
}
hashList = hashList[1:]
filterHeaders = filterHeaders[1:]
filterHashes = filterHashes[1:]
}
// Populate HeaderHashes.
for i, headerBytes := range filterHeaders {
if len(headerBytes) == 0 {
peerLog.Warnf("Could not obtain CF header for %v", hashList[i])
for i, hashBytes := range filterHashes {
if len(hashBytes) == 0 {
peerLog.Warnf("Could not obtain CF hash for %v", hashList[i])
return
}
// Deserialize the hash.
var header chainhash.Hash
err = header.SetBytes(headerBytes)
filterHash, err := chainhash.NewHash(hashBytes)
if err != nil {
peerLog.Warnf("Committed filter header deserialize "+
peerLog.Warnf("Committed filter hash deserialize "+
"failed: %v", err)
return
}
headersMsg.AddCFHeader(&header)
headersMsg.AddCFHash(filterHash)
}
headersMsg.FilterType = msg.FilterType
headersMsg.StopHash = hashList[len(hashList)-1]
headersMsg.StopHash = msg.StopHash
sp.QueueMessage(headersMsg, nil)
}

View file

@ -30,18 +30,18 @@ type MsgCFHeaders struct {
FilterType FilterType
StopHash chainhash.Hash
PrevFilterHeader chainhash.Hash
HeaderHashes []*chainhash.Hash
FilterHashes []*chainhash.Hash
}
// AddCFHeader adds a new committed filter header to the message.
func (msg *MsgCFHeaders) AddCFHeader(headerHash *chainhash.Hash) error {
if len(msg.HeaderHashes)+1 > MaxCFHeadersPerMsg {
func (msg *MsgCFHeaders) AddCFHash(hash *chainhash.Hash) error {
if len(msg.FilterHashes)+1 > MaxCFHeadersPerMsg {
str := fmt.Sprintf("too many block headers in message [max %v]",
MaxBlockHeadersPerMsg)
return messageError("MsgCFHeaders.AddCFHeader", str)
return messageError("MsgCFHeaders.AddCFHash", str)
}
msg.HeaderHashes = append(msg.HeaderHashes, headerHash)
msg.FilterHashes = append(msg.FilterHashes, hash)
return nil
}
@ -80,16 +80,16 @@ func (msg *MsgCFHeaders) BtcDecode(r io.Reader, pver uint32, _ MessageEncoding)
return messageError("MsgCFHeaders.BtcDecode", str)
}
// Create a contiguous slice of headers to deserialize into in order to
// Create a contiguous slice of hashes to deserialize into in order to
// reduce the number of allocations.
msg.HeaderHashes = make([]*chainhash.Hash, 0, count)
msg.FilterHashes = make([]*chainhash.Hash, 0, count)
for i := uint64(0); i < count; i++ {
var cfh chainhash.Hash
err := readElement(r, &cfh)
if err != nil {
return err
}
msg.AddCFHeader(&cfh)
msg.AddCFHash(&cfh)
}
return nil
@ -117,7 +117,7 @@ func (msg *MsgCFHeaders) BtcEncode(w io.Writer, pver uint32, _ MessageEncoding)
}
// Limit to max committed headers per message.
count := len(msg.HeaderHashes)
count := len(msg.FilterHashes)
if count > MaxCFHeadersPerMsg {
str := fmt.Sprintf("too many committed filter headers for "+
"message [count %v, max %v]", count,
@ -130,7 +130,7 @@ func (msg *MsgCFHeaders) BtcEncode(w io.Writer, pver uint32, _ MessageEncoding)
return err
}
for _, cfh := range msg.HeaderHashes {
for _, cfh := range msg.FilterHashes {
err := writeElement(w, cfh)
if err != nil {
return err
@ -175,6 +175,6 @@ func (msg *MsgCFHeaders) MaxPayloadLength(pver uint32) uint32 {
// the Message interface. See MsgCFHeaders for details.
func NewMsgCFHeaders() *MsgCFHeaders {
return &MsgCFHeaders{
HeaderHashes: make([]*chainhash.Hash, 0, MaxCFHeadersPerMsg),
FilterHashes: make([]*chainhash.Hash, 0, MaxCFHeadersPerMsg),
}
}