github.com/palcoin-project/palcd@v1.0.0/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  	return err
    41  }
    42  
    43  // BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
    44  // This is part of the Message interface implementation.
    45  func (msg *MsgFilterAdd) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
    46  	if pver < BIP0037Version {
    47  		str := fmt.Sprintf("filteradd message invalid for protocol "+
    48  			"version %d", pver)
    49  		return messageError("MsgFilterAdd.BtcEncode", str)
    50  	}
    51  
    52  	size := len(msg.Data)
    53  	if size > MaxFilterAddDataSize {
    54  		str := fmt.Sprintf("filteradd size too large for message "+
    55  			"[size %v, max %v]", size, MaxFilterAddDataSize)
    56  		return messageError("MsgFilterAdd.BtcEncode", str)
    57  	}
    58  
    59  	return WriteVarBytes(w, pver, msg.Data)
    60  }
    61  
    62  // Command returns the protocol command string for the message.  This is part
    63  // of the Message interface implementation.
    64  func (msg *MsgFilterAdd) Command() string {
    65  	return CmdFilterAdd
    66  }
    67  
    68  // MaxPayloadLength returns the maximum length the payload can be for the
    69  // receiver.  This is part of the Message interface implementation.
    70  func (msg *MsgFilterAdd) MaxPayloadLength(pver uint32) uint32 {
    71  	return uint32(VarIntSerializeSize(MaxFilterAddDataSize)) +
    72  		MaxFilterAddDataSize
    73  }
    74  
    75  // NewMsgFilterAdd returns a new bitcoin filteradd message that conforms to the
    76  // Message interface.  See MsgFilterAdd for details.
    77  func NewMsgFilterAdd(data []byte) *MsgFilterAdd {
    78  	return &MsgFilterAdd{
    79  		Data: data,
    80  	}
    81  }