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// MsgGetCFHeaders is a message similar to MsgGetHeaders, but for committed
14// filter headers. It allows to set the FilterType field to get headers in the
15// chain of basic (0x00) or extended (0x01) headers.
16type MsgGetCFHeaders struct {
17	FilterType  FilterType
18	StartHeight uint32
19	StopHash    chainhash.Hash
20}
21
22// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
23// This is part of the Message interface implementation.
24func (msg *MsgGetCFHeaders) BtcDecode(r io.Reader, pver uint32, _ MessageEncoding) error {
25	err := readElement(r, &msg.FilterType)
26	if err != nil {
27		return err
28	}
29
30	err = readElement(r, &msg.StartHeight)
31	if err != nil {
32		return err
33	}
34
35	return readElement(r, &msg.StopHash)
36}
37
38// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
39// This is part of the Message interface implementation.
40func (msg *MsgGetCFHeaders) BtcEncode(w io.Writer, pver uint32, _ MessageEncoding) error {
41	err := writeElement(w, msg.FilterType)
42	if err != nil {
43		return err
44	}
45
46	err = writeElement(w, &msg.StartHeight)
47	if err != nil {
48		return err
49	}
50
51	return writeElement(w, &msg.StopHash)
52}
53
54// Command returns the protocol command string for the message.  This is part
55// of the Message interface implementation.
56func (msg *MsgGetCFHeaders) Command() string {
57	return CmdGetCFHeaders
58}
59
60// MaxPayloadLength returns the maximum length the payload can be for the
61// receiver.  This is part of the Message interface implementation.
62func (msg *MsgGetCFHeaders) MaxPayloadLength(pver uint32) uint32 {
63	// Filter type + uint32 + block hash
64	return 1 + 4 + chainhash.HashSize
65}
66
67// NewMsgGetCFHeaders returns a new bitcoin getcfheader message that conforms to
68// the Message interface using the passed parameters and defaults for the
69// remaining fields.
70func NewMsgGetCFHeaders(filterType FilterType, startHeight uint32,
71	stopHash *chainhash.Hash) *MsgGetCFHeaders {
72	return &MsgGetCFHeaders{
73		FilterType:  filterType,
74		StartHeight: startHeight,
75		StopHash:    *stopHash,
76	}
77}
78