2015-05-01 08:28:01 +02:00
|
|
|
// Copyright (c) 2014-2015 The btcsuite developers
|
2014-04-11 04:29:00 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-01-31 19:32:55 +01:00
|
|
|
package wire
|
2014-04-11 04:29:00 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// MaxFilterAddDataSize is the maximum byte size of a data
|
|
|
|
// element to add to the Bloom filter. It is equal to the
|
|
|
|
// maximum element size of a script.
|
|
|
|
MaxFilterAddDataSize = 520
|
|
|
|
)
|
|
|
|
|
2014-05-05 09:15:18 +02:00
|
|
|
// MsgFilterAdd implements the Message interface and represents a bitcoin
|
|
|
|
// filteradd message. It is used to add a data element to an existing Bloom
|
|
|
|
// filter.
|
2014-04-11 04:29:00 +02:00
|
|
|
//
|
|
|
|
// This message was not added until protocol version BIP0037Version.
|
|
|
|
type MsgFilterAdd struct {
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
|
|
// This is part of the Message interface implementation.
|
|
|
|
func (msg *MsgFilterAdd) BtcDecode(r io.Reader, pver uint32) error {
|
|
|
|
if pver < BIP0037Version {
|
|
|
|
str := fmt.Sprintf("filteradd message invalid for protocol "+
|
|
|
|
"version %d", pver)
|
|
|
|
return messageError("MsgFilterAdd.BtcDecode", str)
|
|
|
|
}
|
|
|
|
|
2014-05-05 09:15:18 +02:00
|
|
|
var err error
|
2016-02-22 17:13:11 +01:00
|
|
|
msg.Data, err = ReadVarBytes(r, pver, MaxFilterAddDataSize,
|
2014-05-05 09:15:18 +02:00
|
|
|
"filteradd data")
|
2014-04-11 04:29:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
|
|
// This is part of the Message interface implementation.
|
|
|
|
func (msg *MsgFilterAdd) BtcEncode(w io.Writer, pver uint32) error {
|
|
|
|
if pver < BIP0037Version {
|
|
|
|
str := fmt.Sprintf("filteradd message invalid for protocol "+
|
|
|
|
"version %d", pver)
|
|
|
|
return messageError("MsgFilterAdd.BtcEncode", str)
|
|
|
|
}
|
|
|
|
|
|
|
|
size := len(msg.Data)
|
|
|
|
if size > MaxFilterAddDataSize {
|
|
|
|
str := fmt.Sprintf("filteradd size too large for message "+
|
|
|
|
"[size %v, max %v]", size, MaxFilterAddDataSize)
|
|
|
|
return messageError("MsgFilterAdd.BtcEncode", str)
|
|
|
|
}
|
|
|
|
|
2016-02-22 17:13:11 +01:00
|
|
|
err := WriteVarBytes(w, pver, msg.Data)
|
2014-04-11 04:29:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Command returns the protocol command string for the message. This is part
|
|
|
|
// of the Message interface implementation.
|
|
|
|
func (msg *MsgFilterAdd) Command() string {
|
2014-07-14 17:38:29 +02:00
|
|
|
return CmdFilterAdd
|
2014-04-11 04:29:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
|
|
// receiver. This is part of the Message interface implementation.
|
|
|
|
func (msg *MsgFilterAdd) MaxPayloadLength(pver uint32) uint32 {
|
2014-05-05 09:15:18 +02:00
|
|
|
return uint32(VarIntSerializeSize(MaxFilterAddDataSize)) +
|
|
|
|
MaxFilterAddDataSize
|
2014-04-11 04:29:00 +02:00
|
|
|
}
|
|
|
|
|
2014-05-05 09:15:18 +02:00
|
|
|
// NewMsgFilterAdd returns a new bitcoin filteradd message that conforms to the
|
|
|
|
// Message interface. See MsgFilterAdd for details.
|
2014-04-11 04:29:00 +02:00
|
|
|
func NewMsgFilterAdd(data []byte) *MsgFilterAdd {
|
|
|
|
return &MsgFilterAdd{
|
|
|
|
Data: data,
|
|
|
|
}
|
|
|
|
}
|