• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 13536249039

26 Feb 2025 03:42AM UTC coverage: 57.462% (-1.4%) from 58.835%
13536249039

Pull #8453

github

Roasbeef
peer: update chooseDeliveryScript to gen script if needed

In this commit, we update `chooseDeliveryScript` to generate a new
script if needed. This allows us to fold in a few other lines that
always followed this function into this expanded function.

The tests have been updated accordingly.
Pull Request #8453: [4/4] - multi: integrate new rbf coop close FSM into the existing peer flow

275 of 1318 new or added lines in 22 files covered. (20.86%)

19521 existing lines in 257 files now uncovered.

103858 of 180741 relevant lines covered (57.46%)

24750.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/peer/chan_observer.go
1
package peer
2

3
import (
4
        "github.com/btcsuite/btcd/wire"
5
        "github.com/lightningnetwork/lnd/channeldb"
6
        "github.com/lightningnetwork/lnd/fn/v2"
7
        "github.com/lightningnetwork/lnd/htlcswitch"
8
        "github.com/lightningnetwork/lnd/lntypes"
9
        "github.com/lightningnetwork/lnd/lnwallet/chancloser"
10
)
11

12
// channelView is a view into the current active/global channel state machine
13
// for a given link.
14
type channelView interface {
15
        // OweCommitment returns a boolean value reflecting whether we need to
16
        // send out a commitment signature because there are outstanding local
17
        // updates and/or updates in the local commit tx that aren't reflected
18
        // in the remote commit tx yet.
19
        OweCommitment() bool
20

21
        // IsChannelClean returns true if neither side has pending commitments,
22
        // neither side has HTLC's, and all updates are locked in irrevocably.
23
        IsChannelClean() bool
24

25
        // MarkCoopBroadcasted persistently marks that the channel close
26
        // transaction has been broadcast.
27
        MarkCoopBroadcasted(*wire.MsgTx, lntypes.ChannelParty) error
28

29
        // StateSnapshot returns a snapshot of the current fully committed
30
        // state within the channel.
31
        StateSnapshot() *channeldb.ChannelSnapshot
32

33
        // MarkShutdownSent persists the given ShutdownInfo. The existence of
34
        // the ShutdownInfo represents the fact that the Shutdown message has
35
        // been sent by us and so should be re-sent on re-establish.
36
        MarkShutdownSent(info *channeldb.ShutdownInfo) error
37
}
38

39
// linkController is capable of controlling the flow out incoming/outgoing
40
// HTLCs to/from the link.
41
type linkController interface {
42
        // DisableAdds sets the ChannelUpdateHandler state to allow/reject
43
        // UpdateAddHtlc's in the specified direction. It returns true if the
44
        // state was changed and false if the desired state was already set
45
        // before the method was called.
46
        DisableAdds(outgoing bool) bool
47

48
        // IsFlushing returns true when UpdateAddHtlc's are disabled in the
49
        // direction of the argument.
50
        IsFlushing(direction bool) bool
51
}
52

53
// linkNetworkController is an interface that represents an object capable of
54
// managing interactions with the active channel links from the PoV of the
55
// gossip network.
56
//
57
//nolint:iface
58
type linkNetworkController interface {
59
        // RequestDisable disables a channel by its channel point.
60
        RequestDisable(wire.OutPoint, bool) error
61
}
62

63
// chanObserver implements the chancloser.ChanObserver interface for the
64
// existing LightningChannel struct/instance.
65
type chanObserver struct {
66
        chanView    channelView
67
        link        linkController
68
        linkNetwork linkNetworkController
69
}
70

71
// newChanObserver creates a new instance of a chanObserver from an active
72
// channelView.
73
func newChanObserver(chanView channelView,
NEW
74
        link linkController, linkNetwork linkNetworkController) *chanObserver {
×
NEW
75

×
NEW
76
        return &chanObserver{
×
NEW
77
                chanView:    chanView,
×
NEW
78
                link:        link,
×
NEW
79
                linkNetwork: linkNetwork,
×
NEW
80
        }
×
NEW
81
}
×
82

83
// NoDanglingUpdates returns true if there are no dangling updates in the
84
// channel. In other words, there are no active update messages that haven't
85
// already been covered by a commit sig.
NEW
86
func (l *chanObserver) NoDanglingUpdates() bool {
×
NEW
87
        return !l.chanView.OweCommitment()
×
NEW
88
}
×
89

90
// DisableIncomingAdds instructs the channel link to disable process new
91
// incoming add messages.
NEW
92
func (l *chanObserver) DisableIncomingAdds() error {
×
NEW
93
        // If there's no link, then we don't need to disable any adds.
×
NEW
94
        if l.link == nil {
×
NEW
95
                return nil
×
NEW
96
        }
×
97

NEW
98
        _ = l.link.DisableAdds(htlcswitch.Incoming)
×
NEW
99

×
NEW
100
        return nil
×
101
}
102

103
// DisableOutgoingAdds instructs the channel link to disable process new
104
// outgoing add messages.
NEW
105
func (l *chanObserver) DisableOutgoingAdds() error {
×
NEW
106
        // If there's no link, then we don't need to disable any adds.
×
NEW
107
        if l.link == nil {
×
NEW
108
                return nil
×
NEW
109
        }
×
110

NEW
111
        _ = l.link.DisableAdds(htlcswitch.Outgoing)
×
NEW
112

×
NEW
113
        return nil
×
114
}
115

116
// MarkCoopBroadcasted persistently marks that the channel close transaction
117
// has been broadcast.
NEW
118
func (l *chanObserver) MarkCoopBroadcasted(tx *wire.MsgTx, local bool) error {
×
NEW
119
        return l.chanView.MarkCoopBroadcasted(tx, lntypes.Local)
×
NEW
120
}
×
121

122
// MarkShutdownSent persists the given ShutdownInfo. The existence of the
123
// ShutdownInfo represents the fact that the Shutdown message has been sent by
124
// us and so should be re-sent on re-establish.
125
func (l *chanObserver) MarkShutdownSent(deliveryAddr []byte,
NEW
126
        isInitiator bool) error {
×
NEW
127

×
NEW
128
        shutdownInfo := channeldb.NewShutdownInfo(deliveryAddr, isInitiator)
×
NEW
129
        return l.chanView.MarkShutdownSent(shutdownInfo)
×
NEW
130
}
×
131

132
// FinalBalances is the balances of the channel once it has been flushed. If
133
// Some, then this indicates that the channel is now in a state where it's
134
// always flushed, so we can accelerate the state transitions.
NEW
135
func (l *chanObserver) FinalBalances() fn.Option[chancloser.ShutdownBalances] {
×
NEW
136
        chanClean := l.chanView.IsChannelClean()
×
NEW
137

×
NEW
138
        switch {
×
139
        // If we have a link, then the balances are final if both the incoming
140
        // and outgoing adds are disabled _and_ the channel is clean.
141
        case l.link != nil && l.link.IsFlushing(htlcswitch.Incoming) &&
NEW
142
                l.link.IsFlushing(htlcswitch.Outgoing) && chanClean:
×
NEW
143

×
NEW
144
                fallthrough
×
145

146
        // If we don't have a link, then this is a restart case, so the
147
        // balances are final.
NEW
148
        case l.link == nil:
×
NEW
149
                snapshot := l.chanView.StateSnapshot()
×
NEW
150

×
NEW
151
                return fn.Some[chancloser.ShutdownBalances](
×
NEW
152
                        chancloser.ShutdownBalances{
×
NEW
153
                                LocalBalance:  snapshot.LocalBalance,
×
NEW
154
                                RemoteBalance: snapshot.RemoteBalance,
×
NEW
155
                        },
×
NEW
156
                )
×
157

158
        // Otherwise, the link is still active and not flushed, so the balances
159
        // aren't yet final.
NEW
160
        default:
×
NEW
161
                return fn.None[chancloser.ShutdownBalances]()
×
162
        }
163
}
164

165
// DisableChannel disables the target channel.
NEW
166
func (l *chanObserver) DisableChannel() error {
×
NEW
167
        op := l.chanView.StateSnapshot().ChannelPoint
×
NEW
168
        return l.linkNetwork.RequestDisable(op, false)
×
NEW
169
}
×
170

171
// A compile-time assertion to ensure that chanObserver meets the
172
// chancloser.ChanStateObserver interface.
173
var _ chancloser.ChanStateObserver = (*chanObserver)(nil)
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc