1 package wire
2 3 import (
4 "fmt"
5 "io"
6 )
7 8 const (
9 // MaxFilterAddDataSize is the maximum byte size of a data element to add to the Bloom filter. It is equal to the
10 // maximum element size of a script.
11 MaxFilterAddDataSize = 520
12 )
13 14 // MsgFilterAdd implements the Message interface and represents a bitcoin filteradd message. It is used to add a data
15 // element to an existing Bloom filter. This message was not added until protocol version BIP0037Version.
16 type MsgFilterAdd struct {
17 Data []byte
18 }
19 20 // BtcDecode decodes r using the bitcoin protocol encoding into the receiver. This is part of the Message interface implementation.
21 func (msg *MsgFilterAdd) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) (e error) {
22 if pver < BIP0037Version {
23 str := fmt.Sprintf(
24 "filteradd message invalid for protocol version %d", pver,
25 )
26 return messageError("MsgFilterAdd.BtcDecode", str)
27 }
28 msg.Data, e = ReadVarBytes(r, pver, MaxFilterAddDataSize, "filteradd data")
29 return e
30 }
31 32 // BtcEncode encodes the receiver to w using the bitcoin protocol encoding. This is part of the Message interface
33 // implementation.
34 func (msg *MsgFilterAdd) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) (e error) {
35 if pver < BIP0037Version {
36 str := fmt.Sprintf(
37 "filteradd message invalid for protocol version %d", pver,
38 )
39 return messageError("MsgFilterAdd.BtcEncode", str)
40 }
41 size := len(msg.Data)
42 if size > MaxFilterAddDataSize {
43 str := fmt.Sprintf(
44 "filteradd size too large for message [size %v, max %v]", size, MaxFilterAddDataSize,
45 )
46 return messageError("MsgFilterAdd.BtcEncode", str)
47 }
48 return WriteVarBytes(w, pver, msg.Data)
49 }
50 51 // Command returns the protocol command string for the message. This is part of the Message interface implementation.
52 func (msg *MsgFilterAdd) Command() string {
53 return CmdFilterAdd
54 }
55 56 // MaxPayloadLength returns the maximum length the payload can be for the receiver. This is part of the Message
57 // interface implementation.
58 func (msg *MsgFilterAdd) MaxPayloadLength(pver uint32) uint32 {
59 return uint32(VarIntSerializeSize(MaxFilterAddDataSize)) +
60 MaxFilterAddDataSize
61 }
62 63 // NewMsgFilterAdd returns a new bitcoin filteradd message that conforms to the Message interface. See MsgFilterAdd for
64 // details.
65 func NewMsgFilterAdd(data []byte) *MsgFilterAdd {
66 return &MsgFilterAdd{
67 Data: data,
68 }
69 }
70