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

lightningnetwork / lnd / 11170835610

03 Oct 2024 10:41PM UTC coverage: 49.188% (-9.6%) from 58.738%
11170835610

push

github

web-flow
Merge pull request #9154 from ziggie1984/master

multi: bump btcd version.

3 of 6 new or added lines in 6 files covered. (50.0%)

26110 existing lines in 428 files now uncovered.

97359 of 197934 relevant lines covered (49.19%)

1.04 hits per line

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

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

41
// encodedLen calculates the length of the encoded Timestamps.
42
func (t *Timestamps) encodedLen() uint64 {
2✔
43
        return uint64(1 + timestampPairSize*(len(*t)))
2✔
44
}
2✔
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 {
2✔
49
        if v, ok := val.(*Timestamps); ok {
4✔
50
                var buf bytes.Buffer
2✔
51

2✔
52
                // Add the encoding byte.
2✔
53
                err := WriteQueryEncoding(&buf, EncodingSortedPlain)
2✔
54
                if err != nil {
2✔
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 {
4✔
61
                        err = WriteUint32(&buf, timestamps.Timestamp1)
2✔
62
                        if err != nil {
2✔
63
                                return err
×
64
                        }
×
65

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

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

2✔
74
                return err
2✔
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 {
2✔
84

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

91
                encoding := QueryEncoding(encodingByte[0])
2✔
92
                if encoding != EncodingSortedPlain {
2✔
UNCOV
93
                        return fmt.Errorf("unsupported encoding: %x", encoding)
×
UNCOV
94
                }
×
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
2✔
99

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

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

117
                *v = timestamps
2✔
118

2✔
119
                return nil
2✔
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