1 // Copyright (c) 2013-2016 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 // MaxBlockHeadersPerMsg is the maximum number of block headers that can be in
13 // a single bitcoin headers message.
14 const MaxBlockHeadersPerMsg = 2000
15 16 // MsgHeaders implements the Message interface and represents a bitcoin headers
17 // message. It is used to deliver block header information in response
18 // to a getheaders message (MsgGetHeaders). The maximum number of block headers
19 // per message is currently 2000. See MsgGetHeaders for details on requesting
20 // the headers.
21 type MsgHeaders struct {
22 Headers []*BlockHeader
23 }
24 25 // AddBlockHeader adds a new block header to the message.
26 func (msg *MsgHeaders) AddBlockHeader(bh *BlockHeader) error {
27 if len(msg.Headers)+1 > MaxBlockHeadersPerMsg {
28 str := fmt.Sprintf("too many block headers in message [max %v]",
29 MaxBlockHeadersPerMsg)
30 return messageError("MsgHeaders.AddBlockHeader", str)
31 }
32 33 msg.Headers = append(msg.Headers, bh)
34 return nil
35 }
36 37 // BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
38 // This is part of the Message interface implementation.
39 func (msg *MsgHeaders) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
40 buf := binarySerializer.Borrow()
41 defer binarySerializer.Return(buf)
42 43 count, err := ReadVarIntBuf(r, pver, buf)
44 if err != nil {
45 return err
46 }
47 48 // Limit to max block headers per message.
49 if count > MaxBlockHeadersPerMsg {
50 str := fmt.Sprintf("too many block headers for message "+
51 "[count %v, max %v]", count, MaxBlockHeadersPerMsg)
52 return messageError("MsgHeaders.BtcDecode", str)
53 }
54 if err := validateElementCount(
55 r, count, MaxBlockHeaderPayload+1,
56 ); err != nil {
57 return err
58 }
59 60 // Create a contiguous slice of headers to deserialize into in order to
61 // reduce the number of allocations.
62 headers := make([]BlockHeader, count)
63 msg.Headers = make([]*BlockHeader, 0, count)
64 for i := uint64(0); i < count; i++ {
65 bh := &headers[i]
66 err := readBlockHeaderBuf(r, pver, bh, buf)
67 if err != nil {
68 return err
69 }
70 71 txCount, err := ReadVarIntBuf(r, pver, buf)
72 if err != nil {
73 return err
74 }
75 76 // Ensure the transaction count is zero for headers.
77 if txCount > 0 {
78 str := fmt.Sprintf("block headers may not contain "+
79 "transactions [count %v]", txCount)
80 return messageError("MsgHeaders.BtcDecode", str)
81 }
82 msg.AddBlockHeader(bh)
83 }
84 85 return nil
86 }
87 88 // BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
89 // This is part of the Message interface implementation.
90 func (msg *MsgHeaders) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
91 // Limit to max block headers per message.
92 count := len(msg.Headers)
93 if count > MaxBlockHeadersPerMsg {
94 str := fmt.Sprintf("too many block headers for message "+
95 "[count %v, max %v]", count, MaxBlockHeadersPerMsg)
96 return messageError("MsgHeaders.BtcEncode", str)
97 }
98 99 buf := binarySerializer.Borrow()
100 defer binarySerializer.Return(buf)
101 102 err := WriteVarIntBuf(w, pver, uint64(count), buf)
103 if err != nil {
104 return err
105 }
106 107 for _, bh := range msg.Headers {
108 err := writeBlockHeaderBuf(w, pver, bh, buf)
109 if err != nil {
110 return err
111 }
112 113 // The wire protocol encoding always includes a 0 for the number
114 // of transactions on header messages. This is really just an
115 // artifact of the way the original implementation serializes
116 // block headers, but it is required.
117 err = WriteVarIntBuf(w, pver, 0, buf)
118 if err != nil {
119 return err
120 }
121 }
122 123 return nil
124 }
125 126 // Command returns the protocol command string for the message. This is part
127 // of the Message interface implementation.
128 func (msg *MsgHeaders) Command() string {
129 return CmdHeaders
130 }
131 132 // MaxPayloadLength returns the maximum length the payload can be for the
133 // receiver. This is part of the Message interface implementation.
134 func (msg *MsgHeaders) MaxPayloadLength(pver uint32) uint32 {
135 // Num headers (varInt) + max allowed headers (header length + 1 byte
136 // for the number of transactions which is always 0).
137 return MaxVarIntPayload + ((MaxBlockHeaderPayload + 1) *
138 MaxBlockHeadersPerMsg)
139 }
140 141 // NewMsgHeaders returns a new bitcoin headers message that conforms to the
142 // Message interface. See MsgHeaders for details.
143 func NewMsgHeaders() *MsgHeaders {
144 return &MsgHeaders{
145 Headers: make([]*BlockHeader, 0, MaxBlockHeadersPerMsg),
146 }
147 }
148