Skip to content

Commit 5d20d08

Browse files
committed
htlcswitch: add new LinkFailureAction enum
In this commit, we add a new LinkFailureAction enum to take over the old force close bool. Force closing isn't the only thing we might want to do when we decide to fail the link, so this is a prep refactoring for an upcoming change.
1 parent fcbe684 commit 5d20d08

File tree

4 files changed

+33
-21
lines changed

4 files changed

+33
-21
lines changed

htlcswitch/link.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,8 +1036,8 @@ func (l *channelLink) htlcManager() {
10361036
// storing the transaction in the db.
10371037
l.fail(
10381038
LinkFailureError{
1039-
code: ErrSyncError,
1040-
ForceClose: true,
1039+
code: ErrSyncError,
1040+
FailureAction: LinkFailureForceClose, // nolint:lll
10411041
},
10421042
"unable to synchronize channel "+
10431043
"states: %v", err,
@@ -1077,8 +1077,8 @@ func (l *channelLink) htlcManager() {
10771077

10781078
l.fail(
10791079
LinkFailureError{
1080-
code: ErrRecoveryError,
1081-
ForceClose: false,
1080+
code: ErrRecoveryError,
1081+
FailureAction: LinkFailureForceNone,
10821082
},
10831083
"unable to synchronize channel "+
10841084
"states: %v", err,
@@ -1782,8 +1782,8 @@ func (l *channelLink) handleUpstreamMsg(msg lnwire.Message) {
17821782
if err := l.channel.ReceiveHTLCSettle(pre, idx); err != nil {
17831783
l.fail(
17841784
LinkFailureError{
1785-
code: ErrInvalidUpdate,
1786-
ForceClose: true,
1785+
code: ErrInvalidUpdate,
1786+
FailureAction: LinkFailureForceClose,
17871787
},
17881788
"unable to handle upstream settle HTLC: %v", err,
17891789
)
@@ -1947,9 +1947,9 @@ func (l *channelLink) handleUpstreamMsg(msg lnwire.Message) {
19471947
}
19481948
l.fail(
19491949
LinkFailureError{
1950-
code: ErrInvalidCommitment,
1951-
ForceClose: true,
1952-
SendData: sendData,
1950+
code: ErrInvalidCommitment,
1951+
FailureAction: LinkFailureForceClose,
1952+
SendData: sendData,
19531953
},
19541954
"ChannelPoint(%v): unable to accept new "+
19551955
"commitment: %v",

htlcswitch/link_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5457,8 +5457,9 @@ func TestChannelLinkFail(t *testing.T) {
54575457
// If we expect the link to force close the channel in this
54585458
// case, check that it happens. If not, make sure it does not
54595459
// happen.
5460-
require.Equal(
5461-
t, test.shouldForceClose, linkErr.ForceClose, test.name,
5460+
isForceCloseErr := (linkErr.FailureAction == LinkFailureForceClose)
5461+
require.True(
5462+
t, test.shouldForceClose == isForceCloseErr, test.name,
54625463
)
54635464
require.Equal(
54645465
t, test.permanentFailure, linkErr.PermanentFailure,
@@ -6523,7 +6524,7 @@ func TestPipelineSettle(t *testing.T) {
65236524
// ForceClose should be false.
65246525
select {
65256526
case linkErr := <-linkErrors:
6526-
require.False(t, linkErr.ForceClose)
6527+
require.False(t, linkErr.FailureAction == LinkFailureForceClose)
65276528
case <-forwardChan:
65286529
t.Fatal("packet was erroneously forwarded")
65296530
}
@@ -6559,7 +6560,7 @@ func TestPipelineSettle(t *testing.T) {
65596560
// ForceClose should be false.
65606561
select {
65616562
case linkErr := <-linkErrors:
6562-
require.False(t, linkErr.ForceClose)
6563+
require.False(t, linkErr.FailureAction == LinkFailureForceClose)
65636564
case <-forwardChan:
65646565
t.Fatal("packet was erroneously forwarded")
65656566
}

htlcswitch/linkfailure.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ const (
5353
ErrCircuitError
5454
)
5555

56+
// LinkFailureAction is an enum-like type that describes the action that should
57+
// be taken in response to a link failure.
58+
type LinkFailureAction uint8
59+
60+
const (
61+
// LinkFailureForceNone indicates no action is to be taken.
62+
LinkFailureForceNone LinkFailureAction = iota
63+
64+
// LinkFailureForceClose indicates that the channel should be force
65+
// closed.
66+
LinkFailureForceClose
67+
)
68+
5669
// LinkFailureError encapsulates an error that will make us fail the current
5770
// link. It contains the necessary information needed to determine if we should
5871
// force close the channel in the process, and if any error data should be sent
@@ -61,9 +74,8 @@ type LinkFailureError struct {
6174
// code is the type of error this LinkFailureError encapsulates.
6275
code errorCode
6376

64-
// ForceClose indicates whether we should force close the channel
65-
// because of this error.
66-
ForceClose bool
77+
// FailureAction describes what we should do to fail the channel.
78+
FailureAction LinkFailureAction
6779

6880
// PermanentFailure indicates whether this failure is permanent, and
6981
// the channel should not be attempted loaded again.

peer/brontide.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3094,11 +3094,10 @@ func (p *Brontide) handleLinkFailure(failure linkFailureReport) {
30943094
// being applied.
30953095
p.WipeChannel(&failure.chanPoint)
30963096

3097-
// If the error encountered was severe enough, we'll now force close the
3098-
// channel to prevent reading it to the switch in the future.
3099-
if failure.linkErr.ForceClose {
3100-
p.log.Warnf("Force closing link(%v)",
3101-
failure.shortChanID)
3097+
// If the error encountered was severe enough, we'll now force close
3098+
// the channel to prevent reading it to the switch in the future.
3099+
if failure.linkErr.FailureAction == htlcswitch.LinkFailureForceClose {
3100+
p.log.Warnf("Force closing link(%v)", failure.shortChanID)
31023101

31033102
closeTx, err := p.cfg.ChainArb.ForceCloseContract(
31043103
failure.chanPoint,

0 commit comments

Comments
 (0)