github.com/mit-dci/lit@v0.0.0-20221102210550-8c3d3b49f2ce/wire/msgfilteradd.go (about)

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