htlcswitch: extract updateCommitTxOrFail in link

Deduplicate code and prepare for further split of
handleDownstreamPkt.
This commit is contained in:
Joost Jager 2020-05-13 15:22:56 +02:00
parent 7afb43a012
commit b559811bf5
No known key found for this signature in database
GPG Key ID: A61B9D4C393C59C7

@ -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.