1 // Copyright (c) 2017 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 "github.com/btcsuite/btcd/chainhash/v2"
12 )
13 14 // FilterType is used to represent a filter type.
15 type FilterType uint8
16 17 const (
18 // GCSFilterRegular is the regular filter type.
19 GCSFilterRegular FilterType = iota
20 )
21 22 const (
23 // MaxCFilterDataSize is the maximum byte size of a committed filter.
24 // The maximum size is currently defined as 256KiB.
25 MaxCFilterDataSize = 256 * 1024
26 )
27 28 // MsgCFilter implements the Message interface and represents a bitcoin cfilter
29 // message. It is used to deliver a committed filter in response to a
30 // getcfilters (MsgGetCFilters) message.
31 type MsgCFilter struct {
32 FilterType FilterType
33 BlockHash chainhash.Hash
34 Data []byte
35 }
36 37 // BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
38 // This is part of the Message interface implementation.
39 func (msg *MsgCFilter) BtcDecode(r io.Reader, pver uint32, _ MessageEncoding) error {
40 // Read filter type
41 buf := binarySerializer.Borrow()
42 defer binarySerializer.Return(buf)
43 44 if _, err := io.ReadFull(r, buf[:1]); err != nil {
45 return err
46 }
47 msg.FilterType = FilterType(buf[0])
48 49 // Read the hash of the filter's block
50 if _, err := io.ReadFull(r, msg.BlockHash[:]); err != nil {
51 return err
52 }
53 54 // Read filter data
55 var err error
56 msg.Data, err = ReadVarBytesBuf(r, pver, buf, MaxCFilterDataSize,
57 "cfilter data")
58 return err
59 }
60 61 // BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
62 // This is part of the Message interface implementation.
63 func (msg *MsgCFilter) BtcEncode(w io.Writer, pver uint32, _ MessageEncoding) error {
64 size := len(msg.Data)
65 if size > MaxCFilterDataSize {
66 str := fmt.Sprintf("cfilter size too large for message "+
67 "[size %v, max %v]", size, MaxCFilterDataSize)
68 return messageError("MsgCFilter.BtcEncode", str)
69 }
70 71 buf := binarySerializer.Borrow()
72 defer binarySerializer.Return(buf)
73 74 buf[0] = byte(msg.FilterType)
75 if _, err := w.Write(buf[:1]); err != nil {
76 return err
77 }
78 79 if _, err := w.Write(msg.BlockHash[:]); err != nil {
80 return err
81 }
82 83 err := WriteVarBytesBuf(w, pver, msg.Data, buf)
84 return err
85 }
86 87 // Deserialize decodes a filter from r into the receiver using a format that is
88 // suitable for long-term storage such as a database. This function differs
89 // from BtcDecode in that BtcDecode decodes from the bitcoin wire protocol as
90 // it was sent across the network. The wire encoding can technically differ
91 // depending on the protocol version and doesn't even really need to match the
92 // format of a stored filter at all. As of the time this comment was written,
93 // the encoded filter is the same in both instances, but there is a distinct
94 // difference and separating the two allows the API to be flexible enough to
95 // deal with changes.
96 func (msg *MsgCFilter) Deserialize(r io.Reader) error {
97 // At the current time, there is no difference between the wire encoding
98 // and the stable long-term storage format. As a result, make use of
99 // BtcDecode.
100 return msg.BtcDecode(r, 0, BaseEncoding)
101 }
102 103 // Command returns the protocol command string for the message. This is part
104 // of the Message interface implementation.
105 func (msg *MsgCFilter) Command() string {
106 return CmdCFilter
107 }
108 109 // MaxPayloadLength returns the maximum length the payload can be for the
110 // receiver. This is part of the Message interface implementation.
111 func (msg *MsgCFilter) MaxPayloadLength(pver uint32) uint32 {
112 return uint32(VarIntSerializeSize(MaxCFilterDataSize)) +
113 MaxCFilterDataSize + chainhash.HashSize + 1
114 }
115 116 // NewMsgCFilter returns a new bitcoin cfilter message that conforms to the
117 // Message interface. See MsgCFilter for details.
118 func NewMsgCFilter(filterType FilterType, blockHash *chainhash.Hash,
119 data []byte) *MsgCFilter {
120 return &MsgCFilter{
121 FilterType: filterType,
122 BlockHash: *blockHash,
123 Data: data,
124 }
125 }
126