From b559811bf5e93f3200b68475bb748792acb5b5f9 Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Wed, 13 May 2020 15:22:56 +0200 Subject: [PATCH] htlcswitch: extract updateCommitTxOrFail in link Deduplicate code and prepare for further split of handleDownstreamPkt. --- htlcswitch/link.go | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/htlcswitch/link.go b/htlcswitch/link.go index 019ba0071..db7f15ff4 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -1097,9 +1097,7 @@ func (l *channelLink) htlcManager() { // including all the currently pending entries. If the // send was unsuccessful, then abandon the update, // waiting for the revocation window to open up. - if err := l.updateCommitTx(); err != nil { - l.fail(LinkFailureError{code: ErrInternalError}, - "unable to update commitment: %v", err) + if !l.updateCommitTxOrFail() { return } @@ -1476,9 +1474,7 @@ func (l *channelLink) handleDownstreamPkt(pkt *htlcPacket) { if l.channel.PendingLocalUpdateCount() >= uint64(l.cfg.BatchSize) || isSettle { - if err := l.updateCommitTx(); err != nil { - l.fail(LinkFailureError{code: ErrInternalError}, - "unable to update commitment: %v", err) + if !l.updateCommitTxOrFail() { return } } @@ -1753,9 +1749,7 @@ func (l *channelLink) handleUpstreamMsg(msg lnwire.Message) { // Otherwise, the remote party initiated the state transition, // so we'll reply with a signature to provide them with their // version of the latest commitment. - if err := l.updateCommitTx(); err != nil { - l.fail(LinkFailureError{code: ErrInternalError}, - "unable to update commitment: %v", err) + if !l.updateCommitTxOrFail() { return } @@ -1832,9 +1826,7 @@ func (l *channelLink) handleUpstreamMsg(msg lnwire.Message) { // but there are still remote updates that are not in the remote // commit tx yet, send out an update. if l.channel.OweCommitment(true) { - if err := l.updateCommitTx(); err != nil { - l.fail(LinkFailureError{code: ErrInternalError}, - "unable to update commitment: %v", err) + if !l.updateCommitTxOrFail() { return } } @@ -1918,6 +1910,18 @@ func (l *channelLink) ackDownStreamPackets() error { return nil } +// updateCommitTxOrFail updates the commitment tx and if that fails, it fails +// the link. +func (l *channelLink) updateCommitTxOrFail() bool { + if err := l.updateCommitTx(); err != nil { + l.fail(LinkFailureError{code: ErrInternalError}, + "unable to update commitment: %v", err) + return false + } + + return true +} + // updateCommitTx signs, then sends an update to the remote peer adding a new // commitment to their commitment chain which includes all the latest updates // we've received+processed up to this point.