• 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

63.64
/lnwire/custom.go
1
package lnwire
2

3
import (
4
        "bytes"
5
        "fmt"
6
        "io"
7
        "sync"
8

9
        "pgregory.net/rapid"
10
)
11

12
// CustomTypeStart is the start of the custom type range for peer messages as
13
// defined in BOLT 01.
14
const CustomTypeStart MessageType = 32768
15

16
var (
17
        // customTypeOverride contains a set of message types < CustomTypeStart
18
        // that lnd allows to be treated as custom messages. This allows us to
19
        // override messages reserved for the protocol level and treat them as
20
        // custom messages. This set of message types is stored as a global so
21
        // that we do not need to pass around state when accounting for this
22
        // set of messages in message creation.
23
        //
24
        // Note: This global is protected by the customTypeOverride mutex.
25
        customTypeOverride map[MessageType]struct{}
26

27
        // customTypeOverrideMtx manages concurrent access to
28
        // customTypeOverride.
29
        customTypeOverrideMtx sync.RWMutex
30
)
31

32
// SetCustomOverrides validates that the set of override types are outside of
33
// the custom message range (there's no reason to override messages that are
34
// already within the range), and updates the customTypeOverride global to hold
35
// this set of message types. Note that this function will completely overwrite
36
// the set of overrides, so should be called with the full set of types.
37
func SetCustomOverrides(overrideTypes []uint16) error {
3✔
38
        customTypeOverrideMtx.Lock()
3✔
39
        defer customTypeOverrideMtx.Unlock()
3✔
40

3✔
41
        customTypeOverride = make(map[MessageType]struct{}, len(overrideTypes))
3✔
42

3✔
43
        for _, t := range overrideTypes {
6✔
44
                msgType := MessageType(t)
3✔
45

3✔
46
                if msgType >= CustomTypeStart {
3✔
47
                        return fmt.Errorf("can't override type: %v, already "+
×
48
                                "in custom range", t)
×
49
                }
×
50

51
                customTypeOverride[msgType] = struct{}{}
3✔
52
        }
53

54
        return nil
3✔
55
}
56

57
// IsCustomOverride returns a bool indicating whether the message type is one
58
// of the protocol messages that we override for custom use.
59
func IsCustomOverride(t MessageType) bool {
3✔
60
        customTypeOverrideMtx.RLock()
3✔
61
        defer customTypeOverrideMtx.RUnlock()
3✔
62

3✔
63
        _, ok := customTypeOverride[t]
3✔
64

3✔
65
        return ok
3✔
66
}
3✔
67

68
// Custom represents an application-defined wire message.
69
type Custom struct {
70
        Type MessageType
71
        Data []byte
72
}
73

74
// A compile time check to ensure Custom implements the lnwire.Message
75
// interface.
76
var _ Message = (*Custom)(nil)
77

78
// A compile time check to ensure Custom implements the lnwire.SizeableMessage
79
// interface.
80
var _ SizeableMessage = (*Custom)(nil)
81

82
// A compile time check to ensure Custom implements the lnwire.TestMessage
83
// interface.
84
var _ TestMessage = (*Custom)(nil)
85

86
// NewCustom instantiates a new custom message.
87
func NewCustom(msgType MessageType, data []byte) (*Custom, error) {
3✔
88
        if msgType < CustomTypeStart && !IsCustomOverride(msgType) {
6✔
89
                return nil, fmt.Errorf("msg type: %d not in custom range: %v "+
3✔
90
                        "and not overridden", msgType, CustomTypeStart)
3✔
91
        }
3✔
92

93
        return &Custom{
3✔
94
                Type: msgType,
3✔
95
                Data: data,
3✔
96
        }, nil
3✔
97
}
98

99
// Encode serializes the target Custom message into the passed io.Writer
100
// implementation.
101
//
102
// This is part of the lnwire.Message interface.
103
func (c *Custom) Encode(b *bytes.Buffer, pver uint32) error {
3✔
104
        _, err := b.Write(c.Data)
3✔
105
        return err
3✔
106
}
3✔
107

108
// Decode deserializes the serialized Custom message stored in the passed
109
// io.Reader into the target Custom message.
110
//
111
// This is part of the lnwire.Message interface.
112
func (c *Custom) Decode(r io.Reader, pver uint32) error {
3✔
113
        var b bytes.Buffer
3✔
114
        if _, err := io.Copy(&b, r); err != nil {
3✔
115
                return err
×
116
        }
×
117

118
        c.Data = b.Bytes()
3✔
119

3✔
120
        return nil
3✔
121
}
122

123
// MsgType returns the uint32 code which uniquely identifies this message as a
124
// Custom message on the wire.
125
//
126
// This is part of the lnwire.Message interface.
127
func (c *Custom) MsgType() MessageType {
3✔
128
        return c.Type
3✔
129
}
3✔
130

131
// SerializedSize returns the serialized size of the message in bytes.
132
//
133
// This is part of the lnwire.SizeableMessage interface.
NEW
134
func (c *Custom) SerializedSize() (uint32, error) {
×
NEW
135
        return MessageSerializedSize(c)
×
NEW
136
}
×
137

138
// RandTestMessage populates the message with random data suitable for testing.
139
// It uses the rapid testing framework to generate random values.
140
//
141
// This is part of the TestMessage interface.
NEW
142
func (c *Custom) RandTestMessage(t *rapid.T) Message {
×
NEW
143
        msgType := MessageType(
×
NEW
144
                rapid.IntRange(int(CustomTypeStart), 65535).Draw(
×
NEW
145
                        t, "customMsgType",
×
NEW
146
                ),
×
NEW
147
        )
×
NEW
148

×
NEW
149
        dataLen := rapid.IntRange(0, 1000).Draw(t, "customDataLength")
×
NEW
150
        data := rapid.SliceOfN(rapid.Byte(), dataLen, dataLen).Draw(
×
NEW
151
                t, "customData",
×
NEW
152
        )
×
NEW
153

×
NEW
154
        msg, err := NewCustom(msgType, data)
×
NEW
155
        if err != nil {
×
NEW
156
                panic(fmt.Sprintf("Error creating custom message: %v", err))
×
157
        }
158

NEW
159
        return msg
×
160
}
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