Files

136 lines
4.2 KiB
Go
Raw Permalink Normal View History

2015-05-01 01:28:01 -05:00
// Copyright (c) 2014-2015 The btcsuite developers
2014-04-10 22:29:00 -04:00
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
2015-01-31 12:32:55 -06:00
package wire
2014-04-10 22:29:00 -04:00
import (
"fmt"
"io"
)
2014-05-05 02:15:18 -05:00
// BloomUpdateType specifies how the filter is updated when a match is found
2014-04-10 22:29:00 -04:00
type BloomUpdateType uint8
const (
2014-05-05 02:15:18 -05:00
// BloomUpdateNone indicates the filter is not adjusted when a match is
// found.
2014-04-10 22:29:00 -04:00
BloomUpdateNone BloomUpdateType = 0
// BloomUpdateAll indicates if the filter matches any data element in a
2014-05-05 02:15:18 -05:00
// public key script, the outpoint is serialized and inserted into the
// filter.
2014-04-10 22:29:00 -04:00
BloomUpdateAll BloomUpdateType = 1
2014-05-05 02:15:18 -05:00
// BloomUpdateP2PubkeyOnly indicates if the filter matches a data
// element in a public key script and the script is of the standard
// pay-to-pubkey or multisig, the outpoint is serialized and inserted
// into the filter.
2014-04-10 22:29:00 -04:00
BloomUpdateP2PubkeyOnly BloomUpdateType = 2
)
const (
2014-05-05 02:15:18 -05:00
// MaxFilterLoadHashFuncs is the maximum number of hash functions to
// load into the Bloom filter.
2014-04-10 22:29:00 -04:00
MaxFilterLoadHashFuncs = 50
// MaxFilterLoadFilterSize is the maximum size in bytes a filter may be.
MaxFilterLoadFilterSize = 36000
)
2014-05-05 02:15:18 -05:00
// MsgFilterLoad implements the Message interface and represents a bitcoin
// filterload message which is used to reset a Bloom filter.
2014-04-10 22:29:00 -04:00
//
// This message was not added until protocol version BIP0037Version.
type MsgFilterLoad struct {
Filter []byte
HashFuncs uint32
Tweak uint32
Flags BloomUpdateType
}
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
// This is part of the Message interface implementation.
func (msg *MsgFilterLoad) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
2014-04-10 22:29:00 -04:00
if pver < BIP0037Version {
str := fmt.Sprintf("filterload message invalid for protocol "+
"version %d", pver)
return messageError("MsgFilterLoad.BtcDecode", str)
}
2014-05-05 02:15:18 -05:00
var err error
2016-02-22 17:13:11 +01:00
msg.Filter, err = ReadVarBytes(r, pver, MaxFilterLoadFilterSize,
2014-05-05 02:15:18 -05:00
"filterload filter size")
2014-04-10 22:29:00 -04:00
if err != nil {
return err
}
err = readElements(r, &msg.HashFuncs, &msg.Tweak, &msg.Flags)
if err != nil {
return err
}
if msg.HashFuncs > MaxFilterLoadHashFuncs {
str := fmt.Sprintf("too many filter hash functions for message "+
"[count %v, max %v]", msg.HashFuncs, MaxFilterLoadHashFuncs)
return messageError("MsgFilterLoad.BtcDecode", str)
}
return nil
}
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
// This is part of the Message interface implementation.
func (msg *MsgFilterLoad) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
2014-04-10 22:29:00 -04:00
if pver < BIP0037Version {
str := fmt.Sprintf("filterload message invalid for protocol "+
"version %d", pver)
return messageError("MsgFilterLoad.BtcEncode", str)
}
size := len(msg.Filter)
if size > MaxFilterLoadFilterSize {
str := fmt.Sprintf("filterload filter size too large for message "+
"[size %v, max %v]", size, MaxFilterLoadFilterSize)
return messageError("MsgFilterLoad.BtcEncode", str)
}
if msg.HashFuncs > MaxFilterLoadHashFuncs {
str := fmt.Sprintf("too many filter hash functions for message "+
"[count %v, max %v]", msg.HashFuncs, MaxFilterLoadHashFuncs)
return messageError("MsgFilterLoad.BtcEncode", str)
}
2016-02-22 17:13:11 +01:00
err := WriteVarBytes(w, pver, msg.Filter)
2014-04-10 22:29:00 -04:00
if err != nil {
return err
}
2014-05-05 02:15:18 -05:00
2016-11-02 23:02:04 -05:00
return writeElements(w, msg.HashFuncs, msg.Tweak, msg.Flags)
2014-04-10 22:29:00 -04:00
}
// Command returns the protocol command string for the message. This is part
// of the Message interface implementation.
func (msg *MsgFilterLoad) Command() string {
2014-07-14 10:38:29 -05:00
return CmdFilterLoad
2014-04-10 22:29:00 -04:00
}
// MaxPayloadLength returns the maximum length the payload can be for the
// receiver. This is part of the Message interface implementation.
func (msg *MsgFilterLoad) MaxPayloadLength(pver uint32) uint32 {
2014-05-05 02:15:18 -05:00
// Num filter bytes (varInt) + filter + 4 bytes hash funcs +
// 4 bytes tweak + 1 byte flags.
return uint32(VarIntSerializeSize(MaxFilterLoadFilterSize)) +
MaxFilterLoadFilterSize + 9
2014-04-10 22:29:00 -04:00
}
2014-05-05 02:15:18 -05:00
// NewMsgFilterLoad returns a new bitcoin filterload message that conforms to
// the Message interface. See MsgFilterLoad for details.
2014-04-10 22:29:00 -04:00
func NewMsgFilterLoad(filter []byte, hashFuncs uint32, tweak uint32, flags BloomUpdateType) *MsgFilterLoad {
return &MsgFilterLoad{
Filter: filter,
HashFuncs: hashFuncs,
Tweak: tweak,
Flags: flags,
}
}