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