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
5package wire
6
7import (
8	"io"
9
10	"github.com/btcsuite/btcd/chaincfg/chainhash"
11)
12
13// MaxGetCFiltersReqRange the maximum number of filters that may be requested in
14// a getcfheaders message.
15const MaxGetCFiltersReqRange = 1000
16
17// MsgGetCFilters implements the Message interface and represents a bitcoin
18// getcfilters message. It is used to request committed filters for a range of
19// blocks.
20type MsgGetCFilters struct {
21	FilterType  FilterType
22	StartHeight uint32
23	StopHash    chainhash.Hash
24}
25
26// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
27// This is part of the Message interface implementation.
28func (msg *MsgGetCFilters) BtcDecode(r io.Reader, pver uint32, _ MessageEncoding) error {
29	err := readElement(r, &msg.FilterType)
30	if err != nil {
31		return err
32	}
33
34	err = readElement(r, &msg.StartHeight)
35	if err != nil {
36		return err
37	}
38
39	return readElement(r, &msg.StopHash)
40}
41
42// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
43// This is part of the Message interface implementation.
44func (msg *MsgGetCFilters) BtcEncode(w io.Writer, pver uint32, _ MessageEncoding) error {
45	err := writeElement(w, msg.FilterType)
46	if err != nil {
47		return err
48	}
49
50	err = writeElement(w, &msg.StartHeight)
51	if err != nil {
52		return err
53	}
54
55	return writeElement(w, &msg.StopHash)
56}
57
58// Command returns the protocol command string for the message.  This is part
59// of the Message interface implementation.
60func (msg *MsgGetCFilters) Command() string {
61	return CmdGetCFilters
62}
63
64// MaxPayloadLength returns the maximum length the payload can be for the
65// receiver.  This is part of the Message interface implementation.
66func (msg *MsgGetCFilters) MaxPayloadLength(pver uint32) uint32 {
67	// Filter type + uint32 + block hash
68	return 1 + 4 + chainhash.HashSize
69}
70
71// NewMsgGetCFilters returns a new bitcoin getcfilters message that conforms to
72// the Message interface using the passed parameters and defaults for the
73// remaining fields.
74func NewMsgGetCFilters(filterType FilterType, startHeight uint32,
75	stopHash *chainhash.Hash) *MsgGetCFilters {
76	return &MsgGetCFilters{
77		FilterType:  filterType,
78		StartHeight: startHeight,
79		StopHash:    *stopHash,
80	}
81}
82