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

lightningnetwork / lnd / 12199391122

06 Dec 2024 01:10PM UTC coverage: 49.807% (-9.1%) from 58.933%
12199391122

push

github

web-flow
Merge pull request #9337 from Guayaba221/patch-1

chore: fix typo in ruby.md

100137 of 201051 relevant lines covered (49.81%)

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

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

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

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

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

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

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

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

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

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

117
                *v = timestamps
4✔
118

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