msgaddrv2.go raw
1 package wire
2
3 import (
4 "fmt"
5 "io"
6 )
7
8 // MaxV2AddrPerMsg is the maximum number of version 2 addresses that will exist
9 // in a single addrv2 message (MsgAddrV2).
10 const MaxV2AddrPerMsg = 1000
11
12 // MsgAddrV2 implements the Message interface and represents a bitcoin addrv2
13 // message that can support longer-length addresses like torv3, cjdns, and i2p.
14 // It is used to gossip addresses on the network. Each message is limited to
15 // MaxV2AddrPerMsg addresses. This is the same limit as MsgAddr.
16 type MsgAddrV2 struct {
17 AddrList []*NetAddressV2
18 }
19
20 // BtcDecode decodes r using the bitcoin protocol into a MsgAddrV2.
21 func (m *MsgAddrV2) BtcDecode(r io.Reader, pver uint32,
22 enc MessageEncoding) error {
23
24 count, err := ReadVarInt(r, pver)
25 if err != nil {
26 return err
27 }
28
29 // Limit to max addresses per message.
30 if count > MaxV2AddrPerMsg {
31 str := fmt.Sprintf("too many addresses for message [count %v,"+
32 " max %v]", count, MaxV2AddrPerMsg)
33 return messageError("MsgAddrV2.BtcDecode", str)
34 }
35 if err := validateElementCount(
36 r, count, minNetAddressV2Payload,
37 ); err != nil {
38 return err
39 }
40
41 addrList := make([]NetAddressV2, count)
42 m.AddrList = make([]*NetAddressV2, 0, count)
43 for i := uint64(0); i < count; i++ {
44 na := &addrList[i]
45 err := readNetAddressV2(r, pver, na)
46 if err != nil {
47 // A network address of a type we don't know of may be
48 // safely skipped. All other errors mean we should stop
49 // processing the message.
50 if err == ErrSkippedNetworkID {
51 continue
52 }
53
54 return err
55 }
56
57 m.AddrList = append(m.AddrList, na)
58 }
59
60 return nil
61 }
62
63 // BtcEncode encodes the MsgAddrV2 into a writer w.
64 func (m *MsgAddrV2) BtcEncode(w io.Writer, pver uint32,
65 enc MessageEncoding) error {
66
67 count := len(m.AddrList)
68 if count > MaxV2AddrPerMsg {
69 str := fmt.Sprintf("too many addresses for message: "+
70 "got %v, max %v", count, MaxV2AddrPerMsg)
71 return messageError("MsgAddrV2.BtcEncode", str)
72 }
73
74 err := WriteVarInt(w, pver, uint64(count))
75 if err != nil {
76 return err
77 }
78
79 for _, na := range m.AddrList {
80 err = writeNetAddressV2(w, pver, na)
81 if err != nil {
82 return err
83 }
84 }
85
86 return nil
87 }
88
89 // Command returns the protocol command string for MsgAddrV2.
90 func (m *MsgAddrV2) Command() string {
91 return CmdAddrV2
92 }
93
94 // MaxPayloadLength returns the maximum length payload possible for MsgAddrV2.
95 func (m *MsgAddrV2) MaxPayloadLength(pver uint32) uint32 {
96 // The varint that can store the maximum number of addresses is 3 bytes
97 // long. The maximum payload is then 3 + 1000 * maxNetAddressV2Payload.
98 return 3 + (MaxV2AddrPerMsg * maxNetAddressV2Payload())
99 }
100
101 // NewMsgAddrV2 returns a new bitcoin addrv2 message that conforms to the
102 // Message interface.
103 func NewMsgAddrV2() *MsgAddrV2 {
104 return &MsgAddrV2{
105 AddrList: make([]*NetAddressV2, 0, MaxV2AddrPerMsg),
106 }
107 }
108