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

lightningnetwork / lnd / 13980275562

20 Mar 2025 10:06PM UTC coverage: 58.6% (-10.2%) from 68.789%
13980275562

Pull #9623

github

web-flow
Merge b9b960345 into 09b674508
Pull Request #9623: Size msg test msg

0 of 1518 new or added lines in 42 files covered. (0.0%)

26603 existing lines in 443 files now uncovered.

96807 of 165200 relevant lines covered (58.6%)

1.82 hits per line

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

36.17
/lnwire/gossip_timestamp_range.go
1
package lnwire
2

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

7
        "github.com/btcsuite/btcd/chaincfg/chainhash"
8
        "github.com/lightningnetwork/lnd/tlv"
9
        "pgregory.net/rapid"
10
)
11

12
// GossipTimestampRange is a message that allows the sender to restrict the set
13
// of future gossip announcements sent by the receiver. Nodes should send this
14
// if they have the gossip-queries feature bit active. Nodes are able to send
15
// new GossipTimestampRange messages to replace the prior window.
16
type GossipTimestampRange struct {
17
        // ChainHash denotes the chain that the sender wishes to restrict the
18
        // set of received announcements of.
19
        ChainHash chainhash.Hash
20

21
        // FirstTimestamp is the timestamp of the earliest announcement message
22
        // that should be sent by the receiver. This is only to be used for
23
        // querying message of gossip 1.0 which are timestamped using Unix
24
        // timestamps. FirstBlockHeight and BlockRange should be used to
25
        // query for announcement messages timestamped using block heights.
26
        FirstTimestamp uint32
27

28
        // TimestampRange is the horizon beyond the FirstTimestamp that any
29
        // announcement messages should be sent for. The receiving node MUST
30
        // NOT send any announcements that have a timestamp greater than
31
        // FirstTimestamp + TimestampRange. This is used together with
32
        // FirstTimestamp to query for gossip 1.0 messages timestamped with
33
        // Unix timestamps.
34
        TimestampRange uint32
35

36
        // FirstBlockHeight is the height of earliest announcement message that
37
        // should be sent by the receiver. This is used only for querying
38
        // announcement messages that use block heights as a timestamp.
39
        FirstBlockHeight tlv.OptionalRecordT[tlv.TlvType2, uint32]
40

41
        // BlockRange is the horizon beyond FirstBlockHeight that any
42
        // announcement messages should be sent for. The receiving node MUST NOT
43
        // send any announcements that have a timestamp greater than
44
        // FirstBlockHeight + BlockRange.
45
        BlockRange tlv.OptionalRecordT[tlv.TlvType4, uint32]
46

47
        // ExtraData is the set of data that was appended to this message to
48
        // fill out the full maximum transport message size. These fields can
49
        // be used to specify optional data such as custom TLV fields.
50
        ExtraData ExtraOpaqueData
51
}
52

53
// NewGossipTimestampRange creates a new empty GossipTimestampRange message.
54
func NewGossipTimestampRange() *GossipTimestampRange {
×
55
        return &GossipTimestampRange{}
×
56
}
×
57

58
// A compile time check to ensure GossipTimestampRange implements the
59
// lnwire.Message interface.
60
var _ Message = (*GossipTimestampRange)(nil)
61

62
// A compile time check to ensure GossipTimestampRange implements the lnwire.SizeableMessage interface.
63
var _ SizeableMessage = (*GossipTimestampRange)(nil)
64

65
// A compile time check to ensure GossipTimestampRange implements the lnwire.TestMessage interface.
66
var _ TestMessage = (*GossipTimestampRange)(nil)
67

68
// Decode deserializes a serialized GossipTimestampRange message stored in the
69
// passed io.Reader observing the specified protocol version.
70
//
71
// This is part of the lnwire.Message interface.
72
func (g *GossipTimestampRange) Decode(r io.Reader, _ uint32) error {
3✔
73
        err := ReadElements(r,
3✔
74
                g.ChainHash[:],
3✔
75
                &g.FirstTimestamp,
3✔
76
                &g.TimestampRange,
3✔
77
        )
3✔
78
        if err != nil {
3✔
UNCOV
79
                return err
×
UNCOV
80
        }
×
81

82
        var tlvRecords ExtraOpaqueData
3✔
83
        if err := ReadElements(r, &tlvRecords); err != nil {
3✔
UNCOV
84
                return err
×
UNCOV
85
        }
×
86

87
        var (
3✔
88
                firstBlock = tlv.ZeroRecordT[tlv.TlvType2, uint32]()
3✔
89
                blockRange = tlv.ZeroRecordT[tlv.TlvType4, uint32]()
3✔
90
        )
3✔
91
        typeMap, err := tlvRecords.ExtractRecords(&firstBlock, &blockRange)
3✔
92
        if err != nil {
3✔
UNCOV
93
                return err
×
UNCOV
94
        }
×
95

96
        if val, ok := typeMap[g.FirstBlockHeight.TlvType()]; ok && val == nil {
3✔
UNCOV
97
                g.FirstBlockHeight = tlv.SomeRecordT(firstBlock)
×
UNCOV
98
        }
×
99
        if val, ok := typeMap[g.BlockRange.TlvType()]; ok && val == nil {
3✔
UNCOV
100
                g.BlockRange = tlv.SomeRecordT(blockRange)
×
UNCOV
101
        }
×
102

103
        if len(tlvRecords) != 0 {
3✔
UNCOV
104
                g.ExtraData = tlvRecords
×
UNCOV
105
        }
×
106

107
        return nil
3✔
108
}
109

110
// Encode serializes the target GossipTimestampRange into the passed io.Writer
111
// observing the protocol version specified.
112
//
113
// This is part of the lnwire.Message interface.
114
func (g *GossipTimestampRange) Encode(w *bytes.Buffer, pver uint32) error {
3✔
115
        if err := WriteBytes(w, g.ChainHash[:]); err != nil {
3✔
UNCOV
116
                return err
×
UNCOV
117
        }
×
118

119
        if err := WriteUint32(w, g.FirstTimestamp); err != nil {
3✔
UNCOV
120
                return err
×
UNCOV
121
        }
×
122

123
        if err := WriteUint32(w, g.TimestampRange); err != nil {
3✔
UNCOV
124
                return err
×
UNCOV
125
        }
×
126

127
        recordProducers := make([]tlv.RecordProducer, 0, 2)
3✔
128
        g.FirstBlockHeight.WhenSome(
3✔
129
                func(height tlv.RecordT[tlv.TlvType2, uint32]) {
3✔
UNCOV
130
                        recordProducers = append(recordProducers, &height)
×
UNCOV
131
                },
×
132
        )
133
        g.BlockRange.WhenSome(
3✔
134
                func(blockRange tlv.RecordT[tlv.TlvType4, uint32]) {
3✔
UNCOV
135
                        recordProducers = append(recordProducers, &blockRange)
×
UNCOV
136
                },
×
137
        )
138
        err := EncodeMessageExtraData(&g.ExtraData, recordProducers...)
3✔
139
        if err != nil {
3✔
UNCOV
140
                return err
×
UNCOV
141
        }
×
142

143
        return WriteBytes(w, g.ExtraData)
3✔
144
}
145

146
// MsgType returns the integer uniquely identifying this message type on the
147
// wire.
148
//
149
// This is part of the lnwire.Message interface.
150
func (g *GossipTimestampRange) MsgType() MessageType {
3✔
151
        return MsgGossipTimestampRange
3✔
152
}
3✔
153

154
// SerializedSize returns the serialized size of the message in bytes.
155
//
156
// This is part of the lnwire.SizeableMessage interface.
NEW
157
func (g *GossipTimestampRange) SerializedSize() (uint32, error) {
×
NEW
158
        return MessageSerializedSize(g)
×
NEW
159
}
×
160

161
// RandTestMessage populates the message with random data suitable for testing.
162
// It uses the rapid testing framework to generate random values.
163
//
164
// This is part of the TestMessage interface.
NEW
165
func (g *GossipTimestampRange) RandTestMessage(t *rapid.T) Message {
×
NEW
166
        var chainHash chainhash.Hash
×
NEW
167
        hashBytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "chainHash")
×
NEW
168
        copy(chainHash[:], hashBytes)
×
NEW
169

×
NEW
170
        msg := &GossipTimestampRange{
×
NEW
171
                ChainHash:      chainHash,
×
NEW
172
                FirstTimestamp: rapid.Uint32().Draw(t, "firstTimestamp"),
×
NEW
173
                TimestampRange: rapid.Uint32().Draw(t, "timestampRange"),
×
NEW
174
                ExtraData:      RandExtraOpaqueData(t, nil),
×
NEW
175
        }
×
NEW
176

×
NEW
177
        includeFirstBlockHeight := rapid.Bool().Draw(
×
NEW
178
                t, "includeFirstBlockHeight",
×
NEW
179
        )
×
NEW
180
        includeBlockRange := rapid.Bool().Draw(t, "includeBlockRange")
×
NEW
181

×
NEW
182
        if includeFirstBlockHeight {
×
NEW
183
                height := rapid.Uint32().Draw(t, "firstBlockHeight")
×
NEW
184
                msg.FirstBlockHeight = tlv.SomeRecordT(
×
NEW
185
                        tlv.RecordT[tlv.TlvType2, uint32]{Val: height},
×
NEW
186
                )
×
NEW
187
        }
×
188

NEW
189
        if includeBlockRange {
×
NEW
190
                blockRange := rapid.Uint32().Draw(t, "blockRange")
×
NEW
191
                msg.BlockRange = tlv.SomeRecordT(
×
NEW
192
                        tlv.RecordT[tlv.TlvType4, uint32]{Val: blockRange},
×
NEW
193
                )
×
NEW
194
        }
×
195

NEW
196
        return msg
×
197
}
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