• 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

42.65
/lnwire/error.go
1
package lnwire
2

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

8
        "pgregory.net/rapid"
9
)
10

11
// FundingError represents a set of errors that can be encountered and sent
12
// during the funding workflow.
13
type FundingError uint8
14

15
const (
16
        // ErrMaxPendingChannels is returned by remote peer when the number of
17
        // active pending channels exceeds their maximum policy limit.
18
        ErrMaxPendingChannels FundingError = 1
19

20
        // ErrChanTooLarge is returned by a remote peer that receives a
21
        // FundingOpen request for a channel that is above their current
22
        // soft-limit.
23
        ErrChanTooLarge FundingError = 2
24
)
25

26
// String returns a human readable version of the target FundingError.
27
func (e FundingError) String() string {
3✔
28
        switch e {
3✔
29
        case ErrMaxPendingChannels:
3✔
30
                return "Number of pending channels exceed maximum"
3✔
31
        case ErrChanTooLarge:
×
32
                return "channel too large"
×
33
        default:
×
34
                return "unknown error"
×
35
        }
36
}
37

38
// Error returns the human readable version of the target FundingError.
39
//
40
// NOTE: Satisfies the Error interface.
41
func (e FundingError) Error() string {
3✔
42
        return e.String()
3✔
43
}
3✔
44

45
// ErrorData is a set of bytes associated with a particular sent error. A
46
// receiving node SHOULD only print out data verbatim if the string is composed
47
// solely of printable ASCII characters. For reference, the printable character
48
// set includes byte values 32 through 127 inclusive.
49
type ErrorData []byte
50

51
// Error represents a generic error bound to an exact channel. The message
52
// format is purposefully general in order to allow expression of a wide array
53
// of possible errors. Each Error message is directed at a particular open
54
// channel referenced by ChannelPoint.
55
type Error struct {
56
        // ChanID references the active channel in which the error occurred
57
        // within. If the ChanID is all zeros, then this error applies to the
58
        // entire established connection.
59
        ChanID ChannelID
60

61
        // Data is the attached error data that describes the exact failure
62
        // which caused the error message to be sent.
63
        Data ErrorData
64
}
65

66
// NewError creates a new Error message.
67
func NewError() *Error {
×
68
        return &Error{}
×
69
}
×
70

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

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

79
// A compile time check to ensure Error implements the lnwire.TestMessage
80
// interface.
81
var _ TestMessage = (*Error)(nil)
82

83
// Error returns the string representation to Error.
84
//
85
// NOTE: Satisfies the error interface.
86
func (c *Error) Error() string {
3✔
87
        errMsg := "non-ascii data"
3✔
88
        if isASCII(c.Data) {
6✔
89
                errMsg = string(c.Data)
3✔
90
        }
3✔
91

92
        return fmt.Sprintf("chan_id=%v, err=%v", c.ChanID, errMsg)
3✔
93
}
94

95
// Decode deserializes a serialized Error message stored in the passed
96
// io.Reader observing the specified protocol version.
97
//
98
// This is part of the lnwire.Message interface.
99
func (c *Error) Decode(r io.Reader, pver uint32) error {
3✔
100
        return ReadElements(r,
3✔
101
                &c.ChanID,
3✔
102
                &c.Data,
3✔
103
        )
3✔
104
}
3✔
105

106
// Encode serializes the target Error into the passed io.Writer observing the
107
// protocol version specified.
108
//
109
// This is part of the lnwire.Message interface.
110
func (c *Error) Encode(w *bytes.Buffer, pver uint32) error {
3✔
111
        if err := WriteBytes(w, c.ChanID[:]); err != nil {
3✔
112
                return err
×
113
        }
×
114

115
        return WriteErrorData(w, c.Data)
3✔
116
}
117

118
// MsgType returns the integer uniquely identifying an Error message on the
119
// wire.
120
//
121
// This is part of the lnwire.Message interface.
122
func (c *Error) MsgType() MessageType {
3✔
123
        return MsgError
3✔
124
}
3✔
125

126
// SerializedSize returns the serialized size of the message in bytes.
127
//
128
// This is part of the lnwire.SizeableMessage interface.
NEW
129
func (c *Error) SerializedSize() (uint32, error) {
×
NEW
130
        return MessageSerializedSize(c)
×
NEW
131
}
×
132

133
// isASCII is a helper method that checks whether all bytes in `data` would be
134
// printable ASCII characters if interpreted as a string.
135
func isASCII(data []byte) bool {
3✔
136
        for _, c := range data {
6✔
137
                if c < 32 || c > 126 {
3✔
138
                        return false
×
139
                }
×
140
        }
141
        return true
3✔
142
}
143

144
// RandTestMessage populates the message with random data suitable for testing.
145
// It uses the rapid testing framework to generate random values.
146
//
147
// This is part of the TestMessage interface.
NEW
148
func (c *Error) RandTestMessage(t *rapid.T) Message {
×
NEW
149
        msg := &Error{
×
NEW
150
                ChanID: RandChannelID(t),
×
NEW
151
        }
×
NEW
152

×
NEW
153
        useASCII := rapid.Bool().Draw(t, "useASCII")
×
NEW
154
        if useASCII {
×
NEW
155
                length := rapid.IntRange(1, 100).Draw(t, "errorDataLength")
×
NEW
156
                data := make([]byte, length)
×
NEW
157
                for i := 0; i < length; i++ {
×
NEW
158
                        data[i] = byte(
×
NEW
159
                                rapid.IntRange(32, 126).Draw(
×
NEW
160
                                        t, fmt.Sprintf("errorDataByte-%d", i),
×
NEW
161
                                ),
×
NEW
162
                        )
×
NEW
163
                }
×
NEW
164
                msg.Data = data
×
NEW
165
        } else {
×
NEW
166
                // Generate random binary data
×
NEW
167
                length := rapid.IntRange(1, 100).Draw(t, "errorDataLength")
×
NEW
168
                msg.Data = rapid.SliceOfN(
×
NEW
169
                        rapid.Byte(), length, length,
×
NEW
170
                ).Draw(t, "errorData")
×
NEW
171
        }
×
172

NEW
173
        return msg
×
174
}
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