From 2df5a36048069597907f160850f542178663e64a Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Wed, 25 Sep 2019 12:00:59 -0700 Subject: [PATCH] peer+lnwire: add LinkUpdater iface and impl on relevant msgs Removes longstanding TODO to simplify parsing of target chanid. --- lnwire/commit_sig.go | 12 +++++++- lnwire/revoke_and_ack.go | 8 ++++++ lnwire/update_add_htlc.go | 12 +++++++- lnwire/update_fail_htlc.go | 12 +++++++- lnwire/update_fail_malformed_htlc.go | 8 ++++++ lnwire/update_fee.go | 8 ++++++ lnwire/update_fulfill_htlc.go | 12 +++++++- peer.go | 42 +++++++++++----------------- 8 files changed, 84 insertions(+), 30 deletions(-) diff --git a/lnwire/commit_sig.go b/lnwire/commit_sig.go index 5002d5fe..72c235b3 100644 --- a/lnwire/commit_sig.go +++ b/lnwire/commit_sig.go @@ -1,6 +1,8 @@ package lnwire -import "io" +import ( + "io" +) // CommitSig is sent by either side to stage any pending HTLC's in the // receiver's pending set into a new commitment state. Implicitly, the new @@ -83,3 +85,11 @@ func (c *CommitSig) MaxPayloadLength(uint32) uint32 { // 32 + 64 + 2 + max_allowed_htlcs return MaxMessagePayload } + +// TargetChanID returns the channel id of the link for which this message is +// intended. +// +// NOTE: Part of lnd.LinkUpdater interface. +func (c *CommitSig) TargetChanID() ChannelID { + return c.ChanID +} diff --git a/lnwire/revoke_and_ack.go b/lnwire/revoke_and_ack.go index 623bcc36..f6395108 100644 --- a/lnwire/revoke_and_ack.go +++ b/lnwire/revoke_and_ack.go @@ -81,3 +81,11 @@ func (c *RevokeAndAck) MaxPayloadLength(uint32) uint32 { // 32 + 32 + 33 return 97 } + +// TargetChanID returns the channel id of the link for which this message is +// intended. +// +// NOTE: Part of lnd.LinkUpdater interface. +func (c *RevokeAndAck) TargetChanID() ChannelID { + return c.ChanID +} diff --git a/lnwire/update_add_htlc.go b/lnwire/update_add_htlc.go index d127db44..b3add950 100644 --- a/lnwire/update_add_htlc.go +++ b/lnwire/update_add_htlc.go @@ -1,6 +1,8 @@ package lnwire -import "io" +import ( + "io" +) // OnionPacketSize is the size of the serialized Sphinx onion packet included // in each UpdateAddHTLC message. The breakdown of the onion packet is as @@ -107,3 +109,11 @@ func (c *UpdateAddHTLC) MaxPayloadLength(uint32) uint32 { // 1450 return 32 + 8 + 4 + 8 + 32 + 1366 } + +// TargetChanID returns the channel id of the link for which this message is +// intended. +// +// NOTE: Part of lnd.LinkUpdater interface. +func (c *UpdateAddHTLC) TargetChanID() ChannelID { + return c.ChanID +} diff --git a/lnwire/update_fail_htlc.go b/lnwire/update_fail_htlc.go index ed59af17..17fc3cd4 100644 --- a/lnwire/update_fail_htlc.go +++ b/lnwire/update_fail_htlc.go @@ -1,6 +1,8 @@ package lnwire -import "io" +import ( + "io" +) // OpaqueReason is an opaque encrypted byte slice that encodes the exact // failure reason and additional some supplemental data. The contents of this @@ -83,3 +85,11 @@ func (c *UpdateFailHTLC) MaxPayloadLength(uint32) uint32 { return length } + +// TargetChanID returns the channel id of the link for which this message is +// intended. +// +// NOTE: Part of lnd.LinkUpdater interface. +func (c *UpdateFailHTLC) TargetChanID() ChannelID { + return c.ChanID +} diff --git a/lnwire/update_fail_malformed_htlc.go b/lnwire/update_fail_malformed_htlc.go index 6415b882..68f0a61b 100644 --- a/lnwire/update_fail_malformed_htlc.go +++ b/lnwire/update_fail_malformed_htlc.go @@ -73,3 +73,11 @@ func (c *UpdateFailMalformedHTLC) MaxPayloadLength(uint32) uint32 { // 32 + 8 + 32 + 2 return 74 } + +// TargetChanID returns the channel id of the link for which this message is +// intended. +// +// NOTE: Part of lnd.LinkUpdater interface. +func (c *UpdateFailMalformedHTLC) TargetChanID() ChannelID { + return c.ChanID +} diff --git a/lnwire/update_fee.go b/lnwire/update_fee.go index fde0cb8b..5657633b 100644 --- a/lnwire/update_fee.go +++ b/lnwire/update_fee.go @@ -68,3 +68,11 @@ func (c *UpdateFee) MaxPayloadLength(uint32) uint32 { // 32 + 4 return 36 } + +// TargetChanID returns the channel id of the link for which this message is +// intended. +// +// NOTE: Part of lnd.LinkUpdater interface. +func (c *UpdateFee) TargetChanID() ChannelID { + return c.ChanID +} diff --git a/lnwire/update_fulfill_htlc.go b/lnwire/update_fulfill_htlc.go index 050e81fd..49344008 100644 --- a/lnwire/update_fulfill_htlc.go +++ b/lnwire/update_fulfill_htlc.go @@ -1,6 +1,8 @@ package lnwire -import "io" +import ( + "io" +) // UpdateFulfillHTLC is sent by Alice to Bob when she wishes to settle a // particular HTLC referenced by its HTLCKey within a specific active channel @@ -76,3 +78,11 @@ func (c *UpdateFulfillHTLC) MaxPayloadLength(uint32) uint32 { // 32 + 8 + 32 return 72 } + +// TargetChanID returns the channel id of the link for which this message is +// intended. +// +// NOTE: Part of lnd.LinkUpdater interface. +func (c *UpdateFulfillHTLC) TargetChanID() ChannelID { + return c.ChanID +} diff --git a/peer.go b/peer.go index 247daab7..df33543c 100644 --- a/peer.go +++ b/peer.go @@ -1038,7 +1038,7 @@ out: } var ( - isChanUpdate bool + isLinkUpdate bool targetChan lnwire.ChannelID ) @@ -1105,7 +1105,7 @@ out: // If not we hand the error to the channel link for // this channel. default: - isChanUpdate = true + isLinkUpdate = true targetChan = msg.ChanID // Also marked this channel as failed, so we @@ -1114,32 +1114,14 @@ out: p.failedChannels[targetChan] = struct{}{} } - // TODO(roasbeef): create ChanUpdater interface for the below - case *lnwire.UpdateAddHTLC: - isChanUpdate = true - targetChan = msg.ChanID - case *lnwire.UpdateFulfillHTLC: - isChanUpdate = true - targetChan = msg.ChanID - case *lnwire.UpdateFailMalformedHTLC: - isChanUpdate = true - targetChan = msg.ChanID - case *lnwire.UpdateFailHTLC: - isChanUpdate = true - targetChan = msg.ChanID - case *lnwire.RevokeAndAck: - isChanUpdate = true - targetChan = msg.ChanID - case *lnwire.CommitSig: - isChanUpdate = true - targetChan = msg.ChanID - case *lnwire.UpdateFee: - isChanUpdate = true - targetChan = msg.ChanID case *lnwire.ChannelReestablish: - isChanUpdate = true + isLinkUpdate = true targetChan = msg.ChanID + case LinkUpdater: + isLinkUpdate = true + targetChan = msg.TargetChanID() + case *lnwire.ChannelUpdate, *lnwire.ChannelAnnouncement, *lnwire.NodeAnnouncement, @@ -1157,7 +1139,7 @@ out: "%v", uint16(msg.MsgType()), p) } - if isChanUpdate { + if isLinkUpdate { // If this is a channel update, then we need to feed it // into the channel's in-order message stream. chanStream, ok := chanMsgStreams[targetChan] @@ -2543,4 +2525,12 @@ func (p *peer) StartTime() time.Time { return p.startTime } +// LinkUpdater is an interface implemented by most messages in BOLT 2 that are +// allowed to update the channel state. +type LinkUpdater interface { + // TargetChanID returns the channel id of the link for which this + // message is intended. + TargetChanID() lnwire.ChannelID +} + // TODO(roasbeef): make all start/stop mutexes a CAS