forked from gcash/bchd
/
msgsendcmpct.go
80 lines (67 loc) · 2.49 KB
/
msgsendcmpct.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
package wire
import (
"fmt"
"io"
)
// CompactBlocksProtocolVersion is the current version of the compact blocks protocol
const CompactBlocksProtocolVersion = 1
// MsgSendCmpct implements the Message interface and represents a Bitcoin sendcmpct
// message. It is sent to the remove peer immediately after receiving a a version
// message to signal that they wish to receive compact blocks.
type MsgSendCmpct struct {
// If announce is set to false the receive node must announce new blocks
// via the standard inv relay. If announce is true, a new Compact Block
// can be pushed directly to the peer.
Announce bool
// The version of this protocol is currently 1.
Version uint64
}
// BchDecode decodes r using the bitcoin protocol encoding into the receiver.
// This is part of the Message interface implementation.
func (msg *MsgSendCmpct) BchDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
if pver < BIP0152Version {
str := fmt.Sprintf("sendcmpct message invalid for protocol "+
"version %d", pver)
return messageError("MsgSendCmpct.BchDecode", str)
}
if err := readElement(r, &msg.Announce); err != nil {
return err
}
if err := readElement(r, &msg.Version); err != nil {
return err
}
return nil
}
// BchEncode encodes the receiver to w using the bitcoin protocol encoding.
// This is part of the Message interface implementation.
func (msg *MsgSendCmpct) BchEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
if pver < BIP0152Version {
str := fmt.Sprintf("sendcmpct message invalid for protocol "+
"version %d", pver)
return messageError("MsgSendCmpct.BchDecode", str)
}
if err := writeElement(w, msg.Announce); err != nil {
return err
}
if err := writeElement(w, msg.Version); 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 *MsgSendCmpct) Command() string {
return CmdSendCmpct
}
// MaxPayloadLength returns the maximum length the payload can be for the
// receiver. This is part of the Message interface implementation.
func (msg *MsgSendCmpct) MaxPayloadLength(pver uint32) uint32 {
// One byte bool and eight byte uint64
return 9
}
// NewMsgSendCmpct returns a new bitcoin sendcmpct message that conforms to the
// Message interface using the passed parameters and defaults for the remaining
// fields.
func NewMsgSendCmpct(announce bool, version uint64) *MsgSendCmpct {
return &MsgSendCmpct{Announce: announce, Version: version}
}