/
msgheaders.go
125 lines (116 loc) · 3.93 KB
/
msgheaders.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package wire
import (
"fmt"
"io"
)
// MaxBlockHeadersPerMsg is the maximum number of block headers that can be in a
// single bitcoin headers message.
const MaxBlockHeadersPerMsg = 2000
// MsgHeaders implements the Message interface and represents a bitcoin headers
// message. It is used to deliver block header information in response to a
// getheaders message (MsgGetHeaders). The maximum number of block headers per
// message is currently 2000. See MsgGetHeaders for details on requesting the
// headers.
type MsgHeaders struct {
Headers []*BlockHeader
}
// AddBlockHeader adds a new block header to the message.
func (msg *MsgHeaders) AddBlockHeader(bh *BlockHeader) (e error) {
if len(msg.Headers)+1 > MaxBlockHeadersPerMsg {
str := fmt.Sprintf(
"too many block headers in message [max %v]",
MaxBlockHeadersPerMsg,
)
return messageError("MsgHeaders.AddBlockHeader", str)
}
msg.Headers = append(msg.Headers, bh)
return nil
}
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
// This is part of the Message interface implementation.
func (msg *MsgHeaders) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) (e error) {
var count uint64
if count, e = ReadVarInt(r, pver); E.Chk(e) {
return
}
// Limit to max block headers per message.
if count > MaxBlockHeadersPerMsg {
str := fmt.Sprintf(
"too many block headers for message "+
"[count %v, max %v]", count, MaxBlockHeadersPerMsg,
)
return messageError("MsgHeaders.BtcDecode", str)
}
// Create a contiguous slice of headers to deserialize into in order to reduce
// the number of allocations.
headers := make([]BlockHeader, count)
msg.Headers = make([]*BlockHeader, 0, count)
for i := uint64(0); i < count; i++ {
bh := &headers[i]
if e = readBlockHeader(r, pver, bh); E.Chk(e) {
return
}
txCount, e := ReadVarInt(r, pver)
if e != nil {
return e
}
// Ensure the transaction count is zero for headers.
if txCount > 0 {
str := fmt.Sprintf(
"block headers may not contain "+
"transactions [count %v]", txCount,
)
return messageError("MsgHeaders.BtcDecode", str)
}
if e = msg.AddBlockHeader(bh); E.Chk(e) {
}
}
return
}
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding. This
// is part of the Message interface implementation.
func (msg *MsgHeaders) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) (e error) {
// Limit to max block headers per message.
count := len(msg.Headers)
if count > MaxBlockHeadersPerMsg {
str := fmt.Sprintf(
"too many block headers for message "+
"[count %v, max %v]", count, MaxBlockHeadersPerMsg,
)
return messageError("MsgHeaders.BtcEncode", str)
}
if e = WriteVarInt(w, pver, uint64(count)); E.Chk(e) {
return
}
for _, bh := range msg.Headers {
if e = writeBlockHeader(w, pver, bh); E.Chk(e) {
return
}
// The wire protocol encoding always includes a 0 for the number of transactions
// on header messages. This is really just an artifact of the way the original
// implementation serializes block headers, but it is required.
if e = WriteVarInt(w, pver, 0); E.Chk(e) {
return
}
}
return
}
// Command returns the protocol command string for the message. This is part of the Message interface implementation.
func (msg *MsgHeaders) Command() string {
return CmdHeaders
}
// MaxPayloadLength returns the maximum length the payload can be for the receiver. This is part of the Message
// interface implementation.
func (msg *MsgHeaders) MaxPayloadLength(pver uint32) uint32 {
// Num headers (varInt) + max allowed headers (header length + 1 byte for the number of transactions which is always
// 0).
return MaxVarIntPayload + ((MaxBlockHeaderPayload + 1) *
MaxBlockHeadersPerMsg)
}
// NewMsgHeaders returns a new bitcoin headers message that conforms to the Message interface. See MsgHeaders for
// details.
func NewMsgHeaders() *MsgHeaders {
return &MsgHeaders{
Headers: make([]*BlockHeader, 0, MaxBlockHeadersPerMsg),
}
}