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

lightningnetwork / lnd / 13211764208

08 Feb 2025 03:08AM UTC coverage: 49.288% (-9.5%) from 58.815%
13211764208

Pull #9489

github

calvinrzachman
itest: verify switchrpc server enforces send then track

We prevent the rpc server from allowing onion dispatches for
attempt IDs which have already been tracked by rpc clients.

This helps protect the client from leaking a duplicate onion
attempt. NOTE: This is not the only method for solving this
issue! The issue could be addressed via careful client side
programming which accounts for the uncertainty and async
nature of dispatching onions to a remote process via RPC.
This would require some lnd ChannelRouter changes for how
we intend to use these RPCs though.
Pull Request #9489: multi: add BuildOnion, SendOnion, and TrackOnion RPCs

474 of 990 new or added lines in 11 files covered. (47.88%)

27321 existing lines in 435 files now uncovered.

101192 of 205306 relevant lines covered (49.29%)

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

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

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

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

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

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

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

91
                encoding := QueryEncoding(encodingByte[0])
3✔
92
                if encoding != EncodingSortedPlain {
3✔
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
3✔
99

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

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

117
                *v = timestamps
3✔
118

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