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

lightningnetwork / lnd / 13980275562

20 Mar 2025 10:06PM UTC coverage: 58.6% (-10.2%) from 68.789%
13980275562

Pull #9623

github

web-flow
Merge b9b960345 into 09b674508
Pull Request #9623: Size msg test msg

0 of 1518 new or added lines in 42 files covered. (0.0%)

26603 existing lines in 443 files now uncovered.

96807 of 165200 relevant lines covered (58.6%)

1.82 hits per line

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

45.26
/lnwire/shutdown.go
1
package lnwire
2

3
import (
4
        "bytes"
5
        "io"
6

7
        "github.com/lightningnetwork/lnd/tlv"
8
        "pgregory.net/rapid"
9
)
10

11
type (
12
        // ShutdownNonceType is the type of the shutdown nonce TLV record.
13
        ShutdownNonceType = tlv.TlvType8
14

15
        // ShutdownNonceTLV is the TLV record that contains the shutdown nonce.
16
        ShutdownNonceTLV = tlv.OptionalRecordT[ShutdownNonceType, Musig2Nonce]
17
)
18

19
// SomeShutdownNonce returns a ShutdownNonceTLV with the given nonce.
20
func SomeShutdownNonce(nonce Musig2Nonce) ShutdownNonceTLV {
3✔
21
        return tlv.SomeRecordT(
3✔
22
                tlv.NewRecordT[ShutdownNonceType, Musig2Nonce](nonce),
3✔
23
        )
3✔
24
}
3✔
25

26
// Shutdown is sent by either side in order to initiate the cooperative closure
27
// of a channel. This message is sparse as both sides implicitly have the
28
// information necessary to construct a transaction that will send the settled
29
// funds of both parties to the final delivery addresses negotiated during the
30
// funding workflow.
31
type Shutdown struct {
32
        // ChannelID serves to identify which channel is to be closed.
33
        ChannelID ChannelID
34

35
        // Address is the script to which the channel funds will be paid.
36
        Address DeliveryAddress
37

38
        // ShutdownNonce is the nonce the sender will use to sign the first
39
        // co-op sign offer.
40
        ShutdownNonce ShutdownNonceTLV
41

42
        // CustomRecords maps TLV types to byte slices, storing arbitrary data
43
        // intended for inclusion in the ExtraData field of the Shutdown
44
        // message.
45
        CustomRecords CustomRecords
46

47
        // ExtraData is the set of data that was appended to this message to
48
        // fill out the full maximum transport message size. These fields can
49
        // be used to specify optional data such as custom TLV fields.
50
        ExtraData ExtraOpaqueData
51
}
52

53
// NewShutdown creates a new Shutdown message.
54
func NewShutdown(cid ChannelID, addr DeliveryAddress) *Shutdown {
3✔
55
        return &Shutdown{
3✔
56
                ChannelID: cid,
3✔
57
                Address:   addr,
3✔
58
        }
3✔
59
}
3✔
60

61
// A compile-time check to ensure Shutdown implements the lnwire.Message
62
// interface.
63
var _ Message = (*Shutdown)(nil)
64

65
// A compile-time check to ensure Shutdown implements the lnwire.SizeableMessage interface.
66
var _ SizeableMessage = (*Shutdown)(nil)
67

68
// A compile-time check to ensure Shutdown implements the lnwire.TestMessage interface.
69
var _ TestMessage = (*Shutdown)(nil)
70

71
// Decode deserializes a serialized Shutdown from the passed io.Reader,
72
// observing the specified protocol version.
73
//
74
// This is part of the lnwire.Message interface.
75
func (s *Shutdown) Decode(r io.Reader, pver uint32) error {
3✔
76
        err := ReadElements(r, &s.ChannelID, &s.Address)
3✔
77
        if err != nil {
3✔
UNCOV
78
                return err
×
UNCOV
79
        }
×
80

81
        var tlvRecords ExtraOpaqueData
3✔
82
        if err := ReadElements(r, &tlvRecords); err != nil {
3✔
UNCOV
83
                return err
×
UNCOV
84
        }
×
85

86
        // Extract TLV records from the extra data field.
87
        musigNonce := s.ShutdownNonce.Zero()
3✔
88

3✔
89
        customRecords, parsed, extraData, err := ParseAndExtractCustomRecords(
3✔
90
                tlvRecords, &musigNonce,
3✔
91
        )
3✔
92
        if err != nil {
3✔
UNCOV
93
                return err
×
UNCOV
94
        }
×
95

96
        // Assign the parsed records back to the message.
97
        if _, ok := parsed[musigNonce.TlvType()]; ok {
6✔
98
                s.ShutdownNonce = tlv.SomeRecordT(musigNonce)
3✔
99
        }
3✔
100

101
        s.CustomRecords = customRecords
3✔
102
        s.ExtraData = extraData
3✔
103

3✔
104
        return nil
3✔
105
}
106

107
// Encode serializes the target Shutdown into the passed io.Writer observing
108
// the protocol version specified.
109
//
110
// This is part of the lnwire.Message interface.
111
func (s *Shutdown) Encode(w *bytes.Buffer, pver uint32) error {
3✔
112
        if err := WriteChannelID(w, s.ChannelID); err != nil {
3✔
UNCOV
113
                return err
×
UNCOV
114
        }
×
115

116
        if err := WriteDeliveryAddress(w, s.Address); err != nil {
3✔
UNCOV
117
                return err
×
UNCOV
118
        }
×
119

120
        // Only include nonce in extra data if present.
121
        var records []tlv.RecordProducer
3✔
122
        s.ShutdownNonce.WhenSome(
3✔
123
                func(nonce tlv.RecordT[ShutdownNonceType, Musig2Nonce]) {
6✔
124
                        records = append(records, &nonce)
3✔
125
                },
3✔
126
        )
127

128
        extraData, err := MergeAndEncode(records, s.ExtraData, s.CustomRecords)
3✔
129
        if err != nil {
3✔
UNCOV
130
                return err
×
UNCOV
131
        }
×
132

133
        return WriteBytes(w, extraData)
3✔
134
}
135

136
// MsgType returns the integer uniquely identifying this message type on the
137
// wire.
138
//
139
// This is part of the lnwire.Message interface.
140
func (s *Shutdown) MsgType() MessageType {
3✔
141
        return MsgShutdown
3✔
142
}
3✔
143

144
// SerializedSize returns the serialized size of the message in bytes.
145
//
146
// This is part of the lnwire.SizeableMessage interface.
NEW
147
func (s *Shutdown) SerializedSize() (uint32, error) {
×
NEW
148
        return MessageSerializedSize(s)
×
NEW
149
}
×
150

151
// RandTestMessage populates the message with random data suitable for testing.
152
// It uses the rapid testing framework to generate random values.
153
//
154
// This is part of the TestMessage interface.
NEW
155
func (s *Shutdown) RandTestMessage(t *rapid.T) Message {
×
NEW
156
        // Generate random delivery address
×
NEW
157
        // First decide the address type (P2PKH, P2SH, P2WPKH, P2WSH, P2TR)
×
NEW
158
        addrType := rapid.IntRange(0, 4).Draw(t, "addrType")
×
NEW
159

×
NEW
160
        // Generate random address length based on type
×
NEW
161
        var addrLen int
×
NEW
162
        switch addrType {
×
163
        // P2PKH
NEW
164
        case 0:
×
NEW
165
                addrLen = 25
×
166
        // P2SH
NEW
167
        case 1:
×
NEW
168
                addrLen = 23
×
169
        // P2WPKH
NEW
170
        case 2:
×
NEW
171
                addrLen = 22
×
172
        // P2WSH
NEW
173
        case 3:
×
NEW
174
                addrLen = 34
×
175
        // P2TR
NEW
176
        case 4:
×
NEW
177
                addrLen = 34
×
178
        }
179

NEW
180
        addr := rapid.SliceOfN(rapid.Byte(), addrLen, addrLen).Draw(
×
NEW
181
                t, "address",
×
NEW
182
        )
×
NEW
183

×
NEW
184
        // Randomly decide whether to include a shutdown nonce
×
NEW
185
        includeNonce := rapid.Bool().Draw(t, "includeNonce")
×
NEW
186
        var shutdownNonce ShutdownNonceTLV
×
NEW
187

×
NEW
188
        if includeNonce {
×
NEW
189
                shutdownNonce = SomeShutdownNonce(RandMusig2Nonce(t))
×
NEW
190
        }
×
191

NEW
192
        cr, _ := RandCustomRecords(t, nil, true)
×
NEW
193

×
NEW
194
        return &Shutdown{
×
NEW
195
                ChannelID:     RandChannelID(t),
×
NEW
196
                Address:       addr,
×
NEW
197
                ShutdownNonce: shutdownNonce,
×
NEW
198
                CustomRecords: cr,
×
NEW
199
        }
×
200
}
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