msgnotfound.go raw

   1  // Copyright (c) 2013-2015 The btcsuite developers
   2  // Use of this source code is governed by an ISC
   3  // license that can be found in the LICENSE file.
   4  
   5  package wire
   6  
   7  import (
   8  	"fmt"
   9  	"io"
  10  )
  11  
  12  // MsgNotFound defines a bitcoin notfound message which is sent in response to
  13  // a getdata message if any of the requested data in not available on the peer.
  14  // Each message is limited to a maximum number of inventory vectors, which is
  15  // currently 50,000.
  16  //
  17  // Use the AddInvVect function to build up the list of inventory vectors when
  18  // sending a notfound message to another peer.
  19  type MsgNotFound struct {
  20  	InvList []*InvVect
  21  }
  22  
  23  // AddInvVect adds an inventory vector to the message.
  24  func (msg *MsgNotFound) AddInvVect(iv *InvVect) error {
  25  	if len(msg.InvList)+1 > MaxInvPerMsg {
  26  		str := fmt.Sprintf("too many invvect in message [max %v]",
  27  			MaxInvPerMsg)
  28  		return messageError("MsgNotFound.AddInvVect", str)
  29  	}
  30  
  31  	msg.InvList = append(msg.InvList, iv)
  32  	return nil
  33  }
  34  
  35  // BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
  36  // This is part of the Message interface implementation.
  37  func (msg *MsgNotFound) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
  38  	buf := binarySerializer.Borrow()
  39  	defer binarySerializer.Return(buf)
  40  
  41  	count, err := ReadVarIntBuf(r, pver, buf)
  42  	if err != nil {
  43  		return err
  44  	}
  45  
  46  	// Limit to max inventory vectors per message.
  47  	if count > MaxInvPerMsg {
  48  		str := fmt.Sprintf("too many invvect in message [%v]", count)
  49  		return messageError("MsgNotFound.BtcDecode", str)
  50  	}
  51  	if err := validateElementCount(
  52  		r, count, maxInvVectPayload,
  53  	); err != nil {
  54  		return err
  55  	}
  56  
  57  	// Create a contiguous slice of inventory vectors to deserialize into in
  58  	// order to reduce the number of allocations.
  59  	invList := make([]InvVect, count)
  60  	msg.InvList = make([]*InvVect, 0, count)
  61  	for i := uint64(0); i < count; i++ {
  62  		iv := &invList[i]
  63  		err := readInvVectBuf(r, pver, iv, buf)
  64  		if err != nil {
  65  			return err
  66  		}
  67  		msg.AddInvVect(iv)
  68  	}
  69  
  70  	return nil
  71  }
  72  
  73  // BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
  74  // This is part of the Message interface implementation.
  75  func (msg *MsgNotFound) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
  76  	// Limit to max inventory vectors per message.
  77  	count := len(msg.InvList)
  78  	if count > MaxInvPerMsg {
  79  		str := fmt.Sprintf("too many invvect in message [%v]", count)
  80  		return messageError("MsgNotFound.BtcEncode", str)
  81  	}
  82  
  83  	buf := binarySerializer.Borrow()
  84  	defer binarySerializer.Return(buf)
  85  
  86  	err := WriteVarIntBuf(w, pver, uint64(count), buf)
  87  	if err != nil {
  88  		return err
  89  	}
  90  
  91  	for _, iv := range msg.InvList {
  92  		err := writeInvVectBuf(w, pver, iv, buf)
  93  		if err != nil {
  94  			return err
  95  		}
  96  	}
  97  
  98  	return nil
  99  }
 100  
 101  // Command returns the protocol command string for the message.  This is part
 102  // of the Message interface implementation.
 103  func (msg *MsgNotFound) Command() string {
 104  	return CmdNotFound
 105  }
 106  
 107  // MaxPayloadLength returns the maximum length the payload can be for the
 108  // receiver.  This is part of the Message interface implementation.
 109  func (msg *MsgNotFound) MaxPayloadLength(pver uint32) uint32 {
 110  	// Max var int 9 bytes + max InvVects at 36 bytes each.
 111  	// Num inventory vectors (varInt) + max allowed inventory vectors.
 112  	return MaxVarIntPayload + (MaxInvPerMsg * maxInvVectPayload)
 113  }
 114  
 115  // NewMsgNotFound returns a new bitcoin notfound message that conforms to the
 116  // Message interface.  See MsgNotFound for details.
 117  func NewMsgNotFound() *MsgNotFound {
 118  	return &MsgNotFound{
 119  		InvList: make([]*InvVect, 0, defaultInvListAlloc),
 120  	}
 121  }
 122