msggetcfcheckpt.go raw

   1  // Copyright (c) 2018 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  // MsgGetCFCheckpt is a request for filter headers at evenly spaced intervals
  14  // throughout the blockchain history. It allows to set the FilterType field to
  15  // get headers in the chain of basic (0x00) or extended (0x01) headers.
  16  type MsgGetCFCheckpt struct {
  17  	FilterType FilterType
  18  	StopHash   chainhash.Hash
  19  }
  20  
  21  // BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
  22  // This is part of the Message interface implementation.
  23  func (msg *MsgGetCFCheckpt) BtcDecode(r io.Reader, pver uint32, _ MessageEncoding) error {
  24  	buf := binarySerializer.Borrow()
  25  	defer binarySerializer.Return(buf)
  26  
  27  	if _, err := io.ReadFull(r, buf[:1]); err != nil {
  28  		return err
  29  	}
  30  	msg.FilterType = FilterType(buf[0])
  31  
  32  	_, err := io.ReadFull(r, msg.StopHash[:])
  33  	return err
  34  }
  35  
  36  // BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
  37  // This is part of the Message interface implementation.
  38  func (msg *MsgGetCFCheckpt) BtcEncode(w io.Writer, pver uint32, _ MessageEncoding) error {
  39  	buf := binarySerializer.Borrow()
  40  	defer binarySerializer.Return(buf)
  41  
  42  	buf[0] = byte(msg.FilterType)
  43  	if _, err := w.Write(buf[:1]); err != nil {
  44  		return err
  45  	}
  46  
  47  	_, err := w.Write(msg.StopHash[:])
  48  	return err
  49  }
  50  
  51  // Command returns the protocol command string for the message.  This is part
  52  // of the Message interface implementation.
  53  func (msg *MsgGetCFCheckpt) Command() string {
  54  	return CmdGetCFCheckpt
  55  }
  56  
  57  // MaxPayloadLength returns the maximum length the payload can be for the
  58  // receiver.  This is part of the Message interface implementation.
  59  func (msg *MsgGetCFCheckpt) MaxPayloadLength(pver uint32) uint32 {
  60  	// Filter type + uint32 + block hash
  61  	return 1 + chainhash.HashSize
  62  }
  63  
  64  // NewMsgGetCFCheckpt returns a new bitcoin getcfcheckpt message that conforms
  65  // to the Message interface using the passed parameters and defaults for the
  66  // remaining fields.
  67  func NewMsgGetCFCheckpt(filterType FilterType, stopHash *chainhash.Hash) *MsgGetCFCheckpt {
  68  	return &MsgGetCFCheckpt{
  69  		FilterType: filterType,
  70  		StopHash:   *stopHash,
  71  	}
  72  }
  73