2017-01-20 16:55:09 +01:00
|
|
|
// Copyright (c) 2017 The btcsuite developers
|
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package wire
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-04-04 03:05:39 +02:00
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2017-01-20 16:55:09 +01:00
|
|
|
)
|
|
|
|
|
2018-01-19 03:00:42 +01:00
|
|
|
// FilterType is used to represent a filter type.
|
|
|
|
type FilterType uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
// GCSFilterRegular is the regular filter type.
|
|
|
|
GCSFilterRegular FilterType = iota
|
|
|
|
)
|
|
|
|
|
2017-01-20 16:55:09 +01:00
|
|
|
const (
|
|
|
|
// MaxCFilterDataSize is the maximum byte size of a committed filter.
|
2017-10-10 21:34:31 +02:00
|
|
|
// The maximum size is currently defined as 256KiB.
|
|
|
|
MaxCFilterDataSize = 256 * 1024
|
2017-01-20 16:55:09 +01:00
|
|
|
)
|
2017-04-04 03:05:39 +02:00
|
|
|
|
2017-10-10 21:34:31 +02:00
|
|
|
// MsgCFilter implements the Message interface and represents a bitcoin cfilter
|
|
|
|
// message. It is used to deliver a committed filter in response to a
|
2018-01-19 04:10:42 +01:00
|
|
|
// getcfilters (MsgGetCFilters) message.
|
2017-01-20 16:55:09 +01:00
|
|
|
type MsgCFilter struct {
|
2017-10-31 07:24:57 +01:00
|
|
|
FilterType FilterType
|
2018-01-19 04:10:42 +01:00
|
|
|
BlockHash chainhash.Hash
|
2017-09-13 14:42:24 +02:00
|
|
|
Data []byte
|
2017-01-20 16:55:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
|
|
// This is part of the Message interface implementation.
|
2017-08-25 01:29:57 +02:00
|
|
|
func (msg *MsgCFilter) BtcDecode(r io.Reader, pver uint32, _ MessageEncoding) error {
|
2018-01-19 04:10:42 +01:00
|
|
|
// Read filter type
|
|
|
|
err := readElement(r, &msg.FilterType)
|
2017-04-04 22:50:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-19 04:10:42 +01:00
|
|
|
|
|
|
|
// Read the hash of the filter's block
|
|
|
|
err = readElement(r, &msg.BlockHash)
|
2017-04-04 22:50:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-19 04:10:42 +01:00
|
|
|
|
2017-04-04 22:50:27 +02:00
|
|
|
// Read filter data
|
2017-01-20 16:55:09 +01:00
|
|
|
msg.Data, err = ReadVarBytes(r, pver, MaxCFilterDataSize,
|
2017-04-04 03:05:39 +02:00
|
|
|
"cfilter data")
|
2017-01-20 16:55:09 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
|
|
// This is part of the Message interface implementation.
|
2017-08-25 01:29:57 +02:00
|
|
|
func (msg *MsgCFilter) BtcEncode(w io.Writer, pver uint32, _ MessageEncoding) error {
|
2017-01-20 16:55:09 +01:00
|
|
|
size := len(msg.Data)
|
|
|
|
if size > MaxCFilterDataSize {
|
|
|
|
str := fmt.Sprintf("cfilter size too large for message "+
|
|
|
|
"[size %v, max %v]", size, MaxCFilterDataSize)
|
|
|
|
return messageError("MsgCFilter.BtcEncode", str)
|
|
|
|
}
|
|
|
|
|
2018-01-19 04:10:42 +01:00
|
|
|
err := writeElement(w, msg.FilterType)
|
2017-04-04 03:05:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-19 04:10:42 +01:00
|
|
|
err = writeElement(w, msg.BlockHash)
|
2017-04-04 22:50:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-20 16:55:09 +01:00
|
|
|
return WriteVarBytes(w, pver, msg.Data)
|
|
|
|
}
|
|
|
|
|
2017-02-09 14:54:30 +01:00
|
|
|
// Deserialize decodes a filter from r into the receiver using a format that is
|
|
|
|
// suitable for long-term storage such as a database. This function differs
|
|
|
|
// from BtcDecode in that BtcDecode decodes from the bitcoin wire protocol as
|
|
|
|
// it was sent across the network. The wire encoding can technically differ
|
|
|
|
// depending on the protocol version and doesn't even really need to match the
|
|
|
|
// format of a stored filter at all. As of the time this comment was written,
|
|
|
|
// the encoded filter is the same in both instances, but there is a distinct
|
|
|
|
// difference and separating the two allows the API to be flexible enough to
|
|
|
|
// deal with changes.
|
|
|
|
func (msg *MsgCFilter) Deserialize(r io.Reader) error {
|
|
|
|
// At the current time, there is no difference between the wire encoding
|
|
|
|
// and the stable long-term storage format. As a result, make use of
|
|
|
|
// BtcDecode.
|
2017-08-25 01:29:57 +02:00
|
|
|
return msg.BtcDecode(r, 0, BaseEncoding)
|
2017-02-09 14:54:30 +01:00
|
|
|
}
|
|
|
|
|
2017-01-20 16:55:09 +01:00
|
|
|
// Command returns the protocol command string for the message. This is part
|
|
|
|
// of the Message interface implementation.
|
|
|
|
func (msg *MsgCFilter) Command() string {
|
|
|
|
return CmdCFilter
|
|
|
|
}
|
|
|
|
|
|
|
|
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
|
|
// receiver. This is part of the Message interface implementation.
|
|
|
|
func (msg *MsgCFilter) MaxPayloadLength(pver uint32) uint32 {
|
|
|
|
return uint32(VarIntSerializeSize(MaxCFilterDataSize)) +
|
2017-04-04 22:50:27 +02:00
|
|
|
MaxCFilterDataSize + chainhash.HashSize + 1
|
2017-01-20 16:55:09 +01:00
|
|
|
}
|
|
|
|
|
2017-04-04 03:05:39 +02:00
|
|
|
// NewMsgCFilter returns a new bitcoin cfilter message that conforms to the
|
2017-01-20 16:55:09 +01:00
|
|
|
// Message interface. See MsgCFilter for details.
|
2018-01-19 04:10:42 +01:00
|
|
|
func NewMsgCFilter(filterType FilterType, blockHash *chainhash.Hash,
|
2017-04-04 22:50:27 +02:00
|
|
|
data []byte) *MsgCFilter {
|
2017-01-20 16:55:09 +01:00
|
|
|
return &MsgCFilter{
|
2017-09-13 14:42:24 +02:00
|
|
|
FilterType: filterType,
|
2018-01-19 04:10:42 +01:00
|
|
|
BlockHash: *blockHash,
|
2017-09-13 14:42:24 +02:00
|
|
|
Data: data,
|
2017-01-20 16:55:09 +01:00
|
|
|
}
|
|
|
|
}
|