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.
This commit is contained in:
Elle Mouton committed 2024-02-21 11:35:11 +02:00
1 parent 71753af8ee
commit 972f57e9a7
6 files changed
+45 -80

No files matched your search

+8 -6
View File
@@ -135,14 +135,16 @@ type ChannelUpdateHandler interface {
MayAddOutgoingHtlc(lnwire.MilliSatoshi) error MayAddOutgoingHtlc(lnwire.MilliSatoshi) error
// EnableAdds sets the ChannelUpdateHandler state to allow // EnableAdds sets the ChannelUpdateHandler state to allow
// UpdateAddHtlc's in the specified direction. It returns an error if // UpdateAddHtlc's in the specified direction. It returns true if the
// the state already allowed those adds. // state was changed and false if the desired state was already set
EnableAdds(direction LinkDirection) error // before the method was called.
EnableAdds(direction LinkDirection) bool
// DisableAdds sets the ChannelUpdateHandler state to allow // DisableAdds sets the ChannelUpdateHandler state to allow
// UpdateAddHtlc's in the specified direction. It returns an error if // UpdateAddHtlc's in the specified direction. It returns true if the
// the state already disallowed those adds. // state was changed and false if the desired state was already set
DisableAdds(direction LinkDirection) error // before the method was called.
DisableAdds(direction LinkDirection) bool
// IsFlushing returns true when UpdateAddHtlc's are disabled in the // IsFlushing returns true when UpdateAddHtlc's are disabled in the
// direction of the argument. // direction of the argument.
+10 -26
View File
@@ -618,41 +618,25 @@ func (l *channelLink) EligibleToUpdate() bool {
} }
// EnableAdds sets the ChannelUpdateHandler state to allow UpdateAddHtlc's in // EnableAdds sets the ChannelUpdateHandler state to allow UpdateAddHtlc's in
// the specified direction. It returns an error if the state already allowed // the specified direction. It returns true if the state was changed and false
// those adds. // if the desired state was already set before the method was called.
func (l *channelLink) EnableAdds(linkDirection LinkDirection) error { func (l *channelLink) EnableAdds(linkDirection LinkDirection) bool {
if linkDirection == Outgoing { if linkDirection == Outgoing {
if !l.isOutgoingAddBlocked.Swap(false) { return l.isOutgoingAddBlocked.Swap(false)
return errors.New("outgoing adds already enabled")
}
} }
if linkDirection == Incoming { return l.isIncomingAddBlocked.Swap(false)
if !l.isIncomingAddBlocked.Swap(false) {
return errors.New("incoming adds already enabled")
}
}
return nil
} }
// DisableAdds sets the ChannelUpdateHandler state to allow UpdateAddHtlc's in // DisableAdds sets the ChannelUpdateHandler state to allow UpdateAddHtlc's in
// the specified direction. It returns an error if the state already disallowed // the specified direction. It returns true if the state was changed and false
// those adds. // if the desired state was already set before the method was called.
func (l *channelLink) DisableAdds(linkDirection LinkDirection) error { func (l *channelLink) DisableAdds(linkDirection LinkDirection) bool {
if linkDirection == Outgoing { if linkDirection == Outgoing {
if l.isOutgoingAddBlocked.Swap(true) { return !l.isOutgoingAddBlocked.Swap(true)
return errors.New("outgoing adds already disabled")
}
} }
if linkDirection == Incoming { return !l.isIncomingAddBlocked.Swap(true)
if l.isIncomingAddBlocked.Swap(true) {
return errors.New("incoming adds already disabled")
}
}
return nil
} }
// IsFlushing returns true when UpdateAddHtlc's are disabled in the direction of // IsFlushing returns true when UpdateAddHtlc's are disabled in the direction of
+4 -9
View File
@@ -6969,27 +6969,22 @@ func TestLinkFlushApiDirectionIsolation(t *testing.T) {
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
if prand.Uint64()%2 == 0 { if prand.Uint64()%2 == 0 {
//nolint:errcheck
aliceLink.EnableAdds(Outgoing) aliceLink.EnableAdds(Outgoing)
require.False(t, aliceLink.IsFlushing(Outgoing)) require.False(t, aliceLink.IsFlushing(Outgoing))
} else { } else {
//nolint:errcheck
aliceLink.DisableAdds(Outgoing) aliceLink.DisableAdds(Outgoing)
require.True(t, aliceLink.IsFlushing(Outgoing)) require.True(t, aliceLink.IsFlushing(Outgoing))
} }
require.False(t, aliceLink.IsFlushing(Incoming)) require.False(t, aliceLink.IsFlushing(Incoming))
} }
//nolint:errcheck
aliceLink.EnableAdds(Outgoing) aliceLink.EnableAdds(Outgoing)
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
if prand.Uint64()%2 == 0 { if prand.Uint64()%2 == 0 {
//nolint:errcheck
aliceLink.EnableAdds(Incoming) aliceLink.EnableAdds(Incoming)
require.False(t, aliceLink.IsFlushing(Incoming)) require.False(t, aliceLink.IsFlushing(Incoming))
} else { } else {
//nolint:errcheck
aliceLink.DisableAdds(Incoming) aliceLink.DisableAdds(Incoming)
require.True(t, aliceLink.IsFlushing(Incoming)) require.True(t, aliceLink.IsFlushing(Incoming))
} }
@@ -7010,16 +7005,16 @@ func TestLinkFlushApiGateStateIdempotence(t *testing.T) {
) )
for _, dir := range []LinkDirection{Incoming, Outgoing} { for _, dir := range []LinkDirection{Incoming, Outgoing} {
require.Nil(t, aliceLink.DisableAdds(dir)) require.True(t, aliceLink.DisableAdds(dir))
require.True(t, aliceLink.IsFlushing(dir)) require.True(t, aliceLink.IsFlushing(dir))
require.NotNil(t, aliceLink.DisableAdds(dir)) require.False(t, aliceLink.DisableAdds(dir))
require.True(t, aliceLink.IsFlushing(dir)) require.True(t, aliceLink.IsFlushing(dir))
require.Nil(t, aliceLink.EnableAdds(dir)) require.True(t, aliceLink.EnableAdds(dir))
require.False(t, aliceLink.IsFlushing(dir)) require.False(t, aliceLink.IsFlushing(dir))
require.NotNil(t, aliceLink.EnableAdds(dir)) require.False(t, aliceLink.EnableAdds(dir))
require.False(t, aliceLink.IsFlushing(dir)) require.False(t, aliceLink.IsFlushing(dir))
} }
} }
+5 -4
View File
@@ -906,13 +906,14 @@ func (f *mockChannelLink) UpdateShortChanID() (lnwire.ShortChannelID, error) {
return f.shortChanID, nil return f.shortChanID, nil
} }
func (f *mockChannelLink) EnableAdds(linkDirection LinkDirection) error { func (f *mockChannelLink) EnableAdds(linkDirection LinkDirection) bool {
// TODO(proofofkeags): Implement // TODO(proofofkeags): Implement
return nil return true
} }
func (f *mockChannelLink) DisableAdds(linkDirection LinkDirection) error {
func (f *mockChannelLink) DisableAdds(linkDirection LinkDirection) bool {
// TODO(proofofkeags): Implement // TODO(proofofkeags): Implement
return nil return true
} }
func (f *mockChannelLink) IsFlushing(linkDirection LinkDirection) bool { func (f *mockChannelLink) IsFlushing(linkDirection LinkDirection) bool {
// TODO(proofofkeags): Implement // TODO(proofofkeags): Implement
+10 -12
View File
@@ -2991,9 +2991,8 @@ func (p *Brontide) handleLocalCloseReq(req *htlcswitch.ChanClose) {
} }
link.OnCommitOnce(htlcswitch.Outgoing, func() { link.OnCommitOnce(htlcswitch.Outgoing, func() {
err := link.DisableAdds(htlcswitch.Outgoing) if !link.DisableAdds(htlcswitch.Outgoing) {
if err != nil { p.log.Warnf("Outgoing link adds already "+
p.log.Warnf("outgoing link adds already "+
"disabled: %v", link.ChanID()) "disabled: %v", link.ChanID())
} }
@@ -3619,12 +3618,9 @@ func (p *Brontide) handleCloseMsg(msg *closeMsg) {
switch typed := msg.msg.(type) { switch typed := msg.msg.(type) {
case *lnwire.Shutdown: case *lnwire.Shutdown:
// Disable incoming adds immediately. // Disable incoming adds immediately.
if link != nil { if link != nil && !link.DisableAdds(htlcswitch.Incoming) {
err := link.DisableAdds(htlcswitch.Incoming) p.log.Warnf("Incoming link adds already disabled: %v",
if err != nil { link.ChanID())
p.log.Warnf("incoming link adds already "+
"disabled: %v", link.ChanID())
}
} }
oShutdown, err := chanCloser.ReceiveShutdown(*typed) oShutdown, err := chanCloser.ReceiveShutdown(*typed)
@@ -3646,10 +3642,12 @@ func (p *Brontide) handleCloseMsg(msg *closeMsg) {
// next time we send a CommitSig to remain spec // next time we send a CommitSig to remain spec
// compliant. // compliant.
link.OnCommitOnce(htlcswitch.Outgoing, func() { link.OnCommitOnce(htlcswitch.Outgoing, func() {
err := link.DisableAdds(htlcswitch.Outgoing) if !link.DisableAdds(htlcswitch.Outgoing) {
if err != nil { p.log.Warnf("Outgoing link adds "+
p.log.Warn(err.Error()) "already disabled: %v",
link.ChanID())
} }
p.queueMsg(&msg, nil) p.queueMsg(&msg, nil)
}) })
}) })
+8 -23
View File
@@ -4,7 +4,6 @@ import (
"bytes" "bytes"
crand "crypto/rand" crand "crypto/rand"
"encoding/binary" "encoding/binary"
"fmt"
"io" "io"
"math/rand" "math/rand"
"net" "net"
@@ -510,34 +509,20 @@ type mockMessageConn struct {
readRaceDetectingCounter int readRaceDetectingCounter int
} }
func (m *mockUpdateHandler) EnableAdds(dir htlcswitch.LinkDirection) error { func (m *mockUpdateHandler) EnableAdds(dir htlcswitch.LinkDirection) bool {
switch dir { if dir == htlcswitch.Outgoing {
case htlcswitch.Outgoing: return m.isOutgoingAddBlocked.Swap(false)
if !m.isOutgoingAddBlocked.Swap(false) {
return fmt.Errorf("%v adds already enabled", dir)
}
case htlcswitch.Incoming:
if !m.isIncomingAddBlocked.Swap(false) {
return fmt.Errorf("%v adds already enabled", dir)
}
} }
return nil return m.isIncomingAddBlocked.Swap(false)
} }
func (m *mockUpdateHandler) DisableAdds(dir htlcswitch.LinkDirection) error { func (m *mockUpdateHandler) DisableAdds(dir htlcswitch.LinkDirection) bool {
switch dir { if dir == htlcswitch.Outgoing {
case htlcswitch.Outgoing: return !m.isOutgoingAddBlocked.Swap(true)
if m.isOutgoingAddBlocked.Swap(true) {
return fmt.Errorf("%v adds already disabled", dir)
}
case htlcswitch.Incoming:
if m.isIncomingAddBlocked.Swap(true) {
return fmt.Errorf("%v adds already disabled", dir)
}
} }
return nil return !m.isIncomingAddBlocked.Swap(true)
} }
func (m *mockUpdateHandler) IsFlushing(dir htlcswitch.LinkDirection) bool { func (m *mockUpdateHandler) IsFlushing(dir htlcswitch.LinkDirection) bool {