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

lightningnetwork / lnd / 15869455718

25 Jun 2025 06:53AM UTC coverage: 55.806% (-12.2%) from 67.978%
15869455718

Pull #9148

github

web-flow
Merge c64e3a6c3 into 4335d9cfb
Pull Request #9148: DynComms [2/n]: lnwire: add authenticated wire messages for Dyn*

232 of 270 new or added lines in 5 files covered. (85.93%)

23628 existing lines in 289 files now uncovered.

108377 of 194204 relevant lines covered (55.81%)

22552.93 hits per line

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

76.54
/lnwire/dyn_commit.go
1
package lnwire
2

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

7
        "github.com/btcsuite/btcd/btcutil"
8
        "github.com/lightningnetwork/lnd/tlv"
9
)
10

11
// DynCommit is a composite message that is used to irrefutably execute a
12
// dynamic commitment update.
13
type DynCommit struct {
14
        // DynPropose is an embedded version of the original DynPropose message
15
        // that initiated this negotiation.
16
        DynPropose
17

18
        // DynAck is an embedded version of the original DynAck message that
19
        // countersigned this negotiation.
20
        DynAck
21

22
        // ExtraData is the set of data that was appended to this message to
23
        // fill out the full maximum transport message size. These fields can
24
        // be used to specify optional data such as custom TLV fields.
25
        ExtraData ExtraOpaqueData
26
}
27

28
// A compile time check to ensure DynCommit implements the lnwire.Message
29
// interface.
30
var _ Message = (*DynCommit)(nil)
31

32
// A compile time check to ensure DynCommit implements the
33
// lnwire.SizeableMessage interface.
34
var _ SizeableMessage = (*DynCommit)(nil)
35

36
// Encode serializes the target DynAck into the passed io.Writer. Serialization
37
// will observe the rules defined by the passed protocol version.
38
//
39
// This is a part of the lnwire.Message interface.
40
func (dc *DynCommit) Encode(w *bytes.Buffer, _ uint32) error {
100✔
41
        if err := WriteChannelID(w, dc.DynPropose.ChanID); err != nil {
100✔
NEW
42
                return err
×
NEW
43
        }
×
44

45
        if err := WriteSig(w, dc.Sig); err != nil {
100✔
NEW
46
                return err
×
NEW
47
        }
×
48

49
        var extra ExtraOpaqueData
100✔
50
        err := extra.PackRecords(dynProposeRecords(&dc.DynPropose)...)
100✔
51
        if err != nil {
100✔
NEW
52
                return err
×
NEW
53
        }
×
54
        dc.ExtraData = extra
100✔
55

100✔
56
        return WriteBytes(w, dc.ExtraData)
100✔
57
}
58

59
// Decode deserializes the serialized DynCommit stored in the passed io.Reader
60
// into the target DynAck using the deserialization rules defined by the passed
61
// protocol version.
62
//
63
// This is a part of the lnwire.Message interface.
64
func (dc *DynCommit) Decode(r io.Reader, _ uint32) error {
100✔
65
        // Parse out main message.
100✔
66
        if err := ReadElements(r, &dc.DynPropose.ChanID, &dc.Sig); err != nil {
100✔
NEW
67
                return err
×
NEW
68
        }
×
69
        dc.DynAck.ChanID = dc.DynPropose.ChanID
100✔
70

100✔
71
        // Parse out TLV records.
100✔
72
        var tlvRecords ExtraOpaqueData
100✔
73
        if err := ReadElement(r, &tlvRecords); err != nil {
100✔
NEW
74
                return err
×
NEW
75
        }
×
76

77
        // Prepare receiving buffers to be filled by TLV extraction.
78
        var dustLimit tlv.RecordT[tlv.TlvType0, uint64]
100✔
79
        var maxValue tlv.RecordT[tlv.TlvType2, uint64]
100✔
80
        var htlcMin tlv.RecordT[tlv.TlvType4, uint64]
100✔
81
        var reserve tlv.RecordT[tlv.TlvType6, uint64]
100✔
82
        csvDelay := dc.CsvDelay.Zero()
100✔
83
        maxHtlcs := dc.MaxAcceptedHTLCs.Zero()
100✔
84
        chanType := dc.ChannelType.Zero()
100✔
85

100✔
86
        typeMap, err := tlvRecords.ExtractRecords(
100✔
87
                &dustLimit, &maxValue, &htlcMin, &reserve, &csvDelay, &maxHtlcs,
100✔
88
                &chanType,
100✔
89
        )
100✔
90
        if err != nil {
100✔
NEW
91
                return err
×
NEW
92
        }
×
93

94
        // Check the results of the TLV Stream decoding and appropriately set
95
        // message fields.
96
        if val, ok := typeMap[dc.DustLimit.TlvType()]; ok && val == nil {
149✔
97
                var rec tlv.RecordT[tlv.TlvType0, btcutil.Amount]
49✔
98
                rec.Val = btcutil.Amount(dustLimit.Val)
49✔
99
                dc.DustLimit = tlv.SomeRecordT(rec)
49✔
100
        }
49✔
101
        if val, ok := typeMap[dc.MaxValueInFlight.TlvType()]; ok && val == nil {
144✔
102
                var rec tlv.RecordT[tlv.TlvType2, MilliSatoshi]
44✔
103
                rec.Val = MilliSatoshi(maxValue.Val)
44✔
104
                dc.MaxValueInFlight = tlv.SomeRecordT(rec)
44✔
105
        }
44✔
106
        if val, ok := typeMap[dc.HtlcMinimum.TlvType()]; ok && val == nil {
100✔
NEW
107
                var rec tlv.RecordT[tlv.TlvType4, MilliSatoshi]
×
NEW
108
                rec.Val = MilliSatoshi(htlcMin.Val)
×
NEW
109
                dc.HtlcMinimum = tlv.SomeRecordT(rec)
×
NEW
110
        }
×
111
        if val, ok := typeMap[dc.ChannelReserve.TlvType()]; ok && val == nil {
151✔
112
                var rec tlv.RecordT[tlv.TlvType6, btcutil.Amount]
51✔
113
                rec.Val = btcutil.Amount(reserve.Val)
51✔
114
                dc.ChannelReserve = tlv.SomeRecordT(rec)
51✔
115
        }
51✔
116
        if val, ok := typeMap[dc.CsvDelay.TlvType()]; ok && val == nil {
145✔
117
                dc.CsvDelay = tlv.SomeRecordT(csvDelay)
45✔
118
        }
45✔
119
        if val, ok := typeMap[dc.MaxAcceptedHTLCs.TlvType()]; ok && val == nil {
153✔
120
                dc.MaxAcceptedHTLCs = tlv.SomeRecordT(maxHtlcs)
53✔
121
        }
53✔
122
        if val, ok := typeMap[dc.ChannelType.TlvType()]; ok && val == nil {
151✔
123
                dc.ChannelType = tlv.SomeRecordT(chanType)
51✔
124
        }
51✔
125

126
        if len(tlvRecords) != 0 {
199✔
127
                dc.ExtraData = tlvRecords
99✔
128
        }
99✔
129

130
        return nil
100✔
131
}
132

133
// MsgType returns the MessageType code which uniquely identifies this message
134
// as a DynCommit on the wire.
135
//
136
// This is part of the lnwire.Message interface.
137
func (dc *DynCommit) MsgType() MessageType {
100✔
138
        return MsgDynCommit
100✔
139
}
100✔
140

141
// SerializedSize returns the serialized size of the message in bytes.
142
//
143
// This is part of the lnwire.SizeableMessage interface.
NEW
144
func (dc *DynCommit) SerializedSize() (uint32, error) {
×
NEW
145
        return MessageSerializedSize(dc)
×
NEW
146
}
×
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