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 // defaultInvListAlloc is the default size used for the backing array for an
13 // inventory list. The array will dynamically grow as needed, but this
14 // figure is intended to provide enough space for the max number of inventory
15 // vectors in a *typical* inventory message without needing to grow the backing
16 // array multiple times. Technically, the list can grow to MaxInvPerMsg, but
17 // rather than using that large figure, this figure more accurately reflects the
18 // typical case.
19 const defaultInvListAlloc = 1000
20 21 // MsgInv implements the Message interface and represents a bitcoin inv message.
22 // It is used to advertise a peer's known data such as blocks and transactions
23 // through inventory vectors. It may be sent unsolicited to inform other peers
24 // of the data or in response to a getblocks message (MsgGetBlocks). Each
25 // message is limited to a maximum number of inventory vectors, which is
26 // currently 50,000.
27 //
28 // Use the AddInvVect function to build up the list of inventory vectors when
29 // sending an inv message to another peer.
30 type MsgInv struct {
31 InvList []*InvVect
32 }
33 34 // AddInvVect adds an inventory vector to the message.
35 func (msg *MsgInv) AddInvVect(iv *InvVect) error {
36 if len(msg.InvList)+1 > MaxInvPerMsg {
37 str := fmt.Sprintf("too many invvect in message [max %v]",
38 MaxInvPerMsg)
39 return messageError("MsgInv.AddInvVect", str)
40 }
41 42 msg.InvList = append(msg.InvList, iv)
43 return nil
44 }
45 46 // BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
47 // This is part of the Message interface implementation.
48 func (msg *MsgInv) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
49 buf := binarySerializer.Borrow()
50 defer binarySerializer.Return(buf)
51 52 count, err := ReadVarIntBuf(r, pver, buf)
53 if err != nil {
54 return err
55 }
56 57 // Limit to max inventory vectors per message.
58 if count > MaxInvPerMsg {
59 str := fmt.Sprintf("too many invvect in message [%v]", count)
60 return messageError("MsgInv.BtcDecode", str)
61 }
62 if err := validateElementCount(
63 r, count, maxInvVectPayload,
64 ); err != nil {
65 return err
66 }
67 68 // Create a contiguous slice of inventory vectors to deserialize into in
69 // order to reduce the number of allocations.
70 invList := make([]InvVect, count)
71 msg.InvList = make([]*InvVect, 0, count)
72 for i := uint64(0); i < count; i++ {
73 iv := &invList[i]
74 err := readInvVectBuf(r, pver, iv, buf)
75 if err != nil {
76 return err
77 }
78 msg.AddInvVect(iv)
79 }
80 81 return nil
82 }
83 84 // BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
85 // This is part of the Message interface implementation.
86 func (msg *MsgInv) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
87 // Limit to max inventory vectors per message.
88 count := len(msg.InvList)
89 if count > MaxInvPerMsg {
90 str := fmt.Sprintf("too many invvect in message [%v]", count)
91 return messageError("MsgInv.BtcEncode", str)
92 }
93 94 buf := binarySerializer.Borrow()
95 defer binarySerializer.Return(buf)
96 97 err := WriteVarIntBuf(w, pver, uint64(count), buf)
98 if err != nil {
99 return err
100 }
101 102 for _, iv := range msg.InvList {
103 err := writeInvVectBuf(w, pver, iv, buf)
104 if err != nil {
105 return err
106 }
107 }
108 109 return nil
110 }
111 112 // Command returns the protocol command string for the message. This is part
113 // of the Message interface implementation.
114 func (msg *MsgInv) Command() string {
115 return CmdInv
116 }
117 118 // MaxPayloadLength returns the maximum length the payload can be for the
119 // receiver. This is part of the Message interface implementation.
120 func (msg *MsgInv) MaxPayloadLength(pver uint32) uint32 {
121 // Num inventory vectors (varInt) + max allowed inventory vectors.
122 return MaxVarIntPayload + (MaxInvPerMsg * maxInvVectPayload)
123 }
124 125 // NewMsgInv returns a new bitcoin inv message that conforms to the Message
126 // interface. See MsgInv for details.
127 func NewMsgInv() *MsgInv {
128 return &MsgInv{
129 InvList: make([]*InvVect, 0, defaultInvListAlloc),
130 }
131 }
132 133 // NewMsgInvSizeHint returns a new bitcoin inv message that conforms to the
134 // Message interface. See MsgInv for details. This function differs from
135 // NewMsgInv in that it allows a default allocation size for the backing array
136 // which houses the inventory vector list. This allows callers who know in
137 // advance how large the inventory list will grow to avoid the overhead of
138 // growing the internal backing array several times when appending large amounts
139 // of inventory vectors with AddInvVect. Note that the specified hint is just
140 // that - a hint that is used for the default allocation size. Adding more
141 // (or less) inventory vectors will still work properly. The size hint is
142 // limited to MaxInvPerMsg.
143 func NewMsgInvSizeHint(sizeHint uint) *MsgInv {
144 // Limit the specified hint to the maximum allow per message.
145 if sizeHint > MaxInvPerMsg {
146 sizeHint = MaxInvPerMsg
147 }
148 149 return &MsgInv{
150 InvList: make([]*InvVect, 0, sizeHint),
151 }
152 }
153