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

lightningnetwork / lnd / 14880107727

07 May 2025 09:35AM UTC coverage: 58.446% (-10.5%) from 68.992%
14880107727

Pull #9789

github

web-flow
Merge ed3471042 into 67a40c90a
Pull Request #9789: multi: use updated TLV SizeFunc signature

54 of 95 new or added lines in 19 files covered. (56.84%)

28462 existing lines in 449 files now uncovered.

97165 of 166247 relevant lines covered (58.45%)

1.81 hits per line

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

72.13
/lnwire/timestamps.go
1
package lnwire
2

3
import (
4
        "bytes"
5
        "fmt"
6
        "io"
7

8
        "github.com/lightningnetwork/lnd/tlv"
9
)
10

11
const (
12
        // TimestampsRecordType is the TLV number of the timestamps TLV record
13
        // in the reply_channel_range message.
14
        TimestampsRecordType tlv.Type = 1
15

16
        // timestampPairSize is the number of bytes required to encode two
17
        // timestamps. Each timestamp is four bytes.
18
        timestampPairSize = 8
19
)
20

21
// Timestamps is a type representing the timestamps TLV field used in the
22
// reply_channel_range message to communicate the timestamps info of the updates
23
// of the SCID list being communicated.
24
type Timestamps []ChanUpdateTimestamps
25

26
// ChanUpdateTimestamps holds the timestamp info of the latest known channel
27
// updates corresponding to the two sides of a channel.
28
type ChanUpdateTimestamps struct {
29
        Timestamp1 uint32
30
        Timestamp2 uint32
31
}
32

33
// Record constructs the tlv.Record from the Timestamps.
34
func (t *Timestamps) Record() tlv.Record {
3✔
35
        return tlv.MakeDynamicRecord(
3✔
36
                TimestampsRecordType, t, func() (uint64, error) {
6✔
37
                        return t.encodedLen(), nil
3✔
38
                }, timeStampsEncoder, timeStampsDecoder,
3✔
39
        )
40
}
41

42
// encodedLen calculates the length of the encoded Timestamps.
43
func (t *Timestamps) encodedLen() uint64 {
3✔
44
        return uint64(1 + timestampPairSize*(len(*t)))
3✔
45
}
3✔
46

47
// timeStampsEncoder encodes the Timestamps and writes the encoded bytes to the
48
// given writer.
49
func timeStampsEncoder(w io.Writer, val interface{}, _ *[8]byte) error {
3✔
50
        if v, ok := val.(*Timestamps); ok {
6✔
51
                var buf bytes.Buffer
3✔
52

3✔
53
                // Add the encoding byte.
3✔
54
                err := WriteQueryEncoding(&buf, EncodingSortedPlain)
3✔
55
                if err != nil {
3✔
56
                        return err
×
57
                }
×
58

59
                // For each timestamp, write 4 byte timestamp of node 1 and the
60
                // 4 byte timestamp of node 2.
61
                for _, timestamps := range *v {
6✔
62
                        err = WriteUint32(&buf, timestamps.Timestamp1)
3✔
63
                        if err != nil {
3✔
64
                                return err
×
65
                        }
×
66

67
                        err = WriteUint32(&buf, timestamps.Timestamp2)
3✔
68
                        if err != nil {
3✔
69
                                return err
×
70
                        }
×
71
                }
72

73
                _, err = w.Write(buf.Bytes())
3✔
74

3✔
75
                return err
3✔
76
        }
77

78
        return tlv.NewTypeForEncodingErr(val, "lnwire.Timestamps")
×
79
}
80

81
// timeStampsDecoder attempts to read and reconstruct a Timestamps object from
82
// the given reader.
83
func timeStampsDecoder(r io.Reader, val interface{}, _ *[8]byte,
84
        l uint64) error {
3✔
85

3✔
86
        if v, ok := val.(*Timestamps); ok {
6✔
87
                var encodingByte [1]byte
3✔
88
                if _, err := r.Read(encodingByte[:]); err != nil {
3✔
UNCOV
89
                        return err
×
UNCOV
90
                }
×
91

92
                encoding := QueryEncoding(encodingByte[0])
3✔
93
                if encoding != EncodingSortedPlain {
3✔
UNCOV
94
                        return fmt.Errorf("unsupported encoding: %x", encoding)
×
UNCOV
95
                }
×
96

97
                // The number of timestamps bytes is equal to the passed length
98
                // minus one since the first byte is used for the encoding type.
99
                numTimestampBytes := l - 1
3✔
100

3✔
101
                if numTimestampBytes%timestampPairSize != 0 {
3✔
UNCOV
102
                        return fmt.Errorf("whole number of timestamps not " +
×
UNCOV
103
                                "encoded")
×
UNCOV
104
                }
×
105

106
                numTimestamps := int(numTimestampBytes) / timestampPairSize
3✔
107
                timestamps := make(Timestamps, numTimestamps)
3✔
108
                for i := 0; i < numTimestamps; i++ {
6✔
109
                        err := ReadElements(
3✔
110
                                r, &timestamps[i].Timestamp1,
3✔
111
                                &timestamps[i].Timestamp2,
3✔
112
                        )
3✔
113
                        if err != nil {
3✔
UNCOV
114
                                return err
×
UNCOV
115
                        }
×
116
                }
117

118
                *v = timestamps
3✔
119

3✔
120
                return nil
3✔
121
        }
122

123
        return tlv.NewTypeForEncodingErr(val, "lnwire.Timestamps")
×
124
}
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