2015-05-01 08:28:01 +02:00
|
|
|
// Copyright (c) 2013-2015 The btcsuite developers
|
2013-05-08 21:31:00 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-01-31 19:32:55 +01:00
|
|
|
package wire
|
2013-05-08 21:31:00 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MsgNotFound defines a bitcoin notfound message which is sent in response to
|
|
|
|
// a getdata message if any of the requested data in not available on the peer.
|
|
|
|
// Each message is limited to a maximum number of inventory vectors, which is
|
|
|
|
// currently 50,000.
|
|
|
|
//
|
|
|
|
// Use the AddInvVect function to build up the list of inventory vectors when
|
|
|
|
// sending a notfound message to another peer.
|
|
|
|
type MsgNotFound struct {
|
|
|
|
InvList []*InvVect
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddInvVect adds an inventory vector to the message.
|
|
|
|
func (msg *MsgNotFound) AddInvVect(iv *InvVect) error {
|
|
|
|
if len(msg.InvList)+1 > MaxInvPerMsg {
|
2013-05-10 18:32:20 +02:00
|
|
|
str := fmt.Sprintf("too many invvect in message [max %v]",
|
|
|
|
MaxInvPerMsg)
|
|
|
|
return messageError("MsgNotFound.AddInvVect", str)
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
msg.InvList = append(msg.InvList, iv)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
|
|
// This is part of the Message interface implementation.
|
2016-10-19 01:21:48 +02:00
|
|
|
func (msg *MsgNotFound) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
|
2016-02-22 17:13:11 +01:00
|
|
|
count, err := ReadVarInt(r, pver)
|
2013-05-08 21:31:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Limit to max inventory vectors per message.
|
|
|
|
if count > MaxInvPerMsg {
|
2013-05-10 18:32:20 +02:00
|
|
|
str := fmt.Sprintf("too many invvect in message [%v]", count)
|
|
|
|
return messageError("MsgNotFound.BtcDecode", str)
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
2016-04-21 23:49:38 +02:00
|
|
|
// Create a contiguous slice of inventory vectors to deserialize into in
|
|
|
|
// order to reduce the number of allocations.
|
|
|
|
invList := make([]InvVect, count)
|
2013-09-25 20:57:01 +02:00
|
|
|
msg.InvList = make([]*InvVect, 0, count)
|
2013-05-08 21:31:00 +02:00
|
|
|
for i := uint64(0); i < count; i++ {
|
2016-04-21 23:49:38 +02:00
|
|
|
iv := &invList[i]
|
|
|
|
err := readInvVect(r, pver, iv)
|
2013-05-08 21:31:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-21 23:49:38 +02:00
|
|
|
msg.AddInvVect(iv)
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
|
|
// This is part of the Message interface implementation.
|
2016-10-19 01:21:48 +02:00
|
|
|
func (msg *MsgNotFound) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
|
2013-05-08 21:31:00 +02:00
|
|
|
// Limit to max inventory vectors per message.
|
|
|
|
count := len(msg.InvList)
|
|
|
|
if count > MaxInvPerMsg {
|
2013-05-10 18:32:20 +02:00
|
|
|
str := fmt.Sprintf("too many invvect in message [%v]", count)
|
|
|
|
return messageError("MsgNotFound.BtcEncode", str)
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
2016-02-22 17:13:11 +01:00
|
|
|
err := WriteVarInt(w, pver, uint64(count))
|
2013-05-08 21:31:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, iv := range msg.InvList {
|
|
|
|
err := writeInvVect(w, pver, iv)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Command returns the protocol command string for the message. This is part
|
|
|
|
// of the Message interface implementation.
|
|
|
|
func (msg *MsgNotFound) Command() string {
|
2014-07-14 17:38:29 +02:00
|
|
|
return CmdNotFound
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
|
|
// receiver. This is part of the Message interface implementation.
|
|
|
|
func (msg *MsgNotFound) MaxPayloadLength(pver uint32) uint32 {
|
|
|
|
// Max var int 9 bytes + max InvVects at 36 bytes each.
|
|
|
|
// Num inventory vectors (varInt) + max allowed inventory vectors.
|
2014-03-12 02:09:55 +01:00
|
|
|
return MaxVarIntPayload + (MaxInvPerMsg * maxInvVectPayload)
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMsgNotFound returns a new bitcoin notfound message that conforms to the
|
|
|
|
// Message interface. See MsgNotFound for details.
|
|
|
|
func NewMsgNotFound() *MsgNotFound {
|
2013-09-25 20:57:01 +02:00
|
|
|
return &MsgNotFound{
|
|
|
|
InvList: make([]*InvVect, 0, defaultInvListAlloc),
|
|
|
|
}
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|