msggetcfheaders.go raw

   1  package wire
   2  
   3  import (
   4  	"io"
   5  	
   6  	"github.com/p9c/p9/pkg/chainhash"
   7  )
   8  
   9  // MsgGetCFHeaders is a message similar to MsgGetHeaders, but for committed filter headers. It allows to set the
  10  // FilterType field to get headers in the chain of basic (0x00) or extended (0x01) headers.
  11  type MsgGetCFHeaders struct {
  12  	FilterType  FilterType
  13  	StartHeight uint32
  14  	StopHash    chainhash.Hash
  15  }
  16  
  17  // BtcDecode decodes r using the bitcoin protocol encoding into the receiver. This is part of the Message interface
  18  // implementation.
  19  func (msg *MsgGetCFHeaders) BtcDecode(r io.Reader, pver uint32, _ MessageEncoding) (e error) {
  20  	if e = readElement(r, &msg.FilterType); E.Chk(e) {
  21  		return
  22  	}
  23  	if e = readElement(r, &msg.StartHeight); E.Chk(e) {
  24  		return
  25  	}
  26  	return readElement(r, &msg.StopHash)
  27  }
  28  
  29  // BtcEncode encodes the receiver to w using the bitcoin protocol encoding. This is part of the Message interface
  30  // implementation.
  31  func (msg *MsgGetCFHeaders) BtcEncode(w io.Writer, pver uint32, _ MessageEncoding) (e error) {
  32  	if e = writeElement(w, msg.FilterType); E.Chk(e) {
  33  		return
  34  	}
  35  	if e = writeElement(w, &msg.StartHeight); E.Chk(e) {
  36  		return
  37  	}
  38  	return writeElement(w, &msg.StopHash)
  39  }
  40  
  41  // Command returns the protocol command string for the message. This is part of the Message interface implementation.
  42  func (msg *MsgGetCFHeaders) Command() string {
  43  	return CmdGetCFHeaders
  44  }
  45  
  46  // MaxPayloadLength returns the maximum length the payload can be for the receiver. This is part of the Message
  47  // interface implementation.
  48  func (msg *MsgGetCFHeaders) MaxPayloadLength(pver uint32) uint32 {
  49  	// Filter type + uint32 + block hash
  50  	return 1 + 4 + chainhash.HashSize
  51  }
  52  
  53  // NewMsgGetCFHeaders returns a new bitcoin getcfheader message that conforms to the Message interface using the passed
  54  // parameters and defaults for the remaining fields.
  55  func NewMsgGetCFHeaders(filterType FilterType, startHeight uint32,
  56  	stopHash *chainhash.Hash,
  57  ) *MsgGetCFHeaders {
  58  	return &MsgGetCFHeaders{
  59  		FilterType:  filterType,
  60  		StartHeight: startHeight,
  61  		StopHash:    *stopHash,
  62  	}
  63  }
  64