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

lightningnetwork / lnd / 13577875015

27 Feb 2025 11:26PM UTC coverage: 57.682%. First build
13577875015

Pull #9559

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 #9559: multi: preliminary changes towards integrating the new RBF close protocol feature

58 of 253 new or added lines in 15 files covered. (22.92%)

103798 of 179949 relevant lines covered (57.68%)

24692.64 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
type linkNetworkController interface {
57
        // RequestDisable disables a channel by its channel point.
58
        RequestDisable(wire.OutPoint, bool) error
59
}
60

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

69
// newChanObserver creates a new instance of a chanObserver from an active
70
// channelView.
71
//
72
//nolint:unused
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
        disabled := l.link.DisableAdds(htlcswitch.Incoming)
×
NEW
99
        if disabled {
×
NEW
100
                chanPoint := l.chanView.StateSnapshot().ChannelPoint
×
NEW
101
                peerLog.Debugf("ChannelPoint(%v): link already disabled",
×
NEW
102
                        chanPoint)
×
NEW
103
        }
×
104

NEW
105
        return nil
×
106
}
107

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

NEW
116
        _ = l.link.DisableAdds(htlcswitch.Outgoing)
×
NEW
117

×
NEW
118
        return nil
×
119
}
120

121
// MarkCoopBroadcasted persistently marks that the channel close transaction
122
// has been broadcast.
NEW
123
func (l *chanObserver) MarkCoopBroadcasted(tx *wire.MsgTx, local bool) error {
×
NEW
124
        return l.chanView.MarkCoopBroadcasted(tx, lntypes.Local)
×
NEW
125
}
×
126

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

×
NEW
133
        shutdownInfo := channeldb.NewShutdownInfo(deliveryAddr, isInitiator)
×
NEW
134
        return l.chanView.MarkShutdownSent(shutdownInfo)
×
NEW
135
}
×
136

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

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

×
NEW
149
                fallthrough
×
150

151
        // If we don't have a link, then this is a restart case, so the
152
        // balances are final.
NEW
153
        case l.link == nil:
×
NEW
154
                snapshot := l.chanView.StateSnapshot()
×
NEW
155

×
NEW
156
                return fn.Some(chancloser.ShutdownBalances{
×
NEW
157
                        LocalBalance:  snapshot.LocalBalance,
×
NEW
158
                        RemoteBalance: snapshot.RemoteBalance,
×
NEW
159
                })
×
160

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

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

174
// A compile-time assertion to ensure that chanObserver meets the
175
// chancloser.ChanStateObserver interface.
176
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