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

lightningnetwork / lnd / 11389453157

17 Oct 2024 04:50PM UTC coverage: 57.875% (-0.9%) from 58.81%
11389453157

Pull #9148

github

ProofOfKeags
lnwire: convert DynPropose and DynCommit to use typed tlv records
Pull Request #9148: DynComms [2/n]: lnwire: add authenticated wire messages for Dyn*

336 of 477 new or added lines in 12 files covered. (70.44%)

18956 existing lines in 244 files now uncovered.

99218 of 171435 relevant lines covered (57.87%)

36987.44 hits per line

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

81.33
/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 DynAck implements the lnwire.Message
29
// interface.
30
var _ Message = (*DynCommit)(nil)
31

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

41
        if err := WriteSig(w, dc.Sig); err != nil {
100✔
NEW
42
                return err
×
NEW
43
        }
×
44

45
        var extra ExtraOpaqueData
100✔
46
        extra.PackRecords(dynProposeRecords(&dc.DynPropose)...)
100✔
47
        dc.ExtraData = extra
100✔
48

100✔
49
        return WriteBytes(w, dc.ExtraData)
100✔
50
}
51

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

100✔
64
        // Parse out TLV records.
100✔
65
        var tlvRecords ExtraOpaqueData
100✔
66
        if err := ReadElement(r, &tlvRecords); err != nil {
100✔
NEW
67
                return err
×
NEW
68
        }
×
69

70
        // Prepare receiving buffers to be filled by TLV extraction.
71
        var dustLimit tlv.RecordT[tlv.TlvType0, uint64]
100✔
72
        var maxValue tlv.RecordT[tlv.TlvType2, uint64]
100✔
73
        var htlcMin tlv.RecordT[tlv.TlvType4, uint64]
100✔
74
        var reserve tlv.RecordT[tlv.TlvType6, uint64]
100✔
75
        csvDelay := dc.CsvDelay.Zero()
100✔
76
        maxHtlcs := dc.MaxAcceptedHTLCs.Zero()
100✔
77
        chanType := dc.ChannelType.Zero()
100✔
78

100✔
79
        typeMap, err := tlvRecords.ExtractRecords(
100✔
80
                &dustLimit, &maxValue, &htlcMin, &reserve, &csvDelay, &maxHtlcs,
100✔
81
                &chanType,
100✔
82
        )
100✔
83
        if err != nil {
100✔
NEW
84
                return err
×
NEW
85
        }
×
86

87
        // Check the results of the TLV Stream decoding and appropriately set
88
        // message fields.
89
        if val, ok := typeMap[dc.DustLimit.TlvType()]; ok && val == nil {
150✔
90
                var rec tlv.RecordT[tlv.TlvType0, btcutil.Amount]
50✔
91
                rec.Val = btcutil.Amount(dustLimit.Val)
50✔
92
                dc.DustLimit = tlv.SomeRecordT(rec)
50✔
93
        }
50✔
94
        if val, ok := typeMap[dc.MaxValueInFlight.TlvType()]; ok && val == nil {
148✔
95
                var rec tlv.RecordT[tlv.TlvType2, MilliSatoshi]
48✔
96
                rec.Val = MilliSatoshi(maxValue.Val)
48✔
97
                dc.MaxValueInFlight = tlv.SomeRecordT(rec)
48✔
98
        }
48✔
99
        if val, ok := typeMap[dc.HtlcMinimum.TlvType()]; ok && val == nil {
100✔
NEW
100
                var rec tlv.RecordT[tlv.TlvType4, MilliSatoshi]
×
NEW
101
                rec.Val = MilliSatoshi(htlcMin.Val)
×
NEW
102
                dc.HtlcMinimum = tlv.SomeRecordT(rec)
×
NEW
103
        }
×
104
        if val, ok := typeMap[dc.ChannelReserve.TlvType()]; ok && val == nil {
146✔
105
                var rec tlv.RecordT[tlv.TlvType6, btcutil.Amount]
46✔
106
                rec.Val = btcutil.Amount(reserve.Val)
46✔
107
                dc.ChannelReserve = tlv.SomeRecordT(rec)
46✔
108
        }
46✔
109
        if val, ok := typeMap[dc.CsvDelay.TlvType()]; ok && val == nil {
144✔
110
                dc.CsvDelay = tlv.SomeRecordT(csvDelay)
44✔
111
        }
44✔
112
        if val, ok := typeMap[dc.MaxAcceptedHTLCs.TlvType()]; ok && val == nil {
155✔
113
                dc.MaxAcceptedHTLCs = tlv.SomeRecordT(maxHtlcs)
55✔
114
        }
55✔
115
        if val, ok := typeMap[dc.ChannelType.TlvType()]; ok && val == nil {
150✔
116
                dc.ChannelType = tlv.SomeRecordT(chanType)
50✔
117
        }
50✔
118

119
        if len(tlvRecords) != 0 {
198✔
120
                dc.ExtraData = tlvRecords
98✔
121
        }
98✔
122

123
        return nil
100✔
124
}
125

126
// MsgType returns the MessageType code which uniquely identifies this message
127
// as a DynCommit on the wire.
128
//
129
// This is part of the lnwire.Message interface.
130
func (dc *DynCommit) MsgType() MessageType {
100✔
131
        return MsgDynCommit
100✔
132
}
100✔
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