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

lightningnetwork / lnd / 12118348650

02 Dec 2024 11:25AM UTC coverage: 58.552% (-0.4%) from 58.977%
12118348650

Pull #9175

github

ellemouton
lnwire: add NodeAnnouncement2
Pull Request #9175: lnwire+netann: update structure of g175 messages to be pure TLV

405 of 571 new or added lines in 11 files covered. (70.93%)

1754 existing lines in 33 files now uncovered.

133774 of 228469 relevant lines covered (58.55%)

19422.52 hits per line

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

93.33
/lnwire/channel_id.go
1
package lnwire
2

3
import (
4
        "encoding/binary"
5
        "encoding/hex"
6
        "io"
7
        "math"
8

9
        "github.com/btcsuite/btcd/chaincfg/chainhash"
10
        "github.com/btcsuite/btcd/wire"
11
        "github.com/lightningnetwork/lnd/tlv"
12
)
13

14
const (
15
        // MaxFundingTxOutputs is the maximum number of allowed outputs on a
16
        // funding transaction within the protocol. This is due to the fact
17
        // that we use 2-bytes to encode the index within the funding output
18
        // during the funding workflow. Funding transaction with more outputs
19
        // than this are considered invalid within the protocol.
20
        MaxFundingTxOutputs = math.MaxUint16
21
)
22

23
// ChannelID is a series of 32-bytes that uniquely identifies all channels
24
// within the network. The ChannelID is computed using the outpoint of the
25
// funding transaction (the txid, and output index). Given a funding output the
26
// ChannelID can be calculated by XOR'ing the big-endian serialization of the
27
// txid and the big-endian serialization of the output index, truncated to
28
// 2 bytes.
29
type ChannelID [32]byte
30

31
// ConnectionWideID is an all-zero ChannelID, which is used to represent a
32
// message intended for all channels to specific peer.
33
var ConnectionWideID = ChannelID{}
34

35
// String returns the string representation of the ChannelID. This is just the
36
// hex string encoding of the ChannelID itself.
37
func (c ChannelID) String() string {
5,028✔
38
        return hex.EncodeToString(c[:])
5,028✔
39
}
5,028✔
40

41
// Record returns a TLV record that can be used to encode/decode a ChannelID
42
// to/from a TLV stream.
43
func (c *ChannelID) Record() tlv.Record {
200✔
44
        return tlv.MakeStaticRecord(0, c, 32, encodeChannelID, decodeChannelID)
200✔
45
}
200✔
46

47
func encodeChannelID(w io.Writer, val interface{}, buf *[8]byte) error {
100✔
48
        if v, ok := val.(*ChannelID); ok {
200✔
49
                bigSize := [32]byte(*v)
100✔
50

100✔
51
                return tlv.EBytes32(w, &bigSize, buf)
100✔
52
        }
100✔
53

NEW
54
        return tlv.NewTypeForEncodingErr(val, "lnwire.ChannelID")
×
55
}
56

57
func decodeChannelID(r io.Reader, val interface{}, buf *[8]byte,
58
        l uint64) error {
100✔
59

100✔
60
        if v, ok := val.(*ChannelID); ok {
200✔
61
                var id [32]byte
100✔
62
                err := tlv.DBytes32(r, &id, buf, l)
100✔
63
                if err != nil {
100✔
NEW
64
                        return err
×
NEW
65
                }
×
66

67
                *v = id
100✔
68

100✔
69
                return nil
100✔
70
        }
71

NEW
72
        return tlv.NewTypeForDecodingErr(val, "lnwire.ChannelID", l, l)
×
73
}
74

75
// NewChanIDFromOutPoint converts a target OutPoint into a ChannelID that is
76
// usable within the network. In order to convert the OutPoint into a ChannelID,
77
// we XOR the lower 2-bytes of the txid within the OutPoint with the big-endian
78
// serialization of the Index of the OutPoint, truncated to 2-bytes.
79
func NewChanIDFromOutPoint(op wire.OutPoint) ChannelID {
153,572✔
80
        // First we'll copy the txid of the outpoint into our channel ID slice.
153,572✔
81
        var cid ChannelID
153,572✔
82
        copy(cid[:], op.Hash[:])
153,572✔
83

153,572✔
84
        // With the txid copied over, we'll now XOR the lower 2-bytes of the
153,572✔
85
        // partial channelID with big-endian serialization of output index.
153,572✔
86
        xorTxid(&cid, uint16(op.Index))
153,572✔
87

153,572✔
88
        return cid
153,572✔
89
}
153,572✔
90

91
// xorTxid performs the transformation needed to transform an OutPoint into a
92
// ChannelID. To do this, we expect the cid parameter to contain the txid
93
// unaltered and the outputIndex to be the output index
94
func xorTxid(cid *ChannelID, outputIndex uint16) {
219,107✔
95
        var buf [2]byte
219,107✔
96
        binary.BigEndian.PutUint16(buf[:], outputIndex)
219,107✔
97

219,107✔
98
        cid[30] ^= buf[0]
219,107✔
99
        cid[31] ^= buf[1]
219,107✔
100
}
219,107✔
101

102
// GenPossibleOutPoints generates all the possible outputs given a channel ID.
103
// In order to generate these possible outpoints, we perform a brute-force
104
// search through the candidate output index space, performing a reverse
105
// mapping from channelID back to OutPoint.
106
func (c *ChannelID) GenPossibleOutPoints() [MaxFundingTxOutputs]wire.OutPoint {
1✔
107
        var possiblePoints [MaxFundingTxOutputs]wire.OutPoint
1✔
108
        for i := uint16(0); i < MaxFundingTxOutputs; i++ {
65,536✔
109
                cidCopy := *c
65,535✔
110
                xorTxid(&cidCopy, i)
65,535✔
111

65,535✔
112
                possiblePoints[i] = wire.OutPoint{
65,535✔
113
                        Hash:  chainhash.Hash(cidCopy),
65,535✔
114
                        Index: uint32(i),
65,535✔
115
                }
65,535✔
116
        }
65,535✔
117

118
        return possiblePoints
1✔
119
}
120

121
// IsChanPoint returns true if the OutPoint passed corresponds to the target
122
// ChannelID.
123
func (c ChannelID) IsChanPoint(op *wire.OutPoint) bool {
70,890✔
124
        candidateCid := NewChanIDFromOutPoint(*op)
70,890✔
125

70,890✔
126
        return candidateCid == c
70,890✔
127
}
70,890✔
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