2016-12-07 18:46:22 +03:00
|
|
|
package lnwire
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-01-12 20:59:43 +03:00
|
|
|
"fmt"
|
2016-12-07 18:46:22 +03:00
|
|
|
"io"
|
2018-09-01 05:33:05 +03:00
|
|
|
"io/ioutil"
|
2016-12-22 23:17:24 +03:00
|
|
|
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2016-12-07 18:46:22 +03:00
|
|
|
)
|
|
|
|
|
2019-01-12 20:59:43 +03:00
|
|
|
// ChanUpdateMsgFlags is a bitfield that signals whether optional fields are
|
|
|
|
// present in the ChannelUpdate.
|
|
|
|
type ChanUpdateMsgFlags uint8
|
|
|
|
|
2019-01-12 20:59:43 +03:00
|
|
|
const (
|
|
|
|
// ChanUpdateOptionMaxHtlc is a bit that indicates whether the
|
|
|
|
// optional htlc_maximum_msat field is present in this ChannelUpdate.
|
|
|
|
ChanUpdateOptionMaxHtlc ChanUpdateMsgFlags = 1 << iota
|
|
|
|
)
|
|
|
|
|
2019-01-12 20:59:43 +03:00
|
|
|
// String returns the bitfield flags as a string.
|
|
|
|
func (c ChanUpdateMsgFlags) String() string {
|
|
|
|
return fmt.Sprintf("%08b", c)
|
|
|
|
}
|
|
|
|
|
2019-01-16 14:43:46 +03:00
|
|
|
// HasMaxHtlc returns true if the htlc_maximum_msat option bit is set in the
|
|
|
|
// message flags.
|
|
|
|
func (c ChanUpdateMsgFlags) HasMaxHtlc() bool {
|
|
|
|
return c&ChanUpdateOptionMaxHtlc != 0
|
|
|
|
}
|
|
|
|
|
2019-01-12 20:59:43 +03:00
|
|
|
// ChanUpdateChanFlags is a bitfield that signals various options concerning a
|
2017-12-01 09:21:27 +03:00
|
|
|
// particular channel edge. Each bit is to be examined in order to determine
|
|
|
|
// how the ChannelUpdate message is to be interpreted.
|
2019-01-12 20:59:43 +03:00
|
|
|
type ChanUpdateChanFlags uint8
|
2017-12-01 09:21:27 +03:00
|
|
|
|
|
|
|
const (
|
|
|
|
// ChanUpdateDirection indicates the direction of a channel update. If
|
|
|
|
// this bit is set to 0 if Node1 (the node with the "smaller" Node ID)
|
|
|
|
// is updating the channel, and to 1 otherwise.
|
2019-01-12 20:59:43 +03:00
|
|
|
ChanUpdateDirection ChanUpdateChanFlags = 1 << iota
|
2017-12-01 09:21:27 +03:00
|
|
|
|
|
|
|
// ChanUpdateDisabled is a bit that indicates if the channel edge
|
|
|
|
// selected by the ChanUpdateDirection bit is to be treated as being
|
|
|
|
// disabled.
|
|
|
|
ChanUpdateDisabled
|
|
|
|
)
|
|
|
|
|
2020-11-20 04:31:58 +03:00
|
|
|
// IsDisabled determines whether the channel flags has the disabled bit set.
|
|
|
|
func (c ChanUpdateChanFlags) IsDisabled() bool {
|
|
|
|
return c&ChanUpdateDisabled == ChanUpdateDisabled
|
|
|
|
}
|
|
|
|
|
2019-01-12 20:59:43 +03:00
|
|
|
// String returns the bitfield flags as a string.
|
|
|
|
func (c ChanUpdateChanFlags) String() string {
|
|
|
|
return fmt.Sprintf("%08b", c)
|
|
|
|
}
|
|
|
|
|
2017-04-18 02:20:15 +03:00
|
|
|
// ChannelUpdate message is used after channel has been initially announced.
|
|
|
|
// Each side independently announces its fees and minimum expiry for HTLCs and
|
2017-12-18 05:40:05 +03:00
|
|
|
// other parameters. Also this message is used to redeclare initially set
|
2017-04-18 02:20:15 +03:00
|
|
|
// channel parameters.
|
|
|
|
type ChannelUpdate struct {
|
2016-12-07 18:46:22 +03:00
|
|
|
// Signature is used to validate the announced data and prove the
|
|
|
|
// ownership of node id.
|
2018-01-31 06:41:52 +03:00
|
|
|
Signature Sig
|
2016-12-07 18:46:22 +03:00
|
|
|
|
2017-08-22 08:45:58 +03:00
|
|
|
// ChainHash denotes the target chain that this channel was opened
|
|
|
|
// within. This value should be the genesis hash of the target chain.
|
|
|
|
// Along with the short channel ID, this uniquely identifies the
|
|
|
|
// channel globally in a blockchain.
|
|
|
|
ChainHash chainhash.Hash
|
|
|
|
|
2017-03-27 18:22:37 +03:00
|
|
|
// ShortChannelID is the unique description of the funding transaction.
|
|
|
|
ShortChannelID ShortChannelID
|
2016-12-07 18:46:22 +03:00
|
|
|
|
2020-11-26 00:12:29 +03:00
|
|
|
// Timestamp allows ordering in the case of multiple announcements. We
|
2017-08-22 08:45:58 +03:00
|
|
|
// should ignore the message if timestamp is not greater than
|
2016-12-07 18:46:22 +03:00
|
|
|
// the last-received.
|
|
|
|
Timestamp uint32
|
|
|
|
|
2019-01-12 20:59:43 +03:00
|
|
|
// MessageFlags is a bitfield that describes whether optional fields
|
|
|
|
// are present in this update. Currently, the least-significant bit
|
|
|
|
// must be set to 1 if the optional field MaxHtlc is present.
|
|
|
|
MessageFlags ChanUpdateMsgFlags
|
|
|
|
|
|
|
|
// ChannelFlags is a bitfield that describes additional meta-data
|
|
|
|
// concerning how the update is to be interpreted. Currently, the
|
2017-12-01 09:21:27 +03:00
|
|
|
// least-significant bit must be set to 0 if the creating node
|
2017-03-06 07:43:34 +03:00
|
|
|
// corresponds to the first node in the previously sent channel
|
2017-12-01 09:21:27 +03:00
|
|
|
// announcement and 1 otherwise. If the second bit is set, then the
|
|
|
|
// channel is set to be disabled.
|
2019-01-12 20:59:43 +03:00
|
|
|
ChannelFlags ChanUpdateChanFlags
|
2016-12-07 18:46:22 +03:00
|
|
|
|
2017-03-06 07:43:34 +03:00
|
|
|
// TimeLockDelta is the minimum number of blocks this node requires to
|
|
|
|
// be added to the expiry of HTLCs. This is a security parameter
|
|
|
|
// determined by the node operator. This value represents the required
|
|
|
|
// gap between the time locks of the incoming and outgoing HTLC's set
|
|
|
|
// to this node.
|
|
|
|
TimeLockDelta uint16
|
2016-12-07 18:46:22 +03:00
|
|
|
|
2017-03-09 01:20:00 +03:00
|
|
|
// HtlcMinimumMsat is the minimum HTLC value which will be accepted.
|
2017-08-22 08:45:58 +03:00
|
|
|
HtlcMinimumMsat MilliSatoshi
|
2016-12-07 18:46:22 +03:00
|
|
|
|
2017-06-16 23:46:31 +03:00
|
|
|
// BaseFee is the base fee that must be used for incoming HTLC's to
|
|
|
|
// this particular channel. This value will be tacked onto the required
|
|
|
|
// for a payment independent of the size of the payment.
|
|
|
|
BaseFee uint32
|
2016-12-07 18:46:22 +03:00
|
|
|
|
2017-06-16 23:46:31 +03:00
|
|
|
// FeeRate is the fee rate that will be charged per millionth of a
|
|
|
|
// satoshi.
|
|
|
|
FeeRate uint32
|
2018-09-01 05:33:05 +03:00
|
|
|
|
2019-01-12 20:59:43 +03:00
|
|
|
// HtlcMaximumMsat is the maximum HTLC value which will be accepted.
|
|
|
|
HtlcMaximumMsat MilliSatoshi
|
|
|
|
|
2018-09-01 05:33:05 +03:00
|
|
|
// ExtraOpaqueData is the set of data that was appended to this
|
|
|
|
// message, some of which we may not actually know how to iterate or
|
|
|
|
// parse. By holding onto this data, we ensure that we're able to
|
|
|
|
// properly validate the set of signatures that cover these new fields,
|
|
|
|
// and ensure we're able to make upgrades to the network in a forwards
|
|
|
|
// compatible manner.
|
|
|
|
ExtraOpaqueData []byte
|
2016-12-07 18:46:22 +03:00
|
|
|
}
|
|
|
|
|
2017-04-18 02:20:15 +03:00
|
|
|
// A compile time check to ensure ChannelUpdate implements the lnwire.Message
|
|
|
|
// interface.
|
|
|
|
var _ Message = (*ChannelUpdate)(nil)
|
2016-12-07 18:46:22 +03:00
|
|
|
|
2017-04-18 02:20:15 +03:00
|
|
|
// Decode deserializes a serialized ChannelUpdate stored in the passed
|
|
|
|
// io.Reader observing the specified protocol version.
|
2016-12-07 18:46:22 +03:00
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2017-04-18 02:20:15 +03:00
|
|
|
func (a *ChannelUpdate) Decode(r io.Reader, pver uint32) error {
|
2018-12-10 05:27:41 +03:00
|
|
|
err := ReadElements(r,
|
2017-02-23 22:56:47 +03:00
|
|
|
&a.Signature,
|
2017-08-22 08:45:58 +03:00
|
|
|
a.ChainHash[:],
|
2017-03-27 18:22:37 +03:00
|
|
|
&a.ShortChannelID,
|
2017-02-23 22:56:47 +03:00
|
|
|
&a.Timestamp,
|
2019-01-12 20:59:43 +03:00
|
|
|
&a.MessageFlags,
|
|
|
|
&a.ChannelFlags,
|
2017-02-23 22:56:47 +03:00
|
|
|
&a.TimeLockDelta,
|
|
|
|
&a.HtlcMinimumMsat,
|
2017-06-16 23:46:31 +03:00
|
|
|
&a.BaseFee,
|
|
|
|
&a.FeeRate,
|
2016-12-07 18:46:22 +03:00
|
|
|
)
|
2018-09-01 05:33:05 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-12 20:59:43 +03:00
|
|
|
// Now check whether the max HTLC field is present and read it if so.
|
2019-01-16 14:43:46 +03:00
|
|
|
if a.MessageFlags.HasMaxHtlc() {
|
2019-01-12 20:59:43 +03:00
|
|
|
if err := ReadElements(r, &a.HtlcMaximumMsat); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-01 05:33:05 +03:00
|
|
|
// Now that we've read out all the fields that we explicitly know of,
|
|
|
|
// we'll collect the remainder into the ExtraOpaqueData field. If there
|
|
|
|
// aren't any bytes, then we'll snip off the slice to avoid carrying
|
|
|
|
// around excess capacity.
|
|
|
|
a.ExtraOpaqueData, err = ioutil.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(a.ExtraOpaqueData) == 0 {
|
|
|
|
a.ExtraOpaqueData = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2016-12-07 18:46:22 +03:00
|
|
|
}
|
|
|
|
|
2017-04-18 02:20:15 +03:00
|
|
|
// Encode serializes the target ChannelUpdate into the passed io.Writer
|
|
|
|
// observing the protocol version specified.
|
2016-12-07 18:46:22 +03:00
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2017-04-18 02:20:15 +03:00
|
|
|
func (a *ChannelUpdate) Encode(w io.Writer, pver uint32) error {
|
2019-01-12 20:59:43 +03:00
|
|
|
err := WriteElements(w,
|
2017-02-23 22:56:47 +03:00
|
|
|
a.Signature,
|
2017-08-22 08:45:58 +03:00
|
|
|
a.ChainHash[:],
|
2017-03-27 18:22:37 +03:00
|
|
|
a.ShortChannelID,
|
2017-02-23 22:56:47 +03:00
|
|
|
a.Timestamp,
|
2019-01-12 20:59:43 +03:00
|
|
|
a.MessageFlags,
|
|
|
|
a.ChannelFlags,
|
2017-02-23 22:56:47 +03:00
|
|
|
a.TimeLockDelta,
|
|
|
|
a.HtlcMinimumMsat,
|
2017-06-16 23:46:31 +03:00
|
|
|
a.BaseFee,
|
|
|
|
a.FeeRate,
|
2016-12-07 18:46:22 +03:00
|
|
|
)
|
2019-01-12 20:59:43 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now append optional fields if they are set. Currently, the only
|
|
|
|
// optional field is max HTLC.
|
2019-01-16 14:43:46 +03:00
|
|
|
if a.MessageFlags.HasMaxHtlc() {
|
2019-01-12 20:59:43 +03:00
|
|
|
if err := WriteElements(w, a.HtlcMaximumMsat); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, append any extra opaque data.
|
|
|
|
return WriteElements(w, a.ExtraOpaqueData)
|
2016-12-07 18:46:22 +03:00
|
|
|
}
|
|
|
|
|
2017-04-20 01:57:43 +03:00
|
|
|
// MsgType returns the integer uniquely identifying this message type on the
|
2016-12-07 18:46:22 +03:00
|
|
|
// wire.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2017-04-20 01:57:43 +03:00
|
|
|
func (a *ChannelUpdate) MsgType() MessageType {
|
|
|
|
return MsgChannelUpdate
|
2016-12-07 18:46:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// MaxPayloadLength returns the maximum allowed payload size for this message
|
|
|
|
// observing the specified protocol version.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2017-04-18 02:20:15 +03:00
|
|
|
func (a *ChannelUpdate) MaxPayloadLength(pver uint32) uint32 {
|
2018-09-01 05:33:05 +03:00
|
|
|
return 65533
|
2016-12-07 18:46:22 +03:00
|
|
|
}
|
|
|
|
|
2017-04-18 02:20:15 +03:00
|
|
|
// DataToSign is used to retrieve part of the announcement message which should
|
|
|
|
// be signed.
|
|
|
|
func (a *ChannelUpdate) DataToSign() ([]byte, error) {
|
2016-12-07 18:46:22 +03:00
|
|
|
|
|
|
|
// We should not include the signatures itself.
|
|
|
|
var w bytes.Buffer
|
2018-12-10 05:27:41 +03:00
|
|
|
err := WriteElements(&w,
|
2017-08-22 08:45:58 +03:00
|
|
|
a.ChainHash[:],
|
2017-03-27 18:22:37 +03:00
|
|
|
a.ShortChannelID,
|
2017-02-23 22:56:47 +03:00
|
|
|
a.Timestamp,
|
2019-01-12 20:59:43 +03:00
|
|
|
a.MessageFlags,
|
|
|
|
a.ChannelFlags,
|
2017-02-23 22:56:47 +03:00
|
|
|
a.TimeLockDelta,
|
|
|
|
a.HtlcMinimumMsat,
|
2017-06-16 23:46:31 +03:00
|
|
|
a.BaseFee,
|
|
|
|
a.FeeRate,
|
2016-12-07 18:46:22 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-12 20:59:43 +03:00
|
|
|
// Now append optional fields if they are set. Currently, the only
|
|
|
|
// optional field is max HTLC.
|
2019-01-16 14:43:46 +03:00
|
|
|
if a.MessageFlags.HasMaxHtlc() {
|
2019-01-12 20:59:43 +03:00
|
|
|
if err := WriteElements(&w, a.HtlcMaximumMsat); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, append any extra opaque data.
|
|
|
|
if err := WriteElements(&w, a.ExtraOpaqueData); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-12-07 18:46:22 +03:00
|
|
|
return w.Bytes(), nil
|
|
|
|
}
|