github.com/decred/dcrlnd@v0.7.6/lnwire/reply_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 // ReplyChannelRange is the response to the QueryChannelRange message. It 12 // includes the original query, and the next streaming chunk of encoded short 13 // channel ID's as the response. We'll also include a byte that indicates if 14 // this is the last query in the message. 15 type ReplyChannelRange 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 // Complete denotes if this is the conclusion of the set of streaming 30 // responses to the original query. 31 Complete uint8 32 33 // EncodingType is a signal to the receiver of the message that 34 // indicates exactly how the set of short channel ID's that follow have 35 // been encoded. 36 EncodingType ShortChanIDEncoding 37 38 // ShortChanIDs is a slice of decoded short channel ID's. 39 ShortChanIDs []ShortChannelID 40 41 // ExtraData is the set of data that was appended to this message to 42 // fill out the full maximum transport message size. These fields can 43 // be used to specify optional data such as custom TLV fields. 44 ExtraData ExtraOpaqueData 45 46 // noSort indicates whether or not to sort the short channel ids before 47 // writing them out. 48 // 49 // NOTE: This should only be used for testing. 50 noSort bool 51 } 52 53 // NewReplyChannelRange creates a new empty ReplyChannelRange message. 54 func NewReplyChannelRange() *ReplyChannelRange { 55 return &ReplyChannelRange{} 56 } 57 58 // A compile time check to ensure ReplyChannelRange implements the 59 // lnwire.Message interface. 60 var _ Message = (*ReplyChannelRange)(nil) 61 62 // Decode deserializes a serialized ReplyChannelRange message stored in the 63 // passed io.Reader observing the specified protocol version. 64 // 65 // This is part of the lnwire.Message interface. 66 func (c *ReplyChannelRange) Decode(r io.Reader, pver uint32) error { 67 err := ReadElements(r, 68 c.ChainHash[:], 69 &c.FirstBlockHeight, 70 &c.NumBlocks, 71 &c.Complete, 72 ) 73 if err != nil { 74 return err 75 } 76 77 c.EncodingType, c.ShortChanIDs, err = decodeShortChanIDs(r) 78 if err != nil { 79 return err 80 } 81 82 return c.ExtraData.Decode(r) 83 } 84 85 // Encode serializes the target ReplyChannelRange into the passed io.Writer 86 // observing the protocol version specified. 87 // 88 // This is part of the lnwire.Message interface. 89 func (c *ReplyChannelRange) Encode(w *bytes.Buffer, pver uint32) error { 90 if err := WriteBytes(w, c.ChainHash[:]); err != nil { 91 return err 92 } 93 94 if err := WriteUint32(w, c.FirstBlockHeight); err != nil { 95 return err 96 } 97 98 if err := WriteUint32(w, c.NumBlocks); err != nil { 99 return err 100 } 101 102 if err := WriteUint8(w, c.Complete); err != nil { 103 return err 104 } 105 106 err := encodeShortChanIDs(w, c.EncodingType, c.ShortChanIDs, c.noSort) 107 if err != nil { 108 return err 109 } 110 111 return WriteBytes(w, c.ExtraData) 112 } 113 114 // MsgType returns the integer uniquely identifying this message type on the 115 // wire. 116 // 117 // This is part of the lnwire.Message interface. 118 func (c *ReplyChannelRange) MsgType() MessageType { 119 return MsgReplyChannelRange 120 } 121 122 // LastBlockHeight returns the last block height covered by the range of a 123 // QueryChannelRange message. 124 func (c *ReplyChannelRange) LastBlockHeight() uint32 { 125 // Handle overflows by casting to uint64. 126 lastBlockHeight := uint64(c.FirstBlockHeight) + uint64(c.NumBlocks) - 1 127 if lastBlockHeight > math.MaxUint32 { 128 return math.MaxUint32 129 } 130 return uint32(lastBlockHeight) 131 }