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

lightningnetwork / lnd / 15561477203

10 Jun 2025 01:54PM UTC coverage: 58.351% (-10.1%) from 68.487%
15561477203

Pull #9356

github

web-flow
Merge 6440b25db into c6d6d4c0b
Pull Request #9356: lnrpc: add incoming/outgoing channel ids filter to forwarding history request

33 of 36 new or added lines in 2 files covered. (91.67%)

28366 existing lines in 455 files now uncovered.

97715 of 167461 relevant lines covered (58.35%)

1.81 hits per line

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

84.0
/lnwire/custom.go
1
package lnwire
2

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

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

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

25
        // customTypeOverrideMtx manages concurrent access to
26
        // customTypeOverride.
27
        customTypeOverrideMtx sync.RWMutex
28
)
29

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

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

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

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

49
                customTypeOverride[msgType] = struct{}{}
3✔
50
        }
51

52
        return nil
3✔
53
}
54

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

3✔
61
        _, ok := customTypeOverride[t]
3✔
62

3✔
63
        return ok
3✔
64
}
3✔
65

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

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

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

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

87
        return &Custom{
3✔
88
                Type: msgType,
3✔
89
                Data: data,
3✔
90
        }, nil
3✔
91
}
92

93
// Encode serializes the target Custom message into the passed io.Writer
94
// implementation.
95
//
96
// This is part of the lnwire.Message interface.
97
func (c *Custom) Encode(b *bytes.Buffer, pver uint32) error {
3✔
98
        _, err := b.Write(c.Data)
3✔
99
        return err
3✔
100
}
3✔
101

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

112
        c.Data = b.Bytes()
3✔
113

3✔
114
        return nil
3✔
115
}
116

117
// MsgType returns the uint32 code which uniquely identifies this message as a
118
// Custom message on the wire.
119
//
120
// This is part of the lnwire.Message interface.
121
func (c *Custom) MsgType() MessageType {
3✔
122
        return c.Type
3✔
123
}
3✔
124

125
// SerializedSize returns the serialized size of the message in bytes.
126
//
127
// This is part of the lnwire.SizeableMessage interface.
UNCOV
128
func (c *Custom) SerializedSize() (uint32, error) {
×
UNCOV
129
        return MessageSerializedSize(c)
×
UNCOV
130
}
×
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