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

lightningnetwork / lnd / 13586005509

28 Feb 2025 10:14AM UTC coverage: 68.629% (+9.9%) from 58.77%
13586005509

Pull #9521

github

web-flow
Merge 37d3a70a5 into 8532955b3
Pull Request #9521: unit: remove GOACC, use Go 1.20 native coverage functionality

129950 of 189351 relevant lines covered (68.63%)

23726.46 hits per line

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

66.98
/watchtower/wtdb/migration8/codec.go
1
package migration8
2

3
import (
4
        "bytes"
5
        "encoding/binary"
6
        "encoding/hex"
7
        "fmt"
8
        "io"
9

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

14
// BreachHintSize is the length of the identifier used to detect remote
15
// commitment broadcasts.
16
const BreachHintSize = 16
17

18
// BreachHint is the first 16-bytes of SHA256(txid), which is used to identify
19
// the breach transaction.
20
type BreachHint [BreachHintSize]byte
21

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

30
// writeBigSize will encode the given uint64 as a BigSize byte slice.
31
func writeBigSize(i uint64) ([]byte, error) {
28✔
32
        var b bytes.Buffer
28✔
33
        err := tlv.WriteVarInt(&b, i, &[8]byte{})
28✔
34
        if err != nil {
28✔
35
                return nil, err
×
36
        }
×
37

38
        return b.Bytes(), nil
28✔
39
}
40

41
// readBigSize converts the given byte slice into a uint64 and assumes that the
42
// bytes slice is using BigSize encoding.
43
func readBigSize(b []byte) (uint64, error) {
12✔
44
        r := bytes.NewReader(b)
12✔
45
        i, err := tlv.ReadVarInt(r, &[8]byte{})
12✔
46
        if err != nil {
12✔
47
                return 0, err
×
48
        }
×
49

50
        return i, nil
12✔
51
}
52

53
// CommittedUpdate holds a state update sent by a client along with its
54
// allocated sequence number and the exact remote commitment the encrypted
55
// justice transaction can rectify.
56
type CommittedUpdate struct {
57
        // SeqNum is the unique sequence number allocated by the session to this
58
        // update.
59
        SeqNum uint16
60

61
        CommittedUpdateBody
62
}
63

64
// BackupID identifies a particular revoked, remote commitment by channel id and
65
// commitment height.
66
type BackupID struct {
67
        // ChanID is the channel id of the revoked commitment.
68
        ChanID ChannelID
69

70
        // CommitHeight is the commitment height of the revoked commitment.
71
        CommitHeight uint64
72
}
73

74
// Encode writes the BackupID from the passed io.Writer.
75
func (b *BackupID) Encode(w io.Writer) error {
3✔
76
        return WriteElements(w,
3✔
77
                b.ChanID,
3✔
78
                b.CommitHeight,
3✔
79
        )
3✔
80
}
3✔
81

82
// Decode reads a BackupID from the passed io.Reader.
83
func (b *BackupID) Decode(r io.Reader) error {
3✔
84
        return ReadElements(r,
3✔
85
                &b.ChanID,
3✔
86
                &b.CommitHeight,
3✔
87
        )
3✔
88
}
3✔
89

90
// String returns a human-readable encoding of a BackupID.
91
func (b BackupID) String() string {
×
92
        return fmt.Sprintf("backup(%v, %d)", b.ChanID, b.CommitHeight)
×
93
}
×
94

95
// WriteElements serializes a variadic list of elements into the given
96
// io.Writer.
97
func WriteElements(w io.Writer, elements ...interface{}) error {
6✔
98
        for _, element := range elements {
18✔
99
                if err := WriteElement(w, element); err != nil {
12✔
100
                        return err
×
101
                }
×
102
        }
103

104
        return nil
6✔
105
}
106

107
// ReadElements deserializes the provided io.Reader into a variadic list of
108
// target elements.
109
func ReadElements(r io.Reader, elements ...interface{}) error {
6✔
110
        for _, element := range elements {
18✔
111
                if err := ReadElement(r, element); err != nil {
12✔
112
                        return err
×
113
                }
×
114
        }
115

116
        return nil
6✔
117
}
118

119
// WriteElement serializes a single element into the provided io.Writer.
120
func WriteElement(w io.Writer, element interface{}) error {
12✔
121
        switch e := element.(type) {
12✔
122
        case ChannelID:
3✔
123
                if _, err := w.Write(e[:]); err != nil {
3✔
124
                        return err
×
125
                }
×
126

127
        case uint64:
3✔
128
                if err := binary.Write(w, byteOrder, e); err != nil {
3✔
129
                        return err
×
130
                }
×
131

132
        case BreachHint:
3✔
133
                if _, err := w.Write(e[:]); err != nil {
3✔
134
                        return err
×
135
                }
×
136

137
        case []byte:
3✔
138
                if err := wire.WriteVarBytes(w, 0, e); err != nil {
3✔
139
                        return err
×
140
                }
×
141

142
        default:
×
143
                return fmt.Errorf("unexpected type")
×
144
        }
145

146
        return nil
12✔
147
}
148

149
// ReadElement deserializes a single element from the provided io.Reader.
150
func ReadElement(r io.Reader, element interface{}) error {
12✔
151
        switch e := element.(type) {
12✔
152
        case *ChannelID:
3✔
153
                if _, err := io.ReadFull(r, e[:]); err != nil {
3✔
154
                        return err
×
155
                }
×
156

157
        case *uint64:
3✔
158
                if err := binary.Read(r, byteOrder, e); err != nil {
3✔
159
                        return err
×
160
                }
×
161

162
        case *BreachHint:
3✔
163
                if _, err := io.ReadFull(r, e[:]); err != nil {
3✔
164
                        return err
×
165
                }
×
166

167
        case *[]byte:
3✔
168
                bytes, err := wire.ReadVarBytes(r, 0, 66000, "[]byte")
3✔
169
                if err != nil {
3✔
170
                        return err
×
171
                }
×
172

173
                *e = bytes
3✔
174

175
        default:
×
176
                return fmt.Errorf("unexpected type")
×
177
        }
178

179
        return nil
12✔
180
}
181

182
// CommittedUpdateBody represents the primary components of a CommittedUpdate.
183
// On disk, this is stored under the sequence number, which acts as its key.
184
type CommittedUpdateBody struct {
185
        // BackupID identifies the breached commitment that the encrypted blob
186
        // can spend from.
187
        BackupID BackupID
188

189
        // Hint is the 16-byte prefix of the revoked commitment transaction ID.
190
        Hint BreachHint
191

192
        // EncryptedBlob is a ciphertext containing the sweep information for
193
        // exacting justice if the commitment transaction matching the breach
194
        // hint is broadcast.
195
        EncryptedBlob []byte
196
}
197

198
// Encode writes the CommittedUpdateBody to the passed io.Writer.
199
func (u *CommittedUpdateBody) Encode(w io.Writer) error {
3✔
200
        err := u.BackupID.Encode(w)
3✔
201
        if err != nil {
3✔
202
                return err
×
203
        }
×
204

205
        return WriteElements(w,
3✔
206
                u.Hint,
3✔
207
                u.EncryptedBlob,
3✔
208
        )
3✔
209
}
210

211
// Decode reads a CommittedUpdateBody from the passed io.Reader.
212
func (u *CommittedUpdateBody) Decode(r io.Reader) error {
3✔
213
        err := u.BackupID.Decode(r)
3✔
214
        if err != nil {
3✔
215
                return err
×
216
        }
×
217

218
        return ReadElements(r,
3✔
219
                &u.Hint,
3✔
220
                &u.EncryptedBlob,
3✔
221
        )
3✔
222
}
223

224
// SessionIDSize is 33-bytes; it is a serialized, compressed public key.
225
const SessionIDSize = 33
226

227
// SessionID is created from the remote public key of a client, and serves as a
228
// unique identifier and authentication for sending state updates.
229
type SessionID [SessionIDSize]byte
230

231
// String returns a hex encoding of the session id.
232
func (s SessionID) String() string {
5✔
233
        return hex.EncodeToString(s[:])
5✔
234
}
5✔
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