github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/wire/msgfilteradd.go (about)

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