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