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