channel_update.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package lnwire
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "github.com/btcsuite/btcd/chaincfg/chainhash"
  7. )
  8. // ChanUpdateMsgFlags is a bitfield that signals whether optional fields are
  9. // present in the ChannelUpdate.
  10. type ChanUpdateMsgFlags uint8
  11. const (
  12. // ChanUpdateRequiredMaxHtlc is a bit that indicates whether the
  13. // required htlc_maximum_msat field is present in this ChannelUpdate.
  14. ChanUpdateRequiredMaxHtlc ChanUpdateMsgFlags = 1 << iota
  15. )
  16. // String returns the bitfield flags as a string.
  17. func (c ChanUpdateMsgFlags) String() string {
  18. return fmt.Sprintf("%08b", c)
  19. }
  20. // HasMaxHtlc returns true if the htlc_maximum_msat option bit is set in the
  21. // message flags.
  22. func (c ChanUpdateMsgFlags) HasMaxHtlc() bool {
  23. return c&ChanUpdateRequiredMaxHtlc != 0
  24. }
  25. // ChanUpdateChanFlags is a bitfield that signals various options concerning a
  26. // particular channel edge. Each bit is to be examined in order to determine
  27. // how the ChannelUpdate message is to be interpreted.
  28. type ChanUpdateChanFlags uint8
  29. const (
  30. // ChanUpdateDirection indicates the direction of a channel update. If
  31. // this bit is set to 0 if Node1 (the node with the "smaller" Node ID)
  32. // is updating the channel, and to 1 otherwise.
  33. ChanUpdateDirection ChanUpdateChanFlags = 1 << iota
  34. // ChanUpdateDisabled is a bit that indicates if the channel edge
  35. // selected by the ChanUpdateDirection bit is to be treated as being
  36. // disabled.
  37. ChanUpdateDisabled
  38. )
  39. // IsDisabled determines whether the channel flags has the disabled bit set.
  40. func (c ChanUpdateChanFlags) IsDisabled() bool {
  41. return c&ChanUpdateDisabled == ChanUpdateDisabled
  42. }
  43. // String returns the bitfield flags as a string.
  44. func (c ChanUpdateChanFlags) String() string {
  45. return fmt.Sprintf("%08b", c)
  46. }
  47. // ChannelUpdate message is used after channel has been initially announced.
  48. // Each side independently announces its fees and minimum expiry for HTLCs and
  49. // other parameters. Also this message is used to redeclare initially set
  50. // channel parameters.
  51. type ChannelUpdate struct {
  52. // Signature is used to validate the announced data and prove the
  53. // ownership of node id.
  54. Signature Sig
  55. // ChainHash denotes the target chain that this channel was opened
  56. // within. This value should be the genesis hash of the target chain.
  57. // Along with the short channel ID, this uniquely identifies the
  58. // channel globally in a blockchain.
  59. ChainHash chainhash.Hash
  60. // ShortChannelID is the unique description of the funding transaction.
  61. ShortChannelID ShortChannelID
  62. // Timestamp allows ordering in the case of multiple announcements. We
  63. // should ignore the message if timestamp is not greater than
  64. // the last-received.
  65. Timestamp uint32
  66. // MessageFlags is a bitfield that describes whether optional fields
  67. // are present in this update. Currently, the least-significant bit
  68. // must be set to 1 if the optional field MaxHtlc is present.
  69. MessageFlags ChanUpdateMsgFlags
  70. // ChannelFlags is a bitfield that describes additional meta-data
  71. // concerning how the update is to be interpreted. Currently, the
  72. // least-significant bit must be set to 0 if the creating node
  73. // corresponds to the first node in the previously sent channel
  74. // announcement and 1 otherwise. If the second bit is set, then the
  75. // channel is set to be disabled.
  76. ChannelFlags ChanUpdateChanFlags
  77. // TimeLockDelta is the minimum number of blocks this node requires to
  78. // be added to the expiry of HTLCs. This is a security parameter
  79. // determined by the node operator. This value represents the required
  80. // gap between the time locks of the incoming and outgoing HTLC's set
  81. // to this node.
  82. TimeLockDelta uint16
  83. // HtlcMinimumMsat is the minimum HTLC value which will be accepted.
  84. HtlcMinimumMsat MilliSatoshi
  85. // BaseFee is the base fee that must be used for incoming HTLC's to
  86. // this particular channel. This value will be tacked onto the required
  87. // for a payment independent of the size of the payment.
  88. BaseFee uint32
  89. // FeeRate is the fee rate that will be charged per millionth of a
  90. // satoshi.
  91. FeeRate uint32
  92. // HtlcMaximumMsat is the maximum HTLC value which will be accepted.
  93. HtlcMaximumMsat MilliSatoshi
  94. // ExtraData is the set of data that was appended to this message to
  95. // fill out the full maximum transport message size. These fields can
  96. // be used to specify optional data such as custom TLV fields.
  97. ExtraOpaqueData ExtraOpaqueData
  98. }
  99. // A compile time check to ensure ChannelUpdate implements the lnwire.Message
  100. // interface.
  101. var _ Message = (*ChannelUpdate)(nil)
  102. // Decode deserializes a serialized ChannelUpdate stored in the passed
  103. // io.Reader observing the specified protocol version.
  104. //
  105. // This is part of the lnwire.Message interface.
  106. func (a *ChannelUpdate) Decode(r io.Reader, pver uint32) error {
  107. err := ReadElements(r,
  108. &a.Signature,
  109. a.ChainHash[:],
  110. &a.ShortChannelID,
  111. &a.Timestamp,
  112. &a.MessageFlags,
  113. &a.ChannelFlags,
  114. &a.TimeLockDelta,
  115. &a.HtlcMinimumMsat,
  116. &a.BaseFee,
  117. &a.FeeRate,
  118. )
  119. if err != nil {
  120. return err
  121. }
  122. // Now check whether the max HTLC field is present and read it if so.
  123. if a.MessageFlags.HasMaxHtlc() {
  124. if err := ReadElements(r, &a.HtlcMaximumMsat); err != nil {
  125. return err
  126. }
  127. }
  128. return a.ExtraOpaqueData.Decode(r)
  129. }
  130. // Encode serializes the target ChannelUpdate into the passed io.Writer
  131. // observing the protocol version specified.
  132. //
  133. // This is part of the lnwire.Message interface.
  134. func (a *ChannelUpdate) Encode(w *bytes.Buffer, pver uint32) error {
  135. if err := WriteSig(w, a.Signature); err != nil {
  136. return err
  137. }
  138. if err := WriteBytes(w, a.ChainHash[:]); err != nil {
  139. return err
  140. }
  141. if err := WriteShortChannelID(w, a.ShortChannelID); err != nil {
  142. return err
  143. }
  144. if err := WriteUint32(w, a.Timestamp); err != nil {
  145. return err
  146. }
  147. if err := WriteChanUpdateMsgFlags(w, a.MessageFlags); err != nil {
  148. return err
  149. }
  150. if err := WriteChanUpdateChanFlags(w, a.ChannelFlags); err != nil {
  151. return err
  152. }
  153. if err := WriteUint16(w, a.TimeLockDelta); err != nil {
  154. return err
  155. }
  156. if err := WriteMilliSatoshi(w, a.HtlcMinimumMsat); err != nil {
  157. return err
  158. }
  159. if err := WriteUint32(w, a.BaseFee); err != nil {
  160. return err
  161. }
  162. if err := WriteUint32(w, a.FeeRate); err != nil {
  163. return err
  164. }
  165. // Now append optional fields if they are set. Currently, the only
  166. // optional field is max HTLC.
  167. if a.MessageFlags.HasMaxHtlc() {
  168. err := WriteMilliSatoshi(w, a.HtlcMaximumMsat)
  169. if err != nil {
  170. return err
  171. }
  172. }
  173. // Finally, append any extra opaque data.
  174. return WriteBytes(w, a.ExtraOpaqueData)
  175. }
  176. // MsgType returns the integer uniquely identifying this message type on the
  177. // wire.
  178. //
  179. // This is part of the lnwire.Message interface.
  180. func (a *ChannelUpdate) MsgType() MessageType {
  181. return MsgChannelUpdate
  182. }
  183. // DataToSign is used to retrieve part of the announcement message which should
  184. // be signed.
  185. func (a *ChannelUpdate) DataToSign() ([]byte, error) {
  186. // We should not include the signatures itself.
  187. b := make([]byte, 0, MaxMsgBody)
  188. buf := bytes.NewBuffer(b)
  189. if err := WriteBytes(buf, a.ChainHash[:]); err != nil {
  190. return nil, err
  191. }
  192. if err := WriteShortChannelID(buf, a.ShortChannelID); err != nil {
  193. return nil, err
  194. }
  195. if err := WriteUint32(buf, a.Timestamp); err != nil {
  196. return nil, err
  197. }
  198. if err := WriteChanUpdateMsgFlags(buf, a.MessageFlags); err != nil {
  199. return nil, err
  200. }
  201. if err := WriteChanUpdateChanFlags(buf, a.ChannelFlags); err != nil {
  202. return nil, err
  203. }
  204. if err := WriteUint16(buf, a.TimeLockDelta); err != nil {
  205. return nil, err
  206. }
  207. if err := WriteMilliSatoshi(buf, a.HtlcMinimumMsat); err != nil {
  208. return nil, err
  209. }
  210. if err := WriteUint32(buf, a.BaseFee); err != nil {
  211. return nil, err
  212. }
  213. if err := WriteUint32(buf, a.FeeRate); err != nil {
  214. return nil, err
  215. }
  216. // Now append optional fields if they are set. Currently, the only
  217. // optional field is max HTLC.
  218. if a.MessageFlags.HasMaxHtlc() {
  219. err := WriteMilliSatoshi(buf, a.HtlcMaximumMsat)
  220. if err != nil {
  221. return nil, err
  222. }
  223. }
  224. // Finally, append any extra opaque data.
  225. if err := WriteBytes(buf, a.ExtraOpaqueData); err != nil {
  226. return nil, err
  227. }
  228. return buf.Bytes(), nil
  229. }