github.com/decred/dcrlnd@v0.7.6/lnwire/query_channel_range.go (about)

     1  package lnwire
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"math"
     7  
     8  	"github.com/decred/dcrd/chaincfg/chainhash"
     9  )
    10  
    11  // QueryChannelRange is a message sent by a node in order to query the
    12  // receiving node of the set of open channel they know of with short channel
    13  // ID's after the specified block height, capped at the number of blocks beyond
    14  // that block height. This will be used by nodes upon initial connect to
    15  // synchronize their views of the network.
    16  type QueryChannelRange struct {
    17  	// ChainHash denotes the target chain that we're trying to synchronize
    18  	// channel graph state for.
    19  	ChainHash chainhash.Hash
    20  
    21  	// FirstBlockHeight is the first block in the query range. The
    22  	// responder should send all new short channel IDs from this block
    23  	// until this block plus the specified number of blocks.
    24  	FirstBlockHeight uint32
    25  
    26  	// NumBlocks is the number of blocks beyond the first block that short
    27  	// channel ID's should be sent for.
    28  	NumBlocks uint32
    29  
    30  	// ExtraData is the set of data that was appended to this message to
    31  	// fill out the full maximum transport message size. These fields can
    32  	// be used to specify optional data such as custom TLV fields.
    33  	ExtraData ExtraOpaqueData
    34  }
    35  
    36  // NewQueryChannelRange creates a new empty QueryChannelRange message.
    37  func NewQueryChannelRange() *QueryChannelRange {
    38  	return &QueryChannelRange{}
    39  }
    40  
    41  // A compile time check to ensure QueryChannelRange implements the
    42  // lnwire.Message interface.
    43  var _ Message = (*QueryChannelRange)(nil)
    44  
    45  // Decode deserializes a serialized QueryChannelRange message stored in the
    46  // passed io.Reader observing the specified protocol version.
    47  //
    48  // This is part of the lnwire.Message interface.
    49  func (q *QueryChannelRange) Decode(r io.Reader, pver uint32) error {
    50  	return ReadElements(r,
    51  		q.ChainHash[:],
    52  		&q.FirstBlockHeight,
    53  		&q.NumBlocks,
    54  		&q.ExtraData,
    55  	)
    56  }
    57  
    58  // Encode serializes the target QueryChannelRange into the passed io.Writer
    59  // observing the protocol version specified.
    60  //
    61  // This is part of the lnwire.Message interface.
    62  func (q *QueryChannelRange) Encode(w *bytes.Buffer, pver uint32) error {
    63  	if err := WriteBytes(w, q.ChainHash[:]); err != nil {
    64  		return err
    65  	}
    66  
    67  	if err := WriteUint32(w, q.FirstBlockHeight); err != nil {
    68  		return err
    69  	}
    70  
    71  	if err := WriteUint32(w, q.NumBlocks); err != nil {
    72  		return err
    73  	}
    74  
    75  	return WriteBytes(w, q.ExtraData)
    76  }
    77  
    78  // MsgType returns the integer uniquely identifying this message type on the
    79  // wire.
    80  //
    81  // This is part of the lnwire.Message interface.
    82  func (q *QueryChannelRange) MsgType() MessageType {
    83  	return MsgQueryChannelRange
    84  }
    85  
    86  // LastBlockHeight returns the last block height covered by the range of a
    87  // QueryChannelRange message.
    88  func (q *QueryChannelRange) LastBlockHeight() uint32 {
    89  	// Handle overflows by casting to uint64.
    90  	lastBlockHeight := uint64(q.FirstBlockHeight) + uint64(q.NumBlocks) - 1
    91  	if lastBlockHeight > math.MaxUint32 {
    92  		return math.MaxUint32
    93  	}
    94  	return uint32(lastBlockHeight)
    95  }