lnwire+multi: define HasMaxHtlc helper on msgFlags

This commit is contained in:
Johan T. Halseth 2019-01-16 12:43:46 +01:00
parent 4fb1536f54
commit 7d34ce9d08
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26
4 changed files with 14 additions and 8 deletions

View File

@ -3372,7 +3372,7 @@ func serializeChanEdgePolicy(w io.Writer, edge *ChannelEdgePolicy,
// of the opaque data.
// TODO(halseth): clean up when moving to TLV.
var opaqueBuf bytes.Buffer
if edge.MessageFlags&lnwire.ChanUpdateOptionMaxHtlc != 0 {
if edge.MessageFlags.HasMaxHtlc() {
err := binary.Write(&opaqueBuf, byteOrder, uint64(edge.MaxHTLC))
if err != nil {
return err
@ -3465,7 +3465,7 @@ func deserializeChanEdgePolicy(r io.Reader,
}
// See if optional fields are present.
if edge.MessageFlags&lnwire.ChanUpdateOptionMaxHtlc != 0 {
if edge.MessageFlags.HasMaxHtlc() {
// The max_htlc field should be at the beginning of the opaque
// bytes.
opq := edge.ExtraOpaqueData

View File

@ -1324,7 +1324,7 @@ func (d *AuthenticatedGossiper) retransmitStaleChannels() error {
// If this edge has a ChannelUpdate that was created before the
// introduction of the MaxHTLC field, then we'll update this
// edge to propagate this information in the network.
if edge.MessageFlags&lnwire.ChanUpdateOptionMaxHtlc == 0 {
if !edge.MessageFlags.HasMaxHtlc() {
edgesToUpdate = append(edgesToUpdate, updateTuple{
info: info,
edge: edge,
@ -2520,7 +2520,7 @@ func (d *AuthenticatedGossiper) updateChannel(info *channeldb.ChannelEdgeInfo,
// We'll make sure we support the new max_htlc field if not already
// present.
if edge.MessageFlags&lnwire.ChanUpdateOptionMaxHtlc == 0 {
if !edge.MessageFlags.HasMaxHtlc() {
edge.MessageFlags |= lnwire.ChanUpdateOptionMaxHtlc
edge.MaxHTLC = lnwire.NewMSatFromSatoshis(info.Capacity)
}

View File

@ -24,6 +24,12 @@ func (c ChanUpdateMsgFlags) String() string {
return fmt.Sprintf("%08b", c)
}
// 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
}
// ChanUpdateChanFlags is a bitfield that signals various options concerning a
// particular channel edge. Each bit is to be examined in order to determine
// how the ChannelUpdate message is to be interpreted.
@ -139,7 +145,7 @@ func (a *ChannelUpdate) Decode(r io.Reader, pver uint32) error {
}
// Now check whether the max HTLC field is present and read it if so.
if a.MessageFlags&ChanUpdateOptionMaxHtlc != 0 {
if a.MessageFlags.HasMaxHtlc() {
if err := ReadElements(r, &a.HtlcMaximumMsat); err != nil {
return err
}
@ -183,7 +189,7 @@ func (a *ChannelUpdate) Encode(w io.Writer, pver uint32) error {
// Now append optional fields if they are set. Currently, the only
// optional field is max HTLC.
if a.MessageFlags&ChanUpdateOptionMaxHtlc != 0 {
if a.MessageFlags.HasMaxHtlc() {
if err := WriteElements(w, a.HtlcMaximumMsat); err != nil {
return err
}
@ -232,7 +238,7 @@ func (a *ChannelUpdate) DataToSign() ([]byte, error) {
// Now append optional fields if they are set. Currently, the only
// optional field is max HTLC.
if a.MessageFlags&ChanUpdateOptionMaxHtlc != 0 {
if a.MessageFlags.HasMaxHtlc() {
if err := WriteElements(&w, a.HtlcMaximumMsat); err != nil {
return nil, err
}

View File

@ -156,7 +156,7 @@ func ValidateChannelUpdateAnn(pubKey *btcec.PublicKey, capacity btcutil.Amount,
func validateOptionalFields(capacity btcutil.Amount,
msg *lnwire.ChannelUpdate) error {
if msg.MessageFlags&lnwire.ChanUpdateOptionMaxHtlc != 0 {
if msg.MessageFlags.HasMaxHtlc() {
maxHtlc := msg.HtlcMaximumMsat
if maxHtlc == 0 || maxHtlc < msg.HtlcMinimumMsat {
return errors.Errorf("invalid max htlc for channel "+