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

lightningnetwork / lnd / 11294395806

11 Oct 2024 02:38PM UTC coverage: 49.177% (-0.002%) from 49.179%
11294395806

Pull #9175

github

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

0 of 207 new or added lines in 7 files covered. (0.0%)

73 existing lines in 17 files now uncovered.

97421 of 198103 relevant lines covered (49.18%)

1.04 hits per line

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

43.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 {
2✔
38
        return hex.EncodeToString(c[:])
2✔
39
}
2✔
40

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

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

×
NEW
51
                return tlv.EBytes32(w, &bigSize, buf)
×
NEW
52
        }
×
53

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

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

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

NEW
67
                *v = id
×
NEW
68

×
NEW
69
                return nil
×
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 {
2✔
80
        // First we'll copy the txid of the outpoint into our channel ID slice.
2✔
81
        var cid ChannelID
2✔
82
        copy(cid[:], op.Hash[:])
2✔
83

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

2✔
88
        return cid
2✔
89
}
2✔
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) {
2✔
95
        var buf [2]byte
2✔
96
        binary.BigEndian.PutUint16(buf[:], outputIndex)
2✔
97

2✔
98
        cid[30] ^= buf[0]
2✔
99
        cid[31] ^= buf[1]
2✔
100
}
2✔
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 {
×
107
        var possiblePoints [MaxFundingTxOutputs]wire.OutPoint
×
108
        for i := uint16(0); i < MaxFundingTxOutputs; i++ {
×
109
                cidCopy := *c
×
110
                xorTxid(&cidCopy, i)
×
111

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

118
        return possiblePoints
×
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 {
2✔
124
        candidateCid := NewChanIDFromOutPoint(*op)
2✔
125

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