msggetcfheaders.go raw
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 "io"
9
10 "github.com/btcsuite/btcd/chainhash/v2"
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.
16 type 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.
24 func (msg *MsgGetCFHeaders) BtcDecode(r io.Reader, pver uint32, _ MessageEncoding) error {
25 buf := binarySerializer.Borrow()
26 defer binarySerializer.Return(buf)
27
28 if _, err := io.ReadFull(r, buf[:1]); err != nil {
29 return err
30 }
31 msg.FilterType = FilterType(buf[0])
32
33 if _, err := io.ReadFull(r, buf[:4]); err != nil {
34 return err
35 }
36 msg.StartHeight = littleEndian.Uint32(buf[:4])
37
38 _, err := io.ReadFull(r, msg.StopHash[:])
39 return err
40 }
41
42 // BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
43 // This is part of the Message interface implementation.
44 func (msg *MsgGetCFHeaders) BtcEncode(w io.Writer, pver uint32, _ MessageEncoding) error {
45 buf := binarySerializer.Borrow()
46 defer binarySerializer.Return(buf)
47
48 buf[0] = byte(msg.FilterType)
49 if _, err := w.Write(buf[:1]); err != nil {
50 return err
51 }
52
53 littleEndian.PutUint32(buf[:4], msg.StartHeight)
54 if _, err := w.Write(buf[:4]); err != nil {
55 return err
56 }
57
58 _, err := w.Write(msg.StopHash[:])
59 return err
60 }
61
62 // Command returns the protocol command string for the message. This is part
63 // of the Message interface implementation.
64 func (msg *MsgGetCFHeaders) Command() string {
65 return CmdGetCFHeaders
66 }
67
68 // MaxPayloadLength returns the maximum length the payload can be for the
69 // receiver. This is part of the Message interface implementation.
70 func (msg *MsgGetCFHeaders) MaxPayloadLength(pver uint32) uint32 {
71 // Filter type + uint32 + block hash
72 return 1 + 4 + chainhash.HashSize
73 }
74
75 // NewMsgGetCFHeaders returns a new bitcoin getcfheader message that conforms to
76 // the Message interface using the passed parameters and defaults for the
77 // remaining fields.
78 func NewMsgGetCFHeaders(filterType FilterType, startHeight uint32,
79 stopHash *chainhash.Hash) *MsgGetCFHeaders {
80 return &MsgGetCFHeaders{
81 FilterType: filterType,
82 StartHeight: startHeight,
83 StopHash: *stopHash,
84 }
85 }
86