Skip to content

Commit 972f57e

Browse files
committed
peer+htlcswitch: update Enable/DisableAdds API
In this commit, the `ChannelUpdateHandler`'s `EnableAdds` and `DisableAdds` methods are adjusted to return booleans instead of errors. This is done becuase currently, any error returned by these methods is treated by just logging the error since today all it means is that the proposed update has already been done. And so all we do today is log the error. But in future, if these methods are updated to return actual errors that need to be handled, then we might forget to handle them correctly at the various call sights. So we instead change the signature of the function to just return a boolean. In future, if we do need to return any error, we will have to go inspect every call sight in any case to fix compliation & then we can be sure we are handling the errors correctly.
1 parent 71753af commit 972f57e

File tree

6 files changed

+45
-80
lines changed

6 files changed

+45
-80
lines changed

htlcswitch/interfaces.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,16 @@ type ChannelUpdateHandler interface {
135135
MayAddOutgoingHtlc(lnwire.MilliSatoshi) error
136136

137137
// EnableAdds sets the ChannelUpdateHandler state to allow
138-
// UpdateAddHtlc's in the specified direction. It returns an error if
139-
// the state already allowed those adds.
140-
EnableAdds(direction LinkDirection) error
138+
// UpdateAddHtlc's in the specified direction. It returns true if the
139+
// state was changed and false if the desired state was already set
140+
// before the method was called.
141+
EnableAdds(direction LinkDirection) bool
141142

142143
// DisableAdds sets the ChannelUpdateHandler state to allow
143-
// UpdateAddHtlc's in the specified direction. It returns an error if
144-
// the state already disallowed those adds.
145-
DisableAdds(direction LinkDirection) error
144+
// UpdateAddHtlc's in the specified direction. It returns true if the
145+
// state was changed and false if the desired state was already set
146+
// before the method was called.
147+
DisableAdds(direction LinkDirection) bool
146148

147149
// IsFlushing returns true when UpdateAddHtlc's are disabled in the
148150
// direction of the argument.

htlcswitch/link.go

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -618,41 +618,25 @@ func (l *channelLink) EligibleToUpdate() bool {
618618
}
619619

620620
// EnableAdds sets the ChannelUpdateHandler state to allow UpdateAddHtlc's in
621-
// the specified direction. It returns an error if the state already allowed
622-
// those adds.
623-
func (l *channelLink) EnableAdds(linkDirection LinkDirection) error {
621+
// the specified direction. It returns true if the state was changed and false
622+
// if the desired state was already set before the method was called.
623+
func (l *channelLink) EnableAdds(linkDirection LinkDirection) bool {
624624
if linkDirection == Outgoing {
625-
if !l.isOutgoingAddBlocked.Swap(false) {
626-
return errors.New("outgoing adds already enabled")
627-
}
625+
return l.isOutgoingAddBlocked.Swap(false)
628626
}
629627

630-
if linkDirection == Incoming {
631-
if !l.isIncomingAddBlocked.Swap(false) {
632-
return errors.New("incoming adds already enabled")
633-
}
634-
}
635-
636-
return nil
628+
return l.isIncomingAddBlocked.Swap(false)
637629
}
638630

639631
// DisableAdds sets the ChannelUpdateHandler state to allow UpdateAddHtlc's in
640-
// the specified direction. It returns an error if the state already disallowed
641-
// those adds.
642-
func (l *channelLink) DisableAdds(linkDirection LinkDirection) error {
632+
// the specified direction. It returns true if the state was changed and false
633+
// if the desired state was already set before the method was called.
634+
func (l *channelLink) DisableAdds(linkDirection LinkDirection) bool {
643635
if linkDirection == Outgoing {
644-
if l.isOutgoingAddBlocked.Swap(true) {
645-
return errors.New("outgoing adds already disabled")
646-
}
636+
return !l.isOutgoingAddBlocked.Swap(true)
647637
}
648638

649-
if linkDirection == Incoming {
650-
if l.isIncomingAddBlocked.Swap(true) {
651-
return errors.New("incoming adds already disabled")
652-
}
653-
}
654-
655-
return nil
639+
return !l.isIncomingAddBlocked.Swap(true)
656640
}
657641

658642
// IsFlushing returns true when UpdateAddHtlc's are disabled in the direction of

htlcswitch/link_test.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6969,27 +6969,22 @@ func TestLinkFlushApiDirectionIsolation(t *testing.T) {
69696969

69706970
for i := 0; i < 10; i++ {
69716971
if prand.Uint64()%2 == 0 {
6972-
//nolint:errcheck
69736972
aliceLink.EnableAdds(Outgoing)
69746973
require.False(t, aliceLink.IsFlushing(Outgoing))
69756974
} else {
6976-
//nolint:errcheck
69776975
aliceLink.DisableAdds(Outgoing)
69786976
require.True(t, aliceLink.IsFlushing(Outgoing))
69796977
}
69806978
require.False(t, aliceLink.IsFlushing(Incoming))
69816979
}
69826980

6983-
//nolint:errcheck
69846981
aliceLink.EnableAdds(Outgoing)
69856982

69866983
for i := 0; i < 10; i++ {
69876984
if prand.Uint64()%2 == 0 {
6988-
//nolint:errcheck
69896985
aliceLink.EnableAdds(Incoming)
69906986
require.False(t, aliceLink.IsFlushing(Incoming))
69916987
} else {
6992-
//nolint:errcheck
69936988
aliceLink.DisableAdds(Incoming)
69946989
require.True(t, aliceLink.IsFlushing(Incoming))
69956990
}
@@ -7010,16 +7005,16 @@ func TestLinkFlushApiGateStateIdempotence(t *testing.T) {
70107005
)
70117006

70127007
for _, dir := range []LinkDirection{Incoming, Outgoing} {
7013-
require.Nil(t, aliceLink.DisableAdds(dir))
7008+
require.True(t, aliceLink.DisableAdds(dir))
70147009
require.True(t, aliceLink.IsFlushing(dir))
70157010

7016-
require.NotNil(t, aliceLink.DisableAdds(dir))
7011+
require.False(t, aliceLink.DisableAdds(dir))
70177012
require.True(t, aliceLink.IsFlushing(dir))
70187013

7019-
require.Nil(t, aliceLink.EnableAdds(dir))
7014+
require.True(t, aliceLink.EnableAdds(dir))
70207015
require.False(t, aliceLink.IsFlushing(dir))
70217016

7022-
require.NotNil(t, aliceLink.EnableAdds(dir))
7017+
require.False(t, aliceLink.EnableAdds(dir))
70237018
require.False(t, aliceLink.IsFlushing(dir))
70247019
}
70257020
}

htlcswitch/mock.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -906,13 +906,14 @@ func (f *mockChannelLink) UpdateShortChanID() (lnwire.ShortChannelID, error) {
906906
return f.shortChanID, nil
907907
}
908908

909-
func (f *mockChannelLink) EnableAdds(linkDirection LinkDirection) error {
909+
func (f *mockChannelLink) EnableAdds(linkDirection LinkDirection) bool {
910910
// TODO(proofofkeags): Implement
911-
return nil
911+
return true
912912
}
913-
func (f *mockChannelLink) DisableAdds(linkDirection LinkDirection) error {
913+
914+
func (f *mockChannelLink) DisableAdds(linkDirection LinkDirection) bool {
914915
// TODO(proofofkeags): Implement
915-
return nil
916+
return true
916917
}
917918
func (f *mockChannelLink) IsFlushing(linkDirection LinkDirection) bool {
918919
// TODO(proofofkeags): Implement

peer/brontide.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2991,9 +2991,8 @@ func (p *Brontide) handleLocalCloseReq(req *htlcswitch.ChanClose) {
29912991
}
29922992

29932993
link.OnCommitOnce(htlcswitch.Outgoing, func() {
2994-
err := link.DisableAdds(htlcswitch.Outgoing)
2995-
if err != nil {
2996-
p.log.Warnf("outgoing link adds already "+
2994+
if !link.DisableAdds(htlcswitch.Outgoing) {
2995+
p.log.Warnf("Outgoing link adds already "+
29972996
"disabled: %v", link.ChanID())
29982997
}
29992998

@@ -3619,12 +3618,9 @@ func (p *Brontide) handleCloseMsg(msg *closeMsg) {
36193618
switch typed := msg.msg.(type) {
36203619
case *lnwire.Shutdown:
36213620
// Disable incoming adds immediately.
3622-
if link != nil {
3623-
err := link.DisableAdds(htlcswitch.Incoming)
3624-
if err != nil {
3625-
p.log.Warnf("incoming link adds already "+
3626-
"disabled: %v", link.ChanID())
3627-
}
3621+
if link != nil && !link.DisableAdds(htlcswitch.Incoming) {
3622+
p.log.Warnf("Incoming link adds already disabled: %v",
3623+
link.ChanID())
36283624
}
36293625

36303626
oShutdown, err := chanCloser.ReceiveShutdown(*typed)
@@ -3646,10 +3642,12 @@ func (p *Brontide) handleCloseMsg(msg *closeMsg) {
36463642
// next time we send a CommitSig to remain spec
36473643
// compliant.
36483644
link.OnCommitOnce(htlcswitch.Outgoing, func() {
3649-
err := link.DisableAdds(htlcswitch.Outgoing)
3650-
if err != nil {
3651-
p.log.Warn(err.Error())
3645+
if !link.DisableAdds(htlcswitch.Outgoing) {
3646+
p.log.Warnf("Outgoing link adds "+
3647+
"already disabled: %v",
3648+
link.ChanID())
36523649
}
3650+
36533651
p.queueMsg(&msg, nil)
36543652
})
36553653
})

peer/test_utils.go

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
crand "crypto/rand"
66
"encoding/binary"
7-
"fmt"
87
"io"
98
"math/rand"
109
"net"
@@ -510,34 +509,20 @@ type mockMessageConn struct {
510509
readRaceDetectingCounter int
511510
}
512511

513-
func (m *mockUpdateHandler) EnableAdds(dir htlcswitch.LinkDirection) error {
514-
switch dir {
515-
case htlcswitch.Outgoing:
516-
if !m.isOutgoingAddBlocked.Swap(false) {
517-
return fmt.Errorf("%v adds already enabled", dir)
518-
}
519-
case htlcswitch.Incoming:
520-
if !m.isIncomingAddBlocked.Swap(false) {
521-
return fmt.Errorf("%v adds already enabled", dir)
522-
}
512+
func (m *mockUpdateHandler) EnableAdds(dir htlcswitch.LinkDirection) bool {
513+
if dir == htlcswitch.Outgoing {
514+
return m.isOutgoingAddBlocked.Swap(false)
523515
}
524516

525-
return nil
517+
return m.isIncomingAddBlocked.Swap(false)
526518
}
527519

528-
func (m *mockUpdateHandler) DisableAdds(dir htlcswitch.LinkDirection) error {
529-
switch dir {
530-
case htlcswitch.Outgoing:
531-
if m.isOutgoingAddBlocked.Swap(true) {
532-
return fmt.Errorf("%v adds already disabled", dir)
533-
}
534-
case htlcswitch.Incoming:
535-
if m.isIncomingAddBlocked.Swap(true) {
536-
return fmt.Errorf("%v adds already disabled", dir)
537-
}
520+
func (m *mockUpdateHandler) DisableAdds(dir htlcswitch.LinkDirection) bool {
521+
if dir == htlcswitch.Outgoing {
522+
return !m.isOutgoingAddBlocked.Swap(true)
538523
}
539524

540-
return nil
525+
return !m.isIncomingAddBlocked.Swap(true)
541526
}
542527

543528
func (m *mockUpdateHandler) IsFlushing(dir htlcswitch.LinkDirection) bool {

0 commit comments

Comments
 (0)