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

lightningnetwork / lnd / 19155841408

07 Nov 2025 02:03AM UTC coverage: 66.675% (-0.04%) from 66.712%
19155841408

Pull #10352

github

web-flow
Merge e4313eba8 into 096ab65b1
Pull Request #10352: [WIP] chainrpc: return Unavailable while notifier starts

137328 of 205965 relevant lines covered (66.68%)

21333.36 hits per line

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

87.1
/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 {
877✔
35
        return tlv.MakeDynamicRecord(
877✔
36
                TimestampsRecordType, t, t.encodedLen, timeStampsEncoder,
877✔
37
                timeStampsDecoder,
877✔
38
        )
877✔
39
}
877✔
40

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

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

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

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

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

72
                _, err = w.Write(buf.Bytes())
43✔
73

43✔
74
                return err
43✔
75
        }
76

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

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

70✔
85
        if v, ok := val.(*Timestamps); ok {
140✔
86
                var encodingByte [1]byte
70✔
87
                if _, err := r.Read(encodingByte[:]); err != nil {
71✔
88
                        return err
1✔
89
                }
1✔
90

91
                encoding := QueryEncoding(encodingByte[0])
69✔
92
                if encoding != EncodingSortedPlain {
71✔
93
                        return fmt.Errorf("unsupported encoding: %x", encoding)
2✔
94
                }
2✔
95

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

67✔
100
                if numTimestampBytes%timestampPairSize != 0 {
68✔
101
                        return fmt.Errorf("whole number of timestamps not " +
1✔
102
                                "encoded")
1✔
103
                }
1✔
104

105
                numTimestamps := int(numTimestampBytes) / timestampPairSize
66✔
106
                timestamps := make(Timestamps, numTimestamps)
66✔
107
                for i := 0; i < numTimestamps; i++ {
1,099✔
108
                        err := ReadElements(
1,033✔
109
                                r, &timestamps[i].Timestamp1,
1,033✔
110
                                &timestamps[i].Timestamp2,
1,033✔
111
                        )
1,033✔
112
                        if err != nil {
1,050✔
113
                                return err
17✔
114
                        }
17✔
115
                }
116

117
                *v = timestamps
49✔
118

49✔
119
                return nil
49✔
120
        }
121

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