github.com/btcsuite/btcd@v0.24.0/wire/msgaddrv2.go (about) 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 36 addrList := make([]NetAddressV2, count) 37 m.AddrList = make([]*NetAddressV2, 0, count) 38 for i := uint64(0); i < count; i++ { 39 na := &addrList[i] 40 err := readNetAddressV2(r, pver, na) 41 switch err { 42 case ErrSkippedNetworkID: 43 // This may be a network ID we don't know of, but is 44 // still valid. We can safely skip those. 45 continue 46 case ErrInvalidAddressSize: 47 // The encoding used by the peer does not follow 48 // BIP-155 and we should stop processing this message. 49 return err 50 } 51 52 m.AddrList = append(m.AddrList, na) 53 } 54 55 return nil 56 } 57 58 // BtcEncode encodes the MsgAddrV2 into a writer w. 59 func (m *MsgAddrV2) BtcEncode(w io.Writer, pver uint32, 60 enc MessageEncoding) error { 61 62 count := len(m.AddrList) 63 if count > MaxV2AddrPerMsg { 64 str := fmt.Sprintf("too many addresses for message [count %v,"+ 65 " max %v]", count, MaxV2AddrPerMsg) 66 return messageError("MsgAddrV2.BtcEncode", str) 67 } 68 69 err := WriteVarInt(w, pver, uint64(count)) 70 if err != nil { 71 return err 72 } 73 74 for _, na := range m.AddrList { 75 err = writeNetAddressV2(w, pver, na) 76 if err != nil { 77 return err 78 } 79 } 80 81 return nil 82 } 83 84 // Command returns the protocol command string for MsgAddrV2. 85 func (m *MsgAddrV2) Command() string { 86 return CmdAddrV2 87 } 88 89 // MaxPayloadLength returns the maximum length payload possible for MsgAddrV2. 90 func (m *MsgAddrV2) MaxPayloadLength(pver uint32) uint32 { 91 // The varint that can store the maximum number of addresses is 3 bytes 92 // long. The maximum payload is then 3 + 1000 * maxNetAddressV2Payload. 93 return 3 + (MaxV2AddrPerMsg * maxNetAddressV2Payload()) 94 } 95 96 // NewMsgAddrV2 returns a new bitcoin addrv2 message that conforms to the 97 // Message interface. 98 func NewMsgAddrV2() *MsgAddrV2 { 99 return &MsgAddrV2{ 100 AddrList: make([]*NetAddressV2, 0, MaxV2AddrPerMsg), 101 } 102 }