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

lightningnetwork / lnd / 11393106485

17 Oct 2024 09:10PM UTC coverage: 57.848% (-1.0%) from 58.81%
11393106485

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*

142 of 177 new or added lines in 4 files covered. (80.23%)

18983 existing lines in 242 files now uncovered.

99003 of 171143 relevant lines covered (57.85%)

36968.25 hits per line

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

79.49
/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
        err := extra.PackRecords(dynProposeRecords(&dc.DynPropose)...)
100✔
47
        if err != nil {
100✔
NEW
48
                return err
×
NEW
49
        }
×
50
        dc.ExtraData = extra
100✔
51

100✔
52
        return WriteBytes(w, dc.ExtraData)
100✔
53
}
54

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

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

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

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

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

122
        if len(tlvRecords) != 0 {
199✔
123
                dc.ExtraData = tlvRecords
99✔
124
        }
99✔
125

126
        return nil
100✔
127
}
128

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