/
msgminingstate.go
201 lines (172 loc) · 5.54 KB
/
msgminingstate.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2015-2019 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire
import (
"fmt"
"io"
"github.com/Decred-Next/dcrnd/chaincfg/chainhash/v8"
)
// MaxMSBlocksAtHeadPerMsg is the maximum number of block hashes allowed
// per message.
const MaxMSBlocksAtHeadPerMsg = 8
// MaxMSVotesAtHeadPerMsg is the maximum number of votes at head per message.
const MaxMSVotesAtHeadPerMsg = 40 // 8 * 5
// MsgMiningState implements the Message interface and represents a mining state
// message. It is used to request a list of blocks located at the chain tip
// along with all votes for those blocks. The list is returned is limited by
// the maximum number of blocks per message and the maximum number of votes per
// message.
type MsgMiningState struct {
Version uint32
Height uint32
BlockHashes []*chainhash.Hash
VoteHashes []*chainhash.Hash
}
// AddBlockHash adds a new block hash to the message.
func (msg *MsgMiningState) AddBlockHash(hash *chainhash.Hash) error {
if len(msg.BlockHashes)+1 > MaxMSBlocksAtHeadPerMsg {
str := fmt.Sprintf("too many block hashes for message [max %v]",
MaxMSBlocksAtHeadPerMsg)
return messageError("MsgMiningState.AddBlockHash", str)
}
msg.BlockHashes = append(msg.BlockHashes, hash)
return nil
}
// AddVoteHash adds a new vote hash to the message.
func (msg *MsgMiningState) AddVoteHash(hash *chainhash.Hash) error {
if len(msg.VoteHashes)+1 > MaxMSVotesAtHeadPerMsg {
str := fmt.Sprintf("too many vote hashes for message [max %v]",
MaxMSVotesAtHeadPerMsg)
return messageError("MsgMiningState.AddVoteHash", str)
}
msg.VoteHashes = append(msg.VoteHashes, hash)
return nil
}
// BtcDecode decodes r using the protocol encoding into the receiver.
// This is part of the Message interface implementation.
func (msg *MsgMiningState) BtcDecode(r io.Reader, pver uint32) error {
err := readElement(r, &msg.Version)
if err != nil {
return err
}
err = readElement(r, &msg.Height)
if err != nil {
return err
}
// Read num block hashes and limit to max.
count, err := ReadVarInt(r, pver)
if err != nil {
return err
}
if count > MaxMSBlocksAtHeadPerMsg {
str := fmt.Sprintf("too many block hashes for message "+
"[count %v, max %v]", count, MaxMSBlocksAtHeadPerMsg)
return messageError("MsgMiningState.BtcDecode", str)
}
msg.BlockHashes = make([]*chainhash.Hash, 0, count)
for i := uint64(0); i < count; i++ {
hash := chainhash.Hash{}
err := readElement(r, &hash)
if err != nil {
return err
}
msg.AddBlockHash(&hash)
}
// Read num vote hashes and limit to max.
count, err = ReadVarInt(r, pver)
if err != nil {
return err
}
if count > MaxMSVotesAtHeadPerMsg {
str := fmt.Sprintf("too many vote hashes for message "+
"[count %v, max %v]", count, MaxMSVotesAtHeadPerMsg)
return messageError("MsgMiningState.BtcDecode", str)
}
msg.VoteHashes = make([]*chainhash.Hash, 0, count)
for i := uint64(0); i < count; i++ {
hash := chainhash.Hash{}
err := readElement(r, &hash)
if err != nil {
return err
}
err = msg.AddVoteHash(&hash)
if err != nil {
return err
}
}
return nil
}
// BtcEncode encodes the receiver to w using the protocol encoding.
// This is part of the Message interface implementation.
func (msg *MsgMiningState) BtcEncode(w io.Writer, pver uint32) error {
err := writeElement(w, msg.Version)
if err != nil {
return err
}
err = writeElement(w, msg.Height)
if err != nil {
return err
}
// Write block hashes.
count := len(msg.BlockHashes)
if count > MaxMSBlocksAtHeadPerMsg {
str := fmt.Sprintf("too many block hashes for message "+
"[count %v, max %v]", count, MaxMSBlocksAtHeadPerMsg)
return messageError("MsgMiningState.BtcEncode", str)
}
err = WriteVarInt(w, pver, uint64(count))
if err != nil {
return err
}
for _, hash := range msg.BlockHashes {
err = writeElement(w, hash)
if err != nil {
return err
}
}
// Write vote hashes.
count = len(msg.VoteHashes)
if count > MaxMSVotesAtHeadPerMsg {
str := fmt.Sprintf("too many vote hashes for message "+
"[count %v, max %v]", count, MaxMSVotesAtHeadPerMsg)
return messageError("MsgMiningState.BtcEncode", str)
}
err = WriteVarInt(w, pver, uint64(count))
if err != nil {
return err
}
for _, hash := range msg.VoteHashes {
err = writeElement(w, hash)
if err != nil {
return err
}
}
return nil
}
// Command returns the protocol command string for the message. This is part
// of the Message interface implementation.
func (msg *MsgMiningState) Command() string {
return CmdMiningState
}
// MaxPayloadLength returns the maximum length the payload can be for the
// receiver. This is part of the Message interface implementation.
func (msg *MsgMiningState) MaxPayloadLength(pver uint32) uint32 {
// Protocol version 4 bytes + Height 4 bytes + num block hashes (varInt)
// 1 byte + block hashes + num vote hashes (varInt) 1 byte + vote hashes
return 4 + 4 + uint32(VarIntSerializeSize(MaxMSBlocksAtHeadPerMsg)) +
(MaxMSBlocksAtHeadPerMsg * chainhash.HashSize) +
uint32(VarIntSerializeSize(MaxMSVotesAtHeadPerMsg)) +
(MaxMSVotesAtHeadPerMsg * chainhash.HashSize)
}
// NewMsgMiningState returns a new Decred miningstate message that conforms to
// the Message interface using the defaults for the fields.
func NewMsgMiningState() *MsgMiningState {
return &MsgMiningState{
Version: 1,
Height: 0,
BlockHashes: make([]*chainhash.Hash, 0, MaxMSBlocksAtHeadPerMsg),
VoteHashes: make([]*chainhash.Hash, 0, MaxMSVotesAtHeadPerMsg),
}
}